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