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