Skip to contentSkip to conversation

kvm tools: Add abstract virtio transport layer

PATCH Superseded Sasha Levin 2 participants 2 messages Nov 9, 2011 KVM

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kvm tools: Add abstract virtio transport layer

Since virtio-mmio was introduced in 3.2, virtio-pci isn't the only transport layer between the kernel and virtio devices.

This patch adds an abstract virtio-transport layer which allows to easily use different transports while making it transparent to the device.

This is the first step in adding virtio-mmio support.

Cc Pawel Moll <[email protected]>
Cc Peter Maydell <[email protected]>
---
 tools/kvm/Makefile                   |    1 +
 tools/kvm/include/kvm/virtio-9p.h    |    4 +-
 tools/kvm/include/kvm/virtio-pci.h   |   23 +++-----------
 tools/kvm/include/kvm/virtio-trans.h |   41 +++++++++++++++++++++++++
 tools/kvm/virtio/9p.c                |    8 +++--
 tools/kvm/virtio/balloon.c           |   18 ++++++-----
 tools/kvm/virtio/blk.c               |   11 ++++--
 tools/kvm/virtio/console.c           |   13 +++++---
 tools/kvm/virtio/net.c               |   14 +++++----
 tools/kvm/virtio/pci.c               |   56 ++++++++++++++++++++-------------
 tools/kvm/virtio/rng.c               |   12 ++++---
 tools/kvm/virtio/trans.c             |   26 ++++++++++++++++
 12 files changed, 154 insertions(+), 73 deletions(-)
 create mode 100644 tools/kvm/include/kvm/virtio-trans.h
 create mode 100644 tools/kvm/virtio/trans.c
tools/kvm/Makefile +1 -0
index 20389f9..f4741f2 100644
@@ -56,6 +56,7 @@ OBJS	+= virtio/net.o
 OBJS	+= virtio/rng.o
 OBJS    += virtio/balloon.o
 OBJS	+= virtio/pci.o
+OBJS	+= virtio/trans.o
 OBJS	+= disk/blk.o
 OBJS	+= disk/qcow.o
 OBJS	+= disk/raw.o
tools/kvm/include/kvm/virtio-9p.h +2 -2
index 3096df2..ac3dc45 100644
@@ -3,7 +3,7 @@
 #include "kvm/virtio.h"
 #include "kvm/pci.h"
 #include "kvm/threadpool.h"
-#include "kvm/virtio-pci.h"
+#include "kvm/virtio-trans.h"
 
 #include <sys/types.h>
 #include <dirent.h>
@@ -42,7 +42,7 @@ struct p9_dev_job {
 
 struct p9_dev {
 	struct list_head	list;
-	struct virtio_pci	vpci;
+	struct virtio_trans	vtrans;
 
 	struct virtio_9p_config	*config;
 	u32			features;
tools/kvm/include/kvm/virtio-pci.h +5 -18
index 81196e5..09bd0c6 100644
@@ -2,6 +2,7 @@
 #define KVM__VIRTIO_PCI_H
 
 #include "kvm/pci.h"
+#include "kvm/virtio-trans.h"
 
 #include <linux/types.h>
 
@@ -10,27 +11,13 @@
 
 struct kvm;
 
-struct virtio_pci_ops {
-	void (*set_config)(struct kvm *kvm, void *dev, u8 data, u32 offset);
-	u8 (*get_config)(struct kvm *kvm, void *dev, u32 offset);
-
-	u32 (*get_host_features)(struct kvm *kvm, void *dev);
-	void (*set_guest_features)(struct kvm *kvm, void *dev, u32 features);
-
-	int (*init_vq)(struct kvm *kvm, void *dev, u32 vq, u32 pfn);
-	int (*notify_vq)(struct kvm *kvm, void *dev, u32 vq);
-	int (*get_pfn_vq)(struct kvm *kvm, void *dev, u32 vq);
-	int (*get_size_vq)(struct kvm *kvm, void *dev, u32 vq);
-};
-
 struct virtio_pci_ioevent_param {
-	struct virtio_pci	*vpci;
+	struct virtio_trans	*vtrans;
 	u32			vq;
 };
 
 struct virtio_pci {
 	struct pci_device_header pci_hdr;
-	struct virtio_pci_ops	ops;
 	void			*dev;
 
 	u16			base_addr;
@@ -52,9 +39,9 @@ struct virtio_pci {
 	struct virtio_pci_ioevent_param ioeventfds[VIRTIO_PCI_MAX_VQ];
 };
 
-int virtio_pci__init(struct kvm *kvm, struct virtio_pci *vpci, void *dev,
+int virtio_pci__init(struct kvm *kvm, struct virtio_trans *vtrans, void *dev,
 			int device_id, int subsys_id, int class);
-int virtio_pci__signal_vq(struct kvm *kvm, struct virtio_pci *vpci, u32 vq);
-int virtio_pci__signal_config(struct kvm *kvm, struct virtio_pci *vpci);
+int virtio_pci__signal_vq(struct kvm *kvm, struct virtio_trans *vtrans, u32 vq);
+int virtio_pci__signal_config(struct kvm *kvm, struct virtio_trans *vtrans);
 
 #endif
tools/kvm/include/kvm/virtio-trans.h new file +41 -0

42 lines not on this page · raw message

tools/kvm/virtio/9p.c +5 -3
index 2392bd1..50b4d62 100644
@@ -1164,7 +1164,7 @@ static void virtio_p9_do_io(struct kvm *kvm, void *param)
 
 	while (virt_queue__available(vq)) {
 		virtio_p9_do_io_request(kvm, job);
-		virtio_pci__signal_vq(kvm, &p9dev->vpci, vq - p9dev->vqs);
+		p9dev->vtrans.trans_ops.signal_vq(kvm, &p9dev->vtrans, vq - p9dev->vqs);
 	}
 }
 
@@ -1245,8 +1245,10 @@ int virtio_9p__init(struct kvm *kvm)
 	struct p9_dev *p9dev;
 
 	list_for_each_entry(p9dev, &devs, list) {
-		virtio_pci__init(kvm, &p9dev->vpci, p9dev, PCI_DEVICE_ID_VIRTIO_P9, VIRTIO_ID_9P, PCI_CLASS_P9);
-		p9dev->vpci.ops = (struct virtio_pci_ops) {
+		virtio_trans_init(&p9dev->vtrans, VIRTIO_PCI);
+		p9dev->vtrans.trans_ops.init(kvm, &p9dev->vtrans, p9dev,
+					PCI_DEVICE_ID_VIRTIO_P9, VIRTIO_ID_9P, PCI_CLASS_P9);
+		p9dev->vtrans.virtio_ops = (struct virtio_ops) {
 			.set_config		= set_config,
 			.get_config		= get_config,
 			.get_host_features	= get_host_features,
tools/kvm/virtio/balloon.c +10 -8
index 9a52108..41f9ea9 100644
@@ -8,7 +8,7 @@
 #include "kvm/pci.h"
 #include "kvm/threadpool.h"
 #include "kvm/guest_compat.h"
-#include "kvm/virtio-pci.h"
+#include "kvm/virtio-trans.h"
 #include "kvm/kvm-ipc.h"
 
 #include <linux/virtio_ring.h>
@@ -31,7 +31,7 @@
 
 struct bln_dev {
 	struct list_head	list;
-	struct virtio_pci	vpci;
+	struct virtio_trans	vtrans;
 
 	u32			features;
 
@@ -116,13 +116,13 @@ static void virtio_bln_do_io(struct kvm *kvm, void *param)
 
 	if (vq == &bdev.vqs[VIRTIO_BLN_STATS]) {
 		virtio_bln_do_stat_request(kvm, &bdev, vq);
-		virtio_pci__signal_vq(kvm, &bdev.vpci, VIRTIO_BLN_STATS);
+		bdev.vtrans.trans_ops.signal_vq(kvm, &bdev.vtrans, VIRTIO_BLN_STATS);
 		return;
 	}
 
 	while (virt_queue__available(vq)) {
 		virtio_bln_do_io_request(kvm, &bdev, vq);
-		virtio_pci__signal_vq(kvm, &bdev.vpci, vq - bdev.vqs);
+		bdev.vtrans.trans_ops.signal_vq(kvm, &bdev.vtrans, vq - bdev.vqs);
 	}
 }
 
@@ -132,7 +132,7 @@ static int virtio_bln__collect_stats(void)
 
 	virt_queue__set_used_elem(&bdev.vqs[VIRTIO_BLN_STATS], bdev.cur_stat_head,
 				  sizeof(struct virtio_balloon_stat));
-	virtio_pci__signal_vq(kvm, &bdev.vpci, VIRTIO_BLN_STATS);
+	bdev.vtrans.trans_ops.signal_vq(kvm, &bdev.vtrans, VIRTIO_BLN_STATS);
 
 	if (read(bdev.stat_waitfd, &tmp, sizeof(tmp)) <= 0)
 		return -EFAULT;
@@ -166,7 +166,7 @@ static void handle_mem(int fd, u32 type, u32 len, u8 *msg)
 	}
 
 	/* Notify that the configuration space has changed */
-	virtio_pci__signal_config(kvm, &bdev.vpci);
+	bdev.vtrans.trans_ops.signal_config(kvm, &bdev.vtrans);
 }
 
 static void set_config(struct kvm *kvm, void *dev, u8 data, u32 offset)
@@ -242,8 +242,10 @@ void virtio_bln__init(struct kvm *kvm)
 	bdev.stat_waitfd	= eventfd(0, 0);
 	memset(&bdev.config, 0, sizeof(struct virtio_balloon_config));
 
-	virtio_pci__init(kvm, &bdev.vpci, &bdev, PCI_DEVICE_ID_VIRTIO_BLN, VIRTIO_ID_BALLOON, PCI_CLASS_BLN);
-	bdev.vpci.ops = (struct virtio_pci_ops) {
+	virtio_trans_init(&bdev.vtrans, VIRTIO_PCI);
+	bdev.vtrans.trans_ops.init(kvm, &bdev.vtrans, &bdev, PCI_DEVICE_ID_VIRTIO_BLN,
+					VIRTIO_ID_BALLOON, PCI_CLASS_BLN);
+	bdev.vtrans.virtio_ops = (struct virtio_ops) {
 		.set_config		= set_config,
 		.get_config		= get_config,
 		.get_host_features	= get_host_features,
tools/kvm/virtio/blk.c +7 -4
index 223e79f..cb33944 100644
@@ -11,6 +11,7 @@
 #include "kvm/guest_compat.h"
 #include "kvm/virtio-pci.h"
 #include "kvm/virtio.h"
+#include "kvm/virtio-trans.h"
 
 #include <linux/virtio_ring.h>
 #include <linux/virtio_blk.h>
@@ -44,7 +45,7 @@ struct blk_dev {
 	struct list_head		list;
 	struct list_head		req_list;
 
-	struct virtio_pci		vpci;
+	struct virtio_trans		vtrans;
 	struct virtio_blk_config	blk_config;
 	struct disk_image		*disk;
 	u32				features;
@@ -92,7 +93,7 @@ void virtio_blk_complete(void *param, long len)
 	virt_queue__set_used_elem(req->vq, req->head, len);
 	mutex_unlock(&bdev->mutex);
 
-	virtio_pci__signal_vq(req->kvm, &bdev->vpci, queueid);
+	bdev->vtrans.trans_ops.signal_vq(req->kvm, &bdev->vtrans, queueid);
 
 	virtio_blk_req_push(req->bdev, req);
 }
@@ -239,8 +240,10 @@ void virtio_blk__init(struct kvm *kvm, struct disk_image *disk)
 		},
 	};
 
-	virtio_pci__init(kvm, &bdev->vpci, bdev, PCI_DEVICE_ID_VIRTIO_BLK, VIRTIO_ID_BLOCK, PCI_CLASS_BLK);
-	bdev->vpci.ops = (struct virtio_pci_ops) {
+	virtio_trans_init(&bdev->vtrans, VIRTIO_PCI);
+	bdev->vtrans.trans_ops.init(kvm, &bdev->vtrans, bdev, PCI_DEVICE_ID_VIRTIO_BLK,
+					VIRTIO_ID_BLOCK, PCI_CLASS_BLK);
+	bdev->vtrans.virtio_ops = (struct virtio_ops) {
 		.set_config		= set_config,
 		.get_config		= get_config,
 		.get_host_features	= get_host_features,
tools/kvm/virtio/console.c +8 -5
index f0e4bf8..2bbae93 100644
@@ -11,7 +11,7 @@
 #include "kvm/threadpool.h"
 #include "kvm/irq.h"
 #include "kvm/guest_compat.h"
-#include "kvm/virtio-pci.h"
+#include "kvm/virtio-trans.h"
 
 #include <linux/virtio_console.h>
 #include <linux/virtio_ring.h>
@@ -33,7 +33,7 @@
 struct con_dev {
 	pthread_mutex_t			mutex;
 
-	struct virtio_pci		vpci;
+	struct virtio_trans		vtrans;
 	struct virt_queue		vqs[VIRTIO_CONSOLE_NUM_QUEUES];
 	struct virtio_console_config	config;
 	u32				features;
@@ -72,7 +72,7 @@ static void virtio_console__inject_interrupt_callback(struct kvm *kvm, void *par
 		head = virt_queue__get_iov(vq, iov, &out, &in, kvm);
 		len = term_getc_iov(CONSOLE_VIRTIO, iov, in, 0);
 		virt_queue__set_used_elem(vq, head, len);
-		virtio_pci__signal_vq(kvm, &cdev.vpci, vq - cdev.vqs);
+		cdev.vtrans.trans_ops.signal_vq(kvm, &cdev.vtrans, vq - cdev.vqs);
 	}
 
 	mutex_unlock(&cdev.mutex);
@@ -177,8 +177,11 @@ static int get_size_vq(struct kvm *kvm, void *dev, u32 vq)
 
 void virtio_console__init(struct kvm *kvm)
 {
-	virtio_pci__init(kvm, &cdev.vpci, &cdev, PCI_DEVICE_ID_VIRTIO_CONSOLE, VIRTIO_ID_CONSOLE, PCI_CLASS_CONSOLE);
-	cdev.vpci.ops = (struct virtio_pci_ops) {
+	virtio_trans_init(&cdev.vtrans, VIRTIO_PCI);
+
+	cdev.vtrans.trans_ops.init(kvm, &cdev.vtrans, &cdev, PCI_DEVICE_ID_VIRTIO_CONSOLE,
+					VIRTIO_ID_CONSOLE, PCI_CLASS_CONSOLE);
+	cdev.vtrans.virtio_ops = (struct virtio_ops) {
 		.set_config		= set_config,
 		.get_config		= get_config,
 		.get_host_features	= get_host_features,
tools/kvm/virtio/net.c +8 -6
index 7cd6794..f9fe009 100644
@@ -8,7 +8,7 @@
 #include "kvm/irq.h"
 #include "kvm/uip.h"
 #include "kvm/guest_compat.h"
-#include "kvm/virtio-pci.h"
+#include "kvm/virtio-trans.h"
 
 #include <linux/virtio_net.h>
 #include <linux/if_tun.h>
@@ -42,7 +42,7 @@ struct net_dev_operations {
 
 struct net_dev {
 	pthread_mutex_t			mutex;
-	struct virtio_pci		vpci;
+	struct virtio_trans		vtrans;
 	struct list_head		list;
 
 	struct virt_queue		vqs[VIRTIO_NET_NUM_QUEUES];
@@ -99,7 +99,7 @@ static void *virtio_net_rx_thread(void *p)
 			virt_queue__set_used_elem(vq, head, len);
 
 			/* We should interrupt guest right now, otherwise latency is huge. */
-			virtio_pci__signal_vq(kvm, &ndev->vpci, VIRTIO_NET_RX_QUEUE);
+			ndev->vtrans.trans_ops.signal_vq(kvm, &ndev->vtrans, VIRTIO_NET_RX_QUEUE);
 		}
 
 	}
@@ -137,7 +137,7 @@ static void *virtio_net_tx_thread(void *p)
 			virt_queue__set_used_elem(vq, head, len);
 		}
 
-		virtio_pci__signal_vq(kvm, &ndev->vpci, VIRTIO_NET_TX_QUEUE);
+		ndev->vtrans.trans_ops.signal_vq(kvm, &ndev->vtrans, VIRTIO_NET_TX_QUEUE);
 	}
 
 	pthread_exit(NULL);
@@ -399,8 +399,10 @@ void virtio_net__init(const struct virtio_net_params *params)
 		ndev->ops = &uip_ops;
 	}
 
-	virtio_pci__init(kvm, &ndev->vpci, ndev, PCI_DEVICE_ID_VIRTIO_NET, VIRTIO_ID_NET, PCI_CLASS_NET);
-	ndev->vpci.ops = (struct virtio_pci_ops) {
+	virtio_trans_init(&ndev->vtrans, VIRTIO_PCI);
+	ndev->vtrans.trans_ops.init(kvm, &ndev->vtrans, ndev, PCI_DEVICE_ID_VIRTIO_NET,
+					VIRTIO_ID_NET, PCI_CLASS_NET);
+	ndev->vtrans.virtio_ops = (struct virtio_ops) {
 		.set_config		= set_config,
 		.get_config		= get_config,
 		.get_host_features	= get_host_features,
tools/kvm/virtio/pci.c +34 -22

185 lines not on this page · raw message

tools/kvm/virtio/rng.c +7 -5
index efb94c9..f42e288 100644
@@ -7,7 +7,7 @@
 #include "kvm/kvm.h"
 #include "kvm/threadpool.h"
 #include "kvm/guest_compat.h"
-#include "kvm/virtio-pci.h"
+#include "kvm/virtio-trans.h"
 
 #include <linux/virtio_ring.h>
 #include <linux/virtio_rng.h>
@@ -30,7 +30,7 @@ struct rng_dev_job {
 
 struct rng_dev {
 	struct list_head	list;
-	struct virtio_pci	vpci;
+	struct virtio_trans	vtrans;
 
 	int			fd;
 
@@ -87,7 +87,7 @@ static void virtio_rng_do_io(struct kvm *kvm, void *param)
 	while (virt_queue__available(vq))
 		virtio_rng_do_io_request(kvm, rdev, vq);
 
-	virtio_pci__signal_vq(kvm, &rdev->vpci, vq - rdev->vqs);
+	rdev->vtrans.trans_ops.signal_vq(kvm, &rdev->vtrans, vq - rdev->vqs);
 }
 
 static int init_vq(struct kvm *kvm, void *dev, u32 vq, u32 pfn)
@@ -150,8 +150,10 @@ void virtio_rng__init(struct kvm *kvm)
 	if (rdev->fd < 0)
 		die("Failed initializing RNG");
 
-	virtio_pci__init(kvm, &rdev->vpci, rdev, PCI_DEVICE_ID_VIRTIO_RNG, VIRTIO_ID_RNG, PCI_CLASS_RNG);
-	rdev->vpci.ops = (struct virtio_pci_ops) {
+	virtio_trans_init(&rdev->vtrans, VIRTIO_PCI);
+	rdev->vtrans.trans_ops.init(kvm, &rdev->vtrans, rdev, PCI_DEVICE_ID_VIRTIO_RNG,
+					VIRTIO_ID_RNG, PCI_CLASS_RNG);
+	rdev->vtrans.virtio_ops = (struct virtio_ops) {
 		.set_config		= set_config,
 		.get_config		= get_config,
 		.get_host_features	= get_host_features,
tools/kvm/virtio/trans.c new file +26 -0
new file mode 100644
index 0000000..b608d54
@@ -0,0 +1,26 @@
+#include "kvm/virtio-trans.h"
+
+#include "kvm/virtio-pci.h"
+#include "kvm/util.h"
+
+#include <stdlib.h>
+
+int virtio_trans_init(struct virtio_trans *vtrans, enum VIRTIO_TRANS_TYPE type)
+{
+	void *trans;
+
+	switch (type) {
+	case VIRTIO_PCI:
+		trans = calloc(sizeof(struct virtio_pci), 1);
+		if (!trans)
+			die("Failed allocating virtio transport");
+		vtrans->virtio = trans;
+		vtrans->trans_ops = (struct virtio_trans_ops) {
+			.signal_vq	= virtio_pci__signal_vq,
+			.signal_config	= virtio_pci__signal_config,
+			.init		= virtio_pci__init,
+		};
+	default:
+		return -1;
+	};
+}
\ No newline at end of file
Signature · 1 line
1.7.7.2
#1 lore raw

tools/kvm/include/kvm/virtio-trans.h line 37

Quoted 6 lines · Sasha Levin
tools/kvm/include/kvm/virtio-trans.h new file +6 -0
+struct virtio_trans {
+       void                    *virtio;
+       enum VIRTIO_TRANS_TYPE  type;
+       struct virtio_trans_ops trans_ops;
+       struct virtio_ops       virtio_ops;
+};

Why are the ops not pointers? And why is the enum name in ALL CAPS?

Pekka