Skip to contentSkip to conversation

Add virtio transport for AF_VSOCK

RFC Superseded Stefan Hajnoczi 4 participants 19 messages Apr 1, 2016 – Apr 13, 2016

Subsystems (8)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Stefan Hajnoczi

author maintainer
#0 lore raw

v5 cover Add virtio transport for AF_VSOCK

This series is based on Michael Tsirkin's vhost branch (v4.5-rc6).

I'm about to process Claudio Imbrenda's locking fixes for virtio-vsock but first I want to share the latest version of the code. Several people are playing with vsock now so sharing the latest code should avoid duplicate work.

v5:
* Transport reset event for live migration support
* Reorder virtqueues, drop unused ctrl virtqueue
* Switch to a free virtio device ID
* More small changes, see patches for individual items

v4:
* Addressed code review comments from Alex Bennee
* MAINTAINERS file entries for new files
* Trace events instead of pr_debug()
* RST packet is sent when there is no listen socket
* Allow guest->host connections again (began discussing netfilter support with Matt Benjamin instead of hard-coding security policy in virtio-vsock code)
* Many checkpatch.pl cleanups (will be 100% clean in v5)

v3:
* Remove unnecessary 3-way handshake, just do REQUEST/RESPONSE instead of REQUEST/RESPONSE/ACK
* Remove SOCK_DGRAM support and focus on SOCK_STREAM first (also drop v2 Patch 1, it's only needed for SOCK_DGRAM)
* Only allow host->guest connections (same security model as latest VMware)
* Don't put vhost vsock driver into staging
* Add missing Kconfig dependencies (Arnd Bergmann <[email protected]>)
* Remove unneeded variable used to store return value (Fengguang Wu <[email protected]> and Julia Lawall <[email protected]>)

v2:
* Rebased onto Linux v4.4-rc2
* vhost: Refuse to assign reserved CIDs
* vhost: Refuse guest CID if already in use
* vhost: Only accept correctly addressed packets (no spoofing!)
* vhost: Support flexible rx/tx descriptor layout
* vhost: Add missing total_tx_buf decrement
* virtio_transport: Fix total_tx_buf accounting
* virtio_transport: Add virtio_transport global mutex to prevent races
* common: Notify other side of SOCK_STREAM disconnect (fixes shutdown semantics)
* common: Avoid recursive mutex_lock(tx_lock) for write_space (fixes deadlock)
* common: Define VIRTIO_VSOCK_TYPE_STREAM/DGRAM hardware interface constants
* common: Define VIRTIO_VSOCK_SHUTDOWN_RCV/SEND hardware interface constants
* common: Fix peer_buf_alloc inheritance on child socket

This patch series adds a virtio transport for AF_VSOCK (net/vmw_vsock/). AF_VSOCK is designed for communication between virtual machines and hypervisors. It is currently only implemented for VMware's VMCI transport.

Most of the work was done by Asias He and Gerd Hoffmann a while back. I have picked up the series again.

The QEMU userspace changes are here:
https://github.com/stefanha/qemu/commits/vsock

Why virtio-vsock?
-----------------
Guest<->host communication is currently done over the virtio-serial device. This makes it hard to port sockets API-based applications and is limited to static ports.

virtio-vsock uses the sockets API so that applications can rely on familiar SOCK_STREAM semantics. Applications on the host can easily connect to guest agents because the sockets API allows multiple connections to a listen socket (unlike virtio-serial). This simplifies the guest<->host communication and eliminates the need for extra processes on the host to arbitrate virtio-serial ports.

Overview
--------
This series adds 3 pieces:

1. virtio_transport_common.ko - core virtio vsock code that uses vsock.ko

2. virtio_transport.ko - guest driver

3. drivers/vhost/vsock.ko - host driver

Howto
-----
The following kernel options are needed:
CONFIG_VSOCKETS=y
CONFIG_VIRTIO_VSOCKETS=y
CONFIG_VIRTIO_VSOCKETS_COMMON=y
CONFIG_VHOST_VSOCK=m

Launch QEMU as follows:

  # qemu ... -device vhost-vsock-pci,id=vhost-vsock-pci0,guest-cid=3

Guest and host can communicate via AF_VSOCK sockets. The host's CID (address) is 2 and the guest must be assigned a CID (3 in the example above).

Asias He (4):
VSOCK: Introduce virtio_vsock_common.ko
VSOCK: Introduce virtio_transport.ko
VSOCK: Introduce vhost_vsock.ko
VSOCK: Add Makefile and Kconfig

Stefan Hajnoczi (1):
VSOCK: transport-specific vsock_transport functions

 MAINTAINERS                                        |  13 +
 drivers/vhost/Kconfig                              |  15 +
 drivers/vhost/Makefile                             |   4 +
 drivers/vhost/vsock.c                              | 694 +++++++++++++++++
 drivers/vhost/vsock.h                              |   5 +
 include/linux/virtio_vsock.h                       | 167 ++++
 include/net/af_vsock.h                             |   3 +
 .../trace/events/vsock_virtio_transport_common.h   | 144 ++++
 include/uapi/linux/virtio_ids.h                    |   1 +
 include/uapi/linux/virtio_vsock.h                  |  94 +++
 net/vmw_vsock/Kconfig                              |  19 +
 net/vmw_vsock/Makefile                             |   2 +
 net/vmw_vsock/af_vsock.c                           |   9 +
 net/vmw_vsock/virtio_transport.c                   | 584 ++++++++++++++
 net/vmw_vsock/virtio_transport_common.c            | 838 +++++++++++++++++++++
 15 files changed, 2592 insertions(+)
 create mode 100644 drivers/vhost/vsock.c
 create mode 100644 drivers/vhost/vsock.h
 create mode 100644 include/linux/virtio_vsock.h
 create mode 100644 include/trace/events/vsock_virtio_transport_common.h
 create mode 100644 include/uapi/linux/virtio_vsock.h
 create mode 100644 net/vmw_vsock/virtio_transport.c
 create mode 100644 net/vmw_vsock/virtio_transport_common.c
Signature · 1 line
2.5.5
author maintainer
#1 lore raw

RFC v5 1/5 VSOCK: transport-specific vsock_transport functions

struct vsock_transport contains function pointers called by AF_VSOCK core code. The transport may want its own transport-specific function pointers and they can be added after struct vsock_transport.

Allow the transport to fetch vsock_transport. It can downcast it to access transport-specific function pointers.

The virtio transport will use this.

---
 include/net/af_vsock.h   | 3 +++
 net/vmw_vsock/af_vsock.c | 9 +++++++++
 2 files changed, 12 insertions(+)
include/net/af_vsock.h +3 -0
index e9eb2d6..23f5525 100644
@@ -165,6 +165,9 @@ static inline int vsock_core_init(const struct vsock_transport *t)
 }
 void vsock_core_exit(void);
 
+/* The transport may downcast this to access transport-specific functions */
+const struct vsock_transport *vsock_core_get_transport(void);
+
 /**** UTILS ****/
 
 void vsock_release_pending(struct sock *pending);
net/vmw_vsock/af_vsock.c +9 -0
index bbe65dc..1e5f5ed 100644
@@ -1987,6 +1987,15 @@ void vsock_core_exit(void)
 }
 EXPORT_SYMBOL_GPL(vsock_core_exit);
 
+const struct vsock_transport *vsock_core_get_transport(void)
+{
+	/* vsock_register_mutex not taken since only the transport uses this
+	 * function and only while registered.
+	 */
+	return transport;
+}
+EXPORT_SYMBOL_GPL(vsock_core_get_transport);
+
 MODULE_AUTHOR("VMware, Inc.");
 MODULE_DESCRIPTION("VMware Virtual Socket Family");
 MODULE_VERSION("1.0.1.0-k");
Signature · 1 line
2.5.5
author maintainer
#2 lore raw

RFC v5 1/5 VSOCK: transport-specific vsock_transport functions

In reply to Stefan Hajnoczi

struct vsock_transport contains function pointers called by AF_VSOCK core code. The transport may want its own transport-specific function pointers and they can be added after struct vsock_transport.

Allow the transport to fetch vsock_transport. It can downcast it to access transport-specific function pointers.

The virtio transport will use this.

---
 include/net/af_vsock.h   | 3 +++
 net/vmw_vsock/af_vsock.c | 9 +++++++++
 2 files changed, 12 insertions(+)
include/net/af_vsock.h +3 -0
index e9eb2d6..23f5525 100644
@@ -165,6 +165,9 @@ static inline int vsock_core_init(const struct vsock_transport *t)
 }
 void vsock_core_exit(void);
 
+/* The transport may downcast this to access transport-specific functions */
+const struct vsock_transport *vsock_core_get_transport(void);
+
 /**** UTILS ****/
 
 void vsock_release_pending(struct sock *pending);
net/vmw_vsock/af_vsock.c +9 -0
index bbe65dc..1e5f5ed 100644
@@ -1987,6 +1987,15 @@ void vsock_core_exit(void)
 }
 EXPORT_SYMBOL_GPL(vsock_core_exit);
 
+const struct vsock_transport *vsock_core_get_transport(void)
+{
+	/* vsock_register_mutex not taken since only the transport uses this
+	 * function and only while registered.
+	 */
+	return transport;
+}
+EXPORT_SYMBOL_GPL(vsock_core_get_transport);
+
 MODULE_AUTHOR("VMware, Inc.");
 MODULE_DESCRIPTION("VMware Virtual Socket Family");
 MODULE_VERSION("1.0.1.0-k");
Signature · 1 line
2.5.5
author maintainer
#3 lore raw

RFC v5 2/5 VSOCK: Introduce virtio_vsock_common.ko

In reply to Stefan Hajnoczi

From: Asias He <[email protected]>

This module contains the common code and header files for the following virtio_transporto and vhost_vsock kernel modules.

---
v5:
* Add event virtqueue, struct virtio_vsock_event, and transport reset event
* Reorder virtqueue indices: rx, tx, event
* Drop unused virtqueue_pairs config field
* Drop unused ctrl virtqueue
* Switch to a free virtio device ID, the previous one was reserved
v4:
* Add MAINTAINERS file entry
* checkpatch.pl cleanups
* linux_vsock.h: drop wrong copy-pasted license header
* Move tx sock refcounting to virtio_transport_alloc/free_pkt() to fix leaks in error paths
* Add send_pkt_no_sock() to send RST packets with no listen socket
* Rename per-socket state from virtio_transport to virtio_vsock_sock
* Move send_pkt_ops to new virtio_transport struct
* Drop dumppkt function, packet capture will be added in the future
* Drop empty virtio_transport_dec_tx_pkt()
* Allow guest->host connections again
* Use trace events instead of pr_debug()
v3:
* Remove unnecessary 3-way handshake, just do REQUEST/RESPONSE instead of REQUEST/RESPONSE/ACK
* Remove SOCK_DGRAM support and focus on SOCK_STREAM first
* Only allow host->guest connections (same security model as latest VMware)
v2:
* Fix peer_buf_alloc inheritance on child socket
* Notify other side of SOCK_STREAM disconnect (fixes shutdown semantics)
* Avoid recursive mutex_lock(tx_lock) for write_space (fixes deadlock)
* Define VIRTIO_VSOCK_TYPE_STREAM/DGRAM hardware interface constants
* Define VIRTIO_VSOCK_SHUTDOWN_RCV/SEND hardware interface constants

---
 MAINTAINERS                                        |  10 +
 include/linux/virtio_vsock.h                       | 167 ++++
 .../trace/events/vsock_virtio_transport_common.h   | 144 ++++
 include/uapi/linux/virtio_ids.h                    |   1 +
 include/uapi/linux/virtio_vsock.h                  |  94 +++
 net/vmw_vsock/virtio_transport_common.c            | 838 +++++++++++++++++++++
 6 files changed, 1254 insertions(+)
 create mode 100644 include/linux/virtio_vsock.h
 create mode 100644 include/trace/events/vsock_virtio_transport_common.h
 create mode 100644 include/uapi/linux/virtio_vsock.h
 create mode 100644 net/vmw_vsock/virtio_transport_common.c
MAINTAINERS +10 -0
index da3e4d8..fb9c47a 100644
@@ -11602,6 +11602,16 @@ S:	Maintained
 F:	drivers/media/v4l2-core/videobuf2-*
 F:	include/media/videobuf2-*
 
+VIRTIO AND VHOST VSOCK DRIVER
+M:	Stefan Hajnoczi <[email protected]>
+L:	[email protected]
+L:	[email protected]
+L:	[email protected]
+S:	Maintained
+F:	include/linux/virtio_vsock.h
+F:	include/uapi/linux/virtio_vsock.h
+F:	net/vmw_vsock/virtio_transport_common.c
+
 VIRTUAL SERIO DEVICE DRIVER
 M:	Stephen Chandler Paul <[email protected]>
 S:	Maintained
include/linux/virtio_vsock.h new file +167 -0

167 lines not on this page · raw message

include/trace/events/vsock_virtio_transport_common.h new file +144 -0

144 lines not on this page · raw message

include/uapi/linux/virtio_ids.h +1 -0
index 77925f5..3228d58 100644
@@ -41,5 +41,6 @@
 #define VIRTIO_ID_CAIF	       12 /* Virtio caif */
 #define VIRTIO_ID_GPU          16 /* virtio GPU */
 #define VIRTIO_ID_INPUT        18 /* virtio input */
+#define VIRTIO_ID_VSOCK        19 /* virtio vsock transport */
 
 #endif /* _LINUX_VIRTIO_IDS_H */
include/uapi/linux/virtio_vsock.h new file +94 -0

94 lines not on this page · raw message

net/vmw_vsock/virtio_transport_common.c new file +838 -0

838 lines not on this page · raw message

Signature · 1 line
2.5.5
author maintainer
#4 lore raw

RFC v5 3/5 VSOCK: Introduce virtio_transport.ko

In reply to Stefan Hajnoczi

From: Asias He <[email protected]>

VM sockets virtio transport implementation. This driver runs in the guest.

---
v5:
* Add transport reset event handling
* Drop ctrl virtqueue
v4:
* Add MAINTAINERS file entry
* Drop short/long rx packets
* checkpatch.pl cleanups
* Clarify locking in struct virtio_vsock
* Narrow local variable scopes as suggested by Alex Bennee
* Call wake_up() after decrementing total_tx_buf to avoid deadlock
v2:
* Fix total_tx_buf accounting
* Add virtio_transport global mutex to prevent races

---
 MAINTAINERS                      |   1 +
 net/vmw_vsock/virtio_transport.c | 584 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 585 insertions(+)
 create mode 100644 net/vmw_vsock/virtio_transport.c
MAINTAINERS +1 -0
index fb9c47a..1ed7364 100644
@@ -11611,6 +11611,7 @@ S:	Maintained
 F:	include/linux/virtio_vsock.h
 F:	include/uapi/linux/virtio_vsock.h
 F:	net/vmw_vsock/virtio_transport_common.c
+F:	net/vmw_vsock/virtio_transport.c
 
 VIRTUAL SERIO DEVICE DRIVER
 M:	Stephen Chandler Paul <[email protected]>
net/vmw_vsock/virtio_transport.c new file +584 -0

584 lines not on this page · raw message

Signature · 1 line
2.5.5
author maintainer
#5 lore raw

RFC v5 3/5 VSOCK: Introduce virtio_transport.ko

In reply to Stefan Hajnoczi

From: Asias He <[email protected]>

VM sockets virtio transport implementation. This driver runs in the guest.

---
v5:
* Add transport reset event handling
* Drop ctrl virtqueue
v4:
* Add MAINTAINERS file entry
* Drop short/long rx packets
* checkpatch.pl cleanups
* Clarify locking in struct virtio_vsock
* Narrow local variable scopes as suggested by Alex Bennee
* Call wake_up() after decrementing total_tx_buf to avoid deadlock
v2:
* Fix total_tx_buf accounting
* Add virtio_transport global mutex to prevent races

---
 MAINTAINERS                      |   1 +
 net/vmw_vsock/virtio_transport.c | 584 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 585 insertions(+)
 create mode 100644 net/vmw_vsock/virtio_transport.c
MAINTAINERS +1 -0
index fb9c47a..1ed7364 100644
@@ -11611,6 +11611,7 @@ S:	Maintained
 F:	include/linux/virtio_vsock.h
 F:	include/uapi/linux/virtio_vsock.h
 F:	net/vmw_vsock/virtio_transport_common.c
+F:	net/vmw_vsock/virtio_transport.c
 
 VIRTUAL SERIO DEVICE DRIVER
 M:	Stephen Chandler Paul <[email protected]>
net/vmw_vsock/virtio_transport.c new file +584 -0

584 lines not on this page · raw message

Signature · 1 line
2.5.5
author maintainer
#6 lore raw

RFC v5 4/5 VSOCK: Introduce vhost_vsock.ko

In reply to Stefan Hajnoczi

From: Asias He <[email protected]>

VM sockets vhost transport implementation. This driver runs on the host.

---
v5:
* Only take rx/tx virtqueues, userspace handles the other virtqueues
* Explicitly skip instances without a CID when transferring packets
* Add VHOST_VSOCK_START ioctl to being vhost virtqueue processing
* Reset established connections when device is closed
v4:
* Add MAINTAINERS file entry
* virtqueue used len is now sizeof(pkt->hdr) + pkt->len instead of just pkt->len
* checkpatch.pl cleanups
* Clarify struct vhost_vsock locking
* Add comments about optimization that disables virtqueue notify
* Drop unused vhost_vsock_handle_ctl_kick()
* Call wake_up() after decrementing total_tx_buf to prevent deadlock
v3:
* Remove unneeded variable used to store return value (Fengguang Wu <[email protected]> and Julia Lawall <[email protected]>)
v2:
* Add missing total_tx_buf decrement
* Support flexible rx/tx descriptor layout
* Refuse to assign reserved CIDs
* Refuse guest CID if already in use
* Only accept correctly addressed packets

---
 MAINTAINERS           |   2 +
 drivers/vhost/vsock.c | 694 ++++++++++++++++++++++++++++++++++++++++++++++++++
 drivers/vhost/vsock.h |   5 +
 3 files changed, 701 insertions(+)
 create mode 100644 drivers/vhost/vsock.c
 create mode 100644 drivers/vhost/vsock.h
MAINTAINERS +2 -0
index 1ed7364..7899c3c 100644
@@ -11612,6 +11612,8 @@ F:	include/linux/virtio_vsock.h
 F:	include/uapi/linux/virtio_vsock.h
 F:	net/vmw_vsock/virtio_transport_common.c
 F:	net/vmw_vsock/virtio_transport.c
+F:	drivers/vhost/vsock.c
+F:	drivers/vhost/vsock.h
 
 VIRTUAL SERIO DEVICE DRIVER
 M:	Stephen Chandler Paul <[email protected]>
drivers/vhost/vsock.c new file +694 -0

694 lines not on this page · raw message

drivers/vhost/vsock.h new file +5 -0
new file mode 100644
index 0000000..173f9fc
@@ -0,0 +1,5 @@
+#ifndef VHOST_VSOCK_H
+#define VHOST_VSOCK_H
+#define VHOST_VSOCK_SET_GUEST_CID	_IOW(VHOST_VIRTIO, 0x60, __u32)
+#define VHOST_VSOCK_START		_IO(VHOST_VIRTIO, 0x61)
+#endif
Signature · 1 line
2.5.5
author maintainer
#7 lore raw

RFC v5 4/5 VSOCK: Introduce vhost_vsock.ko

In reply to Stefan Hajnoczi

From: Asias He <[email protected]>

VM sockets vhost transport implementation. This driver runs on the host.

---
v5:
* Only take rx/tx virtqueues, userspace handles the other virtqueues
* Explicitly skip instances without a CID when transferring packets
* Add VHOST_VSOCK_START ioctl to being vhost virtqueue processing
* Reset established connections when device is closed
v4:
* Add MAINTAINERS file entry
* virtqueue used len is now sizeof(pkt->hdr) + pkt->len instead of just pkt->len
* checkpatch.pl cleanups
* Clarify struct vhost_vsock locking
* Add comments about optimization that disables virtqueue notify
* Drop unused vhost_vsock_handle_ctl_kick()
* Call wake_up() after decrementing total_tx_buf to prevent deadlock
v3:
* Remove unneeded variable used to store return value (Fengguang Wu <[email protected]> and Julia Lawall <[email protected]>)
v2:
* Add missing total_tx_buf decrement
* Support flexible rx/tx descriptor layout
* Refuse to assign reserved CIDs
* Refuse guest CID if already in use
* Only accept correctly addressed packets

---
 MAINTAINERS           |   2 +
 drivers/vhost/vsock.c | 694 ++++++++++++++++++++++++++++++++++++++++++++++++++
 drivers/vhost/vsock.h |   5 +
 3 files changed, 701 insertions(+)
 create mode 100644 drivers/vhost/vsock.c
 create mode 100644 drivers/vhost/vsock.h
MAINTAINERS +2 -0
index 1ed7364..7899c3c 100644
@@ -11612,6 +11612,8 @@ F:	include/linux/virtio_vsock.h
 F:	include/uapi/linux/virtio_vsock.h
 F:	net/vmw_vsock/virtio_transport_common.c
 F:	net/vmw_vsock/virtio_transport.c
+F:	drivers/vhost/vsock.c
+F:	drivers/vhost/vsock.h
 
 VIRTUAL SERIO DEVICE DRIVER
 M:	Stephen Chandler Paul <[email protected]>
drivers/vhost/vsock.c new file +694 -0

694 lines not on this page · raw message

drivers/vhost/vsock.h new file +5 -0
new file mode 100644
index 0000000..173f9fc
@@ -0,0 +1,5 @@
+#ifndef VHOST_VSOCK_H
+#define VHOST_VSOCK_H
+#define VHOST_VSOCK_SET_GUEST_CID	_IOW(VHOST_VIRTIO, 0x60, __u32)
+#define VHOST_VSOCK_START		_IO(VHOST_VIRTIO, 0x61)
+#endif
Signature · 1 line
2.5.5
author maintainer
#8 lore raw

RFC v5 5/5 VSOCK: Add Makefile and Kconfig

In reply to Stefan Hajnoczi

From: Asias He <[email protected]>

Enable virtio-vsock and vhost-vsock.

---
v4:
* Make checkpatch.pl happy with longer option description
* Clarify dependency on virtio rather than QEMU as suggested by Alex Bennee
v3:
* Don't put vhost vsock driver into staging
* Add missing Kconfig dependencies (Arnd Bergmann <[email protected]>)

---
 drivers/vhost/Kconfig  | 15 +++++++++++++++
 drivers/vhost/Makefile |  4 ++++
 net/vmw_vsock/Kconfig  | 19 +++++++++++++++++++
 net/vmw_vsock/Makefile |  2 ++
 4 files changed, 40 insertions(+)
drivers/vhost/Kconfig +15 -0
index 533eaf0..d7aae9e 100644
@@ -21,6 +21,21 @@ config VHOST_SCSI
 	Say M here to enable the vhost_scsi TCM fabric module
 	for use with virtio-scsi guests
 
+config VHOST_VSOCK
+	tristate "vhost virtio-vsock driver"
+	depends on VSOCKETS && EVENTFD
+	select VIRTIO_VSOCKETS_COMMON
+	select VHOST
+	select VHOST_RING
+	default n
+	---help---
+	This kernel module can be loaded in the host kernel to provide AF_VSOCK
+	sockets for communicating with guests.  The guests must have the
+	virtio_transport.ko driver loaded to use the virtio-vsock device.
+
+	To compile this driver as a module, choose M here: the module will be called
+	vhost_vsock.
+
 config VHOST_RING
 	tristate
 	---help---
drivers/vhost/Makefile +4 -0
index e0441c3..6b012b9 100644
@@ -4,5 +4,9 @@ vhost_net-y := net.o
 obj-$(CONFIG_VHOST_SCSI) += vhost_scsi.o
 vhost_scsi-y := scsi.o
 
+obj-$(CONFIG_VHOST_VSOCK) += vhost_vsock.o
+vhost_vsock-y := vsock.o
+
 obj-$(CONFIG_VHOST_RING) += vringh.o
+
 obj-$(CONFIG_VHOST)	+= vhost.o
net/vmw_vsock/Kconfig +19 -0
index 14810ab..f27e74b 100644
@@ -26,3 +26,22 @@ config VMWARE_VMCI_VSOCKETS
 
 	  To compile this driver as a module, choose M here: the module
 	  will be called vmw_vsock_vmci_transport. If unsure, say N.
+
+config VIRTIO_VSOCKETS
+	tristate "virtio transport for Virtual Sockets"
+	depends on VSOCKETS && VIRTIO
+	select VIRTIO_VSOCKETS_COMMON
+	help
+	  This module implements a virtio transport for Virtual Sockets.
+
+	  Enable this transport if your Virtual Machine host supports Virtual
+	  Sockets over virtio.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called virtio_vsock_transport. If unsure, say N.
+
+config VIRTIO_VSOCKETS_COMMON
+       tristate
+       ---help---
+         This option is selected by any driver which needs to access
+         the virtio_vsock.
net/vmw_vsock/Makefile +2 -0
index 2ce52d7..cf4c294 100644
@@ -1,5 +1,7 @@
 obj-$(CONFIG_VSOCKETS) += vsock.o
 obj-$(CONFIG_VMWARE_VMCI_VSOCKETS) += vmw_vsock_vmci_transport.o
+obj-$(CONFIG_VIRTIO_VSOCKETS) += virtio_transport.o
+obj-$(CONFIG_VIRTIO_VSOCKETS_COMMON) += virtio_transport_common.o
 
 vsock-y += af_vsock.o vsock_addr.o
 
Signature · 1 line
2.5.5
author maintainer
#9 lore raw

RFC v5 5/5 VSOCK: Add Makefile and Kconfig

In reply to Stefan Hajnoczi

From: Asias He <[email protected]>

Enable virtio-vsock and vhost-vsock.

---
v4:
* Make checkpatch.pl happy with longer option description
* Clarify dependency on virtio rather than QEMU as suggested by Alex Bennee
v3:
* Don't put vhost vsock driver into staging
* Add missing Kconfig dependencies (Arnd Bergmann <[email protected]>)

---
 drivers/vhost/Kconfig  | 15 +++++++++++++++
 drivers/vhost/Makefile |  4 ++++
 net/vmw_vsock/Kconfig  | 19 +++++++++++++++++++
 net/vmw_vsock/Makefile |  2 ++
 4 files changed, 40 insertions(+)
drivers/vhost/Kconfig +15 -0
index 533eaf0..d7aae9e 100644
@@ -21,6 +21,21 @@ config VHOST_SCSI
 	Say M here to enable the vhost_scsi TCM fabric module
 	for use with virtio-scsi guests
 
+config VHOST_VSOCK
+	tristate "vhost virtio-vsock driver"
+	depends on VSOCKETS && EVENTFD
+	select VIRTIO_VSOCKETS_COMMON
+	select VHOST
+	select VHOST_RING
+	default n
+	---help---
+	This kernel module can be loaded in the host kernel to provide AF_VSOCK
+	sockets for communicating with guests.  The guests must have the
+	virtio_transport.ko driver loaded to use the virtio-vsock device.
+
+	To compile this driver as a module, choose M here: the module will be called
+	vhost_vsock.
+
 config VHOST_RING
 	tristate
 	---help---
drivers/vhost/Makefile +4 -0
index e0441c3..6b012b9 100644
@@ -4,5 +4,9 @@ vhost_net-y := net.o
 obj-$(CONFIG_VHOST_SCSI) += vhost_scsi.o
 vhost_scsi-y := scsi.o
 
+obj-$(CONFIG_VHOST_VSOCK) += vhost_vsock.o
+vhost_vsock-y := vsock.o
+
 obj-$(CONFIG_VHOST_RING) += vringh.o
+
 obj-$(CONFIG_VHOST)	+= vhost.o
net/vmw_vsock/Kconfig +19 -0
index 14810ab..f27e74b 100644
@@ -26,3 +26,22 @@ config VMWARE_VMCI_VSOCKETS
 
 	  To compile this driver as a module, choose M here: the module
 	  will be called vmw_vsock_vmci_transport. If unsure, say N.
+
+config VIRTIO_VSOCKETS
+	tristate "virtio transport for Virtual Sockets"
+	depends on VSOCKETS && VIRTIO
+	select VIRTIO_VSOCKETS_COMMON
+	help
+	  This module implements a virtio transport for Virtual Sockets.
+
+	  Enable this transport if your Virtual Machine host supports Virtual
+	  Sockets over virtio.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called virtio_vsock_transport. If unsure, say N.
+
+config VIRTIO_VSOCKETS_COMMON
+       tristate
+       ---help---
+         This option is selected by any driver which needs to access
+         the virtio_vsock.
net/vmw_vsock/Makefile +2 -0
index 2ce52d7..cf4c294 100644
@@ -1,5 +1,7 @@
 obj-$(CONFIG_VSOCKETS) += vsock.o
 obj-$(CONFIG_VMWARE_VMCI_VSOCKETS) += vmw_vsock_vmci_transport.o
+obj-$(CONFIG_VIRTIO_VSOCKETS) += virtio_transport.o
+obj-$(CONFIG_VIRTIO_VSOCKETS_COMMON) += virtio_transport_common.o
 
 vsock-y += af_vsock.o vsock_addr.o
 
Signature · 1 line
2.5.5
#10 lore raw
In reply to Stefan Hajnoczi
Quoted 5 lines · Stefan Hajnoczi

This series is based on Michael Tsirkin's vhost branch (v4.5-rc6).

I'm about to process Claudio Imbrenda's locking fixes for virtio-vsock but first I want to share the latest version of the code.  Several people are playing with vsock now so sharing the latest code should avoid duplicate work.

Thanks for this, I've been using it in my project and it mostly seems fine.

One wrinkle I came across, which I'm not sure if it is by design or a problem is that I can see this sequence coming from the guest (with other activity in between):

    1) OP_SHUTDOWN w/ flags == SHUTDOWN_RX
    2) OP_SHUTDOWN w/ flags == SHUTDOWN_TX
    3) OP_SHUTDOWN w/ flags == SHUTDOWN_TX|SHUTDOWN_RX

I orignally had my backend close things down at #2, however this meant that when #3 arrived it was for a non-existent socket (or, worse, an active one if the ports got reused). I checked v5 of the spec proposal[0] which says:
If these bits are set and there are no more virtqueue buffers pending the socket is disconnected.

but I'm not entirely sure if this behaviour contradicts this or not (the bits have both been set at #2, but not at the same time).

BTW, how does one tell if there are no more virtqueue buffers pending or not while processing the op?

Another thing I noticed, which is really more to do with the generic AF_VSOCK bits than anything to do with your patches is that there is no limitations on which vsock ports a non-privileged user can bind to and relatedly that there is no netns support so e.g. users in unproivileged containers can bind to any vsock port and talk to the host, which might be undesirable. For my use for now I just went with the big hammer approach of denying access from anything other than init_net namespace[1] while I consider what the right answer is.

Ian.

[0] http://thread.gmane.org/gmane.comp.emulators.virtio.devel/1092
[1]
From 366c9c42afb9bd54f92f72518470c09e46f12e88 Mon Sep 17 00:00:00 2001
From: Ian Campbell <[email protected]>
Date: Mon, 4 Apr 2016 14:50:10 +0100
Subject: [PATCH] VSOCK: Only allow host network namespace to use AF_VSOCK.

The VSOCK addressing schema does not really lend itself to simply creating an alternative end point address within a namespace.

---
 net/vmw_vsock/af_vsock.c | 3 +++

 1 file changed, 3 insertions(+)
net/vmw_vsock/af_vsock.c +0 -0
index 1e5f5ed..cdb3dd3 100644
@@ -1840,6 +1840,9 @@ static const struct proto_ops vsock_stream_ops = {

 static int vsock_create(struct net *net, struct socket *sock, int protocol, int kern)
 {
+ if (!net_eq(net, &init_net))
+ return -EAFNOSUPPORT;
+
  if (!sock)
  return -EINVAL;

--
2.8.0.rc3

#18 lore raw
In reply to Stefan Hajnoczi
Quoted 5 lines · Stefan Hajnoczi

This series is based on Michael Tsirkin's vhost branch (v4.5-rc6).

I'm about to process Claudio Imbrenda's locking fixes for virtio-vsock but first I want to share the latest version of the code.  Several people are playing with vsock now so sharing the latest code should avoid duplicate work.

Thanks for this, I've been using it in my project and it mostly seems fine.

One wrinkle I came across, which I'm not sure if it is by design or a problem is that I can see this sequence coming from the guest (with other activity in between):

    1) OP_SHUTDOWN w/ flags == SHUTDOWN_RX
    2) OP_SHUTDOWN w/ flags == SHUTDOWN_TX
    3) OP_SHUTDOWN w/ flags == SHUTDOWN_TX|SHUTDOWN_RX

I orignally had my backend close things down at #2, however this meant that when #3 arrived it was for a non-existent socket (or, worse, an active one if the ports got reused). I checked v5 of the spec proposal[0] which says:
If these bits are set and there are no more virtqueue buffers pending the socket is disconnected.

but I'm not entirely sure if this behaviour contradicts this or not (the bits have both been set at #2, but not at the same time).

BTW, how does one tell if there are no more virtqueue buffers pending or not while processing the op?

Another thing I noticed, which is really more to do with the generic AF_VSOCK bits than anything to do with your patches is that there is no limitations on which vsock ports a non-privileged user can bind to and relatedly that there is no netns support so e.g. users in unproivileged containers can bind to any vsock port and talk to the host, which might be undesirable. For my use for now I just went with the big hammer approach of denying access from anything other than init_net namespace[1] while I consider what the right answer is.

Ian.

[0] http://thread.gmane.org/gmane.comp.emulators.virtio.devel/1092
[1]
From 366c9c42afb9bd54f92f72518470c09e46f12e88 Mon Sep 17 00:00:00 2001
From: Ian Campbell <[email protected]>
Date: Mon, 4 Apr 2016 14:50:10 +0100
Subject: [PATCH] VSOCK: Only allow host network namespace to use AF_VSOCK.

The VSOCK addressing schema does not really lend itself to simply creating an alternative end point address within a namespace.

---
 net/vmw_vsock/af_vsock.c | 3 +++

 1 file changed, 3 insertions(+)
net/vmw_vsock/af_vsock.c +0 -0
index 1e5f5ed..cdb3dd3 100644
@@ -1840,6 +1840,9 @@ static const struct proto_ops vsock_stream_ops = {

 static int vsock_create(struct net *net, struct socket *sock, int protocol, int kern)
 {
+ if (!net_eq(net, &init_net))
+ return -EAFNOSUPPORT;
+
  if (!sock)
  return -EINVAL;

--
2.8.0.rc3

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#11 lore raw
In reply to Ian Campbell
Quoted 25 lines · Ian Campbell
Quoted 5 lines · Stefan Hajnoczi

This series is based on Michael Tsirkin's vhost branch (v4.5-rc6).

I'm about to process Claudio Imbrenda's locking fixes for virtio-vsock but first I want to share the latest version of the code.  Several people are playing with vsock now so sharing the latest code should avoid duplicate work.

Thanks for this, I've been using it in my project and it mostly seems fine.

One wrinkle I came across, which I'm not sure if it is by design or a problem is that I can see this sequence coming from the guest (with other activity in between):

    1) OP_SHUTDOWN w/ flags == SHUTDOWN_RX
    2) OP_SHUTDOWN w/ flags == SHUTDOWN_TX
    3) OP_SHUTDOWN w/ flags == SHUTDOWN_TX|SHUTDOWN_RX

I orignally had my backend close things down at #2, however this meant that when #3 arrived it was for a non-existent socket (or, worse, an active one if the ports got reused). I checked v5 of the spec proposal[0] which says:
If these bits are set and there are no more virtqueue buffers pending the socket is disconnected.

Quoted 5 lines

but I'm not entirely sure if this behaviour contradicts this or not (the bits have both been set at #2, but not at the same time).

BTW, how does one tell if there are no more virtqueue buffers pending or not while processing the op?

#2 is odd. The shutdown bits are sticky so they cannot be cleared once set. I would have expected just #1 and #3. The behavior you observe look like a bug.

The spec text does not convey the meaning of OP_SHUTDOWN well. OP_SHUTDOWN SHUTDOWN_TX|SHUTDOWN_RX means no further rx/tx is possible for this connection. "there are no more virtqueue buffers pending the socket" really means that this isn't an immediate close from the perspective of the application. If the application still has unread rx buffers then the socket stays readable until the rx data has been fully read.

Quoted 8 lines

Another thing I noticed, which is really more to do with the generic AF_VSOCK bits than anything to do with your patches is that there is no limitations on which vsock ports a non-privileged user can bind to and relatedly that there is no netns support so e.g. users in unproivileged containers can bind to any vsock port and talk to the host, which might be undesirable. For my use for now I just went with the big hammer approach of denying access from anything other than init_net namespace[1] while I consider what the right answer is.

From the vhost point of view each netns should have its own AF_VSOCK namespace. This way two containers could act as "the host" (CID 2) for their respective guests.

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#12 lore raw
Quoted 36 lines · Stefan Hajnoczi
Quoted 25 lines · Ian Campbell
Quoted 5 lines · Stefan Hajnoczi

This series is based on Michael Tsirkin's vhost branch (v4.5-rc6).

I'm about to process Claudio Imbrenda's locking fixes for virtio-vsock but first I want to share the latest version of the code.  Several people are playing with vsock now so sharing the latest code should avoid duplicate work.

Thanks for this, I've been using it in my project and it mostly seems fine.

One wrinkle I came across, which I'm not sure if it is by design or a problem is that I can see this sequence coming from the guest (with other activity in between):

    1) OP_SHUTDOWN w/ flags == SHUTDOWN_RX
    2) OP_SHUTDOWN w/ flags == SHUTDOWN_TX
    3) OP_SHUTDOWN w/ flags == SHUTDOWN_TX|SHUTDOWN_RX

I orignally had my backend close things down at #2, however this meant that when #3 arrived it was for a non-existent socket (or, worse, an active one if the ports got reused). I checked v5 of the spec proposal[0] which says:
If these bits are set and there are no more virtqueue buffers pending the socket is disconnected.

Quoted 5 lines

but I'm not entirely sure if this behaviour contradicts this or not (the bits have both been set at #2, but not at the same time).

BTW, how does one tell if there are no more virtqueue buffers pending or not while processing the op?

#2 is odd. The shutdown bits are sticky so they cannot be cleared once set. I would have expected just #1 and #3. The behavior you observe look like a bug.

Quoted 7 lines

The spec text does not convey the meaning of OP_SHUTDOWN well. OP_SHUTDOWN SHUTDOWN_TX|SHUTDOWN_RX means no further rx/tx is possible for this connection. "there are no more virtqueue buffers pending the socket" really means that this isn't an immediate close from the perspective of the application. If the application still has unread rx buffers then the socket stays readable until the rx data has been fully read.

Yes but you also wrote:
If these bits are set and there are no more virtqueue buffers pending the socket is disconnected.

how does remote know that there are no buffers pending and so it's safe to reuse the same source/destination address now? Maybe destination should send RST at that point?

Quoted 9 lines
Quoted 8 lines

Another thing I noticed, which is really more to do with the generic AF_VSOCK bits than anything to do with your patches is that there is no limitations on which vsock ports a non-privileged user can bind to and relatedly that there is no netns support so e.g. users in unproivileged containers can bind to any vsock port and talk to the host, which might be undesirable. For my use for now I just went with the big hammer approach of denying access from anything other than init_net namespace[1] while I consider what the right answer is.

Quoted 3 lines

From the vhost point of view each netns should have its own AF_VSOCK namespace. This way two containers could act as "the host" (CID 2) for their respective guests.

I wonder how this interacts with the disconnect on migration idea that you discussed. Specifically, socket has to stay connected

author maintainer
#13 lore raw
Quoted 12 lines · Michael S. Tsirkin
Quoted 11 lines · Stefan Hajnoczi
Quoted 10 lines · Ian Campbell
Quoted 5 lines · Stefan Hajnoczi

This series is based on Michael Tsirkin's vhost branch (v4.5-rc6).

I'm about to process Claudio Imbrenda's locking fixes for virtio-vsock but first I want to share the latest version of the code.  Several people are playing with vsock now so sharing the latest code should avoid duplicate work.

Thanks for this, I've been using it in my project and it mostly seems fine.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

One wrinkle I came across, which I'm not sure if it is by design or a problem is that I can see this sequence coming from the guest (with other activity in between):

    1) OP_SHUTDOWN w/ flags == SHUTDOWN_RX
    2) OP_SHUTDOWN w/ flags == SHUTDOWN_TX
    3) OP_SHUTDOWN w/ flags == SHUTDOWN_TX|SHUTDOWN_RX

How did you trigger this sequence? I'd like to reproduce it.

Quoted 25 lines
Quoted 17 lines
Quoted 7 lines

I orignally had my backend close things down at #2, however this meant that when #3 arrived it was for a non-existent socket (or, worse, an active one if the ports got reused). I checked v5 of the spec proposal[0] which says:
If these bits are set and there are no more virtqueue buffers pending the socket is disconnected.

Quoted 5 lines

but I'm not entirely sure if this behaviour contradicts this or not (the bits have both been set at #2, but not at the same time).

BTW, how does one tell if there are no more virtqueue buffers pending or not while processing the op?

#2 is odd. The shutdown bits are sticky so they cannot be cleared once set. I would have expected just #1 and #3. The behavior you observe look like a bug.

Quoted 7 lines

The spec text does not convey the meaning of OP_SHUTDOWN well. OP_SHUTDOWN SHUTDOWN_TX|SHUTDOWN_RX means no further rx/tx is possible for this connection. "there are no more virtqueue buffers pending the socket" really means that this isn't an immediate close from the perspective of the application. If the application still has unread rx buffers then the socket stays readable until the rx data has been fully read.

Quoted 7 lines

Yes but you also wrote:
If these bits are set and there are no more virtqueue buffers pending the socket is disconnected.

how does remote know that there are no buffers pending and so it's safe to reuse the same source/destination address now? Maybe destination should send RST at that point?

You are right, the source/destination address could be reused while the remote still has the connection in their table. Connection establishment would fail with a RST reply.

I can think of two solutions:

1. Implementations must remove connections from their table as soon as SHUTDOWN_TX|SHUTDOWN_RX is received. This way the source/destination address tuple can be reused immediately, i.e. new connections with the same source/destination would be possible while an application is still draining the receive buffers of an old connection.

2. Extend the connection lifecycle so that an A->B SHUTDOWN_TX|SHUTDOWN_RX must be followed by a by a B->A RST to close a connection. This way the source/destination address is only in use once at a time.

Option #2 seems safer because there is no overlap in source/destination address usage.

author maintainer
#16 lore raw
In reply to Michael S. Tsirkin
Quoted 12 lines · Michael S. Tsirkin
Quoted 11 lines · Stefan Hajnoczi
Quoted 10 lines · Ian Campbell
Quoted 5 lines · Stefan Hajnoczi

This series is based on Michael Tsirkin's vhost branch (v4.5-rc6).

I'm about to process Claudio Imbrenda's locking fixes for virtio-vsock but first I want to share the latest version of the code.  Several people are playing with vsock now so sharing the latest code should avoid duplicate work.

Thanks for this, I've been using it in my project and it mostly seems fine.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

One wrinkle I came across, which I'm not sure if it is by design or a problem is that I can see this sequence coming from the guest (with other activity in between):

    1) OP_SHUTDOWN w/ flags == SHUTDOWN_RX
    2) OP_SHUTDOWN w/ flags == SHUTDOWN_TX
    3) OP_SHUTDOWN w/ flags == SHUTDOWN_TX|SHUTDOWN_RX

How did you trigger this sequence? I'd like to reproduce it.

Quoted 25 lines
Quoted 17 lines
Quoted 7 lines

I orignally had my backend close things down at #2, however this meant that when #3 arrived it was for a non-existent socket (or, worse, an active one if the ports got reused). I checked v5 of the spec proposal[0] which says:
If these bits are set and there are no more virtqueue buffers pending the socket is disconnected.

Quoted 5 lines

but I'm not entirely sure if this behaviour contradicts this or not (the bits have both been set at #2, but not at the same time).

BTW, how does one tell if there are no more virtqueue buffers pending or not while processing the op?

#2 is odd. The shutdown bits are sticky so they cannot be cleared once set. I would have expected just #1 and #3. The behavior you observe look like a bug.

Quoted 7 lines

The spec text does not convey the meaning of OP_SHUTDOWN well. OP_SHUTDOWN SHUTDOWN_TX|SHUTDOWN_RX means no further rx/tx is possible for this connection. "there are no more virtqueue buffers pending the socket" really means that this isn't an immediate close from the perspective of the application. If the application still has unread rx buffers then the socket stays readable until the rx data has been fully read.

Quoted 7 lines

Yes but you also wrote:
If these bits are set and there are no more virtqueue buffers pending the socket is disconnected.

how does remote know that there are no buffers pending and so it's safe to reuse the same source/destination address now? Maybe destination should send RST at that point?

You are right, the source/destination address could be reused while the remote still has the connection in their table. Connection establishment would fail with a RST reply.

I can think of two solutions:

1. Implementations must remove connections from their table as soon as SHUTDOWN_TX|SHUTDOWN_RX is received. This way the source/destination address tuple can be reused immediately, i.e. new connections with the same source/destination would be possible while an application is still draining the receive buffers of an old connection.

2. Extend the connection lifecycle so that an A->B SHUTDOWN_TX|SHUTDOWN_RX must be followed by a by a B->A RST to close a connection. This way the source/destination address is only in use once at a time.

Option #2 seems safer because there is no overlap in source/destination address usage.

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#17 lore raw
In reply to Michael S. Tsirkin

Some how Stefan's reply disapeared from my INBOX (although I did see it) so replying here.

Quoted 38 lines · Michael S. Tsirkin
Quoted 37 lines · Stefan Hajnoczi
Quoted 26 lines · Ian Campbell
Quoted 6 lines · Stefan Hajnoczi

This series is based on Michael Tsirkin's vhost branch (v4.5-rc6).

I'm about to process Claudio Imbrenda's locking fixes for virtio-vsock but first I want to share the latest version of the code.  Several people are playing with vsock now so sharing the latest code should avoid duplicate work.

Thanks for this, I've been using it in my project and it mostly seems fine.

One wrinkle I came across, which I'm not sure if it is by design or a problem is that I can see this sequence coming from the guest (with other activity in between):

    1) OP_SHUTDOWN w/ flags == SHUTDOWN_RX
    2) OP_SHUTDOWN w/ flags == SHUTDOWN_TX
    3) OP_SHUTDOWN w/ flags == SHUTDOWN_TX|SHUTDOWN_RX

I orignally had my backend close things down at #2, however this meant that when #3 arrived it was for a non-existent socket (or, worse, an active one if the ports got reused). I checked v5 of the spec proposal[0] which says:
    If these bits are set and there are no more virtqueue buffers pending the socket is disconnected.

Quoted 5 lines

but I'm not entirely sure if this behaviour contradicts this or not (the bits have both been set at #2, but not at the same time).

BTW, how does one tell if there are no more virtqueue buffers pending or not while processing the op?

#2 is odd.  The shutdown bits are sticky so they cannot be cleared once set.  I would have expected just #1 and #3.  The behavior you observe look like a bug.

Quoted 7 lines
Quoted 7 lines

The spec text does not convey the meaning of OP_SHUTDOWN well. OP_SHUTDOWN SHUTDOWN_TX|SHUTDOWN_RX means no further rx/tx is possible for this connection.  "there are no more virtqueue buffers pending the socket" really means that this isn't an immediate close from the perspective of the application.  If the application still has unread rx buffers then the socket stays readable until the rx data has been fully read.

Thanks, distinguishing the local buffer to the application from the vring would make that clearer. Perhaps by not talking about "virtqueue buffers" since they sound like a vring thing.

However, as Michael observes I'm not sure that's the whole story.

Quoted 6 lines

Yes but you also wrote:
If these bits are set and there are no more virtqueue buffers pending the socket is disconnected.

how does remote know that there are no buffers pending and so it's safe to reuse the same source/destination address now?

Indeed this is one of the things I struggled with. e.g. If I send a SHUTDOWN_RX to my peer am I supposed to wait for that buffer to come back (so I know the peer has seen it) and then wait for an entire "cycle" of the TX ring to know there is nothing still in flight? That's some tricky book-keeping.

Quoted 2 lines

  Maybe destination
should send RST at that point?

i.e. upon receipt of SHUTDOWN_RX|SHUTDOWN_TX from the peer you are expected to send a RST. When the peer observes that then they know there is no further data in that connection on the ring?

That sounds like it would be helpful.

Quoted 11 lines
Quoted 11 lines
Quoted 8 lines

Another thing I noticed, which is really more to do with the generic AF_VSOCK bits than anything to do with your patches is that there is no limitations on which vsock ports a non-privileged user can bind to and relatedly that there is no netns support so e.g. users in unproivileged containers can bind to any vsock port and talk to the host, which might be undesirable. For my use for now I just went with the big hammer approach of denying access from anything other than init_net namespace[1] while I consider what the right answer is.

From the vhost point of view each netns should have its own AF_VSOCK namespace.  This way two containers could act as "the host" (CID 2) for their respective guests.

When you say "should" you mean that's the intended design as opposed to what the current code is actually doing, right?

Ian.
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#14 lore raw
In reply to Stefan Hajnoczi

For some reason your mails in this thread only appear in the gmail web UI and not on the IMAP version of my mailbox (my own and Michael's mails are fine).

So I'm replying via the web interface, sorry for the inevitable formatting mess :-/

I've CCd another mailbox in the hopes of getting your mails in that IMAP folder instead/aswell so I can avoid this next time.

Quoted 11 lines · Stefan Hajnoczi
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

One wrinkle I came across, which I'm not sure if it is by design or a problem is that I can see this sequence coming from the guest (with other activity in between):

1) OP_SHUTDOWN w/ flags == SHUTDOWN_RX
2) OP_SHUTDOWN w/ flags == SHUTDOWN_TX
3) OP_SHUTDOWN w/ flags == SHUTDOWN_TX|SHUTDOWN_RX

How did you trigger this sequence? I'd like to reproduce it.

Nothing magic. I've written some logging into my backend and captured the result for a simple backend initiated connection.

In the log "TX" and "RX" indicate the thread doing the processing (with "TX" being the one which processes the guest's TX ring, i.e. data coming from the guest to the host). "<=" indicates a buffer going from guest to host and "=>" is from host to guest. NB that guest to host replies are queued synchronously by the TX thread onto the RX ring which is why the somewhat odd looking "TX: =>" combination can occur. A host initiated connection also happens from the TX thread in the same way.

The trace is of a simple request response (which both fit in one buffer in each direction), the lines without an "?X:" prefix are my annotations/guesses as to what is going on:

TX: =>SRC:00000002.00010002 DST:00000003.00000948
TX:   LEN:00000000 TYPE:0001 OP:1=REQUEST
TX:  FLAGS:00000000 BUF_ALLOC:00008000 FWD_CNT:00000000
TX: <=SRC:00000003.00000948 DST:00000002.00010002
TX:   LEN:00000000 TYPE:0001 OP:2=RESPONSE
TX:  FLAGS:00000000 BUF_ALLOC:00040000 FWD_CNT:00000000

REQUEST + RESPONSE == Channel open successfully

RX: =>SRC:00000002.00010002 DST:00000003.00000948
RX:   LEN:0000005e TYPE:0001 OP:5=RW
RX:  FLAGS:00000000 BUF_ALLOC:00008000 FWD_CNT:00000000

Host sends a request to the guest

TX: <=SRC:00000003.00000948 DST:00000002.00010002
TX:   LEN:00000000 TYPE:0001 OP:6=CREDIT_UPDATE
TX:  FLAGS:00000000 BUF_ALLOC:00040000 FWD_CNT:0000005e

Guest replies with a credit update

TX: <=SRC:00000003.00000948 DST:00000002.00010002
TX:   LEN:00000091 TYPE:0001 OP:5=RW
TX:  FLAGS:00000000 BUF_ALLOC:00040000 FWD_CNT:0000005e

Guest replies with the answer to the request

RX: =>SRC:00000002.00010002 DST:00000003.00000948
RX:   LEN:00000000 TYPE:0001 OP:4=SHUTDOWN
RX:  FLAGS:00000002 BUF_ALLOC:00008000 FWD_CNT:00000091

Host has sent its only request, so host app must have done shutdown(SHUT_WR) I suppose and host therefore sends SHUTDOWN_TX.

TX: <=SRC:00000003.00000948 DST:00000002.00010002
TX:   LEN:00000000 TYPE:0001 OP:4=SHUTDOWN
TX:  FLAGS:00000001 BUF_ALLOC:00040000 FWD_CNT:0000005e

Guest SHUTDOWN_RX. I'm not sure if this is a direct kernel response to the SHUTDOWN_TX or if the application inside the guest saw an EOF when reading the socket and did the corresponding shutdown(SHUT_RD).

TX: <=SRC:00000003.00000948 DST:00000002.00010002
TX:   LEN:00000000 TYPE:0001 OP:4=SHUTDOWN
TX:  FLAGS:00000002 BUF_ALLOC:00040000 FWD_CNT:0000005e

Guest SHUTDOWN_TX, I presume that having sent the only response it is going to it then does shutdown(SHUT_WR).

TX: <=SRC:00000003.00000948 DST:00000002.00010002
TX:   LEN:00000000 TYPE:0001 OP:4=SHUTDOWN
TX:  FLAGS:00000003 BUF_ALLOC:00040000 FWD_CNT:0000005e

Guest shuts down both directions.

Perhaps the guest end is turning shutdown(foo) directly into a vsock message without or-ing in the current state?

Quoted 45 lines
Quoted 25 lines
Quoted 17 lines

I orignally had my backend close things down at #2, however this meant

Quoted 11 lines

that when #3 arrived it was for a non-existent socket (or, worse, an active one if the ports got reused). I checked v5 of the spec proposal[0] which says:
If these bits are set and there are no more virtqueue buffers pending the socket is disconnected.

but I'm not entirely sure if this behaviour contradicts this or not (the bits have both been set at #2, but not at the same time).

BTW, how does one tell if there are no more virtqueue buffers pending or not while processing the op?

#2 is odd. The shutdown bits are sticky so they cannot be cleared once set. I would have expected just #1 and #3. The behavior you observe look like a bug.

Quoted 7 lines

The spec text does not convey the meaning of OP_SHUTDOWN well. OP_SHUTDOWN SHUTDOWN_TX|SHUTDOWN_RX means no further rx/tx is possible for this connection. "there are no more virtqueue buffers pending the socket" really means that this isn't an immediate close from the perspective of the application. If the application still has unread rx buffers then the socket stays readable until the rx data has been fully read.

Quoted 7 lines

Yes but you also wrote:
If these bits are set and there are no more virtqueue buffers pending the socket is disconnected.

how does remote know that there are no buffers pending and so it's safe to reuse the same source/destination address now? Maybe destination should send RST at that point?

You are right, the source/destination address could be reused while the remote still has the connection in their table. Connection establishment would fail with a RST reply.

I can think of two solutions:

1. Implementations must remove connections from their table as soon as SHUTDOWN_TX|SHUTDOWN_RX is received. This way the source/destination address tuple can be reused immediately, i.e. new connections with the same source/destination would be possible while an application is still draining the receive buffers of an old connection.

Quoted 8 lines

2. Extend the connection lifecycle so that an A->B SHUTDOWN_TX|SHUTDOWN_RX must be followed by a by a B->A RST to close a connection. This way the source/destination address is only in use once at a time.

Option #2 seems safer because there is no overlap in source/destination address usage.

I agree (I hadn't seen this when I replied to Michael similar suggestion).

Ian. (sorry again for the formatting)

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author maintainer
#15 lore raw
Quoted 2 lines · Ian Campbell

Perhaps the guest end is turning shutdown(foo) directly into a vsock message without or-ing in the current state?

Yes, you are right:

  lock_sock(sk);
  sk->sk_shutdown |= mode;
  sk->sk_state_change(sk);
  release_sock(sk);

  if (sk->sk_type == SOCK_STREAM) {
      sock_reset_flag(sk, SOCK_DONE);
      vsock_send_shutdown(sk, mode);

Although sk_shutdown is ORed correctly, vsock_send_shutdown() is called with just the shutdown() argument.

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization