Skip to contentSkip to conversation

hw/net/virtio-net: add support for notification coalescing

PATCH Superseded Koushik Dutta 2 participants 4 messages Mar 11 – Mar 23 Networking

v2 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 hw/net/virtio-net: add support for notification coalescing

Implement VirtIO Network Notification Coalescing (Bit 53). This allows the guest to manage interrupt frequency using ethtool -C for both RX and TX paths.

- Added VIRTIO_NET_F_NOTF_COAL to host features.
- Implemented VIRTIO_NET_CTRL_NOTF_COAL class handling in virtio_net_handle_ctrl_iov.
- Added logic to store and apply rx/tx usecs and max_packets.
- Added packet counters and threshold logic for both RX and TX data paths.
- Dynamic Dispatcher: Implemented a dispatcher mechanism that dynamically switches/activates the notification callback logic only after the guest enables TX coalescing via ethtool.

This reduces interrupt overhead by batching notifications based on either a packet count or a time-based threshold.

---
 hw/net/virtio-net.c                | 114 ++++++++++++++++++++++++-----
 hw/virtio/vhost-shadow-virtqueue.c |   1 +
 hw/virtio/vhost-vdpa.c             |   3 +
 include/hw/virtio/virtio-net.h     |   8 ++
 net/vhost-vdpa.c                   |  10 +++
 5 files changed, 119 insertions(+), 17 deletions(-)
hw/net/virtio-net.c +97 -17
index eccb48ad42..96aa70aea8 100644
@@ -157,6 +157,15 @@ static void flush_or_purge_queued_packets(NetClientState *nc)
  * - we could suppress RX interrupt if we were so inclined.
  */
 
+static void virtio_net_rx_notify(void *opaque)
+{
+    VirtIONet *n = opaque;
+    VirtIODevice *vdev = VIRTIO_DEVICE(n);
+
+    n->rx_pkt_cnt = 0;
+    virtio_notify(vdev, n->vqs[0].rx_vq);
+}
+
 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
 {
     VirtIONet *n = VIRTIO_NET(vdev);
@@ -1081,6 +1090,29 @@ static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd,
     }
 }
 
+static int virtio_net_handle_coal(VirtIONet *n, uint8_t cmd,
+                                  struct iovec *iov, unsigned int iov_cnt)
+{
+    struct virtio_net_ctrl_coal coal;
+    size_t s;
+
+    s = iov_to_buf(iov, iov_cnt, 0, &coal, sizeof(coal));
+    if (s != sizeof(coal)) {
+        return VIRTIO_NET_ERR;
+    }
+
+    if (cmd == VIRTIO_NET_CTRL_NOTF_COAL_RX_SET) {
+        n->rx_coal_usecs = le32_to_cpu(coal.max_usecs);
+        n->rx_coal_packets = le32_to_cpu(coal.max_packets);
+    } else if (cmd == VIRTIO_NET_CTRL_NOTF_COAL_TX_SET) {
+        n->tx_coal_usecs = le32_to_cpu(coal.max_usecs);
+        n->tx_coal_packets = le32_to_cpu(coal.max_packets);
+        n->tx_timeout = n->tx_coal_usecs * 1000;
+    }
+
+    return VIRTIO_NET_OK;
+}
+
 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
                                  struct iovec *iov, unsigned int iov_cnt)
 {
@@ -1582,6 +1614,8 @@ size_t virtio_net_handle_ctrl_iov(VirtIODevice *vdev,
         status = virtio_net_handle_mq(n, ctrl.cmd, iov, out_num);
     } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
         status = virtio_net_handle_offloads(n, ctrl.cmd, iov, out_num);
+    } else if (ctrl.class == VIRTIO_NET_CTRL_NOTF_COAL) {
+        status = virtio_net_handle_coal(n, ctrl.cmd, iov, out_num);
     }
 
     s = iov_from_buf(in_sg, in_num, 0, &status, sizeof(status));
@@ -2041,7 +2075,18 @@ static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf,
     }
 
     virtqueue_flush(q->rx_vq, i);
-    virtio_notify(vdev, q->rx_vq);
+
+    /* rx coalescing */
+    n->rx_pkt_cnt += i;
+    if (n->rx_coal_usecs == 0 || n->rx_pkt_cnt >= n->rx_coal_packets) {
+        timer_del(n->rx_index_timer);
+        virtio_net_rx_notify(n);
+    } else {
+        if (n->rx_pkt_cnt == i) {
+            timer_mod(n->rx_index_timer,
+                      qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + n->rx_coal_usecs);
+        }
+    }
 
     return size;
 
@@ -2900,6 +2945,7 @@ static void virtio_net_tx_timer(void *opaque)
     if (ret == -EBUSY || ret == -EINVAL) {
         return;
     }
+    n->tx_pkt_cnt -= ret;
     /*
      * If we flush a full burst of packets, assume there are
      * more coming and immediately rearm
@@ -2919,6 +2965,7 @@ static void virtio_net_tx_timer(void *opaque)
     ret = virtio_net_flush_tx(q);
     if (ret > 0) {
         virtio_queue_set_notification(q->tx_vq, 0);
+        n->tx_pkt_cnt -= ret;
         q->tx_waiting = 1;
         timer_mod(q->tx_timer,
                   qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
@@ -2974,27 +3021,45 @@ static void virtio_net_tx_bh(void *opaque)
     }
 }
 
-static void virtio_net_add_queue(VirtIONet *n, int index)
+static void virtio_net_handle_tx_dispatch(VirtIODevice *vdev, VirtQueue *vq)
 {
-    VirtIODevice *vdev = VIRTIO_DEVICE(n);
+    VirtIONet *n = VIRTIO_NET(vdev);
 
-    n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size,
-                                           virtio_net_handle_rx);
+    n->tx_pkt_cnt++;
+    bool use_timer = n->tx_timer_activate || n->tx_coal_usecs > 0 ||
+                     n->tx_coal_packets > 0;
 
-    if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) {
-        n->vqs[index].tx_vq =
-            virtio_add_queue(vdev, n->net_conf.tx_queue_size,
-                             virtio_net_handle_tx_timer);
-        n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
-                                              virtio_net_tx_timer,
-                                              &n->vqs[index]);
+    if (use_timer && n->tx_pkt_cnt < n->tx_coal_packets) {
+        virtio_net_handle_tx_timer(vdev, vq);
     } else {
-        n->vqs[index].tx_vq =
-            virtio_add_queue(vdev, n->net_conf.tx_queue_size,
-                             virtio_net_handle_tx_bh);
-        n->vqs[index].tx_bh = qemu_bh_new_guarded(virtio_net_tx_bh, &n->vqs[index],
-                                                  &DEVICE(vdev)->mem_reentrancy_guard);
+        n->tx_pkt_cnt = 0;
+        virtio_net_handle_tx_bh(vdev, vq);
     }
+}
+
+static void virtio_net_add_queue(VirtIONet *n, int index)
+{
+    VirtIODevice *vdev = VIRTIO_DEVICE(n);
+
+    n->vqs[index].rx_vq =
+        virtio_add_queue(vdev,
+                         n->net_conf.rx_queue_size,
+                         virtio_net_handle_rx);
+
+    n->vqs[index].tx_vq =
+        virtio_add_queue(vdev,
+                         n->net_conf.tx_queue_size,
+                         virtio_net_handle_tx_dispatch);
+
+    n->vqs[index].tx_timer =
+        timer_new_ns(QEMU_CLOCK_VIRTUAL,
+                     virtio_net_tx_timer,
+                     &n->vqs[index]);
+
+    n->vqs[index].tx_bh =
+        qemu_bh_new_guarded(virtio_net_tx_bh,
+                            &n->vqs[index],
+                            &DEVICE(vdev)->mem_reentrancy_guard);
 
     n->vqs[index].tx_waiting = 0;
     n->vqs[index].n = n;
@@ -3089,6 +3154,7 @@ static void virtio_net_get_features(VirtIODevice *vdev, uint64_t *features,
     virtio_features_or(features, features, n->host_features_ex);
 
     virtio_add_feature_ex(features, VIRTIO_NET_F_MAC);
+    virtio_add_feature_ex(features, VIRTIO_NET_F_NOTF_COAL);
 
     if (!peer_has_vnet_hdr(n)) {
         virtio_clear_feature_ex(features, VIRTIO_NET_F_CSUM);
@@ -3972,6 +4038,10 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)
         error_printf("Defaulting to \"bh\"");
     }
 
+    if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer")) {
+        n->tx_timer_activate = true;
+    }
+
     n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n),
                                     n->net_conf.tx_queue_size);
 
@@ -4048,6 +4118,14 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)
             n->rss_data.specified_hash_types.on_bits |
             n->rss_data.specified_hash_types.auto_bits;
     }
+    n->rx_pkt_cnt = 0;
+    n->tx_pkt_cnt = 0;
+    n->rx_coal_usecs = 0;
+    n->tx_coal_usecs = 0;
+    n->rx_coal_packets = 0;
+    n->tx_coal_packets = 0;
+    n->rx_index_timer = timer_new_us(QEMU_CLOCK_VIRTUAL,
+                                     virtio_net_rx_notify, n);
 }
 
 static void virtio_net_device_unrealize(DeviceState *dev)
@@ -4262,6 +4340,8 @@ static const Property virtio_net_properties[] = {
                       VIRTIO_NET_F_GUEST_USO6, true),
     DEFINE_PROP_BIT64("host_uso", VirtIONet, host_features,
                       VIRTIO_NET_F_HOST_USO, true),
+    DEFINE_PROP_BIT64("vq_notf_coal", VirtIONet, host_features,
+                      VIRTIO_NET_F_NOTF_COAL, true),
     DEFINE_PROP_ON_OFF_AUTO_BIT64("hash-ipv4", VirtIONet,
                                   rss_data.specified_hash_types,
                                   VIRTIO_NET_HASH_REPORT_IPv4 - 1,
hw/virtio/vhost-shadow-virtqueue.c +1 -0
index 6242aeb69c..104ed0ce8f 100644
@@ -37,6 +37,7 @@ bool vhost_svq_valid_features(uint64_t features, Error **errp)
         case VIRTIO_RING_F_INDIRECT_DESC:
             continue;
 
+        case VIRTIO_F_RING_RESET:
         case VIRTIO_F_ACCESS_PLATFORM:
             /* SVQ trust in the host's IOMMU to translate addresses */
         case VIRTIO_F_VERSION_1:
hw/virtio/vhost-vdpa.c +3 -0
index 2f8f11df86..8eb9bdb961 100644
@@ -1541,6 +1541,9 @@ static int vhost_vdpa_get_features(struct vhost_dev *dev,
     if (ret == 0) {
         /* Add SVQ logging capabilities */
         *features |= BIT_ULL(VHOST_F_LOG_ALL);
+
+        /* Add notification coalescing features */
+        *features |= BIT_ULL(VIRTIO_NET_F_NOTF_COAL);
     }
 
     return ret;
include/hw/virtio/virtio-net.h +8 -0
index 5b8ab7bda7..024501ed37 100644
@@ -231,6 +231,14 @@ struct VirtIONet {
     struct EBPFRSSContext ebpf_rss;
     uint32_t nr_ebpf_rss_fds;
     char **ebpf_rss_fds;
+    QEMUTimer *rx_index_timer;
+    uint32_t rx_coal_usecs;
+    uint32_t rx_coal_packets;
+    uint32_t rx_pkt_cnt;
+    uint32_t tx_coal_usecs;
+    uint32_t tx_coal_packets;
+    uint32_t tx_pkt_cnt;
+    bool tx_timer_activate;
 };
 
 size_t virtio_net_handle_ctrl_iov(VirtIODevice *vdev,
net/vhost-vdpa.c +10 -0
index 3df6091274..6a5d0f4cae 100644
@@ -61,6 +61,7 @@ static const int vdpa_feature_bits[] = {
     VIRTIO_F_NOTIFY_ON_EMPTY,
     VIRTIO_F_RING_PACKED,
     VIRTIO_F_RING_RESET,
+    VIRTIO_F_ORDER_PLATFORM,
     VIRTIO_F_VERSION_1,
     VIRTIO_F_IN_ORDER,
     VIRTIO_F_NOTIFICATION_DATA,
@@ -70,6 +71,8 @@ static const int vdpa_feature_bits[] = {
     VIRTIO_NET_F_CTRL_RX,
     VIRTIO_NET_F_CTRL_RX_EXTRA,
     VIRTIO_NET_F_CTRL_VLAN,
+    VIRTIO_NET_F_GUEST_ANNOUNCE,
+    VIRTIO_NET_F_NOTF_COAL,
     VIRTIO_NET_F_CTRL_VQ,
     VIRTIO_NET_F_GSO,
     VIRTIO_NET_F_GUEST_CSUM,
@@ -115,6 +118,13 @@ static const uint64_t vdpa_svq_device_features =
     BIT_ULL(VIRTIO_NET_F_HOST_UFO) |
     BIT_ULL(VIRTIO_NET_F_MRG_RXBUF) |
     BIT_ULL(VIRTIO_NET_F_STATUS) |
+    BIT_ULL(VIRTIO_F_RING_RESET) |
+    BIT_ULL(VIRTIO_F_ORDER_PLATFORM) |
+    BIT_ULL(VIRTIO_NET_F_GUEST_USO4) |
+    BIT_ULL(VIRTIO_NET_F_GUEST_USO6) |
+    BIT_ULL(VIRTIO_NET_F_HOST_USO) |
+    BIT_ULL(VIRTIO_NET_F_GUEST_ANNOUNCE) |
+    BIT_ULL(VIRTIO_NET_F_NOTF_COAL) |
     BIT_ULL(VIRTIO_NET_F_CTRL_VQ) |
     BIT_ULL(VIRTIO_NET_F_GSO) |
     BIT_ULL(VIRTIO_NET_F_CTRL_RX) |
Signature · 1 line
2.53.0
#1 lore raw
Quoted 88 lines · Koushik Dutta

Implement VirtIO Network Notification Coalescing (Bit 53). This allows the guest to manage interrupt frequency using ethtool -C for both RX and TX paths.

- Added VIRTIO_NET_F_NOTF_COAL to host features.
- Implemented VIRTIO_NET_CTRL_NOTF_COAL class handling in virtio_net_handle_ctrl_iov.
- Added logic to store and apply rx/tx usecs and max_packets.
- Added packet counters and threshold logic for both RX and TX data paths.
- Dynamic Dispatcher: Implemented a dispatcher mechanism that dynamically switches/activates the notification callback logic only after the guest enables TX coalescing via ethtool.

This reduces interrupt overhead by batching notifications based on either a packet count or a time-based threshold.

---
 hw/net/virtio-net.c                | 114 ++++++++++++++++++++++++-----
 hw/virtio/vhost-shadow-virtqueue.c |   1 +
 hw/virtio/vhost-vdpa.c             |   3 +
 include/hw/virtio/virtio-net.h     |   8 ++
 net/vhost-vdpa.c                   |  10 +++
 5 files changed, 119 insertions(+), 17 deletions(-)
hw/net/virtio-net.c +34 -0
index eccb48ad42..96aa70aea8 100644
@@ -157,6 +157,15 @@ static void flush_or_purge_queued_packets(NetClientState *nc)
  * - we could suppress RX interrupt if we were so inclined.
  */
 
+static void virtio_net_rx_notify(void *opaque)
+{
+    VirtIONet *n = opaque;
+    VirtIODevice *vdev = VIRTIO_DEVICE(n);
+
+    n->rx_pkt_cnt = 0;
+    virtio_notify(vdev, n->vqs[0].rx_vq);
+}
+
 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
 {
     VirtIONet *n = VIRTIO_NET(vdev);
@@ -1081,6 +1090,29 @@ static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd,
     }
 }
 
+static int virtio_net_handle_coal(VirtIONet *n, uint8_t cmd,
+                                  struct iovec *iov, unsigned int iov_cnt)
+{
+    struct virtio_net_ctrl_coal coal;
+    size_t s;
+
+    s = iov_to_buf(iov, iov_cnt, 0, &coal, sizeof(coal));
+    if (s != sizeof(coal)) {
+        return VIRTIO_NET_ERR;
+    }
+
+    if (cmd == VIRTIO_NET_CTRL_NOTF_COAL_RX_SET) {
+        n->rx_coal_usecs = le32_to_cpu(coal.max_usecs);
+        n->rx_coal_packets = le32_to_cpu(coal.max_packets);
+    } else if (cmd == VIRTIO_NET_CTRL_NOTF_COAL_TX_SET) {
+        n->tx_coal_usecs = le32_to_cpu(coal.max_usecs);
+        n->tx_coal_packets = le32_to_cpu(coal.max_packets);
+        n->tx_timeout = n->tx_coal_usecs * 1000;
+    }
+
+    return VIRTIO_NET_OK;
+}
+
 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
                                  struct iovec *iov, unsigned int iov_cnt)
 {
@@ -1582,6 +1614,8 @@ size_t virtio_net_handle_ctrl_iov(VirtIODevice *vdev,
         status = virtio_net_handle_mq(n, ctrl.cmd, iov, out_num);
     } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
         status = virtio_net_handle_offloads(n, ctrl.cmd, iov, out_num);
+    } else if (ctrl.class == VIRTIO_NET_CTRL_NOTF_COAL) {
+        status = virtio_net_handle_coal(n, ctrl.cmd, iov, out_num);
     }
 
     s = iov_from_buf(in_sg, in_num, 0, &status, sizeof(status));
@@ -2041,7 +2075,18 @@ static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf,
     }
 

hw/net/virtio-net.c static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf, · line 2085

Quoted 10 lines
hw/net/virtio-net.c +8 -1
     virtqueue_flush(q->rx_vq, i);
-    virtio_notify(vdev, q->rx_vq);
+
+    /* rx coalescing */
+    n->rx_pkt_cnt += i;
+    if (n->rx_coal_usecs == 0 || n->rx_pkt_cnt >= n->rx_coal_packets) {
+        timer_del(n->rx_index_timer);
+        virtio_net_rx_notify(n);
+    } else {
+        if (n->rx_pkt_cnt == i) {

Maybe it's better to check if !timer_pending? It wasn't immediately clear to me that n->rx_pkt_cnt == i until I realized that n->rx_pkt_cnt is 0 at the beginning of the function for that condition to be true.

Quoted 16 lines
hw/net/virtio-net.c +5 -0
+            timer_mod(n->rx_index_timer,
+                      qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + n->rx_coal_usecs);
+        }
+    }
 
     return size;
 
@@ -2900,6 +2945,7 @@ static void virtio_net_tx_timer(void *opaque)
     if (ret == -EBUSY || ret == -EINVAL) {
         return;
     }
+    n->tx_pkt_cnt -= ret;
     /*
      * If we flush a full burst of packets, assume there are
      * more coming and immediately rearm

hw/net/virtio-net.c static void virtio_net_tx_timer(void *opaque) · line 2968

Quoted 4 lines
hw/net/virtio-net.c +1 -0
@@ -2919,6 +2965,7 @@ static void virtio_net_tx_timer(void *opaque)
     ret = virtio_net_flush_tx(q);
     if (ret > 0) {
         virtio_queue_set_notification(q->tx_vq, 0);
+        n->tx_pkt_cnt -= ret;

If we got his way, we must control for underflow here.

But I think it should be n->pt_pkt_cnt = 0, isn't it?

Quoted 8 lines
hw/net/virtio-net.c +0 -1
         q->tx_waiting = 1;
         timer_mod(q->tx_timer,
                   qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
@@ -2974,27 +3021,45 @@ static void virtio_net_tx_bh(void *opaque)
     }
 }
 
-static void virtio_net_add_queue(VirtIONet *n, int index)

hw/net/virtio-net.c static void virtio_net_tx_bh(void *opaque) · line 3030

Quoted 10 lines
hw/net/virtio-net.c +5 -3
+static void virtio_net_handle_tx_dispatch(VirtIODevice *vdev, VirtQueue *vq)
 {
-    VirtIODevice *vdev = VIRTIO_DEVICE(n);
+    VirtIONet *n = VIRTIO_NET(vdev);
 
-    n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size,
-                                           virtio_net_handle_rx);
+    n->tx_pkt_cnt++;
+    bool use_timer = n->tx_timer_activate || n->tx_coal_usecs > 0 ||
+                     n->tx_coal_packets > 0;

No declarations in the middle of the function.

Also, why use_timer = true as long as n->tx_coal_packets > 0?

Quoted 29 lines
hw/net/virtio-net.c +14 -12
-    if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) {
-        n->vqs[index].tx_vq =
-            virtio_add_queue(vdev, n->net_conf.tx_queue_size,
-                             virtio_net_handle_tx_timer);
-        n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
-                                              virtio_net_tx_timer,
-                                              &n->vqs[index]);
+    if (use_timer && n->tx_pkt_cnt < n->tx_coal_packets) {
+        virtio_net_handle_tx_timer(vdev, vq);
     } else {
-        n->vqs[index].tx_vq =
-            virtio_add_queue(vdev, n->net_conf.tx_queue_size,
-                             virtio_net_handle_tx_bh);
-        n->vqs[index].tx_bh = qemu_bh_new_guarded(virtio_net_tx_bh, &n->vqs[index],
-                                                  &DEVICE(vdev)->mem_reentrancy_guard);
+        n->tx_pkt_cnt = 0;
+        virtio_net_handle_tx_bh(vdev, vq);
     }
+}
+
+static void virtio_net_add_queue(VirtIONet *n, int index)
+{
+    VirtIODevice *vdev = VIRTIO_DEVICE(n);
+
+    n->vqs[index].rx_vq =
+        virtio_add_queue(vdev,
+                         n->net_conf.rx_queue_size,
+                         virtio_net_handle_rx);

hw/net/virtio-net.c static void virtio_net_tx_bh(void *opaque) · line 3057

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+
+    n->vqs[index].tx_vq =
+        virtio_add_queue(vdev,
+                         n->net_conf.tx_queue_size,
+                         virtio_net_handle_tx_dispatch);
+
+    n->vqs[index].tx_timer =
+        timer_new_ns(QEMU_CLOCK_VIRTUAL,
+                     virtio_net_tx_timer,
+                     &n->vqs[index]);

We should avoid creating a new timer if it will not be used by default. Create it only if cmdline enables it or the guest enable it via control vq.

hw/net/virtio-net.c static void virtio_net_get_features(VirtIODevice *vdev, uint64_t *features, · line 3157

Quoted 13 lines
hw/net/virtio-net.c +6 -0
+
+    n->vqs[index].tx_bh =
+        qemu_bh_new_guarded(virtio_net_tx_bh,
+                            &n->vqs[index],
+                            &DEVICE(vdev)->mem_reentrancy_guard);
 
     n->vqs[index].tx_waiting = 0;
     n->vqs[index].n = n;
@@ -3089,6 +3154,7 @@ static void virtio_net_get_features(VirtIODevice *vdev, uint64_t *features,
     virtio_features_or(features, features, n->host_features_ex);
 
     virtio_add_feature_ex(features, VIRTIO_NET_F_MAC);
+    virtio_add_feature_ex(features, VIRTIO_NET_F_NOTF_COAL);

We shouldn't enable VIRTIO_NET_F_NOTF_COAL unconditionally, it should be regulated by the cmdline property.

Quoted 16 lines
hw/net/virtio-net.c +4 -0
     if (!peer_has_vnet_hdr(n)) {
         virtio_clear_feature_ex(features, VIRTIO_NET_F_CSUM);
@@ -3972,6 +4038,10 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)
         error_printf("Defaulting to \"bh\"");
     }
 
+    if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer")) {
+        n->tx_timer_activate = true;
+    }
+
     n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n),
                                     n->net_conf.tx_queue_size);
 
@@ -4048,6 +4118,14 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)
             n->rss_data.specified_hash_types.on_bits |

hw/net/virtio-net.c static void virtio_net_device_realize(DeviceState *dev, Error **errp) · line 4128

Quoted 10 lines
hw/net/virtio-net.c +8 -0
             n->rss_data.specified_hash_types.auto_bits;
     }
+    n->rx_pkt_cnt = 0;
+    n->tx_pkt_cnt = 0;
+    n->rx_coal_usecs = 0;
+    n->tx_coal_usecs = 0;
+    n->rx_coal_packets = 0;
+    n->tx_coal_packets = 0;
+    n->rx_index_timer = timer_new_us(QEMU_CLOCK_VIRTUAL,
+                                     virtio_net_rx_notify, n);

Same as the tx timer. Is it worth creating a new timer even if it will not be used by default?

Quoted 17 lines
hw/net/virtio-net.c +2 -0
 }
 
 static void virtio_net_device_unrealize(DeviceState *dev)
@@ -4262,6 +4340,8 @@ static const Property virtio_net_properties[] = {
                       VIRTIO_NET_F_GUEST_USO6, true),
     DEFINE_PROP_BIT64("host_uso", VirtIONet, host_features,
                       VIRTIO_NET_F_HOST_USO, true),
+    DEFINE_PROP_BIT64("vq_notf_coal", VirtIONet, host_features,
+                      VIRTIO_NET_F_NOTF_COAL, true),
     DEFINE_PROP_ON_OFF_AUTO_BIT64("hash-ipv4", VirtIONet,
                                   rss_data.specified_hash_types,
                                   VIRTIO_NET_HASH_REPORT_IPv4 - 1,
hw/virtio/vhost-shadow-virtqueue.c +0 -0
index 6242aeb69c..104ed0ce8f 100644

hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_valid_features(uint64_t features, Error **errp) · line 40

Quoted 4 lines
hw/virtio/vhost-shadow-virtqueue.c +1 -0
@@ -37,6 +37,7 @@ bool vhost_svq_valid_features(uint64_t features, Error **errp)
         case VIRTIO_RING_F_INDIRECT_DESC:
             continue;
 
+        case VIRTIO_F_RING_RESET:

This patch does not handle VIRTIO_F_RING_RESET, so this should not be enabled.

Quoted 49 lines
hw/virtio/vhost-shadow-virtqueue.c +0 -0
         case VIRTIO_F_ACCESS_PLATFORM:
             /* SVQ trust in the host's IOMMU to translate addresses */
         case VIRTIO_F_VERSION_1:
hw/virtio/vhost-vdpa.c +3 -0
index 2f8f11df86..8eb9bdb961 100644
@@ -1541,6 +1541,9 @@ static int vhost_vdpa_get_features(struct vhost_dev *dev,
     if (ret == 0) {
         /* Add SVQ logging capabilities */
         *features |= BIT_ULL(VHOST_F_LOG_ALL);
+
+        /* Add notification coalescing features */
+        *features |= BIT_ULL(VIRTIO_NET_F_NOTF_COAL);
     }
 
     return ret;
include/hw/virtio/virtio-net.h +8 -0
index 5b8ab7bda7..024501ed37 100644
@@ -231,6 +231,14 @@ struct VirtIONet {
     struct EBPFRSSContext ebpf_rss;
     uint32_t nr_ebpf_rss_fds;
     char **ebpf_rss_fds;
+    QEMUTimer *rx_index_timer;
+    uint32_t rx_coal_usecs;
+    uint32_t rx_coal_packets;
+    uint32_t rx_pkt_cnt;
+    uint32_t tx_coal_usecs;
+    uint32_t tx_coal_packets;
+    uint32_t tx_pkt_cnt;
+    bool tx_timer_activate;
 };
 
 size_t virtio_net_handle_ctrl_iov(VirtIODevice *vdev,
net/vhost-vdpa.c +1 -0
index 3df6091274..6a5d0f4cae 100644
@@ -61,6 +61,7 @@ static const int vdpa_feature_bits[] = {
     VIRTIO_F_NOTIFY_ON_EMPTY,
     VIRTIO_F_RING_PACKED,
     VIRTIO_F_RING_RESET,
+    VIRTIO_F_ORDER_PLATFORM,
     VIRTIO_F_VERSION_1,
     VIRTIO_F_IN_ORDER,
     VIRTIO_F_NOTIFICATION_DATA,

net/vhost-vdpa.c static const int vdpa_feature_bits[] = { · line 75

Quoted 5 lines
net/vhost-vdpa.c +2 -0
@@ -70,6 +71,8 @@ static const int vdpa_feature_bits[] = {
     VIRTIO_NET_F_CTRL_RX,
     VIRTIO_NET_F_CTRL_RX_EXTRA,
     VIRTIO_NET_F_CTRL_VLAN,
+    VIRTIO_NET_F_GUEST_ANNOUNCE,
+    VIRTIO_NET_F_NOTF_COAL,

Only this feature can be added to vdpa_feature_bits. The rest of them must not be added to any other array.

Quoted 20 lines
net/vhost-vdpa.c +7 -1
     VIRTIO_NET_F_CTRL_VQ,
     VIRTIO_NET_F_GSO,
     VIRTIO_NET_F_GUEST_CSUM,
@@ -115,6 +118,13 @@ static const uint64_t vdpa_svq_device_features =
     BIT_ULL(VIRTIO_NET_F_HOST_UFO) |
     BIT_ULL(VIRTIO_NET_F_MRG_RXBUF) |
     BIT_ULL(VIRTIO_NET_F_STATUS) |
+    BIT_ULL(VIRTIO_F_RING_RESET) |
+    BIT_ULL(VIRTIO_F_ORDER_PLATFORM) |
+    BIT_ULL(VIRTIO_NET_F_GUEST_USO4) |
+    BIT_ULL(VIRTIO_NET_F_GUEST_USO6) |
+    BIT_ULL(VIRTIO_NET_F_HOST_USO) |
+    BIT_ULL(VIRTIO_NET_F_GUEST_ANNOUNCE) |
+    BIT_ULL(VIRTIO_NET_F_NOTF_COAL) |
     BIT_ULL(VIRTIO_NET_F_CTRL_VQ) |
     BIT_ULL(VIRTIO_NET_F_GSO) |
     BIT_ULL(VIRTIO_NET_F_CTRL_RX) |
--

2.53.0

#2 lore raw

Hi Eugenio,
Regarding the point about not enabling VIRTIO_NET_F_NOTF_COAL
unconditionally: since feature negotiation happens during VM bootup and coalescing is configured via ethtool after the VM has already booted, how would we handle this under a conditional property?

If the feature isn't negotiated at the start, we wouldn't be able to enable it dynamically later. Could you clarify how you'd like the command-line regulation to interact with the guest's ability to enable it later?
------------------------------------------------------------

Review bool vhost_svq_valid_features(uint64_t features, Error

Quoted 1 line
(quoted diff) +0 -0
@@ -37,6 +37,7 @@ bool vhost_svq_valid_features(uint64_t features, Error

**errp)

hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_valid_features(uint64_t features, Error **errp) · line 40

Quoted 4 lines
hw/virtio/vhost-shadow-virtqueue.c +1 -0
         case VIRTIO_RING_F_INDIRECT_DESC:
             continue;
 
+        case VIRTIO_F_RING_RESET:

This patch does not handle VIRTIO_F_RING_RESET, so this should not be enabled.

=> I included VIRTIO_F_RING_RESET in the valid features list because the vDPA host device offers it by default; without it, SVQ fails the feature negotiation check during initialization.
qemu-system-x86_64: -netdev type=vhost-vdpa,vhostdev=/dev/vhost-vdpa-0,id=vhost-vdpa0,x-svq=on: SVQ Invalid device feature flags, offer: 0x1e0010330bfffa7, ok:
0x1e0000330bfffa7

Regards,
Koushik Dutta

Quoted 369 lines · Eugenio Perez Martin
Quoted 10 lines · Koushik Dutta

Implement VirtIO Network Notification Coalescing (Bit 53). This allows the guest to manage interrupt frequency using ethtool -C for both RX and TX paths.

- Added VIRTIO_NET_F_NOTF_COAL to host features.
- Implemented VIRTIO_NET_CTRL_NOTF_COAL class handling in virtio_net_handle_ctrl_iov.
- Added logic to store and apply rx/tx usecs and max_packets.
- Added packet counters and threshold logic for both RX and TX data

paths.

hw/net/virtio-net.c static void

Quoted 21 lines

- Dynamic Dispatcher: Implemented a dispatcher mechanism that dynamically switches/activates the notification callback logic only after the guest enables TX coalescing via ethtool.

This reduces interrupt overhead by batching notifications based on either a packet count or a time-based threshold.

---
 hw/net/virtio-net.c                | 114 ++++++++++++++++++++++++-----
 hw/virtio/vhost-shadow-virtqueue.c |   1 +
 hw/virtio/vhost-vdpa.c             |   3 +
 include/hw/virtio/virtio-net.h     |   8 ++
 net/vhost-vdpa.c                   |  10 +++
 5 files changed, 119 insertions(+), 17 deletions(-)
hw/net/virtio-net.c +0 -0
index eccb48ad42..96aa70aea8 100644
@@ -157,6 +157,15 @@ static void

flush_or_purge_queued_packets(NetClientState *nc)

hw/net/virtio-net.c static int virtio_net_handle_offloads(VirtIONet

Quoted 16 lines
hw/net/virtio-net.c +9 -0
  * - we could suppress RX interrupt if we were so inclined.
  */
 
+static void virtio_net_rx_notify(void *opaque)
+{
+    VirtIONet *n = opaque;
+    VirtIODevice *vdev = VIRTIO_DEVICE(n);
+
+    n->rx_pkt_cnt = 0;
+    virtio_notify(vdev, n->vqs[0].rx_vq);
+}
+
 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
 {
     VirtIONet *n = VIRTIO_NET(vdev);
@@ -1081,6 +1090,29 @@ static int virtio_net_handle_offloads(VirtIONet

*n, uint8_t cmd,

hw/net/virtio-net.c static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd,

Quoted 5 lines
hw/net/virtio-net.c +2 -0
     }
 }
 
+static int virtio_net_handle_coal(VirtIONet *n, uint8_t cmd,
+                                  struct iovec *iov, unsigned int

iov_cnt)

Quoted 13 lines
hw/net/virtio-net.c +13 -0
+{
+    struct virtio_net_ctrl_coal coal;
+    size_t s;
+
+    s = iov_to_buf(iov, iov_cnt, 0, &coal, sizeof(coal));
+    if (s != sizeof(coal)) {
+        return VIRTIO_NET_ERR;
+    }
+
+    if (cmd == VIRTIO_NET_CTRL_NOTF_COAL_RX_SET) {
+        n->rx_coal_usecs = le32_to_cpu(coal.max_usecs);
+        n->rx_coal_packets = le32_to_cpu(coal.max_packets);
+    } else if (cmd == VIRTIO_NET_CTRL_NOTF_COAL_TX_SET) {

hw/net/virtio-net.c static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd,

Quoted 10 lines
hw/net/virtio-net.c +8 -0
+        n->tx_coal_usecs = le32_to_cpu(coal.max_usecs);
+        n->tx_coal_packets = le32_to_cpu(coal.max_packets);
+        n->tx_timeout = n->tx_coal_usecs * 1000;
+    }
+
+    return VIRTIO_NET_OK;
+}
+
 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
                                  struct iovec *iov, unsigned int

iov_cnt)

hw/net/virtio-net.c size_t virtio_net_handle_ctrl_iov(VirtIODevice

Quoted 2 lines
hw/net/virtio-net.c +0 -0
 {
@@ -1582,6 +1614,8 @@ size_t virtio_net_handle_ctrl_iov(VirtIODevice

*vdev,

hw/net/virtio-net.c static ssize_t

Quoted 9 lines
hw/net/virtio-net.c +2 -0
         status = virtio_net_handle_mq(n, ctrl.cmd, iov, out_num);
     } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
         status = virtio_net_handle_offloads(n, ctrl.cmd, iov, out_num);
+    } else if (ctrl.class == VIRTIO_NET_CTRL_NOTF_COAL) {
+        status = virtio_net_handle_coal(n, ctrl.cmd, iov, out_num);
     }
 
     s = iov_from_buf(in_sg, in_num, 0, &status, sizeof(status));
@@ -2041,7 +2075,18 @@ static ssize_t

virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf,

hw/net/virtio-net.c static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf, · line 2085

Quoted 12 lines
hw/net/virtio-net.c +8 -1
     }
 
     virtqueue_flush(q->rx_vq, i);
-    virtio_notify(vdev, q->rx_vq);
+
+    /* rx coalescing */
+    n->rx_pkt_cnt += i;
+    if (n->rx_coal_usecs == 0 || n->rx_pkt_cnt >= n->rx_coal_packets) {
+        timer_del(n->rx_index_timer);
+        virtio_net_rx_notify(n);
+    } else {
+        if (n->rx_pkt_cnt == i) {

Maybe it's better to check if !timer_pending? It wasn't immediately clear to me that n->rx_pkt_cnt == i until I realized that n->rx_pkt_cnt is 0 at the beginning of the function for that condition to be true.

hw/net/virtio-net.c static ssize_t · line 2087

Quoted 2 lines
hw/net/virtio-net.c +2 -0
+            timer_mod(n->rx_index_timer,
+                      qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) +

n->rx_coal_usecs);

Quoted 14 lines
hw/net/virtio-net.c +3 -0
+        }
+    }
 
     return size;
 
@@ -2900,6 +2945,7 @@ static void virtio_net_tx_timer(void *opaque)
     if (ret == -EBUSY || ret == -EINVAL) {
         return;
     }
+    n->tx_pkt_cnt -= ret;
     /*
      * If we flush a full burst of packets, assume there are
      * more coming and immediately rearm

hw/net/virtio-net.c static void virtio_net_tx_timer(void *opaque) · line 2968

Quoted 4 lines
hw/net/virtio-net.c +1 -0
@@ -2919,6 +2965,7 @@ static void virtio_net_tx_timer(void *opaque)
     ret = virtio_net_flush_tx(q);
     if (ret > 0) {
         virtio_queue_set_notification(q->tx_vq, 0);
+        n->tx_pkt_cnt -= ret;

If we got his way, we must control for underflow here.

But I think it should be n->pt_pkt_cnt = 0, isn't it?

hw/net/virtio-net.c static void virtio_net_tx_timer(void *opaque)

Quoted 3 lines
hw/net/virtio-net.c +0 -0
         q->tx_waiting = 1;
         timer_mod(q->tx_timer,
                   qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +

n->tx_timeout);

hw/net/virtio-net.c static void virtio_net_tx_bh(void *opaque) · line 3024

Quoted 6 lines
hw/net/virtio-net.c +1 -1
@@ -2974,27 +3021,45 @@ static void virtio_net_tx_bh(void *opaque)
     }
 }
 
-static void virtio_net_add_queue(VirtIONet *n, int index)
+static void virtio_net_handle_tx_dispatch(VirtIODevice *vdev, VirtQueue

*vq)

hw/net/virtio-net.c static void virtio_net_tx_bh(void *opaque)

Quoted 5 lines
hw/net/virtio-net.c +1 -2
 {
-    VirtIODevice *vdev = VIRTIO_DEVICE(n);
+    VirtIONet *n = VIRTIO_NET(vdev);
 
-    n->vqs[index].rx_vq = virtio_add_queue(vdev,

n->net_conf.rx_queue_size,

hw/net/virtio-net.c static void virtio_net_tx_bh(void *opaque) · line 3030

Quoted 4 lines
hw/net/virtio-net.c +3 -1
-                                           virtio_net_handle_rx);
+    n->tx_pkt_cnt++;
+    bool use_timer = n->tx_timer_activate || n->tx_coal_usecs > 0 ||
+                     n->tx_coal_packets > 0;

No declarations in the middle of the function.

Also, why use_timer = true as long as n->tx_coal_packets > 0?

Quoted 5 lines
hw/net/virtio-net.c +0 -4
-    if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) {
-        n->vqs[index].tx_vq =
-            virtio_add_queue(vdev, n->net_conf.tx_queue_size,
-                             virtio_net_handle_tx_timer);

hw/net/virtio-net.c static void virtio_net_tx_bh(void *opaque)

Quoted 10 lines
hw/net/virtio-net.c +2 -7
-        n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
-                                              virtio_net_tx_timer,
-                                              &n->vqs[index]);
+    if (use_timer && n->tx_pkt_cnt < n->tx_coal_packets) {
+        virtio_net_handle_tx_timer(vdev, vq);
     } else {
-        n->vqs[index].tx_vq =
-            virtio_add_queue(vdev, n->net_conf.tx_queue_size,
-                             virtio_net_handle_tx_bh);
-        n->vqs[index].tx_bh = qemu_bh_new_guarded(virtio_net_tx_bh,

&n->vqs[index],

hw/net/virtio-net.c static void virtio_net_tx_bh(void *opaque) · line 2995

Quoted 1 line
hw/net/virtio-net.c +0 -1
-

&DEVICE(vdev)->mem_reentrancy_guard);

Quoted 13 lines
hw/net/virtio-net.c +12 -0
+        n->tx_pkt_cnt = 0;
+        virtio_net_handle_tx_bh(vdev, vq);
     }
+}
+
+static void virtio_net_add_queue(VirtIONet *n, int index)
+{
+    VirtIODevice *vdev = VIRTIO_DEVICE(n);
+
+    n->vqs[index].rx_vq =
+        virtio_add_queue(vdev,
+                         n->net_conf.rx_queue_size,
+                         virtio_net_handle_rx);

hw/net/virtio-net.c static void virtio_net_tx_bh(void *opaque) · line 3057

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+
+    n->vqs[index].tx_vq =
+        virtio_add_queue(vdev,
+                         n->net_conf.tx_queue_size,
+                         virtio_net_handle_tx_dispatch);
+
+    n->vqs[index].tx_timer =
+        timer_new_ns(QEMU_CLOCK_VIRTUAL,
+                     virtio_net_tx_timer,
+                     &n->vqs[index]);

We should avoid creating a new timer if it will not be used by default. Create it only if cmdline enables it or the guest enable it via control vq.

hw/net/virtio-net.c static void virtio_net_get_features(VirtIODevice

Quoted 9 lines
hw/net/virtio-net.c +5 -0
+
+    n->vqs[index].tx_bh =
+        qemu_bh_new_guarded(virtio_net_tx_bh,
+                            &n->vqs[index],
+                            &DEVICE(vdev)->mem_reentrancy_guard);
 
     n->vqs[index].tx_waiting = 0;
     n->vqs[index].n = n;
@@ -3089,6 +3154,7 @@ static void virtio_net_get_features(VirtIODevice

*vdev, uint64_t *features,

hw/net/virtio-net.c static void virtio_net_get_features(VirtIODevice *vdev, uint64_t *features, · line 3157

Quoted 4 lines
hw/net/virtio-net.c +1 -0
     virtio_features_or(features, features, n->host_features_ex);
 
     virtio_add_feature_ex(features, VIRTIO_NET_F_MAC);
+    virtio_add_feature_ex(features, VIRTIO_NET_F_NOTF_COAL);

We shouldn't enable VIRTIO_NET_F_NOTF_COAL unconditionally, it should be regulated by the cmdline property.

hw/net/virtio-net.c static void virtio_net_device_realize(DeviceState

Quoted 4 lines
hw/net/virtio-net.c +0 -0
     if (!peer_has_vnet_hdr(n)) {
         virtio_clear_feature_ex(features, VIRTIO_NET_F_CSUM);
@@ -3972,6 +4038,10 @@ static void virtio_net_device_realize(DeviceState

*dev, Error **errp)

hw/net/virtio-net.c static void virtio_net_device_realize(DeviceState

Quoted 11 lines
hw/net/virtio-net.c +4 -0
         error_printf("Defaulting to \"bh\"");
     }
 
+    if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer")) {
+        n->tx_timer_activate = true;
+    }
+
     n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n),
                                     n->net_conf.tx_queue_size);
 
@@ -4048,6 +4118,14 @@ static void virtio_net_device_realize(DeviceState

*dev, Error **errp)

hw/net/virtio-net.c static void virtio_net_device_realize(DeviceState *dev, Error **errp) · line 4128

Quoted 11 lines
hw/net/virtio-net.c +8 -0
             n->rss_data.specified_hash_types.on_bits |
             n->rss_data.specified_hash_types.auto_bits;
     }
+    n->rx_pkt_cnt = 0;
+    n->tx_pkt_cnt = 0;
+    n->rx_coal_usecs = 0;
+    n->tx_coal_usecs = 0;
+    n->rx_coal_packets = 0;
+    n->tx_coal_packets = 0;
+    n->rx_index_timer = timer_new_us(QEMU_CLOCK_VIRTUAL,
+                                     virtio_net_rx_notify, n);

Same as the tx timer. Is it worth creating a new timer even if it will not be used by default?

Review

Quoted 13 lines
hw/net/virtio-net.c +2 -0
 }
 
 static void virtio_net_device_unrealize(DeviceState *dev)
@@ -4262,6 +4340,8 @@ static const Property virtio_net_properties[] = {
                       VIRTIO_NET_F_GUEST_USO6, true),
     DEFINE_PROP_BIT64("host_uso", VirtIONet, host_features,
                       VIRTIO_NET_F_HOST_USO, true),
+    DEFINE_PROP_BIT64("vq_notf_coal", VirtIONet, host_features,
+                      VIRTIO_NET_F_NOTF_COAL, true),
     DEFINE_PROP_ON_OFF_AUTO_BIT64("hash-ipv4", VirtIONet,
                                   rss_data.specified_hash_types,
                                   VIRTIO_NET_HASH_REPORT_IPv4 - 1,
(quoted diff) +0 -0

b/hw/virtio/vhost-shadow-virtqueue.c

hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_valid_features(uint64_t features, Error

Quoted 4 lines

index 6242aeb69c..104ed0ce8f 100644

hw/virtio/vhost-shadow-virtqueue.c +0 -0
@@ -37,6 +37,7 @@ bool vhost_svq_valid_features(uint64_t features, Error

**errp)

hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_valid_features(uint64_t features, Error **errp) · line 40

Quoted 4 lines
hw/virtio/vhost-shadow-virtqueue.c +1 -0
         case VIRTIO_RING_F_INDIRECT_DESC:
             continue;
 
+        case VIRTIO_F_RING_RESET:

This patch does not handle VIRTIO_F_RING_RESET, so this should not be enabled.

hw/virtio/vhost-vdpa.c static int vhost_vdpa_get_features(struct

Quoted 8 lines
hw/virtio/vhost-shadow-virtqueue.c +0 -0
         case VIRTIO_F_ACCESS_PLATFORM:
             /* SVQ trust in the host's IOMMU to translate addresses */
         case VIRTIO_F_VERSION_1:
hw/virtio/vhost-vdpa.c +0 -0
index 2f8f11df86..8eb9bdb961 100644
@@ -1541,6 +1541,9 @@ static int vhost_vdpa_get_features(struct

vhost_dev *dev,

Review

Quoted 10 lines
hw/virtio/vhost-vdpa.c +3 -0
     if (ret == 0) {
         /* Add SVQ logging capabilities */
         *features |= BIT_ULL(VHOST_F_LOG_ALL);
+
+        /* Add notification coalescing features */
+        *features |= BIT_ULL(VIRTIO_NET_F_NOTF_COAL);
     }
 
     return ret;
(quoted diff) +0 -0

b/include/hw/virtio/virtio-net.h

Quoted 31 lines

index 5b8ab7bda7..024501ed37 100644

include/hw/virtio/virtio-net.h +8 -0
@@ -231,6 +231,14 @@ struct VirtIONet {
     struct EBPFRSSContext ebpf_rss;
     uint32_t nr_ebpf_rss_fds;
     char **ebpf_rss_fds;
+    QEMUTimer *rx_index_timer;
+    uint32_t rx_coal_usecs;
+    uint32_t rx_coal_packets;
+    uint32_t rx_pkt_cnt;
+    uint32_t tx_coal_usecs;
+    uint32_t tx_coal_packets;
+    uint32_t tx_pkt_cnt;
+    bool tx_timer_activate;
 };
 
 size_t virtio_net_handle_ctrl_iov(VirtIODevice *vdev,
net/vhost-vdpa.c +1 -0
index 3df6091274..6a5d0f4cae 100644
@@ -61,6 +61,7 @@ static const int vdpa_feature_bits[] = {
     VIRTIO_F_NOTIFY_ON_EMPTY,
     VIRTIO_F_RING_PACKED,
     VIRTIO_F_RING_RESET,
+    VIRTIO_F_ORDER_PLATFORM,
     VIRTIO_F_VERSION_1,
     VIRTIO_F_IN_ORDER,
     VIRTIO_F_NOTIFICATION_DATA,

net/vhost-vdpa.c static const int vdpa_feature_bits[] = { · line 75

Quoted 5 lines
net/vhost-vdpa.c +2 -0
@@ -70,6 +71,8 @@ static const int vdpa_feature_bits[] = {
     VIRTIO_NET_F_CTRL_RX,
     VIRTIO_NET_F_CTRL_RX_EXTRA,
     VIRTIO_NET_F_CTRL_VLAN,
+    VIRTIO_NET_F_GUEST_ANNOUNCE,
+    VIRTIO_NET_F_NOTF_COAL,

Only this feature can be added to vdpa_feature_bits. The rest of them must not be added to any other array.

Quoted 20 lines
net/vhost-vdpa.c +7 -1
     VIRTIO_NET_F_CTRL_VQ,
     VIRTIO_NET_F_GSO,
     VIRTIO_NET_F_GUEST_CSUM,
@@ -115,6 +118,13 @@ static const uint64_t vdpa_svq_device_features =
     BIT_ULL(VIRTIO_NET_F_HOST_UFO) |
     BIT_ULL(VIRTIO_NET_F_MRG_RXBUF) |
     BIT_ULL(VIRTIO_NET_F_STATUS) |
+    BIT_ULL(VIRTIO_F_RING_RESET) |
+    BIT_ULL(VIRTIO_F_ORDER_PLATFORM) |
+    BIT_ULL(VIRTIO_NET_F_GUEST_USO4) |
+    BIT_ULL(VIRTIO_NET_F_GUEST_USO6) |
+    BIT_ULL(VIRTIO_NET_F_HOST_USO) |
+    BIT_ULL(VIRTIO_NET_F_GUEST_ANNOUNCE) |
+    BIT_ULL(VIRTIO_NET_F_NOTF_COAL) |
     BIT_ULL(VIRTIO_NET_F_CTRL_VQ) |
     BIT_ULL(VIRTIO_NET_F_GSO) |
     BIT_ULL(VIRTIO_NET_F_CTRL_RX) |
--

2.53.0

#3 lore raw
Quoted 5 lines · Koushik Dutta

Hi Eugenio,
Regarding the point about not enabling VIRTIO_NET_F_NOTF_COAL unconditionally: since feature negotiation happens during VM bootup and coalescing is configured via ethtool after the VM has already booted, how would we handle this under a conditional property?

If the feature isn't negotiated at the start, we wouldn't be able to enable it dynamically later. Could you clarify how you'd like the command-line regulation to interact with the guest's ability to enable it later?

Replying at the position of the comment, marked with [1]. Please interleave your reply for the next time, as it makes revision easier :) [2].

Quoted 6 lines

------------------------------------------------------------

Quoted 4 lines
hw/virtio/vhost-shadow-virtqueue.c +0 -0
@@ -37,6 +37,7 @@ bool vhost_svq_valid_features(uint64_t features, Error **errp)
         case VIRTIO_RING_F_INDIRECT_DESC:
             continue;
 
Quoted 7 lines

hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_valid_features(uint64_t features, Error **errp) · line 40

Quoted 1 line
hw/virtio/vhost-shadow-virtqueue.c +1 -0
+        case VIRTIO_F_RING_RESET:

This patch does not handle VIRTIO_F_RING_RESET, so this should not be enabled.

=> I included VIRTIO_F_RING_RESET in the valid features list because the vDPA host device offers it by default; without it, SVQ fails the feature negotiation check during initialization.
qemu-system-x86_64: -netdev type=vhost-vdpa,vhostdev=/dev/vhost-vdpa-0,id=vhost-vdpa0,x-svq=on: SVQ Invalid device feature flags, offer: 0x1e0010330bfffa7, ok: 0x1e0000330bfffa7

Since you're testing the feature with nested virtualization, you can
disable the features in the L0 QEMU with:
-device virtio-net-pci,...,queue_reset=off,iommu_platform=off,guest_uso4=off,guest_uso6=off,host_uso=off,guest_announce=off

Quoted 7 lines

Regards,
Koushik Dutta

Quoted 3 lines · Eugenio Perez Martin
Quoted 1 line · Koushik Dutta

[...]

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
hw/net/virtio-net.c +16 -0
+
+static void virtio_net_add_queue(VirtIONet *n, int index)
+{
+    VirtIODevice *vdev = VIRTIO_DEVICE(n);
+
+    n->vqs[index].rx_vq =
+        virtio_add_queue(vdev,
+                         n->net_conf.rx_queue_size,
+                         virtio_net_handle_rx);
+
+    n->vqs[index].tx_vq =
+        virtio_add_queue(vdev,
+                         n->net_conf.tx_queue_size,
+                         virtio_net_handle_tx_dispatch);
+
+    n->vqs[index].tx_timer =
Quoted 8 lines
Quoted 8 lines

hw/net/virtio-net.c static void virtio_net_tx_bh(void *opaque) · line 3057

Quoted 3 lines
hw/net/virtio-net.c +3 -0
+        timer_new_ns(QEMU_CLOCK_VIRTUAL,
+                     virtio_net_tx_timer,
+                     &n->vqs[index]);

We should avoid creating a new timer if it will not be used by default. Create it only if cmdline enables it or the guest enable it via control vq.

[1]

I'm proposing we switch this to something like:

if (!strcmp(n->net_conf.tx, "timer")) {
n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
virtio_net_tx_timer, &n->vqs[index]);
}

And, in the CVQ code that you created,

if (n->tx_coal_usecs) {
n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
virtio_net_tx_timer, &n->vqs[index]);
}

And the same applies to RX timers.

[2] https://subspace.kernel.org/etiquette.html#do-not-top-post-when-replying