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