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