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