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