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