1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (C) 2009 Red Hat, Inc. 3 * Author: Michael S. Tsirkin <mst@redhat.com> 4 * 5 * virtio-net server in host kernel. 6 */ 7 8 #include <linux/compat.h> 9 #include <linux/eventfd.h> 10 #include <linux/vhost.h> 11 #include <linux/virtio_net.h> 12 #include <linux/miscdevice.h> 13 #include <linux/module.h> 14 #include <linux/moduleparam.h> 15 #include <linux/mutex.h> 16 #include <linux/workqueue.h> 17 #include <linux/file.h> 18 #include <linux/slab.h> 19 #include <linux/sched/clock.h> 20 #include <linux/sched/signal.h> 21 #include <linux/vmalloc.h> 22 23 #include <linux/net.h> 24 #include <linux/if_packet.h> 25 #include <linux/if_arp.h> 26 #include <linux/if_tun.h> 27 #include <linux/if_macvlan.h> 28 #include <linux/if_tap.h> 29 #include <linux/if_vlan.h> 30 #include <linux/skb_array.h> 31 #include <linux/skbuff.h> 32 33 #include <net/sock.h> 34 #include <net/xdp.h> 35 36 #include "vhost.h" 37 38 static int experimental_zcopytx = 0; 39 module_param(experimental_zcopytx, int, 0444); 40 MODULE_PARM_DESC(experimental_zcopytx, "Enable Zero Copy TX;" 41 " 1 -Enable; 0 - Disable"); 42 43 /* Max number of bytes transferred before requeueing the job. 44 * Using this limit prevents one virtqueue from starving others. */ 45 #define VHOST_NET_WEIGHT 0x80000 46 47 /* Max number of packets transferred before requeueing the job. 48 * Using this limit prevents one virtqueue from starving others with small 49 * pkts. 50 */ 51 #define VHOST_NET_PKT_WEIGHT 256 52 53 /* MAX number of TX used buffers for outstanding zerocopy */ 54 #define VHOST_MAX_PEND 128 55 #define VHOST_GOODCOPY_LEN 256 56 57 /* 58 * For transmit, used buffer len is unused; we override it to track buffer 59 * status internally; used for zerocopy tx only. 60 */ 61 /* Lower device DMA failed */ 62 #define VHOST_DMA_FAILED_LEN ((__force __virtio32)3) 63 /* Lower device DMA done */ 64 #define VHOST_DMA_DONE_LEN ((__force __virtio32)2) 65 /* Lower device DMA in progress */ 66 #define VHOST_DMA_IN_PROGRESS ((__force __virtio32)1) 67 /* Buffer unused */ 68 #define VHOST_DMA_CLEAR_LEN ((__force __virtio32)0) 69 70 #define VHOST_DMA_IS_DONE(len) ((__force u32)(len) >= (__force u32)VHOST_DMA_DONE_LEN) 71 72 static const int vhost_net_bits[] = { 73 VHOST_FEATURES, 74 VHOST_NET_F_VIRTIO_NET_HDR, 75 VIRTIO_NET_F_MRG_RXBUF, 76 VIRTIO_F_ACCESS_PLATFORM, 77 VIRTIO_F_RING_RESET, 78 VIRTIO_F_IN_ORDER, 79 VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO, 80 VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO 81 }; 82 83 enum { 84 VHOST_NET_BACKEND_FEATURES = (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2) 85 }; 86 87 enum { 88 VHOST_NET_VQ_RX = 0, 89 VHOST_NET_VQ_TX = 1, 90 VHOST_NET_VQ_MAX = 2, 91 }; 92 93 struct vhost_net_ubuf_ref { 94 /* refcount follows semantics similar to kref: 95 * 0: object is released 96 * 1: no outstanding ubufs 97 * >1: outstanding ubufs 98 */ 99 atomic_t refcount; 100 wait_queue_head_t wait; 101 struct vhost_virtqueue *vq; 102 struct rcu_head rcu; 103 }; 104 105 #define VHOST_NET_BATCH 64 106 struct vhost_net_buf { 107 void **queue; 108 int tail; 109 int head; 110 }; 111 112 struct vhost_net_virtqueue { 113 struct vhost_virtqueue vq; 114 size_t vhost_hlen; 115 size_t sock_hlen; 116 /* vhost zerocopy support fields below: */ 117 /* last used idx for outstanding DMA zerocopy buffers */ 118 int upend_idx; 119 /* For TX, first used idx for DMA done zerocopy buffers 120 * For RX, number of batched heads 121 */ 122 int done_idx; 123 /* Number of XDP frames batched */ 124 int batched_xdp; 125 /* an array of userspace buffers info */ 126 struct ubuf_info_msgzc *ubuf_info; 127 /* Reference counting for outstanding ubufs. 128 * Protected by vq mutex. Writers must also take device mutex. */ 129 struct vhost_net_ubuf_ref *ubufs; 130 struct ptr_ring *rx_ring; 131 struct vhost_net_buf rxq; 132 /* Batched XDP buffs */ 133 struct xdp_buff *xdp; 134 }; 135 136 struct vhost_net { 137 struct vhost_dev dev; 138 struct vhost_net_virtqueue vqs[VHOST_NET_VQ_MAX]; 139 struct vhost_poll poll[VHOST_NET_VQ_MAX]; 140 /* Number of TX recently submitted. 141 * Protected by tx vq lock. */ 142 unsigned tx_packets; 143 /* Number of times zerocopy TX recently failed. 144 * Protected by tx vq lock. */ 145 unsigned tx_zcopy_err; 146 /* Flush in progress. Protected by tx vq lock. */ 147 bool tx_flush; 148 /* Private page frag cache */ 149 struct page_frag_cache pf_cache; 150 }; 151 152 static unsigned vhost_net_zcopy_mask __read_mostly; 153 154 static void *vhost_net_buf_get_ptr(struct vhost_net_buf *rxq) 155 { 156 if (rxq->tail != rxq->head) 157 return rxq->queue[rxq->head]; 158 else 159 return NULL; 160 } 161 162 static int vhost_net_buf_get_size(struct vhost_net_buf *rxq) 163 { 164 return rxq->tail - rxq->head; 165 } 166 167 static int vhost_net_buf_is_empty(struct vhost_net_buf *rxq) 168 { 169 return rxq->tail == rxq->head; 170 } 171 172 static void *vhost_net_buf_consume(struct vhost_net_buf *rxq) 173 { 174 void *ret = vhost_net_buf_get_ptr(rxq); 175 ++rxq->head; 176 return ret; 177 } 178 179 static int vhost_net_buf_produce(struct vhost_net_virtqueue *nvq) 180 { 181 struct vhost_net_buf *rxq = &nvq->rxq; 182 183 rxq->head = 0; 184 rxq->tail = ptr_ring_consume_batched(nvq->rx_ring, rxq->queue, 185 VHOST_NET_BATCH); 186 return rxq->tail; 187 } 188 189 static void vhost_net_buf_unproduce(struct vhost_net_virtqueue *nvq) 190 { 191 struct vhost_net_buf *rxq = &nvq->rxq; 192 193 if (nvq->rx_ring && !vhost_net_buf_is_empty(rxq)) { 194 ptr_ring_unconsume(nvq->rx_ring, rxq->queue + rxq->head, 195 vhost_net_buf_get_size(rxq), 196 tun_ptr_free); 197 rxq->head = rxq->tail = 0; 198 } 199 } 200 201 static int vhost_net_buf_peek_len(void *ptr) 202 { 203 if (tun_is_xdp_frame(ptr)) { 204 struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr); 205 206 return xdpf->len; 207 } 208 209 return __skb_array_len_with_tag(ptr); 210 } 211 212 static int vhost_net_buf_peek(struct vhost_net_virtqueue *nvq) 213 { 214 struct vhost_net_buf *rxq = &nvq->rxq; 215 216 if (!vhost_net_buf_is_empty(rxq)) 217 goto out; 218 219 if (!vhost_net_buf_produce(nvq)) 220 return 0; 221 222 out: 223 return vhost_net_buf_peek_len(vhost_net_buf_get_ptr(rxq)); 224 } 225 226 static void vhost_net_buf_init(struct vhost_net_buf *rxq) 227 { 228 rxq->head = rxq->tail = 0; 229 } 230 231 static void vhost_net_enable_zcopy(int vq) 232 { 233 vhost_net_zcopy_mask |= 0x1 << vq; 234 } 235 236 static struct vhost_net_ubuf_ref * 237 vhost_net_ubuf_alloc(struct vhost_virtqueue *vq, bool zcopy) 238 { 239 struct vhost_net_ubuf_ref *ubufs; 240 /* No zero copy backend? Nothing to count. */ 241 if (!zcopy) 242 return NULL; 243 ubufs = kmalloc_obj(*ubufs); 244 if (!ubufs) 245 return ERR_PTR(-ENOMEM); 246 atomic_set(&ubufs->refcount, 1); 247 init_waitqueue_head(&ubufs->wait); 248 ubufs->vq = vq; 249 return ubufs; 250 } 251 252 static int vhost_net_ubuf_put(struct vhost_net_ubuf_ref *ubufs) 253 { 254 int r; 255 256 rcu_read_lock(); 257 r = atomic_sub_return(1, &ubufs->refcount); 258 if (unlikely(!r)) 259 wake_up(&ubufs->wait); 260 rcu_read_unlock(); 261 return r; 262 } 263 264 static void vhost_net_ubuf_put_and_wait(struct vhost_net_ubuf_ref *ubufs) 265 { 266 vhost_net_ubuf_put(ubufs); 267 wait_event(ubufs->wait, !atomic_read(&ubufs->refcount)); 268 } 269 270 static void vhost_net_ubuf_put_wait_and_free(struct vhost_net_ubuf_ref *ubufs) 271 { 272 vhost_net_ubuf_put_and_wait(ubufs); 273 kfree_rcu(ubufs, rcu); 274 } 275 276 static void vhost_net_clear_ubuf_info(struct vhost_net *n) 277 { 278 int i; 279 280 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) { 281 kfree(n->vqs[i].ubuf_info); 282 n->vqs[i].ubuf_info = NULL; 283 } 284 } 285 286 static int vhost_net_set_ubuf_info(struct vhost_net *n) 287 { 288 bool zcopy; 289 int i; 290 291 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) { 292 zcopy = vhost_net_zcopy_mask & (0x1 << i); 293 if (!zcopy) 294 continue; 295 n->vqs[i].ubuf_info = 296 kmalloc_objs(*n->vqs[i].ubuf_info, UIO_MAXIOV); 297 if (!n->vqs[i].ubuf_info) 298 goto err; 299 } 300 return 0; 301 302 err: 303 vhost_net_clear_ubuf_info(n); 304 return -ENOMEM; 305 } 306 307 static void vhost_net_vq_reset(struct vhost_net *n) 308 { 309 int i; 310 311 vhost_net_clear_ubuf_info(n); 312 313 for (i = 0; i < VHOST_NET_VQ_MAX; i++) { 314 n->vqs[i].done_idx = 0; 315 n->vqs[i].upend_idx = 0; 316 n->vqs[i].ubufs = NULL; 317 n->vqs[i].vhost_hlen = 0; 318 n->vqs[i].sock_hlen = 0; 319 vhost_net_buf_init(&n->vqs[i].rxq); 320 } 321 322 } 323 324 static void vhost_net_tx_packet(struct vhost_net *net) 325 { 326 ++net->tx_packets; 327 if (net->tx_packets < 1024) 328 return; 329 net->tx_packets = 0; 330 net->tx_zcopy_err = 0; 331 } 332 333 static void vhost_net_tx_err(struct vhost_net *net) 334 { 335 ++net->tx_zcopy_err; 336 } 337 338 static bool vhost_net_tx_select_zcopy(struct vhost_net *net) 339 { 340 /* TX flush waits for outstanding DMAs to be done. 341 * Don't start new DMAs. 342 */ 343 return !net->tx_flush && 344 net->tx_packets / 64 >= net->tx_zcopy_err; 345 } 346 347 static bool vhost_sock_zcopy(struct socket *sock) 348 { 349 return unlikely(experimental_zcopytx) && 350 sock_flag(sock->sk, SOCK_ZEROCOPY); 351 } 352 353 static bool vhost_sock_xdp(struct socket *sock) 354 { 355 return sock_flag(sock->sk, SOCK_XDP); 356 } 357 358 /* In case of DMA done not in order in lower device driver for some reason. 359 * upend_idx is used to track end of used idx, done_idx is used to track head 360 * of used idx. Once lower device DMA done contiguously, we will signal KVM 361 * guest used idx. 362 */ 363 static void vhost_zerocopy_signal_used(struct vhost_net *net, 364 struct vhost_virtqueue *vq) 365 { 366 struct vhost_net_virtqueue *nvq = 367 container_of(vq, struct vhost_net_virtqueue, vq); 368 int i, add; 369 int j = 0; 370 371 for (i = nvq->done_idx; i != nvq->upend_idx; i = (i + 1) % UIO_MAXIOV) { 372 if (vq->heads[i].len == VHOST_DMA_FAILED_LEN) 373 vhost_net_tx_err(net); 374 if (VHOST_DMA_IS_DONE(vq->heads[i].len)) { 375 vq->heads[i].len = VHOST_DMA_CLEAR_LEN; 376 ++j; 377 } else 378 break; 379 } 380 while (j) { 381 add = min(UIO_MAXIOV - nvq->done_idx, j); 382 vhost_add_used_and_signal_n(vq->dev, vq, 383 &vq->heads[nvq->done_idx], 384 NULL, add); 385 nvq->done_idx = (nvq->done_idx + add) % UIO_MAXIOV; 386 j -= add; 387 } 388 } 389 390 static void vhost_zerocopy_complete(struct sk_buff *skb, 391 struct ubuf_info *ubuf_base, bool success) 392 { 393 struct ubuf_info_msgzc *ubuf = uarg_to_msgzc(ubuf_base); 394 struct vhost_net_ubuf_ref *ubufs = ubuf->ctx; 395 struct vhost_virtqueue *vq = ubufs->vq; 396 int cnt; 397 398 rcu_read_lock_bh(); 399 400 /* set len to mark this desc buffers done DMA */ 401 vq->heads[ubuf->desc].len = success ? 402 VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN; 403 cnt = vhost_net_ubuf_put(ubufs); 404 405 /* 406 * Trigger polling thread if guest stopped submitting new buffers: 407 * in this case, the refcount after decrement will eventually reach 1. 408 * We also trigger polling periodically after each 16 packets 409 * (the value 16 here is more or less arbitrary, it's tuned to trigger 410 * less than 10% of times). 411 */ 412 if (cnt <= 1 || !(cnt % 16)) 413 vhost_poll_queue(&vq->poll); 414 415 rcu_read_unlock_bh(); 416 } 417 418 static const struct ubuf_info_ops vhost_ubuf_ops = { 419 .complete = vhost_zerocopy_complete, 420 }; 421 422 static inline unsigned long busy_clock(void) 423 { 424 return local_clock() >> 10; 425 } 426 427 static bool vhost_can_busy_poll(unsigned long endtime) 428 { 429 return likely(!need_resched() && !time_after(busy_clock(), endtime) && 430 !signal_pending(current)); 431 } 432 433 static void vhost_net_disable_vq(struct vhost_net *n, 434 struct vhost_virtqueue *vq) 435 { 436 struct vhost_net_virtqueue *nvq = 437 container_of(vq, struct vhost_net_virtqueue, vq); 438 struct vhost_poll *poll = n->poll + (nvq - n->vqs); 439 if (!vhost_vq_get_backend(vq)) 440 return; 441 vhost_poll_stop(poll); 442 } 443 444 static int vhost_net_enable_vq(struct vhost_net *n, 445 struct vhost_virtqueue *vq) 446 { 447 struct vhost_net_virtqueue *nvq = 448 container_of(vq, struct vhost_net_virtqueue, vq); 449 struct vhost_poll *poll = n->poll + (nvq - n->vqs); 450 struct socket *sock; 451 452 sock = vhost_vq_get_backend(vq); 453 if (!sock) 454 return 0; 455 456 return vhost_poll_start(poll, sock->file); 457 } 458 459 static void vhost_net_signal_used(struct vhost_net_virtqueue *nvq, 460 unsigned int count) 461 { 462 struct vhost_virtqueue *vq = &nvq->vq; 463 struct vhost_dev *dev = vq->dev; 464 465 if (!nvq->done_idx) 466 return; 467 468 vhost_add_used_and_signal_n(dev, vq, vq->heads, 469 vq->nheads, count); 470 nvq->done_idx = 0; 471 } 472 473 static void vhost_tx_batch(struct vhost_net *net, 474 struct vhost_net_virtqueue *nvq, 475 struct socket *sock, 476 struct msghdr *msghdr) 477 { 478 struct vhost_virtqueue *vq = &nvq->vq; 479 bool in_order = vhost_has_feature(vq, VIRTIO_F_IN_ORDER); 480 struct tun_msg_ctl ctl = { 481 .type = TUN_MSG_PTR, 482 .num = nvq->batched_xdp, 483 .ptr = nvq->xdp, 484 }; 485 int i, err; 486 487 if (in_order) { 488 vq->heads[0].len = 0; 489 vq->nheads[0] = nvq->done_idx; 490 } 491 492 if (nvq->batched_xdp == 0) 493 goto signal_used; 494 495 msghdr->msg_control = &ctl; 496 msghdr->msg_controllen = sizeof(ctl); 497 err = sock->ops->sendmsg(sock, msghdr, 0); 498 if (unlikely(err < 0)) { 499 vq_err(&nvq->vq, "Fail to batch sending packets\n"); 500 501 /* free pages owned by XDP; since this is an unlikely error path, 502 * keep it simple and avoid more complex bulk update for the 503 * used pages 504 */ 505 for (i = 0; i < nvq->batched_xdp; ++i) 506 put_page(virt_to_head_page(nvq->xdp[i].data)); 507 nvq->batched_xdp = 0; 508 nvq->done_idx = 0; 509 return; 510 } 511 512 signal_used: 513 vhost_net_signal_used(nvq, in_order ? 1 : nvq->done_idx); 514 nvq->batched_xdp = 0; 515 } 516 517 static int sock_has_rx_data(struct socket *sock) 518 { 519 if (unlikely(!sock)) 520 return 0; 521 522 if (sock->ops->peek_len) 523 return sock->ops->peek_len(sock); 524 525 return skb_queue_empty(&sock->sk->sk_receive_queue); 526 } 527 528 static void vhost_net_busy_poll_try_queue(struct vhost_net *net, 529 struct vhost_virtqueue *vq) 530 { 531 if (!vhost_vq_avail_empty(&net->dev, vq)) { 532 vhost_poll_queue(&vq->poll); 533 } else if (unlikely(vhost_enable_notify(&net->dev, vq))) { 534 vhost_disable_notify(&net->dev, vq); 535 vhost_poll_queue(&vq->poll); 536 } 537 } 538 539 static void vhost_net_busy_poll(struct vhost_net *net, 540 struct vhost_virtqueue *rvq, 541 struct vhost_virtqueue *tvq, 542 bool *busyloop_intr, 543 bool poll_rx) 544 { 545 unsigned long busyloop_timeout; 546 unsigned long endtime; 547 struct socket *sock; 548 struct vhost_virtqueue *vq = poll_rx ? tvq : rvq; 549 550 /* Try to hold the vq mutex of the paired virtqueue. We can't 551 * use mutex_lock() here since we could not guarantee a 552 * consistenet lock ordering. 553 */ 554 if (!mutex_trylock(&vq->mutex)) 555 return; 556 557 vhost_disable_notify(&net->dev, vq); 558 sock = vhost_vq_get_backend(rvq); 559 560 busyloop_timeout = poll_rx ? rvq->busyloop_timeout: 561 tvq->busyloop_timeout; 562 563 preempt_disable(); 564 endtime = busy_clock() + busyloop_timeout; 565 566 while (vhost_can_busy_poll(endtime)) { 567 if (vhost_vq_has_work(vq)) { 568 *busyloop_intr = true; 569 break; 570 } 571 572 if ((sock_has_rx_data(sock) && 573 !vhost_vq_avail_empty(&net->dev, rvq)) || 574 !vhost_vq_avail_empty(&net->dev, tvq)) 575 break; 576 577 cpu_relax(); 578 } 579 580 preempt_enable(); 581 582 if (poll_rx || sock_has_rx_data(sock)) 583 vhost_net_busy_poll_try_queue(net, vq); 584 else if (!poll_rx) /* On tx here, sock has no rx data. */ 585 vhost_enable_notify(&net->dev, rvq); 586 587 mutex_unlock(&vq->mutex); 588 } 589 590 static int vhost_net_tx_get_vq_desc(struct vhost_net *net, 591 struct vhost_net_virtqueue *tnvq, 592 unsigned int *out_num, unsigned int *in_num, 593 struct msghdr *msghdr, bool *busyloop_intr, 594 unsigned int *ndesc) 595 { 596 struct vhost_net_virtqueue *rnvq = &net->vqs[VHOST_NET_VQ_RX]; 597 struct vhost_virtqueue *rvq = &rnvq->vq; 598 struct vhost_virtqueue *tvq = &tnvq->vq; 599 600 int r = vhost_get_vq_desc_n(tvq, tvq->iov, ARRAY_SIZE(tvq->iov), 601 out_num, in_num, NULL, NULL, ndesc); 602 603 if (r == tvq->num && tvq->busyloop_timeout) { 604 /* Flush batched packets first */ 605 if (!vhost_sock_zcopy(vhost_vq_get_backend(tvq))) 606 vhost_tx_batch(net, tnvq, 607 vhost_vq_get_backend(tvq), 608 msghdr); 609 610 vhost_net_busy_poll(net, rvq, tvq, busyloop_intr, false); 611 612 r = vhost_get_vq_desc_n(tvq, tvq->iov, ARRAY_SIZE(tvq->iov), 613 out_num, in_num, NULL, NULL, ndesc); 614 } 615 616 return r; 617 } 618 619 static bool vhost_exceeds_maxpend(struct vhost_net *net) 620 { 621 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX]; 622 struct vhost_virtqueue *vq = &nvq->vq; 623 624 return (nvq->upend_idx + UIO_MAXIOV - nvq->done_idx) % UIO_MAXIOV > 625 min_t(unsigned int, VHOST_MAX_PEND, vq->num >> 2); 626 } 627 628 static size_t init_iov_iter(struct vhost_virtqueue *vq, struct iov_iter *iter, 629 size_t hdr_size, int out) 630 { 631 /* Skip header. TODO: support TSO. */ 632 size_t len = iov_length(vq->iov, out); 633 634 iov_iter_init(iter, ITER_SOURCE, vq->iov, out, len); 635 iov_iter_advance(iter, hdr_size); 636 637 return iov_iter_count(iter); 638 } 639 640 static int get_tx_bufs(struct vhost_net *net, 641 struct vhost_net_virtqueue *nvq, 642 struct msghdr *msg, 643 unsigned int *out, unsigned int *in, 644 size_t *len, bool *busyloop_intr, 645 unsigned int *ndesc) 646 { 647 struct vhost_virtqueue *vq = &nvq->vq; 648 int ret; 649 650 ret = vhost_net_tx_get_vq_desc(net, nvq, out, in, msg, 651 busyloop_intr, ndesc); 652 653 if (ret < 0 || ret == vq->num) 654 return ret; 655 656 if (*in) { 657 vq_err(vq, "Unexpected descriptor format for TX: out %d, int %d\n", 658 *out, *in); 659 return -EFAULT; 660 } 661 662 /* Sanity check */ 663 *len = init_iov_iter(vq, &msg->msg_iter, nvq->vhost_hlen, *out); 664 if (*len == 0) { 665 vq_err(vq, "Unexpected header len for TX: %zd expected %zd\n", 666 *len, nvq->vhost_hlen); 667 return -EFAULT; 668 } 669 670 return ret; 671 } 672 673 static bool tx_can_batch(struct vhost_virtqueue *vq, size_t total_len) 674 { 675 return total_len < VHOST_NET_WEIGHT && 676 !vhost_vq_avail_empty(vq->dev, vq); 677 } 678 679 #define VHOST_NET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD) 680 681 static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq, 682 struct iov_iter *from) 683 { 684 struct vhost_virtqueue *vq = &nvq->vq; 685 struct vhost_net *net = container_of(vq->dev, struct vhost_net, 686 dev); 687 struct socket *sock = vhost_vq_get_backend(vq); 688 struct virtio_net_hdr *gso; 689 struct xdp_buff *xdp = &nvq->xdp[nvq->batched_xdp]; 690 size_t len = iov_iter_count(from); 691 int headroom = vhost_sock_xdp(sock) ? XDP_PACKET_HEADROOM : 0; 692 int buflen = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 693 int pad = SKB_DATA_ALIGN(VHOST_NET_RX_PAD + headroom + nvq->sock_hlen); 694 int sock_hlen = nvq->sock_hlen; 695 void *buf; 696 int copied; 697 int ret; 698 699 if (unlikely(len < nvq->sock_hlen)) 700 return -EFAULT; 701 702 if (SKB_DATA_ALIGN(len + pad) + 703 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) > PAGE_SIZE) 704 return -ENOSPC; 705 706 buflen += SKB_DATA_ALIGN(len + pad); 707 buf = page_frag_alloc_align(&net->pf_cache, buflen, GFP_KERNEL, 708 SMP_CACHE_BYTES); 709 if (unlikely(!buf)) 710 return -ENOMEM; 711 712 copied = copy_from_iter(buf + pad - sock_hlen, len, from); 713 if (copied != len) { 714 ret = -EFAULT; 715 goto err; 716 } 717 718 gso = buf + pad - sock_hlen; 719 720 if (!sock_hlen) 721 memset(buf, 0, pad); 722 723 if ((gso->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && 724 vhost16_to_cpu(vq, gso->csum_start) + 725 vhost16_to_cpu(vq, gso->csum_offset) + 2 > 726 vhost16_to_cpu(vq, gso->hdr_len)) { 727 gso->hdr_len = cpu_to_vhost16(vq, 728 vhost16_to_cpu(vq, gso->csum_start) + 729 vhost16_to_cpu(vq, gso->csum_offset) + 2); 730 731 if (vhost16_to_cpu(vq, gso->hdr_len) > len) { 732 ret = -EINVAL; 733 goto err; 734 } 735 } 736 737 /* pad contains sock_hlen */ 738 memcpy(buf, buf + pad - sock_hlen, sock_hlen); 739 740 xdp_init_buff(xdp, buflen, NULL); 741 xdp_prepare_buff(xdp, buf, pad, len - sock_hlen, true); 742 743 ++nvq->batched_xdp; 744 745 return 0; 746 747 err: 748 page_frag_free(buf); 749 return ret; 750 } 751 752 static void handle_tx_copy(struct vhost_net *net, struct socket *sock) 753 { 754 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX]; 755 struct vhost_virtqueue *vq = &nvq->vq; 756 unsigned out, in; 757 int head; 758 struct msghdr msg = { 759 .msg_name = NULL, 760 .msg_namelen = 0, 761 .msg_control = NULL, 762 .msg_controllen = 0, 763 .msg_flags = MSG_DONTWAIT, 764 }; 765 size_t len, total_len = 0; 766 int err; 767 int sent_pkts = 0; 768 bool sock_can_batch = (sock->sk->sk_sndbuf == INT_MAX); 769 bool in_order = vhost_has_feature(vq, VIRTIO_F_IN_ORDER); 770 unsigned int ndesc = 0; 771 772 do { 773 bool busyloop_intr = false; 774 775 if (nvq->done_idx == VHOST_NET_BATCH) 776 vhost_tx_batch(net, nvq, sock, &msg); 777 778 head = get_tx_bufs(net, nvq, &msg, &out, &in, &len, 779 &busyloop_intr, &ndesc); 780 /* On error, stop handling until the next kick. */ 781 if (unlikely(head < 0)) 782 break; 783 /* Nothing new? Wait for eventfd to tell us they refilled. */ 784 if (head == vq->num) { 785 /* Flush batched packets to handle pending RX 786 * work (if busyloop_intr is set) and to avoid 787 * unnecessary virtqueue kicks. 788 */ 789 vhost_tx_batch(net, nvq, sock, &msg); 790 if (unlikely(busyloop_intr)) { 791 vhost_poll_queue(&vq->poll); 792 } else if (unlikely(vhost_enable_notify(&net->dev, 793 vq))) { 794 vhost_disable_notify(&net->dev, vq); 795 continue; 796 } 797 break; 798 } 799 800 total_len += len; 801 802 /* For simplicity, TX batching is only enabled if 803 * sndbuf is unlimited. 804 */ 805 if (sock_can_batch) { 806 err = vhost_net_build_xdp(nvq, &msg.msg_iter); 807 if (!err) { 808 goto done; 809 } else if (unlikely(err != -ENOSPC)) { 810 vhost_tx_batch(net, nvq, sock, &msg); 811 vhost_discard_vq_desc(vq, 1, ndesc); 812 vhost_net_enable_vq(net, vq); 813 break; 814 } 815 816 if (nvq->batched_xdp) { 817 /* We can't build XDP buff, go for single 818 * packet path but let's flush batched 819 * packets. 820 */ 821 vhost_tx_batch(net, nvq, sock, &msg); 822 } 823 msg.msg_control = NULL; 824 } else { 825 if (tx_can_batch(vq, total_len)) 826 msg.msg_flags |= MSG_MORE; 827 else 828 msg.msg_flags &= ~MSG_MORE; 829 } 830 831 err = sock->ops->sendmsg(sock, &msg, len); 832 if (unlikely(err < 0)) { 833 if (err == -EAGAIN || err == -ENOMEM || err == -ENOBUFS) { 834 vhost_discard_vq_desc(vq, 1, ndesc); 835 vhost_net_enable_vq(net, vq); 836 break; 837 } 838 pr_debug("Fail to send packet: err %d", err); 839 } else if (unlikely(err != len)) 840 pr_debug("Truncated TX packet: len %d != %zd\n", 841 err, len); 842 done: 843 if (in_order) { 844 vq->heads[0].id = cpu_to_vhost32(vq, head); 845 } else { 846 vq->heads[nvq->done_idx].id = cpu_to_vhost32(vq, head); 847 vq->heads[nvq->done_idx].len = 0; 848 } 849 ++nvq->done_idx; 850 } while (likely(!vhost_exceeds_weight(vq, ++sent_pkts, total_len))); 851 852 vhost_tx_batch(net, nvq, sock, &msg); 853 } 854 855 static void handle_tx_zerocopy(struct vhost_net *net, struct socket *sock) 856 { 857 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX]; 858 struct vhost_virtqueue *vq = &nvq->vq; 859 unsigned out, in; 860 int head; 861 struct msghdr msg = { 862 .msg_name = NULL, 863 .msg_namelen = 0, 864 .msg_control = NULL, 865 .msg_controllen = 0, 866 .msg_flags = MSG_DONTWAIT, 867 }; 868 struct tun_msg_ctl ctl; 869 size_t len, total_len = 0; 870 int err; 871 struct vhost_net_ubuf_ref *ubufs; 872 struct ubuf_info_msgzc *ubuf; 873 unsigned int ndesc = 0; 874 bool zcopy_used; 875 int sent_pkts = 0; 876 877 do { 878 bool busyloop_intr; 879 880 /* Release DMAs done buffers first */ 881 vhost_zerocopy_signal_used(net, vq); 882 883 busyloop_intr = false; 884 head = get_tx_bufs(net, nvq, &msg, &out, &in, &len, 885 &busyloop_intr, &ndesc); 886 /* On error, stop handling until the next kick. */ 887 if (unlikely(head < 0)) 888 break; 889 /* Nothing new? Wait for eventfd to tell us they refilled. */ 890 if (head == vq->num) { 891 if (unlikely(busyloop_intr)) { 892 vhost_poll_queue(&vq->poll); 893 } else if (unlikely(vhost_enable_notify(&net->dev, vq))) { 894 vhost_disable_notify(&net->dev, vq); 895 continue; 896 } 897 break; 898 } 899 900 zcopy_used = len >= VHOST_GOODCOPY_LEN 901 && !vhost_exceeds_maxpend(net) 902 && vhost_net_tx_select_zcopy(net); 903 904 /* use msg_control to pass vhost zerocopy ubuf info to skb */ 905 if (zcopy_used) { 906 ubuf = nvq->ubuf_info + nvq->upend_idx; 907 vq->heads[nvq->upend_idx].id = cpu_to_vhost32(vq, head); 908 vq->heads[nvq->upend_idx].len = VHOST_DMA_IN_PROGRESS; 909 ubuf->ctx = nvq->ubufs; 910 ubuf->desc = nvq->upend_idx; 911 ubuf->ubuf.ops = &vhost_ubuf_ops; 912 ubuf->ubuf.flags = SKBFL_ZEROCOPY_FRAG; 913 refcount_set(&ubuf->ubuf.refcnt, 1); 914 msg.msg_control = &ctl; 915 ctl.type = TUN_MSG_UBUF; 916 ctl.ptr = &ubuf->ubuf; 917 msg.msg_controllen = sizeof(ctl); 918 ubufs = nvq->ubufs; 919 atomic_inc(&ubufs->refcount); 920 nvq->upend_idx = (nvq->upend_idx + 1) % UIO_MAXIOV; 921 } else { 922 msg.msg_control = NULL; 923 ubufs = NULL; 924 } 925 total_len += len; 926 if (tx_can_batch(vq, total_len) && 927 likely(!vhost_exceeds_maxpend(net))) { 928 msg.msg_flags |= MSG_MORE; 929 } else { 930 msg.msg_flags &= ~MSG_MORE; 931 } 932 933 err = sock->ops->sendmsg(sock, &msg, len); 934 if (unlikely(err < 0)) { 935 bool retry = err == -EAGAIN || err == -ENOMEM || err == -ENOBUFS; 936 937 if (zcopy_used) { 938 if (vq->heads[ubuf->desc].len == VHOST_DMA_IN_PROGRESS) 939 vhost_net_ubuf_put(ubufs); 940 if (retry) 941 nvq->upend_idx = ((unsigned)nvq->upend_idx - 1) 942 % UIO_MAXIOV; 943 else 944 vq->heads[ubuf->desc].len = VHOST_DMA_DONE_LEN; 945 } 946 if (retry) { 947 vhost_discard_vq_desc(vq, 1, ndesc); 948 vhost_net_enable_vq(net, vq); 949 break; 950 } 951 pr_debug("Fail to send packet: err %d", err); 952 } else if (unlikely(err != len)) 953 pr_debug("Truncated TX packet: " 954 " len %d != %zd\n", err, len); 955 if (!zcopy_used) 956 vhost_add_used_and_signal(&net->dev, vq, head, 0); 957 else 958 vhost_zerocopy_signal_used(net, vq); 959 vhost_net_tx_packet(net); 960 } while (likely(!vhost_exceeds_weight(vq, ++sent_pkts, total_len))); 961 } 962 963 /* Expects to be always run from workqueue - which acts as 964 * read-size critical section for our kind of RCU. */ 965 static void handle_tx(struct vhost_net *net) 966 { 967 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX]; 968 struct vhost_virtqueue *vq = &nvq->vq; 969 struct socket *sock; 970 971 mutex_lock_nested(&vq->mutex, VHOST_NET_VQ_TX); 972 sock = vhost_vq_get_backend(vq); 973 if (!sock) 974 goto out; 975 976 if (!vq_meta_prefetch(vq)) 977 goto out; 978 979 vhost_disable_notify(&net->dev, vq); 980 vhost_net_disable_vq(net, vq); 981 982 if (vhost_sock_zcopy(sock)) 983 handle_tx_zerocopy(net, sock); 984 else 985 handle_tx_copy(net, sock); 986 987 out: 988 mutex_unlock(&vq->mutex); 989 } 990 991 static int peek_head_len(struct vhost_net_virtqueue *rvq, struct sock *sk) 992 { 993 struct sk_buff *head; 994 int len = 0; 995 unsigned long flags; 996 997 if (rvq->rx_ring) 998 return vhost_net_buf_peek(rvq); 999 1000 spin_lock_irqsave(&sk->sk_receive_queue.lock, flags); 1001 head = skb_peek(&sk->sk_receive_queue); 1002 if (likely(head)) { 1003 len = head->len; 1004 if (skb_vlan_tag_present(head)) 1005 len += VLAN_HLEN; 1006 } 1007 1008 spin_unlock_irqrestore(&sk->sk_receive_queue.lock, flags); 1009 return len; 1010 } 1011 1012 static int vhost_net_rx_peek_head_len(struct vhost_net *net, struct sock *sk, 1013 bool *busyloop_intr, unsigned int *count) 1014 { 1015 struct vhost_net_virtqueue *rnvq = &net->vqs[VHOST_NET_VQ_RX]; 1016 struct vhost_net_virtqueue *tnvq = &net->vqs[VHOST_NET_VQ_TX]; 1017 struct vhost_virtqueue *rvq = &rnvq->vq; 1018 struct vhost_virtqueue *tvq = &tnvq->vq; 1019 int len = peek_head_len(rnvq, sk); 1020 1021 if (!len && rvq->busyloop_timeout) { 1022 /* Flush batched heads first */ 1023 vhost_net_signal_used(rnvq, *count); 1024 *count = 0; 1025 /* Both tx vq and rx socket were polled here */ 1026 vhost_net_busy_poll(net, rvq, tvq, busyloop_intr, true); 1027 1028 len = peek_head_len(rnvq, sk); 1029 } 1030 1031 return len; 1032 } 1033 1034 /* This is a multi-buffer version of vhost_get_desc, that works if 1035 * vq has read descriptors only. 1036 * @nvq - the relevant vhost_net virtqueue 1037 * @datalen - data length we'll be reading 1038 * @iovcount - returned count of io vectors we fill 1039 * @log - vhost log 1040 * @log_num - log offset 1041 * @quota - headcount quota, 1 for big buffer 1042 * returns number of buffer heads allocated, negative on error 1043 */ 1044 static int get_rx_bufs(struct vhost_net_virtqueue *nvq, 1045 struct vring_used_elem *heads, 1046 u16 *nheads, 1047 int datalen, 1048 unsigned *iovcount, 1049 struct vhost_log *log, 1050 unsigned *log_num, 1051 unsigned int quota, 1052 unsigned int *ndesc) 1053 { 1054 struct vhost_virtqueue *vq = &nvq->vq; 1055 bool in_order = vhost_has_feature(vq, VIRTIO_F_IN_ORDER); 1056 unsigned int out, in, desc_num, n = 0; 1057 int seg = 0; 1058 int headcount = 0; 1059 unsigned d; 1060 int r, nlogs = 0; 1061 /* len is always initialized before use since we are always called with 1062 * datalen > 0. 1063 */ 1064 u32 len; 1065 1066 while (datalen > 0 && headcount < quota) { 1067 if (unlikely(seg >= UIO_MAXIOV)) { 1068 r = -ENOBUFS; 1069 goto err; 1070 } 1071 r = vhost_get_vq_desc_n(vq, vq->iov + seg, 1072 ARRAY_SIZE(vq->iov) - seg, &out, 1073 &in, log, log_num, &desc_num); 1074 if (unlikely(r < 0)) 1075 goto err; 1076 1077 d = r; 1078 if (d == vq->num) { 1079 r = 0; 1080 goto err; 1081 } 1082 if (unlikely(out || in <= 0)) { 1083 vq_err(vq, "unexpected descriptor format for RX: " 1084 "out %d, in %d\n", out, in); 1085 r = -EINVAL; 1086 goto err; 1087 } 1088 if (unlikely(log)) { 1089 nlogs += *log_num; 1090 log += *log_num; 1091 } 1092 len = iov_length(vq->iov + seg, in); 1093 if (!in_order) { 1094 heads[headcount].id = cpu_to_vhost32(vq, d); 1095 heads[headcount].len = cpu_to_vhost32(vq, len); 1096 } 1097 ++headcount; 1098 datalen -= len; 1099 seg += in; 1100 n += desc_num; 1101 } 1102 1103 *iovcount = seg; 1104 if (unlikely(log)) 1105 *log_num = nlogs; 1106 1107 /* Detect overrun */ 1108 if (unlikely(datalen > 0)) { 1109 r = UIO_MAXIOV + 1; 1110 goto err; 1111 } 1112 1113 if (!in_order) 1114 heads[headcount - 1].len = cpu_to_vhost32(vq, len + datalen); 1115 else { 1116 heads[0].len = cpu_to_vhost32(vq, len + datalen); 1117 heads[0].id = cpu_to_vhost32(vq, d); 1118 nheads[0] = headcount; 1119 } 1120 1121 *ndesc = n; 1122 1123 return headcount; 1124 err: 1125 vhost_discard_vq_desc(vq, headcount, n); 1126 return r; 1127 } 1128 1129 /* Expects to be always run from workqueue - which acts as 1130 * read-size critical section for our kind of RCU. */ 1131 static void handle_rx(struct vhost_net *net) 1132 { 1133 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_RX]; 1134 struct vhost_virtqueue *vq = &nvq->vq; 1135 bool in_order = vhost_has_feature(vq, VIRTIO_F_IN_ORDER); 1136 unsigned int count = 0; 1137 unsigned in, log; 1138 struct vhost_log *vq_log; 1139 struct msghdr msg = { 1140 .msg_name = NULL, 1141 .msg_namelen = 0, 1142 .msg_control = NULL, /* FIXME: get and handle RX aux data. */ 1143 .msg_controllen = 0, 1144 .msg_flags = MSG_DONTWAIT, 1145 }; 1146 struct virtio_net_hdr hdr = { 1147 .flags = 0, 1148 .gso_type = VIRTIO_NET_HDR_GSO_NONE 1149 }; 1150 size_t total_len = 0; 1151 int err, mergeable; 1152 s16 headcount; 1153 size_t vhost_hlen, sock_hlen; 1154 size_t vhost_len, sock_len; 1155 bool busyloop_intr = false; 1156 bool set_num_buffers; 1157 struct socket *sock; 1158 struct iov_iter fixup; 1159 __virtio16 num_buffers; 1160 int recv_pkts = 0; 1161 unsigned int ndesc; 1162 1163 mutex_lock_nested(&vq->mutex, VHOST_NET_VQ_RX); 1164 sock = vhost_vq_get_backend(vq); 1165 if (!sock) 1166 goto out; 1167 1168 if (!vq_meta_prefetch(vq)) 1169 goto out; 1170 1171 vhost_disable_notify(&net->dev, vq); 1172 vhost_net_disable_vq(net, vq); 1173 1174 vhost_hlen = nvq->vhost_hlen; 1175 sock_hlen = nvq->sock_hlen; 1176 1177 vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ? 1178 vq->log : NULL; 1179 mergeable = vhost_has_feature(vq, VIRTIO_NET_F_MRG_RXBUF); 1180 set_num_buffers = mergeable || 1181 vhost_has_feature(vq, VIRTIO_F_VERSION_1); 1182 1183 do { 1184 sock_len = vhost_net_rx_peek_head_len(net, sock->sk, 1185 &busyloop_intr, &count); 1186 if (!sock_len) 1187 break; 1188 sock_len += sock_hlen; 1189 vhost_len = sock_len + vhost_hlen; 1190 headcount = get_rx_bufs(nvq, vq->heads + count, 1191 vq->nheads + count, 1192 vhost_len, &in, vq_log, &log, 1193 likely(mergeable) ? UIO_MAXIOV : 1, 1194 &ndesc); 1195 /* On error, stop handling until the next kick. */ 1196 if (unlikely(headcount < 0)) 1197 goto out; 1198 /* OK, now we need to know about added descriptors. */ 1199 if (!headcount) { 1200 if (unlikely(busyloop_intr)) { 1201 vhost_poll_queue(&vq->poll); 1202 } else if (unlikely(vhost_enable_notify(&net->dev, vq))) { 1203 /* They have slipped one in as we were 1204 * doing that: check again. */ 1205 vhost_disable_notify(&net->dev, vq); 1206 continue; 1207 } 1208 /* Nothing new? Wait for eventfd to tell us 1209 * they refilled. */ 1210 goto out; 1211 } 1212 busyloop_intr = false; 1213 if (nvq->rx_ring) 1214 msg.msg_control = vhost_net_buf_consume(&nvq->rxq); 1215 /* On overrun, truncate and discard */ 1216 if (unlikely(headcount > UIO_MAXIOV)) { 1217 iov_iter_init(&msg.msg_iter, ITER_DEST, vq->iov, 1, 1); 1218 err = sock->ops->recvmsg(sock, &msg, 1219 1, MSG_DONTWAIT | MSG_TRUNC); 1220 pr_debug("Discarded rx packet: len %zd\n", sock_len); 1221 continue; 1222 } 1223 /* We don't need to be notified again. */ 1224 iov_iter_init(&msg.msg_iter, ITER_DEST, vq->iov, in, vhost_len); 1225 fixup = msg.msg_iter; 1226 if (unlikely((vhost_hlen))) { 1227 /* We will supply the header ourselves 1228 * TODO: support TSO. 1229 */ 1230 iov_iter_advance(&msg.msg_iter, vhost_hlen); 1231 } 1232 err = sock->ops->recvmsg(sock, &msg, 1233 sock_len, MSG_DONTWAIT | MSG_TRUNC); 1234 /* Userspace might have consumed the packet meanwhile: 1235 * it's not supposed to do this usually, but might be hard 1236 * to prevent. Discard data we got (if any) and keep going. */ 1237 if (unlikely(err != sock_len)) { 1238 pr_debug("Discarded rx packet: " 1239 " len %d, expected %zd\n", err, sock_len); 1240 vhost_discard_vq_desc(vq, headcount, ndesc); 1241 continue; 1242 } 1243 /* Supply virtio_net_hdr if VHOST_NET_F_VIRTIO_NET_HDR */ 1244 if (unlikely(vhost_hlen)) { 1245 if (copy_to_iter(&hdr, sizeof(hdr), 1246 &fixup) != sizeof(hdr)) { 1247 vq_err(vq, "Unable to write vnet_hdr " 1248 "at addr %p\n", vq->iov->iov_base); 1249 goto out; 1250 } 1251 } else { 1252 /* Header came from socket; we'll need to patch 1253 * ->num_buffers over if VIRTIO_NET_F_MRG_RXBUF 1254 */ 1255 iov_iter_advance(&fixup, sizeof(hdr)); 1256 } 1257 /* TODO: Should check and handle checksum. */ 1258 1259 num_buffers = cpu_to_vhost16(vq, headcount); 1260 if (likely(set_num_buffers) && 1261 copy_to_iter(&num_buffers, sizeof num_buffers, 1262 &fixup) != sizeof num_buffers) { 1263 vq_err(vq, "Failed num_buffers write"); 1264 vhost_discard_vq_desc(vq, headcount, ndesc); 1265 goto out; 1266 } 1267 nvq->done_idx += headcount; 1268 count += in_order ? 1 : headcount; 1269 if (nvq->done_idx > VHOST_NET_BATCH) { 1270 vhost_net_signal_used(nvq, count); 1271 count = 0; 1272 } 1273 if (unlikely(vq_log)) 1274 vhost_log_write(vq, vq_log, log, vhost_len, 1275 vq->iov, in); 1276 total_len += vhost_len; 1277 } while (likely(!vhost_exceeds_weight(vq, ++recv_pkts, total_len))); 1278 1279 if (unlikely(busyloop_intr)) 1280 vhost_poll_queue(&vq->poll); 1281 else if (!sock_len) 1282 vhost_net_enable_vq(net, vq); 1283 out: 1284 vhost_net_signal_used(nvq, count); 1285 mutex_unlock(&vq->mutex); 1286 } 1287 1288 static void handle_tx_kick(struct vhost_work *work) 1289 { 1290 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue, 1291 poll.work); 1292 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev); 1293 1294 handle_tx(net); 1295 } 1296 1297 static void handle_rx_kick(struct vhost_work *work) 1298 { 1299 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue, 1300 poll.work); 1301 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev); 1302 1303 handle_rx(net); 1304 } 1305 1306 static void handle_tx_net(struct vhost_work *work) 1307 { 1308 struct vhost_net *net = container_of(work, struct vhost_net, 1309 poll[VHOST_NET_VQ_TX].work); 1310 handle_tx(net); 1311 } 1312 1313 static void handle_rx_net(struct vhost_work *work) 1314 { 1315 struct vhost_net *net = container_of(work, struct vhost_net, 1316 poll[VHOST_NET_VQ_RX].work); 1317 handle_rx(net); 1318 } 1319 1320 static int vhost_net_open(struct inode *inode, struct file *f) 1321 { 1322 struct vhost_net *n; 1323 struct vhost_dev *dev; 1324 struct vhost_virtqueue **vqs; 1325 void **queue; 1326 struct xdp_buff *xdp; 1327 int i; 1328 1329 n = kvmalloc_obj(*n, GFP_KERNEL | __GFP_RETRY_MAYFAIL); 1330 if (!n) 1331 return -ENOMEM; 1332 vqs = kmalloc_objs(*vqs, VHOST_NET_VQ_MAX); 1333 if (!vqs) { 1334 kvfree(n); 1335 return -ENOMEM; 1336 } 1337 1338 queue = kmalloc_array(VHOST_NET_BATCH, sizeof(void *), 1339 GFP_KERNEL); 1340 if (!queue) { 1341 kfree(vqs); 1342 kvfree(n); 1343 return -ENOMEM; 1344 } 1345 n->vqs[VHOST_NET_VQ_RX].rxq.queue = queue; 1346 1347 xdp = kmalloc_objs(*xdp, VHOST_NET_BATCH); 1348 if (!xdp) { 1349 kfree(vqs); 1350 kvfree(n); 1351 kfree(queue); 1352 return -ENOMEM; 1353 } 1354 n->vqs[VHOST_NET_VQ_TX].xdp = xdp; 1355 1356 dev = &n->dev; 1357 vqs[VHOST_NET_VQ_TX] = &n->vqs[VHOST_NET_VQ_TX].vq; 1358 vqs[VHOST_NET_VQ_RX] = &n->vqs[VHOST_NET_VQ_RX].vq; 1359 n->vqs[VHOST_NET_VQ_TX].vq.handle_kick = handle_tx_kick; 1360 n->vqs[VHOST_NET_VQ_RX].vq.handle_kick = handle_rx_kick; 1361 for (i = 0; i < VHOST_NET_VQ_MAX; i++) { 1362 n->vqs[i].ubufs = NULL; 1363 n->vqs[i].ubuf_info = NULL; 1364 n->vqs[i].upend_idx = 0; 1365 n->vqs[i].done_idx = 0; 1366 n->vqs[i].batched_xdp = 0; 1367 n->vqs[i].vhost_hlen = 0; 1368 n->vqs[i].sock_hlen = 0; 1369 n->vqs[i].rx_ring = NULL; 1370 vhost_net_buf_init(&n->vqs[i].rxq); 1371 } 1372 vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX, 1373 UIO_MAXIOV + VHOST_NET_BATCH, 1374 VHOST_NET_PKT_WEIGHT, VHOST_NET_WEIGHT, true, 1375 NULL); 1376 1377 vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, EPOLLOUT, dev, 1378 vqs[VHOST_NET_VQ_TX]); 1379 vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, EPOLLIN, dev, 1380 vqs[VHOST_NET_VQ_RX]); 1381 1382 f->private_data = n; 1383 page_frag_cache_init(&n->pf_cache); 1384 1385 return 0; 1386 } 1387 1388 static struct socket *vhost_net_stop_vq(struct vhost_net *n, 1389 struct vhost_virtqueue *vq) 1390 { 1391 struct socket *sock; 1392 struct vhost_net_virtqueue *nvq = 1393 container_of(vq, struct vhost_net_virtqueue, vq); 1394 1395 mutex_lock(&vq->mutex); 1396 sock = vhost_vq_get_backend(vq); 1397 vhost_net_disable_vq(n, vq); 1398 vhost_vq_set_backend(vq, NULL); 1399 vhost_net_buf_unproduce(nvq); 1400 nvq->rx_ring = NULL; 1401 mutex_unlock(&vq->mutex); 1402 return sock; 1403 } 1404 1405 static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock, 1406 struct socket **rx_sock) 1407 { 1408 *tx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_TX].vq); 1409 *rx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_RX].vq); 1410 } 1411 1412 static void vhost_net_flush(struct vhost_net *n) 1413 { 1414 vhost_dev_flush(&n->dev); 1415 if (n->vqs[VHOST_NET_VQ_TX].ubufs) { 1416 mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex); 1417 n->tx_flush = true; 1418 mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex); 1419 /* Wait for all lower device DMAs done. */ 1420 vhost_net_ubuf_put_and_wait(n->vqs[VHOST_NET_VQ_TX].ubufs); 1421 mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex); 1422 n->tx_flush = false; 1423 atomic_set(&n->vqs[VHOST_NET_VQ_TX].ubufs->refcount, 1); 1424 mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex); 1425 } 1426 } 1427 1428 static int vhost_net_release(struct inode *inode, struct file *f) 1429 { 1430 struct vhost_net *n = f->private_data; 1431 struct socket *tx_sock; 1432 struct socket *rx_sock; 1433 1434 vhost_net_stop(n, &tx_sock, &rx_sock); 1435 vhost_net_flush(n); 1436 vhost_dev_stop(&n->dev); 1437 vhost_dev_cleanup(&n->dev); 1438 vhost_net_vq_reset(n); 1439 if (tx_sock) 1440 sockfd_put(tx_sock); 1441 if (rx_sock) 1442 sockfd_put(rx_sock); 1443 /* Make sure no callbacks are outstanding */ 1444 synchronize_rcu(); 1445 /* We do an extra flush before freeing memory, 1446 * since jobs can re-queue themselves. */ 1447 vhost_net_flush(n); 1448 kfree(n->vqs[VHOST_NET_VQ_RX].rxq.queue); 1449 kfree(n->vqs[VHOST_NET_VQ_TX].xdp); 1450 kfree(n->dev.vqs); 1451 page_frag_cache_drain(&n->pf_cache); 1452 kvfree(n); 1453 return 0; 1454 } 1455 1456 static struct socket *get_raw_socket(int fd) 1457 { 1458 int r; 1459 struct socket *sock = sockfd_lookup(fd, &r); 1460 1461 if (!sock) 1462 return ERR_PTR(-ENOTSOCK); 1463 1464 /* Parameter checking */ 1465 if (sock->sk->sk_type != SOCK_RAW) { 1466 r = -ESOCKTNOSUPPORT; 1467 goto err; 1468 } 1469 1470 if (sock->sk->sk_family != AF_PACKET) { 1471 r = -EPFNOSUPPORT; 1472 goto err; 1473 } 1474 return sock; 1475 err: 1476 sockfd_put(sock); 1477 return ERR_PTR(r); 1478 } 1479 1480 static struct ptr_ring *get_tap_ptr_ring(struct file *file) 1481 { 1482 struct ptr_ring *ring; 1483 ring = tun_get_tx_ring(file); 1484 if (!IS_ERR(ring)) 1485 goto out; 1486 ring = tap_get_ptr_ring(file); 1487 if (!IS_ERR(ring)) 1488 goto out; 1489 ring = NULL; 1490 out: 1491 return ring; 1492 } 1493 1494 static struct socket *get_tap_socket(int fd) 1495 { 1496 struct file *file = fget(fd); 1497 struct socket *sock; 1498 1499 if (!file) 1500 return ERR_PTR(-EBADF); 1501 sock = tun_get_socket(file); 1502 if (!IS_ERR(sock)) 1503 return sock; 1504 sock = tap_get_socket(file); 1505 if (IS_ERR(sock)) 1506 fput(file); 1507 return sock; 1508 } 1509 1510 static struct socket *get_socket(int fd) 1511 { 1512 struct socket *sock; 1513 1514 /* special case to disable backend */ 1515 if (fd == -1) 1516 return NULL; 1517 sock = get_raw_socket(fd); 1518 if (!IS_ERR(sock)) 1519 return sock; 1520 sock = get_tap_socket(fd); 1521 if (!IS_ERR(sock)) 1522 return sock; 1523 return ERR_PTR(-ENOTSOCK); 1524 } 1525 1526 static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd) 1527 { 1528 struct socket *sock, *oldsock; 1529 struct vhost_virtqueue *vq; 1530 struct vhost_net_virtqueue *nvq; 1531 struct vhost_net_ubuf_ref *ubufs, *oldubufs = NULL; 1532 int r; 1533 1534 mutex_lock(&n->dev.mutex); 1535 r = vhost_dev_check_owner(&n->dev); 1536 if (r) 1537 goto err; 1538 1539 if (index >= VHOST_NET_VQ_MAX) { 1540 r = -ENOBUFS; 1541 goto err; 1542 } 1543 vq = &n->vqs[index].vq; 1544 nvq = &n->vqs[index]; 1545 mutex_lock(&vq->mutex); 1546 1547 if (fd == -1) 1548 vhost_clear_msg(&n->dev); 1549 1550 /* Verify that ring has been setup correctly. */ 1551 if (!vhost_vq_access_ok(vq)) { 1552 r = -EFAULT; 1553 goto err_vq; 1554 } 1555 sock = get_socket(fd); 1556 if (IS_ERR(sock)) { 1557 r = PTR_ERR(sock); 1558 goto err_vq; 1559 } 1560 1561 /* start polling new socket */ 1562 oldsock = vhost_vq_get_backend(vq); 1563 if (sock != oldsock) { 1564 ubufs = vhost_net_ubuf_alloc(vq, 1565 sock && vhost_sock_zcopy(sock)); 1566 if (IS_ERR(ubufs)) { 1567 r = PTR_ERR(ubufs); 1568 goto err_ubufs; 1569 } 1570 1571 vhost_net_disable_vq(n, vq); 1572 vhost_vq_set_backend(vq, sock); 1573 vhost_net_buf_unproduce(nvq); 1574 r = vhost_vq_init_access(vq); 1575 if (r) 1576 goto err_used; 1577 r = vhost_net_enable_vq(n, vq); 1578 if (r) 1579 goto err_used; 1580 if (index == VHOST_NET_VQ_RX) { 1581 if (sock) 1582 nvq->rx_ring = get_tap_ptr_ring(sock->file); 1583 else 1584 nvq->rx_ring = NULL; 1585 } 1586 1587 oldubufs = nvq->ubufs; 1588 nvq->ubufs = ubufs; 1589 1590 n->tx_packets = 0; 1591 n->tx_zcopy_err = 0; 1592 n->tx_flush = false; 1593 } 1594 1595 mutex_unlock(&vq->mutex); 1596 1597 if (oldubufs) { 1598 vhost_net_ubuf_put_wait_and_free(oldubufs); 1599 mutex_lock(&vq->mutex); 1600 vhost_zerocopy_signal_used(n, vq); 1601 mutex_unlock(&vq->mutex); 1602 } 1603 1604 if (oldsock) { 1605 vhost_dev_flush(&n->dev); 1606 sockfd_put(oldsock); 1607 } 1608 1609 mutex_unlock(&n->dev.mutex); 1610 return 0; 1611 1612 err_used: 1613 vhost_vq_set_backend(vq, oldsock); 1614 vhost_net_enable_vq(n, vq); 1615 if (ubufs) 1616 vhost_net_ubuf_put_wait_and_free(ubufs); 1617 err_ubufs: 1618 if (sock) 1619 sockfd_put(sock); 1620 err_vq: 1621 mutex_unlock(&vq->mutex); 1622 err: 1623 mutex_unlock(&n->dev.mutex); 1624 return r; 1625 } 1626 1627 static long vhost_net_reset_owner(struct vhost_net *n) 1628 { 1629 struct socket *tx_sock = NULL; 1630 struct socket *rx_sock = NULL; 1631 long err; 1632 struct vhost_iotlb *umem; 1633 1634 mutex_lock(&n->dev.mutex); 1635 err = vhost_dev_check_owner(&n->dev); 1636 if (err) 1637 goto done; 1638 umem = vhost_dev_reset_owner_prepare(); 1639 if (!umem) { 1640 err = -ENOMEM; 1641 goto done; 1642 } 1643 vhost_net_stop(n, &tx_sock, &rx_sock); 1644 vhost_net_flush(n); 1645 vhost_dev_stop(&n->dev); 1646 vhost_dev_reset_owner(&n->dev, umem); 1647 vhost_net_vq_reset(n); 1648 done: 1649 mutex_unlock(&n->dev.mutex); 1650 if (tx_sock) 1651 sockfd_put(tx_sock); 1652 if (rx_sock) 1653 sockfd_put(rx_sock); 1654 return err; 1655 } 1656 1657 static int vhost_net_set_features(struct vhost_net *n, const u64 *features) 1658 { 1659 size_t vhost_hlen, sock_hlen, hdr_len; 1660 int i; 1661 1662 hdr_len = virtio_features_test_bit(features, VIRTIO_NET_F_MRG_RXBUF) || 1663 virtio_features_test_bit(features, VIRTIO_F_VERSION_1) ? 1664 sizeof(struct virtio_net_hdr_mrg_rxbuf) : 1665 sizeof(struct virtio_net_hdr); 1666 1667 if (virtio_features_test_bit(features, 1668 VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO) || 1669 virtio_features_test_bit(features, 1670 VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO)) 1671 hdr_len = sizeof(struct virtio_net_hdr_v1_hash_tunnel); 1672 1673 if (virtio_features_test_bit(features, VHOST_NET_F_VIRTIO_NET_HDR)) { 1674 /* vhost provides vnet_hdr */ 1675 vhost_hlen = hdr_len; 1676 sock_hlen = 0; 1677 } else { 1678 /* socket provides vnet_hdr */ 1679 vhost_hlen = 0; 1680 sock_hlen = hdr_len; 1681 } 1682 mutex_lock(&n->dev.mutex); 1683 if (virtio_features_test_bit(features, VHOST_F_LOG_ALL) && 1684 !vhost_log_access_ok(&n->dev)) 1685 goto out_unlock; 1686 1687 if (virtio_features_test_bit(features, VIRTIO_F_ACCESS_PLATFORM)) { 1688 if (vhost_init_device_iotlb(&n->dev)) 1689 goto out_unlock; 1690 } 1691 1692 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) { 1693 mutex_lock(&n->vqs[i].vq.mutex); 1694 virtio_features_copy(n->vqs[i].vq.acked_features_array, 1695 features); 1696 n->vqs[i].vhost_hlen = vhost_hlen; 1697 n->vqs[i].sock_hlen = sock_hlen; 1698 mutex_unlock(&n->vqs[i].vq.mutex); 1699 } 1700 mutex_unlock(&n->dev.mutex); 1701 return 0; 1702 1703 out_unlock: 1704 mutex_unlock(&n->dev.mutex); 1705 return -EFAULT; 1706 } 1707 1708 static long vhost_net_set_owner(struct vhost_net *n) 1709 { 1710 int r; 1711 1712 mutex_lock(&n->dev.mutex); 1713 if (vhost_dev_has_owner(&n->dev)) { 1714 r = -EBUSY; 1715 goto out; 1716 } 1717 r = vhost_net_set_ubuf_info(n); 1718 if (r) 1719 goto out; 1720 r = vhost_dev_set_owner(&n->dev); 1721 if (r) 1722 vhost_net_clear_ubuf_info(n); 1723 vhost_net_flush(n); 1724 out: 1725 mutex_unlock(&n->dev.mutex); 1726 return r; 1727 } 1728 1729 static long vhost_net_ioctl(struct file *f, unsigned int ioctl, 1730 unsigned long arg) 1731 { 1732 const DEFINE_VHOST_FEATURES_ARRAY(vhost_net_features, vhost_net_bits); 1733 u64 all_features[VIRTIO_FEATURES_U64S]; 1734 struct vhost_net *n = f->private_data; 1735 void __user *argp = (void __user *)arg; 1736 u64 __user *featurep = argp; 1737 struct vhost_vring_file backend; 1738 u64 features, count, copied; 1739 int r, i; 1740 1741 switch (ioctl) { 1742 case VHOST_NET_SET_BACKEND: 1743 if (copy_from_user(&backend, argp, sizeof backend)) 1744 return -EFAULT; 1745 return vhost_net_set_backend(n, backend.index, backend.fd); 1746 case VHOST_GET_FEATURES: 1747 features = vhost_net_features[0]; 1748 if (copy_to_user(featurep, &features, sizeof features)) 1749 return -EFAULT; 1750 return 0; 1751 case VHOST_SET_FEATURES: 1752 if (copy_from_user(&features, featurep, sizeof features)) 1753 return -EFAULT; 1754 if (features & ~vhost_net_features[0]) 1755 return -EOPNOTSUPP; 1756 1757 virtio_features_from_u64(all_features, features); 1758 return vhost_net_set_features(n, all_features); 1759 case VHOST_GET_FEATURES_ARRAY: 1760 if (copy_from_user(&count, featurep, sizeof(count))) 1761 return -EFAULT; 1762 1763 /* Copy the net features, up to the user-provided buffer size */ 1764 argp += sizeof(u64); 1765 copied = min(count, (u64)VIRTIO_FEATURES_U64S); 1766 if (copy_to_user(argp, vhost_net_features, 1767 copied * sizeof(u64))) 1768 return -EFAULT; 1769 1770 /* Zero the trailing space provided by user-space, if any */ 1771 if (clear_user(argp, size_mul(count - copied, sizeof(u64)))) 1772 return -EFAULT; 1773 return 0; 1774 case VHOST_SET_FEATURES_ARRAY: 1775 if (copy_from_user(&count, featurep, sizeof(count))) 1776 return -EFAULT; 1777 1778 virtio_features_zero(all_features); 1779 argp += sizeof(u64); 1780 copied = min(count, (u64)VIRTIO_FEATURES_U64S); 1781 if (copy_from_user(all_features, argp, copied * sizeof(u64))) 1782 return -EFAULT; 1783 1784 /* 1785 * Any feature specified by user-space above 1786 * VIRTIO_FEATURES_BITS is not supported by definition. 1787 */ 1788 for (i = copied; i < count; ++i) { 1789 if (copy_from_user(&features, featurep + 1 + i, 1790 sizeof(features))) 1791 return -EFAULT; 1792 if (features) 1793 return -EOPNOTSUPP; 1794 } 1795 1796 for (i = 0; i < VIRTIO_FEATURES_U64S; i++) 1797 if (all_features[i] & ~vhost_net_features[i]) 1798 return -EOPNOTSUPP; 1799 1800 return vhost_net_set_features(n, all_features); 1801 case VHOST_GET_BACKEND_FEATURES: 1802 features = VHOST_NET_BACKEND_FEATURES; 1803 if (copy_to_user(featurep, &features, sizeof(features))) 1804 return -EFAULT; 1805 return 0; 1806 case VHOST_SET_BACKEND_FEATURES: 1807 if (copy_from_user(&features, featurep, sizeof(features))) 1808 return -EFAULT; 1809 if (features & ~VHOST_NET_BACKEND_FEATURES) 1810 return -EOPNOTSUPP; 1811 vhost_set_backend_features(&n->dev, features); 1812 return 0; 1813 case VHOST_RESET_OWNER: 1814 return vhost_net_reset_owner(n); 1815 case VHOST_SET_OWNER: 1816 return vhost_net_set_owner(n); 1817 default: 1818 mutex_lock(&n->dev.mutex); 1819 r = vhost_dev_ioctl(&n->dev, ioctl, argp); 1820 if (r == -ENOIOCTLCMD) 1821 r = vhost_vring_ioctl(&n->dev, ioctl, argp); 1822 else 1823 vhost_net_flush(n); 1824 mutex_unlock(&n->dev.mutex); 1825 return r; 1826 } 1827 } 1828 1829 static ssize_t vhost_net_chr_read_iter(struct kiocb *iocb, struct iov_iter *to) 1830 { 1831 struct file *file = iocb->ki_filp; 1832 struct vhost_net *n = file->private_data; 1833 struct vhost_dev *dev = &n->dev; 1834 int noblock = file->f_flags & O_NONBLOCK; 1835 1836 return vhost_chr_read_iter(dev, to, noblock); 1837 } 1838 1839 static ssize_t vhost_net_chr_write_iter(struct kiocb *iocb, 1840 struct iov_iter *from) 1841 { 1842 struct file *file = iocb->ki_filp; 1843 struct vhost_net *n = file->private_data; 1844 struct vhost_dev *dev = &n->dev; 1845 1846 return vhost_chr_write_iter(dev, from); 1847 } 1848 1849 static __poll_t vhost_net_chr_poll(struct file *file, poll_table *wait) 1850 { 1851 struct vhost_net *n = file->private_data; 1852 struct vhost_dev *dev = &n->dev; 1853 1854 return vhost_chr_poll(file, dev, wait); 1855 } 1856 1857 static const struct file_operations vhost_net_fops = { 1858 .owner = THIS_MODULE, 1859 .release = vhost_net_release, 1860 .read_iter = vhost_net_chr_read_iter, 1861 .write_iter = vhost_net_chr_write_iter, 1862 .poll = vhost_net_chr_poll, 1863 .unlocked_ioctl = vhost_net_ioctl, 1864 .compat_ioctl = compat_ptr_ioctl, 1865 .open = vhost_net_open, 1866 .llseek = noop_llseek, 1867 }; 1868 1869 static struct miscdevice vhost_net_misc = { 1870 .minor = VHOST_NET_MINOR, 1871 .name = "vhost-net", 1872 .fops = &vhost_net_fops, 1873 }; 1874 1875 static int __init vhost_net_init(void) 1876 { 1877 if (experimental_zcopytx) 1878 vhost_net_enable_zcopy(VHOST_NET_VQ_TX); 1879 return misc_register(&vhost_net_misc); 1880 } 1881 module_init(vhost_net_init); 1882 1883 static void __exit vhost_net_exit(void) 1884 { 1885 misc_deregister(&vhost_net_misc); 1886 } 1887 module_exit(vhost_net_exit); 1888 1889 MODULE_VERSION("0.0.1"); 1890 MODULE_LICENSE("GPL v2"); 1891 MODULE_AUTHOR("Michael S. Tsirkin"); 1892 MODULE_DESCRIPTION("Host kernel accelerator for virtio net"); 1893 MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR); 1894 MODULE_ALIAS("devname:vhost-net"); 1895