patches and low-level development discussion
 help / color / mirror / Atom feed
From: Alyssa Ross <hi@alyssa.is>
To: devel@spectrum-os.org
Subject: [RFC PATCH 4/4] crosvm: use vhost-user-net instead of virtio-net
Date: Wed, 12 May 2021 17:08:12 +0000
Message-ID: <20210512170812.192540-5-hi@alyssa.is> (raw)
In-Reply-To: <20210512170812.192540-1-hi@alyssa.is>

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 a proof of concept.
---
 src/linux.rs | 32 ++++++++++++++++++++++++++++----
 src/main.rs  |  2 +-
 2 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/src/linux.rs b/src/linux.rs
index 31b9d325..3b040a87 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 a864833c..d19def4a 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(),
-- 
2.31.0


      parent reply	other threads:[~2021-05-13  0:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-12 17:08 [RFC PATCH crosvm 0/4] Implement vhost-user-net Alyssa Ross
2021-05-12 17:08 ` [RFC PATCH 1/4] devices: port vhost-user-net from cloud-hypervisor Alyssa Ross
2021-05-12 17:08 ` [RFC PATCH 2/4] vhost_rs: vhost_user: Add missing protocol features Alyssa Ross
2021-05-12 17:08 ` [RFC PATCH 3/4] devices: vhost-user: Vring should be enabled after initialization Alyssa Ross
2021-05-12 17:08 ` Alyssa Ross [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210512170812.192540-5-hi@alyssa.is \
    --to=hi@alyssa.is \
    --cc=devel@spectrum-os.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

patches and low-level development discussion

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://spectrum-os.org/lists/archives/spectrum-devel/0 spectrum-devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 spectrum-devel spectrum-devel/ https://spectrum-os.org/lists/archives/spectrum-devel \
		public-inbox+spectrum-devel@spectrum-os.org devel@spectrum-os.org
	public-inbox-index spectrum-devel

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntps://spectrum-os.org/inbox.comp.spectrum.devel
	nntp://spectrum-os.org/inbox.comp.spectrum.devel


code repositories for the project(s) associated with this inbox:

	https://spectrum-os.org/git/nixpkgs
	https://spectrum-os.org/git/ucspi-vsock
	https://spectrum-os.org/git/www

AGPL code for this site: https://ftp.qyliss.net/public-inbox/public-inbox-1.6.1-qyliss-yjdmfarc701widk5ma6pcr7jrb69jyb3.tar.gz