GNU Linux-libre 4.19.286-gnu1
[releases.git] / net / vmw_vsock / hyperv_transport.c
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 /* How long to wait for graceful shutdown of a connection */
39 #define HVS_CLOSE_TIMEOUT (8 * HZ)
40
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.                              *
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  *
154  */
155
156 /* 00000000-facb-11e6-bd58-64006a7986d3 */
157 static const uuid_le srv_id_template =
158         UUID_LE(0x00000000, 0xfacb, 0x11e6, 0xbd, 0x58,
159                 0x64, 0x00, 0x6a, 0x79, 0x86, 0xd3);
160
161 static 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
166 static unsigned int get_port_by_srv_id(const uuid_le *svr_id)
167 {
168         return *((unsigned int *)svr_id);
169 }
170
171 static 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
178 static 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
183         virt_mb();
184 }
185
186 static 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
194 static 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
215 static 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
232 static 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
242 static 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
252         if (hv_get_bytes_to_write(&chan->outbound) > 0)
253                 sk->sk_write_space(sk);
254 }
255
256 static void hvs_do_close_lock_held(struct vsock_sock *vsk,
257                                    bool cancel_timeout)
258 {
259         struct sock *sk = sk_vsock(vsk);
260
261         sock_set_flag(sk, SOCK_DONE);
262         vsk->peer_shutdown = SHUTDOWN_MASK;
263         if (vsock_stream_has_data(vsk) <= 0)
264                 sk->sk_state = TCP_CLOSING;
265         sk->sk_state_change(sk);
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
276 static void hvs_close_connection(struct vmbus_channel *chan)
277 {
278         struct sock *sk = get_per_channel_state(chan);
279
280         lock_sock(sk);
281         hvs_do_close_lock_held(vsock_sk(sk), true);
282         release_sock(sk);
283
284         /* Release the refcnt for the channel that's opened in
285          * hvs_open_connection().
286          */
287         sock_put(sk);
288 }
289
290 static 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;
297         struct vsock_sock *vnew = NULL;
298         struct hvsock *hvs = NULL;
299         struct hvsock *hvs_new = NULL;
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];
305         if (!is_valid_srv_id(if_type))
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
313         lock_sock(sk);
314         if ((conn_from_host && sk->sk_state != TCP_LISTEN) ||
315             (!conn_from_host && sk->sk_state != TCP_SYN_SENT))
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
327                 new->sk_state = TCP_SYN_SENT;
328                 vnew = vsock_sk(new);
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);
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);
358
359         /* This reference will be dropped by hvs_close_connection(). */
360         sock_hold(conn_from_host ? new : sk);
361         vmbus_set_chn_rescind_callback(chan, hvs_close_connection);
362
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
370         if (conn_from_host) {
371                 new->sk_state = TCP_ESTABLISHED;
372                 sk->sk_ack_backlog++;
373
374                 hvs_addr_init(&vnew->local_addr, if_type);
375                 hvs_new->vm_srv_id = *if_type;
376                 hvs_new->host_srv_id = *if_instance;
377
378                 vsock_insert_connected(vnew);
379
380                 vsock_enqueue_accept(sk, new);
381         } else {
382                 sk->sk_state = TCP_ESTABLISHED;
383                 sk->sk_socket->state = SS_CONNECTED;
384
385                 vsock_insert_connected(vsock_sk(sk));
386         }
387
388         sk->sk_state_change(sk);
389
390 out:
391         /* Release refcnt obtained when we called vsock_find_bound_socket() */
392         sock_put(sk);
393
394         release_sock(sk);
395 }
396
397 static u32 hvs_get_local_cid(void)
398 {
399         return VMADDR_CID_ANY;
400 }
401
402 static 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
416 static 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
432 static 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
444 static int hvs_shutdown(struct vsock_sock *vsk, int mode)
445 {
446         if (!(mode & SEND_SHUTDOWN))
447                 return 0;
448
449         hvs_shutdown_lock_held(vsk->trans, mode);
450         return 0;
451 }
452
453 static void hvs_close_timeout(struct work_struct *work)
454 {
455         struct vsock_sock *vsk =
456                 container_of(work, struct vsock_sock, close_work.work);
457         struct sock *sk = sk_vsock(vsk);
458
459         sock_hold(sk);
460         lock_sock(sk);
461         if (!sock_flag(sk, SOCK_DONE))
462                 hvs_do_close_lock_held(vsk, false);
463
464         vsk->close_work_scheduled = false;
465         release_sock(sk);
466         sock_put(sk);
467 }
468
469 /* Returns true, if it is safe to remove socket; false otherwise */
470 static bool hvs_close_lock_held(struct vsock_sock *vsk)
471 {
472         struct sock *sk = sk_vsock(vsk);
473
474         if (!(sk->sk_state == TCP_ESTABLISHED ||
475               sk->sk_state == TCP_CLOSING))
476                 return true;
477
478         if ((sk->sk_shutdown & SHUTDOWN_MASK) != SHUTDOWN_MASK)
479                 hvs_shutdown_lock_held(vsk->trans, SHUTDOWN_MASK);
480
481         if (sock_flag(sk, SOCK_DONE))
482                 return true;
483
484         /* This reference will be dropped by the delayed close routine */
485         sock_hold(sk);
486         INIT_DELAYED_WORK(&vsk->close_work, hvs_close_timeout);
487         vsk->close_work_scheduled = true;
488         schedule_delayed_work(&vsk->close_work, HVS_CLOSE_TIMEOUT);
489         return false;
490 }
491
492 static void hvs_release(struct vsock_sock *vsk)
493 {
494         struct sock *sk = sk_vsock(vsk);
495         bool remove_sock;
496
497         lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
498         remove_sock = hvs_close_lock_held(vsk);
499         release_sock(sk);
500         if (remove_sock)
501                 vsock_remove_sock(vsk);
502 }
503
504 static void hvs_destruct(struct vsock_sock *vsk)
505 {
506         struct hvsock *hvs = vsk->trans;
507         struct vmbus_channel *chan = hvs->chan;
508
509         if (chan)
510                 vmbus_hvsock_device_unregister(chan);
511
512         kfree(hvs);
513 }
514
515 static int hvs_dgram_bind(struct vsock_sock *vsk, struct sockaddr_vm *addr)
516 {
517         return -EOPNOTSUPP;
518 }
519
520 static int hvs_dgram_dequeue(struct vsock_sock *vsk, struct msghdr *msg,
521                              size_t len, int flags)
522 {
523         return -EOPNOTSUPP;
524 }
525
526 static int hvs_dgram_enqueue(struct vsock_sock *vsk,
527                              struct sockaddr_vm *remote, struct msghdr *msg,
528                              size_t dgram_len)
529 {
530         return -EOPNOTSUPP;
531 }
532
533 static bool hvs_dgram_allow(u32 cid, u32 port)
534 {
535         return false;
536 }
537
538 static int hvs_update_recv_data(struct hvsock *hvs)
539 {
540         struct hvs_recv_buf *recv_buf;
541         u32 payload_len;
542
543         recv_buf = (struct hvs_recv_buf *)(hvs->recv_desc + 1);
544         payload_len = recv_buf->hdr.data_size;
545
546         if (payload_len > HVS_MTU_SIZE)
547                 return -EIO;
548
549         if (payload_len == 0)
550                 hvs->vsk->peer_shutdown |= SEND_SHUTDOWN;
551
552         hvs->recv_data_len = payload_len;
553         hvs->recv_data_off = 0;
554
555         return 0;
556 }
557
558 static ssize_t hvs_stream_dequeue(struct vsock_sock *vsk, struct msghdr *msg,
559                                   size_t len, int flags)
560 {
561         struct hvsock *hvs = vsk->trans;
562         bool need_refill = !hvs->recv_desc;
563         struct hvs_recv_buf *recv_buf;
564         u32 to_read;
565         int ret;
566
567         if (flags & MSG_PEEK)
568                 return -EOPNOTSUPP;
569
570         if (need_refill) {
571                 hvs->recv_desc = hv_pkt_iter_first(hvs->chan);
572                 ret = hvs_update_recv_data(hvs);
573                 if (ret)
574                         return ret;
575         }
576
577         recv_buf = (struct hvs_recv_buf *)(hvs->recv_desc + 1);
578         to_read = min_t(u32, len, hvs->recv_data_len);
579         ret = memcpy_to_msg(msg, recv_buf->data + hvs->recv_data_off, to_read);
580         if (ret != 0)
581                 return ret;
582
583         hvs->recv_data_len -= to_read;
584         if (hvs->recv_data_len == 0) {
585                 hvs->recv_desc = hv_pkt_iter_next(hvs->chan, hvs->recv_desc);
586                 if (hvs->recv_desc) {
587                         ret = hvs_update_recv_data(hvs);
588                         if (ret)
589                                 return ret;
590                 }
591         } else {
592                 hvs->recv_data_off += to_read;
593         }
594
595         return to_read;
596 }
597
598 static ssize_t hvs_stream_enqueue(struct vsock_sock *vsk, struct msghdr *msg,
599                                   size_t len)
600 {
601         struct hvsock *hvs = vsk->trans;
602         struct vmbus_channel *chan = hvs->chan;
603         struct hvs_send_buf *send_buf;
604         ssize_t to_write, max_writable, ret;
605
606         BUILD_BUG_ON(sizeof(*send_buf) != PAGE_SIZE_4K);
607
608         send_buf = kmalloc(sizeof(*send_buf), GFP_KERNEL);
609         if (!send_buf)
610                 return -ENOMEM;
611
612         max_writable = hvs_channel_writable_bytes(chan);
613         to_write = min_t(ssize_t, len, max_writable);
614         to_write = min_t(ssize_t, to_write, HVS_SEND_BUF_SIZE);
615
616         ret = memcpy_from_msg(send_buf->data, msg, to_write);
617         if (ret < 0)
618                 goto out;
619
620         ret = hvs_send_data(hvs->chan, send_buf, to_write);
621         if (ret < 0)
622                 goto out;
623
624         ret = to_write;
625 out:
626         kfree(send_buf);
627         return ret;
628 }
629
630 static s64 hvs_stream_has_data(struct vsock_sock *vsk)
631 {
632         struct hvsock *hvs = vsk->trans;
633         s64 ret;
634
635         if (hvs->recv_data_len > 0)
636                 return 1;
637
638         switch (hvs_channel_readable_payload(hvs->chan)) {
639         case 1:
640                 ret = 1;
641                 break;
642         case 0:
643                 vsk->peer_shutdown |= SEND_SHUTDOWN;
644                 ret = 0;
645                 break;
646         default: /* -1 */
647                 ret = 0;
648                 break;
649         }
650
651         return ret;
652 }
653
654 static s64 hvs_stream_has_space(struct vsock_sock *vsk)
655 {
656         struct hvsock *hvs = vsk->trans;
657
658         return hvs_channel_writable_bytes(hvs->chan);
659 }
660
661 static u64 hvs_stream_rcvhiwat(struct vsock_sock *vsk)
662 {
663         return HVS_MTU_SIZE + 1;
664 }
665
666 static bool hvs_stream_is_active(struct vsock_sock *vsk)
667 {
668         struct hvsock *hvs = vsk->trans;
669
670         return hvs->chan != NULL;
671 }
672
673 static bool hvs_stream_allow(u32 cid, u32 port)
674 {
675         if (cid == VMADDR_CID_HOST)
676                 return true;
677
678         return false;
679 }
680
681 static
682 int hvs_notify_poll_in(struct vsock_sock *vsk, size_t target, bool *readable)
683 {
684         struct hvsock *hvs = vsk->trans;
685
686         *readable = hvs_channel_readable(hvs->chan);
687         return 0;
688 }
689
690 static
691 int hvs_notify_poll_out(struct vsock_sock *vsk, size_t target, bool *writable)
692 {
693         *writable = hvs_stream_has_space(vsk) > 0;
694
695         return 0;
696 }
697
698 static
699 int hvs_notify_recv_init(struct vsock_sock *vsk, size_t target,
700                          struct vsock_transport_recv_notify_data *d)
701 {
702         return 0;
703 }
704
705 static
706 int hvs_notify_recv_pre_block(struct vsock_sock *vsk, size_t target,
707                               struct vsock_transport_recv_notify_data *d)
708 {
709         return 0;
710 }
711
712 static
713 int hvs_notify_recv_pre_dequeue(struct vsock_sock *vsk, size_t target,
714                                 struct vsock_transport_recv_notify_data *d)
715 {
716         return 0;
717 }
718
719 static
720 int hvs_notify_recv_post_dequeue(struct vsock_sock *vsk, size_t target,
721                                  ssize_t copied, bool data_read,
722                                  struct vsock_transport_recv_notify_data *d)
723 {
724         return 0;
725 }
726
727 static
728 int hvs_notify_send_init(struct vsock_sock *vsk,
729                          struct vsock_transport_send_notify_data *d)
730 {
731         return 0;
732 }
733
734 static
735 int hvs_notify_send_pre_block(struct vsock_sock *vsk,
736                               struct vsock_transport_send_notify_data *d)
737 {
738         return 0;
739 }
740
741 static
742 int hvs_notify_send_pre_enqueue(struct vsock_sock *vsk,
743                                 struct vsock_transport_send_notify_data *d)
744 {
745         return 0;
746 }
747
748 static
749 int hvs_notify_send_post_enqueue(struct vsock_sock *vsk, ssize_t written,
750                                  struct vsock_transport_send_notify_data *d)
751 {
752         return 0;
753 }
754
755 static void hvs_set_buffer_size(struct vsock_sock *vsk, u64 val)
756 {
757         /* Ignored. */
758 }
759
760 static void hvs_set_min_buffer_size(struct vsock_sock *vsk, u64 val)
761 {
762         /* Ignored. */
763 }
764
765 static void hvs_set_max_buffer_size(struct vsock_sock *vsk, u64 val)
766 {
767         /* Ignored. */
768 }
769
770 static u64 hvs_get_buffer_size(struct vsock_sock *vsk)
771 {
772         return -ENOPROTOOPT;
773 }
774
775 static u64 hvs_get_min_buffer_size(struct vsock_sock *vsk)
776 {
777         return -ENOPROTOOPT;
778 }
779
780 static u64 hvs_get_max_buffer_size(struct vsock_sock *vsk)
781 {
782         return -ENOPROTOOPT;
783 }
784
785 static struct vsock_transport hvs_transport = {
786         .get_local_cid            = hvs_get_local_cid,
787
788         .init                     = hvs_sock_init,
789         .destruct                 = hvs_destruct,
790         .release                  = hvs_release,
791         .connect                  = hvs_connect,
792         .shutdown                 = hvs_shutdown,
793
794         .dgram_bind               = hvs_dgram_bind,
795         .dgram_dequeue            = hvs_dgram_dequeue,
796         .dgram_enqueue            = hvs_dgram_enqueue,
797         .dgram_allow              = hvs_dgram_allow,
798
799         .stream_dequeue           = hvs_stream_dequeue,
800         .stream_enqueue           = hvs_stream_enqueue,
801         .stream_has_data          = hvs_stream_has_data,
802         .stream_has_space         = hvs_stream_has_space,
803         .stream_rcvhiwat          = hvs_stream_rcvhiwat,
804         .stream_is_active         = hvs_stream_is_active,
805         .stream_allow             = hvs_stream_allow,
806
807         .notify_poll_in           = hvs_notify_poll_in,
808         .notify_poll_out          = hvs_notify_poll_out,
809         .notify_recv_init         = hvs_notify_recv_init,
810         .notify_recv_pre_block    = hvs_notify_recv_pre_block,
811         .notify_recv_pre_dequeue  = hvs_notify_recv_pre_dequeue,
812         .notify_recv_post_dequeue = hvs_notify_recv_post_dequeue,
813         .notify_send_init         = hvs_notify_send_init,
814         .notify_send_pre_block    = hvs_notify_send_pre_block,
815         .notify_send_pre_enqueue  = hvs_notify_send_pre_enqueue,
816         .notify_send_post_enqueue = hvs_notify_send_post_enqueue,
817
818         .set_buffer_size          = hvs_set_buffer_size,
819         .set_min_buffer_size      = hvs_set_min_buffer_size,
820         .set_max_buffer_size      = hvs_set_max_buffer_size,
821         .get_buffer_size          = hvs_get_buffer_size,
822         .get_min_buffer_size      = hvs_get_min_buffer_size,
823         .get_max_buffer_size      = hvs_get_max_buffer_size,
824 };
825
826 static int hvs_probe(struct hv_device *hdev,
827                      const struct hv_vmbus_device_id *dev_id)
828 {
829         struct vmbus_channel *chan = hdev->channel;
830
831         hvs_open_connection(chan);
832
833         /* Always return success to suppress the unnecessary error message
834          * in vmbus_probe(): on error the host will rescind the device in
835          * 30 seconds and we can do cleanup at that time in
836          * vmbus_onoffer_rescind().
837          */
838         return 0;
839 }
840
841 static int hvs_remove(struct hv_device *hdev)
842 {
843         struct vmbus_channel *chan = hdev->channel;
844
845         vmbus_close(chan);
846
847         return 0;
848 }
849
850 /* This isn't really used. See vmbus_match() and vmbus_probe() */
851 static const struct hv_vmbus_device_id id_table[] = {
852         {},
853 };
854
855 static struct hv_driver hvs_drv = {
856         .name           = "hv_sock",
857         .hvsock         = true,
858         .id_table       = id_table,
859         .probe          = hvs_probe,
860         .remove         = hvs_remove,
861 };
862
863 static int __init hvs_init(void)
864 {
865         int ret;
866
867         if (vmbus_proto_version < VERSION_WIN10)
868                 return -ENODEV;
869
870         ret = vmbus_driver_register(&hvs_drv);
871         if (ret != 0)
872                 return ret;
873
874         ret = vsock_core_init(&hvs_transport);
875         if (ret) {
876                 vmbus_driver_unregister(&hvs_drv);
877                 return ret;
878         }
879
880         return 0;
881 }
882
883 static void __exit hvs_exit(void)
884 {
885         vsock_core_exit();
886         vmbus_driver_unregister(&hvs_drv);
887 }
888
889 module_init(hvs_init);
890 module_exit(hvs_exit);
891
892 MODULE_DESCRIPTION("Hyper-V Sockets");
893 MODULE_VERSION("1.0.0");
894 MODULE_LICENSE("GPL");
895 MODULE_ALIAS_NETPROTO(PF_VSOCK);