]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/qemu-xen.git/commitdiff
ivshmem: Store file descriptor for vhost-user negotiation
authorTetsuya Mukawa <mukawa@igel.co.jp>
Mon, 21 Dec 2015 03:47:34 +0000 (12:47 +0900)
committerMichael S. Tsirkin <mst@redhat.com>
Sat, 9 Jan 2016 21:20:20 +0000 (23:20 +0200)
If virtio-net driver allocates memory in ivshmem shared memory,
vhost-net will work correctly, but vhost-user will not work because
a fd of shared memory will not be sent to vhost-user backend.
This patch fixes ivshmem to store file descriptor of shared memory.
It will be used when vhost-user negotiates vhost-user backend.

Signed-off-by: Tetsuya Mukawa <mukawa@igel.co.jp>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
exec.c
hw/misc/ivshmem.c
include/exec/ram_addr.h

diff --git a/exec.c b/exec.c
index 8718a75b64a5c9147f9ef699e6769ad3f4c6a9a3..7f0ce42af0c70772d784f0976d3452b1cb7689fe 100644 (file)
--- a/exec.c
+++ b/exec.c
@@ -1757,6 +1757,16 @@ int qemu_get_ram_fd(ram_addr_t addr)
     return fd;
 }
 
+void qemu_set_ram_fd(ram_addr_t addr, int fd)
+{
+    RAMBlock *block;
+
+    rcu_read_lock();
+    block = qemu_get_ram_block(addr);
+    block->fd = fd;
+    rcu_read_unlock();
+}
+
 void *qemu_get_ram_block_host_ptr(ram_addr_t addr)
 {
     RAMBlock *block;
index f73f0c2b17b09d006c026eb5eb5b0a66e3c1e55d..df585de10f72a36a919e42862e50761314386ca5 100644 (file)
@@ -29,6 +29,7 @@
 #include "sysemu/char.h"
 #include "sysemu/hostmem.h"
 #include "qapi/visitor.h"
+#include "exec/ram_addr.h"
 
 #include "hw/misc/ivshmem.h"
 
@@ -422,6 +423,7 @@ static int create_shared_memory_BAR(IVShmemState *s, int fd, uint8_t attr,
 
     memory_region_init_ram_ptr(&s->ivshmem, OBJECT(s), "ivshmem.bar2",
                                s->ivshmem_size, ptr);
+    qemu_set_ram_fd(s->ivshmem.ram_addr, fd);
     vmstate_register_ram(&s->ivshmem, DEVICE(s));
     memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
 
@@ -682,6 +684,7 @@ static void ivshmem_read(void *opaque, const uint8_t *buf, int size)
         }
         memory_region_init_ram_ptr(&s->ivshmem, OBJECT(s),
                                    "ivshmem.bar2", s->ivshmem_size, map_ptr);
+        qemu_set_ram_fd(s->ivshmem.ram_addr, incoming_fd);
         vmstate_register_ram(&s->ivshmem, DEVICE(s));
 
         IVSHMEM_DPRINTF("guest h/w addr = %p, size = %" PRIu64 "\n",
@@ -689,7 +692,6 @@ static void ivshmem_read(void *opaque, const uint8_t *buf, int size)
 
         memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
 
-        close(incoming_fd);
         return;
     }
 
@@ -991,7 +993,6 @@ static void pci_ivshmem_realize(PCIDevice *dev, Error **errp)
         }
 
         create_shared_memory_BAR(s, fd, attr, errp);
-        close(fd);
     }
 }
 
@@ -1010,11 +1011,15 @@ static void pci_ivshmem_exit(PCIDevice *dev)
     if (memory_region_is_mapped(&s->ivshmem)) {
         if (!s->hostmem) {
             void *addr = memory_region_get_ram_ptr(&s->ivshmem);
+            int fd;
 
             if (munmap(addr, s->ivshmem_size) == -1) {
                 error_report("Failed to munmap shared memory %s",
                              strerror(errno));
             }
+
+            if ((fd = qemu_get_ram_fd(s->ivshmem.ram_addr)) != -1)
+                close(fd);
         }
 
         vmstate_unregister_ram(&s->ivshmem, DEVICE(dev));
index ba4c04d202347609cf18f4382d9818a6c402ecc0..ef1489da77a3f1c7c4e5a1d7ed14a1fa3f505b67 100644 (file)
@@ -72,6 +72,7 @@ ram_addr_t qemu_ram_alloc_resizeable(ram_addr_t size, ram_addr_t max_size,
                                                      void *host),
                                      MemoryRegion *mr, Error **errp);
 int qemu_get_ram_fd(ram_addr_t addr);
+void qemu_set_ram_fd(ram_addr_t addr, int fd);
 void *qemu_get_ram_block_host_ptr(ram_addr_t addr);
 void qemu_ram_free(ram_addr_t addr);