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