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