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