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