]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
kvm tools: Add helper functions for virtqueue code
authorAsias He <asias.hejun@gmail.com>
Fri, 8 Apr 2011 14:12:49 +0000 (22:12 +0800)
committerPekka Enberg <penberg@kernel.org>
Fri, 8 Apr 2011 11:53:25 +0000 (14:53 +0300)
Signed-off-by: Asias He <asias.hejun@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
tools/kvm/include/kvm/virtqueue.h

index 83f5293d8f08e69186fe7ce9e286768c1d08d2e3..de360aee9a2cc3efa0f4849834f7b53f9bbcc4bf 100644 (file)
@@ -4,6 +4,9 @@
 #include <linux/virtio_ring.h>
 
 #include <stdint.h>
+#include <sys/uio.h>
+
+#include "kvm/kvm.h"
 
 struct virt_queue {
        struct vring                    vring;
@@ -28,4 +31,43 @@ static inline struct vring_used_elem *virt_queue__get_used_elem(struct virt_queu
        return &queue->vring.used->ring[queue->vring.used->idx++ % queue->vring.num];
 }
 
+static inline 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;
+}
+
+static inline bool virt_queue__available(struct virt_queue *vq)
+{
+       return vq->vring.avail->idx !=  vq->last_avail_idx;
+}
+
+static inline 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;
+}
+
 #endif /* KVM__VIRTQUEUE_H */