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