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