summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorStephen Barber <smbarber@chromium.org>2018-02-13 22:47:07 -0800
committerchrome-bot <chrome-bot@chromium.org>2018-02-21 01:06:42 -0800
commit308ff60601994ece51e94c1afa3b0e4d0beaea33 (patch)
tree917593cc724d9cdd42563fd2bd8b003bb1ee2089 /src
parent8f002f5c4a4c294b8838560948649b655dd3d772 (diff)
downloadcrosvm-308ff60601994ece51e94c1afa3b0e4d0beaea33.tar
crosvm-308ff60601994ece51e94c1afa3b0e4d0beaea33.tar.gz
crosvm-308ff60601994ece51e94c1afa3b0e4d0beaea33.tar.bz2
crosvm-308ff60601994ece51e94c1afa3b0e4d0beaea33.tar.lz
crosvm-308ff60601994ece51e94c1afa3b0e4d0beaea33.tar.xz
crosvm-308ff60601994ece51e94c1afa3b0e4d0beaea33.tar.zst
crosvm-308ff60601994ece51e94c1afa3b0e4d0beaea33.zip
net_util: add tap support for mac address
Allow get/set for the host mac on the tap interface. Also add read accessors
for the host IP address and netmask, and make using IFF_VNET_HDR optional.

BUG=none
TEST=./build_test

Change-Id: I9999bf5aa8aa35b8cae702d9bc6f94602d6fe32e
Reviewed-on: https://chromium-review.googlesource.com/918406
Commit-Ready: Stephen Barber <smbarber@chromium.org>
Tested-by: Stephen Barber <smbarber@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Diffstat (limited to 'src')
-rw-r--r--src/linux.rs42
-rw-r--r--src/main.rs13
2 files changed, 34 insertions, 21 deletions
diff --git a/src/linux.rs b/src/linux.rs
index 9d215c7..c0b9094 100644
--- a/src/linux.rs
+++ b/src/linux.rs
@@ -353,30 +353,34 @@ fn setup_mmio_bus(cfg: &Config,
     // We checked above that if the IP is defined, then the netmask is, too.
     if let Some(host_ip) = cfg.host_ip {
         if let Some(netmask) = cfg.netmask {
-            let net_box: Box<devices::virtio::VirtioDevice> = if cfg.vhost_net {
-                Box::new(devices::virtio::vhost::Net::<Tap, vhost::Net<Tap>>::new(host_ip, netmask, &mem)
-                                   .map_err(|e| Error::VhostNetDeviceNew(e))?)
-            } else {
-                Box::new(devices::virtio::Net::<Tap>::new(host_ip, netmask)
-                                   .map_err(|e| Error::NetDeviceNew(e))?)
-            };
+            if let Some(mac_address) = cfg.mac_address {
+                let net_box: Box<devices::virtio::VirtioDevice> = if cfg.vhost_net {
+                    Box::new(devices::virtio::vhost::Net::<Tap, vhost::Net<Tap>>::new(host_ip,
+                                                                                      netmask,
+                                                                                      mac_address,
+                                                                                      &mem)
+                                       .map_err(|e| Error::VhostNetDeviceNew(e))?)
+                } else {
+                    Box::new(devices::virtio::Net::<Tap>::new(host_ip, netmask, mac_address)
+                                       .map_err(|e| Error::NetDeviceNew(e))?)
+                };
 
-            let jail = if cfg.multiprocess {
-                let policy_path: PathBuf = if cfg.vhost_net {
-                    cfg.seccomp_policy_dir.join("vhost_net_device.policy")
+                let jail = if cfg.multiprocess {
+                    let policy_path: PathBuf = if cfg.vhost_net {
+                        cfg.seccomp_policy_dir.join("vhost_net_device.policy")
+                    } else {
+                        cfg.seccomp_policy_dir.join("net_device.policy")
+                    };
+
+                    Some(create_base_minijail(empty_root_path, &policy_path)?)
                 } else {
-                    cfg.seccomp_policy_dir.join("net_device.policy")
+                    None
                 };
 
-                Some(create_base_minijail(empty_root_path, &policy_path)?)
+                device_manager
+                    .register_mmio(net_box, jail, cmdline)
+                    .map_err(Error::RegisterNet)?;
             }
-            else {
-                None
-            };
-
-            device_manager
-                .register_mmio(net_box, jail, cmdline)
-                .map_err(Error::RegisterNet)?;
         }
     }
 
diff --git a/src/main.rs b/src/main.rs
index bb6692d..8b0506d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -65,7 +65,7 @@ pub struct Config {
     params: Vec<String>,
     host_ip: Option<net::Ipv4Addr>,
     netmask: Option<net::Ipv4Addr>,
-    mac_address: Option<String>,
+    mac_address: Option<net_util::MacAddress>,
     vhost_net: bool,
     wayland_socket_path: Option<PathBuf>,
     socket_path: Option<PathBuf>,
@@ -239,7 +239,16 @@ fn set_argument(cfg: &mut Config, name: &str, value: Option<&str>) -> argument::
             if cfg.mac_address.is_some() {
                 return Err(argument::Error::TooManyArguments("`mac` already given".to_owned()));
             }
-            cfg.mac_address = Some(value.unwrap().to_owned());
+            cfg.mac_address =
+                Some(value
+                         .unwrap()
+                         .parse()
+                         .map_err(|_| {
+                                      argument::Error::InvalidValue {
+                                          value: value.unwrap().to_owned(),
+                                          expected: "`mac` needs to be in the form \"XX:XX:XX:XX:XX:XX\"",
+                                      }
+                                  })?)
         }
         "wayland-sock" => {
             if cfg.wayland_socket_path.is_some() {