Skip to contentSkip to conversation

ivshmem: update documentation, add client/server tools

PATCH Superseded David Marchand 7 participants 18 messages Aug 8, 2014 – Sep 9, 2014 KVM

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover ivshmem: update documentation, add client/server tools

Here is a patchset containing an update on ivshmem specs documentation and importing ivshmem server and client tools.
These tools have been written from scratch and are not related to what is available in nahanni repository.
I put them in contrib/ directory as the qemu-doc.texi was already telling the server was supposed to be there.

Changes since v2:
- fixed license issues in ivshmem client/server (I took hw/virtio/virtio-rng.c file as a reference).

Changes since v1:
- moved client/server import patch before doc update,
- tried to re-organise the ivshmem_device_spec.txt file based on Claudio comments (still not sure if the result is that great, comments welcome),
- incorporated comments from Claudio, Eric and Cam,
- added more details on the server <-> client messages exchange (but sorry, no ASCII art here).

By the way, there are still some functionnalities that need description (use of ioeventfd, the lack of irqfd support) and some parts of the ivshmem code clearly need cleanup. I will try to address this in future patches when these first patches are ok.

Signature · 28 lines
David Marchand

David Marchand (2):
  contrib: add ivshmem client and server
  docs: update ivshmem device spec

 contrib/ivshmem-client/Makefile         |   29 +++
 contrib/ivshmem-client/ivshmem-client.c |  418 ++++++++++++++++++++++++++++++
 contrib/ivshmem-client/ivshmem-client.h |  238 ++++++++++++++++++
 contrib/ivshmem-client/main.c           |  246 ++++++++++++++++++
 contrib/ivshmem-server/Makefile         |   29 +++
 contrib/ivshmem-server/ivshmem-server.c |  420 +++++++++++++++++++++++++++++++
 contrib/ivshmem-server/ivshmem-server.h |  185 ++++++++++++++
 contrib/ivshmem-server/main.c           |  296 ++++++++++++++++++++++
 docs/specs/ivshmem_device_spec.txt      |  124 ++++++---
 qemu-doc.texi                           |   10 +-
 10 files changed, 1961 insertions(+), 34 deletions(-)
 create mode 100644 contrib/ivshmem-client/Makefile
 create mode 100644 contrib/ivshmem-client/ivshmem-client.c
 create mode 100644 contrib/ivshmem-client/ivshmem-client.h
 create mode 100644 contrib/ivshmem-client/main.c
 create mode 100644 contrib/ivshmem-server/Makefile
 create mode 100644 contrib/ivshmem-server/ivshmem-server.c
 create mode 100644 contrib/ivshmem-server/ivshmem-server.h
 create mode 100644 contrib/ivshmem-server/main.c

-- 
1.7.10.4
#1 lore raw

PATCH v3 1/2 contrib: add ivshmem client and server

When using ivshmem devices, notifications between guests can be sent as interrupts using a ivshmem-server (typical use described in documentation). The client is provided as a debug tool.

---
 contrib/ivshmem-client/Makefile         |   29 +++
 contrib/ivshmem-client/ivshmem-client.c |  418 ++++++++++++++++++++++++++++++
 contrib/ivshmem-client/ivshmem-client.h |  238 ++++++++++++++++++
 contrib/ivshmem-client/main.c           |  246 ++++++++++++++++++
 contrib/ivshmem-server/Makefile         |   29 +++
 contrib/ivshmem-server/ivshmem-server.c |  420 +++++++++++++++++++++++++++++++
 contrib/ivshmem-server/ivshmem-server.h |  185 ++++++++++++++
 contrib/ivshmem-server/main.c           |  296 ++++++++++++++++++++++
 qemu-doc.texi                           |   10 +-
 9 files changed, 1868 insertions(+), 3 deletions(-)
 create mode 100644 contrib/ivshmem-client/Makefile
 create mode 100644 contrib/ivshmem-client/ivshmem-client.c
 create mode 100644 contrib/ivshmem-client/ivshmem-client.h
 create mode 100644 contrib/ivshmem-client/main.c
 create mode 100644 contrib/ivshmem-server/Makefile
 create mode 100644 contrib/ivshmem-server/ivshmem-server.c
 create mode 100644 contrib/ivshmem-server/ivshmem-server.h
 create mode 100644 contrib/ivshmem-server/main.c
contrib/ivshmem-client/Makefile new file +29 -0
new file mode 100644
index 0000000..eee97c6
@@ -0,0 +1,29 @@
+# Copyright 6WIND S.A., 2014
+#
+# This work is licensed under the terms of the GNU GPL, version 2 or
+# (at your option) any later version.  See the COPYING file in the
+# top-level directory.
+
+S ?= $(CURDIR)
+O ?= $(CURDIR)
+
+CFLAGS += -Wall -Wextra -Werror -g
+LDFLAGS +=
+LDLIBS += -lrt
+
+VPATH = $(S)
+PROG = ivshmem-client
+OBJS := $(O)/ivshmem-client.o
+OBJS += $(O)/main.o
+
+$(O)/%.o: %.c
+	$(CC) $(CFLAGS) -o $@ -c $<
+
+$(O)/$(PROG): $(OBJS)
+	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
+
+.PHONY: all
+all: $(O)/$(PROG)
+
+clean:
+	rm -f $(OBJS) $(O)/$(PROG)
contrib/ivshmem-client/ivshmem-client.c new file +418 -0

418 lines not on this page · raw message

contrib/ivshmem-client/ivshmem-client.h new file +238 -0

238 lines not on this page · raw message

contrib/ivshmem-client/main.c new file +246 -0

246 lines not on this page · raw message

contrib/ivshmem-server/Makefile new file +29 -0
new file mode 100644
index 0000000..26b4a72
@@ -0,0 +1,29 @@
+# Copyright 6WIND S.A., 2014
+#
+# This work is licensed under the terms of the GNU GPL, version 2 or
+# (at your option) any later version.  See the COPYING file in the
+# top-level directory.
+
+S ?= $(CURDIR)
+O ?= $(CURDIR)
+
+CFLAGS += -Wall -Wextra -Werror -g
+LDFLAGS +=
+LDLIBS += -lrt
+
+VPATH = $(S)
+PROG = ivshmem-server
+OBJS := $(O)/ivshmem-server.o
+OBJS += $(O)/main.o
+
+$(O)/%.o: %.c
+	$(CC) $(CFLAGS) -o $@ -c $<
+
+$(O)/$(PROG): $(OBJS)
+	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
+
+.PHONY: all
+all: $(O)/$(PROG)
+
+clean:
+	rm -f $(OBJS) $(O)/$(PROG)
contrib/ivshmem-server/ivshmem-server.c new file +420 -0

420 lines not on this page · raw message

contrib/ivshmem-server/ivshmem-server.h new file +185 -0

185 lines not on this page · raw message

contrib/ivshmem-server/main.c new file +296 -0

296 lines not on this page · raw message

qemu-doc.texi +7 -3
index 2b232ae..380d573 100644
@@ -1250,9 +1250,13 @@ is qemu.git/contrib/ivshmem-server.  An example syntax when using the shared
 memory server is:
 
 @example
-qemu-system-i386 -device ivshmem,size=<size in format accepted by -m>[,chardev=<id>]
-                 [,msi=on][,ioeventfd=on][,vectors=n][,role=peer|master]
-qemu-system-i386 -chardev socket,path=<path>,id=<id>
+# First start the ivshmem server once and for all
+ivshmem-server -p <pidfile> -S <path> -m <shm name> -l <shm size> -n <vectors n>
+
+# Then start your qemu instances with matching arguments
+qemu-system-i386 -device ivshmem,size=<shm size>,vectors=<vectors n>,chardev=<id>
+                 [,msi=on][,ioeventfd=on][,role=peer|master]
+                 -chardev socket,path=<path>,id=<id>
 @end example
 
 When using the server, the guest will be assigned a VM ID (>=0) that allows guests
Signature · 1 line
1.7.10.4
#6 lore raw

PATCH v3 2/2 docs: update ivshmem device spec

In reply to David Marchand

Add some notes on the parts needed to use ivshmem devices: more specifically, explain the purpose of an ivshmem server and the basic concept to use the ivshmem devices in guests.
Move some parts of the documentation and re-organise it.

---
 docs/specs/ivshmem_device_spec.txt |  124 +++++++++++++++++++++++++++---------
 1 file changed, 93 insertions(+), 31 deletions(-)
docs/specs/ivshmem_device_spec.txt +93 -31
index 667a862..f5f2b95 100644
@@ -2,30 +2,103 @@
 Device Specification for Inter-VM shared memory device
 ------------------------------------------------------
 
-The Inter-VM shared memory device is designed to share a region of memory to
-userspace in multiple virtual guests.  The memory region does not belong to any
-guest, but is a POSIX memory object on the host.  Optionally, the device may
-support sending interrupts to other guests sharing the same memory region.
+The Inter-VM shared memory device is designed to share a memory region (created
+on the host via the POSIX shared memory API) between multiple QEMU processes
+running different guests. In order for all guests to be able to pick up the
+shared memory area, it is modeled by QEMU as a PCI device exposing said memory
+to the guest as a PCI BAR.
+The memory region does not belong to any guest, but is a POSIX memory object on
+the host. The host can access this shared memory if needed.
+
+The device also provides an optional communication mechanism between guests
+sharing the same memory object. More details about that in the section 'Guest to
+guest communication' section.
 
 
 The Inter-VM PCI device
 -----------------------
 
-*BARs*
+From the VM point of view, the ivshmem PCI device supports three BARs.
+
+- BAR0 is a 1 Kbyte MMIO region to support registers and interrupts when MSI is
+  not used.
+- BAR1 is used for MSI-X when it is enabled in the device.
+- BAR2 is used to access the shared memory object.
+
+It is your choice how to use the device but you must choose between two
+behaviors :
+
+- basically, if you only need the shared memory part, you will map BAR2.
+  This way, you have access to the shared memory in guest and can use it as you
+  see fit (memnic, for example, uses it in userland
+  http://dpdk.org/browse/memnic).
+
+- BAR0 and BAR1 are used to implement an optional communication mechanism
+  through interrupts in the guests. If you need an event mechanism between the
+  guests accessing the shared memory, you will most likely want to write a
+  kernel driver that will handle interrupts. See details in the section 'Guest
+  to guest communication' section.
+
+The behavior is chosen when starting your QEMU processes:
+- no communication mechanism needed, the first QEMU to start creates the shared
+  memory on the host, subsequent QEMU processes will use it.
+
+- communication mechanism needed, an ivshmem server must be started before any
+  QEMU processes, then each QEMU process connects to the server unix socket.
+
+For more details on the QEMU ivshmem parameters, see qemu-doc documentation.
+
+
+Guest to guest communication
+----------------------------
+
+This section details the communication mechanism between the guests accessing
+the ivhsmem shared memory.
 
-The device supports three BARs.  BAR0 is a 1 Kbyte MMIO region to support
-registers.  BAR1 is used for MSI-X when it is enabled in the device.  BAR2 is
-used to map the shared memory object from the host.  The size of BAR2 is
-specified when the guest is started and must be a power of 2 in size.
+*ivshmem server*
 
-*Registers*
+This server code is available in qemu.git/contrib/ivshmem-server.
 
-The device currently supports 4 registers of 32-bits each.  Registers
-are used for synchronization between guests sharing the same memory object when
-interrupts are supported (this requires using the shared memory server).
+The server must be started on the host before any guest.
+It creates a shared memory object then waits for clients to connect on an unix
+socket.
 
-The server assigns each VM an ID number and sends this ID number to the QEMU
-process when the guest starts.
+For each client (QEMU processes) that connects to the server:
+- the server assigns an ID for this client and sends this ID to him as the first
+  message,
+- the server sends a fd to the shared memory object to this client,
+- the server creates a new set of host eventfds associated to the new client and
+  sends this set to all already connected clients,
+- finally, the server sends all the eventfds sets for all clients to the new
+  client.
+
+The server signals all clients when one of them disconnects.
+
+The client IDs are limited to 16 bits because of the current implementation (see
+Doorbell register in 'PCI device registers' subsection). Hence on 65536 clients
+are supported.
+
+All the file descriptors (fd to the shared memory, eventfds for each client)
+are passed to clients using SCM_RIGHTS over the server unix socket.
+
+Apart from the current ivshmem implementation in QEMU, an ivshmem client has
+been provided in qemu.git/contrib/ivshmem-client for debug.
+
+*QEMU as an ivshmem client*
+
+At initialisation, when creating the ivshmem device, QEMU gets its ID from the
+server then make it available through BAR0 IVPosition register for the VM to use
+(see 'PCI device registers' subsection).
+QEMU then uses the fd to the shared memory to map it to BAR2.
+eventfds for all other clients received from the server are stored to implement
+BAR0 Doorbell register (see 'PCI device registers' subsection).
+Finally, eventfds assigned to this QEMU process are used to send interrupts in
+this VM.
+
+*PCI device registers*
+
+From the VM point of view, the ivshmem PCI device supports 4 registers of
+32-bits each.
 
 enum ivshmem_registers {
     IntrMask = 0,
@@ -49,8 +122,8 @@ bit to 0 and unmasked by setting the first bit to 1.
 IVPosition Register: The IVPosition register is read-only and reports the
 guest's ID number.  The guest IDs are non-negative integers.  When using the
 server, since the server is a separate process, the VM ID will only be set when
-the device is ready (shared memory is received from the server and accessible via
-the device).  If the device is not ready, the IVPosition will return -1.
+the device is ready (shared memory is received from the server and accessible
+via the device).  If the device is not ready, the IVPosition will return -1.
 Applications should ensure that they have a valid VM ID before accessing the
 shared memory.
 
@@ -59,8 +132,8 @@ Doorbell register.  The doorbell register is 32-bits, logically divided into
 two 16-bit fields.  The high 16-bits are the guest ID to interrupt and the low
 16-bits are the interrupt vector to trigger.  The semantics of the value
 written to the doorbell depends on whether the device is using MSI or a regular
-pin-based interrupt.  In short, MSI uses vectors while regular interrupts set the
-status register.
+pin-based interrupt.  In short, MSI uses vectors while regular interrupts set
+the status register.
 
 Regular Interrupts
 
@@ -71,7 +144,7 @@ interrupt in the destination guest.
 
 Message Signalled Interrupts
 
-A ivshmem device may support multiple MSI vectors.  If so, the lower 16-bits
+An ivshmem device may support multiple MSI vectors.  If so, the lower 16-bits
 written to the Doorbell register must be between 0 and the maximum number of
 vectors the guest supports.  The lower 16 bits written to the doorbell is the
 MSI vector that will be raised in the destination guest.  The number of MSI
@@ -83,14 +156,3 @@ interrupt itself should be communicated via the shared memory region.  Devices
 supporting multiple MSI vectors can use different vectors to indicate different
 events have occurred.  The semantics of interrupt vectors are left to the
 user's discretion.
-
-
-Usage in the Guest
-------------------
-
-The shared memory device is intended to be used with the provided UIO driver.
-Very little configuration is needed.  The guest should map BAR0 to access the
-registers (an array of 32-bit ints allows simple writing) and map BAR2 to
-access the shared memory region itself.  The size of the shared memory region
-is specified when the guest (or shared memory server) is started.  A guest may
-map the whole shared memory region or only part of it.
Signature · 1 line
1.7.10.4
#7 lore raw

Re: [PATCH v3 2/2] docs: update ivshmem device spec

Hello David,

Quoted 6 lines · David Marchand

Add some notes on the parts needed to use ivshmem devices: more specifically, explain the purpose of an ivshmem server and the basic concept to use the ivshmem devices in guests.
Move some parts of the documentation and re-organise it.

You did not include my Reviewed-by: tag, did you change this from v2?

Ciao,

Claudio

Quoted 174 lines
---
 docs/specs/ivshmem_device_spec.txt |  124 +++++++++++++++++++++++++++---------
 1 file changed, 93 insertions(+), 31 deletions(-)
docs/specs/ivshmem_device_spec.txt +93 -31
index 667a862..f5f2b95 100644
@@ -2,30 +2,103 @@
 Device Specification for Inter-VM shared memory device
 ------------------------------------------------------
 
-The Inter-VM shared memory device is designed to share a region of memory to
-userspace in multiple virtual guests.  The memory region does not belong to any
-guest, but is a POSIX memory object on the host.  Optionally, the device may
-support sending interrupts to other guests sharing the same memory region.
+The Inter-VM shared memory device is designed to share a memory region (created
+on the host via the POSIX shared memory API) between multiple QEMU processes
+running different guests. In order for all guests to be able to pick up the
+shared memory area, it is modeled by QEMU as a PCI device exposing said memory
+to the guest as a PCI BAR.
+The memory region does not belong to any guest, but is a POSIX memory object on
+the host. The host can access this shared memory if needed.
+
+The device also provides an optional communication mechanism between guests
+sharing the same memory object. More details about that in the section 'Guest to
+guest communication' section.
 
 
 The Inter-VM PCI device
 -----------------------
 
-*BARs*
+From the VM point of view, the ivshmem PCI device supports three BARs.
+
+- BAR0 is a 1 Kbyte MMIO region to support registers and interrupts when MSI is
+  not used.
+- BAR1 is used for MSI-X when it is enabled in the device.
+- BAR2 is used to access the shared memory object.
+
+It is your choice how to use the device but you must choose between two
+behaviors :
+
+- basically, if you only need the shared memory part, you will map BAR2.
+  This way, you have access to the shared memory in guest and can use it as you
+  see fit (memnic, for example, uses it in userland
+  http://dpdk.org/browse/memnic).
+
+- BAR0 and BAR1 are used to implement an optional communication mechanism
+  through interrupts in the guests. If you need an event mechanism between the
+  guests accessing the shared memory, you will most likely want to write a
+  kernel driver that will handle interrupts. See details in the section 'Guest
+  to guest communication' section.
+
+The behavior is chosen when starting your QEMU processes:
+- no communication mechanism needed, the first QEMU to start creates the shared
+  memory on the host, subsequent QEMU processes will use it.
+
+- communication mechanism needed, an ivshmem server must be started before any
+  QEMU processes, then each QEMU process connects to the server unix socket.
+
+For more details on the QEMU ivshmem parameters, see qemu-doc documentation.
+
+
+Guest to guest communication
+----------------------------
+
+This section details the communication mechanism between the guests accessing
+the ivhsmem shared memory.
 
-The device supports three BARs.  BAR0 is a 1 Kbyte MMIO region to support
-registers.  BAR1 is used for MSI-X when it is enabled in the device.  BAR2 is
-used to map the shared memory object from the host.  The size of BAR2 is
-specified when the guest is started and must be a power of 2 in size.
+*ivshmem server*
 
-*Registers*
+This server code is available in qemu.git/contrib/ivshmem-server.
 
-The device currently supports 4 registers of 32-bits each.  Registers
-are used for synchronization between guests sharing the same memory object when
-interrupts are supported (this requires using the shared memory server).
+The server must be started on the host before any guest.
+It creates a shared memory object then waits for clients to connect on an unix
+socket.
 
-The server assigns each VM an ID number and sends this ID number to the QEMU
-process when the guest starts.
+For each client (QEMU processes) that connects to the server:
+- the server assigns an ID for this client and sends this ID to him as the first
+  message,
+- the server sends a fd to the shared memory object to this client,
+- the server creates a new set of host eventfds associated to the new client and
+  sends this set to all already connected clients,
+- finally, the server sends all the eventfds sets for all clients to the new
+  client.
+
+The server signals all clients when one of them disconnects.
+
+The client IDs are limited to 16 bits because of the current implementation (see
+Doorbell register in 'PCI device registers' subsection). Hence on 65536 clients
+are supported.
+
+All the file descriptors (fd to the shared memory, eventfds for each client)
+are passed to clients using SCM_RIGHTS over the server unix socket.
+
+Apart from the current ivshmem implementation in QEMU, an ivshmem client has
+been provided in qemu.git/contrib/ivshmem-client for debug.
+
+*QEMU as an ivshmem client*
+
+At initialisation, when creating the ivshmem device, QEMU gets its ID from the
+server then make it available through BAR0 IVPosition register for the VM to use
+(see 'PCI device registers' subsection).
+QEMU then uses the fd to the shared memory to map it to BAR2.
+eventfds for all other clients received from the server are stored to implement
+BAR0 Doorbell register (see 'PCI device registers' subsection).
+Finally, eventfds assigned to this QEMU process are used to send interrupts in
+this VM.
+
+*PCI device registers*
+
+From the VM point of view, the ivshmem PCI device supports 4 registers of
+32-bits each.
 
 enum ivshmem_registers {
     IntrMask = 0,
@@ -49,8 +122,8 @@ bit to 0 and unmasked by setting the first bit to 1.
 IVPosition Register: The IVPosition register is read-only and reports the
 guest's ID number.  The guest IDs are non-negative integers.  When using the
 server, since the server is a separate process, the VM ID will only be set when
-the device is ready (shared memory is received from the server and accessible via
-the device).  If the device is not ready, the IVPosition will return -1.
+the device is ready (shared memory is received from the server and accessible
+via the device).  If the device is not ready, the IVPosition will return -1.
 Applications should ensure that they have a valid VM ID before accessing the
 shared memory.
 
@@ -59,8 +132,8 @@ Doorbell register.  The doorbell register is 32-bits, logically divided into
 two 16-bit fields.  The high 16-bits are the guest ID to interrupt and the low
 16-bits are the interrupt vector to trigger.  The semantics of the value
 written to the doorbell depends on whether the device is using MSI or a regular
-pin-based interrupt.  In short, MSI uses vectors while regular interrupts set the
-status register.
+pin-based interrupt.  In short, MSI uses vectors while regular interrupts set
+the status register.
 
 Regular Interrupts
 
@@ -71,7 +144,7 @@ interrupt in the destination guest.
 
 Message Signalled Interrupts
 
-A ivshmem device may support multiple MSI vectors.  If so, the lower 16-bits
+An ivshmem device may support multiple MSI vectors.  If so, the lower 16-bits
 written to the Doorbell register must be between 0 and the maximum number of
 vectors the guest supports.  The lower 16 bits written to the doorbell is the
 MSI vector that will be raised in the destination guest.  The number of MSI
@@ -83,14 +156,3 @@ interrupt itself should be communicated via the shared memory region.  Devices
 supporting multiple MSI vectors can use different vectors to indicate different
 events have occurred.  The semantics of interrupt vectors are left to the
 user's discretion.
-
-
-Usage in the Guest
-------------------
-
-The shared memory device is intended to be used with the provided UIO driver.
-Very little configuration is needed.  The guest should map BAR0 to access the
-registers (an array of 32-bit ints allows simple writing) and map BAR2 to
-access the shared memory region itself.  The size of the shared memory region
-is specified when the guest (or shared memory server) is started.  A guest may
-map the whole shared memory region or only part of it.
Signature · 7 lines
Claudio Fontana
Server Virtualization Architect
Huawei Technologies Duesseldorf GmbH
Riesstraße 25 - 80992 München

office: +49 89 158834 4135
mobile: +49 15253060158
#15 lore raw
In reply to David Marchand

Hi,

Quoted 22 lines

Subject: [Qemu-devel] [PATCH v3 0/2] ivshmem: update documentation, add client/server tools

Here is a patchset containing an update on ivshmem specs documentation and importing ivshmem server and client tools.
These tools have been written from scratch and are not related to what is available in nahanni repository.
I put them in contrib/ directory as the qemu-doc.texi was already telling the server was supposed to be there.

Changes since v2:
- fixed license issues in ivshmem client/server (I took hw/virtio/virtio-rng.c file as a reference).

Changes since v1:
- moved client/server import patch before doc update,
- tried to re-organise the ivshmem_device_spec.txt file based on Claudio comments (still not sure if the result is that great, comments welcome),
- incorporated comments from Claudio, Eric and Cam,
- added more details on the server <-> client messages exchange (but sorry, no ASCII art here).

Quoted 6 lines

By the way, there are still some functionnalities that need description (use of ioeventfd, the lack of irqfd support) and some parts of the ivshmem code clearly need cleanup. I will try to address this in future patches when these first patches are ok.

If you can describe the steps of using example about your ivshmem-client and ivshmem-server will be great IMHO.

Best regards,
-Gonglei

Quoted 33 lines

--
David Marchand

David Marchand (2):
contrib: add ivshmem client and server
docs: update ivshmem device spec

 contrib/ivshmem-client/Makefile         |   29 +++
 contrib/ivshmem-client/ivshmem-client.c |  418
++++++++++++++++++++++++++++++
 contrib/ivshmem-client/ivshmem-client.h |  238 ++++++++++++++++++
 contrib/ivshmem-client/main.c           |  246 ++++++++++++++++++
 contrib/ivshmem-server/Makefile         |   29 +++
 contrib/ivshmem-server/ivshmem-server.c |  420
+++++++++++++++++++++++++++++++
 contrib/ivshmem-server/ivshmem-server.h |  185 ++++++++++++++
 contrib/ivshmem-server/main.c           |  296
++++++++++++++++++++++
 docs/specs/ivshmem_device_spec.txt      |  124 ++++++---
 qemu-doc.texi                           |   10 +-
 10 files changed, 1961 insertions(+), 34 deletions(-)
 create mode 100644 contrib/ivshmem-client/Makefile
 create mode 100644 contrib/ivshmem-client/ivshmem-client.c
 create mode 100644 contrib/ivshmem-client/ivshmem-client.h
 create mode 100644 contrib/ivshmem-client/main.c
 create mode 100644 contrib/ivshmem-server/Makefile
 create mode 100644 contrib/ivshmem-server/ivshmem-server.c
 create mode 100644 contrib/ivshmem-server/ivshmem-server.h
 create mode 100644 contrib/ivshmem-server/main.c

--
1.7.10.4

#8 lore raw

Re: [PATCH v3 2/2] docs: update ivshmem device spec

In reply to Claudio Fontana

Hello Claudio,

Quoted 10 lines · Claudio Fontana
Quoted 6 lines · David Marchand

Add some notes on the parts needed to use ivshmem devices: more specifically, explain the purpose of an ivshmem server and the basic concept to use the ivshmem devices in guests.
Move some parts of the documentation and re-organise it.

You did not include my Reviewed-by: tag, did you change this from v2?

No, I did not change anything in the documentation patch, I only touched the client/server patch.

I forgot to add your Reviewed-by tag ... (added to my tree for now, will send a v4 when I have some feedback on the client/server code).

Thanks Claudio.

Signature · 1 line
David Marchand
#16 lore raw
In reply to Gonglei (Arei)

Hello Gonglei,

Quoted 2 lines · Gonglei (Arei)

If you can describe the steps of using example about your ivshmem-client and ivshmem-server will be great IMHO.

I already have included a note in the qemu-doc.texi file on how to start the ivshmem-server. The (debug) client is started by only specifying -S /path/to/ivshmem_socket.

We made comments into the source code, so I am not sure what could be added. What do you miss ?

Signature · 1 line
David Marchand
#17 lore raw

Hi,

Quoted 9 lines

Subject: Re: [Qemu-devel] [PATCH v3 0/2] ivshmem: update documentation, add client/server tools

Hello Gonglei,

Quoted 2 lines · Gonglei (Arei)

If you can describe the steps of using example about your ivshmem-client and ivshmem-server will be great IMHO.

Quoted 7 lines

I already have included a note in the qemu-doc.texi file on how to start the ivshmem-server.
The (debug) client is started by only specifying -S /path/to/ivshmem_socket.

We made comments into the source code, so I am not sure what could be added. What do you miss ?

OK, thanks.
I will test it and review the patch sets during the next few days.

Best regards,
-Gonglei

#2 lore raw

Re: [Qemu-devel] [PATCH v3 1/2] contrib: add ivshmem client and server

In reply to David Marchand

On Fri, Aug 08, 2014 at 10:55:17AM +0200, David Marchand wrote:

Looks good, a few minor comments:

Quoted 25 lines
contrib/ivshmem-client/Makefile new file +19 -0
new file mode 100644
index 0000000..eee97c6
@@ -0,0 +1,29 @@
+# Copyright 6WIND S.A., 2014
+#
+# This work is licensed under the terms of the GNU GPL, version 2 or
+# (at your option) any later version.  See the COPYING file in the
+# top-level directory.
+
+S ?= $(CURDIR)
+O ?= $(CURDIR)
+
+CFLAGS += -Wall -Wextra -Werror -g
+LDFLAGS +=
+LDLIBS += -lrt
+
+VPATH = $(S)
+PROG = ivshmem-client
+OBJS := $(O)/ivshmem-client.o
+OBJS += $(O)/main.o
+
+$(O)/%.o: %.c

contrib/ivshmem-client/Makefile line 29

Quoted 10 lines
contrib/ivshmem-client/Makefile new file +10 -0
+	$(CC) $(CFLAGS) -o $@ -c $<
+
+$(O)/$(PROG): $(OBJS)
+	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
+
+.PHONY: all
+all: $(O)/$(PROG)
+
+clean:
+	rm -f $(OBJS) $(O)/$(PROG)

CCed Peter Maydell for a second opinion, I'd suggest hooking up to QEMU's top-level ./Makefile. QEMU does not do recursive make.

The advantages of hooking up QEMU's Makefile are:

1. So that ivshmem client/server code is built by default (on supported host platforms) and bitrot is avoided.

2. So that you don't have to duplicate rules.mak or any other build infrastructure.

Quoted 7 lines
contrib/ivshmem-client/Makefile new file +7 -0
+/**
+ * Structure storing a peer
+ *
+ * Each time a client connects to an ivshmem server, it is advertised to
+ * all connected clients through the unix socket. When our ivshmem
+ * client receives a notification, it creates a ivshmem_client_peer
+ * structure to store the infos of this peer.

contrib/ivshmem-client/Makefile

Quoted 10 lines
contrib/ivshmem-client/Makefile new file +10 -0
+ *
+ * This structure is also used to store the information of our own
+ * client in (struct ivshmem_client)->local.
+ */
+struct ivshmem_client_peer {
+    TAILQ_ENTRY(ivshmem_client_peer) next;    /**< next in list*/
+    long id;                                    /**< the id of the peer */
+    int vectors[IVSHMEM_CLIENT_MAX_VECTORS];  /**< one fd per vector */
+    unsigned vectors_count;                     /**< number of vectors */
+};

It would be nice to follow QEMU coding style:

typedef struct IvshmemClientPeer {
    ...
} IvshmemClientPeer;

(Use scripts/checkpatch.pl to check coding style)

contrib/ivshmem-client/Makefile

Quoted 7 lines
contrib/ivshmem-client/Makefile new file +7 -0
+/* browse the queue, allowing to remove/free the current element */
+#define    TAILQ_FOREACH_SAFE(var, var2, head, field)            \
+    for ((var) = TAILQ_FIRST((head)),                            \
+             (var2) = ((var) ? TAILQ_NEXT((var), field) : NULL); \
+         (var);                                                  \
+         (var) = (var2),                                         \
+             (var2) = ((var2) ? TAILQ_NEXT((var2), field) : NULL))

Please reuse include/qemu/queue.h. It's a copy of the BSD <sys/queue.h> and it has QTAILQ_FOREACH_SAFE().

contrib/ivshmem-client/Makefile

Quoted 4 lines
contrib/ivshmem-client/Makefile new file +4 -0
+    ret = sendmsg(sock_fd, &msg, 0);
+    if (ret <= 0) {
+        return -1;
+    }

This is a blocking sendmsg(2) so it could hang the server if sock_fd's sndbuf fills up. This shouldn't happen since the amount of data that gets sent in the lifetime of a session is relatively small, but there is a chance.

If hung clients should not be able to block the server then sock_fd needs to be non-blocking.

contrib/ivshmem-client/Makefile

Quoted 9 lines
contrib/ivshmem-client/Makefile new file +9 -0
+struct ivshmem_server {
+    char unix_sock_path[PATH_MAX];  /**< path to unix socket */
+    int sock_fd;                    /**< unix sock file descriptor */
+    char shm_path[PATH_MAX];        /**< path to shm */
+    size_t shm_size;                /**< size of shm */
+    int shm_fd;                     /**< shm file descriptor */
+    unsigned n_vectors;             /**< number of vectors */
+    long cur_id;                    /**< id to be given to next client */
+    int verbose;                    /**< true in verbose mode */

C99 bool is fine to use in QEMU code. It makes the code easier to read because you can be sure something is just true/false and not a bitmap or integer counter.

contrib/ivshmem-client/Makefile

Quoted 3 lines
contrib/ivshmem-client/Makefile new file +3 -0
+/* parse the size of shm */
+static int
+parse_size(const char *val_str, size_t *val)

Looks similar to QEMU's util/qemu-option.c:parse_option_size().

#9 lore raw

Re: [Qemu-devel] [PATCH v3 2/2] docs: update ivshmem device spec

In reply to David Marchand

docs/specs/ivshmem_device_spec.txt line 73

Quoted 8 lines · David Marchand
docs/specs/ivshmem_device_spec.txt +8 -0
+For each client (QEMU processes) that connects to the server:
+- the server assigns an ID for this client and sends this ID to him as the first
+  message,
+- the server sends a fd to the shared memory object to this client,
+- the server creates a new set of host eventfds associated to the new client and
+  sends this set to all already connected clients,
+- finally, the server sends all the eventfds sets for all clients to the new
+  client.

The protocol is not extensible and no version number is exchanged. For the most part this should be okay because clients must run on the same machine as the server. It is assumed clients and server are compatible with each other.

I wonder if we'll get into trouble later if the protocol needs to be extended or some operation needs to happen, like upgrading QEMU or the ivshmem-server. At the very least someone building from source but using system QEMU or ivshmem-server could get confusing failures if the protocol doesn't match.

How about sending a version message as the first thing during a connection?

Stefan

#4 lore raw

Re: [PATCH v3 1/2] contrib: add ivshmem client and server

In reply to David Marchand

Hi,

Quoted 177 lines

Subject: [Qemu-devel] [PATCH v3 1/2] contrib: add ivshmem client and server

When using ivshmem devices, notifications between guests can be sent as interrupts using a ivshmem-server (typical use described in documentation). The client is provided as a debug tool.

---
 contrib/ivshmem-client/Makefile         |   29 +++
 contrib/ivshmem-client/ivshmem-client.c |  418
++++++++++++++++++++++++++++++
 contrib/ivshmem-client/ivshmem-client.h |  238 ++++++++++++++++++
 contrib/ivshmem-client/main.c           |  246 ++++++++++++++++++
 contrib/ivshmem-server/Makefile         |   29 +++
 contrib/ivshmem-server/ivshmem-server.c |  420
+++++++++++++++++++++++++++++++
 contrib/ivshmem-server/ivshmem-server.h |  185 ++++++++++++++
 contrib/ivshmem-server/main.c           |  296
++++++++++++++++++++++
 qemu-doc.texi                           |   10 +-
 9 files changed, 1868 insertions(+), 3 deletions(-)
 create mode 100644 contrib/ivshmem-client/Makefile
 create mode 100644 contrib/ivshmem-client/ivshmem-client.c
 create mode 100644 contrib/ivshmem-client/ivshmem-client.h
 create mode 100644 contrib/ivshmem-client/main.c
 create mode 100644 contrib/ivshmem-server/Makefile
 create mode 100644 contrib/ivshmem-server/ivshmem-server.c
 create mode 100644 contrib/ivshmem-server/ivshmem-server.h
 create mode 100644 contrib/ivshmem-server/main.c
contrib/ivshmem-client/Makefile new file +29 -0
new file mode 100644
index 0000000..eee97c6
@@ -0,0 +1,29 @@
+# Copyright 6WIND S.A., 2014
+#
+# This work is licensed under the terms of the GNU GPL, version 2 or
+# (at your option) any later version.  See the COPYING file in the
+# top-level directory.
+
+S ?= $(CURDIR)
+O ?= $(CURDIR)
+
+CFLAGS += -Wall -Wextra -Werror -g
+LDFLAGS +=
+LDLIBS += -lrt
+
+VPATH = $(S)
+PROG = ivshmem-client
+OBJS := $(O)/ivshmem-client.o
+OBJS += $(O)/main.o
+
+$(O)/%.o: %.c
+	$(CC) $(CFLAGS) -o $@ -c $<
+
+$(O)/$(PROG): $(OBJS)
+	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
+
+.PHONY: all
+all: $(O)/$(PROG)
+
+clean:
+	rm -f $(OBJS) $(O)/$(PROG)
contrib/ivshmem-client/ivshmem-client.c new file +66 -0
new file mode 100644
index 0000000..2166b64
@@ -0,0 +1,418 @@
+/*
+ * Copyright 6WIND S.A., 2014
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * (at your option) any later version.  See the COPYING file in the
+ * top-level directory.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <signal.h>
+#include <unistd.h>
+#include <inttypes.h>
+#include <sys/queue.h>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include "ivshmem-client.h"
+
+/* log a message on stdout if verbose=1 */
+#define debug_log(client, fmt, ...) do { \
+        if ((client)->verbose) {         \
+            printf(fmt, ## __VA_ARGS__); \
+        }                                \
+    } while (0)
+
+/* read message from the unix socket */
+static int
+read_one_msg(struct ivshmem_client *client, long *index, int *fd)
+{
+    int ret;
+    struct msghdr msg;
+    struct iovec iov[1];
+    union {
+        struct cmsghdr cmsg;
+        char control[CMSG_SPACE(sizeof(int))];
+    } msg_control;
+    struct cmsghdr *cmsg;
+
+    iov[0].iov_base = index;
+    iov[0].iov_len = sizeof(*index);
+
+    memset(&msg, 0, sizeof(msg));
+    msg.msg_iov = iov;
+    msg.msg_iovlen = 1;
+    msg.msg_control = &msg_control;
+    msg.msg_controllen = sizeof(msg_control);
+
+    ret = recvmsg(client->sock_fd, &msg, 0);
+    if (ret < 0) {
+        debug_log(client, "cannot read message: %s\n", strerror(errno));
+        return -1;
+    }
+    if (ret == 0) {
+        debug_log(client, "lost connection to server\n");
+        return -1;
+    }
+
+    *fd = -1;
+
+    for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg =

CMSG_NXTHDR(&msg, cmsg)) {

contrib/ivshmem-client/ivshmem-client.c new file +37 -0
+
+        if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
+            cmsg->cmsg_level != SOL_SOCKET ||
+            cmsg->cmsg_type != SCM_RIGHTS) {
+            continue;
+        }
+
+        memcpy(fd, CMSG_DATA(cmsg), sizeof(*fd));
+    }
+
+    return 0;
+}
+
+/* free a peer when the server advertise a disconnection or when the
+ * client is freed */
+static void
+free_peer(struct ivshmem_client *client, struct ivshmem_client_peer *peer)
+{
+    unsigned vector;
+
+    TAILQ_REMOVE(&client->peer_list, peer, next);
+    for (vector = 0; vector < peer->vectors_count; vector++) {
+        close(peer->vectors[vector]);
+    }
+
+    free(peer);
+}
+
+/* handle message coming from server (new peer, new vectors) */
+static int
+handle_server_msg(struct ivshmem_client *client)
+{
+    struct ivshmem_client_peer *peer;
+    long peer_id;
+    int ret, fd;
+
+    ret = read_one_msg(client, &peer_id, &fd);

contrib/ivshmem-client/ivshmem-client.c line 113

Quoted 10 lines
contrib/ivshmem-client/ivshmem-client.c new file +10 -0
+    if (ret < 0) {
+        return -1;
+    }
+
+    /* can return a peer or the local client */
+    peer = ivshmem_client_search_peer(client, peer_id);
+
+    /* delete peer */
+    if (fd == -1) {
+

Maybe the above check should be moved before getting the peer. And the next check peer is extra.

contrib/ivshmem-client/ivshmem-client.c

Quoted 2 lines
contrib/ivshmem-client/ivshmem-client.c new file +2 -0
+        if (peer == NULL || peer == &client->local) {
+            debug_log(client, "receive delete for invalid peer %ld",

Missing '\n' ?

contrib/ivshmem-client/ivshmem-client.c line 126

Quoted 12 lines

peer_id);

contrib/ivshmem-client/ivshmem-client.c new file +11 -0
+            return -1;
+        }
+
+        debug_log(client, "delete peer id = %ld\n", peer_id);
+        free_peer(client, peer);
+        return 0;
+    }
+
+    /* new peer */
+    if (peer == NULL) {
+        peer = malloc(sizeof(*peer));

g_malloc0 ?.

Quoted 34 lines
contrib/ivshmem-client/ivshmem-client.c new file +23 -0
+        if (peer == NULL) {
+            debug_log(client, "cannot allocate new peer\n");
+            return -1;
+        }
+        memset(peer, 0, sizeof(*peer));
+        peer->id = peer_id;
+        peer->vectors_count = 0;
+        TAILQ_INSERT_TAIL(&client->peer_list, peer, next);
+        debug_log(client, "new peer id = %ld\n", peer_id);
+    }
+
+    /* new vector */
+    debug_log(client, "  new vector %d (fd=%d) for peer id %ld\n",
+              peer->vectors_count, fd, peer->id);
+    peer->vectors[peer->vectors_count] = fd;
+    peer->vectors_count++;
+
+    return 0;
+}
+
+/* init a new ivshmem client */
+int
+ivshmem_client_init(struct ivshmem_client *client, const char

*unix_sock_path,

contrib/ivshmem-client/ivshmem-client.c new file +10 -0
+                    ivshmem_client_notif_cb_t notif_cb, void *notif_arg,
+                    int verbose)
+{
+    unsigned i;
+
+    memset(client, 0, sizeof(*client));
+
+    snprintf(client->unix_sock_path, sizeof(client->unix_sock_path),
+             "%s", unix_sock_path);
+

contrib/ivshmem-client/ivshmem-client.c line 169

Quoted 10 lines
contrib/ivshmem-client/ivshmem-client.c new file +10 -0
+    for (i = 0; i < IVSHMEM_CLIENT_MAX_VECTORS; i++) {
+        client->local.vectors[i] = -1;
+    }
+
+    TAILQ_INIT(&client->peer_list);
+    client->local.id = -1;
+
+    client->notif_cb = notif_cb;
+    client->notif_arg = notif_arg;
+    client->verbose = verbose;

Missing client->sock_fd = -1; ?

Quoted 24 lines
contrib/ivshmem-client/ivshmem-client.c new file +22 -0
+
+    return 0;
+}
+
+/* create and connect to the unix socket */
+int
+ivshmem_client_connect(struct ivshmem_client *client)
+{
+    struct sockaddr_un sun;
+    int fd;
+    long tmp;
+
+    debug_log(client, "connect to client %s\n", client->unix_sock_path);
+
+    client->sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
+    if (client->sock_fd < 0) {
+        debug_log(client, "cannot create socket: %s\n", strerror(errno));
+        return -1;
+    }
+
+    sun.sun_family = AF_UNIX;
+    snprintf(sun.sun_path, sizeof(sun.sun_path), "%s",

client->unix_sock_path);

contrib/ivshmem-client/ivshmem-client.c new file +1 -0
+    if (connect(client->sock_fd, (struct sockaddr *)&sun, sizeof(sun)) < 0) {

contrib/ivshmem-client/ivshmem-client.c line 202

Quoted 10 lines
contrib/ivshmem-client/ivshmem-client.c new file +10 -0
+        debug_log(client, "cannot connect to %s: %s\n", sun.sun_path,
+                  strerror(errno));
+        close(client->sock_fd);
+        client->sock_fd = -1;
+        return -1;
+    }
+
+    /* first, we expect our index + a fd == -1 */
+    if (read_one_msg(client, &client->local.id, &fd) < 0 ||
+        client->local.id < 0 || fd != -1) {

Why not fd < 0 ?

Quoted 5 lines
contrib/ivshmem-client/ivshmem-client.c new file +5 -0
+        debug_log(client, "cannot read from server\n");
+        close(client->sock_fd);
+        client->sock_fd = -1;
+        return -1;
+    }

contrib/ivshmem-client/ivshmem-client.c line 217

Quoted 10 lines
contrib/ivshmem-client/ivshmem-client.c new file +10 -0
+    debug_log(client, "our_id=%ld\n", client->local.id);
+
+    /* now, we expect shared mem fd + a -1 index, note that shm fd
+     * is not used */
+    if (read_one_msg(client, &tmp, &fd) < 0 ||
+        tmp != -1 || fd < 0) {
+        debug_log(client, "cannot read from server (2)\n");
+        close(client->sock_fd);
+        client->sock_fd = -1;
+        return -1;

I think the error logic handle can move the end of this function, reducing duplicated code. Something like this:

	goto err;
  }
err:
	debug_log(client, "cannot read from server (2)\n");
    close(client->sock_fd);
    client->sock_fd = -1;
    return -1;
Quoted 106 lines
contrib/ivshmem-client/ivshmem-client.c new file +87 -0
+    }
+    debug_log(client, "shm_fd=%d\n", fd);
+
+    return 0;
+}
+
+/* close connection to the server, and free all peer structures */
+void
+ivshmem_client_close(struct ivshmem_client *client)
+{
+    struct ivshmem_client_peer *peer;
+    unsigned i;
+
+    debug_log(client, "close client\n");
+
+    while ((peer = TAILQ_FIRST(&client->peer_list)) != NULL) {
+        free_peer(client, peer);
+    }
+
+    close(client->sock_fd);
+    client->sock_fd = -1;
+    client->local.id = -1;
+    for (i = 0; i < IVSHMEM_CLIENT_MAX_VECTORS; i++) {
+        client->local.vectors[i] = -1;
+    }
+}
+
+/* get the fd_set according to the unix socket and peer list */
+void
+ivshmem_client_get_fds(const struct ivshmem_client *client, fd_set *fds,
+                       int *maxfd)
+{
+    int fd;
+    unsigned vector;
+
+    FD_SET(client->sock_fd, fds);
+    if (client->sock_fd >= *maxfd) {
+        *maxfd = client->sock_fd + 1;
+    }
+
+    for (vector = 0; vector < client->local.vectors_count; vector++) {
+        fd = client->local.vectors[vector];
+        FD_SET(fd, fds);
+        if (fd >= *maxfd) {
+            *maxfd = fd + 1;
+        }
+    }
+}
+
+/* handle events from eventfd: just print a message on notification */
+static int
+handle_event(struct ivshmem_client *client, const fd_set *cur, int maxfd)
+{
+    struct ivshmem_client_peer *peer;
+    uint64_t kick;
+    unsigned i;
+    int ret;
+
+    peer = &client->local;
+
+    for (i = 0; i < peer->vectors_count; i++) {
+        if (peer->vectors[i] >= maxfd || !FD_ISSET(peer->vectors[i], cur)) {
+            continue;
+        }
+
+        ret = read(peer->vectors[i], &kick, sizeof(kick));
+        if (ret < 0) {
+            return ret;
+        }
+        if (ret != sizeof(kick)) {
+            debug_log(client, "invalid read size = %d\n", ret);
+            errno = EINVAL;
+            return -1;
+        }
+        debug_log(client, "received event on fd %d vector %d: %ld\n",
+                  peer->vectors[i], i, kick);
+        if (client->notif_cb != NULL) {
+            client->notif_cb(client, peer, i, client->notif_arg);
+        }
+    }
+
+    return 0;
+}
+
+/* read and handle new messages on the given fd_set */
+int
+ivshmem_client_handle_fds(struct ivshmem_client *client, fd_set *fds, int

maxfd)

contrib/ivshmem-client/ivshmem-client.c new file +17 -0
+{
+    if (client->sock_fd < maxfd && FD_ISSET(client->sock_fd, fds) &&
+        handle_server_msg(client) < 0 && errno != EINTR) {
+        debug_log(client, "handle_server_msg() failed\n");
+        return -1;
+    } else if (handle_event(client, fds, maxfd) < 0 && errno != EINTR) {
+        debug_log(client, "handle_event() failed\n");
+        return -1;
+    }
+
+    return 0;
+}
+
+/* send a notification on a vector of a peer */
+int
+ivshmem_client_notify(const struct ivshmem_client *client,
+                      const struct ivshmem_client_peer *peer, unsigned

vector)

contrib/ivshmem-client/ivshmem-client.c line 326

Quoted 5 lines
contrib/ivshmem-client/ivshmem-client.c new file +5 -0
+{
+    uint64_t kick;
+    int fd;
+
+    if (vector > peer->vectors_count) {

Maybe if (vector >= peer->vectors_count) , otherwise the peer->vectors[] array bounds.

contrib/ivshmem-client/ivshmem-client.c line 330

Quoted 5 lines

+ debug_log(client, "invalid vector %u on peer %ld\n", vector,
peer->id);

contrib/ivshmem-client/ivshmem-client.c new file +3 -0
+        return -1;
+    }
+    fd = peer->vectors[vector];

Or fd = peer->vectors[vector - 1]; ?

contrib/ivshmem-client/ivshmem-client.c line 337

Quoted 8 lines

+ debug_log(client, "notify peer %ld on vector %d, fd %d\n", peer->id,
vector,

contrib/ivshmem-client/ivshmem-client.c new file +6 -0
+              fd);
+
+    kick = 1;
+    if (write(fd, &kick, sizeof(kick)) != sizeof(kick)) {
+        fprintf(stderr, "could not write to %d: %s\n", peer->vectors[vector],
+                strerror(errno));

Why not debug_log() at this here?

Quoted 10 lines
contrib/ivshmem-client/ivshmem-client.c new file +9 -0
+        return -1;
+    }
+    return 0;
+}
+
+/* send a notification to all vectors of a peer */
+int
+ivshmem_client_notify_all_vects(const struct ivshmem_client *client,
+                                const struct ivshmem_client_peer

*peer)

contrib/ivshmem-client/ivshmem-client.c line 353

Quoted 7 lines
contrib/ivshmem-client/ivshmem-client.c new file +7 -0
+{
+    unsigned vector;
+    int ret = 0;
+
+    for (vector = 0; vector < peer->vectors_count; vector++) {
+        if (ivshmem_client_notify(client, peer, vector) < 0) {
+            ret = -1;

The ret's value will be covered when multi clients failed. Do we need store the failed status for server?.

Quoted 55 lines
contrib/ivshmem-client/ivshmem-client.c new file +55 -0
+        }
+    }
+
+    return ret;
+}
+
+/* send a notification to all peers */
+int
+ivshmem_client_notify_broadcast(const struct ivshmem_client *client)
+{
+    struct ivshmem_client_peer *peer;
+    int ret = 0;
+
+    TAILQ_FOREACH(peer, &client->peer_list, next) {
+        if (ivshmem_client_notify_all_vects(client, peer) < 0) {
+            ret = -1;
+        }
+    }
+
+    return ret;
+}
+
+/* lookup peer from its id */
+struct ivshmem_client_peer *
+ivshmem_client_search_peer(struct ivshmem_client *client, long peer_id)
+{
+    struct ivshmem_client_peer *peer;
+
+    if (peer_id == client->local.id) {
+        return &client->local;
+    }
+
+    TAILQ_FOREACH(peer, &client->peer_list, next) {
+        if (peer->id == peer_id) {
+            return peer;
+        }
+    }
+    return NULL;
+}
+
+/* dump our info, the list of peers their vectors on stdout */
+void
+ivshmem_client_dump(const struct ivshmem_client *client)
+{
+    const struct ivshmem_client_peer *peer;
+    unsigned vector;
+
+    /* dump local infos */
+    peer = &client->local;
+    printf("our_id = %ld\n", peer->id);
+    for (vector = 0; vector < peer->vectors_count; vector++) {
+        printf("  vector %d is enabled (fd=%d)\n", vector,
+               peer->vectors[vector]);
+    }
+

contrib/ivshmem-client/ivshmem-client.c line 418

Quoted 10 lines
contrib/ivshmem-client/ivshmem-client.c new file +10 -0
+    /* dump peers */
+    TAILQ_FOREACH(peer, &client->peer_list, next) {
+        printf("peer_id = %ld\n", peer->id);
+
+        for (vector = 0; vector < peer->vectors_count; vector++) {
+            printf("  vector %d is enabled (fd=%d)\n", vector,
+                   peer->vectors[vector]);
+        }
+    }
+}

To be continued...

Best regards,
-Gonglei

#3 lore raw

Re: [Qemu-devel] [PATCH v3 1/2] contrib: add ivshmem client and server

In reply to Stefan Hajnoczi
Quoted 14 lines · Stefan Hajnoczi

On Fri, Aug 08, 2014 at 10:55:17AM +0200, David Marchand wrote:

Looks good, a few minor comments:

contrib/ivshmem-client/Makefile

Quoted 6 lines
contrib/ivshmem-client/Makefile new file +0 -0
new file mode 100644
index 0000000..eee97c6
@@ -0,0 +1,29 @@

CCed Peter Maydell for a second opinion, I'd suggest hooking up to QEMU's top-level ./Makefile. QEMU does not do recursive make.

Quoted 8 lines

The advantages of hooking up QEMU's Makefile are:

1. So that ivshmem client/server code is built by default (on supported host platforms) and bitrot is avoided.

2. So that you don't have to duplicate rules.mak or any other build infrastructure.

Ok, done.
But in this case, should we really keep the files in contrib/ ? I used this directory but I am not too sure about this.

Maybe hw/misc/ivshmem/ would be better ?

The rest of your comments have been integrated in my tree.

Is it preferred to send fixes aside the original patches ? or should I send a squashed version ? (I suppose the former is better as it is easier to read).

Thanks Stefan.

Signature · 1 line
David Marchand
#5 lore raw

Re: [Qemu-devel] [PATCH v3 1/2] contrib: add ivshmem client and server

In reply to Gonglei
Quoted 8 lines · Gonglei

contrib/ivshmem-client/ivshmem-client.c line 113

Quoted 6 lines
contrib/ivshmem-client/ivshmem-client.c new file +6 -0
+    /* can return a peer or the local client */
+    peer = ivshmem_client_search_peer(client, peer_id);
+
+    /* delete peer */
+    if (fd == -1) {
+

Maybe the above check should be moved before getting the peer. And the next check peer is extra.

We always need to know the peer, either for a deletion, creation or update.

Quoted 5 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+        if (peer == NULL || peer == &client->local) {
+            debug_log(client, "receive delete for invalid peer %ld",

Missing '\n' ?

Ok.

Quoted 15 lines

contrib/ivshmem-client/ivshmem-client.c line 126

Quoted 13 lines

peer_id);

contrib/ivshmem-client/ivshmem-client.c new file +11 -0
+            return -1;
+        }
+
+        debug_log(client, "delete peer id = %ld\n", peer_id);
+        free_peer(client, peer);
+        return 0;
+    }
+
+    /* new peer */
+    if (peer == NULL) {
+        peer = malloc(sizeof(*peer));

g_malloc0 ?.

Ok, replaced malloc/free with g_malloc/g_free in client and server.

Quoted 5 lines

contrib/ivshmem-client/ivshmem-client.c line 169

Quoted 3 lines
contrib/ivshmem-client/ivshmem-client.c new file +3 -0
+    client->notif_cb = notif_cb;
+    client->notif_arg = notif_arg;
+    client->verbose = verbose;

Missing client->sock_fd = -1; ?

Ok.

Quoted 5 lines

contrib/ivshmem-client/ivshmem-client.c line 202

Quoted 4 lines
contrib/ivshmem-client/ivshmem-client.c new file +4 -0
+
+    /* first, we expect our index + a fd == -1 */
+    if (read_one_msg(client, &client->local.id, &fd) < 0 ||
+        client->local.id < 0 || fd != -1) {

Why not fd < 0 ?

Because the server will send us an id and a fd == -1 see ivshmem-server.c send_initial_info().

Quoted 19 lines
Quoted 6 lines
contrib/ivshmem-client/ivshmem-client.c new file +5 -0
+        debug_log(client, "cannot read from server\n");
+        close(client->sock_fd);
+        client->sock_fd = -1;
+        return -1;
+    }

contrib/ivshmem-client/ivshmem-client.c line 217

Quoted 10 lines
contrib/ivshmem-client/ivshmem-client.c new file +10 -0
+    debug_log(client, "our_id=%ld\n", client->local.id);
+
+    /* now, we expect shared mem fd + a -1 index, note that shm fd
+     * is not used */
+    if (read_one_msg(client, &tmp, &fd) < 0 ||
+        tmp != -1 || fd < 0) {
+        debug_log(client, "cannot read from server (2)\n");
+        close(client->sock_fd);
+        client->sock_fd = -1;
+        return -1;

I think the error logic handle can move the end of this function, reducing duplicated code. Something like this:

Quoted 7 lines
	goto err;
   }
err:
	debug_log(client, "cannot read from server (2)\n");
     close(client->sock_fd);
     client->sock_fd = -1;
     return -1;

Ok, I also updated the server.

Quoted 6 lines

contrib/ivshmem-client/ivshmem-client.c line 326

Quoted 3 lines
contrib/ivshmem-client/ivshmem-client.c new file +3 -0
+    int fd;
+
+    if (vector > peer->vectors_count) {

Maybe if (vector >= peer->vectors_count) , otherwise the peer->vectors[] array bounds.

Oh yes, good catch.
It should not happen, at the moment, but it is wrong, indeed.

Quoted 6 lines
Quoted 6 lines
contrib/ivshmem-client/ivshmem-client.c new file +4 -0
+/* send a notification to all vectors of a peer */
+int
+ivshmem_client_notify_all_vects(const struct ivshmem_client *client,
+                                const struct ivshmem_client_peer

*peer)
+{

Quoted 8 lines

contrib/ivshmem-client/ivshmem-client.c line 353

Quoted 6 lines
contrib/ivshmem-client/ivshmem-client.c new file +6 -0
+    unsigned vector;
+    int ret = 0;
+
+    for (vector = 0; vector < peer->vectors_count; vector++) {
+        if (ivshmem_client_notify(client, peer, vector) < 0) {
+            ret = -1;

The ret's value will be covered when multi clients failed. Do we need store the failed status for server?.

It indicates that we could not notify *all* clients.

Thanks Gonglei.

Signature · 1 line
David Marchand
#10 lore raw

Re: [Qemu-devel] [PATCH v3 2/2] docs: update ivshmem device spec

In reply to Stefan Hajnoczi

Hello Stefan,

Quoted 15 lines · Stefan Hajnoczi

docs/specs/ivshmem_device_spec.txt line 73

Quoted 8 lines · David Marchand
docs/specs/ivshmem_device_spec.txt +8 -0
+For each client (QEMU processes) that connects to the server:
+- the server assigns an ID for this client and sends this ID to him as the first
+  message,
+- the server sends a fd to the shared memory object to this client,
+- the server creates a new set of host eventfds associated to the new client and
+  sends this set to all already connected clients,
+- finally, the server sends all the eventfds sets for all clients to the new
+  client.

The protocol is not extensible and no version number is exchanged. For the most part this should be okay because clients must run on the same machine as the server. It is assumed clients and server are compatible with each other.

Quoted 8 lines

I wonder if we'll get into trouble later if the protocol needs to be extended or some operation needs to happen, like upgrading QEMU or the ivshmem-server. At the very least someone building from source but using system QEMU or ivshmem-server could get confusing failures if the protocol doesn't match.

How about sending a version message as the first thing during a connection?

I am not too sure about this.

This would break current base version.

Using a version message supposes we want to keep ivshmem-server and QEMU separated (for example, in two distribution packages) while we can avoid this, so why would we do so ?

If we want the ivshmem-server to come with QEMU, then both are supposed to be aligned on your system.
If you want to test local modifications, then it means you know what you are doing and you will call the right ivshmem-server binary with the right QEMU binary.

Thanks.

Signature · 1 line
David Marchand
maintainer
#11 lore raw

Re: [Qemu-devel] [PATCH v3 2/2] docs: update ivshmem device spec

Il 26/08/2014 08:47, David Marchand ha scritto:

Quoted 7 lines

Using a version message supposes we want to keep ivshmem-server and QEMU separated (for example, in two distribution packages) while we can avoid this, so why would we do so ?

If we want the ivshmem-server to come with QEMU, then both are supposed to be aligned on your system.

What about upgrading QEMU and ivshmem-server while you have existing guests? You cannot restart ivshmem-server, and the new QEMU would have to talk to the old ivshmem-server.

Paolo

Quoted 3 lines

If you want to test local modifications, then it means you know what you are doing and you will call the right ivshmem-server binary with the right QEMU binary.

#12 lore raw

Re: [Qemu-devel] [PATCH v3 2/2] docs: update ivshmem device spec

Quoted 6 lines · Paolo Bonzini

Il 26/08/2014 08:47, David Marchand ha scritto:

Quoted 5 lines

Using a version message supposes we want to keep ivshmem-server and QEMU separated (for example, in two distribution packages) while we can avoid this, so why would we do so ?

Quoted 6 lines
Quoted 2 lines

If we want the ivshmem-server to come with QEMU, then both are supposed to be aligned on your system.

What about upgrading QEMU and ivshmem-server while you have existing guests? You cannot restart ivshmem-server, and the new QEMU would have to talk to the old ivshmem-server.

Version negotiation also helps avoid confusion if someone combines ivshmem-server and QEMU from different origins (e.g. built from source and distro packaged).

It's a safeguard to prevent hard-to-diagnose failures when the system is misconfigured.

Stefan

#13 lore raw

Re: [Qemu-devel] [PATCH v3 2/2] docs: update ivshmem device spec

Quoted 14 lines · Stefan Hajnoczi
Quoted 6 lines · Paolo Bonzini

Il 26/08/2014 08:47, David Marchand ha scritto:

Quoted 5 lines

Using a version message supposes we want to keep ivshmem-server and QEMU separated (for example, in two distribution packages) while we can avoid this, so why would we do so ?

Quoted 6 lines
Quoted 2 lines

If we want the ivshmem-server to come with QEMU, then both are supposed to be aligned on your system.

What about upgrading QEMU and ivshmem-server while you have existing guests? You cannot restart ivshmem-server, and the new QEMU would have to talk to the old ivshmem-server.

Quoted 7 lines

Version negotiation also helps avoid confusion if someone combines ivshmem-server and QEMU from different origins (e.g. built from source and distro packaged).

It's a safeguard to prevent hard-to-diagnose failures when the system is misconfigured.

Hum, so you want the code to be defensive against mis-use, why not.

I wanted to keep modifications on ivshmem as little as possible in a first phase (all the more so as there are potential ivshmem users out there that I think will be impacted by a protocol change).

Sending the version as the first "vm_id" with an associated fd to -1 before sending the real client id should work with existing QEMU client code (hw/misc/ivshmem.c).

Do you have a better idea ?
Is there a best practice in QEMU for "version negotiation" that could work with ivshmem protocol ?

I have a v4 ready with this (and all the pending comments), I will send it later unless a better idea is exposed.

Thanks.

Signature · 1 line
David Marchand
#14 lore raw

Re: [PATCH v3 2/2] docs: update ivshmem device spec

Quoted 7 lines · David Marchand
Quoted 7 lines
Quoted 3 lines

What about upgrading QEMU and ivshmem-server while you have existing guests? You cannot restart ivshmem-server, and the new QEMU would have to talk to the old ivshmem-server.

Version negotiation also helps avoid confusion if someone combines ivshmem-server and QEMU from different origins (e.g. built from source and distro packaged).

Don't underestimate the likelihood of this happening. Any long-running process (which an ivshmem-server will be) continues running at the old version, even when a package upgrade installs a new qemu binary; the new binary should still be able to manage connections to the already-running server.

Even neater would be a solution where an existing ivshmem-server could re-exec an updated ivshmem-server binary that resulted from a distro upgrade, hand over all state required for the new server to take over from the point managed by the old server, so that you aren't stuck running the old binary forever. But that's a lot trickier to write, so it is not necessary for a first implementation; and if you do that, then you have the reverse situation to worry about (the new server must still accept communication from existing old qemu binaries).

Note that the goal here is to support upgrades; it is probably okay if downgrading from a new binary back to an old doesn't work correctly (because the new software was using a feature not present in the old).

Quoted 10 lines
Quoted 4 lines

It's a safeguard to prevent hard-to-diagnose failures when the system is misconfigured.

Hum, so you want the code to be defensive against mis-use, why not.

I wanted to keep modifications on ivshmem as little as possible in a first phase (all the more so as there are potential ivshmem users out there that I think will be impacted by a protocol change).

Existing ivshmem users MUST be aware that they are using something that is not yet polished, and be prepared to make the upgrade to the polished version. It's best to minimize the hassle by making them upgrade exactly once to a fully-robust version, rather than to have them upgrade to a slightly-more robust version only to find out we didn't plan ahead well enough to make further extensions in a back-compatible manner.

Quoted 8 lines

Sending the version as the first "vm_id" with an associated fd to -1 before sending the real client id should work with existing QEMU client code (hw/misc/ivshmem.c).

Do you have a better idea ?
Is there a best practice in QEMU for "version negotiation" that could work with ivshmem protocol ?

QMP starts off with a mandatory "qmp_capabilities" handshake, although we haven't yet had to define any capabilities where cross-versioned communication differs as a result. Migration is somewhat of an example, except that it is one-directional (we don't have a feedback path), so it is somewhat best effort. The qcow2 v3 file format is an example of declaring features, rather than version numbers, and making decisions about whether a feature is compatible (older clients can safely ignore the bit, without corrupting the image but possibly having worse performance) vs. incompatible (older clients must reject the image, because not handling the feature correctly would corrupt the image). The best handshakes are bi-directional - both sides advertise their version (or better, their features), and a well-defined algorithm for settling on the common subset of advertised features then ensures that the two sides know how to talk to each other, or give a reason for either side to disconnect early because of a missing feature.

Quoted 7 lines

I have a v4 ready with this (and all the pending comments), I will send it later unless a better idea is exposed.

Thanks.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org