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