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