Skip to contentSkip to conversation

virtio-net: Add support to MTU feature

RFC Superseded Maxime Coquelin 2 participants 7 messages Sep 6, 2016 – Sep 16, 2016 Networking

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover virtio-net: Add support to MTU feature

This series is based on on-going RFC from Aaron Conole which defines a way for the host to expose its max MTU to the guest [0].

Using it, the guest can size its receive buffers accordingly, and if GRO is disabled, can disable the mergeable buffers to improve performance.

In this version, the only way to pass the MTU relies on the added MTU protocol feature, but it could also be done with a new property.

Thanks,
Maxime

[0]: https://lists.oasis-open.org/archives/virtio-dev/201609/msg00021.html

Maxime Coquelin (4):
virtio-net: reuse MTU related bits from Linux header
vhost-user: Add new protocol feature MTU
vhost-net: Add new MTU feature support
virtio-net: Add MTU feature support

 hw/net/vhost_net.c                          | 11 +++++++++++
 hw/net/virtio-net.c                         | 16 ++++++++++++++++
 hw/virtio/vhost-user.c                      | 11 +++++++++++
 include/hw/virtio/vhost.h                   |  1 +
 include/hw/virtio/virtio-net.h              |  1 +
 include/net/vhost_net.h                     |  2 ++
 include/standard-headers/linux/virtio_net.h |  3 +++
 7 files changed, 45 insertions(+)
Signature · 1 line
2.7.4
#1 lore raw

RFC 1/4 virtio-net: reuse MTU related bits from Linux header

VIRTIO_NET_F_MTU has been introduced in Linux v4.8-rc1. Reuse its define and the related virtio_net_config struct update.

Cc Michael S. Tsirkin <[email protected]>
Cc Aaron Conole <[email protected]>
---
 include/standard-headers/linux/virtio_net.h | 3 +++
 1 file changed, 3 insertions(+)
include/standard-headers/linux/virtio_net.h +3 -0
index a78f33e..30ff249 100644
@@ -35,6 +35,7 @@
 #define VIRTIO_NET_F_CSUM	0	/* Host handles pkts w/ partial csum */
 #define VIRTIO_NET_F_GUEST_CSUM	1	/* Guest handles pkts w/ partial csum */
 #define VIRTIO_NET_F_CTRL_GUEST_OFFLOADS 2 /* Dynamic offload configuration. */
+#define VIRTIO_NET_F_MTU	3	/* Initial MTU advice */
 #define VIRTIO_NET_F_MAC	5	/* Host has given MAC address. */
 #define VIRTIO_NET_F_GUEST_TSO4	7	/* Guest can handle TSOv4 in. */
 #define VIRTIO_NET_F_GUEST_TSO6	8	/* Guest can handle TSOv6 in. */
@@ -73,6 +74,8 @@ struct virtio_net_config {
 	 * Legal values are between 1 and 0x8000
 	 */
 	uint16_t max_virtqueue_pairs;
+	/* Default maximum transmit unit advice */
+	uint16_t mtu;
 } QEMU_PACKED;
 
 /*
Signature · 1 line
2.7.4
#2 lore raw

RFC 2/4 vhost-user: Add new protocol feature MTU

In reply to Maxime Coquelin

This patch adds VHOST_USER_PROTOCOL_F_MTU protocol feature.

If supported, QEMU sends VHOST_USER_GET_MTU request to the client, and expects a u64 reply containing the MTU advised for the guest.

Cc Michael S. Tsirkin <[email protected]>
Cc Aaron Conole <[email protected]>
---
 hw/virtio/vhost-user.c    | 11 +++++++++++
 include/hw/virtio/vhost.h |  1 +
 2 files changed, 12 insertions(+)
hw/virtio/vhost-user.c +11 -0
index b57454a..d72dd37 100644
@@ -32,6 +32,7 @@ enum VhostUserProtocolFeature {
     VHOST_USER_PROTOCOL_F_LOG_SHMFD = 1,
     VHOST_USER_PROTOCOL_F_RARP = 2,
     VHOST_USER_PROTOCOL_F_REPLY_ACK = 3,
+    VHOST_USER_PROTOCOL_F_MTU = 4,
 
     VHOST_USER_PROTOCOL_F_MAX
 };
@@ -59,6 +60,7 @@ typedef enum VhostUserRequest {
     VHOST_USER_GET_QUEUE_NUM = 17,
     VHOST_USER_SET_VRING_ENABLE = 18,
     VHOST_USER_SEND_RARP = 19,
+    VHOST_USER_GET_MTU = 20,
     VHOST_USER_MAX
 } VhostUserRequest;
 
@@ -186,6 +188,7 @@ static bool vhost_user_one_time_request(VhostUserRequest request)
     case VHOST_USER_RESET_OWNER:
     case VHOST_USER_SET_MEM_TABLE:
     case VHOST_USER_GET_QUEUE_NUM:
+    case VHOST_USER_GET_MTU:
         return true;
     default:
         return false;
@@ -602,6 +605,14 @@ static int vhost_user_init(struct vhost_dev *dev, void *opaque)
                 return err;
             }
         }
+
+        /* query the MTU we support if backend supports MTU feature */
+        if (dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_MTU)) {
+            err = vhost_user_get_u64(dev, VHOST_USER_GET_MTU, &dev->mtu);
+            if (err < 0) {
+                return err;
+            }
+        }
     }
 
     if (dev->migration_blocker == NULL &&
include/hw/virtio/vhost.h +1 -0
index e433089..99c8d9b 100644
@@ -50,6 +50,7 @@ struct vhost_dev {
     uint64_t backend_features;
     uint64_t protocol_features;
     uint64_t max_queues;
+    uint64_t mtu;
     bool started;
     bool log_enabled;
     uint64_t log_size;
Signature · 1 line
2.7.4
#3 lore raw

RFC 3/4 vhost-net: Add new MTU feature support

In reply to Maxime Coquelin

If VHOST_USER_F_MTU feature is negociated, vhost-net makes the advised MTU available to virtio-net through a vhost_net_get_mtu() call.

Cc Michael S. Tsirkin <[email protected]>
Cc Aaron Conole <[email protected]>
---
 hw/net/vhost_net.c      | 11 +++++++++++
 include/net/vhost_net.h |  2 ++
 2 files changed, 13 insertions(+)
hw/net/vhost_net.c +11 -0
index f2d49ad..21057d6 100644
@@ -74,6 +74,7 @@ static const int user_feature_bits[] = {
     VIRTIO_NET_F_HOST_ECN,
     VIRTIO_NET_F_HOST_UFO,
     VIRTIO_NET_F_MRG_RXBUF,
+    VIRTIO_NET_F_MTU,
 
     /* This bit implies RARP isn't sent by QEMU out of band */
     VIRTIO_NET_F_GUEST_ANNOUNCE,
@@ -435,6 +436,11 @@ int vhost_set_vring_enable(NetClientState *nc, int enable)
     return 0;
 }
 
+uint64_t vhost_net_get_mtu(struct vhost_net *net)
+{
+    return net->dev.mtu;
+}
+
 #else
 uint64_t vhost_net_get_max_queues(VHostNetState *net)
 {
@@ -501,4 +507,9 @@ int vhost_set_vring_enable(NetClientState *nc, int enable)
 {
     return 0;
 }
+
+uint64_t vhost_net_get_mtu(struct vhost_net *net)
+{
+    return 0;
+}
 #endif
include/net/vhost_net.h +2 -0
index 5a08eff..37de17b 100644
@@ -35,4 +35,6 @@ int vhost_set_vring_enable(NetClientState * nc, int enable);
 
 uint64_t vhost_net_get_acked_features(VHostNetState *net);
 
+uint64_t vhost_net_get_mtu(struct vhost_net *net);
+
 #endif
Signature · 1 line
2.7.4
#4 lore raw

RFC 4/4 virtio-net: Add MTU feature support

In reply to Maxime Coquelin

If negociated, virtio-net gets the advised MTU from vhost-net, and provides it to the guest through a new virtio_net_config entry.

Cc Michael S. Tsirkin <[email protected]>
Cc Aaron Conole <[email protected]
---
 hw/net/virtio-net.c            | 16 ++++++++++++++++
 include/hw/virtio/virtio-net.h |  1 +
 2 files changed, 17 insertions(+)
hw/net/virtio-net.c +16 -0
index 01f1351..0974d87 100644
@@ -50,6 +50,8 @@ static VirtIOFeature feature_sizes[] = {
      .end = endof(struct virtio_net_config, status)},
     {.flags = 1 << VIRTIO_NET_F_MQ,
      .end = endof(struct virtio_net_config, max_virtqueue_pairs)},
+    {.flags = 1 << VIRTIO_NET_F_MTU,
+     .end = endof(struct virtio_net_config, mtu)},
     {}
 };
 
@@ -74,6 +76,10 @@ static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
     VirtIONet *n = VIRTIO_NET(vdev);
     struct virtio_net_config netcfg;
 
+    if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MTU)) {
+        virtio_stw_p(vdev, &netcfg.mtu, n->mtu);
+    }
+
     virtio_stw_p(vdev, &netcfg.status, n->status);
     virtio_stw_p(vdev, &netcfg.max_virtqueue_pairs, n->max_queues);
     memcpy(netcfg.mac, n->mac, ETH_ALEN);
@@ -526,6 +532,7 @@ static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features,
     features |= n->host_features;
 
     virtio_add_feature(&features, VIRTIO_NET_F_MAC);
+    virtio_add_feature(&features, VIRTIO_NET_F_MTU);
 
     if (!peer_has_vnet_hdr(n)) {
         virtio_clear_feature(&features, VIRTIO_NET_F_CSUM);
@@ -627,6 +634,14 @@ static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features)
     } else {
         memset(n->vlans, 0xff, MAX_VLAN >> 3);
     }
+
+    if (virtio_has_feature(features, VIRTIO_NET_F_MTU)) {
+        NetClientState *nc = qemu_get_queue(n->nic);
+
+        if (get_vhost_net(nc->peer)) {
+            n->mtu = vhost_net_get_mtu(get_vhost_net(nc->peer));
+        }
+    }
 }
 
 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
@@ -1688,6 +1703,7 @@ static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features)
 {
     int i, config_size = 0;
     virtio_add_feature(&host_features, VIRTIO_NET_F_MAC);
+    virtio_add_feature(&host_features, VIRTIO_NET_F_MTU);
     for (i = 0; feature_sizes[i].flags != 0; i++) {
         if (host_features & feature_sizes[i].flags) {
             config_size = MAX(feature_sizes[i].end, config_size);
include/hw/virtio/virtio-net.h +1 -0
index 91ed97c..13b2a8d 100644
@@ -95,6 +95,7 @@ typedef struct VirtIONet {
     QEMUTimer *announce_timer;
     int announce_counter;
     bool needs_vnet_hdr_swap;
+    uint16_t mtu;
 } VirtIONet;
 
 void virtio_net_set_netclient_name(VirtIONet *n, const char *name,
Signature · 1 line
2.7.4
#5 lore raw

Re: [Qemu-devel] [RFC 4/4] virtio-net: Add MTU feature support

hw/net/virtio-net.c static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config) · line 81

Quoted 6 lines · Maxime Coquelin
     VirtIONet *n = VIRTIO_NET(vdev);
     struct virtio_net_config netcfg;
hw/net/virtio-net.c +3 -0
+    if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MTU)) {
+        virtio_stw_p(vdev, &netcfg.mtu, n->mtu);
+    }

This write needs to be unconditional, otherwise you are leaking a few bytes of QEMU's stack (corresponding to netcfg.mtu) to the guest.

Paolo

Quoted 3 lines
     virtio_stw_p(vdev, &netcfg.status, n->status);
     virtio_stw_p(vdev, &netcfg.max_virtqueue_pairs, n->max_queues);
     memcpy(netcfg.mac, n->mac, ETH_ALEN);
#6 lore raw

Re: [Qemu-devel] [RFC 4/4] virtio-net: Add MTU feature support

Quoted 6 lines · Paolo Bonzini
Quoted 3 lines · Maxime Coquelin
     VirtIONet *n = VIRTIO_NET(vdev);
     struct virtio_net_config netcfg;
Quoted 6 lines

hw/net/virtio-net.c static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config) · line 81

Quoted 3 lines
hw/net/virtio-net.c +3 -0
+    if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MTU)) {
+        virtio_stw_p(vdev, &netcfg.mtu, n->mtu);
+    }

This write needs to be unconditional, otherwise you are leaking a few bytes of QEMU's stack (corresponding to netcfg.mtu) to the guest.

Right, thanks for pointing this out. I'll make it unconditional.

Maxime

Quoted 5 lines

Paolo

Quoted 3 lines
     virtio_stw_p(vdev, &netcfg.status, n->status);
     virtio_stw_p(vdev, &netcfg.max_virtqueue_pairs, n->max_queues);
     memcpy(netcfg.mac, n->mac, ETH_ALEN);