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