diff --git a/net_util/src/open_tap.rs b/net_util/src/open_tap.rs index dd5d498688..e8cece7606 100644 --- a/net_util/src/open_tap.rs +++ b/net_util/src/open_tap.rs @@ -72,6 +72,10 @@ pub fn open_tap( let mut taps: Vec = Vec::new(); let mut ifname: String = String::new(); let vnet_hdr_size = vnet_hdr_len() as i32; + // Check if the given interface exists before we create it. + let tap_existed = if_name.map_or(false, |n| { + Path::new(&format!("/sys/class/net/{n}")).exists() + }); // In case the tap interface already exists, check if the number of // queues is appropriate. The tap might not support multiqueue while @@ -87,11 +91,16 @@ pub fn open_tap( Some(name) => Tap::open_named(name, num_rx_q, flags).map_err(Error::TapOpen)?, None => Tap::new(num_rx_q).map_err(Error::TapOpen)?, }; - if let Some(ip) = ip_addr { - tap.set_ip_addr(ip).map_err(Error::TapSetIp)?; - } - if let Some(mask) = netmask { - tap.set_netmask(mask).map_err(Error::TapSetNetmask)?; + // Don't overwrite ip configuration of existing interfaces: + if !tap_existed { + if let Some(ip) = ip_addr { + tap.set_ip_addr(ip).map_err(Error::TapSetIp)?; + } + if let Some(mask) = netmask { + tap.set_netmask(mask).map_err(Error::TapSetNetmask)?; + } + } else { + warn!("Tap {} already exists. IP configuration will not be overwritten.", if_name.unwrap_or_default()); } if let Some(mac) = host_mac { tap.set_mac_addr(*mac).map_err(Error::TapSetMac)? diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index ed0f7d1166..883f0c0407 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -2464,8 +2464,8 @@ impl DeviceManager { virtio_devices::Net::new( id.clone(), Some(tap_if_name), - None, - None, + Some(net_cfg.ip), + Some(net_cfg.mask), Some(net_cfg.mac), &mut net_cfg.host_mac, net_cfg.mtu,