kvm tools: Add abstract virtio transport layer
PATCH v2 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]>
Signed-off-by Sasha Levin <[email protected]>
--- tools/kvm/Makefile | 1 + tools/kvm/include/kvm/virtio-9p.h | 4 +- tools/kvm/include/kvm/virtio-pci.h | 25 ++++--------- tools/kvm/include/kvm/virtio-trans.h | 41 +++++++++++++++++++++ tools/kvm/virtio/9p.c | 28 ++++++++------ tools/kvm/virtio/balloon.c | 38 +++++++++++--------- tools/kvm/virtio/blk.c | 31 +++++++++------- tools/kvm/virtio/console.c | 33 ++++++++++------- tools/kvm/virtio/net.c | 34 ++++++++++-------- tools/kvm/virtio/pci.c | 66 ++++++++++++++++++++++----------- tools/kvm/virtio/rng.c | 32 +++++++++------- tools/kvm/virtio/trans.c | 22 +++++++++++ 12 files changed, 228 insertions(+), 127 deletions(-) create mode 100644 tools/kvm/include/kvm/virtio-trans.h create mode 100644 tools/kvm/virtio/trans.c
tools/kvm/Makefile +1 -0
@@ -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
@@ -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 +7 -18
@@ -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,11 @@ 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); + +struct virtio_trans_ops *virtio_pci__get_trans_ops (void );#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 +16 -12
@@ -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); } }
@@ -1240,22 +1240,26 @@ static int get_size_vq(struct kvm *kvm, void *dev, u32 vq)return VIRTQUEUE_NUM ; } +struct virtio_ops p9_dev_virtio_ops = (struct virtio_ops ) { + .set_config = set_config, + .get_config = get_config, + .get_host_features = get_host_features, + .set_guest_features = set_guest_features, + .init_vq = init_vq, + .notify_vq = notify_vq, + .get_pfn_vq = get_pfn_vq, + .get_size_vq = get_size_vq, +}; +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 ) { - .set_config = set_config, - .get_config = get_config, - .get_host_features = get_host_features, - .set_guest_features = set_guest_features, - .init_vq = init_vq, - .notify_vq = notify_vq, - .get_pfn_vq = get_pfn_vq, - .get_size_vq = get_size_vq, - }; +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 = &p9_dev_virtio_ops; }return 0 ;
tools/kvm/virtio/balloon.c +21 -17
@@ -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)
@@ -234,6 +234,17 @@ static int get_size_vq(struct kvm *kvm, void *dev, u32 vq)return VIRTIO_BLN_QUEUE_SIZE ; } +struct virtio_ops bln_dev_virtio_ops = (struct virtio_ops ) { + .set_config = set_config, + .get_config = get_config, + .get_host_features = get_host_features, + .set_guest_features = set_guest_features, + .init_vq = init_vq, + .notify_vq = notify_vq, + .get_pfn_vq = get_pfn_vq, + .get_size_vq = get_size_vq, +}; +void virtio_bln__init (struct kvm *kvm) {kvm_ipc__register_handler (KVM_IPC_BALLOON , handle_mem);
@@ -242,17 +253,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 ) { - .set_config = set_config, - .get_config = get_config, - .get_host_features = get_host_features, - .set_guest_features = set_guest_features, - .init_vq = init_vq, - .notify_vq = notify_vq, - .get_pfn_vq = get_pfn_vq, - .get_size_vq = get_size_vq, - }; +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 = &bln_dev_virtio_ops;if (compat_id !=-1 ) compat_id = compat__add_message("virtio-balloon device was not detected",
tools/kvm/virtio/blk.c +18 -13
@@ -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); }
@@ -217,6 +218,17 @@ static int get_size_vq(struct kvm *kvm, void *dev, u32 vq)return VIRTIO_BLK_QUEUE_SIZE ; } +static struct virtio_ops blk_dev_virtio_ops = (struct virtio_ops ) { + .set_config = set_config, + .get_config = get_config, + .get_host_features = get_host_features, + .set_guest_features = set_guest_features, + .init_vq = init_vq, + .notify_vq = notify_vq, + .get_pfn_vq = get_pfn_vq, + .get_size_vq = get_size_vq, +}; +void virtio_blk__init (struct kvm *kvm,struct disk_image *disk) {struct blk_dev *bdev;
@@ -239,17 +251,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 ) { - .set_config = set_config, - .get_config = get_config, - .get_host_features = get_host_features, - .set_guest_features = set_guest_features, - .init_vq = init_vq, - .notify_vq = notify_vq, - .get_pfn_vq = get_pfn_vq, - .get_size_vq = get_size_vq, - }; +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 = &blk_dev_virtio_ops;list_add_tail (&bdev->list, &bdevs);
tools/kvm/virtio/console.c +19 -14
@@ -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);
@@ -175,19 +175,24 @@ static int get_size_vq(struct kvm *kvm, void *dev, u32 vq)return VIRTIO_CONSOLE_QUEUE_SIZE ; } +static struct virtio_ops con_dev_virtio_ops = (struct virtio_ops ) { + .set_config = set_config, + .get_config = get_config, + .get_host_features = get_host_features, + .set_guest_features = set_guest_features, + .init_vq = init_vq, + .notify_vq = notify_vq, + .get_pfn_vq = get_pfn_vq, + .get_size_vq = get_size_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 ) { - .set_config = set_config, - .get_config = get_config, - .get_host_features = get_host_features, - .set_guest_features = set_guest_features, - .init_vq = init_vq, - .notify_vq = notify_vq, - .get_pfn_vq = get_pfn_vq, - .get_size_vq = get_size_vq, - }; +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 = &con_dev_virtio_ops;if (compat_id !=-1 ) compat_id = compat__add_message("virtio-console device was not detected",
tools/kvm/virtio/net.c +19 -15
@@ -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 );
@@ -359,6 +359,17 @@ static int get_size_vq(struct kvm *kvm, void *dev, u32 vq)return VIRTIO_NET_QUEUE_SIZE ; } +static struct virtio_ops net_dev_virtio_ops = (struct virtio_ops ) { + .set_config = set_config, + .get_config = get_config, + .get_host_features = get_host_features, + .set_guest_features = set_guest_features, + .init_vq = init_vq, + .notify_vq = notify_vq, + .get_pfn_vq = get_pfn_vq, + .get_size_vq = get_size_vq, +}; +void virtio_net__init (const struct virtio_net_params *params) {int i;
@@ -399,17 +410,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 ) { - .set_config = set_config, - .get_config = get_config, - .get_host_features = get_host_features, - .set_guest_features = set_guest_features, - .init_vq = init_vq, - .notify_vq = notify_vq, - .get_pfn_vq = get_pfn_vq, - .get_size_vq = get_size_vq, - }; +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 = &net_dev_virtio_ops;virtio_net__io_thread_init (params->kvm, ndev);
tools/kvm/virtio/pci.c +44 -22
196 lines not on this page · raw message
tools/kvm/virtio/rng.c +18 -14
@@ -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)
@@ -138,6 +138,17 @@ static int get_size_vq(struct kvm *kvm, void *dev, u32 vq)return VIRTIO_RNG_QUEUE_SIZE ; } +static struct virtio_ops rng_dev_virtio_ops = (struct virtio_ops ) { + .set_config = set_config, + .get_config = get_config, + .get_host_features = get_host_features, + .set_guest_features = set_guest_features, + .init_vq = init_vq, + .notify_vq = notify_vq, + .get_pfn_vq = get_pfn_vq, + .get_size_vq = get_size_vq, +}; +void virtio_rng__init (struct kvm *kvm) {struct rng_dev *rdev;
@@ -150,17 +161,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 ) { - .set_config = set_config, - .get_config = get_config, - .get_host_features = get_host_features, - .set_guest_features = set_guest_features, - .init_vq = init_vq, - .notify_vq = notify_vq, - .get_pfn_vq = get_pfn_vq, - .get_size_vq = get_size_vq, - }; +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 = &rng_dev_virtio_ops;list_add_tail (&rdev->list, &rdevs);
tools/kvm/virtio/trans.c new file +22 -0
@@ -0,0 +1,22 @@ +#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 =virtio_pci__get_trans_ops (); +default : +return -1 ; + }; +}
Signature · 1 line
1.7.7.2