Linux Audio

Check our new training course

Loading...
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Hyper-V transport for vsock
  4 *
  5 * Hyper-V Sockets supplies a byte-stream based communication mechanism
  6 * between the host and the VM. This driver implements the necessary
  7 * support in the VM by introducing the new vsock transport.
  8 *
  9 * Copyright (c) 2017, Microsoft Corporation.
 10 */
 11#include <linux/module.h>
 12#include <linux/vmalloc.h>
 13#include <linux/hyperv.h>
 14#include <net/sock.h>
 15#include <net/af_vsock.h>
 16#include <asm/hyperv-tlfs.h>
 17
 18/* Older (VMBUS version 'VERSION_WIN10' or before) Windows hosts have some
 19 * stricter requirements on the hv_sock ring buffer size of six 4K pages.
 20 * hyperv-tlfs defines HV_HYP_PAGE_SIZE as 4K. Newer hosts don't have this
 21 * limitation; but, keep the defaults the same for compat.
 22 */
 23#define RINGBUFFER_HVS_RCV_SIZE (HV_HYP_PAGE_SIZE * 6)
 24#define RINGBUFFER_HVS_SND_SIZE (HV_HYP_PAGE_SIZE * 6)
 25#define RINGBUFFER_HVS_MAX_SIZE (HV_HYP_PAGE_SIZE * 64)
 26
 27/* The MTU is 16KB per the host side's design */
 28#define HVS_MTU_SIZE		(1024 * 16)
 29
 30/* How long to wait for graceful shutdown of a connection */
 31#define HVS_CLOSE_TIMEOUT (8 * HZ)
 32
 33struct vmpipe_proto_header {
 34	u32 pkt_type;
 35	u32 data_size;
 36};
 37
 38/* For recv, we use the VMBus in-place packet iterator APIs to directly copy
 39 * data from the ringbuffer into the userspace buffer.
 40 */
 41struct hvs_recv_buf {
 42	/* The header before the payload data */
 43	struct vmpipe_proto_header hdr;
 44
 45	/* The payload */
 46	u8 data[HVS_MTU_SIZE];
 47};
 48
 49/* We can send up to HVS_MTU_SIZE bytes of payload to the host, but let's use
 50 * a smaller size, i.e. HVS_SEND_BUF_SIZE, to maximize concurrency between the
 51 * guest and the host processing as one VMBUS packet is the smallest processing
 52 * unit.
 53 *
 54 * Note: the buffer can be eliminated in the future when we add new VMBus
 55 * ringbuffer APIs that allow us to directly copy data from userspace buffer
 56 * to VMBus ringbuffer.
 57 */
 58#define HVS_SEND_BUF_SIZE \
 59		(HV_HYP_PAGE_SIZE - sizeof(struct vmpipe_proto_header))
 60
 61struct hvs_send_buf {
 62	/* The header before the payload data */
 63	struct vmpipe_proto_header hdr;
 64
 65	/* The payload */
 66	u8 data[HVS_SEND_BUF_SIZE];
 67};
 68
 69#define HVS_HEADER_LEN	(sizeof(struct vmpacket_descriptor) + \
 70			 sizeof(struct vmpipe_proto_header))
 71
 72/* See 'prev_indices' in hv_ringbuffer_read(), hv_ringbuffer_write(), and
 73 * __hv_pkt_iter_next().
 74 */
 75#define VMBUS_PKT_TRAILER_SIZE	(sizeof(u64))
 76
 77#define HVS_PKT_LEN(payload_len)	(HVS_HEADER_LEN + \
 78					 ALIGN((payload_len), 8) + \
 79					 VMBUS_PKT_TRAILER_SIZE)
 80
 81union hvs_service_id {
 82	guid_t	srv_id;
 83
 84	struct {
 85		unsigned int svm_port;
 86		unsigned char b[sizeof(guid_t) - sizeof(unsigned int)];
 87	};
 88};
 89
 90/* Per-socket state (accessed via vsk->trans) */
 91struct hvsock {
 92	struct vsock_sock *vsk;
 93
 94	guid_t vm_srv_id;
 95	guid_t host_srv_id;
 96
 97	struct vmbus_channel *chan;
 98	struct vmpacket_descriptor *recv_desc;
 99
100	/* The length of the payload not delivered to userland yet */
101	u32 recv_data_len;
102	/* The offset of the payload */
103	u32 recv_data_off;
104
105	/* Have we sent the zero-length packet (FIN)? */
106	bool fin_sent;
107};
108
109/* In the VM, we support Hyper-V Sockets with AF_VSOCK, and the endpoint is
110 * <cid, port> (see struct sockaddr_vm). Note: cid is not really used here:
111 * when we write apps to connect to the host, we can only use VMADDR_CID_ANY
112 * or VMADDR_CID_HOST (both are equivalent) as the remote cid, and when we
113 * write apps to bind() & listen() in the VM, we can only use VMADDR_CID_ANY
114 * as the local cid.
115 *
116 * On the host, Hyper-V Sockets are supported by Winsock AF_HYPERV:
117 * https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/user-
118 * guide/make-integration-service, and the endpoint is <VmID, ServiceId> with
119 * the below sockaddr:
120 *
121 * struct SOCKADDR_HV
122 * {
123 *    ADDRESS_FAMILY Family;
124 *    USHORT Reserved;
125 *    GUID VmId;
126 *    GUID ServiceId;
127 * };
128 * Note: VmID is not used by Linux VM and actually it isn't transmitted via
129 * VMBus, because here it's obvious the host and the VM can easily identify
130 * each other. Though the VmID is useful on the host, especially in the case
131 * of Windows container, Linux VM doesn't need it at all.
132 *
133 * To make use of the AF_VSOCK infrastructure in Linux VM, we have to limit
134 * the available GUID space of SOCKADDR_HV so that we can create a mapping
135 * between AF_VSOCK port and SOCKADDR_HV Service GUID. The rule of writing
136 * Hyper-V Sockets apps on the host and in Linux VM is:
137 *
138 ****************************************************************************
139 * The only valid Service GUIDs, from the perspectives of both the host and *
140 * Linux VM, that can be connected by the other end, must conform to this   *
141 * format: <port>-facb-11e6-bd58-64006a7986d3.                              *
142 ****************************************************************************
143 *
144 * When we write apps on the host to connect(), the GUID ServiceID is used.
145 * When we write apps in Linux VM to connect(), we only need to specify the
146 * port and the driver will form the GUID and use that to request the host.
147 *
148 */
149
150/* 00000000-facb-11e6-bd58-64006a7986d3 */
151static const guid_t srv_id_template =
152	GUID_INIT(0x00000000, 0xfacb, 0x11e6, 0xbd, 0x58,
153		  0x64, 0x00, 0x6a, 0x79, 0x86, 0xd3);
154
155static bool hvs_check_transport(struct vsock_sock *vsk);
156
157static bool is_valid_srv_id(const guid_t *id)
158{
159	return !memcmp(&id->b[4], &srv_id_template.b[4], sizeof(guid_t) - 4);
160}
161
162static unsigned int get_port_by_srv_id(const guid_t *svr_id)
163{
164	return *((unsigned int *)svr_id);
165}
166
167static void hvs_addr_init(struct sockaddr_vm *addr, const guid_t *svr_id)
168{
169	unsigned int port = get_port_by_srv_id(svr_id);
170
171	vsock_addr_init(addr, VMADDR_CID_ANY, port);
172}
173
174static void hvs_set_channel_pending_send_size(struct vmbus_channel *chan)
175{
176	set_channel_pending_send_size(chan,
177				      HVS_PKT_LEN(HVS_SEND_BUF_SIZE));
178
179	virt_mb();
180}
181
182static bool hvs_channel_readable(struct vmbus_channel *chan)
183{
184	u32 readable = hv_get_bytes_to_read(&chan->inbound);
185
186	/* 0-size payload means FIN */
187	return readable >= HVS_PKT_LEN(0);
188}
189
190static int hvs_channel_readable_payload(struct vmbus_channel *chan)
191{
192	u32 readable = hv_get_bytes_to_read(&chan->inbound);
193
194	if (readable > HVS_PKT_LEN(0)) {
195		/* At least we have 1 byte to read. We don't need to return
196		 * the exact readable bytes: see vsock_stream_recvmsg() ->
197		 * vsock_stream_has_data().
198		 */
199		return 1;
200	}
201
202	if (readable == HVS_PKT_LEN(0)) {
203		/* 0-size payload means FIN */
204		return 0;
205	}
206
207	/* No payload or FIN */
208	return -1;
209}
210
211static size_t hvs_channel_writable_bytes(struct vmbus_channel *chan)
212{
213	u32 writeable = hv_get_bytes_to_write(&chan->outbound);
214	size_t ret;
215
216	/* The ringbuffer mustn't be 100% full, and we should reserve a
217	 * zero-length-payload packet for the FIN: see hv_ringbuffer_write()
218	 * and hvs_shutdown().
219	 */
220	if (writeable <= HVS_PKT_LEN(1) + HVS_PKT_LEN(0))
221		return 0;
222
223	ret = writeable - HVS_PKT_LEN(1) - HVS_PKT_LEN(0);
224
225	return round_down(ret, 8);
226}
227
228static int hvs_send_data(struct vmbus_channel *chan,
229			 struct hvs_send_buf *send_buf, size_t to_write)
230{
231	send_buf->hdr.pkt_type = 1;
232	send_buf->hdr.data_size = to_write;
233	return vmbus_sendpacket(chan, &send_buf->hdr,
234				sizeof(send_buf->hdr) + to_write,
235				0, VM_PKT_DATA_INBAND, 0);
236}
237
238static void hvs_channel_cb(void *ctx)
239{
240	struct sock *sk = (struct sock *)ctx;
241	struct vsock_sock *vsk = vsock_sk(sk);
242	struct hvsock *hvs = vsk->trans;
243	struct vmbus_channel *chan = hvs->chan;
244
245	if (hvs_channel_readable(chan))
246		sk->sk_data_ready(sk);
247
248	if (hv_get_bytes_to_write(&chan->outbound) > 0)
249		sk->sk_write_space(sk);
250}
251
252static void hvs_do_close_lock_held(struct vsock_sock *vsk,
253				   bool cancel_timeout)
254{
255	struct sock *sk = sk_vsock(vsk);
256
257	sock_set_flag(sk, SOCK_DONE);
258	vsk->peer_shutdown = SHUTDOWN_MASK;
259	if (vsock_stream_has_data(vsk) <= 0)
260		sk->sk_state = TCP_CLOSING;
261	sk->sk_state_change(sk);
262	if (vsk->close_work_scheduled &&
263	    (!cancel_timeout || cancel_delayed_work(&vsk->close_work))) {
264		vsk->close_work_scheduled = false;
265		vsock_remove_sock(vsk);
266
267		/* Release the reference taken while scheduling the timeout */
268		sock_put(sk);
269	}
270}
271
272static void hvs_close_connection(struct vmbus_channel *chan)
273{
274	struct sock *sk = get_per_channel_state(chan);
275
276	lock_sock(sk);
277	hvs_do_close_lock_held(vsock_sk(sk), true);
278	release_sock(sk);
279
280	/* Release the refcnt for the channel that's opened in
281	 * hvs_open_connection().
282	 */
283	sock_put(sk);
284}
285
286static void hvs_open_connection(struct vmbus_channel *chan)
287{
288	guid_t *if_instance, *if_type;
289	unsigned char conn_from_host;
290
291	struct sockaddr_vm addr;
292	struct sock *sk, *new = NULL;
293	struct vsock_sock *vnew = NULL;
294	struct hvsock *hvs = NULL;
295	struct hvsock *hvs_new = NULL;
296	int rcvbuf;
297	int ret;
298	int sndbuf;
299
300	if_type = &chan->offermsg.offer.if_type;
301	if_instance = &chan->offermsg.offer.if_instance;
302	conn_from_host = chan->offermsg.offer.u.pipe.user_def[0];
303	if (!is_valid_srv_id(if_type))
304		return;
305
306	hvs_addr_init(&addr, conn_from_host ? if_type : if_instance);
307	sk = vsock_find_bound_socket(&addr);
308	if (!sk)
309		return;
310
311	lock_sock(sk);
312	if ((conn_from_host && sk->sk_state != TCP_LISTEN) ||
313	    (!conn_from_host && sk->sk_state != TCP_SYN_SENT))
314		goto out;
315
316	if (conn_from_host) {
317		if (sk->sk_ack_backlog >= sk->sk_max_ack_backlog)
318			goto out;
319
320		new = vsock_create_connected(sk);
321		if (!new)
322			goto out;
323
324		new->sk_state = TCP_SYN_SENT;
325		vnew = vsock_sk(new);
326
327		hvs_addr_init(&vnew->local_addr, if_type);
328
329		/* Remote peer is always the host */
330		vsock_addr_init(&vnew->remote_addr,
331				VMADDR_CID_HOST, VMADDR_PORT_ANY);
332		vnew->remote_addr.svm_port = get_port_by_srv_id(if_instance);
333		ret = vsock_assign_transport(vnew, vsock_sk(sk));
334		/* Transport assigned (looking at remote_addr) must be the
335		 * same where we received the request.
336		 */
337		if (ret || !hvs_check_transport(vnew)) {
338			sock_put(new);
339			goto out;
340		}
341		hvs_new = vnew->trans;
342		hvs_new->chan = chan;
343	} else {
344		hvs = vsock_sk(sk)->trans;
345		hvs->chan = chan;
346	}
347
348	set_channel_read_mode(chan, HV_CALL_DIRECT);
349
350	/* Use the socket buffer sizes as hints for the VMBUS ring size. For
351	 * server side sockets, 'sk' is the parent socket and thus, this will
352	 * allow the child sockets to inherit the size from the parent. Keep
353	 * the mins to the default value and align to page size as per VMBUS
354	 * requirements.
355	 * For the max, the socket core library will limit the socket buffer
356	 * size that can be set by the user, but, since currently, the hv_sock
357	 * VMBUS ring buffer is physically contiguous allocation, restrict it
358	 * further.
359	 * Older versions of hv_sock host side code cannot handle bigger VMBUS
360	 * ring buffer size. Use the version number to limit the change to newer
361	 * versions.
362	 */
363	if (vmbus_proto_version < VERSION_WIN10_V5) {
364		sndbuf = RINGBUFFER_HVS_SND_SIZE;
365		rcvbuf = RINGBUFFER_HVS_RCV_SIZE;
366	} else {
367		sndbuf = max_t(int, sk->sk_sndbuf, RINGBUFFER_HVS_SND_SIZE);
368		sndbuf = min_t(int, sndbuf, RINGBUFFER_HVS_MAX_SIZE);
369		sndbuf = ALIGN(sndbuf, HV_HYP_PAGE_SIZE);
370		rcvbuf = max_t(int, sk->sk_rcvbuf, RINGBUFFER_HVS_RCV_SIZE);
371		rcvbuf = min_t(int, rcvbuf, RINGBUFFER_HVS_MAX_SIZE);
372		rcvbuf = ALIGN(rcvbuf, HV_HYP_PAGE_SIZE);
373	}
374
375	ret = vmbus_open(chan, sndbuf, rcvbuf, NULL, 0, hvs_channel_cb,
376			 conn_from_host ? new : sk);
377	if (ret != 0) {
378		if (conn_from_host) {
379			hvs_new->chan = NULL;
380			sock_put(new);
381		} else {
382			hvs->chan = NULL;
383		}
384		goto out;
385	}
386
387	set_per_channel_state(chan, conn_from_host ? new : sk);
388
389	/* This reference will be dropped by hvs_close_connection(). */
390	sock_hold(conn_from_host ? new : sk);
391	vmbus_set_chn_rescind_callback(chan, hvs_close_connection);
392
393	/* Set the pending send size to max packet size to always get
394	 * notifications from the host when there is enough writable space.
395	 * The host is optimized to send notifications only when the pending
396	 * size boundary is crossed, and not always.
397	 */
398	hvs_set_channel_pending_send_size(chan);
399
400	if (conn_from_host) {
401		new->sk_state = TCP_ESTABLISHED;
402		sk_acceptq_added(sk);
403
404		hvs_new->vm_srv_id = *if_type;
405		hvs_new->host_srv_id = *if_instance;
406
407		vsock_insert_connected(vnew);
408
409		vsock_enqueue_accept(sk, new);
410	} else {
411		sk->sk_state = TCP_ESTABLISHED;
412		sk->sk_socket->state = SS_CONNECTED;
413
414		vsock_insert_connected(vsock_sk(sk));
415	}
416
417	sk->sk_state_change(sk);
418
419out:
420	/* Release refcnt obtained when we called vsock_find_bound_socket() */
421	sock_put(sk);
422
423	release_sock(sk);
424}
425
426static u32 hvs_get_local_cid(void)
427{
428	return VMADDR_CID_ANY;
429}
430
431static int hvs_sock_init(struct vsock_sock *vsk, struct vsock_sock *psk)
432{
433	struct hvsock *hvs;
434	struct sock *sk = sk_vsock(vsk);
435
436	hvs = kzalloc(sizeof(*hvs), GFP_KERNEL);
437	if (!hvs)
438		return -ENOMEM;
439
440	vsk->trans = hvs;
441	hvs->vsk = vsk;
442	sk->sk_sndbuf = RINGBUFFER_HVS_SND_SIZE;
443	sk->sk_rcvbuf = RINGBUFFER_HVS_RCV_SIZE;
444	return 0;
445}
446
447static int hvs_connect(struct vsock_sock *vsk)
448{
449	union hvs_service_id vm, host;
450	struct hvsock *h = vsk->trans;
451
452	vm.srv_id = srv_id_template;
453	vm.svm_port = vsk->local_addr.svm_port;
454	h->vm_srv_id = vm.srv_id;
455
456	host.srv_id = srv_id_template;
457	host.svm_port = vsk->remote_addr.svm_port;
458	h->host_srv_id = host.srv_id;
459
460	return vmbus_send_tl_connect_request(&h->vm_srv_id, &h->host_srv_id);
461}
462
463static void hvs_shutdown_lock_held(struct hvsock *hvs, int mode)
464{
465	struct vmpipe_proto_header hdr;
466
467	if (hvs->fin_sent || !hvs->chan)
468		return;
469
470	/* It can't fail: see hvs_channel_writable_bytes(). */
471	(void)hvs_send_data(hvs->chan, (struct hvs_send_buf *)&hdr, 0);
472	hvs->fin_sent = true;
473}
474
475static int hvs_shutdown(struct vsock_sock *vsk, int mode)
476{
 
 
477	if (!(mode & SEND_SHUTDOWN))
478		return 0;
479
 
480	hvs_shutdown_lock_held(vsk->trans, mode);
 
481	return 0;
482}
483
484static void hvs_close_timeout(struct work_struct *work)
485{
486	struct vsock_sock *vsk =
487		container_of(work, struct vsock_sock, close_work.work);
488	struct sock *sk = sk_vsock(vsk);
489
490	sock_hold(sk);
491	lock_sock(sk);
492	if (!sock_flag(sk, SOCK_DONE))
493		hvs_do_close_lock_held(vsk, false);
494
495	vsk->close_work_scheduled = false;
496	release_sock(sk);
497	sock_put(sk);
498}
499
500/* Returns true, if it is safe to remove socket; false otherwise */
501static bool hvs_close_lock_held(struct vsock_sock *vsk)
502{
503	struct sock *sk = sk_vsock(vsk);
504
505	if (!(sk->sk_state == TCP_ESTABLISHED ||
506	      sk->sk_state == TCP_CLOSING))
507		return true;
508
509	if ((sk->sk_shutdown & SHUTDOWN_MASK) != SHUTDOWN_MASK)
510		hvs_shutdown_lock_held(vsk->trans, SHUTDOWN_MASK);
511
512	if (sock_flag(sk, SOCK_DONE))
513		return true;
514
515	/* This reference will be dropped by the delayed close routine */
516	sock_hold(sk);
517	INIT_DELAYED_WORK(&vsk->close_work, hvs_close_timeout);
518	vsk->close_work_scheduled = true;
519	schedule_delayed_work(&vsk->close_work, HVS_CLOSE_TIMEOUT);
520	return false;
521}
522
523static void hvs_release(struct vsock_sock *vsk)
524{
525	bool remove_sock;
526
527	remove_sock = hvs_close_lock_held(vsk);
528	if (remove_sock)
529		vsock_remove_sock(vsk);
530}
531
532static void hvs_destruct(struct vsock_sock *vsk)
533{
534	struct hvsock *hvs = vsk->trans;
535	struct vmbus_channel *chan = hvs->chan;
536
537	if (chan)
538		vmbus_hvsock_device_unregister(chan);
539
540	kfree(hvs);
541}
542
543static int hvs_dgram_bind(struct vsock_sock *vsk, struct sockaddr_vm *addr)
544{
545	return -EOPNOTSUPP;
546}
547
548static int hvs_dgram_dequeue(struct vsock_sock *vsk, struct msghdr *msg,
549			     size_t len, int flags)
550{
551	return -EOPNOTSUPP;
552}
553
554static int hvs_dgram_enqueue(struct vsock_sock *vsk,
555			     struct sockaddr_vm *remote, struct msghdr *msg,
556			     size_t dgram_len)
557{
558	return -EOPNOTSUPP;
559}
560
561static bool hvs_dgram_allow(u32 cid, u32 port)
562{
563	return false;
564}
565
566static int hvs_update_recv_data(struct hvsock *hvs)
567{
568	struct hvs_recv_buf *recv_buf;
569	u32 payload_len;
570
571	recv_buf = (struct hvs_recv_buf *)(hvs->recv_desc + 1);
572	payload_len = recv_buf->hdr.data_size;
573
574	if (payload_len > HVS_MTU_SIZE)
575		return -EIO;
576
577	if (payload_len == 0)
578		hvs->vsk->peer_shutdown |= SEND_SHUTDOWN;
579
580	hvs->recv_data_len = payload_len;
581	hvs->recv_data_off = 0;
582
583	return 0;
584}
585
586static ssize_t hvs_stream_dequeue(struct vsock_sock *vsk, struct msghdr *msg,
587				  size_t len, int flags)
588{
589	struct hvsock *hvs = vsk->trans;
590	bool need_refill = !hvs->recv_desc;
591	struct hvs_recv_buf *recv_buf;
592	u32 to_read;
593	int ret;
594
595	if (flags & MSG_PEEK)
596		return -EOPNOTSUPP;
597
598	if (need_refill) {
599		hvs->recv_desc = hv_pkt_iter_first_raw(hvs->chan);
600		ret = hvs_update_recv_data(hvs);
601		if (ret)
602			return ret;
603	}
604
605	recv_buf = (struct hvs_recv_buf *)(hvs->recv_desc + 1);
606	to_read = min_t(u32, len, hvs->recv_data_len);
607	ret = memcpy_to_msg(msg, recv_buf->data + hvs->recv_data_off, to_read);
608	if (ret != 0)
609		return ret;
610
611	hvs->recv_data_len -= to_read;
612	if (hvs->recv_data_len == 0) {
613		hvs->recv_desc = hv_pkt_iter_next_raw(hvs->chan, hvs->recv_desc);
614		if (hvs->recv_desc) {
615			ret = hvs_update_recv_data(hvs);
616			if (ret)
617				return ret;
618		}
619	} else {
620		hvs->recv_data_off += to_read;
621	}
622
623	return to_read;
624}
625
626static ssize_t hvs_stream_enqueue(struct vsock_sock *vsk, struct msghdr *msg,
627				  size_t len)
628{
629	struct hvsock *hvs = vsk->trans;
630	struct vmbus_channel *chan = hvs->chan;
631	struct hvs_send_buf *send_buf;
632	ssize_t to_write, max_writable;
633	ssize_t ret = 0;
634	ssize_t bytes_written = 0;
635
636	BUILD_BUG_ON(sizeof(*send_buf) != HV_HYP_PAGE_SIZE);
637
638	send_buf = kmalloc(sizeof(*send_buf), GFP_KERNEL);
639	if (!send_buf)
640		return -ENOMEM;
641
642	/* Reader(s) could be draining data from the channel as we write.
643	 * Maximize bandwidth, by iterating until the channel is found to be
644	 * full.
645	 */
646	while (len) {
647		max_writable = hvs_channel_writable_bytes(chan);
648		if (!max_writable)
649			break;
650		to_write = min_t(ssize_t, len, max_writable);
651		to_write = min_t(ssize_t, to_write, HVS_SEND_BUF_SIZE);
652		/* memcpy_from_msg is safe for loop as it advances the offsets
653		 * within the message iterator.
654		 */
655		ret = memcpy_from_msg(send_buf->data, msg, to_write);
656		if (ret < 0)
657			goto out;
658
659		ret = hvs_send_data(hvs->chan, send_buf, to_write);
660		if (ret < 0)
661			goto out;
662
663		bytes_written += to_write;
664		len -= to_write;
665	}
666out:
667	/* If any data has been sent, return that */
668	if (bytes_written)
669		ret = bytes_written;
670	kfree(send_buf);
671	return ret;
672}
673
674static s64 hvs_stream_has_data(struct vsock_sock *vsk)
675{
676	struct hvsock *hvs = vsk->trans;
677	s64 ret;
678
679	if (hvs->recv_data_len > 0)
680		return 1;
681
682	switch (hvs_channel_readable_payload(hvs->chan)) {
683	case 1:
684		ret = 1;
685		break;
686	case 0:
687		vsk->peer_shutdown |= SEND_SHUTDOWN;
688		ret = 0;
689		break;
690	default: /* -1 */
691		ret = 0;
692		break;
693	}
694
695	return ret;
696}
697
698static s64 hvs_stream_has_space(struct vsock_sock *vsk)
699{
700	struct hvsock *hvs = vsk->trans;
701
702	return hvs_channel_writable_bytes(hvs->chan);
703}
704
705static u64 hvs_stream_rcvhiwat(struct vsock_sock *vsk)
706{
707	return HVS_MTU_SIZE + 1;
708}
709
710static bool hvs_stream_is_active(struct vsock_sock *vsk)
711{
712	struct hvsock *hvs = vsk->trans;
713
714	return hvs->chan != NULL;
715}
716
717static bool hvs_stream_allow(u32 cid, u32 port)
718{
719	if (cid == VMADDR_CID_HOST)
720		return true;
721
722	return false;
723}
724
725static
726int hvs_notify_poll_in(struct vsock_sock *vsk, size_t target, bool *readable)
727{
728	struct hvsock *hvs = vsk->trans;
729
730	*readable = hvs_channel_readable(hvs->chan);
731	return 0;
732}
733
734static
735int hvs_notify_poll_out(struct vsock_sock *vsk, size_t target, bool *writable)
736{
737	*writable = hvs_stream_has_space(vsk) > 0;
738
739	return 0;
740}
741
742static
743int hvs_notify_recv_init(struct vsock_sock *vsk, size_t target,
744			 struct vsock_transport_recv_notify_data *d)
745{
746	return 0;
747}
748
749static
750int hvs_notify_recv_pre_block(struct vsock_sock *vsk, size_t target,
751			      struct vsock_transport_recv_notify_data *d)
752{
753	return 0;
754}
755
756static
757int hvs_notify_recv_pre_dequeue(struct vsock_sock *vsk, size_t target,
758				struct vsock_transport_recv_notify_data *d)
759{
760	return 0;
761}
762
763static
764int hvs_notify_recv_post_dequeue(struct vsock_sock *vsk, size_t target,
765				 ssize_t copied, bool data_read,
766				 struct vsock_transport_recv_notify_data *d)
767{
768	return 0;
769}
770
771static
772int hvs_notify_send_init(struct vsock_sock *vsk,
773			 struct vsock_transport_send_notify_data *d)
774{
775	return 0;
776}
777
778static
779int hvs_notify_send_pre_block(struct vsock_sock *vsk,
780			      struct vsock_transport_send_notify_data *d)
781{
782	return 0;
783}
784
785static
786int hvs_notify_send_pre_enqueue(struct vsock_sock *vsk,
787				struct vsock_transport_send_notify_data *d)
788{
789	return 0;
790}
791
792static
793int hvs_notify_send_post_enqueue(struct vsock_sock *vsk, ssize_t written,
794				 struct vsock_transport_send_notify_data *d)
795{
796	return 0;
797}
798
799static struct vsock_transport hvs_transport = {
800	.module                   = THIS_MODULE,
801
802	.get_local_cid            = hvs_get_local_cid,
803
804	.init                     = hvs_sock_init,
805	.destruct                 = hvs_destruct,
806	.release                  = hvs_release,
807	.connect                  = hvs_connect,
808	.shutdown                 = hvs_shutdown,
809
810	.dgram_bind               = hvs_dgram_bind,
811	.dgram_dequeue            = hvs_dgram_dequeue,
812	.dgram_enqueue            = hvs_dgram_enqueue,
813	.dgram_allow              = hvs_dgram_allow,
814
815	.stream_dequeue           = hvs_stream_dequeue,
816	.stream_enqueue           = hvs_stream_enqueue,
817	.stream_has_data          = hvs_stream_has_data,
818	.stream_has_space         = hvs_stream_has_space,
819	.stream_rcvhiwat          = hvs_stream_rcvhiwat,
820	.stream_is_active         = hvs_stream_is_active,
821	.stream_allow             = hvs_stream_allow,
822
823	.notify_poll_in           = hvs_notify_poll_in,
824	.notify_poll_out          = hvs_notify_poll_out,
825	.notify_recv_init         = hvs_notify_recv_init,
826	.notify_recv_pre_block    = hvs_notify_recv_pre_block,
827	.notify_recv_pre_dequeue  = hvs_notify_recv_pre_dequeue,
828	.notify_recv_post_dequeue = hvs_notify_recv_post_dequeue,
829	.notify_send_init         = hvs_notify_send_init,
830	.notify_send_pre_block    = hvs_notify_send_pre_block,
831	.notify_send_pre_enqueue  = hvs_notify_send_pre_enqueue,
832	.notify_send_post_enqueue = hvs_notify_send_post_enqueue,
833
834};
835
836static bool hvs_check_transport(struct vsock_sock *vsk)
837{
838	return vsk->transport == &hvs_transport;
839}
840
841static int hvs_probe(struct hv_device *hdev,
842		     const struct hv_vmbus_device_id *dev_id)
843{
844	struct vmbus_channel *chan = hdev->channel;
845
846	hvs_open_connection(chan);
847
848	/* Always return success to suppress the unnecessary error message
849	 * in vmbus_probe(): on error the host will rescind the device in
850	 * 30 seconds and we can do cleanup at that time in
851	 * vmbus_onoffer_rescind().
852	 */
853	return 0;
854}
855
856static int hvs_remove(struct hv_device *hdev)
857{
858	struct vmbus_channel *chan = hdev->channel;
859
860	vmbus_close(chan);
861
862	return 0;
863}
864
865/* hv_sock connections can not persist across hibernation, and all the hv_sock
866 * channels are forced to be rescinded before hibernation: see
867 * vmbus_bus_suspend(). Here the dummy hvs_suspend() and hvs_resume()
868 * are only needed because hibernation requires that every vmbus device's
869 * driver should have a .suspend and .resume callback: see vmbus_suspend().
870 */
871static int hvs_suspend(struct hv_device *hv_dev)
872{
873	/* Dummy */
874	return 0;
875}
876
877static int hvs_resume(struct hv_device *dev)
878{
879	/* Dummy */
880	return 0;
881}
882
883/* This isn't really used. See vmbus_match() and vmbus_probe() */
884static const struct hv_vmbus_device_id id_table[] = {
885	{},
886};
887
888static struct hv_driver hvs_drv = {
889	.name		= "hv_sock",
890	.hvsock		= true,
891	.id_table	= id_table,
892	.probe		= hvs_probe,
893	.remove		= hvs_remove,
894	.suspend	= hvs_suspend,
895	.resume		= hvs_resume,
896};
897
898static int __init hvs_init(void)
899{
900	int ret;
901
902	if (vmbus_proto_version < VERSION_WIN10)
903		return -ENODEV;
904
905	ret = vmbus_driver_register(&hvs_drv);
906	if (ret != 0)
907		return ret;
908
909	ret = vsock_core_register(&hvs_transport, VSOCK_TRANSPORT_F_G2H);
910	if (ret) {
911		vmbus_driver_unregister(&hvs_drv);
912		return ret;
913	}
914
915	return 0;
916}
917
918static void __exit hvs_exit(void)
919{
920	vsock_core_unregister(&hvs_transport);
921	vmbus_driver_unregister(&hvs_drv);
922}
923
924module_init(hvs_init);
925module_exit(hvs_exit);
926
927MODULE_DESCRIPTION("Hyper-V Sockets");
928MODULE_VERSION("1.0.0");
929MODULE_LICENSE("GPL");
930MODULE_ALIAS_NETPROTO(PF_VSOCK);
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Hyper-V transport for vsock
  4 *
  5 * Hyper-V Sockets supplies a byte-stream based communication mechanism
  6 * between the host and the VM. This driver implements the necessary
  7 * support in the VM by introducing the new vsock transport.
  8 *
  9 * Copyright (c) 2017, Microsoft Corporation.
 10 */
 11#include <linux/module.h>
 12#include <linux/vmalloc.h>
 13#include <linux/hyperv.h>
 14#include <net/sock.h>
 15#include <net/af_vsock.h>
 16#include <asm/hyperv-tlfs.h>
 17
 18/* Older (VMBUS version 'VERSION_WIN10' or before) Windows hosts have some
 19 * stricter requirements on the hv_sock ring buffer size of six 4K pages.
 20 * hyperv-tlfs defines HV_HYP_PAGE_SIZE as 4K. Newer hosts don't have this
 21 * limitation; but, keep the defaults the same for compat.
 22 */
 23#define RINGBUFFER_HVS_RCV_SIZE (HV_HYP_PAGE_SIZE * 6)
 24#define RINGBUFFER_HVS_SND_SIZE (HV_HYP_PAGE_SIZE * 6)
 25#define RINGBUFFER_HVS_MAX_SIZE (HV_HYP_PAGE_SIZE * 64)
 26
 27/* The MTU is 16KB per the host side's design */
 28#define HVS_MTU_SIZE		(1024 * 16)
 29
 30/* How long to wait for graceful shutdown of a connection */
 31#define HVS_CLOSE_TIMEOUT (8 * HZ)
 32
 33struct vmpipe_proto_header {
 34	u32 pkt_type;
 35	u32 data_size;
 36};
 37
 38/* For recv, we use the VMBus in-place packet iterator APIs to directly copy
 39 * data from the ringbuffer into the userspace buffer.
 40 */
 41struct hvs_recv_buf {
 42	/* The header before the payload data */
 43	struct vmpipe_proto_header hdr;
 44
 45	/* The payload */
 46	u8 data[HVS_MTU_SIZE];
 47};
 48
 49/* We can send up to HVS_MTU_SIZE bytes of payload to the host, but let's use
 50 * a smaller size, i.e. HVS_SEND_BUF_SIZE, to maximize concurrency between the
 51 * guest and the host processing as one VMBUS packet is the smallest processing
 52 * unit.
 53 *
 54 * Note: the buffer can be eliminated in the future when we add new VMBus
 55 * ringbuffer APIs that allow us to directly copy data from userspace buffer
 56 * to VMBus ringbuffer.
 57 */
 58#define HVS_SEND_BUF_SIZE \
 59		(HV_HYP_PAGE_SIZE - sizeof(struct vmpipe_proto_header))
 60
 61struct hvs_send_buf {
 62	/* The header before the payload data */
 63	struct vmpipe_proto_header hdr;
 64
 65	/* The payload */
 66	u8 data[HVS_SEND_BUF_SIZE];
 67};
 68
 69#define HVS_HEADER_LEN	(sizeof(struct vmpacket_descriptor) + \
 70			 sizeof(struct vmpipe_proto_header))
 71
 72/* See 'prev_indices' in hv_ringbuffer_read(), hv_ringbuffer_write(), and
 73 * __hv_pkt_iter_next().
 74 */
 75#define VMBUS_PKT_TRAILER_SIZE	(sizeof(u64))
 76
 77#define HVS_PKT_LEN(payload_len)	(HVS_HEADER_LEN + \
 78					 ALIGN((payload_len), 8) + \
 79					 VMBUS_PKT_TRAILER_SIZE)
 80
 81union hvs_service_id {
 82	guid_t	srv_id;
 83
 84	struct {
 85		unsigned int svm_port;
 86		unsigned char b[sizeof(guid_t) - sizeof(unsigned int)];
 87	};
 88};
 89
 90/* Per-socket state (accessed via vsk->trans) */
 91struct hvsock {
 92	struct vsock_sock *vsk;
 93
 94	guid_t vm_srv_id;
 95	guid_t host_srv_id;
 96
 97	struct vmbus_channel *chan;
 98	struct vmpacket_descriptor *recv_desc;
 99
100	/* The length of the payload not delivered to userland yet */
101	u32 recv_data_len;
102	/* The offset of the payload */
103	u32 recv_data_off;
104
105	/* Have we sent the zero-length packet (FIN)? */
106	bool fin_sent;
107};
108
109/* In the VM, we support Hyper-V Sockets with AF_VSOCK, and the endpoint is
110 * <cid, port> (see struct sockaddr_vm). Note: cid is not really used here:
111 * when we write apps to connect to the host, we can only use VMADDR_CID_ANY
112 * or VMADDR_CID_HOST (both are equivalent) as the remote cid, and when we
113 * write apps to bind() & listen() in the VM, we can only use VMADDR_CID_ANY
114 * as the local cid.
115 *
116 * On the host, Hyper-V Sockets are supported by Winsock AF_HYPERV:
117 * https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/user-
118 * guide/make-integration-service, and the endpoint is <VmID, ServiceId> with
119 * the below sockaddr:
120 *
121 * struct SOCKADDR_HV
122 * {
123 *    ADDRESS_FAMILY Family;
124 *    USHORT Reserved;
125 *    GUID VmId;
126 *    GUID ServiceId;
127 * };
128 * Note: VmID is not used by Linux VM and actually it isn't transmitted via
129 * VMBus, because here it's obvious the host and the VM can easily identify
130 * each other. Though the VmID is useful on the host, especially in the case
131 * of Windows container, Linux VM doesn't need it at all.
132 *
133 * To make use of the AF_VSOCK infrastructure in Linux VM, we have to limit
134 * the available GUID space of SOCKADDR_HV so that we can create a mapping
135 * between AF_VSOCK port and SOCKADDR_HV Service GUID. The rule of writing
136 * Hyper-V Sockets apps on the host and in Linux VM is:
137 *
138 ****************************************************************************
139 * The only valid Service GUIDs, from the perspectives of both the host and *
140 * Linux VM, that can be connected by the other end, must conform to this   *
141 * format: <port>-facb-11e6-bd58-64006a7986d3.                              *
142 ****************************************************************************
143 *
144 * When we write apps on the host to connect(), the GUID ServiceID is used.
145 * When we write apps in Linux VM to connect(), we only need to specify the
146 * port and the driver will form the GUID and use that to request the host.
147 *
148 */
149
150/* 00000000-facb-11e6-bd58-64006a7986d3 */
151static const guid_t srv_id_template =
152	GUID_INIT(0x00000000, 0xfacb, 0x11e6, 0xbd, 0x58,
153		  0x64, 0x00, 0x6a, 0x79, 0x86, 0xd3);
154
155static bool hvs_check_transport(struct vsock_sock *vsk);
156
157static bool is_valid_srv_id(const guid_t *id)
158{
159	return !memcmp(&id->b[4], &srv_id_template.b[4], sizeof(guid_t) - 4);
160}
161
162static unsigned int get_port_by_srv_id(const guid_t *svr_id)
163{
164	return *((unsigned int *)svr_id);
165}
166
167static void hvs_addr_init(struct sockaddr_vm *addr, const guid_t *svr_id)
168{
169	unsigned int port = get_port_by_srv_id(svr_id);
170
171	vsock_addr_init(addr, VMADDR_CID_ANY, port);
172}
173
174static void hvs_set_channel_pending_send_size(struct vmbus_channel *chan)
175{
176	set_channel_pending_send_size(chan,
177				      HVS_PKT_LEN(HVS_SEND_BUF_SIZE));
178
179	virt_mb();
180}
181
182static bool hvs_channel_readable(struct vmbus_channel *chan)
183{
184	u32 readable = hv_get_bytes_to_read(&chan->inbound);
185
186	/* 0-size payload means FIN */
187	return readable >= HVS_PKT_LEN(0);
188}
189
190static int hvs_channel_readable_payload(struct vmbus_channel *chan)
191{
192	u32 readable = hv_get_bytes_to_read(&chan->inbound);
193
194	if (readable > HVS_PKT_LEN(0)) {
195		/* At least we have 1 byte to read. We don't need to return
196		 * the exact readable bytes: see vsock_stream_recvmsg() ->
197		 * vsock_stream_has_data().
198		 */
199		return 1;
200	}
201
202	if (readable == HVS_PKT_LEN(0)) {
203		/* 0-size payload means FIN */
204		return 0;
205	}
206
207	/* No payload or FIN */
208	return -1;
209}
210
211static size_t hvs_channel_writable_bytes(struct vmbus_channel *chan)
212{
213	u32 writeable = hv_get_bytes_to_write(&chan->outbound);
214	size_t ret;
215
216	/* The ringbuffer mustn't be 100% full, and we should reserve a
217	 * zero-length-payload packet for the FIN: see hv_ringbuffer_write()
218	 * and hvs_shutdown().
219	 */
220	if (writeable <= HVS_PKT_LEN(1) + HVS_PKT_LEN(0))
221		return 0;
222
223	ret = writeable - HVS_PKT_LEN(1) - HVS_PKT_LEN(0);
224
225	return round_down(ret, 8);
226}
227
228static int hvs_send_data(struct vmbus_channel *chan,
229			 struct hvs_send_buf *send_buf, size_t to_write)
230{
231	send_buf->hdr.pkt_type = 1;
232	send_buf->hdr.data_size = to_write;
233	return vmbus_sendpacket(chan, &send_buf->hdr,
234				sizeof(send_buf->hdr) + to_write,
235				0, VM_PKT_DATA_INBAND, 0);
236}
237
238static void hvs_channel_cb(void *ctx)
239{
240	struct sock *sk = (struct sock *)ctx;
241	struct vsock_sock *vsk = vsock_sk(sk);
242	struct hvsock *hvs = vsk->trans;
243	struct vmbus_channel *chan = hvs->chan;
244
245	if (hvs_channel_readable(chan))
246		sk->sk_data_ready(sk);
247
248	if (hv_get_bytes_to_write(&chan->outbound) > 0)
249		sk->sk_write_space(sk);
250}
251
252static void hvs_do_close_lock_held(struct vsock_sock *vsk,
253				   bool cancel_timeout)
254{
255	struct sock *sk = sk_vsock(vsk);
256
257	sock_set_flag(sk, SOCK_DONE);
258	vsk->peer_shutdown = SHUTDOWN_MASK;
259	if (vsock_stream_has_data(vsk) <= 0)
260		sk->sk_state = TCP_CLOSING;
261	sk->sk_state_change(sk);
262	if (vsk->close_work_scheduled &&
263	    (!cancel_timeout || cancel_delayed_work(&vsk->close_work))) {
264		vsk->close_work_scheduled = false;
265		vsock_remove_sock(vsk);
266
267		/* Release the reference taken while scheduling the timeout */
268		sock_put(sk);
269	}
270}
271
272static void hvs_close_connection(struct vmbus_channel *chan)
273{
274	struct sock *sk = get_per_channel_state(chan);
275
276	lock_sock(sk);
277	hvs_do_close_lock_held(vsock_sk(sk), true);
278	release_sock(sk);
279
280	/* Release the refcnt for the channel that's opened in
281	 * hvs_open_connection().
282	 */
283	sock_put(sk);
284}
285
286static void hvs_open_connection(struct vmbus_channel *chan)
287{
288	guid_t *if_instance, *if_type;
289	unsigned char conn_from_host;
290
291	struct sockaddr_vm addr;
292	struct sock *sk, *new = NULL;
293	struct vsock_sock *vnew = NULL;
294	struct hvsock *hvs = NULL;
295	struct hvsock *hvs_new = NULL;
296	int rcvbuf;
297	int ret;
298	int sndbuf;
299
300	if_type = &chan->offermsg.offer.if_type;
301	if_instance = &chan->offermsg.offer.if_instance;
302	conn_from_host = chan->offermsg.offer.u.pipe.user_def[0];
303	if (!is_valid_srv_id(if_type))
304		return;
305
306	hvs_addr_init(&addr, conn_from_host ? if_type : if_instance);
307	sk = vsock_find_bound_socket(&addr);
308	if (!sk)
309		return;
310
311	lock_sock(sk);
312	if ((conn_from_host && sk->sk_state != TCP_LISTEN) ||
313	    (!conn_from_host && sk->sk_state != TCP_SYN_SENT))
314		goto out;
315
316	if (conn_from_host) {
317		if (sk->sk_ack_backlog >= sk->sk_max_ack_backlog)
318			goto out;
319
320		new = vsock_create_connected(sk);
321		if (!new)
322			goto out;
323
324		new->sk_state = TCP_SYN_SENT;
325		vnew = vsock_sk(new);
326
327		hvs_addr_init(&vnew->local_addr, if_type);
328
329		/* Remote peer is always the host */
330		vsock_addr_init(&vnew->remote_addr,
331				VMADDR_CID_HOST, VMADDR_PORT_ANY);
332		vnew->remote_addr.svm_port = get_port_by_srv_id(if_instance);
333		ret = vsock_assign_transport(vnew, vsock_sk(sk));
334		/* Transport assigned (looking at remote_addr) must be the
335		 * same where we received the request.
336		 */
337		if (ret || !hvs_check_transport(vnew)) {
338			sock_put(new);
339			goto out;
340		}
341		hvs_new = vnew->trans;
342		hvs_new->chan = chan;
343	} else {
344		hvs = vsock_sk(sk)->trans;
345		hvs->chan = chan;
346	}
347
348	set_channel_read_mode(chan, HV_CALL_DIRECT);
349
350	/* Use the socket buffer sizes as hints for the VMBUS ring size. For
351	 * server side sockets, 'sk' is the parent socket and thus, this will
352	 * allow the child sockets to inherit the size from the parent. Keep
353	 * the mins to the default value and align to page size as per VMBUS
354	 * requirements.
355	 * For the max, the socket core library will limit the socket buffer
356	 * size that can be set by the user, but, since currently, the hv_sock
357	 * VMBUS ring buffer is physically contiguous allocation, restrict it
358	 * further.
359	 * Older versions of hv_sock host side code cannot handle bigger VMBUS
360	 * ring buffer size. Use the version number to limit the change to newer
361	 * versions.
362	 */
363	if (vmbus_proto_version < VERSION_WIN10_V5) {
364		sndbuf = RINGBUFFER_HVS_SND_SIZE;
365		rcvbuf = RINGBUFFER_HVS_RCV_SIZE;
366	} else {
367		sndbuf = max_t(int, sk->sk_sndbuf, RINGBUFFER_HVS_SND_SIZE);
368		sndbuf = min_t(int, sndbuf, RINGBUFFER_HVS_MAX_SIZE);
369		sndbuf = ALIGN(sndbuf, HV_HYP_PAGE_SIZE);
370		rcvbuf = max_t(int, sk->sk_rcvbuf, RINGBUFFER_HVS_RCV_SIZE);
371		rcvbuf = min_t(int, rcvbuf, RINGBUFFER_HVS_MAX_SIZE);
372		rcvbuf = ALIGN(rcvbuf, HV_HYP_PAGE_SIZE);
373	}
374
375	ret = vmbus_open(chan, sndbuf, rcvbuf, NULL, 0, hvs_channel_cb,
376			 conn_from_host ? new : sk);
377	if (ret != 0) {
378		if (conn_from_host) {
379			hvs_new->chan = NULL;
380			sock_put(new);
381		} else {
382			hvs->chan = NULL;
383		}
384		goto out;
385	}
386
387	set_per_channel_state(chan, conn_from_host ? new : sk);
388
389	/* This reference will be dropped by hvs_close_connection(). */
390	sock_hold(conn_from_host ? new : sk);
391	vmbus_set_chn_rescind_callback(chan, hvs_close_connection);
392
393	/* Set the pending send size to max packet size to always get
394	 * notifications from the host when there is enough writable space.
395	 * The host is optimized to send notifications only when the pending
396	 * size boundary is crossed, and not always.
397	 */
398	hvs_set_channel_pending_send_size(chan);
399
400	if (conn_from_host) {
401		new->sk_state = TCP_ESTABLISHED;
402		sk_acceptq_added(sk);
403
404		hvs_new->vm_srv_id = *if_type;
405		hvs_new->host_srv_id = *if_instance;
406
407		vsock_insert_connected(vnew);
408
409		vsock_enqueue_accept(sk, new);
410	} else {
411		sk->sk_state = TCP_ESTABLISHED;
412		sk->sk_socket->state = SS_CONNECTED;
413
414		vsock_insert_connected(vsock_sk(sk));
415	}
416
417	sk->sk_state_change(sk);
418
419out:
420	/* Release refcnt obtained when we called vsock_find_bound_socket() */
421	sock_put(sk);
422
423	release_sock(sk);
424}
425
426static u32 hvs_get_local_cid(void)
427{
428	return VMADDR_CID_ANY;
429}
430
431static int hvs_sock_init(struct vsock_sock *vsk, struct vsock_sock *psk)
432{
433	struct hvsock *hvs;
434	struct sock *sk = sk_vsock(vsk);
435
436	hvs = kzalloc(sizeof(*hvs), GFP_KERNEL);
437	if (!hvs)
438		return -ENOMEM;
439
440	vsk->trans = hvs;
441	hvs->vsk = vsk;
442	sk->sk_sndbuf = RINGBUFFER_HVS_SND_SIZE;
443	sk->sk_rcvbuf = RINGBUFFER_HVS_RCV_SIZE;
444	return 0;
445}
446
447static int hvs_connect(struct vsock_sock *vsk)
448{
449	union hvs_service_id vm, host;
450	struct hvsock *h = vsk->trans;
451
452	vm.srv_id = srv_id_template;
453	vm.svm_port = vsk->local_addr.svm_port;
454	h->vm_srv_id = vm.srv_id;
455
456	host.srv_id = srv_id_template;
457	host.svm_port = vsk->remote_addr.svm_port;
458	h->host_srv_id = host.srv_id;
459
460	return vmbus_send_tl_connect_request(&h->vm_srv_id, &h->host_srv_id);
461}
462
463static void hvs_shutdown_lock_held(struct hvsock *hvs, int mode)
464{
465	struct vmpipe_proto_header hdr;
466
467	if (hvs->fin_sent || !hvs->chan)
468		return;
469
470	/* It can't fail: see hvs_channel_writable_bytes(). */
471	(void)hvs_send_data(hvs->chan, (struct hvs_send_buf *)&hdr, 0);
472	hvs->fin_sent = true;
473}
474
475static int hvs_shutdown(struct vsock_sock *vsk, int mode)
476{
477	struct sock *sk = sk_vsock(vsk);
478
479	if (!(mode & SEND_SHUTDOWN))
480		return 0;
481
482	lock_sock(sk);
483	hvs_shutdown_lock_held(vsk->trans, mode);
484	release_sock(sk);
485	return 0;
486}
487
488static void hvs_close_timeout(struct work_struct *work)
489{
490	struct vsock_sock *vsk =
491		container_of(work, struct vsock_sock, close_work.work);
492	struct sock *sk = sk_vsock(vsk);
493
494	sock_hold(sk);
495	lock_sock(sk);
496	if (!sock_flag(sk, SOCK_DONE))
497		hvs_do_close_lock_held(vsk, false);
498
499	vsk->close_work_scheduled = false;
500	release_sock(sk);
501	sock_put(sk);
502}
503
504/* Returns true, if it is safe to remove socket; false otherwise */
505static bool hvs_close_lock_held(struct vsock_sock *vsk)
506{
507	struct sock *sk = sk_vsock(vsk);
508
509	if (!(sk->sk_state == TCP_ESTABLISHED ||
510	      sk->sk_state == TCP_CLOSING))
511		return true;
512
513	if ((sk->sk_shutdown & SHUTDOWN_MASK) != SHUTDOWN_MASK)
514		hvs_shutdown_lock_held(vsk->trans, SHUTDOWN_MASK);
515
516	if (sock_flag(sk, SOCK_DONE))
517		return true;
518
519	/* This reference will be dropped by the delayed close routine */
520	sock_hold(sk);
521	INIT_DELAYED_WORK(&vsk->close_work, hvs_close_timeout);
522	vsk->close_work_scheduled = true;
523	schedule_delayed_work(&vsk->close_work, HVS_CLOSE_TIMEOUT);
524	return false;
525}
526
527static void hvs_release(struct vsock_sock *vsk)
528{
529	bool remove_sock;
530
531	remove_sock = hvs_close_lock_held(vsk);
532	if (remove_sock)
533		vsock_remove_sock(vsk);
534}
535
536static void hvs_destruct(struct vsock_sock *vsk)
537{
538	struct hvsock *hvs = vsk->trans;
539	struct vmbus_channel *chan = hvs->chan;
540
541	if (chan)
542		vmbus_hvsock_device_unregister(chan);
543
544	kfree(hvs);
545}
546
547static int hvs_dgram_bind(struct vsock_sock *vsk, struct sockaddr_vm *addr)
548{
549	return -EOPNOTSUPP;
550}
551
552static int hvs_dgram_dequeue(struct vsock_sock *vsk, struct msghdr *msg,
553			     size_t len, int flags)
554{
555	return -EOPNOTSUPP;
556}
557
558static int hvs_dgram_enqueue(struct vsock_sock *vsk,
559			     struct sockaddr_vm *remote, struct msghdr *msg,
560			     size_t dgram_len)
561{
562	return -EOPNOTSUPP;
563}
564
565static bool hvs_dgram_allow(u32 cid, u32 port)
566{
567	return false;
568}
569
570static int hvs_update_recv_data(struct hvsock *hvs)
571{
572	struct hvs_recv_buf *recv_buf;
573	u32 payload_len;
574
575	recv_buf = (struct hvs_recv_buf *)(hvs->recv_desc + 1);
576	payload_len = recv_buf->hdr.data_size;
577
578	if (payload_len > HVS_MTU_SIZE)
579		return -EIO;
580
581	if (payload_len == 0)
582		hvs->vsk->peer_shutdown |= SEND_SHUTDOWN;
583
584	hvs->recv_data_len = payload_len;
585	hvs->recv_data_off = 0;
586
587	return 0;
588}
589
590static ssize_t hvs_stream_dequeue(struct vsock_sock *vsk, struct msghdr *msg,
591				  size_t len, int flags)
592{
593	struct hvsock *hvs = vsk->trans;
594	bool need_refill = !hvs->recv_desc;
595	struct hvs_recv_buf *recv_buf;
596	u32 to_read;
597	int ret;
598
599	if (flags & MSG_PEEK)
600		return -EOPNOTSUPP;
601
602	if (need_refill) {
603		hvs->recv_desc = hv_pkt_iter_first(hvs->chan);
604		ret = hvs_update_recv_data(hvs);
605		if (ret)
606			return ret;
607	}
608
609	recv_buf = (struct hvs_recv_buf *)(hvs->recv_desc + 1);
610	to_read = min_t(u32, len, hvs->recv_data_len);
611	ret = memcpy_to_msg(msg, recv_buf->data + hvs->recv_data_off, to_read);
612	if (ret != 0)
613		return ret;
614
615	hvs->recv_data_len -= to_read;
616	if (hvs->recv_data_len == 0) {
617		hvs->recv_desc = hv_pkt_iter_next(hvs->chan, hvs->recv_desc);
618		if (hvs->recv_desc) {
619			ret = hvs_update_recv_data(hvs);
620			if (ret)
621				return ret;
622		}
623	} else {
624		hvs->recv_data_off += to_read;
625	}
626
627	return to_read;
628}
629
630static ssize_t hvs_stream_enqueue(struct vsock_sock *vsk, struct msghdr *msg,
631				  size_t len)
632{
633	struct hvsock *hvs = vsk->trans;
634	struct vmbus_channel *chan = hvs->chan;
635	struct hvs_send_buf *send_buf;
636	ssize_t to_write, max_writable;
637	ssize_t ret = 0;
638	ssize_t bytes_written = 0;
639
640	BUILD_BUG_ON(sizeof(*send_buf) != HV_HYP_PAGE_SIZE);
641
642	send_buf = kmalloc(sizeof(*send_buf), GFP_KERNEL);
643	if (!send_buf)
644		return -ENOMEM;
645
646	/* Reader(s) could be draining data from the channel as we write.
647	 * Maximize bandwidth, by iterating until the channel is found to be
648	 * full.
649	 */
650	while (len) {
651		max_writable = hvs_channel_writable_bytes(chan);
652		if (!max_writable)
653			break;
654		to_write = min_t(ssize_t, len, max_writable);
655		to_write = min_t(ssize_t, to_write, HVS_SEND_BUF_SIZE);
656		/* memcpy_from_msg is safe for loop as it advances the offsets
657		 * within the message iterator.
658		 */
659		ret = memcpy_from_msg(send_buf->data, msg, to_write);
660		if (ret < 0)
661			goto out;
662
663		ret = hvs_send_data(hvs->chan, send_buf, to_write);
664		if (ret < 0)
665			goto out;
666
667		bytes_written += to_write;
668		len -= to_write;
669	}
670out:
671	/* If any data has been sent, return that */
672	if (bytes_written)
673		ret = bytes_written;
674	kfree(send_buf);
675	return ret;
676}
677
678static s64 hvs_stream_has_data(struct vsock_sock *vsk)
679{
680	struct hvsock *hvs = vsk->trans;
681	s64 ret;
682
683	if (hvs->recv_data_len > 0)
684		return 1;
685
686	switch (hvs_channel_readable_payload(hvs->chan)) {
687	case 1:
688		ret = 1;
689		break;
690	case 0:
691		vsk->peer_shutdown |= SEND_SHUTDOWN;
692		ret = 0;
693		break;
694	default: /* -1 */
695		ret = 0;
696		break;
697	}
698
699	return ret;
700}
701
702static s64 hvs_stream_has_space(struct vsock_sock *vsk)
703{
704	struct hvsock *hvs = vsk->trans;
705
706	return hvs_channel_writable_bytes(hvs->chan);
707}
708
709static u64 hvs_stream_rcvhiwat(struct vsock_sock *vsk)
710{
711	return HVS_MTU_SIZE + 1;
712}
713
714static bool hvs_stream_is_active(struct vsock_sock *vsk)
715{
716	struct hvsock *hvs = vsk->trans;
717
718	return hvs->chan != NULL;
719}
720
721static bool hvs_stream_allow(u32 cid, u32 port)
722{
723	if (cid == VMADDR_CID_HOST)
724		return true;
725
726	return false;
727}
728
729static
730int hvs_notify_poll_in(struct vsock_sock *vsk, size_t target, bool *readable)
731{
732	struct hvsock *hvs = vsk->trans;
733
734	*readable = hvs_channel_readable(hvs->chan);
735	return 0;
736}
737
738static
739int hvs_notify_poll_out(struct vsock_sock *vsk, size_t target, bool *writable)
740{
741	*writable = hvs_stream_has_space(vsk) > 0;
742
743	return 0;
744}
745
746static
747int hvs_notify_recv_init(struct vsock_sock *vsk, size_t target,
748			 struct vsock_transport_recv_notify_data *d)
749{
750	return 0;
751}
752
753static
754int hvs_notify_recv_pre_block(struct vsock_sock *vsk, size_t target,
755			      struct vsock_transport_recv_notify_data *d)
756{
757	return 0;
758}
759
760static
761int hvs_notify_recv_pre_dequeue(struct vsock_sock *vsk, size_t target,
762				struct vsock_transport_recv_notify_data *d)
763{
764	return 0;
765}
766
767static
768int hvs_notify_recv_post_dequeue(struct vsock_sock *vsk, size_t target,
769				 ssize_t copied, bool data_read,
770				 struct vsock_transport_recv_notify_data *d)
771{
772	return 0;
773}
774
775static
776int hvs_notify_send_init(struct vsock_sock *vsk,
777			 struct vsock_transport_send_notify_data *d)
778{
779	return 0;
780}
781
782static
783int hvs_notify_send_pre_block(struct vsock_sock *vsk,
784			      struct vsock_transport_send_notify_data *d)
785{
786	return 0;
787}
788
789static
790int hvs_notify_send_pre_enqueue(struct vsock_sock *vsk,
791				struct vsock_transport_send_notify_data *d)
792{
793	return 0;
794}
795
796static
797int hvs_notify_send_post_enqueue(struct vsock_sock *vsk, ssize_t written,
798				 struct vsock_transport_send_notify_data *d)
799{
800	return 0;
801}
802
803static struct vsock_transport hvs_transport = {
804	.module                   = THIS_MODULE,
805
806	.get_local_cid            = hvs_get_local_cid,
807
808	.init                     = hvs_sock_init,
809	.destruct                 = hvs_destruct,
810	.release                  = hvs_release,
811	.connect                  = hvs_connect,
812	.shutdown                 = hvs_shutdown,
813
814	.dgram_bind               = hvs_dgram_bind,
815	.dgram_dequeue            = hvs_dgram_dequeue,
816	.dgram_enqueue            = hvs_dgram_enqueue,
817	.dgram_allow              = hvs_dgram_allow,
818
819	.stream_dequeue           = hvs_stream_dequeue,
820	.stream_enqueue           = hvs_stream_enqueue,
821	.stream_has_data          = hvs_stream_has_data,
822	.stream_has_space         = hvs_stream_has_space,
823	.stream_rcvhiwat          = hvs_stream_rcvhiwat,
824	.stream_is_active         = hvs_stream_is_active,
825	.stream_allow             = hvs_stream_allow,
826
827	.notify_poll_in           = hvs_notify_poll_in,
828	.notify_poll_out          = hvs_notify_poll_out,
829	.notify_recv_init         = hvs_notify_recv_init,
830	.notify_recv_pre_block    = hvs_notify_recv_pre_block,
831	.notify_recv_pre_dequeue  = hvs_notify_recv_pre_dequeue,
832	.notify_recv_post_dequeue = hvs_notify_recv_post_dequeue,
833	.notify_send_init         = hvs_notify_send_init,
834	.notify_send_pre_block    = hvs_notify_send_pre_block,
835	.notify_send_pre_enqueue  = hvs_notify_send_pre_enqueue,
836	.notify_send_post_enqueue = hvs_notify_send_post_enqueue,
837
838};
839
840static bool hvs_check_transport(struct vsock_sock *vsk)
841{
842	return vsk->transport == &hvs_transport;
843}
844
845static int hvs_probe(struct hv_device *hdev,
846		     const struct hv_vmbus_device_id *dev_id)
847{
848	struct vmbus_channel *chan = hdev->channel;
849
850	hvs_open_connection(chan);
851
852	/* Always return success to suppress the unnecessary error message
853	 * in vmbus_probe(): on error the host will rescind the device in
854	 * 30 seconds and we can do cleanup at that time in
855	 * vmbus_onoffer_rescind().
856	 */
857	return 0;
858}
859
860static int hvs_remove(struct hv_device *hdev)
861{
862	struct vmbus_channel *chan = hdev->channel;
863
864	vmbus_close(chan);
865
866	return 0;
867}
868
869/* hv_sock connections can not persist across hibernation, and all the hv_sock
870 * channels are forced to be rescinded before hibernation: see
871 * vmbus_bus_suspend(). Here the dummy hvs_suspend() and hvs_resume()
872 * are only needed because hibernation requires that every vmbus device's
873 * driver should have a .suspend and .resume callback: see vmbus_suspend().
874 */
875static int hvs_suspend(struct hv_device *hv_dev)
876{
877	/* Dummy */
878	return 0;
879}
880
881static int hvs_resume(struct hv_device *dev)
882{
883	/* Dummy */
884	return 0;
885}
886
887/* This isn't really used. See vmbus_match() and vmbus_probe() */
888static const struct hv_vmbus_device_id id_table[] = {
889	{},
890};
891
892static struct hv_driver hvs_drv = {
893	.name		= "hv_sock",
894	.hvsock		= true,
895	.id_table	= id_table,
896	.probe		= hvs_probe,
897	.remove		= hvs_remove,
898	.suspend	= hvs_suspend,
899	.resume		= hvs_resume,
900};
901
902static int __init hvs_init(void)
903{
904	int ret;
905
906	if (vmbus_proto_version < VERSION_WIN10)
907		return -ENODEV;
908
909	ret = vmbus_driver_register(&hvs_drv);
910	if (ret != 0)
911		return ret;
912
913	ret = vsock_core_register(&hvs_transport, VSOCK_TRANSPORT_F_G2H);
914	if (ret) {
915		vmbus_driver_unregister(&hvs_drv);
916		return ret;
917	}
918
919	return 0;
920}
921
922static void __exit hvs_exit(void)
923{
924	vsock_core_unregister(&hvs_transport);
925	vmbus_driver_unregister(&hvs_drv);
926}
927
928module_init(hvs_init);
929module_exit(hvs_exit);
930
931MODULE_DESCRIPTION("Hyper-V Sockets");
932MODULE_VERSION("1.0.0");
933MODULE_LICENSE("GPL");
934MODULE_ALIAS_NETPROTO(PF_VSOCK);