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