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