GNU Linux-libre 4.14.290-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
315         if ((conn_from_host && sk->sk_state != TCP_LISTEN) ||
316             (!conn_from_host && sk->sk_state != TCP_SYN_SENT))
317                 goto out;
318
319         if (conn_from_host) {
320                 if (sk->sk_ack_backlog >= sk->sk_max_ack_backlog)
321                         goto out;
322
323                 new = __vsock_create(sock_net(sk), NULL, sk, GFP_KERNEL,
324                                      sk->sk_type, 0);
325                 if (!new)
326                         goto out;
327
328                 new->sk_state = TCP_SYN_SENT;
329                 vnew = vsock_sk(new);
330
331                 hvs_addr_init(&vnew->local_addr, if_type);
332
333                 /* Remote peer is always the host */
334                 vsock_addr_init(&vnew->remote_addr,
335                                 VMADDR_CID_HOST, VMADDR_PORT_ANY);
336                 vnew->remote_addr.svm_port = get_port_by_srv_id(if_instance);
337                 hvs_new = vnew->trans;
338                 hvs_new->chan = chan;
339         } else {
340                 hvs = vsock_sk(sk)->trans;
341                 hvs->chan = chan;
342         }
343
344         set_channel_read_mode(chan, HV_CALL_DIRECT);
345         ret = vmbus_open(chan, RINGBUFFER_HVS_SND_SIZE,
346                          RINGBUFFER_HVS_RCV_SIZE, NULL, 0,
347                          hvs_channel_cb, conn_from_host ? new : sk);
348         if (ret != 0) {
349                 if (conn_from_host) {
350                         hvs_new->chan = NULL;
351                         sock_put(new);
352                 } else {
353                         hvs->chan = NULL;
354                 }
355                 goto out;
356         }
357
358         set_per_channel_state(chan, conn_from_host ? new : sk);
359
360         /* This reference will be dropped by hvs_close_connection(). */
361         sock_hold(conn_from_host ? new : sk);
362         vmbus_set_chn_rescind_callback(chan, hvs_close_connection);
363
364         /* Set the pending send size to max packet size to always get
365          * notifications from the host when there is enough writable space.
366          * The host is optimized to send notifications only when the pending
367          * size boundary is crossed, and not always.
368          */
369         hvs_set_channel_pending_send_size(chan);
370
371         if (conn_from_host) {
372                 new->sk_state = TCP_ESTABLISHED;
373                 sk->sk_ack_backlog++;
374
375                 hvs_addr_init(&vnew->local_addr, if_type);
376                 hvs_new->vm_srv_id = *if_type;
377                 hvs_new->host_srv_id = *if_instance;
378
379                 vsock_insert_connected(vnew);
380
381                 vsock_enqueue_accept(sk, new);
382         } else {
383                 sk->sk_state = TCP_ESTABLISHED;
384                 sk->sk_socket->state = SS_CONNECTED;
385
386                 vsock_insert_connected(vsock_sk(sk));
387         }
388
389         sk->sk_state_change(sk);
390
391 out:
392         /* Release refcnt obtained when we called vsock_find_bound_socket() */
393         sock_put(sk);
394
395         release_sock(sk);
396 }
397
398 static u32 hvs_get_local_cid(void)
399 {
400         return VMADDR_CID_ANY;
401 }
402
403 static int hvs_sock_init(struct vsock_sock *vsk, struct vsock_sock *psk)
404 {
405         struct hvsock *hvs;
406
407         hvs = kzalloc(sizeof(*hvs), GFP_KERNEL);
408         if (!hvs)
409                 return -ENOMEM;
410
411         vsk->trans = hvs;
412         hvs->vsk = vsk;
413
414         return 0;
415 }
416
417 static int hvs_connect(struct vsock_sock *vsk)
418 {
419         union hvs_service_id vm, host;
420         struct hvsock *h = vsk->trans;
421
422         vm.srv_id = srv_id_template;
423         vm.svm_port = vsk->local_addr.svm_port;
424         h->vm_srv_id = vm.srv_id;
425
426         host.srv_id = srv_id_template;
427         host.svm_port = vsk->remote_addr.svm_port;
428         h->host_srv_id = host.srv_id;
429
430         return vmbus_send_tl_connect_request(&h->vm_srv_id, &h->host_srv_id);
431 }
432
433 static void hvs_shutdown_lock_held(struct hvsock *hvs, int mode)
434 {
435         struct vmpipe_proto_header hdr;
436
437         if (hvs->fin_sent || !hvs->chan)
438                 return;
439
440         /* It can't fail: see hvs_channel_writable_bytes(). */
441         (void)hvs_send_data(hvs->chan, (struct hvs_send_buf *)&hdr, 0);
442         hvs->fin_sent = true;
443 }
444
445 static int hvs_shutdown(struct vsock_sock *vsk, int mode)
446 {
447         if (!(mode & SEND_SHUTDOWN))
448                 return 0;
449
450         hvs_shutdown_lock_held(vsk->trans, mode);
451         return 0;
452 }
453
454 static void hvs_close_timeout(struct work_struct *work)
455 {
456         struct vsock_sock *vsk =
457                 container_of(work, struct vsock_sock, close_work.work);
458         struct sock *sk = sk_vsock(vsk);
459
460         sock_hold(sk);
461         lock_sock(sk);
462         if (!sock_flag(sk, SOCK_DONE))
463                 hvs_do_close_lock_held(vsk, false);
464
465         vsk->close_work_scheduled = false;
466         release_sock(sk);
467         sock_put(sk);
468 }
469
470 /* Returns true, if it is safe to remove socket; false otherwise */
471 static bool hvs_close_lock_held(struct vsock_sock *vsk)
472 {
473         struct sock *sk = sk_vsock(vsk);
474
475         if (!(sk->sk_state == TCP_ESTABLISHED ||
476               sk->sk_state == TCP_CLOSING))
477                 return true;
478
479         if ((sk->sk_shutdown & SHUTDOWN_MASK) != SHUTDOWN_MASK)
480                 hvs_shutdown_lock_held(vsk->trans, SHUTDOWN_MASK);
481
482         if (sock_flag(sk, SOCK_DONE))
483                 return true;
484
485         /* This reference will be dropped by the delayed close routine */
486         sock_hold(sk);
487         INIT_DELAYED_WORK(&vsk->close_work, hvs_close_timeout);
488         vsk->close_work_scheduled = true;
489         schedule_delayed_work(&vsk->close_work, HVS_CLOSE_TIMEOUT);
490         return false;
491 }
492
493 static void hvs_release(struct vsock_sock *vsk)
494 {
495         struct sock *sk = sk_vsock(vsk);
496         bool remove_sock;
497
498         lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
499         remove_sock = hvs_close_lock_held(vsk);
500         release_sock(sk);
501         if (remove_sock)
502                 vsock_remove_sock(vsk);
503 }
504
505 static void hvs_destruct(struct vsock_sock *vsk)
506 {
507         struct hvsock *hvs = vsk->trans;
508         struct vmbus_channel *chan = hvs->chan;
509
510         if (chan)
511                 vmbus_hvsock_device_unregister(chan);
512
513         kfree(hvs);
514 }
515
516 static int hvs_dgram_bind(struct vsock_sock *vsk, struct sockaddr_vm *addr)
517 {
518         return -EOPNOTSUPP;
519 }
520
521 static int hvs_dgram_dequeue(struct vsock_sock *vsk, struct msghdr *msg,
522                              size_t len, int flags)
523 {
524         return -EOPNOTSUPP;
525 }
526
527 static int hvs_dgram_enqueue(struct vsock_sock *vsk,
528                              struct sockaddr_vm *remote, struct msghdr *msg,
529                              size_t dgram_len)
530 {
531         return -EOPNOTSUPP;
532 }
533
534 static bool hvs_dgram_allow(u32 cid, u32 port)
535 {
536         return false;
537 }
538
539 static int hvs_update_recv_data(struct hvsock *hvs)
540 {
541         struct hvs_recv_buf *recv_buf;
542         u32 payload_len;
543
544         recv_buf = (struct hvs_recv_buf *)(hvs->recv_desc + 1);
545         payload_len = recv_buf->hdr.data_size;
546
547         if (payload_len > HVS_MTU_SIZE)
548                 return -EIO;
549
550         if (payload_len == 0)
551                 hvs->vsk->peer_shutdown |= SEND_SHUTDOWN;
552
553         hvs->recv_data_len = payload_len;
554         hvs->recv_data_off = 0;
555
556         return 0;
557 }
558
559 static ssize_t hvs_stream_dequeue(struct vsock_sock *vsk, struct msghdr *msg,
560                                   size_t len, int flags)
561 {
562         struct hvsock *hvs = vsk->trans;
563         bool need_refill = !hvs->recv_desc;
564         struct hvs_recv_buf *recv_buf;
565         u32 to_read;
566         int ret;
567
568         if (flags & MSG_PEEK)
569                 return -EOPNOTSUPP;
570
571         if (need_refill) {
572                 hvs->recv_desc = hv_pkt_iter_first(hvs->chan);
573                 ret = hvs_update_recv_data(hvs);
574                 if (ret)
575                         return ret;
576         }
577
578         recv_buf = (struct hvs_recv_buf *)(hvs->recv_desc + 1);
579         to_read = min_t(u32, len, hvs->recv_data_len);
580         ret = memcpy_to_msg(msg, recv_buf->data + hvs->recv_data_off, to_read);
581         if (ret != 0)
582                 return ret;
583
584         hvs->recv_data_len -= to_read;
585         if (hvs->recv_data_len == 0) {
586                 hvs->recv_desc = hv_pkt_iter_next(hvs->chan, hvs->recv_desc);
587                 if (hvs->recv_desc) {
588                         ret = hvs_update_recv_data(hvs);
589                         if (ret)
590                                 return ret;
591                 }
592         } else {
593                 hvs->recv_data_off += to_read;
594         }
595
596         return to_read;
597 }
598
599 static ssize_t hvs_stream_enqueue(struct vsock_sock *vsk, struct msghdr *msg,
600                                   size_t len)
601 {
602         struct hvsock *hvs = vsk->trans;
603         struct vmbus_channel *chan = hvs->chan;
604         struct hvs_send_buf *send_buf;
605         ssize_t to_write, max_writable, ret;
606
607         BUILD_BUG_ON(sizeof(*send_buf) != PAGE_SIZE_4K);
608
609         send_buf = kmalloc(sizeof(*send_buf), GFP_KERNEL);
610         if (!send_buf)
611                 return -ENOMEM;
612
613         max_writable = hvs_channel_writable_bytes(chan);
614         to_write = min_t(ssize_t, len, max_writable);
615         to_write = min_t(ssize_t, to_write, HVS_SEND_BUF_SIZE);
616
617         ret = memcpy_from_msg(send_buf->data, msg, to_write);
618         if (ret < 0)
619                 goto out;
620
621         ret = hvs_send_data(hvs->chan, send_buf, to_write);
622         if (ret < 0)
623                 goto out;
624
625         ret = to_write;
626 out:
627         kfree(send_buf);
628         return ret;
629 }
630
631 static s64 hvs_stream_has_data(struct vsock_sock *vsk)
632 {
633         struct hvsock *hvs = vsk->trans;
634         s64 ret;
635
636         if (hvs->recv_data_len > 0)
637                 return 1;
638
639         switch (hvs_channel_readable_payload(hvs->chan)) {
640         case 1:
641                 ret = 1;
642                 break;
643         case 0:
644                 vsk->peer_shutdown |= SEND_SHUTDOWN;
645                 ret = 0;
646                 break;
647         default: /* -1 */
648                 ret = 0;
649                 break;
650         }
651
652         return ret;
653 }
654
655 static s64 hvs_stream_has_space(struct vsock_sock *vsk)
656 {
657         struct hvsock *hvs = vsk->trans;
658
659         return hvs_channel_writable_bytes(hvs->chan);
660 }
661
662 static u64 hvs_stream_rcvhiwat(struct vsock_sock *vsk)
663 {
664         return HVS_MTU_SIZE + 1;
665 }
666
667 static bool hvs_stream_is_active(struct vsock_sock *vsk)
668 {
669         struct hvsock *hvs = vsk->trans;
670
671         return hvs->chan != NULL;
672 }
673
674 static bool hvs_stream_allow(u32 cid, u32 port)
675 {
676         if (cid == VMADDR_CID_HOST)
677                 return true;
678
679         return false;
680 }
681
682 static
683 int hvs_notify_poll_in(struct vsock_sock *vsk, size_t target, bool *readable)
684 {
685         struct hvsock *hvs = vsk->trans;
686
687         *readable = hvs_channel_readable(hvs->chan);
688         return 0;
689 }
690
691 static
692 int hvs_notify_poll_out(struct vsock_sock *vsk, size_t target, bool *writable)
693 {
694         *writable = hvs_stream_has_space(vsk) > 0;
695
696         return 0;
697 }
698
699 static
700 int hvs_notify_recv_init(struct vsock_sock *vsk, size_t target,
701                          struct vsock_transport_recv_notify_data *d)
702 {
703         return 0;
704 }
705
706 static
707 int hvs_notify_recv_pre_block(struct vsock_sock *vsk, size_t target,
708                               struct vsock_transport_recv_notify_data *d)
709 {
710         return 0;
711 }
712
713 static
714 int hvs_notify_recv_pre_dequeue(struct vsock_sock *vsk, size_t target,
715                                 struct vsock_transport_recv_notify_data *d)
716 {
717         return 0;
718 }
719
720 static
721 int hvs_notify_recv_post_dequeue(struct vsock_sock *vsk, size_t target,
722                                  ssize_t copied, bool data_read,
723                                  struct vsock_transport_recv_notify_data *d)
724 {
725         return 0;
726 }
727
728 static
729 int hvs_notify_send_init(struct vsock_sock *vsk,
730                          struct vsock_transport_send_notify_data *d)
731 {
732         return 0;
733 }
734
735 static
736 int hvs_notify_send_pre_block(struct vsock_sock *vsk,
737                               struct vsock_transport_send_notify_data *d)
738 {
739         return 0;
740 }
741
742 static
743 int hvs_notify_send_pre_enqueue(struct vsock_sock *vsk,
744                                 struct vsock_transport_send_notify_data *d)
745 {
746         return 0;
747 }
748
749 static
750 int hvs_notify_send_post_enqueue(struct vsock_sock *vsk, ssize_t written,
751                                  struct vsock_transport_send_notify_data *d)
752 {
753         return 0;
754 }
755
756 static void hvs_set_buffer_size(struct vsock_sock *vsk, u64 val)
757 {
758         /* Ignored. */
759 }
760
761 static void hvs_set_min_buffer_size(struct vsock_sock *vsk, u64 val)
762 {
763         /* Ignored. */
764 }
765
766 static void hvs_set_max_buffer_size(struct vsock_sock *vsk, u64 val)
767 {
768         /* Ignored. */
769 }
770
771 static u64 hvs_get_buffer_size(struct vsock_sock *vsk)
772 {
773         return -ENOPROTOOPT;
774 }
775
776 static u64 hvs_get_min_buffer_size(struct vsock_sock *vsk)
777 {
778         return -ENOPROTOOPT;
779 }
780
781 static u64 hvs_get_max_buffer_size(struct vsock_sock *vsk)
782 {
783         return -ENOPROTOOPT;
784 }
785
786 static struct vsock_transport hvs_transport = {
787         .get_local_cid            = hvs_get_local_cid,
788
789         .init                     = hvs_sock_init,
790         .destruct                 = hvs_destruct,
791         .release                  = hvs_release,
792         .connect                  = hvs_connect,
793         .shutdown                 = hvs_shutdown,
794
795         .dgram_bind               = hvs_dgram_bind,
796         .dgram_dequeue            = hvs_dgram_dequeue,
797         .dgram_enqueue            = hvs_dgram_enqueue,
798         .dgram_allow              = hvs_dgram_allow,
799
800         .stream_dequeue           = hvs_stream_dequeue,
801         .stream_enqueue           = hvs_stream_enqueue,
802         .stream_has_data          = hvs_stream_has_data,
803         .stream_has_space         = hvs_stream_has_space,
804         .stream_rcvhiwat          = hvs_stream_rcvhiwat,
805         .stream_is_active         = hvs_stream_is_active,
806         .stream_allow             = hvs_stream_allow,
807
808         .notify_poll_in           = hvs_notify_poll_in,
809         .notify_poll_out          = hvs_notify_poll_out,
810         .notify_recv_init         = hvs_notify_recv_init,
811         .notify_recv_pre_block    = hvs_notify_recv_pre_block,
812         .notify_recv_pre_dequeue  = hvs_notify_recv_pre_dequeue,
813         .notify_recv_post_dequeue = hvs_notify_recv_post_dequeue,
814         .notify_send_init         = hvs_notify_send_init,
815         .notify_send_pre_block    = hvs_notify_send_pre_block,
816         .notify_send_pre_enqueue  = hvs_notify_send_pre_enqueue,
817         .notify_send_post_enqueue = hvs_notify_send_post_enqueue,
818
819         .set_buffer_size          = hvs_set_buffer_size,
820         .set_min_buffer_size      = hvs_set_min_buffer_size,
821         .set_max_buffer_size      = hvs_set_max_buffer_size,
822         .get_buffer_size          = hvs_get_buffer_size,
823         .get_min_buffer_size      = hvs_get_min_buffer_size,
824         .get_max_buffer_size      = hvs_get_max_buffer_size,
825 };
826
827 static int hvs_probe(struct hv_device *hdev,
828                      const struct hv_vmbus_device_id *dev_id)
829 {
830         struct vmbus_channel *chan = hdev->channel;
831
832         hvs_open_connection(chan);
833
834         /* Always return success to suppress the unnecessary error message
835          * in vmbus_probe(): on error the host will rescind the device in
836          * 30 seconds and we can do cleanup at that time in
837          * vmbus_onoffer_rescind().
838          */
839         return 0;
840 }
841
842 static int hvs_remove(struct hv_device *hdev)
843 {
844         struct vmbus_channel *chan = hdev->channel;
845
846         vmbus_close(chan);
847
848         return 0;
849 }
850
851 /* This isn't really used. See vmbus_match() and vmbus_probe() */
852 static const struct hv_vmbus_device_id id_table[] = {
853         {},
854 };
855
856 static struct hv_driver hvs_drv = {
857         .name           = "hv_sock",
858         .hvsock         = true,
859         .id_table       = id_table,
860         .probe          = hvs_probe,
861         .remove         = hvs_remove,
862 };
863
864 static int __init hvs_init(void)
865 {
866         int ret;
867
868         if (vmbus_proto_version < VERSION_WIN10)
869                 return -ENODEV;
870
871         ret = vmbus_driver_register(&hvs_drv);
872         if (ret != 0)
873                 return ret;
874
875         ret = vsock_core_init(&hvs_transport);
876         if (ret) {
877                 vmbus_driver_unregister(&hvs_drv);
878                 return ret;
879         }
880
881         return 0;
882 }
883
884 static void __exit hvs_exit(void)
885 {
886         vsock_core_exit();
887         vmbus_driver_unregister(&hvs_drv);
888 }
889
890 module_init(hvs_init);
891 module_exit(hvs_exit);
892
893 MODULE_DESCRIPTION("Hyper-V Sockets");
894 MODULE_VERSION("1.0.0");
895 MODULE_LICENSE("GPL");
896 MODULE_ALIAS_NETPROTO(PF_VSOCK);