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 <linux/dim.h> 23 #include <net/route.h> 24 #include <net/xdp.h> 25 #include <net/net_failover.h> 26 #include <net/netdev_rx_queue.h> 27 #include <net/netdev_queues.h> 28 29 static int napi_weight = NAPI_POLL_WEIGHT; 30 module_param(napi_weight, int, 0444); 31 32 static bool csum = true, gso = true, napi_tx = true; 33 module_param(csum, bool, 0444); 34 module_param(gso, bool, 0444); 35 module_param(napi_tx, bool, 0644); 36 37 /* FIXME: MTU in config. */ 38 #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN) 39 #define GOOD_COPY_LEN 128 40 41 #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD) 42 43 /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */ 44 #define VIRTIO_XDP_HEADROOM 256 45 46 /* Separating two types of XDP xmit */ 47 #define VIRTIO_XDP_TX BIT(0) 48 #define VIRTIO_XDP_REDIR BIT(1) 49 50 #define VIRTIO_XDP_FLAG BIT(0) 51 52 /* RX packet size EWMA. The average packet size is used to determine the packet 53 * buffer size when refilling RX rings. As the entire RX ring may be refilled 54 * at once, the weight is chosen so that the EWMA will be insensitive to short- 55 * term, transient changes in packet size. 56 */ 57 DECLARE_EWMA(pkt_len, 0, 64) 58 59 #define VIRTNET_DRIVER_VERSION "1.0.0" 60 61 static const unsigned long guest_offloads[] = { 62 VIRTIO_NET_F_GUEST_TSO4, 63 VIRTIO_NET_F_GUEST_TSO6, 64 VIRTIO_NET_F_GUEST_ECN, 65 VIRTIO_NET_F_GUEST_UFO, 66 VIRTIO_NET_F_GUEST_CSUM, 67 VIRTIO_NET_F_GUEST_USO4, 68 VIRTIO_NET_F_GUEST_USO6, 69 VIRTIO_NET_F_GUEST_HDRLEN 70 }; 71 72 #define GUEST_OFFLOAD_GRO_HW_MASK ((1ULL << VIRTIO_NET_F_GUEST_TSO4) | \ 73 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \ 74 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \ 75 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \ 76 (1ULL << VIRTIO_NET_F_GUEST_USO4) | \ 77 (1ULL << VIRTIO_NET_F_GUEST_USO6)) 78 79 struct virtnet_stat_desc { 80 char desc[ETH_GSTRING_LEN]; 81 size_t offset; 82 size_t qstat_offset; 83 }; 84 85 struct virtnet_sq_free_stats { 86 u64 packets; 87 u64 bytes; 88 }; 89 90 struct virtnet_sq_stats { 91 struct u64_stats_sync syncp; 92 u64_stats_t packets; 93 u64_stats_t bytes; 94 u64_stats_t xdp_tx; 95 u64_stats_t xdp_tx_drops; 96 u64_stats_t kicks; 97 u64_stats_t tx_timeouts; 98 u64_stats_t stop; 99 u64_stats_t wake; 100 }; 101 102 struct virtnet_rq_stats { 103 struct u64_stats_sync syncp; 104 u64_stats_t packets; 105 u64_stats_t bytes; 106 u64_stats_t drops; 107 u64_stats_t xdp_packets; 108 u64_stats_t xdp_tx; 109 u64_stats_t xdp_redirects; 110 u64_stats_t xdp_drops; 111 u64_stats_t kicks; 112 }; 113 114 #define VIRTNET_SQ_STAT(name, m) {name, offsetof(struct virtnet_sq_stats, m), -1} 115 #define VIRTNET_RQ_STAT(name, m) {name, offsetof(struct virtnet_rq_stats, m), -1} 116 117 #define VIRTNET_SQ_STAT_QSTAT(name, m) \ 118 { \ 119 name, \ 120 offsetof(struct virtnet_sq_stats, m), \ 121 offsetof(struct netdev_queue_stats_tx, m), \ 122 } 123 124 #define VIRTNET_RQ_STAT_QSTAT(name, m) \ 125 { \ 126 name, \ 127 offsetof(struct virtnet_rq_stats, m), \ 128 offsetof(struct netdev_queue_stats_rx, m), \ 129 } 130 131 static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = { 132 VIRTNET_SQ_STAT("xdp_tx", xdp_tx), 133 VIRTNET_SQ_STAT("xdp_tx_drops", xdp_tx_drops), 134 VIRTNET_SQ_STAT("kicks", kicks), 135 VIRTNET_SQ_STAT("tx_timeouts", tx_timeouts), 136 }; 137 138 static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = { 139 VIRTNET_RQ_STAT("drops", drops), 140 VIRTNET_RQ_STAT("xdp_packets", xdp_packets), 141 VIRTNET_RQ_STAT("xdp_tx", xdp_tx), 142 VIRTNET_RQ_STAT("xdp_redirects", xdp_redirects), 143 VIRTNET_RQ_STAT("xdp_drops", xdp_drops), 144 VIRTNET_RQ_STAT("kicks", kicks), 145 }; 146 147 static const struct virtnet_stat_desc virtnet_sq_stats_desc_qstat[] = { 148 VIRTNET_SQ_STAT_QSTAT("packets", packets), 149 VIRTNET_SQ_STAT_QSTAT("bytes", bytes), 150 VIRTNET_SQ_STAT_QSTAT("stop", stop), 151 VIRTNET_SQ_STAT_QSTAT("wake", wake), 152 }; 153 154 static const struct virtnet_stat_desc virtnet_rq_stats_desc_qstat[] = { 155 VIRTNET_RQ_STAT_QSTAT("packets", packets), 156 VIRTNET_RQ_STAT_QSTAT("bytes", bytes), 157 }; 158 159 #define VIRTNET_STATS_DESC_CQ(name) \ 160 {#name, offsetof(struct virtio_net_stats_cvq, name), -1} 161 162 #define VIRTNET_STATS_DESC_RX(class, name) \ 163 {#name, offsetof(struct virtio_net_stats_rx_ ## class, rx_ ## name), -1} 164 165 #define VIRTNET_STATS_DESC_TX(class, name) \ 166 {#name, offsetof(struct virtio_net_stats_tx_ ## class, tx_ ## name), -1} 167 168 169 static const struct virtnet_stat_desc virtnet_stats_cvq_desc[] = { 170 VIRTNET_STATS_DESC_CQ(command_num), 171 VIRTNET_STATS_DESC_CQ(ok_num), 172 }; 173 174 static const struct virtnet_stat_desc virtnet_stats_rx_basic_desc[] = { 175 VIRTNET_STATS_DESC_RX(basic, packets), 176 VIRTNET_STATS_DESC_RX(basic, bytes), 177 178 VIRTNET_STATS_DESC_RX(basic, notifications), 179 VIRTNET_STATS_DESC_RX(basic, interrupts), 180 }; 181 182 static const struct virtnet_stat_desc virtnet_stats_tx_basic_desc[] = { 183 VIRTNET_STATS_DESC_TX(basic, packets), 184 VIRTNET_STATS_DESC_TX(basic, bytes), 185 186 VIRTNET_STATS_DESC_TX(basic, notifications), 187 VIRTNET_STATS_DESC_TX(basic, interrupts), 188 }; 189 190 static const struct virtnet_stat_desc virtnet_stats_rx_csum_desc[] = { 191 VIRTNET_STATS_DESC_RX(csum, needs_csum), 192 }; 193 194 static const struct virtnet_stat_desc virtnet_stats_tx_gso_desc[] = { 195 VIRTNET_STATS_DESC_TX(gso, gso_packets_noseg), 196 VIRTNET_STATS_DESC_TX(gso, gso_bytes_noseg), 197 }; 198 199 static const struct virtnet_stat_desc virtnet_stats_rx_speed_desc[] = { 200 VIRTNET_STATS_DESC_RX(speed, ratelimit_bytes), 201 }; 202 203 static const struct virtnet_stat_desc virtnet_stats_tx_speed_desc[] = { 204 VIRTNET_STATS_DESC_TX(speed, ratelimit_bytes), 205 }; 206 207 #define VIRTNET_STATS_DESC_RX_QSTAT(class, name, qstat_field) \ 208 { \ 209 #name, \ 210 offsetof(struct virtio_net_stats_rx_ ## class, rx_ ## name), \ 211 offsetof(struct netdev_queue_stats_rx, qstat_field), \ 212 } 213 214 #define VIRTNET_STATS_DESC_TX_QSTAT(class, name, qstat_field) \ 215 { \ 216 #name, \ 217 offsetof(struct virtio_net_stats_tx_ ## class, tx_ ## name), \ 218 offsetof(struct netdev_queue_stats_tx, qstat_field), \ 219 } 220 221 static const struct virtnet_stat_desc virtnet_stats_rx_basic_desc_qstat[] = { 222 VIRTNET_STATS_DESC_RX_QSTAT(basic, drops, hw_drops), 223 VIRTNET_STATS_DESC_RX_QSTAT(basic, drop_overruns, hw_drop_overruns), 224 }; 225 226 static const struct virtnet_stat_desc virtnet_stats_tx_basic_desc_qstat[] = { 227 VIRTNET_STATS_DESC_TX_QSTAT(basic, drops, hw_drops), 228 VIRTNET_STATS_DESC_TX_QSTAT(basic, drop_malformed, hw_drop_errors), 229 }; 230 231 static const struct virtnet_stat_desc virtnet_stats_rx_csum_desc_qstat[] = { 232 VIRTNET_STATS_DESC_RX_QSTAT(csum, csum_valid, csum_unnecessary), 233 VIRTNET_STATS_DESC_RX_QSTAT(csum, csum_none, csum_none), 234 VIRTNET_STATS_DESC_RX_QSTAT(csum, csum_bad, csum_bad), 235 }; 236 237 static const struct virtnet_stat_desc virtnet_stats_tx_csum_desc_qstat[] = { 238 VIRTNET_STATS_DESC_TX_QSTAT(csum, csum_none, csum_none), 239 VIRTNET_STATS_DESC_TX_QSTAT(csum, needs_csum, needs_csum), 240 }; 241 242 static const struct virtnet_stat_desc virtnet_stats_rx_gso_desc_qstat[] = { 243 VIRTNET_STATS_DESC_RX_QSTAT(gso, gso_packets, hw_gro_packets), 244 VIRTNET_STATS_DESC_RX_QSTAT(gso, gso_bytes, hw_gro_bytes), 245 VIRTNET_STATS_DESC_RX_QSTAT(gso, gso_packets_coalesced, hw_gro_wire_packets), 246 VIRTNET_STATS_DESC_RX_QSTAT(gso, gso_bytes_coalesced, hw_gro_wire_bytes), 247 }; 248 249 static const struct virtnet_stat_desc virtnet_stats_tx_gso_desc_qstat[] = { 250 VIRTNET_STATS_DESC_TX_QSTAT(gso, gso_packets, hw_gso_packets), 251 VIRTNET_STATS_DESC_TX_QSTAT(gso, gso_bytes, hw_gso_bytes), 252 VIRTNET_STATS_DESC_TX_QSTAT(gso, gso_segments, hw_gso_wire_packets), 253 VIRTNET_STATS_DESC_TX_QSTAT(gso, gso_segments_bytes, hw_gso_wire_bytes), 254 }; 255 256 static const struct virtnet_stat_desc virtnet_stats_rx_speed_desc_qstat[] = { 257 VIRTNET_STATS_DESC_RX_QSTAT(speed, ratelimit_packets, hw_drop_ratelimits), 258 }; 259 260 static const struct virtnet_stat_desc virtnet_stats_tx_speed_desc_qstat[] = { 261 VIRTNET_STATS_DESC_TX_QSTAT(speed, ratelimit_packets, hw_drop_ratelimits), 262 }; 263 264 #define VIRTNET_Q_TYPE_RX 0 265 #define VIRTNET_Q_TYPE_TX 1 266 #define VIRTNET_Q_TYPE_CQ 2 267 268 struct virtnet_interrupt_coalesce { 269 u32 max_packets; 270 u32 max_usecs; 271 }; 272 273 /* The dma information of pages allocated at a time. */ 274 struct virtnet_rq_dma { 275 dma_addr_t addr; 276 u32 ref; 277 u16 len; 278 u16 need_sync; 279 }; 280 281 /* Internal representation of a send virtqueue */ 282 struct send_queue { 283 /* Virtqueue associated with this send _queue */ 284 struct virtqueue *vq; 285 286 /* TX: fragments + linear part + virtio header */ 287 struct scatterlist sg[MAX_SKB_FRAGS + 2]; 288 289 /* Name of the send queue: output.$index */ 290 char name[16]; 291 292 struct virtnet_sq_stats stats; 293 294 struct virtnet_interrupt_coalesce intr_coal; 295 296 struct napi_struct napi; 297 298 /* Record whether sq is in reset state. */ 299 bool reset; 300 }; 301 302 /* Internal representation of a receive virtqueue */ 303 struct receive_queue { 304 /* Virtqueue associated with this receive_queue */ 305 struct virtqueue *vq; 306 307 struct napi_struct napi; 308 309 struct bpf_prog __rcu *xdp_prog; 310 311 struct virtnet_rq_stats stats; 312 313 /* The number of rx notifications */ 314 u16 calls; 315 316 /* Is dynamic interrupt moderation enabled? */ 317 bool dim_enabled; 318 319 /* Used to protect dim_enabled and inter_coal */ 320 struct mutex dim_lock; 321 322 /* Dynamic Interrupt Moderation */ 323 struct dim dim; 324 325 u32 packets_in_napi; 326 327 struct virtnet_interrupt_coalesce intr_coal; 328 329 /* Chain pages by the private ptr. */ 330 struct page *pages; 331 332 /* Average packet length for mergeable receive buffers. */ 333 struct ewma_pkt_len mrg_avg_pkt_len; 334 335 /* Page frag for packet buffer allocation. */ 336 struct page_frag alloc_frag; 337 338 /* RX: fragments + linear part + virtio header */ 339 struct scatterlist sg[MAX_SKB_FRAGS + 2]; 340 341 /* Min single buffer size for mergeable buffers case. */ 342 unsigned int min_buf_len; 343 344 /* Name of this receive queue: input.$index */ 345 char name[16]; 346 347 struct xdp_rxq_info xdp_rxq; 348 349 /* Record the last dma info to free after new pages is allocated. */ 350 struct virtnet_rq_dma *last_dma; 351 }; 352 353 /* This structure can contain rss message with maximum settings for indirection table and keysize 354 * Note, that default structure that describes RSS configuration virtio_net_rss_config 355 * contains same info but can't handle table values. 356 * In any case, structure would be passed to virtio hw through sg_buf split by parts 357 * because table sizes may be differ according to the device configuration. 358 */ 359 #define VIRTIO_NET_RSS_MAX_KEY_SIZE 40 360 #define VIRTIO_NET_RSS_MAX_TABLE_LEN 128 361 struct virtio_net_ctrl_rss { 362 u32 hash_types; 363 u16 indirection_table_mask; 364 u16 unclassified_queue; 365 u16 indirection_table[VIRTIO_NET_RSS_MAX_TABLE_LEN]; 366 u16 max_tx_vq; 367 u8 hash_key_length; 368 u8 key[VIRTIO_NET_RSS_MAX_KEY_SIZE]; 369 }; 370 371 /* Control VQ buffers: protected by the rtnl lock */ 372 struct control_buf { 373 struct virtio_net_ctrl_hdr hdr; 374 virtio_net_ctrl_ack status; 375 }; 376 377 struct virtnet_info { 378 struct virtio_device *vdev; 379 struct virtqueue *cvq; 380 struct net_device *dev; 381 struct send_queue *sq; 382 struct receive_queue *rq; 383 unsigned int status; 384 385 /* Max # of queue pairs supported by the device */ 386 u16 max_queue_pairs; 387 388 /* # of queue pairs currently used by the driver */ 389 u16 curr_queue_pairs; 390 391 /* # of XDP queue pairs currently used by the driver */ 392 u16 xdp_queue_pairs; 393 394 /* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */ 395 bool xdp_enabled; 396 397 /* I like... big packets and I cannot lie! */ 398 bool big_packets; 399 400 /* number of sg entries allocated for big packets */ 401 unsigned int big_packets_num_skbfrags; 402 403 /* Host will merge rx buffers for big packets (shake it! shake it!) */ 404 bool mergeable_rx_bufs; 405 406 /* Host supports rss and/or hash report */ 407 bool has_rss; 408 bool has_rss_hash_report; 409 u8 rss_key_size; 410 u16 rss_indir_table_size; 411 u32 rss_hash_types_supported; 412 u32 rss_hash_types_saved; 413 struct virtio_net_ctrl_rss rss; 414 415 /* Has control virtqueue */ 416 bool has_cvq; 417 418 /* Lock to protect the control VQ */ 419 struct mutex cvq_lock; 420 421 /* Host can handle any s/g split between our header and packet data */ 422 bool any_header_sg; 423 424 /* Packet virtio header size */ 425 u8 hdr_len; 426 427 /* Work struct for delayed refilling if we run low on memory. */ 428 struct delayed_work refill; 429 430 /* Is delayed refill enabled? */ 431 bool refill_enabled; 432 433 /* The lock to synchronize the access to refill_enabled */ 434 spinlock_t refill_lock; 435 436 /* Work struct for config space updates */ 437 struct work_struct config_work; 438 439 /* Work struct for setting rx mode */ 440 struct work_struct rx_mode_work; 441 442 /* OK to queue work setting RX mode? */ 443 bool rx_mode_work_enabled; 444 445 /* Does the affinity hint is set for virtqueues? */ 446 bool affinity_hint_set; 447 448 /* CPU hotplug instances for online & dead */ 449 struct hlist_node node; 450 struct hlist_node node_dead; 451 452 struct control_buf *ctrl; 453 454 /* Ethtool settings */ 455 u8 duplex; 456 u32 speed; 457 458 /* Is rx dynamic interrupt moderation enabled? */ 459 bool rx_dim_enabled; 460 461 /* Interrupt coalescing settings */ 462 struct virtnet_interrupt_coalesce intr_coal_tx; 463 struct virtnet_interrupt_coalesce intr_coal_rx; 464 465 unsigned long guest_offloads; 466 unsigned long guest_offloads_capable; 467 468 /* failover when STANDBY feature enabled */ 469 struct failover *failover; 470 471 u64 device_stats_cap; 472 }; 473 474 struct padded_vnet_hdr { 475 struct virtio_net_hdr_v1_hash hdr; 476 /* 477 * hdr is in a separate sg buffer, and data sg buffer shares same page 478 * with this header sg. This padding makes next sg 16 byte aligned 479 * after the header. 480 */ 481 char padding[12]; 482 }; 483 484 struct virtio_net_common_hdr { 485 union { 486 struct virtio_net_hdr hdr; 487 struct virtio_net_hdr_mrg_rxbuf mrg_hdr; 488 struct virtio_net_hdr_v1_hash hash_v1_hdr; 489 }; 490 }; 491 492 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf); 493 494 static bool is_xdp_frame(void *ptr) 495 { 496 return (unsigned long)ptr & VIRTIO_XDP_FLAG; 497 } 498 499 static void *xdp_to_ptr(struct xdp_frame *ptr) 500 { 501 return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG); 502 } 503 504 static struct xdp_frame *ptr_to_xdp(void *ptr) 505 { 506 return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG); 507 } 508 509 static void __free_old_xmit(struct send_queue *sq, bool in_napi, 510 struct virtnet_sq_free_stats *stats) 511 { 512 unsigned int len; 513 void *ptr; 514 515 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) { 516 ++stats->packets; 517 518 if (!is_xdp_frame(ptr)) { 519 struct sk_buff *skb = ptr; 520 521 pr_debug("Sent skb %p\n", skb); 522 523 stats->bytes += skb->len; 524 napi_consume_skb(skb, in_napi); 525 } else { 526 struct xdp_frame *frame = ptr_to_xdp(ptr); 527 528 stats->bytes += xdp_get_frame_len(frame); 529 xdp_return_frame(frame); 530 } 531 } 532 } 533 534 /* Converting between virtqueue no. and kernel tx/rx queue no. 535 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq 536 */ 537 static int vq2txq(struct virtqueue *vq) 538 { 539 return (vq->index - 1) / 2; 540 } 541 542 static int txq2vq(int txq) 543 { 544 return txq * 2 + 1; 545 } 546 547 static int vq2rxq(struct virtqueue *vq) 548 { 549 return vq->index / 2; 550 } 551 552 static int rxq2vq(int rxq) 553 { 554 return rxq * 2; 555 } 556 557 static int vq_type(struct virtnet_info *vi, int qid) 558 { 559 if (qid == vi->max_queue_pairs * 2) 560 return VIRTNET_Q_TYPE_CQ; 561 562 if (qid % 2) 563 return VIRTNET_Q_TYPE_TX; 564 565 return VIRTNET_Q_TYPE_RX; 566 } 567 568 static inline struct virtio_net_common_hdr * 569 skb_vnet_common_hdr(struct sk_buff *skb) 570 { 571 return (struct virtio_net_common_hdr *)skb->cb; 572 } 573 574 /* 575 * private is used to chain pages for big packets, put the whole 576 * most recent used list in the beginning for reuse 577 */ 578 static void give_pages(struct receive_queue *rq, struct page *page) 579 { 580 struct page *end; 581 582 /* Find end of list, sew whole thing into vi->rq.pages. */ 583 for (end = page; end->private; end = (struct page *)end->private); 584 end->private = (unsigned long)rq->pages; 585 rq->pages = page; 586 } 587 588 static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask) 589 { 590 struct page *p = rq->pages; 591 592 if (p) { 593 rq->pages = (struct page *)p->private; 594 /* clear private here, it is used to chain pages */ 595 p->private = 0; 596 } else 597 p = alloc_page(gfp_mask); 598 return p; 599 } 600 601 static void virtnet_rq_free_buf(struct virtnet_info *vi, 602 struct receive_queue *rq, void *buf) 603 { 604 if (vi->mergeable_rx_bufs) 605 put_page(virt_to_head_page(buf)); 606 else if (vi->big_packets) 607 give_pages(rq, buf); 608 else 609 put_page(virt_to_head_page(buf)); 610 } 611 612 static void enable_delayed_refill(struct virtnet_info *vi) 613 { 614 spin_lock_bh(&vi->refill_lock); 615 vi->refill_enabled = true; 616 spin_unlock_bh(&vi->refill_lock); 617 } 618 619 static void disable_delayed_refill(struct virtnet_info *vi) 620 { 621 spin_lock_bh(&vi->refill_lock); 622 vi->refill_enabled = false; 623 spin_unlock_bh(&vi->refill_lock); 624 } 625 626 static void enable_rx_mode_work(struct virtnet_info *vi) 627 { 628 rtnl_lock(); 629 vi->rx_mode_work_enabled = true; 630 rtnl_unlock(); 631 } 632 633 static void disable_rx_mode_work(struct virtnet_info *vi) 634 { 635 rtnl_lock(); 636 vi->rx_mode_work_enabled = false; 637 rtnl_unlock(); 638 } 639 640 static void virtqueue_napi_schedule(struct napi_struct *napi, 641 struct virtqueue *vq) 642 { 643 if (napi_schedule_prep(napi)) { 644 virtqueue_disable_cb(vq); 645 __napi_schedule(napi); 646 } 647 } 648 649 static bool virtqueue_napi_complete(struct napi_struct *napi, 650 struct virtqueue *vq, int processed) 651 { 652 int opaque; 653 654 opaque = virtqueue_enable_cb_prepare(vq); 655 if (napi_complete_done(napi, processed)) { 656 if (unlikely(virtqueue_poll(vq, opaque))) 657 virtqueue_napi_schedule(napi, vq); 658 else 659 return true; 660 } else { 661 virtqueue_disable_cb(vq); 662 } 663 664 return false; 665 } 666 667 static void skb_xmit_done(struct virtqueue *vq) 668 { 669 struct virtnet_info *vi = vq->vdev->priv; 670 struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi; 671 672 /* Suppress further interrupts. */ 673 virtqueue_disable_cb(vq); 674 675 if (napi->weight) 676 virtqueue_napi_schedule(napi, vq); 677 else 678 /* We were probably waiting for more output buffers. */ 679 netif_wake_subqueue(vi->dev, vq2txq(vq)); 680 } 681 682 #define MRG_CTX_HEADER_SHIFT 22 683 static void *mergeable_len_to_ctx(unsigned int truesize, 684 unsigned int headroom) 685 { 686 return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize); 687 } 688 689 static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx) 690 { 691 return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT; 692 } 693 694 static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx) 695 { 696 return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1); 697 } 698 699 static struct sk_buff *virtnet_build_skb(void *buf, unsigned int buflen, 700 unsigned int headroom, 701 unsigned int len) 702 { 703 struct sk_buff *skb; 704 705 skb = build_skb(buf, buflen); 706 if (unlikely(!skb)) 707 return NULL; 708 709 skb_reserve(skb, headroom); 710 skb_put(skb, len); 711 712 return skb; 713 } 714 715 /* Called from bottom half context */ 716 static struct sk_buff *page_to_skb(struct virtnet_info *vi, 717 struct receive_queue *rq, 718 struct page *page, unsigned int offset, 719 unsigned int len, unsigned int truesize, 720 unsigned int headroom) 721 { 722 struct sk_buff *skb; 723 struct virtio_net_common_hdr *hdr; 724 unsigned int copy, hdr_len, hdr_padded_len; 725 struct page *page_to_free = NULL; 726 int tailroom, shinfo_size; 727 char *p, *hdr_p, *buf; 728 729 p = page_address(page) + offset; 730 hdr_p = p; 731 732 hdr_len = vi->hdr_len; 733 if (vi->mergeable_rx_bufs) 734 hdr_padded_len = hdr_len; 735 else 736 hdr_padded_len = sizeof(struct padded_vnet_hdr); 737 738 buf = p - headroom; 739 len -= hdr_len; 740 offset += hdr_padded_len; 741 p += hdr_padded_len; 742 tailroom = truesize - headroom - hdr_padded_len - len; 743 744 shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 745 746 if (!NET_IP_ALIGN && len > GOOD_COPY_LEN && tailroom >= shinfo_size) { 747 skb = virtnet_build_skb(buf, truesize, p - buf, len); 748 if (unlikely(!skb)) 749 return NULL; 750 751 page = (struct page *)page->private; 752 if (page) 753 give_pages(rq, page); 754 goto ok; 755 } 756 757 /* copy small packet so we can reuse these pages for small data */ 758 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN); 759 if (unlikely(!skb)) 760 return NULL; 761 762 /* Copy all frame if it fits skb->head, otherwise 763 * we let virtio_net_hdr_to_skb() and GRO pull headers as needed. 764 */ 765 if (len <= skb_tailroom(skb)) 766 copy = len; 767 else 768 copy = ETH_HLEN; 769 skb_put_data(skb, p, copy); 770 771 len -= copy; 772 offset += copy; 773 774 if (vi->mergeable_rx_bufs) { 775 if (len) 776 skb_add_rx_frag(skb, 0, page, offset, len, truesize); 777 else 778 page_to_free = page; 779 goto ok; 780 } 781 782 /* 783 * Verify that we can indeed put this data into a skb. 784 * This is here to handle cases when the device erroneously 785 * tries to receive more than is possible. This is usually 786 * the case of a broken device. 787 */ 788 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) { 789 net_dbg_ratelimited("%s: too much data\n", skb->dev->name); 790 dev_kfree_skb(skb); 791 return NULL; 792 } 793 BUG_ON(offset >= PAGE_SIZE); 794 while (len) { 795 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len); 796 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset, 797 frag_size, truesize); 798 len -= frag_size; 799 page = (struct page *)page->private; 800 offset = 0; 801 } 802 803 if (page) 804 give_pages(rq, page); 805 806 ok: 807 hdr = skb_vnet_common_hdr(skb); 808 memcpy(hdr, hdr_p, hdr_len); 809 if (page_to_free) 810 put_page(page_to_free); 811 812 return skb; 813 } 814 815 static void virtnet_rq_unmap(struct receive_queue *rq, void *buf, u32 len) 816 { 817 struct page *page = virt_to_head_page(buf); 818 struct virtnet_rq_dma *dma; 819 void *head; 820 int offset; 821 822 head = page_address(page); 823 824 dma = head; 825 826 --dma->ref; 827 828 if (dma->need_sync && len) { 829 offset = buf - (head + sizeof(*dma)); 830 831 virtqueue_dma_sync_single_range_for_cpu(rq->vq, dma->addr, 832 offset, len, 833 DMA_FROM_DEVICE); 834 } 835 836 if (dma->ref) 837 return; 838 839 virtqueue_dma_unmap_single_attrs(rq->vq, dma->addr, dma->len, 840 DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC); 841 put_page(page); 842 } 843 844 static void *virtnet_rq_get_buf(struct receive_queue *rq, u32 *len, void **ctx) 845 { 846 void *buf; 847 848 buf = virtqueue_get_buf_ctx(rq->vq, len, ctx); 849 if (buf) 850 virtnet_rq_unmap(rq, buf, *len); 851 852 return buf; 853 } 854 855 static void virtnet_rq_init_one_sg(struct receive_queue *rq, void *buf, u32 len) 856 { 857 struct virtnet_rq_dma *dma; 858 dma_addr_t addr; 859 u32 offset; 860 void *head; 861 862 head = page_address(rq->alloc_frag.page); 863 864 offset = buf - head; 865 866 dma = head; 867 868 addr = dma->addr - sizeof(*dma) + offset; 869 870 sg_init_table(rq->sg, 1); 871 rq->sg[0].dma_address = addr; 872 rq->sg[0].length = len; 873 } 874 875 static void *virtnet_rq_alloc(struct receive_queue *rq, u32 size, gfp_t gfp) 876 { 877 struct page_frag *alloc_frag = &rq->alloc_frag; 878 struct virtnet_rq_dma *dma; 879 void *buf, *head; 880 dma_addr_t addr; 881 882 if (unlikely(!skb_page_frag_refill(size, alloc_frag, gfp))) 883 return NULL; 884 885 head = page_address(alloc_frag->page); 886 887 dma = head; 888 889 /* new pages */ 890 if (!alloc_frag->offset) { 891 if (rq->last_dma) { 892 /* Now, the new page is allocated, the last dma 893 * will not be used. So the dma can be unmapped 894 * if the ref is 0. 895 */ 896 virtnet_rq_unmap(rq, rq->last_dma, 0); 897 rq->last_dma = NULL; 898 } 899 900 dma->len = alloc_frag->size - sizeof(*dma); 901 902 addr = virtqueue_dma_map_single_attrs(rq->vq, dma + 1, 903 dma->len, DMA_FROM_DEVICE, 0); 904 if (virtqueue_dma_mapping_error(rq->vq, addr)) 905 return NULL; 906 907 dma->addr = addr; 908 dma->need_sync = virtqueue_dma_need_sync(rq->vq, addr); 909 910 /* Add a reference to dma to prevent the entire dma from 911 * being released during error handling. This reference 912 * will be freed after the pages are no longer used. 913 */ 914 get_page(alloc_frag->page); 915 dma->ref = 1; 916 alloc_frag->offset = sizeof(*dma); 917 918 rq->last_dma = dma; 919 } 920 921 ++dma->ref; 922 923 buf = head + alloc_frag->offset; 924 925 get_page(alloc_frag->page); 926 alloc_frag->offset += size; 927 928 return buf; 929 } 930 931 static void virtnet_rq_set_premapped(struct virtnet_info *vi) 932 { 933 int i; 934 935 /* disable for big mode */ 936 if (!vi->mergeable_rx_bufs && vi->big_packets) 937 return; 938 939 for (i = 0; i < vi->max_queue_pairs; i++) 940 /* error should never happen */ 941 BUG_ON(virtqueue_set_dma_premapped(vi->rq[i].vq)); 942 } 943 944 static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf) 945 { 946 struct virtnet_info *vi = vq->vdev->priv; 947 struct receive_queue *rq; 948 int i = vq2rxq(vq); 949 950 rq = &vi->rq[i]; 951 952 if (!vi->big_packets || vi->mergeable_rx_bufs) 953 virtnet_rq_unmap(rq, buf, 0); 954 955 virtnet_rq_free_buf(vi, rq, buf); 956 } 957 958 static void free_old_xmit(struct send_queue *sq, bool in_napi) 959 { 960 struct virtnet_sq_free_stats stats = {0}; 961 962 __free_old_xmit(sq, in_napi, &stats); 963 964 /* Avoid overhead when no packets have been processed 965 * happens when called speculatively from start_xmit. 966 */ 967 if (!stats.packets) 968 return; 969 970 u64_stats_update_begin(&sq->stats.syncp); 971 u64_stats_add(&sq->stats.bytes, stats.bytes); 972 u64_stats_add(&sq->stats.packets, stats.packets); 973 u64_stats_update_end(&sq->stats.syncp); 974 } 975 976 static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q) 977 { 978 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs)) 979 return false; 980 else if (q < vi->curr_queue_pairs) 981 return true; 982 else 983 return false; 984 } 985 986 static void check_sq_full_and_disable(struct virtnet_info *vi, 987 struct net_device *dev, 988 struct send_queue *sq) 989 { 990 bool use_napi = sq->napi.weight; 991 int qnum; 992 993 qnum = sq - vi->sq; 994 995 /* If running out of space, stop queue to avoid getting packets that we 996 * are then unable to transmit. 997 * An alternative would be to force queuing layer to requeue the skb by 998 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be 999 * returned in a normal path of operation: it means that driver is not 1000 * maintaining the TX queue stop/start state properly, and causes 1001 * the stack to do a non-trivial amount of useless work. 1002 * Since most packets only take 1 or 2 ring slots, stopping the queue 1003 * early means 16 slots are typically wasted. 1004 */ 1005 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) { 1006 netif_stop_subqueue(dev, qnum); 1007 u64_stats_update_begin(&sq->stats.syncp); 1008 u64_stats_inc(&sq->stats.stop); 1009 u64_stats_update_end(&sq->stats.syncp); 1010 if (use_napi) { 1011 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) 1012 virtqueue_napi_schedule(&sq->napi, sq->vq); 1013 } else if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) { 1014 /* More just got used, free them then recheck. */ 1015 free_old_xmit(sq, false); 1016 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) { 1017 netif_start_subqueue(dev, qnum); 1018 u64_stats_update_begin(&sq->stats.syncp); 1019 u64_stats_inc(&sq->stats.wake); 1020 u64_stats_update_end(&sq->stats.syncp); 1021 virtqueue_disable_cb(sq->vq); 1022 } 1023 } 1024 } 1025 } 1026 1027 static int __virtnet_xdp_xmit_one(struct virtnet_info *vi, 1028 struct send_queue *sq, 1029 struct xdp_frame *xdpf) 1030 { 1031 struct virtio_net_hdr_mrg_rxbuf *hdr; 1032 struct skb_shared_info *shinfo; 1033 u8 nr_frags = 0; 1034 int err, i; 1035 1036 if (unlikely(xdpf->headroom < vi->hdr_len)) 1037 return -EOVERFLOW; 1038 1039 if (unlikely(xdp_frame_has_frags(xdpf))) { 1040 shinfo = xdp_get_shared_info_from_frame(xdpf); 1041 nr_frags = shinfo->nr_frags; 1042 } 1043 1044 /* In wrapping function virtnet_xdp_xmit(), we need to free 1045 * up the pending old buffers, where we need to calculate the 1046 * position of skb_shared_info in xdp_get_frame_len() and 1047 * xdp_return_frame(), which will involve to xdpf->data and 1048 * xdpf->headroom. Therefore, we need to update the value of 1049 * headroom synchronously here. 1050 */ 1051 xdpf->headroom -= vi->hdr_len; 1052 xdpf->data -= vi->hdr_len; 1053 /* Zero header and leave csum up to XDP layers */ 1054 hdr = xdpf->data; 1055 memset(hdr, 0, vi->hdr_len); 1056 xdpf->len += vi->hdr_len; 1057 1058 sg_init_table(sq->sg, nr_frags + 1); 1059 sg_set_buf(sq->sg, xdpf->data, xdpf->len); 1060 for (i = 0; i < nr_frags; i++) { 1061 skb_frag_t *frag = &shinfo->frags[i]; 1062 1063 sg_set_page(&sq->sg[i + 1], skb_frag_page(frag), 1064 skb_frag_size(frag), skb_frag_off(frag)); 1065 } 1066 1067 err = virtqueue_add_outbuf(sq->vq, sq->sg, nr_frags + 1, 1068 xdp_to_ptr(xdpf), GFP_ATOMIC); 1069 if (unlikely(err)) 1070 return -ENOSPC; /* Caller handle free/refcnt */ 1071 1072 return 0; 1073 } 1074 1075 /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on 1076 * the current cpu, so it does not need to be locked. 1077 * 1078 * Here we use marco instead of inline functions because we have to deal with 1079 * three issues at the same time: 1. the choice of sq. 2. judge and execute the 1080 * lock/unlock of txq 3. make sparse happy. It is difficult for two inline 1081 * functions to perfectly solve these three problems at the same time. 1082 */ 1083 #define virtnet_xdp_get_sq(vi) ({ \ 1084 int cpu = smp_processor_id(); \ 1085 struct netdev_queue *txq; \ 1086 typeof(vi) v = (vi); \ 1087 unsigned int qp; \ 1088 \ 1089 if (v->curr_queue_pairs > nr_cpu_ids) { \ 1090 qp = v->curr_queue_pairs - v->xdp_queue_pairs; \ 1091 qp += cpu; \ 1092 txq = netdev_get_tx_queue(v->dev, qp); \ 1093 __netif_tx_acquire(txq); \ 1094 } else { \ 1095 qp = cpu % v->curr_queue_pairs; \ 1096 txq = netdev_get_tx_queue(v->dev, qp); \ 1097 __netif_tx_lock(txq, cpu); \ 1098 } \ 1099 v->sq + qp; \ 1100 }) 1101 1102 #define virtnet_xdp_put_sq(vi, q) { \ 1103 struct netdev_queue *txq; \ 1104 typeof(vi) v = (vi); \ 1105 \ 1106 txq = netdev_get_tx_queue(v->dev, (q) - v->sq); \ 1107 if (v->curr_queue_pairs > nr_cpu_ids) \ 1108 __netif_tx_release(txq); \ 1109 else \ 1110 __netif_tx_unlock(txq); \ 1111 } 1112 1113 static int virtnet_xdp_xmit(struct net_device *dev, 1114 int n, struct xdp_frame **frames, u32 flags) 1115 { 1116 struct virtnet_info *vi = netdev_priv(dev); 1117 struct virtnet_sq_free_stats stats = {0}; 1118 struct receive_queue *rq = vi->rq; 1119 struct bpf_prog *xdp_prog; 1120 struct send_queue *sq; 1121 int nxmit = 0; 1122 int kicks = 0; 1123 int ret; 1124 int i; 1125 1126 /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this 1127 * indicate XDP resources have been successfully allocated. 1128 */ 1129 xdp_prog = rcu_access_pointer(rq->xdp_prog); 1130 if (!xdp_prog) 1131 return -ENXIO; 1132 1133 sq = virtnet_xdp_get_sq(vi); 1134 1135 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) { 1136 ret = -EINVAL; 1137 goto out; 1138 } 1139 1140 /* Free up any pending old buffers before queueing new ones. */ 1141 __free_old_xmit(sq, false, &stats); 1142 1143 for (i = 0; i < n; i++) { 1144 struct xdp_frame *xdpf = frames[i]; 1145 1146 if (__virtnet_xdp_xmit_one(vi, sq, xdpf)) 1147 break; 1148 nxmit++; 1149 } 1150 ret = nxmit; 1151 1152 if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq)) 1153 check_sq_full_and_disable(vi, dev, sq); 1154 1155 if (flags & XDP_XMIT_FLUSH) { 1156 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) 1157 kicks = 1; 1158 } 1159 out: 1160 u64_stats_update_begin(&sq->stats.syncp); 1161 u64_stats_add(&sq->stats.bytes, stats.bytes); 1162 u64_stats_add(&sq->stats.packets, stats.packets); 1163 u64_stats_add(&sq->stats.xdp_tx, n); 1164 u64_stats_add(&sq->stats.xdp_tx_drops, n - nxmit); 1165 u64_stats_add(&sq->stats.kicks, kicks); 1166 u64_stats_update_end(&sq->stats.syncp); 1167 1168 virtnet_xdp_put_sq(vi, sq); 1169 return ret; 1170 } 1171 1172 static void put_xdp_frags(struct xdp_buff *xdp) 1173 { 1174 struct skb_shared_info *shinfo; 1175 struct page *xdp_page; 1176 int i; 1177 1178 if (xdp_buff_has_frags(xdp)) { 1179 shinfo = xdp_get_shared_info_from_buff(xdp); 1180 for (i = 0; i < shinfo->nr_frags; i++) { 1181 xdp_page = skb_frag_page(&shinfo->frags[i]); 1182 put_page(xdp_page); 1183 } 1184 } 1185 } 1186 1187 static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp, 1188 struct net_device *dev, 1189 unsigned int *xdp_xmit, 1190 struct virtnet_rq_stats *stats) 1191 { 1192 struct xdp_frame *xdpf; 1193 int err; 1194 u32 act; 1195 1196 act = bpf_prog_run_xdp(xdp_prog, xdp); 1197 u64_stats_inc(&stats->xdp_packets); 1198 1199 switch (act) { 1200 case XDP_PASS: 1201 return act; 1202 1203 case XDP_TX: 1204 u64_stats_inc(&stats->xdp_tx); 1205 xdpf = xdp_convert_buff_to_frame(xdp); 1206 if (unlikely(!xdpf)) { 1207 netdev_dbg(dev, "convert buff to frame failed for xdp\n"); 1208 return XDP_DROP; 1209 } 1210 1211 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0); 1212 if (unlikely(!err)) { 1213 xdp_return_frame_rx_napi(xdpf); 1214 } else if (unlikely(err < 0)) { 1215 trace_xdp_exception(dev, xdp_prog, act); 1216 return XDP_DROP; 1217 } 1218 *xdp_xmit |= VIRTIO_XDP_TX; 1219 return act; 1220 1221 case XDP_REDIRECT: 1222 u64_stats_inc(&stats->xdp_redirects); 1223 err = xdp_do_redirect(dev, xdp, xdp_prog); 1224 if (err) 1225 return XDP_DROP; 1226 1227 *xdp_xmit |= VIRTIO_XDP_REDIR; 1228 return act; 1229 1230 default: 1231 bpf_warn_invalid_xdp_action(dev, xdp_prog, act); 1232 fallthrough; 1233 case XDP_ABORTED: 1234 trace_xdp_exception(dev, xdp_prog, act); 1235 fallthrough; 1236 case XDP_DROP: 1237 return XDP_DROP; 1238 } 1239 } 1240 1241 static unsigned int virtnet_get_headroom(struct virtnet_info *vi) 1242 { 1243 return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0; 1244 } 1245 1246 /* We copy the packet for XDP in the following cases: 1247 * 1248 * 1) Packet is scattered across multiple rx buffers. 1249 * 2) Headroom space is insufficient. 1250 * 1251 * This is inefficient but it's a temporary condition that 1252 * we hit right after XDP is enabled and until queue is refilled 1253 * with large buffers with sufficient headroom - so it should affect 1254 * at most queue size packets. 1255 * Afterwards, the conditions to enable 1256 * XDP should preclude the underlying device from sending packets 1257 * across multiple buffers (num_buf > 1), and we make sure buffers 1258 * have enough headroom. 1259 */ 1260 static struct page *xdp_linearize_page(struct receive_queue *rq, 1261 int *num_buf, 1262 struct page *p, 1263 int offset, 1264 int page_off, 1265 unsigned int *len) 1266 { 1267 int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 1268 struct page *page; 1269 1270 if (page_off + *len + tailroom > PAGE_SIZE) 1271 return NULL; 1272 1273 page = alloc_page(GFP_ATOMIC); 1274 if (!page) 1275 return NULL; 1276 1277 memcpy(page_address(page) + page_off, page_address(p) + offset, *len); 1278 page_off += *len; 1279 1280 while (--*num_buf) { 1281 unsigned int buflen; 1282 void *buf; 1283 int off; 1284 1285 buf = virtnet_rq_get_buf(rq, &buflen, NULL); 1286 if (unlikely(!buf)) 1287 goto err_buf; 1288 1289 p = virt_to_head_page(buf); 1290 off = buf - page_address(p); 1291 1292 /* guard against a misconfigured or uncooperative backend that 1293 * is sending packet larger than the MTU. 1294 */ 1295 if ((page_off + buflen + tailroom) > PAGE_SIZE) { 1296 put_page(p); 1297 goto err_buf; 1298 } 1299 1300 memcpy(page_address(page) + page_off, 1301 page_address(p) + off, buflen); 1302 page_off += buflen; 1303 put_page(p); 1304 } 1305 1306 /* Headroom does not contribute to packet length */ 1307 *len = page_off - VIRTIO_XDP_HEADROOM; 1308 return page; 1309 err_buf: 1310 __free_pages(page, 0); 1311 return NULL; 1312 } 1313 1314 static struct sk_buff *receive_small_build_skb(struct virtnet_info *vi, 1315 unsigned int xdp_headroom, 1316 void *buf, 1317 unsigned int len) 1318 { 1319 unsigned int header_offset; 1320 unsigned int headroom; 1321 unsigned int buflen; 1322 struct sk_buff *skb; 1323 1324 header_offset = VIRTNET_RX_PAD + xdp_headroom; 1325 headroom = vi->hdr_len + header_offset; 1326 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) + 1327 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 1328 1329 skb = virtnet_build_skb(buf, buflen, headroom, len); 1330 if (unlikely(!skb)) 1331 return NULL; 1332 1333 buf += header_offset; 1334 memcpy(skb_vnet_common_hdr(skb), buf, vi->hdr_len); 1335 1336 return skb; 1337 } 1338 1339 static struct sk_buff *receive_small_xdp(struct net_device *dev, 1340 struct virtnet_info *vi, 1341 struct receive_queue *rq, 1342 struct bpf_prog *xdp_prog, 1343 void *buf, 1344 unsigned int xdp_headroom, 1345 unsigned int len, 1346 unsigned int *xdp_xmit, 1347 struct virtnet_rq_stats *stats) 1348 { 1349 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom; 1350 unsigned int headroom = vi->hdr_len + header_offset; 1351 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset; 1352 struct page *page = virt_to_head_page(buf); 1353 struct page *xdp_page; 1354 unsigned int buflen; 1355 struct xdp_buff xdp; 1356 struct sk_buff *skb; 1357 unsigned int metasize = 0; 1358 u32 act; 1359 1360 if (unlikely(hdr->hdr.gso_type)) 1361 goto err_xdp; 1362 1363 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) + 1364 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 1365 1366 if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) { 1367 int offset = buf - page_address(page) + header_offset; 1368 unsigned int tlen = len + vi->hdr_len; 1369 int num_buf = 1; 1370 1371 xdp_headroom = virtnet_get_headroom(vi); 1372 header_offset = VIRTNET_RX_PAD + xdp_headroom; 1373 headroom = vi->hdr_len + header_offset; 1374 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) + 1375 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 1376 xdp_page = xdp_linearize_page(rq, &num_buf, page, 1377 offset, header_offset, 1378 &tlen); 1379 if (!xdp_page) 1380 goto err_xdp; 1381 1382 buf = page_address(xdp_page); 1383 put_page(page); 1384 page = xdp_page; 1385 } 1386 1387 xdp_init_buff(&xdp, buflen, &rq->xdp_rxq); 1388 xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len, 1389 xdp_headroom, len, true); 1390 1391 act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats); 1392 1393 switch (act) { 1394 case XDP_PASS: 1395 /* Recalculate length in case bpf program changed it */ 1396 len = xdp.data_end - xdp.data; 1397 metasize = xdp.data - xdp.data_meta; 1398 break; 1399 1400 case XDP_TX: 1401 case XDP_REDIRECT: 1402 goto xdp_xmit; 1403 1404 default: 1405 goto err_xdp; 1406 } 1407 1408 skb = virtnet_build_skb(buf, buflen, xdp.data - buf, len); 1409 if (unlikely(!skb)) 1410 goto err; 1411 1412 if (metasize) 1413 skb_metadata_set(skb, metasize); 1414 1415 return skb; 1416 1417 err_xdp: 1418 u64_stats_inc(&stats->xdp_drops); 1419 err: 1420 u64_stats_inc(&stats->drops); 1421 put_page(page); 1422 xdp_xmit: 1423 return NULL; 1424 } 1425 1426 static struct sk_buff *receive_small(struct net_device *dev, 1427 struct virtnet_info *vi, 1428 struct receive_queue *rq, 1429 void *buf, void *ctx, 1430 unsigned int len, 1431 unsigned int *xdp_xmit, 1432 struct virtnet_rq_stats *stats) 1433 { 1434 unsigned int xdp_headroom = (unsigned long)ctx; 1435 struct page *page = virt_to_head_page(buf); 1436 struct sk_buff *skb; 1437 1438 len -= vi->hdr_len; 1439 u64_stats_add(&stats->bytes, len); 1440 1441 if (unlikely(len > GOOD_PACKET_LEN)) { 1442 pr_debug("%s: rx error: len %u exceeds max size %d\n", 1443 dev->name, len, GOOD_PACKET_LEN); 1444 DEV_STATS_INC(dev, rx_length_errors); 1445 goto err; 1446 } 1447 1448 if (unlikely(vi->xdp_enabled)) { 1449 struct bpf_prog *xdp_prog; 1450 1451 rcu_read_lock(); 1452 xdp_prog = rcu_dereference(rq->xdp_prog); 1453 if (xdp_prog) { 1454 skb = receive_small_xdp(dev, vi, rq, xdp_prog, buf, 1455 xdp_headroom, len, xdp_xmit, 1456 stats); 1457 rcu_read_unlock(); 1458 return skb; 1459 } 1460 rcu_read_unlock(); 1461 } 1462 1463 skb = receive_small_build_skb(vi, xdp_headroom, buf, len); 1464 if (likely(skb)) 1465 return skb; 1466 1467 err: 1468 u64_stats_inc(&stats->drops); 1469 put_page(page); 1470 return NULL; 1471 } 1472 1473 static struct sk_buff *receive_big(struct net_device *dev, 1474 struct virtnet_info *vi, 1475 struct receive_queue *rq, 1476 void *buf, 1477 unsigned int len, 1478 struct virtnet_rq_stats *stats) 1479 { 1480 struct page *page = buf; 1481 struct sk_buff *skb = 1482 page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, 0); 1483 1484 u64_stats_add(&stats->bytes, len - vi->hdr_len); 1485 if (unlikely(!skb)) 1486 goto err; 1487 1488 return skb; 1489 1490 err: 1491 u64_stats_inc(&stats->drops); 1492 give_pages(rq, page); 1493 return NULL; 1494 } 1495 1496 static void mergeable_buf_free(struct receive_queue *rq, int num_buf, 1497 struct net_device *dev, 1498 struct virtnet_rq_stats *stats) 1499 { 1500 struct page *page; 1501 void *buf; 1502 int len; 1503 1504 while (num_buf-- > 1) { 1505 buf = virtnet_rq_get_buf(rq, &len, NULL); 1506 if (unlikely(!buf)) { 1507 pr_debug("%s: rx error: %d buffers missing\n", 1508 dev->name, num_buf); 1509 DEV_STATS_INC(dev, rx_length_errors); 1510 break; 1511 } 1512 u64_stats_add(&stats->bytes, len); 1513 page = virt_to_head_page(buf); 1514 put_page(page); 1515 } 1516 } 1517 1518 /* Why not use xdp_build_skb_from_frame() ? 1519 * XDP core assumes that xdp frags are PAGE_SIZE in length, while in 1520 * virtio-net there are 2 points that do not match its requirements: 1521 * 1. The size of the prefilled buffer is not fixed before xdp is set. 1522 * 2. xdp_build_skb_from_frame() does more checks that we don't need, 1523 * like eth_type_trans() (which virtio-net does in receive_buf()). 1524 */ 1525 static struct sk_buff *build_skb_from_xdp_buff(struct net_device *dev, 1526 struct virtnet_info *vi, 1527 struct xdp_buff *xdp, 1528 unsigned int xdp_frags_truesz) 1529 { 1530 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp); 1531 unsigned int headroom, data_len; 1532 struct sk_buff *skb; 1533 int metasize; 1534 u8 nr_frags; 1535 1536 if (unlikely(xdp->data_end > xdp_data_hard_end(xdp))) { 1537 pr_debug("Error building skb as missing reserved tailroom for xdp"); 1538 return NULL; 1539 } 1540 1541 if (unlikely(xdp_buff_has_frags(xdp))) 1542 nr_frags = sinfo->nr_frags; 1543 1544 skb = build_skb(xdp->data_hard_start, xdp->frame_sz); 1545 if (unlikely(!skb)) 1546 return NULL; 1547 1548 headroom = xdp->data - xdp->data_hard_start; 1549 data_len = xdp->data_end - xdp->data; 1550 skb_reserve(skb, headroom); 1551 __skb_put(skb, data_len); 1552 1553 metasize = xdp->data - xdp->data_meta; 1554 metasize = metasize > 0 ? metasize : 0; 1555 if (metasize) 1556 skb_metadata_set(skb, metasize); 1557 1558 if (unlikely(xdp_buff_has_frags(xdp))) 1559 xdp_update_skb_shared_info(skb, nr_frags, 1560 sinfo->xdp_frags_size, 1561 xdp_frags_truesz, 1562 xdp_buff_is_frag_pfmemalloc(xdp)); 1563 1564 return skb; 1565 } 1566 1567 /* TODO: build xdp in big mode */ 1568 static int virtnet_build_xdp_buff_mrg(struct net_device *dev, 1569 struct virtnet_info *vi, 1570 struct receive_queue *rq, 1571 struct xdp_buff *xdp, 1572 void *buf, 1573 unsigned int len, 1574 unsigned int frame_sz, 1575 int *num_buf, 1576 unsigned int *xdp_frags_truesize, 1577 struct virtnet_rq_stats *stats) 1578 { 1579 struct virtio_net_hdr_mrg_rxbuf *hdr = buf; 1580 unsigned int headroom, tailroom, room; 1581 unsigned int truesize, cur_frag_size; 1582 struct skb_shared_info *shinfo; 1583 unsigned int xdp_frags_truesz = 0; 1584 struct page *page; 1585 skb_frag_t *frag; 1586 int offset; 1587 void *ctx; 1588 1589 xdp_init_buff(xdp, frame_sz, &rq->xdp_rxq); 1590 xdp_prepare_buff(xdp, buf - VIRTIO_XDP_HEADROOM, 1591 VIRTIO_XDP_HEADROOM + vi->hdr_len, len - vi->hdr_len, true); 1592 1593 if (!*num_buf) 1594 return 0; 1595 1596 if (*num_buf > 1) { 1597 /* If we want to build multi-buffer xdp, we need 1598 * to specify that the flags of xdp_buff have the 1599 * XDP_FLAGS_HAS_FRAG bit. 1600 */ 1601 if (!xdp_buff_has_frags(xdp)) 1602 xdp_buff_set_frags_flag(xdp); 1603 1604 shinfo = xdp_get_shared_info_from_buff(xdp); 1605 shinfo->nr_frags = 0; 1606 shinfo->xdp_frags_size = 0; 1607 } 1608 1609 if (*num_buf > MAX_SKB_FRAGS + 1) 1610 return -EINVAL; 1611 1612 while (--*num_buf > 0) { 1613 buf = virtnet_rq_get_buf(rq, &len, &ctx); 1614 if (unlikely(!buf)) { 1615 pr_debug("%s: rx error: %d buffers out of %d missing\n", 1616 dev->name, *num_buf, 1617 virtio16_to_cpu(vi->vdev, hdr->num_buffers)); 1618 DEV_STATS_INC(dev, rx_length_errors); 1619 goto err; 1620 } 1621 1622 u64_stats_add(&stats->bytes, len); 1623 page = virt_to_head_page(buf); 1624 offset = buf - page_address(page); 1625 1626 truesize = mergeable_ctx_to_truesize(ctx); 1627 headroom = mergeable_ctx_to_headroom(ctx); 1628 tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 1629 room = SKB_DATA_ALIGN(headroom + tailroom); 1630 1631 cur_frag_size = truesize; 1632 xdp_frags_truesz += cur_frag_size; 1633 if (unlikely(len > truesize - room || cur_frag_size > PAGE_SIZE)) { 1634 put_page(page); 1635 pr_debug("%s: rx error: len %u exceeds truesize %lu\n", 1636 dev->name, len, (unsigned long)(truesize - room)); 1637 DEV_STATS_INC(dev, rx_length_errors); 1638 goto err; 1639 } 1640 1641 frag = &shinfo->frags[shinfo->nr_frags++]; 1642 skb_frag_fill_page_desc(frag, page, offset, len); 1643 if (page_is_pfmemalloc(page)) 1644 xdp_buff_set_frag_pfmemalloc(xdp); 1645 1646 shinfo->xdp_frags_size += len; 1647 } 1648 1649 *xdp_frags_truesize = xdp_frags_truesz; 1650 return 0; 1651 1652 err: 1653 put_xdp_frags(xdp); 1654 return -EINVAL; 1655 } 1656 1657 static void *mergeable_xdp_get_buf(struct virtnet_info *vi, 1658 struct receive_queue *rq, 1659 struct bpf_prog *xdp_prog, 1660 void *ctx, 1661 unsigned int *frame_sz, 1662 int *num_buf, 1663 struct page **page, 1664 int offset, 1665 unsigned int *len, 1666 struct virtio_net_hdr_mrg_rxbuf *hdr) 1667 { 1668 unsigned int truesize = mergeable_ctx_to_truesize(ctx); 1669 unsigned int headroom = mergeable_ctx_to_headroom(ctx); 1670 struct page *xdp_page; 1671 unsigned int xdp_room; 1672 1673 /* Transient failure which in theory could occur if 1674 * in-flight packets from before XDP was enabled reach 1675 * the receive path after XDP is loaded. 1676 */ 1677 if (unlikely(hdr->hdr.gso_type)) 1678 return NULL; 1679 1680 /* Now XDP core assumes frag size is PAGE_SIZE, but buffers 1681 * with headroom may add hole in truesize, which 1682 * make their length exceed PAGE_SIZE. So we disabled the 1683 * hole mechanism for xdp. See add_recvbuf_mergeable(). 1684 */ 1685 *frame_sz = truesize; 1686 1687 if (likely(headroom >= virtnet_get_headroom(vi) && 1688 (*num_buf == 1 || xdp_prog->aux->xdp_has_frags))) { 1689 return page_address(*page) + offset; 1690 } 1691 1692 /* This happens when headroom is not enough because 1693 * of the buffer was prefilled before XDP is set. 1694 * This should only happen for the first several packets. 1695 * In fact, vq reset can be used here to help us clean up 1696 * the prefilled buffers, but many existing devices do not 1697 * support it, and we don't want to bother users who are 1698 * using xdp normally. 1699 */ 1700 if (!xdp_prog->aux->xdp_has_frags) { 1701 /* linearize data for XDP */ 1702 xdp_page = xdp_linearize_page(rq, num_buf, 1703 *page, offset, 1704 VIRTIO_XDP_HEADROOM, 1705 len); 1706 if (!xdp_page) 1707 return NULL; 1708 } else { 1709 xdp_room = SKB_DATA_ALIGN(VIRTIO_XDP_HEADROOM + 1710 sizeof(struct skb_shared_info)); 1711 if (*len + xdp_room > PAGE_SIZE) 1712 return NULL; 1713 1714 xdp_page = alloc_page(GFP_ATOMIC); 1715 if (!xdp_page) 1716 return NULL; 1717 1718 memcpy(page_address(xdp_page) + VIRTIO_XDP_HEADROOM, 1719 page_address(*page) + offset, *len); 1720 } 1721 1722 *frame_sz = PAGE_SIZE; 1723 1724 put_page(*page); 1725 1726 *page = xdp_page; 1727 1728 return page_address(*page) + VIRTIO_XDP_HEADROOM; 1729 } 1730 1731 static struct sk_buff *receive_mergeable_xdp(struct net_device *dev, 1732 struct virtnet_info *vi, 1733 struct receive_queue *rq, 1734 struct bpf_prog *xdp_prog, 1735 void *buf, 1736 void *ctx, 1737 unsigned int len, 1738 unsigned int *xdp_xmit, 1739 struct virtnet_rq_stats *stats) 1740 { 1741 struct virtio_net_hdr_mrg_rxbuf *hdr = buf; 1742 int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers); 1743 struct page *page = virt_to_head_page(buf); 1744 int offset = buf - page_address(page); 1745 unsigned int xdp_frags_truesz = 0; 1746 struct sk_buff *head_skb; 1747 unsigned int frame_sz; 1748 struct xdp_buff xdp; 1749 void *data; 1750 u32 act; 1751 int err; 1752 1753 data = mergeable_xdp_get_buf(vi, rq, xdp_prog, ctx, &frame_sz, &num_buf, &page, 1754 offset, &len, hdr); 1755 if (unlikely(!data)) 1756 goto err_xdp; 1757 1758 err = virtnet_build_xdp_buff_mrg(dev, vi, rq, &xdp, data, len, frame_sz, 1759 &num_buf, &xdp_frags_truesz, stats); 1760 if (unlikely(err)) 1761 goto err_xdp; 1762 1763 act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats); 1764 1765 switch (act) { 1766 case XDP_PASS: 1767 head_skb = build_skb_from_xdp_buff(dev, vi, &xdp, xdp_frags_truesz); 1768 if (unlikely(!head_skb)) 1769 break; 1770 return head_skb; 1771 1772 case XDP_TX: 1773 case XDP_REDIRECT: 1774 return NULL; 1775 1776 default: 1777 break; 1778 } 1779 1780 put_xdp_frags(&xdp); 1781 1782 err_xdp: 1783 put_page(page); 1784 mergeable_buf_free(rq, num_buf, dev, stats); 1785 1786 u64_stats_inc(&stats->xdp_drops); 1787 u64_stats_inc(&stats->drops); 1788 return NULL; 1789 } 1790 1791 static struct sk_buff *receive_mergeable(struct net_device *dev, 1792 struct virtnet_info *vi, 1793 struct receive_queue *rq, 1794 void *buf, 1795 void *ctx, 1796 unsigned int len, 1797 unsigned int *xdp_xmit, 1798 struct virtnet_rq_stats *stats) 1799 { 1800 struct virtio_net_hdr_mrg_rxbuf *hdr = buf; 1801 int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers); 1802 struct page *page = virt_to_head_page(buf); 1803 int offset = buf - page_address(page); 1804 struct sk_buff *head_skb, *curr_skb; 1805 unsigned int truesize = mergeable_ctx_to_truesize(ctx); 1806 unsigned int headroom = mergeable_ctx_to_headroom(ctx); 1807 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 1808 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom); 1809 1810 head_skb = NULL; 1811 u64_stats_add(&stats->bytes, len - vi->hdr_len); 1812 1813 if (unlikely(len > truesize - room)) { 1814 pr_debug("%s: rx error: len %u exceeds truesize %lu\n", 1815 dev->name, len, (unsigned long)(truesize - room)); 1816 DEV_STATS_INC(dev, rx_length_errors); 1817 goto err_skb; 1818 } 1819 1820 if (unlikely(vi->xdp_enabled)) { 1821 struct bpf_prog *xdp_prog; 1822 1823 rcu_read_lock(); 1824 xdp_prog = rcu_dereference(rq->xdp_prog); 1825 if (xdp_prog) { 1826 head_skb = receive_mergeable_xdp(dev, vi, rq, xdp_prog, buf, ctx, 1827 len, xdp_xmit, stats); 1828 rcu_read_unlock(); 1829 return head_skb; 1830 } 1831 rcu_read_unlock(); 1832 } 1833 1834 head_skb = page_to_skb(vi, rq, page, offset, len, truesize, headroom); 1835 curr_skb = head_skb; 1836 1837 if (unlikely(!curr_skb)) 1838 goto err_skb; 1839 while (--num_buf) { 1840 int num_skb_frags; 1841 1842 buf = virtnet_rq_get_buf(rq, &len, &ctx); 1843 if (unlikely(!buf)) { 1844 pr_debug("%s: rx error: %d buffers out of %d missing\n", 1845 dev->name, num_buf, 1846 virtio16_to_cpu(vi->vdev, 1847 hdr->num_buffers)); 1848 DEV_STATS_INC(dev, rx_length_errors); 1849 goto err_buf; 1850 } 1851 1852 u64_stats_add(&stats->bytes, len); 1853 page = virt_to_head_page(buf); 1854 1855 truesize = mergeable_ctx_to_truesize(ctx); 1856 headroom = mergeable_ctx_to_headroom(ctx); 1857 tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 1858 room = SKB_DATA_ALIGN(headroom + tailroom); 1859 if (unlikely(len > truesize - room)) { 1860 pr_debug("%s: rx error: len %u exceeds truesize %lu\n", 1861 dev->name, len, (unsigned long)(truesize - room)); 1862 DEV_STATS_INC(dev, rx_length_errors); 1863 goto err_skb; 1864 } 1865 1866 num_skb_frags = skb_shinfo(curr_skb)->nr_frags; 1867 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) { 1868 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC); 1869 1870 if (unlikely(!nskb)) 1871 goto err_skb; 1872 if (curr_skb == head_skb) 1873 skb_shinfo(curr_skb)->frag_list = nskb; 1874 else 1875 curr_skb->next = nskb; 1876 curr_skb = nskb; 1877 head_skb->truesize += nskb->truesize; 1878 num_skb_frags = 0; 1879 } 1880 if (curr_skb != head_skb) { 1881 head_skb->data_len += len; 1882 head_skb->len += len; 1883 head_skb->truesize += truesize; 1884 } 1885 offset = buf - page_address(page); 1886 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) { 1887 put_page(page); 1888 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1, 1889 len, truesize); 1890 } else { 1891 skb_add_rx_frag(curr_skb, num_skb_frags, page, 1892 offset, len, truesize); 1893 } 1894 } 1895 1896 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len); 1897 return head_skb; 1898 1899 err_skb: 1900 put_page(page); 1901 mergeable_buf_free(rq, num_buf, dev, stats); 1902 1903 err_buf: 1904 u64_stats_inc(&stats->drops); 1905 dev_kfree_skb(head_skb); 1906 return NULL; 1907 } 1908 1909 static void virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash *hdr_hash, 1910 struct sk_buff *skb) 1911 { 1912 enum pkt_hash_types rss_hash_type; 1913 1914 if (!hdr_hash || !skb) 1915 return; 1916 1917 switch (__le16_to_cpu(hdr_hash->hash_report)) { 1918 case VIRTIO_NET_HASH_REPORT_TCPv4: 1919 case VIRTIO_NET_HASH_REPORT_UDPv4: 1920 case VIRTIO_NET_HASH_REPORT_TCPv6: 1921 case VIRTIO_NET_HASH_REPORT_UDPv6: 1922 case VIRTIO_NET_HASH_REPORT_TCPv6_EX: 1923 case VIRTIO_NET_HASH_REPORT_UDPv6_EX: 1924 rss_hash_type = PKT_HASH_TYPE_L4; 1925 break; 1926 case VIRTIO_NET_HASH_REPORT_IPv4: 1927 case VIRTIO_NET_HASH_REPORT_IPv6: 1928 case VIRTIO_NET_HASH_REPORT_IPv6_EX: 1929 rss_hash_type = PKT_HASH_TYPE_L3; 1930 break; 1931 case VIRTIO_NET_HASH_REPORT_NONE: 1932 default: 1933 rss_hash_type = PKT_HASH_TYPE_NONE; 1934 } 1935 skb_set_hash(skb, __le32_to_cpu(hdr_hash->hash_value), rss_hash_type); 1936 } 1937 1938 static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq, 1939 void *buf, unsigned int len, void **ctx, 1940 unsigned int *xdp_xmit, 1941 struct virtnet_rq_stats *stats) 1942 { 1943 struct net_device *dev = vi->dev; 1944 struct sk_buff *skb; 1945 struct virtio_net_common_hdr *hdr; 1946 1947 if (unlikely(len < vi->hdr_len + ETH_HLEN)) { 1948 pr_debug("%s: short packet %i\n", dev->name, len); 1949 DEV_STATS_INC(dev, rx_length_errors); 1950 virtnet_rq_free_buf(vi, rq, buf); 1951 return; 1952 } 1953 1954 if (vi->mergeable_rx_bufs) 1955 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit, 1956 stats); 1957 else if (vi->big_packets) 1958 skb = receive_big(dev, vi, rq, buf, len, stats); 1959 else 1960 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats); 1961 1962 if (unlikely(!skb)) 1963 return; 1964 1965 hdr = skb_vnet_common_hdr(skb); 1966 if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report) 1967 virtio_skb_set_hash(&hdr->hash_v1_hdr, skb); 1968 1969 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) 1970 skb->ip_summed = CHECKSUM_UNNECESSARY; 1971 1972 if (virtio_net_hdr_to_skb(skb, &hdr->hdr, 1973 virtio_is_little_endian(vi->vdev))) { 1974 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n", 1975 dev->name, hdr->hdr.gso_type, 1976 hdr->hdr.gso_size); 1977 goto frame_err; 1978 } 1979 1980 skb_record_rx_queue(skb, vq2rxq(rq->vq)); 1981 skb->protocol = eth_type_trans(skb, dev); 1982 pr_debug("Receiving skb proto 0x%04x len %i type %i\n", 1983 ntohs(skb->protocol), skb->len, skb->pkt_type); 1984 1985 napi_gro_receive(&rq->napi, skb); 1986 return; 1987 1988 frame_err: 1989 DEV_STATS_INC(dev, rx_frame_errors); 1990 dev_kfree_skb(skb); 1991 } 1992 1993 /* Unlike mergeable buffers, all buffers are allocated to the 1994 * same size, except for the headroom. For this reason we do 1995 * not need to use mergeable_len_to_ctx here - it is enough 1996 * to store the headroom as the context ignoring the truesize. 1997 */ 1998 static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq, 1999 gfp_t gfp) 2000 { 2001 char *buf; 2002 unsigned int xdp_headroom = virtnet_get_headroom(vi); 2003 void *ctx = (void *)(unsigned long)xdp_headroom; 2004 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom; 2005 int err; 2006 2007 len = SKB_DATA_ALIGN(len) + 2008 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 2009 2010 buf = virtnet_rq_alloc(rq, len, gfp); 2011 if (unlikely(!buf)) 2012 return -ENOMEM; 2013 2014 virtnet_rq_init_one_sg(rq, buf + VIRTNET_RX_PAD + xdp_headroom, 2015 vi->hdr_len + GOOD_PACKET_LEN); 2016 2017 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp); 2018 if (err < 0) { 2019 virtnet_rq_unmap(rq, buf, 0); 2020 put_page(virt_to_head_page(buf)); 2021 } 2022 2023 return err; 2024 } 2025 2026 static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq, 2027 gfp_t gfp) 2028 { 2029 struct page *first, *list = NULL; 2030 char *p; 2031 int i, err, offset; 2032 2033 sg_init_table(rq->sg, vi->big_packets_num_skbfrags + 2); 2034 2035 /* page in rq->sg[vi->big_packets_num_skbfrags + 1] is list tail */ 2036 for (i = vi->big_packets_num_skbfrags + 1; i > 1; --i) { 2037 first = get_a_page(rq, gfp); 2038 if (!first) { 2039 if (list) 2040 give_pages(rq, list); 2041 return -ENOMEM; 2042 } 2043 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE); 2044 2045 /* chain new page in list head to match sg */ 2046 first->private = (unsigned long)list; 2047 list = first; 2048 } 2049 2050 first = get_a_page(rq, gfp); 2051 if (!first) { 2052 give_pages(rq, list); 2053 return -ENOMEM; 2054 } 2055 p = page_address(first); 2056 2057 /* rq->sg[0], rq->sg[1] share the same page */ 2058 /* a separated rq->sg[0] for header - required in case !any_header_sg */ 2059 sg_set_buf(&rq->sg[0], p, vi->hdr_len); 2060 2061 /* rq->sg[1] for data packet, from offset */ 2062 offset = sizeof(struct padded_vnet_hdr); 2063 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset); 2064 2065 /* chain first in list head */ 2066 first->private = (unsigned long)list; 2067 err = virtqueue_add_inbuf(rq->vq, rq->sg, vi->big_packets_num_skbfrags + 2, 2068 first, gfp); 2069 if (err < 0) 2070 give_pages(rq, first); 2071 2072 return err; 2073 } 2074 2075 static unsigned int get_mergeable_buf_len(struct receive_queue *rq, 2076 struct ewma_pkt_len *avg_pkt_len, 2077 unsigned int room) 2078 { 2079 struct virtnet_info *vi = rq->vq->vdev->priv; 2080 const size_t hdr_len = vi->hdr_len; 2081 unsigned int len; 2082 2083 if (room) 2084 return PAGE_SIZE - room; 2085 2086 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len), 2087 rq->min_buf_len, PAGE_SIZE - hdr_len); 2088 2089 return ALIGN(len, L1_CACHE_BYTES); 2090 } 2091 2092 static int add_recvbuf_mergeable(struct virtnet_info *vi, 2093 struct receive_queue *rq, gfp_t gfp) 2094 { 2095 struct page_frag *alloc_frag = &rq->alloc_frag; 2096 unsigned int headroom = virtnet_get_headroom(vi); 2097 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 2098 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom); 2099 unsigned int len, hole; 2100 void *ctx; 2101 char *buf; 2102 int err; 2103 2104 /* Extra tailroom is needed to satisfy XDP's assumption. This 2105 * means rx frags coalescing won't work, but consider we've 2106 * disabled GSO for XDP, it won't be a big issue. 2107 */ 2108 len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room); 2109 2110 buf = virtnet_rq_alloc(rq, len + room, gfp); 2111 if (unlikely(!buf)) 2112 return -ENOMEM; 2113 2114 buf += headroom; /* advance address leaving hole at front of pkt */ 2115 hole = alloc_frag->size - alloc_frag->offset; 2116 if (hole < len + room) { 2117 /* To avoid internal fragmentation, if there is very likely not 2118 * enough space for another buffer, add the remaining space to 2119 * the current buffer. 2120 * XDP core assumes that frame_size of xdp_buff and the length 2121 * of the frag are PAGE_SIZE, so we disable the hole mechanism. 2122 */ 2123 if (!headroom) 2124 len += hole; 2125 alloc_frag->offset += hole; 2126 } 2127 2128 virtnet_rq_init_one_sg(rq, buf, len); 2129 2130 ctx = mergeable_len_to_ctx(len + room, headroom); 2131 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp); 2132 if (err < 0) { 2133 virtnet_rq_unmap(rq, buf, 0); 2134 put_page(virt_to_head_page(buf)); 2135 } 2136 2137 return err; 2138 } 2139 2140 /* 2141 * Returns false if we couldn't fill entirely (OOM). 2142 * 2143 * Normally run in the receive path, but can also be run from ndo_open 2144 * before we're receiving packets, or from refill_work which is 2145 * careful to disable receiving (using napi_disable). 2146 */ 2147 static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq, 2148 gfp_t gfp) 2149 { 2150 int err; 2151 bool oom; 2152 2153 do { 2154 if (vi->mergeable_rx_bufs) 2155 err = add_recvbuf_mergeable(vi, rq, gfp); 2156 else if (vi->big_packets) 2157 err = add_recvbuf_big(vi, rq, gfp); 2158 else 2159 err = add_recvbuf_small(vi, rq, gfp); 2160 2161 oom = err == -ENOMEM; 2162 if (err) 2163 break; 2164 } while (rq->vq->num_free); 2165 if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) { 2166 unsigned long flags; 2167 2168 flags = u64_stats_update_begin_irqsave(&rq->stats.syncp); 2169 u64_stats_inc(&rq->stats.kicks); 2170 u64_stats_update_end_irqrestore(&rq->stats.syncp, flags); 2171 } 2172 2173 return !oom; 2174 } 2175 2176 static void skb_recv_done(struct virtqueue *rvq) 2177 { 2178 struct virtnet_info *vi = rvq->vdev->priv; 2179 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)]; 2180 2181 rq->calls++; 2182 virtqueue_napi_schedule(&rq->napi, rvq); 2183 } 2184 2185 static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi) 2186 { 2187 napi_enable(napi); 2188 2189 /* If all buffers were filled by other side before we napi_enabled, we 2190 * won't get another interrupt, so process any outstanding packets now. 2191 * Call local_bh_enable after to trigger softIRQ processing. 2192 */ 2193 local_bh_disable(); 2194 virtqueue_napi_schedule(napi, vq); 2195 local_bh_enable(); 2196 } 2197 2198 static void virtnet_napi_tx_enable(struct virtnet_info *vi, 2199 struct virtqueue *vq, 2200 struct napi_struct *napi) 2201 { 2202 if (!napi->weight) 2203 return; 2204 2205 /* Tx napi touches cachelines on the cpu handling tx interrupts. Only 2206 * enable the feature if this is likely affine with the transmit path. 2207 */ 2208 if (!vi->affinity_hint_set) { 2209 napi->weight = 0; 2210 return; 2211 } 2212 2213 return virtnet_napi_enable(vq, napi); 2214 } 2215 2216 static void virtnet_napi_tx_disable(struct napi_struct *napi) 2217 { 2218 if (napi->weight) 2219 napi_disable(napi); 2220 } 2221 2222 static void refill_work(struct work_struct *work) 2223 { 2224 struct virtnet_info *vi = 2225 container_of(work, struct virtnet_info, refill.work); 2226 bool still_empty; 2227 int i; 2228 2229 for (i = 0; i < vi->curr_queue_pairs; i++) { 2230 struct receive_queue *rq = &vi->rq[i]; 2231 2232 napi_disable(&rq->napi); 2233 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL); 2234 virtnet_napi_enable(rq->vq, &rq->napi); 2235 2236 /* In theory, this can happen: if we don't get any buffers in 2237 * we will *never* try to fill again. 2238 */ 2239 if (still_empty) 2240 schedule_delayed_work(&vi->refill, HZ/2); 2241 } 2242 } 2243 2244 static int virtnet_receive(struct receive_queue *rq, int budget, 2245 unsigned int *xdp_xmit) 2246 { 2247 struct virtnet_info *vi = rq->vq->vdev->priv; 2248 struct virtnet_rq_stats stats = {}; 2249 unsigned int len; 2250 int packets = 0; 2251 void *buf; 2252 int i; 2253 2254 if (!vi->big_packets || vi->mergeable_rx_bufs) { 2255 void *ctx; 2256 2257 while (packets < budget && 2258 (buf = virtnet_rq_get_buf(rq, &len, &ctx))) { 2259 receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats); 2260 packets++; 2261 } 2262 } else { 2263 while (packets < budget && 2264 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) { 2265 receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats); 2266 packets++; 2267 } 2268 } 2269 2270 if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) { 2271 if (!try_fill_recv(vi, rq, GFP_ATOMIC)) { 2272 spin_lock(&vi->refill_lock); 2273 if (vi->refill_enabled) 2274 schedule_delayed_work(&vi->refill, 0); 2275 spin_unlock(&vi->refill_lock); 2276 } 2277 } 2278 2279 u64_stats_set(&stats.packets, packets); 2280 u64_stats_update_begin(&rq->stats.syncp); 2281 for (i = 0; i < ARRAY_SIZE(virtnet_rq_stats_desc); i++) { 2282 size_t offset = virtnet_rq_stats_desc[i].offset; 2283 u64_stats_t *item, *src; 2284 2285 item = (u64_stats_t *)((u8 *)&rq->stats + offset); 2286 src = (u64_stats_t *)((u8 *)&stats + offset); 2287 u64_stats_add(item, u64_stats_read(src)); 2288 } 2289 2290 u64_stats_add(&rq->stats.packets, u64_stats_read(&stats.packets)); 2291 u64_stats_add(&rq->stats.bytes, u64_stats_read(&stats.bytes)); 2292 2293 u64_stats_update_end(&rq->stats.syncp); 2294 2295 return packets; 2296 } 2297 2298 static void virtnet_poll_cleantx(struct receive_queue *rq) 2299 { 2300 struct virtnet_info *vi = rq->vq->vdev->priv; 2301 unsigned int index = vq2rxq(rq->vq); 2302 struct send_queue *sq = &vi->sq[index]; 2303 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index); 2304 2305 if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index)) 2306 return; 2307 2308 if (__netif_tx_trylock(txq)) { 2309 if (sq->reset) { 2310 __netif_tx_unlock(txq); 2311 return; 2312 } 2313 2314 do { 2315 virtqueue_disable_cb(sq->vq); 2316 free_old_xmit(sq, true); 2317 } while (unlikely(!virtqueue_enable_cb_delayed(sq->vq))); 2318 2319 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) { 2320 if (netif_tx_queue_stopped(txq)) { 2321 u64_stats_update_begin(&sq->stats.syncp); 2322 u64_stats_inc(&sq->stats.wake); 2323 u64_stats_update_end(&sq->stats.syncp); 2324 } 2325 netif_tx_wake_queue(txq); 2326 } 2327 2328 __netif_tx_unlock(txq); 2329 } 2330 } 2331 2332 static void virtnet_rx_dim_update(struct virtnet_info *vi, struct receive_queue *rq) 2333 { 2334 struct dim_sample cur_sample = {}; 2335 2336 if (!rq->packets_in_napi) 2337 return; 2338 2339 u64_stats_update_begin(&rq->stats.syncp); 2340 dim_update_sample(rq->calls, 2341 u64_stats_read(&rq->stats.packets), 2342 u64_stats_read(&rq->stats.bytes), 2343 &cur_sample); 2344 u64_stats_update_end(&rq->stats.syncp); 2345 2346 net_dim(&rq->dim, cur_sample); 2347 rq->packets_in_napi = 0; 2348 } 2349 2350 static int virtnet_poll(struct napi_struct *napi, int budget) 2351 { 2352 struct receive_queue *rq = 2353 container_of(napi, struct receive_queue, napi); 2354 struct virtnet_info *vi = rq->vq->vdev->priv; 2355 struct send_queue *sq; 2356 unsigned int received; 2357 unsigned int xdp_xmit = 0; 2358 bool napi_complete; 2359 2360 virtnet_poll_cleantx(rq); 2361 2362 received = virtnet_receive(rq, budget, &xdp_xmit); 2363 rq->packets_in_napi += received; 2364 2365 if (xdp_xmit & VIRTIO_XDP_REDIR) 2366 xdp_do_flush(); 2367 2368 /* Out of packets? */ 2369 if (received < budget) { 2370 napi_complete = virtqueue_napi_complete(napi, rq->vq, received); 2371 /* Intentionally not taking dim_lock here. This may result in a 2372 * spurious net_dim call. But if that happens virtnet_rx_dim_work 2373 * will not act on the scheduled work. 2374 */ 2375 if (napi_complete && rq->dim_enabled) 2376 virtnet_rx_dim_update(vi, rq); 2377 } 2378 2379 if (xdp_xmit & VIRTIO_XDP_TX) { 2380 sq = virtnet_xdp_get_sq(vi); 2381 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) { 2382 u64_stats_update_begin(&sq->stats.syncp); 2383 u64_stats_inc(&sq->stats.kicks); 2384 u64_stats_update_end(&sq->stats.syncp); 2385 } 2386 virtnet_xdp_put_sq(vi, sq); 2387 } 2388 2389 return received; 2390 } 2391 2392 static void virtnet_disable_queue_pair(struct virtnet_info *vi, int qp_index) 2393 { 2394 virtnet_napi_tx_disable(&vi->sq[qp_index].napi); 2395 napi_disable(&vi->rq[qp_index].napi); 2396 xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq); 2397 } 2398 2399 static int virtnet_enable_queue_pair(struct virtnet_info *vi, int qp_index) 2400 { 2401 struct net_device *dev = vi->dev; 2402 int err; 2403 2404 err = xdp_rxq_info_reg(&vi->rq[qp_index].xdp_rxq, dev, qp_index, 2405 vi->rq[qp_index].napi.napi_id); 2406 if (err < 0) 2407 return err; 2408 2409 err = xdp_rxq_info_reg_mem_model(&vi->rq[qp_index].xdp_rxq, 2410 MEM_TYPE_PAGE_SHARED, NULL); 2411 if (err < 0) 2412 goto err_xdp_reg_mem_model; 2413 2414 virtnet_napi_enable(vi->rq[qp_index].vq, &vi->rq[qp_index].napi); 2415 virtnet_napi_tx_enable(vi, vi->sq[qp_index].vq, &vi->sq[qp_index].napi); 2416 2417 return 0; 2418 2419 err_xdp_reg_mem_model: 2420 xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq); 2421 return err; 2422 } 2423 2424 static int virtnet_open(struct net_device *dev) 2425 { 2426 struct virtnet_info *vi = netdev_priv(dev); 2427 int i, err; 2428 2429 enable_delayed_refill(vi); 2430 2431 for (i = 0; i < vi->max_queue_pairs; i++) { 2432 if (i < vi->curr_queue_pairs) 2433 /* Make sure we have some buffers: if oom use wq. */ 2434 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 2435 schedule_delayed_work(&vi->refill, 0); 2436 2437 err = virtnet_enable_queue_pair(vi, i); 2438 if (err < 0) 2439 goto err_enable_qp; 2440 } 2441 2442 return 0; 2443 2444 err_enable_qp: 2445 disable_delayed_refill(vi); 2446 cancel_delayed_work_sync(&vi->refill); 2447 2448 for (i--; i >= 0; i--) { 2449 virtnet_disable_queue_pair(vi, i); 2450 cancel_work_sync(&vi->rq[i].dim.work); 2451 } 2452 2453 return err; 2454 } 2455 2456 static int virtnet_poll_tx(struct napi_struct *napi, int budget) 2457 { 2458 struct send_queue *sq = container_of(napi, struct send_queue, napi); 2459 struct virtnet_info *vi = sq->vq->vdev->priv; 2460 unsigned int index = vq2txq(sq->vq); 2461 struct netdev_queue *txq; 2462 int opaque; 2463 bool done; 2464 2465 if (unlikely(is_xdp_raw_buffer_queue(vi, index))) { 2466 /* We don't need to enable cb for XDP */ 2467 napi_complete_done(napi, 0); 2468 return 0; 2469 } 2470 2471 txq = netdev_get_tx_queue(vi->dev, index); 2472 __netif_tx_lock(txq, raw_smp_processor_id()); 2473 virtqueue_disable_cb(sq->vq); 2474 free_old_xmit(sq, true); 2475 2476 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) { 2477 if (netif_tx_queue_stopped(txq)) { 2478 u64_stats_update_begin(&sq->stats.syncp); 2479 u64_stats_inc(&sq->stats.wake); 2480 u64_stats_update_end(&sq->stats.syncp); 2481 } 2482 netif_tx_wake_queue(txq); 2483 } 2484 2485 opaque = virtqueue_enable_cb_prepare(sq->vq); 2486 2487 done = napi_complete_done(napi, 0); 2488 2489 if (!done) 2490 virtqueue_disable_cb(sq->vq); 2491 2492 __netif_tx_unlock(txq); 2493 2494 if (done) { 2495 if (unlikely(virtqueue_poll(sq->vq, opaque))) { 2496 if (napi_schedule_prep(napi)) { 2497 __netif_tx_lock(txq, raw_smp_processor_id()); 2498 virtqueue_disable_cb(sq->vq); 2499 __netif_tx_unlock(txq); 2500 __napi_schedule(napi); 2501 } 2502 } 2503 } 2504 2505 return 0; 2506 } 2507 2508 static int xmit_skb(struct send_queue *sq, struct sk_buff *skb) 2509 { 2510 struct virtio_net_hdr_mrg_rxbuf *hdr; 2511 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; 2512 struct virtnet_info *vi = sq->vq->vdev->priv; 2513 int num_sg; 2514 unsigned hdr_len = vi->hdr_len; 2515 bool can_push; 2516 2517 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); 2518 2519 can_push = vi->any_header_sg && 2520 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) && 2521 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len; 2522 /* Even if we can, don't push here yet as this would skew 2523 * csum_start offset below. */ 2524 if (can_push) 2525 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len); 2526 else 2527 hdr = &skb_vnet_common_hdr(skb)->mrg_hdr; 2528 2529 if (virtio_net_hdr_from_skb(skb, &hdr->hdr, 2530 virtio_is_little_endian(vi->vdev), false, 2531 0)) 2532 return -EPROTO; 2533 2534 if (vi->mergeable_rx_bufs) 2535 hdr->num_buffers = 0; 2536 2537 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2)); 2538 if (can_push) { 2539 __skb_push(skb, hdr_len); 2540 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len); 2541 if (unlikely(num_sg < 0)) 2542 return num_sg; 2543 /* Pull header back to avoid skew in tx bytes calculations. */ 2544 __skb_pull(skb, hdr_len); 2545 } else { 2546 sg_set_buf(sq->sg, hdr, hdr_len); 2547 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len); 2548 if (unlikely(num_sg < 0)) 2549 return num_sg; 2550 num_sg++; 2551 } 2552 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC); 2553 } 2554 2555 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) 2556 { 2557 struct virtnet_info *vi = netdev_priv(dev); 2558 int qnum = skb_get_queue_mapping(skb); 2559 struct send_queue *sq = &vi->sq[qnum]; 2560 int err; 2561 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum); 2562 bool kick = !netdev_xmit_more(); 2563 bool use_napi = sq->napi.weight; 2564 2565 /* Free up any pending old buffers before queueing new ones. */ 2566 do { 2567 if (use_napi) 2568 virtqueue_disable_cb(sq->vq); 2569 2570 free_old_xmit(sq, false); 2571 2572 } while (use_napi && kick && 2573 unlikely(!virtqueue_enable_cb_delayed(sq->vq))); 2574 2575 /* timestamp packet in software */ 2576 skb_tx_timestamp(skb); 2577 2578 /* Try to transmit */ 2579 err = xmit_skb(sq, skb); 2580 2581 /* This should not happen! */ 2582 if (unlikely(err)) { 2583 DEV_STATS_INC(dev, tx_fifo_errors); 2584 if (net_ratelimit()) 2585 dev_warn(&dev->dev, 2586 "Unexpected TXQ (%d) queue failure: %d\n", 2587 qnum, err); 2588 DEV_STATS_INC(dev, tx_dropped); 2589 dev_kfree_skb_any(skb); 2590 return NETDEV_TX_OK; 2591 } 2592 2593 /* Don't wait up for transmitted skbs to be freed. */ 2594 if (!use_napi) { 2595 skb_orphan(skb); 2596 nf_reset_ct(skb); 2597 } 2598 2599 check_sq_full_and_disable(vi, dev, sq); 2600 2601 if (kick || netif_xmit_stopped(txq)) { 2602 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) { 2603 u64_stats_update_begin(&sq->stats.syncp); 2604 u64_stats_inc(&sq->stats.kicks); 2605 u64_stats_update_end(&sq->stats.syncp); 2606 } 2607 } 2608 2609 return NETDEV_TX_OK; 2610 } 2611 2612 static int virtnet_rx_resize(struct virtnet_info *vi, 2613 struct receive_queue *rq, u32 ring_num) 2614 { 2615 bool running = netif_running(vi->dev); 2616 int err, qindex; 2617 2618 qindex = rq - vi->rq; 2619 2620 if (running) { 2621 napi_disable(&rq->napi); 2622 cancel_work_sync(&rq->dim.work); 2623 } 2624 2625 err = virtqueue_resize(rq->vq, ring_num, virtnet_rq_unmap_free_buf); 2626 if (err) 2627 netdev_err(vi->dev, "resize rx fail: rx queue index: %d err: %d\n", qindex, err); 2628 2629 if (!try_fill_recv(vi, rq, GFP_KERNEL)) 2630 schedule_delayed_work(&vi->refill, 0); 2631 2632 if (running) 2633 virtnet_napi_enable(rq->vq, &rq->napi); 2634 return err; 2635 } 2636 2637 static int virtnet_tx_resize(struct virtnet_info *vi, 2638 struct send_queue *sq, u32 ring_num) 2639 { 2640 bool running = netif_running(vi->dev); 2641 struct netdev_queue *txq; 2642 int err, qindex; 2643 2644 qindex = sq - vi->sq; 2645 2646 if (running) 2647 virtnet_napi_tx_disable(&sq->napi); 2648 2649 txq = netdev_get_tx_queue(vi->dev, qindex); 2650 2651 /* 1. wait all ximt complete 2652 * 2. fix the race of netif_stop_subqueue() vs netif_start_subqueue() 2653 */ 2654 __netif_tx_lock_bh(txq); 2655 2656 /* Prevent rx poll from accessing sq. */ 2657 sq->reset = true; 2658 2659 /* Prevent the upper layer from trying to send packets. */ 2660 netif_stop_subqueue(vi->dev, qindex); 2661 2662 __netif_tx_unlock_bh(txq); 2663 2664 err = virtqueue_resize(sq->vq, ring_num, virtnet_sq_free_unused_buf); 2665 if (err) 2666 netdev_err(vi->dev, "resize tx fail: tx queue index: %d err: %d\n", qindex, err); 2667 2668 __netif_tx_lock_bh(txq); 2669 sq->reset = false; 2670 netif_tx_wake_queue(txq); 2671 __netif_tx_unlock_bh(txq); 2672 2673 if (running) 2674 virtnet_napi_tx_enable(vi, sq->vq, &sq->napi); 2675 return err; 2676 } 2677 2678 /* 2679 * Send command via the control virtqueue and check status. Commands 2680 * supported by the hypervisor, as indicated by feature bits, should 2681 * never fail unless improperly formatted. 2682 */ 2683 static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd, 2684 struct scatterlist *out, 2685 struct scatterlist *in) 2686 { 2687 struct scatterlist *sgs[5], hdr, stat; 2688 u32 out_num = 0, tmp, in_num = 0; 2689 bool ok; 2690 int ret; 2691 2692 /* Caller should know better */ 2693 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)); 2694 2695 mutex_lock(&vi->cvq_lock); 2696 vi->ctrl->status = ~0; 2697 vi->ctrl->hdr.class = class; 2698 vi->ctrl->hdr.cmd = cmd; 2699 /* Add header */ 2700 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr)); 2701 sgs[out_num++] = &hdr; 2702 2703 if (out) 2704 sgs[out_num++] = out; 2705 2706 /* Add return status. */ 2707 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status)); 2708 sgs[out_num + in_num++] = &stat; 2709 2710 if (in) 2711 sgs[out_num + in_num++] = in; 2712 2713 BUG_ON(out_num + in_num > ARRAY_SIZE(sgs)); 2714 ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, in_num, vi, GFP_ATOMIC); 2715 if (ret < 0) { 2716 dev_warn(&vi->vdev->dev, 2717 "Failed to add sgs for command vq: %d\n.", ret); 2718 mutex_unlock(&vi->cvq_lock); 2719 return false; 2720 } 2721 2722 if (unlikely(!virtqueue_kick(vi->cvq))) 2723 goto unlock; 2724 2725 /* Spin for a response, the kick causes an ioport write, trapping 2726 * into the hypervisor, so the request should be handled immediately. 2727 */ 2728 while (!virtqueue_get_buf(vi->cvq, &tmp) && 2729 !virtqueue_is_broken(vi->cvq)) { 2730 cond_resched(); 2731 cpu_relax(); 2732 } 2733 2734 unlock: 2735 ok = vi->ctrl->status == VIRTIO_NET_OK; 2736 mutex_unlock(&vi->cvq_lock); 2737 return ok; 2738 } 2739 2740 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, 2741 struct scatterlist *out) 2742 { 2743 return virtnet_send_command_reply(vi, class, cmd, out, NULL); 2744 } 2745 2746 static int virtnet_set_mac_address(struct net_device *dev, void *p) 2747 { 2748 struct virtnet_info *vi = netdev_priv(dev); 2749 struct virtio_device *vdev = vi->vdev; 2750 int ret; 2751 struct sockaddr *addr; 2752 struct scatterlist sg; 2753 2754 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY)) 2755 return -EOPNOTSUPP; 2756 2757 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL); 2758 if (!addr) 2759 return -ENOMEM; 2760 2761 ret = eth_prepare_mac_addr_change(dev, addr); 2762 if (ret) 2763 goto out; 2764 2765 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 2766 sg_init_one(&sg, addr->sa_data, dev->addr_len); 2767 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 2768 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) { 2769 dev_warn(&vdev->dev, 2770 "Failed to set mac address by vq command.\n"); 2771 ret = -EINVAL; 2772 goto out; 2773 } 2774 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) && 2775 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) { 2776 unsigned int i; 2777 2778 /* Naturally, this has an atomicity problem. */ 2779 for (i = 0; i < dev->addr_len; i++) 2780 virtio_cwrite8(vdev, 2781 offsetof(struct virtio_net_config, mac) + 2782 i, addr->sa_data[i]); 2783 } 2784 2785 eth_commit_mac_addr_change(dev, p); 2786 ret = 0; 2787 2788 out: 2789 kfree(addr); 2790 return ret; 2791 } 2792 2793 static void virtnet_stats(struct net_device *dev, 2794 struct rtnl_link_stats64 *tot) 2795 { 2796 struct virtnet_info *vi = netdev_priv(dev); 2797 unsigned int start; 2798 int i; 2799 2800 for (i = 0; i < vi->max_queue_pairs; i++) { 2801 u64 tpackets, tbytes, terrors, rpackets, rbytes, rdrops; 2802 struct receive_queue *rq = &vi->rq[i]; 2803 struct send_queue *sq = &vi->sq[i]; 2804 2805 do { 2806 start = u64_stats_fetch_begin(&sq->stats.syncp); 2807 tpackets = u64_stats_read(&sq->stats.packets); 2808 tbytes = u64_stats_read(&sq->stats.bytes); 2809 terrors = u64_stats_read(&sq->stats.tx_timeouts); 2810 } while (u64_stats_fetch_retry(&sq->stats.syncp, start)); 2811 2812 do { 2813 start = u64_stats_fetch_begin(&rq->stats.syncp); 2814 rpackets = u64_stats_read(&rq->stats.packets); 2815 rbytes = u64_stats_read(&rq->stats.bytes); 2816 rdrops = u64_stats_read(&rq->stats.drops); 2817 } while (u64_stats_fetch_retry(&rq->stats.syncp, start)); 2818 2819 tot->rx_packets += rpackets; 2820 tot->tx_packets += tpackets; 2821 tot->rx_bytes += rbytes; 2822 tot->tx_bytes += tbytes; 2823 tot->rx_dropped += rdrops; 2824 tot->tx_errors += terrors; 2825 } 2826 2827 tot->tx_dropped = DEV_STATS_READ(dev, tx_dropped); 2828 tot->tx_fifo_errors = DEV_STATS_READ(dev, tx_fifo_errors); 2829 tot->rx_length_errors = DEV_STATS_READ(dev, rx_length_errors); 2830 tot->rx_frame_errors = DEV_STATS_READ(dev, rx_frame_errors); 2831 } 2832 2833 static void virtnet_ack_link_announce(struct virtnet_info *vi) 2834 { 2835 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE, 2836 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL)) 2837 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n"); 2838 } 2839 2840 static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 2841 { 2842 struct virtio_net_ctrl_mq *mq __free(kfree) = NULL; 2843 struct scatterlist sg; 2844 struct net_device *dev = vi->dev; 2845 2846 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ)) 2847 return 0; 2848 2849 mq = kzalloc(sizeof(*mq), GFP_KERNEL); 2850 if (!mq) 2851 return -ENOMEM; 2852 2853 mq->virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs); 2854 sg_init_one(&sg, mq, sizeof(*mq)); 2855 2856 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, 2857 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) { 2858 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n", 2859 queue_pairs); 2860 return -EINVAL; 2861 } else { 2862 vi->curr_queue_pairs = queue_pairs; 2863 /* virtnet_open() will refill when device is going to up. */ 2864 if (dev->flags & IFF_UP) 2865 schedule_delayed_work(&vi->refill, 0); 2866 } 2867 2868 return 0; 2869 } 2870 2871 static int virtnet_close(struct net_device *dev) 2872 { 2873 struct virtnet_info *vi = netdev_priv(dev); 2874 int i; 2875 2876 /* Make sure NAPI doesn't schedule refill work */ 2877 disable_delayed_refill(vi); 2878 /* Make sure refill_work doesn't re-enable napi! */ 2879 cancel_delayed_work_sync(&vi->refill); 2880 2881 for (i = 0; i < vi->max_queue_pairs; i++) { 2882 virtnet_disable_queue_pair(vi, i); 2883 cancel_work_sync(&vi->rq[i].dim.work); 2884 } 2885 2886 return 0; 2887 } 2888 2889 static void virtnet_rx_mode_work(struct work_struct *work) 2890 { 2891 struct virtnet_info *vi = 2892 container_of(work, struct virtnet_info, rx_mode_work); 2893 u8 *promisc_allmulti __free(kfree) = NULL; 2894 struct net_device *dev = vi->dev; 2895 struct scatterlist sg[2]; 2896 struct virtio_net_ctrl_mac *mac_data; 2897 struct netdev_hw_addr *ha; 2898 int uc_count; 2899 int mc_count; 2900 void *buf; 2901 int i; 2902 2903 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */ 2904 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) 2905 return; 2906 2907 promisc_allmulti = kzalloc(sizeof(*promisc_allmulti), GFP_KERNEL); 2908 if (!promisc_allmulti) { 2909 dev_warn(&dev->dev, "Failed to set RX mode, no memory.\n"); 2910 return; 2911 } 2912 2913 rtnl_lock(); 2914 2915 *promisc_allmulti = !!(dev->flags & IFF_PROMISC); 2916 sg_init_one(sg, promisc_allmulti, sizeof(*promisc_allmulti)); 2917 2918 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 2919 VIRTIO_NET_CTRL_RX_PROMISC, sg)) 2920 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", 2921 *promisc_allmulti ? "en" : "dis"); 2922 2923 *promisc_allmulti = !!(dev->flags & IFF_ALLMULTI); 2924 sg_init_one(sg, promisc_allmulti, sizeof(*promisc_allmulti)); 2925 2926 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 2927 VIRTIO_NET_CTRL_RX_ALLMULTI, sg)) 2928 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", 2929 *promisc_allmulti ? "en" : "dis"); 2930 2931 netif_addr_lock_bh(dev); 2932 2933 uc_count = netdev_uc_count(dev); 2934 mc_count = netdev_mc_count(dev); 2935 /* MAC filter - use one buffer for both lists */ 2936 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) + 2937 (2 * sizeof(mac_data->entries)), GFP_ATOMIC); 2938 mac_data = buf; 2939 if (!buf) { 2940 netif_addr_unlock_bh(dev); 2941 rtnl_unlock(); 2942 return; 2943 } 2944 2945 sg_init_table(sg, 2); 2946 2947 /* Store the unicast list and count in the front of the buffer */ 2948 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count); 2949 i = 0; 2950 netdev_for_each_uc_addr(ha, dev) 2951 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 2952 2953 sg_set_buf(&sg[0], mac_data, 2954 sizeof(mac_data->entries) + (uc_count * ETH_ALEN)); 2955 2956 /* multicast list and count fill the end */ 2957 mac_data = (void *)&mac_data->macs[uc_count][0]; 2958 2959 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count); 2960 i = 0; 2961 netdev_for_each_mc_addr(ha, dev) 2962 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 2963 2964 netif_addr_unlock_bh(dev); 2965 2966 sg_set_buf(&sg[1], mac_data, 2967 sizeof(mac_data->entries) + (mc_count * ETH_ALEN)); 2968 2969 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 2970 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg)) 2971 dev_warn(&dev->dev, "Failed to set MAC filter table.\n"); 2972 2973 rtnl_unlock(); 2974 2975 kfree(buf); 2976 } 2977 2978 static void virtnet_set_rx_mode(struct net_device *dev) 2979 { 2980 struct virtnet_info *vi = netdev_priv(dev); 2981 2982 if (vi->rx_mode_work_enabled) 2983 schedule_work(&vi->rx_mode_work); 2984 } 2985 2986 static int virtnet_vlan_rx_add_vid(struct net_device *dev, 2987 __be16 proto, u16 vid) 2988 { 2989 struct virtnet_info *vi = netdev_priv(dev); 2990 __virtio16 *_vid __free(kfree) = NULL; 2991 struct scatterlist sg; 2992 2993 _vid = kzalloc(sizeof(*_vid), GFP_KERNEL); 2994 if (!_vid) 2995 return -ENOMEM; 2996 2997 *_vid = cpu_to_virtio16(vi->vdev, vid); 2998 sg_init_one(&sg, _vid, sizeof(*_vid)); 2999 3000 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 3001 VIRTIO_NET_CTRL_VLAN_ADD, &sg)) 3002 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); 3003 return 0; 3004 } 3005 3006 static int virtnet_vlan_rx_kill_vid(struct net_device *dev, 3007 __be16 proto, u16 vid) 3008 { 3009 struct virtnet_info *vi = netdev_priv(dev); 3010 __virtio16 *_vid __free(kfree) = NULL; 3011 struct scatterlist sg; 3012 3013 _vid = kzalloc(sizeof(*_vid), GFP_KERNEL); 3014 if (!_vid) 3015 return -ENOMEM; 3016 3017 *_vid = cpu_to_virtio16(vi->vdev, vid); 3018 sg_init_one(&sg, _vid, sizeof(*_vid)); 3019 3020 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 3021 VIRTIO_NET_CTRL_VLAN_DEL, &sg)) 3022 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); 3023 return 0; 3024 } 3025 3026 static void virtnet_clean_affinity(struct virtnet_info *vi) 3027 { 3028 int i; 3029 3030 if (vi->affinity_hint_set) { 3031 for (i = 0; i < vi->max_queue_pairs; i++) { 3032 virtqueue_set_affinity(vi->rq[i].vq, NULL); 3033 virtqueue_set_affinity(vi->sq[i].vq, NULL); 3034 } 3035 3036 vi->affinity_hint_set = false; 3037 } 3038 } 3039 3040 static void virtnet_set_affinity(struct virtnet_info *vi) 3041 { 3042 cpumask_var_t mask; 3043 int stragglers; 3044 int group_size; 3045 int i, j, cpu; 3046 int num_cpu; 3047 int stride; 3048 3049 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) { 3050 virtnet_clean_affinity(vi); 3051 return; 3052 } 3053 3054 num_cpu = num_online_cpus(); 3055 stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1); 3056 stragglers = num_cpu >= vi->curr_queue_pairs ? 3057 num_cpu % vi->curr_queue_pairs : 3058 0; 3059 cpu = cpumask_first(cpu_online_mask); 3060 3061 for (i = 0; i < vi->curr_queue_pairs; i++) { 3062 group_size = stride + (i < stragglers ? 1 : 0); 3063 3064 for (j = 0; j < group_size; j++) { 3065 cpumask_set_cpu(cpu, mask); 3066 cpu = cpumask_next_wrap(cpu, cpu_online_mask, 3067 nr_cpu_ids, false); 3068 } 3069 virtqueue_set_affinity(vi->rq[i].vq, mask); 3070 virtqueue_set_affinity(vi->sq[i].vq, mask); 3071 __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS); 3072 cpumask_clear(mask); 3073 } 3074 3075 vi->affinity_hint_set = true; 3076 free_cpumask_var(mask); 3077 } 3078 3079 static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node) 3080 { 3081 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 3082 node); 3083 virtnet_set_affinity(vi); 3084 return 0; 3085 } 3086 3087 static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node) 3088 { 3089 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 3090 node_dead); 3091 virtnet_set_affinity(vi); 3092 return 0; 3093 } 3094 3095 static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node) 3096 { 3097 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 3098 node); 3099 3100 virtnet_clean_affinity(vi); 3101 return 0; 3102 } 3103 3104 static enum cpuhp_state virtionet_online; 3105 3106 static int virtnet_cpu_notif_add(struct virtnet_info *vi) 3107 { 3108 int ret; 3109 3110 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node); 3111 if (ret) 3112 return ret; 3113 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD, 3114 &vi->node_dead); 3115 if (!ret) 3116 return ret; 3117 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 3118 return ret; 3119 } 3120 3121 static void virtnet_cpu_notif_remove(struct virtnet_info *vi) 3122 { 3123 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 3124 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD, 3125 &vi->node_dead); 3126 } 3127 3128 static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi, 3129 u16 vqn, u32 max_usecs, u32 max_packets) 3130 { 3131 struct virtio_net_ctrl_coal_vq *coal_vq __free(kfree) = NULL; 3132 struct scatterlist sgs; 3133 3134 coal_vq = kzalloc(sizeof(*coal_vq), GFP_KERNEL); 3135 if (!coal_vq) 3136 return -ENOMEM; 3137 3138 coal_vq->vqn = cpu_to_le16(vqn); 3139 coal_vq->coal.max_usecs = cpu_to_le32(max_usecs); 3140 coal_vq->coal.max_packets = cpu_to_le32(max_packets); 3141 sg_init_one(&sgs, coal_vq, sizeof(*coal_vq)); 3142 3143 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL, 3144 VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET, 3145 &sgs)) 3146 return -EINVAL; 3147 3148 return 0; 3149 } 3150 3151 static int virtnet_send_rx_ctrl_coal_vq_cmd(struct virtnet_info *vi, 3152 u16 queue, u32 max_usecs, 3153 u32 max_packets) 3154 { 3155 int err; 3156 3157 err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(queue), 3158 max_usecs, max_packets); 3159 if (err) 3160 return err; 3161 3162 vi->rq[queue].intr_coal.max_usecs = max_usecs; 3163 vi->rq[queue].intr_coal.max_packets = max_packets; 3164 3165 return 0; 3166 } 3167 3168 static int virtnet_send_tx_ctrl_coal_vq_cmd(struct virtnet_info *vi, 3169 u16 queue, u32 max_usecs, 3170 u32 max_packets) 3171 { 3172 int err; 3173 3174 err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(queue), 3175 max_usecs, max_packets); 3176 if (err) 3177 return err; 3178 3179 vi->sq[queue].intr_coal.max_usecs = max_usecs; 3180 vi->sq[queue].intr_coal.max_packets = max_packets; 3181 3182 return 0; 3183 } 3184 3185 static void virtnet_get_ringparam(struct net_device *dev, 3186 struct ethtool_ringparam *ring, 3187 struct kernel_ethtool_ringparam *kernel_ring, 3188 struct netlink_ext_ack *extack) 3189 { 3190 struct virtnet_info *vi = netdev_priv(dev); 3191 3192 ring->rx_max_pending = vi->rq[0].vq->num_max; 3193 ring->tx_max_pending = vi->sq[0].vq->num_max; 3194 ring->rx_pending = virtqueue_get_vring_size(vi->rq[0].vq); 3195 ring->tx_pending = virtqueue_get_vring_size(vi->sq[0].vq); 3196 } 3197 3198 static int virtnet_set_ringparam(struct net_device *dev, 3199 struct ethtool_ringparam *ring, 3200 struct kernel_ethtool_ringparam *kernel_ring, 3201 struct netlink_ext_ack *extack) 3202 { 3203 struct virtnet_info *vi = netdev_priv(dev); 3204 u32 rx_pending, tx_pending; 3205 struct receive_queue *rq; 3206 struct send_queue *sq; 3207 int i, err; 3208 3209 if (ring->rx_mini_pending || ring->rx_jumbo_pending) 3210 return -EINVAL; 3211 3212 rx_pending = virtqueue_get_vring_size(vi->rq[0].vq); 3213 tx_pending = virtqueue_get_vring_size(vi->sq[0].vq); 3214 3215 if (ring->rx_pending == rx_pending && 3216 ring->tx_pending == tx_pending) 3217 return 0; 3218 3219 if (ring->rx_pending > vi->rq[0].vq->num_max) 3220 return -EINVAL; 3221 3222 if (ring->tx_pending > vi->sq[0].vq->num_max) 3223 return -EINVAL; 3224 3225 for (i = 0; i < vi->max_queue_pairs; i++) { 3226 rq = vi->rq + i; 3227 sq = vi->sq + i; 3228 3229 if (ring->tx_pending != tx_pending) { 3230 err = virtnet_tx_resize(vi, sq, ring->tx_pending); 3231 if (err) 3232 return err; 3233 3234 /* Upon disabling and re-enabling a transmit virtqueue, the device must 3235 * set the coalescing parameters of the virtqueue to those configured 3236 * through the VIRTIO_NET_CTRL_NOTF_COAL_TX_SET command, or, if the driver 3237 * did not set any TX coalescing parameters, to 0. 3238 */ 3239 err = virtnet_send_tx_ctrl_coal_vq_cmd(vi, i, 3240 vi->intr_coal_tx.max_usecs, 3241 vi->intr_coal_tx.max_packets); 3242 if (err) 3243 return err; 3244 } 3245 3246 if (ring->rx_pending != rx_pending) { 3247 err = virtnet_rx_resize(vi, rq, ring->rx_pending); 3248 if (err) 3249 return err; 3250 3251 /* The reason is same as the transmit virtqueue reset */ 3252 mutex_lock(&vi->rq[i].dim_lock); 3253 err = virtnet_send_rx_ctrl_coal_vq_cmd(vi, i, 3254 vi->intr_coal_rx.max_usecs, 3255 vi->intr_coal_rx.max_packets); 3256 mutex_unlock(&vi->rq[i].dim_lock); 3257 if (err) 3258 return err; 3259 } 3260 } 3261 3262 return 0; 3263 } 3264 3265 static bool virtnet_commit_rss_command(struct virtnet_info *vi) 3266 { 3267 struct net_device *dev = vi->dev; 3268 struct scatterlist sgs[4]; 3269 unsigned int sg_buf_size; 3270 3271 /* prepare sgs */ 3272 sg_init_table(sgs, 4); 3273 3274 sg_buf_size = offsetof(struct virtio_net_ctrl_rss, indirection_table); 3275 sg_set_buf(&sgs[0], &vi->rss, sg_buf_size); 3276 3277 sg_buf_size = sizeof(uint16_t) * (vi->rss.indirection_table_mask + 1); 3278 sg_set_buf(&sgs[1], vi->rss.indirection_table, sg_buf_size); 3279 3280 sg_buf_size = offsetof(struct virtio_net_ctrl_rss, key) 3281 - offsetof(struct virtio_net_ctrl_rss, max_tx_vq); 3282 sg_set_buf(&sgs[2], &vi->rss.max_tx_vq, sg_buf_size); 3283 3284 sg_buf_size = vi->rss_key_size; 3285 sg_set_buf(&sgs[3], vi->rss.key, sg_buf_size); 3286 3287 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, 3288 vi->has_rss ? VIRTIO_NET_CTRL_MQ_RSS_CONFIG 3289 : VIRTIO_NET_CTRL_MQ_HASH_CONFIG, sgs)) 3290 goto err; 3291 3292 return true; 3293 3294 err: 3295 dev_warn(&dev->dev, "VIRTIONET issue with committing RSS sgs\n"); 3296 return false; 3297 3298 } 3299 3300 static void virtnet_init_default_rss(struct virtnet_info *vi) 3301 { 3302 u32 indir_val = 0; 3303 int i = 0; 3304 3305 vi->rss.hash_types = vi->rss_hash_types_supported; 3306 vi->rss_hash_types_saved = vi->rss_hash_types_supported; 3307 vi->rss.indirection_table_mask = vi->rss_indir_table_size 3308 ? vi->rss_indir_table_size - 1 : 0; 3309 vi->rss.unclassified_queue = 0; 3310 3311 for (; i < vi->rss_indir_table_size; ++i) { 3312 indir_val = ethtool_rxfh_indir_default(i, vi->curr_queue_pairs); 3313 vi->rss.indirection_table[i] = indir_val; 3314 } 3315 3316 vi->rss.max_tx_vq = vi->has_rss ? vi->curr_queue_pairs : 0; 3317 vi->rss.hash_key_length = vi->rss_key_size; 3318 3319 netdev_rss_key_fill(vi->rss.key, vi->rss_key_size); 3320 } 3321 3322 static void virtnet_get_hashflow(const struct virtnet_info *vi, struct ethtool_rxnfc *info) 3323 { 3324 info->data = 0; 3325 switch (info->flow_type) { 3326 case TCP_V4_FLOW: 3327 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) { 3328 info->data = RXH_IP_SRC | RXH_IP_DST | 3329 RXH_L4_B_0_1 | RXH_L4_B_2_3; 3330 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) { 3331 info->data = RXH_IP_SRC | RXH_IP_DST; 3332 } 3333 break; 3334 case TCP_V6_FLOW: 3335 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) { 3336 info->data = RXH_IP_SRC | RXH_IP_DST | 3337 RXH_L4_B_0_1 | RXH_L4_B_2_3; 3338 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) { 3339 info->data = RXH_IP_SRC | RXH_IP_DST; 3340 } 3341 break; 3342 case UDP_V4_FLOW: 3343 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) { 3344 info->data = RXH_IP_SRC | RXH_IP_DST | 3345 RXH_L4_B_0_1 | RXH_L4_B_2_3; 3346 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) { 3347 info->data = RXH_IP_SRC | RXH_IP_DST; 3348 } 3349 break; 3350 case UDP_V6_FLOW: 3351 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) { 3352 info->data = RXH_IP_SRC | RXH_IP_DST | 3353 RXH_L4_B_0_1 | RXH_L4_B_2_3; 3354 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) { 3355 info->data = RXH_IP_SRC | RXH_IP_DST; 3356 } 3357 break; 3358 case IPV4_FLOW: 3359 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) 3360 info->data = RXH_IP_SRC | RXH_IP_DST; 3361 3362 break; 3363 case IPV6_FLOW: 3364 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) 3365 info->data = RXH_IP_SRC | RXH_IP_DST; 3366 3367 break; 3368 default: 3369 info->data = 0; 3370 break; 3371 } 3372 } 3373 3374 static bool virtnet_set_hashflow(struct virtnet_info *vi, struct ethtool_rxnfc *info) 3375 { 3376 u32 new_hashtypes = vi->rss_hash_types_saved; 3377 bool is_disable = info->data & RXH_DISCARD; 3378 bool is_l4 = info->data == (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3); 3379 3380 /* supports only 'sd', 'sdfn' and 'r' */ 3381 if (!((info->data == (RXH_IP_SRC | RXH_IP_DST)) | is_l4 | is_disable)) 3382 return false; 3383 3384 switch (info->flow_type) { 3385 case TCP_V4_FLOW: 3386 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_TCPv4); 3387 if (!is_disable) 3388 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4 3389 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv4 : 0); 3390 break; 3391 case UDP_V4_FLOW: 3392 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_UDPv4); 3393 if (!is_disable) 3394 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4 3395 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv4 : 0); 3396 break; 3397 case IPV4_FLOW: 3398 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv4; 3399 if (!is_disable) 3400 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv4; 3401 break; 3402 case TCP_V6_FLOW: 3403 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_TCPv6); 3404 if (!is_disable) 3405 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6 3406 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv6 : 0); 3407 break; 3408 case UDP_V6_FLOW: 3409 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_UDPv6); 3410 if (!is_disable) 3411 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6 3412 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv6 : 0); 3413 break; 3414 case IPV6_FLOW: 3415 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv6; 3416 if (!is_disable) 3417 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv6; 3418 break; 3419 default: 3420 /* unsupported flow */ 3421 return false; 3422 } 3423 3424 /* if unsupported hashtype was set */ 3425 if (new_hashtypes != (new_hashtypes & vi->rss_hash_types_supported)) 3426 return false; 3427 3428 if (new_hashtypes != vi->rss_hash_types_saved) { 3429 vi->rss_hash_types_saved = new_hashtypes; 3430 vi->rss.hash_types = vi->rss_hash_types_saved; 3431 if (vi->dev->features & NETIF_F_RXHASH) 3432 return virtnet_commit_rss_command(vi); 3433 } 3434 3435 return true; 3436 } 3437 3438 static void virtnet_get_drvinfo(struct net_device *dev, 3439 struct ethtool_drvinfo *info) 3440 { 3441 struct virtnet_info *vi = netdev_priv(dev); 3442 struct virtio_device *vdev = vi->vdev; 3443 3444 strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); 3445 strscpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version)); 3446 strscpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info)); 3447 3448 } 3449 3450 /* TODO: Eliminate OOO packets during switching */ 3451 static int virtnet_set_channels(struct net_device *dev, 3452 struct ethtool_channels *channels) 3453 { 3454 struct virtnet_info *vi = netdev_priv(dev); 3455 u16 queue_pairs = channels->combined_count; 3456 int err; 3457 3458 /* We don't support separate rx/tx channels. 3459 * We don't allow setting 'other' channels. 3460 */ 3461 if (channels->rx_count || channels->tx_count || channels->other_count) 3462 return -EINVAL; 3463 3464 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0) 3465 return -EINVAL; 3466 3467 /* For now we don't support modifying channels while XDP is loaded 3468 * also when XDP is loaded all RX queues have XDP programs so we only 3469 * need to check a single RX queue. 3470 */ 3471 if (vi->rq[0].xdp_prog) 3472 return -EINVAL; 3473 3474 cpus_read_lock(); 3475 err = virtnet_set_queues(vi, queue_pairs); 3476 if (err) { 3477 cpus_read_unlock(); 3478 goto err; 3479 } 3480 virtnet_set_affinity(vi); 3481 cpus_read_unlock(); 3482 3483 netif_set_real_num_tx_queues(dev, queue_pairs); 3484 netif_set_real_num_rx_queues(dev, queue_pairs); 3485 err: 3486 return err; 3487 } 3488 3489 static void virtnet_stats_sprintf(u8 **p, const char *fmt, const char *noq_fmt, 3490 int num, int qid, const struct virtnet_stat_desc *desc) 3491 { 3492 int i; 3493 3494 if (qid < 0) { 3495 for (i = 0; i < num; ++i) 3496 ethtool_sprintf(p, noq_fmt, desc[i].desc); 3497 } else { 3498 for (i = 0; i < num; ++i) 3499 ethtool_sprintf(p, fmt, qid, desc[i].desc); 3500 } 3501 } 3502 3503 /* qid == -1: for rx/tx queue total field */ 3504 static void virtnet_get_stats_string(struct virtnet_info *vi, int type, int qid, u8 **data) 3505 { 3506 const struct virtnet_stat_desc *desc; 3507 const char *fmt, *noq_fmt; 3508 u8 *p = *data; 3509 u32 num; 3510 3511 if (type == VIRTNET_Q_TYPE_CQ && qid >= 0) { 3512 noq_fmt = "cq_hw_%s"; 3513 3514 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_CVQ) { 3515 desc = &virtnet_stats_cvq_desc[0]; 3516 num = ARRAY_SIZE(virtnet_stats_cvq_desc); 3517 3518 virtnet_stats_sprintf(&p, NULL, noq_fmt, num, -1, desc); 3519 } 3520 } 3521 3522 if (type == VIRTNET_Q_TYPE_RX) { 3523 fmt = "rx%u_%s"; 3524 noq_fmt = "rx_%s"; 3525 3526 desc = &virtnet_rq_stats_desc[0]; 3527 num = ARRAY_SIZE(virtnet_rq_stats_desc); 3528 3529 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc); 3530 3531 fmt = "rx%u_hw_%s"; 3532 noq_fmt = "rx_hw_%s"; 3533 3534 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) { 3535 desc = &virtnet_stats_rx_basic_desc[0]; 3536 num = ARRAY_SIZE(virtnet_stats_rx_basic_desc); 3537 3538 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc); 3539 } 3540 3541 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) { 3542 desc = &virtnet_stats_rx_csum_desc[0]; 3543 num = ARRAY_SIZE(virtnet_stats_rx_csum_desc); 3544 3545 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc); 3546 } 3547 3548 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED) { 3549 desc = &virtnet_stats_rx_speed_desc[0]; 3550 num = ARRAY_SIZE(virtnet_stats_rx_speed_desc); 3551 3552 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc); 3553 } 3554 } 3555 3556 if (type == VIRTNET_Q_TYPE_TX) { 3557 fmt = "tx%u_%s"; 3558 noq_fmt = "tx_%s"; 3559 3560 desc = &virtnet_sq_stats_desc[0]; 3561 num = ARRAY_SIZE(virtnet_sq_stats_desc); 3562 3563 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc); 3564 3565 fmt = "tx%u_hw_%s"; 3566 noq_fmt = "tx_hw_%s"; 3567 3568 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) { 3569 desc = &virtnet_stats_tx_basic_desc[0]; 3570 num = ARRAY_SIZE(virtnet_stats_tx_basic_desc); 3571 3572 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc); 3573 } 3574 3575 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) { 3576 desc = &virtnet_stats_tx_gso_desc[0]; 3577 num = ARRAY_SIZE(virtnet_stats_tx_gso_desc); 3578 3579 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc); 3580 } 3581 3582 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED) { 3583 desc = &virtnet_stats_tx_speed_desc[0]; 3584 num = ARRAY_SIZE(virtnet_stats_tx_speed_desc); 3585 3586 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc); 3587 } 3588 } 3589 3590 *data = p; 3591 } 3592 3593 struct virtnet_stats_ctx { 3594 /* The stats are write to qstats or ethtool -S */ 3595 bool to_qstat; 3596 3597 /* Used to calculate the offset inside the output buffer. */ 3598 u32 desc_num[3]; 3599 3600 /* The actual supported stat types. */ 3601 u32 bitmap[3]; 3602 3603 /* Used to calculate the reply buffer size. */ 3604 u32 size[3]; 3605 3606 /* Record the output buffer. */ 3607 u64 *data; 3608 }; 3609 3610 static void virtnet_stats_ctx_init(struct virtnet_info *vi, 3611 struct virtnet_stats_ctx *ctx, 3612 u64 *data, bool to_qstat) 3613 { 3614 u32 queue_type; 3615 3616 ctx->data = data; 3617 ctx->to_qstat = to_qstat; 3618 3619 if (to_qstat) { 3620 ctx->desc_num[VIRTNET_Q_TYPE_RX] = ARRAY_SIZE(virtnet_rq_stats_desc_qstat); 3621 ctx->desc_num[VIRTNET_Q_TYPE_TX] = ARRAY_SIZE(virtnet_sq_stats_desc_qstat); 3622 3623 queue_type = VIRTNET_Q_TYPE_RX; 3624 3625 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) { 3626 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_BASIC; 3627 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_basic_desc_qstat); 3628 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_basic); 3629 } 3630 3631 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) { 3632 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_CSUM; 3633 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_csum_desc_qstat); 3634 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_csum); 3635 } 3636 3637 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_GSO) { 3638 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_GSO; 3639 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_gso_desc_qstat); 3640 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_gso); 3641 } 3642 3643 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED) { 3644 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_SPEED; 3645 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_speed_desc_qstat); 3646 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_speed); 3647 } 3648 3649 queue_type = VIRTNET_Q_TYPE_TX; 3650 3651 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) { 3652 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_BASIC; 3653 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_basic_desc_qstat); 3654 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_basic); 3655 } 3656 3657 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_CSUM) { 3658 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_CSUM; 3659 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_csum_desc_qstat); 3660 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_csum); 3661 } 3662 3663 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) { 3664 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_GSO; 3665 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_gso_desc_qstat); 3666 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_gso); 3667 } 3668 3669 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED) { 3670 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_SPEED; 3671 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_speed_desc_qstat); 3672 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_speed); 3673 } 3674 3675 return; 3676 } 3677 3678 ctx->desc_num[VIRTNET_Q_TYPE_RX] = ARRAY_SIZE(virtnet_rq_stats_desc); 3679 ctx->desc_num[VIRTNET_Q_TYPE_TX] = ARRAY_SIZE(virtnet_sq_stats_desc); 3680 3681 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_CVQ) { 3682 queue_type = VIRTNET_Q_TYPE_CQ; 3683 3684 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_CVQ; 3685 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_cvq_desc); 3686 ctx->size[queue_type] += sizeof(struct virtio_net_stats_cvq); 3687 } 3688 3689 queue_type = VIRTNET_Q_TYPE_RX; 3690 3691 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) { 3692 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_BASIC; 3693 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_basic_desc); 3694 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_basic); 3695 } 3696 3697 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) { 3698 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_CSUM; 3699 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_csum_desc); 3700 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_csum); 3701 } 3702 3703 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED) { 3704 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_SPEED; 3705 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_speed_desc); 3706 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_speed); 3707 } 3708 3709 queue_type = VIRTNET_Q_TYPE_TX; 3710 3711 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) { 3712 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_BASIC; 3713 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_basic_desc); 3714 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_basic); 3715 } 3716 3717 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) { 3718 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_GSO; 3719 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_gso_desc); 3720 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_gso); 3721 } 3722 3723 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED) { 3724 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_SPEED; 3725 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_speed_desc); 3726 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_speed); 3727 } 3728 } 3729 3730 /* stats_sum_queue - Calculate the sum of the same fields in sq or rq. 3731 * @sum: the position to store the sum values 3732 * @num: field num 3733 * @q_value: the first queue fields 3734 * @q_num: number of the queues 3735 */ 3736 static void stats_sum_queue(u64 *sum, u32 num, u64 *q_value, u32 q_num) 3737 { 3738 u32 step = num; 3739 int i, j; 3740 u64 *p; 3741 3742 for (i = 0; i < num; ++i) { 3743 p = sum + i; 3744 *p = 0; 3745 3746 for (j = 0; j < q_num; ++j) 3747 *p += *(q_value + i + j * step); 3748 } 3749 } 3750 3751 static void virtnet_fill_total_fields(struct virtnet_info *vi, 3752 struct virtnet_stats_ctx *ctx) 3753 { 3754 u64 *data, *first_rx_q, *first_tx_q; 3755 u32 num_cq, num_rx, num_tx; 3756 3757 num_cq = ctx->desc_num[VIRTNET_Q_TYPE_CQ]; 3758 num_rx = ctx->desc_num[VIRTNET_Q_TYPE_RX]; 3759 num_tx = ctx->desc_num[VIRTNET_Q_TYPE_TX]; 3760 3761 first_rx_q = ctx->data + num_rx + num_tx + num_cq; 3762 first_tx_q = first_rx_q + vi->curr_queue_pairs * num_rx; 3763 3764 data = ctx->data; 3765 3766 stats_sum_queue(data, num_rx, first_rx_q, vi->curr_queue_pairs); 3767 3768 data = ctx->data + num_rx; 3769 3770 stats_sum_queue(data, num_tx, first_tx_q, vi->curr_queue_pairs); 3771 } 3772 3773 static void virtnet_fill_stats_qstat(struct virtnet_info *vi, u32 qid, 3774 struct virtnet_stats_ctx *ctx, 3775 const u8 *base, bool drv_stats, u8 reply_type) 3776 { 3777 const struct virtnet_stat_desc *desc; 3778 const u64_stats_t *v_stat; 3779 u64 offset, bitmap; 3780 const __le64 *v; 3781 u32 queue_type; 3782 int i, num; 3783 3784 queue_type = vq_type(vi, qid); 3785 bitmap = ctx->bitmap[queue_type]; 3786 3787 if (drv_stats) { 3788 if (queue_type == VIRTNET_Q_TYPE_RX) { 3789 desc = &virtnet_rq_stats_desc_qstat[0]; 3790 num = ARRAY_SIZE(virtnet_rq_stats_desc_qstat); 3791 } else { 3792 desc = &virtnet_sq_stats_desc_qstat[0]; 3793 num = ARRAY_SIZE(virtnet_sq_stats_desc_qstat); 3794 } 3795 3796 for (i = 0; i < num; ++i) { 3797 offset = desc[i].qstat_offset / sizeof(*ctx->data); 3798 v_stat = (const u64_stats_t *)(base + desc[i].offset); 3799 ctx->data[offset] = u64_stats_read(v_stat); 3800 } 3801 return; 3802 } 3803 3804 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_BASIC) { 3805 desc = &virtnet_stats_rx_basic_desc_qstat[0]; 3806 num = ARRAY_SIZE(virtnet_stats_rx_basic_desc_qstat); 3807 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_BASIC) 3808 goto found; 3809 } 3810 3811 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_CSUM) { 3812 desc = &virtnet_stats_rx_csum_desc_qstat[0]; 3813 num = ARRAY_SIZE(virtnet_stats_rx_csum_desc_qstat); 3814 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_CSUM) 3815 goto found; 3816 } 3817 3818 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_GSO) { 3819 desc = &virtnet_stats_rx_gso_desc_qstat[0]; 3820 num = ARRAY_SIZE(virtnet_stats_rx_gso_desc_qstat); 3821 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_GSO) 3822 goto found; 3823 } 3824 3825 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_SPEED) { 3826 desc = &virtnet_stats_rx_speed_desc_qstat[0]; 3827 num = ARRAY_SIZE(virtnet_stats_rx_speed_desc_qstat); 3828 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_SPEED) 3829 goto found; 3830 } 3831 3832 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_BASIC) { 3833 desc = &virtnet_stats_tx_basic_desc_qstat[0]; 3834 num = ARRAY_SIZE(virtnet_stats_tx_basic_desc_qstat); 3835 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_BASIC) 3836 goto found; 3837 } 3838 3839 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_CSUM) { 3840 desc = &virtnet_stats_tx_csum_desc_qstat[0]; 3841 num = ARRAY_SIZE(virtnet_stats_tx_csum_desc_qstat); 3842 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_CSUM) 3843 goto found; 3844 } 3845 3846 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_GSO) { 3847 desc = &virtnet_stats_tx_gso_desc_qstat[0]; 3848 num = ARRAY_SIZE(virtnet_stats_tx_gso_desc_qstat); 3849 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_GSO) 3850 goto found; 3851 } 3852 3853 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_SPEED) { 3854 desc = &virtnet_stats_tx_speed_desc_qstat[0]; 3855 num = ARRAY_SIZE(virtnet_stats_tx_speed_desc_qstat); 3856 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_SPEED) 3857 goto found; 3858 } 3859 3860 return; 3861 3862 found: 3863 for (i = 0; i < num; ++i) { 3864 offset = desc[i].qstat_offset / sizeof(*ctx->data); 3865 v = (const __le64 *)(base + desc[i].offset); 3866 ctx->data[offset] = le64_to_cpu(*v); 3867 } 3868 } 3869 3870 /* virtnet_fill_stats - copy the stats to qstats or ethtool -S 3871 * The stats source is the device or the driver. 3872 * 3873 * @vi: virtio net info 3874 * @qid: the vq id 3875 * @ctx: stats ctx (initiated by virtnet_stats_ctx_init()) 3876 * @base: pointer to the device reply or the driver stats structure. 3877 * @drv_stats: designate the base type (device reply, driver stats) 3878 * @type: the type of the device reply (if drv_stats is true, this must be zero) 3879 */ 3880 static void virtnet_fill_stats(struct virtnet_info *vi, u32 qid, 3881 struct virtnet_stats_ctx *ctx, 3882 const u8 *base, bool drv_stats, u8 reply_type) 3883 { 3884 u32 queue_type, num_rx, num_tx, num_cq; 3885 const struct virtnet_stat_desc *desc; 3886 const u64_stats_t *v_stat; 3887 u64 offset, bitmap; 3888 const __le64 *v; 3889 int i, num; 3890 3891 if (ctx->to_qstat) 3892 return virtnet_fill_stats_qstat(vi, qid, ctx, base, drv_stats, reply_type); 3893 3894 num_cq = ctx->desc_num[VIRTNET_Q_TYPE_CQ]; 3895 num_rx = ctx->desc_num[VIRTNET_Q_TYPE_RX]; 3896 num_tx = ctx->desc_num[VIRTNET_Q_TYPE_TX]; 3897 3898 queue_type = vq_type(vi, qid); 3899 bitmap = ctx->bitmap[queue_type]; 3900 3901 /* skip the total fields of pairs */ 3902 offset = num_rx + num_tx; 3903 3904 if (queue_type == VIRTNET_Q_TYPE_TX) { 3905 offset += num_cq + num_rx * vi->curr_queue_pairs + num_tx * (qid / 2); 3906 3907 num = ARRAY_SIZE(virtnet_sq_stats_desc); 3908 if (drv_stats) { 3909 desc = &virtnet_sq_stats_desc[0]; 3910 goto drv_stats; 3911 } 3912 3913 offset += num; 3914 3915 } else if (queue_type == VIRTNET_Q_TYPE_RX) { 3916 offset += num_cq + num_rx * (qid / 2); 3917 3918 num = ARRAY_SIZE(virtnet_rq_stats_desc); 3919 if (drv_stats) { 3920 desc = &virtnet_rq_stats_desc[0]; 3921 goto drv_stats; 3922 } 3923 3924 offset += num; 3925 } 3926 3927 if (bitmap & VIRTIO_NET_STATS_TYPE_CVQ) { 3928 desc = &virtnet_stats_cvq_desc[0]; 3929 num = ARRAY_SIZE(virtnet_stats_cvq_desc); 3930 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_CVQ) 3931 goto found; 3932 3933 offset += num; 3934 } 3935 3936 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_BASIC) { 3937 desc = &virtnet_stats_rx_basic_desc[0]; 3938 num = ARRAY_SIZE(virtnet_stats_rx_basic_desc); 3939 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_BASIC) 3940 goto found; 3941 3942 offset += num; 3943 } 3944 3945 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_CSUM) { 3946 desc = &virtnet_stats_rx_csum_desc[0]; 3947 num = ARRAY_SIZE(virtnet_stats_rx_csum_desc); 3948 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_CSUM) 3949 goto found; 3950 3951 offset += num; 3952 } 3953 3954 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_SPEED) { 3955 desc = &virtnet_stats_rx_speed_desc[0]; 3956 num = ARRAY_SIZE(virtnet_stats_rx_speed_desc); 3957 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_SPEED) 3958 goto found; 3959 3960 offset += num; 3961 } 3962 3963 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_BASIC) { 3964 desc = &virtnet_stats_tx_basic_desc[0]; 3965 num = ARRAY_SIZE(virtnet_stats_tx_basic_desc); 3966 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_BASIC) 3967 goto found; 3968 3969 offset += num; 3970 } 3971 3972 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_GSO) { 3973 desc = &virtnet_stats_tx_gso_desc[0]; 3974 num = ARRAY_SIZE(virtnet_stats_tx_gso_desc); 3975 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_GSO) 3976 goto found; 3977 3978 offset += num; 3979 } 3980 3981 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_SPEED) { 3982 desc = &virtnet_stats_tx_speed_desc[0]; 3983 num = ARRAY_SIZE(virtnet_stats_tx_speed_desc); 3984 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_SPEED) 3985 goto found; 3986 3987 offset += num; 3988 } 3989 3990 return; 3991 3992 found: 3993 for (i = 0; i < num; ++i) { 3994 v = (const __le64 *)(base + desc[i].offset); 3995 ctx->data[offset + i] = le64_to_cpu(*v); 3996 } 3997 3998 return; 3999 4000 drv_stats: 4001 for (i = 0; i < num; ++i) { 4002 v_stat = (const u64_stats_t *)(base + desc[i].offset); 4003 ctx->data[offset + i] = u64_stats_read(v_stat); 4004 } 4005 } 4006 4007 static int __virtnet_get_hw_stats(struct virtnet_info *vi, 4008 struct virtnet_stats_ctx *ctx, 4009 struct virtio_net_ctrl_queue_stats *req, 4010 int req_size, void *reply, int res_size) 4011 { 4012 struct virtio_net_stats_reply_hdr *hdr; 4013 struct scatterlist sgs_in, sgs_out; 4014 void *p; 4015 u32 qid; 4016 int ok; 4017 4018 sg_init_one(&sgs_out, req, req_size); 4019 sg_init_one(&sgs_in, reply, res_size); 4020 4021 ok = virtnet_send_command_reply(vi, VIRTIO_NET_CTRL_STATS, 4022 VIRTIO_NET_CTRL_STATS_GET, 4023 &sgs_out, &sgs_in); 4024 4025 if (!ok) 4026 return ok; 4027 4028 for (p = reply; p - reply < res_size; p += le16_to_cpu(hdr->size)) { 4029 hdr = p; 4030 qid = le16_to_cpu(hdr->vq_index); 4031 virtnet_fill_stats(vi, qid, ctx, p, false, hdr->type); 4032 } 4033 4034 return 0; 4035 } 4036 4037 static void virtnet_make_stat_req(struct virtnet_info *vi, 4038 struct virtnet_stats_ctx *ctx, 4039 struct virtio_net_ctrl_queue_stats *req, 4040 int qid, int *idx) 4041 { 4042 int qtype = vq_type(vi, qid); 4043 u64 bitmap = ctx->bitmap[qtype]; 4044 4045 if (!bitmap) 4046 return; 4047 4048 req->stats[*idx].vq_index = cpu_to_le16(qid); 4049 req->stats[*idx].types_bitmap[0] = cpu_to_le64(bitmap); 4050 *idx += 1; 4051 } 4052 4053 /* qid: -1: get stats of all vq. 4054 * > 0: get the stats for the special vq. This must not be cvq. 4055 */ 4056 static int virtnet_get_hw_stats(struct virtnet_info *vi, 4057 struct virtnet_stats_ctx *ctx, int qid) 4058 { 4059 int qnum, i, j, res_size, qtype, last_vq, first_vq; 4060 struct virtio_net_ctrl_queue_stats *req; 4061 bool enable_cvq; 4062 void *reply; 4063 int ok; 4064 4065 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_DEVICE_STATS)) 4066 return 0; 4067 4068 if (qid == -1) { 4069 last_vq = vi->curr_queue_pairs * 2 - 1; 4070 first_vq = 0; 4071 enable_cvq = true; 4072 } else { 4073 last_vq = qid; 4074 first_vq = qid; 4075 enable_cvq = false; 4076 } 4077 4078 qnum = 0; 4079 res_size = 0; 4080 for (i = first_vq; i <= last_vq ; ++i) { 4081 qtype = vq_type(vi, i); 4082 if (ctx->bitmap[qtype]) { 4083 ++qnum; 4084 res_size += ctx->size[qtype]; 4085 } 4086 } 4087 4088 if (enable_cvq && ctx->bitmap[VIRTNET_Q_TYPE_CQ]) { 4089 res_size += ctx->size[VIRTNET_Q_TYPE_CQ]; 4090 qnum += 1; 4091 } 4092 4093 req = kcalloc(qnum, sizeof(*req), GFP_KERNEL); 4094 if (!req) 4095 return -ENOMEM; 4096 4097 reply = kmalloc(res_size, GFP_KERNEL); 4098 if (!reply) { 4099 kfree(req); 4100 return -ENOMEM; 4101 } 4102 4103 j = 0; 4104 for (i = first_vq; i <= last_vq ; ++i) 4105 virtnet_make_stat_req(vi, ctx, req, i, &j); 4106 4107 if (enable_cvq) 4108 virtnet_make_stat_req(vi, ctx, req, vi->max_queue_pairs * 2, &j); 4109 4110 ok = __virtnet_get_hw_stats(vi, ctx, req, sizeof(*req) * j, reply, res_size); 4111 4112 kfree(req); 4113 kfree(reply); 4114 4115 return ok; 4116 } 4117 4118 static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data) 4119 { 4120 struct virtnet_info *vi = netdev_priv(dev); 4121 unsigned int i; 4122 u8 *p = data; 4123 4124 switch (stringset) { 4125 case ETH_SS_STATS: 4126 /* Generate the total field names. */ 4127 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_RX, -1, &p); 4128 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_TX, -1, &p); 4129 4130 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_CQ, 0, &p); 4131 4132 for (i = 0; i < vi->curr_queue_pairs; ++i) 4133 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_RX, i, &p); 4134 4135 for (i = 0; i < vi->curr_queue_pairs; ++i) 4136 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_TX, i, &p); 4137 break; 4138 } 4139 } 4140 4141 static int virtnet_get_sset_count(struct net_device *dev, int sset) 4142 { 4143 struct virtnet_info *vi = netdev_priv(dev); 4144 struct virtnet_stats_ctx ctx = {0}; 4145 u32 pair_count; 4146 4147 switch (sset) { 4148 case ETH_SS_STATS: 4149 virtnet_stats_ctx_init(vi, &ctx, NULL, false); 4150 4151 pair_count = ctx.desc_num[VIRTNET_Q_TYPE_RX] + ctx.desc_num[VIRTNET_Q_TYPE_TX]; 4152 4153 return pair_count + ctx.desc_num[VIRTNET_Q_TYPE_CQ] + 4154 vi->curr_queue_pairs * pair_count; 4155 default: 4156 return -EOPNOTSUPP; 4157 } 4158 } 4159 4160 static void virtnet_get_ethtool_stats(struct net_device *dev, 4161 struct ethtool_stats *stats, u64 *data) 4162 { 4163 struct virtnet_info *vi = netdev_priv(dev); 4164 struct virtnet_stats_ctx ctx = {0}; 4165 unsigned int start, i; 4166 const u8 *stats_base; 4167 4168 virtnet_stats_ctx_init(vi, &ctx, data, false); 4169 if (virtnet_get_hw_stats(vi, &ctx, -1)) 4170 dev_warn(&vi->dev->dev, "Failed to get hw stats.\n"); 4171 4172 for (i = 0; i < vi->curr_queue_pairs; i++) { 4173 struct receive_queue *rq = &vi->rq[i]; 4174 struct send_queue *sq = &vi->sq[i]; 4175 4176 stats_base = (const u8 *)&rq->stats; 4177 do { 4178 start = u64_stats_fetch_begin(&rq->stats.syncp); 4179 virtnet_fill_stats(vi, i * 2, &ctx, stats_base, true, 0); 4180 } while (u64_stats_fetch_retry(&rq->stats.syncp, start)); 4181 4182 stats_base = (const u8 *)&sq->stats; 4183 do { 4184 start = u64_stats_fetch_begin(&sq->stats.syncp); 4185 virtnet_fill_stats(vi, i * 2 + 1, &ctx, stats_base, true, 0); 4186 } while (u64_stats_fetch_retry(&sq->stats.syncp, start)); 4187 } 4188 4189 virtnet_fill_total_fields(vi, &ctx); 4190 } 4191 4192 static void virtnet_get_channels(struct net_device *dev, 4193 struct ethtool_channels *channels) 4194 { 4195 struct virtnet_info *vi = netdev_priv(dev); 4196 4197 channels->combined_count = vi->curr_queue_pairs; 4198 channels->max_combined = vi->max_queue_pairs; 4199 channels->max_other = 0; 4200 channels->rx_count = 0; 4201 channels->tx_count = 0; 4202 channels->other_count = 0; 4203 } 4204 4205 static int virtnet_set_link_ksettings(struct net_device *dev, 4206 const struct ethtool_link_ksettings *cmd) 4207 { 4208 struct virtnet_info *vi = netdev_priv(dev); 4209 4210 return ethtool_virtdev_set_link_ksettings(dev, cmd, 4211 &vi->speed, &vi->duplex); 4212 } 4213 4214 static int virtnet_get_link_ksettings(struct net_device *dev, 4215 struct ethtool_link_ksettings *cmd) 4216 { 4217 struct virtnet_info *vi = netdev_priv(dev); 4218 4219 cmd->base.speed = vi->speed; 4220 cmd->base.duplex = vi->duplex; 4221 cmd->base.port = PORT_OTHER; 4222 4223 return 0; 4224 } 4225 4226 static int virtnet_send_tx_notf_coal_cmds(struct virtnet_info *vi, 4227 struct ethtool_coalesce *ec) 4228 { 4229 struct virtio_net_ctrl_coal_tx *coal_tx __free(kfree) = NULL; 4230 struct scatterlist sgs_tx; 4231 int i; 4232 4233 coal_tx = kzalloc(sizeof(*coal_tx), GFP_KERNEL); 4234 if (!coal_tx) 4235 return -ENOMEM; 4236 4237 coal_tx->tx_usecs = cpu_to_le32(ec->tx_coalesce_usecs); 4238 coal_tx->tx_max_packets = cpu_to_le32(ec->tx_max_coalesced_frames); 4239 sg_init_one(&sgs_tx, coal_tx, sizeof(*coal_tx)); 4240 4241 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL, 4242 VIRTIO_NET_CTRL_NOTF_COAL_TX_SET, 4243 &sgs_tx)) 4244 return -EINVAL; 4245 4246 vi->intr_coal_tx.max_usecs = ec->tx_coalesce_usecs; 4247 vi->intr_coal_tx.max_packets = ec->tx_max_coalesced_frames; 4248 for (i = 0; i < vi->max_queue_pairs; i++) { 4249 vi->sq[i].intr_coal.max_usecs = ec->tx_coalesce_usecs; 4250 vi->sq[i].intr_coal.max_packets = ec->tx_max_coalesced_frames; 4251 } 4252 4253 return 0; 4254 } 4255 4256 static int virtnet_send_rx_notf_coal_cmds(struct virtnet_info *vi, 4257 struct ethtool_coalesce *ec) 4258 { 4259 struct virtio_net_ctrl_coal_rx *coal_rx __free(kfree) = NULL; 4260 bool rx_ctrl_dim_on = !!ec->use_adaptive_rx_coalesce; 4261 struct scatterlist sgs_rx; 4262 int i; 4263 4264 if (rx_ctrl_dim_on && !virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) 4265 return -EOPNOTSUPP; 4266 4267 if (rx_ctrl_dim_on && (ec->rx_coalesce_usecs != vi->intr_coal_rx.max_usecs || 4268 ec->rx_max_coalesced_frames != vi->intr_coal_rx.max_packets)) 4269 return -EINVAL; 4270 4271 if (rx_ctrl_dim_on && !vi->rx_dim_enabled) { 4272 vi->rx_dim_enabled = true; 4273 for (i = 0; i < vi->max_queue_pairs; i++) { 4274 mutex_lock(&vi->rq[i].dim_lock); 4275 vi->rq[i].dim_enabled = true; 4276 mutex_unlock(&vi->rq[i].dim_lock); 4277 } 4278 return 0; 4279 } 4280 4281 coal_rx = kzalloc(sizeof(*coal_rx), GFP_KERNEL); 4282 if (!coal_rx) 4283 return -ENOMEM; 4284 4285 if (!rx_ctrl_dim_on && vi->rx_dim_enabled) { 4286 vi->rx_dim_enabled = false; 4287 for (i = 0; i < vi->max_queue_pairs; i++) { 4288 mutex_lock(&vi->rq[i].dim_lock); 4289 vi->rq[i].dim_enabled = false; 4290 mutex_unlock(&vi->rq[i].dim_lock); 4291 } 4292 } 4293 4294 /* Since the per-queue coalescing params can be set, 4295 * we need apply the global new params even if they 4296 * are not updated. 4297 */ 4298 coal_rx->rx_usecs = cpu_to_le32(ec->rx_coalesce_usecs); 4299 coal_rx->rx_max_packets = cpu_to_le32(ec->rx_max_coalesced_frames); 4300 sg_init_one(&sgs_rx, coal_rx, sizeof(*coal_rx)); 4301 4302 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL, 4303 VIRTIO_NET_CTRL_NOTF_COAL_RX_SET, 4304 &sgs_rx)) 4305 return -EINVAL; 4306 4307 vi->intr_coal_rx.max_usecs = ec->rx_coalesce_usecs; 4308 vi->intr_coal_rx.max_packets = ec->rx_max_coalesced_frames; 4309 for (i = 0; i < vi->max_queue_pairs; i++) { 4310 mutex_lock(&vi->rq[i].dim_lock); 4311 vi->rq[i].intr_coal.max_usecs = ec->rx_coalesce_usecs; 4312 vi->rq[i].intr_coal.max_packets = ec->rx_max_coalesced_frames; 4313 mutex_unlock(&vi->rq[i].dim_lock); 4314 } 4315 4316 return 0; 4317 } 4318 4319 static int virtnet_send_notf_coal_cmds(struct virtnet_info *vi, 4320 struct ethtool_coalesce *ec) 4321 { 4322 int err; 4323 4324 err = virtnet_send_tx_notf_coal_cmds(vi, ec); 4325 if (err) 4326 return err; 4327 4328 err = virtnet_send_rx_notf_coal_cmds(vi, ec); 4329 if (err) 4330 return err; 4331 4332 return 0; 4333 } 4334 4335 static int virtnet_send_rx_notf_coal_vq_cmds(struct virtnet_info *vi, 4336 struct ethtool_coalesce *ec, 4337 u16 queue) 4338 { 4339 bool rx_ctrl_dim_on = !!ec->use_adaptive_rx_coalesce; 4340 u32 max_usecs, max_packets; 4341 bool cur_rx_dim; 4342 int err; 4343 4344 mutex_lock(&vi->rq[queue].dim_lock); 4345 cur_rx_dim = vi->rq[queue].dim_enabled; 4346 max_usecs = vi->rq[queue].intr_coal.max_usecs; 4347 max_packets = vi->rq[queue].intr_coal.max_packets; 4348 4349 if (rx_ctrl_dim_on && (ec->rx_coalesce_usecs != max_usecs || 4350 ec->rx_max_coalesced_frames != max_packets)) { 4351 mutex_unlock(&vi->rq[queue].dim_lock); 4352 return -EINVAL; 4353 } 4354 4355 if (rx_ctrl_dim_on && !cur_rx_dim) { 4356 vi->rq[queue].dim_enabled = true; 4357 mutex_unlock(&vi->rq[queue].dim_lock); 4358 return 0; 4359 } 4360 4361 if (!rx_ctrl_dim_on && cur_rx_dim) 4362 vi->rq[queue].dim_enabled = false; 4363 4364 /* If no params are updated, userspace ethtool will 4365 * reject the modification. 4366 */ 4367 err = virtnet_send_rx_ctrl_coal_vq_cmd(vi, queue, 4368 ec->rx_coalesce_usecs, 4369 ec->rx_max_coalesced_frames); 4370 mutex_unlock(&vi->rq[queue].dim_lock); 4371 return err; 4372 } 4373 4374 static int virtnet_send_notf_coal_vq_cmds(struct virtnet_info *vi, 4375 struct ethtool_coalesce *ec, 4376 u16 queue) 4377 { 4378 int err; 4379 4380 err = virtnet_send_rx_notf_coal_vq_cmds(vi, ec, queue); 4381 if (err) 4382 return err; 4383 4384 err = virtnet_send_tx_ctrl_coal_vq_cmd(vi, queue, 4385 ec->tx_coalesce_usecs, 4386 ec->tx_max_coalesced_frames); 4387 if (err) 4388 return err; 4389 4390 return 0; 4391 } 4392 4393 static void virtnet_rx_dim_work(struct work_struct *work) 4394 { 4395 struct dim *dim = container_of(work, struct dim, work); 4396 struct receive_queue *rq = container_of(dim, 4397 struct receive_queue, dim); 4398 struct virtnet_info *vi = rq->vq->vdev->priv; 4399 struct net_device *dev = vi->dev; 4400 struct dim_cq_moder update_moder; 4401 int qnum, err; 4402 4403 qnum = rq - vi->rq; 4404 4405 mutex_lock(&rq->dim_lock); 4406 if (!rq->dim_enabled) 4407 goto out; 4408 4409 update_moder = net_dim_get_rx_moderation(dim->mode, dim->profile_ix); 4410 if (update_moder.usec != rq->intr_coal.max_usecs || 4411 update_moder.pkts != rq->intr_coal.max_packets) { 4412 err = virtnet_send_rx_ctrl_coal_vq_cmd(vi, qnum, 4413 update_moder.usec, 4414 update_moder.pkts); 4415 if (err) 4416 pr_debug("%s: Failed to send dim parameters on rxq%d\n", 4417 dev->name, qnum); 4418 } 4419 out: 4420 dim->state = DIM_START_MEASURE; 4421 mutex_unlock(&rq->dim_lock); 4422 } 4423 4424 static int virtnet_coal_params_supported(struct ethtool_coalesce *ec) 4425 { 4426 /* usecs coalescing is supported only if VIRTIO_NET_F_NOTF_COAL 4427 * or VIRTIO_NET_F_VQ_NOTF_COAL feature is negotiated. 4428 */ 4429 if (ec->rx_coalesce_usecs || ec->tx_coalesce_usecs) 4430 return -EOPNOTSUPP; 4431 4432 if (ec->tx_max_coalesced_frames > 1 || 4433 ec->rx_max_coalesced_frames != 1) 4434 return -EINVAL; 4435 4436 return 0; 4437 } 4438 4439 static int virtnet_should_update_vq_weight(int dev_flags, int weight, 4440 int vq_weight, bool *should_update) 4441 { 4442 if (weight ^ vq_weight) { 4443 if (dev_flags & IFF_UP) 4444 return -EBUSY; 4445 *should_update = true; 4446 } 4447 4448 return 0; 4449 } 4450 4451 static int virtnet_set_coalesce(struct net_device *dev, 4452 struct ethtool_coalesce *ec, 4453 struct kernel_ethtool_coalesce *kernel_coal, 4454 struct netlink_ext_ack *extack) 4455 { 4456 struct virtnet_info *vi = netdev_priv(dev); 4457 int ret, queue_number, napi_weight; 4458 bool update_napi = false; 4459 4460 /* Can't change NAPI weight if the link is up */ 4461 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0; 4462 for (queue_number = 0; queue_number < vi->max_queue_pairs; queue_number++) { 4463 ret = virtnet_should_update_vq_weight(dev->flags, napi_weight, 4464 vi->sq[queue_number].napi.weight, 4465 &update_napi); 4466 if (ret) 4467 return ret; 4468 4469 if (update_napi) { 4470 /* All queues that belong to [queue_number, vi->max_queue_pairs] will be 4471 * updated for the sake of simplicity, which might not be necessary 4472 */ 4473 break; 4474 } 4475 } 4476 4477 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) 4478 ret = virtnet_send_notf_coal_cmds(vi, ec); 4479 else 4480 ret = virtnet_coal_params_supported(ec); 4481 4482 if (ret) 4483 return ret; 4484 4485 if (update_napi) { 4486 for (; queue_number < vi->max_queue_pairs; queue_number++) 4487 vi->sq[queue_number].napi.weight = napi_weight; 4488 } 4489 4490 return ret; 4491 } 4492 4493 static int virtnet_get_coalesce(struct net_device *dev, 4494 struct ethtool_coalesce *ec, 4495 struct kernel_ethtool_coalesce *kernel_coal, 4496 struct netlink_ext_ack *extack) 4497 { 4498 struct virtnet_info *vi = netdev_priv(dev); 4499 4500 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) { 4501 ec->rx_coalesce_usecs = vi->intr_coal_rx.max_usecs; 4502 ec->tx_coalesce_usecs = vi->intr_coal_tx.max_usecs; 4503 ec->tx_max_coalesced_frames = vi->intr_coal_tx.max_packets; 4504 ec->rx_max_coalesced_frames = vi->intr_coal_rx.max_packets; 4505 ec->use_adaptive_rx_coalesce = vi->rx_dim_enabled; 4506 } else { 4507 ec->rx_max_coalesced_frames = 1; 4508 4509 if (vi->sq[0].napi.weight) 4510 ec->tx_max_coalesced_frames = 1; 4511 } 4512 4513 return 0; 4514 } 4515 4516 static int virtnet_set_per_queue_coalesce(struct net_device *dev, 4517 u32 queue, 4518 struct ethtool_coalesce *ec) 4519 { 4520 struct virtnet_info *vi = netdev_priv(dev); 4521 int ret, napi_weight; 4522 bool update_napi = false; 4523 4524 if (queue >= vi->max_queue_pairs) 4525 return -EINVAL; 4526 4527 /* Can't change NAPI weight if the link is up */ 4528 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0; 4529 ret = virtnet_should_update_vq_weight(dev->flags, napi_weight, 4530 vi->sq[queue].napi.weight, 4531 &update_napi); 4532 if (ret) 4533 return ret; 4534 4535 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) 4536 ret = virtnet_send_notf_coal_vq_cmds(vi, ec, queue); 4537 else 4538 ret = virtnet_coal_params_supported(ec); 4539 4540 if (ret) 4541 return ret; 4542 4543 if (update_napi) 4544 vi->sq[queue].napi.weight = napi_weight; 4545 4546 return 0; 4547 } 4548 4549 static int virtnet_get_per_queue_coalesce(struct net_device *dev, 4550 u32 queue, 4551 struct ethtool_coalesce *ec) 4552 { 4553 struct virtnet_info *vi = netdev_priv(dev); 4554 4555 if (queue >= vi->max_queue_pairs) 4556 return -EINVAL; 4557 4558 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) { 4559 mutex_lock(&vi->rq[queue].dim_lock); 4560 ec->rx_coalesce_usecs = vi->rq[queue].intr_coal.max_usecs; 4561 ec->tx_coalesce_usecs = vi->sq[queue].intr_coal.max_usecs; 4562 ec->tx_max_coalesced_frames = vi->sq[queue].intr_coal.max_packets; 4563 ec->rx_max_coalesced_frames = vi->rq[queue].intr_coal.max_packets; 4564 ec->use_adaptive_rx_coalesce = vi->rq[queue].dim_enabled; 4565 mutex_unlock(&vi->rq[queue].dim_lock); 4566 } else { 4567 ec->rx_max_coalesced_frames = 1; 4568 4569 if (vi->sq[queue].napi.weight) 4570 ec->tx_max_coalesced_frames = 1; 4571 } 4572 4573 return 0; 4574 } 4575 4576 static void virtnet_init_settings(struct net_device *dev) 4577 { 4578 struct virtnet_info *vi = netdev_priv(dev); 4579 4580 vi->speed = SPEED_UNKNOWN; 4581 vi->duplex = DUPLEX_UNKNOWN; 4582 } 4583 4584 static void virtnet_update_settings(struct virtnet_info *vi) 4585 { 4586 u32 speed; 4587 u8 duplex; 4588 4589 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX)) 4590 return; 4591 4592 virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed); 4593 4594 if (ethtool_validate_speed(speed)) 4595 vi->speed = speed; 4596 4597 virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex); 4598 4599 if (ethtool_validate_duplex(duplex)) 4600 vi->duplex = duplex; 4601 } 4602 4603 static u32 virtnet_get_rxfh_key_size(struct net_device *dev) 4604 { 4605 return ((struct virtnet_info *)netdev_priv(dev))->rss_key_size; 4606 } 4607 4608 static u32 virtnet_get_rxfh_indir_size(struct net_device *dev) 4609 { 4610 return ((struct virtnet_info *)netdev_priv(dev))->rss_indir_table_size; 4611 } 4612 4613 static int virtnet_get_rxfh(struct net_device *dev, 4614 struct ethtool_rxfh_param *rxfh) 4615 { 4616 struct virtnet_info *vi = netdev_priv(dev); 4617 int i; 4618 4619 if (rxfh->indir) { 4620 for (i = 0; i < vi->rss_indir_table_size; ++i) 4621 rxfh->indir[i] = vi->rss.indirection_table[i]; 4622 } 4623 4624 if (rxfh->key) 4625 memcpy(rxfh->key, vi->rss.key, vi->rss_key_size); 4626 4627 rxfh->hfunc = ETH_RSS_HASH_TOP; 4628 4629 return 0; 4630 } 4631 4632 static int virtnet_set_rxfh(struct net_device *dev, 4633 struct ethtool_rxfh_param *rxfh, 4634 struct netlink_ext_ack *extack) 4635 { 4636 struct virtnet_info *vi = netdev_priv(dev); 4637 bool update = false; 4638 int i; 4639 4640 if (rxfh->hfunc != ETH_RSS_HASH_NO_CHANGE && 4641 rxfh->hfunc != ETH_RSS_HASH_TOP) 4642 return -EOPNOTSUPP; 4643 4644 if (rxfh->indir) { 4645 if (!vi->has_rss) 4646 return -EOPNOTSUPP; 4647 4648 for (i = 0; i < vi->rss_indir_table_size; ++i) 4649 vi->rss.indirection_table[i] = rxfh->indir[i]; 4650 update = true; 4651 } 4652 4653 if (rxfh->key) { 4654 /* If either _F_HASH_REPORT or _F_RSS are negotiated, the 4655 * device provides hash calculation capabilities, that is, 4656 * hash_key is configured. 4657 */ 4658 if (!vi->has_rss && !vi->has_rss_hash_report) 4659 return -EOPNOTSUPP; 4660 4661 memcpy(vi->rss.key, rxfh->key, vi->rss_key_size); 4662 update = true; 4663 } 4664 4665 if (update) 4666 virtnet_commit_rss_command(vi); 4667 4668 return 0; 4669 } 4670 4671 static int virtnet_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info, u32 *rule_locs) 4672 { 4673 struct virtnet_info *vi = netdev_priv(dev); 4674 int rc = 0; 4675 4676 switch (info->cmd) { 4677 case ETHTOOL_GRXRINGS: 4678 info->data = vi->curr_queue_pairs; 4679 break; 4680 case ETHTOOL_GRXFH: 4681 virtnet_get_hashflow(vi, info); 4682 break; 4683 default: 4684 rc = -EOPNOTSUPP; 4685 } 4686 4687 return rc; 4688 } 4689 4690 static int virtnet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info) 4691 { 4692 struct virtnet_info *vi = netdev_priv(dev); 4693 int rc = 0; 4694 4695 switch (info->cmd) { 4696 case ETHTOOL_SRXFH: 4697 if (!virtnet_set_hashflow(vi, info)) 4698 rc = -EINVAL; 4699 4700 break; 4701 default: 4702 rc = -EOPNOTSUPP; 4703 } 4704 4705 return rc; 4706 } 4707 4708 static const struct ethtool_ops virtnet_ethtool_ops = { 4709 .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES | 4710 ETHTOOL_COALESCE_USECS | ETHTOOL_COALESCE_USE_ADAPTIVE_RX, 4711 .get_drvinfo = virtnet_get_drvinfo, 4712 .get_link = ethtool_op_get_link, 4713 .get_ringparam = virtnet_get_ringparam, 4714 .set_ringparam = virtnet_set_ringparam, 4715 .get_strings = virtnet_get_strings, 4716 .get_sset_count = virtnet_get_sset_count, 4717 .get_ethtool_stats = virtnet_get_ethtool_stats, 4718 .set_channels = virtnet_set_channels, 4719 .get_channels = virtnet_get_channels, 4720 .get_ts_info = ethtool_op_get_ts_info, 4721 .get_link_ksettings = virtnet_get_link_ksettings, 4722 .set_link_ksettings = virtnet_set_link_ksettings, 4723 .set_coalesce = virtnet_set_coalesce, 4724 .get_coalesce = virtnet_get_coalesce, 4725 .set_per_queue_coalesce = virtnet_set_per_queue_coalesce, 4726 .get_per_queue_coalesce = virtnet_get_per_queue_coalesce, 4727 .get_rxfh_key_size = virtnet_get_rxfh_key_size, 4728 .get_rxfh_indir_size = virtnet_get_rxfh_indir_size, 4729 .get_rxfh = virtnet_get_rxfh, 4730 .set_rxfh = virtnet_set_rxfh, 4731 .get_rxnfc = virtnet_get_rxnfc, 4732 .set_rxnfc = virtnet_set_rxnfc, 4733 }; 4734 4735 static void virtnet_get_queue_stats_rx(struct net_device *dev, int i, 4736 struct netdev_queue_stats_rx *stats) 4737 { 4738 struct virtnet_info *vi = netdev_priv(dev); 4739 struct receive_queue *rq = &vi->rq[i]; 4740 struct virtnet_stats_ctx ctx = {0}; 4741 4742 virtnet_stats_ctx_init(vi, &ctx, (void *)stats, true); 4743 4744 virtnet_get_hw_stats(vi, &ctx, i * 2); 4745 virtnet_fill_stats(vi, i * 2, &ctx, (void *)&rq->stats, true, 0); 4746 } 4747 4748 static void virtnet_get_queue_stats_tx(struct net_device *dev, int i, 4749 struct netdev_queue_stats_tx *stats) 4750 { 4751 struct virtnet_info *vi = netdev_priv(dev); 4752 struct send_queue *sq = &vi->sq[i]; 4753 struct virtnet_stats_ctx ctx = {0}; 4754 4755 virtnet_stats_ctx_init(vi, &ctx, (void *)stats, true); 4756 4757 virtnet_get_hw_stats(vi, &ctx, i * 2 + 1); 4758 virtnet_fill_stats(vi, i * 2 + 1, &ctx, (void *)&sq->stats, true, 0); 4759 } 4760 4761 static void virtnet_get_base_stats(struct net_device *dev, 4762 struct netdev_queue_stats_rx *rx, 4763 struct netdev_queue_stats_tx *tx) 4764 { 4765 struct virtnet_info *vi = netdev_priv(dev); 4766 4767 /* The queue stats of the virtio-net will not be reset. So here we 4768 * return 0. 4769 */ 4770 rx->bytes = 0; 4771 rx->packets = 0; 4772 4773 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) { 4774 rx->hw_drops = 0; 4775 rx->hw_drop_overruns = 0; 4776 } 4777 4778 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) { 4779 rx->csum_unnecessary = 0; 4780 rx->csum_none = 0; 4781 rx->csum_bad = 0; 4782 } 4783 4784 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_GSO) { 4785 rx->hw_gro_packets = 0; 4786 rx->hw_gro_bytes = 0; 4787 rx->hw_gro_wire_packets = 0; 4788 rx->hw_gro_wire_bytes = 0; 4789 } 4790 4791 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED) 4792 rx->hw_drop_ratelimits = 0; 4793 4794 tx->bytes = 0; 4795 tx->packets = 0; 4796 tx->stop = 0; 4797 tx->wake = 0; 4798 4799 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) { 4800 tx->hw_drops = 0; 4801 tx->hw_drop_errors = 0; 4802 } 4803 4804 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_CSUM) { 4805 tx->csum_none = 0; 4806 tx->needs_csum = 0; 4807 } 4808 4809 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) { 4810 tx->hw_gso_packets = 0; 4811 tx->hw_gso_bytes = 0; 4812 tx->hw_gso_wire_packets = 0; 4813 tx->hw_gso_wire_bytes = 0; 4814 } 4815 4816 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED) 4817 tx->hw_drop_ratelimits = 0; 4818 } 4819 4820 static const struct netdev_stat_ops virtnet_stat_ops = { 4821 .get_queue_stats_rx = virtnet_get_queue_stats_rx, 4822 .get_queue_stats_tx = virtnet_get_queue_stats_tx, 4823 .get_base_stats = virtnet_get_base_stats, 4824 }; 4825 4826 static void virtnet_freeze_down(struct virtio_device *vdev) 4827 { 4828 struct virtnet_info *vi = vdev->priv; 4829 4830 /* Make sure no work handler is accessing the device */ 4831 flush_work(&vi->config_work); 4832 disable_rx_mode_work(vi); 4833 flush_work(&vi->rx_mode_work); 4834 4835 netif_tx_lock_bh(vi->dev); 4836 netif_device_detach(vi->dev); 4837 netif_tx_unlock_bh(vi->dev); 4838 if (netif_running(vi->dev)) 4839 virtnet_close(vi->dev); 4840 } 4841 4842 static int init_vqs(struct virtnet_info *vi); 4843 4844 static int virtnet_restore_up(struct virtio_device *vdev) 4845 { 4846 struct virtnet_info *vi = vdev->priv; 4847 int err; 4848 4849 err = init_vqs(vi); 4850 if (err) 4851 return err; 4852 4853 virtio_device_ready(vdev); 4854 4855 enable_delayed_refill(vi); 4856 enable_rx_mode_work(vi); 4857 4858 if (netif_running(vi->dev)) { 4859 err = virtnet_open(vi->dev); 4860 if (err) 4861 return err; 4862 } 4863 4864 netif_tx_lock_bh(vi->dev); 4865 netif_device_attach(vi->dev); 4866 netif_tx_unlock_bh(vi->dev); 4867 return err; 4868 } 4869 4870 static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads) 4871 { 4872 __virtio64 *_offloads __free(kfree) = NULL; 4873 struct scatterlist sg; 4874 4875 _offloads = kzalloc(sizeof(*_offloads), GFP_KERNEL); 4876 if (!_offloads) 4877 return -ENOMEM; 4878 4879 *_offloads = cpu_to_virtio64(vi->vdev, offloads); 4880 4881 sg_init_one(&sg, _offloads, sizeof(*_offloads)); 4882 4883 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS, 4884 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) { 4885 dev_warn(&vi->dev->dev, "Fail to set guest offload.\n"); 4886 return -EINVAL; 4887 } 4888 4889 return 0; 4890 } 4891 4892 static int virtnet_clear_guest_offloads(struct virtnet_info *vi) 4893 { 4894 u64 offloads = 0; 4895 4896 if (!vi->guest_offloads) 4897 return 0; 4898 4899 return virtnet_set_guest_offloads(vi, offloads); 4900 } 4901 4902 static int virtnet_restore_guest_offloads(struct virtnet_info *vi) 4903 { 4904 u64 offloads = vi->guest_offloads; 4905 4906 if (!vi->guest_offloads) 4907 return 0; 4908 4909 return virtnet_set_guest_offloads(vi, offloads); 4910 } 4911 4912 static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog, 4913 struct netlink_ext_ack *extack) 4914 { 4915 unsigned int room = SKB_DATA_ALIGN(VIRTIO_XDP_HEADROOM + 4916 sizeof(struct skb_shared_info)); 4917 unsigned int max_sz = PAGE_SIZE - room - ETH_HLEN; 4918 struct virtnet_info *vi = netdev_priv(dev); 4919 struct bpf_prog *old_prog; 4920 u16 xdp_qp = 0, curr_qp; 4921 int i, err; 4922 4923 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) 4924 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) || 4925 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) || 4926 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) || 4927 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) || 4928 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM) || 4929 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) || 4930 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6))) { 4931 NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing GRO_HW/CSUM, disable GRO_HW/CSUM first"); 4932 return -EOPNOTSUPP; 4933 } 4934 4935 if (vi->mergeable_rx_bufs && !vi->any_header_sg) { 4936 NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required"); 4937 return -EINVAL; 4938 } 4939 4940 if (prog && !prog->aux->xdp_has_frags && dev->mtu > max_sz) { 4941 NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP without frags"); 4942 netdev_warn(dev, "single-buffer XDP requires MTU less than %u\n", max_sz); 4943 return -EINVAL; 4944 } 4945 4946 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs; 4947 if (prog) 4948 xdp_qp = nr_cpu_ids; 4949 4950 /* XDP requires extra queues for XDP_TX */ 4951 if (curr_qp + xdp_qp > vi->max_queue_pairs) { 4952 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", 4953 curr_qp + xdp_qp, vi->max_queue_pairs); 4954 xdp_qp = 0; 4955 } 4956 4957 old_prog = rtnl_dereference(vi->rq[0].xdp_prog); 4958 if (!prog && !old_prog) 4959 return 0; 4960 4961 if (prog) 4962 bpf_prog_add(prog, vi->max_queue_pairs - 1); 4963 4964 /* Make sure NAPI is not using any XDP TX queues for RX. */ 4965 if (netif_running(dev)) { 4966 for (i = 0; i < vi->max_queue_pairs; i++) { 4967 napi_disable(&vi->rq[i].napi); 4968 virtnet_napi_tx_disable(&vi->sq[i].napi); 4969 } 4970 } 4971 4972 if (!prog) { 4973 for (i = 0; i < vi->max_queue_pairs; i++) { 4974 rcu_assign_pointer(vi->rq[i].xdp_prog, prog); 4975 if (i == 0) 4976 virtnet_restore_guest_offloads(vi); 4977 } 4978 synchronize_net(); 4979 } 4980 4981 err = virtnet_set_queues(vi, curr_qp + xdp_qp); 4982 if (err) 4983 goto err; 4984 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp); 4985 vi->xdp_queue_pairs = xdp_qp; 4986 4987 if (prog) { 4988 vi->xdp_enabled = true; 4989 for (i = 0; i < vi->max_queue_pairs; i++) { 4990 rcu_assign_pointer(vi->rq[i].xdp_prog, prog); 4991 if (i == 0 && !old_prog) 4992 virtnet_clear_guest_offloads(vi); 4993 } 4994 if (!old_prog) 4995 xdp_features_set_redirect_target(dev, true); 4996 } else { 4997 xdp_features_clear_redirect_target(dev); 4998 vi->xdp_enabled = false; 4999 } 5000 5001 for (i = 0; i < vi->max_queue_pairs; i++) { 5002 if (old_prog) 5003 bpf_prog_put(old_prog); 5004 if (netif_running(dev)) { 5005 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 5006 virtnet_napi_tx_enable(vi, vi->sq[i].vq, 5007 &vi->sq[i].napi); 5008 } 5009 } 5010 5011 return 0; 5012 5013 err: 5014 if (!prog) { 5015 virtnet_clear_guest_offloads(vi); 5016 for (i = 0; i < vi->max_queue_pairs; i++) 5017 rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog); 5018 } 5019 5020 if (netif_running(dev)) { 5021 for (i = 0; i < vi->max_queue_pairs; i++) { 5022 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 5023 virtnet_napi_tx_enable(vi, vi->sq[i].vq, 5024 &vi->sq[i].napi); 5025 } 5026 } 5027 if (prog) 5028 bpf_prog_sub(prog, vi->max_queue_pairs - 1); 5029 return err; 5030 } 5031 5032 static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp) 5033 { 5034 switch (xdp->command) { 5035 case XDP_SETUP_PROG: 5036 return virtnet_xdp_set(dev, xdp->prog, xdp->extack); 5037 default: 5038 return -EINVAL; 5039 } 5040 } 5041 5042 static int virtnet_get_phys_port_name(struct net_device *dev, char *buf, 5043 size_t len) 5044 { 5045 struct virtnet_info *vi = netdev_priv(dev); 5046 int ret; 5047 5048 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY)) 5049 return -EOPNOTSUPP; 5050 5051 ret = snprintf(buf, len, "sby"); 5052 if (ret >= len) 5053 return -EOPNOTSUPP; 5054 5055 return 0; 5056 } 5057 5058 static int virtnet_set_features(struct net_device *dev, 5059 netdev_features_t features) 5060 { 5061 struct virtnet_info *vi = netdev_priv(dev); 5062 u64 offloads; 5063 int err; 5064 5065 if ((dev->features ^ features) & NETIF_F_GRO_HW) { 5066 if (vi->xdp_enabled) 5067 return -EBUSY; 5068 5069 if (features & NETIF_F_GRO_HW) 5070 offloads = vi->guest_offloads_capable; 5071 else 5072 offloads = vi->guest_offloads_capable & 5073 ~GUEST_OFFLOAD_GRO_HW_MASK; 5074 5075 err = virtnet_set_guest_offloads(vi, offloads); 5076 if (err) 5077 return err; 5078 vi->guest_offloads = offloads; 5079 } 5080 5081 if ((dev->features ^ features) & NETIF_F_RXHASH) { 5082 if (features & NETIF_F_RXHASH) 5083 vi->rss.hash_types = vi->rss_hash_types_saved; 5084 else 5085 vi->rss.hash_types = VIRTIO_NET_HASH_REPORT_NONE; 5086 5087 if (!virtnet_commit_rss_command(vi)) 5088 return -EINVAL; 5089 } 5090 5091 return 0; 5092 } 5093 5094 static void virtnet_tx_timeout(struct net_device *dev, unsigned int txqueue) 5095 { 5096 struct virtnet_info *priv = netdev_priv(dev); 5097 struct send_queue *sq = &priv->sq[txqueue]; 5098 struct netdev_queue *txq = netdev_get_tx_queue(dev, txqueue); 5099 5100 u64_stats_update_begin(&sq->stats.syncp); 5101 u64_stats_inc(&sq->stats.tx_timeouts); 5102 u64_stats_update_end(&sq->stats.syncp); 5103 5104 netdev_err(dev, "TX timeout on queue: %u, sq: %s, vq: 0x%x, name: %s, %u usecs ago\n", 5105 txqueue, sq->name, sq->vq->index, sq->vq->name, 5106 jiffies_to_usecs(jiffies - READ_ONCE(txq->trans_start))); 5107 } 5108 5109 static const struct net_device_ops virtnet_netdev = { 5110 .ndo_open = virtnet_open, 5111 .ndo_stop = virtnet_close, 5112 .ndo_start_xmit = start_xmit, 5113 .ndo_validate_addr = eth_validate_addr, 5114 .ndo_set_mac_address = virtnet_set_mac_address, 5115 .ndo_set_rx_mode = virtnet_set_rx_mode, 5116 .ndo_get_stats64 = virtnet_stats, 5117 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, 5118 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, 5119 .ndo_bpf = virtnet_xdp, 5120 .ndo_xdp_xmit = virtnet_xdp_xmit, 5121 .ndo_features_check = passthru_features_check, 5122 .ndo_get_phys_port_name = virtnet_get_phys_port_name, 5123 .ndo_set_features = virtnet_set_features, 5124 .ndo_tx_timeout = virtnet_tx_timeout, 5125 }; 5126 5127 static void virtnet_config_changed_work(struct work_struct *work) 5128 { 5129 struct virtnet_info *vi = 5130 container_of(work, struct virtnet_info, config_work); 5131 u16 v; 5132 5133 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS, 5134 struct virtio_net_config, status, &v) < 0) 5135 return; 5136 5137 if (v & VIRTIO_NET_S_ANNOUNCE) { 5138 netdev_notify_peers(vi->dev); 5139 virtnet_ack_link_announce(vi); 5140 } 5141 5142 /* Ignore unknown (future) status bits */ 5143 v &= VIRTIO_NET_S_LINK_UP; 5144 5145 if (vi->status == v) 5146 return; 5147 5148 vi->status = v; 5149 5150 if (vi->status & VIRTIO_NET_S_LINK_UP) { 5151 virtnet_update_settings(vi); 5152 netif_carrier_on(vi->dev); 5153 netif_tx_wake_all_queues(vi->dev); 5154 } else { 5155 netif_carrier_off(vi->dev); 5156 netif_tx_stop_all_queues(vi->dev); 5157 } 5158 } 5159 5160 static void virtnet_config_changed(struct virtio_device *vdev) 5161 { 5162 struct virtnet_info *vi = vdev->priv; 5163 5164 schedule_work(&vi->config_work); 5165 } 5166 5167 static void virtnet_free_queues(struct virtnet_info *vi) 5168 { 5169 int i; 5170 5171 for (i = 0; i < vi->max_queue_pairs; i++) { 5172 __netif_napi_del(&vi->rq[i].napi); 5173 __netif_napi_del(&vi->sq[i].napi); 5174 } 5175 5176 /* We called __netif_napi_del(), 5177 * we need to respect an RCU grace period before freeing vi->rq 5178 */ 5179 synchronize_net(); 5180 5181 kfree(vi->rq); 5182 kfree(vi->sq); 5183 kfree(vi->ctrl); 5184 } 5185 5186 static void _free_receive_bufs(struct virtnet_info *vi) 5187 { 5188 struct bpf_prog *old_prog; 5189 int i; 5190 5191 for (i = 0; i < vi->max_queue_pairs; i++) { 5192 while (vi->rq[i].pages) 5193 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0); 5194 5195 old_prog = rtnl_dereference(vi->rq[i].xdp_prog); 5196 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL); 5197 if (old_prog) 5198 bpf_prog_put(old_prog); 5199 } 5200 } 5201 5202 static void free_receive_bufs(struct virtnet_info *vi) 5203 { 5204 rtnl_lock(); 5205 _free_receive_bufs(vi); 5206 rtnl_unlock(); 5207 } 5208 5209 static void free_receive_page_frags(struct virtnet_info *vi) 5210 { 5211 int i; 5212 for (i = 0; i < vi->max_queue_pairs; i++) 5213 if (vi->rq[i].alloc_frag.page) { 5214 if (vi->rq[i].last_dma) 5215 virtnet_rq_unmap(&vi->rq[i], vi->rq[i].last_dma, 0); 5216 put_page(vi->rq[i].alloc_frag.page); 5217 } 5218 } 5219 5220 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf) 5221 { 5222 if (!is_xdp_frame(buf)) 5223 dev_kfree_skb(buf); 5224 else 5225 xdp_return_frame(ptr_to_xdp(buf)); 5226 } 5227 5228 static void free_unused_bufs(struct virtnet_info *vi) 5229 { 5230 void *buf; 5231 int i; 5232 5233 for (i = 0; i < vi->max_queue_pairs; i++) { 5234 struct virtqueue *vq = vi->sq[i].vq; 5235 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) 5236 virtnet_sq_free_unused_buf(vq, buf); 5237 cond_resched(); 5238 } 5239 5240 for (i = 0; i < vi->max_queue_pairs; i++) { 5241 struct virtqueue *vq = vi->rq[i].vq; 5242 5243 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) 5244 virtnet_rq_unmap_free_buf(vq, buf); 5245 cond_resched(); 5246 } 5247 } 5248 5249 static void virtnet_del_vqs(struct virtnet_info *vi) 5250 { 5251 struct virtio_device *vdev = vi->vdev; 5252 5253 virtnet_clean_affinity(vi); 5254 5255 vdev->config->del_vqs(vdev); 5256 5257 virtnet_free_queues(vi); 5258 } 5259 5260 /* How large should a single buffer be so a queue full of these can fit at 5261 * least one full packet? 5262 * Logic below assumes the mergeable buffer header is used. 5263 */ 5264 static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq) 5265 { 5266 const unsigned int hdr_len = vi->hdr_len; 5267 unsigned int rq_size = virtqueue_get_vring_size(vq); 5268 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu; 5269 unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len; 5270 unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size); 5271 5272 return max(max(min_buf_len, hdr_len) - hdr_len, 5273 (unsigned int)GOOD_PACKET_LEN); 5274 } 5275 5276 static int virtnet_find_vqs(struct virtnet_info *vi) 5277 { 5278 vq_callback_t **callbacks; 5279 struct virtqueue **vqs; 5280 const char **names; 5281 int ret = -ENOMEM; 5282 int total_vqs; 5283 bool *ctx; 5284 u16 i; 5285 5286 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by 5287 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by 5288 * possible control vq. 5289 */ 5290 total_vqs = vi->max_queue_pairs * 2 + 5291 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ); 5292 5293 /* Allocate space for find_vqs parameters */ 5294 vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL); 5295 if (!vqs) 5296 goto err_vq; 5297 callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL); 5298 if (!callbacks) 5299 goto err_callback; 5300 names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL); 5301 if (!names) 5302 goto err_names; 5303 if (!vi->big_packets || vi->mergeable_rx_bufs) { 5304 ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL); 5305 if (!ctx) 5306 goto err_ctx; 5307 } else { 5308 ctx = NULL; 5309 } 5310 5311 /* Parameters for control virtqueue, if any */ 5312 if (vi->has_cvq) { 5313 callbacks[total_vqs - 1] = NULL; 5314 names[total_vqs - 1] = "control"; 5315 } 5316 5317 /* Allocate/initialize parameters for send/receive virtqueues */ 5318 for (i = 0; i < vi->max_queue_pairs; i++) { 5319 callbacks[rxq2vq(i)] = skb_recv_done; 5320 callbacks[txq2vq(i)] = skb_xmit_done; 5321 sprintf(vi->rq[i].name, "input.%u", i); 5322 sprintf(vi->sq[i].name, "output.%u", i); 5323 names[rxq2vq(i)] = vi->rq[i].name; 5324 names[txq2vq(i)] = vi->sq[i].name; 5325 if (ctx) 5326 ctx[rxq2vq(i)] = true; 5327 } 5328 5329 ret = virtio_find_vqs_ctx(vi->vdev, total_vqs, vqs, callbacks, 5330 names, ctx, NULL); 5331 if (ret) 5332 goto err_find; 5333 5334 if (vi->has_cvq) { 5335 vi->cvq = vqs[total_vqs - 1]; 5336 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) 5337 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 5338 } 5339 5340 for (i = 0; i < vi->max_queue_pairs; i++) { 5341 vi->rq[i].vq = vqs[rxq2vq(i)]; 5342 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq); 5343 vi->sq[i].vq = vqs[txq2vq(i)]; 5344 } 5345 5346 /* run here: ret == 0. */ 5347 5348 5349 err_find: 5350 kfree(ctx); 5351 err_ctx: 5352 kfree(names); 5353 err_names: 5354 kfree(callbacks); 5355 err_callback: 5356 kfree(vqs); 5357 err_vq: 5358 return ret; 5359 } 5360 5361 static int virtnet_alloc_queues(struct virtnet_info *vi) 5362 { 5363 int i; 5364 5365 if (vi->has_cvq) { 5366 vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL); 5367 if (!vi->ctrl) 5368 goto err_ctrl; 5369 } else { 5370 vi->ctrl = NULL; 5371 } 5372 vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL); 5373 if (!vi->sq) 5374 goto err_sq; 5375 vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL); 5376 if (!vi->rq) 5377 goto err_rq; 5378 5379 INIT_DELAYED_WORK(&vi->refill, refill_work); 5380 for (i = 0; i < vi->max_queue_pairs; i++) { 5381 vi->rq[i].pages = NULL; 5382 netif_napi_add_weight(vi->dev, &vi->rq[i].napi, virtnet_poll, 5383 napi_weight); 5384 netif_napi_add_tx_weight(vi->dev, &vi->sq[i].napi, 5385 virtnet_poll_tx, 5386 napi_tx ? napi_weight : 0); 5387 5388 INIT_WORK(&vi->rq[i].dim.work, virtnet_rx_dim_work); 5389 vi->rq[i].dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE; 5390 5391 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg)); 5392 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len); 5393 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg)); 5394 5395 u64_stats_init(&vi->rq[i].stats.syncp); 5396 u64_stats_init(&vi->sq[i].stats.syncp); 5397 mutex_init(&vi->rq[i].dim_lock); 5398 } 5399 5400 return 0; 5401 5402 err_rq: 5403 kfree(vi->sq); 5404 err_sq: 5405 kfree(vi->ctrl); 5406 err_ctrl: 5407 return -ENOMEM; 5408 } 5409 5410 static int init_vqs(struct virtnet_info *vi) 5411 { 5412 int ret; 5413 5414 /* Allocate send & receive queues */ 5415 ret = virtnet_alloc_queues(vi); 5416 if (ret) 5417 goto err; 5418 5419 ret = virtnet_find_vqs(vi); 5420 if (ret) 5421 goto err_free; 5422 5423 virtnet_rq_set_premapped(vi); 5424 5425 cpus_read_lock(); 5426 virtnet_set_affinity(vi); 5427 cpus_read_unlock(); 5428 5429 return 0; 5430 5431 err_free: 5432 virtnet_free_queues(vi); 5433 err: 5434 return ret; 5435 } 5436 5437 #ifdef CONFIG_SYSFS 5438 static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue, 5439 char *buf) 5440 { 5441 struct virtnet_info *vi = netdev_priv(queue->dev); 5442 unsigned int queue_index = get_netdev_rx_queue_index(queue); 5443 unsigned int headroom = virtnet_get_headroom(vi); 5444 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 5445 struct ewma_pkt_len *avg; 5446 5447 BUG_ON(queue_index >= vi->max_queue_pairs); 5448 avg = &vi->rq[queue_index].mrg_avg_pkt_len; 5449 return sprintf(buf, "%u\n", 5450 get_mergeable_buf_len(&vi->rq[queue_index], avg, 5451 SKB_DATA_ALIGN(headroom + tailroom))); 5452 } 5453 5454 static struct rx_queue_attribute mergeable_rx_buffer_size_attribute = 5455 __ATTR_RO(mergeable_rx_buffer_size); 5456 5457 static struct attribute *virtio_net_mrg_rx_attrs[] = { 5458 &mergeable_rx_buffer_size_attribute.attr, 5459 NULL 5460 }; 5461 5462 static const struct attribute_group virtio_net_mrg_rx_group = { 5463 .name = "virtio_net", 5464 .attrs = virtio_net_mrg_rx_attrs 5465 }; 5466 #endif 5467 5468 static bool virtnet_fail_on_feature(struct virtio_device *vdev, 5469 unsigned int fbit, 5470 const char *fname, const char *dname) 5471 { 5472 if (!virtio_has_feature(vdev, fbit)) 5473 return false; 5474 5475 dev_err(&vdev->dev, "device advertises feature %s but not %s", 5476 fname, dname); 5477 5478 return true; 5479 } 5480 5481 #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \ 5482 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit) 5483 5484 static bool virtnet_validate_features(struct virtio_device *vdev) 5485 { 5486 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) && 5487 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX, 5488 "VIRTIO_NET_F_CTRL_VQ") || 5489 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN, 5490 "VIRTIO_NET_F_CTRL_VQ") || 5491 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE, 5492 "VIRTIO_NET_F_CTRL_VQ") || 5493 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") || 5494 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR, 5495 "VIRTIO_NET_F_CTRL_VQ") || 5496 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_RSS, 5497 "VIRTIO_NET_F_CTRL_VQ") || 5498 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_HASH_REPORT, 5499 "VIRTIO_NET_F_CTRL_VQ") || 5500 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_NOTF_COAL, 5501 "VIRTIO_NET_F_CTRL_VQ") || 5502 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_VQ_NOTF_COAL, 5503 "VIRTIO_NET_F_CTRL_VQ"))) { 5504 return false; 5505 } 5506 5507 return true; 5508 } 5509 5510 #define MIN_MTU ETH_MIN_MTU 5511 #define MAX_MTU ETH_MAX_MTU 5512 5513 static int virtnet_validate(struct virtio_device *vdev) 5514 { 5515 if (!vdev->config->get) { 5516 dev_err(&vdev->dev, "%s failure: config access disabled\n", 5517 __func__); 5518 return -EINVAL; 5519 } 5520 5521 if (!virtnet_validate_features(vdev)) 5522 return -EINVAL; 5523 5524 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 5525 int mtu = virtio_cread16(vdev, 5526 offsetof(struct virtio_net_config, 5527 mtu)); 5528 if (mtu < MIN_MTU) 5529 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU); 5530 } 5531 5532 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY) && 5533 !virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) { 5534 dev_warn(&vdev->dev, "device advertises feature VIRTIO_NET_F_STANDBY but not VIRTIO_NET_F_MAC, disabling standby"); 5535 __virtio_clear_bit(vdev, VIRTIO_NET_F_STANDBY); 5536 } 5537 5538 return 0; 5539 } 5540 5541 static bool virtnet_check_guest_gso(const struct virtnet_info *vi) 5542 { 5543 return virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) || 5544 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) || 5545 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) || 5546 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) || 5547 (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) && 5548 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6)); 5549 } 5550 5551 static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu) 5552 { 5553 bool guest_gso = virtnet_check_guest_gso(vi); 5554 5555 /* If device can receive ANY guest GSO packets, regardless of mtu, 5556 * allocate packets of maximum size, otherwise limit it to only 5557 * mtu size worth only. 5558 */ 5559 if (mtu > ETH_DATA_LEN || guest_gso) { 5560 vi->big_packets = true; 5561 vi->big_packets_num_skbfrags = guest_gso ? MAX_SKB_FRAGS : DIV_ROUND_UP(mtu, PAGE_SIZE); 5562 } 5563 } 5564 5565 #define VIRTIO_NET_HASH_REPORT_MAX_TABLE 10 5566 static enum xdp_rss_hash_type 5567 virtnet_xdp_rss_type[VIRTIO_NET_HASH_REPORT_MAX_TABLE] = { 5568 [VIRTIO_NET_HASH_REPORT_NONE] = XDP_RSS_TYPE_NONE, 5569 [VIRTIO_NET_HASH_REPORT_IPv4] = XDP_RSS_TYPE_L3_IPV4, 5570 [VIRTIO_NET_HASH_REPORT_TCPv4] = XDP_RSS_TYPE_L4_IPV4_TCP, 5571 [VIRTIO_NET_HASH_REPORT_UDPv4] = XDP_RSS_TYPE_L4_IPV4_UDP, 5572 [VIRTIO_NET_HASH_REPORT_IPv6] = XDP_RSS_TYPE_L3_IPV6, 5573 [VIRTIO_NET_HASH_REPORT_TCPv6] = XDP_RSS_TYPE_L4_IPV6_TCP, 5574 [VIRTIO_NET_HASH_REPORT_UDPv6] = XDP_RSS_TYPE_L4_IPV6_UDP, 5575 [VIRTIO_NET_HASH_REPORT_IPv6_EX] = XDP_RSS_TYPE_L3_IPV6_EX, 5576 [VIRTIO_NET_HASH_REPORT_TCPv6_EX] = XDP_RSS_TYPE_L4_IPV6_TCP_EX, 5577 [VIRTIO_NET_HASH_REPORT_UDPv6_EX] = XDP_RSS_TYPE_L4_IPV6_UDP_EX 5578 }; 5579 5580 static int virtnet_xdp_rx_hash(const struct xdp_md *_ctx, u32 *hash, 5581 enum xdp_rss_hash_type *rss_type) 5582 { 5583 const struct xdp_buff *xdp = (void *)_ctx; 5584 struct virtio_net_hdr_v1_hash *hdr_hash; 5585 struct virtnet_info *vi; 5586 u16 hash_report; 5587 5588 if (!(xdp->rxq->dev->features & NETIF_F_RXHASH)) 5589 return -ENODATA; 5590 5591 vi = netdev_priv(xdp->rxq->dev); 5592 hdr_hash = (struct virtio_net_hdr_v1_hash *)(xdp->data - vi->hdr_len); 5593 hash_report = __le16_to_cpu(hdr_hash->hash_report); 5594 5595 if (hash_report >= VIRTIO_NET_HASH_REPORT_MAX_TABLE) 5596 hash_report = VIRTIO_NET_HASH_REPORT_NONE; 5597 5598 *rss_type = virtnet_xdp_rss_type[hash_report]; 5599 *hash = __le32_to_cpu(hdr_hash->hash_value); 5600 return 0; 5601 } 5602 5603 static const struct xdp_metadata_ops virtnet_xdp_metadata_ops = { 5604 .xmo_rx_hash = virtnet_xdp_rx_hash, 5605 }; 5606 5607 static int virtnet_probe(struct virtio_device *vdev) 5608 { 5609 int i, err = -ENOMEM; 5610 struct net_device *dev; 5611 struct virtnet_info *vi; 5612 u16 max_queue_pairs; 5613 int mtu = 0; 5614 5615 /* Find if host supports multiqueue/rss virtio_net device */ 5616 max_queue_pairs = 1; 5617 if (virtio_has_feature(vdev, VIRTIO_NET_F_MQ) || virtio_has_feature(vdev, VIRTIO_NET_F_RSS)) 5618 max_queue_pairs = 5619 virtio_cread16(vdev, offsetof(struct virtio_net_config, max_virtqueue_pairs)); 5620 5621 /* We need at least 2 queue's */ 5622 if (max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 5623 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 5624 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 5625 max_queue_pairs = 1; 5626 5627 /* Allocate ourselves a network device with room for our info */ 5628 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs); 5629 if (!dev) 5630 return -ENOMEM; 5631 5632 /* Set up network device as normal. */ 5633 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE | 5634 IFF_TX_SKB_NO_LINEAR; 5635 dev->netdev_ops = &virtnet_netdev; 5636 dev->stat_ops = &virtnet_stat_ops; 5637 dev->features = NETIF_F_HIGHDMA; 5638 5639 dev->ethtool_ops = &virtnet_ethtool_ops; 5640 SET_NETDEV_DEV(dev, &vdev->dev); 5641 5642 /* Do we support "hardware" checksums? */ 5643 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { 5644 /* This opens up the world of extra features. */ 5645 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG; 5646 if (csum) 5647 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG; 5648 5649 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { 5650 dev->hw_features |= NETIF_F_TSO 5651 | NETIF_F_TSO_ECN | NETIF_F_TSO6; 5652 } 5653 /* Individual feature bits: what can host handle? */ 5654 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) 5655 dev->hw_features |= NETIF_F_TSO; 5656 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) 5657 dev->hw_features |= NETIF_F_TSO6; 5658 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) 5659 dev->hw_features |= NETIF_F_TSO_ECN; 5660 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_USO)) 5661 dev->hw_features |= NETIF_F_GSO_UDP_L4; 5662 5663 dev->features |= NETIF_F_GSO_ROBUST; 5664 5665 if (gso) 5666 dev->features |= dev->hw_features & NETIF_F_ALL_TSO; 5667 /* (!csum && gso) case will be fixed by register_netdev() */ 5668 } 5669 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM)) 5670 dev->features |= NETIF_F_RXCSUM; 5671 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || 5672 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)) 5673 dev->features |= NETIF_F_GRO_HW; 5674 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) 5675 dev->hw_features |= NETIF_F_GRO_HW; 5676 5677 dev->vlan_features = dev->features; 5678 dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT; 5679 5680 /* MTU range: 68 - 65535 */ 5681 dev->min_mtu = MIN_MTU; 5682 dev->max_mtu = MAX_MTU; 5683 5684 /* Configuration may specify what MAC to use. Otherwise random. */ 5685 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) { 5686 u8 addr[ETH_ALEN]; 5687 5688 virtio_cread_bytes(vdev, 5689 offsetof(struct virtio_net_config, mac), 5690 addr, ETH_ALEN); 5691 eth_hw_addr_set(dev, addr); 5692 } else { 5693 eth_hw_addr_random(dev); 5694 dev_info(&vdev->dev, "Assigned random MAC address %pM\n", 5695 dev->dev_addr); 5696 } 5697 5698 /* Set up our device-specific information */ 5699 vi = netdev_priv(dev); 5700 vi->dev = dev; 5701 vi->vdev = vdev; 5702 vdev->priv = vi; 5703 5704 INIT_WORK(&vi->config_work, virtnet_config_changed_work); 5705 INIT_WORK(&vi->rx_mode_work, virtnet_rx_mode_work); 5706 spin_lock_init(&vi->refill_lock); 5707 5708 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) { 5709 vi->mergeable_rx_bufs = true; 5710 dev->xdp_features |= NETDEV_XDP_ACT_RX_SG; 5711 } 5712 5713 if (virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT)) 5714 vi->has_rss_hash_report = true; 5715 5716 if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS)) { 5717 vi->has_rss = true; 5718 5719 vi->rss_indir_table_size = 5720 virtio_cread16(vdev, offsetof(struct virtio_net_config, 5721 rss_max_indirection_table_length)); 5722 } 5723 5724 if (vi->has_rss || vi->has_rss_hash_report) { 5725 vi->rss_key_size = 5726 virtio_cread8(vdev, offsetof(struct virtio_net_config, rss_max_key_size)); 5727 5728 vi->rss_hash_types_supported = 5729 virtio_cread32(vdev, offsetof(struct virtio_net_config, supported_hash_types)); 5730 vi->rss_hash_types_supported &= 5731 ~(VIRTIO_NET_RSS_HASH_TYPE_IP_EX | 5732 VIRTIO_NET_RSS_HASH_TYPE_TCP_EX | 5733 VIRTIO_NET_RSS_HASH_TYPE_UDP_EX); 5734 5735 dev->hw_features |= NETIF_F_RXHASH; 5736 dev->xdp_metadata_ops = &virtnet_xdp_metadata_ops; 5737 } 5738 5739 if (vi->has_rss_hash_report) 5740 vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash); 5741 else if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) || 5742 virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 5743 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 5744 else 5745 vi->hdr_len = sizeof(struct virtio_net_hdr); 5746 5747 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) || 5748 virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 5749 vi->any_header_sg = true; 5750 5751 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 5752 vi->has_cvq = true; 5753 5754 mutex_init(&vi->cvq_lock); 5755 5756 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 5757 mtu = virtio_cread16(vdev, 5758 offsetof(struct virtio_net_config, 5759 mtu)); 5760 if (mtu < dev->min_mtu) { 5761 /* Should never trigger: MTU was previously validated 5762 * in virtnet_validate. 5763 */ 5764 dev_err(&vdev->dev, 5765 "device MTU appears to have changed it is now %d < %d", 5766 mtu, dev->min_mtu); 5767 err = -EINVAL; 5768 goto free; 5769 } 5770 5771 dev->mtu = mtu; 5772 dev->max_mtu = mtu; 5773 } 5774 5775 virtnet_set_big_packets(vi, mtu); 5776 5777 if (vi->any_header_sg) 5778 dev->needed_headroom = vi->hdr_len; 5779 5780 /* Enable multiqueue by default */ 5781 if (num_online_cpus() >= max_queue_pairs) 5782 vi->curr_queue_pairs = max_queue_pairs; 5783 else 5784 vi->curr_queue_pairs = num_online_cpus(); 5785 vi->max_queue_pairs = max_queue_pairs; 5786 5787 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */ 5788 err = init_vqs(vi); 5789 if (err) 5790 goto free; 5791 5792 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) { 5793 vi->intr_coal_rx.max_usecs = 0; 5794 vi->intr_coal_tx.max_usecs = 0; 5795 vi->intr_coal_rx.max_packets = 0; 5796 5797 /* Keep the default values of the coalescing parameters 5798 * aligned with the default napi_tx state. 5799 */ 5800 if (vi->sq[0].napi.weight) 5801 vi->intr_coal_tx.max_packets = 1; 5802 else 5803 vi->intr_coal_tx.max_packets = 0; 5804 } 5805 5806 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) { 5807 /* The reason is the same as VIRTIO_NET_F_NOTF_COAL. */ 5808 for (i = 0; i < vi->max_queue_pairs; i++) 5809 if (vi->sq[i].napi.weight) 5810 vi->sq[i].intr_coal.max_packets = 1; 5811 } 5812 5813 #ifdef CONFIG_SYSFS 5814 if (vi->mergeable_rx_bufs) 5815 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group; 5816 #endif 5817 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs); 5818 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs); 5819 5820 virtnet_init_settings(dev); 5821 5822 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) { 5823 vi->failover = net_failover_create(vi->dev); 5824 if (IS_ERR(vi->failover)) { 5825 err = PTR_ERR(vi->failover); 5826 goto free_vqs; 5827 } 5828 } 5829 5830 if (vi->has_rss || vi->has_rss_hash_report) 5831 virtnet_init_default_rss(vi); 5832 5833 enable_rx_mode_work(vi); 5834 5835 /* serialize netdev register + virtio_device_ready() with ndo_open() */ 5836 rtnl_lock(); 5837 5838 err = register_netdevice(dev); 5839 if (err) { 5840 pr_debug("virtio_net: registering device failed\n"); 5841 rtnl_unlock(); 5842 goto free_failover; 5843 } 5844 5845 virtio_device_ready(vdev); 5846 5847 virtnet_set_queues(vi, vi->curr_queue_pairs); 5848 5849 /* a random MAC address has been assigned, notify the device. 5850 * We don't fail probe if VIRTIO_NET_F_CTRL_MAC_ADDR is not there 5851 * because many devices work fine without getting MAC explicitly 5852 */ 5853 if (!virtio_has_feature(vdev, VIRTIO_NET_F_MAC) && 5854 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 5855 struct scatterlist sg; 5856 5857 sg_init_one(&sg, dev->dev_addr, dev->addr_len); 5858 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 5859 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) { 5860 pr_debug("virtio_net: setting MAC address failed\n"); 5861 rtnl_unlock(); 5862 err = -EINVAL; 5863 goto free_unregister_netdev; 5864 } 5865 } 5866 5867 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_DEVICE_STATS)) { 5868 struct virtio_net_stats_capabilities *stats_cap __free(kfree) = NULL; 5869 struct scatterlist sg; 5870 __le64 v; 5871 5872 stats_cap = kzalloc(sizeof(*stats_cap), GFP_KERNEL); 5873 if (!stats_cap) { 5874 rtnl_unlock(); 5875 err = -ENOMEM; 5876 goto free_unregister_netdev; 5877 } 5878 5879 sg_init_one(&sg, stats_cap, sizeof(*stats_cap)); 5880 5881 if (!virtnet_send_command_reply(vi, VIRTIO_NET_CTRL_STATS, 5882 VIRTIO_NET_CTRL_STATS_QUERY, 5883 NULL, &sg)) { 5884 pr_debug("virtio_net: fail to get stats capability\n"); 5885 rtnl_unlock(); 5886 err = -EINVAL; 5887 goto free_unregister_netdev; 5888 } 5889 5890 v = stats_cap->supported_stats_types[0]; 5891 vi->device_stats_cap = le64_to_cpu(v); 5892 } 5893 5894 rtnl_unlock(); 5895 5896 err = virtnet_cpu_notif_add(vi); 5897 if (err) { 5898 pr_debug("virtio_net: registering cpu notifier failed\n"); 5899 goto free_unregister_netdev; 5900 } 5901 5902 /* Assume link up if device can't report link status, 5903 otherwise get link status from config. */ 5904 netif_carrier_off(dev); 5905 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) { 5906 schedule_work(&vi->config_work); 5907 } else { 5908 vi->status = VIRTIO_NET_S_LINK_UP; 5909 virtnet_update_settings(vi); 5910 netif_carrier_on(dev); 5911 } 5912 5913 for (i = 0; i < ARRAY_SIZE(guest_offloads); i++) 5914 if (virtio_has_feature(vi->vdev, guest_offloads[i])) 5915 set_bit(guest_offloads[i], &vi->guest_offloads); 5916 vi->guest_offloads_capable = vi->guest_offloads; 5917 5918 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n", 5919 dev->name, max_queue_pairs); 5920 5921 return 0; 5922 5923 free_unregister_netdev: 5924 unregister_netdev(dev); 5925 free_failover: 5926 net_failover_destroy(vi->failover); 5927 free_vqs: 5928 virtio_reset_device(vdev); 5929 cancel_delayed_work_sync(&vi->refill); 5930 free_receive_page_frags(vi); 5931 virtnet_del_vqs(vi); 5932 free: 5933 free_netdev(dev); 5934 return err; 5935 } 5936 5937 static void remove_vq_common(struct virtnet_info *vi) 5938 { 5939 virtio_reset_device(vi->vdev); 5940 5941 /* Free unused buffers in both send and recv, if any. */ 5942 free_unused_bufs(vi); 5943 5944 free_receive_bufs(vi); 5945 5946 free_receive_page_frags(vi); 5947 5948 virtnet_del_vqs(vi); 5949 } 5950 5951 static void virtnet_remove(struct virtio_device *vdev) 5952 { 5953 struct virtnet_info *vi = vdev->priv; 5954 5955 virtnet_cpu_notif_remove(vi); 5956 5957 /* Make sure no work handler is accessing the device. */ 5958 flush_work(&vi->config_work); 5959 disable_rx_mode_work(vi); 5960 flush_work(&vi->rx_mode_work); 5961 5962 unregister_netdev(vi->dev); 5963 5964 net_failover_destroy(vi->failover); 5965 5966 remove_vq_common(vi); 5967 5968 free_netdev(vi->dev); 5969 } 5970 5971 static __maybe_unused int virtnet_freeze(struct virtio_device *vdev) 5972 { 5973 struct virtnet_info *vi = vdev->priv; 5974 5975 virtnet_cpu_notif_remove(vi); 5976 virtnet_freeze_down(vdev); 5977 remove_vq_common(vi); 5978 5979 return 0; 5980 } 5981 5982 static __maybe_unused int virtnet_restore(struct virtio_device *vdev) 5983 { 5984 struct virtnet_info *vi = vdev->priv; 5985 int err; 5986 5987 err = virtnet_restore_up(vdev); 5988 if (err) 5989 return err; 5990 virtnet_set_queues(vi, vi->curr_queue_pairs); 5991 5992 err = virtnet_cpu_notif_add(vi); 5993 if (err) { 5994 virtnet_freeze_down(vdev); 5995 remove_vq_common(vi); 5996 return err; 5997 } 5998 5999 return 0; 6000 } 6001 6002 static struct virtio_device_id id_table[] = { 6003 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 6004 { 0 }, 6005 }; 6006 6007 #define VIRTNET_FEATURES \ 6008 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \ 6009 VIRTIO_NET_F_MAC, \ 6010 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \ 6011 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \ 6012 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \ 6013 VIRTIO_NET_F_HOST_USO, VIRTIO_NET_F_GUEST_USO4, VIRTIO_NET_F_GUEST_USO6, \ 6014 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \ 6015 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \ 6016 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \ 6017 VIRTIO_NET_F_CTRL_MAC_ADDR, \ 6018 VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \ 6019 VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY, \ 6020 VIRTIO_NET_F_RSS, VIRTIO_NET_F_HASH_REPORT, VIRTIO_NET_F_NOTF_COAL, \ 6021 VIRTIO_NET_F_VQ_NOTF_COAL, \ 6022 VIRTIO_NET_F_GUEST_HDRLEN, VIRTIO_NET_F_DEVICE_STATS 6023 6024 static unsigned int features[] = { 6025 VIRTNET_FEATURES, 6026 }; 6027 6028 static unsigned int features_legacy[] = { 6029 VIRTNET_FEATURES, 6030 VIRTIO_NET_F_GSO, 6031 VIRTIO_F_ANY_LAYOUT, 6032 }; 6033 6034 static struct virtio_driver virtio_net_driver = { 6035 .feature_table = features, 6036 .feature_table_size = ARRAY_SIZE(features), 6037 .feature_table_legacy = features_legacy, 6038 .feature_table_size_legacy = ARRAY_SIZE(features_legacy), 6039 .driver.name = KBUILD_MODNAME, 6040 .id_table = id_table, 6041 .validate = virtnet_validate, 6042 .probe = virtnet_probe, 6043 .remove = virtnet_remove, 6044 .config_changed = virtnet_config_changed, 6045 #ifdef CONFIG_PM_SLEEP 6046 .freeze = virtnet_freeze, 6047 .restore = virtnet_restore, 6048 #endif 6049 }; 6050 6051 static __init int virtio_net_driver_init(void) 6052 { 6053 int ret; 6054 6055 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online", 6056 virtnet_cpu_online, 6057 virtnet_cpu_down_prep); 6058 if (ret < 0) 6059 goto out; 6060 virtionet_online = ret; 6061 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead", 6062 NULL, virtnet_cpu_dead); 6063 if (ret) 6064 goto err_dead; 6065 ret = register_virtio_driver(&virtio_net_driver); 6066 if (ret) 6067 goto err_virtio; 6068 return 0; 6069 err_virtio: 6070 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 6071 err_dead: 6072 cpuhp_remove_multi_state(virtionet_online); 6073 out: 6074 return ret; 6075 } 6076 module_init(virtio_net_driver_init); 6077 6078 static __exit void virtio_net_driver_exit(void) 6079 { 6080 unregister_virtio_driver(&virtio_net_driver); 6081 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 6082 cpuhp_remove_multi_state(virtionet_online); 6083 } 6084 module_exit(virtio_net_driver_exit); 6085 6086 MODULE_DEVICE_TABLE(virtio, id_table); 6087 MODULE_DESCRIPTION("Virtio network driver"); 6088 MODULE_LICENSE("GPL"); 6089