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