[PATCH 3/3][QEMU] Support TX mitigation in QEMU's virtio_net device

Anthony Liguori
2008-01-07T21:41:30Z

This patch adds support for TX mitigation in QEMU using the new NOTIFY_ON_FULL
virtio ring flag.

Signed-off-by: Anthony Liguori <aliguori-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>

diff --git a/qemu/hw/virtio-net.c b/qemu/hw/virtio-net.c
index 3940743..5fd4114 100644
--- a/qemu/hw/virtio-net.c
+++ b/qemu/hw/virtio-net.c
@@ -14,6 +14,7 @@
#include "virtio.h"
#include "net.h"
#include "pc.h"
+#include "qemu-timer.h"

/* from Linux's virtio_net.h */

@@ -28,6 +29,10 @@
#define VIRTIO_NET_F_TSO6 4
#define VIRTIO_NET_F_MAC 5

+#define USE_TX_TIMER
+
+#define TX_TIMER_INTERVAL (1000 / 500)
+
/* The config defining mac address (6 bytes) */
struct virtio_net_config
{
@@ -63,6 +68,8 @@ typedef struct VirtIONet
int tap_fd;
struct VirtIONet *next;
int do_notify;
+ QEMUTimer *tx_timer;
+ int tx_timer_active;
} VirtIONet;

static VirtIONet *VirtIONetHead = NULL;
@@ -222,10 +229,10 @@ again:
}

/* TX */
-static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
+static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
{
- VirtIONet *n = to_virtio_net(vdev);
VirtQueueElement elem;
+ int count = 0;

if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
return;
@@ -241,11 +248,39 @@ static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
len += elem.out_sg[i].iov_len;
}

+ count++;
+
virtqueue_push(vq, &elem, sizeof(struct virtio_net_hdr) + len);
virtio_notify(&n->vdev, vq);
}
}

+static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
+{
+ VirtIONet *n = to_virtio_net(vdev);
+
+ if (n->tx_timer_active &&
+ (vq->vring.avail->idx - vq->last_avail_idx) == 64) {
+ vq->vring.used->flags &= ~VRING_USED_F_NOTIFY_ON_FULL;
+ qemu_del_timer(n->tx_timer);
+ n->tx_timer_active = 0;
+ virtio_net_flush_tx(n, vq);
+ } else {
+ qemu_mod_timer(n->tx_timer, qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
+ n->tx_timer_active = 1;
+ vq->vring.used->flags |= VRING_USED_F_NOTIFY_ON_FULL;
+ }
+}
+
+static void virtio_net_tx_timer(void *opaque)
+{
+ VirtIONet *n = opaque;
+
+ n->tx_vq->vring.used->flags &= ~VRING_USED_F_NOTIFY_ON_FULL;
+ n->tx_timer_active = 0;
+ virtio_net_flush_tx(n, n->tx_vq);
+}
+
void *virtio_net_init(PCIBus *bus, NICInfo *nd, int devfn)
{
VirtIONet *n;
@@ -270,5 +305,8 @@ void *virtio_net_init(PCIBus *bus, NICInfo *nd, int devfn)
VirtIONetHead = n;
}

+ n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
+ n->tx_timer_active = 0;
+
return &n->vdev;
}
diff --git a/qemu/hw/virtio.h b/qemu/hw/virtio.h
index dee97ba..21b96a3 100644
--- a/qemu/hw/virtio.h
+++ b/qemu/hw/virtio.h
@@ -39,6 +39,7 @@

/* This means don't notify other side when buffer added. */
#define VRING_USED_F_NO_NOTIFY 1
+#define VRING_USED_F_NOTIFY_ON_FULL 2
/* This means don't interrupt guest when buffer consumed. */
#define VRING_AVAIL_F_NO_INTERRUPT 1