1 /* A network driver using virtio. 2 * 3 * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, see <http://www.gnu.org/licenses/>. 17 */ 18 //#define DEBUG 19 #include <linux/netdevice.h> 20 #include <linux/etherdevice.h> 21 #include <linux/ethtool.h> 22 #include <linux/module.h> 23 #include <linux/virtio.h> 24 #include <linux/virtio_net.h> 25 #include <linux/bpf.h> 26 #include <linux/bpf_trace.h> 27 #include <linux/scatterlist.h> 28 #include <linux/if_vlan.h> 29 #include <linux/slab.h> 30 #include <linux/cpu.h> 31 #include <linux/average.h> 32 #include <linux/filter.h> 33 #include <net/route.h> 34 #include <net/xdp.h> 35 36 static int napi_weight = NAPI_POLL_WEIGHT; 37 module_param(napi_weight, int, 0444); 38 39 static bool csum = true, gso = true, napi_tx; 40 module_param(csum, bool, 0444); 41 module_param(gso, bool, 0444); 42 module_param(napi_tx, bool, 0644); 43 44 /* FIXME: MTU in config. */ 45 #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN) 46 #define GOOD_COPY_LEN 128 47 48 #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD) 49 50 /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */ 51 #define VIRTIO_XDP_HEADROOM 256 52 53 /* RX packet size EWMA. The average packet size is used to determine the packet 54 * buffer size when refilling RX rings. As the entire RX ring may be refilled 55 * at once, the weight is chosen so that the EWMA will be insensitive to short- 56 * term, transient changes in packet size. 57 */ 58 DECLARE_EWMA(pkt_len, 0, 64) 59 60 #define VIRTNET_DRIVER_VERSION "1.0.0" 61 62 static const unsigned long guest_offloads[] = { 63 VIRTIO_NET_F_GUEST_TSO4, 64 VIRTIO_NET_F_GUEST_TSO6, 65 VIRTIO_NET_F_GUEST_ECN, 66 VIRTIO_NET_F_GUEST_UFO 67 }; 68 69 struct virtnet_stat_desc { 70 char desc[ETH_GSTRING_LEN]; 71 size_t offset; 72 }; 73 74 struct virtnet_sq_stats { 75 struct u64_stats_sync syncp; 76 u64 packets; 77 u64 bytes; 78 }; 79 80 struct virtnet_rq_stats { 81 struct u64_stats_sync syncp; 82 u64 packets; 83 u64 bytes; 84 }; 85 86 #define VIRTNET_SQ_STAT(m) offsetof(struct virtnet_sq_stats, m) 87 #define VIRTNET_RQ_STAT(m) offsetof(struct virtnet_rq_stats, m) 88 89 static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = { 90 { "packets", VIRTNET_SQ_STAT(packets) }, 91 { "bytes", VIRTNET_SQ_STAT(bytes) }, 92 }; 93 94 static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = { 95 { "packets", VIRTNET_RQ_STAT(packets) }, 96 { "bytes", VIRTNET_RQ_STAT(bytes) }, 97 }; 98 99 #define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc) 100 #define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc) 101 102 /* Internal representation of a send virtqueue */ 103 struct send_queue { 104 /* Virtqueue associated with this send _queue */ 105 struct virtqueue *vq; 106 107 /* TX: fragments + linear part + virtio header */ 108 struct scatterlist sg[MAX_SKB_FRAGS + 2]; 109 110 /* Name of the send queue: output.$index */ 111 char name[40]; 112 113 struct virtnet_sq_stats stats; 114 115 struct napi_struct napi; 116 }; 117 118 /* Internal representation of a receive virtqueue */ 119 struct receive_queue { 120 /* Virtqueue associated with this receive_queue */ 121 struct virtqueue *vq; 122 123 struct napi_struct napi; 124 125 struct bpf_prog __rcu *xdp_prog; 126 127 struct virtnet_rq_stats stats; 128 129 /* Chain pages by the private ptr. */ 130 struct page *pages; 131 132 /* Average packet length for mergeable receive buffers. */ 133 struct ewma_pkt_len mrg_avg_pkt_len; 134 135 /* Page frag for packet buffer allocation. */ 136 struct page_frag alloc_frag; 137 138 /* RX: fragments + linear part + virtio header */ 139 struct scatterlist sg[MAX_SKB_FRAGS + 2]; 140 141 /* Min single buffer size for mergeable buffers case. */ 142 unsigned int min_buf_len; 143 144 /* Name of this receive queue: input.$index */ 145 char name[40]; 146 147 struct xdp_rxq_info xdp_rxq; 148 }; 149 150 struct virtnet_info { 151 struct virtio_device *vdev; 152 struct virtqueue *cvq; 153 struct net_device *dev; 154 struct send_queue *sq; 155 struct receive_queue *rq; 156 unsigned int status; 157 158 /* Max # of queue pairs supported by the device */ 159 u16 max_queue_pairs; 160 161 /* # of queue pairs currently used by the driver */ 162 u16 curr_queue_pairs; 163 164 /* # of XDP queue pairs currently used by the driver */ 165 u16 xdp_queue_pairs; 166 167 /* I like... big packets and I cannot lie! */ 168 bool big_packets; 169 170 /* Host will merge rx buffers for big packets (shake it! shake it!) */ 171 bool mergeable_rx_bufs; 172 173 /* Has control virtqueue */ 174 bool has_cvq; 175 176 /* Host can handle any s/g split between our header and packet data */ 177 bool any_header_sg; 178 179 /* Packet virtio header size */ 180 u8 hdr_len; 181 182 /* Work struct for refilling if we run low on memory. */ 183 struct delayed_work refill; 184 185 /* Work struct for config space updates */ 186 struct work_struct config_work; 187 188 /* Does the affinity hint is set for virtqueues? */ 189 bool affinity_hint_set; 190 191 /* CPU hotplug instances for online & dead */ 192 struct hlist_node node; 193 struct hlist_node node_dead; 194 195 /* Control VQ buffers: protected by the rtnl lock */ 196 struct virtio_net_ctrl_hdr ctrl_hdr; 197 virtio_net_ctrl_ack ctrl_status; 198 struct virtio_net_ctrl_mq ctrl_mq; 199 u8 ctrl_promisc; 200 u8 ctrl_allmulti; 201 u16 ctrl_vid; 202 u64 ctrl_offloads; 203 204 /* Ethtool settings */ 205 u8 duplex; 206 u32 speed; 207 208 unsigned long guest_offloads; 209 }; 210 211 struct padded_vnet_hdr { 212 struct virtio_net_hdr_mrg_rxbuf hdr; 213 /* 214 * hdr is in a separate sg buffer, and data sg buffer shares same page 215 * with this header sg. This padding makes next sg 16 byte aligned 216 * after the header. 217 */ 218 char padding[4]; 219 }; 220 221 /* Converting between virtqueue no. and kernel tx/rx queue no. 222 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq 223 */ 224 static int vq2txq(struct virtqueue *vq) 225 { 226 return (vq->index - 1) / 2; 227 } 228 229 static int txq2vq(int txq) 230 { 231 return txq * 2 + 1; 232 } 233 234 static int vq2rxq(struct virtqueue *vq) 235 { 236 return vq->index / 2; 237 } 238 239 static int rxq2vq(int rxq) 240 { 241 return rxq * 2; 242 } 243 244 static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb) 245 { 246 return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb; 247 } 248 249 /* 250 * private is used to chain pages for big packets, put the whole 251 * most recent used list in the beginning for reuse 252 */ 253 static void give_pages(struct receive_queue *rq, struct page *page) 254 { 255 struct page *end; 256 257 /* Find end of list, sew whole thing into vi->rq.pages. */ 258 for (end = page; end->private; end = (struct page *)end->private); 259 end->private = (unsigned long)rq->pages; 260 rq->pages = page; 261 } 262 263 static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask) 264 { 265 struct page *p = rq->pages; 266 267 if (p) { 268 rq->pages = (struct page *)p->private; 269 /* clear private here, it is used to chain pages */ 270 p->private = 0; 271 } else 272 p = alloc_page(gfp_mask); 273 return p; 274 } 275 276 static void virtqueue_napi_schedule(struct napi_struct *napi, 277 struct virtqueue *vq) 278 { 279 if (napi_schedule_prep(napi)) { 280 virtqueue_disable_cb(vq); 281 __napi_schedule(napi); 282 } 283 } 284 285 static void virtqueue_napi_complete(struct napi_struct *napi, 286 struct virtqueue *vq, int processed) 287 { 288 int opaque; 289 290 opaque = virtqueue_enable_cb_prepare(vq); 291 if (napi_complete_done(napi, processed)) { 292 if (unlikely(virtqueue_poll(vq, opaque))) 293 virtqueue_napi_schedule(napi, vq); 294 } else { 295 virtqueue_disable_cb(vq); 296 } 297 } 298 299 static void skb_xmit_done(struct virtqueue *vq) 300 { 301 struct virtnet_info *vi = vq->vdev->priv; 302 struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi; 303 304 /* Suppress further interrupts. */ 305 virtqueue_disable_cb(vq); 306 307 if (napi->weight) 308 virtqueue_napi_schedule(napi, vq); 309 else 310 /* We were probably waiting for more output buffers. */ 311 netif_wake_subqueue(vi->dev, vq2txq(vq)); 312 } 313 314 #define MRG_CTX_HEADER_SHIFT 22 315 static void *mergeable_len_to_ctx(unsigned int truesize, 316 unsigned int headroom) 317 { 318 return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize); 319 } 320 321 static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx) 322 { 323 return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT; 324 } 325 326 static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx) 327 { 328 return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1); 329 } 330 331 /* Called from bottom half context */ 332 static struct sk_buff *page_to_skb(struct virtnet_info *vi, 333 struct receive_queue *rq, 334 struct page *page, unsigned int offset, 335 unsigned int len, unsigned int truesize) 336 { 337 struct sk_buff *skb; 338 struct virtio_net_hdr_mrg_rxbuf *hdr; 339 unsigned int copy, hdr_len, hdr_padded_len; 340 char *p; 341 342 p = page_address(page) + offset; 343 344 /* copy small packet so we can reuse these pages for small data */ 345 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN); 346 if (unlikely(!skb)) 347 return NULL; 348 349 hdr = skb_vnet_hdr(skb); 350 351 hdr_len = vi->hdr_len; 352 if (vi->mergeable_rx_bufs) 353 hdr_padded_len = sizeof(*hdr); 354 else 355 hdr_padded_len = sizeof(struct padded_vnet_hdr); 356 357 memcpy(hdr, p, hdr_len); 358 359 len -= hdr_len; 360 offset += hdr_padded_len; 361 p += hdr_padded_len; 362 363 copy = len; 364 if (copy > skb_tailroom(skb)) 365 copy = skb_tailroom(skb); 366 skb_put_data(skb, p, copy); 367 368 len -= copy; 369 offset += copy; 370 371 if (vi->mergeable_rx_bufs) { 372 if (len) 373 skb_add_rx_frag(skb, 0, page, offset, len, truesize); 374 else 375 put_page(page); 376 return skb; 377 } 378 379 /* 380 * Verify that we can indeed put this data into a skb. 381 * This is here to handle cases when the device erroneously 382 * tries to receive more than is possible. This is usually 383 * the case of a broken device. 384 */ 385 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) { 386 net_dbg_ratelimited("%s: too much data\n", skb->dev->name); 387 dev_kfree_skb(skb); 388 return NULL; 389 } 390 BUG_ON(offset >= PAGE_SIZE); 391 while (len) { 392 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len); 393 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset, 394 frag_size, truesize); 395 len -= frag_size; 396 page = (struct page *)page->private; 397 offset = 0; 398 } 399 400 if (page) 401 give_pages(rq, page); 402 403 return skb; 404 } 405 406 static void virtnet_xdp_flush(struct net_device *dev) 407 { 408 struct virtnet_info *vi = netdev_priv(dev); 409 struct send_queue *sq; 410 unsigned int qp; 411 412 qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id(); 413 sq = &vi->sq[qp]; 414 415 virtqueue_kick(sq->vq); 416 } 417 418 static bool __virtnet_xdp_xmit(struct virtnet_info *vi, 419 struct xdp_buff *xdp) 420 { 421 struct virtio_net_hdr_mrg_rxbuf *hdr; 422 unsigned int len; 423 struct send_queue *sq; 424 unsigned int qp; 425 void *xdp_sent; 426 int err; 427 428 qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id(); 429 sq = &vi->sq[qp]; 430 431 /* Free up any pending old buffers before queueing new ones. */ 432 while ((xdp_sent = virtqueue_get_buf(sq->vq, &len)) != NULL) { 433 struct page *sent_page = virt_to_head_page(xdp_sent); 434 435 put_page(sent_page); 436 } 437 438 xdp->data -= vi->hdr_len; 439 /* Zero header and leave csum up to XDP layers */ 440 hdr = xdp->data; 441 memset(hdr, 0, vi->hdr_len); 442 443 sg_init_one(sq->sg, xdp->data, xdp->data_end - xdp->data); 444 445 err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp->data, GFP_ATOMIC); 446 if (unlikely(err)) 447 return false; /* Caller handle free/refcnt */ 448 449 return true; 450 } 451 452 static int virtnet_xdp_xmit(struct net_device *dev, struct xdp_buff *xdp) 453 { 454 struct virtnet_info *vi = netdev_priv(dev); 455 struct receive_queue *rq = vi->rq; 456 struct bpf_prog *xdp_prog; 457 bool sent; 458 459 /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this 460 * indicate XDP resources have been successfully allocated. 461 */ 462 xdp_prog = rcu_dereference(rq->xdp_prog); 463 if (!xdp_prog) 464 return -ENXIO; 465 466 sent = __virtnet_xdp_xmit(vi, xdp); 467 if (!sent) 468 return -ENOSPC; 469 return 0; 470 } 471 472 static unsigned int virtnet_get_headroom(struct virtnet_info *vi) 473 { 474 return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0; 475 } 476 477 /* We copy the packet for XDP in the following cases: 478 * 479 * 1) Packet is scattered across multiple rx buffers. 480 * 2) Headroom space is insufficient. 481 * 482 * This is inefficient but it's a temporary condition that 483 * we hit right after XDP is enabled and until queue is refilled 484 * with large buffers with sufficient headroom - so it should affect 485 * at most queue size packets. 486 * Afterwards, the conditions to enable 487 * XDP should preclude the underlying device from sending packets 488 * across multiple buffers (num_buf > 1), and we make sure buffers 489 * have enough headroom. 490 */ 491 static struct page *xdp_linearize_page(struct receive_queue *rq, 492 u16 *num_buf, 493 struct page *p, 494 int offset, 495 int page_off, 496 unsigned int *len) 497 { 498 struct page *page = alloc_page(GFP_ATOMIC); 499 500 if (!page) 501 return NULL; 502 503 memcpy(page_address(page) + page_off, page_address(p) + offset, *len); 504 page_off += *len; 505 506 while (--*num_buf) { 507 int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 508 unsigned int buflen; 509 void *buf; 510 int off; 511 512 buf = virtqueue_get_buf(rq->vq, &buflen); 513 if (unlikely(!buf)) 514 goto err_buf; 515 516 p = virt_to_head_page(buf); 517 off = buf - page_address(p); 518 519 /* guard against a misconfigured or uncooperative backend that 520 * is sending packet larger than the MTU. 521 */ 522 if ((page_off + buflen + tailroom) > PAGE_SIZE) { 523 put_page(p); 524 goto err_buf; 525 } 526 527 memcpy(page_address(page) + page_off, 528 page_address(p) + off, buflen); 529 page_off += buflen; 530 put_page(p); 531 } 532 533 /* Headroom does not contribute to packet length */ 534 *len = page_off - VIRTIO_XDP_HEADROOM; 535 return page; 536 err_buf: 537 __free_pages(page, 0); 538 return NULL; 539 } 540 541 static struct sk_buff *receive_small(struct net_device *dev, 542 struct virtnet_info *vi, 543 struct receive_queue *rq, 544 void *buf, void *ctx, 545 unsigned int len, 546 bool *xdp_xmit) 547 { 548 struct sk_buff *skb; 549 struct bpf_prog *xdp_prog; 550 unsigned int xdp_headroom = (unsigned long)ctx; 551 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom; 552 unsigned int headroom = vi->hdr_len + header_offset; 553 unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) + 554 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 555 struct page *page = virt_to_head_page(buf); 556 unsigned int delta = 0; 557 struct page *xdp_page; 558 bool sent; 559 int err; 560 561 len -= vi->hdr_len; 562 563 rcu_read_lock(); 564 xdp_prog = rcu_dereference(rq->xdp_prog); 565 if (xdp_prog) { 566 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset; 567 struct xdp_buff xdp; 568 void *orig_data; 569 u32 act; 570 571 if (unlikely(hdr->hdr.gso_type)) 572 goto err_xdp; 573 574 if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) { 575 int offset = buf - page_address(page) + header_offset; 576 unsigned int tlen = len + vi->hdr_len; 577 u16 num_buf = 1; 578 579 xdp_headroom = virtnet_get_headroom(vi); 580 header_offset = VIRTNET_RX_PAD + xdp_headroom; 581 headroom = vi->hdr_len + header_offset; 582 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) + 583 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 584 xdp_page = xdp_linearize_page(rq, &num_buf, page, 585 offset, header_offset, 586 &tlen); 587 if (!xdp_page) 588 goto err_xdp; 589 590 buf = page_address(xdp_page); 591 put_page(page); 592 page = xdp_page; 593 } 594 595 xdp.data_hard_start = buf + VIRTNET_RX_PAD + vi->hdr_len; 596 xdp.data = xdp.data_hard_start + xdp_headroom; 597 xdp_set_data_meta_invalid(&xdp); 598 xdp.data_end = xdp.data + len; 599 xdp.rxq = &rq->xdp_rxq; 600 orig_data = xdp.data; 601 act = bpf_prog_run_xdp(xdp_prog, &xdp); 602 603 switch (act) { 604 case XDP_PASS: 605 /* Recalculate length in case bpf program changed it */ 606 delta = orig_data - xdp.data; 607 break; 608 case XDP_TX: 609 sent = __virtnet_xdp_xmit(vi, &xdp); 610 if (unlikely(!sent)) { 611 trace_xdp_exception(vi->dev, xdp_prog, act); 612 goto err_xdp; 613 } 614 *xdp_xmit = true; 615 rcu_read_unlock(); 616 goto xdp_xmit; 617 case XDP_REDIRECT: 618 err = xdp_do_redirect(dev, &xdp, xdp_prog); 619 if (err) 620 goto err_xdp; 621 *xdp_xmit = true; 622 rcu_read_unlock(); 623 goto xdp_xmit; 624 default: 625 bpf_warn_invalid_xdp_action(act); 626 case XDP_ABORTED: 627 trace_xdp_exception(vi->dev, xdp_prog, act); 628 case XDP_DROP: 629 goto err_xdp; 630 } 631 } 632 rcu_read_unlock(); 633 634 skb = build_skb(buf, buflen); 635 if (!skb) { 636 put_page(page); 637 goto err; 638 } 639 skb_reserve(skb, headroom - delta); 640 skb_put(skb, len + delta); 641 if (!delta) { 642 buf += header_offset; 643 memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len); 644 } /* keep zeroed vnet hdr since packet was changed by bpf */ 645 646 err: 647 return skb; 648 649 err_xdp: 650 rcu_read_unlock(); 651 dev->stats.rx_dropped++; 652 put_page(page); 653 xdp_xmit: 654 return NULL; 655 } 656 657 static struct sk_buff *receive_big(struct net_device *dev, 658 struct virtnet_info *vi, 659 struct receive_queue *rq, 660 void *buf, 661 unsigned int len) 662 { 663 struct page *page = buf; 664 struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE); 665 666 if (unlikely(!skb)) 667 goto err; 668 669 return skb; 670 671 err: 672 dev->stats.rx_dropped++; 673 give_pages(rq, page); 674 return NULL; 675 } 676 677 static struct sk_buff *receive_mergeable(struct net_device *dev, 678 struct virtnet_info *vi, 679 struct receive_queue *rq, 680 void *buf, 681 void *ctx, 682 unsigned int len, 683 bool *xdp_xmit) 684 { 685 struct virtio_net_hdr_mrg_rxbuf *hdr = buf; 686 u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers); 687 struct page *page = virt_to_head_page(buf); 688 int offset = buf - page_address(page); 689 struct sk_buff *head_skb, *curr_skb; 690 struct bpf_prog *xdp_prog; 691 unsigned int truesize; 692 unsigned int headroom = mergeable_ctx_to_headroom(ctx); 693 bool sent; 694 int err; 695 696 head_skb = NULL; 697 698 rcu_read_lock(); 699 xdp_prog = rcu_dereference(rq->xdp_prog); 700 if (xdp_prog) { 701 struct page *xdp_page; 702 struct xdp_buff xdp; 703 void *data; 704 u32 act; 705 706 /* This happens when rx buffer size is underestimated 707 * or headroom is not enough because of the buffer 708 * was refilled before XDP is set. This should only 709 * happen for the first several packets, so we don't 710 * care much about its performance. 711 */ 712 if (unlikely(num_buf > 1 || 713 headroom < virtnet_get_headroom(vi))) { 714 /* linearize data for XDP */ 715 xdp_page = xdp_linearize_page(rq, &num_buf, 716 page, offset, 717 VIRTIO_XDP_HEADROOM, 718 &len); 719 if (!xdp_page) 720 goto err_xdp; 721 offset = VIRTIO_XDP_HEADROOM; 722 } else { 723 xdp_page = page; 724 } 725 726 /* Transient failure which in theory could occur if 727 * in-flight packets from before XDP was enabled reach 728 * the receive path after XDP is loaded. In practice I 729 * was not able to create this condition. 730 */ 731 if (unlikely(hdr->hdr.gso_type)) 732 goto err_xdp; 733 734 /* Allow consuming headroom but reserve enough space to push 735 * the descriptor on if we get an XDP_TX return code. 736 */ 737 data = page_address(xdp_page) + offset; 738 xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len; 739 xdp.data = data + vi->hdr_len; 740 xdp_set_data_meta_invalid(&xdp); 741 xdp.data_end = xdp.data + (len - vi->hdr_len); 742 xdp.rxq = &rq->xdp_rxq; 743 744 act = bpf_prog_run_xdp(xdp_prog, &xdp); 745 746 switch (act) { 747 case XDP_PASS: 748 /* recalculate offset to account for any header 749 * adjustments. Note other cases do not build an 750 * skb and avoid using offset 751 */ 752 offset = xdp.data - 753 page_address(xdp_page) - vi->hdr_len; 754 755 /* We can only create skb based on xdp_page. */ 756 if (unlikely(xdp_page != page)) { 757 rcu_read_unlock(); 758 put_page(page); 759 head_skb = page_to_skb(vi, rq, xdp_page, 760 offset, len, PAGE_SIZE); 761 return head_skb; 762 } 763 break; 764 case XDP_TX: 765 sent = __virtnet_xdp_xmit(vi, &xdp); 766 if (unlikely(!sent)) { 767 trace_xdp_exception(vi->dev, xdp_prog, act); 768 if (unlikely(xdp_page != page)) 769 put_page(xdp_page); 770 goto err_xdp; 771 } 772 *xdp_xmit = true; 773 if (unlikely(xdp_page != page)) 774 goto err_xdp; 775 rcu_read_unlock(); 776 goto xdp_xmit; 777 case XDP_REDIRECT: 778 err = xdp_do_redirect(dev, &xdp, xdp_prog); 779 if (err) { 780 if (unlikely(xdp_page != page)) 781 put_page(xdp_page); 782 goto err_xdp; 783 } 784 *xdp_xmit = true; 785 if (unlikely(xdp_page != page)) 786 goto err_xdp; 787 rcu_read_unlock(); 788 goto xdp_xmit; 789 default: 790 bpf_warn_invalid_xdp_action(act); 791 case XDP_ABORTED: 792 trace_xdp_exception(vi->dev, xdp_prog, act); 793 case XDP_DROP: 794 if (unlikely(xdp_page != page)) 795 __free_pages(xdp_page, 0); 796 goto err_xdp; 797 } 798 } 799 rcu_read_unlock(); 800 801 truesize = mergeable_ctx_to_truesize(ctx); 802 if (unlikely(len > truesize)) { 803 pr_debug("%s: rx error: len %u exceeds truesize %lu\n", 804 dev->name, len, (unsigned long)ctx); 805 dev->stats.rx_length_errors++; 806 goto err_skb; 807 } 808 809 head_skb = page_to_skb(vi, rq, page, offset, len, truesize); 810 curr_skb = head_skb; 811 812 if (unlikely(!curr_skb)) 813 goto err_skb; 814 while (--num_buf) { 815 int num_skb_frags; 816 817 buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx); 818 if (unlikely(!buf)) { 819 pr_debug("%s: rx error: %d buffers out of %d missing\n", 820 dev->name, num_buf, 821 virtio16_to_cpu(vi->vdev, 822 hdr->num_buffers)); 823 dev->stats.rx_length_errors++; 824 goto err_buf; 825 } 826 827 page = virt_to_head_page(buf); 828 829 truesize = mergeable_ctx_to_truesize(ctx); 830 if (unlikely(len > truesize)) { 831 pr_debug("%s: rx error: len %u exceeds truesize %lu\n", 832 dev->name, len, (unsigned long)ctx); 833 dev->stats.rx_length_errors++; 834 goto err_skb; 835 } 836 837 num_skb_frags = skb_shinfo(curr_skb)->nr_frags; 838 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) { 839 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC); 840 841 if (unlikely(!nskb)) 842 goto err_skb; 843 if (curr_skb == head_skb) 844 skb_shinfo(curr_skb)->frag_list = nskb; 845 else 846 curr_skb->next = nskb; 847 curr_skb = nskb; 848 head_skb->truesize += nskb->truesize; 849 num_skb_frags = 0; 850 } 851 if (curr_skb != head_skb) { 852 head_skb->data_len += len; 853 head_skb->len += len; 854 head_skb->truesize += truesize; 855 } 856 offset = buf - page_address(page); 857 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) { 858 put_page(page); 859 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1, 860 len, truesize); 861 } else { 862 skb_add_rx_frag(curr_skb, num_skb_frags, page, 863 offset, len, truesize); 864 } 865 } 866 867 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len); 868 return head_skb; 869 870 err_xdp: 871 rcu_read_unlock(); 872 err_skb: 873 put_page(page); 874 while (--num_buf) { 875 buf = virtqueue_get_buf(rq->vq, &len); 876 if (unlikely(!buf)) { 877 pr_debug("%s: rx error: %d buffers missing\n", 878 dev->name, num_buf); 879 dev->stats.rx_length_errors++; 880 break; 881 } 882 page = virt_to_head_page(buf); 883 put_page(page); 884 } 885 err_buf: 886 dev->stats.rx_dropped++; 887 dev_kfree_skb(head_skb); 888 xdp_xmit: 889 return NULL; 890 } 891 892 static int receive_buf(struct virtnet_info *vi, struct receive_queue *rq, 893 void *buf, unsigned int len, void **ctx, bool *xdp_xmit) 894 { 895 struct net_device *dev = vi->dev; 896 struct sk_buff *skb; 897 struct virtio_net_hdr_mrg_rxbuf *hdr; 898 int ret; 899 900 if (unlikely(len < vi->hdr_len + ETH_HLEN)) { 901 pr_debug("%s: short packet %i\n", dev->name, len); 902 dev->stats.rx_length_errors++; 903 if (vi->mergeable_rx_bufs) { 904 put_page(virt_to_head_page(buf)); 905 } else if (vi->big_packets) { 906 give_pages(rq, buf); 907 } else { 908 put_page(virt_to_head_page(buf)); 909 } 910 return 0; 911 } 912 913 if (vi->mergeable_rx_bufs) 914 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit); 915 else if (vi->big_packets) 916 skb = receive_big(dev, vi, rq, buf, len); 917 else 918 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit); 919 920 if (unlikely(!skb)) 921 return 0; 922 923 hdr = skb_vnet_hdr(skb); 924 925 ret = skb->len; 926 927 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) 928 skb->ip_summed = CHECKSUM_UNNECESSARY; 929 930 if (virtio_net_hdr_to_skb(skb, &hdr->hdr, 931 virtio_is_little_endian(vi->vdev))) { 932 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n", 933 dev->name, hdr->hdr.gso_type, 934 hdr->hdr.gso_size); 935 goto frame_err; 936 } 937 938 skb->protocol = eth_type_trans(skb, dev); 939 pr_debug("Receiving skb proto 0x%04x len %i type %i\n", 940 ntohs(skb->protocol), skb->len, skb->pkt_type); 941 942 napi_gro_receive(&rq->napi, skb); 943 return ret; 944 945 frame_err: 946 dev->stats.rx_frame_errors++; 947 dev_kfree_skb(skb); 948 return 0; 949 } 950 951 /* Unlike mergeable buffers, all buffers are allocated to the 952 * same size, except for the headroom. For this reason we do 953 * not need to use mergeable_len_to_ctx here - it is enough 954 * to store the headroom as the context ignoring the truesize. 955 */ 956 static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq, 957 gfp_t gfp) 958 { 959 struct page_frag *alloc_frag = &rq->alloc_frag; 960 char *buf; 961 unsigned int xdp_headroom = virtnet_get_headroom(vi); 962 void *ctx = (void *)(unsigned long)xdp_headroom; 963 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom; 964 int err; 965 966 len = SKB_DATA_ALIGN(len) + 967 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 968 if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp))) 969 return -ENOMEM; 970 971 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 972 get_page(alloc_frag->page); 973 alloc_frag->offset += len; 974 sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom, 975 vi->hdr_len + GOOD_PACKET_LEN); 976 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp); 977 if (err < 0) 978 put_page(virt_to_head_page(buf)); 979 return err; 980 } 981 982 static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq, 983 gfp_t gfp) 984 { 985 struct page *first, *list = NULL; 986 char *p; 987 int i, err, offset; 988 989 sg_init_table(rq->sg, MAX_SKB_FRAGS + 2); 990 991 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */ 992 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) { 993 first = get_a_page(rq, gfp); 994 if (!first) { 995 if (list) 996 give_pages(rq, list); 997 return -ENOMEM; 998 } 999 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE); 1000 1001 /* chain new page in list head to match sg */ 1002 first->private = (unsigned long)list; 1003 list = first; 1004 } 1005 1006 first = get_a_page(rq, gfp); 1007 if (!first) { 1008 give_pages(rq, list); 1009 return -ENOMEM; 1010 } 1011 p = page_address(first); 1012 1013 /* rq->sg[0], rq->sg[1] share the same page */ 1014 /* a separated rq->sg[0] for header - required in case !any_header_sg */ 1015 sg_set_buf(&rq->sg[0], p, vi->hdr_len); 1016 1017 /* rq->sg[1] for data packet, from offset */ 1018 offset = sizeof(struct padded_vnet_hdr); 1019 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset); 1020 1021 /* chain first in list head */ 1022 first->private = (unsigned long)list; 1023 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2, 1024 first, gfp); 1025 if (err < 0) 1026 give_pages(rq, first); 1027 1028 return err; 1029 } 1030 1031 static unsigned int get_mergeable_buf_len(struct receive_queue *rq, 1032 struct ewma_pkt_len *avg_pkt_len, 1033 unsigned int room) 1034 { 1035 const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 1036 unsigned int len; 1037 1038 if (room) 1039 return PAGE_SIZE - room; 1040 1041 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len), 1042 rq->min_buf_len, PAGE_SIZE - hdr_len); 1043 1044 return ALIGN(len, L1_CACHE_BYTES); 1045 } 1046 1047 static int add_recvbuf_mergeable(struct virtnet_info *vi, 1048 struct receive_queue *rq, gfp_t gfp) 1049 { 1050 struct page_frag *alloc_frag = &rq->alloc_frag; 1051 unsigned int headroom = virtnet_get_headroom(vi); 1052 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 1053 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom); 1054 char *buf; 1055 void *ctx; 1056 int err; 1057 unsigned int len, hole; 1058 1059 /* Extra tailroom is needed to satisfy XDP's assumption. This 1060 * means rx frags coalescing won't work, but consider we've 1061 * disabled GSO for XDP, it won't be a big issue. 1062 */ 1063 len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room); 1064 if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp))) 1065 return -ENOMEM; 1066 1067 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 1068 buf += headroom; /* advance address leaving hole at front of pkt */ 1069 get_page(alloc_frag->page); 1070 alloc_frag->offset += len + room; 1071 hole = alloc_frag->size - alloc_frag->offset; 1072 if (hole < len + room) { 1073 /* To avoid internal fragmentation, if there is very likely not 1074 * enough space for another buffer, add the remaining space to 1075 * the current buffer. 1076 */ 1077 len += hole; 1078 alloc_frag->offset += hole; 1079 } 1080 1081 sg_init_one(rq->sg, buf, len); 1082 ctx = mergeable_len_to_ctx(len, headroom); 1083 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp); 1084 if (err < 0) 1085 put_page(virt_to_head_page(buf)); 1086 1087 return err; 1088 } 1089 1090 /* 1091 * Returns false if we couldn't fill entirely (OOM). 1092 * 1093 * Normally run in the receive path, but can also be run from ndo_open 1094 * before we're receiving packets, or from refill_work which is 1095 * careful to disable receiving (using napi_disable). 1096 */ 1097 static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq, 1098 gfp_t gfp) 1099 { 1100 int err; 1101 bool oom; 1102 1103 do { 1104 if (vi->mergeable_rx_bufs) 1105 err = add_recvbuf_mergeable(vi, rq, gfp); 1106 else if (vi->big_packets) 1107 err = add_recvbuf_big(vi, rq, gfp); 1108 else 1109 err = add_recvbuf_small(vi, rq, gfp); 1110 1111 oom = err == -ENOMEM; 1112 if (err) 1113 break; 1114 } while (rq->vq->num_free); 1115 virtqueue_kick(rq->vq); 1116 return !oom; 1117 } 1118 1119 static void skb_recv_done(struct virtqueue *rvq) 1120 { 1121 struct virtnet_info *vi = rvq->vdev->priv; 1122 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)]; 1123 1124 virtqueue_napi_schedule(&rq->napi, rvq); 1125 } 1126 1127 static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi) 1128 { 1129 napi_enable(napi); 1130 1131 /* If all buffers were filled by other side before we napi_enabled, we 1132 * won't get another interrupt, so process any outstanding packets now. 1133 * Call local_bh_enable after to trigger softIRQ processing. 1134 */ 1135 local_bh_disable(); 1136 virtqueue_napi_schedule(napi, vq); 1137 local_bh_enable(); 1138 } 1139 1140 static void virtnet_napi_tx_enable(struct virtnet_info *vi, 1141 struct virtqueue *vq, 1142 struct napi_struct *napi) 1143 { 1144 if (!napi->weight) 1145 return; 1146 1147 /* Tx napi touches cachelines on the cpu handling tx interrupts. Only 1148 * enable the feature if this is likely affine with the transmit path. 1149 */ 1150 if (!vi->affinity_hint_set) { 1151 napi->weight = 0; 1152 return; 1153 } 1154 1155 return virtnet_napi_enable(vq, napi); 1156 } 1157 1158 static void virtnet_napi_tx_disable(struct napi_struct *napi) 1159 { 1160 if (napi->weight) 1161 napi_disable(napi); 1162 } 1163 1164 static void refill_work(struct work_struct *work) 1165 { 1166 struct virtnet_info *vi = 1167 container_of(work, struct virtnet_info, refill.work); 1168 bool still_empty; 1169 int i; 1170 1171 for (i = 0; i < vi->curr_queue_pairs; i++) { 1172 struct receive_queue *rq = &vi->rq[i]; 1173 1174 napi_disable(&rq->napi); 1175 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL); 1176 virtnet_napi_enable(rq->vq, &rq->napi); 1177 1178 /* In theory, this can happen: if we don't get any buffers in 1179 * we will *never* try to fill again. 1180 */ 1181 if (still_empty) 1182 schedule_delayed_work(&vi->refill, HZ/2); 1183 } 1184 } 1185 1186 static int virtnet_receive(struct receive_queue *rq, int budget, bool *xdp_xmit) 1187 { 1188 struct virtnet_info *vi = rq->vq->vdev->priv; 1189 unsigned int len, received = 0, bytes = 0; 1190 void *buf; 1191 1192 if (!vi->big_packets || vi->mergeable_rx_bufs) { 1193 void *ctx; 1194 1195 while (received < budget && 1196 (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) { 1197 bytes += receive_buf(vi, rq, buf, len, ctx, xdp_xmit); 1198 received++; 1199 } 1200 } else { 1201 while (received < budget && 1202 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) { 1203 bytes += receive_buf(vi, rq, buf, len, NULL, xdp_xmit); 1204 received++; 1205 } 1206 } 1207 1208 if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) { 1209 if (!try_fill_recv(vi, rq, GFP_ATOMIC)) 1210 schedule_delayed_work(&vi->refill, 0); 1211 } 1212 1213 u64_stats_update_begin(&rq->stats.syncp); 1214 rq->stats.bytes += bytes; 1215 rq->stats.packets += received; 1216 u64_stats_update_end(&rq->stats.syncp); 1217 1218 return received; 1219 } 1220 1221 static void free_old_xmit_skbs(struct send_queue *sq) 1222 { 1223 struct sk_buff *skb; 1224 unsigned int len; 1225 unsigned int packets = 0; 1226 unsigned int bytes = 0; 1227 1228 while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) { 1229 pr_debug("Sent skb %p\n", skb); 1230 1231 bytes += skb->len; 1232 packets++; 1233 1234 dev_consume_skb_any(skb); 1235 } 1236 1237 /* Avoid overhead when no packets have been processed 1238 * happens when called speculatively from start_xmit. 1239 */ 1240 if (!packets) 1241 return; 1242 1243 u64_stats_update_begin(&sq->stats.syncp); 1244 sq->stats.bytes += bytes; 1245 sq->stats.packets += packets; 1246 u64_stats_update_end(&sq->stats.syncp); 1247 } 1248 1249 static void virtnet_poll_cleantx(struct receive_queue *rq) 1250 { 1251 struct virtnet_info *vi = rq->vq->vdev->priv; 1252 unsigned int index = vq2rxq(rq->vq); 1253 struct send_queue *sq = &vi->sq[index]; 1254 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index); 1255 1256 if (!sq->napi.weight) 1257 return; 1258 1259 if (__netif_tx_trylock(txq)) { 1260 free_old_xmit_skbs(sq); 1261 __netif_tx_unlock(txq); 1262 } 1263 1264 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) 1265 netif_tx_wake_queue(txq); 1266 } 1267 1268 static int virtnet_poll(struct napi_struct *napi, int budget) 1269 { 1270 struct receive_queue *rq = 1271 container_of(napi, struct receive_queue, napi); 1272 struct virtnet_info *vi = rq->vq->vdev->priv; 1273 struct send_queue *sq; 1274 unsigned int received, qp; 1275 bool xdp_xmit = false; 1276 1277 virtnet_poll_cleantx(rq); 1278 1279 received = virtnet_receive(rq, budget, &xdp_xmit); 1280 1281 /* Out of packets? */ 1282 if (received < budget) 1283 virtqueue_napi_complete(napi, rq->vq, received); 1284 1285 if (xdp_xmit) { 1286 qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + 1287 smp_processor_id(); 1288 sq = &vi->sq[qp]; 1289 virtqueue_kick(sq->vq); 1290 xdp_do_flush_map(); 1291 } 1292 1293 return received; 1294 } 1295 1296 static int virtnet_open(struct net_device *dev) 1297 { 1298 struct virtnet_info *vi = netdev_priv(dev); 1299 int i, err; 1300 1301 for (i = 0; i < vi->max_queue_pairs; i++) { 1302 if (i < vi->curr_queue_pairs) 1303 /* Make sure we have some buffers: if oom use wq. */ 1304 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 1305 schedule_delayed_work(&vi->refill, 0); 1306 1307 err = xdp_rxq_info_reg(&vi->rq[i].xdp_rxq, dev, i); 1308 if (err < 0) 1309 return err; 1310 1311 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 1312 virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi); 1313 } 1314 1315 return 0; 1316 } 1317 1318 static int virtnet_poll_tx(struct napi_struct *napi, int budget) 1319 { 1320 struct send_queue *sq = container_of(napi, struct send_queue, napi); 1321 struct virtnet_info *vi = sq->vq->vdev->priv; 1322 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, vq2txq(sq->vq)); 1323 1324 __netif_tx_lock(txq, raw_smp_processor_id()); 1325 free_old_xmit_skbs(sq); 1326 __netif_tx_unlock(txq); 1327 1328 virtqueue_napi_complete(napi, sq->vq, 0); 1329 1330 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) 1331 netif_tx_wake_queue(txq); 1332 1333 return 0; 1334 } 1335 1336 static int xmit_skb(struct send_queue *sq, struct sk_buff *skb) 1337 { 1338 struct virtio_net_hdr_mrg_rxbuf *hdr; 1339 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; 1340 struct virtnet_info *vi = sq->vq->vdev->priv; 1341 int num_sg; 1342 unsigned hdr_len = vi->hdr_len; 1343 bool can_push; 1344 1345 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); 1346 1347 can_push = vi->any_header_sg && 1348 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) && 1349 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len; 1350 /* Even if we can, don't push here yet as this would skew 1351 * csum_start offset below. */ 1352 if (can_push) 1353 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len); 1354 else 1355 hdr = skb_vnet_hdr(skb); 1356 1357 if (virtio_net_hdr_from_skb(skb, &hdr->hdr, 1358 virtio_is_little_endian(vi->vdev), false)) 1359 BUG(); 1360 1361 if (vi->mergeable_rx_bufs) 1362 hdr->num_buffers = 0; 1363 1364 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2)); 1365 if (can_push) { 1366 __skb_push(skb, hdr_len); 1367 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len); 1368 if (unlikely(num_sg < 0)) 1369 return num_sg; 1370 /* Pull header back to avoid skew in tx bytes calculations. */ 1371 __skb_pull(skb, hdr_len); 1372 } else { 1373 sg_set_buf(sq->sg, hdr, hdr_len); 1374 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len); 1375 if (unlikely(num_sg < 0)) 1376 return num_sg; 1377 num_sg++; 1378 } 1379 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC); 1380 } 1381 1382 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) 1383 { 1384 struct virtnet_info *vi = netdev_priv(dev); 1385 int qnum = skb_get_queue_mapping(skb); 1386 struct send_queue *sq = &vi->sq[qnum]; 1387 int err; 1388 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum); 1389 bool kick = !skb->xmit_more; 1390 bool use_napi = sq->napi.weight; 1391 1392 /* Free up any pending old buffers before queueing new ones. */ 1393 free_old_xmit_skbs(sq); 1394 1395 if (use_napi && kick) 1396 virtqueue_enable_cb_delayed(sq->vq); 1397 1398 /* timestamp packet in software */ 1399 skb_tx_timestamp(skb); 1400 1401 /* Try to transmit */ 1402 err = xmit_skb(sq, skb); 1403 1404 /* This should not happen! */ 1405 if (unlikely(err)) { 1406 dev->stats.tx_fifo_errors++; 1407 if (net_ratelimit()) 1408 dev_warn(&dev->dev, 1409 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err); 1410 dev->stats.tx_dropped++; 1411 dev_kfree_skb_any(skb); 1412 return NETDEV_TX_OK; 1413 } 1414 1415 /* Don't wait up for transmitted skbs to be freed. */ 1416 if (!use_napi) { 1417 skb_orphan(skb); 1418 nf_reset(skb); 1419 } 1420 1421 /* If running out of space, stop queue to avoid getting packets that we 1422 * are then unable to transmit. 1423 * An alternative would be to force queuing layer to requeue the skb by 1424 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be 1425 * returned in a normal path of operation: it means that driver is not 1426 * maintaining the TX queue stop/start state properly, and causes 1427 * the stack to do a non-trivial amount of useless work. 1428 * Since most packets only take 1 or 2 ring slots, stopping the queue 1429 * early means 16 slots are typically wasted. 1430 */ 1431 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) { 1432 netif_stop_subqueue(dev, qnum); 1433 if (!use_napi && 1434 unlikely(!virtqueue_enable_cb_delayed(sq->vq))) { 1435 /* More just got used, free them then recheck. */ 1436 free_old_xmit_skbs(sq); 1437 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) { 1438 netif_start_subqueue(dev, qnum); 1439 virtqueue_disable_cb(sq->vq); 1440 } 1441 } 1442 } 1443 1444 if (kick || netif_xmit_stopped(txq)) 1445 virtqueue_kick(sq->vq); 1446 1447 return NETDEV_TX_OK; 1448 } 1449 1450 /* 1451 * Send command via the control virtqueue and check status. Commands 1452 * supported by the hypervisor, as indicated by feature bits, should 1453 * never fail unless improperly formatted. 1454 */ 1455 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, 1456 struct scatterlist *out) 1457 { 1458 struct scatterlist *sgs[4], hdr, stat; 1459 unsigned out_num = 0, tmp; 1460 1461 /* Caller should know better */ 1462 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)); 1463 1464 vi->ctrl_status = ~0; 1465 vi->ctrl_hdr.class = class; 1466 vi->ctrl_hdr.cmd = cmd; 1467 /* Add header */ 1468 sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr)); 1469 sgs[out_num++] = &hdr; 1470 1471 if (out) 1472 sgs[out_num++] = out; 1473 1474 /* Add return status. */ 1475 sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status)); 1476 sgs[out_num] = &stat; 1477 1478 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs)); 1479 virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC); 1480 1481 if (unlikely(!virtqueue_kick(vi->cvq))) 1482 return vi->ctrl_status == VIRTIO_NET_OK; 1483 1484 /* Spin for a response, the kick causes an ioport write, trapping 1485 * into the hypervisor, so the request should be handled immediately. 1486 */ 1487 while (!virtqueue_get_buf(vi->cvq, &tmp) && 1488 !virtqueue_is_broken(vi->cvq)) 1489 cpu_relax(); 1490 1491 return vi->ctrl_status == VIRTIO_NET_OK; 1492 } 1493 1494 static int virtnet_set_mac_address(struct net_device *dev, void *p) 1495 { 1496 struct virtnet_info *vi = netdev_priv(dev); 1497 struct virtio_device *vdev = vi->vdev; 1498 int ret; 1499 struct sockaddr *addr; 1500 struct scatterlist sg; 1501 1502 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL); 1503 if (!addr) 1504 return -ENOMEM; 1505 1506 ret = eth_prepare_mac_addr_change(dev, addr); 1507 if (ret) 1508 goto out; 1509 1510 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 1511 sg_init_one(&sg, addr->sa_data, dev->addr_len); 1512 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 1513 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) { 1514 dev_warn(&vdev->dev, 1515 "Failed to set mac address by vq command.\n"); 1516 ret = -EINVAL; 1517 goto out; 1518 } 1519 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) && 1520 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) { 1521 unsigned int i; 1522 1523 /* Naturally, this has an atomicity problem. */ 1524 for (i = 0; i < dev->addr_len; i++) 1525 virtio_cwrite8(vdev, 1526 offsetof(struct virtio_net_config, mac) + 1527 i, addr->sa_data[i]); 1528 } 1529 1530 eth_commit_mac_addr_change(dev, p); 1531 ret = 0; 1532 1533 out: 1534 kfree(addr); 1535 return ret; 1536 } 1537 1538 static void virtnet_stats(struct net_device *dev, 1539 struct rtnl_link_stats64 *tot) 1540 { 1541 struct virtnet_info *vi = netdev_priv(dev); 1542 unsigned int start; 1543 int i; 1544 1545 for (i = 0; i < vi->max_queue_pairs; i++) { 1546 u64 tpackets, tbytes, rpackets, rbytes; 1547 struct receive_queue *rq = &vi->rq[i]; 1548 struct send_queue *sq = &vi->sq[i]; 1549 1550 do { 1551 start = u64_stats_fetch_begin_irq(&sq->stats.syncp); 1552 tpackets = sq->stats.packets; 1553 tbytes = sq->stats.bytes; 1554 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start)); 1555 1556 do { 1557 start = u64_stats_fetch_begin_irq(&rq->stats.syncp); 1558 rpackets = rq->stats.packets; 1559 rbytes = rq->stats.bytes; 1560 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start)); 1561 1562 tot->rx_packets += rpackets; 1563 tot->tx_packets += tpackets; 1564 tot->rx_bytes += rbytes; 1565 tot->tx_bytes += tbytes; 1566 } 1567 1568 tot->tx_dropped = dev->stats.tx_dropped; 1569 tot->tx_fifo_errors = dev->stats.tx_fifo_errors; 1570 tot->rx_dropped = dev->stats.rx_dropped; 1571 tot->rx_length_errors = dev->stats.rx_length_errors; 1572 tot->rx_frame_errors = dev->stats.rx_frame_errors; 1573 } 1574 1575 #ifdef CONFIG_NET_POLL_CONTROLLER 1576 static void virtnet_netpoll(struct net_device *dev) 1577 { 1578 struct virtnet_info *vi = netdev_priv(dev); 1579 int i; 1580 1581 for (i = 0; i < vi->curr_queue_pairs; i++) 1582 napi_schedule(&vi->rq[i].napi); 1583 } 1584 #endif 1585 1586 static void virtnet_ack_link_announce(struct virtnet_info *vi) 1587 { 1588 rtnl_lock(); 1589 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE, 1590 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL)) 1591 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n"); 1592 rtnl_unlock(); 1593 } 1594 1595 static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 1596 { 1597 struct scatterlist sg; 1598 struct net_device *dev = vi->dev; 1599 1600 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ)) 1601 return 0; 1602 1603 vi->ctrl_mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs); 1604 sg_init_one(&sg, &vi->ctrl_mq, sizeof(vi->ctrl_mq)); 1605 1606 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, 1607 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) { 1608 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n", 1609 queue_pairs); 1610 return -EINVAL; 1611 } else { 1612 vi->curr_queue_pairs = queue_pairs; 1613 /* virtnet_open() will refill when device is going to up. */ 1614 if (dev->flags & IFF_UP) 1615 schedule_delayed_work(&vi->refill, 0); 1616 } 1617 1618 return 0; 1619 } 1620 1621 static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 1622 { 1623 int err; 1624 1625 rtnl_lock(); 1626 err = _virtnet_set_queues(vi, queue_pairs); 1627 rtnl_unlock(); 1628 return err; 1629 } 1630 1631 static int virtnet_close(struct net_device *dev) 1632 { 1633 struct virtnet_info *vi = netdev_priv(dev); 1634 int i; 1635 1636 /* Make sure refill_work doesn't re-enable napi! */ 1637 cancel_delayed_work_sync(&vi->refill); 1638 1639 for (i = 0; i < vi->max_queue_pairs; i++) { 1640 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq); 1641 napi_disable(&vi->rq[i].napi); 1642 virtnet_napi_tx_disable(&vi->sq[i].napi); 1643 } 1644 1645 return 0; 1646 } 1647 1648 static void virtnet_set_rx_mode(struct net_device *dev) 1649 { 1650 struct virtnet_info *vi = netdev_priv(dev); 1651 struct scatterlist sg[2]; 1652 struct virtio_net_ctrl_mac *mac_data; 1653 struct netdev_hw_addr *ha; 1654 int uc_count; 1655 int mc_count; 1656 void *buf; 1657 int i; 1658 1659 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */ 1660 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) 1661 return; 1662 1663 vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0); 1664 vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0); 1665 1666 sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc)); 1667 1668 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1669 VIRTIO_NET_CTRL_RX_PROMISC, sg)) 1670 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", 1671 vi->ctrl_promisc ? "en" : "dis"); 1672 1673 sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti)); 1674 1675 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1676 VIRTIO_NET_CTRL_RX_ALLMULTI, sg)) 1677 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", 1678 vi->ctrl_allmulti ? "en" : "dis"); 1679 1680 uc_count = netdev_uc_count(dev); 1681 mc_count = netdev_mc_count(dev); 1682 /* MAC filter - use one buffer for both lists */ 1683 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) + 1684 (2 * sizeof(mac_data->entries)), GFP_ATOMIC); 1685 mac_data = buf; 1686 if (!buf) 1687 return; 1688 1689 sg_init_table(sg, 2); 1690 1691 /* Store the unicast list and count in the front of the buffer */ 1692 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count); 1693 i = 0; 1694 netdev_for_each_uc_addr(ha, dev) 1695 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 1696 1697 sg_set_buf(&sg[0], mac_data, 1698 sizeof(mac_data->entries) + (uc_count * ETH_ALEN)); 1699 1700 /* multicast list and count fill the end */ 1701 mac_data = (void *)&mac_data->macs[uc_count][0]; 1702 1703 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count); 1704 i = 0; 1705 netdev_for_each_mc_addr(ha, dev) 1706 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 1707 1708 sg_set_buf(&sg[1], mac_data, 1709 sizeof(mac_data->entries) + (mc_count * ETH_ALEN)); 1710 1711 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 1712 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg)) 1713 dev_warn(&dev->dev, "Failed to set MAC filter table.\n"); 1714 1715 kfree(buf); 1716 } 1717 1718 static int virtnet_vlan_rx_add_vid(struct net_device *dev, 1719 __be16 proto, u16 vid) 1720 { 1721 struct virtnet_info *vi = netdev_priv(dev); 1722 struct scatterlist sg; 1723 1724 vi->ctrl_vid = vid; 1725 sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid)); 1726 1727 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 1728 VIRTIO_NET_CTRL_VLAN_ADD, &sg)) 1729 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); 1730 return 0; 1731 } 1732 1733 static int virtnet_vlan_rx_kill_vid(struct net_device *dev, 1734 __be16 proto, u16 vid) 1735 { 1736 struct virtnet_info *vi = netdev_priv(dev); 1737 struct scatterlist sg; 1738 1739 vi->ctrl_vid = vid; 1740 sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid)); 1741 1742 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 1743 VIRTIO_NET_CTRL_VLAN_DEL, &sg)) 1744 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); 1745 return 0; 1746 } 1747 1748 static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu) 1749 { 1750 int i; 1751 1752 if (vi->affinity_hint_set) { 1753 for (i = 0; i < vi->max_queue_pairs; i++) { 1754 virtqueue_set_affinity(vi->rq[i].vq, -1); 1755 virtqueue_set_affinity(vi->sq[i].vq, -1); 1756 } 1757 1758 vi->affinity_hint_set = false; 1759 } 1760 } 1761 1762 static void virtnet_set_affinity(struct virtnet_info *vi) 1763 { 1764 int i; 1765 int cpu; 1766 1767 /* In multiqueue mode, when the number of cpu is equal to the number of 1768 * queue pairs, we let the queue pairs to be private to one cpu by 1769 * setting the affinity hint to eliminate the contention. 1770 */ 1771 if (vi->curr_queue_pairs == 1 || 1772 vi->max_queue_pairs != num_online_cpus()) { 1773 virtnet_clean_affinity(vi, -1); 1774 return; 1775 } 1776 1777 i = 0; 1778 for_each_online_cpu(cpu) { 1779 virtqueue_set_affinity(vi->rq[i].vq, cpu); 1780 virtqueue_set_affinity(vi->sq[i].vq, cpu); 1781 netif_set_xps_queue(vi->dev, cpumask_of(cpu), i); 1782 i++; 1783 } 1784 1785 vi->affinity_hint_set = true; 1786 } 1787 1788 static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node) 1789 { 1790 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 1791 node); 1792 virtnet_set_affinity(vi); 1793 return 0; 1794 } 1795 1796 static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node) 1797 { 1798 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 1799 node_dead); 1800 virtnet_set_affinity(vi); 1801 return 0; 1802 } 1803 1804 static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node) 1805 { 1806 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 1807 node); 1808 1809 virtnet_clean_affinity(vi, cpu); 1810 return 0; 1811 } 1812 1813 static enum cpuhp_state virtionet_online; 1814 1815 static int virtnet_cpu_notif_add(struct virtnet_info *vi) 1816 { 1817 int ret; 1818 1819 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node); 1820 if (ret) 1821 return ret; 1822 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD, 1823 &vi->node_dead); 1824 if (!ret) 1825 return ret; 1826 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 1827 return ret; 1828 } 1829 1830 static void virtnet_cpu_notif_remove(struct virtnet_info *vi) 1831 { 1832 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 1833 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD, 1834 &vi->node_dead); 1835 } 1836 1837 static void virtnet_get_ringparam(struct net_device *dev, 1838 struct ethtool_ringparam *ring) 1839 { 1840 struct virtnet_info *vi = netdev_priv(dev); 1841 1842 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq); 1843 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq); 1844 ring->rx_pending = ring->rx_max_pending; 1845 ring->tx_pending = ring->tx_max_pending; 1846 } 1847 1848 1849 static void virtnet_get_drvinfo(struct net_device *dev, 1850 struct ethtool_drvinfo *info) 1851 { 1852 struct virtnet_info *vi = netdev_priv(dev); 1853 struct virtio_device *vdev = vi->vdev; 1854 1855 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); 1856 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version)); 1857 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info)); 1858 1859 } 1860 1861 /* TODO: Eliminate OOO packets during switching */ 1862 static int virtnet_set_channels(struct net_device *dev, 1863 struct ethtool_channels *channels) 1864 { 1865 struct virtnet_info *vi = netdev_priv(dev); 1866 u16 queue_pairs = channels->combined_count; 1867 int err; 1868 1869 /* We don't support separate rx/tx channels. 1870 * We don't allow setting 'other' channels. 1871 */ 1872 if (channels->rx_count || channels->tx_count || channels->other_count) 1873 return -EINVAL; 1874 1875 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0) 1876 return -EINVAL; 1877 1878 /* For now we don't support modifying channels while XDP is loaded 1879 * also when XDP is loaded all RX queues have XDP programs so we only 1880 * need to check a single RX queue. 1881 */ 1882 if (vi->rq[0].xdp_prog) 1883 return -EINVAL; 1884 1885 get_online_cpus(); 1886 err = _virtnet_set_queues(vi, queue_pairs); 1887 if (!err) { 1888 netif_set_real_num_tx_queues(dev, queue_pairs); 1889 netif_set_real_num_rx_queues(dev, queue_pairs); 1890 1891 virtnet_set_affinity(vi); 1892 } 1893 put_online_cpus(); 1894 1895 return err; 1896 } 1897 1898 static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data) 1899 { 1900 struct virtnet_info *vi = netdev_priv(dev); 1901 char *p = (char *)data; 1902 unsigned int i, j; 1903 1904 switch (stringset) { 1905 case ETH_SS_STATS: 1906 for (i = 0; i < vi->curr_queue_pairs; i++) { 1907 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) { 1908 snprintf(p, ETH_GSTRING_LEN, "rx_queue_%u_%s", 1909 i, virtnet_rq_stats_desc[j].desc); 1910 p += ETH_GSTRING_LEN; 1911 } 1912 } 1913 1914 for (i = 0; i < vi->curr_queue_pairs; i++) { 1915 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) { 1916 snprintf(p, ETH_GSTRING_LEN, "tx_queue_%u_%s", 1917 i, virtnet_sq_stats_desc[j].desc); 1918 p += ETH_GSTRING_LEN; 1919 } 1920 } 1921 break; 1922 } 1923 } 1924 1925 static int virtnet_get_sset_count(struct net_device *dev, int sset) 1926 { 1927 struct virtnet_info *vi = netdev_priv(dev); 1928 1929 switch (sset) { 1930 case ETH_SS_STATS: 1931 return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN + 1932 VIRTNET_SQ_STATS_LEN); 1933 default: 1934 return -EOPNOTSUPP; 1935 } 1936 } 1937 1938 static void virtnet_get_ethtool_stats(struct net_device *dev, 1939 struct ethtool_stats *stats, u64 *data) 1940 { 1941 struct virtnet_info *vi = netdev_priv(dev); 1942 unsigned int idx = 0, start, i, j; 1943 const u8 *stats_base; 1944 size_t offset; 1945 1946 for (i = 0; i < vi->curr_queue_pairs; i++) { 1947 struct receive_queue *rq = &vi->rq[i]; 1948 1949 stats_base = (u8 *)&rq->stats; 1950 do { 1951 start = u64_stats_fetch_begin_irq(&rq->stats.syncp); 1952 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) { 1953 offset = virtnet_rq_stats_desc[j].offset; 1954 data[idx + j] = *(u64 *)(stats_base + offset); 1955 } 1956 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start)); 1957 idx += VIRTNET_RQ_STATS_LEN; 1958 } 1959 1960 for (i = 0; i < vi->curr_queue_pairs; i++) { 1961 struct send_queue *sq = &vi->sq[i]; 1962 1963 stats_base = (u8 *)&sq->stats; 1964 do { 1965 start = u64_stats_fetch_begin_irq(&sq->stats.syncp); 1966 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) { 1967 offset = virtnet_sq_stats_desc[j].offset; 1968 data[idx + j] = *(u64 *)(stats_base + offset); 1969 } 1970 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start)); 1971 idx += VIRTNET_SQ_STATS_LEN; 1972 } 1973 } 1974 1975 static void virtnet_get_channels(struct net_device *dev, 1976 struct ethtool_channels *channels) 1977 { 1978 struct virtnet_info *vi = netdev_priv(dev); 1979 1980 channels->combined_count = vi->curr_queue_pairs; 1981 channels->max_combined = vi->max_queue_pairs; 1982 channels->max_other = 0; 1983 channels->rx_count = 0; 1984 channels->tx_count = 0; 1985 channels->other_count = 0; 1986 } 1987 1988 /* Check if the user is trying to change anything besides speed/duplex */ 1989 static bool 1990 virtnet_validate_ethtool_cmd(const struct ethtool_link_ksettings *cmd) 1991 { 1992 struct ethtool_link_ksettings diff1 = *cmd; 1993 struct ethtool_link_ksettings diff2 = {}; 1994 1995 /* cmd is always set so we need to clear it, validate the port type 1996 * and also without autonegotiation we can ignore advertising 1997 */ 1998 diff1.base.speed = 0; 1999 diff2.base.port = PORT_OTHER; 2000 ethtool_link_ksettings_zero_link_mode(&diff1, advertising); 2001 diff1.base.duplex = 0; 2002 diff1.base.cmd = 0; 2003 diff1.base.link_mode_masks_nwords = 0; 2004 2005 return !memcmp(&diff1.base, &diff2.base, sizeof(diff1.base)) && 2006 bitmap_empty(diff1.link_modes.supported, 2007 __ETHTOOL_LINK_MODE_MASK_NBITS) && 2008 bitmap_empty(diff1.link_modes.advertising, 2009 __ETHTOOL_LINK_MODE_MASK_NBITS) && 2010 bitmap_empty(diff1.link_modes.lp_advertising, 2011 __ETHTOOL_LINK_MODE_MASK_NBITS); 2012 } 2013 2014 static int virtnet_set_link_ksettings(struct net_device *dev, 2015 const struct ethtool_link_ksettings *cmd) 2016 { 2017 struct virtnet_info *vi = netdev_priv(dev); 2018 u32 speed; 2019 2020 speed = cmd->base.speed; 2021 /* don't allow custom speed and duplex */ 2022 if (!ethtool_validate_speed(speed) || 2023 !ethtool_validate_duplex(cmd->base.duplex) || 2024 !virtnet_validate_ethtool_cmd(cmd)) 2025 return -EINVAL; 2026 vi->speed = speed; 2027 vi->duplex = cmd->base.duplex; 2028 2029 return 0; 2030 } 2031 2032 static int virtnet_get_link_ksettings(struct net_device *dev, 2033 struct ethtool_link_ksettings *cmd) 2034 { 2035 struct virtnet_info *vi = netdev_priv(dev); 2036 2037 cmd->base.speed = vi->speed; 2038 cmd->base.duplex = vi->duplex; 2039 cmd->base.port = PORT_OTHER; 2040 2041 return 0; 2042 } 2043 2044 static void virtnet_init_settings(struct net_device *dev) 2045 { 2046 struct virtnet_info *vi = netdev_priv(dev); 2047 2048 vi->speed = SPEED_UNKNOWN; 2049 vi->duplex = DUPLEX_UNKNOWN; 2050 } 2051 2052 static void virtnet_update_settings(struct virtnet_info *vi) 2053 { 2054 u32 speed; 2055 u8 duplex; 2056 2057 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX)) 2058 return; 2059 2060 speed = virtio_cread32(vi->vdev, offsetof(struct virtio_net_config, 2061 speed)); 2062 if (ethtool_validate_speed(speed)) 2063 vi->speed = speed; 2064 duplex = virtio_cread8(vi->vdev, offsetof(struct virtio_net_config, 2065 duplex)); 2066 if (ethtool_validate_duplex(duplex)) 2067 vi->duplex = duplex; 2068 } 2069 2070 static const struct ethtool_ops virtnet_ethtool_ops = { 2071 .get_drvinfo = virtnet_get_drvinfo, 2072 .get_link = ethtool_op_get_link, 2073 .get_ringparam = virtnet_get_ringparam, 2074 .get_strings = virtnet_get_strings, 2075 .get_sset_count = virtnet_get_sset_count, 2076 .get_ethtool_stats = virtnet_get_ethtool_stats, 2077 .set_channels = virtnet_set_channels, 2078 .get_channels = virtnet_get_channels, 2079 .get_ts_info = ethtool_op_get_ts_info, 2080 .get_link_ksettings = virtnet_get_link_ksettings, 2081 .set_link_ksettings = virtnet_set_link_ksettings, 2082 }; 2083 2084 static void virtnet_freeze_down(struct virtio_device *vdev) 2085 { 2086 struct virtnet_info *vi = vdev->priv; 2087 int i; 2088 2089 /* Make sure no work handler is accessing the device */ 2090 flush_work(&vi->config_work); 2091 2092 netif_device_detach(vi->dev); 2093 netif_tx_disable(vi->dev); 2094 cancel_delayed_work_sync(&vi->refill); 2095 2096 if (netif_running(vi->dev)) { 2097 for (i = 0; i < vi->max_queue_pairs; i++) { 2098 napi_disable(&vi->rq[i].napi); 2099 virtnet_napi_tx_disable(&vi->sq[i].napi); 2100 } 2101 } 2102 } 2103 2104 static int init_vqs(struct virtnet_info *vi); 2105 2106 static int virtnet_restore_up(struct virtio_device *vdev) 2107 { 2108 struct virtnet_info *vi = vdev->priv; 2109 int err, i; 2110 2111 err = init_vqs(vi); 2112 if (err) 2113 return err; 2114 2115 virtio_device_ready(vdev); 2116 2117 if (netif_running(vi->dev)) { 2118 for (i = 0; i < vi->curr_queue_pairs; i++) 2119 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 2120 schedule_delayed_work(&vi->refill, 0); 2121 2122 for (i = 0; i < vi->max_queue_pairs; i++) { 2123 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 2124 virtnet_napi_tx_enable(vi, vi->sq[i].vq, 2125 &vi->sq[i].napi); 2126 } 2127 } 2128 2129 netif_device_attach(vi->dev); 2130 return err; 2131 } 2132 2133 static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads) 2134 { 2135 struct scatterlist sg; 2136 vi->ctrl_offloads = cpu_to_virtio64(vi->vdev, offloads); 2137 2138 sg_init_one(&sg, &vi->ctrl_offloads, sizeof(vi->ctrl_offloads)); 2139 2140 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS, 2141 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) { 2142 dev_warn(&vi->dev->dev, "Fail to set guest offload. \n"); 2143 return -EINVAL; 2144 } 2145 2146 return 0; 2147 } 2148 2149 static int virtnet_clear_guest_offloads(struct virtnet_info *vi) 2150 { 2151 u64 offloads = 0; 2152 2153 if (!vi->guest_offloads) 2154 return 0; 2155 2156 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM)) 2157 offloads = 1ULL << VIRTIO_NET_F_GUEST_CSUM; 2158 2159 return virtnet_set_guest_offloads(vi, offloads); 2160 } 2161 2162 static int virtnet_restore_guest_offloads(struct virtnet_info *vi) 2163 { 2164 u64 offloads = vi->guest_offloads; 2165 2166 if (!vi->guest_offloads) 2167 return 0; 2168 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM)) 2169 offloads |= 1ULL << VIRTIO_NET_F_GUEST_CSUM; 2170 2171 return virtnet_set_guest_offloads(vi, offloads); 2172 } 2173 2174 static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog, 2175 struct netlink_ext_ack *extack) 2176 { 2177 unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr); 2178 struct virtnet_info *vi = netdev_priv(dev); 2179 struct bpf_prog *old_prog; 2180 u16 xdp_qp = 0, curr_qp; 2181 int i, err; 2182 2183 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) 2184 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) || 2185 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) || 2186 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) || 2187 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO))) { 2188 NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing LRO, disable LRO first"); 2189 return -EOPNOTSUPP; 2190 } 2191 2192 if (vi->mergeable_rx_bufs && !vi->any_header_sg) { 2193 NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required"); 2194 return -EINVAL; 2195 } 2196 2197 if (dev->mtu > max_sz) { 2198 NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP"); 2199 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz); 2200 return -EINVAL; 2201 } 2202 2203 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs; 2204 if (prog) 2205 xdp_qp = nr_cpu_ids; 2206 2207 /* XDP requires extra queues for XDP_TX */ 2208 if (curr_qp + xdp_qp > vi->max_queue_pairs) { 2209 NL_SET_ERR_MSG_MOD(extack, "Too few free TX rings available"); 2210 netdev_warn(dev, "request %i queues but max is %i\n", 2211 curr_qp + xdp_qp, vi->max_queue_pairs); 2212 return -ENOMEM; 2213 } 2214 2215 if (prog) { 2216 prog = bpf_prog_add(prog, vi->max_queue_pairs - 1); 2217 if (IS_ERR(prog)) 2218 return PTR_ERR(prog); 2219 } 2220 2221 /* Make sure NAPI is not using any XDP TX queues for RX. */ 2222 if (netif_running(dev)) 2223 for (i = 0; i < vi->max_queue_pairs; i++) 2224 napi_disable(&vi->rq[i].napi); 2225 2226 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp); 2227 err = _virtnet_set_queues(vi, curr_qp + xdp_qp); 2228 if (err) 2229 goto err; 2230 vi->xdp_queue_pairs = xdp_qp; 2231 2232 for (i = 0; i < vi->max_queue_pairs; i++) { 2233 old_prog = rtnl_dereference(vi->rq[i].xdp_prog); 2234 rcu_assign_pointer(vi->rq[i].xdp_prog, prog); 2235 if (i == 0) { 2236 if (!old_prog) 2237 virtnet_clear_guest_offloads(vi); 2238 if (!prog) 2239 virtnet_restore_guest_offloads(vi); 2240 } 2241 if (old_prog) 2242 bpf_prog_put(old_prog); 2243 if (netif_running(dev)) 2244 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 2245 } 2246 2247 return 0; 2248 2249 err: 2250 for (i = 0; i < vi->max_queue_pairs; i++) 2251 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 2252 if (prog) 2253 bpf_prog_sub(prog, vi->max_queue_pairs - 1); 2254 return err; 2255 } 2256 2257 static u32 virtnet_xdp_query(struct net_device *dev) 2258 { 2259 struct virtnet_info *vi = netdev_priv(dev); 2260 const struct bpf_prog *xdp_prog; 2261 int i; 2262 2263 for (i = 0; i < vi->max_queue_pairs; i++) { 2264 xdp_prog = rtnl_dereference(vi->rq[i].xdp_prog); 2265 if (xdp_prog) 2266 return xdp_prog->aux->id; 2267 } 2268 return 0; 2269 } 2270 2271 static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp) 2272 { 2273 switch (xdp->command) { 2274 case XDP_SETUP_PROG: 2275 return virtnet_xdp_set(dev, xdp->prog, xdp->extack); 2276 case XDP_QUERY_PROG: 2277 xdp->prog_id = virtnet_xdp_query(dev); 2278 xdp->prog_attached = !!xdp->prog_id; 2279 return 0; 2280 default: 2281 return -EINVAL; 2282 } 2283 } 2284 2285 static const struct net_device_ops virtnet_netdev = { 2286 .ndo_open = virtnet_open, 2287 .ndo_stop = virtnet_close, 2288 .ndo_start_xmit = start_xmit, 2289 .ndo_validate_addr = eth_validate_addr, 2290 .ndo_set_mac_address = virtnet_set_mac_address, 2291 .ndo_set_rx_mode = virtnet_set_rx_mode, 2292 .ndo_get_stats64 = virtnet_stats, 2293 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, 2294 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, 2295 #ifdef CONFIG_NET_POLL_CONTROLLER 2296 .ndo_poll_controller = virtnet_netpoll, 2297 #endif 2298 .ndo_bpf = virtnet_xdp, 2299 .ndo_xdp_xmit = virtnet_xdp_xmit, 2300 .ndo_xdp_flush = virtnet_xdp_flush, 2301 .ndo_features_check = passthru_features_check, 2302 }; 2303 2304 static void virtnet_config_changed_work(struct work_struct *work) 2305 { 2306 struct virtnet_info *vi = 2307 container_of(work, struct virtnet_info, config_work); 2308 u16 v; 2309 2310 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS, 2311 struct virtio_net_config, status, &v) < 0) 2312 return; 2313 2314 if (v & VIRTIO_NET_S_ANNOUNCE) { 2315 netdev_notify_peers(vi->dev); 2316 virtnet_ack_link_announce(vi); 2317 } 2318 2319 /* Ignore unknown (future) status bits */ 2320 v &= VIRTIO_NET_S_LINK_UP; 2321 2322 if (vi->status == v) 2323 return; 2324 2325 vi->status = v; 2326 2327 if (vi->status & VIRTIO_NET_S_LINK_UP) { 2328 virtnet_update_settings(vi); 2329 netif_carrier_on(vi->dev); 2330 netif_tx_wake_all_queues(vi->dev); 2331 } else { 2332 netif_carrier_off(vi->dev); 2333 netif_tx_stop_all_queues(vi->dev); 2334 } 2335 } 2336 2337 static void virtnet_config_changed(struct virtio_device *vdev) 2338 { 2339 struct virtnet_info *vi = vdev->priv; 2340 2341 schedule_work(&vi->config_work); 2342 } 2343 2344 static void virtnet_free_queues(struct virtnet_info *vi) 2345 { 2346 int i; 2347 2348 for (i = 0; i < vi->max_queue_pairs; i++) { 2349 napi_hash_del(&vi->rq[i].napi); 2350 netif_napi_del(&vi->rq[i].napi); 2351 netif_napi_del(&vi->sq[i].napi); 2352 } 2353 2354 /* We called napi_hash_del() before netif_napi_del(), 2355 * we need to respect an RCU grace period before freeing vi->rq 2356 */ 2357 synchronize_net(); 2358 2359 kfree(vi->rq); 2360 kfree(vi->sq); 2361 } 2362 2363 static void _free_receive_bufs(struct virtnet_info *vi) 2364 { 2365 struct bpf_prog *old_prog; 2366 int i; 2367 2368 for (i = 0; i < vi->max_queue_pairs; i++) { 2369 while (vi->rq[i].pages) 2370 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0); 2371 2372 old_prog = rtnl_dereference(vi->rq[i].xdp_prog); 2373 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL); 2374 if (old_prog) 2375 bpf_prog_put(old_prog); 2376 } 2377 } 2378 2379 static void free_receive_bufs(struct virtnet_info *vi) 2380 { 2381 rtnl_lock(); 2382 _free_receive_bufs(vi); 2383 rtnl_unlock(); 2384 } 2385 2386 static void free_receive_page_frags(struct virtnet_info *vi) 2387 { 2388 int i; 2389 for (i = 0; i < vi->max_queue_pairs; i++) 2390 if (vi->rq[i].alloc_frag.page) 2391 put_page(vi->rq[i].alloc_frag.page); 2392 } 2393 2394 static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q) 2395 { 2396 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs)) 2397 return false; 2398 else if (q < vi->curr_queue_pairs) 2399 return true; 2400 else 2401 return false; 2402 } 2403 2404 static void free_unused_bufs(struct virtnet_info *vi) 2405 { 2406 void *buf; 2407 int i; 2408 2409 for (i = 0; i < vi->max_queue_pairs; i++) { 2410 struct virtqueue *vq = vi->sq[i].vq; 2411 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { 2412 if (!is_xdp_raw_buffer_queue(vi, i)) 2413 dev_kfree_skb(buf); 2414 else 2415 put_page(virt_to_head_page(buf)); 2416 } 2417 } 2418 2419 for (i = 0; i < vi->max_queue_pairs; i++) { 2420 struct virtqueue *vq = vi->rq[i].vq; 2421 2422 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { 2423 if (vi->mergeable_rx_bufs) { 2424 put_page(virt_to_head_page(buf)); 2425 } else if (vi->big_packets) { 2426 give_pages(&vi->rq[i], buf); 2427 } else { 2428 put_page(virt_to_head_page(buf)); 2429 } 2430 } 2431 } 2432 } 2433 2434 static void virtnet_del_vqs(struct virtnet_info *vi) 2435 { 2436 struct virtio_device *vdev = vi->vdev; 2437 2438 virtnet_clean_affinity(vi, -1); 2439 2440 vdev->config->del_vqs(vdev); 2441 2442 virtnet_free_queues(vi); 2443 } 2444 2445 /* How large should a single buffer be so a queue full of these can fit at 2446 * least one full packet? 2447 * Logic below assumes the mergeable buffer header is used. 2448 */ 2449 static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq) 2450 { 2451 const unsigned int hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 2452 unsigned int rq_size = virtqueue_get_vring_size(vq); 2453 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu; 2454 unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len; 2455 unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size); 2456 2457 return max(max(min_buf_len, hdr_len) - hdr_len, 2458 (unsigned int)GOOD_PACKET_LEN); 2459 } 2460 2461 static int virtnet_find_vqs(struct virtnet_info *vi) 2462 { 2463 vq_callback_t **callbacks; 2464 struct virtqueue **vqs; 2465 int ret = -ENOMEM; 2466 int i, total_vqs; 2467 const char **names; 2468 bool *ctx; 2469 2470 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by 2471 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by 2472 * possible control vq. 2473 */ 2474 total_vqs = vi->max_queue_pairs * 2 + 2475 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ); 2476 2477 /* Allocate space for find_vqs parameters */ 2478 vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL); 2479 if (!vqs) 2480 goto err_vq; 2481 callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL); 2482 if (!callbacks) 2483 goto err_callback; 2484 names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL); 2485 if (!names) 2486 goto err_names; 2487 if (!vi->big_packets || vi->mergeable_rx_bufs) { 2488 ctx = kzalloc(total_vqs * sizeof(*ctx), GFP_KERNEL); 2489 if (!ctx) 2490 goto err_ctx; 2491 } else { 2492 ctx = NULL; 2493 } 2494 2495 /* Parameters for control virtqueue, if any */ 2496 if (vi->has_cvq) { 2497 callbacks[total_vqs - 1] = NULL; 2498 names[total_vqs - 1] = "control"; 2499 } 2500 2501 /* Allocate/initialize parameters for send/receive virtqueues */ 2502 for (i = 0; i < vi->max_queue_pairs; i++) { 2503 callbacks[rxq2vq(i)] = skb_recv_done; 2504 callbacks[txq2vq(i)] = skb_xmit_done; 2505 sprintf(vi->rq[i].name, "input.%d", i); 2506 sprintf(vi->sq[i].name, "output.%d", i); 2507 names[rxq2vq(i)] = vi->rq[i].name; 2508 names[txq2vq(i)] = vi->sq[i].name; 2509 if (ctx) 2510 ctx[rxq2vq(i)] = true; 2511 } 2512 2513 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks, 2514 names, ctx, NULL); 2515 if (ret) 2516 goto err_find; 2517 2518 if (vi->has_cvq) { 2519 vi->cvq = vqs[total_vqs - 1]; 2520 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) 2521 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 2522 } 2523 2524 for (i = 0; i < vi->max_queue_pairs; i++) { 2525 vi->rq[i].vq = vqs[rxq2vq(i)]; 2526 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq); 2527 vi->sq[i].vq = vqs[txq2vq(i)]; 2528 } 2529 2530 kfree(names); 2531 kfree(callbacks); 2532 kfree(vqs); 2533 kfree(ctx); 2534 2535 return 0; 2536 2537 err_find: 2538 kfree(ctx); 2539 err_ctx: 2540 kfree(names); 2541 err_names: 2542 kfree(callbacks); 2543 err_callback: 2544 kfree(vqs); 2545 err_vq: 2546 return ret; 2547 } 2548 2549 static int virtnet_alloc_queues(struct virtnet_info *vi) 2550 { 2551 int i; 2552 2553 vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL); 2554 if (!vi->sq) 2555 goto err_sq; 2556 vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL); 2557 if (!vi->rq) 2558 goto err_rq; 2559 2560 INIT_DELAYED_WORK(&vi->refill, refill_work); 2561 for (i = 0; i < vi->max_queue_pairs; i++) { 2562 vi->rq[i].pages = NULL; 2563 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll, 2564 napi_weight); 2565 netif_tx_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx, 2566 napi_tx ? napi_weight : 0); 2567 2568 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg)); 2569 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len); 2570 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg)); 2571 2572 u64_stats_init(&vi->rq[i].stats.syncp); 2573 u64_stats_init(&vi->sq[i].stats.syncp); 2574 } 2575 2576 return 0; 2577 2578 err_rq: 2579 kfree(vi->sq); 2580 err_sq: 2581 return -ENOMEM; 2582 } 2583 2584 static int init_vqs(struct virtnet_info *vi) 2585 { 2586 int ret; 2587 2588 /* Allocate send & receive queues */ 2589 ret = virtnet_alloc_queues(vi); 2590 if (ret) 2591 goto err; 2592 2593 ret = virtnet_find_vqs(vi); 2594 if (ret) 2595 goto err_free; 2596 2597 get_online_cpus(); 2598 virtnet_set_affinity(vi); 2599 put_online_cpus(); 2600 2601 return 0; 2602 2603 err_free: 2604 virtnet_free_queues(vi); 2605 err: 2606 return ret; 2607 } 2608 2609 #ifdef CONFIG_SYSFS 2610 static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue, 2611 char *buf) 2612 { 2613 struct virtnet_info *vi = netdev_priv(queue->dev); 2614 unsigned int queue_index = get_netdev_rx_queue_index(queue); 2615 unsigned int headroom = virtnet_get_headroom(vi); 2616 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 2617 struct ewma_pkt_len *avg; 2618 2619 BUG_ON(queue_index >= vi->max_queue_pairs); 2620 avg = &vi->rq[queue_index].mrg_avg_pkt_len; 2621 return sprintf(buf, "%u\n", 2622 get_mergeable_buf_len(&vi->rq[queue_index], avg, 2623 SKB_DATA_ALIGN(headroom + tailroom))); 2624 } 2625 2626 static struct rx_queue_attribute mergeable_rx_buffer_size_attribute = 2627 __ATTR_RO(mergeable_rx_buffer_size); 2628 2629 static struct attribute *virtio_net_mrg_rx_attrs[] = { 2630 &mergeable_rx_buffer_size_attribute.attr, 2631 NULL 2632 }; 2633 2634 static const struct attribute_group virtio_net_mrg_rx_group = { 2635 .name = "virtio_net", 2636 .attrs = virtio_net_mrg_rx_attrs 2637 }; 2638 #endif 2639 2640 static bool virtnet_fail_on_feature(struct virtio_device *vdev, 2641 unsigned int fbit, 2642 const char *fname, const char *dname) 2643 { 2644 if (!virtio_has_feature(vdev, fbit)) 2645 return false; 2646 2647 dev_err(&vdev->dev, "device advertises feature %s but not %s", 2648 fname, dname); 2649 2650 return true; 2651 } 2652 2653 #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \ 2654 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit) 2655 2656 static bool virtnet_validate_features(struct virtio_device *vdev) 2657 { 2658 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) && 2659 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX, 2660 "VIRTIO_NET_F_CTRL_VQ") || 2661 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN, 2662 "VIRTIO_NET_F_CTRL_VQ") || 2663 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE, 2664 "VIRTIO_NET_F_CTRL_VQ") || 2665 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") || 2666 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR, 2667 "VIRTIO_NET_F_CTRL_VQ"))) { 2668 return false; 2669 } 2670 2671 return true; 2672 } 2673 2674 #define MIN_MTU ETH_MIN_MTU 2675 #define MAX_MTU ETH_MAX_MTU 2676 2677 static int virtnet_validate(struct virtio_device *vdev) 2678 { 2679 if (!vdev->config->get) { 2680 dev_err(&vdev->dev, "%s failure: config access disabled\n", 2681 __func__); 2682 return -EINVAL; 2683 } 2684 2685 if (!virtnet_validate_features(vdev)) 2686 return -EINVAL; 2687 2688 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 2689 int mtu = virtio_cread16(vdev, 2690 offsetof(struct virtio_net_config, 2691 mtu)); 2692 if (mtu < MIN_MTU) 2693 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU); 2694 } 2695 2696 return 0; 2697 } 2698 2699 static int virtnet_probe(struct virtio_device *vdev) 2700 { 2701 int i, err = -ENOMEM; 2702 struct net_device *dev; 2703 struct virtnet_info *vi; 2704 u16 max_queue_pairs; 2705 int mtu; 2706 2707 /* Find if host supports multiqueue virtio_net device */ 2708 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ, 2709 struct virtio_net_config, 2710 max_virtqueue_pairs, &max_queue_pairs); 2711 2712 /* We need at least 2 queue's */ 2713 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 2714 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 2715 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 2716 max_queue_pairs = 1; 2717 2718 /* Allocate ourselves a network device with room for our info */ 2719 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs); 2720 if (!dev) 2721 return -ENOMEM; 2722 2723 /* Set up network device as normal. */ 2724 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE; 2725 dev->netdev_ops = &virtnet_netdev; 2726 dev->features = NETIF_F_HIGHDMA; 2727 2728 dev->ethtool_ops = &virtnet_ethtool_ops; 2729 SET_NETDEV_DEV(dev, &vdev->dev); 2730 2731 /* Do we support "hardware" checksums? */ 2732 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { 2733 /* This opens up the world of extra features. */ 2734 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG; 2735 if (csum) 2736 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG; 2737 2738 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { 2739 dev->hw_features |= NETIF_F_TSO 2740 | NETIF_F_TSO_ECN | NETIF_F_TSO6; 2741 } 2742 /* Individual feature bits: what can host handle? */ 2743 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) 2744 dev->hw_features |= NETIF_F_TSO; 2745 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) 2746 dev->hw_features |= NETIF_F_TSO6; 2747 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) 2748 dev->hw_features |= NETIF_F_TSO_ECN; 2749 2750 dev->features |= NETIF_F_GSO_ROBUST; 2751 2752 if (gso) 2753 dev->features |= dev->hw_features & NETIF_F_ALL_TSO; 2754 /* (!csum && gso) case will be fixed by register_netdev() */ 2755 } 2756 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM)) 2757 dev->features |= NETIF_F_RXCSUM; 2758 2759 dev->vlan_features = dev->features; 2760 2761 /* MTU range: 68 - 65535 */ 2762 dev->min_mtu = MIN_MTU; 2763 dev->max_mtu = MAX_MTU; 2764 2765 /* Configuration may specify what MAC to use. Otherwise random. */ 2766 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) 2767 virtio_cread_bytes(vdev, 2768 offsetof(struct virtio_net_config, mac), 2769 dev->dev_addr, dev->addr_len); 2770 else 2771 eth_hw_addr_random(dev); 2772 2773 /* Set up our device-specific information */ 2774 vi = netdev_priv(dev); 2775 vi->dev = dev; 2776 vi->vdev = vdev; 2777 vdev->priv = vi; 2778 2779 INIT_WORK(&vi->config_work, virtnet_config_changed_work); 2780 2781 /* If we can receive ANY GSO packets, we must allocate large ones. */ 2782 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || 2783 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) || 2784 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) || 2785 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO)) 2786 vi->big_packets = true; 2787 2788 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) 2789 vi->mergeable_rx_bufs = true; 2790 2791 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) || 2792 virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 2793 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 2794 else 2795 vi->hdr_len = sizeof(struct virtio_net_hdr); 2796 2797 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) || 2798 virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 2799 vi->any_header_sg = true; 2800 2801 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 2802 vi->has_cvq = true; 2803 2804 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 2805 mtu = virtio_cread16(vdev, 2806 offsetof(struct virtio_net_config, 2807 mtu)); 2808 if (mtu < dev->min_mtu) { 2809 /* Should never trigger: MTU was previously validated 2810 * in virtnet_validate. 2811 */ 2812 dev_err(&vdev->dev, "device MTU appears to have changed " 2813 "it is now %d < %d", mtu, dev->min_mtu); 2814 goto free; 2815 } 2816 2817 dev->mtu = mtu; 2818 dev->max_mtu = mtu; 2819 2820 /* TODO: size buffers correctly in this case. */ 2821 if (dev->mtu > ETH_DATA_LEN) 2822 vi->big_packets = true; 2823 } 2824 2825 if (vi->any_header_sg) 2826 dev->needed_headroom = vi->hdr_len; 2827 2828 /* Enable multiqueue by default */ 2829 if (num_online_cpus() >= max_queue_pairs) 2830 vi->curr_queue_pairs = max_queue_pairs; 2831 else 2832 vi->curr_queue_pairs = num_online_cpus(); 2833 vi->max_queue_pairs = max_queue_pairs; 2834 2835 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */ 2836 err = init_vqs(vi); 2837 if (err) 2838 goto free; 2839 2840 #ifdef CONFIG_SYSFS 2841 if (vi->mergeable_rx_bufs) 2842 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group; 2843 #endif 2844 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs); 2845 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs); 2846 2847 virtnet_init_settings(dev); 2848 2849 err = register_netdev(dev); 2850 if (err) { 2851 pr_debug("virtio_net: registering device failed\n"); 2852 goto free_vqs; 2853 } 2854 2855 virtio_device_ready(vdev); 2856 2857 err = virtnet_cpu_notif_add(vi); 2858 if (err) { 2859 pr_debug("virtio_net: registering cpu notifier failed\n"); 2860 goto free_unregister_netdev; 2861 } 2862 2863 virtnet_set_queues(vi, vi->curr_queue_pairs); 2864 2865 /* Assume link up if device can't report link status, 2866 otherwise get link status from config. */ 2867 netif_carrier_off(dev); 2868 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) { 2869 schedule_work(&vi->config_work); 2870 } else { 2871 vi->status = VIRTIO_NET_S_LINK_UP; 2872 virtnet_update_settings(vi); 2873 netif_carrier_on(dev); 2874 } 2875 2876 for (i = 0; i < ARRAY_SIZE(guest_offloads); i++) 2877 if (virtio_has_feature(vi->vdev, guest_offloads[i])) 2878 set_bit(guest_offloads[i], &vi->guest_offloads); 2879 2880 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n", 2881 dev->name, max_queue_pairs); 2882 2883 return 0; 2884 2885 free_unregister_netdev: 2886 vi->vdev->config->reset(vdev); 2887 2888 unregister_netdev(dev); 2889 free_vqs: 2890 cancel_delayed_work_sync(&vi->refill); 2891 free_receive_page_frags(vi); 2892 virtnet_del_vqs(vi); 2893 free: 2894 free_netdev(dev); 2895 return err; 2896 } 2897 2898 static void remove_vq_common(struct virtnet_info *vi) 2899 { 2900 vi->vdev->config->reset(vi->vdev); 2901 2902 /* Free unused buffers in both send and recv, if any. */ 2903 free_unused_bufs(vi); 2904 2905 free_receive_bufs(vi); 2906 2907 free_receive_page_frags(vi); 2908 2909 virtnet_del_vqs(vi); 2910 } 2911 2912 static void virtnet_remove(struct virtio_device *vdev) 2913 { 2914 struct virtnet_info *vi = vdev->priv; 2915 2916 virtnet_cpu_notif_remove(vi); 2917 2918 /* Make sure no work handler is accessing the device. */ 2919 flush_work(&vi->config_work); 2920 2921 unregister_netdev(vi->dev); 2922 2923 remove_vq_common(vi); 2924 2925 free_netdev(vi->dev); 2926 } 2927 2928 static __maybe_unused int virtnet_freeze(struct virtio_device *vdev) 2929 { 2930 struct virtnet_info *vi = vdev->priv; 2931 2932 virtnet_cpu_notif_remove(vi); 2933 virtnet_freeze_down(vdev); 2934 remove_vq_common(vi); 2935 2936 return 0; 2937 } 2938 2939 static __maybe_unused int virtnet_restore(struct virtio_device *vdev) 2940 { 2941 struct virtnet_info *vi = vdev->priv; 2942 int err; 2943 2944 err = virtnet_restore_up(vdev); 2945 if (err) 2946 return err; 2947 virtnet_set_queues(vi, vi->curr_queue_pairs); 2948 2949 err = virtnet_cpu_notif_add(vi); 2950 if (err) 2951 return err; 2952 2953 return 0; 2954 } 2955 2956 static struct virtio_device_id id_table[] = { 2957 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 2958 { 0 }, 2959 }; 2960 2961 #define VIRTNET_FEATURES \ 2962 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \ 2963 VIRTIO_NET_F_MAC, \ 2964 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \ 2965 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \ 2966 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \ 2967 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \ 2968 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \ 2969 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \ 2970 VIRTIO_NET_F_CTRL_MAC_ADDR, \ 2971 VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \ 2972 VIRTIO_NET_F_SPEED_DUPLEX 2973 2974 static unsigned int features[] = { 2975 VIRTNET_FEATURES, 2976 }; 2977 2978 static unsigned int features_legacy[] = { 2979 VIRTNET_FEATURES, 2980 VIRTIO_NET_F_GSO, 2981 VIRTIO_F_ANY_LAYOUT, 2982 }; 2983 2984 static struct virtio_driver virtio_net_driver = { 2985 .feature_table = features, 2986 .feature_table_size = ARRAY_SIZE(features), 2987 .feature_table_legacy = features_legacy, 2988 .feature_table_size_legacy = ARRAY_SIZE(features_legacy), 2989 .driver.name = KBUILD_MODNAME, 2990 .driver.owner = THIS_MODULE, 2991 .id_table = id_table, 2992 .validate = virtnet_validate, 2993 .probe = virtnet_probe, 2994 .remove = virtnet_remove, 2995 .config_changed = virtnet_config_changed, 2996 #ifdef CONFIG_PM_SLEEP 2997 .freeze = virtnet_freeze, 2998 .restore = virtnet_restore, 2999 #endif 3000 }; 3001 3002 static __init int virtio_net_driver_init(void) 3003 { 3004 int ret; 3005 3006 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online", 3007 virtnet_cpu_online, 3008 virtnet_cpu_down_prep); 3009 if (ret < 0) 3010 goto out; 3011 virtionet_online = ret; 3012 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead", 3013 NULL, virtnet_cpu_dead); 3014 if (ret) 3015 goto err_dead; 3016 3017 ret = register_virtio_driver(&virtio_net_driver); 3018 if (ret) 3019 goto err_virtio; 3020 return 0; 3021 err_virtio: 3022 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 3023 err_dead: 3024 cpuhp_remove_multi_state(virtionet_online); 3025 out: 3026 return ret; 3027 } 3028 module_init(virtio_net_driver_init); 3029 3030 static __exit void virtio_net_driver_exit(void) 3031 { 3032 unregister_virtio_driver(&virtio_net_driver); 3033 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 3034 cpuhp_remove_multi_state(virtionet_online); 3035 } 3036 module_exit(virtio_net_driver_exit); 3037 3038 MODULE_DEVICE_TABLE(virtio, id_table); 3039 MODULE_DESCRIPTION("Virtio network driver"); 3040 MODULE_LICENSE("GPL"); 3041