summary refs log tree commit diff
path: root/src/linux.rs
diff options
context:
space:
mode:
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))]