1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* A network driver using virtio. 3 * 4 * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation 5 */ 6 //#define DEBUG 7 #include <linux/netdevice.h> 8 #include <linux/etherdevice.h> 9 #include <linux/ethtool.h> 10 #include <linux/module.h> 11 #include <linux/virtio.h> 12 #include <linux/virtio_net.h> 13 #include <linux/bpf.h> 14 #include <linux/bpf_trace.h> 15 #include <linux/scatterlist.h> 16 #include <linux/if_vlan.h> 17 #include <linux/slab.h> 18 #include <linux/cpu.h> 19 #include <linux/average.h> 20 #include <linux/filter.h> 21 #include <linux/kernel.h> 22 #include <net/route.h> 23 #include <net/xdp.h> 24 #include <net/net_failover.h> 25 #include <net/netdev_rx_queue.h> 26 27 static int napi_weight = NAPI_POLL_WEIGHT; 28 module_param(napi_weight, int, 0444); 29 30 static bool csum = true, gso = true, napi_tx = true; 31 module_param(csum, bool, 0444); 32 module_param(gso, bool, 0444); 33 module_param(napi_tx, bool, 0644); 34 35 /* FIXME: MTU in config. */ 36 #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN) 37 #define GOOD_COPY_LEN 128 38 39 #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD) 40 41 /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */ 42 #define VIRTIO_XDP_HEADROOM 256 43 44 /* Separating two types of XDP xmit */ 45 #define VIRTIO_XDP_TX BIT(0) 46 #define VIRTIO_XDP_REDIR BIT(1) 47 48 #define VIRTIO_XDP_FLAG BIT(0) 49 50 /* RX packet size EWMA. The average packet size is used to determine the packet 51 * buffer size when refilling RX rings. As the entire RX ring may be refilled 52 * at once, the weight is chosen so that the EWMA will be insensitive to short- 53 * term, transient changes in packet size. 54 */ 55 DECLARE_EWMA(pkt_len, 0, 64) 56 57 #define VIRTNET_DRIVER_VERSION "1.0.0" 58 59 static const unsigned long guest_offloads[] = { 60 VIRTIO_NET_F_GUEST_TSO4, 61 VIRTIO_NET_F_GUEST_TSO6, 62 VIRTIO_NET_F_GUEST_ECN, 63 VIRTIO_NET_F_GUEST_UFO, 64 VIRTIO_NET_F_GUEST_CSUM, 65 VIRTIO_NET_F_GUEST_USO4, 66 VIRTIO_NET_F_GUEST_USO6, 67 VIRTIO_NET_F_GUEST_HDRLEN 68 }; 69 70 #define GUEST_OFFLOAD_GRO_HW_MASK ((1ULL << VIRTIO_NET_F_GUEST_TSO4) | \ 71 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \ 72 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \ 73 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \ 74 (1ULL << VIRTIO_NET_F_GUEST_USO4) | \ 75 (1ULL << VIRTIO_NET_F_GUEST_USO6)) 76 77 struct virtnet_stat_desc { 78 char desc[ETH_GSTRING_LEN]; 79 size_t offset; 80 }; 81 82 struct virtnet_sq_stats { 83 struct u64_stats_sync syncp; 84 u64 packets; 85 u64 bytes; 86 u64 xdp_tx; 87 u64 xdp_tx_drops; 88 u64 kicks; 89 u64 tx_timeouts; 90 }; 91 92 struct virtnet_rq_stats { 93 struct u64_stats_sync syncp; 94 u64 packets; 95 u64 bytes; 96 u64 drops; 97 u64 xdp_packets; 98 u64 xdp_tx; 99 u64 xdp_redirects; 100 u64 xdp_drops; 101 u64 kicks; 102 }; 103 104 #define VIRTNET_SQ_STAT(m) offsetof(struct virtnet_sq_stats, m) 105 #define VIRTNET_RQ_STAT(m) offsetof(struct virtnet_rq_stats, m) 106 107 static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = { 108 { "packets", VIRTNET_SQ_STAT(packets) }, 109 { "bytes", VIRTNET_SQ_STAT(bytes) }, 110 { "xdp_tx", VIRTNET_SQ_STAT(xdp_tx) }, 111 { "xdp_tx_drops", VIRTNET_SQ_STAT(xdp_tx_drops) }, 112 { "kicks", VIRTNET_SQ_STAT(kicks) }, 113 { "tx_timeouts", VIRTNET_SQ_STAT(tx_timeouts) }, 114 }; 115 116 static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = { 117 { "packets", VIRTNET_RQ_STAT(packets) }, 118 { "bytes", VIRTNET_RQ_STAT(bytes) }, 119 { "drops", VIRTNET_RQ_STAT(drops) }, 120 { "xdp_packets", VIRTNET_RQ_STAT(xdp_packets) }, 121 { "xdp_tx", VIRTNET_RQ_STAT(xdp_tx) }, 122 { "xdp_redirects", VIRTNET_RQ_STAT(xdp_redirects) }, 123 { "xdp_drops", VIRTNET_RQ_STAT(xdp_drops) }, 124 { "kicks", VIRTNET_RQ_STAT(kicks) }, 125 }; 126 127 #define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc) 128 #define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc) 129 130 struct virtnet_interrupt_coalesce { 131 u32 max_packets; 132 u32 max_usecs; 133 }; 134 135 /* Internal representation of a send virtqueue */ 136 struct send_queue { 137 /* Virtqueue associated with this send _queue */ 138 struct virtqueue *vq; 139 140 /* TX: fragments + linear part + virtio header */ 141 struct scatterlist sg[MAX_SKB_FRAGS + 2]; 142 143 /* Name of the send queue: output.$index */ 144 char name[16]; 145 146 struct virtnet_sq_stats stats; 147 148 struct virtnet_interrupt_coalesce intr_coal; 149 150 struct napi_struct napi; 151 152 /* Record whether sq is in reset state. */ 153 bool reset; 154 }; 155 156 /* Internal representation of a receive virtqueue */ 157 struct receive_queue { 158 /* Virtqueue associated with this receive_queue */ 159 struct virtqueue *vq; 160 161 struct napi_struct napi; 162 163 struct bpf_prog __rcu *xdp_prog; 164 165 struct virtnet_rq_stats stats; 166 167 struct virtnet_interrupt_coalesce intr_coal; 168 169 /* Chain pages by the private ptr. */ 170 struct page *pages; 171 172 /* Average packet length for mergeable receive buffers. */ 173 struct ewma_pkt_len mrg_avg_pkt_len; 174 175 /* Page frag for packet buffer allocation. */ 176 struct page_frag alloc_frag; 177 178 /* RX: fragments + linear part + virtio header */ 179 struct scatterlist sg[MAX_SKB_FRAGS + 2]; 180 181 /* Min single buffer size for mergeable buffers case. */ 182 unsigned int min_buf_len; 183 184 /* Name of this receive queue: input.$index */ 185 char name[16]; 186 187 struct xdp_rxq_info xdp_rxq; 188 }; 189 190 /* This structure can contain rss message with maximum settings for indirection table and keysize 191 * Note, that default structure that describes RSS configuration virtio_net_rss_config 192 * contains same info but can't handle table values. 193 * In any case, structure would be passed to virtio hw through sg_buf split by parts 194 * because table sizes may be differ according to the device configuration. 195 */ 196 #define VIRTIO_NET_RSS_MAX_KEY_SIZE 40 197 #define VIRTIO_NET_RSS_MAX_TABLE_LEN 128 198 struct virtio_net_ctrl_rss { 199 u32 hash_types; 200 u16 indirection_table_mask; 201 u16 unclassified_queue; 202 u16 indirection_table[VIRTIO_NET_RSS_MAX_TABLE_LEN]; 203 u16 max_tx_vq; 204 u8 hash_key_length; 205 u8 key[VIRTIO_NET_RSS_MAX_KEY_SIZE]; 206 }; 207 208 /* Control VQ buffers: protected by the rtnl lock */ 209 struct control_buf { 210 struct virtio_net_ctrl_hdr hdr; 211 virtio_net_ctrl_ack status; 212 struct virtio_net_ctrl_mq mq; 213 u8 promisc; 214 u8 allmulti; 215 __virtio16 vid; 216 __virtio64 offloads; 217 struct virtio_net_ctrl_rss rss; 218 struct virtio_net_ctrl_coal_tx coal_tx; 219 struct virtio_net_ctrl_coal_rx coal_rx; 220 struct virtio_net_ctrl_coal_vq coal_vq; 221 }; 222 223 struct virtnet_info { 224 struct virtio_device *vdev; 225 struct virtqueue *cvq; 226 struct net_device *dev; 227 struct send_queue *sq; 228 struct receive_queue *rq; 229 unsigned int status; 230 231 /* Max # of queue pairs supported by the device */ 232 u16 max_queue_pairs; 233 234 /* # of queue pairs currently used by the driver */ 235 u16 curr_queue_pairs; 236 237 /* # of XDP queue pairs currently used by the driver */ 238 u16 xdp_queue_pairs; 239 240 /* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */ 241 bool xdp_enabled; 242 243 /* I like... big packets and I cannot lie! */ 244 bool big_packets; 245 246 /* number of sg entries allocated for big packets */ 247 unsigned int big_packets_num_skbfrags; 248 249 /* Host will merge rx buffers for big packets (shake it! shake it!) */ 250 bool mergeable_rx_bufs; 251 252 /* Host supports rss and/or hash report */ 253 bool has_rss; 254 bool has_rss_hash_report; 255 u8 rss_key_size; 256 u16 rss_indir_table_size; 257 u32 rss_hash_types_supported; 258 u32 rss_hash_types_saved; 259 260 /* Has control virtqueue */ 261 bool has_cvq; 262 263 /* Host can handle any s/g split between our header and packet data */ 264 bool any_header_sg; 265 266 /* Packet virtio header size */ 267 u8 hdr_len; 268 269 /* Work struct for delayed refilling if we run low on memory. */ 270 struct delayed_work refill; 271 272 /* Is delayed refill enabled? */ 273 bool refill_enabled; 274 275 /* The lock to synchronize the access to refill_enabled */ 276 spinlock_t refill_lock; 277 278 /* Work struct for config space updates */ 279 struct work_struct config_work; 280 281 /* Does the affinity hint is set for virtqueues? */ 282 bool affinity_hint_set; 283 284 /* CPU hotplug instances for online & dead */ 285 struct hlist_node node; 286 struct hlist_node node_dead; 287 288 struct control_buf *ctrl; 289 290 /* Ethtool settings */ 291 u8 duplex; 292 u32 speed; 293 294 /* Interrupt coalescing settings */ 295 struct virtnet_interrupt_coalesce intr_coal_tx; 296 struct virtnet_interrupt_coalesce intr_coal_rx; 297 298 unsigned long guest_offloads; 299 unsigned long guest_offloads_capable; 300 301 /* failover when STANDBY feature enabled */ 302 struct failover *failover; 303 }; 304 305 struct padded_vnet_hdr { 306 struct virtio_net_hdr_v1_hash hdr; 307 /* 308 * hdr is in a separate sg buffer, and data sg buffer shares same page 309 * with this header sg. This padding makes next sg 16 byte aligned 310 * after the header. 311 */ 312 char padding[12]; 313 }; 314 315 static void virtnet_rq_free_unused_buf(struct virtqueue *vq, void *buf); 316 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf); 317 318 static bool is_xdp_frame(void *ptr) 319 { 320 return (unsigned long)ptr & VIRTIO_XDP_FLAG; 321 } 322 323 static void *xdp_to_ptr(struct xdp_frame *ptr) 324 { 325 return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG); 326 } 327 328 static struct xdp_frame *ptr_to_xdp(void *ptr) 329 { 330 return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG); 331 } 332 333 /* Converting between virtqueue no. and kernel tx/rx queue no. 334 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq 335 */ 336 static int vq2txq(struct virtqueue *vq) 337 { 338 return (vq->index - 1) / 2; 339 } 340 341 static int txq2vq(int txq) 342 { 343 return txq * 2 + 1; 344 } 345 346 static int vq2rxq(struct virtqueue *vq) 347 { 348 return vq->index / 2; 349 } 350 351 static int rxq2vq(int rxq) 352 { 353 return rxq * 2; 354 } 355 356 static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb) 357 { 358 return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb; 359 } 360 361 /* 362 * private is used to chain pages for big packets, put the whole 363 * most recent used list in the beginning for reuse 364 */ 365 static void give_pages(struct receive_queue *rq, struct page *page) 366 { 367 struct page *end; 368 369 /* Find end of list, sew whole thing into vi->rq.pages. */ 370 for (end = page; end->private; end = (struct page *)end->private); 371 end->private = (unsigned long)rq->pages; 372 rq->pages = page; 373 } 374 375 static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask) 376 { 377 struct page *p = rq->pages; 378 379 if (p) { 380 rq->pages = (struct page *)p->private; 381 /* clear private here, it is used to chain pages */ 382 p->private = 0; 383 } else 384 p = alloc_page(gfp_mask); 385 return p; 386 } 387 388 static void enable_delayed_refill(struct virtnet_info *vi) 389 { 390 spin_lock_bh(&vi->refill_lock); 391 vi->refill_enabled = true; 392 spin_unlock_bh(&vi->refill_lock); 393 } 394 395 static void disable_delayed_refill(struct virtnet_info *vi) 396 { 397 spin_lock_bh(&vi->refill_lock); 398 vi->refill_enabled = false; 399 spin_unlock_bh(&vi->refill_lock); 400 } 401 402 static void virtqueue_napi_schedule(struct napi_struct *napi, 403 struct virtqueue *vq) 404 { 405 if (napi_schedule_prep(napi)) { 406 virtqueue_disable_cb(vq); 407 __napi_schedule(napi); 408 } 409 } 410 411 static void virtqueue_napi_complete(struct napi_struct *napi, 412 struct virtqueue *vq, int processed) 413 { 414 int opaque; 415 416 opaque = virtqueue_enable_cb_prepare(vq); 417 if (napi_complete_done(napi, processed)) { 418 if (unlikely(virtqueue_poll(vq, opaque))) 419 virtqueue_napi_schedule(napi, vq); 420 } else { 421 virtqueue_disable_cb(vq); 422 } 423 } 424 425 static void skb_xmit_done(struct virtqueue *vq) 426 { 427 struct virtnet_info *vi = vq->vdev->priv; 428 struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi; 429 430 /* Suppress further interrupts. */ 431 virtqueue_disable_cb(vq); 432 433 if (napi->weight) 434 virtqueue_napi_schedule(napi, vq); 435 else 436 /* We were probably waiting for more output buffers. */ 437 netif_wake_subqueue(vi->dev, vq2txq(vq)); 438 } 439 440 #define MRG_CTX_HEADER_SHIFT 22 441 static void *mergeable_len_to_ctx(unsigned int truesize, 442 unsigned int headroom) 443 { 444 return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize); 445 } 446 447 static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx) 448 { 449 return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT; 450 } 451 452 static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx) 453 { 454 return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1); 455 } 456 457 static struct sk_buff *virtnet_build_skb(void *buf, unsigned int buflen, 458 unsigned int headroom, 459 unsigned int len) 460 { 461 struct sk_buff *skb; 462 463 skb = build_skb(buf, buflen); 464 if (unlikely(!skb)) 465 return NULL; 466 467 skb_reserve(skb, headroom); 468 skb_put(skb, len); 469 470 return skb; 471 } 472 473 /* Called from bottom half context */ 474 static struct sk_buff *page_to_skb(struct virtnet_info *vi, 475 struct receive_queue *rq, 476 struct page *page, unsigned int offset, 477 unsigned int len, unsigned int truesize, 478 unsigned int headroom) 479 { 480 struct sk_buff *skb; 481 struct virtio_net_hdr_mrg_rxbuf *hdr; 482 unsigned int copy, hdr_len, hdr_padded_len; 483 struct page *page_to_free = NULL; 484 int tailroom, shinfo_size; 485 char *p, *hdr_p, *buf; 486 487 p = page_address(page) + offset; 488 hdr_p = p; 489 490 hdr_len = vi->hdr_len; 491 if (vi->mergeable_rx_bufs) 492 hdr_padded_len = hdr_len; 493 else 494 hdr_padded_len = sizeof(struct padded_vnet_hdr); 495 496 buf = p - headroom; 497 len -= hdr_len; 498 offset += hdr_padded_len; 499 p += hdr_padded_len; 500 tailroom = truesize - headroom - hdr_padded_len - len; 501 502 shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 503 504 /* copy small packet so we can reuse these pages */ 505 if (!NET_IP_ALIGN && len > GOOD_COPY_LEN && tailroom >= shinfo_size) { 506 skb = virtnet_build_skb(buf, truesize, p - buf, len); 507 if (unlikely(!skb)) 508 return NULL; 509 510 page = (struct page *)page->private; 511 if (page) 512 give_pages(rq, page); 513 goto ok; 514 } 515 516 /* copy small packet so we can reuse these pages for small data */ 517 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN); 518 if (unlikely(!skb)) 519 return NULL; 520 521 /* Copy all frame if it fits skb->head, otherwise 522 * we let virtio_net_hdr_to_skb() and GRO pull headers as needed. 523 */ 524 if (len <= skb_tailroom(skb)) 525 copy = len; 526 else 527 copy = ETH_HLEN; 528 skb_put_data(skb, p, copy); 529 530 len -= copy; 531 offset += copy; 532 533 if (vi->mergeable_rx_bufs) { 534 if (len) 535 skb_add_rx_frag(skb, 0, page, offset, len, truesize); 536 else 537 page_to_free = page; 538 goto ok; 539 } 540 541 /* 542 * Verify that we can indeed put this data into a skb. 543 * This is here to handle cases when the device erroneously 544 * tries to receive more than is possible. This is usually 545 * the case of a broken device. 546 */ 547 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) { 548 net_dbg_ratelimited("%s: too much data\n", skb->dev->name); 549 dev_kfree_skb(skb); 550 return NULL; 551 } 552 BUG_ON(offset >= PAGE_SIZE); 553 while (len) { 554 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len); 555 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset, 556 frag_size, truesize); 557 len -= frag_size; 558 page = (struct page *)page->private; 559 offset = 0; 560 } 561 562 if (page) 563 give_pages(rq, page); 564 565 ok: 566 hdr = skb_vnet_hdr(skb); 567 memcpy(hdr, hdr_p, hdr_len); 568 if (page_to_free) 569 put_page(page_to_free); 570 571 return skb; 572 } 573 574 static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi) 575 { 576 unsigned int len; 577 unsigned int packets = 0; 578 unsigned int bytes = 0; 579 void *ptr; 580 581 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) { 582 if (likely(!is_xdp_frame(ptr))) { 583 struct sk_buff *skb = ptr; 584 585 pr_debug("Sent skb %p\n", skb); 586 587 bytes += skb->len; 588 napi_consume_skb(skb, in_napi); 589 } else { 590 struct xdp_frame *frame = ptr_to_xdp(ptr); 591 592 bytes += xdp_get_frame_len(frame); 593 xdp_return_frame(frame); 594 } 595 packets++; 596 } 597 598 /* Avoid overhead when no packets have been processed 599 * happens when called speculatively from start_xmit. 600 */ 601 if (!packets) 602 return; 603 604 u64_stats_update_begin(&sq->stats.syncp); 605 sq->stats.bytes += bytes; 606 sq->stats.packets += packets; 607 u64_stats_update_end(&sq->stats.syncp); 608 } 609 610 static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q) 611 { 612 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs)) 613 return false; 614 else if (q < vi->curr_queue_pairs) 615 return true; 616 else 617 return false; 618 } 619 620 static void check_sq_full_and_disable(struct virtnet_info *vi, 621 struct net_device *dev, 622 struct send_queue *sq) 623 { 624 bool use_napi = sq->napi.weight; 625 int qnum; 626 627 qnum = sq - vi->sq; 628 629 /* If running out of space, stop queue to avoid getting packets that we 630 * are then unable to transmit. 631 * An alternative would be to force queuing layer to requeue the skb by 632 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be 633 * returned in a normal path of operation: it means that driver is not 634 * maintaining the TX queue stop/start state properly, and causes 635 * the stack to do a non-trivial amount of useless work. 636 * Since most packets only take 1 or 2 ring slots, stopping the queue 637 * early means 16 slots are typically wasted. 638 */ 639 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) { 640 netif_stop_subqueue(dev, qnum); 641 if (use_napi) { 642 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) 643 virtqueue_napi_schedule(&sq->napi, sq->vq); 644 } else if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) { 645 /* More just got used, free them then recheck. */ 646 free_old_xmit_skbs(sq, false); 647 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) { 648 netif_start_subqueue(dev, qnum); 649 virtqueue_disable_cb(sq->vq); 650 } 651 } 652 } 653 } 654 655 static int __virtnet_xdp_xmit_one(struct virtnet_info *vi, 656 struct send_queue *sq, 657 struct xdp_frame *xdpf) 658 { 659 struct virtio_net_hdr_mrg_rxbuf *hdr; 660 struct skb_shared_info *shinfo; 661 u8 nr_frags = 0; 662 int err, i; 663 664 if (unlikely(xdpf->headroom < vi->hdr_len)) 665 return -EOVERFLOW; 666 667 if (unlikely(xdp_frame_has_frags(xdpf))) { 668 shinfo = xdp_get_shared_info_from_frame(xdpf); 669 nr_frags = shinfo->nr_frags; 670 } 671 672 /* In wrapping function virtnet_xdp_xmit(), we need to free 673 * up the pending old buffers, where we need to calculate the 674 * position of skb_shared_info in xdp_get_frame_len() and 675 * xdp_return_frame(), which will involve to xdpf->data and 676 * xdpf->headroom. Therefore, we need to update the value of 677 * headroom synchronously here. 678 */ 679 xdpf->headroom -= vi->hdr_len; 680 xdpf->data -= vi->hdr_len; 681 /* Zero header and leave csum up to XDP layers */ 682 hdr = xdpf->data; 683 memset(hdr, 0, vi->hdr_len); 684 xdpf->len += vi->hdr_len; 685 686 sg_init_table(sq->sg, nr_frags + 1); 687 sg_set_buf(sq->sg, xdpf->data, xdpf->len); 688 for (i = 0; i < nr_frags; i++) { 689 skb_frag_t *frag = &shinfo->frags[i]; 690 691 sg_set_page(&sq->sg[i + 1], skb_frag_page(frag), 692 skb_frag_size(frag), skb_frag_off(frag)); 693 } 694 695 err = virtqueue_add_outbuf(sq->vq, sq->sg, nr_frags + 1, 696 xdp_to_ptr(xdpf), GFP_ATOMIC); 697 if (unlikely(err)) 698 return -ENOSPC; /* Caller handle free/refcnt */ 699 700 return 0; 701 } 702 703 /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on 704 * the current cpu, so it does not need to be locked. 705 * 706 * Here we use marco instead of inline functions because we have to deal with 707 * three issues at the same time: 1. the choice of sq. 2. judge and execute the 708 * lock/unlock of txq 3. make sparse happy. It is difficult for two inline 709 * functions to perfectly solve these three problems at the same time. 710 */ 711 #define virtnet_xdp_get_sq(vi) ({ \ 712 int cpu = smp_processor_id(); \ 713 struct netdev_queue *txq; \ 714 typeof(vi) v = (vi); \ 715 unsigned int qp; \ 716 \ 717 if (v->curr_queue_pairs > nr_cpu_ids) { \ 718 qp = v->curr_queue_pairs - v->xdp_queue_pairs; \ 719 qp += cpu; \ 720 txq = netdev_get_tx_queue(v->dev, qp); \ 721 __netif_tx_acquire(txq); \ 722 } else { \ 723 qp = cpu % v->curr_queue_pairs; \ 724 txq = netdev_get_tx_queue(v->dev, qp); \ 725 __netif_tx_lock(txq, cpu); \ 726 } \ 727 v->sq + qp; \ 728 }) 729 730 #define virtnet_xdp_put_sq(vi, q) { \ 731 struct netdev_queue *txq; \ 732 typeof(vi) v = (vi); \ 733 \ 734 txq = netdev_get_tx_queue(v->dev, (q) - v->sq); \ 735 if (v->curr_queue_pairs > nr_cpu_ids) \ 736 __netif_tx_release(txq); \ 737 else \ 738 __netif_tx_unlock(txq); \ 739 } 740 741 static int virtnet_xdp_xmit(struct net_device *dev, 742 int n, struct xdp_frame **frames, u32 flags) 743 { 744 struct virtnet_info *vi = netdev_priv(dev); 745 struct receive_queue *rq = vi->rq; 746 struct bpf_prog *xdp_prog; 747 struct send_queue *sq; 748 unsigned int len; 749 int packets = 0; 750 int bytes = 0; 751 int nxmit = 0; 752 int kicks = 0; 753 void *ptr; 754 int ret; 755 int i; 756 757 /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this 758 * indicate XDP resources have been successfully allocated. 759 */ 760 xdp_prog = rcu_access_pointer(rq->xdp_prog); 761 if (!xdp_prog) 762 return -ENXIO; 763 764 sq = virtnet_xdp_get_sq(vi); 765 766 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) { 767 ret = -EINVAL; 768 goto out; 769 } 770 771 /* Free up any pending old buffers before queueing new ones. */ 772 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) { 773 if (likely(is_xdp_frame(ptr))) { 774 struct xdp_frame *frame = ptr_to_xdp(ptr); 775 776 bytes += xdp_get_frame_len(frame); 777 xdp_return_frame(frame); 778 } else { 779 struct sk_buff *skb = ptr; 780 781 bytes += skb->len; 782 napi_consume_skb(skb, false); 783 } 784 packets++; 785 } 786 787 for (i = 0; i < n; i++) { 788 struct xdp_frame *xdpf = frames[i]; 789 790 if (__virtnet_xdp_xmit_one(vi, sq, xdpf)) 791 break; 792 nxmit++; 793 } 794 ret = nxmit; 795 796 if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq)) 797 check_sq_full_and_disable(vi, dev, sq); 798 799 if (flags & XDP_XMIT_FLUSH) { 800 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) 801 kicks = 1; 802 } 803 out: 804 u64_stats_update_begin(&sq->stats.syncp); 805 sq->stats.bytes += bytes; 806 sq->stats.packets += packets; 807 sq->stats.xdp_tx += n; 808 sq->stats.xdp_tx_drops += n - nxmit; 809 sq->stats.kicks += kicks; 810 u64_stats_update_end(&sq->stats.syncp); 811 812 virtnet_xdp_put_sq(vi, sq); 813 return ret; 814 } 815 816 static void put_xdp_frags(struct xdp_buff *xdp) 817 { 818 struct skb_shared_info *shinfo; 819 struct page *xdp_page; 820 int i; 821 822 if (xdp_buff_has_frags(xdp)) { 823 shinfo = xdp_get_shared_info_from_buff(xdp); 824 for (i = 0; i < shinfo->nr_frags; i++) { 825 xdp_page = skb_frag_page(&shinfo->frags[i]); 826 put_page(xdp_page); 827 } 828 } 829 } 830 831 static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp, 832 struct net_device *dev, 833 unsigned int *xdp_xmit, 834 struct virtnet_rq_stats *stats) 835 { 836 struct xdp_frame *xdpf; 837 int err; 838 u32 act; 839 840 act = bpf_prog_run_xdp(xdp_prog, xdp); 841 stats->xdp_packets++; 842 843 switch (act) { 844 case XDP_PASS: 845 return act; 846 847 case XDP_TX: 848 stats->xdp_tx++; 849 xdpf = xdp_convert_buff_to_frame(xdp); 850 if (unlikely(!xdpf)) { 851 netdev_dbg(dev, "convert buff to frame failed for xdp\n"); 852 return XDP_DROP; 853 } 854 855 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0); 856 if (unlikely(!err)) { 857 xdp_return_frame_rx_napi(xdpf); 858 } else if (unlikely(err < 0)) { 859 trace_xdp_exception(dev, xdp_prog, act); 860 return XDP_DROP; 861 } 862 *xdp_xmit |= VIRTIO_XDP_TX; 863 return act; 864 865 case XDP_REDIRECT: 866 stats->xdp_redirects++; 867 err = xdp_do_redirect(dev, xdp, xdp_prog); 868 if (err) 869 return XDP_DROP; 870 871 *xdp_xmit |= VIRTIO_XDP_REDIR; 872 return act; 873 874 default: 875 bpf_warn_invalid_xdp_action(dev, xdp_prog, act); 876 fallthrough; 877 case XDP_ABORTED: 878 trace_xdp_exception(dev, xdp_prog, act); 879 fallthrough; 880 case XDP_DROP: 881 return XDP_DROP; 882 } 883 } 884 885 static unsigned int virtnet_get_headroom(struct virtnet_info *vi) 886 { 887 return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0; 888 } 889 890 /* We copy the packet for XDP in the following cases: 891 * 892 * 1) Packet is scattered across multiple rx buffers. 893 * 2) Headroom space is insufficient. 894 * 895 * This is inefficient but it's a temporary condition that 896 * we hit right after XDP is enabled and until queue is refilled 897 * with large buffers with sufficient headroom - so it should affect 898 * at most queue size packets. 899 * Afterwards, the conditions to enable 900 * XDP should preclude the underlying device from sending packets 901 * across multiple buffers (num_buf > 1), and we make sure buffers 902 * have enough headroom. 903 */ 904 static struct page *xdp_linearize_page(struct receive_queue *rq, 905 int *num_buf, 906 struct page *p, 907 int offset, 908 int page_off, 909 unsigned int *len) 910 { 911 int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 912 struct page *page; 913 914 if (page_off + *len + tailroom > PAGE_SIZE) 915 return NULL; 916 917 page = alloc_page(GFP_ATOMIC); 918 if (!page) 919 return NULL; 920 921 memcpy(page_address(page) + page_off, page_address(p) + offset, *len); 922 page_off += *len; 923 924 while (--*num_buf) { 925 unsigned int buflen; 926 void *buf; 927 int off; 928 929 buf = virtqueue_get_buf(rq->vq, &buflen); 930 if (unlikely(!buf)) 931 goto err_buf; 932 933 p = virt_to_head_page(buf); 934 off = buf - page_address(p); 935 936 /* guard against a misconfigured or uncooperative backend that 937 * is sending packet larger than the MTU. 938 */ 939 if ((page_off + buflen + tailroom) > PAGE_SIZE) { 940 put_page(p); 941 goto err_buf; 942 } 943 944 memcpy(page_address(page) + page_off, 945 page_address(p) + off, buflen); 946 page_off += buflen; 947 put_page(p); 948 } 949 950 /* Headroom does not contribute to packet length */ 951 *len = page_off - VIRTIO_XDP_HEADROOM; 952 return page; 953 err_buf: 954 __free_pages(page, 0); 955 return NULL; 956 } 957 958 static struct sk_buff *receive_small_build_skb(struct virtnet_info *vi, 959 unsigned int xdp_headroom, 960 void *buf, 961 unsigned int len) 962 { 963 unsigned int header_offset; 964 unsigned int headroom; 965 unsigned int buflen; 966 struct sk_buff *skb; 967 968 header_offset = VIRTNET_RX_PAD + xdp_headroom; 969 headroom = vi->hdr_len + header_offset; 970 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) + 971 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 972 973 skb = virtnet_build_skb(buf, buflen, headroom, len); 974 if (unlikely(!skb)) 975 return NULL; 976 977 buf += header_offset; 978 memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len); 979 980 return skb; 981 } 982 983 static struct sk_buff *receive_small_xdp(struct net_device *dev, 984 struct virtnet_info *vi, 985 struct receive_queue *rq, 986 struct bpf_prog *xdp_prog, 987 void *buf, 988 unsigned int xdp_headroom, 989 unsigned int len, 990 unsigned int *xdp_xmit, 991 struct virtnet_rq_stats *stats) 992 { 993 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom; 994 unsigned int headroom = vi->hdr_len + header_offset; 995 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset; 996 struct page *page = virt_to_head_page(buf); 997 struct page *xdp_page; 998 unsigned int buflen; 999 struct xdp_buff xdp; 1000 struct sk_buff *skb; 1001 unsigned int metasize = 0; 1002 u32 act; 1003 1004 if (unlikely(hdr->hdr.gso_type)) 1005 goto err_xdp; 1006 1007 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) + 1008 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 1009 1010 if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) { 1011 int offset = buf - page_address(page) + header_offset; 1012 unsigned int tlen = len + vi->hdr_len; 1013 int num_buf = 1; 1014 1015 xdp_headroom = virtnet_get_headroom(vi); 1016 header_offset = VIRTNET_RX_PAD + xdp_headroom; 1017 headroom = vi->hdr_len + header_offset; 1018 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) + 1019 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 1020 xdp_page = xdp_linearize_page(rq, &num_buf, page, 1021 offset, header_offset, 1022 &tlen); 1023 if (!xdp_page) 1024 goto err_xdp; 1025 1026 buf = page_address(xdp_page); 1027 put_page(page); 1028 page = xdp_page; 1029 } 1030 1031 xdp_init_buff(&xdp, buflen, &rq->xdp_rxq); 1032 xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len, 1033 xdp_headroom, len, true); 1034 1035 act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats); 1036 1037 switch (act) { 1038 case XDP_PASS: 1039 /* Recalculate length in case bpf program changed it */ 1040 len = xdp.data_end - xdp.data; 1041 metasize = xdp.data - xdp.data_meta; 1042 break; 1043 1044 case XDP_TX: 1045 case XDP_REDIRECT: 1046 goto xdp_xmit; 1047 1048 default: 1049 goto err_xdp; 1050 } 1051 1052 skb = virtnet_build_skb(buf, buflen, xdp.data - buf, len); 1053 if (unlikely(!skb)) 1054 goto err; 1055 1056 if (metasize) 1057 skb_metadata_set(skb, metasize); 1058 1059 return skb; 1060 1061 err_xdp: 1062 stats->xdp_drops++; 1063 err: 1064 stats->drops++; 1065 put_page(page); 1066 xdp_xmit: 1067 return NULL; 1068 } 1069 1070 static struct sk_buff *receive_small(struct net_device *dev, 1071 struct virtnet_info *vi, 1072 struct receive_queue *rq, 1073 void *buf, void *ctx, 1074 unsigned int len, 1075 unsigned int *xdp_xmit, 1076 struct virtnet_rq_stats *stats) 1077 { 1078 unsigned int xdp_headroom = (unsigned long)ctx; 1079 struct page *page = virt_to_head_page(buf); 1080 struct sk_buff *skb; 1081 1082 len -= vi->hdr_len; 1083 stats->bytes += len; 1084 1085 if (unlikely(len > GOOD_PACKET_LEN)) { 1086 pr_debug("%s: rx error: len %u exceeds max size %d\n", 1087 dev->name, len, GOOD_PACKET_LEN); 1088 dev->stats.rx_length_errors++; 1089 goto err; 1090 } 1091 1092 if (unlikely(vi->xdp_enabled)) { 1093 struct bpf_prog *xdp_prog; 1094 1095 rcu_read_lock(); 1096 xdp_prog = rcu_dereference(rq->xdp_prog); 1097 if (xdp_prog) { 1098 skb = receive_small_xdp(dev, vi, rq, xdp_prog, buf, 1099 xdp_headroom, len, xdp_xmit, 1100 stats); 1101 rcu_read_unlock(); 1102 return skb; 1103 } 1104 rcu_read_unlock(); 1105 } 1106 1107 skb = receive_small_build_skb(vi, xdp_headroom, buf, len); 1108 if (likely(skb)) 1109 return skb; 1110 1111 err: 1112 stats->drops++; 1113 put_page(page); 1114 return NULL; 1115 } 1116 1117 static struct sk_buff *receive_big(struct net_device *dev, 1118 struct virtnet_info *vi, 1119 struct receive_queue *rq, 1120 void *buf, 1121 unsigned int len, 1122 struct virtnet_rq_stats *stats) 1123 { 1124 struct page *page = buf; 1125 struct sk_buff *skb = 1126 page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, 0); 1127 1128 stats->bytes += len - vi->hdr_len; 1129 if (unlikely(!skb)) 1130 goto err; 1131 1132 return skb; 1133 1134 err: 1135 stats->drops++; 1136 give_pages(rq, page); 1137 return NULL; 1138 } 1139 1140 static void mergeable_buf_free(struct receive_queue *rq, int num_buf, 1141 struct net_device *dev, 1142 struct virtnet_rq_stats *stats) 1143 { 1144 struct page *page; 1145 void *buf; 1146 int len; 1147 1148 while (num_buf-- > 1) { 1149 buf = virtqueue_get_buf(rq->vq, &len); 1150 if (unlikely(!buf)) { 1151 pr_debug("%s: rx error: %d buffers missing\n", 1152 dev->name, num_buf); 1153 dev->stats.rx_length_errors++; 1154 break; 1155 } 1156 stats->bytes += len; 1157 page = virt_to_head_page(buf); 1158 put_page(page); 1159 } 1160 } 1161 1162 /* Why not use xdp_build_skb_from_frame() ? 1163 * XDP core assumes that xdp frags are PAGE_SIZE in length, while in 1164 * virtio-net there are 2 points that do not match its requirements: 1165 * 1. The size of the prefilled buffer is not fixed before xdp is set. 1166 * 2. xdp_build_skb_from_frame() does more checks that we don't need, 1167 * like eth_type_trans() (which virtio-net does in receive_buf()). 1168 */ 1169 static struct sk_buff *build_skb_from_xdp_buff(struct net_device *dev, 1170 struct virtnet_info *vi, 1171 struct xdp_buff *xdp, 1172 unsigned int xdp_frags_truesz) 1173 { 1174 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp); 1175 unsigned int headroom, data_len; 1176 struct sk_buff *skb; 1177 int metasize; 1178 u8 nr_frags; 1179 1180 if (unlikely(xdp->data_end > xdp_data_hard_end(xdp))) { 1181 pr_debug("Error building skb as missing reserved tailroom for xdp"); 1182 return NULL; 1183 } 1184 1185 if (unlikely(xdp_buff_has_frags(xdp))) 1186 nr_frags = sinfo->nr_frags; 1187 1188 skb = build_skb(xdp->data_hard_start, xdp->frame_sz); 1189 if (unlikely(!skb)) 1190 return NULL; 1191 1192 headroom = xdp->data - xdp->data_hard_start; 1193 data_len = xdp->data_end - xdp->data; 1194 skb_reserve(skb, headroom); 1195 __skb_put(skb, data_len); 1196 1197 metasize = xdp->data - xdp->data_meta; 1198 metasize = metasize > 0 ? metasize : 0; 1199 if (metasize) 1200 skb_metadata_set(skb, metasize); 1201 1202 if (unlikely(xdp_buff_has_frags(xdp))) 1203 xdp_update_skb_shared_info(skb, nr_frags, 1204 sinfo->xdp_frags_size, 1205 xdp_frags_truesz, 1206 xdp_buff_is_frag_pfmemalloc(xdp)); 1207 1208 return skb; 1209 } 1210 1211 /* TODO: build xdp in big mode */ 1212 static int virtnet_build_xdp_buff_mrg(struct net_device *dev, 1213 struct virtnet_info *vi, 1214 struct receive_queue *rq, 1215 struct xdp_buff *xdp, 1216 void *buf, 1217 unsigned int len, 1218 unsigned int frame_sz, 1219 int *num_buf, 1220 unsigned int *xdp_frags_truesize, 1221 struct virtnet_rq_stats *stats) 1222 { 1223 struct virtio_net_hdr_mrg_rxbuf *hdr = buf; 1224 unsigned int headroom, tailroom, room; 1225 unsigned int truesize, cur_frag_size; 1226 struct skb_shared_info *shinfo; 1227 unsigned int xdp_frags_truesz = 0; 1228 struct page *page; 1229 skb_frag_t *frag; 1230 int offset; 1231 void *ctx; 1232 1233 xdp_init_buff(xdp, frame_sz, &rq->xdp_rxq); 1234 xdp_prepare_buff(xdp, buf - VIRTIO_XDP_HEADROOM, 1235 VIRTIO_XDP_HEADROOM + vi->hdr_len, len - vi->hdr_len, true); 1236 1237 if (!*num_buf) 1238 return 0; 1239 1240 if (*num_buf > 1) { 1241 /* If we want to build multi-buffer xdp, we need 1242 * to specify that the flags of xdp_buff have the 1243 * XDP_FLAGS_HAS_FRAG bit. 1244 */ 1245 if (!xdp_buff_has_frags(xdp)) 1246 xdp_buff_set_frags_flag(xdp); 1247 1248 shinfo = xdp_get_shared_info_from_buff(xdp); 1249 shinfo->nr_frags = 0; 1250 shinfo->xdp_frags_size = 0; 1251 } 1252 1253 if (*num_buf > MAX_SKB_FRAGS + 1) 1254 return -EINVAL; 1255 1256 while (--*num_buf > 0) { 1257 buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx); 1258 if (unlikely(!buf)) { 1259 pr_debug("%s: rx error: %d buffers out of %d missing\n", 1260 dev->name, *num_buf, 1261 virtio16_to_cpu(vi->vdev, hdr->num_buffers)); 1262 dev->stats.rx_length_errors++; 1263 goto err; 1264 } 1265 1266 stats->bytes += len; 1267 page = virt_to_head_page(buf); 1268 offset = buf - page_address(page); 1269 1270 truesize = mergeable_ctx_to_truesize(ctx); 1271 headroom = mergeable_ctx_to_headroom(ctx); 1272 tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 1273 room = SKB_DATA_ALIGN(headroom + tailroom); 1274 1275 cur_frag_size = truesize; 1276 xdp_frags_truesz += cur_frag_size; 1277 if (unlikely(len > truesize - room || cur_frag_size > PAGE_SIZE)) { 1278 put_page(page); 1279 pr_debug("%s: rx error: len %u exceeds truesize %lu\n", 1280 dev->name, len, (unsigned long)(truesize - room)); 1281 dev->stats.rx_length_errors++; 1282 goto err; 1283 } 1284 1285 frag = &shinfo->frags[shinfo->nr_frags++]; 1286 skb_frag_fill_page_desc(frag, page, offset, len); 1287 if (page_is_pfmemalloc(page)) 1288 xdp_buff_set_frag_pfmemalloc(xdp); 1289 1290 shinfo->xdp_frags_size += len; 1291 } 1292 1293 *xdp_frags_truesize = xdp_frags_truesz; 1294 return 0; 1295 1296 err: 1297 put_xdp_frags(xdp); 1298 return -EINVAL; 1299 } 1300 1301 static void *mergeable_xdp_get_buf(struct virtnet_info *vi, 1302 struct receive_queue *rq, 1303 struct bpf_prog *xdp_prog, 1304 void *ctx, 1305 unsigned int *frame_sz, 1306 int *num_buf, 1307 struct page **page, 1308 int offset, 1309 unsigned int *len, 1310 struct virtio_net_hdr_mrg_rxbuf *hdr) 1311 { 1312 unsigned int truesize = mergeable_ctx_to_truesize(ctx); 1313 unsigned int headroom = mergeable_ctx_to_headroom(ctx); 1314 struct page *xdp_page; 1315 unsigned int xdp_room; 1316 1317 /* Transient failure which in theory could occur if 1318 * in-flight packets from before XDP was enabled reach 1319 * the receive path after XDP is loaded. 1320 */ 1321 if (unlikely(hdr->hdr.gso_type)) 1322 return NULL; 1323 1324 /* Now XDP core assumes frag size is PAGE_SIZE, but buffers 1325 * with headroom may add hole in truesize, which 1326 * make their length exceed PAGE_SIZE. So we disabled the 1327 * hole mechanism for xdp. See add_recvbuf_mergeable(). 1328 */ 1329 *frame_sz = truesize; 1330 1331 if (likely(headroom >= virtnet_get_headroom(vi) && 1332 (*num_buf == 1 || xdp_prog->aux->xdp_has_frags))) { 1333 return page_address(*page) + offset; 1334 } 1335 1336 /* This happens when headroom is not enough because 1337 * of the buffer was prefilled before XDP is set. 1338 * This should only happen for the first several packets. 1339 * In fact, vq reset can be used here to help us clean up 1340 * the prefilled buffers, but many existing devices do not 1341 * support it, and we don't want to bother users who are 1342 * using xdp normally. 1343 */ 1344 if (!xdp_prog->aux->xdp_has_frags) { 1345 /* linearize data for XDP */ 1346 xdp_page = xdp_linearize_page(rq, num_buf, 1347 *page, offset, 1348 VIRTIO_XDP_HEADROOM, 1349 len); 1350 if (!xdp_page) 1351 return NULL; 1352 } else { 1353 xdp_room = SKB_DATA_ALIGN(VIRTIO_XDP_HEADROOM + 1354 sizeof(struct skb_shared_info)); 1355 if (*len + xdp_room > PAGE_SIZE) 1356 return NULL; 1357 1358 xdp_page = alloc_page(GFP_ATOMIC); 1359 if (!xdp_page) 1360 return NULL; 1361 1362 memcpy(page_address(xdp_page) + VIRTIO_XDP_HEADROOM, 1363 page_address(*page) + offset, *len); 1364 } 1365 1366 *frame_sz = PAGE_SIZE; 1367 1368 put_page(*page); 1369 1370 *page = xdp_page; 1371 1372 return page_address(*page) + VIRTIO_XDP_HEADROOM; 1373 } 1374 1375 static struct sk_buff *receive_mergeable_xdp(struct net_device *dev, 1376 struct virtnet_info *vi, 1377 struct receive_queue *rq, 1378 struct bpf_prog *xdp_prog, 1379 void *buf, 1380 void *ctx, 1381 unsigned int len, 1382 unsigned int *xdp_xmit, 1383 struct virtnet_rq_stats *stats) 1384 { 1385 struct virtio_net_hdr_mrg_rxbuf *hdr = buf; 1386 int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers); 1387 struct page *page = virt_to_head_page(buf); 1388 int offset = buf - page_address(page); 1389 unsigned int xdp_frags_truesz = 0; 1390 struct sk_buff *head_skb; 1391 unsigned int frame_sz; 1392 struct xdp_buff xdp; 1393 void *data; 1394 u32 act; 1395 int err; 1396 1397 data = mergeable_xdp_get_buf(vi, rq, xdp_prog, ctx, &frame_sz, &num_buf, &page, 1398 offset, &len, hdr); 1399 if (unlikely(!data)) 1400 goto err_xdp; 1401 1402 err = virtnet_build_xdp_buff_mrg(dev, vi, rq, &xdp, data, len, frame_sz, 1403 &num_buf, &xdp_frags_truesz, stats); 1404 if (unlikely(err)) 1405 goto err_xdp; 1406 1407 act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats); 1408 1409 switch (act) { 1410 case XDP_PASS: 1411 head_skb = build_skb_from_xdp_buff(dev, vi, &xdp, xdp_frags_truesz); 1412 if (unlikely(!head_skb)) 1413 break; 1414 return head_skb; 1415 1416 case XDP_TX: 1417 case XDP_REDIRECT: 1418 return NULL; 1419 1420 default: 1421 break; 1422 } 1423 1424 put_xdp_frags(&xdp); 1425 1426 err_xdp: 1427 put_page(page); 1428 mergeable_buf_free(rq, num_buf, dev, stats); 1429 1430 stats->xdp_drops++; 1431 stats->drops++; 1432 return NULL; 1433 } 1434 1435 static struct sk_buff *receive_mergeable(struct net_device *dev, 1436 struct virtnet_info *vi, 1437 struct receive_queue *rq, 1438 void *buf, 1439 void *ctx, 1440 unsigned int len, 1441 unsigned int *xdp_xmit, 1442 struct virtnet_rq_stats *stats) 1443 { 1444 struct virtio_net_hdr_mrg_rxbuf *hdr = buf; 1445 int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers); 1446 struct page *page = virt_to_head_page(buf); 1447 int offset = buf - page_address(page); 1448 struct sk_buff *head_skb, *curr_skb; 1449 unsigned int truesize = mergeable_ctx_to_truesize(ctx); 1450 unsigned int headroom = mergeable_ctx_to_headroom(ctx); 1451 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 1452 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom); 1453 1454 head_skb = NULL; 1455 stats->bytes += len - vi->hdr_len; 1456 1457 if (unlikely(len > truesize - room)) { 1458 pr_debug("%s: rx error: len %u exceeds truesize %lu\n", 1459 dev->name, len, (unsigned long)(truesize - room)); 1460 dev->stats.rx_length_errors++; 1461 goto err_skb; 1462 } 1463 1464 if (unlikely(vi->xdp_enabled)) { 1465 struct bpf_prog *xdp_prog; 1466 1467 rcu_read_lock(); 1468 xdp_prog = rcu_dereference(rq->xdp_prog); 1469 if (xdp_prog) { 1470 head_skb = receive_mergeable_xdp(dev, vi, rq, xdp_prog, buf, ctx, 1471 len, xdp_xmit, stats); 1472 rcu_read_unlock(); 1473 return head_skb; 1474 } 1475 rcu_read_unlock(); 1476 } 1477 1478 head_skb = page_to_skb(vi, rq, page, offset, len, truesize, headroom); 1479 curr_skb = head_skb; 1480 1481 if (unlikely(!curr_skb)) 1482 goto err_skb; 1483 while (--num_buf) { 1484 int num_skb_frags; 1485 1486 buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx); 1487 if (unlikely(!buf)) { 1488 pr_debug("%s: rx error: %d buffers out of %d missing\n", 1489 dev->name, num_buf, 1490 virtio16_to_cpu(vi->vdev, 1491 hdr->num_buffers)); 1492 dev->stats.rx_length_errors++; 1493 goto err_buf; 1494 } 1495 1496 stats->bytes += len; 1497 page = virt_to_head_page(buf); 1498 1499 truesize = mergeable_ctx_to_truesize(ctx); 1500 headroom = mergeable_ctx_to_headroom(ctx); 1501 tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 1502 room = SKB_DATA_ALIGN(headroom + tailroom); 1503 if (unlikely(len > truesize - room)) { 1504 pr_debug("%s: rx error: len %u exceeds truesize %lu\n", 1505 dev->name, len, (unsigned long)(truesize - room)); 1506 dev->stats.rx_length_errors++; 1507 goto err_skb; 1508 } 1509 1510 num_skb_frags = skb_shinfo(curr_skb)->nr_frags; 1511 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) { 1512 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC); 1513 1514 if (unlikely(!nskb)) 1515 goto err_skb; 1516 if (curr_skb == head_skb) 1517 skb_shinfo(curr_skb)->frag_list = nskb; 1518 else 1519 curr_skb->next = nskb; 1520 curr_skb = nskb; 1521 head_skb->truesize += nskb->truesize; 1522 num_skb_frags = 0; 1523 } 1524 if (curr_skb != head_skb) { 1525 head_skb->data_len += len; 1526 head_skb->len += len; 1527 head_skb->truesize += truesize; 1528 } 1529 offset = buf - page_address(page); 1530 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) { 1531 put_page(page); 1532 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1, 1533 len, truesize); 1534 } else { 1535 skb_add_rx_frag(curr_skb, num_skb_frags, page, 1536 offset, len, truesize); 1537 } 1538 } 1539 1540 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len); 1541 return head_skb; 1542 1543 err_skb: 1544 put_page(page); 1545 mergeable_buf_free(rq, num_buf, dev, stats); 1546 1547 err_buf: 1548 stats->drops++; 1549 dev_kfree_skb(head_skb); 1550 return NULL; 1551 } 1552 1553 static void virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash *hdr_hash, 1554 struct sk_buff *skb) 1555 { 1556 enum pkt_hash_types rss_hash_type; 1557 1558 if (!hdr_hash || !skb) 1559 return; 1560 1561 switch (__le16_to_cpu(hdr_hash->hash_report)) { 1562 case VIRTIO_NET_HASH_REPORT_TCPv4: 1563 case VIRTIO_NET_HASH_REPORT_UDPv4: 1564 case VIRTIO_NET_HASH_REPORT_TCPv6: 1565 case VIRTIO_NET_HASH_REPORT_UDPv6: 1566 case VIRTIO_NET_HASH_REPORT_TCPv6_EX: 1567 case VIRTIO_NET_HASH_REPORT_UDPv6_EX: 1568 rss_hash_type = PKT_HASH_TYPE_L4; 1569 break; 1570 case VIRTIO_NET_HASH_REPORT_IPv4: 1571 case VIRTIO_NET_HASH_REPORT_IPv6: 1572 case VIRTIO_NET_HASH_REPORT_IPv6_EX: 1573 rss_hash_type = PKT_HASH_TYPE_L3; 1574 break; 1575 case VIRTIO_NET_HASH_REPORT_NONE: 1576 default: 1577 rss_hash_type = PKT_HASH_TYPE_NONE; 1578 } 1579 skb_set_hash(skb, __le32_to_cpu(hdr_hash->hash_value), rss_hash_type); 1580 } 1581 1582 static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq, 1583 void *buf, unsigned int len, void **ctx, 1584 unsigned int *xdp_xmit, 1585 struct virtnet_rq_stats *stats) 1586 { 1587 struct net_device *dev = vi->dev; 1588 struct sk_buff *skb; 1589 struct virtio_net_hdr_mrg_rxbuf *hdr; 1590 1591 if (unlikely(len < vi->hdr_len + ETH_HLEN)) { 1592 pr_debug("%s: short packet %i\n", dev->name, len); 1593 dev->stats.rx_length_errors++; 1594 virtnet_rq_free_unused_buf(rq->vq, buf); 1595 return; 1596 } 1597 1598 if (vi->mergeable_rx_bufs) 1599 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit, 1600 stats); 1601 else if (vi->big_packets) 1602 skb = receive_big(dev, vi, rq, buf, len, stats); 1603 else 1604 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats); 1605 1606 if (unlikely(!skb)) 1607 return; 1608 1609 hdr = skb_vnet_hdr(skb); 1610 if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report) 1611 virtio_skb_set_hash((const struct virtio_net_hdr_v1_hash *)hdr, skb); 1612 1613 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) 1614 skb->ip_summed = CHECKSUM_UNNECESSARY; 1615 1616 if (virtio_net_hdr_to_skb(skb, &hdr->hdr, 1617 virtio_is_little_endian(vi->vdev))) { 1618 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n", 1619 dev->name, hdr->hdr.gso_type, 1620 hdr->hdr.gso_size); 1621 goto frame_err; 1622 } 1623 1624 skb_record_rx_queue(skb, vq2rxq(rq->vq)); 1625 skb->protocol = eth_type_trans(skb, dev); 1626 pr_debug("Receiving skb proto 0x%04x len %i type %i\n", 1627 ntohs(skb->protocol), skb->len, skb->pkt_type); 1628 1629 napi_gro_receive(&rq->napi, skb); 1630 return; 1631 1632 frame_err: 1633 dev->stats.rx_frame_errors++; 1634 dev_kfree_skb(skb); 1635 } 1636 1637 /* Unlike mergeable buffers, all buffers are allocated to the 1638 * same size, except for the headroom. For this reason we do 1639 * not need to use mergeable_len_to_ctx here - it is enough 1640 * to store the headroom as the context ignoring the truesize. 1641 */ 1642 static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq, 1643 gfp_t gfp) 1644 { 1645 struct page_frag *alloc_frag = &rq->alloc_frag; 1646 char *buf; 1647 unsigned int xdp_headroom = virtnet_get_headroom(vi); 1648 void *ctx = (void *)(unsigned long)xdp_headroom; 1649 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom; 1650 int err; 1651 1652 len = SKB_DATA_ALIGN(len) + 1653 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 1654 if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp))) 1655 return -ENOMEM; 1656 1657 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 1658 get_page(alloc_frag->page); 1659 alloc_frag->offset += len; 1660 sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom, 1661 vi->hdr_len + GOOD_PACKET_LEN); 1662 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp); 1663 if (err < 0) 1664 put_page(virt_to_head_page(buf)); 1665 return err; 1666 } 1667 1668 static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq, 1669 gfp_t gfp) 1670 { 1671 struct page *first, *list = NULL; 1672 char *p; 1673 int i, err, offset; 1674 1675 sg_init_table(rq->sg, vi->big_packets_num_skbfrags + 2); 1676 1677 /* page in rq->sg[vi->big_packets_num_skbfrags + 1] is list tail */ 1678 for (i = vi->big_packets_num_skbfrags + 1; i > 1; --i) { 1679 first = get_a_page(rq, gfp); 1680 if (!first) { 1681 if (list) 1682 give_pages(rq, list); 1683 return -ENOMEM; 1684 } 1685 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE); 1686 1687 /* chain new page in list head to match sg */ 1688 first->private = (unsigned long)list; 1689 list = first; 1690 } 1691 1692 first = get_a_page(rq, gfp); 1693 if (!first) { 1694 give_pages(rq, list); 1695 return -ENOMEM; 1696 } 1697 p = page_address(first); 1698 1699 /* rq->sg[0], rq->sg[1] share the same page */ 1700 /* a separated rq->sg[0] for header - required in case !any_header_sg */ 1701 sg_set_buf(&rq->sg[0], p, vi->hdr_len); 1702 1703 /* rq->sg[1] for data packet, from offset */ 1704 offset = sizeof(struct padded_vnet_hdr); 1705 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset); 1706 1707 /* chain first in list head */ 1708 first->private = (unsigned long)list; 1709 err = virtqueue_add_inbuf(rq->vq, rq->sg, vi->big_packets_num_skbfrags + 2, 1710 first, gfp); 1711 if (err < 0) 1712 give_pages(rq, first); 1713 1714 return err; 1715 } 1716 1717 static unsigned int get_mergeable_buf_len(struct receive_queue *rq, 1718 struct ewma_pkt_len *avg_pkt_len, 1719 unsigned int room) 1720 { 1721 struct virtnet_info *vi = rq->vq->vdev->priv; 1722 const size_t hdr_len = vi->hdr_len; 1723 unsigned int len; 1724 1725 if (room) 1726 return PAGE_SIZE - room; 1727 1728 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len), 1729 rq->min_buf_len, PAGE_SIZE - hdr_len); 1730 1731 return ALIGN(len, L1_CACHE_BYTES); 1732 } 1733 1734 static int add_recvbuf_mergeable(struct virtnet_info *vi, 1735 struct receive_queue *rq, gfp_t gfp) 1736 { 1737 struct page_frag *alloc_frag = &rq->alloc_frag; 1738 unsigned int headroom = virtnet_get_headroom(vi); 1739 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 1740 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom); 1741 char *buf; 1742 void *ctx; 1743 int err; 1744 unsigned int len, hole; 1745 1746 /* Extra tailroom is needed to satisfy XDP's assumption. This 1747 * means rx frags coalescing won't work, but consider we've 1748 * disabled GSO for XDP, it won't be a big issue. 1749 */ 1750 len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room); 1751 if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp))) 1752 return -ENOMEM; 1753 1754 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 1755 buf += headroom; /* advance address leaving hole at front of pkt */ 1756 get_page(alloc_frag->page); 1757 alloc_frag->offset += len + room; 1758 hole = alloc_frag->size - alloc_frag->offset; 1759 if (hole < len + room) { 1760 /* To avoid internal fragmentation, if there is very likely not 1761 * enough space for another buffer, add the remaining space to 1762 * the current buffer. 1763 * XDP core assumes that frame_size of xdp_buff and the length 1764 * of the frag are PAGE_SIZE, so we disable the hole mechanism. 1765 */ 1766 if (!headroom) 1767 len += hole; 1768 alloc_frag->offset += hole; 1769 } 1770 1771 sg_init_one(rq->sg, buf, len); 1772 ctx = mergeable_len_to_ctx(len + room, headroom); 1773 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp); 1774 if (err < 0) 1775 put_page(virt_to_head_page(buf)); 1776 1777 return err; 1778 } 1779 1780 /* 1781 * Returns false if we couldn't fill entirely (OOM). 1782 * 1783 * Normally run in the receive path, but can also be run from ndo_open 1784 * before we're receiving packets, or from refill_work which is 1785 * careful to disable receiving (using napi_disable). 1786 */ 1787 static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq, 1788 gfp_t gfp) 1789 { 1790 int err; 1791 bool oom; 1792 1793 do { 1794 if (vi->mergeable_rx_bufs) 1795 err = add_recvbuf_mergeable(vi, rq, gfp); 1796 else if (vi->big_packets) 1797 err = add_recvbuf_big(vi, rq, gfp); 1798 else 1799 err = add_recvbuf_small(vi, rq, gfp); 1800 1801 oom = err == -ENOMEM; 1802 if (err) 1803 break; 1804 } while (rq->vq->num_free); 1805 if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) { 1806 unsigned long flags; 1807 1808 flags = u64_stats_update_begin_irqsave(&rq->stats.syncp); 1809 rq->stats.kicks++; 1810 u64_stats_update_end_irqrestore(&rq->stats.syncp, flags); 1811 } 1812 1813 return !oom; 1814 } 1815 1816 static void skb_recv_done(struct virtqueue *rvq) 1817 { 1818 struct virtnet_info *vi = rvq->vdev->priv; 1819 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)]; 1820 1821 virtqueue_napi_schedule(&rq->napi, rvq); 1822 } 1823 1824 static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi) 1825 { 1826 napi_enable(napi); 1827 1828 /* If all buffers were filled by other side before we napi_enabled, we 1829 * won't get another interrupt, so process any outstanding packets now. 1830 * Call local_bh_enable after to trigger softIRQ processing. 1831 */ 1832 local_bh_disable(); 1833 virtqueue_napi_schedule(napi, vq); 1834 local_bh_enable(); 1835 } 1836 1837 static void virtnet_napi_tx_enable(struct virtnet_info *vi, 1838 struct virtqueue *vq, 1839 struct napi_struct *napi) 1840 { 1841 if (!napi->weight) 1842 return; 1843 1844 /* Tx napi touches cachelines on the cpu handling tx interrupts. Only 1845 * enable the feature if this is likely affine with the transmit path. 1846 */ 1847 if (!vi->affinity_hint_set) { 1848 napi->weight = 0; 1849 return; 1850 } 1851 1852 return virtnet_napi_enable(vq, napi); 1853 } 1854 1855 static void virtnet_napi_tx_disable(struct napi_struct *napi) 1856 { 1857 if (napi->weight) 1858 napi_disable(napi); 1859 } 1860 1861 static void refill_work(struct work_struct *work) 1862 { 1863 struct virtnet_info *vi = 1864 container_of(work, struct virtnet_info, refill.work); 1865 bool still_empty; 1866 int i; 1867 1868 for (i = 0; i < vi->curr_queue_pairs; i++) { 1869 struct receive_queue *rq = &vi->rq[i]; 1870 1871 napi_disable(&rq->napi); 1872 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL); 1873 virtnet_napi_enable(rq->vq, &rq->napi); 1874 1875 /* In theory, this can happen: if we don't get any buffers in 1876 * we will *never* try to fill again. 1877 */ 1878 if (still_empty) 1879 schedule_delayed_work(&vi->refill, HZ/2); 1880 } 1881 } 1882 1883 static int virtnet_receive(struct receive_queue *rq, int budget, 1884 unsigned int *xdp_xmit) 1885 { 1886 struct virtnet_info *vi = rq->vq->vdev->priv; 1887 struct virtnet_rq_stats stats = {}; 1888 unsigned int len; 1889 void *buf; 1890 int i; 1891 1892 if (!vi->big_packets || vi->mergeable_rx_bufs) { 1893 void *ctx; 1894 1895 while (stats.packets < budget && 1896 (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) { 1897 receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats); 1898 stats.packets++; 1899 } 1900 } else { 1901 while (stats.packets < budget && 1902 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) { 1903 receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats); 1904 stats.packets++; 1905 } 1906 } 1907 1908 if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) { 1909 if (!try_fill_recv(vi, rq, GFP_ATOMIC)) { 1910 spin_lock(&vi->refill_lock); 1911 if (vi->refill_enabled) 1912 schedule_delayed_work(&vi->refill, 0); 1913 spin_unlock(&vi->refill_lock); 1914 } 1915 } 1916 1917 u64_stats_update_begin(&rq->stats.syncp); 1918 for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) { 1919 size_t offset = virtnet_rq_stats_desc[i].offset; 1920 u64 *item; 1921 1922 item = (u64 *)((u8 *)&rq->stats + offset); 1923 *item += *(u64 *)((u8 *)&stats + offset); 1924 } 1925 u64_stats_update_end(&rq->stats.syncp); 1926 1927 return stats.packets; 1928 } 1929 1930 static void virtnet_poll_cleantx(struct receive_queue *rq) 1931 { 1932 struct virtnet_info *vi = rq->vq->vdev->priv; 1933 unsigned int index = vq2rxq(rq->vq); 1934 struct send_queue *sq = &vi->sq[index]; 1935 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index); 1936 1937 if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index)) 1938 return; 1939 1940 if (__netif_tx_trylock(txq)) { 1941 if (sq->reset) { 1942 __netif_tx_unlock(txq); 1943 return; 1944 } 1945 1946 do { 1947 virtqueue_disable_cb(sq->vq); 1948 free_old_xmit_skbs(sq, true); 1949 } while (unlikely(!virtqueue_enable_cb_delayed(sq->vq))); 1950 1951 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) 1952 netif_tx_wake_queue(txq); 1953 1954 __netif_tx_unlock(txq); 1955 } 1956 } 1957 1958 static int virtnet_poll(struct napi_struct *napi, int budget) 1959 { 1960 struct receive_queue *rq = 1961 container_of(napi, struct receive_queue, napi); 1962 struct virtnet_info *vi = rq->vq->vdev->priv; 1963 struct send_queue *sq; 1964 unsigned int received; 1965 unsigned int xdp_xmit = 0; 1966 1967 virtnet_poll_cleantx(rq); 1968 1969 received = virtnet_receive(rq, budget, &xdp_xmit); 1970 1971 if (xdp_xmit & VIRTIO_XDP_REDIR) 1972 xdp_do_flush(); 1973 1974 /* Out of packets? */ 1975 if (received < budget) 1976 virtqueue_napi_complete(napi, rq->vq, received); 1977 1978 if (xdp_xmit & VIRTIO_XDP_TX) { 1979 sq = virtnet_xdp_get_sq(vi); 1980 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) { 1981 u64_stats_update_begin(&sq->stats.syncp); 1982 sq->stats.kicks++; 1983 u64_stats_update_end(&sq->stats.syncp); 1984 } 1985 virtnet_xdp_put_sq(vi, sq); 1986 } 1987 1988 return received; 1989 } 1990 1991 static void virtnet_disable_queue_pair(struct virtnet_info *vi, int qp_index) 1992 { 1993 virtnet_napi_tx_disable(&vi->sq[qp_index].napi); 1994 napi_disable(&vi->rq[qp_index].napi); 1995 xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq); 1996 } 1997 1998 static int virtnet_enable_queue_pair(struct virtnet_info *vi, int qp_index) 1999 { 2000 struct net_device *dev = vi->dev; 2001 int err; 2002 2003 err = xdp_rxq_info_reg(&vi->rq[qp_index].xdp_rxq, dev, qp_index, 2004 vi->rq[qp_index].napi.napi_id); 2005 if (err < 0) 2006 return err; 2007 2008 err = xdp_rxq_info_reg_mem_model(&vi->rq[qp_index].xdp_rxq, 2009 MEM_TYPE_PAGE_SHARED, NULL); 2010 if (err < 0) 2011 goto err_xdp_reg_mem_model; 2012 2013 virtnet_napi_enable(vi->rq[qp_index].vq, &vi->rq[qp_index].napi); 2014 virtnet_napi_tx_enable(vi, vi->sq[qp_index].vq, &vi->sq[qp_index].napi); 2015 2016 return 0; 2017 2018 err_xdp_reg_mem_model: 2019 xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq); 2020 return err; 2021 } 2022 2023 static int virtnet_open(struct net_device *dev) 2024 { 2025 struct virtnet_info *vi = netdev_priv(dev); 2026 int i, err; 2027 2028 enable_delayed_refill(vi); 2029 2030 for (i = 0; i < vi->max_queue_pairs; i++) { 2031 if (i < vi->curr_queue_pairs) 2032 /* Make sure we have some buffers: if oom use wq. */ 2033 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 2034 schedule_delayed_work(&vi->refill, 0); 2035 2036 err = virtnet_enable_queue_pair(vi, i); 2037 if (err < 0) 2038 goto err_enable_qp; 2039 } 2040 2041 return 0; 2042 2043 err_enable_qp: 2044 disable_delayed_refill(vi); 2045 cancel_delayed_work_sync(&vi->refill); 2046 2047 for (i--; i >= 0; i--) 2048 virtnet_disable_queue_pair(vi, i); 2049 return err; 2050 } 2051 2052 static int virtnet_poll_tx(struct napi_struct *napi, int budget) 2053 { 2054 struct send_queue *sq = container_of(napi, struct send_queue, napi); 2055 struct virtnet_info *vi = sq->vq->vdev->priv; 2056 unsigned int index = vq2txq(sq->vq); 2057 struct netdev_queue *txq; 2058 int opaque; 2059 bool done; 2060 2061 if (unlikely(is_xdp_raw_buffer_queue(vi, index))) { 2062 /* We don't need to enable cb for XDP */ 2063 napi_complete_done(napi, 0); 2064 return 0; 2065 } 2066 2067 txq = netdev_get_tx_queue(vi->dev, index); 2068 __netif_tx_lock(txq, raw_smp_processor_id()); 2069 virtqueue_disable_cb(sq->vq); 2070 free_old_xmit_skbs(sq, true); 2071 2072 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) 2073 netif_tx_wake_queue(txq); 2074 2075 opaque = virtqueue_enable_cb_prepare(sq->vq); 2076 2077 done = napi_complete_done(napi, 0); 2078 2079 if (!done) 2080 virtqueue_disable_cb(sq->vq); 2081 2082 __netif_tx_unlock(txq); 2083 2084 if (done) { 2085 if (unlikely(virtqueue_poll(sq->vq, opaque))) { 2086 if (napi_schedule_prep(napi)) { 2087 __netif_tx_lock(txq, raw_smp_processor_id()); 2088 virtqueue_disable_cb(sq->vq); 2089 __netif_tx_unlock(txq); 2090 __napi_schedule(napi); 2091 } 2092 } 2093 } 2094 2095 return 0; 2096 } 2097 2098 static int xmit_skb(struct send_queue *sq, struct sk_buff *skb) 2099 { 2100 struct virtio_net_hdr_mrg_rxbuf *hdr; 2101 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; 2102 struct virtnet_info *vi = sq->vq->vdev->priv; 2103 int num_sg; 2104 unsigned hdr_len = vi->hdr_len; 2105 bool can_push; 2106 2107 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); 2108 2109 can_push = vi->any_header_sg && 2110 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) && 2111 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len; 2112 /* Even if we can, don't push here yet as this would skew 2113 * csum_start offset below. */ 2114 if (can_push) 2115 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len); 2116 else 2117 hdr = skb_vnet_hdr(skb); 2118 2119 if (virtio_net_hdr_from_skb(skb, &hdr->hdr, 2120 virtio_is_little_endian(vi->vdev), false, 2121 0)) 2122 return -EPROTO; 2123 2124 if (vi->mergeable_rx_bufs) 2125 hdr->num_buffers = 0; 2126 2127 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2)); 2128 if (can_push) { 2129 __skb_push(skb, hdr_len); 2130 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len); 2131 if (unlikely(num_sg < 0)) 2132 return num_sg; 2133 /* Pull header back to avoid skew in tx bytes calculations. */ 2134 __skb_pull(skb, hdr_len); 2135 } else { 2136 sg_set_buf(sq->sg, hdr, hdr_len); 2137 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len); 2138 if (unlikely(num_sg < 0)) 2139 return num_sg; 2140 num_sg++; 2141 } 2142 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC); 2143 } 2144 2145 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) 2146 { 2147 struct virtnet_info *vi = netdev_priv(dev); 2148 int qnum = skb_get_queue_mapping(skb); 2149 struct send_queue *sq = &vi->sq[qnum]; 2150 int err; 2151 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum); 2152 bool kick = !netdev_xmit_more(); 2153 bool use_napi = sq->napi.weight; 2154 2155 /* Free up any pending old buffers before queueing new ones. */ 2156 do { 2157 if (use_napi) 2158 virtqueue_disable_cb(sq->vq); 2159 2160 free_old_xmit_skbs(sq, false); 2161 2162 } while (use_napi && kick && 2163 unlikely(!virtqueue_enable_cb_delayed(sq->vq))); 2164 2165 /* timestamp packet in software */ 2166 skb_tx_timestamp(skb); 2167 2168 /* Try to transmit */ 2169 err = xmit_skb(sq, skb); 2170 2171 /* This should not happen! */ 2172 if (unlikely(err)) { 2173 dev->stats.tx_fifo_errors++; 2174 if (net_ratelimit()) 2175 dev_warn(&dev->dev, 2176 "Unexpected TXQ (%d) queue failure: %d\n", 2177 qnum, err); 2178 dev->stats.tx_dropped++; 2179 dev_kfree_skb_any(skb); 2180 return NETDEV_TX_OK; 2181 } 2182 2183 /* Don't wait up for transmitted skbs to be freed. */ 2184 if (!use_napi) { 2185 skb_orphan(skb); 2186 nf_reset_ct(skb); 2187 } 2188 2189 check_sq_full_and_disable(vi, dev, sq); 2190 2191 if (kick || netif_xmit_stopped(txq)) { 2192 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) { 2193 u64_stats_update_begin(&sq->stats.syncp); 2194 sq->stats.kicks++; 2195 u64_stats_update_end(&sq->stats.syncp); 2196 } 2197 } 2198 2199 return NETDEV_TX_OK; 2200 } 2201 2202 static int virtnet_rx_resize(struct virtnet_info *vi, 2203 struct receive_queue *rq, u32 ring_num) 2204 { 2205 bool running = netif_running(vi->dev); 2206 int err, qindex; 2207 2208 qindex = rq - vi->rq; 2209 2210 if (running) 2211 napi_disable(&rq->napi); 2212 2213 err = virtqueue_resize(rq->vq, ring_num, virtnet_rq_free_unused_buf); 2214 if (err) 2215 netdev_err(vi->dev, "resize rx fail: rx queue index: %d err: %d\n", qindex, err); 2216 2217 if (!try_fill_recv(vi, rq, GFP_KERNEL)) 2218 schedule_delayed_work(&vi->refill, 0); 2219 2220 if (running) 2221 virtnet_napi_enable(rq->vq, &rq->napi); 2222 return err; 2223 } 2224 2225 static int virtnet_tx_resize(struct virtnet_info *vi, 2226 struct send_queue *sq, u32 ring_num) 2227 { 2228 bool running = netif_running(vi->dev); 2229 struct netdev_queue *txq; 2230 int err, qindex; 2231 2232 qindex = sq - vi->sq; 2233 2234 if (running) 2235 virtnet_napi_tx_disable(&sq->napi); 2236 2237 txq = netdev_get_tx_queue(vi->dev, qindex); 2238 2239 /* 1. wait all ximt complete 2240 * 2. fix the race of netif_stop_subqueue() vs netif_start_subqueue() 2241 */ 2242 __netif_tx_lock_bh(txq); 2243 2244 /* Prevent rx poll from accessing sq. */ 2245 sq->reset = true; 2246 2247 /* Prevent the upper layer from trying to send packets. */ 2248 netif_stop_subqueue(vi->dev, qindex); 2249 2250 __netif_tx_unlock_bh(txq); 2251 2252 err = virtqueue_resize(sq->vq, ring_num, virtnet_sq_free_unused_buf); 2253 if (err) 2254 netdev_err(vi->dev, "resize tx fail: tx queue index: %d err: %d\n", qindex, err); 2255 2256 __netif_tx_lock_bh(txq); 2257 sq->reset = false; 2258 netif_tx_wake_queue(txq); 2259 __netif_tx_unlock_bh(txq); 2260 2261 if (running) 2262 virtnet_napi_tx_enable(vi, sq->vq, &sq->napi); 2263 return err; 2264 } 2265 2266 /* 2267 * Send command via the control virtqueue and check status. Commands 2268 * supported by the hypervisor, as indicated by feature bits, should 2269 * never fail unless improperly formatted. 2270 */ 2271 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, 2272 struct scatterlist *out) 2273 { 2274 struct scatterlist *sgs[4], hdr, stat; 2275 unsigned out_num = 0, tmp; 2276 int ret; 2277 2278 /* Caller should know better */ 2279 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)); 2280 2281 vi->ctrl->status = ~0; 2282 vi->ctrl->hdr.class = class; 2283 vi->ctrl->hdr.cmd = cmd; 2284 /* Add header */ 2285 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr)); 2286 sgs[out_num++] = &hdr; 2287 2288 if (out) 2289 sgs[out_num++] = out; 2290 2291 /* Add return status. */ 2292 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status)); 2293 sgs[out_num] = &stat; 2294 2295 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs)); 2296 ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC); 2297 if (ret < 0) { 2298 dev_warn(&vi->vdev->dev, 2299 "Failed to add sgs for command vq: %d\n.", ret); 2300 return false; 2301 } 2302 2303 if (unlikely(!virtqueue_kick(vi->cvq))) 2304 return vi->ctrl->status == VIRTIO_NET_OK; 2305 2306 /* Spin for a response, the kick causes an ioport write, trapping 2307 * into the hypervisor, so the request should be handled immediately. 2308 */ 2309 while (!virtqueue_get_buf(vi->cvq, &tmp) && 2310 !virtqueue_is_broken(vi->cvq)) 2311 cpu_relax(); 2312 2313 return vi->ctrl->status == VIRTIO_NET_OK; 2314 } 2315 2316 static int virtnet_set_mac_address(struct net_device *dev, void *p) 2317 { 2318 struct virtnet_info *vi = netdev_priv(dev); 2319 struct virtio_device *vdev = vi->vdev; 2320 int ret; 2321 struct sockaddr *addr; 2322 struct scatterlist sg; 2323 2324 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY)) 2325 return -EOPNOTSUPP; 2326 2327 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL); 2328 if (!addr) 2329 return -ENOMEM; 2330 2331 ret = eth_prepare_mac_addr_change(dev, addr); 2332 if (ret) 2333 goto out; 2334 2335 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 2336 sg_init_one(&sg, addr->sa_data, dev->addr_len); 2337 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 2338 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) { 2339 dev_warn(&vdev->dev, 2340 "Failed to set mac address by vq command.\n"); 2341 ret = -EINVAL; 2342 goto out; 2343 } 2344 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) && 2345 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) { 2346 unsigned int i; 2347 2348 /* Naturally, this has an atomicity problem. */ 2349 for (i = 0; i < dev->addr_len; i++) 2350 virtio_cwrite8(vdev, 2351 offsetof(struct virtio_net_config, mac) + 2352 i, addr->sa_data[i]); 2353 } 2354 2355 eth_commit_mac_addr_change(dev, p); 2356 ret = 0; 2357 2358 out: 2359 kfree(addr); 2360 return ret; 2361 } 2362 2363 static void virtnet_stats(struct net_device *dev, 2364 struct rtnl_link_stats64 *tot) 2365 { 2366 struct virtnet_info *vi = netdev_priv(dev); 2367 unsigned int start; 2368 int i; 2369 2370 for (i = 0; i < vi->max_queue_pairs; i++) { 2371 u64 tpackets, tbytes, terrors, rpackets, rbytes, rdrops; 2372 struct receive_queue *rq = &vi->rq[i]; 2373 struct send_queue *sq = &vi->sq[i]; 2374 2375 do { 2376 start = u64_stats_fetch_begin(&sq->stats.syncp); 2377 tpackets = sq->stats.packets; 2378 tbytes = sq->stats.bytes; 2379 terrors = sq->stats.tx_timeouts; 2380 } while (u64_stats_fetch_retry(&sq->stats.syncp, start)); 2381 2382 do { 2383 start = u64_stats_fetch_begin(&rq->stats.syncp); 2384 rpackets = rq->stats.packets; 2385 rbytes = rq->stats.bytes; 2386 rdrops = rq->stats.drops; 2387 } while (u64_stats_fetch_retry(&rq->stats.syncp, start)); 2388 2389 tot->rx_packets += rpackets; 2390 tot->tx_packets += tpackets; 2391 tot->rx_bytes += rbytes; 2392 tot->tx_bytes += tbytes; 2393 tot->rx_dropped += rdrops; 2394 tot->tx_errors += terrors; 2395 } 2396 2397 tot->tx_dropped = dev->stats.tx_dropped; 2398 tot->tx_fifo_errors = dev->stats.tx_fifo_errors; 2399 tot->rx_length_errors = dev->stats.rx_length_errors; 2400 tot->rx_frame_errors = dev->stats.rx_frame_errors; 2401 } 2402 2403 static void virtnet_ack_link_announce(struct virtnet_info *vi) 2404 { 2405 rtnl_lock(); 2406 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE, 2407 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL)) 2408 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n"); 2409 rtnl_unlock(); 2410 } 2411 2412 static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 2413 { 2414 struct scatterlist sg; 2415 struct net_device *dev = vi->dev; 2416 2417 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ)) 2418 return 0; 2419 2420 vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs); 2421 sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq)); 2422 2423 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, 2424 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) { 2425 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n", 2426 queue_pairs); 2427 return -EINVAL; 2428 } else { 2429 vi->curr_queue_pairs = queue_pairs; 2430 /* virtnet_open() will refill when device is going to up. */ 2431 if (dev->flags & IFF_UP) 2432 schedule_delayed_work(&vi->refill, 0); 2433 } 2434 2435 return 0; 2436 } 2437 2438 static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 2439 { 2440 int err; 2441 2442 rtnl_lock(); 2443 err = _virtnet_set_queues(vi, queue_pairs); 2444 rtnl_unlock(); 2445 return err; 2446 } 2447 2448 static int virtnet_close(struct net_device *dev) 2449 { 2450 struct virtnet_info *vi = netdev_priv(dev); 2451 int i; 2452 2453 /* Make sure NAPI doesn't schedule refill work */ 2454 disable_delayed_refill(vi); 2455 /* Make sure refill_work doesn't re-enable napi! */ 2456 cancel_delayed_work_sync(&vi->refill); 2457 2458 for (i = 0; i < vi->max_queue_pairs; i++) 2459 virtnet_disable_queue_pair(vi, i); 2460 2461 return 0; 2462 } 2463 2464 static void virtnet_set_rx_mode(struct net_device *dev) 2465 { 2466 struct virtnet_info *vi = netdev_priv(dev); 2467 struct scatterlist sg[2]; 2468 struct virtio_net_ctrl_mac *mac_data; 2469 struct netdev_hw_addr *ha; 2470 int uc_count; 2471 int mc_count; 2472 void *buf; 2473 int i; 2474 2475 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */ 2476 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) 2477 return; 2478 2479 vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0); 2480 vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0); 2481 2482 sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc)); 2483 2484 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 2485 VIRTIO_NET_CTRL_RX_PROMISC, sg)) 2486 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", 2487 vi->ctrl->promisc ? "en" : "dis"); 2488 2489 sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti)); 2490 2491 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 2492 VIRTIO_NET_CTRL_RX_ALLMULTI, sg)) 2493 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", 2494 vi->ctrl->allmulti ? "en" : "dis"); 2495 2496 uc_count = netdev_uc_count(dev); 2497 mc_count = netdev_mc_count(dev); 2498 /* MAC filter - use one buffer for both lists */ 2499 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) + 2500 (2 * sizeof(mac_data->entries)), GFP_ATOMIC); 2501 mac_data = buf; 2502 if (!buf) 2503 return; 2504 2505 sg_init_table(sg, 2); 2506 2507 /* Store the unicast list and count in the front of the buffer */ 2508 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count); 2509 i = 0; 2510 netdev_for_each_uc_addr(ha, dev) 2511 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 2512 2513 sg_set_buf(&sg[0], mac_data, 2514 sizeof(mac_data->entries) + (uc_count * ETH_ALEN)); 2515 2516 /* multicast list and count fill the end */ 2517 mac_data = (void *)&mac_data->macs[uc_count][0]; 2518 2519 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count); 2520 i = 0; 2521 netdev_for_each_mc_addr(ha, dev) 2522 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 2523 2524 sg_set_buf(&sg[1], mac_data, 2525 sizeof(mac_data->entries) + (mc_count * ETH_ALEN)); 2526 2527 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 2528 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg)) 2529 dev_warn(&dev->dev, "Failed to set MAC filter table.\n"); 2530 2531 kfree(buf); 2532 } 2533 2534 static int virtnet_vlan_rx_add_vid(struct net_device *dev, 2535 __be16 proto, u16 vid) 2536 { 2537 struct virtnet_info *vi = netdev_priv(dev); 2538 struct scatterlist sg; 2539 2540 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid); 2541 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid)); 2542 2543 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 2544 VIRTIO_NET_CTRL_VLAN_ADD, &sg)) 2545 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); 2546 return 0; 2547 } 2548 2549 static int virtnet_vlan_rx_kill_vid(struct net_device *dev, 2550 __be16 proto, u16 vid) 2551 { 2552 struct virtnet_info *vi = netdev_priv(dev); 2553 struct scatterlist sg; 2554 2555 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid); 2556 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid)); 2557 2558 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 2559 VIRTIO_NET_CTRL_VLAN_DEL, &sg)) 2560 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); 2561 return 0; 2562 } 2563 2564 static void virtnet_clean_affinity(struct virtnet_info *vi) 2565 { 2566 int i; 2567 2568 if (vi->affinity_hint_set) { 2569 for (i = 0; i < vi->max_queue_pairs; i++) { 2570 virtqueue_set_affinity(vi->rq[i].vq, NULL); 2571 virtqueue_set_affinity(vi->sq[i].vq, NULL); 2572 } 2573 2574 vi->affinity_hint_set = false; 2575 } 2576 } 2577 2578 static void virtnet_set_affinity(struct virtnet_info *vi) 2579 { 2580 cpumask_var_t mask; 2581 int stragglers; 2582 int group_size; 2583 int i, j, cpu; 2584 int num_cpu; 2585 int stride; 2586 2587 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) { 2588 virtnet_clean_affinity(vi); 2589 return; 2590 } 2591 2592 num_cpu = num_online_cpus(); 2593 stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1); 2594 stragglers = num_cpu >= vi->curr_queue_pairs ? 2595 num_cpu % vi->curr_queue_pairs : 2596 0; 2597 cpu = cpumask_first(cpu_online_mask); 2598 2599 for (i = 0; i < vi->curr_queue_pairs; i++) { 2600 group_size = stride + (i < stragglers ? 1 : 0); 2601 2602 for (j = 0; j < group_size; j++) { 2603 cpumask_set_cpu(cpu, mask); 2604 cpu = cpumask_next_wrap(cpu, cpu_online_mask, 2605 nr_cpu_ids, false); 2606 } 2607 virtqueue_set_affinity(vi->rq[i].vq, mask); 2608 virtqueue_set_affinity(vi->sq[i].vq, mask); 2609 __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS); 2610 cpumask_clear(mask); 2611 } 2612 2613 vi->affinity_hint_set = true; 2614 free_cpumask_var(mask); 2615 } 2616 2617 static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node) 2618 { 2619 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 2620 node); 2621 virtnet_set_affinity(vi); 2622 return 0; 2623 } 2624 2625 static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node) 2626 { 2627 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 2628 node_dead); 2629 virtnet_set_affinity(vi); 2630 return 0; 2631 } 2632 2633 static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node) 2634 { 2635 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 2636 node); 2637 2638 virtnet_clean_affinity(vi); 2639 return 0; 2640 } 2641 2642 static enum cpuhp_state virtionet_online; 2643 2644 static int virtnet_cpu_notif_add(struct virtnet_info *vi) 2645 { 2646 int ret; 2647 2648 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node); 2649 if (ret) 2650 return ret; 2651 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD, 2652 &vi->node_dead); 2653 if (!ret) 2654 return ret; 2655 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 2656 return ret; 2657 } 2658 2659 static void virtnet_cpu_notif_remove(struct virtnet_info *vi) 2660 { 2661 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 2662 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD, 2663 &vi->node_dead); 2664 } 2665 2666 static void virtnet_get_ringparam(struct net_device *dev, 2667 struct ethtool_ringparam *ring, 2668 struct kernel_ethtool_ringparam *kernel_ring, 2669 struct netlink_ext_ack *extack) 2670 { 2671 struct virtnet_info *vi = netdev_priv(dev); 2672 2673 ring->rx_max_pending = vi->rq[0].vq->num_max; 2674 ring->tx_max_pending = vi->sq[0].vq->num_max; 2675 ring->rx_pending = virtqueue_get_vring_size(vi->rq[0].vq); 2676 ring->tx_pending = virtqueue_get_vring_size(vi->sq[0].vq); 2677 } 2678 2679 static int virtnet_set_ringparam(struct net_device *dev, 2680 struct ethtool_ringparam *ring, 2681 struct kernel_ethtool_ringparam *kernel_ring, 2682 struct netlink_ext_ack *extack) 2683 { 2684 struct virtnet_info *vi = netdev_priv(dev); 2685 u32 rx_pending, tx_pending; 2686 struct receive_queue *rq; 2687 struct send_queue *sq; 2688 int i, err; 2689 2690 if (ring->rx_mini_pending || ring->rx_jumbo_pending) 2691 return -EINVAL; 2692 2693 rx_pending = virtqueue_get_vring_size(vi->rq[0].vq); 2694 tx_pending = virtqueue_get_vring_size(vi->sq[0].vq); 2695 2696 if (ring->rx_pending == rx_pending && 2697 ring->tx_pending == tx_pending) 2698 return 0; 2699 2700 if (ring->rx_pending > vi->rq[0].vq->num_max) 2701 return -EINVAL; 2702 2703 if (ring->tx_pending > vi->sq[0].vq->num_max) 2704 return -EINVAL; 2705 2706 for (i = 0; i < vi->max_queue_pairs; i++) { 2707 rq = vi->rq + i; 2708 sq = vi->sq + i; 2709 2710 if (ring->tx_pending != tx_pending) { 2711 err = virtnet_tx_resize(vi, sq, ring->tx_pending); 2712 if (err) 2713 return err; 2714 } 2715 2716 if (ring->rx_pending != rx_pending) { 2717 err = virtnet_rx_resize(vi, rq, ring->rx_pending); 2718 if (err) 2719 return err; 2720 } 2721 } 2722 2723 return 0; 2724 } 2725 2726 static bool virtnet_commit_rss_command(struct virtnet_info *vi) 2727 { 2728 struct net_device *dev = vi->dev; 2729 struct scatterlist sgs[4]; 2730 unsigned int sg_buf_size; 2731 2732 /* prepare sgs */ 2733 sg_init_table(sgs, 4); 2734 2735 sg_buf_size = offsetof(struct virtio_net_ctrl_rss, indirection_table); 2736 sg_set_buf(&sgs[0], &vi->ctrl->rss, sg_buf_size); 2737 2738 sg_buf_size = sizeof(uint16_t) * (vi->ctrl->rss.indirection_table_mask + 1); 2739 sg_set_buf(&sgs[1], vi->ctrl->rss.indirection_table, sg_buf_size); 2740 2741 sg_buf_size = offsetof(struct virtio_net_ctrl_rss, key) 2742 - offsetof(struct virtio_net_ctrl_rss, max_tx_vq); 2743 sg_set_buf(&sgs[2], &vi->ctrl->rss.max_tx_vq, sg_buf_size); 2744 2745 sg_buf_size = vi->rss_key_size; 2746 sg_set_buf(&sgs[3], vi->ctrl->rss.key, sg_buf_size); 2747 2748 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, 2749 vi->has_rss ? VIRTIO_NET_CTRL_MQ_RSS_CONFIG 2750 : VIRTIO_NET_CTRL_MQ_HASH_CONFIG, sgs)) { 2751 dev_warn(&dev->dev, "VIRTIONET issue with committing RSS sgs\n"); 2752 return false; 2753 } 2754 return true; 2755 } 2756 2757 static void virtnet_init_default_rss(struct virtnet_info *vi) 2758 { 2759 u32 indir_val = 0; 2760 int i = 0; 2761 2762 vi->ctrl->rss.hash_types = vi->rss_hash_types_supported; 2763 vi->rss_hash_types_saved = vi->rss_hash_types_supported; 2764 vi->ctrl->rss.indirection_table_mask = vi->rss_indir_table_size 2765 ? vi->rss_indir_table_size - 1 : 0; 2766 vi->ctrl->rss.unclassified_queue = 0; 2767 2768 for (; i < vi->rss_indir_table_size; ++i) { 2769 indir_val = ethtool_rxfh_indir_default(i, vi->curr_queue_pairs); 2770 vi->ctrl->rss.indirection_table[i] = indir_val; 2771 } 2772 2773 vi->ctrl->rss.max_tx_vq = vi->curr_queue_pairs; 2774 vi->ctrl->rss.hash_key_length = vi->rss_key_size; 2775 2776 netdev_rss_key_fill(vi->ctrl->rss.key, vi->rss_key_size); 2777 } 2778 2779 static void virtnet_get_hashflow(const struct virtnet_info *vi, struct ethtool_rxnfc *info) 2780 { 2781 info->data = 0; 2782 switch (info->flow_type) { 2783 case TCP_V4_FLOW: 2784 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) { 2785 info->data = RXH_IP_SRC | RXH_IP_DST | 2786 RXH_L4_B_0_1 | RXH_L4_B_2_3; 2787 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) { 2788 info->data = RXH_IP_SRC | RXH_IP_DST; 2789 } 2790 break; 2791 case TCP_V6_FLOW: 2792 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) { 2793 info->data = RXH_IP_SRC | RXH_IP_DST | 2794 RXH_L4_B_0_1 | RXH_L4_B_2_3; 2795 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) { 2796 info->data = RXH_IP_SRC | RXH_IP_DST; 2797 } 2798 break; 2799 case UDP_V4_FLOW: 2800 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) { 2801 info->data = RXH_IP_SRC | RXH_IP_DST | 2802 RXH_L4_B_0_1 | RXH_L4_B_2_3; 2803 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) { 2804 info->data = RXH_IP_SRC | RXH_IP_DST; 2805 } 2806 break; 2807 case UDP_V6_FLOW: 2808 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) { 2809 info->data = RXH_IP_SRC | RXH_IP_DST | 2810 RXH_L4_B_0_1 | RXH_L4_B_2_3; 2811 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) { 2812 info->data = RXH_IP_SRC | RXH_IP_DST; 2813 } 2814 break; 2815 case IPV4_FLOW: 2816 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) 2817 info->data = RXH_IP_SRC | RXH_IP_DST; 2818 2819 break; 2820 case IPV6_FLOW: 2821 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) 2822 info->data = RXH_IP_SRC | RXH_IP_DST; 2823 2824 break; 2825 default: 2826 info->data = 0; 2827 break; 2828 } 2829 } 2830 2831 static bool virtnet_set_hashflow(struct virtnet_info *vi, struct ethtool_rxnfc *info) 2832 { 2833 u32 new_hashtypes = vi->rss_hash_types_saved; 2834 bool is_disable = info->data & RXH_DISCARD; 2835 bool is_l4 = info->data == (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3); 2836 2837 /* supports only 'sd', 'sdfn' and 'r' */ 2838 if (!((info->data == (RXH_IP_SRC | RXH_IP_DST)) | is_l4 | is_disable)) 2839 return false; 2840 2841 switch (info->flow_type) { 2842 case TCP_V4_FLOW: 2843 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_TCPv4); 2844 if (!is_disable) 2845 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4 2846 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv4 : 0); 2847 break; 2848 case UDP_V4_FLOW: 2849 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_UDPv4); 2850 if (!is_disable) 2851 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4 2852 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv4 : 0); 2853 break; 2854 case IPV4_FLOW: 2855 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv4; 2856 if (!is_disable) 2857 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv4; 2858 break; 2859 case TCP_V6_FLOW: 2860 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_TCPv6); 2861 if (!is_disable) 2862 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6 2863 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv6 : 0); 2864 break; 2865 case UDP_V6_FLOW: 2866 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_UDPv6); 2867 if (!is_disable) 2868 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6 2869 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv6 : 0); 2870 break; 2871 case IPV6_FLOW: 2872 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv6; 2873 if (!is_disable) 2874 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv6; 2875 break; 2876 default: 2877 /* unsupported flow */ 2878 return false; 2879 } 2880 2881 /* if unsupported hashtype was set */ 2882 if (new_hashtypes != (new_hashtypes & vi->rss_hash_types_supported)) 2883 return false; 2884 2885 if (new_hashtypes != vi->rss_hash_types_saved) { 2886 vi->rss_hash_types_saved = new_hashtypes; 2887 vi->ctrl->rss.hash_types = vi->rss_hash_types_saved; 2888 if (vi->dev->features & NETIF_F_RXHASH) 2889 return virtnet_commit_rss_command(vi); 2890 } 2891 2892 return true; 2893 } 2894 2895 static void virtnet_get_drvinfo(struct net_device *dev, 2896 struct ethtool_drvinfo *info) 2897 { 2898 struct virtnet_info *vi = netdev_priv(dev); 2899 struct virtio_device *vdev = vi->vdev; 2900 2901 strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); 2902 strscpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version)); 2903 strscpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info)); 2904 2905 } 2906 2907 /* TODO: Eliminate OOO packets during switching */ 2908 static int virtnet_set_channels(struct net_device *dev, 2909 struct ethtool_channels *channels) 2910 { 2911 struct virtnet_info *vi = netdev_priv(dev); 2912 u16 queue_pairs = channels->combined_count; 2913 int err; 2914 2915 /* We don't support separate rx/tx channels. 2916 * We don't allow setting 'other' channels. 2917 */ 2918 if (channels->rx_count || channels->tx_count || channels->other_count) 2919 return -EINVAL; 2920 2921 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0) 2922 return -EINVAL; 2923 2924 /* For now we don't support modifying channels while XDP is loaded 2925 * also when XDP is loaded all RX queues have XDP programs so we only 2926 * need to check a single RX queue. 2927 */ 2928 if (vi->rq[0].xdp_prog) 2929 return -EINVAL; 2930 2931 cpus_read_lock(); 2932 err = _virtnet_set_queues(vi, queue_pairs); 2933 if (err) { 2934 cpus_read_unlock(); 2935 goto err; 2936 } 2937 virtnet_set_affinity(vi); 2938 cpus_read_unlock(); 2939 2940 netif_set_real_num_tx_queues(dev, queue_pairs); 2941 netif_set_real_num_rx_queues(dev, queue_pairs); 2942 err: 2943 return err; 2944 } 2945 2946 static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data) 2947 { 2948 struct virtnet_info *vi = netdev_priv(dev); 2949 unsigned int i, j; 2950 u8 *p = data; 2951 2952 switch (stringset) { 2953 case ETH_SS_STATS: 2954 for (i = 0; i < vi->curr_queue_pairs; i++) { 2955 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) 2956 ethtool_sprintf(&p, "rx_queue_%u_%s", i, 2957 virtnet_rq_stats_desc[j].desc); 2958 } 2959 2960 for (i = 0; i < vi->curr_queue_pairs; i++) { 2961 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) 2962 ethtool_sprintf(&p, "tx_queue_%u_%s", i, 2963 virtnet_sq_stats_desc[j].desc); 2964 } 2965 break; 2966 } 2967 } 2968 2969 static int virtnet_get_sset_count(struct net_device *dev, int sset) 2970 { 2971 struct virtnet_info *vi = netdev_priv(dev); 2972 2973 switch (sset) { 2974 case ETH_SS_STATS: 2975 return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN + 2976 VIRTNET_SQ_STATS_LEN); 2977 default: 2978 return -EOPNOTSUPP; 2979 } 2980 } 2981 2982 static void virtnet_get_ethtool_stats(struct net_device *dev, 2983 struct ethtool_stats *stats, u64 *data) 2984 { 2985 struct virtnet_info *vi = netdev_priv(dev); 2986 unsigned int idx = 0, start, i, j; 2987 const u8 *stats_base; 2988 size_t offset; 2989 2990 for (i = 0; i < vi->curr_queue_pairs; i++) { 2991 struct receive_queue *rq = &vi->rq[i]; 2992 2993 stats_base = (u8 *)&rq->stats; 2994 do { 2995 start = u64_stats_fetch_begin(&rq->stats.syncp); 2996 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) { 2997 offset = virtnet_rq_stats_desc[j].offset; 2998 data[idx + j] = *(u64 *)(stats_base + offset); 2999 } 3000 } while (u64_stats_fetch_retry(&rq->stats.syncp, start)); 3001 idx += VIRTNET_RQ_STATS_LEN; 3002 } 3003 3004 for (i = 0; i < vi->curr_queue_pairs; i++) { 3005 struct send_queue *sq = &vi->sq[i]; 3006 3007 stats_base = (u8 *)&sq->stats; 3008 do { 3009 start = u64_stats_fetch_begin(&sq->stats.syncp); 3010 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) { 3011 offset = virtnet_sq_stats_desc[j].offset; 3012 data[idx + j] = *(u64 *)(stats_base + offset); 3013 } 3014 } while (u64_stats_fetch_retry(&sq->stats.syncp, start)); 3015 idx += VIRTNET_SQ_STATS_LEN; 3016 } 3017 } 3018 3019 static void virtnet_get_channels(struct net_device *dev, 3020 struct ethtool_channels *channels) 3021 { 3022 struct virtnet_info *vi = netdev_priv(dev); 3023 3024 channels->combined_count = vi->curr_queue_pairs; 3025 channels->max_combined = vi->max_queue_pairs; 3026 channels->max_other = 0; 3027 channels->rx_count = 0; 3028 channels->tx_count = 0; 3029 channels->other_count = 0; 3030 } 3031 3032 static int virtnet_set_link_ksettings(struct net_device *dev, 3033 const struct ethtool_link_ksettings *cmd) 3034 { 3035 struct virtnet_info *vi = netdev_priv(dev); 3036 3037 return ethtool_virtdev_set_link_ksettings(dev, cmd, 3038 &vi->speed, &vi->duplex); 3039 } 3040 3041 static int virtnet_get_link_ksettings(struct net_device *dev, 3042 struct ethtool_link_ksettings *cmd) 3043 { 3044 struct virtnet_info *vi = netdev_priv(dev); 3045 3046 cmd->base.speed = vi->speed; 3047 cmd->base.duplex = vi->duplex; 3048 cmd->base.port = PORT_OTHER; 3049 3050 return 0; 3051 } 3052 3053 static int virtnet_send_notf_coal_cmds(struct virtnet_info *vi, 3054 struct ethtool_coalesce *ec) 3055 { 3056 struct scatterlist sgs_tx, sgs_rx; 3057 3058 vi->ctrl->coal_tx.tx_usecs = cpu_to_le32(ec->tx_coalesce_usecs); 3059 vi->ctrl->coal_tx.tx_max_packets = cpu_to_le32(ec->tx_max_coalesced_frames); 3060 sg_init_one(&sgs_tx, &vi->ctrl->coal_tx, sizeof(vi->ctrl->coal_tx)); 3061 3062 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL, 3063 VIRTIO_NET_CTRL_NOTF_COAL_TX_SET, 3064 &sgs_tx)) 3065 return -EINVAL; 3066 3067 /* Save parameters */ 3068 vi->intr_coal_tx.max_usecs = ec->tx_coalesce_usecs; 3069 vi->intr_coal_tx.max_packets = ec->tx_max_coalesced_frames; 3070 3071 vi->ctrl->coal_rx.rx_usecs = cpu_to_le32(ec->rx_coalesce_usecs); 3072 vi->ctrl->coal_rx.rx_max_packets = cpu_to_le32(ec->rx_max_coalesced_frames); 3073 sg_init_one(&sgs_rx, &vi->ctrl->coal_rx, sizeof(vi->ctrl->coal_rx)); 3074 3075 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL, 3076 VIRTIO_NET_CTRL_NOTF_COAL_RX_SET, 3077 &sgs_rx)) 3078 return -EINVAL; 3079 3080 /* Save parameters */ 3081 vi->intr_coal_rx.max_usecs = ec->rx_coalesce_usecs; 3082 vi->intr_coal_rx.max_packets = ec->rx_max_coalesced_frames; 3083 3084 return 0; 3085 } 3086 3087 static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi, 3088 u16 vqn, u32 max_usecs, u32 max_packets) 3089 { 3090 struct scatterlist sgs; 3091 3092 vi->ctrl->coal_vq.vqn = cpu_to_le16(vqn); 3093 vi->ctrl->coal_vq.coal.max_usecs = cpu_to_le32(max_usecs); 3094 vi->ctrl->coal_vq.coal.max_packets = cpu_to_le32(max_packets); 3095 sg_init_one(&sgs, &vi->ctrl->coal_vq, sizeof(vi->ctrl->coal_vq)); 3096 3097 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL, 3098 VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET, 3099 &sgs)) 3100 return -EINVAL; 3101 3102 return 0; 3103 } 3104 3105 static int virtnet_send_notf_coal_vq_cmds(struct virtnet_info *vi, 3106 struct ethtool_coalesce *ec, 3107 u16 queue) 3108 { 3109 int err; 3110 3111 if (ec->rx_coalesce_usecs || ec->rx_max_coalesced_frames) { 3112 err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(queue), 3113 ec->rx_coalesce_usecs, 3114 ec->rx_max_coalesced_frames); 3115 if (err) 3116 return err; 3117 /* Save parameters */ 3118 vi->rq[queue].intr_coal.max_usecs = ec->rx_coalesce_usecs; 3119 vi->rq[queue].intr_coal.max_packets = ec->rx_max_coalesced_frames; 3120 } 3121 3122 if (ec->tx_coalesce_usecs || ec->tx_max_coalesced_frames) { 3123 err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(queue), 3124 ec->tx_coalesce_usecs, 3125 ec->tx_max_coalesced_frames); 3126 if (err) 3127 return err; 3128 /* Save parameters */ 3129 vi->sq[queue].intr_coal.max_usecs = ec->tx_coalesce_usecs; 3130 vi->sq[queue].intr_coal.max_packets = ec->tx_max_coalesced_frames; 3131 } 3132 3133 return 0; 3134 } 3135 3136 static int virtnet_coal_params_supported(struct ethtool_coalesce *ec) 3137 { 3138 /* usecs coalescing is supported only if VIRTIO_NET_F_NOTF_COAL 3139 * feature is negotiated. 3140 */ 3141 if (ec->rx_coalesce_usecs || ec->tx_coalesce_usecs) 3142 return -EOPNOTSUPP; 3143 3144 if (ec->tx_max_coalesced_frames > 1 || 3145 ec->rx_max_coalesced_frames != 1) 3146 return -EINVAL; 3147 3148 return 0; 3149 } 3150 3151 static int virtnet_should_update_vq_weight(int dev_flags, int weight, 3152 int vq_weight, bool *should_update) 3153 { 3154 if (weight ^ vq_weight) { 3155 if (dev_flags & IFF_UP) 3156 return -EBUSY; 3157 *should_update = true; 3158 } 3159 3160 return 0; 3161 } 3162 3163 static int virtnet_set_coalesce(struct net_device *dev, 3164 struct ethtool_coalesce *ec, 3165 struct kernel_ethtool_coalesce *kernel_coal, 3166 struct netlink_ext_ack *extack) 3167 { 3168 struct virtnet_info *vi = netdev_priv(dev); 3169 int ret, queue_number, napi_weight; 3170 bool update_napi = false; 3171 3172 /* Can't change NAPI weight if the link is up */ 3173 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0; 3174 for (queue_number = 0; queue_number < vi->max_queue_pairs; queue_number++) { 3175 ret = virtnet_should_update_vq_weight(dev->flags, napi_weight, 3176 vi->sq[queue_number].napi.weight, 3177 &update_napi); 3178 if (ret) 3179 return ret; 3180 3181 if (update_napi) { 3182 /* All queues that belong to [queue_number, vi->max_queue_pairs] will be 3183 * updated for the sake of simplicity, which might not be necessary 3184 */ 3185 break; 3186 } 3187 } 3188 3189 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) 3190 ret = virtnet_send_notf_coal_cmds(vi, ec); 3191 else 3192 ret = virtnet_coal_params_supported(ec); 3193 3194 if (ret) 3195 return ret; 3196 3197 if (update_napi) { 3198 for (; queue_number < vi->max_queue_pairs; queue_number++) 3199 vi->sq[queue_number].napi.weight = napi_weight; 3200 } 3201 3202 return ret; 3203 } 3204 3205 static int virtnet_get_coalesce(struct net_device *dev, 3206 struct ethtool_coalesce *ec, 3207 struct kernel_ethtool_coalesce *kernel_coal, 3208 struct netlink_ext_ack *extack) 3209 { 3210 struct virtnet_info *vi = netdev_priv(dev); 3211 3212 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) { 3213 ec->rx_coalesce_usecs = vi->intr_coal_rx.max_usecs; 3214 ec->tx_coalesce_usecs = vi->intr_coal_tx.max_usecs; 3215 ec->tx_max_coalesced_frames = vi->intr_coal_tx.max_packets; 3216 ec->rx_max_coalesced_frames = vi->intr_coal_rx.max_packets; 3217 } else { 3218 ec->rx_max_coalesced_frames = 1; 3219 3220 if (vi->sq[0].napi.weight) 3221 ec->tx_max_coalesced_frames = 1; 3222 } 3223 3224 return 0; 3225 } 3226 3227 static int virtnet_set_per_queue_coalesce(struct net_device *dev, 3228 u32 queue, 3229 struct ethtool_coalesce *ec) 3230 { 3231 struct virtnet_info *vi = netdev_priv(dev); 3232 int ret, napi_weight; 3233 bool update_napi = false; 3234 3235 if (queue >= vi->max_queue_pairs) 3236 return -EINVAL; 3237 3238 /* Can't change NAPI weight if the link is up */ 3239 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0; 3240 ret = virtnet_should_update_vq_weight(dev->flags, napi_weight, 3241 vi->sq[queue].napi.weight, 3242 &update_napi); 3243 if (ret) 3244 return ret; 3245 3246 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) 3247 ret = virtnet_send_notf_coal_vq_cmds(vi, ec, queue); 3248 else 3249 ret = virtnet_coal_params_supported(ec); 3250 3251 if (ret) 3252 return ret; 3253 3254 if (update_napi) 3255 vi->sq[queue].napi.weight = napi_weight; 3256 3257 return 0; 3258 } 3259 3260 static int virtnet_get_per_queue_coalesce(struct net_device *dev, 3261 u32 queue, 3262 struct ethtool_coalesce *ec) 3263 { 3264 struct virtnet_info *vi = netdev_priv(dev); 3265 3266 if (queue >= vi->max_queue_pairs) 3267 return -EINVAL; 3268 3269 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) { 3270 ec->rx_coalesce_usecs = vi->rq[queue].intr_coal.max_usecs; 3271 ec->tx_coalesce_usecs = vi->sq[queue].intr_coal.max_usecs; 3272 ec->tx_max_coalesced_frames = vi->sq[queue].intr_coal.max_packets; 3273 ec->rx_max_coalesced_frames = vi->rq[queue].intr_coal.max_packets; 3274 } else { 3275 ec->rx_max_coalesced_frames = 1; 3276 3277 if (vi->sq[0].napi.weight) 3278 ec->tx_max_coalesced_frames = 1; 3279 } 3280 3281 return 0; 3282 } 3283 3284 static void virtnet_init_settings(struct net_device *dev) 3285 { 3286 struct virtnet_info *vi = netdev_priv(dev); 3287 3288 vi->speed = SPEED_UNKNOWN; 3289 vi->duplex = DUPLEX_UNKNOWN; 3290 } 3291 3292 static void virtnet_update_settings(struct virtnet_info *vi) 3293 { 3294 u32 speed; 3295 u8 duplex; 3296 3297 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX)) 3298 return; 3299 3300 virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed); 3301 3302 if (ethtool_validate_speed(speed)) 3303 vi->speed = speed; 3304 3305 virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex); 3306 3307 if (ethtool_validate_duplex(duplex)) 3308 vi->duplex = duplex; 3309 } 3310 3311 static u32 virtnet_get_rxfh_key_size(struct net_device *dev) 3312 { 3313 return ((struct virtnet_info *)netdev_priv(dev))->rss_key_size; 3314 } 3315 3316 static u32 virtnet_get_rxfh_indir_size(struct net_device *dev) 3317 { 3318 return ((struct virtnet_info *)netdev_priv(dev))->rss_indir_table_size; 3319 } 3320 3321 static int virtnet_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, u8 *hfunc) 3322 { 3323 struct virtnet_info *vi = netdev_priv(dev); 3324 int i; 3325 3326 if (indir) { 3327 for (i = 0; i < vi->rss_indir_table_size; ++i) 3328 indir[i] = vi->ctrl->rss.indirection_table[i]; 3329 } 3330 3331 if (key) 3332 memcpy(key, vi->ctrl->rss.key, vi->rss_key_size); 3333 3334 if (hfunc) 3335 *hfunc = ETH_RSS_HASH_TOP; 3336 3337 return 0; 3338 } 3339 3340 static int virtnet_set_rxfh(struct net_device *dev, const u32 *indir, const u8 *key, const u8 hfunc) 3341 { 3342 struct virtnet_info *vi = netdev_priv(dev); 3343 int i; 3344 3345 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP) 3346 return -EOPNOTSUPP; 3347 3348 if (indir) { 3349 for (i = 0; i < vi->rss_indir_table_size; ++i) 3350 vi->ctrl->rss.indirection_table[i] = indir[i]; 3351 } 3352 if (key) 3353 memcpy(vi->ctrl->rss.key, key, vi->rss_key_size); 3354 3355 virtnet_commit_rss_command(vi); 3356 3357 return 0; 3358 } 3359 3360 static int virtnet_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info, u32 *rule_locs) 3361 { 3362 struct virtnet_info *vi = netdev_priv(dev); 3363 int rc = 0; 3364 3365 switch (info->cmd) { 3366 case ETHTOOL_GRXRINGS: 3367 info->data = vi->curr_queue_pairs; 3368 break; 3369 case ETHTOOL_GRXFH: 3370 virtnet_get_hashflow(vi, info); 3371 break; 3372 default: 3373 rc = -EOPNOTSUPP; 3374 } 3375 3376 return rc; 3377 } 3378 3379 static int virtnet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info) 3380 { 3381 struct virtnet_info *vi = netdev_priv(dev); 3382 int rc = 0; 3383 3384 switch (info->cmd) { 3385 case ETHTOOL_SRXFH: 3386 if (!virtnet_set_hashflow(vi, info)) 3387 rc = -EINVAL; 3388 3389 break; 3390 default: 3391 rc = -EOPNOTSUPP; 3392 } 3393 3394 return rc; 3395 } 3396 3397 static const struct ethtool_ops virtnet_ethtool_ops = { 3398 .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES | 3399 ETHTOOL_COALESCE_USECS, 3400 .get_drvinfo = virtnet_get_drvinfo, 3401 .get_link = ethtool_op_get_link, 3402 .get_ringparam = virtnet_get_ringparam, 3403 .set_ringparam = virtnet_set_ringparam, 3404 .get_strings = virtnet_get_strings, 3405 .get_sset_count = virtnet_get_sset_count, 3406 .get_ethtool_stats = virtnet_get_ethtool_stats, 3407 .set_channels = virtnet_set_channels, 3408 .get_channels = virtnet_get_channels, 3409 .get_ts_info = ethtool_op_get_ts_info, 3410 .get_link_ksettings = virtnet_get_link_ksettings, 3411 .set_link_ksettings = virtnet_set_link_ksettings, 3412 .set_coalesce = virtnet_set_coalesce, 3413 .get_coalesce = virtnet_get_coalesce, 3414 .set_per_queue_coalesce = virtnet_set_per_queue_coalesce, 3415 .get_per_queue_coalesce = virtnet_get_per_queue_coalesce, 3416 .get_rxfh_key_size = virtnet_get_rxfh_key_size, 3417 .get_rxfh_indir_size = virtnet_get_rxfh_indir_size, 3418 .get_rxfh = virtnet_get_rxfh, 3419 .set_rxfh = virtnet_set_rxfh, 3420 .get_rxnfc = virtnet_get_rxnfc, 3421 .set_rxnfc = virtnet_set_rxnfc, 3422 }; 3423 3424 static void virtnet_freeze_down(struct virtio_device *vdev) 3425 { 3426 struct virtnet_info *vi = vdev->priv; 3427 3428 /* Make sure no work handler is accessing the device */ 3429 flush_work(&vi->config_work); 3430 3431 netif_tx_lock_bh(vi->dev); 3432 netif_device_detach(vi->dev); 3433 netif_tx_unlock_bh(vi->dev); 3434 if (netif_running(vi->dev)) 3435 virtnet_close(vi->dev); 3436 } 3437 3438 static int init_vqs(struct virtnet_info *vi); 3439 3440 static int virtnet_restore_up(struct virtio_device *vdev) 3441 { 3442 struct virtnet_info *vi = vdev->priv; 3443 int err; 3444 3445 err = init_vqs(vi); 3446 if (err) 3447 return err; 3448 3449 virtio_device_ready(vdev); 3450 3451 enable_delayed_refill(vi); 3452 3453 if (netif_running(vi->dev)) { 3454 err = virtnet_open(vi->dev); 3455 if (err) 3456 return err; 3457 } 3458 3459 netif_tx_lock_bh(vi->dev); 3460 netif_device_attach(vi->dev); 3461 netif_tx_unlock_bh(vi->dev); 3462 return err; 3463 } 3464 3465 static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads) 3466 { 3467 struct scatterlist sg; 3468 vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads); 3469 3470 sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads)); 3471 3472 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS, 3473 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) { 3474 dev_warn(&vi->dev->dev, "Fail to set guest offload.\n"); 3475 return -EINVAL; 3476 } 3477 3478 return 0; 3479 } 3480 3481 static int virtnet_clear_guest_offloads(struct virtnet_info *vi) 3482 { 3483 u64 offloads = 0; 3484 3485 if (!vi->guest_offloads) 3486 return 0; 3487 3488 return virtnet_set_guest_offloads(vi, offloads); 3489 } 3490 3491 static int virtnet_restore_guest_offloads(struct virtnet_info *vi) 3492 { 3493 u64 offloads = vi->guest_offloads; 3494 3495 if (!vi->guest_offloads) 3496 return 0; 3497 3498 return virtnet_set_guest_offloads(vi, offloads); 3499 } 3500 3501 static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog, 3502 struct netlink_ext_ack *extack) 3503 { 3504 unsigned int room = SKB_DATA_ALIGN(VIRTIO_XDP_HEADROOM + 3505 sizeof(struct skb_shared_info)); 3506 unsigned int max_sz = PAGE_SIZE - room - ETH_HLEN; 3507 struct virtnet_info *vi = netdev_priv(dev); 3508 struct bpf_prog *old_prog; 3509 u16 xdp_qp = 0, curr_qp; 3510 int i, err; 3511 3512 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) 3513 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) || 3514 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) || 3515 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) || 3516 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) || 3517 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM) || 3518 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) || 3519 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6))) { 3520 NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing GRO_HW/CSUM, disable GRO_HW/CSUM first"); 3521 return -EOPNOTSUPP; 3522 } 3523 3524 if (vi->mergeable_rx_bufs && !vi->any_header_sg) { 3525 NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required"); 3526 return -EINVAL; 3527 } 3528 3529 if (prog && !prog->aux->xdp_has_frags && dev->mtu > max_sz) { 3530 NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP without frags"); 3531 netdev_warn(dev, "single-buffer XDP requires MTU less than %u\n", max_sz); 3532 return -EINVAL; 3533 } 3534 3535 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs; 3536 if (prog) 3537 xdp_qp = nr_cpu_ids; 3538 3539 /* XDP requires extra queues for XDP_TX */ 3540 if (curr_qp + xdp_qp > vi->max_queue_pairs) { 3541 netdev_warn_once(dev, "XDP request %i queues but max is %i. XDP_TX and XDP_REDIRECT will operate in a slower locked tx mode.\n", 3542 curr_qp + xdp_qp, vi->max_queue_pairs); 3543 xdp_qp = 0; 3544 } 3545 3546 old_prog = rtnl_dereference(vi->rq[0].xdp_prog); 3547 if (!prog && !old_prog) 3548 return 0; 3549 3550 if (prog) 3551 bpf_prog_add(prog, vi->max_queue_pairs - 1); 3552 3553 /* Make sure NAPI is not using any XDP TX queues for RX. */ 3554 if (netif_running(dev)) { 3555 for (i = 0; i < vi->max_queue_pairs; i++) { 3556 napi_disable(&vi->rq[i].napi); 3557 virtnet_napi_tx_disable(&vi->sq[i].napi); 3558 } 3559 } 3560 3561 if (!prog) { 3562 for (i = 0; i < vi->max_queue_pairs; i++) { 3563 rcu_assign_pointer(vi->rq[i].xdp_prog, prog); 3564 if (i == 0) 3565 virtnet_restore_guest_offloads(vi); 3566 } 3567 synchronize_net(); 3568 } 3569 3570 err = _virtnet_set_queues(vi, curr_qp + xdp_qp); 3571 if (err) 3572 goto err; 3573 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp); 3574 vi->xdp_queue_pairs = xdp_qp; 3575 3576 if (prog) { 3577 vi->xdp_enabled = true; 3578 for (i = 0; i < vi->max_queue_pairs; i++) { 3579 rcu_assign_pointer(vi->rq[i].xdp_prog, prog); 3580 if (i == 0 && !old_prog) 3581 virtnet_clear_guest_offloads(vi); 3582 } 3583 if (!old_prog) 3584 xdp_features_set_redirect_target(dev, true); 3585 } else { 3586 xdp_features_clear_redirect_target(dev); 3587 vi->xdp_enabled = false; 3588 } 3589 3590 for (i = 0; i < vi->max_queue_pairs; i++) { 3591 if (old_prog) 3592 bpf_prog_put(old_prog); 3593 if (netif_running(dev)) { 3594 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 3595 virtnet_napi_tx_enable(vi, vi->sq[i].vq, 3596 &vi->sq[i].napi); 3597 } 3598 } 3599 3600 return 0; 3601 3602 err: 3603 if (!prog) { 3604 virtnet_clear_guest_offloads(vi); 3605 for (i = 0; i < vi->max_queue_pairs; i++) 3606 rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog); 3607 } 3608 3609 if (netif_running(dev)) { 3610 for (i = 0; i < vi->max_queue_pairs; i++) { 3611 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 3612 virtnet_napi_tx_enable(vi, vi->sq[i].vq, 3613 &vi->sq[i].napi); 3614 } 3615 } 3616 if (prog) 3617 bpf_prog_sub(prog, vi->max_queue_pairs - 1); 3618 return err; 3619 } 3620 3621 static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp) 3622 { 3623 switch (xdp->command) { 3624 case XDP_SETUP_PROG: 3625 return virtnet_xdp_set(dev, xdp->prog, xdp->extack); 3626 default: 3627 return -EINVAL; 3628 } 3629 } 3630 3631 static int virtnet_get_phys_port_name(struct net_device *dev, char *buf, 3632 size_t len) 3633 { 3634 struct virtnet_info *vi = netdev_priv(dev); 3635 int ret; 3636 3637 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY)) 3638 return -EOPNOTSUPP; 3639 3640 ret = snprintf(buf, len, "sby"); 3641 if (ret >= len) 3642 return -EOPNOTSUPP; 3643 3644 return 0; 3645 } 3646 3647 static int virtnet_set_features(struct net_device *dev, 3648 netdev_features_t features) 3649 { 3650 struct virtnet_info *vi = netdev_priv(dev); 3651 u64 offloads; 3652 int err; 3653 3654 if ((dev->features ^ features) & NETIF_F_GRO_HW) { 3655 if (vi->xdp_enabled) 3656 return -EBUSY; 3657 3658 if (features & NETIF_F_GRO_HW) 3659 offloads = vi->guest_offloads_capable; 3660 else 3661 offloads = vi->guest_offloads_capable & 3662 ~GUEST_OFFLOAD_GRO_HW_MASK; 3663 3664 err = virtnet_set_guest_offloads(vi, offloads); 3665 if (err) 3666 return err; 3667 vi->guest_offloads = offloads; 3668 } 3669 3670 if ((dev->features ^ features) & NETIF_F_RXHASH) { 3671 if (features & NETIF_F_RXHASH) 3672 vi->ctrl->rss.hash_types = vi->rss_hash_types_saved; 3673 else 3674 vi->ctrl->rss.hash_types = VIRTIO_NET_HASH_REPORT_NONE; 3675 3676 if (!virtnet_commit_rss_command(vi)) 3677 return -EINVAL; 3678 } 3679 3680 return 0; 3681 } 3682 3683 static void virtnet_tx_timeout(struct net_device *dev, unsigned int txqueue) 3684 { 3685 struct virtnet_info *priv = netdev_priv(dev); 3686 struct send_queue *sq = &priv->sq[txqueue]; 3687 struct netdev_queue *txq = netdev_get_tx_queue(dev, txqueue); 3688 3689 u64_stats_update_begin(&sq->stats.syncp); 3690 sq->stats.tx_timeouts++; 3691 u64_stats_update_end(&sq->stats.syncp); 3692 3693 netdev_err(dev, "TX timeout on queue: %u, sq: %s, vq: 0x%x, name: %s, %u usecs ago\n", 3694 txqueue, sq->name, sq->vq->index, sq->vq->name, 3695 jiffies_to_usecs(jiffies - READ_ONCE(txq->trans_start))); 3696 } 3697 3698 static const struct net_device_ops virtnet_netdev = { 3699 .ndo_open = virtnet_open, 3700 .ndo_stop = virtnet_close, 3701 .ndo_start_xmit = start_xmit, 3702 .ndo_validate_addr = eth_validate_addr, 3703 .ndo_set_mac_address = virtnet_set_mac_address, 3704 .ndo_set_rx_mode = virtnet_set_rx_mode, 3705 .ndo_get_stats64 = virtnet_stats, 3706 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, 3707 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, 3708 .ndo_bpf = virtnet_xdp, 3709 .ndo_xdp_xmit = virtnet_xdp_xmit, 3710 .ndo_features_check = passthru_features_check, 3711 .ndo_get_phys_port_name = virtnet_get_phys_port_name, 3712 .ndo_set_features = virtnet_set_features, 3713 .ndo_tx_timeout = virtnet_tx_timeout, 3714 }; 3715 3716 static void virtnet_config_changed_work(struct work_struct *work) 3717 { 3718 struct virtnet_info *vi = 3719 container_of(work, struct virtnet_info, config_work); 3720 u16 v; 3721 3722 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS, 3723 struct virtio_net_config, status, &v) < 0) 3724 return; 3725 3726 if (v & VIRTIO_NET_S_ANNOUNCE) { 3727 netdev_notify_peers(vi->dev); 3728 virtnet_ack_link_announce(vi); 3729 } 3730 3731 /* Ignore unknown (future) status bits */ 3732 v &= VIRTIO_NET_S_LINK_UP; 3733 3734 if (vi->status == v) 3735 return; 3736 3737 vi->status = v; 3738 3739 if (vi->status & VIRTIO_NET_S_LINK_UP) { 3740 virtnet_update_settings(vi); 3741 netif_carrier_on(vi->dev); 3742 netif_tx_wake_all_queues(vi->dev); 3743 } else { 3744 netif_carrier_off(vi->dev); 3745 netif_tx_stop_all_queues(vi->dev); 3746 } 3747 } 3748 3749 static void virtnet_config_changed(struct virtio_device *vdev) 3750 { 3751 struct virtnet_info *vi = vdev->priv; 3752 3753 schedule_work(&vi->config_work); 3754 } 3755 3756 static void virtnet_free_queues(struct virtnet_info *vi) 3757 { 3758 int i; 3759 3760 for (i = 0; i < vi->max_queue_pairs; i++) { 3761 __netif_napi_del(&vi->rq[i].napi); 3762 __netif_napi_del(&vi->sq[i].napi); 3763 } 3764 3765 /* We called __netif_napi_del(), 3766 * we need to respect an RCU grace period before freeing vi->rq 3767 */ 3768 synchronize_net(); 3769 3770 kfree(vi->rq); 3771 kfree(vi->sq); 3772 kfree(vi->ctrl); 3773 } 3774 3775 static void _free_receive_bufs(struct virtnet_info *vi) 3776 { 3777 struct bpf_prog *old_prog; 3778 int i; 3779 3780 for (i = 0; i < vi->max_queue_pairs; i++) { 3781 while (vi->rq[i].pages) 3782 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0); 3783 3784 old_prog = rtnl_dereference(vi->rq[i].xdp_prog); 3785 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL); 3786 if (old_prog) 3787 bpf_prog_put(old_prog); 3788 } 3789 } 3790 3791 static void free_receive_bufs(struct virtnet_info *vi) 3792 { 3793 rtnl_lock(); 3794 _free_receive_bufs(vi); 3795 rtnl_unlock(); 3796 } 3797 3798 static void free_receive_page_frags(struct virtnet_info *vi) 3799 { 3800 int i; 3801 for (i = 0; i < vi->max_queue_pairs; i++) 3802 if (vi->rq[i].alloc_frag.page) 3803 put_page(vi->rq[i].alloc_frag.page); 3804 } 3805 3806 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf) 3807 { 3808 if (!is_xdp_frame(buf)) 3809 dev_kfree_skb(buf); 3810 else 3811 xdp_return_frame(ptr_to_xdp(buf)); 3812 } 3813 3814 static void virtnet_rq_free_unused_buf(struct virtqueue *vq, void *buf) 3815 { 3816 struct virtnet_info *vi = vq->vdev->priv; 3817 int i = vq2rxq(vq); 3818 3819 if (vi->mergeable_rx_bufs) 3820 put_page(virt_to_head_page(buf)); 3821 else if (vi->big_packets) 3822 give_pages(&vi->rq[i], buf); 3823 else 3824 put_page(virt_to_head_page(buf)); 3825 } 3826 3827 static void free_unused_bufs(struct virtnet_info *vi) 3828 { 3829 void *buf; 3830 int i; 3831 3832 for (i = 0; i < vi->max_queue_pairs; i++) { 3833 struct virtqueue *vq = vi->sq[i].vq; 3834 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) 3835 virtnet_sq_free_unused_buf(vq, buf); 3836 cond_resched(); 3837 } 3838 3839 for (i = 0; i < vi->max_queue_pairs; i++) { 3840 struct virtqueue *vq = vi->rq[i].vq; 3841 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) 3842 virtnet_rq_free_unused_buf(vq, buf); 3843 cond_resched(); 3844 } 3845 } 3846 3847 static void virtnet_del_vqs(struct virtnet_info *vi) 3848 { 3849 struct virtio_device *vdev = vi->vdev; 3850 3851 virtnet_clean_affinity(vi); 3852 3853 vdev->config->del_vqs(vdev); 3854 3855 virtnet_free_queues(vi); 3856 } 3857 3858 /* How large should a single buffer be so a queue full of these can fit at 3859 * least one full packet? 3860 * Logic below assumes the mergeable buffer header is used. 3861 */ 3862 static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq) 3863 { 3864 const unsigned int hdr_len = vi->hdr_len; 3865 unsigned int rq_size = virtqueue_get_vring_size(vq); 3866 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu; 3867 unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len; 3868 unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size); 3869 3870 return max(max(min_buf_len, hdr_len) - hdr_len, 3871 (unsigned int)GOOD_PACKET_LEN); 3872 } 3873 3874 static int virtnet_find_vqs(struct virtnet_info *vi) 3875 { 3876 vq_callback_t **callbacks; 3877 struct virtqueue **vqs; 3878 int ret = -ENOMEM; 3879 int i, total_vqs; 3880 const char **names; 3881 bool *ctx; 3882 3883 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by 3884 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by 3885 * possible control vq. 3886 */ 3887 total_vqs = vi->max_queue_pairs * 2 + 3888 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ); 3889 3890 /* Allocate space for find_vqs parameters */ 3891 vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL); 3892 if (!vqs) 3893 goto err_vq; 3894 callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL); 3895 if (!callbacks) 3896 goto err_callback; 3897 names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL); 3898 if (!names) 3899 goto err_names; 3900 if (!vi->big_packets || vi->mergeable_rx_bufs) { 3901 ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL); 3902 if (!ctx) 3903 goto err_ctx; 3904 } else { 3905 ctx = NULL; 3906 } 3907 3908 /* Parameters for control virtqueue, if any */ 3909 if (vi->has_cvq) { 3910 callbacks[total_vqs - 1] = NULL; 3911 names[total_vqs - 1] = "control"; 3912 } 3913 3914 /* Allocate/initialize parameters for send/receive virtqueues */ 3915 for (i = 0; i < vi->max_queue_pairs; i++) { 3916 callbacks[rxq2vq(i)] = skb_recv_done; 3917 callbacks[txq2vq(i)] = skb_xmit_done; 3918 sprintf(vi->rq[i].name, "input.%d", i); 3919 sprintf(vi->sq[i].name, "output.%d", i); 3920 names[rxq2vq(i)] = vi->rq[i].name; 3921 names[txq2vq(i)] = vi->sq[i].name; 3922 if (ctx) 3923 ctx[rxq2vq(i)] = true; 3924 } 3925 3926 ret = virtio_find_vqs_ctx(vi->vdev, total_vqs, vqs, callbacks, 3927 names, ctx, NULL); 3928 if (ret) 3929 goto err_find; 3930 3931 if (vi->has_cvq) { 3932 vi->cvq = vqs[total_vqs - 1]; 3933 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) 3934 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 3935 } 3936 3937 for (i = 0; i < vi->max_queue_pairs; i++) { 3938 vi->rq[i].vq = vqs[rxq2vq(i)]; 3939 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq); 3940 vi->sq[i].vq = vqs[txq2vq(i)]; 3941 } 3942 3943 /* run here: ret == 0. */ 3944 3945 3946 err_find: 3947 kfree(ctx); 3948 err_ctx: 3949 kfree(names); 3950 err_names: 3951 kfree(callbacks); 3952 err_callback: 3953 kfree(vqs); 3954 err_vq: 3955 return ret; 3956 } 3957 3958 static int virtnet_alloc_queues(struct virtnet_info *vi) 3959 { 3960 int i; 3961 3962 if (vi->has_cvq) { 3963 vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL); 3964 if (!vi->ctrl) 3965 goto err_ctrl; 3966 } else { 3967 vi->ctrl = NULL; 3968 } 3969 vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL); 3970 if (!vi->sq) 3971 goto err_sq; 3972 vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL); 3973 if (!vi->rq) 3974 goto err_rq; 3975 3976 INIT_DELAYED_WORK(&vi->refill, refill_work); 3977 for (i = 0; i < vi->max_queue_pairs; i++) { 3978 vi->rq[i].pages = NULL; 3979 netif_napi_add_weight(vi->dev, &vi->rq[i].napi, virtnet_poll, 3980 napi_weight); 3981 netif_napi_add_tx_weight(vi->dev, &vi->sq[i].napi, 3982 virtnet_poll_tx, 3983 napi_tx ? napi_weight : 0); 3984 3985 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg)); 3986 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len); 3987 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg)); 3988 3989 u64_stats_init(&vi->rq[i].stats.syncp); 3990 u64_stats_init(&vi->sq[i].stats.syncp); 3991 } 3992 3993 return 0; 3994 3995 err_rq: 3996 kfree(vi->sq); 3997 err_sq: 3998 kfree(vi->ctrl); 3999 err_ctrl: 4000 return -ENOMEM; 4001 } 4002 4003 static int init_vqs(struct virtnet_info *vi) 4004 { 4005 int ret; 4006 4007 /* Allocate send & receive queues */ 4008 ret = virtnet_alloc_queues(vi); 4009 if (ret) 4010 goto err; 4011 4012 ret = virtnet_find_vqs(vi); 4013 if (ret) 4014 goto err_free; 4015 4016 cpus_read_lock(); 4017 virtnet_set_affinity(vi); 4018 cpus_read_unlock(); 4019 4020 return 0; 4021 4022 err_free: 4023 virtnet_free_queues(vi); 4024 err: 4025 return ret; 4026 } 4027 4028 #ifdef CONFIG_SYSFS 4029 static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue, 4030 char *buf) 4031 { 4032 struct virtnet_info *vi = netdev_priv(queue->dev); 4033 unsigned int queue_index = get_netdev_rx_queue_index(queue); 4034 unsigned int headroom = virtnet_get_headroom(vi); 4035 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 4036 struct ewma_pkt_len *avg; 4037 4038 BUG_ON(queue_index >= vi->max_queue_pairs); 4039 avg = &vi->rq[queue_index].mrg_avg_pkt_len; 4040 return sprintf(buf, "%u\n", 4041 get_mergeable_buf_len(&vi->rq[queue_index], avg, 4042 SKB_DATA_ALIGN(headroom + tailroom))); 4043 } 4044 4045 static struct rx_queue_attribute mergeable_rx_buffer_size_attribute = 4046 __ATTR_RO(mergeable_rx_buffer_size); 4047 4048 static struct attribute *virtio_net_mrg_rx_attrs[] = { 4049 &mergeable_rx_buffer_size_attribute.attr, 4050 NULL 4051 }; 4052 4053 static const struct attribute_group virtio_net_mrg_rx_group = { 4054 .name = "virtio_net", 4055 .attrs = virtio_net_mrg_rx_attrs 4056 }; 4057 #endif 4058 4059 static bool virtnet_fail_on_feature(struct virtio_device *vdev, 4060 unsigned int fbit, 4061 const char *fname, const char *dname) 4062 { 4063 if (!virtio_has_feature(vdev, fbit)) 4064 return false; 4065 4066 dev_err(&vdev->dev, "device advertises feature %s but not %s", 4067 fname, dname); 4068 4069 return true; 4070 } 4071 4072 #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \ 4073 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit) 4074 4075 static bool virtnet_validate_features(struct virtio_device *vdev) 4076 { 4077 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) && 4078 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX, 4079 "VIRTIO_NET_F_CTRL_VQ") || 4080 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN, 4081 "VIRTIO_NET_F_CTRL_VQ") || 4082 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE, 4083 "VIRTIO_NET_F_CTRL_VQ") || 4084 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") || 4085 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR, 4086 "VIRTIO_NET_F_CTRL_VQ") || 4087 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_RSS, 4088 "VIRTIO_NET_F_CTRL_VQ") || 4089 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_HASH_REPORT, 4090 "VIRTIO_NET_F_CTRL_VQ") || 4091 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_NOTF_COAL, 4092 "VIRTIO_NET_F_CTRL_VQ") || 4093 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_VQ_NOTF_COAL, 4094 "VIRTIO_NET_F_CTRL_VQ"))) { 4095 return false; 4096 } 4097 4098 return true; 4099 } 4100 4101 #define MIN_MTU ETH_MIN_MTU 4102 #define MAX_MTU ETH_MAX_MTU 4103 4104 static int virtnet_validate(struct virtio_device *vdev) 4105 { 4106 if (!vdev->config->get) { 4107 dev_err(&vdev->dev, "%s failure: config access disabled\n", 4108 __func__); 4109 return -EINVAL; 4110 } 4111 4112 if (!virtnet_validate_features(vdev)) 4113 return -EINVAL; 4114 4115 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 4116 int mtu = virtio_cread16(vdev, 4117 offsetof(struct virtio_net_config, 4118 mtu)); 4119 if (mtu < MIN_MTU) 4120 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU); 4121 } 4122 4123 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY) && 4124 !virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) { 4125 dev_warn(&vdev->dev, "device advertises feature VIRTIO_NET_F_STANDBY but not VIRTIO_NET_F_MAC, disabling standby"); 4126 __virtio_clear_bit(vdev, VIRTIO_NET_F_STANDBY); 4127 } 4128 4129 return 0; 4130 } 4131 4132 static bool virtnet_check_guest_gso(const struct virtnet_info *vi) 4133 { 4134 return virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) || 4135 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) || 4136 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) || 4137 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) || 4138 (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) && 4139 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6)); 4140 } 4141 4142 static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu) 4143 { 4144 bool guest_gso = virtnet_check_guest_gso(vi); 4145 4146 /* If device can receive ANY guest GSO packets, regardless of mtu, 4147 * allocate packets of maximum size, otherwise limit it to only 4148 * mtu size worth only. 4149 */ 4150 if (mtu > ETH_DATA_LEN || guest_gso) { 4151 vi->big_packets = true; 4152 vi->big_packets_num_skbfrags = guest_gso ? MAX_SKB_FRAGS : DIV_ROUND_UP(mtu, PAGE_SIZE); 4153 } 4154 } 4155 4156 static int virtnet_probe(struct virtio_device *vdev) 4157 { 4158 int i, err = -ENOMEM; 4159 struct net_device *dev; 4160 struct virtnet_info *vi; 4161 u16 max_queue_pairs; 4162 int mtu = 0; 4163 4164 /* Find if host supports multiqueue/rss virtio_net device */ 4165 max_queue_pairs = 1; 4166 if (virtio_has_feature(vdev, VIRTIO_NET_F_MQ) || virtio_has_feature(vdev, VIRTIO_NET_F_RSS)) 4167 max_queue_pairs = 4168 virtio_cread16(vdev, offsetof(struct virtio_net_config, max_virtqueue_pairs)); 4169 4170 /* We need at least 2 queue's */ 4171 if (max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 4172 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 4173 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 4174 max_queue_pairs = 1; 4175 4176 /* Allocate ourselves a network device with room for our info */ 4177 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs); 4178 if (!dev) 4179 return -ENOMEM; 4180 4181 /* Set up network device as normal. */ 4182 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE | 4183 IFF_TX_SKB_NO_LINEAR; 4184 dev->netdev_ops = &virtnet_netdev; 4185 dev->features = NETIF_F_HIGHDMA; 4186 4187 dev->ethtool_ops = &virtnet_ethtool_ops; 4188 SET_NETDEV_DEV(dev, &vdev->dev); 4189 4190 /* Do we support "hardware" checksums? */ 4191 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { 4192 /* This opens up the world of extra features. */ 4193 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG; 4194 if (csum) 4195 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG; 4196 4197 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { 4198 dev->hw_features |= NETIF_F_TSO 4199 | NETIF_F_TSO_ECN | NETIF_F_TSO6; 4200 } 4201 /* Individual feature bits: what can host handle? */ 4202 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) 4203 dev->hw_features |= NETIF_F_TSO; 4204 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) 4205 dev->hw_features |= NETIF_F_TSO6; 4206 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) 4207 dev->hw_features |= NETIF_F_TSO_ECN; 4208 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_USO)) 4209 dev->hw_features |= NETIF_F_GSO_UDP_L4; 4210 4211 dev->features |= NETIF_F_GSO_ROBUST; 4212 4213 if (gso) 4214 dev->features |= dev->hw_features & NETIF_F_ALL_TSO; 4215 /* (!csum && gso) case will be fixed by register_netdev() */ 4216 } 4217 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM)) 4218 dev->features |= NETIF_F_RXCSUM; 4219 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || 4220 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)) 4221 dev->features |= NETIF_F_GRO_HW; 4222 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) 4223 dev->hw_features |= NETIF_F_GRO_HW; 4224 4225 dev->vlan_features = dev->features; 4226 dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT; 4227 4228 /* MTU range: 68 - 65535 */ 4229 dev->min_mtu = MIN_MTU; 4230 dev->max_mtu = MAX_MTU; 4231 4232 /* Configuration may specify what MAC to use. Otherwise random. */ 4233 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) { 4234 u8 addr[ETH_ALEN]; 4235 4236 virtio_cread_bytes(vdev, 4237 offsetof(struct virtio_net_config, mac), 4238 addr, ETH_ALEN); 4239 eth_hw_addr_set(dev, addr); 4240 } else { 4241 eth_hw_addr_random(dev); 4242 dev_info(&vdev->dev, "Assigned random MAC address %pM\n", 4243 dev->dev_addr); 4244 } 4245 4246 /* Set up our device-specific information */ 4247 vi = netdev_priv(dev); 4248 vi->dev = dev; 4249 vi->vdev = vdev; 4250 vdev->priv = vi; 4251 4252 INIT_WORK(&vi->config_work, virtnet_config_changed_work); 4253 spin_lock_init(&vi->refill_lock); 4254 4255 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) { 4256 vi->mergeable_rx_bufs = true; 4257 dev->xdp_features |= NETDEV_XDP_ACT_RX_SG; 4258 } 4259 4260 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) { 4261 vi->intr_coal_rx.max_usecs = 0; 4262 vi->intr_coal_tx.max_usecs = 0; 4263 vi->intr_coal_tx.max_packets = 0; 4264 vi->intr_coal_rx.max_packets = 0; 4265 } 4266 4267 if (virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT)) 4268 vi->has_rss_hash_report = true; 4269 4270 if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS)) 4271 vi->has_rss = true; 4272 4273 if (vi->has_rss || vi->has_rss_hash_report) { 4274 vi->rss_indir_table_size = 4275 virtio_cread16(vdev, offsetof(struct virtio_net_config, 4276 rss_max_indirection_table_length)); 4277 vi->rss_key_size = 4278 virtio_cread8(vdev, offsetof(struct virtio_net_config, rss_max_key_size)); 4279 4280 vi->rss_hash_types_supported = 4281 virtio_cread32(vdev, offsetof(struct virtio_net_config, supported_hash_types)); 4282 vi->rss_hash_types_supported &= 4283 ~(VIRTIO_NET_RSS_HASH_TYPE_IP_EX | 4284 VIRTIO_NET_RSS_HASH_TYPE_TCP_EX | 4285 VIRTIO_NET_RSS_HASH_TYPE_UDP_EX); 4286 4287 dev->hw_features |= NETIF_F_RXHASH; 4288 } 4289 4290 if (vi->has_rss_hash_report) 4291 vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash); 4292 else if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) || 4293 virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 4294 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 4295 else 4296 vi->hdr_len = sizeof(struct virtio_net_hdr); 4297 4298 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) || 4299 virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 4300 vi->any_header_sg = true; 4301 4302 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 4303 vi->has_cvq = true; 4304 4305 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 4306 mtu = virtio_cread16(vdev, 4307 offsetof(struct virtio_net_config, 4308 mtu)); 4309 if (mtu < dev->min_mtu) { 4310 /* Should never trigger: MTU was previously validated 4311 * in virtnet_validate. 4312 */ 4313 dev_err(&vdev->dev, 4314 "device MTU appears to have changed it is now %d < %d", 4315 mtu, dev->min_mtu); 4316 err = -EINVAL; 4317 goto free; 4318 } 4319 4320 dev->mtu = mtu; 4321 dev->max_mtu = mtu; 4322 } 4323 4324 virtnet_set_big_packets(vi, mtu); 4325 4326 if (vi->any_header_sg) 4327 dev->needed_headroom = vi->hdr_len; 4328 4329 /* Enable multiqueue by default */ 4330 if (num_online_cpus() >= max_queue_pairs) 4331 vi->curr_queue_pairs = max_queue_pairs; 4332 else 4333 vi->curr_queue_pairs = num_online_cpus(); 4334 vi->max_queue_pairs = max_queue_pairs; 4335 4336 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */ 4337 err = init_vqs(vi); 4338 if (err) 4339 goto free; 4340 4341 #ifdef CONFIG_SYSFS 4342 if (vi->mergeable_rx_bufs) 4343 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group; 4344 #endif 4345 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs); 4346 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs); 4347 4348 virtnet_init_settings(dev); 4349 4350 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) { 4351 vi->failover = net_failover_create(vi->dev); 4352 if (IS_ERR(vi->failover)) { 4353 err = PTR_ERR(vi->failover); 4354 goto free_vqs; 4355 } 4356 } 4357 4358 if (vi->has_rss || vi->has_rss_hash_report) 4359 virtnet_init_default_rss(vi); 4360 4361 _virtnet_set_queues(vi, vi->curr_queue_pairs); 4362 4363 /* serialize netdev register + virtio_device_ready() with ndo_open() */ 4364 rtnl_lock(); 4365 4366 err = register_netdevice(dev); 4367 if (err) { 4368 pr_debug("virtio_net: registering device failed\n"); 4369 rtnl_unlock(); 4370 goto free_failover; 4371 } 4372 4373 virtio_device_ready(vdev); 4374 4375 /* a random MAC address has been assigned, notify the device. 4376 * We don't fail probe if VIRTIO_NET_F_CTRL_MAC_ADDR is not there 4377 * because many devices work fine without getting MAC explicitly 4378 */ 4379 if (!virtio_has_feature(vdev, VIRTIO_NET_F_MAC) && 4380 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 4381 struct scatterlist sg; 4382 4383 sg_init_one(&sg, dev->dev_addr, dev->addr_len); 4384 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 4385 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) { 4386 pr_debug("virtio_net: setting MAC address failed\n"); 4387 rtnl_unlock(); 4388 err = -EINVAL; 4389 goto free_unregister_netdev; 4390 } 4391 } 4392 4393 rtnl_unlock(); 4394 4395 err = virtnet_cpu_notif_add(vi); 4396 if (err) { 4397 pr_debug("virtio_net: registering cpu notifier failed\n"); 4398 goto free_unregister_netdev; 4399 } 4400 4401 /* Assume link up if device can't report link status, 4402 otherwise get link status from config. */ 4403 netif_carrier_off(dev); 4404 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) { 4405 schedule_work(&vi->config_work); 4406 } else { 4407 vi->status = VIRTIO_NET_S_LINK_UP; 4408 virtnet_update_settings(vi); 4409 netif_carrier_on(dev); 4410 } 4411 4412 for (i = 0; i < ARRAY_SIZE(guest_offloads); i++) 4413 if (virtio_has_feature(vi->vdev, guest_offloads[i])) 4414 set_bit(guest_offloads[i], &vi->guest_offloads); 4415 vi->guest_offloads_capable = vi->guest_offloads; 4416 4417 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n", 4418 dev->name, max_queue_pairs); 4419 4420 return 0; 4421 4422 free_unregister_netdev: 4423 unregister_netdev(dev); 4424 free_failover: 4425 net_failover_destroy(vi->failover); 4426 free_vqs: 4427 virtio_reset_device(vdev); 4428 cancel_delayed_work_sync(&vi->refill); 4429 free_receive_page_frags(vi); 4430 virtnet_del_vqs(vi); 4431 free: 4432 free_netdev(dev); 4433 return err; 4434 } 4435 4436 static void remove_vq_common(struct virtnet_info *vi) 4437 { 4438 virtio_reset_device(vi->vdev); 4439 4440 /* Free unused buffers in both send and recv, if any. */ 4441 free_unused_bufs(vi); 4442 4443 free_receive_bufs(vi); 4444 4445 free_receive_page_frags(vi); 4446 4447 virtnet_del_vqs(vi); 4448 } 4449 4450 static void virtnet_remove(struct virtio_device *vdev) 4451 { 4452 struct virtnet_info *vi = vdev->priv; 4453 4454 virtnet_cpu_notif_remove(vi); 4455 4456 /* Make sure no work handler is accessing the device. */ 4457 flush_work(&vi->config_work); 4458 4459 unregister_netdev(vi->dev); 4460 4461 net_failover_destroy(vi->failover); 4462 4463 remove_vq_common(vi); 4464 4465 free_netdev(vi->dev); 4466 } 4467 4468 static __maybe_unused int virtnet_freeze(struct virtio_device *vdev) 4469 { 4470 struct virtnet_info *vi = vdev->priv; 4471 4472 virtnet_cpu_notif_remove(vi); 4473 virtnet_freeze_down(vdev); 4474 remove_vq_common(vi); 4475 4476 return 0; 4477 } 4478 4479 static __maybe_unused int virtnet_restore(struct virtio_device *vdev) 4480 { 4481 struct virtnet_info *vi = vdev->priv; 4482 int err; 4483 4484 err = virtnet_restore_up(vdev); 4485 if (err) 4486 return err; 4487 virtnet_set_queues(vi, vi->curr_queue_pairs); 4488 4489 err = virtnet_cpu_notif_add(vi); 4490 if (err) { 4491 virtnet_freeze_down(vdev); 4492 remove_vq_common(vi); 4493 return err; 4494 } 4495 4496 return 0; 4497 } 4498 4499 static struct virtio_device_id id_table[] = { 4500 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 4501 { 0 }, 4502 }; 4503 4504 #define VIRTNET_FEATURES \ 4505 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \ 4506 VIRTIO_NET_F_MAC, \ 4507 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \ 4508 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \ 4509 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \ 4510 VIRTIO_NET_F_HOST_USO, VIRTIO_NET_F_GUEST_USO4, VIRTIO_NET_F_GUEST_USO6, \ 4511 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \ 4512 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \ 4513 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \ 4514 VIRTIO_NET_F_CTRL_MAC_ADDR, \ 4515 VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \ 4516 VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY, \ 4517 VIRTIO_NET_F_RSS, VIRTIO_NET_F_HASH_REPORT, VIRTIO_NET_F_NOTF_COAL, \ 4518 VIRTIO_NET_F_VQ_NOTF_COAL, \ 4519 VIRTIO_NET_F_GUEST_HDRLEN 4520 4521 static unsigned int features[] = { 4522 VIRTNET_FEATURES, 4523 }; 4524 4525 static unsigned int features_legacy[] = { 4526 VIRTNET_FEATURES, 4527 VIRTIO_NET_F_GSO, 4528 VIRTIO_F_ANY_LAYOUT, 4529 }; 4530 4531 static struct virtio_driver virtio_net_driver = { 4532 .feature_table = features, 4533 .feature_table_size = ARRAY_SIZE(features), 4534 .feature_table_legacy = features_legacy, 4535 .feature_table_size_legacy = ARRAY_SIZE(features_legacy), 4536 .driver.name = KBUILD_MODNAME, 4537 .driver.owner = THIS_MODULE, 4538 .id_table = id_table, 4539 .validate = virtnet_validate, 4540 .probe = virtnet_probe, 4541 .remove = virtnet_remove, 4542 .config_changed = virtnet_config_changed, 4543 #ifdef CONFIG_PM_SLEEP 4544 .freeze = virtnet_freeze, 4545 .restore = virtnet_restore, 4546 #endif 4547 }; 4548 4549 static __init int virtio_net_driver_init(void) 4550 { 4551 int ret; 4552 4553 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online", 4554 virtnet_cpu_online, 4555 virtnet_cpu_down_prep); 4556 if (ret < 0) 4557 goto out; 4558 virtionet_online = ret; 4559 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead", 4560 NULL, virtnet_cpu_dead); 4561 if (ret) 4562 goto err_dead; 4563 ret = register_virtio_driver(&virtio_net_driver); 4564 if (ret) 4565 goto err_virtio; 4566 return 0; 4567 err_virtio: 4568 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 4569 err_dead: 4570 cpuhp_remove_multi_state(virtionet_online); 4571 out: 4572 return ret; 4573 } 4574 module_init(virtio_net_driver_init); 4575 4576 static __exit void virtio_net_driver_exit(void) 4577 { 4578 unregister_virtio_driver(&virtio_net_driver); 4579 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 4580 cpuhp_remove_multi_state(virtionet_online); 4581 } 4582 module_exit(virtio_net_driver_exit); 4583 4584 MODULE_DEVICE_TABLE(virtio, id_table); 4585 MODULE_DESCRIPTION("Virtio network driver"); 4586 MODULE_LICENSE("GPL"); 4587