From patchwork Fri Apr 8 16:10:04 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asias He X-Patchwork-Id: 695201 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id p38GCBji005370 for ; Fri, 8 Apr 2011 16:12:11 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757506Ab1DHQMG (ORCPT ); Fri, 8 Apr 2011 12:12:06 -0400 Received: from mail-pw0-f46.google.com ([209.85.160.46]:38933 "EHLO mail-pw0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757406Ab1DHQMF (ORCPT ); Fri, 8 Apr 2011 12:12:05 -0400 Received: by pwi15 with SMTP id 15so1299294pwi.19 for ; Fri, 08 Apr 2011 09:12:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:to:cc:subject:date:message-id:x-mailer :in-reply-to:references; bh=RqywnYBiAMU4l3ufoDiJDZ/3+L68kAjguaMRsZG4+QE=; b=bpt3N6WCDUvlXuxQKGVcalCMtLNWBXjzz6Topb65xfZcswPmwFAxdKHAUDjj5hzUXY EGKltUIcAnBtm5AjLGLThnl7VvsVGqrMlHCocZLLSjOSx885a/YG4oAEpI2aWWYsVMPe VaIR/QFvjkpQ7tgR0hJRfWVeyEukv5087de9o= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; b=FwC9W1YpSGqXexujPV1PvTtJy5rkENHlEErlCwgSoJ5bV9gl9mTJkTsrb/eMQje/VK sTTkTwkswTrAjiWsh8kG28Qve1tn4A84uCyN6Gt9f42NHFYuov/xheV1+WjQQWe8/2Ja ONimpeuWR4LJrqMByahfe4OGWuCYBd+QIRV3Y= Received: by 10.142.250.42 with SMTP id x42mr2010558wfh.95.1302279102657; Fri, 08 Apr 2011 09:11:42 -0700 (PDT) Received: from localhost.localdomain ([219.224.169.130]) by mx.google.com with ESMTPS id s39sm3812390wfc.16.2011.04.08.09.11.39 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 08 Apr 2011 09:11:41 -0700 (PDT) From: Asias He To: Pekka Enberg , Cyrill Gorcunov , Ingo Molnar Cc: kvm@vger.kernel.org, Asias He Subject: [PATCH 2/5] kvm tools: introduces virtio.c and virtio.h Date: Sat, 9 Apr 2011 00:10:04 +0800 Message-Id: <1302279007-15710-2-git-send-email-asias.hejun@gmail.com> X-Mailer: git-send-email 1.7.4.1 In-Reply-To: <1302279007-15710-1-git-send-email-asias.hejun@gmail.com> References: <1302279007-15710-1-git-send-email-asias.hejun@gmail.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter1.kernel.org [140.211.167.41]); Fri, 08 Apr 2011 16:12:11 +0000 (UTC) all virito core code should be in virtio.c Signed-off-by: Asias He --- 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 + +#include +#include + +#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 - -#include -#include - -#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 +#include +#include +#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; +}