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