From: Anthony Liguori <anthony@codemonkey.ws>
To: "Michael S. Tsirkin" <mst@redhat.com>
Cc: qemu-devel@nongnu.org
Subject: [Qemu-devel] Re: [PATCH-RFC 11/13] vhost net support
Date: Tue, 12 Jan 2010 16:45:08 -0600 [thread overview]
Message-ID: <4B4CFB74.30002@codemonkey.ws> (raw)
In-Reply-To: <20100111172332.GC12084@redhat.com>
On 01/11/2010 11:23 AM, Michael S. Tsirkin wrote:
> This adds vhost net support in qemu. Will be tied to tap device and
> virtio later. Raw backend is currently missing, will be worked
> on/submitted separately.
>
> Signed-off-by: Michael S. Tsirkin<mst@redhat.com>
>
Hit send too quick.. here it is.
> ---
> Makefile.target | 1 +
> hw/vhost.c | 349 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
> hw/vhost.h | 33 +++++
> hw/vhost_net.c | 145 +++++++++++++++++++++++
> hw/vhost_net.h | 20 +++
> 5 files changed, 548 insertions(+), 0 deletions(-)
> create mode 100644 hw/vhost.c
> create mode 100644 hw/vhost.h
> create mode 100644 hw/vhost_net.c
> create mode 100644 hw/vhost_net.h
>
> diff --git a/Makefile.target b/Makefile.target
> index 7c1f30c..61b7148 100644
> --- a/Makefile.target
> +++ b/Makefile.target
> @@ -157,6 +157,7 @@ obj-y = vl.o async.o monitor.o pci.o pci_host.o pcie_host.o machine.o gdbstub.o
> # virtio has to be here due to weird dependency between PCI and virtio-net.
> # need to fix this properly
> obj-y += virtio-blk.o virtio-balloon.o virtio-net.o virtio-console.o virtio-pci.o
> +obj-y += vhost_net.o vhost.o
> obj-$(CONFIG_KVM) += kvm.o kvm-all.o
> obj-$(CONFIG_ISA_MMIO) += isa_mmio.o
> LIBS+=-lz
> diff --git a/hw/vhost.c b/hw/vhost.c
> new file mode 100644
> index 0000000..d23d94c
> --- /dev/null
> +++ b/hw/vhost.c
> @@ -0,0 +1,349 @@
> +#include "linux/vhost.h"
> +#include<sys/ioctl.h>
> +#include<sys/eventfd.h>
> +#include "vhost.h"
> +#include "hw/hw.h"
> +/* For range_get_last */
> +#include "pci.h"
> +
> +static void vhost_dev_unassign_memory(struct vhost_dev *dev,
> + struct vhost_memory *mem,
> + uint64_t start_addr,
> + uint64_t size)
> +{
> + int from, to;
> + for (from = 0, to = 0; from< dev->mem->nregions; ++from, ++to) {
> + struct vhost_memory_region *reg = mem->regions + to;
> + uint64_t reglast;
> + uint64_t memlast;
> + uint64_t change;
> +
> + /* clone old region */
> + memcpy(reg, dev->mem->regions + from, sizeof *reg);
> +
> + /* No overlap is simple */
> + if (!ranges_overlap(reg->guest_phys_addr, reg->memory_size,
> + start_addr, size)) {
> + continue;
> + }
> + reglast = range_get_last(reg->guest_phys_addr, reg->memory_size);
> + memlast = range_get_last(start_addr, size);
> +
> + /* Remove whole region */
> + if (start_addr<= reg->guest_phys_addr&& memlast>= reglast) {
> + --to;
> + continue;
> + }
> +
> + /* Shrink region */
> + if (memlast>= reglast) {
> + reg->memory_size = start_addr - reg->guest_phys_addr;
> + continue;
> + }
> +
> + /* Shift region */
> + if (start_addr<= reg->guest_phys_addr) {
> + change = memlast + 1 - reg->guest_phys_addr;
> + reg->memory_size -= change;
> + reg->guest_phys_addr += change;
> + reg->userspace_addr += change;
> + continue;
> + }
> +
> + /* Split region: shrink first part, shift second part. */
> + memcpy(reg + 1, reg, sizeof *reg);
> + reg[0].memory_size = start_addr - reg->guest_phys_addr;
> + change = memlast + 1 - reg->guest_phys_addr;
> + reg[1].memory_size -= change;
> + reg[1].guest_phys_addr += change;
> + reg[1].userspace_addr += change;
> + ++to;
> + }
> + mem->nregions = to;
> +}
> +
> +/* Called after unassign, so no regions overlap the given range. */
> +static void vhost_dev_assign_memory(struct vhost_dev *dev,
> + struct vhost_memory *mem,
> + uint64_t start_addr,
> + uint64_t size,
> + uint64_t uaddr)
> +{
> + int from, to;
> + struct vhost_memory_region *merged = NULL;
> + for (from = 0, to = 0; from< dev->mem->nregions; ++from, ++to) {
> + struct vhost_memory_region *reg = mem->regions + to;
> + uint64_t prlast, urlast;
> + uint64_t pmlast, umlast;
> + uint64_t s, e, u;
> +
> + /* clone old region */
> + memcpy(reg, dev->mem->regions + from, sizeof *reg);
> + prlast = range_get_last(reg->guest_phys_addr, reg->memory_size);
> + pmlast = range_get_last(start_addr, size);
> + urlast = range_get_last(reg->userspace_addr, reg->memory_size);
> + umlast = range_get_last(uaddr, size);
> +
> + /* Not an adjecent region - do not merge. */
> + if ((prlast + 1 != start_addr || urlast + 1 != uaddr)&&
> + (pmlast + 1 != reg->guest_phys_addr ||
> + umlast + 1 != reg->userspace_addr)) {
> + continue;
> + }
> +
> + if (!merged) {
> + --to;
> + } else {
> + merged = reg;
> + }
> + u = MIN(uaddr, reg->userspace_addr);
> + s = MIN(start_addr, reg->guest_phys_addr);
> + e = MAX(pmlast, prlast);
> + uaddr = merged->userspace_addr = u;
> + start_addr = merged->guest_phys_addr = s;
> + size = merged->memory_size = e - s + 1;
> + }
> +
> + if (!merged) {
> + struct vhost_memory_region *reg = mem->regions + to;
> + reg->memory_size = size;
> + reg->guest_phys_addr = start_addr;
> + reg->userspace_addr = uaddr;
> + ++to;
> + }
> + mem->nregions = to;
> +}
> +
> +static void vhost_client_set_memory(CPUPhysMemoryClient *client,
> + target_phys_addr_t start_addr,
> + ram_addr_t size,
> + ram_addr_t phys_offset)
> +{
> + struct vhost_dev *dev = container_of(client, struct vhost_dev, client);
> + ram_addr_t flags = phys_offset& ~TARGET_PAGE_MASK;
> + int s = offsetof(struct vhost_memory, regions) +
> + (dev->mem->nregions + 1)* sizeof dev->mem->regions[0];
> + struct vhost_memory *mem = qemu_malloc(s);
> + memcpy(mem, dev->mem, s);
> +
> + /* First, remove old mapping for this memory, if any. */
> + vhost_dev_unassign_memory(dev, mem, start_addr, size);
> + if (flags == IO_MEM_RAM) {
> + /* Add given mapping, merging adjacent regions if any */
> + vhost_dev_assign_memory(dev, mem, start_addr, size,
> + (uintptr_t)qemu_get_ram_ptr(phys_offset));
> + }
> + qemu_free(dev->mem);
> + dev->mem = mem;
> +}
>
This is gnarly and basically duplicated with kvm-all.c. Dunno if code
sharing is practical though.diff --git a/hw/vhost.h b/hw/vhost.h
> new file mode 100644
> index 0000000..9f82b42
> --- /dev/null
> +++ b/hw/vhost.h
> @@ -0,0 +1,33 @@
> +#ifndef VHOST_H
> +#define VHOST_H
> +
> +#include "hw/hw.h"
> +#include "hw/virtio.h"
> +
> +/* Generic structures common for any vhost based device. */
> +struct vhost_virtqueue {
> + int kick;
> + int call;
> + void *desc;
> + void *avail;
> + void *used;
> +};
>
Please follow CodingStyle.
Regards,
Anthony Liguori
prev parent reply other threads:[~2010-01-12 22:45 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <cover.1263230079.git.mst@redhat.com>
2010-01-11 17:16 ` [Qemu-devel] [PATCH-RFC 01/13] virtio: export virtqueue structure Michael S. Tsirkin
2010-01-12 22:32 ` [Qemu-devel] " Anthony Liguori
2010-01-25 19:10 ` Michael S. Tsirkin
2010-01-25 19:23 ` Anthony Liguori
2010-01-11 17:17 ` [Qemu-devel] [PATCH-RFC 02/13] kvm: add API to set ioeventfd Michael S. Tsirkin
2010-01-12 22:35 ` [Qemu-devel] " Anthony Liguori
2010-01-25 19:20 ` Michael S. Tsirkin
2010-01-25 19:28 ` Anthony Liguori
2010-01-25 19:28 ` Michael S. Tsirkin
2010-01-25 19:44 ` Anthony Liguori
2010-01-11 17:17 ` [Qemu-devel] [PATCH-RFC 03/13] virtio: add iofd/irqfd support Michael S. Tsirkin
2010-01-12 22:36 ` [Qemu-devel] " Anthony Liguori
2010-01-13 10:50 ` Michael S. Tsirkin
2010-01-11 17:17 ` [Qemu-devel] [PATCH-RFC 04/13] virtio-pci: fill in irqfd/queufd support Michael S. Tsirkin
2010-01-11 17:19 ` [Qemu-devel] [PATCH-RFC 05/13] syborg_virtio: add irqfd/eventfd support Michael S. Tsirkin
2010-01-11 17:20 ` [Qemu-devel] [PATCH-RFC 06/13] s390-virtio: fill in irqfd support Michael S. Tsirkin
2010-01-11 17:20 ` [Qemu-devel] [PATCH-RFC 07/13] virtio: move typedef to qemu-common Michael S. Tsirkin
2010-01-11 17:20 ` [Qemu-devel] [PATCH-RFC 08/13] net/tap: add interface to get device fd Michael S. Tsirkin
2010-01-11 17:22 ` [Qemu-devel] [PATCH-RFC 09/13] tap: add vhost/vhostfd options Michael S. Tsirkin
2010-01-12 22:39 ` [Qemu-devel] " Anthony Liguori
2010-01-13 10:59 ` Michael S. Tsirkin
2010-01-12 22:42 ` Anthony Liguori
2010-01-11 17:22 ` [Qemu-devel] [PATCH-RFC 10/13] tap: add API to retrieve vhost net header Michael S. Tsirkin
2010-01-11 17:23 ` [Qemu-devel] [PATCH-RFC 12/13] virtio: add status change callback Michael S. Tsirkin
2010-01-11 17:23 ` [Qemu-devel] [PATCH-RFC 13/13] virtio-net: connect to vhost net backend Michael S. Tsirkin
2010-01-25 19:58 ` [Qemu-devel] " Anthony Liguori
2010-01-25 20:27 ` Michael S. Tsirkin
2010-01-25 21:00 ` Anthony Liguori
2010-01-25 21:01 ` Michael S. Tsirkin
2010-01-25 21:05 ` Michael S. Tsirkin
2010-01-25 21:11 ` Anthony Liguori
2010-02-24 3:14 ` Paul Brook
2010-02-24 5:29 ` Michael S. Tsirkin
2010-02-24 11:30 ` Paul Brook
2010-02-24 11:46 ` Michael S. Tsirkin
2010-02-24 12:26 ` Paul Brook
2010-02-24 12:40 ` Michael S. Tsirkin
2010-02-24 15:16 ` Anthony Liguori
2010-02-24 14:51 ` Anthony Liguori
2010-01-11 17:23 ` [Qemu-devel] [PATCH-RFC 11/13] vhost net support Michael S. Tsirkin
2010-01-12 22:45 ` Anthony Liguori [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4B4CFB74.30002@codemonkey.ws \
--to=anthony@codemonkey.ws \
--cc=mst@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.