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