1 /* Copyright (C) 2009 Red Hat, Inc. 2 * Author: Michael S. Tsirkin <mst@redhat.com> 3 * 4 * This work is licensed under the terms of the GNU GPL, version 2. 5 * 6 * virtio-net server in host kernel. 7 */ 8 9 #include <linux/compat.h> 10 #include <linux/eventfd.h> 11 #include <linux/vhost.h> 12 #include <linux/virtio_net.h> 13 #include <linux/miscdevice.h> 14 #include <linux/module.h> 15 #include <linux/moduleparam.h> 16 #include <linux/mutex.h> 17 #include <linux/workqueue.h> 18 #include <linux/file.h> 19 #include <linux/slab.h> 20 #include <linux/sched/clock.h> 21 #include <linux/sched/signal.h> 22 #include <linux/vmalloc.h> 23 24 #include <linux/net.h> 25 #include <linux/if_packet.h> 26 #include <linux/if_arp.h> 27 #include <linux/if_tun.h> 28 #include <linux/if_macvlan.h> 29 #include <linux/if_tap.h> 30 #include <linux/if_vlan.h> 31 #include <linux/skb_array.h> 32 #include <linux/skbuff.h> 33 34 #include <net/sock.h> 35 #include <net/xdp.h> 36 37 #include "vhost.h" 38 39 static int experimental_zcopytx = 1; 40 module_param(experimental_zcopytx, int, 0444); 41 MODULE_PARM_DESC(experimental_zcopytx, "Enable Zero Copy TX;" 42 " 1 -Enable; 0 - Disable"); 43 44 /* Max number of bytes transferred before requeueing the job. 45 * Using this limit prevents one virtqueue from starving others. */ 46 #define VHOST_NET_WEIGHT 0x80000 47 48 /* Max number of packets transferred before requeueing the job. 49 * Using this limit prevents one virtqueue from starving others with small 50 * pkts. 51 */ 52 #define VHOST_NET_PKT_WEIGHT 256 53 54 /* MAX number of TX used buffers for outstanding zerocopy */ 55 #define VHOST_MAX_PEND 128 56 #define VHOST_GOODCOPY_LEN 256 57 58 /* 59 * For transmit, used buffer len is unused; we override it to track buffer 60 * status internally; used for zerocopy tx only. 61 */ 62 /* Lower device DMA failed */ 63 #define VHOST_DMA_FAILED_LEN ((__force __virtio32)3) 64 /* Lower device DMA done */ 65 #define VHOST_DMA_DONE_LEN ((__force __virtio32)2) 66 /* Lower device DMA in progress */ 67 #define VHOST_DMA_IN_PROGRESS ((__force __virtio32)1) 68 /* Buffer unused */ 69 #define VHOST_DMA_CLEAR_LEN ((__force __virtio32)0) 70 71 #define VHOST_DMA_IS_DONE(len) ((__force u32)(len) >= (__force u32)VHOST_DMA_DONE_LEN) 72 73 enum { 74 VHOST_NET_FEATURES = VHOST_FEATURES | 75 (1ULL << VHOST_NET_F_VIRTIO_NET_HDR) | 76 (1ULL << VIRTIO_NET_F_MRG_RXBUF) | 77 (1ULL << VIRTIO_F_IOMMU_PLATFORM) 78 }; 79 80 enum { 81 VHOST_NET_VQ_RX = 0, 82 VHOST_NET_VQ_TX = 1, 83 VHOST_NET_VQ_MAX = 2, 84 }; 85 86 struct vhost_net_ubuf_ref { 87 /* refcount follows semantics similar to kref: 88 * 0: object is released 89 * 1: no outstanding ubufs 90 * >1: outstanding ubufs 91 */ 92 atomic_t refcount; 93 wait_queue_head_t wait; 94 struct vhost_virtqueue *vq; 95 }; 96 97 #define VHOST_RX_BATCH 64 98 struct vhost_net_buf { 99 void **queue; 100 int tail; 101 int head; 102 }; 103 104 struct vhost_net_virtqueue { 105 struct vhost_virtqueue vq; 106 size_t vhost_hlen; 107 size_t sock_hlen; 108 /* vhost zerocopy support fields below: */ 109 /* last used idx for outstanding DMA zerocopy buffers */ 110 int upend_idx; 111 /* first used idx for DMA done zerocopy buffers */ 112 int done_idx; 113 /* an array of userspace buffers info */ 114 struct ubuf_info *ubuf_info; 115 /* Reference counting for outstanding ubufs. 116 * Protected by vq mutex. Writers must also take device mutex. */ 117 struct vhost_net_ubuf_ref *ubufs; 118 struct ptr_ring *rx_ring; 119 struct vhost_net_buf rxq; 120 }; 121 122 struct vhost_net { 123 struct vhost_dev dev; 124 struct vhost_net_virtqueue vqs[VHOST_NET_VQ_MAX]; 125 struct vhost_poll poll[VHOST_NET_VQ_MAX]; 126 /* Number of TX recently submitted. 127 * Protected by tx vq lock. */ 128 unsigned tx_packets; 129 /* Number of times zerocopy TX recently failed. 130 * Protected by tx vq lock. */ 131 unsigned tx_zcopy_err; 132 /* Flush in progress. Protected by tx vq lock. */ 133 bool tx_flush; 134 }; 135 136 static unsigned vhost_net_zcopy_mask __read_mostly; 137 138 static void *vhost_net_buf_get_ptr(struct vhost_net_buf *rxq) 139 { 140 if (rxq->tail != rxq->head) 141 return rxq->queue[rxq->head]; 142 else 143 return NULL; 144 } 145 146 static int vhost_net_buf_get_size(struct vhost_net_buf *rxq) 147 { 148 return rxq->tail - rxq->head; 149 } 150 151 static int vhost_net_buf_is_empty(struct vhost_net_buf *rxq) 152 { 153 return rxq->tail == rxq->head; 154 } 155 156 static void *vhost_net_buf_consume(struct vhost_net_buf *rxq) 157 { 158 void *ret = vhost_net_buf_get_ptr(rxq); 159 ++rxq->head; 160 return ret; 161 } 162 163 static int vhost_net_buf_produce(struct vhost_net_virtqueue *nvq) 164 { 165 struct vhost_net_buf *rxq = &nvq->rxq; 166 167 rxq->head = 0; 168 rxq->tail = ptr_ring_consume_batched(nvq->rx_ring, rxq->queue, 169 VHOST_RX_BATCH); 170 return rxq->tail; 171 } 172 173 static void vhost_net_buf_unproduce(struct vhost_net_virtqueue *nvq) 174 { 175 struct vhost_net_buf *rxq = &nvq->rxq; 176 177 if (nvq->rx_ring && !vhost_net_buf_is_empty(rxq)) { 178 ptr_ring_unconsume(nvq->rx_ring, rxq->queue + rxq->head, 179 vhost_net_buf_get_size(rxq), 180 tun_ptr_free); 181 rxq->head = rxq->tail = 0; 182 } 183 } 184 185 static int vhost_net_buf_peek_len(void *ptr) 186 { 187 if (tun_is_xdp_frame(ptr)) { 188 struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr); 189 190 return xdpf->len; 191 } 192 193 return __skb_array_len_with_tag(ptr); 194 } 195 196 static int vhost_net_buf_peek(struct vhost_net_virtqueue *nvq) 197 { 198 struct vhost_net_buf *rxq = &nvq->rxq; 199 200 if (!vhost_net_buf_is_empty(rxq)) 201 goto out; 202 203 if (!vhost_net_buf_produce(nvq)) 204 return 0; 205 206 out: 207 return vhost_net_buf_peek_len(vhost_net_buf_get_ptr(rxq)); 208 } 209 210 static void vhost_net_buf_init(struct vhost_net_buf *rxq) 211 { 212 rxq->head = rxq->tail = 0; 213 } 214 215 static void vhost_net_enable_zcopy(int vq) 216 { 217 vhost_net_zcopy_mask |= 0x1 << vq; 218 } 219 220 static struct vhost_net_ubuf_ref * 221 vhost_net_ubuf_alloc(struct vhost_virtqueue *vq, bool zcopy) 222 { 223 struct vhost_net_ubuf_ref *ubufs; 224 /* No zero copy backend? Nothing to count. */ 225 if (!zcopy) 226 return NULL; 227 ubufs = kmalloc(sizeof(*ubufs), GFP_KERNEL); 228 if (!ubufs) 229 return ERR_PTR(-ENOMEM); 230 atomic_set(&ubufs->refcount, 1); 231 init_waitqueue_head(&ubufs->wait); 232 ubufs->vq = vq; 233 return ubufs; 234 } 235 236 static int vhost_net_ubuf_put(struct vhost_net_ubuf_ref *ubufs) 237 { 238 int r = atomic_sub_return(1, &ubufs->refcount); 239 if (unlikely(!r)) 240 wake_up(&ubufs->wait); 241 return r; 242 } 243 244 static void vhost_net_ubuf_put_and_wait(struct vhost_net_ubuf_ref *ubufs) 245 { 246 vhost_net_ubuf_put(ubufs); 247 wait_event(ubufs->wait, !atomic_read(&ubufs->refcount)); 248 } 249 250 static void vhost_net_ubuf_put_wait_and_free(struct vhost_net_ubuf_ref *ubufs) 251 { 252 vhost_net_ubuf_put_and_wait(ubufs); 253 kfree(ubufs); 254 } 255 256 static void vhost_net_clear_ubuf_info(struct vhost_net *n) 257 { 258 int i; 259 260 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) { 261 kfree(n->vqs[i].ubuf_info); 262 n->vqs[i].ubuf_info = NULL; 263 } 264 } 265 266 static int vhost_net_set_ubuf_info(struct vhost_net *n) 267 { 268 bool zcopy; 269 int i; 270 271 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) { 272 zcopy = vhost_net_zcopy_mask & (0x1 << i); 273 if (!zcopy) 274 continue; 275 n->vqs[i].ubuf_info = kmalloc(sizeof(*n->vqs[i].ubuf_info) * 276 UIO_MAXIOV, GFP_KERNEL); 277 if (!n->vqs[i].ubuf_info) 278 goto err; 279 } 280 return 0; 281 282 err: 283 vhost_net_clear_ubuf_info(n); 284 return -ENOMEM; 285 } 286 287 static void vhost_net_vq_reset(struct vhost_net *n) 288 { 289 int i; 290 291 vhost_net_clear_ubuf_info(n); 292 293 for (i = 0; i < VHOST_NET_VQ_MAX; i++) { 294 n->vqs[i].done_idx = 0; 295 n->vqs[i].upend_idx = 0; 296 n->vqs[i].ubufs = NULL; 297 n->vqs[i].vhost_hlen = 0; 298 n->vqs[i].sock_hlen = 0; 299 vhost_net_buf_init(&n->vqs[i].rxq); 300 } 301 302 } 303 304 static void vhost_net_tx_packet(struct vhost_net *net) 305 { 306 ++net->tx_packets; 307 if (net->tx_packets < 1024) 308 return; 309 net->tx_packets = 0; 310 net->tx_zcopy_err = 0; 311 } 312 313 static void vhost_net_tx_err(struct vhost_net *net) 314 { 315 ++net->tx_zcopy_err; 316 } 317 318 static bool vhost_net_tx_select_zcopy(struct vhost_net *net) 319 { 320 /* TX flush waits for outstanding DMAs to be done. 321 * Don't start new DMAs. 322 */ 323 return !net->tx_flush && 324 net->tx_packets / 64 >= net->tx_zcopy_err; 325 } 326 327 static bool vhost_sock_zcopy(struct socket *sock) 328 { 329 return unlikely(experimental_zcopytx) && 330 sock_flag(sock->sk, SOCK_ZEROCOPY); 331 } 332 333 /* In case of DMA done not in order in lower device driver for some reason. 334 * upend_idx is used to track end of used idx, done_idx is used to track head 335 * of used idx. Once lower device DMA done contiguously, we will signal KVM 336 * guest used idx. 337 */ 338 static void vhost_zerocopy_signal_used(struct vhost_net *net, 339 struct vhost_virtqueue *vq) 340 { 341 struct vhost_net_virtqueue *nvq = 342 container_of(vq, struct vhost_net_virtqueue, vq); 343 int i, add; 344 int j = 0; 345 346 for (i = nvq->done_idx; i != nvq->upend_idx; i = (i + 1) % UIO_MAXIOV) { 347 if (vq->heads[i].len == VHOST_DMA_FAILED_LEN) 348 vhost_net_tx_err(net); 349 if (VHOST_DMA_IS_DONE(vq->heads[i].len)) { 350 vq->heads[i].len = VHOST_DMA_CLEAR_LEN; 351 ++j; 352 } else 353 break; 354 } 355 while (j) { 356 add = min(UIO_MAXIOV - nvq->done_idx, j); 357 vhost_add_used_and_signal_n(vq->dev, vq, 358 &vq->heads[nvq->done_idx], add); 359 nvq->done_idx = (nvq->done_idx + add) % UIO_MAXIOV; 360 j -= add; 361 } 362 } 363 364 static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success) 365 { 366 struct vhost_net_ubuf_ref *ubufs = ubuf->ctx; 367 struct vhost_virtqueue *vq = ubufs->vq; 368 int cnt; 369 370 rcu_read_lock_bh(); 371 372 /* set len to mark this desc buffers done DMA */ 373 vq->heads[ubuf->desc].len = success ? 374 VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN; 375 cnt = vhost_net_ubuf_put(ubufs); 376 377 /* 378 * Trigger polling thread if guest stopped submitting new buffers: 379 * in this case, the refcount after decrement will eventually reach 1. 380 * We also trigger polling periodically after each 16 packets 381 * (the value 16 here is more or less arbitrary, it's tuned to trigger 382 * less than 10% of times). 383 */ 384 if (cnt <= 1 || !(cnt % 16)) 385 vhost_poll_queue(&vq->poll); 386 387 rcu_read_unlock_bh(); 388 } 389 390 static inline unsigned long busy_clock(void) 391 { 392 return local_clock() >> 10; 393 } 394 395 static bool vhost_can_busy_poll(struct vhost_dev *dev, 396 unsigned long endtime) 397 { 398 return likely(!need_resched()) && 399 likely(!time_after(busy_clock(), endtime)) && 400 likely(!signal_pending(current)) && 401 !vhost_has_work(dev); 402 } 403 404 static void vhost_net_disable_vq(struct vhost_net *n, 405 struct vhost_virtqueue *vq) 406 { 407 struct vhost_net_virtqueue *nvq = 408 container_of(vq, struct vhost_net_virtqueue, vq); 409 struct vhost_poll *poll = n->poll + (nvq - n->vqs); 410 if (!vq->private_data) 411 return; 412 vhost_poll_stop(poll); 413 } 414 415 static int vhost_net_enable_vq(struct vhost_net *n, 416 struct vhost_virtqueue *vq) 417 { 418 struct vhost_net_virtqueue *nvq = 419 container_of(vq, struct vhost_net_virtqueue, vq); 420 struct vhost_poll *poll = n->poll + (nvq - n->vqs); 421 struct socket *sock; 422 423 sock = vq->private_data; 424 if (!sock) 425 return 0; 426 427 return vhost_poll_start(poll, sock->file); 428 } 429 430 static int vhost_net_tx_get_vq_desc(struct vhost_net *net, 431 struct vhost_virtqueue *vq, 432 struct iovec iov[], unsigned int iov_size, 433 unsigned int *out_num, unsigned int *in_num) 434 { 435 unsigned long uninitialized_var(endtime); 436 int r = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov), 437 out_num, in_num, NULL, NULL); 438 439 if (r == vq->num && vq->busyloop_timeout) { 440 preempt_disable(); 441 endtime = busy_clock() + vq->busyloop_timeout; 442 while (vhost_can_busy_poll(vq->dev, endtime) && 443 vhost_vq_avail_empty(vq->dev, vq)) 444 cpu_relax(); 445 preempt_enable(); 446 r = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov), 447 out_num, in_num, NULL, NULL); 448 } 449 450 return r; 451 } 452 453 static bool vhost_exceeds_maxpend(struct vhost_net *net) 454 { 455 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX]; 456 struct vhost_virtqueue *vq = &nvq->vq; 457 458 return (nvq->upend_idx + UIO_MAXIOV - nvq->done_idx) % UIO_MAXIOV > 459 min_t(unsigned int, VHOST_MAX_PEND, vq->num >> 2); 460 } 461 462 /* Expects to be always run from workqueue - which acts as 463 * read-size critical section for our kind of RCU. */ 464 static void handle_tx(struct vhost_net *net) 465 { 466 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX]; 467 struct vhost_virtqueue *vq = &nvq->vq; 468 unsigned out, in; 469 int head; 470 struct msghdr msg = { 471 .msg_name = NULL, 472 .msg_namelen = 0, 473 .msg_control = NULL, 474 .msg_controllen = 0, 475 .msg_flags = MSG_DONTWAIT, 476 }; 477 size_t len, total_len = 0; 478 int err; 479 size_t hdr_size; 480 struct socket *sock; 481 struct vhost_net_ubuf_ref *uninitialized_var(ubufs); 482 bool zcopy, zcopy_used; 483 int sent_pkts = 0; 484 485 mutex_lock(&vq->mutex); 486 sock = vq->private_data; 487 if (!sock) 488 goto out; 489 490 if (!vq_iotlb_prefetch(vq)) 491 goto out; 492 493 vhost_disable_notify(&net->dev, vq); 494 vhost_net_disable_vq(net, vq); 495 496 hdr_size = nvq->vhost_hlen; 497 zcopy = nvq->ubufs; 498 499 for (;;) { 500 /* Release DMAs done buffers first */ 501 if (zcopy) 502 vhost_zerocopy_signal_used(net, vq); 503 504 505 head = vhost_net_tx_get_vq_desc(net, vq, vq->iov, 506 ARRAY_SIZE(vq->iov), 507 &out, &in); 508 /* On error, stop handling until the next kick. */ 509 if (unlikely(head < 0)) 510 break; 511 /* Nothing new? Wait for eventfd to tell us they refilled. */ 512 if (head == vq->num) { 513 if (unlikely(vhost_enable_notify(&net->dev, vq))) { 514 vhost_disable_notify(&net->dev, vq); 515 continue; 516 } 517 break; 518 } 519 if (in) { 520 vq_err(vq, "Unexpected descriptor format for TX: " 521 "out %d, int %d\n", out, in); 522 break; 523 } 524 /* Skip header. TODO: support TSO. */ 525 len = iov_length(vq->iov, out); 526 iov_iter_init(&msg.msg_iter, WRITE, vq->iov, out, len); 527 iov_iter_advance(&msg.msg_iter, hdr_size); 528 /* Sanity check */ 529 if (!msg_data_left(&msg)) { 530 vq_err(vq, "Unexpected header len for TX: " 531 "%zd expected %zd\n", 532 len, hdr_size); 533 break; 534 } 535 len = msg_data_left(&msg); 536 537 zcopy_used = zcopy && len >= VHOST_GOODCOPY_LEN 538 && !vhost_exceeds_maxpend(net) 539 && vhost_net_tx_select_zcopy(net); 540 541 /* use msg_control to pass vhost zerocopy ubuf info to skb */ 542 if (zcopy_used) { 543 struct ubuf_info *ubuf; 544 ubuf = nvq->ubuf_info + nvq->upend_idx; 545 546 vq->heads[nvq->upend_idx].id = cpu_to_vhost32(vq, head); 547 vq->heads[nvq->upend_idx].len = VHOST_DMA_IN_PROGRESS; 548 ubuf->callback = vhost_zerocopy_callback; 549 ubuf->ctx = nvq->ubufs; 550 ubuf->desc = nvq->upend_idx; 551 refcount_set(&ubuf->refcnt, 1); 552 msg.msg_control = ubuf; 553 msg.msg_controllen = sizeof(ubuf); 554 ubufs = nvq->ubufs; 555 atomic_inc(&ubufs->refcount); 556 nvq->upend_idx = (nvq->upend_idx + 1) % UIO_MAXIOV; 557 } else { 558 msg.msg_control = NULL; 559 ubufs = NULL; 560 } 561 562 total_len += len; 563 if (total_len < VHOST_NET_WEIGHT && 564 !vhost_vq_avail_empty(&net->dev, vq) && 565 likely(!vhost_exceeds_maxpend(net))) { 566 msg.msg_flags |= MSG_MORE; 567 } else { 568 msg.msg_flags &= ~MSG_MORE; 569 } 570 571 /* TODO: Check specific error and bomb out unless ENOBUFS? */ 572 err = sock->ops->sendmsg(sock, &msg, len); 573 if (unlikely(err < 0)) { 574 if (zcopy_used) { 575 vhost_net_ubuf_put(ubufs); 576 nvq->upend_idx = ((unsigned)nvq->upend_idx - 1) 577 % UIO_MAXIOV; 578 } 579 vhost_discard_vq_desc(vq, 1); 580 vhost_net_enable_vq(net, vq); 581 break; 582 } 583 if (err != len) 584 pr_debug("Truncated TX packet: " 585 " len %d != %zd\n", err, len); 586 if (!zcopy_used) 587 vhost_add_used_and_signal(&net->dev, vq, head, 0); 588 else 589 vhost_zerocopy_signal_used(net, vq); 590 vhost_net_tx_packet(net); 591 if (unlikely(total_len >= VHOST_NET_WEIGHT) || 592 unlikely(++sent_pkts >= VHOST_NET_PKT_WEIGHT)) { 593 vhost_poll_queue(&vq->poll); 594 break; 595 } 596 } 597 out: 598 mutex_unlock(&vq->mutex); 599 } 600 601 static int peek_head_len(struct vhost_net_virtqueue *rvq, struct sock *sk) 602 { 603 struct sk_buff *head; 604 int len = 0; 605 unsigned long flags; 606 607 if (rvq->rx_ring) 608 return vhost_net_buf_peek(rvq); 609 610 spin_lock_irqsave(&sk->sk_receive_queue.lock, flags); 611 head = skb_peek(&sk->sk_receive_queue); 612 if (likely(head)) { 613 len = head->len; 614 if (skb_vlan_tag_present(head)) 615 len += VLAN_HLEN; 616 } 617 618 spin_unlock_irqrestore(&sk->sk_receive_queue.lock, flags); 619 return len; 620 } 621 622 static int sk_has_rx_data(struct sock *sk) 623 { 624 struct socket *sock = sk->sk_socket; 625 626 if (sock->ops->peek_len) 627 return sock->ops->peek_len(sock); 628 629 return skb_queue_empty(&sk->sk_receive_queue); 630 } 631 632 static int vhost_net_rx_peek_head_len(struct vhost_net *net, struct sock *sk) 633 { 634 struct vhost_net_virtqueue *rvq = &net->vqs[VHOST_NET_VQ_RX]; 635 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX]; 636 struct vhost_virtqueue *vq = &nvq->vq; 637 unsigned long uninitialized_var(endtime); 638 int len = peek_head_len(rvq, sk); 639 640 if (!len && vq->busyloop_timeout) { 641 /* Both tx vq and rx socket were polled here */ 642 mutex_lock_nested(&vq->mutex, 1); 643 vhost_disable_notify(&net->dev, vq); 644 645 preempt_disable(); 646 endtime = busy_clock() + vq->busyloop_timeout; 647 648 while (vhost_can_busy_poll(&net->dev, endtime) && 649 !sk_has_rx_data(sk) && 650 vhost_vq_avail_empty(&net->dev, vq)) 651 cpu_relax(); 652 653 preempt_enable(); 654 655 if (!vhost_vq_avail_empty(&net->dev, vq)) 656 vhost_poll_queue(&vq->poll); 657 else if (unlikely(vhost_enable_notify(&net->dev, vq))) { 658 vhost_disable_notify(&net->dev, vq); 659 vhost_poll_queue(&vq->poll); 660 } 661 662 mutex_unlock(&vq->mutex); 663 664 len = peek_head_len(rvq, sk); 665 } 666 667 return len; 668 } 669 670 /* This is a multi-buffer version of vhost_get_desc, that works if 671 * vq has read descriptors only. 672 * @vq - the relevant virtqueue 673 * @datalen - data length we'll be reading 674 * @iovcount - returned count of io vectors we fill 675 * @log - vhost log 676 * @log_num - log offset 677 * @quota - headcount quota, 1 for big buffer 678 * returns number of buffer heads allocated, negative on error 679 */ 680 static int get_rx_bufs(struct vhost_virtqueue *vq, 681 struct vring_used_elem *heads, 682 int datalen, 683 unsigned *iovcount, 684 struct vhost_log *log, 685 unsigned *log_num, 686 unsigned int quota) 687 { 688 unsigned int out, in; 689 int seg = 0; 690 int headcount = 0; 691 unsigned d; 692 int r, nlogs = 0; 693 /* len is always initialized before use since we are always called with 694 * datalen > 0. 695 */ 696 u32 uninitialized_var(len); 697 698 while (datalen > 0 && headcount < quota) { 699 if (unlikely(seg >= UIO_MAXIOV)) { 700 r = -ENOBUFS; 701 goto err; 702 } 703 r = vhost_get_vq_desc(vq, vq->iov + seg, 704 ARRAY_SIZE(vq->iov) - seg, &out, 705 &in, log, log_num); 706 if (unlikely(r < 0)) 707 goto err; 708 709 d = r; 710 if (d == vq->num) { 711 r = 0; 712 goto err; 713 } 714 if (unlikely(out || in <= 0)) { 715 vq_err(vq, "unexpected descriptor format for RX: " 716 "out %d, in %d\n", out, in); 717 r = -EINVAL; 718 goto err; 719 } 720 if (unlikely(log)) { 721 nlogs += *log_num; 722 log += *log_num; 723 } 724 heads[headcount].id = cpu_to_vhost32(vq, d); 725 len = iov_length(vq->iov + seg, in); 726 heads[headcount].len = cpu_to_vhost32(vq, len); 727 datalen -= len; 728 ++headcount; 729 seg += in; 730 } 731 heads[headcount - 1].len = cpu_to_vhost32(vq, len + datalen); 732 *iovcount = seg; 733 if (unlikely(log)) 734 *log_num = nlogs; 735 736 /* Detect overrun */ 737 if (unlikely(datalen > 0)) { 738 r = UIO_MAXIOV + 1; 739 goto err; 740 } 741 return headcount; 742 err: 743 vhost_discard_vq_desc(vq, headcount); 744 return r; 745 } 746 747 /* Expects to be always run from workqueue - which acts as 748 * read-size critical section for our kind of RCU. */ 749 static void handle_rx(struct vhost_net *net) 750 { 751 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_RX]; 752 struct vhost_virtqueue *vq = &nvq->vq; 753 unsigned uninitialized_var(in), log; 754 struct vhost_log *vq_log; 755 struct msghdr msg = { 756 .msg_name = NULL, 757 .msg_namelen = 0, 758 .msg_control = NULL, /* FIXME: get and handle RX aux data. */ 759 .msg_controllen = 0, 760 .msg_flags = MSG_DONTWAIT, 761 }; 762 struct virtio_net_hdr hdr = { 763 .flags = 0, 764 .gso_type = VIRTIO_NET_HDR_GSO_NONE 765 }; 766 size_t total_len = 0; 767 int err, mergeable; 768 s16 headcount, nheads = 0; 769 size_t vhost_hlen, sock_hlen; 770 size_t vhost_len, sock_len; 771 struct socket *sock; 772 struct iov_iter fixup; 773 __virtio16 num_buffers; 774 int recv_pkts = 0; 775 776 mutex_lock_nested(&vq->mutex, 0); 777 sock = vq->private_data; 778 if (!sock) 779 goto out; 780 781 if (!vq_iotlb_prefetch(vq)) 782 goto out; 783 784 vhost_disable_notify(&net->dev, vq); 785 vhost_net_disable_vq(net, vq); 786 787 vhost_hlen = nvq->vhost_hlen; 788 sock_hlen = nvq->sock_hlen; 789 790 vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ? 791 vq->log : NULL; 792 mergeable = vhost_has_feature(vq, VIRTIO_NET_F_MRG_RXBUF); 793 794 while ((sock_len = vhost_net_rx_peek_head_len(net, sock->sk))) { 795 sock_len += sock_hlen; 796 vhost_len = sock_len + vhost_hlen; 797 headcount = get_rx_bufs(vq, vq->heads + nheads, vhost_len, 798 &in, vq_log, &log, 799 likely(mergeable) ? UIO_MAXIOV : 1); 800 /* On error, stop handling until the next kick. */ 801 if (unlikely(headcount < 0)) 802 goto out; 803 /* OK, now we need to know about added descriptors. */ 804 if (!headcount) { 805 if (unlikely(vhost_enable_notify(&net->dev, vq))) { 806 /* They have slipped one in as we were 807 * doing that: check again. */ 808 vhost_disable_notify(&net->dev, vq); 809 continue; 810 } 811 /* Nothing new? Wait for eventfd to tell us 812 * they refilled. */ 813 goto out; 814 } 815 if (nvq->rx_ring) 816 msg.msg_control = vhost_net_buf_consume(&nvq->rxq); 817 /* On overrun, truncate and discard */ 818 if (unlikely(headcount > UIO_MAXIOV)) { 819 iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1); 820 err = sock->ops->recvmsg(sock, &msg, 821 1, MSG_DONTWAIT | MSG_TRUNC); 822 pr_debug("Discarded rx packet: len %zd\n", sock_len); 823 continue; 824 } 825 /* We don't need to be notified again. */ 826 iov_iter_init(&msg.msg_iter, READ, vq->iov, in, vhost_len); 827 fixup = msg.msg_iter; 828 if (unlikely((vhost_hlen))) { 829 /* We will supply the header ourselves 830 * TODO: support TSO. 831 */ 832 iov_iter_advance(&msg.msg_iter, vhost_hlen); 833 } 834 err = sock->ops->recvmsg(sock, &msg, 835 sock_len, MSG_DONTWAIT | MSG_TRUNC); 836 /* Userspace might have consumed the packet meanwhile: 837 * it's not supposed to do this usually, but might be hard 838 * to prevent. Discard data we got (if any) and keep going. */ 839 if (unlikely(err != sock_len)) { 840 pr_debug("Discarded rx packet: " 841 " len %d, expected %zd\n", err, sock_len); 842 vhost_discard_vq_desc(vq, headcount); 843 continue; 844 } 845 /* Supply virtio_net_hdr if VHOST_NET_F_VIRTIO_NET_HDR */ 846 if (unlikely(vhost_hlen)) { 847 if (copy_to_iter(&hdr, sizeof(hdr), 848 &fixup) != sizeof(hdr)) { 849 vq_err(vq, "Unable to write vnet_hdr " 850 "at addr %p\n", vq->iov->iov_base); 851 goto out; 852 } 853 } else { 854 /* Header came from socket; we'll need to patch 855 * ->num_buffers over if VIRTIO_NET_F_MRG_RXBUF 856 */ 857 iov_iter_advance(&fixup, sizeof(hdr)); 858 } 859 /* TODO: Should check and handle checksum. */ 860 861 num_buffers = cpu_to_vhost16(vq, headcount); 862 if (likely(mergeable) && 863 copy_to_iter(&num_buffers, sizeof num_buffers, 864 &fixup) != sizeof num_buffers) { 865 vq_err(vq, "Failed num_buffers write"); 866 vhost_discard_vq_desc(vq, headcount); 867 goto out; 868 } 869 nheads += headcount; 870 if (nheads > VHOST_RX_BATCH) { 871 vhost_add_used_and_signal_n(&net->dev, vq, vq->heads, 872 nheads); 873 nheads = 0; 874 } 875 if (unlikely(vq_log)) 876 vhost_log_write(vq, vq_log, log, vhost_len); 877 total_len += vhost_len; 878 if (unlikely(total_len >= VHOST_NET_WEIGHT) || 879 unlikely(++recv_pkts >= VHOST_NET_PKT_WEIGHT)) { 880 vhost_poll_queue(&vq->poll); 881 goto out; 882 } 883 } 884 vhost_net_enable_vq(net, vq); 885 out: 886 if (nheads) 887 vhost_add_used_and_signal_n(&net->dev, vq, vq->heads, 888 nheads); 889 mutex_unlock(&vq->mutex); 890 } 891 892 static void handle_tx_kick(struct vhost_work *work) 893 { 894 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue, 895 poll.work); 896 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev); 897 898 handle_tx(net); 899 } 900 901 static void handle_rx_kick(struct vhost_work *work) 902 { 903 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue, 904 poll.work); 905 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev); 906 907 handle_rx(net); 908 } 909 910 static void handle_tx_net(struct vhost_work *work) 911 { 912 struct vhost_net *net = container_of(work, struct vhost_net, 913 poll[VHOST_NET_VQ_TX].work); 914 handle_tx(net); 915 } 916 917 static void handle_rx_net(struct vhost_work *work) 918 { 919 struct vhost_net *net = container_of(work, struct vhost_net, 920 poll[VHOST_NET_VQ_RX].work); 921 handle_rx(net); 922 } 923 924 static int vhost_net_open(struct inode *inode, struct file *f) 925 { 926 struct vhost_net *n; 927 struct vhost_dev *dev; 928 struct vhost_virtqueue **vqs; 929 void **queue; 930 int i; 931 932 n = kvmalloc(sizeof *n, GFP_KERNEL | __GFP_RETRY_MAYFAIL); 933 if (!n) 934 return -ENOMEM; 935 vqs = kmalloc(VHOST_NET_VQ_MAX * sizeof(*vqs), GFP_KERNEL); 936 if (!vqs) { 937 kvfree(n); 938 return -ENOMEM; 939 } 940 941 queue = kmalloc_array(VHOST_RX_BATCH, sizeof(void *), 942 GFP_KERNEL); 943 if (!queue) { 944 kfree(vqs); 945 kvfree(n); 946 return -ENOMEM; 947 } 948 n->vqs[VHOST_NET_VQ_RX].rxq.queue = queue; 949 950 dev = &n->dev; 951 vqs[VHOST_NET_VQ_TX] = &n->vqs[VHOST_NET_VQ_TX].vq; 952 vqs[VHOST_NET_VQ_RX] = &n->vqs[VHOST_NET_VQ_RX].vq; 953 n->vqs[VHOST_NET_VQ_TX].vq.handle_kick = handle_tx_kick; 954 n->vqs[VHOST_NET_VQ_RX].vq.handle_kick = handle_rx_kick; 955 for (i = 0; i < VHOST_NET_VQ_MAX; i++) { 956 n->vqs[i].ubufs = NULL; 957 n->vqs[i].ubuf_info = NULL; 958 n->vqs[i].upend_idx = 0; 959 n->vqs[i].done_idx = 0; 960 n->vqs[i].vhost_hlen = 0; 961 n->vqs[i].sock_hlen = 0; 962 n->vqs[i].rx_ring = NULL; 963 vhost_net_buf_init(&n->vqs[i].rxq); 964 } 965 vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX); 966 967 vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, EPOLLOUT, dev); 968 vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, EPOLLIN, dev); 969 970 f->private_data = n; 971 972 return 0; 973 } 974 975 static struct socket *vhost_net_stop_vq(struct vhost_net *n, 976 struct vhost_virtqueue *vq) 977 { 978 struct socket *sock; 979 struct vhost_net_virtqueue *nvq = 980 container_of(vq, struct vhost_net_virtqueue, vq); 981 982 mutex_lock(&vq->mutex); 983 sock = vq->private_data; 984 vhost_net_disable_vq(n, vq); 985 vq->private_data = NULL; 986 vhost_net_buf_unproduce(nvq); 987 nvq->rx_ring = NULL; 988 mutex_unlock(&vq->mutex); 989 return sock; 990 } 991 992 static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock, 993 struct socket **rx_sock) 994 { 995 *tx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_TX].vq); 996 *rx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_RX].vq); 997 } 998 999 static void vhost_net_flush_vq(struct vhost_net *n, int index) 1000 { 1001 vhost_poll_flush(n->poll + index); 1002 vhost_poll_flush(&n->vqs[index].vq.poll); 1003 } 1004 1005 static void vhost_net_flush(struct vhost_net *n) 1006 { 1007 vhost_net_flush_vq(n, VHOST_NET_VQ_TX); 1008 vhost_net_flush_vq(n, VHOST_NET_VQ_RX); 1009 if (n->vqs[VHOST_NET_VQ_TX].ubufs) { 1010 mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex); 1011 n->tx_flush = true; 1012 mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex); 1013 /* Wait for all lower device DMAs done. */ 1014 vhost_net_ubuf_put_and_wait(n->vqs[VHOST_NET_VQ_TX].ubufs); 1015 mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex); 1016 n->tx_flush = false; 1017 atomic_set(&n->vqs[VHOST_NET_VQ_TX].ubufs->refcount, 1); 1018 mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex); 1019 } 1020 } 1021 1022 static int vhost_net_release(struct inode *inode, struct file *f) 1023 { 1024 struct vhost_net *n = f->private_data; 1025 struct socket *tx_sock; 1026 struct socket *rx_sock; 1027 1028 vhost_net_stop(n, &tx_sock, &rx_sock); 1029 vhost_net_flush(n); 1030 vhost_dev_stop(&n->dev); 1031 vhost_dev_cleanup(&n->dev); 1032 vhost_net_vq_reset(n); 1033 if (tx_sock) 1034 sockfd_put(tx_sock); 1035 if (rx_sock) 1036 sockfd_put(rx_sock); 1037 /* Make sure no callbacks are outstanding */ 1038 synchronize_rcu_bh(); 1039 /* We do an extra flush before freeing memory, 1040 * since jobs can re-queue themselves. */ 1041 vhost_net_flush(n); 1042 kfree(n->vqs[VHOST_NET_VQ_RX].rxq.queue); 1043 kfree(n->dev.vqs); 1044 kvfree(n); 1045 return 0; 1046 } 1047 1048 static struct socket *get_raw_socket(int fd) 1049 { 1050 struct { 1051 struct sockaddr_ll sa; 1052 char buf[MAX_ADDR_LEN]; 1053 } uaddr; 1054 int r; 1055 struct socket *sock = sockfd_lookup(fd, &r); 1056 1057 if (!sock) 1058 return ERR_PTR(-ENOTSOCK); 1059 1060 /* Parameter checking */ 1061 if (sock->sk->sk_type != SOCK_RAW) { 1062 r = -ESOCKTNOSUPPORT; 1063 goto err; 1064 } 1065 1066 r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa, 0); 1067 if (r < 0) 1068 goto err; 1069 1070 if (uaddr.sa.sll_family != AF_PACKET) { 1071 r = -EPFNOSUPPORT; 1072 goto err; 1073 } 1074 return sock; 1075 err: 1076 sockfd_put(sock); 1077 return ERR_PTR(r); 1078 } 1079 1080 static struct ptr_ring *get_tap_ptr_ring(int fd) 1081 { 1082 struct ptr_ring *ring; 1083 struct file *file = fget(fd); 1084 1085 if (!file) 1086 return NULL; 1087 ring = tun_get_tx_ring(file); 1088 if (!IS_ERR(ring)) 1089 goto out; 1090 ring = tap_get_ptr_ring(file); 1091 if (!IS_ERR(ring)) 1092 goto out; 1093 ring = NULL; 1094 out: 1095 fput(file); 1096 return ring; 1097 } 1098 1099 static struct socket *get_tap_socket(int fd) 1100 { 1101 struct file *file = fget(fd); 1102 struct socket *sock; 1103 1104 if (!file) 1105 return ERR_PTR(-EBADF); 1106 sock = tun_get_socket(file); 1107 if (!IS_ERR(sock)) 1108 return sock; 1109 sock = tap_get_socket(file); 1110 if (IS_ERR(sock)) 1111 fput(file); 1112 return sock; 1113 } 1114 1115 static struct socket *get_socket(int fd) 1116 { 1117 struct socket *sock; 1118 1119 /* special case to disable backend */ 1120 if (fd == -1) 1121 return NULL; 1122 sock = get_raw_socket(fd); 1123 if (!IS_ERR(sock)) 1124 return sock; 1125 sock = get_tap_socket(fd); 1126 if (!IS_ERR(sock)) 1127 return sock; 1128 return ERR_PTR(-ENOTSOCK); 1129 } 1130 1131 static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd) 1132 { 1133 struct socket *sock, *oldsock; 1134 struct vhost_virtqueue *vq; 1135 struct vhost_net_virtqueue *nvq; 1136 struct vhost_net_ubuf_ref *ubufs, *oldubufs = NULL; 1137 int r; 1138 1139 mutex_lock(&n->dev.mutex); 1140 r = vhost_dev_check_owner(&n->dev); 1141 if (r) 1142 goto err; 1143 1144 if (index >= VHOST_NET_VQ_MAX) { 1145 r = -ENOBUFS; 1146 goto err; 1147 } 1148 vq = &n->vqs[index].vq; 1149 nvq = &n->vqs[index]; 1150 mutex_lock(&vq->mutex); 1151 1152 /* Verify that ring has been setup correctly. */ 1153 if (!vhost_vq_access_ok(vq)) { 1154 r = -EFAULT; 1155 goto err_vq; 1156 } 1157 sock = get_socket(fd); 1158 if (IS_ERR(sock)) { 1159 r = PTR_ERR(sock); 1160 goto err_vq; 1161 } 1162 1163 /* start polling new socket */ 1164 oldsock = vq->private_data; 1165 if (sock != oldsock) { 1166 ubufs = vhost_net_ubuf_alloc(vq, 1167 sock && vhost_sock_zcopy(sock)); 1168 if (IS_ERR(ubufs)) { 1169 r = PTR_ERR(ubufs); 1170 goto err_ubufs; 1171 } 1172 1173 vhost_net_disable_vq(n, vq); 1174 vq->private_data = sock; 1175 vhost_net_buf_unproduce(nvq); 1176 r = vhost_vq_init_access(vq); 1177 if (r) 1178 goto err_used; 1179 r = vhost_net_enable_vq(n, vq); 1180 if (r) 1181 goto err_used; 1182 if (index == VHOST_NET_VQ_RX) 1183 nvq->rx_ring = get_tap_ptr_ring(fd); 1184 1185 oldubufs = nvq->ubufs; 1186 nvq->ubufs = ubufs; 1187 1188 n->tx_packets = 0; 1189 n->tx_zcopy_err = 0; 1190 n->tx_flush = false; 1191 } 1192 1193 mutex_unlock(&vq->mutex); 1194 1195 if (oldubufs) { 1196 vhost_net_ubuf_put_wait_and_free(oldubufs); 1197 mutex_lock(&vq->mutex); 1198 vhost_zerocopy_signal_used(n, vq); 1199 mutex_unlock(&vq->mutex); 1200 } 1201 1202 if (oldsock) { 1203 vhost_net_flush_vq(n, index); 1204 sockfd_put(oldsock); 1205 } 1206 1207 mutex_unlock(&n->dev.mutex); 1208 return 0; 1209 1210 err_used: 1211 vq->private_data = oldsock; 1212 vhost_net_enable_vq(n, vq); 1213 if (ubufs) 1214 vhost_net_ubuf_put_wait_and_free(ubufs); 1215 err_ubufs: 1216 sockfd_put(sock); 1217 err_vq: 1218 mutex_unlock(&vq->mutex); 1219 err: 1220 mutex_unlock(&n->dev.mutex); 1221 return r; 1222 } 1223 1224 static long vhost_net_reset_owner(struct vhost_net *n) 1225 { 1226 struct socket *tx_sock = NULL; 1227 struct socket *rx_sock = NULL; 1228 long err; 1229 struct vhost_umem *umem; 1230 1231 mutex_lock(&n->dev.mutex); 1232 err = vhost_dev_check_owner(&n->dev); 1233 if (err) 1234 goto done; 1235 umem = vhost_dev_reset_owner_prepare(); 1236 if (!umem) { 1237 err = -ENOMEM; 1238 goto done; 1239 } 1240 vhost_net_stop(n, &tx_sock, &rx_sock); 1241 vhost_net_flush(n); 1242 vhost_dev_stop(&n->dev); 1243 vhost_dev_reset_owner(&n->dev, umem); 1244 vhost_net_vq_reset(n); 1245 done: 1246 mutex_unlock(&n->dev.mutex); 1247 if (tx_sock) 1248 sockfd_put(tx_sock); 1249 if (rx_sock) 1250 sockfd_put(rx_sock); 1251 return err; 1252 } 1253 1254 static int vhost_net_set_features(struct vhost_net *n, u64 features) 1255 { 1256 size_t vhost_hlen, sock_hlen, hdr_len; 1257 int i; 1258 1259 hdr_len = (features & ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | 1260 (1ULL << VIRTIO_F_VERSION_1))) ? 1261 sizeof(struct virtio_net_hdr_mrg_rxbuf) : 1262 sizeof(struct virtio_net_hdr); 1263 if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) { 1264 /* vhost provides vnet_hdr */ 1265 vhost_hlen = hdr_len; 1266 sock_hlen = 0; 1267 } else { 1268 /* socket provides vnet_hdr */ 1269 vhost_hlen = 0; 1270 sock_hlen = hdr_len; 1271 } 1272 mutex_lock(&n->dev.mutex); 1273 if ((features & (1 << VHOST_F_LOG_ALL)) && 1274 !vhost_log_access_ok(&n->dev)) 1275 goto out_unlock; 1276 1277 if ((features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))) { 1278 if (vhost_init_device_iotlb(&n->dev, true)) 1279 goto out_unlock; 1280 } 1281 1282 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) { 1283 mutex_lock(&n->vqs[i].vq.mutex); 1284 n->vqs[i].vq.acked_features = features; 1285 n->vqs[i].vhost_hlen = vhost_hlen; 1286 n->vqs[i].sock_hlen = sock_hlen; 1287 mutex_unlock(&n->vqs[i].vq.mutex); 1288 } 1289 mutex_unlock(&n->dev.mutex); 1290 return 0; 1291 1292 out_unlock: 1293 mutex_unlock(&n->dev.mutex); 1294 return -EFAULT; 1295 } 1296 1297 static long vhost_net_set_owner(struct vhost_net *n) 1298 { 1299 int r; 1300 1301 mutex_lock(&n->dev.mutex); 1302 if (vhost_dev_has_owner(&n->dev)) { 1303 r = -EBUSY; 1304 goto out; 1305 } 1306 r = vhost_net_set_ubuf_info(n); 1307 if (r) 1308 goto out; 1309 r = vhost_dev_set_owner(&n->dev); 1310 if (r) 1311 vhost_net_clear_ubuf_info(n); 1312 vhost_net_flush(n); 1313 out: 1314 mutex_unlock(&n->dev.mutex); 1315 return r; 1316 } 1317 1318 static long vhost_net_ioctl(struct file *f, unsigned int ioctl, 1319 unsigned long arg) 1320 { 1321 struct vhost_net *n = f->private_data; 1322 void __user *argp = (void __user *)arg; 1323 u64 __user *featurep = argp; 1324 struct vhost_vring_file backend; 1325 u64 features; 1326 int r; 1327 1328 switch (ioctl) { 1329 case VHOST_NET_SET_BACKEND: 1330 if (copy_from_user(&backend, argp, sizeof backend)) 1331 return -EFAULT; 1332 return vhost_net_set_backend(n, backend.index, backend.fd); 1333 case VHOST_GET_FEATURES: 1334 features = VHOST_NET_FEATURES; 1335 if (copy_to_user(featurep, &features, sizeof features)) 1336 return -EFAULT; 1337 return 0; 1338 case VHOST_SET_FEATURES: 1339 if (copy_from_user(&features, featurep, sizeof features)) 1340 return -EFAULT; 1341 if (features & ~VHOST_NET_FEATURES) 1342 return -EOPNOTSUPP; 1343 return vhost_net_set_features(n, features); 1344 case VHOST_RESET_OWNER: 1345 return vhost_net_reset_owner(n); 1346 case VHOST_SET_OWNER: 1347 return vhost_net_set_owner(n); 1348 default: 1349 mutex_lock(&n->dev.mutex); 1350 r = vhost_dev_ioctl(&n->dev, ioctl, argp); 1351 if (r == -ENOIOCTLCMD) 1352 r = vhost_vring_ioctl(&n->dev, ioctl, argp); 1353 else 1354 vhost_net_flush(n); 1355 mutex_unlock(&n->dev.mutex); 1356 return r; 1357 } 1358 } 1359 1360 #ifdef CONFIG_COMPAT 1361 static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl, 1362 unsigned long arg) 1363 { 1364 return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg)); 1365 } 1366 #endif 1367 1368 static ssize_t vhost_net_chr_read_iter(struct kiocb *iocb, struct iov_iter *to) 1369 { 1370 struct file *file = iocb->ki_filp; 1371 struct vhost_net *n = file->private_data; 1372 struct vhost_dev *dev = &n->dev; 1373 int noblock = file->f_flags & O_NONBLOCK; 1374 1375 return vhost_chr_read_iter(dev, to, noblock); 1376 } 1377 1378 static ssize_t vhost_net_chr_write_iter(struct kiocb *iocb, 1379 struct iov_iter *from) 1380 { 1381 struct file *file = iocb->ki_filp; 1382 struct vhost_net *n = file->private_data; 1383 struct vhost_dev *dev = &n->dev; 1384 1385 return vhost_chr_write_iter(dev, from); 1386 } 1387 1388 static __poll_t vhost_net_chr_poll(struct file *file, poll_table *wait) 1389 { 1390 struct vhost_net *n = file->private_data; 1391 struct vhost_dev *dev = &n->dev; 1392 1393 return vhost_chr_poll(file, dev, wait); 1394 } 1395 1396 static const struct file_operations vhost_net_fops = { 1397 .owner = THIS_MODULE, 1398 .release = vhost_net_release, 1399 .read_iter = vhost_net_chr_read_iter, 1400 .write_iter = vhost_net_chr_write_iter, 1401 .poll = vhost_net_chr_poll, 1402 .unlocked_ioctl = vhost_net_ioctl, 1403 #ifdef CONFIG_COMPAT 1404 .compat_ioctl = vhost_net_compat_ioctl, 1405 #endif 1406 .open = vhost_net_open, 1407 .llseek = noop_llseek, 1408 }; 1409 1410 static struct miscdevice vhost_net_misc = { 1411 .minor = VHOST_NET_MINOR, 1412 .name = "vhost-net", 1413 .fops = &vhost_net_fops, 1414 }; 1415 1416 static int vhost_net_init(void) 1417 { 1418 if (experimental_zcopytx) 1419 vhost_net_enable_zcopy(VHOST_NET_VQ_TX); 1420 return misc_register(&vhost_net_misc); 1421 } 1422 module_init(vhost_net_init); 1423 1424 static void vhost_net_exit(void) 1425 { 1426 misc_deregister(&vhost_net_misc); 1427 } 1428 module_exit(vhost_net_exit); 1429 1430 MODULE_VERSION("0.0.1"); 1431 MODULE_LICENSE("GPL v2"); 1432 MODULE_AUTHOR("Michael S. Tsirkin"); 1433 MODULE_DESCRIPTION("Host kernel accelerator for virtio net"); 1434 MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR); 1435 MODULE_ALIAS("devname:vhost-net"); 1436