]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
kvm tools: Cleanup virtqueue handling
authorPekka Enberg <penberg@kernel.org>
Sun, 23 Jan 2011 19:21:58 +0000 (21:21 +0200)
committerPekka Enberg <penberg@kernel.org>
Sun, 23 Jan 2011 19:21:58 +0000 (21:21 +0200)
This patch introduces some virtqueue handling helper functions and converts the
blk virtio device to use them.

Signed-off-by: Pekka Enberg <penberg@kernel.org>
tools/kvm/blk-virtio.c
tools/kvm/include/kvm/virtqueue.h [new file with mode: 0644]

index 18e085a21afafcb4bb590994a9ba648618ba8408..b79d7457d64ee00e0ea03ee0cf586957aa00a158 100644 (file)
@@ -1,7 +1,9 @@
 #include "kvm/blk-virtio.h"
 
 #include "kvm/virtio_pci.h"
+
 #include "kvm/disk-image.h"
+#include "kvm/virtqueue.h"
 #include "kvm/ioport.h"
 #include "kvm/util.h"
 #include "kvm/kvm.h"
 
 #define VIRTIO_BLK_QUEUE_SIZE  16
 
-struct virt_queue {
-       struct vring                    vring;
-       uint32_t                        pfn;
-       /* The last_avail_idx field is an index to ->ring of struct vring_avail.
-          It's where we assume the next request index is at.  */
-       uint16_t                        last_avail_idx;
-};
-
 struct device {
        struct virtio_blk_config        blk_config;
        uint32_t                        host_features;
@@ -118,12 +112,12 @@ static bool blk_virtio_read(struct kvm *self, struct virt_queue *queue)
        struct vring_used_elem *used_elem;
        struct virtio_blk_outhdr *req;
        struct vring_desc *desc;
-       uint16_t desc_ndx;
        uint32_t block_len;
+       uint16_t desc_ndx;
        uint8_t *status;
        void *block;
 
-       desc_ndx                = queue->vring.avail->ring[queue->last_avail_idx++ % queue->vring.num];
+       desc_ndx                = virt_queue__pop(queue);
 
        if (desc_ndx >= queue->vring.num) {
                warning("fatal I/O error");
@@ -131,20 +125,20 @@ static bool blk_virtio_read(struct kvm *self, struct virt_queue *queue)
        }
 
        /* header */
-       desc                    = &queue->vring.desc[desc_ndx];
+       desc                    = virt_queue__get_desc(queue, desc_ndx);
        assert(!(desc->flags & VRING_DESC_F_INDIRECT));
 
        req                     = guest_flat_to_host(self, desc->addr);
 
        /* block */
-       desc                    = &queue->vring.desc[desc->next];
+       desc                    = virt_queue__get_desc(queue, desc->next);
        assert(!(desc->flags & VRING_DESC_F_INDIRECT));
 
        block                   = guest_flat_to_host(self, desc->addr);
        block_len               = desc->len;
 
        /* status */
-       desc                    = &queue->vring.desc[desc->next];
+       desc                    = virt_queue__get_desc(queue, desc->next);
        assert(!(desc->flags & VRING_DESC_F_INDIRECT));
 
        status                  = guest_flat_to_host(self, desc->addr);
@@ -176,7 +170,7 @@ static bool blk_virtio_read(struct kvm *self, struct virt_queue *queue)
                break;
        }
 
-       used_elem               = &queue->vring.used->ring[queue->vring.used->idx++ % queue->vring.num];
+       used_elem               = virt_queue__get_used_elem(queue);
 
        used_elem->id           = desc_ndx;
        used_elem->len          = 3;
diff --git a/tools/kvm/include/kvm/virtqueue.h b/tools/kvm/include/kvm/virtqueue.h
new file mode 100644 (file)
index 0000000..83f5293
--- /dev/null
@@ -0,0 +1,31 @@
+#ifndef KVM__VIRTQUEUE_H
+#define KVM__VIRTQUEUE_H
+
+#include <linux/virtio_ring.h>
+
+#include <stdint.h>
+
+struct virt_queue {
+       struct vring                    vring;
+       uint32_t                        pfn;
+       /* The last_avail_idx field is an index to ->ring of struct vring_avail.
+          It's where we assume the next request index is at.  */
+       uint16_t                        last_avail_idx;
+};
+
+static inline uint16_t virt_queue__pop(struct virt_queue *queue)
+{
+       return queue->vring.avail->ring[queue->last_avail_idx++ % queue->vring.num];
+}
+
+static inline struct vring_desc *virt_queue__get_desc(struct virt_queue *queue, uint16_t desc_ndx)
+{
+       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];
+}
+
+#endif /* KVM__VIRTQUEUE_H */