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