mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2024-12-22 05:35:20 +00:00
vmm: Allow IP configuration on named TAP interfaces
This commit changes existing behavior of named TAP interfaces. When booting a VM with configuration for a named TAP interface, cloud-hypervisor will create the interface and apply a given IP configuration to that interface. If the named interface already exists on the system, the configuration is NOT overwritten. Setting the ip and netmask fields in a tap interface configuration for a named tap interface now works by handing this configuration to the virtio_devices::Net object when it is created with a name. This commit also touches net_util to make sure that the ip configuration of existing TAP interfaces is not modified with ip or netmask handed to open_tap. Signed-off-by: Markus Sütter <markus.suetter@secunet.com>
This commit is contained in:
parent
283ae7b33e
commit
0e9513f2b7
@ -72,6 +72,10 @@ pub fn open_tap(
|
|||||||
let mut taps: Vec<Tap> = Vec::new();
|
let mut taps: Vec<Tap> = Vec::new();
|
||||||
let mut ifname: String = String::new();
|
let mut ifname: String = String::new();
|
||||||
let vnet_hdr_size = vnet_hdr_len() as i32;
|
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
|
// In case the tap interface already exists, check if the number of
|
||||||
// queues is appropriate. The tap might not support multiqueue while
|
// queues is appropriate. The tap might not support multiqueue while
|
||||||
@ -87,11 +91,19 @@ pub fn open_tap(
|
|||||||
Some(name) => Tap::open_named(name, num_rx_q, flags).map_err(Error::TapOpen)?,
|
Some(name) => Tap::open_named(name, num_rx_q, flags).map_err(Error::TapOpen)?,
|
||||||
None => Tap::new(num_rx_q).map_err(Error::TapOpen)?,
|
None => Tap::new(num_rx_q).map_err(Error::TapOpen)?,
|
||||||
};
|
};
|
||||||
if let Some(ip) = ip_addr {
|
// Don't overwrite ip configuration of existing interfaces:
|
||||||
tap.set_ip_addr(ip).map_err(Error::TapSetIp)?;
|
if !tap_existed {
|
||||||
}
|
if let Some(ip) = ip_addr {
|
||||||
if let Some(mask) = netmask {
|
tap.set_ip_addr(ip).map_err(Error::TapSetIp)?;
|
||||||
tap.set_netmask(mask).map_err(Error::TapSetNetmask)?;
|
}
|
||||||
|
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 {
|
if let Some(mac) = host_mac {
|
||||||
tap.set_mac_addr(*mac).map_err(Error::TapSetMac)?
|
tap.set_mac_addr(*mac).map_err(Error::TapSetMac)?
|
||||||
|
@ -2464,8 +2464,8 @@ impl DeviceManager {
|
|||||||
virtio_devices::Net::new(
|
virtio_devices::Net::new(
|
||||||
id.clone(),
|
id.clone(),
|
||||||
Some(tap_if_name),
|
Some(tap_if_name),
|
||||||
None,
|
Some(net_cfg.ip),
|
||||||
None,
|
Some(net_cfg.mask),
|
||||||
Some(net_cfg.mac),
|
Some(net_cfg.mac),
|
||||||
&mut net_cfg.host_mac,
|
&mut net_cfg.host_mac,
|
||||||
net_cfg.mtu,
|
net_cfg.mtu,
|
||||||
|
Loading…
Reference in New Issue
Block a user