summary refs log tree commit diff
path: root/crosvm_plugin
diff options
context:
space:
mode:
authorStephen Barber <smbarber@chromium.org>2018-02-09 15:16:01 -0800
committerchrome-bot <chrome-bot@chromium.org>2018-03-08 00:37:50 -0800
commita10b2d20a8667a4c730aacc1f4c8d18a6756eaab (patch)
treec6db02d574746f0a18ec2ee1a868dd795c85e795 /crosvm_plugin
parent8da6543e74d5e037f08ed4cc49f89ad63963c3dc (diff)
downloadcrosvm-a10b2d20a8667a4c730aacc1f4c8d18a6756eaab.tar
crosvm-a10b2d20a8667a4c730aacc1f4c8d18a6756eaab.tar.gz
crosvm-a10b2d20a8667a4c730aacc1f4c8d18a6756eaab.tar.bz2
crosvm-a10b2d20a8667a4c730aacc1f4c8d18a6756eaab.tar.lz
crosvm-a10b2d20a8667a4c730aacc1f4c8d18a6756eaab.tar.xz
crosvm-a10b2d20a8667a4c730aacc1f4c8d18a6756eaab.tar.zst
crosvm-a10b2d20a8667a4c730aacc1f4c8d18a6756eaab.zip
plugin: add crosvm_net_get_config
BUG=none
TEST=sudo cargo test --features plugin

Change-Id: Ib38fad250295d73529dff0451345b4274a261073
Reviewed-on: https://chromium-review.googlesource.com/911943
Commit-Ready: Stephen Barber <smbarber@chromium.org>
Tested-by: Stephen Barber <smbarber@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Diffstat (limited to 'crosvm_plugin')
-rw-r--r--crosvm_plugin/Cargo.toml2
-rw-r--r--crosvm_plugin/crosvm.h30
-rw-r--r--crosvm_plugin/src/lib.rs56
3 files changed, 86 insertions, 2 deletions
diff --git a/crosvm_plugin/Cargo.toml b/crosvm_plugin/Cargo.toml
index d34753a..42516ad 100644
--- a/crosvm_plugin/Cargo.toml
+++ b/crosvm_plugin/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "crosvm_plugin"
-version = "0.11.0"
+version = "0.12.0"
 authors = ["The Chromium OS Authors"]
 
 [lib]
diff --git a/crosvm_plugin/crosvm.h b/crosvm_plugin/crosvm.h
index 0dc2a60..30b32c1 100644
--- a/crosvm_plugin/crosvm.h
+++ b/crosvm_plugin/crosvm.h
@@ -47,7 +47,7 @@ extern "C" {
  * do not indicate anything about what version of crosvm is running.
  */
 #define CROSVM_API_MAJOR 0
-#define CROSVM_API_MINOR 11
+#define CROSVM_API_MINOR 12
 #define CROSVM_API_PATCH 0
 
 enum crosvm_address_space {
@@ -130,6 +130,34 @@ int crosvm_get_emulated_cpuid(struct crosvm*, uint32_t __entry_count,
                               uint32_t *__out_count);
 
 /*
+ * The network configuration for a crosvm instance.
+ */
+struct crosvm_net_config {
+  /*
+   * The tap device fd. This fd is owned by the caller, and should be closed
+   * by the caller when it is no longer in use.
+   */
+  int tap_fd;
+  /* The IPv4 address of the tap interface, in network (big-endian) format. */
+  uint32_t host_ip;
+  /* The netmask of the tap interface subnet, in network (big-endian) format. */
+  uint32_t netmask;
+  /* The mac address of the host side of the tap interface. */
+  uint8_t host_mac_address[6];
+  uint8_t _padding[2];
+};
+
+#ifdef static_assert
+static_assert(sizeof(struct crosvm_net_config) == 20,
+              "extra padding in struct crosvm_net_config");
+#endif
+
+/*
+ * Gets the network configuration.
+ */
+int crosvm_net_get_config(struct crosvm*, struct crosvm_net_config*);
+
+/*
  * Registers a range in the given address space that, when accessed, will block
  * and wait for a crosvm_vcpu_resume call.
  *
diff --git a/crosvm_plugin/src/lib.rs b/crosvm_plugin/src/lib.rs
index 34d9d91..4bb763f 100644
--- a/crosvm_plugin/src/lib.rs
+++ b/crosvm_plugin/src/lib.rs
@@ -61,6 +61,16 @@ const CROSVM_VCPU_EVENT_KIND_PAUSED: u32 = 2;
 
 #[repr(C)]
 #[derive(Copy, Clone)]
+pub struct crosvm_net_config {
+    tap_fd: c_int,
+    host_ipv4_address: u32,
+    netmask: u32,
+    host_mac_address: [u8; 6],
+    _reserved: [u8; 2],
+}
+
+#[repr(C)]
+#[derive(Copy, Clone)]
 pub struct anon_irqchip {
     irqchip: u32,
     pin: u32,
@@ -376,6 +386,38 @@ impl crosvm {
             Err(ENOENT)
         }
     }
+
+    fn get_net_config(&mut self) -> result::Result<crosvm_net_config, c_int> {
+        let mut r = MainRequest::new();
+        r.mut_get_net_config();
+
+        let (response, mut files) = self.main_transaction(&r, &[])?;
+        if !response.has_get_net_config() {
+            return Err(EPROTO);
+        }
+        let config = response.get_get_net_config();
+
+        match files.pop() {
+            Some(f) => {
+                let mut net_config = crosvm_net_config {
+                    tap_fd: f.into_raw_fd(),
+                    host_ipv4_address: config.host_ipv4_address,
+                    netmask: config.netmask,
+                    host_mac_address: [0; 6],
+                    _reserved: [0; 2],
+                };
+
+                let mac_addr = config.get_host_mac_address();
+                if mac_addr.len() != net_config.host_mac_address.len() {
+                    return Err(EPROTO)
+                }
+                net_config.host_mac_address.copy_from_slice(mac_addr);
+
+                Ok(net_config)
+            },
+            None => Err(EPROTO),
+        }
+    }
 }
 
 /// This helper macro implements the C API's constructor/destructor for a given type. Because they
@@ -918,6 +960,20 @@ fn crosvm_get_emulated_cpuid(this: *mut crosvm,
 }
 
 #[no_mangle]
+pub unsafe extern "C" fn crosvm_net_get_config(self_: *mut crosvm,
+                                               config: *mut crosvm_net_config)
+                                               -> c_int {
+    let self_ = &mut (*self_);
+    let ret = self_.get_net_config();
+
+    if let Ok(c) = ret {
+        *config = c;
+    }
+
+    to_crosvm_rc(ret)
+}
+
+#[no_mangle]
 pub unsafe extern "C" fn crosvm_reserve_range(self_: *mut crosvm,
                                               space: u32,
                                               start: u64,