summary refs log tree commit diff
path: root/src/linux.rs
diff options
context:
space:
mode:
authorAlyssa Ross <hi@alyssa.is>2020-08-11 11:38:24 +0000
committerAlyssa Ross <hi@alyssa.is>2021-05-11 13:08:33 +0000
commitba774ec6776223033e2756a82a16c7ca43bcdb5f (patch)
tree79db1bdfcbbebc82de4799747af13ca72145be64 /src/linux.rs
parent1952e3cfa1861c5671757f505ee81c83bc821ab9 (diff)
downloadcrosvm-vhost-user.tar
crosvm-vhost-user.tar.gz
crosvm-vhost-user.tar.bz2
crosvm-vhost-user.tar.lz
crosvm-vhost-user.tar.xz
crosvm-vhost-user.tar.zst
crosvm-vhost-user.zip
crosvm: use vhost-user-net instead of virtio-net vhost-user
This is a hack that just assumes vhost-user-net is desired if a MAC
address is given, but not a host IP or netmask.  No sandbox is created
for this device.

It'll do for now.
Diffstat (limited to 'src/linux.rs')
-rw-r--r--src/linux.rs32
1 files changed, 28 insertions, 4 deletions
diff --git a/src/linux.rs b/src/linux.rs
index 31b9d32..3b040a8 100644
--- a/src/linux.rs
+++ b/src/linux.rs
@@ -507,6 +507,28 @@ fn create_balloon_device(cfg: &Config, socket: BalloonControlResponseSocket) ->
     })
 }
 
+fn create_vhost_user_net_device(mac_address: MacAddress) -> DeviceResult {
+    use virtio::vhost_user::cloud_hypervisor::net_util::MacAddr;
+    use virtio::vhost_user::{self, VhostUserConfig};
+
+    let xdg_runtime_dir = std::env::var("XDG_RUNTIME_DIR").expect("XDG_RUNTIME_DIR is not set");
+
+    let dev = vhost_user::Net::new(
+        MacAddr::from_bytes(&mac_address.octets()).unwrap(),
+        VhostUserConfig {
+            sock: &format!("{}/vhost-user0.sock", xdg_runtime_dir),
+            num_queues: 2,
+            queue_size: 256,
+        },
+    )
+    .expect("vhost_user::Net::new");
+
+    Ok(VirtioDeviceStub {
+        dev: Box::new(dev),
+        jail: None,
+    })
+}
+
 fn create_tap_net_device(cfg: &Config, tap_fd: RawFd) -> DeviceResult {
     // Safe because we ensure that we get a unique handle to the fd.
     let tap = unsafe {
@@ -872,10 +894,12 @@ fn create_virtio_devices(
         devs.push(create_tap_net_device(cfg, *tap_fd)?);
     }
 
-    if let (Some(host_ip), Some(netmask), Some(mac_address)) =
-        (cfg.host_ip, cfg.netmask, cfg.mac_address)
-    {
-        devs.push(create_net_device(cfg, host_ip, netmask, mac_address, mem)?);
+    if let Some(mac_address) = cfg.mac_address {
+        if let (Some(host_ip), Some(netmask)) = (cfg.host_ip, cfg.netmask) {
+            devs.push(create_net_device(cfg, host_ip, netmask, mac_address, mem)?);
+        } else {
+            devs.push(create_vhost_user_net_device(mac_address)?);
+        }
     }
 
     #[cfg_attr(not(feature = "gpu"), allow(unused_mut))]