net: Don't override default TAP interface MTU

Adjust MTU logic such that:
1. Apply an MTU to the TAP interface if the user supplies it
2. Always query the TAP interface for the MTU and expose that.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2022-09-26 15:58:54 +02:00 committed by Rob Bradford
parent 44d66ec531
commit 903c08f8a1
4 changed files with 25 additions and 24 deletions

View File

@ -442,10 +442,9 @@ impl Net {
rate_limiter_config: Option<RateLimiterConfig>,
exit_evt: EventFd,
) -> Result<Self> {
let mut mtu = None;
if !taps.is_empty() {
mtu = Some(taps[0].mtu().map_err(Error::TapError)? as u16);
}
assert!(!taps.is_empty());
let mtu = taps[0].mtu().map_err(Error::TapError)? as u16;
let mut avail_features = 1 << VIRTIO_NET_F_CSUM
| 1 << VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
@ -458,12 +457,10 @@ impl Net {
| 1 << VIRTIO_NET_F_HOST_TSO4
| 1 << VIRTIO_NET_F_HOST_TSO6
| 1 << VIRTIO_NET_F_HOST_UFO
| 1 << VIRTIO_NET_F_MTU
| 1 << VIRTIO_RING_F_EVENT_IDX
| 1 << VIRTIO_F_VERSION_1;
if mtu.is_some() {
avail_features |= 1u64 << VIRTIO_NET_F_MTU;
}
if iommu {
avail_features |= 1u64 << VIRTIO_F_IOMMU_PLATFORM;
}
@ -473,9 +470,9 @@ impl Net {
let mut config = VirtioNetConfig::default();
if let Some(mac) = guest_mac {
build_net_config_space(&mut config, mac, num_queues, mtu, &mut avail_features);
build_net_config_space(&mut config, mac, num_queues, Some(mtu), &mut avail_features);
} else {
build_net_config_space_with_mq(&mut config, num_queues, mtu, &mut avail_features);
build_net_config_space_with_mq(&mut config, num_queues, Some(mtu), &mut avail_features);
}
Ok(Net {
@ -545,6 +542,7 @@ impl Net {
id: String,
fds: &[RawFd],
guest_mac: Option<MacAddr>,
mtu: Option<u16>,
iommu: bool,
queue_size: u16,
seccomp_action: SeccompAction,
@ -565,6 +563,12 @@ impl Net {
taps.push(tap);
}
assert!(!taps.is_empty());
if let Some(mtu) = mtu {
taps[0].set_mtu(mtu as i32).map_err(Error::TapError)?;
}
Self::new_with_tap(
id,
taps,

View File

@ -823,7 +823,6 @@ components:
type: string
mtu:
type: integer
default: 1280
iommu:
type: boolean
default: false

View File

@ -1247,8 +1247,8 @@ pub struct NetConfig {
pub mac: MacAddr,
#[serde(default)]
pub host_mac: Option<MacAddr>,
#[serde(default = "default_netconfig_mtu")]
pub mtu: u16,
#[serde(default)]
pub mtu: Option<u16>,
#[serde(default)]
pub iommu: bool,
#[serde(default = "default_netconfig_num_queues")]
@ -1286,10 +1286,6 @@ fn default_netconfig_mac() -> MacAddr {
MacAddr::local_random()
}
fn default_netconfig_mtu() -> u16 {
virtio_devices::net::MIN_MTU
}
fn default_netconfig_num_queues() -> usize {
DEFAULT_NUM_QUEUES_VUNET
}
@ -1306,7 +1302,7 @@ impl Default for NetConfig {
mask: default_netconfig_mask(),
mac: default_netconfig_mac(),
host_mac: None,
mtu: default_netconfig_mtu(),
mtu: None,
iommu: false,
num_queues: default_netconfig_num_queues(),
queue_size: default_netconfig_queue_size(),
@ -1372,8 +1368,7 @@ impl NetConfig {
let host_mac = parser.convert("host_mac").map_err(Error::ParseNetwork)?;
let mtu = parser
.convert("mtu")
.map_err(Error::ParseNetwork)?
.unwrap_or_else(default_netconfig_mtu);
.map_err(Error::ParseNetwork)?;
let iommu = parser
.convert::<Toggle>("iommu")
.map_err(Error::ParseNetwork)?
@ -1515,8 +1510,10 @@ impl NetConfig {
}
}
if self.mtu < virtio_devices::net::MIN_MTU {
return Err(ValidationError::InvalidMtu(self.mtu));
if let Some(mtu) = self.mtu {
if mtu < virtio_devices::net::MIN_MTU {
return Err(ValidationError::InvalidMtu(mtu));
}
}
Ok(())

View File

@ -2244,7 +2244,7 @@ impl DeviceManager {
match virtio_devices::vhost_user::Net::new(
id.clone(),
net_cfg.mac,
Some(net_cfg.mtu),
net_cfg.mtu,
vu_cfg,
server,
self.seccomp_action.clone(),
@ -2275,7 +2275,7 @@ impl DeviceManager {
None,
Some(net_cfg.mac),
&mut net_cfg.host_mac,
None,
net_cfg.mtu,
self.force_iommu | net_cfg.iommu,
net_cfg.num_queues,
net_cfg.queue_size,
@ -2293,6 +2293,7 @@ impl DeviceManager {
id.clone(),
fds,
Some(net_cfg.mac),
net_cfg.mtu,
self.force_iommu | net_cfg.iommu,
net_cfg.queue_size,
self.seccomp_action.clone(),
@ -2312,7 +2313,7 @@ impl DeviceManager {
Some(net_cfg.mask),
Some(net_cfg.mac),
&mut net_cfg.host_mac,
Some(net_cfg.mtu),
net_cfg.mtu,
self.force_iommu | net_cfg.iommu,
net_cfg.num_queues,
net_cfg.queue_size,