patches and low-level development discussion
 help / color / mirror / code / Atom feed
From: "Cole Helbling" <cole.e.helbling@outlook.com>
To: "Alyssa Ross" <hi@alyssa.is>, <devel@spectrum-os.org>
Cc: Cole Helbling <cole.e.helbling@outlook.com>
Subject: Re: [PATCH crosvm v2] crosvm: support setting guest MAC from tap-fd
Date: Mon, 17 May 2021 12:20:40 -0700	[thread overview]
Message-ID: <SJ0PR03MB5581D43B6795522391EE6CE4B32D9@SJ0PR03MB5581.namprd03.prod.outlook.com> (raw)
In-Reply-To: <20210517185700.3591932-1-hi@alyssa.is>

On Mon May 17, 2021 at 11:57 AM PDT, Alyssa Ross wrote:
> This adds a mac= option to crosvm's --tap-fd option.  The virtio-net
> driver in the guest will read the desired MAC from virtio
> configuration space.
>
> See the documentation for VIRTIO_NET_F_MAC in the Virtio spec[1].
>
> [1]: https://docs.oasis-open.org/virtio/virtio/v1.1/virtio-v1.1.html
>
> Thanks-to: Puck Meerburg <puck@puckipedia.com>
> ---
>  devices/src/virtio/net.rs | 12 ++++++--
>  src/crosvm.rs             |  8 +++--
>  src/linux.rs              | 12 ++++----
>  src/main.rs               | 63 ++++++++++++++++++++++++++++++---------
>  4 files changed, 71 insertions(+), 24 deletions(-)

[snip]

> diff --git a/src/main.rs b/src/main.rs
> index 5d02af02f..f8bc0d14e 100644
> --- a/src/main.rs
> +++ b/src/main.rs
> @@ -28,7 +28,7 @@ use base::{
>  };
>  use crosvm::{
>      argument::{self, print_help, set_arguments, Argument},
> -    platform, BindMount, Config, DiskOption, Executable, GidMap, SharedDir, TouchDeviceOption,
> +    platform, BindMount, Config, DiskOption, Executable, GidMap, SharedDir, TapFdOptions, TouchDeviceOption,
>      DISK_ID_LEN,
>  };
>  #[cfg(feature = "gpu")]
> @@ -1319,17 +1319,52 @@ fn set_argument(cfg: &mut Config, name: &str, value: Option<&str>) -> argument::
>          }
>          "vhost-net" => cfg.vhost_net = true,
>          "tap-fd" => {
> -            cfg.tap_fd.push(
> -                value
> -                    .unwrap()
> -                    .parse()
> -                    .map_err(|_| argument::Error::InvalidValue {
> -                        value: value.unwrap().to_owned(),
> -                        expected: String::from(
> -                            "this value for `tap-fd` must be an unsigned integer",
> -                        ),
> -                    })?,
> -            );
> +            let mut components = value.unwrap().split(',');
> +
> +            let fd: RawDescriptor = components
> +                .next()
> +                .and_then(|x| x.parse().ok())
> +                .ok_or_else(|| argument::Error::InvalidValue {
> +                    value: value.unwrap().to_owned(),
> +                    expected: String::from("this value for `tap-fd` must be an unsigned integer"),
> +                })?;
> +
> +            let mut mac = None;
> +            for c in components {
> +                let mut kv = c.splitn(2, '=');
> +                let (kind, value) = match (kv.next(), kv.next()) {
> +                    (Some(kind), Some(value)) => (kind, value),
> +                    _ => {
> +                        return Err(argument::Error::InvalidValue {
> +                            value: c.to_owned(),
> +                            expected: String::from("option must be of the form `kind=value`"),
> +                        })
> +                    }
> +                };
> +                match kind {
> +                    "mac" => {
> +                        mac = Some(value.parse().map_err(|_| argument::Error::InvalidValue {
> +                            value: value.to_owned(),
> +                            expected: String::from(
> +                                "`mac` needs to be in the form \"XX:XX:XX:XX:XX:XX\"",
> +                            ),
> +                        })?)
> +                    }
> +                    _ => {
> +                        return Err(argument::Error::InvalidValue {
> +                            value: kind.to_owned(),
> +                            expected: String::from("unrecognized option"),
> +                        })
> +                    }
> +                }
> +            }
> +            if cfg.tap_fd.contains_key(&fd) {
> +                return Err(argument::Error::TooManyArguments(format!(

Is there a better Error variant for this? `TooManyArguments` seems not-
completely-accurate when specifying an already-in-use FD.

> +                    "TAP FD already used: '{}'",
> +                    name
> +                )));
> +            }
> +            cfg.tap_fd.insert(fd, TapFdOptions { mac });
>          }
>          #[cfg(feature = "gpu")]
>          "gpu" => {
> @@ -1644,8 +1679,8 @@ writeback=BOOL - Indicates whether the VM can use writeback caching (default: fa
>            Argument::value("plugin-gid-map-file", "PATH", "Path to the file listing supplemental GIDs that should be mapped in plugin jail.  Can be given more than once."),
>            Argument::flag("vhost-net", "Use vhost for networking."),
>            Argument::value("tap-fd",
> -                          "fd",
> -                          "File descriptor for configured tap device. A different virtual network card will be added each time this argument is given."),
> +                          "FD[,mac=MAC]",
> +                          "File descriptor for configured tap device. A different virtual network card will be added each time this argument is given. MAC is the MAC address that will be set in the guest."),
>            #[cfg(feature = "gpu")]
>            Argument::flag_or_value("gpu",
>                                    "[width=INT,height=INT]",
>
> base-commit: f35d2c43ff19520855cffee761dc8899c5a439a1
> -- 
> 2.31.1

Else, LGTM.

Reviewed-by: Cole Helbling <cole.e.helbling@outlook.com>

  reply	other threads:[~2021-05-17 19:23 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-17 18:57 Alyssa Ross
2021-05-17 19:20 ` Cole Helbling [this message]
2021-05-18  8:36   ` Alyssa Ross
2021-05-18 15:43     ` Cole Helbling
2021-05-29 14:08       ` Alyssa Ross
2021-05-30  2:20         ` Cole Helbling

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=SJ0PR03MB5581D43B6795522391EE6CE4B32D9@SJ0PR03MB5581.namprd03.prod.outlook.com \
    --to=cole.e.helbling@outlook.com \
    --cc=devel@spectrum-os.org \
    --cc=hi@alyssa.is \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).