summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/linux.rs32
-rw-r--r--src/main.rs2
2 files changed, 29 insertions, 5 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))]
diff --git a/src/main.rs b/src/main.rs
index a864833..d19def4 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -803,7 +803,7 @@ fn run_vm(args: std::env::Args) -> std::result::Result<(), ()> {
         if cfg.executable_path.is_none() {
             return Err(argument::Error::ExpectedArgument("`KERNEL`".to_owned()));
         }
-        if cfg.host_ip.is_some() || cfg.netmask.is_some() || cfg.mac_address.is_some() {
+        if cfg.host_ip.is_some() || cfg.netmask.is_some() {
             if cfg.host_ip.is_none() {
                 return Err(argument::Error::ExpectedArgument(
                     "`host_ip` missing from network config".to_owned(),