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