[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 4/6] virtio network driver
From: |
Anthony Liguori |
Subject: |
[Qemu-devel] [PATCH 4/6] virtio network driver |
Date: |
Sat, 29 Mar 2008 16:55:58 -0500 |
This patch implements the virtio network driver backend. In KVM, this driver
can achieve 1gbit tx/rx performance. More patches are required to improve the
network IO infrastructure to achieve better performance in QEMU.
Signed-off-by: Anthony Liguori <address@hidden>
diff --git a/Makefile.target b/Makefile.target
index 6815ba8..3ea40d1 100644
--- a/Makefile.target
+++ b/Makefile.target
@@ -535,7 +535,7 @@ OBJS += rtl8139.o
OBJS += e1000.o
# virtio devices
-OBJS += virtio.o
+OBJS += virtio.o virtio-net.o
ifeq ($(TARGET_BASE_ARCH), i386)
# Hardware support
diff --git a/hw/pci.c b/hw/pci.c
index 99c206f..8dca481 100644
--- a/hw/pci.c
+++ b/hw/pci.c
@@ -654,9 +654,11 @@ void pci_nic_init(PCIBus *bus, NICInfo *nd, int devfn)
pci_e1000_init(bus, nd, devfn);
} else if (strcmp(nd->model, "pcnet") == 0) {
pci_pcnet_init(bus, nd, devfn);
+ } else if (strcmp(nd->model, "virtio") == 0) {
+ virtio_net_init(bus, nd, devfn);
} else if (strcmp(nd->model, "?") == 0) {
fprintf(stderr, "qemu: Supported PCI NICs: i82551 i82557b i82559er"
- " ne2k_pci pcnet rtl8139 e1000\n");
+ " ne2k_pci pcnet rtl8139 e1000 virtio\n");
exit (1);
} else {
fprintf(stderr, "qemu: Unsupported NIC: %s\n", nd->model);
diff --git a/hw/pci.h b/hw/pci.h
index b965919..f4db366 100644
--- a/hw/pci.h
+++ b/hw/pci.h
@@ -143,4 +143,7 @@ PCIBus *pci_prep_init(qemu_irq *pic);
PCIBus *pci_apb_init(target_phys_addr_t special_base, target_phys_addr_t
mem_base,
qemu_irq *pic);
+/* virtio.c */
+PCIDevice *virtio_net_init(PCIBus *bus, NICInfo *nd, int devfn);
+
#endif
diff --git a/hw/virtio-net.c b/hw/virtio-net.c
new file mode 100644
index 0000000..de4723a
--- /dev/null
+++ b/hw/virtio-net.c
@@ -0,0 +1,180 @@
+/*
+ * Virtio Network Device
+ *
+ * Copyright IBM, Corp. 2007
+ *
+ * Authors:
+ * Anthony Liguori <address@hidden>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2. See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+#include "virtio.h"
+#include "net.h"
+#include "pc.h"
+#include "qemu-timer.h"
+#include "virtio-net.h"
+
+#define TX_TIMER_INTERVAL (1000 / 500)
+
+typedef struct VirtIONet
+{
+ VirtIODevice vdev;
+ uint8_t mac[6];
+ VirtQueue *rx_vq;
+ VirtQueue *tx_vq;
+ VLANClientState *vc;
+ int can_receive;
+ QEMUTimer *tx_timer;
+ int tx_timer_active;
+} VirtIONet;
+
+static VirtIONet *to_virtio_net(VirtIODevice *vdev)
+{
+ return (VirtIONet *)vdev;
+}
+
+static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
+{
+ VirtIONet *n = to_virtio_net(vdev);
+ struct virtio_net_config netcfg;
+
+ memcpy(netcfg.mac, n->mac, 6);
+ memcpy(config, &netcfg, sizeof(netcfg));
+}
+
+static uint32_t virtio_net_get_features(VirtIODevice *vdev)
+{
+ return (1 << VIRTIO_NET_F_MAC);
+}
+
+/* RX */
+
+static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
+{
+ VirtIONet *n = to_virtio_net(vdev);
+ n->can_receive = 1;
+}
+
+static int virtio_net_can_receive(void *opaque)
+{
+ VirtIONet *n = opaque;
+
+ return (n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK) && n->can_receive;
+}
+
+static void virtio_net_receive(void *opaque, const uint8_t *buf, int size)
+{
+ VirtIONet *n = opaque;
+ VirtQueueElement *elem;
+ struct virtio_net_hdr hdr;
+
+ /* FIXME: the drivers really need to set their status better */
+ if (!virtio_ring_inited(n->rx_vq)) {
+ n->can_receive = 0;
+ return;
+ }
+
+ if ((elem = virtqueue_pop(n->rx_vq)) == NULL) {
+ /* wait until the guest adds some rx bufs */
+ n->can_receive = 0;
+ return;
+ }
+
+ memset(&hdr, 0, sizeof(hdr));
+ hdr.flags = 0;
+ hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
+
+ memcpy_to_iovector(&hdr, 0, sizeof(hdr), elem->virt_in);
+ memcpy_to_iovector(buf, sizeof(hdr), size, elem->virt_in);
+
+ /* signal other side */
+ virtqueue_push(n->rx_vq, elem, sizeof(hdr) + size);
+ virtio_notify(&n->vdev, n->rx_vq);
+}
+
+/* TX */
+static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
+{
+ VirtQueueElement *elem;
+
+ if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
+ return;
+
+ while ((elem = virtqueue_pop(vq))) {
+ IOVector *sg;
+ size_t out_size;
+ int i;
+
+ /* ignore the header for now */
+ out_size = iovector_size(elem->virt_out);
+
+ sg = iovector_trim(elem->virt_out, sizeof(struct virtio_net_hdr),
+ out_size - sizeof(struct virtio_net_hdr));
+
+ for (i = 0; i < sg->num; i++)
+ qemu_send_packet(n->vc, sg->sg[i].base, sg->sg[i].len);
+
+ qemu_free(sg);
+
+ virtqueue_push(vq, elem, out_size);
+ 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 &&
+ virtio_ring_avail_size(vq) == 64) {
+ virtio_ring_set_used_notify(vq, 0);
+ 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;
+ virtio_ring_set_used_notify(vq, 1);
+ }
+}
+
+static void virtio_net_tx_timer(void *opaque)
+{
+ VirtIONet *n = opaque;
+
+ n->tx_timer_active = 0;
+
+ /* Just in case the driver is not ready on more */
+ if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
+ return;
+
+ virtio_ring_set_used_notify(n->tx_vq, 0);
+ virtio_net_flush_tx(n, n->tx_vq);
+}
+
+PCIDevice *virtio_net_init(PCIBus *bus, NICInfo *nd, int devfn)
+{
+ VirtIONet *n;
+
+ n = (VirtIONet *)virtio_init_pci(bus, "virtio-net", 6900, 0x1000,
+ 0, VIRTIO_ID_NET,
+ 0x02, 0x00, 0x00,
+ 6, sizeof(VirtIONet));
+
+ n->vdev.get_config = virtio_net_get_config;
+ n->vdev.get_features = virtio_net_get_features;
+ n->rx_vq = virtio_add_queue(&n->vdev, 512, virtio_net_handle_rx);
+ n->tx_vq = virtio_add_queue(&n->vdev, 128, virtio_net_handle_tx);
+ n->can_receive = 0;
+ memcpy(n->mac, nd->macaddr, 6);
+ n->vc = qemu_new_vlan_client(nd->vlan, virtio_net_receive,
+ virtio_net_can_receive, n);
+ n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
+ n->tx_timer_active = 0;
+
+ return (PCIDevice *)n;
+}
diff --git a/hw/virtio-net.h b/hw/virtio-net.h
new file mode 100644
index 0000000..2959198
--- /dev/null
+++ b/hw/virtio-net.h
@@ -0,0 +1,54 @@
+/*
+ * Virtio-net Support
+ *
+ * Copyright IBM, Corp. 2007-2008
+ *
+ * Authors:
+ * Anthony Liguori <address@hidden>
+ * Rusty Russell <address@hidden>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2. See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+#ifndef _QEMU_VIRTIO_NET_H
+#define _QEMU_VIRTIO_NET_H
+
+/* from Linux's virtio_net.h */
+
+/* The ID for virtio_net */
+#define VIRTIO_ID_NET 1
+
+/* The feature bitmap for virtio net */
+#define VIRTIO_NET_F_NO_CSUM 0
+#define VIRTIO_NET_F_MAC 5
+#define VIRTIO_NET_F_GS0 6
+
+/* The config defining mac address (6 bytes) */
+struct virtio_net_config
+{
+ uint8_t mac[6];
+} __attribute__((packed));
+
+/* This is the first element of the scatter-gather list. If you don't
+ * specify GSO or CSUM features, you can simply ignore the header. */
+struct virtio_net_hdr
+{
+#define VIRTIO_NET_HDR_F_NEEDS_CSUM 1 // Use csum_start, csum_offset
+ uint8_t flags;
+#define VIRTIO_NET_HDR_GSO_NONE 0 // Not a GSO frame
+#define VIRTIO_NET_HDR_GSO_TCPV4 1 // GSO frame, IPv4 TCP (TSO)
+/* FIXME: Do we need this? If they said they can handle ECN, do they care? */
+#define VIRTIO_NET_HDR_GSO_TCPV4_ECN 2 // GSO frame, IPv4 TCP w/ ECN
+#define VIRTIO_NET_HDR_GSO_UDP 3 // GSO frame, IPv4 UDP (UFO)
+#define VIRTIO_NET_HDR_GSO_TCPV6 4 // GSO frame, IPv6 TCP
+#define VIRTIO_NET_HDR_GSO_ECN 0x80 // TCP has ECN set
+ uint8_t gso_type;
+ uint16_t hdr_len;
+ uint16_t gso_size;
+ uint16_t csum_start;
+ uint16_t csum_offset;
+};
+
+#endif
Re: [Qemu-devel] [PATCH 2/6] PCI DMA API, Paul Brook, 2008/03/30
[Qemu-devel] Re: [kvm-devel] [PATCH 2/6] PCI DMA API, Avi Kivity, 2008/03/30
[Qemu-devel] [PATCH 4/6] virtio network driver,
Anthony Liguori <=