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