Skip to contentSkip to conversation

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

PATCH Superseded Koushik Dutta 2 participants 5 messages Mar 30 – May 4 Networking

v4 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 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.

vhost-vdpa: add support for SVQ interrupt coalescing.

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

---
 hw/net/virtio-net.c            | 137 +++++++++++++++++++++++++++++----
 include/hw/virtio/virtio-net.h |   8 ++
 net/vhost-vdpa.c               |   2 +
 3 files changed, 131 insertions(+), 16 deletions(-)
hw/net/virtio-net.c +121 -16
index eccb48ad42..16f197cbdb 100644
@@ -131,6 +131,8 @@ static const VirtIOConfigSizeParams cfg_size_params = {
     .feature_sizes = feature_sizes
 };
 
+static void virtio_net_tx_timer(void *opaque);
+
 static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc)
 {
     VirtIONet *n = qemu_get_nic_opaque(nc);
@@ -157,6 +159,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 +1092,43 @@ 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;
+    VirtIONetQueue *q;
+    size_t s;
+    int i;
+
+    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);
+        if (!n->rx_index_timer) {
+            n->rx_index_timer = timer_new_us(QEMU_CLOCK_VIRTUAL,
+                                             virtio_net_rx_notify, n);
+        }
+    } 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;
+        for (i = 0; i < n->max_queue_pairs; i++) {
+            q = &n->vqs[i];
+            if (!q->tx_timer) {
+                q->tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
+                                           virtio_net_tx_timer,
+                                           q);
+            }
+        }
+    }
+
+    return VIRTIO_NET_OK;
+}
+
 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
                                  struct iovec *iov, unsigned int iov_cnt)
 {
@@ -1582,6 +1630,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 +2091,22 @@ 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) {
+        if (n->rx_index_timer) {
+            timer_del(n->rx_index_timer);
+        }
+        virtio_net_rx_notify(n);
+    } else {
+        if (n->rx_index_timer) {
+            if (!timer_pending(n->rx_index_timer)) {
+                timer_mod(n->rx_index_timer,
+                          qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + n->rx_coal_usecs);
+            }
+        }
+    }
 
     return size;
 
@@ -2818,7 +2883,6 @@ detach:
     return -EINVAL;
 }
 
-static void virtio_net_tx_timer(void *opaque);
 
 static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
 {
@@ -2900,6 +2964,12 @@ static void virtio_net_tx_timer(void *opaque)
     if (ret == -EBUSY || ret == -EINVAL) {
         return;
     }
+    if (n->tx_pkt_cnt < ret) {
+        n->tx_pkt_cnt = 0;
+    } else {
+        n->tx_pkt_cnt -= ret;
+    }
+
     /*
      * If we flush a full burst of packets, assume there are
      * more coming and immediately rearm
@@ -2919,6 +2989,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,6 +3045,32 @@ static void virtio_net_tx_bh(void *opaque)
     }
 }
 
+static void virtio_net_handle_tx_dispatch(VirtIODevice *vdev, VirtQueue *vq)
+{
+    VirtIONet *n = VIRTIO_NET(vdev);
+    VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
+    bool use_timer = n->tx_timer_activate || n->tx_coal_usecs > 0 ||
+                     n->tx_coal_packets > 0;
+    bool pkt_limit = (n->tx_coal_packets > 0);
+
+    if (use_timer) {
+        n->tx_pkt_cnt++;
+        if (!pkt_limit || n->tx_pkt_cnt < n->tx_coal_packets) {
+            if (q->tx_timer) {
+                virtio_net_handle_tx_timer(vdev, vq);
+                return;
+            }
+        }
+        n->tx_pkt_cnt = 0;
+        if (q->tx_timer) {
+            timer_del(q->tx_timer);
+        }
+        virtio_net_handle_tx_bh(vdev, vq);
+    } else {
+        virtio_net_handle_tx_bh(vdev, vq);
+    }
+}
+
 static void virtio_net_add_queue(VirtIONet *n, int index)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(n);
@@ -2981,20 +3078,15 @@ static void virtio_net_add_queue(VirtIONet *n, int index)
     n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size,
                                            virtio_net_handle_rx);
 
-    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]);
-    } 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->vqs[index].tx_vq =
+        virtio_add_queue(vdev,
+                         n->net_conf.tx_queue_size,
+                         virtio_net_handle_tx_dispatch);
+
+    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;
@@ -3972,6 +4064,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 +4144,13 @@ 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 = NULL;
 }
 
 static void virtio_net_device_unrealize(DeviceState *dev)
@@ -4262,6 +4365,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,
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 +2 -0
index 3df6091274..a20db78b81 100644
@@ -70,6 +70,7 @@ 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_NOTF_COAL,
     VIRTIO_NET_F_CTRL_VQ,
     VIRTIO_NET_F_GSO,
     VIRTIO_NET_F_GUEST_CSUM,
@@ -115,6 +116,7 @@ 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_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 14 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.

Quoted 2 lines

vhost-vdpa: add support for SVQ interrupt coalescing.

This patch still does not add vhost-vdpa support.

Quoted 15 lines

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

---
 hw/net/virtio-net.c            | 137 +++++++++++++++++++++++++++++----
 include/hw/virtio/virtio-net.h |   8 ++
 net/vhost-vdpa.c               |   2 +
 3 files changed, 131 insertions(+), 16 deletions(-)
hw/net/virtio-net.c +0 -0
index eccb48ad42..16f197cbdb 100644

hw/net/virtio-net.c static const VirtIOConfigSizeParams cfg_size_params = { · line 135

Quoted 5 lines
hw/net/virtio-net.c +2 -0
@@ -131,6 +131,8 @@ static const VirtIOConfigSizeParams cfg_size_params = {
     .feature_sizes = feature_sizes
 };
 
+static void virtio_net_tx_timer(void *opaque);
+

Nit: I think it's better to move yhis declaration just before the function that uses it.

Quoted 4 lines
hw/net/virtio-net.c +0 -0
 static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc)
 {
     VirtIONet *n = qemu_get_nic_opaque(nc);

hw/net/virtio-net.c static void flush_or_purge_queued_packets(NetClientState *nc) · line 168

Quoted 10 lines
hw/net/virtio-net.c +7 -0
@@ -157,6 +159,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);

If we keep the code this way, this function should iterate through all the rx queues.

I'm ok with this version, but it feels more natural to me if each RX VQ has its own RX timer. This paves the way for per-vq coalescing and aligns with the tx code. This version has fewer timers and simplifies some things, so I think it is ok for the moment.

Quoted 16 lines
hw/net/virtio-net.c +9 -0
+}
+
 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
 {
     VirtIONet *n = VIRTIO_NET(vdev);
@@ -1081,6 +1092,43 @@ 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;
+    VirtIONetQueue *q;
+    size_t s;
+    int i;

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

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+
+    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);
+        if (!n->rx_index_timer) {

n->rx_index_timer && n->rx_coal_usecs > 0 ? Also delete it if rx_coal_usecs == 0?

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

Quoted 7 lines
hw/net/virtio-net.c +7 -0
+            n->rx_index_timer = timer_new_us(QEMU_CLOCK_VIRTUAL,
+                                             virtio_net_rx_notify, n);
+        }
+    } 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;

If n->tx_timeout is only used to convert us to ms, we should not allocate a variable for this and just multiply by 1000 at the timer creation time.

Quoted 27 lines
hw/net/virtio-net.c +15 -0
+        for (i = 0; i < n->max_queue_pairs; i++) {
+            q = &n->vqs[i];
+            if (!q->tx_timer) {
+                q->tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
+                                           virtio_net_tx_timer,
+                                           q);
+            }
+        }
+    }
+
+    return VIRTIO_NET_OK;
+}
+
 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
                                  struct iovec *iov, unsigned int iov_cnt)
 {
@@ -1582,6 +1630,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 +2091,22 @@ 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 2100

Quoted 10 lines
hw/net/virtio-net.c +7 -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) {
+        if (n->rx_index_timer) {
+            timer_del(n->rx_index_timer);
+        }

How does the next tick occurs if we delete the timer here?

Quoted 105 lines
hw/net/virtio-net.c +51 -15
+        virtio_net_rx_notify(n);
+    } else {
+        if (n->rx_index_timer) {
+            if (!timer_pending(n->rx_index_timer)) {
+                timer_mod(n->rx_index_timer,
+                          qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + n->rx_coal_usecs);
+            }
+        }
+    }
 
     return size;
 
@@ -2818,7 +2883,6 @@ detach:
     return -EINVAL;
 }
 
-static void virtio_net_tx_timer(void *opaque);
 
 static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
 {
@@ -2900,6 +2964,12 @@ static void virtio_net_tx_timer(void *opaque)
     if (ret == -EBUSY || ret == -EINVAL) {
         return;
     }
+    if (n->tx_pkt_cnt < ret) {
+        n->tx_pkt_cnt = 0;
+    } else {
+        n->tx_pkt_cnt -= ret;
+    }
+
     /*
      * If we flush a full burst of packets, assume there are
      * more coming and immediately rearm
@@ -2919,6 +2989,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,6 +3045,32 @@ static void virtio_net_tx_bh(void *opaque)
     }
 }
 
+static void virtio_net_handle_tx_dispatch(VirtIODevice *vdev, VirtQueue *vq)
+{
+    VirtIONet *n = VIRTIO_NET(vdev);
+    VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
+    bool use_timer = n->tx_timer_activate || n->tx_coal_usecs > 0 ||
+                     n->tx_coal_packets > 0;
+    bool pkt_limit = (n->tx_coal_packets > 0);
+
+    if (use_timer) {
+        n->tx_pkt_cnt++;
+        if (!pkt_limit || n->tx_pkt_cnt < n->tx_coal_packets) {
+            if (q->tx_timer) {
+                virtio_net_handle_tx_timer(vdev, vq);
+                return;
+            }
+        }
+        n->tx_pkt_cnt = 0;
+        if (q->tx_timer) {
+            timer_del(q->tx_timer);
+        }
+        virtio_net_handle_tx_bh(vdev, vq);
+    } else {
+        virtio_net_handle_tx_bh(vdev, vq);
+    }
+}
+
 static void virtio_net_add_queue(VirtIONet *n, int index)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(n);
@@ -2981,20 +3078,15 @@ static void virtio_net_add_queue(VirtIONet *n, int index)
     n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size,
                                            virtio_net_handle_rx);
 
-    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]);
-    } 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->vqs[index].tx_vq =
+        virtio_add_queue(vdev,
+                         n->net_conf.tx_queue_size,
+                         virtio_net_handle_tx_dispatch);
+
+    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;

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

Quoted 7 lines
hw/net/virtio-net.c +4 -0
@@ -3972,6 +4064,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;
+    }
+

I keep thinking it would be easier if we accepted either the timer cmdline or vq_notif_coal, but never both simultaneously. This way we can reuse the exact same logic for both of them.

Quoted 58 lines
hw/net/virtio-net.c +9 -0
     n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n),
                                     n->net_conf.tx_queue_size);
 
@@ -4048,6 +4144,13 @@ 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 = NULL;
 }
 
 static void virtio_net_device_unrealize(DeviceState *dev)
@@ -4262,6 +4365,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,
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..a20db78b81 100644
@@ -70,6 +70,7 @@ 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_NOTF_COAL,
     VIRTIO_NET_F_CTRL_VQ,
     VIRTIO_NET_F_GSO,
     VIRTIO_NET_F_GUEST_CSUM,

net/vhost-vdpa.c static const uint64_t vdpa_svq_device_features = · line 119

Quoted 4 lines
net/vhost-vdpa.c +1 -0
@@ -115,6 +116,7 @@ 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_NET_F_NOTF_COAL) |

This needs to be deleted, SVQ still does not support VIRTIO_NET_F_NOTF_COAL.

Quoted 6 lines
net/vhost-vdpa.c +0 -1
     BIT_ULL(VIRTIO_NET_F_CTRL_VQ) |
     BIT_ULL(VIRTIO_NET_F_GSO) |
     BIT_ULL(VIRTIO_NET_F_CTRL_RX) |
--

2.53.0

Also, VMSTATE_ must be added to vmstate_virtio_net_device to migrate these fields. This can be tested by migrating a device with an emulated device with no vdpa too.

The process of migration is described here but feel free to ping me for help: https://www.linux-kvm.org/page/Migration .

#2 lore raw
Quoted 117 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.

Quoted 6 lines

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

vhost-vdpa: add support for SVQ interrupt coalescing.

This patch still does not add vhost-vdpa support.

hw/net/virtio-net.c static const VirtIOConfigSizeParams cfg_size_params

Quoted 15 lines

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

---
 hw/net/virtio-net.c            | 137 +++++++++++++++++++++++++++++----
 include/hw/virtio/virtio-net.h |   8 ++
 net/vhost-vdpa.c               |   2 +
 3 files changed, 131 insertions(+), 16 deletions(-)
hw/net/virtio-net.c +0 -0
index eccb48ad42..16f197cbdb 100644
@@ -131,6 +131,8 @@ static const VirtIOConfigSizeParams cfg_size_params

= {

hw/net/virtio-net.c static const VirtIOConfigSizeParams cfg_size_params = { · line 135

Quoted 5 lines
hw/net/virtio-net.c +2 -0
     .feature_sizes = feature_sizes
 };
 
+static void virtio_net_tx_timer(void *opaque);
+

Nit: I think it's better to move yhis declaration just before the function that uses it.

hw/net/virtio-net.c static void

Quoted 7 lines
hw/net/virtio-net.c +0 -0
 Resolved in V5 patch set.
 
 

static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc)

 {
     VirtIONet *n = qemu_get_nic_opaque(nc);
hw/net/virtio-net.c +0 -0
@@ -157,6 +159,15 @@ static void

flush_or_purge_queued_packets(NetClientState *nc)

hw/net/virtio-net.c static void flush_or_purge_queued_packets(NetClientState *nc) · line 168

Quoted 10 lines
hw/net/virtio-net.c +7 -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);

If we keep the code this way, this function should iterate through all the rx queues.

I'm ok with this version, but it feels more natural to me if each RX VQ has its own RX timer. This paves the way for per-vq coalescing and aligns with the tx code. This version has fewer timers and simplifies some things, so I think it is ok for the moment.

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

Quoted 9 lines
Quoted 1 line

Resolved in V5 patch set.

hw/net/virtio-net.c +2 -0
+}
+
 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
 {
     VirtIONet *n = VIRTIO_NET(vdev);
@@ -1081,6 +1092,43 @@ 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 5 lines
hw/net/virtio-net.c +5 -0
+{
+    struct virtio_net_ctrl_coal coal;
+    VirtIONetQueue *q;
+    size_t s;
+    int i;

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

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+
+    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);
+        if (!n->rx_index_timer) {

n->rx_index_timer && n->rx_coal_usecs > 0 ? Also delete it if rx_coal_usecs == 0?

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

Quoted 7 lines
hw/net/virtio-net.c +7 -0
+            n->rx_index_timer = timer_new_us(QEMU_CLOCK_VIRTUAL,
+                                             virtio_net_rx_notify, n);
+        }
+    } 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;
Quoted 6 lines

If n->tx_timeout is only used to convert us to ms, we should not allocate a variable for this and just multiply by 1000 at the timer creation time.

Quoted 1 line

The tx_timeout variable is already present in the source code. I

have used the tx_timeout variable to hold the user-given tx coal value in us to ns.

Quoted 42 lines
Quoted 6 lines
hw/net/virtio-net.c +5 -0
+        for (i = 0; i < n->max_queue_pairs; i++) {
+            q = &n->vqs[i];
+            if (!q->tx_timer) {
+                q->tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
+                                           virtio_net_tx_timer,

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
+                                           q);
+            }
+        }
+    }
+
+    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 +1630,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 +2091,22 @@ 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 2100

Quoted 11 lines
hw/net/virtio-net.c +7 -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) {
+        if (n->rx_index_timer) {
+            timer_del(n->rx_index_timer);
+        }
Quoted 3 lines

How does the next tick occurs if we delete the timer here?

Quoted 1 line
hw/net/virtio-net.c +0 -0
 timer_del only stops the "alarm" from ringing, and ``timer_mod''

restarts the timer.

Quoted 213 lines

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

Quoted 7 lines
hw/net/virtio-net.c +6 -0
+        virtio_net_rx_notify(n);
+    } else {
+        if (n->rx_index_timer) {
+            if (!timer_pending(n->rx_index_timer)) {
+                timer_mod(n->rx_index_timer,
+                          qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) +

n->rx_coal_usecs);

hw/net/virtio-net.c detach: · line 2887

Quoted 13 lines
hw/net/virtio-net.c +3 -1
+            }
+        }
+    }
 
     return size;
@@ -2818,7 +2883,6 @@ detach:
     return -EINVAL;
 }
 
-static void virtio_net_tx_timer(void *opaque);
 
 static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue

*vq)

Quoted 15 lines
hw/net/virtio-net.c +6 -0
 {
@@ -2900,6 +2964,12 @@ static void virtio_net_tx_timer(void *opaque)
     if (ret == -EBUSY || ret == -EINVAL) {
         return;
     }
+    if (n->tx_pkt_cnt < ret) {
+        n->tx_pkt_cnt = 0;
+    } else {
+        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 2995

Quoted 7 lines
hw/net/virtio-net.c +1 -0
@@ -2919,6 +2989,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);

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

Quoted 5 lines
hw/net/virtio-net.c +1 -0
@@ -2974,6 +3045,32 @@ static void virtio_net_tx_bh(void *opaque)
     }
 }
 
+static void virtio_net_handle_tx_dispatch(VirtIODevice *vdev, VirtQueue

*vq)

hw/net/virtio-net.c static void virtio_net_add_queue(VirtIONet *n,

Quoted 29 lines
hw/net/virtio-net.c +25 -0
+{
+    VirtIONet *n = VIRTIO_NET(vdev);
+    VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
+    bool use_timer = n->tx_timer_activate || n->tx_coal_usecs > 0 ||
+                     n->tx_coal_packets > 0;
+    bool pkt_limit = (n->tx_coal_packets > 0);
+
+    if (use_timer) {
+        n->tx_pkt_cnt++;
+        if (!pkt_limit || n->tx_pkt_cnt < n->tx_coal_packets) {
+            if (q->tx_timer) {
+                virtio_net_handle_tx_timer(vdev, vq);
+                return;
+            }
+        }
+        n->tx_pkt_cnt = 0;
+        if (q->tx_timer) {
+            timer_del(q->tx_timer);
+        }
+        virtio_net_handle_tx_bh(vdev, vq);
+    } else {
+        virtio_net_handle_tx_bh(vdev, vq);
+    }
+}
+
 static void virtio_net_add_queue(VirtIONet *n, int index)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(n);
@@ -2981,20 +3078,15 @@ static void virtio_net_add_queue(VirtIONet *n,

int index)

hw/net/virtio-net.c static void virtio_net_add_queue(VirtIONet *n, · line 3078

Quoted 1 line
hw/net/virtio-net.c +0 -0
     n->vqs[index].rx_vq = virtio_add_queue(vdev,

n->net_conf.rx_queue_size,

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

hw/net/virtio-net.c static void virtio_net_add_queue(VirtIONet *n, int index)

Quoted 10 lines
hw/net/virtio-net.c +0 -10
-            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]);
-    } 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_add_queue(VirtIONet *n, · line 2996

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

&DEVICE(vdev)->mem_reentrancy_guard);

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

Quoted 14 lines
hw/net/virtio-net.c +9 -1
-    }
+    n->vqs[index].tx_vq =
+        virtio_add_queue(vdev,
+                         n->net_conf.tx_queue_size,
+                         virtio_net_handle_tx_dispatch);
+
+    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;
@@ -3972,6 +4064,10 @@ 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 4070

Quoted 7 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;
+    }
+

I keep thinking it would be easier if we accepted either the timer cmdline or vq_notif_coal, but never both simultaneously. This way we can reuse the exact same logic for both of them.

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

Quoted 4 lines
hw/net/virtio-net.c +0 -0
     n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n),
                                     n->net_conf.tx_queue_size);
 
@@ -4048,6 +4144,13 @@ static void virtio_net_device_realize(DeviceState

*dev, Error **errp)

Review

Quoted 23 lines
hw/net/virtio-net.c +9 -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 = NULL;
 }
 
 static void virtio_net_device_unrealize(DeviceState *dev)
@@ -4262,6 +4365,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/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..a20db78b81 100644
@@ -70,6 +70,7 @@ 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_NOTF_COAL,
     VIRTIO_NET_F_CTRL_VQ,
     VIRTIO_NET_F_GSO,
     VIRTIO_NET_F_GUEST_CSUM,

net/vhost-vdpa.c static const uint64_t vdpa_svq_device_features = · line 119

Quoted 4 lines
net/vhost-vdpa.c +1 -0
@@ -115,6 +116,7 @@ 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_NET_F_NOTF_COAL) |

This needs to be deleted, SVQ still does not support VIRTIO_NET_F_NOTF_COAL.

Quoted 6 lines
net/vhost-vdpa.c +0 -1
     BIT_ULL(VIRTIO_NET_F_CTRL_VQ) |
     BIT_ULL(VIRTIO_NET_F_GSO) |
     BIT_ULL(VIRTIO_NET_F_CTRL_RX) |
--

2.53.0

Also, VMSTATE_ must be added to vmstate_virtio_net_device to migrate these fields. This can be tested by migrating a device with an emulated device with no vdpa too.

Quoted 1 line

Done in V5 patch set.

The process of migration is described here but feel free to ping me for help: https://www.linux-kvm.org/page/Migration .

#3 lore raw
Quoted 117 lines · Koushik Dutta
Quoted 40 lines · Eugenio Perez Martin
Quoted 14 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.

Quoted 2 lines

vhost-vdpa: add support for SVQ interrupt coalescing.

This patch still does not add vhost-vdpa support.

Quoted 18 lines

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

---
 hw/net/virtio-net.c            | 137 +++++++++++++++++++++++++++++----
 include/hw/virtio/virtio-net.h |   8 ++
 net/vhost-vdpa.c               |   2 +
 3 files changed, 131 insertions(+), 16 deletions(-)
hw/net/virtio-net.c +0 -0
index eccb48ad42..16f197cbdb 100644
@@ -131,6 +131,8 @@ static const VirtIOConfigSizeParams cfg_size_params = {
     .feature_sizes = feature_sizes
 };
 
Quoted 5 lines

hw/net/virtio-net.c static const VirtIOConfigSizeParams cfg_size_params = { · line 135

Quoted 2 lines
hw/net/virtio-net.c +2 -0
+static void virtio_net_tx_timer(void *opaque);
+

Nit: I think it's better to move yhis declaration just before the function that uses it.

Resolved in V5 patch set.

Quoted 20 lines
Quoted 6 lines
hw/net/virtio-net.c +0 -0
 static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc)
 {
     VirtIONet *n = qemu_get_nic_opaque(nc);

hw/net/virtio-net.c static void flush_or_purge_queued_packets(NetClientState *nc) · line 168

Quoted 10 lines
hw/net/virtio-net.c +7 -0
@@ -157,6 +159,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);

If we keep the code this way, this function should iterate through all the rx queues.

Quoted 5 lines

I'm ok with this version, but it feels more natural to me if each RX VQ has its own RX timer. This paves the way for per-vq coalescing and aligns with the tx code. This version has fewer timers and simplifies some things, so I think it is ok for the moment.

Resolved in V5 patch set.

Quoted 39 lines
Quoted 17 lines
hw/net/virtio-net.c +9 -0
+}
+
 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
 {
     VirtIONet *n = VIRTIO_NET(vdev);
@@ -1081,6 +1092,43 @@ 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;
+    VirtIONetQueue *q;
+    size_t s;
+    int i;

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

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+
+    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);
+        if (!n->rx_index_timer) {

n->rx_index_timer && n->rx_coal_usecs > 0 ? Also delete it if rx_coal_usecs == 0?

Quoted 7 lines
hw/net/virtio-net.c +7 -0
+            n->rx_index_timer = timer_new_us(QEMU_CLOCK_VIRTUAL,
+                                             virtio_net_rx_notify, n);
+        }
+    } 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;
Quoted 6 lines
Quoted 3 lines

If n->tx_timeout is only used to convert us to ms, we should not allocate a variable for this and just multiply by 1000 at the timer creation time.

The tx_timeout variable is already present in the source code. I have used the tx_timeout variable to hold the user-given tx coal value in us to ns.

So can we just remove tx_timeout and use tx_coal_usecs instead?

Quoted 42 lines
Quoted 30 lines
Quoted 30 lines
hw/net/virtio-net.c +15 -0
+        for (i = 0; i < n->max_queue_pairs; i++) {
+            q = &n->vqs[i];
+            if (!q->tx_timer) {
+                q->tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
+                                           virtio_net_tx_timer,
+                                           q);
+            }
+        }
+    }
+
+    return VIRTIO_NET_OK;
+}
+
 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
                                  struct iovec *iov, unsigned int iov_cnt)
 {
@@ -1582,6 +1630,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 +2091,22 @@ static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf,
     }
 
Quoted 11 lines

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

Quoted 9 lines
hw/net/virtio-net.c +7 -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) {
+        if (n->rx_index_timer) {
+            timer_del(n->rx_index_timer);
+        }

How does the next tick occurs if we delete the timer here?

Quoted 1 line

timer_del only stops the "alarm" from ringing, and ``timer_mod'' restarts the timer.

Ouch, my bad! I thought timer_del totally cleaned the timer.

Quoted 201 lines
Quoted 201 lines
Quoted 107 lines
hw/net/virtio-net.c +51 -15
+        virtio_net_rx_notify(n);
+    } else {
+        if (n->rx_index_timer) {
+            if (!timer_pending(n->rx_index_timer)) {
+                timer_mod(n->rx_index_timer,
+                          qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + n->rx_coal_usecs);
+            }
+        }
+    }
 
     return size;
 
@@ -2818,7 +2883,6 @@ detach:
     return -EINVAL;
 }
 
-static void virtio_net_tx_timer(void *opaque);
 
 static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
 {
@@ -2900,6 +2964,12 @@ static void virtio_net_tx_timer(void *opaque)
     if (ret == -EBUSY || ret == -EINVAL) {
         return;
     }
+    if (n->tx_pkt_cnt < ret) {
+        n->tx_pkt_cnt = 0;
+    } else {
+        n->tx_pkt_cnt -= ret;
+    }
+
     /*
      * If we flush a full burst of packets, assume there are
      * more coming and immediately rearm
@@ -2919,6 +2989,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,6 +3045,32 @@ static void virtio_net_tx_bh(void *opaque)
     }
 }
 
+static void virtio_net_handle_tx_dispatch(VirtIODevice *vdev, VirtQueue *vq)
+{
+    VirtIONet *n = VIRTIO_NET(vdev);
+    VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
+    bool use_timer = n->tx_timer_activate || n->tx_coal_usecs > 0 ||
+                     n->tx_coal_packets > 0;
+    bool pkt_limit = (n->tx_coal_packets > 0);
+
+    if (use_timer) {
+        n->tx_pkt_cnt++;
+        if (!pkt_limit || n->tx_pkt_cnt < n->tx_coal_packets) {
+            if (q->tx_timer) {
+                virtio_net_handle_tx_timer(vdev, vq);
+                return;
+            }
+        }
+        n->tx_pkt_cnt = 0;
+        if (q->tx_timer) {
+            timer_del(q->tx_timer);
+        }
+        virtio_net_handle_tx_bh(vdev, vq);
+    } else {
+        virtio_net_handle_tx_bh(vdev, vq);
+    }
+}
+
 static void virtio_net_add_queue(VirtIONet *n, int index)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(n);
@@ -2981,20 +3078,15 @@ static void virtio_net_add_queue(VirtIONet *n, int index)
     n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size,
                                            virtio_net_handle_rx);
 
-    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]);
-    } 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->vqs[index].tx_vq =
+        virtio_add_queue(vdev,
+                         n->net_conf.tx_queue_size,
+                         virtio_net_handle_tx_dispatch);
+
+    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;

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

Quoted 7 lines
hw/net/virtio-net.c +4 -0
@@ -3972,6 +4064,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;
+    }
+

I keep thinking it would be easier if we accepted either the timer cmdline or vq_notif_coal, but never both simultaneously. This way we can reuse the exact same logic for both of them.

Quoted 58 lines
hw/net/virtio-net.c +9 -0
     n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n),
                                     n->net_conf.tx_queue_size);
 
@@ -4048,6 +4144,13 @@ 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 = NULL;
 }
 
 static void virtio_net_device_unrealize(DeviceState *dev)
@@ -4262,6 +4365,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,
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..a20db78b81 100644
@@ -70,6 +70,7 @@ 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_NOTF_COAL,
     VIRTIO_NET_F_CTRL_VQ,
     VIRTIO_NET_F_GSO,
     VIRTIO_NET_F_GUEST_CSUM,

net/vhost-vdpa.c static const uint64_t vdpa_svq_device_features = · line 119

Quoted 4 lines
net/vhost-vdpa.c +1 -0
@@ -115,6 +116,7 @@ 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_NET_F_NOTF_COAL) |

This needs to be deleted, SVQ still does not support VIRTIO_NET_F_NOTF_COAL.

Quoted 6 lines
net/vhost-vdpa.c +0 -1
     BIT_ULL(VIRTIO_NET_F_CTRL_VQ) |
     BIT_ULL(VIRTIO_NET_F_GSO) |
     BIT_ULL(VIRTIO_NET_F_CTRL_RX) |
--

2.53.0

Also, VMSTATE_ must be added to vmstate_virtio_net_device to migrate these fields. This can be tested by migrating a device with an emulated device with no vdpa too.

Quoted 1 line

Done in V5 patch set.

The process of migration is described here but feel free to ping me for help: https://www.linux-kvm.org/page/Migration .

#4 lore raw

So can we just remove tx_timeout and use tx_coal_usecs instead? Address in the V6 patch set.

Quoted 407 lines · Eugenio Perez Martin
Quoted 4 lines · Koushik Dutta

On Tue, Mar 31, 2026 at 4:42 PM Eugenio Perez Martin <

Quoted 2 lines · [email protected]> wrote:
Quoted 2 lines

On Mon, Mar 30, 2026 at 2:29 PM Koushik Dutta <[email protected]>

Quoted 10 lines · wrote:
Quoted 10 lines
Quoted 10 lines

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.

Quoted 10 lines
Quoted 10 lines
Quoted 6 lines

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

vhost-vdpa: add support for SVQ interrupt coalescing.

This patch still does not add vhost-vdpa support.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

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

---
 hw/net/virtio-net.c            | 137

+++++++++++++++++++++++++++++----

Quoted 9 lines
Quoted 9 lines

hw/net/virtio-net.c static const VirtIOConfigSizeParams

Quoted 9 lines
 include/hw/virtio/virtio-net.h |   8 ++
 net/vhost-vdpa.c               |   2 +
 3 files changed, 131 insertions(+), 16 deletions(-)
hw/net/virtio-net.c +0 -0
index eccb48ad42..16f197cbdb 100644
@@ -131,6 +131,8 @@ static const VirtIOConfigSizeParams

cfg_size_params = {

Quoted 9 lines
Quoted 8 lines

hw/net/virtio-net.c static const VirtIOConfigSizeParams cfg_size_params = { · line 135

Quoted 5 lines
hw/net/virtio-net.c +2 -0
     .feature_sizes = feature_sizes
 };
 
+static void virtio_net_tx_timer(void *opaque);
+

Nit: I think it's better to move yhis declaration just before the function that uses it.

Quoted 7 lines

Resolved in V5 patch set.

Quoted 6 lines

hw/net/virtio-net.c static void

Quoted 6 lines
hw/net/virtio-net.c +0 -0
 static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc)
 {
     VirtIONet *n = qemu_get_nic_opaque(nc);
@@ -157,6 +159,15 @@ static void

flush_or_purge_queued_packets(NetClientState *nc)

Quoted 19 lines
Quoted 14 lines

hw/net/virtio-net.c static void flush_or_purge_queued_packets(NetClientState *nc) · line 168

Quoted 10 lines
hw/net/virtio-net.c +7 -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);

If we keep the code this way, this function should iterate through all the rx queues.

Quoted 5 lines

I'm ok with this version, but it feels more natural to me if each RX VQ has its own RX timer. This paves the way for per-vq coalescing and aligns with the tx code. This version has fewer timers and simplifies some things, so I think it is ok for the moment.

Quoted 6 lines

Resolved in V5 patch set.

Quoted 4 lines

hw/net/virtio-net.c static void flush_or_purge_queued_packets(NetClientState *nc)

Quoted 4 lines
hw/net/virtio-net.c +2 -0
+}
+
 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t

*config)

Quoted 3 lines
Quoted 3 lines

hw/net/virtio-net.c static int

Quoted 3 lines
hw/net/virtio-net.c +0 -0
 {
     VirtIONet *n = VIRTIO_NET(vdev);
@@ -1081,6 +1092,43 @@ static int

virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd,

Quoted 5 lines
Quoted 5 lines

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 19 lines
Quoted 19 lines
Quoted 5 lines
hw/net/virtio-net.c +5 -0
+{
+    struct virtio_net_ctrl_coal coal;
+    VirtIONetQueue *q;
+    size_t s;
+    int i;

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

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+
+    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);
+        if (!n->rx_index_timer) {

n->rx_index_timer && n->rx_coal_usecs > 0 ? Also delete it if rx_coal_usecs == 0?

Quoted 2 lines
Quoted 2 lines

hw/net/virtio-net.c static int · line 1113

Quoted 2 lines
hw/net/virtio-net.c +2 -0
+            n->rx_index_timer = timer_new_us(QEMU_CLOCK_VIRTUAL,
+                                             virtio_net_rx_notify,

n);

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
hw/net/virtio-net.c +5 -0
+        }
+    } 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;
Quoted 6 lines
Quoted 3 lines

If n->tx_timeout is only used to convert us to ms, we should not allocate a variable for this and just multiply by 1000 at the timer creation time.

The tx_timeout variable is already present in the source code. I have

used the tx_timeout variable to hold the user-given tx coal value in us to ns.

So can we just remove tx_timeout and use tx_coal_usecs instead?

Quoted 17 lines
Quoted 17 lines
Quoted 7 lines
hw/net/virtio-net.c +5 -0
+        for (i = 0; i < n->max_queue_pairs; i++) {
+            q = &n->vqs[i];
+            if (!q->tx_timer) {
+                q->tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
+                                           virtio_net_tx_timer,

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
+                                           q);
+            }
+        }
+    }
+
+    return VIRTIO_NET_OK;
+}
+
 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
                                  struct iovec *iov, unsigned int

iov_cnt)

Quoted 2 lines
Quoted 2 lines

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 +1630,8 @@ size_t virtio_net_handle_ctrl_iov(VirtIODevice

*vdev,

Quoted 3 lines
Quoted 3 lines

hw/net/virtio-net.c size_t virtio_net_handle_ctrl_iov(VirtIODevice *vdev,

Quoted 3 lines
hw/net/virtio-net.c +0 -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);

Quoted 6 lines
Quoted 6 lines

hw/net/virtio-net.c static ssize_t

Quoted 6 lines
hw/net/virtio-net.c +2 -0
+    } 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 +2091,22 @@ static ssize_t

virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf,

Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
hw/net/virtio-net.c +4 -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) {

Quoted 7 lines
Quoted 5 lines

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

Quoted 3 lines
hw/net/virtio-net.c +3 -0
+        if (n->rx_index_timer) {
+            timer_del(n->rx_index_timer);
+        }

How does the next tick occurs if we delete the timer here?

timer_del only stops the "alarm" from ringing, and ``timer_mod''

restarts the timer.

Ouch, my bad! I thought timer_del totally cleaned the timer.

Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
hw/net/virtio-net.c +6 -0
+        virtio_net_rx_notify(n);
+    } else {
+        if (n->rx_index_timer) {
+            if (!timer_pending(n->rx_index_timer)) {
+                timer_mod(n->rx_index_timer,
+                          qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) +

n->rx_coal_usecs);

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
hw/net/virtio-net.c +3 -0
+            }
+        }
+    }
 
     return size;
 
Quoted 7 lines
Quoted 7 lines

hw/net/virtio-net.c detach: · line 2887

Quoted 7 lines
hw/net/virtio-net.c +0 -1
@@ -2818,7 +2883,6 @@ detach:
     return -EINVAL;
 }
 
-static void virtio_net_tx_timer(void *opaque);
 
 static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue

*vq)

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

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

Quoted 8 lines
hw/net/virtio-net.c +1 -0
@@ -2919,6 +2989,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);

Quoted 5 lines
Quoted 5 lines

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

Quoted 5 lines
hw/net/virtio-net.c +1 -0
@@ -2974,6 +3045,32 @@ static void virtio_net_tx_bh(void *opaque)
     }
 }
 
+static void virtio_net_handle_tx_dispatch(VirtIODevice *vdev,

VirtQueue *vq)

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
hw/net/virtio-net.c +21 -0
+{
+    VirtIONet *n = VIRTIO_NET(vdev);
+    VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
+    bool use_timer = n->tx_timer_activate || n->tx_coal_usecs > 0 ||
+                     n->tx_coal_packets > 0;
+    bool pkt_limit = (n->tx_coal_packets > 0);
+
+    if (use_timer) {
+        n->tx_pkt_cnt++;
+        if (!pkt_limit || n->tx_pkt_cnt < n->tx_coal_packets) {
+            if (q->tx_timer) {
+                virtio_net_handle_tx_timer(vdev, vq);
+                return;
+            }
+        }
+        n->tx_pkt_cnt = 0;
+        if (q->tx_timer) {
+            timer_del(q->tx_timer);
+        }
+        virtio_net_handle_tx_bh(vdev, vq);
+    } else {
Quoted 8 lines
Quoted 8 lines

hw/net/virtio-net.c static void virtio_net_add_queue(VirtIONet

Quoted 8 lines
hw/net/virtio-net.c +4 -0
+        virtio_net_handle_tx_bh(vdev, vq);
+    }
+}
+
 static void virtio_net_add_queue(VirtIONet *n, int index)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(n);
@@ -2981,20 +3078,15 @@ static void virtio_net_add_queue(VirtIONet

*n, int index)

Quoted 1 line
Quoted 1 line

hw/net/virtio-net.c static void virtio_net_add_queue(VirtIONet · line 3078

Quoted 1 line
hw/net/virtio-net.c +0 -0
     n->vqs[index].rx_vq = virtio_add_queue(vdev,

n->net_conf.rx_queue_size,

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

hw/net/virtio-net.c static void virtio_net_add_queue(VirtIONet *n, int index)

Quoted 10 lines
hw/net/virtio-net.c +0 -10
-            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]);
-    } 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],

Quoted 1 line
Quoted 1 line

hw/net/virtio-net.c static void virtio_net_add_queue(VirtIONet · line 2996

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

&DEVICE(vdev)->mem_reentrancy_guard);

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
hw/net/virtio-net.c +9 -1
-    }
+    n->vqs[index].tx_vq =
+        virtio_add_queue(vdev,
+                         n->net_conf.tx_queue_size,
+                         virtio_net_handle_tx_dispatch);
+
+    n->vqs[index].tx_bh =
+        qemu_bh_new_guarded(virtio_net_tx_bh,
+                            &n->vqs[index],
+                            &DEVICE(vdev)->mem_reentrancy_guard);
 
Quoted 3 lines
Quoted 3 lines

hw/net/virtio-net.c static void

Quoted 3 lines
hw/net/virtio-net.c +0 -0
     n->vqs[index].tx_waiting = 0;
     n->vqs[index].n = n;
@@ -3972,6 +4064,10 @@ static void

virtio_net_device_realize(DeviceState *dev, Error **errp)

Quoted 8 lines
Quoted 8 lines
Quoted 7 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;
+    }
+
Quoted 8 lines
Quoted 8 lines

I keep thinking it would be easier if we accepted either the timer cmdline or vq_notif_coal, but never both simultaneously. This way we can reuse the exact same logic for both of them.

hw/net/virtio-net.c static void

Quoted 4 lines
hw/net/virtio-net.c +0 -0
     n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n),
                                     n->net_conf.tx_queue_size);
 
@@ -4048,6 +4144,13 @@ static void

virtio_net_device_realize(DeviceState *dev, Error **errp)

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
hw/net/virtio-net.c +7 -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 = NULL;
 }
 
Quoted 2 lines
Quoted 2 lines

hw/net/virtio-net.c static const Property virtio_net_properties[] =

Quoted 2 lines
hw/net/virtio-net.c +0 -0
 static void virtio_net_device_unrealize(DeviceState *dev)
@@ -4262,6 +4365,8 @@ static const Property virtio_net_properties[] =

{

Quoted 9 lines
Quoted 9 lines

Review

Quoted 9 lines
hw/net/virtio-net.c +2 -0
                       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/include/hw/virtio/virtio-net.h

Quoted 29 lines
Quoted 29 lines
Quoted 29 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..a20db78b81 100644
@@ -70,6 +70,7 @@ 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_NOTF_COAL,
     VIRTIO_NET_F_CTRL_VQ,
     VIRTIO_NET_F_GSO,
Quoted 8 lines
Quoted 8 lines

net/vhost-vdpa.c static const uint64_t vdpa_svq_device_features = · line 119

Quoted 6 lines
net/vhost-vdpa.c +1 -0
     VIRTIO_NET_F_GUEST_CSUM,
@@ -115,6 +116,7 @@ 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_NET_F_NOTF_COAL) |

This needs to be deleted, SVQ still does not support

VIRTIO_NET_F_NOTF_COAL.

Quoted 18 lines
Quoted 18 lines
Quoted 7 lines
net/vhost-vdpa.c +0 -1
     BIT_ULL(VIRTIO_NET_F_CTRL_VQ) |
     BIT_ULL(VIRTIO_NET_F_GSO) |
     BIT_ULL(VIRTIO_NET_F_CTRL_RX) |
--

2.53.0

Also, VMSTATE_ must be added to vmstate_virtio_net_device to migrate these fields. This can be tested by migrating a device with an emulated device with no vdpa too.

Quoted 1 line

Done in V5 patch set.

The process of migration is described here but feel free to ping me for help: https://www.linux-kvm.org/page/Migration .