From 16c2eebfd1e03fefd2a446a5db8f33362bab32c1 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Wed, 16 Dec 2020 16:44:38 +0000 Subject: [PATCH] net_util: Extend Tap::open_named() to take flags This allows us to open the named TAP device without O_CLOEXEC for an integration test. Signed-off-by: Rob Bradford --- net_util/src/open_tap.rs | 4 ++-- net_util/src/tap.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/net_util/src/open_tap.rs b/net_util/src/open_tap.rs index 4ff4d17c7..95f10ee23 100644 --- a/net_util/src/open_tap.rs +++ b/net_util/src/open_tap.rs @@ -82,7 +82,7 @@ pub fn open_tap( let tap: Tap; if i == 0 { tap = match if_name { - Some(name) => Tap::open_named(name, num_rx_q).map_err(Error::TapOpen)?, + Some(name) => Tap::open_named(name, num_rx_q, None).map_err(Error::TapOpen)?, None => Tap::new(num_rx_q).map_err(Error::TapOpen)?, }; if let Some(ip) = ip_addr { @@ -104,7 +104,7 @@ pub fn open_tap( ifname = String::from_utf8(tap.get_if_name()).unwrap(); } else { - tap = Tap::open_named(ifname.as_str(), num_rx_q).map_err(Error::TapOpen)?; + tap = Tap::open_named(ifname.as_str(), num_rx_q, None).map_err(Error::TapOpen)?; tap.set_offload(flag).map_err(Error::TapSetOffload)?; tap.set_vnet_hdr_size(vnet_hdr_size) diff --git a/net_util/src/tap.rs b/net_util/src/tap.rs index 6ea6237ae..892196030 100644 --- a/net_util/src/tap.rs +++ b/net_util/src/tap.rs @@ -83,7 +83,7 @@ fn build_terminated_if_name(if_name: &str) -> Result> { } impl Tap { - pub fn open_named(if_name: &str, num_queue_pairs: usize) -> Result { + pub fn open_named(if_name: &str, num_queue_pairs: usize, flags: Option) -> Result { let terminated_if_name = build_terminated_if_name(if_name)?; let fd = unsafe { @@ -91,7 +91,7 @@ impl Tap { // string and verify the result. libc::open( b"/dev/net/tun\0".as_ptr() as *const c_char, - libc::O_RDWR | libc::O_NONBLOCK | libc::O_CLOEXEC, + flags.unwrap_or(libc::O_RDWR | libc::O_NONBLOCK | libc::O_CLOEXEC), ) }; if fd < 0 { @@ -150,7 +150,7 @@ impl Tap { /// Create a new tap interface. pub fn new(num_queue_pairs: usize) -> Result { - Self::open_named("vmtap%d", num_queue_pairs) + Self::open_named("vmtap%d", num_queue_pairs, None) } /// Set the host-side IP address for the tap interface.