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 <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2020-12-16 16:44:38 +00:00 committed by Samuel Ortiz
parent 21dcac7721
commit 16c2eebfd1
2 changed files with 5 additions and 5 deletions

View File

@ -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)

View File

@ -83,7 +83,7 @@ fn build_terminated_if_name(if_name: &str) -> Result<Vec<u8>> {
}
impl Tap {
pub fn open_named(if_name: &str, num_queue_pairs: usize) -> Result<Tap> {
pub fn open_named(if_name: &str, num_queue_pairs: usize, flags: Option<i32>) -> Result<Tap> {
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<Tap> {
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.