Skip to contentSkip to conversation

hvsock: introduce Hyper-V VM Sockets feature

PATCH Superseded Dexuan Cui 3 participants 5 messages Jul 14, 2015 – Jul 20, 2015

Subsystems (4)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Dexuan Cui

author maintainer
#0 lore raw

PATCH v2 6/7 hvsock: introduce Hyper-V VM Sockets feature

Hyper-V VM sockets (hvsock) supplies a byte-stream based communication mechanism between the host and a guest. It's kind of TCP over VMBus, but the transportation layer (VMBus) is much simpler than IP. With Hyper-V VM Sockets, applications between the host and a guest can talk with each other directly by the traditional BSD-style socket APIs.

Hyper-V VM Sockets is only available on Windows 10 host and later. The patch implements the necessary support in the guest side by introducing a new socket address family AF_HYPERV.

---

Changes since v1:
- added __init and __exit for the module init/exit functions
- net/hv_sock/Kconfig: "default m" -> "default m if HYPERV"
- MODULE_LICENSE: "Dual MIT/GPL" -> "Dual BSD/GPL"

I know the kernel has already had a VM Sockets driver (AF_VSOCK) based on VMware's VMCI (net/vmw_vsock/, drivers/misc/vmw_vmci), and KVM is proposing AF_VSOCK of virtio version:
http://thread.gmane.org/gmane.linux.network/365205.

However, though Hyper-V VM Sockets may seem conceptually similar to AF_VOSCK, there are differences in the transportation layer, and IMO these make the direct code reusing impractical:

1. In AF_VSOCK, the endpoint type is: <u32 ContextID, u32 Port>, but in AF_HYPERV, the endpoint type is: <GUID VM_ID, GUID ServiceID>. Here GUID is 128-bit.

2. AF_VSOCK supports SOCK_DGRAM, while AF_HYPERV doesn't.

3. AF_VSOCK supports some special sock opts, like SO_VM_SOCKETS_BUFFER_SIZE, SO_VM_SOCKETS_BUFFER_MIN/MAX_SIZE and SO_VM_SOCKETS_CONNECT_TIMEOUT. These are meaningless to AF_HYPERV.

4. Some AF_VSOCK's VMCI transportation ops are meanless to AF_HYPERV/VMBus,
like .notify_recv_init
.notify_recv_pre_block
.notify_recv_pre_dequeue
.notify_recv_post_dequeue
.notify_send_init
.notify_send_pre_block
.notify_send_pre_enqueue
.notify_send_post_enqueue
etc.

So I think we'd better introduce a new address family: AF_HYPERV.

 MAINTAINERS                 |    2 +
 include/linux/socket.h      |    4 +-
 include/net/af_hvsock.h     |   44 ++
 include/uapi/linux/hyperv.h |   16 +
 net/Kconfig                 |    1 +
 net/Makefile                |    1 +
 net/hv_sock/Kconfig         |   10 +
 net/hv_sock/Makefile        |    5 +
 net/hv_sock/af_hvsock.c     | 1469 +++++++++++++++++++++++++++++++++++++++++++
 9 files changed, 1551 insertions(+), 1 deletion(-)
 create mode 100644 include/net/af_hvsock.h
 create mode 100644 net/hv_sock/Kconfig
 create mode 100644 net/hv_sock/Makefile
 create mode 100644 net/hv_sock/af_hvsock.c
MAINTAINERS +2 -0
index e7bdbac..a4a7e03 100644
@@ -4941,7 +4941,9 @@ F:	drivers/input/serio/hyperv-keyboard.c
 F:	drivers/net/hyperv/
 F:	drivers/scsi/storvsc_drv.c
 F:	drivers/video/fbdev/hyperv_fb.c
+F:	net/hv_sock/
 F:	include/linux/hyperv.h
+F:	include/net/af_hvsock.h
 F:	tools/hv/
 
 I2C OVER PARALLEL PORT
include/linux/socket.h +3 -1
index 5bf59c8..d5ef612 100644
@@ -200,7 +200,8 @@ struct ucred {
 #define AF_ALG		38	/* Algorithm sockets		*/
 #define AF_NFC		39	/* NFC sockets			*/
 #define AF_VSOCK	40	/* vSockets			*/
-#define AF_MAX		41	/* For now.. */
+#define AF_HYPERV	41	/* Hyper-V virtual sockets	*/
+#define AF_MAX		42	/* For now.. */
 
 /* Protocol families, same as address families. */
 #define PF_UNSPEC	AF_UNSPEC
@@ -246,6 +247,7 @@ struct ucred {
 #define PF_ALG		AF_ALG
 #define PF_NFC		AF_NFC
 #define PF_VSOCK	AF_VSOCK
+#define PF_HYPERV	AF_HYPERV
 #define PF_MAX		AF_MAX
 
 /* Maximum queue length specifiable by listen.  */
include/net/af_hvsock.h new file +44 -0

44 lines not on this page · raw message

include/uapi/linux/hyperv.h +16 -0
index e4c0a35..ed51402 100644
@@ -26,6 +26,7 @@
 #define _UAPI_HYPERV_H
 
 #include <linux/uuid.h>
+#include <linux/socket.h>
 
 /*
  * Framework version for util services.
@@ -395,4 +396,19 @@ struct hv_kvp_ip_msg {
 	struct hv_kvp_ipaddr_value      kvp_ip_val;
 } __attribute__((packed));
 
+/* This is the Hyper-V socket's address format. */
+struct sockaddr_hv {
+	__kernel_sa_family_t	shv_family;	/* Address family	*/
+	__le16		reserved;		/* Must be zero		*/
+	uuid_le		shv_vm_id;	/* Not used by Linux. Must be Zero. */
+	uuid_le		shv_service_id;		/* Service ID		*/
+};
+
+#define SHV_VMID_GUEST	NULL_UUID_LE
+#define SHV_VMID_HOST	NULL_UUID_LE
+
+#define SHV_SERVICE_ID_ANY	NULL_UUID_LE
+
+#define SHV_PROTO_RAW		1
+
 #endif /* _UAPI_HYPERV_H */
net/Kconfig +1 -0
index 57a7c5a..9ad9f66 100644
@@ -228,6 +228,7 @@ source "net/dns_resolver/Kconfig"
 source "net/batman-adv/Kconfig"
 source "net/openvswitch/Kconfig"
 source "net/vmw_vsock/Kconfig"
+source "net/hv_sock/Kconfig"
 source "net/netlink/Kconfig"
 source "net/mpls/Kconfig"
 source "net/hsr/Kconfig"
net/Makefile +1 -0
index 3995613..d95ff12 100644
@@ -69,6 +69,7 @@ obj-$(CONFIG_BATMAN_ADV)	+= batman-adv/
 obj-$(CONFIG_NFC)		+= nfc/
 obj-$(CONFIG_OPENVSWITCH)	+= openvswitch/
 obj-$(CONFIG_VSOCKETS)	+= vmw_vsock/
+obj-$(CONFIG_HYPERV_SOCK)	+= hv_sock/
 obj-$(CONFIG_MPLS)		+= mpls/
 obj-$(CONFIG_HSR)		+= hsr/
 ifneq ($(CONFIG_NET_SWITCHDEV),)
net/hv_sock/Kconfig new file +10 -0
new file mode 100644
index 0000000..900373f
@@ -0,0 +1,10 @@
+config HYPERV_SOCK
+	tristate "Microsoft Hyper-V Socket (EXPERIMENTAL)"
+	depends on HYPERV
+	default m if HYPERV
+	help
+	  Hyper-V Socket is a socket protocol similar to TCP, allowing
+	  communication between a Linux guest and the host.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called hv_sock.
net/hv_sock/Makefile new file +5 -0
new file mode 100644
index 0000000..a33dbcf
@@ -0,0 +1,5 @@
+obj-$(CONFIG_HYPERV_SOCK) += hv_sock.o
+
+hv_sock-y += af_hvsock.o
+
+ccflags-y	+= -Wall -Werror
net/hv_sock/af_hvsock.c new file +1469 -0

1,469 lines not on this page · raw message

Signature · 1 line
2.1.0
maintainer
#1 lore raw

From: Dexuan Cui <[email protected]>
Date: Tue, 14 Jul 2015 03:00:48 -0700

net/hv_sock/af_hvsock.c line 326

Quoted 1 line
net/hv_sock/af_hvsock.c new file +1 -0
+	pr_debug("hvsock_sk_destruct: called\n");

Debug logging just to state that a function is called is not appropriate, we have very sophisticated tracing facilities in the kernel that can do that transparently, and more.

PLease remove this.

net/hv_sock/af_hvsock.c line 336

Quoted 2 lines
net/hv_sock/af_hvsock.c new file +2 -0
+	if (hvsk->channel) {
+		pr_debug("hvsock_sk_destruct: calling vmbus_close()\n");

Likewise, these kinds of debug logs are totally inappropriate.

net/hv_sock/af_hvsock.c line 383

Quoted 10 lines
net/hv_sock/af_hvsock.c new file +10 -0
+static int hvsock_release(struct socket *sock)
+{
+	/* sock->sk is NULL, if accept() is interrupted by a signal */
+	if (sock->sk) {
+		__hvsock_release(sock->sk);
+		sock->sk = NULL;
+	}
+
+	sock->state = SS_FREE;
+	pr_debug("hvsock_release called\n\n");

Likewise.

author maintainer
#2 lore raw
Quoted 6 lines

From: David Miller
Sent: Thursday, July 16, 2015 12:19

From: Dexuan Cui
Date: Tue, 14 Jul 2015 03:00:48 -0700

Quoted 7 lines

net/hv_sock/af_hvsock.c line 326

Quoted 1 line
net/hv_sock/af_hvsock.c new file +1 -0
+	pr_debug("hvsock_sk_destruct: called\n");

Debug logging just to state that a function is called is not appropriate, we have very sophisticated tracing facilities in the kernel that can do that transparently, and more.

Please remove this.

OK.

Quoted 4 lines

net/hv_sock/af_hvsock.c line 336

Quoted 2 lines
net/hv_sock/af_hvsock.c new file +2 -0
+	if (hvsk->channel) {
+		pr_debug("hvsock_sk_destruct: calling vmbus_close()\n");

Likewise, these kinds of debug logs are totally inappropriate.

OK, I'll remove all the pr_debug() in the patch.

Quoted 4 lines
Quoted 4 lines
net/hv_sock/af_hvsock.c new file +4 -0
+static int hvsock_release(struct socket *sock)
+{
+	/* sock->sk is NULL, if accept() is interrupted by a signal */
+	if (sock->sk) {
Quoted 8 lines

net/hv_sock/af_hvsock.c line 383

Quoted 6 lines
net/hv_sock/af_hvsock.c new file +6 -0
+		__hvsock_release(sock->sk);
+		sock->sk = NULL;
+	}
+
+	sock->state = SS_FREE;
+	pr_debug("hvsock_release called\n\n");

Likewise.

OK. Will fix it.

Thanks,
-- Dexuan

#3 lore raw
Quoted 12 lines · Dexuan Cui
Quoted 12 lines

From: David Miller
Sent: Thursday, July 16, 2015 12:19

From: Dexuan Cui
Date: Tue, 14 Jul 2015 03:00:48 -0700

net/hv_sock/af_hvsock.c line 326

Quoted 1 line
net/hv_sock/af_hvsock.c new file +1 -0
+	pr_debug("hvsock_sk_destruct: called\n");

Debug logging just to state that a function is called is not appropriate, we have very sophisticated tracing facilities in the kernel that can do that transparently, and more.

Quoted 9 lines
Quoted 1 line

Please remove this.

OK.

Quoted 4 lines

net/hv_sock/af_hvsock.c line 336

Quoted 2 lines
net/hv_sock/af_hvsock.c new file +2 -0
+	if (hvsk->channel) {
+		pr_debug("hvsock_sk_destruct: calling vmbus_close()\n");

Likewise, these kinds of debug logs are totally inappropriate.

OK, I'll remove all the pr_debug() in the patch.

I'd suggest we rather use something like net_dbg_ratelimited() intead. The driver is new so issues are expected. Some debugging may be useful)

[...]

Signature · 1 line
  Vitaly
author maintainer
#4 lore raw
Quoted 25 lines

-----Original Message-----
From: Vitaly Kuznetsov
Sent: Friday, July 17, 2015 23:04
To: Dexuan Cui

Quoted 19 lines · Dexuan Cui
Quoted 6 lines

From: David Miller
Sent: Thursday, July 16, 2015 12:19

From: Dexuan Cui
Date: Tue, 14 Jul 2015 03:00:48 -0700

Quoted 7 lines

net/hv_sock/af_hvsock.c line 326

Quoted 1 line
net/hv_sock/af_hvsock.c new file +1 -0
+	pr_debug("hvsock_sk_destruct: called\n");

Debug logging just to state that a function is called is not appropriate, we have very sophisticated tracing facilities in the kernel that can do that transparently, and more.

Please remove this.

OK.

Quoted 4 lines

net/hv_sock/af_hvsock.c line 336

Quoted 2 lines
net/hv_sock/af_hvsock.c new file +2 -0
+	if (hvsk->channel) {
+		pr_debug("hvsock_sk_destruct: calling vmbus_close()\n");

Likewise, these kinds of debug logs are totally inappropriate.

Quoted 8 lines
Quoted 2 lines

OK, I'll remove all the pr_debug() in the patch.

I'd suggest we rather use something like net_dbg_ratelimited() intead. The driver is new so issues are expected. Some debugging may be useful)

Vitaly

The net_*ratelimited() functions may lose message if the messages are printed very frequently, like in hvsock_sendmsg()/hvsock_recvmsg().

I used pr_debug() because I saw it's widely used in net/ipv4/. Usually CONFIG_DYNAMIC_DEBUG=y, so pr_debug() means dynamic_pr_debug(), which won't output anything by default.

Sorry, I'm not sure about the "sophisticated tracing facilities in the kernel that can do that transparently, and more" mentioned by David.

Anyway, I don't mind simply removing the pr_debug() usages. I will send out V3 later today.

Thanks,
-- Dexuan