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