xref: /freebsd-13.1/sys/dev/hyperv/hvsock/hv_sock.c (revision f9832988)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2020 Microsoft Corp.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/domain.h>
35 #include <sys/lock.h>
36 #include <sys/kernel.h>
37 #include <sys/types.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/proc.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/sysctl.h>
45 #include <sys/sysproto.h>
46 #include <sys/systm.h>
47 #include <sys/sockbuf.h>
48 #include <sys/sx.h>
49 #include <sys/uio.h>
50 
51 #include <net/vnet.h>
52 
53 #include <dev/hyperv/vmbus/vmbus_reg.h>
54 
55 #include "hv_sock.h"
56 
57 #define HVSOCK_DBG_NONE			0x0
58 #define HVSOCK_DBG_INFO			0x1
59 #define HVSOCK_DBG_ERR			0x2
60 #define HVSOCK_DBG_VERBOSE		0x3
61 
62 
63 SYSCTL_NODE(_net, OID_AUTO, hvsock, CTLFLAG_RD, 0, "HyperV socket");
64 
65 static int hvs_dbg_level;
66 SYSCTL_INT(_net_hvsock, OID_AUTO, hvs_dbg_level, CTLFLAG_RWTUN, &hvs_dbg_level,
67     0, "hyperv socket debug level: 0 = none, 1 = info, 2 = error, 3 = verbose");
68 
69 
70 #define HVSOCK_DBG(level, ...) do {					\
71 	if (hvs_dbg_level >= (level))					\
72 		printf(__VA_ARGS__);					\
73 	} while (0)
74 
75 MALLOC_DEFINE(M_HVSOCK, "hyperv_socket", "hyperv socket control structures");
76 
77 /* The MTU is 16KB per host side's design */
78 #define HVSOCK_MTU_SIZE		(1024 * 16)
79 #define HVSOCK_SEND_BUF_SZ	(PAGE_SIZE - sizeof(struct vmpipe_proto_header))
80 
81 #define HVSOCK_HEADER_LEN	(sizeof(struct hvs_pkt_header))
82 
83 #define HVSOCK_PKT_LEN(payload_len)	(HVSOCK_HEADER_LEN + \
84 					 roundup2(payload_len, 8) + \
85 					 sizeof(uint64_t))
86 
87 
88 static struct domain		hv_socket_domain;
89 
90 /*
91  * HyperV Transport sockets
92  */
93 static struct pr_usrreqs	hvs_trans_usrreqs = {
94 	.pru_attach =		hvs_trans_attach,
95 	.pru_bind =		hvs_trans_bind,
96 	.pru_listen =		hvs_trans_listen,
97 	.pru_accept =		hvs_trans_accept,
98 	.pru_connect =		hvs_trans_connect,
99 	.pru_peeraddr =		hvs_trans_peeraddr,
100 	.pru_sockaddr =		hvs_trans_sockaddr,
101 	.pru_soreceive =	hvs_trans_soreceive,
102 	.pru_sosend =		hvs_trans_sosend,
103 	.pru_disconnect =	hvs_trans_disconnect,
104 	.pru_close =		hvs_trans_close,
105 	.pru_detach =		hvs_trans_detach,
106 	.pru_shutdown =		hvs_trans_shutdown,
107 	.pru_abort =		hvs_trans_abort,
108 };
109 
110 /*
111  * Definitions of protocols supported in HyperV socket domain
112  */
113 static struct protosw		hv_socket_protosw[] = {
114 {
115 	.pr_type =		SOCK_STREAM,
116 	.pr_domain =		&hv_socket_domain,
117 	.pr_protocol =		HYPERV_SOCK_PROTO_TRANS,
118 	.pr_flags =		PR_CONNREQUIRED,
119 	.pr_init =		hvs_trans_init,
120 	.pr_usrreqs =		&hvs_trans_usrreqs,
121 },
122 };
123 
124 static struct domain		hv_socket_domain = {
125 	.dom_family =		AF_HYPERV,
126 	.dom_name =		"hyperv",
127 	.dom_protosw =		hv_socket_protosw,
128 	.dom_protoswNPROTOSW =	&hv_socket_protosw[nitems(hv_socket_protosw)]
129 };
130 
131 VNET_DOMAIN_SET(hv_socket_);
132 
133 #define MAX_PORT			((uint32_t)0xFFFFFFFF)
134 #define MIN_PORT			((uint32_t)0x0)
135 
136 /* 00000000-facb-11e6-bd58-64006a7986d3 */
137 static const struct hyperv_guid srv_id_template = {
138 	.hv_guid = {
139 	    0x00, 0x00, 0x00, 0x00, 0xcb, 0xfa, 0xe6, 0x11,
140 	    0xbd, 0x58, 0x64, 0x00, 0x6a, 0x79, 0x86, 0xd3 }
141 };
142 
143 static int		hvsock_br_callback(void *, int, void *);
144 static uint32_t		hvsock_canread_check(struct hvs_pcb *);
145 static uint32_t		hvsock_canwrite_check(struct hvs_pcb *);
146 static int		hvsock_send_data(struct vmbus_channel *chan,
147     struct uio *uio, uint32_t to_write, struct sockbuf *sb);
148 
149 
150 
151 /* Globals */
152 static struct sx		hvs_trans_socks_sx;
153 static struct mtx		hvs_trans_socks_mtx;
154 static LIST_HEAD(, hvs_pcb)	hvs_trans_bound_socks;
155 static LIST_HEAD(, hvs_pcb)	hvs_trans_connected_socks;
156 static uint32_t			previous_auto_bound_port;
157 
158 static void
hvsock_print_guid(struct hyperv_guid * guid)159 hvsock_print_guid(struct hyperv_guid *guid)
160 {
161 	unsigned char *p = (unsigned char *)guid;
162 
163 	HVSOCK_DBG(HVSOCK_DBG_INFO,
164 	    "0x%x-0x%x-0x%x-0x%x-0x%x-0x%x-0x%x-0x%x-0x%x-0x%x-0x%x\n",
165 	    *(unsigned int *)p,
166 	    *((unsigned short *) &p[4]),
167 	    *((unsigned short *) &p[6]),
168 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
169 }
170 
171 static bool
is_valid_srv_id(const struct hyperv_guid * id)172 is_valid_srv_id(const struct hyperv_guid *id)
173 {
174 	return !memcmp(&id->hv_guid[4],
175 	    &srv_id_template.hv_guid[4], sizeof(struct hyperv_guid) - 4);
176 }
177 
178 static unsigned int
get_port_by_srv_id(const struct hyperv_guid * srv_id)179 get_port_by_srv_id(const struct hyperv_guid *srv_id)
180 {
181 	return *((const unsigned int *)srv_id);
182 }
183 
184 static void
set_port_by_srv_id(struct hyperv_guid * srv_id,unsigned int port)185 set_port_by_srv_id(struct hyperv_guid *srv_id, unsigned int port)
186 {
187 	*((unsigned int *)srv_id) = port;
188 }
189 
190 
191 static void
__hvs_remove_pcb_from_list(struct hvs_pcb * pcb,unsigned char list)192 __hvs_remove_pcb_from_list(struct hvs_pcb *pcb, unsigned char list)
193 {
194 	struct hvs_pcb *p = NULL;
195 
196 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE, "%s: pcb is %p\n", __func__, pcb);
197 
198 	if (!pcb)
199 		return;
200 
201 	if (list & HVS_LIST_BOUND) {
202 		LIST_FOREACH(p, &hvs_trans_bound_socks, bound_next)
203 			if  (p == pcb)
204 				LIST_REMOVE(p, bound_next);
205 	}
206 
207 	if (list & HVS_LIST_CONNECTED) {
208 		LIST_FOREACH(p, &hvs_trans_connected_socks, connected_next)
209 			if (p == pcb)
210 				LIST_REMOVE(pcb, connected_next);
211 	}
212 }
213 
214 static void
__hvs_remove_socket_from_list(struct socket * so,unsigned char list)215 __hvs_remove_socket_from_list(struct socket *so, unsigned char list)
216 {
217 	struct hvs_pcb *pcb = so2hvspcb(so);
218 
219 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE, "%s: pcb is %p\n", __func__, pcb);
220 
221 	__hvs_remove_pcb_from_list(pcb, list);
222 }
223 
224 static void
__hvs_insert_socket_on_list(struct socket * so,unsigned char list)225 __hvs_insert_socket_on_list(struct socket *so, unsigned char list)
226 {
227 	struct hvs_pcb *pcb = so2hvspcb(so);
228 
229 	if (list & HVS_LIST_BOUND)
230 		LIST_INSERT_HEAD(&hvs_trans_bound_socks,
231 		   pcb, bound_next);
232 
233 	if (list & HVS_LIST_CONNECTED)
234 		LIST_INSERT_HEAD(&hvs_trans_connected_socks,
235 		   pcb, connected_next);
236 }
237 
238 void
hvs_remove_socket_from_list(struct socket * so,unsigned char list)239 hvs_remove_socket_from_list(struct socket *so, unsigned char list)
240 {
241 	if (!so || !so->so_pcb) {
242 		HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
243 		    "%s: socket or so_pcb is null\n", __func__);
244 		return;
245 	}
246 
247 	mtx_lock(&hvs_trans_socks_mtx);
248 	__hvs_remove_socket_from_list(so, list);
249 	mtx_unlock(&hvs_trans_socks_mtx);
250 }
251 
252 static void
hvs_insert_socket_on_list(struct socket * so,unsigned char list)253 hvs_insert_socket_on_list(struct socket *so, unsigned char list)
254 {
255 	if (!so || !so->so_pcb) {
256 		HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
257 		    "%s: socket or so_pcb is null\n", __func__);
258 		return;
259 	}
260 
261 	mtx_lock(&hvs_trans_socks_mtx);
262 	__hvs_insert_socket_on_list(so, list);
263 	mtx_unlock(&hvs_trans_socks_mtx);
264 }
265 
266 static struct socket *
__hvs_find_socket_on_list(struct sockaddr_hvs * addr,unsigned char list)267 __hvs_find_socket_on_list(struct sockaddr_hvs *addr, unsigned char list)
268 {
269 	struct hvs_pcb *p = NULL;
270 
271 	if (list & HVS_LIST_BOUND)
272 		LIST_FOREACH(p, &hvs_trans_bound_socks, bound_next)
273 			if (p->so != NULL &&
274 			    addr->hvs_port == p->local_addr.hvs_port)
275 				return p->so;
276 
277 	if (list & HVS_LIST_CONNECTED)
278 		LIST_FOREACH(p, &hvs_trans_connected_socks, connected_next)
279 			if (p->so != NULL &&
280 			    addr->hvs_port == p->local_addr.hvs_port)
281 				return p->so;
282 
283 	return NULL;
284 }
285 
286 static struct socket *
hvs_find_socket_on_list(struct sockaddr_hvs * addr,unsigned char list)287 hvs_find_socket_on_list(struct sockaddr_hvs *addr, unsigned char list)
288 {
289 	struct socket *s = NULL;
290 
291 	mtx_lock(&hvs_trans_socks_mtx);
292 	s = __hvs_find_socket_on_list(addr, list);
293 	mtx_unlock(&hvs_trans_socks_mtx);
294 
295 	return s;
296 }
297 
298 static inline void
hvs_addr_set(struct sockaddr_hvs * addr,unsigned int port)299 hvs_addr_set(struct sockaddr_hvs *addr, unsigned int port)
300 {
301 	memset(addr, 0, sizeof(*addr));
302 	addr->sa_family = AF_HYPERV;
303 	addr->sa_len = sizeof(*addr);
304 	addr->hvs_port = port;
305 }
306 
307 void
hvs_addr_init(struct sockaddr_hvs * addr,const struct hyperv_guid * svr_id)308 hvs_addr_init(struct sockaddr_hvs *addr, const struct hyperv_guid *svr_id)
309 {
310 	hvs_addr_set(addr, get_port_by_srv_id(svr_id));
311 }
312 
313 int
hvs_trans_lock(void)314 hvs_trans_lock(void)
315 {
316 	sx_xlock(&hvs_trans_socks_sx);
317 	return (0);
318 }
319 
320 void
hvs_trans_unlock(void)321 hvs_trans_unlock(void)
322 {
323 	sx_xunlock(&hvs_trans_socks_sx);
324 }
325 
326 void
hvs_trans_init(void)327 hvs_trans_init(void)
328 {
329 	/* Skip initialization of globals for non-default instances. */
330 	if (!IS_DEFAULT_VNET(curvnet))
331 		return;
332 
333 	if (vm_guest != VM_GUEST_HV)
334 		return;
335 
336 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
337 	    "%s: HyperV Socket hvs_trans_init called\n", __func__);
338 
339 	/* Initialize Globals */
340 	previous_auto_bound_port = MAX_PORT;
341 	sx_init(&hvs_trans_socks_sx, "hvs_trans_sock_sx");
342 	mtx_init(&hvs_trans_socks_mtx,
343 	    "hvs_trans_socks_mtx", NULL, MTX_DEF);
344 	LIST_INIT(&hvs_trans_bound_socks);
345 	LIST_INIT(&hvs_trans_connected_socks);
346 }
347 
348 /*
349  * Called in two cases:
350  * 1) When user calls socket();
351  * 2) When we accept new incoming conneciton and call sonewconn().
352  */
353 int
hvs_trans_attach(struct socket * so,int proto,struct thread * td)354 hvs_trans_attach(struct socket *so, int proto, struct thread *td)
355 {
356 	struct hvs_pcb *pcb = so2hvspcb(so);
357 
358 	if (vm_guest != VM_GUEST_HV)
359 		return (ESOCKTNOSUPPORT);
360 
361 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
362 	    "%s: HyperV Socket hvs_trans_attach called\n", __func__);
363 
364 	if (so->so_type != SOCK_STREAM)
365 		return (ESOCKTNOSUPPORT);
366 
367 	if (proto != 0 && proto != HYPERV_SOCK_PROTO_TRANS)
368 		return (EPROTONOSUPPORT);
369 
370 	if (pcb != NULL)
371 		return (EISCONN);
372 	pcb = malloc(sizeof(struct hvs_pcb), M_HVSOCK, M_NOWAIT | M_ZERO);
373 	if (pcb == NULL)
374 		return (ENOMEM);
375 
376 	pcb->so = so;
377 	so->so_pcb = (void *)pcb;
378 
379 	return (0);
380 }
381 
382 void
hvs_trans_detach(struct socket * so)383 hvs_trans_detach(struct socket *so)
384 {
385 	struct hvs_pcb *pcb;
386 
387 	if (vm_guest != VM_GUEST_HV)
388 		return;
389 
390 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
391 	    "%s: HyperV Socket hvs_trans_detach called\n", __func__);
392 
393 	(void) hvs_trans_lock();
394 	pcb = so2hvspcb(so);
395 	if (pcb == NULL) {
396 		hvs_trans_unlock();
397 		return;
398 	}
399 
400 	if (SOLISTENING(so)) {
401 		bzero(pcb, sizeof(*pcb));
402 		free(pcb, M_HVSOCK);
403 	}
404 
405 	so->so_pcb = NULL;
406 
407 	hvs_trans_unlock();
408 }
409 
410 int
hvs_trans_bind(struct socket * so,struct sockaddr * addr,struct thread * td)411 hvs_trans_bind(struct socket *so, struct sockaddr *addr, struct thread *td)
412 {
413 	struct hvs_pcb *pcb = so2hvspcb(so);
414 	struct sockaddr_hvs *sa = (struct sockaddr_hvs *) addr;
415 	int error = 0;
416 
417 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
418 	    "%s: HyperV Socket hvs_trans_bind called\n", __func__);
419 
420 	if (sa == NULL) {
421 		return (EINVAL);
422 	}
423 
424 	if (pcb == NULL) {
425 		return (EINVAL);
426 	}
427 
428 	if (sa->sa_family != AF_HYPERV) {
429 		HVSOCK_DBG(HVSOCK_DBG_ERR,
430 		    "%s: Not supported, sa_family is %u\n",
431 		    __func__, sa->sa_family);
432 		return (EAFNOSUPPORT);
433 	}
434 	if (sa->sa_len != sizeof(*sa)) {
435 		HVSOCK_DBG(HVSOCK_DBG_ERR,
436 		    "%s: Not supported, sa_len is %u\n",
437 		    __func__, sa->sa_len);
438 		return (EINVAL);
439 	}
440 
441 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
442 	    "%s: binding port = 0x%x\n", __func__, sa->hvs_port);
443 
444 	mtx_lock(&hvs_trans_socks_mtx);
445 	if (__hvs_find_socket_on_list(sa,
446 	    HVS_LIST_BOUND | HVS_LIST_CONNECTED)) {
447 		error = EADDRINUSE;
448 	} else {
449 		/*
450 		 * The address is available for us to bind.
451 		 * Add socket to the bound list.
452 		 */
453 		hvs_addr_set(&pcb->local_addr, sa->hvs_port);
454 		hvs_addr_set(&pcb->remote_addr, HVADDR_PORT_ANY);
455 		__hvs_insert_socket_on_list(so, HVS_LIST_BOUND);
456 	}
457 	mtx_unlock(&hvs_trans_socks_mtx);
458 
459 	return (error);
460 }
461 
462 int
hvs_trans_listen(struct socket * so,int backlog,struct thread * td)463 hvs_trans_listen(struct socket *so, int backlog, struct thread *td)
464 {
465 	struct hvs_pcb *pcb = so2hvspcb(so);
466 	struct socket *bound_so;
467 	int error;
468 
469 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
470 	    "%s: HyperV Socket hvs_trans_listen called\n", __func__);
471 
472 	if (pcb == NULL)
473 		return (EINVAL);
474 
475 	/* Check if the address is already bound and it was by us. */
476 	bound_so = hvs_find_socket_on_list(&pcb->local_addr, HVS_LIST_BOUND);
477 	if (bound_so == NULL || bound_so != so) {
478 		HVSOCK_DBG(HVSOCK_DBG_ERR,
479 		    "%s: Address not bound or not by us.\n", __func__);
480 		return (EADDRNOTAVAIL);
481 	}
482 
483 	SOCK_LOCK(so);
484 	error = solisten_proto_check(so);
485 	if (error == 0)
486 		solisten_proto(so, backlog);
487 	SOCK_UNLOCK(so);
488 
489 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
490 	    "%s: HyperV Socket listen error = %d\n", __func__, error);
491 	return (error);
492 }
493 
494 int
hvs_trans_accept(struct socket * so,struct sockaddr ** nam)495 hvs_trans_accept(struct socket *so, struct sockaddr **nam)
496 {
497 	struct hvs_pcb *pcb = so2hvspcb(so);
498 
499 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
500 	    "%s: HyperV Socket hvs_trans_accept called\n", __func__);
501 
502 	if (pcb == NULL)
503 		return (EINVAL);
504 
505 	*nam = sodupsockaddr((struct sockaddr *) &pcb->remote_addr,
506 	    M_NOWAIT);
507 
508 	return ((*nam == NULL) ? ENOMEM : 0);
509 }
510 
511 int
hvs_trans_connect(struct socket * so,struct sockaddr * nam,struct thread * td)512 hvs_trans_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
513 {
514 	struct hvs_pcb *pcb = so2hvspcb(so);
515 	struct sockaddr_hvs *raddr = (struct sockaddr_hvs *)nam;
516 	bool found_auto_bound_port = false;
517 	int i, error = 0;
518 
519 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
520 	    "%s: HyperV Socket hvs_trans_connect called, remote port is %x\n",
521 	    __func__, raddr->hvs_port);
522 
523 	if (pcb == NULL)
524 		return (EINVAL);
525 
526 	/* Verify the remote address */
527 	if (raddr == NULL)
528 		return (EINVAL);
529 	if (raddr->sa_family != AF_HYPERV)
530 		return (EAFNOSUPPORT);
531 	if (raddr->sa_len != sizeof(*raddr))
532 		return (EINVAL);
533 
534 	mtx_lock(&hvs_trans_socks_mtx);
535 	if (so->so_state &
536 	    (SS_ISCONNECTED|SS_ISDISCONNECTING|SS_ISCONNECTING)) {
537 			HVSOCK_DBG(HVSOCK_DBG_ERR,
538 			    "%s: socket connect in progress\n",
539 			    __func__);
540 			error = EINPROGRESS;
541 			goto out;
542 	}
543 
544 	/*
545 	 * Find an available port for us to auto bind the local
546 	 * address.
547 	 */
548 	hvs_addr_set(&pcb->local_addr, 0);
549 
550 	for (i = previous_auto_bound_port - 1;
551 	    i != previous_auto_bound_port; i --) {
552 		if (i == MIN_PORT)
553 			i = MAX_PORT;
554 
555 		pcb->local_addr.hvs_port = i;
556 
557 		if (__hvs_find_socket_on_list(&pcb->local_addr,
558 		    HVS_LIST_BOUND | HVS_LIST_CONNECTED) == NULL) {
559 			found_auto_bound_port = true;
560 			previous_auto_bound_port = i;
561 			HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
562 			    "%s: found local bound port is %x\n",
563 			    __func__, pcb->local_addr.hvs_port);
564 			break;
565 		}
566 	}
567 
568 	if (found_auto_bound_port == true) {
569 		/* Found available port for auto bound, put on list */
570 		__hvs_insert_socket_on_list(so, HVS_LIST_BOUND);
571 		/* Set VM service ID */
572 		pcb->vm_srv_id = srv_id_template;
573 		set_port_by_srv_id(&pcb->vm_srv_id, pcb->local_addr.hvs_port);
574 		/* Set host service ID and remote port */
575 		pcb->host_srv_id = srv_id_template;
576 		set_port_by_srv_id(&pcb->host_srv_id, raddr->hvs_port);
577 		hvs_addr_set(&pcb->remote_addr, raddr->hvs_port);
578 
579 		/* Change the socket state to SS_ISCONNECTING */
580 		soisconnecting(so);
581 	} else {
582 		HVSOCK_DBG(HVSOCK_DBG_ERR,
583 		    "%s: No local port available for auto bound\n",
584 		    __func__);
585 		error = EADDRINUSE;
586 	}
587 
588 	HVSOCK_DBG(HVSOCK_DBG_INFO, "Connect vm_srv_id is ");
589 	hvsock_print_guid(&pcb->vm_srv_id);
590 	HVSOCK_DBG(HVSOCK_DBG_INFO, "Connect host_srv_id is ");
591 	hvsock_print_guid(&pcb->host_srv_id);
592 
593 out:
594 	mtx_unlock(&hvs_trans_socks_mtx);
595 
596 	if (found_auto_bound_port == true)
597 		 vmbus_req_tl_connect(&pcb->vm_srv_id, &pcb->host_srv_id);
598 
599 	return (error);
600 }
601 
602 int
hvs_trans_disconnect(struct socket * so)603 hvs_trans_disconnect(struct socket *so)
604 {
605 	struct hvs_pcb *pcb;
606 
607 	if (vm_guest != VM_GUEST_HV)
608 		return (ESOCKTNOSUPPORT);
609 
610 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
611 	    "%s: HyperV Socket hvs_trans_disconnect called\n", __func__);
612 
613 	(void) hvs_trans_lock();
614 	pcb = so2hvspcb(so);
615 	if (pcb == NULL) {
616 		hvs_trans_unlock();
617 		return (EINVAL);
618 	}
619 
620 	/* If socket is already disconnected, skip this */
621 	if ((so->so_state & SS_ISDISCONNECTED) == 0)
622 		soisdisconnecting(so);
623 
624 	hvs_trans_unlock();
625 
626 	return (0);
627 }
628 
629 struct hvs_callback_arg {
630 	struct uio *uio;
631 	struct sockbuf *sb;
632 };
633 
634 int
hvs_trans_soreceive(struct socket * so,struct sockaddr ** paddr,struct uio * uio,struct mbuf ** mp0,struct mbuf ** controlp,int * flagsp)635 hvs_trans_soreceive(struct socket *so, struct sockaddr **paddr,
636     struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
637 {
638 	struct hvs_pcb *pcb = so2hvspcb(so);
639 	struct sockbuf *sb;
640 	ssize_t orig_resid;
641 	uint32_t canread, to_read;
642 	int flags, error = 0;
643 	struct hvs_callback_arg cbarg;
644 
645 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
646 	    "%s: HyperV Socket hvs_trans_soreceive called\n", __func__);
647 
648 	if (so->so_type != SOCK_STREAM)
649 		return (EINVAL);
650 	if (pcb == NULL)
651 		return (EINVAL);
652 
653 	if (flagsp != NULL)
654 		flags = *flagsp &~ MSG_EOR;
655 	else
656 		flags = 0;
657 
658 	if (flags & MSG_PEEK)
659 		return (EOPNOTSUPP);
660 
661 	/* If no space to copy out anything */
662 	if (uio->uio_resid == 0 || uio->uio_rw != UIO_READ)
663 		return (EINVAL);
664 
665 	orig_resid = uio->uio_resid;
666 
667 	/* Prevent other readers from entering the socket. */
668 	error = SOCK_IO_RECV_LOCK(so, SBLOCKWAIT(flags));
669 	if (error) {
670 		HVSOCK_DBG(HVSOCK_DBG_ERR,
671 		    "%s: soiolock returned error = %d\n", __func__, error);
672 		return (error);
673 	}
674 
675 	sb = &so->so_rcv;
676 	SOCKBUF_LOCK(sb);
677 
678 	cbarg.uio = uio;
679 	cbarg.sb = sb;
680 	/*
681 	 * If the socket is closing, there might still be some data
682 	 * in rx br to read. However we need to make sure
683 	 * the channel is still open.
684 	 */
685 	if ((sb->sb_state & SBS_CANTRCVMORE) &&
686 	    (so->so_state & SS_ISDISCONNECTED)) {
687 		/* Other thread already closed the channel */
688 		error = EPIPE;
689 		goto out;
690 	}
691 
692 	while (true) {
693 		while (uio->uio_resid > 0 &&
694 		    (canread = hvsock_canread_check(pcb)) > 0) {
695 			to_read = MIN(canread, uio->uio_resid);
696 			HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
697 			    "%s: to_read = %u, skip = %u\n", __func__, to_read,
698 			    (unsigned int)(sizeof(struct hvs_pkt_header) +
699 			    pcb->recv_data_off));
700 
701 			error = vmbus_chan_recv_peek_call(pcb->chan, to_read,
702 			    sizeof(struct hvs_pkt_header) + pcb->recv_data_off,
703 			    hvsock_br_callback, (void *)&cbarg);
704 			/*
705 			 * It is possible socket is disconnected becasue
706 			 * we released lock in hvsock_br_callback. So we
707 			 * need to check the state to make sure it is not
708 			 * disconnected.
709 			 */
710 			if (error || so->so_state & SS_ISDISCONNECTED) {
711 				break;
712 			}
713 
714 			pcb->recv_data_len -= to_read;
715 			pcb->recv_data_off += to_read;
716 		}
717 
718 		if (error)
719 			break;
720 
721 		/* Abort if socket has reported problems. */
722 		if (so->so_error) {
723 			if (so->so_error == ESHUTDOWN &&
724 			    orig_resid > uio->uio_resid) {
725 				/*
726 				 * Although we got a FIN, we also received
727 				 * some data in this round. Delivery it
728 				 * to user.
729 				 */
730 				error = 0;
731 			} else {
732 				if (so->so_error != ESHUTDOWN)
733 					error = so->so_error;
734 			}
735 
736 			break;
737 		}
738 
739 		/* Cannot received more. */
740 		if (sb->sb_state & SBS_CANTRCVMORE)
741 			break;
742 
743 		/* We are done if buffer has been filled */
744 		if (uio->uio_resid == 0)
745 			break;
746 
747 		if (!(flags & MSG_WAITALL) && orig_resid > uio->uio_resid)
748 			break;
749 
750 		/* Buffer ring is empty and we shall not block */
751 		if ((so->so_state & SS_NBIO) ||
752 		    (flags & (MSG_DONTWAIT|MSG_NBIO))) {
753 			if (orig_resid == uio->uio_resid) {
754 				/* We have not read anything */
755 				error = EAGAIN;
756 			}
757 			HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
758 			    "%s: non blocked read return, error %d.\n",
759 			    __func__, error);
760 			break;
761 		}
762 
763 		/*
764 		 * Wait and block until (more) data comes in.
765 		 * Note: Drops the sockbuf lock during wait.
766 		 */
767 		error = sbwait(sb);
768 
769 		if (error)
770 			break;
771 
772 		HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
773 		    "%s: wake up from sbwait, read available is %u\n",
774 		    __func__, vmbus_chan_read_available(pcb->chan));
775 	}
776 
777 out:
778 	SOCKBUF_UNLOCK(sb);
779 	SOCK_IO_RECV_UNLOCK(so);
780 
781 	/* We recieved a FIN in this call */
782 	if (so->so_error == ESHUTDOWN) {
783 		if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
784 			/* Send has already closed */
785 			soisdisconnecting(so);
786 		} else {
787 			/* Just close the receive side */
788 			socantrcvmore(so);
789 		}
790 	}
791 
792 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
793 	    "%s: returning error = %d, so_error = %d\n",
794 	    __func__, error, so->so_error);
795 
796 	return (error);
797 }
798 
799 int
hvs_trans_sosend(struct socket * so,struct sockaddr * addr,struct uio * uio,struct mbuf * top,struct mbuf * controlp,int flags,struct thread * td)800 hvs_trans_sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
801     struct mbuf *top, struct mbuf *controlp, int flags, struct thread *td)
802 {
803 	struct hvs_pcb *pcb = so2hvspcb(so);
804 	struct sockbuf *sb;
805 	ssize_t orig_resid;
806 	uint32_t canwrite, to_write;
807 	int error = 0;
808 
809 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
810 	    "%s: HyperV Socket hvs_trans_sosend called, uio_resid = %zd\n",
811 	    __func__, uio->uio_resid);
812 
813 	if (so->so_type != SOCK_STREAM)
814 		return (EINVAL);
815 	if (pcb == NULL)
816 		return (EINVAL);
817 
818 	/* If nothing to send */
819 	if (uio->uio_resid == 0 || uio->uio_rw != UIO_WRITE)
820 		return (EINVAL);
821 
822 	orig_resid = uio->uio_resid;
823 
824 	/* Prevent other writers from entering the socket. */
825 	error = SOCK_IO_SEND_LOCK(so, SBLOCKWAIT(flags));
826 	if (error) {
827 		HVSOCK_DBG(HVSOCK_DBG_ERR,
828 		    "%s: soiolocak returned error = %d\n", __func__, error);
829 		return (error);
830 	}
831 
832 	sb = &so->so_snd;
833 	SOCKBUF_LOCK(sb);
834 
835 	if ((sb->sb_state & SBS_CANTSENDMORE) ||
836 	    so->so_error == ESHUTDOWN) {
837 		error = EPIPE;
838 		goto out;
839 	}
840 
841 	while (uio->uio_resid > 0) {
842 		canwrite = hvsock_canwrite_check(pcb);
843 		if (canwrite == 0) {
844 			/* We have sent some data */
845 			if (orig_resid > uio->uio_resid)
846 				break;
847 			/*
848 			 * We have not sent any data and it is
849 			 * non-blocked io
850 			 */
851 			if (so->so_state & SS_NBIO ||
852 			    (flags & (MSG_NBIO | MSG_DONTWAIT)) != 0) {
853 				error = EWOULDBLOCK;
854 				break;
855 			} else {
856 				/*
857 				 * We are here because there is no space on
858 				 * send buffer ring. Signal the other side
859 				 * to read and free more space.
860 				 * Sleep wait until space avaiable to send
861 				 * Note: Drops the sockbuf lock during wait.
862 				 */
863 				error = sbwait(sb);
864 
865 				if (error)
866 					break;
867 
868 				HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
869 				    "%s: wake up from sbwait, space avail on "
870 				    "tx ring is %u\n",
871 				    __func__,
872 				    vmbus_chan_write_available(pcb->chan));
873 
874 				continue;
875 			}
876 		}
877 		to_write = MIN(canwrite, uio->uio_resid);
878 		to_write = MIN(to_write, HVSOCK_SEND_BUF_SZ);
879 
880 		HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
881 		    "%s: canwrite is %u, to_write = %u\n", __func__,
882 		    canwrite, to_write);
883 		error = hvsock_send_data(pcb->chan, uio, to_write, sb);
884 
885 		if (error)
886 			break;
887 	}
888 
889 out:
890 	SOCKBUF_UNLOCK(sb);
891 	SOCK_IO_SEND_UNLOCK(so);
892 
893 	return (error);
894 }
895 
896 int
hvs_trans_peeraddr(struct socket * so,struct sockaddr ** nam)897 hvs_trans_peeraddr(struct socket *so, struct sockaddr **nam)
898 {
899 	struct hvs_pcb *pcb = so2hvspcb(so);
900 
901 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
902 	    "%s: HyperV Socket hvs_trans_peeraddr called\n", __func__);
903 
904 	if (pcb == NULL)
905 		return (EINVAL);
906 
907 	*nam = sodupsockaddr((struct sockaddr *) &pcb->remote_addr, M_NOWAIT);
908 
909 	return ((*nam == NULL)? ENOMEM : 0);
910 }
911 
912 int
hvs_trans_sockaddr(struct socket * so,struct sockaddr ** nam)913 hvs_trans_sockaddr(struct socket *so, struct sockaddr **nam)
914 {
915 	struct hvs_pcb *pcb = so2hvspcb(so);
916 
917 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
918 	    "%s: HyperV Socket hvs_trans_sockaddr called\n", __func__);
919 
920 	if (pcb == NULL)
921 		return (EINVAL);
922 
923 	*nam = sodupsockaddr((struct sockaddr *) &pcb->local_addr, M_NOWAIT);
924 
925 	return ((*nam == NULL)? ENOMEM : 0);
926 }
927 
928 void
hvs_trans_close(struct socket * so)929 hvs_trans_close(struct socket *so)
930 {
931 	struct hvs_pcb *pcb;
932 
933 	if (vm_guest != VM_GUEST_HV)
934 		return;
935 
936 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
937 	    "%s: HyperV Socket hvs_trans_close called\n", __func__);
938 
939 	(void) hvs_trans_lock();
940 	pcb = so2hvspcb(so);
941 	if (!pcb) {
942 		hvs_trans_unlock();
943 		return;
944 	}
945 
946 	if (so->so_state & SS_ISCONNECTED) {
947 		/* Send a FIN to peer */
948 		HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
949 		    "%s: hvs_trans_close sending a FIN to host\n", __func__);
950 		(void) hvsock_send_data(pcb->chan, NULL, 0, NULL);
951 	}
952 
953 	if (so->so_state &
954 	    (SS_ISCONNECTED|SS_ISCONNECTING|SS_ISDISCONNECTING))
955 		soisdisconnected(so);
956 
957 	pcb->chan = NULL;
958 	pcb->so = NULL;
959 
960 	if (SOLISTENING(so)) {
961 		mtx_lock(&hvs_trans_socks_mtx);
962 		/* Remove from bound list */
963 		__hvs_remove_socket_from_list(so, HVS_LIST_BOUND);
964 		mtx_unlock(&hvs_trans_socks_mtx);
965 	}
966 
967 	hvs_trans_unlock();
968 
969 	return;
970 }
971 
972 void
hvs_trans_abort(struct socket * so)973 hvs_trans_abort(struct socket *so)
974 {
975 	struct hvs_pcb *pcb = so2hvspcb(so);
976 
977 	if (vm_guest != VM_GUEST_HV)
978 		return;
979 
980 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
981 	    "%s: HyperV Socket hvs_trans_abort called\n", __func__);
982 
983 	(void) hvs_trans_lock();
984 	if (pcb == NULL) {
985 		hvs_trans_unlock();
986 		return;
987 	}
988 
989 	if (SOLISTENING(so)) {
990 		mtx_lock(&hvs_trans_socks_mtx);
991 		/* Remove from bound list */
992 		__hvs_remove_socket_from_list(so, HVS_LIST_BOUND);
993 		mtx_unlock(&hvs_trans_socks_mtx);
994 	}
995 
996 	if (so->so_state & SS_ISCONNECTED) {
997 		(void) sodisconnect(so);
998 	}
999 	hvs_trans_unlock();
1000 
1001 	return;
1002 }
1003 
1004 int
hvs_trans_shutdown(struct socket * so)1005 hvs_trans_shutdown(struct socket *so)
1006 {
1007 	struct hvs_pcb *pcb = so2hvspcb(so);
1008 	struct sockbuf *sb;
1009 
1010 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
1011 	    "%s: HyperV Socket hvs_trans_shutdown called\n", __func__);
1012 
1013 	if (pcb == NULL)
1014 		return (EINVAL);
1015 
1016 	/*
1017 	 * Only get called with the shutdown method is SHUT_WR or
1018 	 * SHUT_RDWR.
1019 	 * When the method is SHUT_RD or SHUT_RDWR, the caller
1020 	 * already set the SBS_CANTRCVMORE on receive side socket
1021 	 * buffer.
1022 	 */
1023 	if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0) {
1024 		/*
1025 		 * SHUT_WR only case.
1026 		 * Receive side is still open. Just close
1027 		 * the send side.
1028 		 */
1029 		socantsendmore(so);
1030 	} else {
1031 		/* SHUT_RDWR case */
1032 		if (so->so_state & SS_ISCONNECTED) {
1033 			/* Send a FIN to peer */
1034 			sb = &so->so_snd;
1035 			SOCKBUF_LOCK(sb);
1036 			(void) hvsock_send_data(pcb->chan, NULL, 0, sb);
1037 			SOCKBUF_UNLOCK(sb);
1038 
1039 			soisdisconnecting(so);
1040 		}
1041 	}
1042 
1043 	return (0);
1044 }
1045 
1046 /* In the VM, we support Hyper-V Sockets with AF_HYPERV, and the endpoint is
1047  * <port> (see struct sockaddr_hvs).
1048  *
1049  * On the host, Hyper-V Sockets are supported by Winsock AF_HYPERV:
1050  * https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/user-
1051  * guide/make-integration-service, and the endpoint is <VmID, ServiceId> with
1052  * the below sockaddr:
1053  *
1054  * struct SOCKADDR_HV
1055  * {
1056  *    ADDRESS_FAMILY Family;
1057  *    USHORT Reserved;
1058  *    GUID VmId;
1059  *    GUID ServiceId;
1060  * };
1061  * Note: VmID is not used by FreeBSD VM and actually it isn't transmitted via
1062  * VMBus, because here it's obvious the host and the VM can easily identify
1063  * each other. Though the VmID is useful on the host, especially in the case
1064  * of Windows container, FreeBSD VM doesn't need it at all.
1065  *
1066  * To be compatible with similar infrastructure in Linux VMs, we have
1067  * to limit the available GUID space of SOCKADDR_HV so that we can create
1068  * a mapping between FreeBSD AF_HYPERV port and SOCKADDR_HV Service GUID.
1069  * The rule of writing Hyper-V Sockets apps on the host and in FreeBSD VM is:
1070  *
1071  ****************************************************************************
1072  * The only valid Service GUIDs, from the perspectives of both the host and *
1073  * FreeBSD VM, that can be connected by the other end, must conform to this *
1074  * format: <port>-facb-11e6-bd58-64006a7986d3.                              *
1075  ****************************************************************************
1076  *
1077  * When we write apps on the host to connect(), the GUID ServiceID is used.
1078  * When we write apps in FreeBSD VM to connect(), we only need to specify the
1079  * port and the driver will form the GUID and use that to request the host.
1080  *
1081  * From the perspective of FreeBSD VM, the remote ephemeral port (i.e. the
1082  * auto-generated remote port for a connect request initiated by the host's
1083  * connect()) is set to HVADDR_PORT_UNKNOWN, which is not realy used on the
1084  * FreeBSD guest.
1085  */
1086 
1087 /*
1088  * Older HyperV hosts (vmbus version 'VMBUS_VERSION_WIN10' or before)
1089  * restricts HyperV socket ring buffer size to six 4K pages. Newer
1090  * HyperV hosts doen't have this limit.
1091  */
1092 #define HVS_RINGBUF_RCV_SIZE	(PAGE_SIZE * 6)
1093 #define HVS_RINGBUF_SND_SIZE	(PAGE_SIZE * 6)
1094 #define HVS_RINGBUF_MAX_SIZE	(PAGE_SIZE * 64)
1095 
1096 struct hvsock_sc {
1097 	device_t		dev;
1098 	struct hvs_pcb		*pcb;
1099 	struct vmbus_channel	*channel;
1100 };
1101 
1102 static bool
hvsock_chan_readable(struct vmbus_channel * chan)1103 hvsock_chan_readable(struct vmbus_channel *chan)
1104 {
1105 	uint32_t readable = vmbus_chan_read_available(chan);
1106 
1107 	return (readable >= HVSOCK_PKT_LEN(0));
1108 }
1109 
1110 static void
hvsock_chan_cb(struct vmbus_channel * chan,void * context)1111 hvsock_chan_cb(struct vmbus_channel *chan, void *context)
1112 {
1113 	struct hvs_pcb *pcb = (struct hvs_pcb *) context;
1114 	struct socket *so;
1115 	uint32_t canwrite;
1116 
1117 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
1118 	    "%s: host send us a wakeup on rb data, pcb = %p\n",
1119 	    __func__, pcb);
1120 
1121 	/*
1122 	 * Check if the socket is still attached and valid.
1123 	 * Here we know channel is still open. Need to make
1124 	 * sure the socket has not been closed or freed.
1125 	 */
1126 	(void) hvs_trans_lock();
1127 	so = hsvpcb2so(pcb);
1128 
1129 	if (pcb->chan != NULL && so != NULL) {
1130 		/*
1131 		 * Wake up reader if there are data to read.
1132 		 */
1133 		SOCKBUF_LOCK(&(so)->so_rcv);
1134 
1135 		HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
1136 		    "%s: read available = %u\n", __func__,
1137 		    vmbus_chan_read_available(pcb->chan));
1138 
1139 		if (hvsock_chan_readable(pcb->chan))
1140 			sorwakeup_locked(so);
1141 		else
1142 			SOCKBUF_UNLOCK(&(so)->so_rcv);
1143 
1144 		/*
1145 		 * Wake up sender if space becomes available to write.
1146 		 */
1147 		SOCKBUF_LOCK(&(so)->so_snd);
1148 		canwrite = hvsock_canwrite_check(pcb);
1149 
1150 		HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
1151 		    "%s: canwrite = %u\n", __func__, canwrite);
1152 
1153 		if (canwrite > 0) {
1154 			sowwakeup_locked(so);
1155 		} else {
1156 			SOCKBUF_UNLOCK(&(so)->so_snd);
1157 		}
1158 	}
1159 
1160 	hvs_trans_unlock();
1161 
1162 	return;
1163 }
1164 
1165 static int
hvsock_br_callback(void * datap,int cplen,void * cbarg)1166 hvsock_br_callback(void *datap, int cplen, void *cbarg)
1167 {
1168 	struct hvs_callback_arg *arg = (struct hvs_callback_arg *)cbarg;
1169 	struct uio *uio = arg->uio;
1170 	struct sockbuf *sb = arg->sb;
1171 	int error = 0;
1172 
1173 	if (cbarg == NULL || datap == NULL)
1174 		return (EINVAL);
1175 
1176 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
1177 	    "%s: called, uio_rw = %s, uio_resid = %zd, cplen = %u, "
1178 	    "datap = %p\n",
1179 	    __func__, (uio->uio_rw == UIO_READ) ? "read from br":"write to br",
1180 	    uio->uio_resid, cplen, datap);
1181 
1182 	if (sb)
1183 		SOCKBUF_UNLOCK(sb);
1184 
1185 	error = uiomove(datap, cplen, uio);
1186 
1187 	if (sb)
1188 		SOCKBUF_LOCK(sb);
1189 
1190 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
1191 	    "%s: after uiomove, uio_resid = %zd, error = %d\n",
1192 	    __func__, uio->uio_resid, error);
1193 
1194 	return (error);
1195 }
1196 
1197 static int
hvsock_send_data(struct vmbus_channel * chan,struct uio * uio,uint32_t to_write,struct sockbuf * sb)1198 hvsock_send_data(struct vmbus_channel *chan, struct uio *uio,
1199     uint32_t to_write, struct sockbuf *sb)
1200 {
1201 	struct hvs_pkt_header hvs_pkt;
1202 	int hvs_pkthlen, hvs_pktlen, pad_pktlen, hlen, error = 0;
1203 	uint64_t pad = 0;
1204 	struct iovec iov[3];
1205 	struct hvs_callback_arg cbarg;
1206 
1207 	if (chan == NULL)
1208 		return (ENOTCONN);
1209 
1210 	hlen = sizeof(struct vmbus_chanpkt_hdr);
1211 	hvs_pkthlen = sizeof(struct hvs_pkt_header);
1212 	hvs_pktlen = hvs_pkthlen + to_write;
1213 	pad_pktlen = VMBUS_CHANPKT_TOTLEN(hvs_pktlen);
1214 
1215 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
1216 	    "%s: hlen = %u, hvs_pkthlen = %u, hvs_pktlen = %u, "
1217 	    "pad_pktlen = %u, data_len = %u\n",
1218 	    __func__, hlen, hvs_pkthlen, hvs_pktlen, pad_pktlen, to_write);
1219 
1220 	hvs_pkt.chan_pkt_hdr.cph_type = VMBUS_CHANPKT_TYPE_INBAND;
1221 	hvs_pkt.chan_pkt_hdr.cph_flags = 0;
1222 	VMBUS_CHANPKT_SETLEN(hvs_pkt.chan_pkt_hdr.cph_hlen, hlen);
1223 	VMBUS_CHANPKT_SETLEN(hvs_pkt.chan_pkt_hdr.cph_tlen, pad_pktlen);
1224 	hvs_pkt.chan_pkt_hdr.cph_xactid = 0;
1225 
1226 	hvs_pkt.vmpipe_pkt_hdr.vmpipe_pkt_type = 1;
1227 	hvs_pkt.vmpipe_pkt_hdr.vmpipe_data_size = to_write;
1228 
1229 	cbarg.uio = uio;
1230 	cbarg.sb = sb;
1231 
1232 	if (uio && to_write > 0) {
1233 		iov[0].iov_base = &hvs_pkt;
1234 		iov[0].iov_len = hvs_pkthlen;
1235 		iov[1].iov_base = NULL;
1236 		iov[1].iov_len = to_write;
1237 		iov[2].iov_base = &pad;
1238 		iov[2].iov_len = pad_pktlen - hvs_pktlen;
1239 
1240 		error = vmbus_chan_iov_send(chan, iov, 3,
1241 		    hvsock_br_callback, &cbarg);
1242 	} else {
1243 		if (to_write == 0) {
1244 			iov[0].iov_base = &hvs_pkt;
1245 			iov[0].iov_len = hvs_pkthlen;
1246 			iov[1].iov_base = &pad;
1247 			iov[1].iov_len = pad_pktlen - hvs_pktlen;
1248 			error = vmbus_chan_iov_send(chan, iov, 2, NULL, NULL);
1249 		}
1250 	}
1251 
1252 	if (error) {
1253 		HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
1254 		    "%s: error = %d\n", __func__, error);
1255 	}
1256 
1257 	return (error);
1258 }
1259 
1260 /*
1261  * Check if we have data on current ring buffer to read
1262  * or not. If not, advance the ring buffer read index to
1263  * next packet. Update the recev_data_len and recev_data_off
1264  * to new value.
1265  * Return the number of bytes can read.
1266  */
1267 static uint32_t
hvsock_canread_check(struct hvs_pcb * pcb)1268 hvsock_canread_check(struct hvs_pcb *pcb)
1269 {
1270 	uint32_t advance;
1271 	uint32_t tlen, hlen, dlen;
1272 	uint32_t bytes_canread = 0;
1273 	int error;
1274 
1275 	if (pcb == NULL || pcb->chan == NULL) {
1276 		pcb->so->so_error = EIO;
1277 		return (0);
1278 	}
1279 
1280 	/* Still have data not read yet on current packet */
1281 	if (pcb->recv_data_len > 0)
1282 		return (pcb->recv_data_len);
1283 
1284 	if (pcb->rb_init)
1285 		advance =
1286 		    VMBUS_CHANPKT_GETLEN(pcb->hvs_pkt.chan_pkt_hdr.cph_tlen);
1287 	else
1288 		advance = 0;
1289 
1290 	bytes_canread = vmbus_chan_read_available(pcb->chan);
1291 
1292 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
1293 	    "%s: bytes_canread on br = %u, advance = %u\n",
1294 	    __func__, bytes_canread, advance);
1295 
1296 	if (pcb->rb_init && bytes_canread == (advance + sizeof(uint64_t))) {
1297 		/*
1298 		 * Nothing to read. Need to advance the rindex before
1299 		 * calling sbwait, so host knows to wake us up when data
1300 		 * is available to read on rb.
1301 		 */
1302 		error = vmbus_chan_recv_idxadv(pcb->chan, advance);
1303 		if (error) {
1304 			HVSOCK_DBG(HVSOCK_DBG_ERR,
1305 			    "%s: after calling vmbus_chan_recv_idxadv, "
1306 			    "got error = %d\n",  __func__, error);
1307 			return (0);
1308 		} else {
1309 			pcb->rb_init = false;
1310 			pcb->recv_data_len = 0;
1311 			pcb->recv_data_off = 0;
1312 			bytes_canread = vmbus_chan_read_available(pcb->chan);
1313 
1314 			HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
1315 			    "%s: advanced %u bytes, "
1316 			    " bytes_canread on br now = %u\n",
1317 			    __func__, advance, bytes_canread);
1318 
1319 			if (bytes_canread == 0)
1320 				return (0);
1321 			else
1322 				advance = 0;
1323 		}
1324 	}
1325 
1326 	if (bytes_canread <
1327 	    advance + (sizeof(struct hvs_pkt_header) + sizeof(uint64_t)))
1328 		return (0);
1329 
1330 	error = vmbus_chan_recv_peek(pcb->chan, &pcb->hvs_pkt,
1331 	    sizeof(struct hvs_pkt_header), advance);
1332 
1333 	/* Don't have anything to read */
1334 	if (error) {
1335 		HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
1336 		    "%s: after calling vmbus_chan_recv_peek, got error = %d\n",
1337 		    __func__, error);
1338 		return (0);
1339 	}
1340 
1341 	/*
1342 	 * We just read in a new packet header. Do some sanity checks.
1343 	 */
1344 	tlen = VMBUS_CHANPKT_GETLEN(pcb->hvs_pkt.chan_pkt_hdr.cph_tlen);
1345 	hlen = VMBUS_CHANPKT_GETLEN(pcb->hvs_pkt.chan_pkt_hdr.cph_hlen);
1346 	dlen = pcb->hvs_pkt.vmpipe_pkt_hdr.vmpipe_data_size;
1347 	if (__predict_false(hlen < sizeof(struct vmbus_chanpkt_hdr)) ||
1348 	    __predict_false(hlen > tlen) ||
1349 	    __predict_false(tlen < dlen + sizeof(struct hvs_pkt_header))) {
1350 		HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
1351 		    "invalid tlen(%u), hlen(%u) or dlen(%u)\n",
1352 		    tlen, hlen, dlen);
1353 		pcb->so->so_error = EIO;
1354 		return (0);
1355 	}
1356 	if (pcb->rb_init == false)
1357 		pcb->rb_init = true;
1358 
1359 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
1360 	    "Got new pkt tlen(%u), hlen(%u) or dlen(%u)\n",
1361 	    tlen, hlen, dlen);
1362 
1363 	/* The other side has sent a close FIN */
1364 	if (dlen == 0) {
1365 		HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
1366 		    "%s: Received FIN from other side\n", __func__);
1367 		/* inform the caller by seting so_error to ESHUTDOWN */
1368 		pcb->so->so_error = ESHUTDOWN;
1369 	}
1370 
1371 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
1372 	    "%s: canread on receive ring is %u \n", __func__, dlen);
1373 
1374 	pcb->recv_data_len = dlen;
1375 	pcb->recv_data_off = 0;
1376 
1377 	return (pcb->recv_data_len);
1378 }
1379 
1380 static uint32_t
hvsock_canwrite_check(struct hvs_pcb * pcb)1381 hvsock_canwrite_check(struct hvs_pcb *pcb)
1382 {
1383 	uint32_t writeable;
1384 	uint32_t ret;
1385 
1386 	if (pcb == NULL || pcb->chan == NULL)
1387 		return (0);
1388 
1389 	writeable = vmbus_chan_write_available(pcb->chan);
1390 
1391 	/*
1392 	 * We must always reserve a 0-length-payload packet for the FIN.
1393 	 */
1394 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
1395 	    "%s: writeable is %u, should be greater than %ju\n",
1396 	    __func__, writeable,
1397 	    (uintmax_t)(HVSOCK_PKT_LEN(1) + HVSOCK_PKT_LEN(0)));
1398 
1399 	if (writeable < HVSOCK_PKT_LEN(1) + HVSOCK_PKT_LEN(0)) {
1400 		/*
1401 		 * The Tx ring seems full.
1402 		 */
1403 		return (0);
1404 	}
1405 
1406 	ret = writeable - HVSOCK_PKT_LEN(0) - HVSOCK_PKT_LEN(0);
1407 
1408 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
1409 	    "%s: available size is %u\n", __func__, rounddown2(ret, 8));
1410 
1411 	return (rounddown2(ret, 8));
1412 }
1413 
1414 static void
hvsock_set_chan_pending_send_size(struct vmbus_channel * chan)1415 hvsock_set_chan_pending_send_size(struct vmbus_channel *chan)
1416 {
1417 	vmbus_chan_set_pending_send_size(chan,
1418 	    HVSOCK_PKT_LEN(HVSOCK_SEND_BUF_SZ));
1419 }
1420 
1421 static int
hvsock_open_channel(struct vmbus_channel * chan,struct socket * so)1422 hvsock_open_channel(struct vmbus_channel *chan, struct socket *so)
1423 {
1424 	unsigned int rcvbuf, sndbuf;
1425 	struct hvs_pcb *pcb = so2hvspcb(so);
1426 	int ret;
1427 
1428 	if (vmbus_current_version < VMBUS_VERSION_WIN10_V5) {
1429 		sndbuf = HVS_RINGBUF_SND_SIZE;
1430 		rcvbuf = HVS_RINGBUF_RCV_SIZE;
1431 	} else {
1432 		sndbuf = MAX(so->so_snd.sb_hiwat, HVS_RINGBUF_SND_SIZE);
1433 		sndbuf = MIN(sndbuf, HVS_RINGBUF_MAX_SIZE);
1434 		sndbuf = rounddown2(sndbuf, PAGE_SIZE);
1435 		rcvbuf = MAX(so->so_rcv.sb_hiwat, HVS_RINGBUF_RCV_SIZE);
1436 		rcvbuf = MIN(rcvbuf, HVS_RINGBUF_MAX_SIZE);
1437 		rcvbuf = rounddown2(rcvbuf, PAGE_SIZE);
1438 	}
1439 
1440 	/*
1441 	 * Can only read whatever user provided size of data
1442 	 * from ring buffer. Turn off batched reading.
1443 	 */
1444 	vmbus_chan_set_readbatch(chan, false);
1445 
1446 	ret = vmbus_chan_open(chan, sndbuf, rcvbuf, NULL, 0,
1447 	    hvsock_chan_cb, pcb);
1448 
1449 	if (ret != 0) {
1450 		HVSOCK_DBG(HVSOCK_DBG_ERR,
1451 		    "%s: failed to open hvsock channel, sndbuf = %u, "
1452 		    "rcvbuf = %u\n", __func__, sndbuf, rcvbuf);
1453 	} else {
1454 		HVSOCK_DBG(HVSOCK_DBG_INFO,
1455 		    "%s: hvsock channel opened, sndbuf = %u, i"
1456 		    "rcvbuf = %u\n", __func__, sndbuf, rcvbuf);
1457 		/*
1458 		 * Se the pending send size so to receive wakeup
1459 		 * signals from host when there is enough space on
1460 		 * rx buffer ring to write.
1461 		 */
1462 		hvsock_set_chan_pending_send_size(chan);
1463 	}
1464 
1465 	return ret;
1466 }
1467 
1468 /*
1469  * Guest is listening passively on the socket. Open channel and
1470  * create a new socket for the conneciton.
1471  */
1472 static void
hvsock_open_conn_passive(struct vmbus_channel * chan,struct socket * so,struct hvsock_sc * sc)1473 hvsock_open_conn_passive(struct vmbus_channel *chan, struct socket *so,
1474     struct hvsock_sc *sc)
1475 {
1476 	struct socket *new_so;
1477 	struct hvs_pcb *new_pcb, *pcb;
1478 	int error;
1479 
1480 	/* Do nothing if socket is not listening */
1481 	if (!SOLISTENING(so)) {
1482 		HVSOCK_DBG(HVSOCK_DBG_ERR,
1483 		    "%s: socket is not a listening one\n", __func__);
1484 		return;
1485 	}
1486 
1487 	/*
1488 	 * Create a new socket. This will call pru_attach to complete
1489 	 * the socket initialization and put the new socket onto
1490 	 * listening socket's sol_incomp list, waiting to be promoted
1491 	 * to sol_comp list.
1492 	 * The new socket created has ref count 0. There is no other
1493 	 * thread that changes the state of this new one at the
1494 	 * moment, so we don't need to hold its lock while opening
1495 	 * channel and filling out its pcb information.
1496 	 */
1497 	new_so = sonewconn(so, 0);
1498 	if (!new_so)
1499 		HVSOCK_DBG(HVSOCK_DBG_ERR,
1500 		    "%s: creating new socket failed\n", __func__);
1501 
1502 	/*
1503 	 * Now open the vmbus channel. If it fails, the socket will be
1504 	 * on the listening socket's sol_incomp queue until it is
1505 	 * replaced and aborted.
1506 	 */
1507 	error = hvsock_open_channel(chan, new_so);
1508 	if (error) {
1509 		new_so->so_error = error;
1510 		return;
1511 	}
1512 
1513 	pcb = so->so_pcb;
1514 	new_pcb = new_so->so_pcb;
1515 
1516 	hvs_addr_set(&(new_pcb->local_addr), pcb->local_addr.hvs_port);
1517 	/* Remote port is unknown to guest in this type of conneciton */
1518 	hvs_addr_set(&(new_pcb->remote_addr), HVADDR_PORT_UNKNOWN);
1519 	new_pcb->chan = chan;
1520 	new_pcb->recv_data_len = 0;
1521 	new_pcb->recv_data_off = 0;
1522 	new_pcb->rb_init = false;
1523 
1524 	new_pcb->vm_srv_id = *vmbus_chan_guid_type(chan);
1525 	new_pcb->host_srv_id = *vmbus_chan_guid_inst(chan);
1526 
1527 	hvs_insert_socket_on_list(new_so, HVS_LIST_CONNECTED);
1528 
1529 	sc->pcb = new_pcb;
1530 
1531 	/*
1532 	 * Change the socket state to SS_ISCONNECTED. This will promote
1533 	 * the socket to sol_comp queue and wake up the thread which
1534 	 * is accepting connection.
1535 	 */
1536 	soisconnected(new_so);
1537 }
1538 
1539 
1540 /*
1541  * Guest is actively connecting to host.
1542  */
1543 static void
hvsock_open_conn_active(struct vmbus_channel * chan,struct socket * so)1544 hvsock_open_conn_active(struct vmbus_channel *chan, struct socket *so)
1545 {
1546 	struct hvs_pcb *pcb;
1547 	int error;
1548 
1549 	error = hvsock_open_channel(chan, so);
1550 	if (error) {
1551 		so->so_error = error;
1552 		return;
1553 	}
1554 
1555 	pcb = so->so_pcb;
1556 	pcb->chan = chan;
1557 	pcb->recv_data_len = 0;
1558 	pcb->recv_data_off = 0;
1559 	pcb->rb_init = false;
1560 
1561 	mtx_lock(&hvs_trans_socks_mtx);
1562 	__hvs_remove_socket_from_list(so, HVS_LIST_BOUND);
1563 	__hvs_insert_socket_on_list(so, HVS_LIST_CONNECTED);
1564 	mtx_unlock(&hvs_trans_socks_mtx);
1565 
1566 	/*
1567 	 * Change the socket state to SS_ISCONNECTED. This will wake up
1568 	 * the thread sleeping in connect call.
1569 	 */
1570 	soisconnected(so);
1571 }
1572 
1573 static void
hvsock_open_connection(struct vmbus_channel * chan,struct hvsock_sc * sc)1574 hvsock_open_connection(struct vmbus_channel *chan, struct hvsock_sc *sc)
1575 {
1576 	struct hyperv_guid *inst_guid, *type_guid;
1577 	bool conn_from_host;
1578 	struct sockaddr_hvs addr;
1579 	struct socket *so;
1580 	struct hvs_pcb *pcb;
1581 
1582 	type_guid = (struct hyperv_guid *) vmbus_chan_guid_type(chan);
1583 	inst_guid = (struct hyperv_guid *) vmbus_chan_guid_inst(chan);
1584 	conn_from_host = vmbus_chan_is_hvs_conn_from_host(chan);
1585 
1586 	HVSOCK_DBG(HVSOCK_DBG_INFO, "type_guid is ");
1587 	hvsock_print_guid(type_guid);
1588 	HVSOCK_DBG(HVSOCK_DBG_INFO, "inst_guid is ");
1589 	hvsock_print_guid(inst_guid);
1590 	HVSOCK_DBG(HVSOCK_DBG_INFO, "connection %s host\n",
1591 	    (conn_from_host == true ) ? "from" : "to");
1592 
1593 	/*
1594 	 * The listening port should be in [0, MAX_LISTEN_PORT]
1595 	 */
1596 	if (!is_valid_srv_id(type_guid))
1597 		return;
1598 
1599 	/*
1600 	 * There should be a bound socket already created no matter
1601 	 * it is a passive or active connection.
1602 	 * For host initiated connection (passive on guest side),
1603 	 * the  type_guid contains the port which guest is bound and
1604 	 * listening.
1605 	 * For the guest initiated connection (active on guest side),
1606 	 * the inst_guid contains the port that guest has auto bound
1607 	 * to.
1608 	 */
1609 	hvs_addr_init(&addr, conn_from_host ? type_guid : inst_guid);
1610 	so = hvs_find_socket_on_list(&addr, HVS_LIST_BOUND);
1611 	if (!so) {
1612 		HVSOCK_DBG(HVSOCK_DBG_ERR,
1613 		    "%s: no bound socket found for port %u\n",
1614 		    __func__, addr.hvs_port);
1615 		return;
1616 	}
1617 
1618 	if (conn_from_host) {
1619 		hvsock_open_conn_passive(chan, so, sc);
1620 	} else {
1621 		(void) hvs_trans_lock();
1622 		pcb = so->so_pcb;
1623 		if (pcb && pcb->so) {
1624 			sc->pcb = so2hvspcb(so);
1625 			hvsock_open_conn_active(chan, so);
1626 		} else {
1627 			HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
1628 			    "%s: channel detached before open\n", __func__);
1629 		}
1630 		hvs_trans_unlock();
1631 	}
1632 
1633 }
1634 
1635 static int
hvsock_probe(device_t dev)1636 hvsock_probe(device_t dev)
1637 {
1638 	struct vmbus_channel *channel = vmbus_get_channel(dev);
1639 
1640 	if (!channel || !vmbus_chan_is_hvs(channel)) {
1641 		HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
1642 		    "hvsock_probe called but not a hvsock channel id %u\n",
1643 		    vmbus_chan_id(channel));
1644 
1645 		return ENXIO;
1646 	} else {
1647 		HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
1648 		    "hvsock_probe got a hvsock channel id %u\n",
1649 		    vmbus_chan_id(channel));
1650 
1651 		return BUS_PROBE_DEFAULT;
1652 	}
1653 }
1654 
1655 static int
hvsock_attach(device_t dev)1656 hvsock_attach(device_t dev)
1657 {
1658 	struct vmbus_channel *channel = vmbus_get_channel(dev);
1659 	struct hvsock_sc *sc = (struct hvsock_sc *)device_get_softc(dev);
1660 
1661 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE, "hvsock_attach called.\n");
1662 
1663 	hvsock_open_connection(channel, sc);
1664 
1665 	/*
1666 	 * Always return success. On error the host will rescind the device
1667 	 * in 30 seconds and we can do cleanup at that time in
1668 	 * vmbus_chan_msgproc_chrescind().
1669 	 */
1670 	return (0);
1671 }
1672 
1673 static int
hvsock_detach(device_t dev)1674 hvsock_detach(device_t dev)
1675 {
1676 	struct hvsock_sc *sc = (struct hvsock_sc *)device_get_softc(dev);
1677 	struct socket *so;
1678 	int retry;
1679 
1680 	if (bootverbose)
1681 		device_printf(dev, "hvsock_detach called.\n");
1682 
1683 	HVSOCK_DBG(HVSOCK_DBG_VERBOSE, "hvsock_detach called.\n");
1684 
1685 	if (sc->pcb != NULL) {
1686 		(void) hvs_trans_lock();
1687 
1688 		so = hsvpcb2so(sc->pcb);
1689 		if (so) {
1690 			/* Close the connection */
1691 			if (so->so_state &
1692 			    (SS_ISCONNECTED|SS_ISCONNECTING|SS_ISDISCONNECTING))
1693 				soisdisconnected(so);
1694 		}
1695 
1696 		mtx_lock(&hvs_trans_socks_mtx);
1697 		__hvs_remove_pcb_from_list(sc->pcb,
1698 		    HVS_LIST_BOUND | HVS_LIST_CONNECTED);
1699 		mtx_unlock(&hvs_trans_socks_mtx);
1700 
1701 		/*
1702 		 * Close channel while no reader and sender are working
1703 		 * on the buffer rings.
1704 		 */
1705 		if (so) {
1706 			retry = 0;
1707 			while (SOCK_IO_RECV_LOCK(so, 0) == EWOULDBLOCK) {
1708 				/*
1709 				 * Someone is reading, rx br is busy
1710 				 */
1711 				soisdisconnected(so);
1712 				DELAY(500);
1713 				HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
1714 				    "waiting for rx reader to exit, "
1715 				    "retry = %d\n", retry++);
1716 			}
1717 			retry = 0;
1718 			while (SOCK_IO_SEND_LOCK(so, 0) == EWOULDBLOCK) {
1719 				/*
1720 				 * Someone is sending, tx br is busy
1721 				 */
1722 				soisdisconnected(so);
1723 				DELAY(500);
1724 				HVSOCK_DBG(HVSOCK_DBG_VERBOSE,
1725 				    "waiting for tx sender to exit, "
1726 				    "retry = %d\n", retry++);
1727 			}
1728 		}
1729 
1730 
1731 		bzero(sc->pcb, sizeof(struct hvs_pcb));
1732 		free(sc->pcb, M_HVSOCK);
1733 		sc->pcb = NULL;
1734 
1735 		if (so) {
1736 			SOCK_IO_RECV_UNLOCK(so);
1737 			SOCK_IO_SEND_UNLOCK(so);
1738 			so->so_pcb = NULL;
1739 		}
1740 
1741 		hvs_trans_unlock();
1742 	}
1743 
1744 	vmbus_chan_close(vmbus_get_channel(dev));
1745 
1746 	return (0);
1747 }
1748 
1749 static device_method_t hvsock_methods[] = {
1750 	/* Device interface */
1751 	DEVMETHOD(device_probe, hvsock_probe),
1752 	DEVMETHOD(device_attach, hvsock_attach),
1753 	DEVMETHOD(device_detach, hvsock_detach),
1754 	DEVMETHOD_END
1755 };
1756 
1757 static driver_t hvsock_driver = {
1758 	"hv_sock",
1759 	hvsock_methods,
1760 	sizeof(struct hvsock_sc)
1761 };
1762 
1763 static devclass_t hvsock_devclass;
1764 
1765 DRIVER_MODULE(hvsock, vmbus, hvsock_driver, hvsock_devclass, NULL, NULL);
1766 MODULE_VERSION(hvsock, 1);
1767 MODULE_DEPEND(hvsock, vmbus, 1, 1, 1);
1768