host and vhost-net support for userspace based backends
v3 cover host and vhost-net support for userspace based backends
In this patch series we would like to introduce our approach for putting a virtio-net backend in an external userspace process. Our eventual target is to run the network backend in the Snabbswitch ethernet switch, while receiving traffic from a guest inside QEMU/KVM which runs an unmodified virtio-net implementation.
For this, we are working into extending vhost to allow equivalent functionality for userspace. Vhost already passes control of the data plane of virtio-net to the host kernel; we want to realize a similar model, but for userspace.
In this patch series the concept of a vhost-backend is introduced.
We define two vhost backend types - vhost-kernel and vhost-user. The former is the interface to the current kernel module implementation. Its control plane is ioctl based. The data plane is the kernel directly accessing the QEMU allocated, guest memory.
In the new vhost-user backend, the control plane is based on communication between QEMU and another userspace process using a unix domain socket. This allows to implement a virtio backend for a guest running in QEMU, inside the other userspace process.
We introduce a new memory related flag: -mem-share. When used the ram is created as a shared memory object. When combined with -mem-path, it will reuse a HugeTLBFS file instead of /dev/shm. This flag also implies -mem-prealloc.
The data path is realized by directly accessing the vrings and the buffer data off the guest's memory.
The current user of vhost-user is only vhost-net. We add new netdev backend that is intended to initialize vhost-net with vhost-user backend.
Example usage:
qemu -m 1024 -mem-share -mem-path /hugetlbfs \
-netdev type=vhost-user,id=net0,file=/path/to/sock \
-device virtio-net-pci,netdev=net0Changes from v2:
- Reconnect when the backend disappears
Changes from v1:
- Implementation of vhost-user netdev backend
- Code improvements
Antonios Motakis (7):
Add -mem-share option
Decouple vhost from kernel interface
Add vhost-user skeleton
Add domain socket communication for vhost-user backend Add vhost-user calls implementation
Add new vhost-user netdev backend
Add vhost-user reconnection
exec.c | 72 +++++--- hmp-commands.hx | 4 +- hw/net/vhost_net.c | 144 +++++++++++---- hw/net/virtio-net.c | 42 ++--- hw/scsi/vhost-scsi.c | 13 +- hw/virtio/Makefile.objs | 2 +- hw/virtio/vhost-backend.c | 372 ++++++++++++++++++++++++++++++++++++++ hw/virtio/vhost.c | 46 ++--- include/exec/cpu-all.h | 1 + include/hw/virtio/vhost-backend.h | 40 ++++ include/hw/virtio/vhost.h | 4 +- include/net/vhost-user.h | 17 ++ include/net/vhost_net.h | 15 +- net/Makefile.objs | 2 +- net/clients.h | 3 + net/hub.c | 1 + net/net.c | 2 + net/tap.c | 16 +- net/vhost-user.c | 167 +++++++++++++++++ qapi-schema.json | 18 +- qemu-options.hx | 12 ++ vl.c | 5 + 22 files changed, 873 insertions(+), 125 deletions(-) create mode 100644 hw/virtio/vhost-backend.c create mode 100644 include/hw/virtio/vhost-backend.h create mode 100644 include/net/vhost-user.h create mode 100644 net/vhost-user.c
Signature · 1 line
1.8.3.2
PATCH v3 1/7 Add -mem-share option
This option complements -mem-path. It implies -mem-prealloc. If specified, the guest RAM is allocated as a shared memory object. If both -mem-path and -mem-share are provided, the memory is allocated from the HugeTLBFS supplied path, and then mmapped with MAP_SHARED.
--- exec.c | 72 +++++++++++++++++++++++++++++++++++--------------- include/exec/cpu-all.h | 1 + qemu-options.hx | 9 +++++++ vl.c | 5 ++++ 4 files changed, 66 insertions(+), 21 deletions(-)
exec.c +51 -21
@@ -883,6 +883,7 @@ void qemu_mutex_unlock_ramlist(void)#include <sys/vfs.h> #define HUGETLBFS_MAGIC 0x958458f6 +#define MIN_HUGE_PAGE_SIZE (2*1024*1024)static long gethugepagesize (const char *path) {
@@ -920,15 +921,24 @@ static void *file_ram_alloc(RAMBlock *block,char *c;void *area;int fd; +int flags;unsigned long hpagesize; - hpagesize =gethugepagesize (path); -if (!hpagesize) { -return NULL ; - } +if (path) { + hpagesize =gethugepagesize (path); -if (memory < hpagesize) { -return NULL ; +if (!hpagesize) { +return NULL ; + } + +if (memory < hpagesize) { +return NULL ; + } + }else { +if (memory <MIN_HUGE_PAGE_SIZE ) { +return NULL ; + } + hpagesize =MIN_HUGE_PAGE_SIZE ; }if (kvm_enabled () && !kvm_has_sync_mmu ()) {
@@ -943,20 +953,37 @@ static void *file_ram_alloc(RAMBlock *block, *c ='_' ; } - filename =g_strdup_printf ("%s/qemu_back_mem.%s.XXXXXX" , path, - sanitized_name); -g_free (sanitized_name); +if (path) { + + filename =g_strdup_printf ("%s/qemu_back_mem.%s.XXXXXX" , path, + sanitized_name); +g_free (sanitized_name); - fd =mkstemp (filename); -if (fd <0 ) { -perror ("unable to create backing store for hugepages" ); + fd =mkstemp (filename); +if (fd <0 ) { +perror ("unable to create backing store for huge" ); +g_free (filename); +return NULL ; + } +unlink (filename);g_free (filename); + memory = (memory + hpagesize -1 ) & ~(hpagesize -1 ); + }else if (mem_share) { + filename =g_strdup_printf ("qemu_back_mem.%s.XXXXXX" , sanitized_name); +g_free (sanitized_name); + fd =shm_open (filename,O_CREAT |O_RDWR |O_EXCL , +S_IRWXU |S_IRWXG |S_IRWXO ); +if (fd <0 ) { +perror ("unable to create backing store for shared memory" ); +g_free (filename); +return NULL ; + } +shm_unlink (filename); +g_free (filename); + }else { +fprintf (stderr,"-mem-path or -mem-share required\n" );return NULL ; } -unlink (filename); -g_free (filename); - - memory = (memory+hpagesize-1 ) & ~(hpagesize-1 );/* * ftruncate is not supported by hugetlbfs in older
@@ -967,7 +994,8 @@ static void *file_ram_alloc(RAMBlock *block,if (ftruncate (fd, memory))perror ("ftruncate" ); - area =mmap (0 , memory,PROT_READ |PROT_WRITE ,MAP_PRIVATE , fd,0 ); + flags = mem_share ?MAP_SHARED :MAP_PRIVATE ; + area =mmap (0 , memory,PROT_READ |PROT_WRITE , flags, fd,0 );if (area ==MAP_FAILED ) {perror ("file_ram_alloc: can't mmap RAM pages" );close (fd);
@@ -1150,13 +1178,14 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host, new_block->host = host; new_block->flags |=RAM_PREALLOC_MASK ; }else if (xen_enabled ()) { -if (mem_path) { -fprintf (stderr,"-mem-path not supported with Xen\n" ); +if (mem_path || mem_share) { +fprintf (stderr, +"-mem-path and -mem-share not supported with Xen\n" );exit (1 ); }xen_ram_alloc (new_block->offset, size, mr); }else { -if (mem_path) { +if (mem_path || mem_share) {if (phys_mem_alloc != qemu_anon_ram_alloc) {/* * file_ram_alloc() needs to allocate just like
@@ -1164,7 +1193,8 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,* a hook there. */ fprintf (stderr, -"-mem-path not supported with this accelerator\n" ); +"-mem-path and -mem-share " +"not supported with this accelerator\n" );exit (1 ); } new_block->host =file_ram_alloc (new_block, size, mem_path);
include/exec/cpu-all.h +1 -0
@@ -469,6 +469,7 @@ extern RAMList ram_list;extern const char *mem_path;extern int mem_prealloc; +extern int mem_share;/* Flags stored in the low bits of the TLB virtual address. These are defined so that fast path ram access is all zeros. */
qemu-options.hx +9 -0
@@ -237,6 +237,15 @@ STEXI Preallocate memory when using -mem-path. ETEXI +DEF("mem-share", 0, QEMU_OPTION_mem_share, + "-mem-share share guest memory (implies -mem-prealloc)\n", + QEMU_ARCH_ALL) +STEXI +@item -mem-share +@findex -mem-share +Allocate guest RAM as a shared memory object. +ETEXI + DEF("k", HAS_ARG, QEMU_OPTION_k, "-k language use keyboard layout (for example 'fr' for French)\n", QEMU_ARCH_ALL)
vl.c +5 -0
@@ -189,6 +189,7 @@ const char* keyboard_layout = NULL;ram_addr_t ram_size;const char *mem_path =NULL ;int mem_prealloc =0 ;/* force preallocation of physical target memory */ +int mem_share =0 ;/* allocate shared memory */ int nb_nics;NICInfo nd_table[MAX_NICS ];int autostart;
@@ -3212,6 +3213,10 @@ int main(int argc, char **argv, char **envp)case QEMU_OPTION_mem_prealloc: mem_prealloc =1 ;break ; +case QEMU_OPTION_mem_share: + mem_share =1 ; + mem_prealloc =1 ; +break ;case QEMU_OPTION_d: log_mask = optarg;break ;
Signature · 1 line
1.8.3.2
Re: [Qemu-devel] [PATCH v3 1/7] Add -mem-share option
Quoted 11 lines · Antonios Motakis
This option complements -mem-path. It implies -mem-prealloc. If specified, the guest RAM is allocated as a shared memory object. If both -mem-path and -mem-share are provided, the memory is allocated from the HugeTLBFS supplied path, and then mmapped with
MAP_SHARED.Signed-off-by Antonios Motakis <[email protected]>Signed-off-by Nikolay Nikolaev <[email protected]>---
+++ b/qemu-options.hx
qemu-options.hx STEXI · line 240
Quoted 4 lines
qemu-options.hx +1
-0@@ -237,6 +237,15 @@ STEXI Preallocate memory when using -mem-path. ETEXI +DEF("mem-share", 0, QEMU_OPTION_mem_share,
Ouch. Doesn't this mean you are defining a boolean option (absent or present) as opposed to a qemuOpts option? I've already been complaining that other boolean opts are currently undiscoverable to QMP; they also have the drawback of having no way to turn the option back off if an alias turned it on earlier in the command line. Can we use qemuOpts here (so query-command-line-options can see it), and with a boolean on/off argument (so it's not a one-way switch)?
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [snabb-devel:650] Re: [PATCH v3 1/7] Add -mem-share option
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Il 14/12/2013 04:53, Eric Blake ha scritto:
Quoted 9 lines
Quoted 1 line
Quoted 1 line
+DEF("mem-share", 0,
QEMU_OPTION_mem_share,Ouch. Doesn't this mean you are defining a boolean option (absent or present) as opposed to a qemuOpts option? I've already been complaining that other boolean opts are currently undiscoverable to QMP; they also have the drawback of having no way to turn the option back off if an alias turned it on earlier in the command line. Can we use qemuOpts here (so query-command-line-options can see it), and with a boolean on/off argument (so it's not a one-way switch)?
This can be replaced with a memdev property as soon as memdev gets in.
We'll need to provide a way to introspect the properties of a QOM object, though.
Paolo
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
iQIcBAEBAgAGBQJSrDx+AAoJEBvWZb6bTYbyf1oP/imUGPTzhoAOVb8MfR+2UnJ5 JQBQa6000nOLH54NnIPtTepJEQ+01sRCqJHykUEQD2q4WNL/ToBfu3FEwJbu7Wyx PHlt3BNG33LY+tM5pANgu/+Pliqe2dzPScTbfnkAawOUDSGFxkQWhJ/dFMHMfmiw HtrfmNF72liz9OJ8VEvt7JmQFW5lGeFpd3XzibDyC3VtjN7tX3KVe8lZ4oUjjNvD z2Q2pFs7fIHvKTTgMn1QQeKj42yXlzq+fNQPwuSlQQmlqqOjyDET9fCe2fYeqlGX b94iTzpqGNUwCRw2OluLftdNnScQKy2LEkokd5LAIqsyJEAVRZnAikLF2JyfdhTy lIGg9VX0VnqIVeauVOcpoBH1HqlDDMcen2DV5Sr5mTHrJ9m2EMm/aqVMYt/kPjP1 5IeYdJ44RxxAXr6aPnpK2/JjaJP+LfjB+xXUYqukjFOibDDXMr4wprKOgkDd5K/9 SkC8KSBsxK2pwUdztr4W1pSxnsYHwlsBKZM0i8MTZv5GsAe4oG3Authp5Uy15JmR xjmm6YfbRGRH/Ov+InlQ8BbzzCVRQkoaGb8B8GosKrDe+7Fnh+GUV2pjxvgySYk1 kUyo9RpeT9KebCpvg4RvasLX5A1yiDdh07KAp+Lyw1JXrw5zIeOPzf02LQJBtfmM 5MS3PXn4GbvmsgW/e1bA
=Ed70
-----END PGP SIGNATURE-----
Re: [Qemu-devel] [PATCH v3 1/7] Add -mem-share option
In reply to Eric BlakeQuoted 16 lines · Eric Blake
Quoted 1 line · Antonios Motakis
This option complements -mem-path. It implies -mem-prealloc. If
specified,
Quoted 13 lines
the guest RAM is allocated as a shared memory object. If both -mem-path and -mem-share are provided, the memory is allocated from the HugeTLBFS supplied path, and then mmapped with
MAP_SHARED.Signed-off-by Antonios Motakis <[email protected]>Signed-off-by Nikolay Nikolaev <[email protected]>---
+++ b/qemu-options.hx
qemu-options.hx +0
-0@@ -237,6 +237,15 @@ STEXI Preallocate memory when using -mem-path. ETEXI
Quoted 10 lines
qemu-options.hx STEXI · line 240
Quoted 1 line
qemu-options.hx +1
-0+DEF("mem-share", 0, QEMU_OPTION_mem_share,Ouch. Doesn't this mean you are defining a boolean option (absent or present) as opposed to a qemuOpts option? I've already been complaining that other boolean opts are currently undiscoverable to QMP; they also have the drawback of having no way to turn the option back off if an alias turned it on earlier in the command line. Can we use qemuOpts here (so query-command-line-options can see it), and with a boolean on/off argument (so it's not a one-way switch)?
Our implementation of mem-share complements mem-path and mem-prealloc. Maybe these options should be combined as one QemuOpts with arguments? Is this the memdev that is referred to by Paolo?
Quoted 5 lines
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v3 1/7] Add -mem-share option
Quoted 29 lines · On Mon, 16 Dec 2013 16:20:04 +0100 Antonios Motakis…
Quoted 16 lines · Eric Blake
Quoted 1 line · Antonios Motakis
This option complements -mem-path. It implies -mem-prealloc. If
specified,
Quoted 13 lines
the guest RAM is allocated as a shared memory object. If both -mem-path and -mem-share are provided, the memory is allocated from the HugeTLBFS supplied path, and then mmapped with
MAP_SHARED.Signed-off-by Antonios Motakis <[email protected]>Signed-off-by Nikolay Nikolaev <[email protected]>---
+++ b/qemu-options.hx
qemu-options.hx +0
-0@@ -237,6 +237,15 @@ STEXI Preallocate memory when using -mem-path. ETEXIQuoted 10 lines
qemu-options.hx STEXI · line 240
Quoted 1 line
qemu-options.hx +1
-0+DEF("mem-share", 0, QEMU_OPTION_mem_share,Ouch. Doesn't this mean you are defining a boolean option (absent or present) as opposed to a qemuOpts option? I've already been complaining that other boolean opts are currently undiscoverable to QMP; they also have the drawback of having no way to turn the option back off if an alias turned it on earlier in the command line. Can we use qemuOpts here (so query-command-line-options can see it), and with a boolean on/off argument (so it's not a one-way switch)?
Quoted 3 lines
Our implementation of mem-share complements mem-path and mem-prealloc. Maybe these options should be combined as one QemuOpts with arguments? Is this the memdev that is referred to by Paolo?
memdev is introduced here: http://lists.gnu.org/archive/html/qemu-devel/2013-11/msg02532.html
as for mem-path & mem-prealloc, I was thinking about adding HugePageMem backend to handle hugepage specifics. mem-share could be a part of ShareMem backend or something like this.
Quoted 7 lines
Quoted 7 lines
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v3 1/7] Add -mem-share option
Il 16/12/2013 16:47, Igor Mammedov ha scritto:
Quoted 5 lines
memdev is introduced here: http://lists.gnu.org/archive/html/qemu-devel/2013-11/msg02532.html
as for mem-path & mem-prealloc, I was thinking about adding HugePageMem backend to handle hugepage specifics. mem-share could be a part of ShareMem backend or something like this.
We need three backends, something like the following:
- anonymous mmap (-object memory-ram,size=128M)
- file mmap (-object memory-file,shared=on/off,size=128M,file=FILENAME), creates file if size property provided.
- mkstemp + file mmap (-object memory-hugepage,size=128M,path=PATH)
I don't think shared=on/off is useful in the third case, but it would be trivial to add.
Paolo
Re: [Qemu-devel] [PATCH v3 1/7] Add -mem-share option
In reply to Antonios MotakisQuoted 4 lines · Antonios Motakis
This option complements -mem-path. It implies -mem-prealloc. If specified, the guest RAM is allocated as a shared memory object. If both -mem-path and -mem-share are provided, the memory is allocated from the HugeTLBFS supplied path, and then mmapped with
MAP_SHARED.
Hi,
Interesting, I've got a similar use-case here where I've added a -mem-shared option. I've got a few comments/questions.
Why do you imply -mem-prealloc? cant the user keep controlling that through -mem-prealloc?
I'd prefer if -mem-share did not use shm_open but took a directory path as arg and created the backing files there. I'd also prefer if the files had deterministic names and where not unlinked after creation. I.e, let the user delete them when no longer needed.
The reason for this is that it makes it easier to use apps that are not aware of shm or QEMU specifics to manipulate the memory backing. I understand that there might be issues (e.g filling up the disk, slow access over NFS etc) but these are at the choice of the user.
Any thoughts around this?
Best regards,
Edgar
Quoted 198 lines
Signed-off-by Antonios Motakis <[email protected]>Signed-off-by Nikolay Nikolaev <[email protected]>--- exec.c | 72 +++++++++++++++++++++++++++++++++++--------------- include/exec/cpu-all.h | 1 + qemu-options.hx | 9 +++++++ vl.c | 5 ++++ 4 files changed, 66 insertions(+), 21 deletions(-)exec.c +51
-21@@ -883,6 +883,7 @@ void qemu_mutex_unlock_ramlist(void)#include <sys/vfs.h> #define HUGETLBFS_MAGIC 0x958458f6 +#define MIN_HUGE_PAGE_SIZE (2*1024*1024)static long gethugepagesize (const char *path) {@@ -920,15 +921,24 @@ static void *file_ram_alloc(RAMBlock *block,char *c;void *area;int fd; +int flags;unsigned long hpagesize; - hpagesize =gethugepagesize (path); -if (!hpagesize) { -return NULL ; - } +if (path) { + hpagesize =gethugepagesize (path); -if (memory < hpagesize) { -return NULL ; +if (!hpagesize) { +return NULL ; + } + +if (memory < hpagesize) { +return NULL ; + } + }else { +if (memory <MIN_HUGE_PAGE_SIZE ) { +return NULL ; + } + hpagesize =MIN_HUGE_PAGE_SIZE ; }if (kvm_enabled () && !kvm_has_sync_mmu ()) {@@ -943,20 +953,37 @@ static void *file_ram_alloc(RAMBlock *block, *c ='_' ; } - filename =g_strdup_printf ("%s/qemu_back_mem.%s.XXXXXX" , path, - sanitized_name); -g_free (sanitized_name); +if (path) { + + filename =g_strdup_printf ("%s/qemu_back_mem.%s.XXXXXX" , path, + sanitized_name); +g_free (sanitized_name); - fd =mkstemp (filename); -if (fd <0 ) { -perror ("unable to create backing store for hugepages" ); + fd =mkstemp (filename); +if (fd <0 ) { +perror ("unable to create backing store for huge" ); +g_free (filename); +return NULL ; + } +unlink (filename);g_free (filename); + memory = (memory + hpagesize -1 ) & ~(hpagesize -1 ); + }else if (mem_share) { + filename =g_strdup_printf ("qemu_back_mem.%s.XXXXXX" , sanitized_name); +g_free (sanitized_name); + fd =shm_open (filename,O_CREAT |O_RDWR |O_EXCL , +S_IRWXU |S_IRWXG |S_IRWXO ); +if (fd <0 ) { +perror ("unable to create backing store for shared memory" ); +g_free (filename); +return NULL ; + } +shm_unlink (filename); +g_free (filename); + }else { +fprintf (stderr,"-mem-path or -mem-share required\n" );return NULL ; } -unlink (filename); -g_free (filename); - - memory = (memory+hpagesize-1 ) & ~(hpagesize-1 );/* * ftruncate is not supported by hugetlbfs in older @@ -967,7 +994,8 @@ static void *file_ram_alloc(RAMBlock *block,if (ftruncate (fd, memory))perror ("ftruncate" ); - area =mmap (0 , memory,PROT_READ |PROT_WRITE ,MAP_PRIVATE , fd,0 ); + flags = mem_share ?MAP_SHARED :MAP_PRIVATE ; + area =mmap (0 , memory,PROT_READ |PROT_WRITE , flags, fd,0 );if (area ==MAP_FAILED ) {perror ("file_ram_alloc: can't mmap RAM pages" );close (fd);@@ -1150,13 +1178,14 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host, new_block->host = host; new_block->flags |=RAM_PREALLOC_MASK ; }else if (xen_enabled ()) { -if (mem_path) { -fprintf (stderr,"-mem-path not supported with Xen\n" ); +if (mem_path || mem_share) { +fprintf (stderr, +"-mem-path and -mem-share not supported with Xen\n" );exit (1 ); }xen_ram_alloc (new_block->offset, size, mr); }else { -if (mem_path) { +if (mem_path || mem_share) {if (phys_mem_alloc != qemu_anon_ram_alloc) {/* * file_ram_alloc() needs to allocate just like @@ -1164,7 +1193,8 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,* a hook there. */ fprintf (stderr, -"-mem-path not supported with this accelerator\n" ); +"-mem-path and -mem-share " +"not supported with this accelerator\n" );exit (1 ); } new_block->host =file_ram_alloc (new_block, size, mem_path);include/exec/cpu-all.h +1
-0@@ -469,6 +469,7 @@ extern RAMList ram_list;extern const char *mem_path;extern int mem_prealloc; +extern int mem_share;/* Flags stored in the low bits of the TLB virtual address. These are defined so that fast path ram access is all zeros. */ qemu-options.hx +9
-0@@ -237,6 +237,15 @@ STEXI Preallocate memory when using -mem-path. ETEXI +DEF("mem-share", 0, QEMU_OPTION_mem_share, + "-mem-share share guest memory (implies -mem-prealloc)\n", + QEMU_ARCH_ALL) +STEXI +@item -mem-share +@findex -mem-share +Allocate guest RAM as a shared memory object. +ETEXI + DEF("k", HAS_ARG, QEMU_OPTION_k, "-k language use keyboard layout (for example 'fr' for French)\n", QEMU_ARCH_ALL)vl.c +5
-0@@ -189,6 +189,7 @@ const char* keyboard_layout = NULL;ram_addr_t ram_size;const char *mem_path =NULL ;int mem_prealloc =0 ;/* force preallocation of physical target memory */ +int mem_share =0 ;/* allocate shared memory */ int nb_nics;NICInfo nd_table[MAX_NICS ];int autostart;@@ -3212,6 +3213,10 @@ int main(int argc, char **argv, char **envp)case QEMU_OPTION_mem_prealloc: mem_prealloc =1 ;break ; +case QEMU_OPTION_mem_share: + mem_share =1 ; + mem_prealloc =1 ; +break ;case QEMU_OPTION_d: log_mask = optarg;break ;Signature · 1 line
1.8.3.2
Re: [Qemu-devel] [PATCH v3 1/7] Add -mem-share option
Il 16/12/2013 08:32, Edgar E. Iglesias ha scritto:
Quoted 19 lines · Antonios Motakis
Quoted 4 lines
This option complements -mem-path. It implies -mem-prealloc. If specified, the guest RAM is allocated as a shared memory object. If both -mem-path and -mem-share are provided, the memory is allocated from the HugeTLBFS supplied path, and then mmapped with
MAP_SHARED.Hi,
Interesting, I've got a similar use-case here where I've added a -mem-shared option. I've got a few comments/questions.
Why do you imply -mem-prealloc? cant the user keep controlling that through -mem-prealloc?
I'd prefer if -mem-share did not use
shm_openbut took a directory path as arg and created the backing files there. I'd also prefer if the files had deterministic names and where not unlinked after creation. I.e, let the user delete them when no longer needed.
Quoted 6 lines
The reason for this is that it makes it easier to use apps that are not aware of shm or QEMU specifics to manipulate the memory backing. I understand that there might be issues (e.g filling up the disk, slow access over NFS etc) but these are at the choice of the user.
Any thoughts around this?
I agree entirely with you.
Paolo
Re: [Qemu-devel] [PATCH v3 1/7] Add -mem-share option
In reply to Edgar E. IglesiasOn Mon, Dec 16, 2013 at 8:32 AM, Edgar E. Iglesias <[email protected]
Quoted 25 lines
wrote:
Quoted 1 line · Antonios Motakis
This option complements -mem-path. It implies -mem-prealloc. If
specified,
Quoted 3 lines
the guest RAM is allocated as a shared memory object. If both -mem-path and -mem-share are provided, the memory is allocated from the HugeTLBFS supplied path, and then mmapped with
MAP_SHARED.Hi,
Interesting, I've got a similar use-case here where I've added a -mem-shared
option. I've got a few comments/questions.Why do you imply -mem-prealloc? cant the user keep controlling that through -mem-prealloc?
I'd prefer if -mem-share did not use
shm_openbut took a directory path as arg
and created the backing files there. I'd also prefer if the files had deterministic names and where not unlinked after creation. I.e, let the user
delete them when no longer needed.
Quoted 8 lines
The reason for this is that it makes it easier to use apps that are not aware of shm or QEMU specifics to manipulate the memory backing. I understand
that there might be issues (e.g filling up the disk, slow access over NFS etc)
but these are at the choice of the user.
Currently -mem-path implies HugeTLBFS for the supplied path. Maybe we should change
its behavior to do what you suggest:
-mem-path - specify a directory where to allocate the mmap-ed ram files (and don't unlink them)
-mem-hugetlbfs - check mem-path directory for HugeTLBFS (do we need this one?)
-mem-share - add MAP_SHARED to mmap
-mem-prealloc - preallocate the memory
How does this sound?
Quoted 214 lines
Any thoughts around this?
Best regards,
EdgarQuoted 5 lines
Signed-off-by Antonios Motakis <[email protected]>Signed-off-by Nikolay Nikolaev <[email protected]>--- exec.c | 72+++++++++++++++++++++++++++++++++++---------------
Quoted 65 lines
include/exec/cpu-all.h | 1 + qemu-options.hx | 9 +++++++ vl.c | 5 ++++ 4 files changed, 66 insertions(+), 21 deletions(-)exec.c +22
-12@@ -883,6 +883,7 @@ void qemu_mutex_unlock_ramlist(void)#include <sys/vfs.h> #define HUGETLBFS_MAGIC 0x958458f6 +#define MIN_HUGE_PAGE_SIZE (2*1024*1024)static long gethugepagesize (const char *path) {@@ -920,15 +921,24 @@ static void *file_ram_alloc(RAMBlock *block,char *c;void *area;int fd; +int flags;unsigned long hpagesize; - hpagesize =gethugepagesize (path); -if (!hpagesize) { -return NULL ; - } +if (path) { + hpagesize =gethugepagesize (path); -if (memory < hpagesize) { -return NULL ; +if (!hpagesize) { +return NULL ; + } + +if (memory < hpagesize) { +return NULL ; + } + }else { +if (memory <MIN_HUGE_PAGE_SIZE ) { +return NULL ; + } + hpagesize =MIN_HUGE_PAGE_SIZE ; }if (kvm_enabled () && !kvm_has_sync_mmu ()) {@@ -943,20 +953,37 @@ static void *file_ram_alloc(RAMBlock *block, *c ='_' ; } - filename =g_strdup_printf ("%s/qemu_back_mem.%s.XXXXXX" , path, - sanitized_name); -g_free (sanitized_name); +if (path) { + + filename =g_strdup_printf ("%s/qemu_back_mem.%s.XXXXXX" , path, + sanitized_name); +g_free (sanitized_name); - fd =mkstemp (filename); -if (fd <0 ) { -perror ("unable to create backing store for hugepages" ); + fd =mkstemp (filename);exec.c static void *file_ram_alloc(RAMBlock *block, · line 972
Quoted 10 lines
exec.c +9
-0+if (fd <0 ) { +perror ("unable to create backing store for huge" ); +g_free (filename); +return NULL ; + } +unlink (filename);g_free (filename); + memory = (memory + hpagesize -1 ) & ~(hpagesize -1 ); + }else if (mem_share) { + filename = g_strdup_printf("qemu_back_mem.%s.XXXXXX",
sanitized_name);exec.c ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t
Quoted 32 lines
exec.c +14
-5+g_free (sanitized_name); + fd =shm_open (filename,O_CREAT |O_RDWR |O_EXCL , +S_IRWXU |S_IRWXG |S_IRWXO ); +if (fd <0 ) { +perror ("unable to create backing store for shared memory" ); +g_free (filename); +return NULL ; + } +shm_unlink (filename); +g_free (filename); + }else { +fprintf (stderr,"-mem-path or -mem-share required\n" );return NULL ; } -unlink (filename); -g_free (filename); - - memory = (memory+hpagesize-1 ) & ~(hpagesize-1 );/* * ftruncate is not supported by hugetlbfs in older @@ -967,7 +994,8 @@ static void *file_ram_alloc(RAMBlock *block,if (ftruncate (fd, memory))perror ("ftruncate" ); - area =mmap (0 , memory,PROT_READ |PROT_WRITE ,MAP_PRIVATE , fd,0 ); + flags = mem_share ?MAP_SHARED :MAP_PRIVATE ; + area =mmap (0 , memory,PROT_READ |PROT_WRITE , flags, fd,0 );if (area ==MAP_FAILED ) {perror ("file_ram_alloc: can't mmap RAM pages" );close (fd);@@ -1150,13 +1178,14 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_tsize, void *host,
exec.c ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
Quoted 8 lines
exec.c +3
-2new_block->host = host; new_block->flags |=RAM_PREALLOC_MASK ; }else if (xen_enabled ()) { -if (mem_path) { -fprintf (stderr,"-mem-path not supported with Xen\n" ); +if (mem_path || mem_share) { + fprintf(stderr, + "-mem-path and -mem-share not supported withXen\n");
exec.c ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t
Quoted 10 lines
exec.c +1
-1exit (1 ); }xen_ram_alloc (new_block->offset, size, mr); }else { -if (mem_path) { +if (mem_path || mem_share) {if (phys_mem_alloc != qemu_anon_ram_alloc) {/* * file_ram_alloc() needs to allocate just like @@ -1164,7 +1193,8 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_tsize, void *host,
exec.c ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
Quoted 4 lines
exec.c +0
-1* a hook there. */ fprintf(stderr, - "-mem-path not supported with thisaccelerator\n");
Quoted 26 lines
exec.c +2
-0+"-mem-path and -mem-share " + "not supported with this accelerator\n");exit (1 ); } new_block->host =file_ram_alloc (new_block, size, mem_path);include/exec/cpu-all.h +1
-0@@ -469,6 +469,7 @@ extern RAMList ram_list;extern const char *mem_path;extern int mem_prealloc; +extern int mem_share;/* Flags stored in the low bits of the TLB virtual address. These are defined so that fast path ram access is all zeros. */ qemu-options.hx +1
-0@@ -237,6 +237,15 @@ STEXI Preallocate memory when using -mem-path. ETEXI +DEF("mem-share", 0, QEMU_OPTION_mem_share,qemu-options.hx STEXI · line 250
Quoted 10 lines
qemu-options.hx +8
-0+ "-mem-share share guest memory (implies -mem-prealloc)\n", + QEMU_ARCH_ALL) +STEXI +@item -mem-share +@findex -mem-share +Allocate guest RAM as a shared memory object. +ETEXI + DEF("k", HAS_ARG, QEMU_OPTION_k, "-k language use keyboard layout (for example 'fr' forFrench)\n",
vl.c const char* keyboard_layout = NULL; · line 191
Quoted 9 lines
qemu-options.hx +0
-0QEMU_ARCH_ALL)vl.c +0
-0@@ -189,6 +189,7 @@ const char* keyboard_layout = NULL;ram_addr_t ram_size;const char *mem_path =NULL ;int mem_prealloc =0 ;/* force preallocation of physical target memory */
Quoted 19 lines
vl.c +5
-1+int mem_share =0 ;/* allocate shared memory */ int nb_nics;NICInfo nd_table[MAX_NICS ];int autostart;@@ -3212,6 +3213,10 @@ int main(int argc, char **argv, char **envp)case QEMU_OPTION_mem_prealloc: mem_prealloc =1 ;break ; +case QEMU_OPTION_mem_share: + mem_share =1 ; + mem_prealloc =1 ; +break ;case QEMU_OPTION_d: log_mask = optarg;break ; --1.8.3.2
Re: [Qemu-devel] [PATCH v3 1/7] Add -mem-share option
Quoted 40 lines · Antonios Motakis
On Mon, Dec 16, 2013 at 8:32 AM, Edgar E. Iglesias <[email protected]> wrote:
On Fri, Dec 13, 2013 at 12:14:31PM +0100, Antonios Motakis wrote:
> This option complements -mem-path. It implies -mem-prealloc. If specified,
> the guest RAM is allocated as a shared memory object. If both -mem-path
> and -mem-share are provided, the memory is allocated from the HugeTLBFS
> supplied path, and then mmapped withMAP_SHARED.Hi,
Interesting, I've got a similar use-case here where I've added a -mem-shared
option. I've got a few comments/questions.Why do you imply -mem-prealloc? cant the user keep controlling that through
-mem-prealloc?I'd prefer if -mem-share did not use
shm_openbut took a directory path as arg
and created the backing files there. I'd also prefer if the files had deterministic names and where not unlinked after creation. I.e, let the user
delete them when no longer needed.The reason for this is that it makes it easier to use apps that are not aware of shm or QEMU specifics to manipulate the memory backing. I understand
that there might be issues (e.g filling up the disk, slow access over NFS etc)
but these are at the choice of the user.Currently -mem-path implies HugeTLBFS for the supplied path. Maybe we should change
its behavior to do what you suggest:
Quoted 7 lines
-mem-path - specify a directory where to allocate the mmap-ed ram files (and don't unlink them)
-mem-hugetlbfs - check mem-path directory for HugeTLBFS (do we need this one?)
-mem-share - addMAP_SHAREDto mmap
-mem-prealloc - preallocate the memory
How does this sound?
It sounds good to me, but I'm not sure its a good thing to change the behaviour of -mem-path and break backwards compatibility. Maybe we could relax the hugetlbfs impl for -mem-path in that it uses it the huge page sizes if the underlying fs is hugetlbfs and otherwise not?
In my local implementation mem-share takes an argument by itself and replaces the need for -mem-path, not very intuitive I agree but its an alternative.
Cheers,
Edgar
PATCH v3 2/7 Decouple vhost from kernel interface
In reply to Antonios MotakisWe introduce the concept of vhost-backend, which can be either vhost-kernel or vhost-user. The existing vhost interface to the kernel is abstracted behind the vhost-kernel backend.
We replace all direct ioctls to the kernel with a vhost_call to the backend. vhost dev->control is referenced only in the vhost-backend (ioctl, open, close).
--- hw/net/vhost_net.c | 13 +++++--- hw/scsi/vhost-scsi.c | 13 +++++--- hw/virtio/Makefile.objs | 2 +- hw/virtio/vhost-backend.c | 64 +++++++++++++++++++++++++++++++++++++++ hw/virtio/vhost.c | 47 ++++++++++++++-------------- include/hw/virtio/vhost-backend.h | 37 ++++++++++++++++++++++ include/hw/virtio/vhost.h | 4 ++- 7 files changed, 147 insertions(+), 33 deletions(-) create mode 100644 hw/virtio/vhost-backend.c create mode 100644 include/hw/virtio/vhost-backend.h
hw/net/vhost_net.c +8 -5
@@ -27,7 +27,6 @@#include <sys/socket.h> #include <linux/kvm.h> #include <fcntl.h> -#include <sys/ioctl.h> #include <linux/virtio_ring.h> #include <netpacket/packet.h> #include <net/ethernet.h>
@@ -113,7 +112,8 @@ struct vhost_net *vhost_net_init(NetClientState *backend, int devfd, net->dev.nvqs =2 ; net->dev.vqs = net->vqs; - r =vhost_dev_init (&net->dev, devfd,"/dev/vhost-net" , force); + r =vhost_dev_init (&net->dev, devfd,"/dev/vhost-net" , +VHOST_BACKEND_TYPE_KERNEL , force);if (r <0 ) { goto fail; }
@@ -170,7 +170,8 @@ static int vhost_net_start_one(struct vhost_net *net,qemu_set_fd_handler (net->backend,NULL ,NULL ,NULL ); file.fd = net->backend;for (file.index =0 ; file.index < net->dev.nvqs; ++file.index) { - r =ioctl (net->dev.control,VHOST_NET_SET_BACKEND , &file); +const VhostOps *vhost_ops = net->dev.vhost_ops; + r = vhost_ops->vhost_call (&net->dev,VHOST_NET_SET_BACKEND , &file);if (r <0 ) { r = -errno; goto fail;
@@ -180,7 +181,8 @@ static int vhost_net_start_one(struct vhost_net *net, fail: file.fd =-1 ;while (file.index-- >0 ) { -int r =ioctl (net->dev.control,VHOST_NET_SET_BACKEND , &file); +const VhostOps *vhost_ops = net->dev.vhost_ops; +int r = vhost_ops->vhost_call (&net->dev,VHOST_NET_SET_BACKEND , &file);assert (r >=0 ); } net->nc->info->poll (net->nc, true);
@@ -201,7 +203,8 @@ static void vhost_net_stop_one(struct vhost_net *net, }for (file.index =0 ; file.index < net->dev.nvqs; ++file.index) { -int r =ioctl (net->dev.control,VHOST_NET_SET_BACKEND , &file); +const VhostOps *vhost_ops = net->dev.vhost_ops; +int r = vhost_ops->vhost_call (&net->dev,VHOST_NET_SET_BACKEND , &file);assert (r >=0 ); } net->nc->info->poll (net->nc, true);
hw/scsi/vhost-scsi.c +9 -4
@@ -27,12 +27,13 @@static int vhost_scsi_set_endpoint (VHostSCSI *s) {VirtIOSCSICommon *vs =VIRTIO_SCSI_COMMON (s); +const VhostOps *vhost_ops = s->dev.vhost_ops;struct vhost_scsi_target backend;int ret;memset (&backend,0 ,sizeof (backend));pstrcpy (backend.vhost_wwpn,sizeof (backend.vhost_wwpn), vs->conf.wwpn); - ret =ioctl (s->dev.control,VHOST_SCSI_SET_ENDPOINT , &backend); + ret = vhost_ops->vhost_call (&s->dev,VHOST_SCSI_SET_ENDPOINT , &backend);if (ret <0 ) {return -errno; }
@@ -43,10 +44,11 @@ static void vhost_scsi_clear_endpoint(VHostSCSI *s) {VirtIOSCSICommon *vs =VIRTIO_SCSI_COMMON (s);struct vhost_scsi_target backend; +const VhostOps *vhost_ops = s->dev.vhost_ops;memset (&backend,0 ,sizeof (backend));pstrcpy (backend.vhost_wwpn,sizeof (backend.vhost_wwpn), vs->conf.wwpn); -ioctl (s->dev.control,VHOST_SCSI_CLEAR_ENDPOINT , &backend); + vhost_ops->vhost_call (&s->dev,VHOST_SCSI_CLEAR_ENDPOINT , &backend); }static int vhost_scsi_start (VHostSCSI *s)
@@ -55,13 +57,15 @@ static int vhost_scsi_start(VHostSCSI *s)VirtIODevice *vdev =VIRTIO_DEVICE (s);BusState *qbus =BUS (qdev_get_parent_bus (DEVICE (vdev)));VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (qbus); +const VhostOps *vhost_ops = s->dev.vhost_ops;if (!k->set_guest_notifiers) {error_report ("binding does not support guest notifiers" );return -ENOSYS ; } - ret =ioctl (s->dev.control,VHOST_SCSI_GET_ABI_VERSION , &abi_version); + ret = vhost_ops->vhost_call (&s->dev, +VHOST_SCSI_GET_ABI_VERSION , &abi_version);if (ret <0 ) {return -errno; }
@@ -225,7 +229,8 @@ static int vhost_scsi_init(VirtIODevice *vdev) s->dev.vqs = g_new(struct vhost_virtqueue, s->dev.nvqs); s->dev.vq_index =0 ; - ret =vhost_dev_init (&s->dev, vhostfd,"/dev/vhost-scsi" , true); + ret =vhost_dev_init (&s->dev, vhostfd,"/dev/vhost-scsi" , +VHOST_BACKEND_TYPE_KERNEL , true);if (ret <0 ) {error_report ("vhost-scsi: vhost initialization failed: %s\n" ,strerror (-ret));
hw/virtio/Makefile.objs +1 -1
@@ -5,4 +5,4 @@ common-obj-y += virtio-mmio.o common-obj-$(CONFIG_VIRTIO_BLK_DATA_PLANE) += dataplane/ obj-y += virtio.o virtio-balloon.o -obj-$(CONFIG_LINUX) += vhost.o +obj-$(CONFIG_LINUX) += vhost.o vhost-backend.o
hw/virtio/vhost-backend.c new file +64 -0
64 lines not on this page · raw message
hw/virtio/vhost.c +25 -22
162 lines not on this page · raw message
include/hw/virtio/vhost-backend.h new file +37 -0
@@ -0,0 +1,37 @@ +/* +* vhost-backend +* +* Copyright (c) 2013 Virtual Open Systems Sarl. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +*/ + +#ifndef VHOST_BACKEND_H_ +#define VHOST_BACKEND_H_ + +typedef enum VhostBackendType { +VHOST_BACKEND_TYPE_NONE =0 , +VHOST_BACKEND_TYPE_KERNEL =1 , +VHOST_BACKEND_TYPE_MAX =2 , +}VhostBackendType ; + +struct vhost_dev ; + +typedef int (*vhost_call )(struct vhost_dev *dev,unsigned long int request, +void *arg); +typedef int (*vhost_backend_init )(struct vhost_dev *dev,const char *devpath); +typedef int (*vhost_backend_cleanup )(struct vhost_dev *dev); + +typedef struct VhostOps { +VhostBackendType backend_type; +vhost_call vhost_call; +vhost_backend_init vhost_backend_init; +vhost_backend_cleanup vhost_backend_cleanup; +}VhostOps ; + +int vhost_set_backend_type (struct vhost_dev *dev, +VhostBackendType backend_type); + +#endif /* VHOST_BACKEND_H_ */
include/hw/virtio/vhost.h +3 -1
@@ -2,6 +2,7 @@#define VHOST_H #include "hw/hw.h" +#include "hw/virtio/vhost-backend.h" #include "hw/virtio/virtio.h" #include "exec/memory.h"
@@ -48,10 +49,11 @@ struct vhost_dev {bool memory_changed;hwaddr mem_changed_start_addr;hwaddr mem_changed_end_addr; +const VhostOps *vhost_ops; };int vhost_dev_init (struct vhost_dev *hdev,int devfd,const char *devpath, -bool force); +VhostBackendType backend_type,bool force);void vhost_dev_cleanup (struct vhost_dev *hdev);bool vhost_dev_query (struct vhost_dev *hdev,VirtIODevice *vdev);int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev);
Signature · 1 line
1.8.3.2
PATCH v3 3/7 Add vhost-user skeleton
In reply to Antonios MotakisAdd empty vhost_call, init and cleanup for the vhost-user backend.
--- hw/net/vhost_net.c | 57 ++++++++++++++++++++++----------------- hw/virtio/vhost-backend.c | 35 ++++++++++++++++++++++++ include/hw/virtio/vhost-backend.h | 3 ++- include/net/vhost_net.h | 13 ++++++++- net/tap.c | 16 ++++++----- 5 files changed, 91 insertions(+), 33 deletions(-)
hw/net/vhost_net.c +32 -25
@@ -91,43 +91,51 @@ static int vhost_net_get_fd(NetClientState *backend) } } -struct vhost_net *vhost_net_init (NetClientState *backend,int devfd, -bool force) +struct vhost_net *vhost_net_init (VhostNetOptions *options) { -int r; +int r =-1 ;struct vhost_net *net =g_malloc (sizeof *net); -if (!backend) { -fprintf (stderr,"vhost-net requires backend to be setup\n" ); + +if (!options->net_backend) { +fprintf (stderr,"vhost-net requires net backend to be setup\n" ); goto fail; } - r =vhost_net_get_fd (backend); -if (r <0 ) { - goto fail; + +if (options->backend_type ==VHOST_BACKEND_TYPE_KERNEL ) { + r =vhost_net_get_fd (options->net_backend); +if (r <0 ) { + goto fail; + } + + net->dev.backend_features = +tap_has_vnet_hdr (options->net_backend) ?0 : + (1 <<VHOST_NET_F_VIRTIO_NET_HDR ); } - net->nc = backend; - net->dev.backend_features =tap_has_vnet_hdr (backend) ?0 : - (1 <<VHOST_NET_F_VIRTIO_NET_HDR ); + + net->nc = options->net_backend; net->backend = r; net->dev.nvqs =2 ; net->dev.vqs = net->vqs; - r =vhost_dev_init (&net->dev, devfd,"/dev/vhost-net" , -VHOST_BACKEND_TYPE_KERNEL , force); + r =vhost_dev_init (&net->dev, options->devfd, options->devpath, + options->backend_type, options->force);if (r <0 ) { goto fail; } -if (!tap_has_vnet_hdr_len (backend, -sizeof (struct virtio_net_hdr_mrg_rxbuf ))) { - net->dev.features &= ~(1 <<VIRTIO_NET_F_MRG_RXBUF ); - } -if (~net->dev.features & net->dev.backend_features) { -fprintf (stderr,"vhost lacks feature mask %" PRIu64" for backend\n" , - (uint64_t )(~net->dev.features & net->dev.backend_features)); -vhost_dev_cleanup (&net->dev); - goto fail; +if (options->backend_type ==VHOST_BACKEND_TYPE_KERNEL ) { +if (!tap_has_vnet_hdr_len (options->net_backend, +sizeof (struct virtio_net_hdr_mrg_rxbuf ))) { + net->dev.features &= ~(1 <<VIRTIO_NET_F_MRG_RXBUF ); + } +if (~net->dev.features & net->dev.backend_features) { +fprintf (stderr,"vhost lacks feature mask %" PRIu64 +" for backend\n" , + (uint64_t )(~net->dev.features & net->dev.backend_features)); +vhost_dev_cleanup (&net->dev); + goto fail; + } } -/* Set sane init value. Override when guest acks. */ vhost_net_ack_features (net,0 );return net;
@@ -286,8 +294,7 @@ void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev, vhost_virtqueue_mask(&net->dev, dev, idx, mask); }#else -struct vhost_net *vhost_net_init (NetClientState *backend,int devfd, -bool force) +struct vhost_net *vhost_net_init (VhostNetOptions *options) {error_report ("vhost-net support is not compiled in" );return NULL ;
hw/virtio/vhost-backend.c +35 -0
@@ -15,6 +15,38 @@#include <unistd.h> #include <sys/ioctl.h> +static int vhost_user_call (struct vhost_dev *dev,unsigned long int request, +void *arg) +{ +assert (dev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_USER ); +fprintf (stderr,"vhost_user_call not implemented\n" ); + +return -1 ; +} + +static int vhost_user_init (struct vhost_dev *dev,const char *devpath) +{ +assert (dev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_USER ); +fprintf (stderr,"vhost_user_init not implemented\n" ); + +return -1 ; +} + +static int vhost_user_cleanup (struct vhost_dev *dev) +{ +assert (dev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_USER ); +fprintf (stderr,"vhost_user_cleanup not implemented\n" ); + +return -1 ; +} + +static const VhostOps user_ops = { + .backend_type =VHOST_BACKEND_TYPE_USER , + .vhost_call = vhost_user_call, + .vhost_backend_init = vhost_user_init, + .vhost_backend_cleanup = vhost_user_cleanup +}; +static int vhost_kernel_call (struct vhost_dev *dev,unsigned long int request,void *arg) {
@@ -55,6 +87,9 @@ int vhost_set_backend_type(struct vhost_dev *dev, VhostBackendType backend_type)case VHOST_BACKEND_TYPE_KERNEL : dev->vhost_ops = &kernel_ops;break ; +case VHOST_BACKEND_TYPE_USER : + dev->vhost_ops = &user_ops; +break ;default :fprintf (stderr,"Unknown vhost backend type\n" ); r =-1 ;
include/hw/virtio/vhost-backend.h +2 -1
@@ -14,7 +14,8 @@typedef enum VhostBackendType {VHOST_BACKEND_TYPE_NONE =0 ,VHOST_BACKEND_TYPE_KERNEL =1 , -VHOST_BACKEND_TYPE_MAX =2 , +VHOST_BACKEND_TYPE_USER =2 , +VHOST_BACKEND_TYPE_MAX =3 , }VhostBackendType ;struct vhost_dev ;
include/net/vhost_net.h +12 -1
@@ -2,11 +2,22 @@#define VHOST_NET_H #include "net/net.h" +#include "hw/virtio/vhost-backend.h" + +#define VHOST_NET_DEFAULT_PATH "/dev/vhost-net"struct vhost_net ;typedef struct vhost_net VHostNetState ; -VHostNetState *vhost_net_init (NetClientState *backend,int devfd,bool force); +typedef struct VhostNetOptions { +VhostBackendType backend_type; +NetClientState *net_backend; +const char *devpath; +int devfd; +bool force; +}VhostNetOptions ; + +struct vhost_net *vhost_net_init (VhostNetOptions *options);bool vhost_net_query (VHostNetState *net,VirtIODevice *dev);int vhost_net_start (VirtIODevice *dev,NetClientState *ncs,int total_queues);
net/tap.c +10 -6
@@ -621,19 +621,23 @@ static int net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,if (tap->has_vhost ? tap->vhost : vhostfdname || (tap->has_vhostforce && tap->vhostforce)) { -int vhostfd; +VhostNetOptions options; + + options.backend_type =VHOST_BACKEND_TYPE_KERNEL ; + options.net_backend = &s->nc; + options.devpath =VHOST_NET_DEFAULT_PATH ; + options.force = tap->has_vhostforce && tap->vhostforce;if (tap->has_vhostfd || tap->has_vhostfds) { - vhostfd =monitor_handle_fd_param (cur_mon, vhostfdname); -if (vhostfd ==-1 ) { + options.devfd =monitor_handle_fd_param (cur_mon, vhostfdname); +if (options.devfd ==-1 ) {return -1 ; } }else { - vhostfd =-1 ; + options.devfd =-1 ; } - s->vhost_net =vhost_net_init (&s->nc, vhostfd, - tap->has_vhostforce && tap->vhostforce); + s->vhost_net =vhost_net_init (&options);if (!s->vhost_net) {error_report ("vhost-net requested but could not be initialized" );return -1 ;
Signature · 1 line
1.8.3.2
PATCH v3 4/7 Add domain socket communication for vhost-user backend
In reply to Antonios MotakisAdd structures for passing vhost-user messages over a unix domain socket. This is the equivalent of the existing vhost-kernel ioctls.
Connect to the named unix domain socket. The system call sendmsg is used for communication. To be able to pass file descriptors between processes - we use SCM_RIGHTS type in the message control header.
--- hw/virtio/vhost-backend.c | 167 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 161 insertions(+), 6 deletions(-)
hw/virtio/vhost-backend.c +161 -6
@@ -14,30 +14,185 @@#include <fcntl.h> #include <unistd.h> #include <sys/ioctl.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <linux/vhost.h> + +#define VHOST_MEMORY_MAX_NREGIONS 8 + +typedef enum VhostUserRequest { +VHOST_USER_NONE =0 , +VHOST_USER_GET_FEATURES =1 , +VHOST_USER_SET_FEATURES =2 , +VHOST_USER_SET_OWNER =3 , +VHOST_USER_RESET_OWNER =4 , +VHOST_USER_SET_MEM_TABLE =5 , +VHOST_USER_SET_LOG_BASE =6 , +VHOST_USER_SET_LOG_FD =7 , +VHOST_USER_SET_VRING_NUM =8 , +VHOST_USER_SET_VRING_ADDR =9 , +VHOST_USER_SET_VRING_BASE =10 , +VHOST_USER_GET_VRING_BASE =11 , +VHOST_USER_SET_VRING_KICK =12 , +VHOST_USER_SET_VRING_CALL =13 , +VHOST_USER_SET_VRING_ERR =14 , +VHOST_USER_NET_SET_BACKEND =15 , +VHOST_USER_MAX +}VhostUserRequest ; + +typedef struct VhostUserMemoryRegion { +__u64 guest_phys_addr; +__u64 memory_size; +__u64 userspace_addr; +}VhostUserMemoryRegion ; + +typedef struct VhostUserMemory { +__u32 nregions; +VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS ]; +}VhostUserMemory ; + +typedef struct VhostUserMsg { +VhostUserRequest request; + +int flags; +union { +uint64_t u64; +int fd; +struct vhost_vring_state state; +struct vhost_vring_addr addr; +struct vhost_vring_file file; + +VhostUserMemory memory; + }; +}VhostUserMsg ; + +static int vhost_user_recv (int fd,VhostUserMsg *msg) +{ +ssize_t r =read (fd, msg,sizeof (VhostUserMsg)); + +return (r ==sizeof (VhostUserMsg)) ?0 :-1 ; +} + +static int vhost_user_send_fds (int fd,const VhostUserMsg *msg,int *fds, +size_t fd_num) +{ +int r; + +struct msghdr msgh; +struct iovec iov[1 ]; + +size_t fd_size = fd_num *sizeof (int ); +char control[CMSG_SPACE (fd_size)]; +struct cmsghdr *cmsg; + +memset (&msgh,0 ,sizeof (msgh)); +memset (control,0 ,sizeof (control)); + +/* set the payload */ + iov[0 ].iov_base = (void *) msg; + iov[0 ].iov_len =sizeof (VhostUserMsg); + + msgh.msg_iov = iov; + msgh.msg_iovlen =1 ; + +if (fd_num) { + msgh.msg_control = control; + msgh.msg_controllen =sizeof (control); + + cmsg =CMSG_FIRSTHDR (&msgh); + + cmsg->cmsg_len =CMSG_LEN (fd_size); + cmsg->cmsg_level =SOL_SOCKET ; + cmsg->cmsg_type =SCM_RIGHTS ; +memcpy (CMSG_DATA (cmsg), fds, fd_size); + }else { + msgh.msg_control =0 ; + msgh.msg_controllen =0 ; + } + +do { + r =sendmsg (fd, &msgh,0 ); + }while (r <0 && errno ==EINTR ); + +if (r <0 ) { +fprintf (stderr,"Failed to send msg(%d), reason: %s\n" , + msg->request,strerror (errno)); + }else { + r =0 ; + } + +return r; +}static int vhost_user_call (struct vhost_dev *dev,unsigned long int request,void *arg) { +int fd = dev->control; +VhostUserMsg msg; +int result =0 , need_reply =0 ; +int fds[VHOST_MEMORY_MAX_NREGIONS ]; +size_t fd_num =0 ; +assert (dev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_USER ); -fprintf (stderr,"vhost_user_call not implemented\n" ); -return -1 ; +switch (request) { +default : +fprintf (stderr,"vhost-user trying to send unhandled ioctl\n" ); +return -1 ; +break ; + } + + result =vhost_user_send_fds (fd, &msg, fds, fd_num); + +if (!result && need_reply) { + result =vhost_user_recv (fd, &msg); +if (!result) { +switch (request) { +default : +break ; + } + } + } + +return result; }static int vhost_user_init (struct vhost_dev *dev,const char *devpath) { +int fd =-1 ; +struct sockaddr_un un; +size_t len; +assert (dev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_USER ); -fprintf (stderr,"vhost_user_init not implemented\n" ); -return -1 ; +/* Create the socket */ + fd =socket (AF_UNIX ,SOCK_STREAM ,0 ); +if (fd ==-1 ) { +perror ("socket" ); +return -1 ; + } + + un.sun_family =AF_UNIX ; +strcpy (un.sun_path, devpath); + + len =sizeof (un.sun_family) +strlen (devpath); + +/* Connect */ +if (connect (fd, (struct sockaddr *) &un, len) ==-1 ) { +perror ("connect" ); +return -1 ; + } + + dev->control = fd; + +return fd; }static int vhost_user_cleanup (struct vhost_dev *dev) {assert (dev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_USER ); -fprintf (stderr,"vhost_user_cleanup not implemented\n" ); -return -1 ; +return close (dev->control); }static const VhostOps user_ops = {
Signature · 1 line
1.8.3.2
PATCH v3 5/7 Add vhost-user calls implementation
In reply to Antonios MotakisEach ioctl request of vhost-kernel has a vhost-user message equivalent, which is sent it over the control socket.
The general approach is to copy the data from the supplied argument pointer to a designated field in the message. If a file descriptor is to be passed it should be placed also in the fds array for inclusion in the sendmsd control header.
VHOST_SET_MEM_TABLE ignores the supplied vhost_memory structure and scans the global ram_list for ram blocks wiht a valid fd field set. This would be set when the -mem-path and -mem-prealloc command line options are used.
--- hw/virtio/vhost-backend.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 98 insertions(+), 1 deletion(-)
hw/virtio/vhost-backend.c +98 -1
@@ -66,6 +66,38 @@ typedef struct VhostUserMsg { }; } VhostUserMsg; +static unsigned long int ioctl_to_vhost_user_request[VHOST_USER_MAX ] = { +-1 ,/* VHOST_USER_NONE */ +VHOST_GET_FEATURES ,/* VHOST_USER_GET_FEATURES */ +VHOST_SET_FEATURES ,/* VHOST_USER_SET_FEATURES */ +VHOST_SET_OWNER ,/* VHOST_USER_SET_OWNER */ +VHOST_RESET_OWNER ,/* VHOST_USER_RESET_OWNER */ +VHOST_SET_MEM_TABLE ,/* VHOST_USER_SET_MEM_TABLE */ +VHOST_SET_LOG_BASE ,/* VHOST_USER_SET_LOG_BASE */ +VHOST_SET_LOG_FD ,/* VHOST_USER_SET_LOG_FD */ +VHOST_SET_VRING_NUM ,/* VHOST_USER_SET_VRING_NUM */ +VHOST_SET_VRING_ADDR ,/* VHOST_USER_SET_VRING_ADDR */ +VHOST_SET_VRING_BASE ,/* VHOST_USER_SET_VRING_BASE */ +VHOST_GET_VRING_BASE ,/* VHOST_USER_GET_VRING_BASE */ +VHOST_SET_VRING_KICK ,/* VHOST_USER_SET_VRING_KICK */ +VHOST_SET_VRING_CALL ,/* VHOST_USER_SET_VRING_CALL */ +VHOST_SET_VRING_ERR ,/* VHOST_USER_SET_VRING_ERR */ +VHOST_NET_SET_BACKEND /* VHOST_USER_NET_SET_BACKEND */ +}; + +static VhostUserRequest vhost_user_request_translate (unsigned long int request) +{ +VhostUserRequest idx; + +for (idx =0 ; idx <VHOST_USER_MAX ; idx++) { +if (ioctl_to_vhost_user_request[idx] == request) { +break ; + } + } + +return (idx ==VHOST_USER_MAX ) ?VHOST_USER_NONE : idx; +} +static int vhost_user_recv (int fd,VhostUserMsg *msg) {ssize_t r =read (fd, msg,sizeof (VhostUserMsg));
@@ -129,13 +161,74 @@ static int vhost_user_call(struct vhost_dev *dev, unsigned long int request, {int fd = dev->control;VhostUserMsg msg; +RAMBlock *block =0 ;int result =0 , need_reply =0 ;int fds[VHOST_MEMORY_MAX_NREGIONS ];size_t fd_num =0 ;assert (dev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_USER ); + msg.request =vhost_user_request_translate (request); + msg.flags =0 ; +switch (request) { +case VHOST_GET_FEATURES : +case VHOST_GET_VRING_BASE : + need_reply =1 ; +break ; + +case VHOST_SET_FEATURES : +case VHOST_SET_LOG_BASE : + msg.u64 = *((uint64_t *) arg); +break ; + +case VHOST_SET_OWNER : +case VHOST_RESET_OWNER : +break ; + +case VHOST_SET_MEM_TABLE : + QTAILQ_FOREACH(block, &ram_list.blocks, next) + { +if (block->fd >0 ) { + msg.memory.regions[fd_num].userspace_addr = (__u64 ) block->host; + msg.memory.regions[fd_num].memory_size = block->length; + msg.memory.regions[fd_num].guest_phys_addr = block->offset; + fds[fd_num++] = block->fd; + } + } + + msg.memory.nregions = fd_num; + +if (!fd_num) { +fprintf (stderr,"Failed initializing vhost-user memory map\n" +"consider -mem-path and -mem-prealloc options\n" ); +return -1 ; + } +break ; + +case VHOST_SET_LOG_FD : + msg.fd = *((int *) arg); +break ; + +case VHOST_SET_VRING_NUM : +case VHOST_SET_VRING_BASE : +memcpy (&msg.state, arg,sizeof (struct vhost_vring_state )); +break ; + +case VHOST_SET_VRING_ADDR : +memcpy (&msg.addr, arg,sizeof (struct vhost_vring_addr )); +break ; + +case VHOST_SET_VRING_KICK : +case VHOST_SET_VRING_CALL : +case VHOST_SET_VRING_ERR : +case VHOST_NET_SET_BACKEND : +memcpy (&msg.file, arg,sizeof (struct vhost_vring_file )); +if (msg.file.fd >0 ) { + fds[0 ] = msg.file.fd; + fd_num =1 ; + } +break ;default :fprintf (stderr,"vhost-user trying to send unhandled ioctl\n" );return -1 ;
@@ -148,7 +241,11 @@ static int vhost_user_call(struct vhost_dev *dev, unsigned long int request, result =vhost_user_recv (fd, &msg);if (!result) {switch (request) { -default : +case VHOST_GET_FEATURES : + *((uint64_t *) arg) = msg.u64; +break ; +case VHOST_GET_VRING_BASE : +memcpy (arg, &msg.state,sizeof (struct vhost_vring_state ));break ; } }
Signature · 1 line
1.8.3.2
Re: [Qemu-devel] [PATCH v3 5/7] Add vhost-user calls implementation
Cool stuff :-)
some thoughts:
Quoted 3 lines · Antonios Motakis
static int
vhost_user_recv(int fd, VhostUserMsg *msg){ ssize_t r = read(fd, msg, sizeof(VhostUserMsg));
Is it worth considering a "timeout and reconnect" check here? I mean so that if the vhost server does not respond for any reason the guess will see link down instead of freezing the hypervisor (?).
hw/virtio/vhost-backend.c static int vhost_user_call(struct vhost_dev *dev, unsigned long int request, · line 220
Quoted 3 lines
hw/virtio/vhost-backend.c +3
-0+case VHOST_SET_VRING_ADDR : +memcpy (&msg.addr, arg,sizeof (struct vhost_vring_addr )); +break ;
I think these vring addresses need to be remapped from qemu address space to guest-physical address space.
PATCH v3 6/7 Add new vhost-user netdev backend
In reply to Antonios MotakisAdd a new QEMU netdev backend that is intended to invoke vhost_net with the vhost-user backend. Also decouple virtio-net from the tap backend.
--- hmp-commands.hx | 4 +- hw/net/vhost_net.c | 66 ++++++++++++++++++++++------ hw/net/virtio-net.c | 42 ++++++++---------- hw/virtio/vhost.c | 1 - include/net/vhost-user.h | 17 ++++++++ include/net/vhost_net.h | 1 + net/Makefile.objs | 2 +- net/clients.h | 3 ++ net/hub.c | 1 + net/net.c | 2 + net/vhost-user.c | 111 +++++++++++++++++++++++++++++++++++++++++++++++ qapi-schema.json | 18 +++++++- qemu-options.hx | 3 ++ 13 files changed, 227 insertions(+), 44 deletions(-) create mode 100644 include/net/vhost-user.h create mode 100644 net/vhost-user.c
hmp-commands.hx +2 -2
@@ -1190,7 +1190,7 @@ ETEXI { .name = "host_net_add", .args_type = "device:s,opts:s?", - .params = "tap|user|socket|vde|netmap|dump [options]", + .params = "tap|user|socket|vde|netmap|vhost-user|dump [options]", .help = "add host VLAN client", .mhandler.cmd = net_host_device_add, },
@@ -1218,7 +1218,7 @@ ETEXI { .name = "netdev_add", .args_type = "netdev:O", - .params = "[user|tap|socket|hubport|netmap],id=str[,prop=value][,...]", + .params = "[user|tap|socket|hubport|netmap|vhost-user],id=str[,prop=value][,...]", .help = "add host network device", .mhandler.cmd = hmp_netdev_add, },
hw/net/vhost_net.c +52 -14
118 lines not on this page · raw message
hw/net/virtio-net.c +17 -25
89 lines not on this page · raw message
hw/virtio/vhost.c +0 -1
@@ -14,7 +14,6 @@*/ #include "hw/virtio/vhost.h" -#include "hw/virtio/vhost-backend.h" #include "hw/hw.h" #include "qemu/atomic.h" #include "qemu/range.h"
include/net/vhost-user.h new file +17 -0
@@ -0,0 +1,17 @@ +/* +* vhost-user.h +* +* Copyright (c) 2013 Virtual Open Systems Sarl. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +*/ + +#ifndef VHOST_USER_H_ +#define VHOST_USER_H_ + +struct vhost_net ; +struct vhost_net *vhost_user_get_vhost_net (NetClientState *nc); + +#endif /* VHOST_USER_H_ */
include/net/vhost_net.h +1 -0
@@ -31,4 +31,5 @@ void vhost_net_ack_features(VHostNetState *net, unsigned features);bool vhost_net_virtqueue_pending (VHostNetState *net,int n);void vhost_net_virtqueue_mask (VHostNetState *net,VirtIODevice *dev,int idx,bool mask); +VHostNetState *get_vhost_net (NetClientState *nc);#endif
net/Makefile.objs +1 -1
@@ -2,7 +2,7 @@ common-obj-y = net.o queue.o checksum.o util.o hub.o common-obj-y += socket.o common-obj-y += dump.o common-obj-y += eth.o -common-obj-$(CONFIG_POSIX) += tap.o +common-obj-$(CONFIG_POSIX) += tap.o vhost-user.o common-obj-$(CONFIG_LINUX) += tap-linux.o common-obj-$(CONFIG_WIN32) += tap-win32.o common-obj-$(CONFIG_BSD) += tap-bsd.o
net/clients.h +3 -0
@@ -57,4 +57,7 @@ int net_init_netmap(const NetClientOptions *opts, const char *name, NetClientState *peer);#endif +int net_init_vhost_user (const NetClientOptions *opts,const char *name, +NetClientState *peer); +#endif /* QEMU_NET_CLIENTS_H */
net/hub.c +1 -0
@@ -322,6 +322,7 @@ void net_hub_check_clients(void)case NET_CLIENT_OPTIONS_KIND_TAP :case NET_CLIENT_OPTIONS_KIND_SOCKET :case NET_CLIENT_OPTIONS_KIND_VDE : +case NET_CLIENT_OPTIONS_KIND_VHOST_USER : has_host_dev =1 ;break ;default :
net/net.c +2 -0
@@ -734,6 +734,7 @@ static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX])( [NET_CLIENT_OPTIONS_KIND_BRIDGE] = net_init_bridge, #endif [NET_CLIENT_OPTIONS_KIND_HUBPORT] = net_init_hubport, + [NET_CLIENT_OPTIONS_KIND_VHOST_USER] = net_init_vhost_user, };
@@ -767,6 +768,7 @@ static int net_client_init1(const void *object, int is_netdev, Error **errp)case NET_CLIENT_OPTIONS_KIND_BRIDGE :#endif case NET_CLIENT_OPTIONS_KIND_HUBPORT : +case NET_CLIENT_OPTIONS_KIND_VHOST_USER :break ;default :
net/vhost-user.c new file +111 -0
111 lines not on this page · raw message
qapi-schema.json +17 -1
@@ -3030,6 +3030,21 @@ '*devname': 'str' } } ## +# @NetdevVhostUserOptions +# +# Vhost-user network backend +# +# @file: control socket path +# +# Since 2.0 +## +{ 'type': 'NetdevVhostUserOptions', + 'data': { + 'file': 'str' } } + +## + +## # @NetClientOptions # # A discriminated record of network device traits.
@@ -3047,7 +3062,8 @@ 'dump': 'NetdevDumpOptions', 'bridge': 'NetdevBridgeOptions', 'hubport': 'NetdevHubPortOptions', - 'netmap': 'NetdevNetmapOptions' } } + 'netmap': 'NetdevNetmapOptions', + 'vhost-user': 'NetdevVhostUserOptions' } } ## # @NetLegacy
qemu-options.hx +3 -0
@@ -1422,6 +1422,8 @@ DEF("net", HAS_ARG, QEMU_OPTION_net, " VALE port (created on the fly) called 'name' ('nmname' is name of the \n" " netmap device, defaults to '/dev/netmap')\n" #endif + "-net vhost-user,file=name\n" + " connect to a unix domain socket implementing vhost-user backend\n" "-net dump[,vlan=n][,file=f][,len=n]\n" " dump traffic on vlan 'n' to file 'f' (max n bytes per packet)\n" "-net none use it alone to have zero network devices. If no -net option\n"
@@ -1439,6 +1441,7 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev, #ifdef CONFIG_NETMAP "netmap|" #endif + "vhost-user|" "socket|" "hubport],id=str[,option][,option][,...]\n", QEMU_ARCH_ALL) STEXI
Signature · 1 line
1.8.3.2
PATCH v3 7/7 Add vhost-user reconnection
In reply to Antonios MotakisAt runtime vhost-user netdev will detect if the vhost backend is up or down. Upon disconnection it will set link_down accordingly and notify virtio-net.
--- hw/net/vhost_net.c | 16 +++++++++++ hw/virtio/vhost-backend.c | 21 +++++++++++++++ include/hw/virtio/vhost-backend.h | 2 ++ include/net/vhost_net.h | 1 + net/vhost-user.c | 56 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 96 insertions(+)
hw/net/vhost_net.c +16 -0
@@ -304,6 +304,17 @@ void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,vhost_virtqueue_mask (&net->dev, dev, idx, mask); } +int vhost_net_link_status (VHostNetState *net) +{ +int r =0 ; + +if (net->dev.vhost_ops->vhost_status) { + r = net->dev.vhost_ops->vhost_status (&net->dev); + } + +return r; +} +VHostNetState *get_vhost_net (NetClientState *nc) {VHostNetState *vhost_net =0 ;
@@ -372,6 +383,11 @@ void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev, { } +int vhost_net_link_status (VHostNetState *net) +{ +return 0 ; +} +VHostNetState *get_vhost_net (NetClientState *nc) {return 0 ;
hw/virtio/vhost-backend.c +21 -0
@@ -168,6 +168,10 @@ static int vhost_user_call(struct vhost_dev *dev, unsigned long int request,assert (dev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_USER ); +if (fd <0 ) { +return 0 ; + } + msg.request =vhost_user_request_translate (request); msg.flags =0 ;
@@ -251,9 +255,24 @@ static int vhost_user_call(struct vhost_dev *dev, unsigned long int request, } } +/* mark the backend non operational */ +if (result <0 ) { + dev->control =-1 ; +return 0 ; + } +return result; } +static int vhost_user_status (struct vhost_dev *dev) +{ +uint64_t features =0 ; + +vhost_user_call (dev,VHOST_GET_FEATURES , &features); + +return (dev->control >=0 ); +} +static int vhost_user_init (struct vhost_dev *dev,const char *devpath) {int fd =-1 ;
@@ -295,6 +314,7 @@ static int vhost_user_cleanup(struct vhost_dev *dev)static const VhostOps user_ops = { .backend_type =VHOST_BACKEND_TYPE_USER , .vhost_call = vhost_user_call, + .vhost_status = vhost_user_status, .vhost_backend_init = vhost_user_init, .vhost_backend_cleanup = vhost_user_cleanup };
@@ -327,6 +347,7 @@ static int vhost_kernel_cleanup(struct vhost_dev *dev)static const VhostOps kernel_ops = { .backend_type =VHOST_BACKEND_TYPE_KERNEL , .vhost_call = vhost_kernel_call, + .vhost_status =0 , .vhost_backend_init = vhost_kernel_init, .vhost_backend_cleanup = vhost_kernel_cleanup };
include/hw/virtio/vhost-backend.h +2 -0
@@ -22,12 +22,14 @@ struct vhost_dev;typedef int (*vhost_call )(struct vhost_dev *dev,unsigned long int request,void *arg); +typedef int (*vhost_status )(struct vhost_dev *dev);typedef int (*vhost_backend_init )(struct vhost_dev *dev,const char *devpath);typedef int (*vhost_backend_cleanup )(struct vhost_dev *dev);typedef struct VhostOps {VhostBackendType backend_type;vhost_call vhost_call; +vhost_status vhost_status;vhost_backend_init vhost_backend_init;vhost_backend_cleanup vhost_backend_cleanup; }VhostOps ;
include/net/vhost_net.h +1 -0
@@ -31,5 +31,6 @@ void vhost_net_ack_features(VHostNetState *net, unsigned features);bool vhost_net_virtqueue_pending (VHostNetState *net,int n);void vhost_net_virtqueue_mask (VHostNetState *net,VirtIODevice *dev,int idx,bool mask); +int vhost_net_link_status (VHostNetState *net);VHostNetState *get_vhost_net (NetClientState *nc);#endif
net/vhost-user.c +56 -0
86 lines not on this page · raw message
Signature · 1 line
1.8.3.2
Re: [Qemu-devel] [PATCH v3 7/7] Add vhost-user reconnection
Quoted 2 lines · Antonios Motakis
At runtime vhost-user netdev will detect if the vhost backend is up or down. Upon disconnection it will set
link_downaccordingly and notify virtio-net.
Based on inspection with 'lsof' I think that the v3 reconnect mechanism leaks socket file descriptors.