]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/vmw_vsock/hyperv_transport.c
hv_sock: Remove the accept port restriction
[mirror_ubuntu-bionic-kernel.git] / net / vmw_vsock / hyperv_transport.c
CommitLineData
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
41struct 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 */
49struct 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
67struct 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
87union 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) */
97struct 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 *
b9331933 147 * format: <port>-facb-11e6-bd58-64006a7986d3. *
ae0078fc
DC
148 ****************************************************************************
149 *
150 * When we write apps on the host to connect(), the GUID ServiceID is used.
151 * When we write apps in Linux VM to connect(), we only need to specify the
152 * port and the driver will form the GUID and use that to request the host.
153 *
ae0078fc
DC
154 */
155
ae0078fc
DC
156/* 00000000-facb-11e6-bd58-64006a7986d3 */
157static const uuid_le srv_id_template =
158 UUID_LE(0x00000000, 0xfacb, 0x11e6, 0xbd, 0x58,
159 0x64, 0x00, 0x6a, 0x79, 0x86, 0xd3);
160
161static bool is_valid_srv_id(const uuid_le *id)
162{
163 return !memcmp(&id->b[4], &srv_id_template.b[4], sizeof(uuid_le) - 4);
164}
165
166static unsigned int get_port_by_srv_id(const uuid_le *svr_id)
167{
168 return *((unsigned int *)svr_id);
169}
170
171static void hvs_addr_init(struct sockaddr_vm *addr, const uuid_le *svr_id)
172{
173 unsigned int port = get_port_by_srv_id(svr_id);
174
175 vsock_addr_init(addr, VMADDR_CID_ANY, port);
176}
177
ae0078fc
DC
178static void hvs_set_channel_pending_send_size(struct vmbus_channel *chan)
179{
180 set_channel_pending_send_size(chan,
181 HVS_PKT_LEN(HVS_SEND_BUF_SIZE));
182
ae0078fc
DC
183 virt_mb();
184}
185
186static bool hvs_channel_readable(struct vmbus_channel *chan)
187{
188 u32 readable = hv_get_bytes_to_read(&chan->inbound);
189
190 /* 0-size payload means FIN */
191 return readable >= HVS_PKT_LEN(0);
192}
193
194static int hvs_channel_readable_payload(struct vmbus_channel *chan)
195{
196 u32 readable = hv_get_bytes_to_read(&chan->inbound);
197
198 if (readable > HVS_PKT_LEN(0)) {
199 /* At least we have 1 byte to read. We don't need to return
200 * the exact readable bytes: see vsock_stream_recvmsg() ->
201 * vsock_stream_has_data().
202 */
203 return 1;
204 }
205
206 if (readable == HVS_PKT_LEN(0)) {
207 /* 0-size payload means FIN */
208 return 0;
209 }
210
211 /* No payload or FIN */
212 return -1;
213}
214
215static size_t hvs_channel_writable_bytes(struct vmbus_channel *chan)
216{
217 u32 writeable = hv_get_bytes_to_write(&chan->outbound);
218 size_t ret;
219
220 /* The ringbuffer mustn't be 100% full, and we should reserve a
221 * zero-length-payload packet for the FIN: see hv_ringbuffer_write()
222 * and hvs_shutdown().
223 */
224 if (writeable <= HVS_PKT_LEN(1) + HVS_PKT_LEN(0))
225 return 0;
226
227 ret = writeable - HVS_PKT_LEN(1) - HVS_PKT_LEN(0);
228
229 return round_down(ret, 8);
230}
231
232static int hvs_send_data(struct vmbus_channel *chan,
233 struct hvs_send_buf *send_buf, size_t to_write)
234{
235 send_buf->hdr.pkt_type = 1;
236 send_buf->hdr.data_size = to_write;
237 return vmbus_sendpacket(chan, &send_buf->hdr,
238 sizeof(send_buf->hdr) + to_write,
239 0, VM_PKT_DATA_INBAND, 0);
240}
241
242static void hvs_channel_cb(void *ctx)
243{
244 struct sock *sk = (struct sock *)ctx;
245 struct vsock_sock *vsk = vsock_sk(sk);
246 struct hvsock *hvs = vsk->trans;
247 struct vmbus_channel *chan = hvs->chan;
248
249 if (hvs_channel_readable(chan))
250 sk->sk_data_ready(sk);
251
ae0078fc
DC
252 if (hv_get_bytes_to_write(&chan->outbound) > 0)
253 sk->sk_write_space(sk);
254}
255
5a044fee
SM
256static void hvs_do_close_lock_held(struct vsock_sock *vsk,
257 bool cancel_timeout)
ae0078fc 258{
5a044fee 259 struct sock *sk = sk_vsock(vsk);
b4562ca7 260
ae0078fc 261 sock_set_flag(sk, SOCK_DONE);
5a044fee
SM
262 vsk->peer_shutdown = SHUTDOWN_MASK;
263 if (vsock_stream_has_data(vsk) <= 0)
264 sk->sk_state = TCP_CLOSING;
ae0078fc 265 sk->sk_state_change(sk);
5a044fee
SM
266 if (vsk->close_work_scheduled &&
267 (!cancel_timeout || cancel_delayed_work(&vsk->close_work))) {
268 vsk->close_work_scheduled = false;
269 vsock_remove_sock(vsk);
270
271 /* Release the reference taken while scheduling the timeout */
272 sock_put(sk);
273 }
274}
275
276static void hvs_close_connection(struct vmbus_channel *chan)
277{
278 struct sock *sk = get_per_channel_state(chan);
b4562ca7 279
5a044fee
SM
280 lock_sock(sk);
281 hvs_do_close_lock_held(vsock_sk(sk), true);
b4562ca7 282 release_sock(sk);
94836a8a
DC
283
284 /* Release the refcnt for the channel that's opened in
285 * hvs_open_connection().
286 */
287 sock_put(sk);
ae0078fc
DC
288}
289
290static void hvs_open_connection(struct vmbus_channel *chan)
291{
292 uuid_le *if_instance, *if_type;
293 unsigned char conn_from_host;
294
295 struct sockaddr_vm addr;
296 struct sock *sk, *new = NULL;
f48d5590
SM
297 struct vsock_sock *vnew = NULL;
298 struct hvsock *hvs = NULL;
299 struct hvsock *hvs_new = NULL;
ae0078fc
DC
300 int ret;
301
302 if_type = &chan->offermsg.offer.if_type;
303 if_instance = &chan->offermsg.offer.if_instance;
304 conn_from_host = chan->offermsg.offer.u.pipe.user_def[0];
b9331933 305 if (!is_valid_srv_id(if_type))
ae0078fc
DC
306 return;
307
308 hvs_addr_init(&addr, conn_from_host ? if_type : if_instance);
309 sk = vsock_find_bound_socket(&addr);
310 if (!sk)
311 return;
312
b4562ca7 313 lock_sock(sk);
3b4477d2
SH
314 if ((conn_from_host && sk->sk_state != TCP_LISTEN) ||
315 (!conn_from_host && sk->sk_state != TCP_SYN_SENT))
ae0078fc
DC
316 goto out;
317
318 if (conn_from_host) {
319 if (sk->sk_ack_backlog >= sk->sk_max_ack_backlog)
320 goto out;
321
322 new = __vsock_create(sock_net(sk), NULL, sk, GFP_KERNEL,
323 sk->sk_type, 0);
324 if (!new)
325 goto out;
326
3b4477d2 327 new->sk_state = TCP_SYN_SENT;
ae0078fc 328 vnew = vsock_sk(new);
b9331933
SM
329
330 hvs_addr_init(&vnew->local_addr, if_type);
331
332 /* Remote peer is always the host */
333 vsock_addr_init(&vnew->remote_addr,
334 VMADDR_CID_HOST, VMADDR_PORT_ANY);
335 vnew->remote_addr.svm_port = get_port_by_srv_id(if_instance);
ae0078fc
DC
336 hvs_new = vnew->trans;
337 hvs_new->chan = chan;
338 } else {
339 hvs = vsock_sk(sk)->trans;
340 hvs->chan = chan;
341 }
342
343 set_channel_read_mode(chan, HV_CALL_DIRECT);
344 ret = vmbus_open(chan, RINGBUFFER_HVS_SND_SIZE,
345 RINGBUFFER_HVS_RCV_SIZE, NULL, 0,
346 hvs_channel_cb, conn_from_host ? new : sk);
347 if (ret != 0) {
348 if (conn_from_host) {
349 hvs_new->chan = NULL;
350 sock_put(new);
351 } else {
352 hvs->chan = NULL;
353 }
354 goto out;
355 }
356
357 set_per_channel_state(chan, conn_from_host ? new : sk);
94836a8a
DC
358
359 /* This reference will be dropped by hvs_close_connection(). */
360 sock_hold(conn_from_host ? new : sk);
ae0078fc
DC
361 vmbus_set_chn_rescind_callback(chan, hvs_close_connection);
362
f48d5590
SM
363 /* Set the pending send size to max packet size to always get
364 * notifications from the host when there is enough writable space.
365 * The host is optimized to send notifications only when the pending
366 * size boundary is crossed, and not always.
367 */
368 hvs_set_channel_pending_send_size(chan);
369
ae0078fc 370 if (conn_from_host) {
3b4477d2 371 new->sk_state = TCP_ESTABLISHED;
ae0078fc
DC
372 sk->sk_ack_backlog++;
373
374 hvs_addr_init(&vnew->local_addr, if_type);
ae0078fc
DC
375 hvs_new->vm_srv_id = *if_type;
376 hvs_new->host_srv_id = *if_instance;
377
378 vsock_insert_connected(vnew);
379
ae0078fc 380 vsock_enqueue_accept(sk, new);
ae0078fc 381 } else {
3b4477d2 382 sk->sk_state = TCP_ESTABLISHED;
ae0078fc
DC
383 sk->sk_socket->state = SS_CONNECTED;
384
385 vsock_insert_connected(vsock_sk(sk));
386 }
387
388 sk->sk_state_change(sk);
389
390out:
391 /* Release refcnt obtained when we called vsock_find_bound_socket() */
392 sock_put(sk);
b4562ca7
DC
393
394 release_sock(sk);
ae0078fc
DC
395}
396
397static u32 hvs_get_local_cid(void)
398{
399 return VMADDR_CID_ANY;
400}
401
402static int hvs_sock_init(struct vsock_sock *vsk, struct vsock_sock *psk)
403{
404 struct hvsock *hvs;
405
406 hvs = kzalloc(sizeof(*hvs), GFP_KERNEL);
407 if (!hvs)
408 return -ENOMEM;
409
410 vsk->trans = hvs;
411 hvs->vsk = vsk;
412
413 return 0;
414}
415
416static int hvs_connect(struct vsock_sock *vsk)
417{
418 union hvs_service_id vm, host;
419 struct hvsock *h = vsk->trans;
420
421 vm.srv_id = srv_id_template;
422 vm.svm_port = vsk->local_addr.svm_port;
423 h->vm_srv_id = vm.srv_id;
424
425 host.srv_id = srv_id_template;
426 host.svm_port = vsk->remote_addr.svm_port;
427 h->host_srv_id = host.srv_id;
428
429 return vmbus_send_tl_connect_request(&h->vm_srv_id, &h->host_srv_id);
430}
431
5a044fee
SM
432static void hvs_shutdown_lock_held(struct hvsock *hvs, int mode)
433{
434 struct vmpipe_proto_header hdr;
435
436 if (hvs->fin_sent || !hvs->chan)
437 return;
438
439 /* It can't fail: see hvs_channel_writable_bytes(). */
440 (void)hvs_send_data(hvs->chan, (struct hvs_send_buf *)&hdr, 0);
441 hvs->fin_sent = true;
442}
443
ae0078fc
DC
444static int hvs_shutdown(struct vsock_sock *vsk, int mode)
445{
446 struct sock *sk = sk_vsock(vsk);
ae0078fc
DC
447
448 if (!(mode & SEND_SHUTDOWN))
449 return 0;
450
451 lock_sock(sk);
5a044fee
SM
452 hvs_shutdown_lock_held(vsk->trans, mode);
453 release_sock(sk);
454 return 0;
455}
ae0078fc 456
5a044fee
SM
457static void hvs_close_timeout(struct work_struct *work)
458{
459 struct vsock_sock *vsk =
460 container_of(work, struct vsock_sock, close_work.work);
461 struct sock *sk = sk_vsock(vsk);
ae0078fc 462
5a044fee
SM
463 sock_hold(sk);
464 lock_sock(sk);
465 if (!sock_flag(sk, SOCK_DONE))
466 hvs_do_close_lock_held(vsk, false);
ae0078fc 467
5a044fee 468 vsk->close_work_scheduled = false;
ae0078fc 469 release_sock(sk);
5a044fee 470 sock_put(sk);
ae0078fc
DC
471}
472
5a044fee
SM
473/* Returns true, if it is safe to remove socket; false otherwise */
474static bool hvs_close_lock_held(struct vsock_sock *vsk)
ae0078fc 475{
b4562ca7 476 struct sock *sk = sk_vsock(vsk);
ae0078fc 477
5a044fee
SM
478 if (!(sk->sk_state == TCP_ESTABLISHED ||
479 sk->sk_state == TCP_CLOSING))
480 return true;
b4562ca7 481
5a044fee
SM
482 if ((sk->sk_shutdown & SHUTDOWN_MASK) != SHUTDOWN_MASK)
483 hvs_shutdown_lock_held(vsk->trans, SHUTDOWN_MASK);
b4562ca7 484
5a044fee
SM
485 if (sock_flag(sk, SOCK_DONE))
486 return true;
ae0078fc 487
5a044fee
SM
488 /* This reference will be dropped by the delayed close routine */
489 sock_hold(sk);
490 INIT_DELAYED_WORK(&vsk->close_work, hvs_close_timeout);
491 vsk->close_work_scheduled = true;
492 schedule_delayed_work(&vsk->close_work, HVS_CLOSE_TIMEOUT);
493 return false;
494}
ae0078fc 495
5a044fee
SM
496static void hvs_release(struct vsock_sock *vsk)
497{
498 struct sock *sk = sk_vsock(vsk);
499 bool remove_sock;
500
e28078fc 501 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
5a044fee
SM
502 remove_sock = hvs_close_lock_held(vsk);
503 release_sock(sk);
504 if (remove_sock)
505 vsock_remove_sock(vsk);
ae0078fc
DC
506}
507
508static void hvs_destruct(struct vsock_sock *vsk)
509{
510 struct hvsock *hvs = vsk->trans;
511 struct vmbus_channel *chan = hvs->chan;
512
513 if (chan)
514 vmbus_hvsock_device_unregister(chan);
515
516 kfree(hvs);
517}
518
519static int hvs_dgram_bind(struct vsock_sock *vsk, struct sockaddr_vm *addr)
520{
521 return -EOPNOTSUPP;
522}
523
524static int hvs_dgram_dequeue(struct vsock_sock *vsk, struct msghdr *msg,
525 size_t len, int flags)
526{
527 return -EOPNOTSUPP;
528}
529
530static int hvs_dgram_enqueue(struct vsock_sock *vsk,
531 struct sockaddr_vm *remote, struct msghdr *msg,
532 size_t dgram_len)
533{
534 return -EOPNOTSUPP;
535}
536
537static bool hvs_dgram_allow(u32 cid, u32 port)
538{
539 return false;
540}
541
542static int hvs_update_recv_data(struct hvsock *hvs)
543{
544 struct hvs_recv_buf *recv_buf;
545 u32 payload_len;
546
547 recv_buf = (struct hvs_recv_buf *)(hvs->recv_desc + 1);
548 payload_len = recv_buf->hdr.data_size;
549
550 if (payload_len > HVS_MTU_SIZE)
551 return -EIO;
552
553 if (payload_len == 0)
554 hvs->vsk->peer_shutdown |= SEND_SHUTDOWN;
555
556 hvs->recv_data_len = payload_len;
557 hvs->recv_data_off = 0;
558
559 return 0;
560}
561
562static ssize_t hvs_stream_dequeue(struct vsock_sock *vsk, struct msghdr *msg,
563 size_t len, int flags)
564{
565 struct hvsock *hvs = vsk->trans;
566 bool need_refill = !hvs->recv_desc;
567 struct hvs_recv_buf *recv_buf;
568 u32 to_read;
569 int ret;
570
571 if (flags & MSG_PEEK)
572 return -EOPNOTSUPP;
573
574 if (need_refill) {
575 hvs->recv_desc = hv_pkt_iter_first(hvs->chan);
576 ret = hvs_update_recv_data(hvs);
577 if (ret)
578 return ret;
579 }
580
581 recv_buf = (struct hvs_recv_buf *)(hvs->recv_desc + 1);
582 to_read = min_t(u32, len, hvs->recv_data_len);
583 ret = memcpy_to_msg(msg, recv_buf->data + hvs->recv_data_off, to_read);
584 if (ret != 0)
585 return ret;
586
587 hvs->recv_data_len -= to_read;
588 if (hvs->recv_data_len == 0) {
589 hvs->recv_desc = hv_pkt_iter_next(hvs->chan, hvs->recv_desc);
590 if (hvs->recv_desc) {
591 ret = hvs_update_recv_data(hvs);
592 if (ret)
593 return ret;
594 }
595 } else {
596 hvs->recv_data_off += to_read;
597 }
598
599 return to_read;
600}
601
602static ssize_t hvs_stream_enqueue(struct vsock_sock *vsk, struct msghdr *msg,
603 size_t len)
604{
605 struct hvsock *hvs = vsk->trans;
606 struct vmbus_channel *chan = hvs->chan;
607 struct hvs_send_buf *send_buf;
608 ssize_t to_write, max_writable, ret;
609
610 BUILD_BUG_ON(sizeof(*send_buf) != PAGE_SIZE_4K);
611
612 send_buf = kmalloc(sizeof(*send_buf), GFP_KERNEL);
613 if (!send_buf)
614 return -ENOMEM;
615
616 max_writable = hvs_channel_writable_bytes(chan);
617 to_write = min_t(ssize_t, len, max_writable);
618 to_write = min_t(ssize_t, to_write, HVS_SEND_BUF_SIZE);
619
620 ret = memcpy_from_msg(send_buf->data, msg, to_write);
621 if (ret < 0)
622 goto out;
623
624 ret = hvs_send_data(hvs->chan, send_buf, to_write);
625 if (ret < 0)
626 goto out;
627
628 ret = to_write;
629out:
630 kfree(send_buf);
631 return ret;
632}
633
634static s64 hvs_stream_has_data(struct vsock_sock *vsk)
635{
636 struct hvsock *hvs = vsk->trans;
637 s64 ret;
638
639 if (hvs->recv_data_len > 0)
640 return 1;
641
642 switch (hvs_channel_readable_payload(hvs->chan)) {
643 case 1:
644 ret = 1;
645 break;
646 case 0:
647 vsk->peer_shutdown |= SEND_SHUTDOWN;
648 ret = 0;
649 break;
650 default: /* -1 */
651 ret = 0;
652 break;
653 }
654
655 return ret;
656}
657
658static s64 hvs_stream_has_space(struct vsock_sock *vsk)
659{
660 struct hvsock *hvs = vsk->trans;
ae0078fc 661
f48d5590 662 return hvs_channel_writable_bytes(hvs->chan);
ae0078fc
DC
663}
664
665static u64 hvs_stream_rcvhiwat(struct vsock_sock *vsk)
666{
667 return HVS_MTU_SIZE + 1;
668}
669
670static bool hvs_stream_is_active(struct vsock_sock *vsk)
671{
672 struct hvsock *hvs = vsk->trans;
673
674 return hvs->chan != NULL;
675}
676
677static bool hvs_stream_allow(u32 cid, u32 port)
678{
ae0078fc
DC
679 if (cid == VMADDR_CID_HOST)
680 return true;
681
682 return false;
683}
684
685static
686int hvs_notify_poll_in(struct vsock_sock *vsk, size_t target, bool *readable)
687{
688 struct hvsock *hvs = vsk->trans;
689
690 *readable = hvs_channel_readable(hvs->chan);
691 return 0;
692}
693
694static
695int hvs_notify_poll_out(struct vsock_sock *vsk, size_t target, bool *writable)
696{
697 *writable = hvs_stream_has_space(vsk) > 0;
698
699 return 0;
700}
701
702static
703int hvs_notify_recv_init(struct vsock_sock *vsk, size_t target,
704 struct vsock_transport_recv_notify_data *d)
705{
706 return 0;
707}
708
709static
710int hvs_notify_recv_pre_block(struct vsock_sock *vsk, size_t target,
711 struct vsock_transport_recv_notify_data *d)
712{
713 return 0;
714}
715
716static
717int hvs_notify_recv_pre_dequeue(struct vsock_sock *vsk, size_t target,
718 struct vsock_transport_recv_notify_data *d)
719{
720 return 0;
721}
722
723static
724int hvs_notify_recv_post_dequeue(struct vsock_sock *vsk, size_t target,
725 ssize_t copied, bool data_read,
726 struct vsock_transport_recv_notify_data *d)
727{
728 return 0;
729}
730
731static
732int hvs_notify_send_init(struct vsock_sock *vsk,
733 struct vsock_transport_send_notify_data *d)
734{
735 return 0;
736}
737
738static
739int hvs_notify_send_pre_block(struct vsock_sock *vsk,
740 struct vsock_transport_send_notify_data *d)
741{
742 return 0;
743}
744
745static
746int hvs_notify_send_pre_enqueue(struct vsock_sock *vsk,
747 struct vsock_transport_send_notify_data *d)
748{
749 return 0;
750}
751
752static
753int hvs_notify_send_post_enqueue(struct vsock_sock *vsk, ssize_t written,
754 struct vsock_transport_send_notify_data *d)
755{
756 return 0;
757}
758
759static void hvs_set_buffer_size(struct vsock_sock *vsk, u64 val)
760{
761 /* Ignored. */
762}
763
764static void hvs_set_min_buffer_size(struct vsock_sock *vsk, u64 val)
765{
766 /* Ignored. */
767}
768
769static void hvs_set_max_buffer_size(struct vsock_sock *vsk, u64 val)
770{
771 /* Ignored. */
772}
773
774static u64 hvs_get_buffer_size(struct vsock_sock *vsk)
775{
776 return -ENOPROTOOPT;
777}
778
779static u64 hvs_get_min_buffer_size(struct vsock_sock *vsk)
780{
781 return -ENOPROTOOPT;
782}
783
784static u64 hvs_get_max_buffer_size(struct vsock_sock *vsk)
785{
786 return -ENOPROTOOPT;
787}
788
789static struct vsock_transport hvs_transport = {
790 .get_local_cid = hvs_get_local_cid,
791
792 .init = hvs_sock_init,
793 .destruct = hvs_destruct,
794 .release = hvs_release,
795 .connect = hvs_connect,
796 .shutdown = hvs_shutdown,
797
798 .dgram_bind = hvs_dgram_bind,
799 .dgram_dequeue = hvs_dgram_dequeue,
800 .dgram_enqueue = hvs_dgram_enqueue,
801 .dgram_allow = hvs_dgram_allow,
802
803 .stream_dequeue = hvs_stream_dequeue,
804 .stream_enqueue = hvs_stream_enqueue,
805 .stream_has_data = hvs_stream_has_data,
806 .stream_has_space = hvs_stream_has_space,
807 .stream_rcvhiwat = hvs_stream_rcvhiwat,
808 .stream_is_active = hvs_stream_is_active,
809 .stream_allow = hvs_stream_allow,
810
811 .notify_poll_in = hvs_notify_poll_in,
812 .notify_poll_out = hvs_notify_poll_out,
813 .notify_recv_init = hvs_notify_recv_init,
814 .notify_recv_pre_block = hvs_notify_recv_pre_block,
815 .notify_recv_pre_dequeue = hvs_notify_recv_pre_dequeue,
816 .notify_recv_post_dequeue = hvs_notify_recv_post_dequeue,
817 .notify_send_init = hvs_notify_send_init,
818 .notify_send_pre_block = hvs_notify_send_pre_block,
819 .notify_send_pre_enqueue = hvs_notify_send_pre_enqueue,
820 .notify_send_post_enqueue = hvs_notify_send_post_enqueue,
821
822 .set_buffer_size = hvs_set_buffer_size,
823 .set_min_buffer_size = hvs_set_min_buffer_size,
824 .set_max_buffer_size = hvs_set_max_buffer_size,
825 .get_buffer_size = hvs_get_buffer_size,
826 .get_min_buffer_size = hvs_get_min_buffer_size,
827 .get_max_buffer_size = hvs_get_max_buffer_size,
828};
829
830static int hvs_probe(struct hv_device *hdev,
831 const struct hv_vmbus_device_id *dev_id)
832{
833 struct vmbus_channel *chan = hdev->channel;
834
835 hvs_open_connection(chan);
836
837 /* Always return success to suppress the unnecessary error message
838 * in vmbus_probe(): on error the host will rescind the device in
839 * 30 seconds and we can do cleanup at that time in
840 * vmbus_onoffer_rescind().
841 */
842 return 0;
843}
844
845static int hvs_remove(struct hv_device *hdev)
846{
847 struct vmbus_channel *chan = hdev->channel;
848
849 vmbus_close(chan);
850
851 return 0;
852}
853
854/* This isn't really used. See vmbus_match() and vmbus_probe() */
855static const struct hv_vmbus_device_id id_table[] = {
856 {},
857};
858
859static struct hv_driver hvs_drv = {
860 .name = "hv_sock",
861 .hvsock = true,
862 .id_table = id_table,
863 .probe = hvs_probe,
864 .remove = hvs_remove,
865};
866
867static int __init hvs_init(void)
868{
869 int ret;
870
871 if (vmbus_proto_version < VERSION_WIN10)
872 return -ENODEV;
873
874 ret = vmbus_driver_register(&hvs_drv);
875 if (ret != 0)
876 return ret;
877
878 ret = vsock_core_init(&hvs_transport);
879 if (ret) {
880 vmbus_driver_unregister(&hvs_drv);
881 return ret;
882 }
883
884 return 0;
885}
886
887static void __exit hvs_exit(void)
888{
889 vsock_core_exit();
890 vmbus_driver_unregister(&hvs_drv);
891}
892
893module_init(hvs_init);
894module_exit(hvs_exit);
895
896MODULE_DESCRIPTION("Hyper-V Sockets");
897MODULE_VERSION("1.0.0");
898MODULE_LICENSE("GPL");
899MODULE_ALIAS_NETPROTO(PF_VSOCK);