[PATCH 3/3] virtio-net: add mtu-from-tap property for automatic MTU detection
Nariman Sayed <[email protected]>
| Newsgroups | org.nongnu.qemu-devel |
|---|---|
| Message-ID | <[email protected]> |
Currently, exposing the host tap device's MTU to the guest via VIRTIO_NET_F_MTU requires manually specifying host_mtu= on the virtio-net-pci device, duplicating the MTU already configured on the host's tap interface. Add a new mtu-from-tap boolean property (default off, preserving existing behavior). When enabled and the device's netdev peer is a tap client, the host tap's MTU is queried via tap_get_mtu() and used to set net_conf.mtu and enable VIRTIO_NET_F_MTU. This detection must happen in virtio_net_device_realize(), before virtio_net_set_config_size() computes the virtio config space size from host_features. Performing the detection later (e.g. in the get_features_ex() callback, which runs after the NIC and its config space are already sized) results in the mtu config field falling outside the sized config space, and the guest reading back an unmapped garbage value (0xffff) instead of the intended MTU. Since qemu_new_nic() has not yet been called at this point in realize(), the tap peer is accessed via n->nic_conf.peers.ncs[0] (populated during property parsing, before realize() runs, via the netdev= property) rather than through n->nic. Signed-off-by: Nariman Sayed <[email protected]> --- hw/net/virtio-net.c | 12 ++++++++++++ include/hw/virtio/virtio-net.h | 1 + 2 files changed, 13 insertions(+) diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c index 814b99a43d..6faeca87d4 100644 --- a/hw/net/virtio-net.c +++ b/hw/net/virtio-net.c @@ -3901,6 +3901,17 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp) n->host_features |= (1ULL << VIRTIO_NET_F_MTU); } + if (n->net_conf.mtu_from_tap) { + NetClientState *tap_peer = n->nic_conf.peers.ncs[0]; + if (tap_peer && tap_peer->info->type == NET_CLIENT_DRIVER_TAP) { + int mtu = tap_get_mtu(tap_peer); + if (mtu > 0) { + n->net_conf.mtu = mtu; + n->host_features |= (1ULL << VIRTIO_NET_F_MTU); + } + } + } + if (n->net_conf.duplex_str) { if (strncmp(n->net_conf.duplex_str, "half", 5) == 0) { n->net_conf.duplex = DUPLEX_HALF; @@ -4275,6 +4286,7 @@ static const Property virtio_net_properties[] = { DEFINE_PROP_UINT16("tx_queue_size", VirtIONet, net_conf.tx_queue_size, VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE), DEFINE_PROP_UINT16("host_mtu", VirtIONet, net_conf.mtu, 0), + DEFINE_PROP_BOOL("mtu-from-tap", VirtIONet, net_conf.mtu_from_tap, false), DEFINE_PROP_INT32("speed", VirtIONet, net_conf.speed, SPEED_UNKNOWN), DEFINE_PROP_STRING("duplex", VirtIONet, net_conf.duplex_str), DEFINE_PROP_BOOL("failover", VirtIONet, failover, false), diff --git a/include/hw/virtio/virtio-net.h b/include/hw/virtio/virtio-net.h index 371e376428..11044b6d27 100644 --- a/include/hw/virtio/virtio-net.h +++ b/include/hw/virtio/virtio-net.h @@ -52,6 +52,7 @@ typedef struct virtio_net_conf uint16_t rx_queue_size; uint16_t tx_queue_size; uint16_t mtu; + bool mtu_from_tap; int32_t speed; char *duplex_str; uint8_t duplex; -- 2.34.1