Skip to contentSkip to conversation

hvsock: introduce Hyper-V VM Sockets feature

PATCH In review Dexuan Cui 2 participants 3 messages Dec 24, 2015 – Jan 6, 2016

Subsystems (4)
lore
Share & tools
lore

Dexuan Cui

author maintainer
#0 lore raw

PATCH v5 9/9 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.

---
 MAINTAINERS                 |    2 +
 include/linux/hyperv.h      |    8 +
 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        |    3 +
 net/hv_sock/af_hvsock.c     | 1473 +++++++++++++++++++++++++++++++++++++++++++
 10 files changed, 1561 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 44666b1..f89a4ba 100644
@@ -5156,7 +5156,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/
 F:	Documentation/ABI/stable/sysfs-bus-vmbus
 
include/linux/hyperv.h +8 -0
index 8e59f98..3495762 100644
@@ -1322,4 +1322,12 @@ struct vmpipe_proto_header {
 #define HVSOCK_HEADER_LEN	(sizeof(struct vmpacket_descriptor) + \
 				 sizeof(struct vmpipe_proto_header))
 
+/* See 'prev_indices' in hv_ringbuffer_read(), hv_ringbuffer_write() */
+#define PREV_INDICES_LEN	(sizeof(u64))
+
+#define HVSOCK_PKT_LEN(payload_len)	(HVSOCK_HEADER_LEN + \
+					ALIGN((payload_len), 8) + \
+					PREV_INDICES_LEN)
+#define HVSOCK_MIN_PKT_LEN	HVSOCK_PKT_LEN(1)
+
 #endif /* _HYPERV_H */
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 e347b24..ded4c13 100644
@@ -26,6 +26,7 @@
 #define _UAPI_HYPERV_H
 
 #include <linux/uuid.h>
+#include <linux/socket.h>
 
 /*
  * Framework version for util services.
@@ -396,4 +397,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. 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 11f8c22..e6b7822 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 a5d0409..ef0abd2 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 +3 -0
new file mode 100644
index 0000000..716c012
@@ -0,0 +1,3 @@
+obj-$(CONFIG_HYPERV_SOCK) += hv_sock.o
+
+hv_sock-y += af_hvsock.o
net/hv_sock/af_hvsock.c new file +1473 -0

1,473 lines not on this page · raw message

Signature · 1 line
2.1.4
#1 lore raw

Dexuan Cui <[email protected]> writes:

Just some minor nitpicks below -- I have to admit I didn't test the feature.

[..skip..]

Quoted 9 lines
net/hv_sock/af_hvsock.c new file +9 -0
+
+	if (sk->sk_err) {
+		ret = -sk->sk_err;
+		goto out_wait_error;
+	} else {
+		ret = 0;
+	}
+
+out_wait:

net/hv_sock/af_hvsock.c line 875

Quoted 10 lines
net/hv_sock/af_hvsock.c new file +10 -0
+	finish_wait(sk_sleep(sk), &wait);
+out:
+	release_sock(sk);
+	return ret;
+
+out_wait_error:
+	sk->sk_state = SS_UNCONNECTED;
+	sock->state = SS_UNCONNECTED;
+	goto out_wait;
+}

Why not just place out_wait_error label before out_wait (and do 'goto out_wait' in ret = 0 case instead of 'goto out_wait_error' in the error case)?

[..skip..]

net/hv_sock/af_hvsock.c line 1430

Quoted 9 lines
net/hv_sock/af_hvsock.c new file +9 -0
+
+static int __init hvsock_init(void)
+{
+	int ret;
+
+	/* Hyper-V socket requires at least VMBus 4.0 */
+	if ((vmbus_proto_version >> 16) < 4) {
+		pr_err("failed to load: VMBus 4 or later is required\n");
+		return -ENODEV;

(Let me pretend I'm Dan :-) So here we return ...

net/hv_sock/af_hvsock.c line 1436

Quoted 6 lines
net/hv_sock/af_hvsock.c new file +6 -0
+	}
+
+	ret = vmbus_driver_register(&hvsock_drv);
+	if (ret) {
+		pr_err("failed to register hv_sock driver\n");
+		goto out;

... and here we goto where we just return. I suggest we bring some consistency by directly returning ret here and eliminating 'out' label.

Quoted 26 lines
net/hv_sock/af_hvsock.c new file +26 -0
+	}
+
+	ret = proto_register(&hvsock_proto, 0);
+	if (ret) {
+		pr_err("failed to register protocol\n");
+		goto unreg_hvsock_drv;
+	}
+
+	ret = sock_register(&hvsock_family_ops);
+	if (ret) {
+		pr_err("failed to register address family\n");
+		goto unreg_proto;
+	}
+
+	return 0;
+
+unreg_proto:
+	proto_unregister(&hvsock_proto);
+unreg_hvsock_drv:
+	vmbus_driver_unregister(&hvsock_drv);
+out:
+	return ret;
+}
+
+static void __exit hvsock_exit(void)
+{

net/hv_sock/af_hvsock.c line 1472

Quoted 10 lines
net/hv_sock/af_hvsock.c new file +10 -0
+	sock_unregister(AF_HYPERV);
+	proto_unregister(&hvsock_proto);
+	vmbus_driver_unregister(&hvsock_drv);
+}
+
+module_init(hvsock_init);
+module_exit(hvsock_exit);
+
+MODULE_DESCRIPTION("Microsoft Hyper-V Virtual Socket Family");
+MODULE_VERSION("0.1");

Do we really need it? When the driver is commited we won't probably be updating it with v0.2 as a whole, we'll be sending patches addressing issues and there always will be a question when to swtich to 0.2, 0.3, ... And we don't have MODULE_VERSION for other Hyper-V drivers.

Quoted 1 line

+MODULE_LICENSE("Dual BSD/GPL");

Signature · 1 line
  Vitaly
author maintainer
#2 lore raw
Quoted 22 lines

From: Vitaly Kuznetsov [mailto:[email protected]]
Sent: Tuesday, January 5, 2016 17:52

Just some minor nitpicks below -- I have to admit I didn't test the feature.

[..skip..]

Quoted 15 lines
net/hv_sock/af_hvsock.c new file +15 -0
+
+	if (sk->sk_err) {
+		ret = -sk->sk_err;
+		goto out_wait_error;
+	} else {
+		ret = 0;
+	}
+
+out_wait:
+	finish_wait(sk_sleep(sk), &wait);
+out:
+	release_sock(sk);
+	return ret;
+
+out_wait_error:
Quoted 8 lines

net/hv_sock/af_hvsock.c line 875

Quoted 4 lines
net/hv_sock/af_hvsock.c new file +4 -0
+	sk->sk_state = SS_UNCONNECTED;
+	sock->state = SS_UNCONNECTED;
+	goto out_wait;
+}

Why not just place out_wait_error label before out_wait (and do 'goto out_wait' in ret = 0 case instead of 'goto out_wait_error' in the error case)?

Good point. I'll update the code.

Quoted 14 lines

[..skip..]

net/hv_sock/af_hvsock.c line 1430

Quoted 9 lines
net/hv_sock/af_hvsock.c new file +9 -0
+
+static int __init hvsock_init(void)
+{
+	int ret;
+
+	/* Hyper-V socket requires at least VMBus 4.0 */
+	if ((vmbus_proto_version >> 16) < 4) {
+		pr_err("failed to load: VMBus 4 or later is required\n");
+		return -ENODEV;

(Let me pretend I'm Dan :-) So here we return ...

Quoted 9 lines

net/hv_sock/af_hvsock.c line 1436

Quoted 6 lines
net/hv_sock/af_hvsock.c new file +6 -0
+	}
+
+	ret = vmbus_driver_register(&hvsock_drv);
+	if (ret) {
+		pr_err("failed to register hv_sock driver\n");
+		goto out;

... and here we goto where we just return. I suggest we bring some consistency by directly returning ret here and eliminating 'out' label.

Thanks! I'll update the code.

Quoted 33 lines
Quoted 33 lines
net/hv_sock/af_hvsock.c new file +33 -0
+	}
+
+	ret = proto_register(&hvsock_proto, 0);
+	if (ret) {
+		pr_err("failed to register protocol\n");
+		goto unreg_hvsock_drv;
+	}
+
+	ret = sock_register(&hvsock_family_ops);
+	if (ret) {
+		pr_err("failed to register address family\n");
+		goto unreg_proto;
+	}
+
+	return 0;
+
+unreg_proto:
+	proto_unregister(&hvsock_proto);
+unreg_hvsock_drv:
+	vmbus_driver_unregister(&hvsock_drv);
+out:
+	return ret;
+}
+
+static void __exit hvsock_exit(void)
+{
+	sock_unregister(AF_HYPERV);
+	proto_unregister(&hvsock_proto);
+	vmbus_driver_unregister(&hvsock_drv);
+}
+
+module_init(hvsock_init);
+module_exit(hvsock_exit);
Quoted 8 lines

net/hv_sock/af_hvsock.c line 1472

Quoted 3 lines
net/hv_sock/af_hvsock.c new file +3 -0
+
+MODULE_DESCRIPTION("Microsoft Hyper-V Virtual Socket Family");
+MODULE_VERSION("0.1");

Do we really need it? When the driver is commited we won't probably be updating it with v0.2 as a whole, we'll be sending patches addressing issues and there always will be a question when to swtich to 0.2, 0.3, ... And we don't have MODULE_VERSION for other Hyper-V drivers.

Good point. I'll remove the line MODULE_VERSION.

Thanks,
-- Dexuan