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