xref: /freebsd/sys/dev/hyperv/hvsock/hv_sock.h (revision 1323ec57)
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  * $FreeBSD$
29  */
30 
31 #ifndef _HVSOCK_H
32 #define _HVSOCK_H
33 #include <sys/socket.h>
34 #include <sys/socketvar.h>
35 #include <sys/queue.h>
36 
37 #include <dev/hyperv/include/hyperv.h>
38 #include <dev/hyperv/include/vmbus.h>
39 
40 /*
41  * HyperV Socket Protocols
42  */
43 #define	HYPERV_SOCK_PROTO_TRANS		1	/* Transport protocol */
44 
45 #define	HVADDR_PORT_ANY			-1U
46 #define	HVADDR_PORT_UNKNOWN		-1U
47 
48 #define HVS_LIST_BOUND			0x01
49 #define HVS_LIST_CONNECTED		0x02
50 #define HVS_LIST_ALL			(HVS_LIST_BOUND | HVS_LIST_CONNECTED)
51 
52 struct sockaddr_hvs {
53 	unsigned char	sa_len;
54 	sa_family_t	sa_family;
55 	unsigned int	hvs_port;
56 	unsigned char	hvs_zero[sizeof(struct sockaddr) -
57 				 sizeof(sa_family_t) -
58 				 sizeof(unsigned char) -
59 				 sizeof(unsigned int)];
60 };
61 
62 struct vmpipe_proto_header {
63 	uint32_t			vmpipe_pkt_type;
64 	uint32_t			vmpipe_data_size;
65 } __packed;
66 
67 struct hvs_pkt_header {
68 	struct vmbus_chanpkt_hdr	chan_pkt_hdr;
69 	struct vmpipe_proto_header	vmpipe_pkt_hdr;
70 } __packed;
71 
72 struct hvs_pcb {
73 	struct socket			*so;		/* Pointer to socket */
74 	struct sockaddr_hvs		local_addr;
75 	struct sockaddr_hvs		remote_addr;
76 
77 	struct hyperv_guid		vm_srv_id;
78 	struct hyperv_guid		host_srv_id;
79 
80 	struct vmbus_channel		*chan;
81 	/* Current packet header on rx ring */
82 	struct hvs_pkt_header		hvs_pkt;
83 	/* Available data in receive br in current packet */
84 	uint32_t			recv_data_len;
85 	/* offset in the packet */
86 	uint32_t			recv_data_off;
87 	bool				rb_init;
88 	/* Link lists for global bound and connected sockets */
89 	LIST_ENTRY(hvs_pcb)		bound_next;
90 	LIST_ENTRY(hvs_pcb)		connected_next;
91 };
92 
93 #define so2hvspcb(so) \
94 	((struct hvs_pcb *)((so)->so_pcb))
95 #define hsvpcb2so(hvspcb) \
96 	((struct socket *)((hvspcb)->so))
97 
98 void	hvs_addr_init(struct sockaddr_hvs *, const struct hyperv_guid *);
99 void	hvs_trans_close(struct socket *);
100 void	hvs_trans_detach(struct socket *);
101 void	hvs_trans_abort(struct socket *);
102 int	hvs_trans_attach(struct socket *, int, struct thread *);
103 int	hvs_trans_bind(struct socket *, struct sockaddr *, struct thread *);
104 int	hvs_trans_listen(struct socket *, int, struct thread *);
105 int	hvs_trans_accept(struct socket *, struct sockaddr **);
106 int	hvs_trans_connect(struct socket *,
107 	    struct sockaddr *, struct thread *);
108 int	hvs_trans_peeraddr(struct socket *, struct sockaddr **);
109 int	hvs_trans_sockaddr(struct socket *, struct sockaddr **);
110 int	hvs_trans_soreceive(struct socket *, struct sockaddr **,
111 	    struct uio *, struct mbuf **, struct mbuf **, int *);
112 int	hvs_trans_sosend(struct socket *, struct sockaddr *, struct uio *,
113 	     struct mbuf *, struct mbuf *, int, struct thread *);
114 int	hvs_trans_disconnect(struct socket *);
115 int	hvs_trans_shutdown(struct socket *);
116 
117 int	hvs_trans_lock(void);
118 void	hvs_trans_unlock(void);
119 
120 void	hvs_remove_socket_from_list(struct socket *, unsigned char);
121 #endif /* _HVSOCK_H */
122