Vhost and vhost-net support for userspace based backends
Subsystems (2)
v9 cover Vhost 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 realized by 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. For this communication we use a chardev with a Unix domain socket backend. Vhost-user is client/server agnostic regarding the chardev, however it does not support the 'nowait' and 'telnet' options.
A reconnection in 'server' mode is supported, but the backend's exposed virtio features need to be compatible with the first connected slave.
We change -mem-path to QemuOpts and add prealloc and share as properties to it. HugeTLBFS is required for this option to work.
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-path /hugetlbfs,share=on \
-chardev socket,id=chr0,path=/path/to/
socket \
-netdev type=vhost-user,id=net0,chardev=chr0 \
-device virtio-net-pci,netdev=net0On non-MSIX guests the vhost feture can be forced using a special option:
...
-netdev type=vhost-user,id=net0,chardev=chr0,vhostforce
...
In order to use ioeventfds, kvm should be enabled.
This code can be pulled from [email protected]:virtualopensystems/qemu.git vhost-user-v9 A simple functional test is available in tests/vhost-user-test.c
A reference vhost-user slave for testing is also available from [email protected]:virtualopensystems/vapp.git
Changes from v8:
- Removed prealloc property from the -mem-path refactoring
- Added and use new function - kvm_eventfds_enabled
- Add virtio_queue_get_avail_idx used in vhost_virtqueue_stop to get a sane value in case of VHOST_GET_VRING_BASE failure
- vhost user uses kvm_eventfds_enabled to check whether the ioeventfd capability of KVM is available
- Added flag VHOST_USER_VRING_NOFD_MASK to be set when KICK, CALL or ERR file descriptor is invalid or ioeventfd is not available
Changes from v7:
- Slave reconnection when using chardev in server mode
- qtest vhost-user-test added
- New qemu_chr_fe_get_msgfds for reading multiple fds from the chardev
- Mandatory features in vhost_dev, used on reconnect to verify for conflicts
- Add vhostforce parameter to -netdev vhost-user (for non-MSIX guests)
- Extend libqemustub.a to support qemu-char.c
Changes from v6:
- Remove the 'unlink' property of '-mem-path'
- Extend qemu-char: blocking read, send fds, monitor for connection close
- Vhost-user uses chardev as a backend
- Poll and reconnect removed (no VHOST_USER_ECHO).
- Disconnect is deteced by the chardev (G_IO_HUP event)
- vhost-backend.c split to vhost-user.c
Changes from v5:
- Split -mem-path unlink option to a separate patch
- Fds are passed only in the ancillary data
- Stricter message size checks on receive/send
- Netdev vhost-user now includes path and poll_time options
- The connection probing interval is configurable
Changes from v4:
- Use error_report for errors
- VhostUserMsg has new field size indicating the following payload length. Field flags now has version and reply bits. The structure is packed.
- Send data is of variable length (size field in message)
- Receive in 2 steps, header and payload
- Add new message type VHOST_USER_ECHO, to check connection status
Changes from v3:
- Convert -mem-path to QemuOpts with prealloc, share and unlink properties
- Set 1 sec timeout when read/write to the unix domain socket
- Fix file descriptor leak
Changes from v2:
- Reconnect when the backend disappears
Changes from v1:
- Implementation of vhost-user netdev backend
- Code improvements
Antonios Motakis (20):
Convert -mem-path to QemuOpts and add share property Add kvm_eventfds_enabled function
Add chardev API qemu_chr_fe_read_all
Add chardev API qemu_chr_fe_set_msgfds
Add chardev API qemu_chr_fe_get_msgfds
Add G_IO_HUP handler for socket chardev
vhost_net should call the poll callback only when it is set Refactor virtio-net to use generic get_vhost_net
Add new virtio API virtio_queue_get_avail_idx Gracefully handle ioctl failure in vhost_virtqueue_stop vhost_net_init will use VhostNetOptions to get all its arguments Add vhost_ops to vhost_dev struct and replace all relevant ioctls Add mandatory_features to vhost_dev
Add vhost-backend and VhostBackendType
Add vhost-user as a vhost backend.
Add new vhost-user netdev backend
Add the vhost-user netdev backend to the command line Add vhost-user protocol documentation
libqemustub: add stubs to be able to use qemu-char.c Add qtest for vhost-user
docs/specs/vhost-user.txt | 261 ++++++++++++++++++++++++++++ exec.c | 21 ++- hmp-commands.hx | 4 +- hw/net/vhost_net.c | 141 ++++++++++----- hw/net/virtio-net.c | 29 +--- hw/scsi/vhost-scsi.c | 20 ++- hw/virtio/Makefile.objs | 2 +- hw/virtio/vhost-backend.c | 71 ++++++++ hw/virtio/vhost-user.c | 356 ++++++++++++++++++++++++++++++++++++++ hw/virtio/vhost.c | 58 +++---- hw/virtio/virtio.c | 5 + include/exec/cpu-all.h | 1 - include/hw/virtio/vhost-backend.h | 38 ++++ include/hw/virtio/vhost.h | 9 +- include/hw/virtio/virtio.h | 1 + include/net/vhost-user.h | 17 ++ include/net/vhost_net.h | 13 +- include/sysemu/char.h | 43 ++++- include/sysemu/kvm.h | 11 ++ kvm-all.c | 4 + kvm-stub.c | 1 + net/Makefile.objs | 2 +- net/clients.h | 3 + net/hub.c | 1 + net/net.c | 25 +-- net/tap.c | 18 +- net/vhost-user.c | 273 +++++++++++++++++++++++++++++ qapi-schema.json | 19 +- qemu-char.c | 272 +++++++++++++++++++++++++---- qemu-options.hx | 25 ++- stubs/Makefile.objs | 8 + stubs/bdrv-commit-all.c | 7 + stubs/chr-msmouse.c | 7 + stubs/get-next-serial.c | 3 + stubs/is-daemonized.c | 7 + stubs/machine-init-done.c | 6 + stubs/monitor-init.c | 6 + stubs/notify-event.c | 6 + stubs/vc-init.c | 7 + tests/Makefile | 4 + tests/vhost-user-test.c | 309 +++++++++++++++++++++++++++++++++ vl.c | 23 ++- 42 files changed, 1979 insertions(+), 158 deletions(-) create mode 100644 docs/specs/vhost-user.txt create mode 100644 hw/virtio/vhost-backend.c create mode 100644 hw/virtio/vhost-user.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 create mode 100644 stubs/bdrv-commit-all.c create mode 100644 stubs/chr-msmouse.c create mode 100644 stubs/get-next-serial.c create mode 100644 stubs/is-daemonized.c create mode 100644 stubs/machine-init-done.c create mode 100644 stubs/monitor-init.c create mode 100644 stubs/notify-event.c create mode 100644 stubs/vc-init.c create mode 100644 tests/vhost-user-test.c
Signature · 1 line
1.8.3.2
PATCH v9 1/20 Convert -mem-path to QemuOpts and add share property
Extend -mem-path with additional property:
- share=on|off - default off, memory is mmapped with MAP_SHARED flag
--- exec.c | 21 +++++++++++++++++++-- include/exec/cpu-all.h | 1 - qemu-options.hx | 8 ++++++-- vl.c | 23 +++++++++++++++++++++-- 4 files changed, 46 insertions(+), 7 deletions(-)
exec.c +19 -2
@@ -1027,7 +1027,10 @@ static void *file_ram_alloc(RAMBlock *block,char *c;void *area;int fd; +int flags;unsigned long hpagesize; +QemuOpts *opts; +unsigned int mem_share =0 ; hpagesize =gethugepagesize (path);if (!hpagesize) {
@@ -1043,6 +1046,12 @@ static void *file_ram_alloc(RAMBlock *block,return NULL ; } +/* Fill config options */ + opts =qemu_opts_find (qemu_find_opts ("mem-path" ),NULL ); +if (opts) { + mem_share =qemu_opt_get_bool (opts,"share" ,0 ); + } +/* Make name safe to use with mkstemp by replacing '/' with '_'. */ sanitized_name =g_strdup (block->mr->name);for (c = sanitized_name; *c !='\0' ; c++) {
@@ -1063,7 +1072,7 @@ static void *file_ram_alloc(RAMBlock *block,unlink (filename);g_free (filename); - memory = (memory+hpagesize-1 ) & ~(hpagesize-1 ); + memory = (memory + hpagesize -1 ) & ~(hpagesize -1 );/* * ftruncate is not supported by hugetlbfs in older
@@ -1074,7 +1083,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);
@@ -1244,6 +1254,8 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host, MemoryRegion *mr) {RAMBlock *block, *new_block; +QemuOpts *opts; +const char *mem_path =0 ;ram_addr_t old_ram_size, new_ram_size; old_ram_size =last_ram_offset () >>TARGET_PAGE_BITS ;
@@ -1252,6 +1264,11 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host, new_block =g_malloc0 (sizeof (*new_block)); new_block->fd =-1 ; + opts =qemu_opts_find (qemu_find_opts ("mem-path" ),NULL ); +if (opts) { + mem_path =qemu_opt_get (opts,"path" ); + } +/* This assumes the iothread lock is taken here too. */ qemu_mutex_lock_ramlist (); new_block->mr = mr;
include/exec/cpu-all.h +0 -1
@@ -468,7 +468,6 @@ typedef struct RAMList { } RAMList;extern RAMList ram_list; -extern const char *mem_path;extern int mem_prealloc;/* Flags stored in the low bits of the TLB virtual address. These are
qemu-options.hx +6 -2
@@ -221,9 +221,13 @@ gigabytes respectively. ETEXI DEF("mem-path", HAS_ARG, QEMU_OPTION_mempath, - "-mem-path FILE provide backing storage for guest RAM\n", QEMU_ARCH_ALL) + "-mem-path [path=]path[,share=on|off]\n" + " provide backing storage for guest RAM\n" + " path= a directory path for the backing store\n" + " share= enable mmap share flag [default disabled]\n", + QEMU_ARCH_ALL) STEXI -@item -mem-path @var{path} +@item -mem-path [path=]@var{path}[,share=on|off] @findex -mem-path Allocate guest RAM from a temporarily created file in @var{path}. ETEXI
vl.c +21 -2
@@ -135,7 +135,6 @@ DisplayType display_type = DT_DEFAULT;static int display_remote;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 nb_nics;NICInfo nd_table[MAX_NICS ];
@@ -479,6 +478,23 @@ static QemuOptsList qemu_msg_opts = { }, }; +static QemuOptsList qemu_mem_path_opts = { + .name ="mem-path" , + .implied_opt_name ="path" , + .head =QTAILQ_HEAD_INITIALIZER (qemu_mem_path_opts.head), + .desc = { + { + .name ="path" , + .type =QEMU_OPT_STRING , + }, + { + .name ="share" , + .type =QEMU_OPT_BOOL , + }, + {/* end of list */ } + }, +}; +/** * Get machine options *
@@ -2863,6 +2879,7 @@ int main(int argc, char **argv, char **envp)qemu_add_opts (&qemu_tpmdev_opts);qemu_add_opts (&qemu_realtime_opts);qemu_add_opts (&qemu_msg_opts); +qemu_add_opts (&qemu_mem_path_opts);runstate_init ();
@@ -3190,7 +3207,9 @@ int main(int argc, char **argv, char **envp)break ;#endif case QEMU_OPTION_mempath: - mem_path = optarg; +if (!qemu_opts_parse (qemu_find_opts ("mem-path" ), optarg,1 )) { +exit (1 ); + }break ;case QEMU_OPTION_mem_prealloc: mem_prealloc =1 ;
Signature · 1 line
1.8.3.2
PATCH v9 2/20 Add kvm_eventfds_enabled function
In reply to Antonios MotakisAdd a function to check if the eventfd capability is present in KVM in the host kernel.
--- include/sysemu/kvm.h | 11 +++++++++++ kvm-all.c | 4 ++++ kvm-stub.c | 1 + 3 files changed, 16 insertions(+)
include/sysemu/kvm.h +11 -0
@@ -43,6 +43,7 @@ extern bool kvm_allowed;extern bool kvm_kernel_irqchip;extern bool kvm_async_interrupts_allowed;extern bool kvm_halt_in_kernel_allowed; +extern bool kvm_eventfds_allowed;extern bool kvm_irqfds_allowed;extern bool kvm_msi_via_irqfd_allowed;extern bool kvm_gsi_routing_allowed;
@@ -83,6 +84,15 @@ extern bool kvm_readonly_mem_allowed;#define kvm_halt_in_kernel () (kvm_halt_in_kernel_allowed)/** +* kvm_eventfds_enabled: +* +* Returns: true if we can use eventfds to receive notifications +* from a KVM CPU (ie the kernel supports eventds and we are running +* with a configuration where it is meaningful to use them). +*/ +#define kvm_eventfds_enabled () (kvm_eventfds_allowed) + +/** * kvm_irqfds_enabled: * * Returns: true if we can use irqfds to inject interrupts into
@@ -128,6 +138,7 @@ extern bool kvm_readonly_mem_allowed;#define kvm_irqchip_in_kernel () (false)#define kvm_async_interrupts_enabled () (false)#define kvm_halt_in_kernel () (false) +#define kvm_eventfds_enabled () (false)#define kvm_irqfds_enabled () (false)#define kvm_msi_via_irqfd_enabled () (false)#define kvm_gsi_routing_allowed () (false)
kvm-all.c +4 -0
@@ -110,6 +110,7 @@ KVMState *kvm_state;bool kvm_kernel_irqchip;bool kvm_async_interrupts_allowed;bool kvm_halt_in_kernel_allowed; +bool kvm_eventfds_allowed;bool kvm_irqfds_allowed;bool kvm_msi_via_irqfd_allowed;bool kvm_gsi_routing_allowed;
@@ -1505,6 +1506,9 @@ int kvm_init(void) (kvm_check_extension (s,KVM_CAP_READONLY_MEM ) >0 );#endif + kvm_eventfds_allowed = + (kvm_check_extension (s,KVM_CAP_IOEVENTFD ) >0 ); + ret =kvm_arch_init (s);if (ret <0 ) { goto err;
kvm-stub.c +1 -0
@@ -22,6 +22,7 @@KVMState *kvm_state;bool kvm_kernel_irqchip;bool kvm_async_interrupts_allowed; +bool kvm_eventfds_allowed;bool kvm_irqfds_allowed;bool kvm_msi_via_irqfd_allowed;bool kvm_gsi_routing_allowed;
Signature · 1 line
1.8.3.2
PATCH v9 3/20 Add chardev API qemu_chr_fe_read_all
In reply to Antonios MotakisThis function will attempt to read data from the chardev trying to fill the buffer up to the given length.
Add tcp_chr_disconnect to reuse disconnection code where needed.
--- include/sysemu/char.h | 14 +++++++++ qemu-char.c | 83 ++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 87 insertions(+), 10 deletions(-)
include/sysemu/char.h +14 -0
@@ -56,6 +56,8 @@ typedef void IOEventHandler(void *opaque, int event);struct CharDriverState {void (*init)(struct CharDriverState *s);int (*chr_write)(struct CharDriverState *s,const uint8_t *buf,int len); +int (*chr_sync_read)(struct CharDriverState *s, +const uint8_t *buf,int len);GSource *(*chr_add_watch)(struct CharDriverState *s,GIOCondition cond);void (*chr_update_read_handler)(struct CharDriverState *s);int (*chr_ioctl)(struct CharDriverState *s,int cmd,void *arg);
@@ -189,6 +191,18 @@ int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len);int qemu_chr_fe_write_all (CharDriverState *s,const uint8_t *buf,int len);/** +* @qemu_chr_fe_read_all: +* +* Read data to a buffer from the back end. +* +* @buf the data buffer +* @len the number of bytes to read +* +* Returns: the number of bytes read +*/ +int qemu_chr_fe_read_all (CharDriverState *s,uint8_t *buf,int len); + +/** * @qemu_chr_fe_ioctl: * * Issue a device specific ioctl to a backend.
qemu-char.c +73 -10
119 lines not on this page · raw message
Signature · 1 line
1.8.3.2
PATCH v9 4/20 Add chardev API qemu_chr_fe_set_msgfds
In reply to Antonios MotakisThis will set an array of file descriptors to the internal structures. The next time a message is send the array will be send as ancillary data. This feature works on the UNIX domain socket backend only.
--- include/sysemu/char.h | 14 ++++++++ qemu-char.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 101 insertions(+), 1 deletion(-)
include/sysemu/char.h +14 -0
@@ -62,6 +62,7 @@ struct CharDriverState {void (*chr_update_read_handler)(struct CharDriverState *s);int (*chr_ioctl)(struct CharDriverState *s,int cmd,void *arg);int (*get_msgfd)(struct CharDriverState *s); +int (*set_msgfds)(struct CharDriverState *s,int *fds,int num);int (*chr_add_client)(struct CharDriverState *chr,int fd);IOEventHandler *chr_event;IOCanReadHandler *chr_can_read;
@@ -229,6 +230,19 @@ int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg);int qemu_chr_fe_get_msgfd (CharDriverState *s);/** +* @qemu_chr_fe_set_msgfds: +* +* For backends capable of fd passing, set an array of fds to be passed with +* the next send operation. +* A subsequent call to this function before calling a write function will +* result in overwriting the fd array with the new value without being send. +* Upon writing the message the fd array is freed. +* +* Returns: -1 if fd passing isn't supported. +*/ +int qemu_chr_fe_set_msgfds (CharDriverState *s,int *fds,int num); + +/** * @qemu_chr_fe_claim: * * Claim a backend before using it, should be called before calling
qemu-char.c +87 -1
132 lines not on this page · raw message
Signature · 1 line
1.8.3.2
PATCH v9 5/20 Add chardev API qemu_chr_fe_get_msgfds
In reply to Antonios MotakisThis extends the existing qemu_chr_fe_get_msgfd by allowing to read a set of fds. The function for receiving the fds - unix_process_msgfd is extended to allocate the needed array size.
--- include/sysemu/char.h | 15 ++++++++- qemu-char.c | 85 ++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 78 insertions(+), 22 deletions(-)
include/sysemu/char.h +14 -1
@@ -61,7 +61,7 @@ struct CharDriverState {GSource *(*chr_add_watch)(struct CharDriverState *s,GIOCondition cond);void (*chr_update_read_handler)(struct CharDriverState *s);int (*chr_ioctl)(struct CharDriverState *s,int cmd,void *arg); -int (*get_msgfd)(struct CharDriverState *s); +int (*get_msgfds)(struct CharDriverState *s,int * fds,int num);int (*set_msgfds)(struct CharDriverState *s,int *fds,int num);int (*chr_add_client)(struct CharDriverState *chr,int fd);IOEventHandler *chr_event;
@@ -230,6 +230,19 @@ int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg);int qemu_chr_fe_get_msgfd (CharDriverState *s);/** +* @qemu_chr_fe_get_msgfds: +* +* For backends capable of fd passing, return the number of file received +* descriptors and fills the fds array up to num elements +* +* Returns: -1 if fd passing isn't supported or there are no pending file +* descriptors. If file descriptors are returned, subsequent calls to +* this function will return -1 until a client sends a new set of file +* descriptors. +*/ +int qemu_chr_fe_get_msgfds (CharDriverState *s,int *fds,int num); + +/** * @qemu_chr_fe_set_msgfds: * * For backends capable of fd passing, set an array of fds to be passed with
qemu-char.c +64 -21
142 lines not on this page · raw message
Signature · 1 line
1.8.3.2
Paolo Bonzini
maintainerIl 04/03/2014 19:22, Antonios Motakis ha scritto:
Quoted 50 lines
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 realized by 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. For this communication we use a chardev with a Unix domain socket backend. Vhost-user is client/server agnostic regarding the chardev, however it does not support the 'nowait' and 'telnet' options.
A reconnection in 'server' mode is supported, but the backend's exposed virtio features need to be compatible with the first connected slave.
We change -mem-path to QemuOpts and add prealloc and share as properties to it. HugeTLBFS is required for this option to work.
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-path /hugetlbfs,share=on \
-chardev socket,id=chr0,path=/path/to/socket \ -netdev type=vhost-user,id=net0,chardev=chr0 \ -device virtio-net-pci,netdev=net0On non-MSIX guests the vhost feture can be forced using a special option:
...
-netdev type=vhost-user,id=net0,chardev=chr0,vhostforce
...
Quoted 6 lines
In order to use ioeventfds, kvm should be enabled.
This code can be pulled from [email protected]:virtualopensystems/qemu.git vhost-user-v9 A simple functional test is available in
tests/vhost-user-test.cA reference vhost-user slave for testing is also available from [email protected]:virtualopensystems/vapp.git
Hi,
did you see the series I posted today? It would be great if you tested vhost-user on top of it. The replacement for the above -mem-path incantation should be the following:
-object memory-file,id=mem,path=/hugetlbfs,share=on \ -numa node,memdev=mem
Do the ideas behind vhost-user work if you have multiple hugetlbfs files, one per node?
Paolo
Quoted 134 lines
Changes from v8:
- Removed prealloc property from the -mem-path refactoring
- Added and use new function -kvm_eventfds_enabled
- Addvirtio_queue_get_avail_idxused invhost_virtqueue_stopto get a sane value in case ofVHOST_GET_VRING_BASEfailure
- vhost user useskvm_eventfds_enabledto check whether the ioeventfd capability of KVM is available
- Added flagVHOST_USER_VRING_NOFD_MASKto be set when KICK, CALL or ERR file descriptor is invalid or ioeventfd is not availableChanges from v7:
- Slave reconnection when using chardev in server mode
- qtest vhost-user-test added
- Newqemu_chr_fe_get_msgfdsfor reading multiple fds from the chardev
- Mandatory features invhost_dev, used on reconnect to verify for conflicts
- Add vhostforce parameter to -netdev vhost-user (for non-MSIX guests)
- Extend libqemustub.a to supportqemu-char.cChanges from v6:
- Remove the 'unlink' property of '-mem-path'
- Extend qemu-char: blocking read, send fds, monitor for connection close
- Vhost-user uses chardev as a backend
- Poll and reconnect removed (noVHOST_USER_ECHO).
- Disconnect is deteced by the chardev (G_IO_HUPevent)
-vhost-backend.csplit tovhost-user.cChanges from v5:
- Split -mem-path unlink option to a separate patch
- Fds are passed only in the ancillary data
- Stricter message size checks on receive/send
- Netdev vhost-user now includes path andpoll_timeoptions
- The connection probing interval is configurableChanges from v4:
- Useerror_reportfor errors
- VhostUserMsg has new fieldsizeindicating the following payload length. Fieldflagsnow has version and reply bits. The structure is packed.
- Send data is of variable length (sizefield in message)
- Receive in 2 steps, header and payload
- Add new message typeVHOST_USER_ECHO, to check connection statusChanges from v3:
- Convert -mem-path to QemuOpts with prealloc, share and unlink properties
- Set 1 sec timeout when read/write to the unix domain socket
- Fix file descriptor leakChanges from v2:
- Reconnect when the backend disappearsChanges from v1:
- Implementation of vhost-user netdev backend
- Code improvementsAntonios Motakis (20):
Convert -mem-path to QemuOpts and add share property Addkvm_eventfds_enabledfunction
Add chardev APIqemu_chr_fe_read_all
Add chardev APIqemu_chr_fe_set_msgfds
Add chardev APIqemu_chr_fe_get_msgfds
AddG_IO_HUPhandler for socket chardev
vhost_netshould call the poll callback only when it is set Refactor virtio-net to use genericget_vhost_net
Add new virtio APIvirtio_queue_get_avail_idxGracefully handle ioctl failure invhost_virtqueue_stopvhost_net_initwill use VhostNetOptions to get all its arguments Addvhost_opstovhost_devstruct and replace all relevant ioctls Addmandatory_featurestovhost_dev
Add vhost-backend and VhostBackendType
Add vhost-user as a vhost backend.
Add new vhost-user netdev backend
Add the vhost-user netdev backend to the command line Add vhost-user protocol documentation
libqemustub: add stubs to be able to useqemu-char.cAdd qtest for vhost-userdocs/specs/vhost-user.txt | 261 ++++++++++++++++++++++++++++ exec.c | 21 ++- hmp-commands.hx | 4 +- hw/net/vhost_net.c | 141 ++++++++++----- hw/net/virtio-net.c | 29 +--- hw/scsi/vhost-scsi.c | 20 ++- hw/virtio/Makefile.objs | 2 +- hw/virtio/vhost-backend.c | 71 ++++++++ hw/virtio/vhost-user.c | 356 ++++++++++++++++++++++++++++++++++++++ hw/virtio/vhost.c | 58 +++---- hw/virtio/virtio.c | 5 + include/exec/cpu-all.h | 1 - include/hw/virtio/vhost-backend.h | 38 ++++ include/hw/virtio/vhost.h | 9 +- include/hw/virtio/virtio.h | 1 + include/net/vhost-user.h | 17 ++ include/net/vhost_net.h | 13 +- include/sysemu/char.h | 43 ++++- include/sysemu/kvm.h | 11 ++ kvm-all.c | 4 + kvm-stub.c | 1 + net/Makefile.objs | 2 +- net/clients.h | 3 + net/hub.c | 1 + net/net.c | 25 +-- net/tap.c | 18 +- net/vhost-user.c | 273 +++++++++++++++++++++++++++++ qapi-schema.json | 19 +- qemu-char.c | 272 +++++++++++++++++++++++++---- qemu-options.hx | 25 ++- stubs/Makefile.objs | 8 + stubs/bdrv-commit-all.c | 7 + stubs/chr-msmouse.c | 7 + stubs/get-next-serial.c | 3 + stubs/is-daemonized.c | 7 + stubs/machine-init-done.c | 6 + stubs/monitor-init.c | 6 + stubs/notify-event.c | 6 + stubs/vc-init.c | 7 + tests/Makefile | 4 + tests/vhost-user-test.c | 309 +++++++++++++++++++++++++++++++++ vl.c | 23 ++- 42 files changed, 1979 insertions(+), 158 deletions(-) create mode 100644 docs/specs/vhost-user.txt create mode 100644 hw/virtio/vhost-backend.c create mode 100644 hw/virtio/vhost-user.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 create mode 100644 stubs/bdrv-commit-all.c create mode 100644 stubs/chr-msmouse.c create mode 100644 stubs/get-next-serial.c create mode 100644 stubs/is-daemonized.c create mode 100644 stubs/machine-init-done.c create mode 100644 stubs/monitor-init.c create mode 100644 stubs/notify-event.c create mode 100644 stubs/vc-init.c create mode 100644 tests/vhost-user-test.c
Quoted 75 lines · Paolo Bonzini
Il 04/03/2014 19:22, Antonios Motakis ha scritto:
In this patch series we would like to introduce our approach for putting a
Quoted 62 lines
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 realized by 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. For this communication we use a chardev with a Unix
domain socket backend. Vhost-user is client/server agnostic regarding the chardev, however it does not support the 'nowait' and 'telnet' options.A reconnection in 'server' mode is supported, but the backend's exposed virtio
features need to be compatible with the first connected slave.We change -mem-path to QemuOpts and add prealloc and share as properties to it. HugeTLBFS is required for this option to work.
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-path /hugetlbfs,share=on \
-chardev socket,id=chr0,path=/path/to/socket \ -netdev type=vhost-user,id=net0,chardev=chr0 \ -device virtio-net-pci,netdev=net0On non-MSIX guests the vhost feture can be forced using a special option:
...
-netdev type=vhost-user,id=net0,chardev=chr0,vhostforce
...Quoted 9 lines
In order to use ioeventfds, kvm should be enabled.
This code can be pulled from [email protected]:virtualopensystems/qemu.git vhost-user-v9
A simple functional test is available intests/vhost-user-test.cA reference vhost-user slave for testing is also available from [email protected]:virtualopensystems/vapp.git
Quoted 9 lines
Hi,
did you see the series I posted today? It would be great if you tested vhost-user on top of it. The replacement for the above -mem-path incantation should be the following:
-object memory-file,id=mem,path=/hugetlbfs,share=on \ -numa node,memdev=mem
Hello Paolo,
Yes we saw your series today, and we plan to try it out. Any idea on when your series will get in? Then we can probably remove our own implementation for shared memory.
Quoted 5 lines
Do the ideas behind vhost-user work if you have multiple hugetlbfs files, one per node?
Yes, they should work.
Best regards
Quoted 145 lines
Paolo
Changes from v8:
Quoted 140 lines
- Removed prealloc property from the -mem-path refactoring
- Added and use new function -kvm_eventfds_enabled
- Addvirtio_queue_get_avail_idxused invhost_virtqueue_stopto get a sane value in case ofVHOST_GET_VRING_BASEfailure
- vhost user useskvm_eventfds_enabledto check whether the ioeventfd capability of KVM is available
- Added flagVHOST_USER_VRING_NOFD_MASKto be set when KICK, CALL or ERR
file
descriptor is invalid or ioeventfd is not availableChanges from v7:
- Slave reconnection when using chardev in server mode
- qtest vhost-user-test added
- Newqemu_chr_fe_get_msgfdsfor reading multiple fds from the chardev
- Mandatory features invhost_dev, used on reconnect to verify for
conflicts
- Add vhostforce parameter to -netdev vhost-user (for non-MSIX guests)
- Extend libqemustub.a to supportqemu-char.cChanges from v6:
- Remove the 'unlink' property of '-mem-path'
- Extend qemu-char: blocking read, send fds, monitor for connection close
- Vhost-user uses chardev as a backend
- Poll and reconnect removed (noVHOST_USER_ECHO).
- Disconnect is deteced by the chardev (G_IO_HUPevent)
-vhost-backend.csplit tovhost-user.cChanges from v5:
- Split -mem-path unlink option to a separate patch
- Fds are passed only in the ancillary data
- Stricter message size checks on receive/send
- Netdev vhost-user now includes path andpoll_timeoptions
- The connection probing interval is configurableChanges from v4:
- Useerror_reportfor errors
- VhostUserMsg has new fieldsizeindicating the following payload
length.
Fieldflagsnow has version and reply bits. The structure is packed.
- Send data is of variable length (sizefield in message)
- Receive in 2 steps, header and payload
- Add new message typeVHOST_USER_ECHO, to check connection statusChanges from v3:
- Convert -mem-path to QemuOpts with prealloc, share and unlink
properties
- Set 1 sec timeout when read/write to the unix domain socket
- Fix file descriptor leakChanges from v2:
- Reconnect when the backend disappearsChanges from v1:
- Implementation of vhost-user netdev backend
- Code improvementsAntonios Motakis (20):
Convert -mem-path to QemuOpts and add share property Addkvm_eventfds_enabledfunction
Add chardev APIqemu_chr_fe_read_all
Add chardev APIqemu_chr_fe_set_msgfds
Add chardev APIqemu_chr_fe_get_msgfds
AddG_IO_HUPhandler for socket chardev
vhost_netshould call the poll callback only when it is set Refactor virtio-net to use genericget_vhost_net
Add new virtio APIvirtio_queue_get_avail_idxGracefully handle ioctl failure invhost_virtqueue_stopvhost_net_initwill use VhostNetOptions to get all its arguments Addvhost_opstovhost_devstruct and replace all relevant ioctls Addmandatory_featurestovhost_dev
Add vhost-backend and VhostBackendType
Add vhost-user as a vhost backend.
Add new vhost-user netdev backend
Add the vhost-user netdev backend to the command line Add vhost-user protocol documentation
libqemustub: add stubs to be able to useqemu-char.cAdd qtest for vhost-userdocs/specs/vhost-user.txt | 261 ++++++++++++++++++++++++++++ exec.c | 21 ++- hmp-commands.hx | 4 +- hw/net/vhost_net.c | 141 ++++++++++----- hw/net/virtio-net.c | 29 +--- hw/scsi/vhost-scsi.c | 20 ++- hw/virtio/Makefile.objs | 2 +- hw/virtio/vhost-backend.c | 71 ++++++++ hw/virtio/vhost-user.c | 356 ++++++++++++++++++++++++++++++ ++++++++ hw/virtio/vhost.c | 58 +++---- hw/virtio/virtio.c | 5 + include/exec/cpu-all.h | 1 - include/hw/virtio/vhost-backend.h | 38 ++++ include/hw/virtio/vhost.h | 9 +- include/hw/virtio/virtio.h | 1 + include/net/vhost-user.h | 17 ++ include/net/vhost_net.h | 13 +- include/sysemu/char.h | 43 ++++- include/sysemu/kvm.h | 11 ++ kvm-all.c | 4 + kvm-stub.c | 1 + net/Makefile.objs | 2 +- net/clients.h | 3 + net/hub.c | 1 + net/net.c | 25 +-- net/tap.c | 18 +- net/vhost-user.c | 273 +++++++++++++++++++++++++++++ qapi-schema.json | 19 +- qemu-char.c | 272 +++++++++++++++++++++++++---- qemu-options.hx | 25 ++- stubs/Makefile.objs | 8 + stubs/bdrv-commit-all.c | 7 + stubs/chr-msmouse.c | 7 + stubs/get-next-serial.c | 3 + stubs/is-daemonized.c | 7 + stubs/machine-init-done.c | 6 + stubs/monitor-init.c | 6 + stubs/notify-event.c | 6 + stubs/vc-init.c | 7 + tests/Makefile | 4 + tests/vhost-user-test.c | 309 ++++++++++++++++++++++++++++++ +++ vl.c | 23 ++- 42 files changed, 1979 insertions(+), 158 deletions(-) create mode 100644 docs/specs/vhost-user.txt create mode 100644 hw/virtio/vhost-backend.c create mode 100644 hw/virtio/vhost-user.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 create mode 100644 stubs/bdrv-commit-all.c create mode 100644 stubs/chr-msmouse.c create mode 100644 stubs/get-next-serial.c create mode 100644 stubs/is-daemonized.c create mode 100644 stubs/machine-init-done.c create mode 100644 stubs/monitor-init.c create mode 100644 stubs/notify-event.c create mode 100644 stubs/vc-init.c create mode 100644 tests/vhost-user-test.c
Signature · 2 lines
Antonios Motakis Virtual Open Systems
Paolo Bonzini
maintainerIl 04/03/2014 19:33, Antonios Motakis ha scritto:
Quoted 6 lines
Hello Paolo,
Yes we saw your series today, and we plan to try it out. Any idea on when your series will get in? Then we can probably remove our own implementation for shared memory.
It's planned for 2.1, together with memory hotplug which is not in the series but based on it.
Paolo
Quoted 7 lines
Do the ideas behind vhost-user work if you have multiple hugetlbfs files, one per node?
Yes, they should work.
Hello Paolo,
Quoted 71 lines · Paolo Bonzini
Il 04/03/2014 19:22, Antonios Motakis ha scritto:
In this patch series we would like to introduce our approach for putting a
Quoted 68 lines
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 realized by 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. For this communication we use a chardev with a Unix
domain socket backend. Vhost-user is client/server agnostic regarding the chardev, however it does not support the 'nowait' and 'telnet' options.A reconnection in 'server' mode is supported, but the backend's exposed virtio
features need to be compatible with the first connected slave.We change -mem-path to QemuOpts and add prealloc and share as properties to it. HugeTLBFS is required for this option to work.
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-path /hugetlbfs,share=on \
-chardev socket,id=chr0,path=/path/to/socket \ -netdev type=vhost-user,id=net0,chardev=chr0 \ -device virtio-net-pci,netdev=net0On non-MSIX guests the vhost feture can be forced using a special option:
...
-netdev type=vhost-user,id=net0,chardev=chr0,vhostforce
...In order to use ioeventfds, kvm should be enabled.
This code can be pulled from [email protected]:virtualopensystems/qemu.git vhost-user-v9
A simple functional test is available intests/vhost-user-test.c
Quoted 8 lines
Quoted 3 lines
A reference vhost-user slave for testing is also available from [email protected]:virtualopensystems/vapp.git
Hi,
did you see the series I posted today? It would be great if you tested vhost-user on top of it.
I am preparing a new version of this patch series and would like to test (or even rebase?) it on top of your patch series. What is its current status? Is there a tree that I can track and use for rebases?
Quoted 7 lines
The replacement for the above -mem-path incantation should be the following:
-object memory-file,id=mem,path=/hugetlbfs,share=on \ -numa node,memdev=mem
Is this command line update still valid with the latest development?
Quoted 4 lines
Do the ideas behind vhost-user work if you have multiple hugetlbfs files, one per node?
Quoted 145 lines
Paolo
Changes from v8:
Quoted 140 lines
- Removed prealloc property from the -mem-path refactoring
- Added and use new function -kvm_eventfds_enabled
- Addvirtio_queue_get_avail_idxused invhost_virtqueue_stopto get a sane value in case ofVHOST_GET_VRING_BASEfailure
- vhost user useskvm_eventfds_enabledto check whether the ioeventfd capability of KVM is available
- Added flagVHOST_USER_VRING_NOFD_MASKto be set when KICK, CALL or ERR
file
descriptor is invalid or ioeventfd is not availableChanges from v7:
- Slave reconnection when using chardev in server mode
- qtest vhost-user-test added
- Newqemu_chr_fe_get_msgfdsfor reading multiple fds from the chardev
- Mandatory features invhost_dev, used on reconnect to verify for
conflicts
- Add vhostforce parameter to -netdev vhost-user (for non-MSIX guests)
- Extend libqemustub.a to supportqemu-char.cChanges from v6:
- Remove the 'unlink' property of '-mem-path'
- Extend qemu-char: blocking read, send fds, monitor for connection close
- Vhost-user uses chardev as a backend
- Poll and reconnect removed (noVHOST_USER_ECHO).
- Disconnect is deteced by the chardev (G_IO_HUPevent)
-vhost-backend.csplit tovhost-user.cChanges from v5:
- Split -mem-path unlink option to a separate patch
- Fds are passed only in the ancillary data
- Stricter message size checks on receive/send
- Netdev vhost-user now includes path andpoll_timeoptions
- The connection probing interval is configurableChanges from v4:
- Useerror_reportfor errors
- VhostUserMsg has new fieldsizeindicating the following payload
length.
Fieldflagsnow has version and reply bits. The structure is packed.
- Send data is of variable length (sizefield in message)
- Receive in 2 steps, header and payload
- Add new message typeVHOST_USER_ECHO, to check connection statusChanges from v3:
- Convert -mem-path to QemuOpts with prealloc, share and unlink
properties
- Set 1 sec timeout when read/write to the unix domain socket
- Fix file descriptor leakChanges from v2:
- Reconnect when the backend disappearsChanges from v1:
- Implementation of vhost-user netdev backend
- Code improvementsAntonios Motakis (20):
Convert -mem-path to QemuOpts and add share property Addkvm_eventfds_enabledfunction
Add chardev APIqemu_chr_fe_read_all
Add chardev APIqemu_chr_fe_set_msgfds
Add chardev APIqemu_chr_fe_get_msgfds
AddG_IO_HUPhandler for socket chardev
vhost_netshould call the poll callback only when it is set Refactor virtio-net to use genericget_vhost_net
Add new virtio APIvirtio_queue_get_avail_idxGracefully handle ioctl failure invhost_virtqueue_stopvhost_net_initwill use VhostNetOptions to get all its arguments Addvhost_opstovhost_devstruct and replace all relevant ioctls Addmandatory_featurestovhost_dev
Add vhost-backend and VhostBackendType
Add vhost-user as a vhost backend.
Add new vhost-user netdev backend
Add the vhost-user netdev backend to the command line Add vhost-user protocol documentation
libqemustub: add stubs to be able to useqemu-char.cAdd qtest for vhost-userdocs/specs/vhost-user.txt | 261 ++++++++++++++++++++++++++++ exec.c | 21 ++- hmp-commands.hx | 4 +- hw/net/vhost_net.c | 141 ++++++++++----- hw/net/virtio-net.c | 29 +--- hw/scsi/vhost-scsi.c | 20 ++- hw/virtio/Makefile.objs | 2 +- hw/virtio/vhost-backend.c | 71 ++++++++ hw/virtio/vhost-user.c | 356 ++++++++++++++++++++++++++++++ ++++++++ hw/virtio/vhost.c | 58 +++---- hw/virtio/virtio.c | 5 + include/exec/cpu-all.h | 1 - include/hw/virtio/vhost-backend.h | 38 ++++ include/hw/virtio/vhost.h | 9 +- include/hw/virtio/virtio.h | 1 + include/net/vhost-user.h | 17 ++ include/net/vhost_net.h | 13 +- include/sysemu/char.h | 43 ++++- include/sysemu/kvm.h | 11 ++ kvm-all.c | 4 + kvm-stub.c | 1 + net/Makefile.objs | 2 +- net/clients.h | 3 + net/hub.c | 1 + net/net.c | 25 +-- net/tap.c | 18 +- net/vhost-user.c | 273 +++++++++++++++++++++++++++++ qapi-schema.json | 19 +- qemu-char.c | 272 +++++++++++++++++++++++++---- qemu-options.hx | 25 ++- stubs/Makefile.objs | 8 + stubs/bdrv-commit-all.c | 7 + stubs/chr-msmouse.c | 7 + stubs/get-next-serial.c | 3 + stubs/is-daemonized.c | 7 + stubs/machine-init-done.c | 6 + stubs/monitor-init.c | 6 + stubs/notify-event.c | 6 + stubs/vc-init.c | 7 + tests/Makefile | 4 + tests/vhost-user-test.c | 309 ++++++++++++++++++++++++++++++ +++ vl.c | 23 ++- 42 files changed, 1979 insertions(+), 158 deletions(-) create mode 100644 docs/specs/vhost-user.txt create mode 100644 hw/virtio/vhost-backend.c create mode 100644 hw/virtio/vhost-user.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 create mode 100644 stubs/bdrv-commit-all.c create mode 100644 stubs/chr-msmouse.c create mode 100644 stubs/get-next-serial.c create mode 100644 stubs/is-daemonized.c create mode 100644 stubs/machine-init-done.c create mode 100644 stubs/monitor-init.c create mode 100644 stubs/notify-event.c create mode 100644 stubs/vc-init.c create mode 100644 tests/vhost-user-test.c
regards,
Nikolay Nikolaev
Paolo Bonzini
maintainerIl 20/05/2014 13:22, Nikolay Nikolaev ha scritto:
Quoted 3 lines
I am preparing a new version of this patch series and would like to test (or even rebase?) it on top of your patch series. What is its current status? Is there a tree that I can track and use for rebases?
Hu Tao has been posting updates lately. I don't know if he has a git tree.
Quoted 7 lines
The replacement for the above -mem-path incantation should be the following:
-object memory-file,id=mem,path=/__hugetlbfs,share=on \
-numa node,memdev=memIs this command line update still valid with the latest development?
Yes, it should.
Paolo
PATCH v9 6/20 Add G_IO_HUP handler for socket chardev
In reply to Antonios MotakisThis is used to detect that the remote end has disconnected. Just call tcp_char_disconnect on receiving this event.
--- qemu-char.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
qemu-char.c +16 -0
@@ -2673,6 +2673,21 @@ CharDriverState *qemu_chr_open_eventfd(int eventfd) }#endif +static gboolean tcp_chr_chan_close (GIOChannel *channel,GIOCondition cond, +void *opaque) +{ +CharDriverState *chr = opaque; + +if (cond !=G_IO_HUP ) { +return FALSE; + } + +/* connection closed */ +tcp_chr_disconnect (chr); + +return TRUE; +} +static void tcp_chr_connect (void *opaque) {CharDriverState *chr = opaque;
@@ -2682,6 +2697,7 @@ static void tcp_chr_connect(void *opaque)if (s->chan) { chr->fd_in_tag =io_add_watch_poll (s->chan, tcp_chr_read_poll, tcp_chr_read, chr); +g_io_add_watch (s->chan,G_IO_HUP , tcp_chr_chan_close, chr); }qemu_chr_be_generic_open (chr); }
Signature · 1 line
1.8.3.2
PATCH v9 7/20 vhost_net should call the poll callback only when it is set
In reply to Antonios MotakisThe poll callback needs to be called when bringing up or down the vhost_net instance. As it is not mandatory for an NetClient to implement it, invoke it only when it is set.
--- hw/net/vhost_net.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
hw/net/vhost_net.c +10 -3
@@ -166,7 +166,10 @@ static int vhost_net_start_one(struct vhost_net *net, goto fail_start; } - net->nc->info->poll (net->nc, false); +if (net->nc->info->poll) { + net->nc->info->poll (net->nc, false); + } +qemu_set_fd_handler (net->backend,NULL ,NULL ,NULL ); file.fd = net->backend;for (file.index =0 ; file.index < net->dev.nvqs; ++file.index) {
@@ -183,7 +186,9 @@ fail:int r =ioctl (net->dev.control,VHOST_NET_SET_BACKEND , &file);assert (r >=0 ); } - net->nc->info->poll (net->nc, true); +if (net->nc->info->poll) { + net->nc->info->poll (net->nc, true); + }vhost_dev_stop (&net->dev, dev); fail_start:vhost_dev_disable_notifiers (&net->dev, dev);
@@ -204,7 +209,9 @@ static void vhost_net_stop_one(struct vhost_net *net,int r =ioctl (net->dev.control,VHOST_NET_SET_BACKEND , &file);assert (r >=0 ); } - net->nc->info->poll (net->nc, true); +if (net->nc->info->poll) { + net->nc->info->poll (net->nc, true); + }vhost_dev_stop (&net->dev, dev);vhost_dev_disable_notifiers (&net->dev, dev); }
Signature · 1 line
1.8.3.2
PATCH v9 8/20 Refactor virtio-net to use generic get_vhost_net
In reply to Antonios MotakisThis decouples virtio-net from the TAP netdev backend and allows support for other backends to be implemented.
--- hw/net/vhost_net.c | 30 +++++++++++++++++++++++++++--- hw/net/virtio-net.c | 29 ++++++++--------------------- include/net/vhost_net.h | 1 + 3 files changed, 36 insertions(+), 24 deletions(-)
hw/net/vhost_net.c +27 -3
@@ -231,7 +231,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs, }for (i =0 ; i < total_queues; i++) { - r =vhost_net_start_one (tap_get_vhost_net (ncs[i].peer), dev, i *2 ); + r =vhost_net_start_one (get_vhost_net (ncs[i].peer), dev, i *2 );if (r <0 ) { goto err;
@@ -248,7 +248,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs, err:while (--i >=0 ) { -vhost_net_stop_one (tap_get_vhost_net (ncs[i].peer), dev); +vhost_net_stop_one (get_vhost_net (ncs[i].peer), dev); }return r; }
@@ -269,7 +269,7 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,assert (r >=0 );for (i =0 ; i < total_queues; i++) { -vhost_net_stop_one (tap_get_vhost_net (ncs[i].peer), dev); +vhost_net_stop_one (get_vhost_net (ncs[i].peer), dev); } }
@@ -289,6 +289,25 @@ void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev, {vhost_virtqueue_mask (&net->dev, dev, idx, mask); } + +VHostNetState *get_vhost_net (NetClientState *nc) +{ +VHostNetState *vhost_net =0 ; + +if (!nc) { +return 0 ; + } + +switch (nc->info->type) { +case NET_CLIENT_OPTIONS_KIND_TAP : + vhost_net =tap_get_vhost_net (nc); +break ; +default : +break ; + } + +return vhost_net; +}#else struct vhost_net *vhost_net_init (NetClientState *backend,int devfd,bool force)
@@ -335,4 +354,9 @@ void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev, int idx, bool mask) { } + +VHostNetState *get_vhost_net (NetClientState *nc) +{ +return 0 ; +}#endif
hw/net/virtio-net.c +8 -21
@@ -105,14 +105,7 @@ static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)NetClientState *nc =qemu_get_queue (n->nic);int queues = n->multiqueue ? n->max_queues :1 ; -if (!nc->peer) { -return ; - } -if (nc->peer->info->type !=NET_CLIENT_OPTIONS_KIND_TAP ) { -return ; - } - -if (!tap_get_vhost_net (nc->peer)) { +if (!get_vhost_net (nc->peer)) {return ; }
@@ -122,7 +115,7 @@ static void virtio_net_vhost_status(VirtIONet *n, uint8_t status) }if (!n->vhost_started) {int r; -if (!vhost_net_query (tap_get_vhost_net (nc->peer), vdev)) { +if (!vhost_net_query (get_vhost_net (nc->peer), vdev)) {return ; } n->vhost_started =1 ;
@@ -433,13 +426,10 @@ static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features) features &= ~(0x1 <<VIRTIO_NET_F_HOST_UFO ); } -if (!nc->peer || nc->peer->info->type !=NET_CLIENT_OPTIONS_KIND_TAP ) { -return features; - } -if (!tap_get_vhost_net (nc->peer)) { +if (!get_vhost_net (nc->peer)) {return features; } -return vhost_net_get_features (tap_get_vhost_net (nc->peer), features); +return vhost_net_get_features (get_vhost_net (nc->peer), features); }static uint32_t virtio_net_bad_features (VirtIODevice *vdev)
@@ -503,13 +493,10 @@ static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)for (i =0 ; i < n->max_queues; i++) {NetClientState *nc =qemu_get_subqueue (n->nic, i); -if (!nc->peer || nc->peer->info->type !=NET_CLIENT_OPTIONS_KIND_TAP ) { -continue ; - } -if (!tap_get_vhost_net (nc->peer)) { +if (!get_vhost_net (nc->peer)) {continue ; } -vhost_net_ack_features (tap_get_vhost_net (nc->peer), features); +vhost_net_ack_features (get_vhost_net (nc->peer), features); } }
@@ -1439,7 +1426,7 @@ static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)VirtIONet *n =VIRTIO_NET (vdev);NetClientState *nc =qemu_get_subqueue (n->nic,vq2q (idx));assert (n->vhost_started); -return vhost_net_virtqueue_pending (tap_get_vhost_net (nc->peer), idx); +return vhost_net_virtqueue_pending (get_vhost_net (nc->peer), idx); } static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
@@ -1448,7 +1435,7 @@ static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,VirtIONet *n =VIRTIO_NET (vdev);NetClientState *nc =qemu_get_subqueue (n->nic,vq2q (idx));assert (n->vhost_started); -vhost_net_virtqueue_mask (tap_get_vhost_net (nc->peer), +vhost_net_virtqueue_mask (get_vhost_net (nc->peer), vdev, idx, mask); }
include/net/vhost_net.h +1 -0
@@ -20,4 +20,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
Signature · 1 line
1.8.3.2
PATCH v9 9/20 Add new virtio API virtio_queue_get_avail_idx
In reply to Antonios MotakisThis function allows to get the current available ring index.
--- hw/virtio/virtio.c | 5 +++++ include/hw/virtio/virtio.h | 1 + 2 files changed, 6 insertions(+)
hw/virtio/virtio.c +5 -0
@@ -1058,6 +1058,11 @@ hwaddr virtio_queue_get_ring_size(VirtIODevice *vdev, int n)virtio_queue_get_used_size (vdev, n); } +uint16_t virtio_queue_get_avail_idx (VirtIODevice *vdev,int n) +{ +return vring_avail_idx (&vdev->vq[n]); +} +uint16_t virtio_queue_get_last_avail_idx (VirtIODevice *vdev,int n) {return vdev->vq[n].last_avail_idx;
include/hw/virtio/virtio.h +1 -0
@@ -239,6 +239,7 @@ hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n);hwaddr virtio_queue_get_avail_size (VirtIODevice *vdev,int n);hwaddr virtio_queue_get_used_size (VirtIODevice *vdev,int n);hwaddr virtio_queue_get_ring_size (VirtIODevice *vdev,int n); +uint16_t virtio_queue_get_avail_idx (VirtIODevice *vdev,int n);uint16_t virtio_queue_get_last_avail_idx (VirtIODevice *vdev,int n);void virtio_queue_set_last_avail_idx (VirtIODevice *vdev,int n,uint16_t idx);void virtio_queue_invalidate_signalled_used (VirtIODevice *vdev,int n);
Signature · 1 line
1.8.3.2
PATCH v9 10/20 Gracefully handle ioctl failure in vhost_virtqueue_stop
In reply to Antonios MotakisOn stopping the vhost, a call to VHOST_GET_VRING_BASE is issued. The received value is stored as last_avail_idx, so the virtqueue can continue operating if the connection is resumed. Handle the failure of this call and use the current avail_idx. Some packets from the avail ring may be omitted but still we keep a sane value and can continue on reconnect.
--- hw/virtio/vhost.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
hw/virtio/vhost.c +2 -1
@@ -758,12 +758,13 @@ static void vhost_virtqueue_stop(struct vhost_dev *dev,assert (idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs); r =ioctl (dev->control,VHOST_GET_VRING_BASE , &state);if (r <0 ) { + state.num =virtio_queue_get_avail_idx (vdev, idx);fprintf (stderr,"vhost VQ %d ring restore failed: %d\n" , idx, r);fflush (stderr); }virtio_queue_set_last_avail_idx (vdev, idx, state.num);virtio_queue_invalidate_signalled_used (vdev, idx); -assert (r >=0 ); +cpu_physical_memory_unmap (vq->ring,virtio_queue_get_ring_size (vdev, idx),0 ,virtio_queue_get_ring_size (vdev, idx)); cpu_physical_memory_unmap(vq->used, virtio_queue_get_used_size(vdev, idx),
Signature · 1 line
1.8.3.2
Re: [Qemu-devel] [PATCH v9 10/20] Gracefully handle ioctl failure in vhost_virtqueue_stop
Quoted 5 lines · Antonios Motakis
On stopping the vhost, a call to
VHOST_GET_VRING_BASEis issued. The received value is stored aslast_avail_idx, so the virtqueue can continue operating if the connection is resumed. Handle the failure of this call and use the currentavail_idx. Some packets from the avail ring may be omitted but still we keep a sane value and can continue on reconnect.
omitted how?
some guests crash if we never complete handling buffers, or networking breaks, etc ...
This would be a big problem for reconnect, some robust way to communicate avail ring state would need to be found. Is reconnect really a mandatory feature for you?
I'd suggest you drop it from v1, focus on basic functionality.
Quoted 3 lines
Signed-off-by Antonios Motakis <[email protected]>Signed-off-by Nikolay Nikolaev <[email protected]>
Problem is, a bunch of stuff breaks if vhost keeps going when we ask it to stop.
In particular it will keep looking at the ring state when guest asked it to stop doing this, this will corrupt guest memory.
Quoted 25 lines
--- hw/virtio/vhost.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)hw/virtio/vhost.c +2
-1@@ -758,12 +758,13 @@ static void vhost_virtqueue_stop(struct vhost_dev *dev,assert (idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs); r =ioctl (dev->control,VHOST_GET_VRING_BASE , &state);if (r <0 ) { + state.num =virtio_queue_get_avail_idx (vdev, idx);fprintf (stderr,"vhost VQ %d ring restore failed: %d\n" , idx, r);fflush (stderr); }virtio_queue_set_last_avail_idx (vdev, idx, state.num);virtio_queue_invalidate_signalled_used (vdev, idx); -assert (r >=0 ); +cpu_physical_memory_unmap (vq->ring,virtio_queue_get_ring_size (vdev, idx),0 ,virtio_queue_get_ring_size (vdev, idx)); cpu_physical_memory_unmap(vq->used, virtio_queue_get_used_size(vdev, idx),Signature · 1 line
1.8.3.2
Re: [Qemu-devel] [PATCH v9 10/20] Gracefully handle ioctl failure in vhost_virtqueue_stop
Hello,
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines · Antonios Motakis
On stopping the vhost, a call to
VHOST_GET_VRING_BASEis issued. The received value is stored aslast_avail_idx, so the virtqueue can continue operating if the connection is resumed. Handle the failure of this call and use the currentavail_idx. Some packets from the avail ring may be omitted but still we keep a sane value and can continue on reconnect.
Quoted 10 lines
omitted how?
some guests crash if we never complete handling buffers, or networking breaks, etc ...This would be a big problem for reconnect, some robust way to communicate avail ring state would need to be found. Is reconnect really a mandatory feature for you?
I'd suggest you drop it from v1, focus on basic functionality.
Reconnect would be a really useful feature for us, so we tried to keep it in a reasonable way.
However we didn't take into account that some guests might crash under those assumptions. Looks like we have no option but to remove reconnect altogether for now; maybe a future extension to the virtio-net spec will allow us to do it cleanly, but I don't see an obvious workaround to keep this in now.
Thanks for pointing this out.
Btw, since it looks like we are closing a final version of the patches, what kind of timeframe should we aim for inclusion? Should we already rebase on top of Paolo's NUMA patch series?
Quoted 41 lines
Quoted 3 lines
Signed-off-by Antonios Motakis <[email protected]>Signed-off-by Nikolay Nikolaev <[email protected]>Problem is, a bunch of stuff breaks if vhost keeps going when we ask it to stop.
In particular it will keep looking at the ring state when guest asked it to stop doing this, this will corrupt guest memory.hw/virtio/vhost.c static void vhost_virtqueue_stop(struct vhost_dev
Quoted 9 lines
--- hw/virtio/vhost.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)hw/virtio/vhost.c +0
-0@@ -758,12 +758,13 @@ static void vhost_virtqueue_stop(struct vhost_dev*dev,
hw/virtio/vhost.c static void vhost_virtqueue_stop(struct vhost_dev *dev,
Quoted 5 lines
hw/virtio/vhost.c +1
-0assert (idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs); r =ioctl (dev->control,VHOST_GET_VRING_BASE , &state);if (r <0 ) { + state.num =virtio_queue_get_avail_idx (vdev, idx); fprintf(stderr, "vhost VQ %d ring restore failed: %d\n", idx,r);
hw/virtio/vhost.c static void vhost_virtqueue_stop(struct vhost_dev *dev,
Quoted 7 lines
hw/virtio/vhost.c +1
-1fflush (stderr); }virtio_queue_set_last_avail_idx (vdev, idx, state.num);virtio_queue_invalidate_signalled_used (vdev, idx); -assert (r >=0 ); + cpu_physical_memory_unmap(vq->ring,
virtio_queue_get_ring_size(vdev, idx),hw/virtio/vhost.c static void vhost_virtqueue_stop(struct vhost_dev · line 770
Quoted 2 lines
hw/virtio/vhost.c +0
-00, virtio_queue_get_ring_size(vdev, idx)); cpu_physical_memory_unmap(vq->used,
virtio_queue_get_used_size(vdev, idx),Quoted 2 lines
--
1.8.3.2
Signature · 2 lines
Antonios Motakis Virtual Open Systems
Re: [Qemu-devel] [PATCH v9 10/20] Gracefully handle ioctl failure in vhost_virtqueue_stop
Quoted 31 lines · Antonios Motakis
Hello,
On Tue, Mar 4, 2014 at 7:45 PM, Michael S. Tsirkin <[email protected]> wrote:
On Tue, Mar 04, 2014 at 07:22:53PM +0100, Antonios Motakis wrote:
> On stopping the vhost, a call toVHOST_GET_VRING_BASEis issued. The > received value is stored aslast_avail_idx, so the virtqueue can continue > operating if the connection is resumed. Handle the failure of this call > and use the currentavail_idx. Some packets from the avail ring may be > omitted but still we keep a sane value and can continue on reconnect.omitted how?
some guests crash if we never complete handling buffers, or networking breaks, etc ...This would be a big problem for reconnect, some robust way to communicate avail ring state would need to be found. Is reconnect really a mandatory feature for you? I'd suggest you drop it from v1, focus on basic functionality.
Reconnect would be a really useful feature for us, so we tried to keep it in a reasonable way.
However we didn't take into account that some guests might crash under those assumptions. Looks like we have no option but to remove reconnect altogether for now; maybe a future extension to the virtio-net spec will allow us to do it cleanly, but I don't see an obvious workaround to keep this in now.
Quoted 6 lines
Thanks for pointing this out.
Btw, since it looks like we are closing a final version of the patches, what kind of timeframe should we aim for inclusion? Should we already rebase on top of Paolo's NUMA patch series?
I think it should be possible to merge after Paolo's patches go in, yes. I haven't followed them closely so I don't know when will that be.
I wish someone else would ack the char dev changes - anyone? But it's not a blocker requirement.
Quoted 47 lines
Quoted 3 lines
Signed-off-by Antonios Motakis <[email protected]>Signed-off-by Nikolay Nikolaev <[email protected]>Problem is, a bunch of stuff breaks if vhost keeps going when we ask it to stop. In particular it will keep looking at the ring state when guest asked it to stop doing this, this will corrupt guest memory. > --- > hw/virtio/vhost.c | 3 ++- > 1 file changed, 2 insertions(+), 1 deletion(-) > > diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c > index 9e336ad..322e2c0 100644 > --- a/hw/virtio/vhost.c > +++ b/hw/virtio/vhost.c > @@ -758,12 +758,13 @@ static void vhost_virtqueue_stop(struct vhost_dev *dev, > assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs); > r = ioctl(dev->control, VHOST_GET_VRING_BASE, &state); > if (r < 0) { > + state.num = virtio_queue_get_avail_idx(vdev, idx); > fprintf(stderr, "vhost VQ %d ring restore failed: %d\n", idx, r); > fflush(stderr); > } > virtio_queue_set_last_avail_idx(vdev, idx, state.num); > virtio_queue_invalidate_signalled_used(vdev, idx); > - assert (r >= 0); > + > cpu_physical_memory_unmap(vq->ring, virtio_queue_get_ring_size(vdev, idx), > 0, virtio_queue_get_ring_size(vdev, idx)); > cpu_physical_memory_unmap(vq->used, virtio_queue_get_used_size(vdev, idx), > -- > 1.8.3.2--
Antonios Motakis
Virtual Open Systems
PATCH v9 11/20 vhost_net_init will use VhostNetOptions to get all its arguments
In reply to Antonios Motakisvhost_dev_init will replace devfd and devpath with a single opaque argument. This is initialised with a file descriptor. When TAP is used (through vhost_net), open /dev/vhost-net and pass the fd as an opaque parameter in VhostNetOptions. The same applies to vhost-scsi - open /dev/vhost-scsi and pass the fd.
--- hw/net/vhost_net.c | 23 ++++++++++++----------- hw/scsi/vhost-scsi.c | 10 +++++++++- hw/virtio/vhost.c | 12 +++--------- include/hw/virtio/vhost.h | 2 +- include/net/vhost_net.h | 8 +++++++- net/tap.c | 17 +++++++++++++---- 6 files changed, 45 insertions(+), 27 deletions(-)
hw/net/vhost_net.c +12 -11
@@ -92,32 +92,34 @@ 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;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); + + r =vhost_net_get_fd (options->net_backend);if (r <0 ) { goto fail; } - net->nc = backend; - net->dev.backend_features =qemu_has_vnet_hdr (backend) ?0 : + net->nc = options->net_backend; + net->dev.backend_features =qemu_has_vnet_hdr (options->net_backend) ?0 : (1 <<VHOST_NET_F_VIRTIO_NET_HDR ); net->backend = r; 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, options->opaque, + options->force);if (r <0 ) { goto fail; } -if (!qemu_has_vnet_hdr_len (backend, +if (!qemu_has_vnet_hdr_len (options->net_backend,sizeof (struct virtio_net_hdr_mrg_rxbuf ))) { net->dev.features &= ~(1 <<VIRTIO_NET_F_MRG_RXBUF ); }
@@ -309,8 +311,7 @@ VHostNetState *get_vhost_net(NetClientState *nc) return vhost_net; }#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/scsi/vhost-scsi.c +9 -1
@@ -215,6 +215,13 @@ static void vhost_scsi_realize(DeviceState *dev, Error **errp)error_setg (errp,"vhost-scsi: unable to parse vhostfd" );return ; } + }else { + vhostfd =open ("/dev/vhost-scsi" ,O_RDWR ); +if (vhostfd <0 ) { +error_setg (errp,"vhost-scsi: open vhost char device failed: %s" , +strerror (errno)); +return ; + } }virtio_scsi_common_realize (dev, &err);
@@ -227,7 +234,8 @@ static void vhost_scsi_realize(DeviceState *dev, Error **errp) 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, (void *)(uintptr_t )vhostfd, + true);if (ret <0 ) {error_setg (errp,"vhost-scsi: vhost initialization failed: %s" ,strerror (-ret));
hw/virtio/vhost.c +3 -9
@@ -815,19 +815,13 @@ static void vhost_virtqueue_cleanup(struct vhost_virtqueue *vq)event_notifier_cleanup (&vq->masked_notifier); } -int vhost_dev_init (struct vhost_dev *hdev,int devfd,const char *devpath, +int vhost_dev_init (struct vhost_dev *hdev,void *opaque,bool force) {uint64_t features;int i, r; -if (devfd >=0 ) { - hdev->control = devfd; - }else { - hdev->control =open (devpath,O_RDWR ); -if (hdev->control <0 ) { -return -errno; - } - } + hdev->control = (uintptr_t ) opaque;; + r =ioctl (hdev->control,VHOST_SET_OWNER ,NULL );if (r <0 ) { goto fail;
include/hw/virtio/vhost.h +1 -1
@@ -50,7 +50,7 @@ struct vhost_dev {hwaddr mem_changed_end_addr; }; -int vhost_dev_init (struct vhost_dev *hdev,int devfd,const char *devpath, +int vhost_dev_init (struct vhost_dev *hdev,void *opaque,bool force);void vhost_dev_cleanup (struct vhost_dev *hdev);bool vhost_dev_query (struct vhost_dev *hdev,VirtIODevice *vdev);
include/net/vhost_net.h +7 -1
@@ -6,7 +6,13 @@struct vhost_net ;typedef struct vhost_net VHostNetState ; -VHostNetState *vhost_net_init (NetClientState *backend,int devfd,bool force); +typedef struct VhostNetOptions { +NetClientState *net_backend; +void *opaque; +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 +13 -4
@@ -597,6 +597,7 @@ static int net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer, int vnet_hdr, int fd) {TAPState *s; +int vhostfd; s =net_tap_fd_init (peer, model, name, fd, vnet_hdr);if (!s) {
@@ -627,7 +628,10 @@ 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.net_backend = &s->nc; + options.force = tap->has_vhostforce && tap->vhostforce;if (tap->has_vhostfd || tap->has_vhostfds) { vhostfd =monitor_handle_fd_param (cur_mon, vhostfdname);
@@ -635,11 +639,16 @@ static int net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,return -1 ; } }else { - vhostfd =-1 ; + vhostfd =open ("/dev/vhost-net" ,O_RDWR ); +if (vhostfd <0 ) { +error_report ("tap: open vhost char device failed: %s" , +strerror (errno)); +return -1 ; + } } + options.opaque = (void *)(uintptr_t )vhostfd; - 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 v9 12/20 Add vhost_ops to vhost_dev struct and replace all relevant ioctls
In reply to Antonios MotakisDecouple vhost from the Linux kernel by introducing vhost_ops. The intention is to provide different backends - a 'kernel' backend based on the ioctl interface, and an 'user' backend based on a UNIX domain socket and shared memory interface.
--- hw/net/vhost_net.c | 10 ++++++---- hw/scsi/vhost-scsi.c | 10 +++++++--- hw/virtio/vhost.c | 41 ++++++++++++++++++++------------------- include/hw/virtio/vhost-backend.h | 27 ++++++++++++++++++++++++++ include/hw/virtio/vhost.h | 2 ++ 5 files changed, 63 insertions(+), 27 deletions(-) create mode 100644 include/hw/virtio/vhost-backend.h
hw/net/vhost_net.c +6 -4
@@ -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>
@@ -175,7 +174,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;
@@ -185,7 +185,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 ); }if (net->nc->info->poll) {
@@ -208,7 +209,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 ); }if (net->nc->info->poll) {
hw/scsi/vhost-scsi.c +7 -3
@@ -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; }
hw/virtio/vhost.c +21 -20
147 lines not on this page · raw message
include/hw/virtio/vhost-backend.h new file +27 -0
@@ -0,0 +1,27 @@ +/* +* 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_ + +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,void *opaque); +typedef int (*vhost_backend_cleanup )(struct vhost_dev *dev); + +typedef struct VhostOps { +vhost_call vhost_call; +vhost_backend_init vhost_backend_init; +vhost_backend_cleanup vhost_backend_cleanup; +}VhostOps ; + +#endif /* VHOST_BACKEND_H_ */
include/hw/virtio/vhost.h +2 -0
@@ -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,6 +49,7 @@ 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, void *opaque,
Signature · 1 line
1.8.3.2
PATCH v9 13/20 Add mandatory_features to vhost_dev
In reply to Antonios MotakisThis will be used in a following patch to ensure that a vhost-user client reconnecting to QEMU supports the features that were exposed by the first client that initiated the virtio-net session.
--- hw/net/vhost_net.c | 10 ++++++++++ include/hw/virtio/vhost.h | 1 + include/net/vhost_net.h | 2 ++ 3 files changed, 13 insertions(+)
hw/net/vhost_net.c +10 -0
@@ -80,6 +80,11 @@ void vhost_net_ack_features(struct vhost_net *net, unsigned features) } } +unsigned long long vhost_net_features (VHostNetState *net) +{ +return net->dev.features; +} +static int vhost_net_get_fd (NetClientState *backend) {switch (backend->info->type) {
@@ -112,6 +117,7 @@ struct vhost_net *vhost_net_init(VhostNetOptions *options) net->dev.nvqs =2 ; net->dev.vqs = net->vqs; + net->dev.mandatory_features = options->mandatory_features; r =vhost_dev_init (&net->dev, options->opaque, options->force);
@@ -347,6 +353,10 @@ unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)void vhost_net_ack_features (struct vhost_net *net,unsigned features) { } +unsigned long long vhost_net_features (struct vhost_net *net) +{ +return 0 ; +}bool vhost_net_virtqueue_pending (VHostNetState *net,int idx) {
include/hw/virtio/vhost.h +1 -0
@@ -41,6 +41,7 @@ struct vhost_dev {unsigned long long features;unsigned long long acked_features;unsigned long long backend_features; +unsigned long long mandatory_features;bool started;bool log_enabled;vhost_log_chunk_t *log;
include/net/vhost_net.h +2 -0
@@ -10,6 +10,7 @@ typedef struct VhostNetOptions {NetClientState *net_backend;void *opaque;bool force; +unsigned long long mandatory_features; } VhostNetOptions;struct vhost_net *vhost_net_init (VhostNetOptions *options);
@@ -22,6 +23,7 @@ void vhost_net_cleanup(VHostNetState *net);unsigned vhost_net_get_features (VHostNetState *net,unsigned features);void vhost_net_ack_features (VHostNetState *net,unsigned features); +unsigned long long vhost_net_features (VHostNetState *net);bool vhost_net_virtqueue_pending (VHostNetState *net,int n); void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
Signature · 1 line
1.8.3.2
Re: [Qemu-devel] [PATCH v9 13/20] Add mandatory_features to vhost_dev
Quoted 6 lines · Antonios Motakis
This will be used in a following patch to ensure that a vhost-user client reconnecting to QEMU supports the features that were exposed by the first client that initiated the virtio-net session.
Signed-off-by Antonios Motakis <[email protected]>Signed-off-by Nikolay Nikolaev <[email protected]>
Why isn't checking features, or backend_features field sufficient?
Quoted 75 lines
--- hw/net/vhost_net.c | 10 ++++++++++ include/hw/virtio/vhost.h | 1 + include/net/vhost_net.h | 2 ++ 3 files changed, 13 insertions(+)hw/net/vhost_net.c +10
-0@@ -80,6 +80,11 @@ void vhost_net_ack_features(struct vhost_net *net, unsigned features) } } +unsigned long long vhost_net_features (VHostNetState *net) +{ +return net->dev.features; +} +static int vhost_net_get_fd (NetClientState *backend) {switch (backend->info->type) {@@ -112,6 +117,7 @@ struct vhost_net *vhost_net_init(VhostNetOptions *options) net->dev.nvqs =2 ; net->dev.vqs = net->vqs; + net->dev.mandatory_features = options->mandatory_features; r =vhost_dev_init (&net->dev, options->opaque, options->force);@@ -347,6 +353,10 @@ unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)void vhost_net_ack_features (struct vhost_net *net,unsigned features) { } +unsigned long long vhost_net_features (struct vhost_net *net) +{ +return 0 ; +}bool vhost_net_virtqueue_pending (VHostNetState *net,int idx) {include/hw/virtio/vhost.h +1
-0@@ -41,6 +41,7 @@ struct vhost_dev {unsigned long long features;unsigned long long acked_features;unsigned long long backend_features; +unsigned long long mandatory_features;bool started;bool log_enabled;vhost_log_chunk_t *log;include/net/vhost_net.h +2
-0@@ -10,6 +10,7 @@ typedef struct VhostNetOptions {NetClientState *net_backend;void *opaque;bool force; +unsigned long long mandatory_features; } VhostNetOptions;struct vhost_net *vhost_net_init (VhostNetOptions *options);@@ -22,6 +23,7 @@ void vhost_net_cleanup(VHostNetState *net);unsigned vhost_net_get_features (VHostNetState *net,unsigned features);void vhost_net_ack_features (VHostNetState *net,unsigned features); +unsigned long long vhost_net_features (VHostNetState *net);bool vhost_net_virtqueue_pending (VHostNetState *net,int n); void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,Signature · 1 line
1.8.3.2
Re: [Qemu-devel] [PATCH v9 13/20] Add mandatory_features to vhost_dev
Quoted 11 lines · Michael S. Tsirkin
Quoted 6 lines · Antonios Motakis
This will be used in a following patch to ensure that a vhost-user client reconnecting to QEMU supports the features that were exposed by the first client that initiated the virtio-net session.
Signed-off-by Antonios Motakis <[email protected]>Signed-off-by Nikolay Nikolaev <[email protected]>Why isn't checking features, or
backend_featuresfield sufficient?
Since we unfortunately need to remove reconnect support, this will go away too.
Quoted 80 lines
hw/net/vhost_net.c void vhost_net_ack_features(struct vhost_net *net,
Quoted 12 lines
--- hw/net/vhost_net.c | 10 ++++++++++ include/hw/virtio/vhost.h | 1 + include/net/vhost_net.h | 2 ++ 3 files changed, 13 insertions(+)hw/net/vhost_net.c +0
-0@@ -80,6 +80,11 @@ void vhost_net_ack_features(struct vhost_net *net,unsigned features)
hw/net/vhost_net.c struct vhost_net *vhost_net_init(VhostNetOptions
Quoted 12 lines
hw/net/vhost_net.c +5
-0} } +unsigned long long vhost_net_features (VHostNetState *net) +{ +return net->dev.features; +} +static int vhost_net_get_fd (NetClientState *backend) {switch (backend->info->type) {@@ -112,6 +117,7 @@ struct vhost_net *vhost_net_init(VhostNetOptions*options)
hw/net/vhost_net.c unsigned vhost_net_get_features(struct vhost_net
Quoted 8 lines
hw/net/vhost_net.c +1
-0net->dev.nvqs =2 ; net->dev.vqs = net->vqs; + net->dev.mandatory_features = options->mandatory_features; r =vhost_dev_init (&net->dev, options->opaque, options->force);@@ -347,6 +353,10 @@ unsigned vhost_net_get_features(struct vhost_net*net, unsigned features)
Quoted 44 lines
hw/net/vhost_net.c +4
-0void vhost_net_ack_features (struct vhost_net *net,unsigned features) { } +unsigned long long vhost_net_features (struct vhost_net *net) +{ +return 0 ; +}bool vhost_net_virtqueue_pending (VHostNetState *net,int idx) {include/hw/virtio/vhost.h +1
-0@@ -41,6 +41,7 @@ struct vhost_dev {unsigned long long features;unsigned long long acked_features;unsigned long long backend_features; +unsigned long long mandatory_features;bool started;bool log_enabled;vhost_log_chunk_t *log;include/net/vhost_net.h +2
-1@@ -10,6 +10,7 @@ typedef struct VhostNetOptions {NetClientState *net_backend;void *opaque;bool force; +unsigned long long mandatory_features; } VhostNetOptions;struct vhost_net *vhost_net_init (VhostNetOptions *options);@@ -22,6 +23,7 @@ void vhost_net_cleanup(VHostNetState *net);unsigned vhost_net_get_features (VHostNetState *net,unsigned features);void vhost_net_ack_features (VHostNetState *net,unsigned features); +unsigned long long vhost_net_features (VHostNetState *net);bool vhost_net_virtqueue_pending (VHostNetState *net,int n); void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev, --1.8.3.2
Signature · 2 lines
Antonios Motakis Virtual Open Systems
PATCH v9 14/20 Add vhost-backend and VhostBackendType
In reply to Antonios MotakisUse vhost_set_backend_type to initialise a proper vhost_ops structure. In vhost_net_init and vhost_net_start_one call conditionally TAP related initialisation depending on the vhost backend type.
--- hw/net/vhost_net.c | 67 +++++++++++++++++++++++---------------- hw/scsi/vhost-scsi.c | 2 +- hw/virtio/Makefile.objs | 2 +- hw/virtio/vhost-backend.c | 66 ++++++++++++++++++++++++++++++++++++++ hw/virtio/vhost.c | 6 +++- include/hw/virtio/vhost-backend.h | 11 +++++++ include/hw/virtio/vhost.h | 4 +-- include/net/vhost_net.h | 2 ++ net/tap.c | 1 + 9 files changed, 129 insertions(+), 32 deletions(-) create mode 100644 hw/virtio/vhost-backend.c
hw/net/vhost_net.c +40 -27
@@ -98,7 +98,7 @@ static int vhost_net_get_fd(NetClientState *backend)struct vhost_net *vhost_net_init (VhostNetOptions *options) { -int r; +int r =-1 ;struct vhost_net *net =g_malloc (sizeof *net);if (!options->net_backend) {
@@ -106,9 +106,11 @@ struct vhost_net *vhost_net_init(VhostNetOptions *options) goto fail; } - r =vhost_net_get_fd (options->net_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->nc = options->net_backend; net->dev.backend_features = qemu_has_vnet_hdr(options->net_backend) ? 0 :
@@ -120,7 +122,7 @@ struct vhost_net *vhost_net_init(VhostNetOptions *options) net->dev.mandatory_features = options->mandatory_features; r =vhost_dev_init (&net->dev, options->opaque, - options->force); + options->backend_type, options->force);if (r <0 ) { goto fail; }
@@ -128,13 +130,15 @@ struct vhost_net *vhost_net_init(VhostNetOptions *options) 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 (~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;
@@ -177,23 +181,29 @@ static int vhost_net_start_one(struct vhost_net *net, net->nc->info->poll (net->nc, false); } -qemu_set_fd_handler (net->backend,NULL ,NULL ,NULL ); - file.fd = net->backend; -for (file.index =0 ; file.index < net->dev.nvqs; ++file.index) { -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; +if (net->nc->info->type ==NET_CLIENT_OPTIONS_KIND_TAP ) { +qemu_set_fd_handler (net->backend,NULL ,NULL ,NULL ); + file.fd = net->backend; +for (file.index =0 ; file.index < net->dev.nvqs; ++file.index) { +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; + } } }return 0 ; fail: file.fd =-1 ; -while (file.index-- >0 ) { -const VhostOps *vhost_ops = net->dev.vhost_ops; -int r = vhost_ops->vhost_call (&net->dev,VHOST_NET_SET_BACKEND , &file); -assert (r >=0 ); +if (net->nc->info->type ==NET_CLIENT_OPTIONS_KIND_TAP ) { +while (file.index-- >0 ) { +const VhostOps *vhost_ops = net->dev.vhost_ops; +int r = vhost_ops->vhost_call (&net->dev,VHOST_NET_SET_BACKEND , + &file); +assert (r >=0 ); + } }if (net->nc->info->poll) { net->nc->info->poll (net->nc, true);
@@ -214,10 +224,13 @@ static void vhost_net_stop_one(struct vhost_net *net,return ; } -for (file.index =0 ; file.index < net->dev.nvqs; ++file.index) { -const VhostOps *vhost_ops = net->dev.vhost_ops; -int r = vhost_ops->vhost_call (&net->dev,VHOST_NET_SET_BACKEND , &file); -assert (r >=0 ); +if (net->nc->info->type ==NET_CLIENT_OPTIONS_KIND_TAP ) { +for (file.index =0 ; file.index < net->dev.nvqs; ++file.index) { +const VhostOps *vhost_ops = net->dev.vhost_ops; +int r = vhost_ops->vhost_call (&net->dev,VHOST_NET_SET_BACKEND , + &file); +assert (r >=0 ); + } }if (net->nc->info->poll) { net->nc->info->poll (net->nc, true);
hw/scsi/vhost-scsi.c +1 -1
@@ -239,7 +239,7 @@ static void vhost_scsi_realize(DeviceState *dev, Error **errp) s->dev.vq_index =0 ; ret =vhost_dev_init (&s->dev, (void *)(uintptr_t )vhostfd, - true); +VHOST_BACKEND_TYPE_KERNEL , true);if (ret <0 ) {error_setg (errp,"vhost-scsi: vhost initialization failed: %s" ,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 +66 -0
66 lines not on this page · raw message
hw/virtio/vhost.c +5 -1
@@ -815,11 +815,15 @@ static void vhost_virtqueue_cleanup(struct vhost_virtqueue *vq) }int vhost_dev_init (struct vhost_dev *hdev,void *opaque, -bool force) +VhostBackendType backend_type,bool force) {uint64_t features;int i, r; +if (vhost_set_backend_type (hdev, backend_type) <0 ) { +return -1 ; + } +if (hdev->vhost_ops->vhost_backend_init (hdev, opaque) <0 ) {return -errno; }
include/hw/virtio/vhost-backend.h +11 -0
@@ -11,6 +11,13 @@#ifndef VHOST_BACKEND_H_ #define VHOST_BACKEND_H_ +typedef enum VhostBackendType { +VHOST_BACKEND_TYPE_NONE =0 , +VHOST_BACKEND_TYPE_KERNEL =1 , +VHOST_BACKEND_TYPE_USER =2 , +VHOST_BACKEND_TYPE_MAX =3 , +}VhostBackendType ; +struct vhost_dev ; typedef int (*vhost_call)(struct vhost_dev *dev, unsigned long int request,
@@ -19,9 +26,13 @@ typedef int (*vhost_backend_init)(struct vhost_dev *dev, void *opaque);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 +2 -2
@@ -30,7 +30,6 @@ typedef unsigned long vhost_log_chunk_t;struct vhost_memory ;struct vhost_dev {MemoryListener memory_listener; -int control;struct vhost_memory *mem;int n_mem_sections;MemoryRegionSection *mem_sections;
@@ -51,10 +50,11 @@ struct vhost_dev {hwaddr mem_changed_start_addr;hwaddr mem_changed_end_addr;const VhostOps *vhost_ops; +void *opaque; };int vhost_dev_init (struct vhost_dev *hdev,void *opaque, -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);
include/net/vhost_net.h +2 -0
@@ -2,11 +2,13 @@#define VHOST_NET_H #include "net/net.h" +#include "hw/virtio/vhost-backend.h" struct vhost_net ;typedef struct vhost_net VHostNetState ; typedef struct VhostNetOptions { + VhostBackendType backend_type; NetClientState *net_backend; void *opaque; bool force;
net/tap.c +1 -0
@@ -630,6 +630,7 @@ static int net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer, vhostfdname || (tap->has_vhostforce && tap->vhostforce)) {VhostNetOptions options; + options.backend_type =VHOST_BACKEND_TYPE_KERNEL ; options.net_backend = &s->nc; options.force = tap->has_vhostforce && tap->vhostforce;
Signature · 1 line
1.8.3.2
PATCH v9 15/20 Add vhost-user as a vhost backend.
In reply to Antonios MotakisThe initialization takes a chardev backed by a unix domain socket. It should implement qemu_fe_set_msgfds in order to be able to pass file descriptors to the remote process.
Each ioctl request of vhost-kernel has a vhost-user message equivalent, which is sent 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 will be placed in the fds array for inclusion in the sendmsg control header.
VHOST_SET_MEM_TABLE ignores the supplied vhost_memory structure and scans the global ram_list for ram blocks with a valid fd field set. This would be set when the -mem-path option with shared=on property is used.
Upon receiving VHOST_USER_GET_FEATURES reply, the new features value is compared to the mandatory features in the vhost_dev.
Message VHOST_USER_GET_VRING_BASE gets a special handling in case of I/O failure - return -1 to indicate the upper layer that it failed.
--- hw/virtio/Makefile.objs | 2 +- hw/virtio/vhost-backend.c | 5 + hw/virtio/vhost-user.c | 356 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 362 insertions(+), 1 deletion(-) create mode 100644 hw/virtio/vhost-user.c
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 vhost-backend.o +obj-$(CONFIG_LINUX) += vhost.o vhost-backend.o vhost-user.o
hw/virtio/vhost-backend.c +5 -0
@@ -14,6 +14,8 @@#include <sys/ioctl.h> +extern const VhostOps user_ops; +static int vhost_kernel_call (struct vhost_dev *dev,unsigned long int request,void *arg) {
@@ -57,6 +59,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 :error_report ("Unknown vhost backend type\n" ); r =-1 ;
hw/virtio/vhost-user.c new file +356 -0
356 lines not on this page · raw message
Signature · 1 line
1.8.3.2
PATCH v9 16/20 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. It uses an Unix socket chardev to establish a communication with the 'slave' (client and server mode supported).
At runtime the netdev will handle OPEN/CLOSE events from the chardev. Upon disconnection it will set link_down accordingly and notify virtio-net; the virtio-net interface will go down.
On reconnection a new vhost-net instance will get the saved features from the previous session from the saved 'mandatory features'. This will ensure that the newly connected slave is compatible with the first one.
--- include/net/vhost-user.h | 17 ++++++ net/Makefile.objs | 2 +- net/clients.h | 3 + net/vhost-user.c | 150 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 include/net/vhost-user.h create mode 100644 net/vhost-user.c
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_ */
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/vhost-user.c new file +150 -0
150 lines not on this page · raw message
Signature · 1 line
1.8.3.2
PATCH v9 17/20 Add the vhost-user netdev backend to the command line
In reply to Antonios MotakisThe supplied chardev id will be inspected for supported options. Only a socket backend, with a set path (i.e. a Unix socket) and optionally the server parameter set, will be allowed. Other options (nowait, telnet) will make the chardev unusable and the netdev will not be initialised.
Additional checks for validity:
- requires -mempath ...,share=on
- requires -device virtio-net-*
The vhostforce option is used to force vhost-net when we deal with non-MSIX guests.
--- hmp-commands.hx | 4 +- hw/net/vhost_net.c | 4 ++ net/hub.c | 1 + net/net.c | 25 ++++++----- net/vhost-user.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++- qapi-schema.json | 19 +++++++- qemu-options.hx | 17 +++++++ 7 files changed, 181 insertions(+), 16 deletions(-)
hmp-commands.hx +2 -2
@@ -1195,7 +1195,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, },
@@ -1223,7 +1223,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 +4 -0
@@ -15,6 +15,7 @@#include "net/net.h" #include "net/tap.h" +#include "net/vhost-user.h" #include "hw/virtio/virtio-net.h" #include "net/vhost_net.h"
@@ -325,6 +326,9 @@ VHostNetState *get_vhost_net(NetClientState *nc)case NET_CLIENT_OPTIONS_KIND_TAP : vhost_net =tap_get_vhost_net (nc);break ; +case NET_CLIENT_OPTIONS_KIND_VHOST_USER : + vhost_net =vhost_user_get_vhost_net (nc); +break ;default :break ; }
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 +14 -11
@@ -769,23 +769,24 @@ static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX])(const NetClientOptions *opts, const char *name, NetClientState *peer) = { - [NET_CLIENT_OPTIONS_KIND_NIC ] = net_init_nic, + [NET_CLIENT_OPTIONS_KIND_NIC ] = net_init_nic, #ifdef CONFIG_SLIRP - [NET_CLIENT_OPTIONS_KIND_USER ] = net_init_slirp, + [NET_CLIENT_OPTIONS_KIND_USER ] = net_init_slirp, #endif - [NET_CLIENT_OPTIONS_KIND_TAP ] = net_init_tap, - [NET_CLIENT_OPTIONS_KIND_SOCKET ] = net_init_socket, + [NET_CLIENT_OPTIONS_KIND_TAP ] = net_init_tap, + [NET_CLIENT_OPTIONS_KIND_SOCKET ] = net_init_socket, #ifdef CONFIG_VDE - [NET_CLIENT_OPTIONS_KIND_VDE ] = net_init_vde, + [NET_CLIENT_OPTIONS_KIND_VDE ] = net_init_vde, #endif #ifdef CONFIG_NETMAP - [NET_CLIENT_OPTIONS_KIND_NETMAP ] = net_init_netmap, + [NET_CLIENT_OPTIONS_KIND_NETMAP ] = net_init_netmap, #endif - [NET_CLIENT_OPTIONS_KIND_DUMP ] = net_init_dump, + [NET_CLIENT_OPTIONS_KIND_DUMP ] = net_init_dump, #ifdef CONFIG_NET_BRIDGE - [NET_CLIENT_OPTIONS_KIND_BRIDGE ] = net_init_bridge, + [NET_CLIENT_OPTIONS_KIND_BRIDGE ] = net_init_bridge, #endif - [NET_CLIENT_OPTIONS_KIND_HUBPORT ] = net_init_hubport, + [NET_CLIENT_OPTIONS_KIND_HUBPORT ] = net_init_hubport, + [NET_CLIENT_OPTIONS_KIND_VHOST_USER ] = net_init_vhost_user, };
@@ -819,6 +820,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 :
@@ -902,11 +904,12 @@ static int net_host_check_device(const char *device) , "bridge" #endif #ifdef CONFIG_SLIRP - ,"user" + , "user" #endif #ifdef CONFIG_VDE - ,"vde" + , "vde" #endif + , "vhost-user" }; for (i = 0; i < ARRAY_SIZE(valid_param_list); i++) { if (!strncmp(valid_param_list[i], device,
net/vhost-user.c +125 -2
155 lines not on this page · raw message
qapi-schema.json +18 -1
@@ -3183,6 +3183,22 @@ '*devname': 'str' } } ## +# @NetdevVhostUserOptions +# +# Vhost-user network backend +# +# @path: control socket path +# +# @vhostforce: #optional vhost on for non-MSIX virtio guests (default: false). +# +# Since 2.0 +## +{ 'type': 'NetdevVhostUserOptions', + 'data': { + 'chardev': 'str', + '*vhostforce': 'bool' } } + +## # @NetClientOptions # # A discriminated record of network device traits.
@@ -3200,7 +3216,8 @@ 'dump': 'NetdevDumpOptions', 'bridge': 'NetdevBridgeOptions', 'hubport': 'NetdevHubPortOptions', - 'netmap': 'NetdevNetmapOptions' } } + 'netmap': 'NetdevNetmapOptions', + 'vhost-user': 'NetdevVhostUserOptions' } } ## # @NetLegacy
qemu-options.hx +17 -0
@@ -1434,6 +1434,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
@@ -1765,6 +1766,22 @@ The hubport netdev lets you connect a NIC to a QEMU "vlan" instead of a single netdev. @code{-net} and @code{-device} with parameter @option{vlan} create the required hub automatically. +@item -netdev vhost-user,chardev=@var{id}[,vhostforce=on|off] + +Establish a vhost-user netdev, backed by a chardev @var{id}. The chardev should +be a unix domain socket backed one. The vhost-user uses a specifically defined +protocol to pass vhost ioctl replacement messages to an application on the other +end of the socket. On non-MSIX guests, the feature can be forced with +@var{vhostforce}. + +Example: +@example +qemu -m 1024 -mem-path /hugetlbfs,prealloc=on,share=on \ + -chardev socket,path=/path/to/socket \ + -netdev type=vhost-user,id=net0,chardev=chr0 \ + -device virtio-net-pci,netdev=net0 +@end example + @item -net dump[,vlan=@var{n}][,file=@var{file}][,len=@var{len}] Dump network traffic on VLAN @var{n} to file @var{file} (@file{qemu-vlan0.pcap} by default). At most @var{len} bytes (64k by default) per packet are stored. The file format is
Signature · 1 line
1.8.3.2
PATCH v9 18/20 Add vhost-user protocol documentation
In reply to Antonios MotakisThis document describes the basic message format used by vhost-user for communication over a unix domain socket. The protocol is based on the existing ioctl interface used for the kernel version of vhost.
--- docs/specs/vhost-user.txt | 261 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 261 insertions(+) create mode 100644 docs/specs/vhost-user.txt
docs/specs/vhost-user.txt new file +261 -0
@@ -0,0 +1,261 @@ +Vhost-user Protocol +=================== + +This protocol is aiming to complement the ioctl interface used to control the +vhost implementation in the Linux kernel. It implements the control plane needed +to establish virtqueue sharing with a user space process on the same host. It +uses communication over a Unix domain socket to share file descriptors in the +ancillary data of the message. + +The protocol defines 2 sides of the communication, master and slave. Master is +the application that shares it's virtqueues, in our case QEMU. Slave is the +consumer of the virtqueues. + +In the current implementation QEMU is the Master, and the Slave is intended to +be a software ethernet switch running in user space, such as Snabbswitch. + +Master and slave can be either a client (i.e. connecting) or server (listening) +in the socket communication. + +Message Specification +--------------------- + +Note that all numbers are in the machine native byte order. A vhost-user message +consists of 3 header fields and a payload: + +------------------------------------ +| request | flags | size | payload | +------------------------------------ + + * Request: 32-bit type of the request + * Flags: 32-bit bit field: + - Lower 2 bits are the version (currently 0x01) + - Bit 2 is the reply flag - needs to be sent on each reply from the slave + * Size - 32-bit size of the payload + + +Depending on the request type, payload can be: + + * A single 64-bit integer + ------- + | u64 | + ------- + + u64: a 64-bit unsigned integer + + * A vring state description + --------------- + | index | num | + --------------- + + Index: a 32-bit index + Num: a 32-bit number + + * A vring address description + -------------------------------------------------------------- + | index | flags | size | descriptor | used | available | log | + -------------------------------------------------------------- + + Index: a 32-bit vring index + Flags: a 32-bit vring flags + Descriptor: a 64-bit user address of the vring descriptor table + Used: a 64-bit user address of the vring used ring + Available: a 64-bit user address of the vring available ring + Log: a 64-bit guest address for logging + + * Memory regions description + --------------------------------------------------- + | num regions | padding | region0 | ... | region7 | + --------------------------------------------------- + + Num regions: a 32-bit number of regions + Padding: 32-bit + + A region is: + --------------------------------------- + | guest address | size | user address | + --------------------------------------- + + Guest address: a 64-bit guest address of the region + Size: a 64-bit size + User address: a 64-bit user address + + +In QEMU the vhost-user message is implemented with the following struct: + +typedef struct VhostUserMsg { + VhostUserRequest request; + uint32_t flags; + uint32_t size; + union { + uint64_t u64; + struct vhost_vring_state state; + struct vhost_vring_addr addr; + VhostUserMemory memory; + }; +} QEMU_PACKED VhostUserMsg; + +Communication +------------- + +The protocol for vhost-user is based on the existing implementation of vhost +for the Linux Kernel. Most messages that can be send via the Unix domain socket +implementing vhost-user have an equivalent ioctl to the kernel implementation. + +The communication consists of master sending message requests and slave sending +message replies. Most of the requests don't require replies. Here is a list of +the ones that do: + + * VHOST_GET_FEATURES + * VHOST_GET_VRING_BASE + +There are several messages that the master sends with file descriptors passed +in the ancillary data: + + * VHOST_SET_MEM_TABLE + * VHOST_SET_LOG_FD + * VHOST_SET_VRING_KICK + * VHOST_SET_VRING_CALL + * VHOST_SET_VRING_ERR + +If Master is unable to send the full message or receives a wrong reply it will +close the connection. An optional reconnection mechanism can be implemented. + +Message types +------------- + + * VHOST_USER_GET_FEATURES + + Id: 2 + Equivalent ioctl: VHOST_GET_FEATURES + Master payload: N/A + Slave payload: u64 + + Get from the underlying vhost implementation the features bitmask. + + * VHOST_USER_SET_FEATURES + + Id: 3 + Ioctl: VHOST_SET_FEATURES + Master payload: u64 + + Enable features in the underlying vhost implementation using a bitmask. + + * VHOST_USER_SET_OWNER + + Id: 4 + Equivalent ioctl: VHOST_SET_OWNER + Master payload: N/A + + Issued when a new connection is established. It sets the current Master + as an owner of the session. This can be used on the Slave as a + "session start" flag. + + * VHOST_USER_RESET_OWNER + + Id: 5 + Equivalent ioctl: VHOST_RESET_OWNER + Master payload: N/A + + Issued when a new connection is about to be closed. The Master will no + longer own this connection (and will usually close it). + + * VHOST_USER_SET_MEM_TABLE + + Id: 6 + Equivalent ioctl: VHOST_SET_MEM_TABLE + Master payload: memory regions description + + Sets the memory map regions on the slave so it can translate the vring + addresses. In the ancillary data there is an array of file descriptors + for each memory mapped region. The size and ordering of the fds matches + the number and ordering of memory regions. + + * VHOST_USER_SET_LOG_BASE + + Id: 7 + Equivalent ioctl: VHOST_SET_LOG_BASE + Master payload: u64 + + Sets the logging base address. + + * VHOST_USER_SET_LOG_FD + + Id: 8 + Equivalent ioctl: VHOST_SET_LOG_FD + Master payload: N/A + + Sets the logging file descriptor, which is passed as ancillary data. + + * VHOST_USER_SET_VRING_NUM + + Id: 9 + Equivalent ioctl: VHOST_SET_VRING_NUM + Master payload: vring state description + + Sets the number of vrings for this owner. + + * VHOST_USER_SET_VRING_ADDR + + Id: 10 + Equivalent ioctl: VHOST_SET_VRING_ADDR + Master payload: vring address description + Slave payload: N/A + + Sets the addresses of the different aspects of the vring. + + * VHOST_USER_SET_VRING_BASE + + Id: 11 + Equivalent ioctl: VHOST_SET_VRING_BASE + Master payload: vring state description + + Sets the base address where the available descriptors are. + + * VHOST_USER_GET_VRING_BASE + + Id: 12 + Equivalent ioctl: VHOST_USER_GET_VRING_BASE + Master payload: vring state description + Slave payload: vring state description + + Get the vring base address. + + * VHOST_USER_SET_VRING_KICK + + Id: 13 + Equivalent ioctl: VHOST_SET_VRING_KICK + Master payload: u64 + + Set the event file descriptor for adding buffers to the vring. It + is passed in the ancillary data. + Bits (0-7) of the payload contain the vring index. Bit 8 is the + invalid FD flag. This flag is set when there is no file descriptor + in the ancillary data. This signals that polling should be used + instead of waiting for a kick. + + * VHOST_USER_SET_VRING_CALL + + Id: 14 + Equivalent ioctl: VHOST_SET_VRING_CALL + Master payload: u64 + + Set the event file descriptor to signal when buffers are used. It + is passed in the ancillary data. + Bits (0-7) of the payload contain the vring index. Bit 8 is the + invalid FD flag. This flag is set when there is no file descriptor + in the ancillary data. This signals that polling will be used + instead of waiting for the call. + + * VHOST_USER_SET_VRING_ERR + + Id: 15 + Equivalent ioctl: VHOST_SET_VRING_ERR + Master payload: u64 + + Set the event file descriptor to signal when error occurs. It + is passed in the ancillary data. + Bits (0-7) of the payload contain the vring index. Bit 8 is the + invalid FD flag. This flag is set when there is no file descriptor + in the ancillary data. +
Signature · 1 line
1.8.3.2
PATCH v9 19/20 libqemustub: add stubs to be able to use qemu-char.c
In reply to Antonios Motakischardev depends on lots of external symbols that are not necessarily needed to be able to use, for example, 'socket chardev'. So add stubs for these functions:
- bdrv_commit_all
- qemu_chr_open_msmouse
- is_daemonized
- qemu_add_machine_init_done_notifier
- monitor_init
- qemu_notify_event
- vc_init
and this array:
- serial_hds
--- stubs/Makefile.objs | 8 ++++++++ stubs/bdrv-commit-all.c | 7 +++++++ stubs/chr-msmouse.c | 7 +++++++ stubs/get-next-serial.c | 3 +++ stubs/is-daemonized.c | 7 +++++++ stubs/machine-init-done.c | 6 ++++++ stubs/monitor-init.c | 6 ++++++ stubs/notify-event.c | 6 ++++++ stubs/vc-init.c | 7 +++++++ 9 files changed, 57 insertions(+) create mode 100644 stubs/bdrv-commit-all.c create mode 100644 stubs/chr-msmouse.c create mode 100644 stubs/get-next-serial.c create mode 100644 stubs/is-daemonized.c create mode 100644 stubs/machine-init-done.c create mode 100644 stubs/monitor-init.c create mode 100644 stubs/notify-event.c create mode 100644 stubs/vc-init.c
stubs/Makefile.objs +8 -0
@@ -1,4 +1,6 @@ stub-obj-y += arch-query-cpu-def.o +stub-obj-y += bdrv-commit-all.o +stub-obj-y += chr-msmouse.o stub-obj-y += clock-warp.o stub-obj-y += cpu-get-clock.o stub-obj-y += cpu-get-icount.o
@@ -9,20 +11,26 @@ stub-obj-y += fdset-get-fd.o stub-obj-y += fdset-remove-fd.o stub-obj-y += gdbstub.o stub-obj-y += get-fd.o +stub-obj-y += get-next-serial.o stub-obj-y += get-vm-name.o stub-obj-y += iothread-lock.o +stub-obj-y += is-daemonized.o +stub-obj-y += machine-init-done.o stub-obj-y += migr-blocker.o stub-obj-y += mon-is-qmp.o stub-obj-y += mon-printf.o stub-obj-y += mon-print-filename.o stub-obj-y += mon-protocol-event.o stub-obj-y += mon-set-error.o +stub-obj-y += monitor-init.o +stub-obj-y += notify-event.o stub-obj-y += pci-drive-hot-add.o stub-obj-y += reset.o stub-obj-y += set-fd-handler.o stub-obj-y += slirp.o stub-obj-y += sysbus.o stub-obj-y += uuid.o +stub-obj-y += vc-init.o stub-obj-y += vm-stop.o stub-obj-y += vmstate.o stub-obj-$(CONFIG_WIN32) += fd-register.o
stubs/bdrv-commit-all.c new file +7 -0
@@ -0,0 +1,7 @@ +#include "qemu-common.h" +#include "block/block.h" + +int bdrv_commit_all (void ) +{ +return 0 ; +}
stubs/chr-msmouse.c new file +7 -0
@@ -0,0 +1,7 @@ +#include "qemu-common.h" +#include "sysemu/char.h" + +CharDriverState *qemu_chr_open_msmouse (void ) +{ +return 0 ; +}
stubs/get-next-serial.c new file +3 -0
@@ -0,0 +1,3 @@ +#include "qemu-common.h" + +CharDriverState *serial_hds[0 ];
stubs/is-daemonized.c new file +7 -0
@@ -0,0 +1,7 @@ +#include "qemu-common.h" +#include "sysemu/os-posix.h" + +bool is_daemonized (void ) +{ +return true; +}
stubs/machine-init-done.c new file +6 -0
@@ -0,0 +1,6 @@ +#include "qemu-common.h" +#include "sysemu/sysemu.h" + +void qemu_add_machine_init_done_notifier (Notifier *notify) +{ +}
stubs/monitor-init.c new file +6 -0
@@ -0,0 +1,6 @@ +#include "qemu-common.h" +#include "monitor/monitor.h" + +void monitor_init (CharDriverState *chr,int flags) +{ +}
stubs/notify-event.c new file +6 -0
@@ -0,0 +1,6 @@ +#include "qemu-common.h" +#include "qemu/main-loop.h" + +void qemu_notify_event (void ) +{ +}
stubs/vc-init.c new file +7 -0
@@ -0,0 +1,7 @@ +#include "qemu-common.h" +#include "ui/console.h" + +CharDriverState *vc_init (ChardevVC *vc) +{ +return 0 ; +}
Signature · 1 line
1.8.3.2
PATCH v9 20/20 Add qtest for vhost-user
In reply to Antonios MotakisThis test creates a 'server' chardev to listen for vhost-user messages. Once VHOST_USER_SET_MEM_TABLE is received it mmaps each received region, and read 1k bytes from it. The read data is compared to data from readl.
The test requires hugetlbfs to be already mounted and writable. The mount point defaults to '/hugetlbfs' and can be specified via the environment variable QTEST_HUGETLBFS_PATH.
The rom pc-bios/pxe-virtio.rom is used to instantiate a virtio pcicontroller.
--- tests/Makefile | 4 + tests/vhost-user-test.c | 309 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 313 insertions(+) create mode 100644 tests/vhost-user-test.c
tests/Makefile +4 -0
@@ -110,6 +110,7 @@ check-qtest-i386-y += tests/vmxnet3-test$(EXESUF) gcov-files-i386-y += hw/net/vmxnet3.c gcov-files-i386-y += hw/net/vmxnet_rx_pkt.c gcov-files-i386-y += hw/net/vmxnet_tx_pkt.c +check-qtest-i386-y += tests/vhost-user-test$(EXESUF) check-qtest-x86_64-y = $(check-qtest-i386-y) gcov-files-i386-y += i386-softmmu/hw/timer/mc146818rtc.c gcov-files-x86_64-y = $(subst i386-softmmu/,x86_64-softmmu/,$(gcov-files-i386-y))
@@ -241,8 +242,11 @@ tests/ipoctal232-test$(EXESUF): tests/ipoctal232-test.o tests/qom-test$(EXESUF): tests/qom-test.o tests/blockdev-test$(EXESUF): tests/blockdev-test.o $(libqos-pc-obj-y) tests/qdev-monitor-test$(EXESUF): tests/qdev-monitor-test.o $(libqos-pc-obj-y) +tests/vhost-user-test$(EXESUF): tests/vhost-user-test.o qemu-char.o qemu-timer.o libqemuutil.a libqemustub.a tests/qemu-iotests/socket_scm_helper$(EXESUF): tests/qemu-iotests/socket_scm_helper.o +LIBS+= -lutil + # QTest rules TARGETS=$(patsubst %-softmmu,%, $(filter %-softmmu,$(TARGET_DIRS)))
tests/vhost-user-test.c new file +309 -0
309 lines not on this page · raw message
Signature · 1 line
1.8.3.2
Re: [Qemu-devel] [PATCH v9 20/20] Add qtest for vhost-user
Am 04.03.2014 19:23, schrieb Antonios Motakis:
Quoted 23 lines
This test creates a 'server' chardev to listen for vhost-user messages. Once
VHOST_USER_SET_MEM_TABLEis received it mmaps each received region, and read 1k bytes from it. The read data is compared to data from readl.The test requires hugetlbfs to be already mounted and writable. The mount point defaults to '/hugetlbfs' and can be specified via the environment variable
QTEST_HUGETLBFS_PATH.The rom pc-bios/pxe-virtio.rom is used to instantiate a virtio pcicontroller.
Signed-off-by Antonios Motakis <[email protected]>Signed-off-by Nikolay Nikolaev <[email protected]>--- tests/Makefile | 4 + tests/vhost-user-test.c | 309 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 313 insertions(+) create mode 100644 tests/vhost-user-test.ctests/Makefile +0
-0
tests/Makefile check-qtest-i386-y += tests/vmxnet3-test$(EXESUF) · line 113
Quoted 4 lines
tests/Makefile +1
-0@@ -110,6 +110,7 @@ check-qtest-i386-y += tests/vmxnet3-test$(EXESUF) gcov-files-i386-y += hw/net/vmxnet3.c gcov-files-i386-y += hw/net/vmxnet_rx_pkt.c gcov-files-i386-y += hw/net/vmxnet_tx_pkt.c +check-qtest-i386-y += tests/vhost-user-test$(EXESUF)
Not sure if I've asked already, but doesn't this test depend on certain Linux host support (hugetlbfs, vhost module)?
One more comment below:
Quoted 315 lines
tests/Makefile +3
-0check-qtest-x86_64-y = $(check-qtest-i386-y) gcov-files-i386-y += i386-softmmu/hw/timer/mc146818rtc.c gcov-files-x86_64-y = $(subst i386-softmmu/,x86_64-softmmu/,$(gcov-files-i386-y))@@ -241,8 +242,11 @@ tests/ipoctal232-test$(EXESUF): tests/ipoctal232-test.o tests/qom-test$(EXESUF): tests/qom-test.o tests/blockdev-test$(EXESUF): tests/blockdev-test.o $(libqos-pc-obj-y) tests/qdev-monitor-test$(EXESUF): tests/qdev-monitor-test.o $(libqos-pc-obj-y) +tests/vhost-user-test$(EXESUF): tests/vhost-user-test.o qemu-char.o qemu-timer.o libqemuutil.a libqemustub.a tests/qemu-iotests/socket_scm_helper$(EXESUF): tests/qemu-iotests/socket_scm_helper.o +LIBS+= -lutil + # QTest rules TARGETS=$(patsubst %-softmmu,%, $(filter %-softmmu,$(TARGET_DIRS)))tests/vhost-user-test.c new file +294
-0@@ -0,0 +1,309 @@ +/* +* QTest testcase for the vhost-user +* +* Copyright (c) 2014 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. +* +*/ + +#include "libqtest.h" +#include "qemu/option.h" +#include "sysemu/char.h" +#include "sysemu/sysemu.h" + +#include <glib.h> +#include <linux/vhost.h> +#include <sys/mman.h> +#include <sys/vfs.h> +#include <qemu/sockets.h> + +#define QEMU_CMD_ACCEL " -machine accel=tcg" +#define QEMU_CMD_MEM " -mem-path %s,share=on" +#define QEMU_CMD_CHR " -chardev socket,id=chr0,path=%s" +#define QEMU_CMD_NETDEV " -netdev vhost-user,id=net0,chardev=chr0,vhostforce" +#define QEMU_CMD_NET " -device virtio-net-pci,netdev=net0 " +#define QEMU_CMD_ROM " -option-rom ../pc-bios/pxe-virtio.rom" + +#define QEMU_CMD QEMU_CMD_ACCEL QEMU_CMD_MEM QEMU_CMD_CHR \ + QEMU_CMD_NETDEV QEMU_CMD_NET QEMU_CMD_ROM + +#define HUGETLBFS_MAGIC 0x958458f6 + +/*********** FROM hw/virtio/vhost-user.c *************************************/ + +#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_MAX +}VhostUserRequest ; + +typedef struct VhostUserMemoryRegion { +uint64_t guest_phys_addr; +uint64_t memory_size; +uint64_t userspace_addr; +}VhostUserMemoryRegion ; + +typedef struct VhostUserMemory { +uint32_t nregions; +uint32_t padding; +VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS ]; +}VhostUserMemory ; + +typedef struct VhostUserMsg { +VhostUserRequest request; + +#define VHOST_USER_VERSION_MASK (0x3) +#define VHOST_USER_REPLY_MASK (0x1<<2) +uint32_t flags; +uint32_t size;/* the following payload size */ +union { +uint64_t u64; +struct vhost_vring_state state; +struct vhost_vring_addr addr; +VhostUserMemory memory; + }; +} QEMU_PACKED VhostUserMsg; + +static VhostUserMsg m __attribute__ ((unused)); +#define VHOST_USER_HDR_SIZE (sizeof(m.request) \ + + sizeof(m.flags) \ + + sizeof(m.size)) + +#define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE) + +/* The version of the protocol we support */ +#define VHOST_USER_VERSION (0x1) +/*****************************************************************************/ + +int fds_num =0 , fds[VHOST_MEMORY_MAX_NREGIONS ]; +static VhostUserMemory memory; +static GMutex data_mutex; +static GCond data_cond; + +static void read_guest_mem (void ) +{ +uint32_t *guest_mem; +gint64 end_time; +int i, j; + +g_mutex_lock (&data_mutex); + + end_time =g_get_monotonic_time () +5 *G_TIME_SPAN_SECOND ; +while (!fds_num) { +if (!g_cond_wait_until (&data_cond, &data_mutex, end_time)) { +/* timeout has passed */ +g_assert (fds_num); +break ; + } + } + +/* check for sanity */ + g_assert_cmpint(fds_num, >, 0); + g_assert_cmpint(fds_num, ==, memory.nregions); + +/* iterate all regions */ +for (i =0 ; i < fds_num; i++) { + +/* We'll check only he region statring at 0x0, suppose it is */ +if (memory.regions[i].guest_phys_addr !=0x0 ) { +continue ; + } + + g_assert_cmpint(memory.regions[i].memory_size, >, 1024); + + guest_mem =mmap (0 , memory.regions[i].memory_size, +PROT_READ |PROT_WRITE ,MAP_SHARED , fds[i],0 ); + +for (j =0 ; j <256 ; j++) { +uint32_t a =readl (memory.regions[i].guest_phys_addr + j*4 ); +uint32_t b = guest_mem[j]; + + g_assert_cmpint(a, ==, b); + } + +munmap (guest_mem, memory.regions[i].memory_size); + } + + g_assert_cmpint(1, ==, 1); +g_mutex_unlock (&data_mutex); +} + +static void *thread_function (void *data) +{ +GMainLoop *loop; + loop =g_main_loop_new (NULL , FALSE); +g_main_loop_run (loop); +return NULL ; +} + +static int chr_can_read (void *opaque) +{ +return VHOST_USER_HDR_SIZE ; +} + +static void chr_read (void *opaque,const uint8_t *buf,int size) +{ +CharDriverState *chr = opaque; +VhostUserMsg msg; +uint8_t *p = (uint8_t *) &msg; +int fd; + +if (size !=VHOST_USER_HDR_SIZE ) { +g_test_message ("Wrong message size received %d\n" , size); +return ; + } + +memcpy (p, buf,VHOST_USER_HDR_SIZE ); + +if (msg.size) { + p +=VHOST_USER_HDR_SIZE ; +qemu_chr_fe_read_all (chr, p, msg.size); + } + +switch (msg.request) { +case VHOST_USER_GET_FEATURES : +/* send back features to qemu */ + msg.flags |=VHOST_USER_REPLY_MASK ; + msg.size =sizeof (m.u64); + msg.u64 =0 ; + p = (uint8_t *) &msg; +qemu_chr_fe_write_all (chr, p,VHOST_USER_HDR_SIZE + msg.size); +break ; + +case VHOST_USER_GET_VRING_BASE : +/* send back vring base to qemu */ + msg.flags |=VHOST_USER_REPLY_MASK ; + msg.size =sizeof (m.state); + msg.state.num =0 ; + p = (uint8_t *) &msg; +qemu_chr_fe_write_all (chr, p,VHOST_USER_HDR_SIZE + msg.size); +break ; + +case VHOST_USER_SET_MEM_TABLE : +/* received the mem table */ +memcpy (&memory, &msg.memory,sizeof (msg.memory)); + fds_num =qemu_chr_fe_get_msgfds (chr, fds,sizeof (fds) /sizeof (int )); + +/* signal the test that it can continue */ +g_cond_signal (&data_cond); +g_mutex_unlock (&data_mutex); +break ; + +case VHOST_USER_SET_VRING_KICK : +case VHOST_USER_SET_VRING_CALL : +/* consume the fd */ +qemu_chr_fe_get_msgfds (chr, &fd,1 ); +/* +* This is a non-blocking eventfd. +* The receive function forces it to be blocking, +* so revert it back to non-blocking. +*/ +qemu_set_nonblock (fd); +break ; +default : +break ; + } +} + +static const char *init_hugepagefs (void ) +{ +const char *path; +struct statfs fs; +int ret; + + path =getenv ("QTEST_HUGETLBFS_PATH" ); +if (!path) { + path ="/hugetlbfs" ; + } + +if (access (path,R_OK |W_OK |X_OK )) { +g_test_message ("access on path (%s): %s\n" , path,strerror (errno)); +return 0 ; + } + +do { + ret =statfs (path, &fs); + }while (ret !=0 && errno ==EINTR ); + +if (ret !=0 ) { +g_test_message ("statfs on path (%s): %s\n" , path,strerror (errno)); +return 0 ; + } + +if (fs.f_type !=HUGETLBFS_MAGIC ) { +g_test_message ("Warning: path not on HugeTLBFS: %s\n" , path); +return 0 ; + } + +return path; +} + +int main (int argc,char **argv) +{ +QTestState *s =NULL ; +CharDriverState *chr =NULL ; +const char *hugefs =0 ; +char *socket_path =0 ; +char *qemu_cmd =0 ; +char *chr_path =0 ; +int ret; + +g_test_init (&argc, &argv,NULL ); + +module_call_init (MODULE_INIT_QOM ); + + hugefs =init_hugepagefs (); +g_assert (hugefs); + + socket_path =g_strdup_printf ("/tmp/vhost-%d.sock" ,getpid ()); + +/* create char dev and add read handlers */ +qemu_add_opts (&qemu_chardev_opts); + chr_path =g_strdup_printf ("unix:%s,server,nowait" , socket_path); + chr =qemu_chr_new ("chr0" , chr_path,NULL ); +g_free (chr_path); +qemu_chr_add_handlers (chr, chr_can_read, chr_read,NULL , chr); + +/* run the main loop thread so the chardev may operate */ +g_mutex_init (&data_mutex); +g_cond_init (&data_cond); +g_mutex_lock (&data_mutex); +g_thread_new (NULL , thread_function,NULL ); + + qemu_cmd =g_strdup_printf (QEMU_CMD , hugefs, socket_path); + s =qtest_start (qemu_cmd); +g_free (qemu_cmd); + +qtest_add_func ("/vhost-user/read-guest-mem" , read_guest_mem);
tests/vhost-user-test.c line 304
Quoted 10 lines
tests/vhost-user-test.c new file +10
-0+ + ret =g_test_run (); + +if (s) { +qtest_quit (s); + } + +/* cleanup */ +unlink (socket_path); +g_free (socket_path);
I think this is probably too late to unlink in case the test fails, should probably be moved to right after qtest_start()?
Thanks for your efforts on this test!
Regards,
Andreas
Quoted 6 lines
tests/vhost-user-test.c new file +5
-0+g_cond_clear (&data_cond); +g_mutex_clear (&data_mutex); + +return ret; +}
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
Re: [Qemu-devel] [PATCH v9 20/20] Add qtest for vhost-user
Hello Andreas,
Quoted 21 lines · Andreas Färber
Am 04.03.2014 19:23, schrieb Antonios Motakis:
Quoted 9 lines
This test creates a 'server' chardev to listen for vhost-user messages. Once
VHOST_USER_SET_MEM_TABLEis received it mmaps each received region, and read 1k bytes from it. The read data is compared to data from readl.The test requires hugetlbfs to be already mounted and writable. The mount point defaults to '/hugetlbfs' and can be specified via the environment variable
QTEST_HUGETLBFS_PATH.The rom pc-bios/pxe-virtio.rom is used to instantiate a virtio
pcicontroller.
Quoted 6 lines
Signed-off-by Antonios Motakis <[email protected]>Signed-off-by Nikolay Nikolaev <[email protected]>--- tests/Makefile | 4 + tests/vhost-user-test.c | 309++++++++++++++++++++++++++++++++++++++++++++++++
Quoted 3 lines
2 files changed, 313 insertions(+) create mode 100644 tests/vhost-user-test.c
Quoted 14 lines
tests/Makefile check-qtest-i386-y += tests/vmxnet3-test$(EXESUF) · line 113
Quoted 9 lines
tests/Makefile +1
-0@@ -110,6 +110,7 @@ check-qtest-i386-y += tests/vmxnet3-test$(EXESUF) gcov-files-i386-y += hw/net/vmxnet3.c gcov-files-i386-y += hw/net/vmxnet_rx_pkt.c gcov-files-i386-y += hw/net/vmxnet_tx_pkt.c +check-qtest-i386-y += tests/vhost-user-test$(EXESUF)Not sure if I've asked already, but doesn't this test depend on certain Linux host support (hugetlbfs, vhost module)?
We depend on hugetlbfs being not only available, but also mounted and writable by us. Otherwise the test will fail.
We don't depend on vhost being available in the kernel; we do our own thing with a userspace vhost implementation, which is provided. So there is no external dependency related to vhost. The only external dependency is hugetlbfs (which we can't avoid, the feature we are testing relies on it).
Quoted 334 lines
One more comment below:
tests/Makefile check-qtest-i386-y += tests/vmxnet3-test$(EXESUF)
Quoted 3 lines
tests/Makefile +0
-0check-qtest-x86_64-y = $(check-qtest-i386-y) gcov-files-i386-y += i386-softmmu/hw/timer/mc146818rtc.c gcov-files-x86_64-y = $(substi386-softmmu/,x86_64-softmmu/,$(gcov-files-i386-y))
tests/Makefile tests/ipoctal232-test$(EXESUF):
Quoted 1 line
tests/Makefile +0
-0@@ -241,8 +242,11 @@ tests/ipoctal232-test$(EXESUF):
tests/ipoctal232-test.otests/Makefile tests/ipoctal232-test$(EXESUF): tests/ipoctal232-test.o
Quoted 3 lines
tests/Makefile +0
-0tests/qom-test$(EXESUF): tests/qom-test.o tests/blockdev-test$(EXESUF): tests/blockdev-test.o $(libqos-pc-obj-y) tests/qdev-monitor-test$(EXESUF): tests/qdev-monitor-test.o$(libqos-pc-obj-y)
tests/Makefile tests/ipoctal232-test$(EXESUF): · line 245
Quoted 1 line
tests/Makefile +1
-0+tests/vhost-user-test$(EXESUF): tests/vhost-user-test.o qemu-char.oqemu-timer.o libqemuutil.a libqemustub.a
tests/Makefile tests/ipoctal232-test$(EXESUF): · line 246
Quoted 1 line
tests/Makefile +0
-0tests/qemu-iotests/socket_scm_helper$(EXESUF):
tests/qemu-iotests/socket_scm_helper.oQuoted 12 lines
tests/Makefile +2
-0+LIBS+= -lutil + # QTest rules TARGETS=$(patsubst %-softmmu,%, $(filter %-softmmu,$(TARGET_DIRS)))tests/vhost-user-test.c new file +0
-0tests/vhost-user-test.c line 6
Quoted 6 lines
tests/vhost-user-test.c new file +6
-0@@ -0,0 +1,309 @@ +/* +* QTest testcase for the vhost-user +* +* Copyright (c) 2014 Virtual Open Systems Sarl. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later.
Quoted 9 lines
tests/vhost-user-test.c new file +9
-0+* See the COPYING file in the top-level directory. +* +*/ + +#include "libqtest.h" +#include "qemu/option.h" +#include "sysemu/char.h" +#include "sysemu/sysemu.h" +tests/vhost-user-test.c
Quoted 10 lines
tests/vhost-user-test.c new file +10
-0+#include <glib.h> +#include <linux/vhost.h> +#include <sys/mman.h> +#include <sys/vfs.h> +#include <qemu/sockets.h> + +#define QEMU_CMD_ACCEL " -machine accel=tcg" +#define QEMU_CMD_MEM " -mem-path %s,share=on" +#define QEMU_CMD_CHR " -chardev socket,id=chr0,path=%s" +#define QEMU_CMD_NETDEV " -netdevvhost-user,id=net0,chardev=chr0,vhostforce"
tests/vhost-user-test.c
Quoted 9 lines
tests/vhost-user-test.c new file +9
-0+#define QEMU_CMD_NET " -device virtio-net-pci,netdev=net0 " +#define QEMU_CMD_ROM " -option-rom ../pc-bios/pxe-virtio.rom" + +#define QEMU_CMD QEMU_CMD_ACCEL QEMU_CMD_MEM QEMU_CMD_CHR \ + QEMU_CMD_NETDEV QEMU_CMD_NET QEMU_CMD_ROM + +#define HUGETLBFS_MAGIC 0x958458f6 + +/*********** FROM hw/virtio/vhost-user.c *************************************/
Quoted 49 lines
tests/vhost-user-test.c new file +48
-0+ +#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_MAX +}VhostUserRequest ; + +typedef struct VhostUserMemoryRegion { +uint64_t guest_phys_addr; +uint64_t memory_size; +uint64_t userspace_addr; +}VhostUserMemoryRegion ; + +typedef struct VhostUserMemory { +uint32_t nregions; +uint32_t padding; +VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS ]; +}VhostUserMemory ; + +typedef struct VhostUserMsg { +VhostUserRequest request; + +#define VHOST_USER_VERSION_MASK (0x3) +#define VHOST_USER_REPLY_MASK (0x1<<2) +uint32_t flags; +uint32_t size;/* the following payload size */ +union { +uint64_t u64; +struct vhost_vring_state state; +struct vhost_vring_addr addr; +VhostUserMemory memory; + }; +} QEMU_PACKED VhostUserMsg;tests/vhost-user-test.c line 92
Quoted 10 lines
tests/vhost-user-test.c new file +10
-0+ +static VhostUserMsg m __attribute__ ((unused)); +#define VHOST_USER_HDR_SIZE (sizeof(m.request) \ + + sizeof(m.flags) \ + + sizeof(m.size)) + +#define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE) + +/* The version of the protocol we support */ +#define VHOST_USER_VERSION (0x1)+/*****************************************************************************/
Quoted 99 lines
tests/vhost-user-test.c new file +99
-0+ +int fds_num =0 , fds[VHOST_MEMORY_MAX_NREGIONS ]; +static VhostUserMemory memory; +static GMutex data_mutex; +static GCond data_cond; + +static void read_guest_mem (void ) +{ +uint32_t *guest_mem; +gint64 end_time; +int i, j; + +g_mutex_lock (&data_mutex); + + end_time =g_get_monotonic_time () +5 *G_TIME_SPAN_SECOND ; +while (!fds_num) { +if (!g_cond_wait_until (&data_cond, &data_mutex, end_time)) { +/* timeout has passed */ +g_assert (fds_num); +break ; + } + } + +/* check for sanity */ + g_assert_cmpint(fds_num, >, 0); + g_assert_cmpint(fds_num, ==, memory.nregions); + +/* iterate all regions */ +for (i =0 ; i < fds_num; i++) { + +/* We'll check only he region statring at 0x0, suppose it is */ +if (memory.regions[i].guest_phys_addr !=0x0 ) { +continue ; + } + + g_assert_cmpint(memory.regions[i].memory_size, >, 1024); + + guest_mem =mmap (0 , memory.regions[i].memory_size, +PROT_READ |PROT_WRITE ,MAP_SHARED , fds[i],0 ); + +for (j =0 ; j <256 ; j++) { +uint32_t a =readl (memory.regions[i].guest_phys_addr + j*4 ); +uint32_t b = guest_mem[j]; + + g_assert_cmpint(a, ==, b); + } + +munmap (guest_mem, memory.regions[i].memory_size); + } + + g_assert_cmpint(1, ==, 1); +g_mutex_unlock (&data_mutex); +} + +static void *thread_function (void *data) +{ +GMainLoop *loop; + loop =g_main_loop_new (NULL , FALSE); +g_main_loop_run (loop); +return NULL ; +} + +static int chr_can_read (void *opaque) +{ +return VHOST_USER_HDR_SIZE ; +} + +static void chr_read (void *opaque,const uint8_t *buf,int size) +{ +CharDriverState *chr = opaque; +VhostUserMsg msg; +uint8_t *p = (uint8_t *) &msg; +int fd; + +if (size !=VHOST_USER_HDR_SIZE ) { +g_test_message ("Wrong message size received %d\n" , size); +return ; + } + +memcpy (p, buf,VHOST_USER_HDR_SIZE ); + +if (msg.size) { + p +=VHOST_USER_HDR_SIZE ; +qemu_chr_fe_read_all (chr, p, msg.size); + } + +switch (msg.request) { +case VHOST_USER_GET_FEATURES : +/* send back features to qemu */ + msg.flags |=VHOST_USER_REPLY_MASK ; + msg.size =sizeof (m.u64); + msg.u64 =0 ; + p = (uint8_t *) &msg; +qemu_chr_fe_write_all (chr, p,VHOST_USER_HDR_SIZE + msg.size); +break ; + +case VHOST_USER_GET_VRING_BASE : +/* send back vring base to qemu */ + msg.flags |=VHOST_USER_REPLY_MASK ;tests/vhost-user-test.c
Quoted 10 lines
tests/vhost-user-test.c new file +10
-0+ msg.size =sizeof (m.state); + msg.state.num =0 ; + p = (uint8_t *) &msg; +qemu_chr_fe_write_all (chr, p,VHOST_USER_HDR_SIZE + msg.size); +break ; + +case VHOST_USER_SET_MEM_TABLE : +/* received the mem table */ +memcpy (&memory, &msg.memory,sizeof (msg.memory)); + fds_num = qemu_chr_fe_get_msgfds(chr, fds, sizeof(fds) /sizeof(int));
Quoted 25 lines
tests/vhost-user-test.c new file +25
-0+ +/* signal the test that it can continue */ +g_cond_signal (&data_cond); +g_mutex_unlock (&data_mutex); +break ; + +case VHOST_USER_SET_VRING_KICK : +case VHOST_USER_SET_VRING_CALL : +/* consume the fd */ +qemu_chr_fe_get_msgfds (chr, &fd,1 ); +/* +* This is a non-blocking eventfd. +* The receive function forces it to be blocking, +* so revert it back to non-blocking. +*/ +qemu_set_nonblock (fd); +break ; +default : +break ; + } +} + +static const char *init_hugepagefs (void ) +{ +const char *path;tests/vhost-user-test.c
Quoted 10 lines
tests/vhost-user-test.c new file +10
-0+struct statfs fs; +int ret; + + path =getenv ("QTEST_HUGETLBFS_PATH" ); +if (!path) { + path ="/hugetlbfs" ; + } + +if (access (path,R_OK |W_OK |X_OK )) { + g_test_message("access on path (%s): %s\n", path,strerror(errno));
tests/vhost-user-test.c
Quoted 9 lines
tests/vhost-user-test.c new file +9
-0+return 0 ; + } + +do { + ret =statfs (path, &fs); + }while (ret !=0 && errno ==EINTR ); + +if (ret !=0 ) { + g_test_message("statfs on path (%s): %s\n", path,strerror(errno));
Quoted 53 lines
tests/vhost-user-test.c new file +53
-0+return 0 ; + } + +if (fs.f_type !=HUGETLBFS_MAGIC ) { +g_test_message ("Warning: path not on HugeTLBFS: %s\n" , path); +return 0 ; + } + +return path; +} + +int main (int argc,char **argv) +{ +QTestState *s =NULL ; +CharDriverState *chr =NULL ; +const char *hugefs =0 ; +char *socket_path =0 ; +char *qemu_cmd =0 ; +char *chr_path =0 ; +int ret; + +g_test_init (&argc, &argv,NULL ); + +module_call_init (MODULE_INIT_QOM ); + + hugefs =init_hugepagefs (); +g_assert (hugefs); + + socket_path =g_strdup_printf ("/tmp/vhost-%d.sock" ,getpid ()); + +/* create char dev and add read handlers */ +qemu_add_opts (&qemu_chardev_opts); + chr_path =g_strdup_printf ("unix:%s,server,nowait" , socket_path); + chr =qemu_chr_new ("chr0" , chr_path,NULL ); +g_free (chr_path); +qemu_chr_add_handlers (chr, chr_can_read, chr_read,NULL , chr); + +/* run the main loop thread so the chardev may operate */ +g_mutex_init (&data_mutex); +g_cond_init (&data_cond); +g_mutex_lock (&data_mutex); +g_thread_new (NULL , thread_function,NULL ); + + qemu_cmd =g_strdup_printf (QEMU_CMD , hugefs, socket_path); + s =qtest_start (qemu_cmd); +g_free (qemu_cmd); + +qtest_add_func ("/vhost-user/read-guest-mem" , read_guest_mem); + + ret =g_test_run (); + +if (s) { +qtest_quit (s);
Quoted 10 lines
tests/vhost-user-test.c line 304
Quoted 5 lines
tests/vhost-user-test.c new file +5
-0+ } + +/* cleanup */ +unlink (socket_path); +g_free (socket_path);I think this is probably too late to unlink in case the test fails, should probably be moved to right after
qtest_start()?
Ack
Quoted 3 lines
Thanks for your efforts on this test!
Thanks for your review!
Quoted 15 lines
Regards,
Andreastests/vhost-user-test.c line 309
Quoted 6 lines
tests/vhost-user-test.c new file +5
-0+g_cond_clear (&data_cond); +g_mutex_clear (&data_mutex); + +return ret; +}--
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
Signature · 2 lines
Antonios Motakis Virtual Open Systems