hv_sock: introduce Hyper-V Sockets
Subsystems (4)
Dexuan Cui
maintainerPATCH v9 1/2 hv_sock: introduce Hyper-V Sockets
Hyper-V Sockets (hv_sock) supplies a byte-stream based communication mechanism between the host and the guest. It's somewhat like TCP over VMBus, but the transportation layer (VMBus) is much simpler than IP.
With Hyper-V Sockets, applications between the host and the guest can talk to each other directly by the traditional BSD-style socket APIs.
Hyper-V Sockets is only available on new Windows hosts, like Windows Server 2016. More info is in this article "Make your own integration services":
https://msdn.microsoft.com/en-us/virtualization/hyperv_on_windows/develop/make_mgmt_service
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 | 16 + include/linux/socket.h | 5 +- include/net/af_hvsock.h | 55 ++ include/uapi/linux/hyperv.h | 25 + net/Kconfig | 1 + net/Makefile | 1 + net/hv_sock/Kconfig | 10 + net/hv_sock/Makefile | 3 + net/hv_sock/af_hvsock.c | 1434 +++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 1550 insertions(+), 2 deletions(-)
MAINTAINERS +2 -0
@@ -5268,7 +5268,9 @@ F: drivers/pci/host/pci-hyperv.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 +16 -0
@@ -1338,4 +1338,20 @@ extern __u32 vmbus_proto_version;int vmbus_send_tl_connect_request (const uuid_le *shv_guest_servie_id,const uuid_le *shv_host_servie_id); +struct vmpipe_proto_header { +u32 pkt_type; +u32 data_size; +}; + +#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 -2
@@ -201,8 +201,8 @@ struct ucred {#define AF_NFC 39/* NFC sockets */ #define AF_VSOCK 40/* vSockets */ #define AF_KCM 41/* Kernel Connection Multiplexor*/ - -#define AF_MAX 42/* For now.. */ +#define AF_HYPERV 42/* Hyper-V Sockets */ +#define AF_MAX 43/* For now.. */ /* Protocol families, same as address families. */ #define PF_UNSPEC AF_UNSPEC
@@ -249,6 +249,7 @@ struct ucred {#define PF_NFC AF_NFC#define PF_VSOCK AF_VSOCK#define PF_KCM AF_KCM +#define PF_HYPERV AF_HYPERV#define PF_MAX AF_MAX/* Maximum queue length specifiable by listen. */
include/net/af_hvsock.h new file +55 -0
55 lines not on this page · raw message
include/uapi/linux/hyperv.h +25 -0
@@ -26,6 +26,7 @@#define _UAPI_HYPERV_H#include <linux/uuid.h> +#include <linux/socket.h> /* * Framework version for util services.
@@ -396,4 +397,28 @@ struct hv_kvp_ip_msg {struct hv_kvp_ipaddr_value kvp_ip_val; } __attribute__((packed)); +/* +* This is the address fromat of Hyper-V Sockets. +* Note: here we just borrow the kernel's built-in type uuid_le. When +* an application calls bind() or connect(), the 2 members of struct +* sockaddr_hv must be of GUID. +* The GUID format differs from the UUID format only in the byte order of +* the first 3 fields. Refer to: +* https://en.wikipedia.org/wiki/Globally_unique_identifier +*/ +#define guid_t uuid_le +struct sockaddr_hv { +__kernel_sa_family_t shv_family;/* Address family */ +__le16 reserved;/* Must be Zero */ +guid_t shv_vm_id;/* VM ID */ +guid_t 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
@@ -231,6 +231,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
@@ -70,6 +70,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
@@ -0,0 +1,10 @@ +config HYPERV_SOCK + tristate "Hyper-V Sockets" + depends on HYPERV + default m if HYPERV + help + Hyper-V Sockets is somewhat like TCP over VMBus, allowing + communication between Linux guest and Hyper-V host without TCP/IP. + + 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
@@ -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 +1434 -0
1,434 lines not on this page · raw message
Signature · 1 line
2.1.0
David Miller
maintainerFrom: Dexuan Cui <[email protected]>
Date: Wed, 4 May 2016 09:56:57 -0700
Quoted 4 lines
include/net/af_hvsock.h new file +4
-0+#define VMBUS_RINGBUFFER_SIZE_HVSOCK_RECV (5 * PAGE_SIZE) +#define VMBUS_RINGBUFFER_SIZE_HVSOCK_SEND (5 * PAGE_SIZE) + +#define HVSOCK_RCV_BUF_SZ VMBUS_RINGBUFFER_SIZE_HVSOCK_RECV
...
Quoted 1 line
+struct
hvsock_sock{
...
Quoted 5 lines
include/net/af_hvsock.h new file +5
-0+/* The 'hdr' and 'buf' in the below 'send' and 'recv' definitions must +* be consecutive: see hvsock_send_data() and hvsock_recv_data(). +*/ +struct { +struct vmpipe_proto_header hdr;
include/net/af_hvsock.h line 52
Quoted 10 lines
include/net/af_hvsock.h new file +10
-0+u8 buf[HVSOCK_SND_BUF_SZ ]; + } send; + +struct { +struct vmpipe_proto_header hdr; +u8 buf[HVSOCK_RCV_BUF_SZ ]; + +unsigned int data_len; +unsigned int data_offset; + } recv;
I don't think allocating 5 pages of unswappable memory for every Hyper-V socket created is reasonable.
Dexuan Cui
maintainerQuoted 32 lines
From: David Miller [mailto:[email protected]]
Sent: Saturday, May 7, 2016 1:04
To: Dexuan Cui <[email protected]>
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; KY Srinivasan <[email protected]>; Haiyang Zhang <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH v9 net-next 1/2]hv_sock: introduce Hyper-V SocketsFrom: Dexuan Cui <[email protected]>
Date: Wed, 4 May 2016 09:56:57 -0700include/net/af_hvsock.h
Quoted 4 lines
include/net/af_hvsock.h new file +4
-0+#define VMBUS_RINGBUFFER_SIZE_HVSOCK_RECV (5 * PAGE_SIZE) +#define VMBUS_RINGBUFFER_SIZE_HVSOCK_SEND (5 * PAGE_SIZE) + +#define HVSOCK_RCV_BUF_SZ
VMBUS_RINGBUFFER_SIZE_HVSOCK_RECV
...Quoted 1 line
+struct
hvsock_sock{...
Quoted 11 lines
include/net/af_hvsock.h new file +11
-0+/* The 'hdr' and 'buf' in the below 'send' and 'recv' definitions must +* be consecutive: see hvsock_send_data() and hvsock_recv_data(). +*/ +struct { +struct vmpipe_proto_header hdr; +u8 buf[HVSOCK_SND_BUF_SZ ]; + } send; + +struct { +struct vmpipe_proto_header hdr; +u8 buf[HVSOCK_RCV_BUF_SZ ];
Quoted 8 lines
include/net/af_hvsock.h line 52
Quoted 4 lines
include/net/af_hvsock.h new file +4
-0+ +unsigned int data_len; +unsigned int data_offset; + } recv;I don't think allocating 5 pages of unswappable memory for every Hyper-V socket
created is reasonable.
Thanks for the comment, David!
I should be able to make 'send', 'recv' here to pointers and use vmalloc() to allocate the memory for them. I will do this.
Thanks,
-- Dexuan
David Miller
maintainerFrom: Dexuan Cui <[email protected]>
Date: Sat, 7 May 2016 10:49:25 +0000
Quoted 2 lines
I should be able to make 'send', 'recv' here to pointers and use
vmalloc()to allocate the memory for them. I will do this.
That's still unswappable kernel memory.
People can open N sockets, where N is something on the order of the FD limit the process has, per process. This allows someone to quickly eat up a lot of memory and hold onto it nearly indefinitely.
Dexuan Cui
maintainerQuoted 10 lines
From: David Miller [mailto:[email protected]]
Sent: Sunday, May 8, 2016 1:41
To: Dexuan Cui <[email protected]>
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; KY Srinivasan <[email protected]>; Haiyang Zhang <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH v9 net-next 1/2]hv_sock: introduce Hyper-V Sockets
Quoted 7 lines
From: Dexuan Cui <[email protected]>
Date: Sat, 7 May 2016 10:49:25 +0000Quoted 2 lines
I should be able to make 'send', 'recv' here to pointers and use
vmalloc()to allocate the memory for them. I will do this.That's still unswappable kernel memory.
Hi David,
My understanding is: kernel pages are not swappable in Linux, so it looks I can't avoid unswappable kernel memory here?
Quoted 3 lines
People can open N sockets, where N is something on the order of the FD limit the process has, per process. This allows someone to quickly eat up a lot of memory and hold onto it nearly indefinitely.
Thanks for pointing this out!
I understand, so I think I should add a module parameter, e.g., "hv_sock.max_socket_number" with a default value, say, 1024?
1 established hv_sock connection takes less than 20 pages, including 10 pages for VMBus ringbuffer, 6 pages for send/recv buffers(I'll use vmalloc() for this), etc.
Here the recv buf needs a size of 5 pages because potentially the host can send the guest a VMBus packet with an up-to-5-page payload, i..e, the VMBus inbound ringbuffer size.
1024 hv_sock connections take less than 20*4KB * 1K = 80MB memory.
A user who needs more connections can change the module parameter without reboot.
hv_sock connection is designed to work only between the host and the guest. I think 1024 connections seem pretty enough.
BTW, a user can't create hv_sock connections without enough privilege. Please see
+static int hvsock_create(struct net *net, struct socket *sock,
+ int protocol, int kern)
+{
+ if (!capable(CAP_SYS_ADMIN) && !capable(CAP_NET_ADMIN))
+ return -EPERM;
David, does this make sense to you?
Thanks,
-- Dexuan
David Miller
maintainerFrom: Dexuan Cui <[email protected]>
Date: Sun, 8 May 2016 06:11:04 +0000
Quoted 3 lines
Thanks for pointing this out!
I understand, so I think I should add a module parameter, e.g., "hv_sock.max_socket_number" with a default value, say, 1024?
No, you should get rid of the huge multi-page buffers.
Dexuan Cui
maintainerQuoted 10 lines
From: David Miller [mailto:[email protected]]
Sent: Monday, May 9, 2016 1:45
To: Dexuan Cui <[email protected]>
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; KY Srinivasan <[email protected]>; Haiyang Zhang <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH v9 net-next 1/2]hv_sock: introduce Hyper-V Sockets
Quoted 8 lines
From: Dexuan Cui <[email protected]>
Date: Sun, 8 May 2016 06:11:04 +0000Quoted 3 lines
Thanks for pointing this out!
I understand, so I think I should add a module parameter, e.g., "hv_sock.max_socket_number" with a default value, say, 1024?No, you should get rid of the huge multi-page buffers.
Hi David,
Ok, how do you like the below proof-of-concept patch snippet?
I use 1 page for the recv buf and another page for send buf. They should be allocated by kmalloc(sizeof(struct hvsock_send/recv_buf), GFP_KERNEL).
And, by default, I use 2 pages for VMBUS send/recv ringbuffers respectively. (Note: 2 is the minimal ringbuffer size because actually 1 page of the two is used as the shared read/write index etc, rather than data)
A module parameter will be added to allow the user to use a big ringbuffer size, if the user cares too much about the performance.
Another parameter will be added to limit how many hvsock sockets can be created at most. The default value can be 1024, meaning at most 1024 * (2+2+1+1) * 4KB = 24MB memory is used.
-#define VMBUS_RINGBUFFER_SIZE_HVSOCK_RECV (5 * PAGE_SIZE) -#define VMBUS_RINGBUFFER_SIZE_HVSOCK_SEND (5 * PAGE_SIZE) +#define VMBUS_RINGBUFFER_SIZE_HVSOCK_RECV (2 * PAGE_SIZE) +#define VMBUS_RINGBUFFER_SIZE_HVSOCK_SEND (2 * PAGE_SIZE)
-#define HVSOCK_RCV_BUF_SZ VMBUS_RINGBUFFER_SIZE_HVSOCK_RECV +#define HVSOCK_RCV_BUF_SZ PAGE_SIZE #define HVSOCK_SND_BUF_SZ PAGE_SIZE
+struct hvsock_send_buf {
+ struct vmpipe_proto_header hdr;
+ u8 buf[HVSOCK_SND_BUF_SZ];
+};
+
+struct hvsock_recv_buf {
+ struct vmpipe_proto_header hdr;
+ u8 buf[HVSOCK_RCV_BUF_SZ];
+
+ unsigned int data_len;
+ unsigned int data_offset;
+};
+
@@ -35,21 +48,8 @@ struct hvsock_sock {
struct vmbus_channel *channel;
- struct {
- struct vmpipe_proto_header hdr;
- u8 buf[HVSOCK_SND_BUF_SZ];
- } send;
-
- struct {
- struct vmpipe_proto_header hdr;
- u8 buf[HVSOCK_RCV_BUF_SZ];
-
- unsigned int data_len;
- unsigned int data_offset;
- } recv; + struct hvsock_send_buf *send_buf; + struct hvsock_recv_buf *recv_buf;
};
Thanks,
-- Dexuan