1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * vhost transport for vsock 4 * 5 * Copyright (C) 2013-2015 Red Hat, Inc. 6 * Author: Asias He <asias@redhat.com> 7 * Stefan Hajnoczi <stefanha@redhat.com> 8 */ 9 #include <linux/miscdevice.h> 10 #include <linux/atomic.h> 11 #include <linux/module.h> 12 #include <linux/mutex.h> 13 #include <linux/vmalloc.h> 14 #include <net/sock.h> 15 #include <linux/virtio_vsock.h> 16 #include <linux/vhost.h> 17 #include <linux/hashtable.h> 18 19 #include <net/af_vsock.h> 20 #include "vhost.h" 21 22 #define VHOST_VSOCK_DEFAULT_HOST_CID 2 23 /* Max number of bytes transferred before requeueing the job. 24 * Using this limit prevents one virtqueue from starving others. */ 25 #define VHOST_VSOCK_WEIGHT 0x80000 26 /* Max number of packets transferred before requeueing the job. 27 * Using this limit prevents one virtqueue from starving others with 28 * small pkts. 29 */ 30 #define VHOST_VSOCK_PKT_WEIGHT 256 31 32 static const int vhost_vsock_bits[] = { 33 VHOST_FEATURES, 34 VIRTIO_F_ACCESS_PLATFORM, 35 VIRTIO_VSOCK_F_SEQPACKET 36 }; 37 38 #define VHOST_VSOCK_FEATURES VHOST_FEATURES_U64(vhost_vsock_bits, 0) 39 40 enum { 41 VHOST_VSOCK_BACKEND_FEATURES = (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2) 42 }; 43 44 /* Used to track all the vhost_vsock instances on the system. */ 45 static DEFINE_MUTEX(vhost_vsock_mutex); 46 static DEFINE_READ_MOSTLY_HASHTABLE(vhost_vsock_hash, 8); 47 48 struct vhost_vsock { 49 struct vhost_dev dev; 50 struct vhost_virtqueue vqs[2]; 51 struct net *net; 52 netns_tracker ns_tracker; 53 54 /* Link to global vhost_vsock_hash, writes use vhost_vsock_mutex */ 55 struct hlist_node hash; 56 57 struct vhost_work send_pkt_work; 58 struct sk_buff_head send_pkt_queue; /* host->guest pending packets */ 59 60 atomic_t queued_replies; 61 62 u32 guest_cid; 63 bool seqpacket_allow; 64 }; 65 66 static u32 vhost_transport_get_local_cid(void) 67 { 68 return VHOST_VSOCK_DEFAULT_HOST_CID; 69 } 70 71 /* Callers must be in an RCU read section or hold the vhost_vsock_mutex. 72 * The return value can only be dereferenced while within the section. 73 */ 74 static struct vhost_vsock *vhost_vsock_get(u32 guest_cid, struct net *net) 75 { 76 struct vhost_vsock *vsock; 77 78 hash_for_each_possible_rcu(vhost_vsock_hash, vsock, hash, guest_cid, 79 lockdep_is_held(&vhost_vsock_mutex)) { 80 u32 other_cid = vsock->guest_cid; 81 82 /* Skip instances that have no CID yet */ 83 if (other_cid == 0) 84 continue; 85 86 if (other_cid == guest_cid && 87 vsock_net_check_mode(net, vsock->net)) 88 return vsock; 89 } 90 91 return NULL; 92 } 93 94 static bool vhost_transport_has_remote_cid(struct vsock_sock *vsk, u32 cid) 95 { 96 struct sock *sk = sk_vsock(vsk); 97 struct net *net = sock_net(sk); 98 bool found; 99 100 rcu_read_lock(); 101 found = !!vhost_vsock_get(cid, net); 102 rcu_read_unlock(); 103 return found; 104 } 105 106 static void 107 vhost_transport_do_send_pkt(struct vhost_vsock *vsock, 108 struct vhost_virtqueue *vq) 109 { 110 struct vhost_virtqueue *tx_vq = &vsock->vqs[VSOCK_VQ_TX]; 111 int pkts = 0, total_len = 0; 112 bool added = false; 113 bool restart_tx = false; 114 115 mutex_lock(&vq->mutex); 116 117 if (!vhost_vq_get_backend(vq)) 118 goto out; 119 120 if (!vq_meta_prefetch(vq)) 121 goto out; 122 123 /* Avoid further vmexits, we're already processing the virtqueue */ 124 vhost_disable_notify(&vsock->dev, vq); 125 126 do { 127 struct virtio_vsock_hdr *hdr; 128 size_t iov_len, payload_len; 129 struct iov_iter iov_iter; 130 u32 flags_to_restore = 0; 131 struct sk_buff *skb; 132 unsigned out, in; 133 size_t nbytes; 134 u32 offset; 135 int head; 136 137 skb = virtio_vsock_skb_dequeue(&vsock->send_pkt_queue); 138 139 if (!skb) { 140 vhost_enable_notify(&vsock->dev, vq); 141 break; 142 } 143 144 head = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov), 145 &out, &in, NULL, NULL); 146 if (head < 0) { 147 virtio_vsock_skb_queue_head(&vsock->send_pkt_queue, skb); 148 break; 149 } 150 151 if (head == vq->num) { 152 virtio_vsock_skb_queue_head(&vsock->send_pkt_queue, skb); 153 /* We cannot finish yet if more buffers snuck in while 154 * re-enabling notify. 155 */ 156 if (unlikely(vhost_enable_notify(&vsock->dev, vq))) { 157 vhost_disable_notify(&vsock->dev, vq); 158 continue; 159 } 160 break; 161 } 162 163 if (out) { 164 kfree_skb(skb); 165 vq_err(vq, "Expected 0 output buffers, got %u\n", out); 166 break; 167 } 168 169 iov_len = iov_length(&vq->iov[out], in); 170 if (iov_len < sizeof(*hdr)) { 171 kfree_skb(skb); 172 vq_err(vq, "Buffer len [%zu] too small\n", iov_len); 173 break; 174 } 175 176 iov_iter_init(&iov_iter, ITER_DEST, &vq->iov[out], in, iov_len); 177 offset = VIRTIO_VSOCK_SKB_CB(skb)->offset; 178 payload_len = skb->len - offset; 179 hdr = virtio_vsock_hdr(skb); 180 181 /* If the packet is greater than the space available in the 182 * buffer, we split it using multiple buffers. 183 */ 184 if (payload_len > iov_len - sizeof(*hdr)) { 185 payload_len = iov_len - sizeof(*hdr); 186 187 /* As we are copying pieces of large packet's buffer to 188 * small rx buffers, headers of packets in rx queue are 189 * created dynamically and are initialized with header 190 * of current packet(except length). But in case of 191 * SOCK_SEQPACKET, we also must clear message delimeter 192 * bit (VIRTIO_VSOCK_SEQ_EOM) and MSG_EOR bit 193 * (VIRTIO_VSOCK_SEQ_EOR) if set. Otherwise, 194 * there will be sequence of packets with these 195 * bits set. After initialized header will be copied to 196 * rx buffer, these required bits will be restored. 197 */ 198 if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOM) { 199 hdr->flags &= ~cpu_to_le32(VIRTIO_VSOCK_SEQ_EOM); 200 flags_to_restore |= VIRTIO_VSOCK_SEQ_EOM; 201 202 if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOR) { 203 hdr->flags &= ~cpu_to_le32(VIRTIO_VSOCK_SEQ_EOR); 204 flags_to_restore |= VIRTIO_VSOCK_SEQ_EOR; 205 } 206 } 207 } 208 209 /* Set the correct length in the header */ 210 hdr->len = cpu_to_le32(payload_len); 211 212 nbytes = copy_to_iter(hdr, sizeof(*hdr), &iov_iter); 213 if (nbytes != sizeof(*hdr)) { 214 kfree_skb(skb); 215 vq_err(vq, "Faulted on copying pkt hdr\n"); 216 break; 217 } 218 219 if (skb_copy_datagram_iter(skb, 220 offset, 221 &iov_iter, 222 payload_len)) { 223 kfree_skb(skb); 224 vq_err(vq, "Faulted on copying pkt buf\n"); 225 break; 226 } 227 228 /* Deliver to monitoring devices all packets that we 229 * will transmit. 230 */ 231 virtio_transport_deliver_tap_pkt(skb); 232 233 vhost_add_used(vq, head, sizeof(*hdr) + payload_len); 234 added = true; 235 236 VIRTIO_VSOCK_SKB_CB(skb)->offset += payload_len; 237 total_len += payload_len; 238 239 /* If we didn't send all the payload we can requeue the packet 240 * to send it with the next available buffer. 241 */ 242 if (VIRTIO_VSOCK_SKB_CB(skb)->offset < skb->len) { 243 hdr->flags |= cpu_to_le32(flags_to_restore); 244 245 /* We are queueing the same skb to handle 246 * the remaining bytes, and we want to deliver it 247 * to monitoring devices in the next iteration. 248 */ 249 virtio_vsock_skb_clear_tap_delivered(skb); 250 virtio_vsock_skb_queue_head(&vsock->send_pkt_queue, skb); 251 } else { 252 if (virtio_vsock_skb_reply(skb)) { 253 int val; 254 255 val = atomic_dec_return(&vsock->queued_replies); 256 257 /* Do we have resources to resume tx 258 * processing? 259 */ 260 if (val + 1 == tx_vq->num) 261 restart_tx = true; 262 } 263 264 virtio_transport_consume_skb_sent(skb, true); 265 } 266 } while(likely(!vhost_exceeds_weight(vq, ++pkts, total_len))); 267 if (added) 268 vhost_signal(&vsock->dev, vq); 269 270 out: 271 mutex_unlock(&vq->mutex); 272 273 if (restart_tx) 274 vhost_poll_queue(&tx_vq->poll); 275 } 276 277 static void vhost_transport_send_pkt_work(struct vhost_work *work) 278 { 279 struct vhost_virtqueue *vq; 280 struct vhost_vsock *vsock; 281 282 vsock = container_of(work, struct vhost_vsock, send_pkt_work); 283 vq = &vsock->vqs[VSOCK_VQ_RX]; 284 285 vhost_transport_do_send_pkt(vsock, vq); 286 } 287 288 static int 289 vhost_transport_send_pkt(struct sk_buff *skb, struct net *net) 290 { 291 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb); 292 struct vhost_vsock *vsock; 293 int len = skb->len; 294 295 rcu_read_lock(); 296 297 /* Find the vhost_vsock according to guest context id */ 298 vsock = vhost_vsock_get(le64_to_cpu(hdr->dst_cid), net); 299 if (!vsock) { 300 rcu_read_unlock(); 301 kfree_skb(skb); 302 return -ENODEV; 303 } 304 305 if (virtio_vsock_skb_reply(skb)) 306 atomic_inc(&vsock->queued_replies); 307 308 virtio_vsock_skb_queue_tail(&vsock->send_pkt_queue, skb); 309 vhost_vq_work_queue(&vsock->vqs[VSOCK_VQ_RX], &vsock->send_pkt_work); 310 311 rcu_read_unlock(); 312 return len; 313 } 314 315 static int 316 vhost_transport_cancel_pkt(struct vsock_sock *vsk) 317 { 318 struct vhost_vsock *vsock; 319 int cnt = 0; 320 int ret = -ENODEV; 321 322 rcu_read_lock(); 323 324 /* Find the vhost_vsock according to guest context id */ 325 vsock = vhost_vsock_get(vsk->remote_addr.svm_cid, 326 sock_net(sk_vsock(vsk))); 327 if (!vsock) 328 goto out; 329 330 cnt = virtio_transport_purge_skbs(vsk, &vsock->send_pkt_queue); 331 332 if (cnt) { 333 struct vhost_virtqueue *tx_vq = &vsock->vqs[VSOCK_VQ_TX]; 334 int new_cnt; 335 336 new_cnt = atomic_sub_return(cnt, &vsock->queued_replies); 337 if (new_cnt + cnt >= tx_vq->num && new_cnt < tx_vq->num) 338 vhost_poll_queue(&tx_vq->poll); 339 } 340 341 ret = 0; 342 out: 343 rcu_read_unlock(); 344 return ret; 345 } 346 347 static struct sk_buff * 348 vhost_vsock_alloc_skb(struct vhost_virtqueue *vq, 349 unsigned int out, unsigned int in) 350 { 351 struct virtio_vsock_hdr *hdr; 352 struct iov_iter iov_iter; 353 struct sk_buff *skb; 354 size_t payload_len; 355 size_t nbytes; 356 size_t len; 357 358 if (in != 0) { 359 vq_err(vq, "Expected 0 input buffers, got %u\n", in); 360 return NULL; 361 } 362 363 len = iov_length(vq->iov, out); 364 365 if (len < VIRTIO_VSOCK_SKB_HEADROOM || 366 len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM) 367 return NULL; 368 369 /* len contains both payload and hdr */ 370 skb = virtio_vsock_alloc_skb(len, GFP_KERNEL); 371 if (!skb) 372 return NULL; 373 374 iov_iter_init(&iov_iter, ITER_SOURCE, vq->iov, out, len); 375 376 hdr = virtio_vsock_hdr(skb); 377 nbytes = copy_from_iter(hdr, sizeof(*hdr), &iov_iter); 378 if (nbytes != sizeof(*hdr)) { 379 vq_err(vq, "Expected %zu bytes for pkt->hdr, got %zu bytes\n", 380 sizeof(*hdr), nbytes); 381 kfree_skb(skb); 382 return NULL; 383 } 384 385 payload_len = le32_to_cpu(hdr->len); 386 387 /* No payload */ 388 if (!payload_len) 389 return skb; 390 391 /* The pkt is too big or the length in the header is invalid */ 392 if (payload_len + sizeof(*hdr) > len) { 393 kfree_skb(skb); 394 return NULL; 395 } 396 397 virtio_vsock_skb_put(skb, payload_len); 398 399 if (skb_copy_datagram_from_iter(skb, 0, &iov_iter, payload_len)) { 400 vq_err(vq, "Failed to copy %zu byte payload\n", payload_len); 401 kfree_skb(skb); 402 return NULL; 403 } 404 405 return skb; 406 } 407 408 /* Is there space left for replies to rx packets? */ 409 static bool vhost_vsock_more_replies(struct vhost_vsock *vsock) 410 { 411 struct vhost_virtqueue *vq = &vsock->vqs[VSOCK_VQ_TX]; 412 int val; 413 414 smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */ 415 val = atomic_read(&vsock->queued_replies); 416 417 return val < vq->num; 418 } 419 420 static bool vhost_transport_msgzerocopy_allow(void) 421 { 422 return true; 423 } 424 425 static bool vhost_transport_seqpacket_allow(struct vsock_sock *vsk, 426 u32 remote_cid); 427 428 static bool 429 vhost_transport_stream_allow(struct vsock_sock *vsk, u32 cid, u32 port) 430 { 431 return true; 432 } 433 434 static struct virtio_transport vhost_transport = { 435 .transport = { 436 .module = THIS_MODULE, 437 438 .get_local_cid = vhost_transport_get_local_cid, 439 .has_remote_cid = vhost_transport_has_remote_cid, 440 441 .init = virtio_transport_do_socket_init, 442 .destruct = virtio_transport_destruct, 443 .release = virtio_transport_release, 444 .connect = virtio_transport_connect, 445 .shutdown = virtio_transport_shutdown, 446 .cancel_pkt = vhost_transport_cancel_pkt, 447 448 .dgram_enqueue = virtio_transport_dgram_enqueue, 449 .dgram_dequeue = virtio_transport_dgram_dequeue, 450 .dgram_bind = virtio_transport_dgram_bind, 451 .dgram_allow = virtio_transport_dgram_allow, 452 453 .stream_enqueue = virtio_transport_stream_enqueue, 454 .stream_dequeue = virtio_transport_stream_dequeue, 455 .stream_has_data = virtio_transport_stream_has_data, 456 .stream_has_space = virtio_transport_stream_has_space, 457 .stream_rcvhiwat = virtio_transport_stream_rcvhiwat, 458 .stream_is_active = virtio_transport_stream_is_active, 459 .stream_allow = vhost_transport_stream_allow, 460 461 .seqpacket_dequeue = virtio_transport_seqpacket_dequeue, 462 .seqpacket_enqueue = virtio_transport_seqpacket_enqueue, 463 .seqpacket_allow = vhost_transport_seqpacket_allow, 464 .seqpacket_has_data = virtio_transport_seqpacket_has_data, 465 466 .msgzerocopy_allow = vhost_transport_msgzerocopy_allow, 467 468 .notify_poll_in = virtio_transport_notify_poll_in, 469 .notify_poll_out = virtio_transport_notify_poll_out, 470 .notify_recv_init = virtio_transport_notify_recv_init, 471 .notify_recv_pre_block = virtio_transport_notify_recv_pre_block, 472 .notify_recv_pre_dequeue = virtio_transport_notify_recv_pre_dequeue, 473 .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue, 474 .notify_send_init = virtio_transport_notify_send_init, 475 .notify_send_pre_block = virtio_transport_notify_send_pre_block, 476 .notify_send_pre_enqueue = virtio_transport_notify_send_pre_enqueue, 477 .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue, 478 .notify_buffer_size = virtio_transport_notify_buffer_size, 479 .notify_set_rcvlowat = virtio_transport_notify_set_rcvlowat, 480 481 .unsent_bytes = virtio_transport_unsent_bytes, 482 483 .read_skb = virtio_transport_read_skb, 484 }, 485 486 .send_pkt = vhost_transport_send_pkt, 487 }; 488 489 static bool vhost_transport_seqpacket_allow(struct vsock_sock *vsk, 490 u32 remote_cid) 491 { 492 struct net *net = sock_net(sk_vsock(vsk)); 493 struct vhost_vsock *vsock; 494 bool seqpacket_allow = false; 495 496 rcu_read_lock(); 497 vsock = vhost_vsock_get(remote_cid, net); 498 499 if (vsock) 500 seqpacket_allow = vsock->seqpacket_allow; 501 502 rcu_read_unlock(); 503 504 return seqpacket_allow; 505 } 506 507 static void vhost_vsock_handle_tx_kick(struct vhost_work *work) 508 { 509 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue, 510 poll.work); 511 struct vhost_vsock *vsock = container_of(vq->dev, struct vhost_vsock, 512 dev); 513 int head, pkts = 0, total_len = 0; 514 unsigned int out, in; 515 struct sk_buff *skb; 516 bool added = false; 517 518 mutex_lock(&vq->mutex); 519 520 if (!vhost_vq_get_backend(vq)) 521 goto out; 522 523 if (!vq_meta_prefetch(vq)) 524 goto out; 525 526 vhost_disable_notify(&vsock->dev, vq); 527 do { 528 struct virtio_vsock_hdr *hdr; 529 530 if (!vhost_vsock_more_replies(vsock)) { 531 /* Stop tx until the device processes already 532 * pending replies. Leave tx virtqueue 533 * callbacks disabled. 534 */ 535 goto no_more_replies; 536 } 537 538 head = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov), 539 &out, &in, NULL, NULL); 540 if (head < 0) 541 break; 542 543 if (head == vq->num) { 544 if (unlikely(vhost_enable_notify(&vsock->dev, vq))) { 545 vhost_disable_notify(&vsock->dev, vq); 546 continue; 547 } 548 break; 549 } 550 551 skb = vhost_vsock_alloc_skb(vq, out, in); 552 if (!skb) { 553 vq_err(vq, "Faulted on pkt\n"); 554 continue; 555 } 556 557 total_len += sizeof(*hdr) + skb->len; 558 559 /* Deliver to monitoring devices all received packets */ 560 virtio_transport_deliver_tap_pkt(skb); 561 562 hdr = virtio_vsock_hdr(skb); 563 564 /* Only accept correctly addressed packets */ 565 if (le64_to_cpu(hdr->src_cid) == vsock->guest_cid && 566 le64_to_cpu(hdr->dst_cid) == 567 vhost_transport_get_local_cid()) 568 virtio_transport_recv_pkt(&vhost_transport, skb, 569 vsock->net); 570 else 571 kfree_skb(skb); 572 573 vhost_add_used(vq, head, 0); 574 added = true; 575 } while(likely(!vhost_exceeds_weight(vq, ++pkts, total_len))); 576 577 no_more_replies: 578 if (added) 579 vhost_signal(&vsock->dev, vq); 580 581 out: 582 mutex_unlock(&vq->mutex); 583 } 584 585 static void vhost_vsock_handle_rx_kick(struct vhost_work *work) 586 { 587 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue, 588 poll.work); 589 struct vhost_vsock *vsock = container_of(vq->dev, struct vhost_vsock, 590 dev); 591 592 vhost_transport_do_send_pkt(vsock, vq); 593 } 594 595 static int vhost_vsock_start(struct vhost_vsock *vsock) 596 { 597 struct vhost_virtqueue *vq; 598 size_t i; 599 int ret; 600 601 mutex_lock(&vsock->dev.mutex); 602 603 ret = vhost_dev_check_owner(&vsock->dev); 604 if (ret) 605 goto err; 606 607 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) { 608 vq = &vsock->vqs[i]; 609 610 mutex_lock(&vq->mutex); 611 612 if (!vhost_vq_access_ok(vq)) { 613 ret = -EFAULT; 614 goto err_vq; 615 } 616 617 if (!vhost_vq_get_backend(vq)) { 618 vhost_vq_set_backend(vq, vsock); 619 ret = vhost_vq_init_access(vq); 620 if (ret) 621 goto err_vq; 622 } 623 624 mutex_unlock(&vq->mutex); 625 } 626 627 /* Some packets may have been queued before the device was started, 628 * let's kick the send worker to send them. 629 */ 630 vhost_vq_work_queue(&vsock->vqs[VSOCK_VQ_RX], &vsock->send_pkt_work); 631 632 mutex_unlock(&vsock->dev.mutex); 633 return 0; 634 635 err_vq: 636 vhost_vq_set_backend(vq, NULL); 637 mutex_unlock(&vq->mutex); 638 639 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) { 640 vq = &vsock->vqs[i]; 641 642 mutex_lock(&vq->mutex); 643 vhost_vq_set_backend(vq, NULL); 644 mutex_unlock(&vq->mutex); 645 } 646 err: 647 mutex_unlock(&vsock->dev.mutex); 648 return ret; 649 } 650 651 static int vhost_vsock_stop(struct vhost_vsock *vsock, bool check_owner) 652 { 653 size_t i; 654 int ret = 0; 655 656 mutex_lock(&vsock->dev.mutex); 657 658 if (check_owner) { 659 ret = vhost_dev_check_owner(&vsock->dev); 660 if (ret) 661 goto err; 662 } 663 664 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) { 665 struct vhost_virtqueue *vq = &vsock->vqs[i]; 666 667 mutex_lock(&vq->mutex); 668 vhost_vq_set_backend(vq, NULL); 669 mutex_unlock(&vq->mutex); 670 } 671 672 err: 673 mutex_unlock(&vsock->dev.mutex); 674 return ret; 675 } 676 677 static void vhost_vsock_free(struct vhost_vsock *vsock) 678 { 679 kvfree(vsock); 680 } 681 682 static int vhost_vsock_dev_open(struct inode *inode, struct file *file) 683 { 684 struct vhost_virtqueue **vqs; 685 struct vhost_vsock *vsock; 686 struct net *net; 687 int ret; 688 689 /* This struct is large and allocation could fail, fall back to vmalloc 690 * if there is no other way. 691 */ 692 vsock = kvmalloc_obj(*vsock, GFP_KERNEL | __GFP_RETRY_MAYFAIL); 693 if (!vsock) 694 return -ENOMEM; 695 696 vqs = kmalloc_objs(*vqs, ARRAY_SIZE(vsock->vqs)); 697 if (!vqs) { 698 ret = -ENOMEM; 699 goto out; 700 } 701 702 net = current->nsproxy->net_ns; 703 vsock->net = get_net_track(net, &vsock->ns_tracker, GFP_KERNEL); 704 705 vsock->guest_cid = 0; /* no CID assigned yet */ 706 vsock->seqpacket_allow = false; 707 708 atomic_set(&vsock->queued_replies, 0); 709 710 vqs[VSOCK_VQ_TX] = &vsock->vqs[VSOCK_VQ_TX]; 711 vqs[VSOCK_VQ_RX] = &vsock->vqs[VSOCK_VQ_RX]; 712 vsock->vqs[VSOCK_VQ_TX].handle_kick = vhost_vsock_handle_tx_kick; 713 vsock->vqs[VSOCK_VQ_RX].handle_kick = vhost_vsock_handle_rx_kick; 714 715 vhost_dev_init(&vsock->dev, vqs, ARRAY_SIZE(vsock->vqs), 716 UIO_MAXIOV, VHOST_VSOCK_PKT_WEIGHT, 717 VHOST_VSOCK_WEIGHT, true, NULL); 718 719 file->private_data = vsock; 720 skb_queue_head_init(&vsock->send_pkt_queue); 721 vhost_work_init(&vsock->send_pkt_work, vhost_transport_send_pkt_work); 722 return 0; 723 724 out: 725 vhost_vsock_free(vsock); 726 return ret; 727 } 728 729 static void vhost_vsock_flush(struct vhost_vsock *vsock) 730 { 731 vhost_dev_flush(&vsock->dev); 732 } 733 734 static void vhost_vsock_reset_orphans(struct sock *sk) 735 { 736 struct vsock_sock *vsk = vsock_sk(sk); 737 738 /* vmci_transport.c doesn't take sk_lock here either. At least we're 739 * under vsock_table_lock so the sock cannot disappear while we're 740 * executing. 741 */ 742 743 rcu_read_lock(); 744 745 /* If the peer is still valid, no need to reset connection */ 746 if (vhost_vsock_get(vsk->remote_addr.svm_cid, sock_net(sk))) { 747 rcu_read_unlock(); 748 return; 749 } 750 751 rcu_read_unlock(); 752 753 /* If the close timeout is pending, let it expire. This avoids races 754 * with the timeout callback. 755 */ 756 if (vsk->close_work_scheduled) 757 return; 758 759 sock_set_flag(sk, SOCK_DONE); 760 vsk->peer_shutdown = SHUTDOWN_MASK; 761 sk->sk_state = SS_UNCONNECTED; 762 sk->sk_err = ECONNRESET; 763 sk_error_report(sk); 764 } 765 766 static int vhost_vsock_dev_release(struct inode *inode, struct file *file) 767 { 768 struct vhost_vsock *vsock = file->private_data; 769 770 mutex_lock(&vhost_vsock_mutex); 771 if (vsock->guest_cid) 772 hash_del_rcu(&vsock->hash); 773 mutex_unlock(&vhost_vsock_mutex); 774 775 /* Wait for other CPUs to finish using vsock */ 776 synchronize_rcu(); 777 778 /* Iterating over all connections for all CIDs to find orphans is 779 * inefficient. Room for improvement here. */ 780 vsock_for_each_connected_socket(&vhost_transport.transport, 781 vhost_vsock_reset_orphans); 782 783 /* Don't check the owner, because we are in the release path, so we 784 * need to stop the vsock device in any case. 785 * vhost_vsock_stop() can not fail in this case, so we don't need to 786 * check the return code. 787 */ 788 vhost_vsock_stop(vsock, false); 789 vhost_vsock_flush(vsock); 790 vhost_dev_stop(&vsock->dev); 791 792 virtio_vsock_skb_queue_purge(&vsock->send_pkt_queue); 793 794 vhost_dev_cleanup(&vsock->dev); 795 put_net_track(vsock->net, &vsock->ns_tracker); 796 kfree(vsock->dev.vqs); 797 vhost_vsock_free(vsock); 798 return 0; 799 } 800 801 static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid) 802 { 803 struct vhost_vsock *other; 804 805 /* Refuse reserved CIDs */ 806 if (guest_cid <= VMADDR_CID_HOST || 807 guest_cid == U32_MAX) 808 return -EINVAL; 809 810 /* 64-bit CIDs are not yet supported */ 811 if (guest_cid > U32_MAX) 812 return -EINVAL; 813 814 /* Refuse if CID is assigned to the guest->host transport (i.e. nested 815 * VM), to make the loopback work. 816 */ 817 if (vsock_find_cid(guest_cid)) 818 return -EADDRINUSE; 819 820 /* Refuse if CID is already in use */ 821 mutex_lock(&vhost_vsock_mutex); 822 other = vhost_vsock_get(guest_cid, vsock->net); 823 if (other && other != vsock) { 824 mutex_unlock(&vhost_vsock_mutex); 825 return -EADDRINUSE; 826 } 827 828 if (vsock->guest_cid) 829 hash_del_rcu(&vsock->hash); 830 831 vsock->guest_cid = guest_cid; 832 hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid); 833 mutex_unlock(&vhost_vsock_mutex); 834 835 return 0; 836 } 837 838 static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features) 839 { 840 struct vhost_virtqueue *vq; 841 int i; 842 843 if (features & ~VHOST_VSOCK_FEATURES) 844 return -EOPNOTSUPP; 845 846 mutex_lock(&vsock->dev.mutex); 847 if ((features & (1 << VHOST_F_LOG_ALL)) && 848 !vhost_log_access_ok(&vsock->dev)) { 849 goto err; 850 } 851 852 if ((features & (1ULL << VIRTIO_F_ACCESS_PLATFORM))) { 853 if (vhost_init_device_iotlb(&vsock->dev)) 854 goto err; 855 } 856 857 vsock->seqpacket_allow = features & (1ULL << VIRTIO_VSOCK_F_SEQPACKET); 858 859 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) { 860 vq = &vsock->vqs[i]; 861 mutex_lock(&vq->mutex); 862 vq->acked_features = features; 863 mutex_unlock(&vq->mutex); 864 } 865 mutex_unlock(&vsock->dev.mutex); 866 return 0; 867 868 err: 869 mutex_unlock(&vsock->dev.mutex); 870 return -EFAULT; 871 } 872 873 static long vhost_vsock_dev_ioctl(struct file *f, unsigned int ioctl, 874 unsigned long arg) 875 { 876 struct vhost_vsock *vsock = f->private_data; 877 void __user *argp = (void __user *)arg; 878 u64 guest_cid; 879 u64 features; 880 int start; 881 int r; 882 883 switch (ioctl) { 884 case VHOST_VSOCK_SET_GUEST_CID: 885 if (copy_from_user(&guest_cid, argp, sizeof(guest_cid))) 886 return -EFAULT; 887 return vhost_vsock_set_cid(vsock, guest_cid); 888 case VHOST_VSOCK_SET_RUNNING: 889 if (copy_from_user(&start, argp, sizeof(start))) 890 return -EFAULT; 891 if (start) 892 return vhost_vsock_start(vsock); 893 else 894 return vhost_vsock_stop(vsock, true); 895 case VHOST_GET_FEATURES: 896 features = VHOST_VSOCK_FEATURES; 897 if (copy_to_user(argp, &features, sizeof(features))) 898 return -EFAULT; 899 return 0; 900 case VHOST_SET_FEATURES: 901 if (copy_from_user(&features, argp, sizeof(features))) 902 return -EFAULT; 903 return vhost_vsock_set_features(vsock, features); 904 case VHOST_GET_BACKEND_FEATURES: 905 features = VHOST_VSOCK_BACKEND_FEATURES; 906 if (copy_to_user(argp, &features, sizeof(features))) 907 return -EFAULT; 908 return 0; 909 case VHOST_SET_BACKEND_FEATURES: 910 if (copy_from_user(&features, argp, sizeof(features))) 911 return -EFAULT; 912 if (features & ~VHOST_VSOCK_BACKEND_FEATURES) 913 return -EOPNOTSUPP; 914 vhost_set_backend_features(&vsock->dev, features); 915 return 0; 916 default: 917 mutex_lock(&vsock->dev.mutex); 918 r = vhost_dev_ioctl(&vsock->dev, ioctl, argp); 919 if (r == -ENOIOCTLCMD) 920 r = vhost_vring_ioctl(&vsock->dev, ioctl, argp); 921 else 922 vhost_vsock_flush(vsock); 923 mutex_unlock(&vsock->dev.mutex); 924 return r; 925 } 926 } 927 928 static ssize_t vhost_vsock_chr_read_iter(struct kiocb *iocb, struct iov_iter *to) 929 { 930 struct file *file = iocb->ki_filp; 931 struct vhost_vsock *vsock = file->private_data; 932 struct vhost_dev *dev = &vsock->dev; 933 int noblock = file->f_flags & O_NONBLOCK; 934 935 return vhost_chr_read_iter(dev, to, noblock); 936 } 937 938 static ssize_t vhost_vsock_chr_write_iter(struct kiocb *iocb, 939 struct iov_iter *from) 940 { 941 struct file *file = iocb->ki_filp; 942 struct vhost_vsock *vsock = file->private_data; 943 struct vhost_dev *dev = &vsock->dev; 944 945 return vhost_chr_write_iter(dev, from); 946 } 947 948 static __poll_t vhost_vsock_chr_poll(struct file *file, poll_table *wait) 949 { 950 struct vhost_vsock *vsock = file->private_data; 951 struct vhost_dev *dev = &vsock->dev; 952 953 return vhost_chr_poll(file, dev, wait); 954 } 955 956 static const struct file_operations vhost_vsock_fops = { 957 .owner = THIS_MODULE, 958 .open = vhost_vsock_dev_open, 959 .release = vhost_vsock_dev_release, 960 .llseek = noop_llseek, 961 .unlocked_ioctl = vhost_vsock_dev_ioctl, 962 .compat_ioctl = compat_ptr_ioctl, 963 .read_iter = vhost_vsock_chr_read_iter, 964 .write_iter = vhost_vsock_chr_write_iter, 965 .poll = vhost_vsock_chr_poll, 966 }; 967 968 static struct miscdevice vhost_vsock_misc = { 969 .minor = VHOST_VSOCK_MINOR, 970 .name = "vhost-vsock", 971 .fops = &vhost_vsock_fops, 972 }; 973 974 static int __init vhost_vsock_init(void) 975 { 976 int ret; 977 978 ret = vsock_core_register(&vhost_transport.transport, 979 VSOCK_TRANSPORT_F_H2G); 980 if (ret < 0) 981 return ret; 982 983 ret = misc_register(&vhost_vsock_misc); 984 if (ret) { 985 vsock_core_unregister(&vhost_transport.transport); 986 return ret; 987 } 988 989 return 0; 990 }; 991 992 static void __exit vhost_vsock_exit(void) 993 { 994 misc_deregister(&vhost_vsock_misc); 995 vsock_core_unregister(&vhost_transport.transport); 996 }; 997 998 module_init(vhost_vsock_init); 999 module_exit(vhost_vsock_exit); 1000 MODULE_LICENSE("GPL v2"); 1001 MODULE_AUTHOR("Asias He"); 1002 MODULE_DESCRIPTION("vhost transport for vsock "); 1003 MODULE_ALIAS_MISCDEV(VHOST_VSOCK_MINOR); 1004 MODULE_ALIAS("devname:vhost-vsock"); 1005