all virito core code should be in virtio.c Signed-off-by: Asias He <asias.hejun@xxxxxxxxx> --- 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 diff --git a/tools/kvm/Makefile b/tools/kvm/Makefile index 55f342d..82d41b6 100644 --- a/tools/kvm/Makefile +++ b/tools/kvm/Makefile @@ -26,6 +26,7 @@ OBJS += mmio.o OBJS += pci.o OBJS += util.o OBJS += term.o +OBJS += virtio.o DEPS := $(patsubst %.o,%.d,$(OBJS)) diff --git a/tools/kvm/blk-virtio.c b/tools/kvm/blk-virtio.c index e94d7b3..2eb9734 100644 --- a/tools/kvm/blk-virtio.c +++ b/tools/kvm/blk-virtio.c @@ -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" diff --git a/tools/kvm/console-virtio.c b/tools/kvm/console-virtio.c index f094610..3926536 100644 --- a/tools/kvm/console-virtio.c +++ b/tools/kvm/console-virtio.c @@ -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" diff --git a/tools/kvm/include/kvm/virtio.h b/tools/kvm/include/kvm/virtio.h new file mode 100644 index 0000000..075387d --- /dev/null +++ b/tools/kvm/include/kvm/virtio.h @@ -0,0 +1,44 @@ +#ifndef KVM__VIRTIO_H +#define KVM__VIRTIO_H + +#include <linux/virtio_ring.h> + +#include <stdint.h> +#include <sys/uio.h> + +#include "kvm/kvm.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]; +} + + +static inline bool virt_queue__available(struct virt_queue *vq) +{ + return vq->vring.avail->idx != vq->last_avail_idx; +} + +struct vring_used_elem *virt_queue__set_used_elem(struct virt_queue *queue, uint32_t head, uint32_t len); + +uint16_t virt_queue__get_iov(struct virt_queue *queue, struct iovec iov[], uint16_t *out, uint16_t *in, struct kvm *kvm); + +#endif /* KVM__VIRTIO_H */ diff --git a/tools/kvm/include/kvm/virtqueue.h b/tools/kvm/include/kvm/virtqueue.h deleted file mode 100644 index de360ae..0000000 --- a/tools/kvm/include/kvm/virtqueue.h +++ /dev/null @@ -1,73 +0,0 @@ -#ifndef KVM__VIRTQUEUE_H -#define KVM__VIRTQUEUE_H - -#include <linux/virtio_ring.h> - -#include <stdint.h> -#include <sys/uio.h> - -#include "kvm/kvm.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]; -} - -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 */ diff --git a/tools/kvm/virtio.c b/tools/kvm/virtio.c new file mode 100644 index 0000000..a658519 --- /dev/null +++ b/tools/kvm/virtio.c @@ -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; +} -- 1.7.4.1 -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html