kvm tools: change struct device to struct blk_device for block device
PATCH 1/5 kvm tools: change struct device to struct blk_device for block device
--- tools/kvm/blk-virtio.c | 28 ++++++++++++++-------------- 1 files changed, 14 insertions(+), 14 deletions(-)
tools/kvm/blk-virtio.c +14 -14
@@ -20,7 +20,7 @@#define VIRTIO_BLK_QUEUE_SIZE 128 -struct device { +struct blk_device {struct virtio_blk_config blk_config;uint32_t host_features;uint32_t guest_features;
@@ -35,7 +35,7 @@ struct device {#define DISK_SEG_MAX 126 -static struct device device = { +static struct blk_device blk_device = { .blk_config = (struct virtio_blk_config ) {/* VIRTIO_BLK_F_SEG_MAX */ .seg_max =DISK_SEG_MAX ,
@@ -50,7 +50,7 @@ static struct device device = {static bool virtio_blk_config_in (void *data,unsigned long offset,int size,uint32_t count) { -uint8_t *config_space = (uint8_t *) &device.blk_config; +uint8_t *config_space = (uint8_t *) &blk_device.blk_config;if (size !=1 || count !=1 )return false;
@@ -68,12 +68,12 @@ static bool blk_virtio_in(struct kvm *self, uint16_t port, void *data, int size,switch (offset) {case VIRTIO_PCI_HOST_FEATURES : -ioport__write32 (data, device.host_features); +ioport__write32 (data, blk_device.host_features);break ;case VIRTIO_PCI_GUEST_FEATURES :return false;case VIRTIO_PCI_QUEUE_PFN : -ioport__write32 (data, device.virt_queues[device.queue_selector].pfn); +ioport__write32 (data, blk_device.virt_queues[blk_device.queue_selector].pfn);break ;case VIRTIO_PCI_QUEUE_NUM :ioport__write16 (data,VIRTIO_BLK_QUEUE_SIZE );
@@ -82,14 +82,14 @@ static bool blk_virtio_in(struct kvm *self, uint16_t port, void *data, int size,case VIRTIO_PCI_QUEUE_NOTIFY :return false;case VIRTIO_PCI_STATUS : -ioport__write8 (data, device.status); +ioport__write8 (data, blk_device.status);break ;case VIRTIO_PCI_ISR :ioport__write8 (data,0x1 );kvm__irq_line (self,VIRTIO_BLK_IRQ ,0 );break ;case VIRTIO_MSI_CONFIG_VECTOR : -ioport__write16 (data, device.config_vector); +ioport__write16 (data, blk_device.config_vector);break ;default :return virtio_blk_config_in (data, offset, size, count);
@@ -205,13 +205,13 @@ static bool blk_virtio_out(struct kvm *self, uint16_t port, void *data, int sizeswitch (offset) {case VIRTIO_PCI_GUEST_FEATURES : - device.guest_features =ioport__read32 (data); + blk_device.guest_features =ioport__read32 (data);break ;case VIRTIO_PCI_QUEUE_PFN : {struct virt_queue *queue;void *p; - queue = &device.virt_queues[device.queue_selector]; + queue = &blk_device.virt_queues[blk_device.queue_selector]; queue->pfn =ioport__read32 (data);
@@ -222,7 +222,7 @@ static bool blk_virtio_out(struct kvm *self, uint16_t port, void *data, int sizebreak ; }case VIRTIO_PCI_QUEUE_SEL : - device.queue_selector =ioport__read16 (data); + blk_device.queue_selector =ioport__read16 (data);break ;case VIRTIO_PCI_QUEUE_NOTIFY : {struct virt_queue *queue;
@@ -230,7 +230,7 @@ static bool blk_virtio_out(struct kvm *self, uint16_t port, void *data, int size queue_index =ioport__read16 (data); - queue = &device.virt_queues[queue_index]; + queue = &blk_device.virt_queues[queue_index];while (queue->vring.avail->idx != queue->last_avail_idx) {if (!blk_virtio_request (self, queue))
@@ -241,10 +241,10 @@ static bool blk_virtio_out(struct kvm *self, uint16_t port, void *data, int sizebreak ; }case VIRTIO_PCI_STATUS : - device.status =ioport__read8 (data); + blk_device.status =ioport__read8 (data);break ;case VIRTIO_MSI_CONFIG_VECTOR : - device.config_vector =VIRTIO_MSI_NO_VECTOR ; + blk_device.config_vector =VIRTIO_MSI_NO_VECTOR ;break ;case VIRTIO_MSI_QUEUE_VECTOR :break ;
@@ -285,7 +285,7 @@ void blk_virtio__init(struct kvm *self)if (!self->disk_image)return ; - device.blk_config.capacity = self->disk_image->size /SECTOR_SIZE ; + blk_device.blk_config.capacity = self->disk_image->size /SECTOR_SIZE ;pci__register (&blk_virtio_pci_device,PCI_VIRTIO_BLK_DEVNUM );
Signature · 1 line
1.7.4.1
PATCH 2/5 kvm tools: introduces virtio.c and virtio.h
all virito core code should be in virtio.c
--- tools/kvm/Makefile | 1 + tools/kvm/blk-virtio.c | 2 +- tools/kvm/console-virtio.c | 2 +- tools/kvm/include/kvm/virtio.h | 44 ++++++++++++++++++++++ tools/kvm/include/kvm/virtqueue.h | 73 ------------------------------------- tools/kvm/virtio.c | 39 ++++++++++++++++++++ 6 files changed, 86 insertions(+), 75 deletions(-) create mode 100644 tools/kvm/include/kvm/virtio.h delete mode 100644 tools/kvm/include/kvm/virtqueue.h create mode 100644 tools/kvm/virtio.c
tools/kvm/Makefile +1 -0
@@ -26,6 +26,7 @@ OBJS += mmio.o OBJS += pci.o OBJS += util.o OBJS += term.o +OBJS += virtio.o DEPS := $(patsubst %.o,%.d,$(OBJS))
tools/kvm/blk-virtio.c +1 -1
@@ -3,7 +3,7 @@#include "kvm/virtio_pci.h" #include "kvm/disk-image.h" -#include "kvm/virtqueue.h" +#include "kvm/virtio.h" #include "kvm/ioport.h" #include "kvm/util.h" #include "kvm/kvm.h"
tools/kvm/console-virtio.c +1 -1
@@ -1,7 +1,7 @@#include "kvm/console-virtio.h" #include "kvm/virtio_pci.h" #include "kvm/disk-image.h" -#include "kvm/virtqueue.h" +#include "kvm/virtio.h" #include "kvm/ioport.h" #include "kvm/util.h" #include "kvm/term.h"
tools/kvm/include/kvm/virtio.h new file +44 -0
44 lines not on this page · raw message
tools/kvm/include/kvm/virtqueue.h deleted +0 -73
73 lines not on this page · raw message
tools/kvm/virtio.c new file +39 -0
@@ -0,0 +1,39 @@ +#include <linux/virtio_ring.h> +#include <stdint.h> +#include <sys/uio.h> +#include "kvm/kvm.h" +#include "kvm/virtio.h" + +struct vring_used_elem *virt_queue__set_used_elem (struct virt_queue *queue,uint32_t head,uint32_t len) +{ +struct vring_used_elem *used_elem; + used_elem = &queue->vring.used->ring[queue->vring.used->idx++ % queue->vring.num]; + used_elem->id = head; + used_elem->len = len; +return used_elem; +} + +uint16_t virt_queue__get_iov (struct virt_queue *queue,struct iovec iov[],uint16_t *out,uint16_t *in,struct kvm *kvm) +{ +struct vring_desc *desc; +uint16_t head, idx; + + idx = head =virt_queue__pop (queue); + *out = *in =0 ; + +do { + desc =virt_queue__get_desc (queue, idx); + iov[*out + *in].iov_base =guest_flat_to_host (kvm, desc->addr); + iov[*out + *in].iov_len = desc->len; +if (desc->flags &VRING_DESC_F_WRITE ) + (*in)++; +else + (*out)++; +if (desc->flags &VRING_DESC_F_NEXT ) + idx = desc->next; +else +break ; + }while (1 ); + +return head; +}
Signature · 1 line
1.7.4.1
PATCH 3/5 kvm tools: this patch unifies the name conventions to virtio-xxx
In reply to Asias He- rename {blk,console}-virtio.* to virtio-{blk,console}.*
- change the virtio block device PCI io space operation callback names
--- tools/kvm/Makefile | 4 +- tools/kvm/blk-virtio.c | 293 -------------------------------- tools/kvm/console-virtio.c | 218 ------------------------ tools/kvm/include/kvm/blk-virtio.h | 8 - tools/kvm/include/kvm/console-virtio.h | 9 - tools/kvm/include/kvm/virtio-blk.h | 8 + tools/kvm/include/kvm/virtio-console.h | 9 + tools/kvm/include/kvm/virtio-pci.h | 59 +++++++ tools/kvm/include/kvm/virtio_pci.h | 59 ------- tools/kvm/main.c | 6 +- tools/kvm/virtio-blk.c | 293 ++++++++++++++++++++++++++++++++ tools/kvm/virtio-console.c | 218 ++++++++++++++++++++++++ 12 files changed, 592 insertions(+), 592 deletions(-) delete mode 100644 tools/kvm/blk-virtio.c delete mode 100644 tools/kvm/console-virtio.c delete mode 100644 tools/kvm/include/kvm/blk-virtio.h delete mode 100644 tools/kvm/include/kvm/console-virtio.h create mode 100644 tools/kvm/include/kvm/virtio-blk.h create mode 100644 tools/kvm/include/kvm/virtio-console.h create mode 100644 tools/kvm/include/kvm/virtio-pci.h delete mode 100644 tools/kvm/include/kvm/virtio_pci.h create mode 100644 tools/kvm/virtio-blk.c create mode 100644 tools/kvm/virtio-console.c
tools/kvm/Makefile +2 -2
@@ -13,8 +13,8 @@ CSCOPE = cscope TAGS = ctags OBJS += 8250-serial.o -OBJS += blk-virtio.o -OBJS += console-virtio.o +OBJS += virtio-blk.o +OBJS += virtio-console.o OBJS += cpuid.o OBJS += read-write.o OBJS += disk-image.o
tools/kvm/blk-virtio.c deleted +0 -293
293 lines not on this page · raw message
tools/kvm/console-virtio.c deleted +0 -218
218 lines not on this page · raw message
tools/kvm/include/kvm/blk-virtio.h deleted +0 -8
@@ -1,8 +0,0 @@ -#ifndef KVM__BLK_VIRTIO_H -#define KVM__BLK_VIRTIO_H - -struct kvm ; - -void blk_virtio__init (struct kvm *self); - -#endif /* KVM__BLK_VIRTIO_H */
tools/kvm/include/kvm/console-virtio.h deleted +0 -9
@@ -1,9 +0,0 @@ -#ifndef KVM__CONSOLE_VIRTIO_H -#define KVM__CONSOLE_VIRTIO_H - -struct kvm ; - -void virtio_console__init (struct kvm *self); -void virtio_console__inject_interrupt (struct kvm *self); - -#endif /* KVM__CONSOLE_VIRTIO_H */
tools/kvm/include/kvm/virtio-blk.h new file +8 -0
@@ -0,0 +1,8 @@ +#ifndef KVM__BLK_VIRTIO_H +#define KVM__BLK_VIRTIO_H + +struct kvm ; + +void virtio_blk__init (struct kvm *self); + +#endif /* KVM__BLK_VIRTIO_H */
tools/kvm/include/kvm/virtio-console.h new file +9 -0
@@ -0,0 +1,9 @@ +#ifndef KVM__CONSOLE_VIRTIO_H +#define KVM__CONSOLE_VIRTIO_H + +struct kvm ; + +void virtio_console__init (struct kvm *self); +void virtio_console__inject_interrupt (struct kvm *self); + +#endif /* KVM__CONSOLE_VIRTIO_H */
tools/kvm/include/kvm/virtio-pci.h new file +59 -0
59 lines not on this page · raw message
tools/kvm/include/kvm/virtio_pci.h deleted +0 -59
59 lines not on this page · raw message
tools/kvm/main.c +3 -3
@@ -1,8 +1,8 @@#include "kvm/kvm.h" #include "kvm/8250-serial.h" -#include "kvm/blk-virtio.h" -#include "kvm/console-virtio.h" +#include "kvm/virtio-blk.h" +#include "kvm/virtio-console.h" #include "kvm/disk-image.h" #include "kvm/util.h" #include "kvm/pci.h"
@@ -145,7 +145,7 @@ int main(int argc, char *argv[])pci__init (); -blk_virtio__init (kvm); +virtio_blk__init (kvm);virtio_console__init (kvm);
tools/kvm/virtio-blk.c new file +293 -0
293 lines not on this page · raw message
tools/kvm/virtio-console.c new file +218 -0
218 lines not on this page · raw message
Signature · 1 line
1.7.4.1
PATCH 4/5 kvm tools: drop virt_queue__get_used_elem virtio helper
In reply to Asias HeUse virt_queue__set_used_elem insted.
--- tools/kvm/include/kvm/virtio.h | 6 ------ tools/kvm/virtio-blk.c | 5 +---- 2 files changed, 1 insertions(+), 10 deletions(-)
tools/kvm/include/kvm/virtio.h +0 -6
@@ -26,12 +26,6 @@ static inline struct vring_desc *virt_queue__get_desc(struct virt_queue *queue,return &queue->vring.desc[desc_ndx]; } -static inline struct vring_used_elem *virt_queue__get_used_elem (struct virt_queue *queue) -{ -return &queue->vring.used->ring[queue->vring.used->idx++ % queue->vring.num]; -} - -static inline bool virt_queue__available (struct virt_queue *vq) {return vq->vring.avail->idx != vq->last_avail_idx;
tools/kvm/virtio-blk.c +1 -4
@@ -100,7 +100,6 @@ static bool virtio_blk_pci_io_in(struct kvm *self, uint16_t port, void *data, instatic bool virtio_blk_request (struct kvm *self,struct virt_queue *queue) { -struct vring_used_elem *used_elem;struct virtio_blk_outhdr *req;uint16_t desc_block_last;struct vring_desc *desc;
@@ -190,9 +189,7 @@ static bool virtio_blk_request(struct kvm *self, struct virt_queue *queue) *status = err_cnt ?VIRTIO_BLK_S_IOERR :VIRTIO_BLK_S_OK ; - used_elem =virt_queue__get_used_elem (queue); - used_elem->id = desc_hdr; - used_elem->len = block_cnt; +virt_queue__set_used_elem (queue, desc_hdr, block_cnt);return true; }
Signature · 1 line
1.7.4.1
PATCH 5/5 kvm tools: use virt_queue__get_iov to simpify virtio blk io handle logic
In reply to Asias HeThis really makes my life much easier!
--- tools/kvm/virtio-blk.c | 109 ++++++++++++++--------------------------------- 1 files changed, 33 insertions(+), 76 deletions(-)
tools/kvm/virtio-blk.c +33 -76
@@ -30,7 +30,7 @@ struct blk_device {/* virtio queue */ uint16_t queue_selector; -struct virt_queue virt_queues[NUM_VIRT_QUEUES ]; +struct virt_queue vqs[NUM_VIRT_QUEUES ]; };#define DISK_SEG_MAX 126
@@ -73,7 +73,7 @@ static bool virtio_blk_pci_io_in(struct kvm *self, uint16_t port, void *data, incase VIRTIO_PCI_GUEST_FEATURES :return false;case VIRTIO_PCI_QUEUE_PFN : -ioport__write32 (data, blk_device.virt_queues[blk_device.queue_selector].pfn); +ioport__write32 (data, blk_device.vqs[blk_device.queue_selector].pfn);break ;case VIRTIO_PCI_QUEUE_NUM :ioport__write16 (data,VIRTIO_BLK_QUEUE_SIZE );
@@ -98,66 +98,29 @@ static bool virtio_blk_pci_io_in(struct kvm *self, uint16_t port, void *data, inreturn true; } -static bool virtio_blk_request (struct kvm *self,struct virt_queue *queue) +static bool virtio_blk_do_io_request (struct kvm *self,struct virt_queue *queue) { + +struct iovec iov[VIRTIO_BLK_QUEUE_SIZE ];struct virtio_blk_outhdr *req; -uint16_t desc_block_last; -struct vring_desc *desc; -uint16_t desc_status; -uint16_t desc_block; -uint32_t block_len; -uint32_t block_cnt; -uint16_t desc_hdr; +uint32_t block_len, block_cnt; +uint16_t out, in, head; +int err,err_cnt, i;uint8_t *status;void *block; -int err; -int err_cnt; - -/* header */ - desc_hdr =virt_queue__pop (queue); - -if (desc_hdr >= queue->vring.num) { -warning ("fatal I/O error" ); -return false; - } - desc =virt_queue__get_desc (queue, desc_hdr); -assert (!(desc->flags &VRING_DESC_F_INDIRECT )); + head =virt_queue__get_iov (queue, iov, &out, &in, self); - req =guest_flat_to_host (self, desc->addr); - -/* status */ - desc_status = desc_hdr; - -do { - desc_block_last = desc_status; - desc_status =virt_queue__get_desc (queue, desc_status)->next; - -if (desc_status >= queue->vring.num) { -warning ("fatal I/O error" ); -return false; - } - - desc =virt_queue__get_desc (queue, desc_status); -assert (!(desc->flags &VRING_DESC_F_INDIRECT )); - - }while (desc->flags &VRING_DESC_F_NEXT ); - - status =guest_flat_to_host (self, desc->addr); +/* head */ + req = iov[0 ].iov_base;/* block */ - desc_block = desc_hdr; - block_cnt =0 ; - err_cnt =0 ; + block_cnt =0 ; + err_cnt =0 ; - do { - desc_block = virt_queue__get_desc(queue, desc_block)->next; - - desc = virt_queue__get_desc(queue, desc_block); - assert(!(desc->flags & VRING_DESC_F_INDIRECT)); - - block = guest_flat_to_host(self, desc->addr); - block_len = desc->len; +for (i =1 ; i < out + in -1 ; i++) { + block = iov[i].iov_base; + block_len = iov[i].iov_len;switch (req->type) {case VIRTIO_BLK_T_IN :
@@ -176,24 +139,28 @@ static bool virtio_blk_request(struct kvm *self, struct virt_queue *queue) req->sector += block_len >>SECTOR_SHIFT ; block_cnt += block_len; + } -if (desc_block == desc_block_last) -break ; - -if (desc_block >= queue->vring.num) { -warning ("fatal I/O error" ); -return false; - } - - }while (true); - +/* status */ + status = iov[out + in -1 ].iov_base; *status = err_cnt ?VIRTIO_BLK_S_IOERR :VIRTIO_BLK_S_OK ; -virt_queue__set_used_elem (queue, desc_hdr, block_cnt); +virt_queue__set_used_elem (queue, head, block_cnt);return true; } +static void virtio_blk_handle_callback (struct kvm *self,uint16_t queue_index) +{ +struct virt_queue *vq; + + vq = &blk_device.vqs[queue_index]; +while (virt_queue__available (vq)) { +virtio_blk_do_io_request (self, vq); + } +kvm__irq_line (self,VIRTIO_BLK_IRQ ,1 ); + +}static bool virtio_blk_pci_io_out (struct kvm *self,uint16_t port,void *data,int size,uint32_t count) {unsigned long offset;
@@ -208,7 +175,7 @@ static bool virtio_blk_pci_io_out(struct kvm *self, uint16_t port, void *data, istruct virt_queue *queue;void *p; - queue = &blk_device.virt_queues[blk_device.queue_selector]; + queue = &blk_device.vqs[blk_device.queue_selector]; queue->pfn =ioport__read32 (data);
@@ -222,19 +189,9 @@ static bool virtio_blk_pci_io_out(struct kvm *self, uint16_t port, void *data, i blk_device.queue_selector =ioport__read16 (data);break ;case VIRTIO_PCI_QUEUE_NOTIFY : { -struct virt_queue *queue;uint16_t queue_index; - queue_index =ioport__read16 (data); - - queue = &blk_device.virt_queues[queue_index]; - -while (queue->vring.avail->idx != queue->last_avail_idx) { -if (!virtio_blk_request (self, queue)) -return false; - } -kvm__irq_line (self,VIRTIO_BLK_IRQ ,1 ); - +virtio_blk_handle_callback (self, queue_index);break ; }case VIRTIO_PCI_STATUS :
Signature · 1 line
1.7.4.1