net: Give the user the ability to set MTU

Add a new "mtu" parameter to the NetConfig structure and therefore to
the --net option. This allows Cloud Hypervisor's users to define the
Maximum Transmission Unit (MTU) they want to use for the network
interface that they create.

In details, there are two main aspects. On the one hand, the TAP
interface is created with the proper MTU if it is provided. And on the
other hand the guest is made aware of the MTU through the VIRTIO
configuration. That means the MTU is properly set on both the TAP on the
host and the network interface in the guest.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf
2022-09-21 11:56:24 +02:00
parent d95220108a
commit 76dbf85b79
12 changed files with 131 additions and 10 deletions

View File

@@ -49,6 +49,9 @@ use vmm_sys_util::eventfd::EventFd;
// Event available on the control queue.
const CTRL_QUEUE_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 1;
// Following the VIRTIO specification, the MTU should be at least 1280.
pub const MIN_MTU: u16 = 1280;
pub struct NetCtrlEpollHandler {
pub mem: GuestMemoryAtomic<GuestMemoryMmap>,
pub kill_evt: EventFd,
@@ -428,7 +431,7 @@ impl VersionMapped for NetState {}
impl Net {
/// Create a new virtio network device with the given TAP interface.
#[allow(clippy::too_many_arguments)]
pub fn new_with_tap(
fn new_with_tap(
id: String,
taps: Vec<Tap>,
guest_mac: Option<MacAddr>,
@@ -439,6 +442,11 @@ 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);
}
let mut avail_features = 1 << VIRTIO_NET_F_CSUM
| 1 << VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
| 1 << VIRTIO_NET_F_GUEST_CSUM
@@ -453,6 +461,9 @@ impl Net {
| 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;
}
@@ -462,9 +473,9 @@ impl Net {
let mut config = VirtioNetConfig::default();
if let Some(mac) = guest_mac {
build_net_config_space(&mut config, mac, num_queues, &mut avail_features);
build_net_config_space(&mut config, mac, num_queues, mtu, &mut avail_features);
} else {
build_net_config_space_with_mq(&mut config, num_queues, &mut avail_features);
build_net_config_space_with_mq(&mut config, num_queues, mtu, &mut avail_features);
}
Ok(Net {
@@ -497,6 +508,7 @@ impl Net {
netmask: Option<Ipv4Addr>,
guest_mac: Option<MacAddr>,
host_mac: &mut Option<MacAddr>,
mtu: Option<u16>,
iommu: bool,
num_queues: usize,
queue_size: u16,
@@ -504,8 +516,16 @@ impl Net {
rate_limiter_config: Option<RateLimiterConfig>,
exit_evt: EventFd,
) -> Result<Self> {
let taps = open_tap(if_name, ip_addr, netmask, host_mac, num_queues / 2, None)
.map_err(Error::OpenTap)?;
let taps = open_tap(
if_name,
ip_addr,
netmask,
host_mac,
mtu,
num_queues / 2,
None,
)
.map_err(Error::OpenTap)?;
Self::new_with_tap(
id,

View File

@@ -24,7 +24,7 @@ use virtio_bindings::bindings::virtio_net::{
VIRTIO_NET_F_CSUM, VIRTIO_NET_F_CTRL_VQ, VIRTIO_NET_F_GUEST_CSUM, VIRTIO_NET_F_GUEST_ECN,
VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, VIRTIO_NET_F_GUEST_UFO,
VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_TSO6, VIRTIO_NET_F_HOST_UFO,
VIRTIO_NET_F_MAC, VIRTIO_NET_F_MRG_RXBUF,
VIRTIO_NET_F_MAC, VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_MTU,
};
use virtio_bindings::bindings::virtio_ring::VIRTIO_RING_F_EVENT_IDX;
use virtio_queue::{Queue, QueueT};
@@ -70,6 +70,7 @@ impl Net {
pub fn new(
id: String,
mac_addr: MacAddr,
mtu: Option<u16>,
vu_cfg: VhostUserConfig,
server: bool,
seccomp_action: SeccompAction,
@@ -126,8 +127,12 @@ impl Net {
| 1 << VIRTIO_F_VERSION_1
| VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits();
if mtu.is_some() {
avail_features |= 1u64 << VIRTIO_NET_F_MTU;
}
let mut config = VirtioNetConfig::default();
build_net_config_space(&mut config, mac_addr, num_queues, &mut avail_features);
build_net_config_space(&mut config, mac_addr, num_queues, mtu, &mut avail_features);
let mut vu =
VhostUserHandle::connect_vhost_user(server, &vu_cfg.socket, num_queues as u64, false)?;