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 false)) 3349 return -EPROTO; 3350 3351 if (vi->mergeable_rx_bufs) 3352 hdr->hash_hdr.hdr.num_buffers = 0; 3353 3354 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2)); 3355 if (can_push) { 3356 __skb_push(skb, hdr_len); 3357 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len); 3358 if (unlikely(num_sg < 0)) 3359 return num_sg; 3360 /* Pull header back to avoid skew in tx bytes calculations. */ 3361 __skb_pull(skb, hdr_len); 3362 } else { 3363 sg_set_buf(sq->sg, hdr, hdr_len); 3364 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len); 3365 if (unlikely(num_sg < 0)) 3366 return num_sg; 3367 num_sg++; 3368 } 3369 3370 return virtnet_add_outbuf(sq, num_sg, skb, 3371 orphan ? VIRTNET_XMIT_TYPE_SKB_ORPHAN : VIRTNET_XMIT_TYPE_SKB); 3372 } 3373 3374 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) 3375 { 3376 struct virtnet_info *vi = netdev_priv(dev); 3377 int qnum = skb_get_queue_mapping(skb); 3378 struct send_queue *sq = &vi->sq[qnum]; 3379 int err; 3380 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum); 3381 bool xmit_more = netdev_xmit_more(); 3382 bool use_napi = sq->napi.weight; 3383 bool kick; 3384 3385 if (!use_napi) 3386 free_old_xmit(sq, txq, false); 3387 else 3388 virtqueue_disable_cb(sq->vq); 3389 3390 /* timestamp packet in software */ 3391 skb_tx_timestamp(skb); 3392 3393 /* Try to transmit */ 3394 err = xmit_skb(sq, skb, !use_napi); 3395 3396 /* This should not happen! */ 3397 if (unlikely(err)) { 3398 DEV_STATS_INC(dev, tx_fifo_errors); 3399 if (net_ratelimit()) 3400 dev_warn(&dev->dev, 3401 "Unexpected TXQ (%d) queue failure: %d\n", 3402 qnum, err); 3403 DEV_STATS_INC(dev, tx_dropped); 3404 dev_kfree_skb_any(skb); 3405 return NETDEV_TX_OK; 3406 } 3407 3408 /* Don't wait up for transmitted skbs to be freed. */ 3409 if (!use_napi) { 3410 skb_orphan(skb); 3411 nf_reset_ct(skb); 3412 } 3413 3414 if (use_napi) 3415 tx_may_stop(vi, dev, sq); 3416 else 3417 check_sq_full_and_disable(vi, dev,sq); 3418 3419 kick = use_napi ? __netdev_tx_sent_queue(txq, skb->len, xmit_more) : 3420 !xmit_more || netif_xmit_stopped(txq); 3421 if (kick) { 3422 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) { 3423 u64_stats_update_begin(&sq->stats.syncp); 3424 u64_stats_inc(&sq->stats.kicks); 3425 u64_stats_update_end(&sq->stats.syncp); 3426 } 3427 } 3428 3429 if (use_napi && kick && unlikely(!virtqueue_enable_cb_delayed(sq->vq))) 3430 virtqueue_napi_schedule(&sq->napi, sq->vq); 3431 3432 return NETDEV_TX_OK; 3433 } 3434 3435 static void __virtnet_rx_pause(struct virtnet_info *vi, 3436 struct receive_queue *rq) 3437 { 3438 bool running = netif_running(vi->dev); 3439 3440 if (running) { 3441 virtnet_napi_disable(rq); 3442 virtnet_cancel_dim(vi, &rq->dim); 3443 } 3444 } 3445 3446 static void virtnet_rx_pause_all(struct virtnet_info *vi) 3447 { 3448 int i; 3449 3450 /* 3451 * Make sure refill_work does not run concurrently to 3452 * avoid napi_disable race which leads to deadlock. 3453 */ 3454 disable_delayed_refill(vi); 3455 cancel_delayed_work_sync(&vi->refill); 3456 for (i = 0; i < vi->max_queue_pairs; i++) 3457 __virtnet_rx_pause(vi, &vi->rq[i]); 3458 } 3459 3460 static void virtnet_rx_pause(struct virtnet_info *vi, struct receive_queue *rq) 3461 { 3462 /* 3463 * Make sure refill_work does not run concurrently to 3464 * avoid napi_disable race which leads to deadlock. 3465 */ 3466 disable_delayed_refill(vi); 3467 cancel_delayed_work_sync(&vi->refill); 3468 __virtnet_rx_pause(vi, rq); 3469 } 3470 3471 static void __virtnet_rx_resume(struct virtnet_info *vi, 3472 struct receive_queue *rq, 3473 bool refill) 3474 { 3475 bool running = netif_running(vi->dev); 3476 bool schedule_refill = false; 3477 3478 if (refill && !try_fill_recv(vi, rq, GFP_KERNEL)) 3479 schedule_refill = true; 3480 if (running) 3481 virtnet_napi_enable(rq); 3482 3483 if (schedule_refill) 3484 schedule_delayed_work(&vi->refill, 0); 3485 } 3486 3487 static void virtnet_rx_resume_all(struct virtnet_info *vi) 3488 { 3489 int i; 3490 3491 enable_delayed_refill(vi); 3492 for (i = 0; i < vi->max_queue_pairs; i++) { 3493 if (i < vi->curr_queue_pairs) 3494 __virtnet_rx_resume(vi, &vi->rq[i], true); 3495 else 3496 __virtnet_rx_resume(vi, &vi->rq[i], false); 3497 } 3498 } 3499 3500 static void virtnet_rx_resume(struct virtnet_info *vi, struct receive_queue *rq) 3501 { 3502 enable_delayed_refill(vi); 3503 __virtnet_rx_resume(vi, rq, true); 3504 } 3505 3506 static int virtnet_rx_resize(struct virtnet_info *vi, 3507 struct receive_queue *rq, u32 ring_num) 3508 { 3509 int err, qindex; 3510 3511 qindex = rq - vi->rq; 3512 3513 virtnet_rx_pause(vi, rq); 3514 3515 err = virtqueue_resize(rq->vq, ring_num, virtnet_rq_unmap_free_buf, NULL); 3516 if (err) 3517 netdev_err(vi->dev, "resize rx fail: rx queue index: %d err: %d\n", qindex, err); 3518 3519 virtnet_rx_resume(vi, rq); 3520 return err; 3521 } 3522 3523 static void virtnet_tx_pause(struct virtnet_info *vi, struct send_queue *sq) 3524 { 3525 bool running = netif_running(vi->dev); 3526 struct netdev_queue *txq; 3527 int qindex; 3528 3529 qindex = sq - vi->sq; 3530 3531 if (running) 3532 virtnet_napi_tx_disable(sq); 3533 3534 txq = netdev_get_tx_queue(vi->dev, qindex); 3535 3536 /* 1. wait all ximt complete 3537 * 2. fix the race of netif_stop_subqueue() vs netif_start_subqueue() 3538 */ 3539 __netif_tx_lock_bh(txq); 3540 3541 /* Prevent rx poll from accessing sq. */ 3542 sq->reset = true; 3543 3544 /* Prevent the upper layer from trying to send packets. */ 3545 netif_stop_subqueue(vi->dev, qindex); 3546 u64_stats_update_begin(&sq->stats.syncp); 3547 u64_stats_inc(&sq->stats.stop); 3548 u64_stats_update_end(&sq->stats.syncp); 3549 3550 __netif_tx_unlock_bh(txq); 3551 } 3552 3553 static void virtnet_tx_resume(struct virtnet_info *vi, struct send_queue *sq) 3554 { 3555 bool running = netif_running(vi->dev); 3556 struct netdev_queue *txq; 3557 int qindex; 3558 3559 qindex = sq - vi->sq; 3560 3561 txq = netdev_get_tx_queue(vi->dev, qindex); 3562 3563 __netif_tx_lock_bh(txq); 3564 sq->reset = false; 3565 virtnet_tx_wake_queue(vi, sq); 3566 __netif_tx_unlock_bh(txq); 3567 3568 if (running) 3569 virtnet_napi_tx_enable(sq); 3570 } 3571 3572 static int virtnet_tx_resize(struct virtnet_info *vi, struct send_queue *sq, 3573 u32 ring_num) 3574 { 3575 int qindex, err; 3576 3577 if (ring_num <= MAX_SKB_FRAGS + 2) { 3578 netdev_err(vi->dev, "tx size (%d) cannot be smaller than %d\n", 3579 ring_num, MAX_SKB_FRAGS + 2); 3580 return -EINVAL; 3581 } 3582 3583 qindex = sq - vi->sq; 3584 3585 virtnet_tx_pause(vi, sq); 3586 3587 err = virtqueue_resize(sq->vq, ring_num, virtnet_sq_free_unused_buf, 3588 virtnet_sq_free_unused_buf_done); 3589 if (err) 3590 netdev_err(vi->dev, "resize tx fail: tx queue index: %d err: %d\n", qindex, err); 3591 3592 virtnet_tx_resume(vi, sq); 3593 3594 return err; 3595 } 3596 3597 /* 3598 * Send command via the control virtqueue and check status. Commands 3599 * supported by the hypervisor, as indicated by feature bits, should 3600 * never fail unless improperly formatted. 3601 */ 3602 static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd, 3603 struct scatterlist *out, 3604 struct scatterlist *in) 3605 { 3606 struct scatterlist *sgs[5], hdr, stat; 3607 u32 out_num = 0, tmp, in_num = 0; 3608 bool ok; 3609 int ret; 3610 3611 /* Caller should know better */ 3612 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)); 3613 3614 mutex_lock(&vi->cvq_lock); 3615 vi->ctrl->status = ~0; 3616 vi->ctrl->hdr.class = class; 3617 vi->ctrl->hdr.cmd = cmd; 3618 /* Add header */ 3619 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr)); 3620 sgs[out_num++] = &hdr; 3621 3622 if (out) 3623 sgs[out_num++] = out; 3624 3625 /* Add return status. */ 3626 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status)); 3627 sgs[out_num + in_num++] = &stat; 3628 3629 if (in) 3630 sgs[out_num + in_num++] = in; 3631 3632 BUG_ON(out_num + in_num > ARRAY_SIZE(sgs)); 3633 ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, in_num, vi, GFP_ATOMIC); 3634 if (ret < 0) { 3635 dev_warn(&vi->vdev->dev, 3636 "Failed to add sgs for command vq: %d\n.", ret); 3637 mutex_unlock(&vi->cvq_lock); 3638 return false; 3639 } 3640 3641 if (unlikely(!virtqueue_kick(vi->cvq))) 3642 goto unlock; 3643 3644 /* Spin for a response, the kick causes an ioport write, trapping 3645 * into the hypervisor, so the request should be handled immediately. 3646 */ 3647 while (!virtqueue_get_buf(vi->cvq, &tmp) && 3648 !virtqueue_is_broken(vi->cvq)) { 3649 cond_resched(); 3650 cpu_relax(); 3651 } 3652 3653 unlock: 3654 ok = vi->ctrl->status == VIRTIO_NET_OK; 3655 mutex_unlock(&vi->cvq_lock); 3656 return ok; 3657 } 3658 3659 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, 3660 struct scatterlist *out) 3661 { 3662 return virtnet_send_command_reply(vi, class, cmd, out, NULL); 3663 } 3664 3665 static int virtnet_set_mac_address(struct net_device *dev, void *p) 3666 { 3667 struct virtnet_info *vi = netdev_priv(dev); 3668 struct virtio_device *vdev = vi->vdev; 3669 int ret; 3670 struct sockaddr *addr; 3671 struct scatterlist sg; 3672 3673 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY)) 3674 return -EOPNOTSUPP; 3675 3676 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL); 3677 if (!addr) 3678 return -ENOMEM; 3679 3680 ret = eth_prepare_mac_addr_change(dev, addr); 3681 if (ret) 3682 goto out; 3683 3684 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 3685 sg_init_one(&sg, addr->sa_data, dev->addr_len); 3686 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 3687 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) { 3688 dev_warn(&vdev->dev, 3689 "Failed to set mac address by vq command.\n"); 3690 ret = -EINVAL; 3691 goto out; 3692 } 3693 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) && 3694 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) { 3695 unsigned int i; 3696 3697 /* Naturally, this has an atomicity problem. */ 3698 for (i = 0; i < dev->addr_len; i++) 3699 virtio_cwrite8(vdev, 3700 offsetof(struct virtio_net_config, mac) + 3701 i, addr->sa_data[i]); 3702 } 3703 3704 eth_commit_mac_addr_change(dev, p); 3705 ret = 0; 3706 3707 out: 3708 kfree(addr); 3709 return ret; 3710 } 3711 3712 static void virtnet_stats(struct net_device *dev, 3713 struct rtnl_link_stats64 *tot) 3714 { 3715 struct virtnet_info *vi = netdev_priv(dev); 3716 unsigned int start; 3717 int i; 3718 3719 for (i = 0; i < vi->max_queue_pairs; i++) { 3720 u64 tpackets, tbytes, terrors, rpackets, rbytes, rdrops; 3721 struct receive_queue *rq = &vi->rq[i]; 3722 struct send_queue *sq = &vi->sq[i]; 3723 3724 do { 3725 start = u64_stats_fetch_begin(&sq->stats.syncp); 3726 tpackets = u64_stats_read(&sq->stats.packets); 3727 tbytes = u64_stats_read(&sq->stats.bytes); 3728 terrors = u64_stats_read(&sq->stats.tx_timeouts); 3729 } while (u64_stats_fetch_retry(&sq->stats.syncp, start)); 3730 3731 do { 3732 start = u64_stats_fetch_begin(&rq->stats.syncp); 3733 rpackets = u64_stats_read(&rq->stats.packets); 3734 rbytes = u64_stats_read(&rq->stats.bytes); 3735 rdrops = u64_stats_read(&rq->stats.drops); 3736 } while (u64_stats_fetch_retry(&rq->stats.syncp, start)); 3737 3738 tot->rx_packets += rpackets; 3739 tot->tx_packets += tpackets; 3740 tot->rx_bytes += rbytes; 3741 tot->tx_bytes += tbytes; 3742 tot->rx_dropped += rdrops; 3743 tot->tx_errors += terrors; 3744 } 3745 3746 tot->tx_dropped = DEV_STATS_READ(dev, tx_dropped); 3747 tot->tx_fifo_errors = DEV_STATS_READ(dev, tx_fifo_errors); 3748 tot->rx_length_errors = DEV_STATS_READ(dev, rx_length_errors); 3749 tot->rx_frame_errors = DEV_STATS_READ(dev, rx_frame_errors); 3750 } 3751 3752 static void virtnet_ack_link_announce(struct virtnet_info *vi) 3753 { 3754 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE, 3755 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL)) 3756 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n"); 3757 } 3758 3759 static bool virtnet_commit_rss_command(struct virtnet_info *vi); 3760 3761 static void virtnet_rss_update_by_qpairs(struct virtnet_info *vi, u16 queue_pairs) 3762 { 3763 u32 indir_val = 0; 3764 int i = 0; 3765 3766 for (; i < vi->rss_indir_table_size; ++i) { 3767 indir_val = ethtool_rxfh_indir_default(i, queue_pairs); 3768 vi->rss_hdr->indirection_table[i] = cpu_to_le16(indir_val); 3769 } 3770 vi->rss_trailer.max_tx_vq = cpu_to_le16(queue_pairs); 3771 } 3772 3773 static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 3774 { 3775 struct virtio_net_ctrl_mq *mq __free(kfree) = NULL; 3776 struct virtio_net_rss_config_hdr *old_rss_hdr; 3777 struct virtio_net_rss_config_trailer old_rss_trailer; 3778 struct net_device *dev = vi->dev; 3779 struct scatterlist sg; 3780 3781 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ)) 3782 return 0; 3783 3784 /* Firstly check if we need update rss. Do updating if both (1) rss enabled and 3785 * (2) no user configuration. 3786 * 3787 * During rss command processing, device updates queue_pairs using rss.max_tx_vq. That is, 3788 * the device updates queue_pairs together with rss, so we can skip the separate queue_pairs 3789 * update (VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET below) and return directly. 3790 */ 3791 if (vi->has_rss && !netif_is_rxfh_configured(dev)) { 3792 old_rss_hdr = vi->rss_hdr; 3793 old_rss_trailer = vi->rss_trailer; 3794 vi->rss_hdr = devm_kzalloc(&dev->dev, virtnet_rss_hdr_size(vi), GFP_KERNEL); 3795 if (!vi->rss_hdr) { 3796 vi->rss_hdr = old_rss_hdr; 3797 return -ENOMEM; 3798 } 3799 3800 *vi->rss_hdr = *old_rss_hdr; 3801 virtnet_rss_update_by_qpairs(vi, queue_pairs); 3802 3803 if (!virtnet_commit_rss_command(vi)) { 3804 /* restore ctrl_rss if commit_rss_command failed */ 3805 devm_kfree(&dev->dev, vi->rss_hdr); 3806 vi->rss_hdr = old_rss_hdr; 3807 vi->rss_trailer = old_rss_trailer; 3808 3809 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d, because committing RSS failed\n", 3810 queue_pairs); 3811 return -EINVAL; 3812 } 3813 devm_kfree(&dev->dev, old_rss_hdr); 3814 goto succ; 3815 } 3816 3817 mq = kzalloc(sizeof(*mq), GFP_KERNEL); 3818 if (!mq) 3819 return -ENOMEM; 3820 3821 mq->virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs); 3822 sg_init_one(&sg, mq, sizeof(*mq)); 3823 3824 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, 3825 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) { 3826 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n", 3827 queue_pairs); 3828 return -EINVAL; 3829 } 3830 succ: 3831 vi->curr_queue_pairs = queue_pairs; 3832 /* virtnet_open() will refill when device is going to up. */ 3833 spin_lock_bh(&vi->refill_lock); 3834 if (dev->flags & IFF_UP && vi->refill_enabled) 3835 schedule_delayed_work(&vi->refill, 0); 3836 spin_unlock_bh(&vi->refill_lock); 3837 3838 return 0; 3839 } 3840 3841 static int virtnet_close(struct net_device *dev) 3842 { 3843 struct virtnet_info *vi = netdev_priv(dev); 3844 int i; 3845 3846 /* Make sure NAPI doesn't schedule refill work */ 3847 disable_delayed_refill(vi); 3848 /* Make sure refill_work doesn't re-enable napi! */ 3849 cancel_delayed_work_sync(&vi->refill); 3850 /* Prevent the config change callback from changing carrier 3851 * after close 3852 */ 3853 virtio_config_driver_disable(vi->vdev); 3854 /* Stop getting status/speed updates: we don't care until next 3855 * open 3856 */ 3857 cancel_work_sync(&vi->config_work); 3858 3859 for (i = 0; i < vi->max_queue_pairs; i++) { 3860 virtnet_disable_queue_pair(vi, i); 3861 virtnet_cancel_dim(vi, &vi->rq[i].dim); 3862 } 3863 3864 netif_carrier_off(dev); 3865 3866 return 0; 3867 } 3868 3869 static void virtnet_rx_mode_work(struct work_struct *work) 3870 { 3871 struct virtnet_info *vi = 3872 container_of(work, struct virtnet_info, rx_mode_work); 3873 u8 *promisc_allmulti __free(kfree) = NULL; 3874 struct net_device *dev = vi->dev; 3875 struct scatterlist sg[2]; 3876 struct virtio_net_ctrl_mac *mac_data; 3877 struct netdev_hw_addr *ha; 3878 int uc_count; 3879 int mc_count; 3880 void *buf; 3881 int i; 3882 3883 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */ 3884 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) 3885 return; 3886 3887 promisc_allmulti = kzalloc(sizeof(*promisc_allmulti), GFP_KERNEL); 3888 if (!promisc_allmulti) { 3889 dev_warn(&dev->dev, "Failed to set RX mode, no memory.\n"); 3890 return; 3891 } 3892 3893 rtnl_lock(); 3894 3895 *promisc_allmulti = !!(dev->flags & IFF_PROMISC); 3896 sg_init_one(sg, promisc_allmulti, sizeof(*promisc_allmulti)); 3897 3898 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 3899 VIRTIO_NET_CTRL_RX_PROMISC, sg)) 3900 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", 3901 *promisc_allmulti ? "en" : "dis"); 3902 3903 *promisc_allmulti = !!(dev->flags & IFF_ALLMULTI); 3904 sg_init_one(sg, promisc_allmulti, sizeof(*promisc_allmulti)); 3905 3906 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 3907 VIRTIO_NET_CTRL_RX_ALLMULTI, sg)) 3908 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", 3909 *promisc_allmulti ? "en" : "dis"); 3910 3911 netif_addr_lock_bh(dev); 3912 3913 uc_count = netdev_uc_count(dev); 3914 mc_count = netdev_mc_count(dev); 3915 /* MAC filter - use one buffer for both lists */ 3916 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) + 3917 (2 * sizeof(mac_data->entries)), GFP_ATOMIC); 3918 mac_data = buf; 3919 if (!buf) { 3920 netif_addr_unlock_bh(dev); 3921 rtnl_unlock(); 3922 return; 3923 } 3924 3925 sg_init_table(sg, 2); 3926 3927 /* Store the unicast list and count in the front of the buffer */ 3928 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count); 3929 i = 0; 3930 netdev_for_each_uc_addr(ha, dev) 3931 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 3932 3933 sg_set_buf(&sg[0], mac_data, 3934 sizeof(mac_data->entries) + (uc_count * ETH_ALEN)); 3935 3936 /* multicast list and count fill the end */ 3937 mac_data = (void *)&mac_data->macs[uc_count][0]; 3938 3939 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count); 3940 i = 0; 3941 netdev_for_each_mc_addr(ha, dev) 3942 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 3943 3944 netif_addr_unlock_bh(dev); 3945 3946 sg_set_buf(&sg[1], mac_data, 3947 sizeof(mac_data->entries) + (mc_count * ETH_ALEN)); 3948 3949 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 3950 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg)) 3951 dev_warn(&dev->dev, "Failed to set MAC filter table.\n"); 3952 3953 rtnl_unlock(); 3954 3955 kfree(buf); 3956 } 3957 3958 static void virtnet_set_rx_mode(struct net_device *dev) 3959 { 3960 struct virtnet_info *vi = netdev_priv(dev); 3961 3962 if (vi->rx_mode_work_enabled) 3963 schedule_work(&vi->rx_mode_work); 3964 } 3965 3966 static int virtnet_vlan_rx_add_vid(struct net_device *dev, 3967 __be16 proto, u16 vid) 3968 { 3969 struct virtnet_info *vi = netdev_priv(dev); 3970 __virtio16 *_vid __free(kfree) = NULL; 3971 struct scatterlist sg; 3972 3973 _vid = kzalloc(sizeof(*_vid), GFP_KERNEL); 3974 if (!_vid) 3975 return -ENOMEM; 3976 3977 *_vid = cpu_to_virtio16(vi->vdev, vid); 3978 sg_init_one(&sg, _vid, sizeof(*_vid)); 3979 3980 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 3981 VIRTIO_NET_CTRL_VLAN_ADD, &sg)) 3982 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); 3983 return 0; 3984 } 3985 3986 static int virtnet_vlan_rx_kill_vid(struct net_device *dev, 3987 __be16 proto, u16 vid) 3988 { 3989 struct virtnet_info *vi = netdev_priv(dev); 3990 __virtio16 *_vid __free(kfree) = NULL; 3991 struct scatterlist sg; 3992 3993 _vid = kzalloc(sizeof(*_vid), GFP_KERNEL); 3994 if (!_vid) 3995 return -ENOMEM; 3996 3997 *_vid = cpu_to_virtio16(vi->vdev, vid); 3998 sg_init_one(&sg, _vid, sizeof(*_vid)); 3999 4000 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 4001 VIRTIO_NET_CTRL_VLAN_DEL, &sg)) 4002 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); 4003 return 0; 4004 } 4005 4006 static void virtnet_clean_affinity(struct virtnet_info *vi) 4007 { 4008 int i; 4009 4010 if (vi->affinity_hint_set) { 4011 for (i = 0; i < vi->max_queue_pairs; i++) { 4012 virtqueue_set_affinity(vi->rq[i].vq, NULL); 4013 virtqueue_set_affinity(vi->sq[i].vq, NULL); 4014 } 4015 4016 vi->affinity_hint_set = false; 4017 } 4018 } 4019 4020 static void virtnet_set_affinity(struct virtnet_info *vi) 4021 { 4022 cpumask_var_t mask; 4023 int stragglers; 4024 int group_size; 4025 int i, start = 0, cpu; 4026 int num_cpu; 4027 int stride; 4028 4029 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) { 4030 virtnet_clean_affinity(vi); 4031 return; 4032 } 4033 4034 num_cpu = num_online_cpus(); 4035 stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1); 4036 stragglers = num_cpu >= vi->curr_queue_pairs ? 4037 num_cpu % vi->curr_queue_pairs : 4038 0; 4039 4040 for (i = 0; i < vi->curr_queue_pairs; i++) { 4041 group_size = stride + (i < stragglers ? 1 : 0); 4042 4043 for_each_online_cpu_wrap(cpu, start) { 4044 if (!group_size--) { 4045 start = cpu; 4046 break; 4047 } 4048 cpumask_set_cpu(cpu, mask); 4049 } 4050 4051 virtqueue_set_affinity(vi->rq[i].vq, mask); 4052 virtqueue_set_affinity(vi->sq[i].vq, mask); 4053 __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS); 4054 cpumask_clear(mask); 4055 } 4056 4057 vi->affinity_hint_set = true; 4058 free_cpumask_var(mask); 4059 } 4060 4061 static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node) 4062 { 4063 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 4064 node); 4065 virtnet_set_affinity(vi); 4066 return 0; 4067 } 4068 4069 static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node) 4070 { 4071 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 4072 node_dead); 4073 virtnet_set_affinity(vi); 4074 return 0; 4075 } 4076 4077 static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node) 4078 { 4079 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 4080 node); 4081 4082 virtnet_clean_affinity(vi); 4083 return 0; 4084 } 4085 4086 static enum cpuhp_state virtionet_online; 4087 4088 static int virtnet_cpu_notif_add(struct virtnet_info *vi) 4089 { 4090 int ret; 4091 4092 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node); 4093 if (ret) 4094 return ret; 4095 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD, 4096 &vi->node_dead); 4097 if (!ret) 4098 return ret; 4099 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 4100 return ret; 4101 } 4102 4103 static void virtnet_cpu_notif_remove(struct virtnet_info *vi) 4104 { 4105 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 4106 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD, 4107 &vi->node_dead); 4108 } 4109 4110 static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi, 4111 u16 vqn, u32 max_usecs, u32 max_packets) 4112 { 4113 struct virtio_net_ctrl_coal_vq *coal_vq __free(kfree) = NULL; 4114 struct scatterlist sgs; 4115 4116 coal_vq = kzalloc(sizeof(*coal_vq), GFP_KERNEL); 4117 if (!coal_vq) 4118 return -ENOMEM; 4119 4120 coal_vq->vqn = cpu_to_le16(vqn); 4121 coal_vq->coal.max_usecs = cpu_to_le32(max_usecs); 4122 coal_vq->coal.max_packets = cpu_to_le32(max_packets); 4123 sg_init_one(&sgs, coal_vq, sizeof(*coal_vq)); 4124 4125 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL, 4126 VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET, 4127 &sgs)) 4128 return -EINVAL; 4129 4130 return 0; 4131 } 4132 4133 static int virtnet_send_rx_ctrl_coal_vq_cmd(struct virtnet_info *vi, 4134 u16 queue, u32 max_usecs, 4135 u32 max_packets) 4136 { 4137 int err; 4138 4139 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) 4140 return -EOPNOTSUPP; 4141 4142 err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(queue), 4143 max_usecs, max_packets); 4144 if (err) 4145 return err; 4146 4147 vi->rq[queue].intr_coal.max_usecs = max_usecs; 4148 vi->rq[queue].intr_coal.max_packets = max_packets; 4149 4150 return 0; 4151 } 4152 4153 static int virtnet_send_tx_ctrl_coal_vq_cmd(struct virtnet_info *vi, 4154 u16 queue, u32 max_usecs, 4155 u32 max_packets) 4156 { 4157 int err; 4158 4159 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) 4160 return -EOPNOTSUPP; 4161 4162 err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(queue), 4163 max_usecs, max_packets); 4164 if (err) 4165 return err; 4166 4167 vi->sq[queue].intr_coal.max_usecs = max_usecs; 4168 vi->sq[queue].intr_coal.max_packets = max_packets; 4169 4170 return 0; 4171 } 4172 4173 static void virtnet_get_ringparam(struct net_device *dev, 4174 struct ethtool_ringparam *ring, 4175 struct kernel_ethtool_ringparam *kernel_ring, 4176 struct netlink_ext_ack *extack) 4177 { 4178 struct virtnet_info *vi = netdev_priv(dev); 4179 4180 ring->rx_max_pending = vi->rq[0].vq->num_max; 4181 ring->tx_max_pending = vi->sq[0].vq->num_max; 4182 ring->rx_pending = virtqueue_get_vring_size(vi->rq[0].vq); 4183 ring->tx_pending = virtqueue_get_vring_size(vi->sq[0].vq); 4184 } 4185 4186 static int virtnet_set_ringparam(struct net_device *dev, 4187 struct ethtool_ringparam *ring, 4188 struct kernel_ethtool_ringparam *kernel_ring, 4189 struct netlink_ext_ack *extack) 4190 { 4191 struct virtnet_info *vi = netdev_priv(dev); 4192 u32 rx_pending, tx_pending; 4193 struct receive_queue *rq; 4194 struct send_queue *sq; 4195 int i, err; 4196 4197 if (ring->rx_mini_pending || ring->rx_jumbo_pending) 4198 return -EINVAL; 4199 4200 rx_pending = virtqueue_get_vring_size(vi->rq[0].vq); 4201 tx_pending = virtqueue_get_vring_size(vi->sq[0].vq); 4202 4203 if (ring->rx_pending == rx_pending && 4204 ring->tx_pending == tx_pending) 4205 return 0; 4206 4207 if (ring->rx_pending > vi->rq[0].vq->num_max) 4208 return -EINVAL; 4209 4210 if (ring->tx_pending > vi->sq[0].vq->num_max) 4211 return -EINVAL; 4212 4213 for (i = 0; i < vi->max_queue_pairs; i++) { 4214 rq = vi->rq + i; 4215 sq = vi->sq + i; 4216 4217 if (ring->tx_pending != tx_pending) { 4218 err = virtnet_tx_resize(vi, sq, ring->tx_pending); 4219 if (err) 4220 return err; 4221 4222 /* Upon disabling and re-enabling a transmit virtqueue, the device must 4223 * set the coalescing parameters of the virtqueue to those configured 4224 * through the VIRTIO_NET_CTRL_NOTF_COAL_TX_SET command, or, if the driver 4225 * did not set any TX coalescing parameters, to 0. 4226 */ 4227 err = virtnet_send_tx_ctrl_coal_vq_cmd(vi, i, 4228 vi->intr_coal_tx.max_usecs, 4229 vi->intr_coal_tx.max_packets); 4230 4231 /* Don't break the tx resize action if the vq coalescing is not 4232 * supported. The same is true for rx resize below. 4233 */ 4234 if (err && err != -EOPNOTSUPP) 4235 return err; 4236 } 4237 4238 if (ring->rx_pending != rx_pending) { 4239 err = virtnet_rx_resize(vi, rq, ring->rx_pending); 4240 if (err) 4241 return err; 4242 4243 /* The reason is same as the transmit virtqueue reset */ 4244 mutex_lock(&vi->rq[i].dim_lock); 4245 err = virtnet_send_rx_ctrl_coal_vq_cmd(vi, i, 4246 vi->intr_coal_rx.max_usecs, 4247 vi->intr_coal_rx.max_packets); 4248 mutex_unlock(&vi->rq[i].dim_lock); 4249 if (err && err != -EOPNOTSUPP) 4250 return err; 4251 } 4252 } 4253 4254 return 0; 4255 } 4256 4257 static bool virtnet_commit_rss_command(struct virtnet_info *vi) 4258 { 4259 struct net_device *dev = vi->dev; 4260 struct scatterlist sgs[2]; 4261 4262 /* prepare sgs */ 4263 sg_init_table(sgs, 2); 4264 sg_set_buf(&sgs[0], vi->rss_hdr, virtnet_rss_hdr_size(vi)); 4265 sg_set_buf(&sgs[1], &vi->rss_trailer, virtnet_rss_trailer_size(vi)); 4266 4267 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, 4268 vi->has_rss ? VIRTIO_NET_CTRL_MQ_RSS_CONFIG 4269 : VIRTIO_NET_CTRL_MQ_HASH_CONFIG, sgs)) 4270 goto err; 4271 4272 return true; 4273 4274 err: 4275 dev_warn(&dev->dev, "VIRTIONET issue with committing RSS sgs\n"); 4276 return false; 4277 4278 } 4279 4280 static void virtnet_init_default_rss(struct virtnet_info *vi) 4281 { 4282 vi->rss_hdr->hash_types = cpu_to_le32(vi->rss_hash_types_supported); 4283 vi->rss_hash_types_saved = vi->rss_hash_types_supported; 4284 vi->rss_hdr->indirection_table_mask = vi->rss_indir_table_size 4285 ? cpu_to_le16(vi->rss_indir_table_size - 1) : 0; 4286 vi->rss_hdr->unclassified_queue = 0; 4287 4288 virtnet_rss_update_by_qpairs(vi, vi->curr_queue_pairs); 4289 4290 vi->rss_trailer.hash_key_length = vi->rss_key_size; 4291 4292 netdev_rss_key_fill(vi->rss_hash_key_data, vi->rss_key_size); 4293 } 4294 4295 static int virtnet_get_hashflow(struct net_device *dev, 4296 struct ethtool_rxfh_fields *info) 4297 { 4298 struct virtnet_info *vi = netdev_priv(dev); 4299 4300 info->data = 0; 4301 switch (info->flow_type) { 4302 case TCP_V4_FLOW: 4303 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) { 4304 info->data = RXH_IP_SRC | RXH_IP_DST | 4305 RXH_L4_B_0_1 | RXH_L4_B_2_3; 4306 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) { 4307 info->data = RXH_IP_SRC | RXH_IP_DST; 4308 } 4309 break; 4310 case TCP_V6_FLOW: 4311 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) { 4312 info->data = RXH_IP_SRC | RXH_IP_DST | 4313 RXH_L4_B_0_1 | RXH_L4_B_2_3; 4314 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) { 4315 info->data = RXH_IP_SRC | RXH_IP_DST; 4316 } 4317 break; 4318 case UDP_V4_FLOW: 4319 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) { 4320 info->data = RXH_IP_SRC | RXH_IP_DST | 4321 RXH_L4_B_0_1 | RXH_L4_B_2_3; 4322 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) { 4323 info->data = RXH_IP_SRC | RXH_IP_DST; 4324 } 4325 break; 4326 case UDP_V6_FLOW: 4327 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) { 4328 info->data = RXH_IP_SRC | RXH_IP_DST | 4329 RXH_L4_B_0_1 | RXH_L4_B_2_3; 4330 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) { 4331 info->data = RXH_IP_SRC | RXH_IP_DST; 4332 } 4333 break; 4334 case IPV4_FLOW: 4335 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) 4336 info->data = RXH_IP_SRC | RXH_IP_DST; 4337 4338 break; 4339 case IPV6_FLOW: 4340 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) 4341 info->data = RXH_IP_SRC | RXH_IP_DST; 4342 4343 break; 4344 default: 4345 info->data = 0; 4346 break; 4347 } 4348 4349 return 0; 4350 } 4351 4352 static int virtnet_set_hashflow(struct net_device *dev, 4353 const struct ethtool_rxfh_fields *info, 4354 struct netlink_ext_ack *extack) 4355 { 4356 struct virtnet_info *vi = netdev_priv(dev); 4357 u32 new_hashtypes = vi->rss_hash_types_saved; 4358 bool is_disable = info->data & RXH_DISCARD; 4359 bool is_l4 = info->data == (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3); 4360 4361 /* supports only 'sd', 'sdfn' and 'r' */ 4362 if (!((info->data == (RXH_IP_SRC | RXH_IP_DST)) | is_l4 | is_disable)) 4363 return -EINVAL; 4364 4365 switch (info->flow_type) { 4366 case TCP_V4_FLOW: 4367 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_TCPv4); 4368 if (!is_disable) 4369 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4 4370 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv4 : 0); 4371 break; 4372 case UDP_V4_FLOW: 4373 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_UDPv4); 4374 if (!is_disable) 4375 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4 4376 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv4 : 0); 4377 break; 4378 case IPV4_FLOW: 4379 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv4; 4380 if (!is_disable) 4381 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv4; 4382 break; 4383 case TCP_V6_FLOW: 4384 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_TCPv6); 4385 if (!is_disable) 4386 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6 4387 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv6 : 0); 4388 break; 4389 case UDP_V6_FLOW: 4390 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_UDPv6); 4391 if (!is_disable) 4392 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6 4393 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv6 : 0); 4394 break; 4395 case IPV6_FLOW: 4396 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv6; 4397 if (!is_disable) 4398 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv6; 4399 break; 4400 default: 4401 /* unsupported flow */ 4402 return -EINVAL; 4403 } 4404 4405 /* if unsupported hashtype was set */ 4406 if (new_hashtypes != (new_hashtypes & vi->rss_hash_types_supported)) 4407 return -EINVAL; 4408 4409 if (new_hashtypes != vi->rss_hash_types_saved) { 4410 vi->rss_hash_types_saved = new_hashtypes; 4411 vi->rss_hdr->hash_types = cpu_to_le32(vi->rss_hash_types_saved); 4412 if (vi->dev->features & NETIF_F_RXHASH) 4413 if (!virtnet_commit_rss_command(vi)) 4414 return -EINVAL; 4415 } 4416 4417 return 0; 4418 } 4419 4420 static void virtnet_get_drvinfo(struct net_device *dev, 4421 struct ethtool_drvinfo *info) 4422 { 4423 struct virtnet_info *vi = netdev_priv(dev); 4424 struct virtio_device *vdev = vi->vdev; 4425 4426 strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); 4427 strscpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version)); 4428 strscpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info)); 4429 4430 } 4431 4432 /* TODO: Eliminate OOO packets during switching */ 4433 static int virtnet_set_channels(struct net_device *dev, 4434 struct ethtool_channels *channels) 4435 { 4436 struct virtnet_info *vi = netdev_priv(dev); 4437 u16 queue_pairs = channels->combined_count; 4438 int err; 4439 4440 /* We don't support separate rx/tx channels. 4441 * We don't allow setting 'other' channels. 4442 */ 4443 if (channels->rx_count || channels->tx_count || channels->other_count) 4444 return -EINVAL; 4445 4446 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0) 4447 return -EINVAL; 4448 4449 /* For now we don't support modifying channels while XDP is loaded 4450 * also when XDP is loaded all RX queues have XDP programs so we only 4451 * need to check a single RX queue. 4452 */ 4453 if (vi->rq[0].xdp_prog) 4454 return -EINVAL; 4455 4456 cpus_read_lock(); 4457 err = virtnet_set_queues(vi, queue_pairs); 4458 if (err) { 4459 cpus_read_unlock(); 4460 goto err; 4461 } 4462 virtnet_set_affinity(vi); 4463 cpus_read_unlock(); 4464 4465 netif_set_real_num_tx_queues(dev, queue_pairs); 4466 netif_set_real_num_rx_queues(dev, queue_pairs); 4467 err: 4468 return err; 4469 } 4470 4471 static void virtnet_stats_sprintf(u8 **p, const char *fmt, const char *noq_fmt, 4472 int num, int qid, const struct virtnet_stat_desc *desc) 4473 { 4474 int i; 4475 4476 if (qid < 0) { 4477 for (i = 0; i < num; ++i) 4478 ethtool_sprintf(p, noq_fmt, desc[i].desc); 4479 } else { 4480 for (i = 0; i < num; ++i) 4481 ethtool_sprintf(p, fmt, qid, desc[i].desc); 4482 } 4483 } 4484 4485 /* qid == -1: for rx/tx queue total field */ 4486 static void virtnet_get_stats_string(struct virtnet_info *vi, int type, int qid, u8 **data) 4487 { 4488 const struct virtnet_stat_desc *desc; 4489 const char *fmt, *noq_fmt; 4490 u8 *p = *data; 4491 u32 num; 4492 4493 if (type == VIRTNET_Q_TYPE_CQ && qid >= 0) { 4494 noq_fmt = "cq_hw_%s"; 4495 4496 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_CVQ) { 4497 desc = &virtnet_stats_cvq_desc[0]; 4498 num = ARRAY_SIZE(virtnet_stats_cvq_desc); 4499 4500 virtnet_stats_sprintf(&p, NULL, noq_fmt, num, -1, desc); 4501 } 4502 } 4503 4504 if (type == VIRTNET_Q_TYPE_RX) { 4505 fmt = "rx%u_%s"; 4506 noq_fmt = "rx_%s"; 4507 4508 desc = &virtnet_rq_stats_desc[0]; 4509 num = ARRAY_SIZE(virtnet_rq_stats_desc); 4510 4511 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc); 4512 4513 fmt = "rx%u_hw_%s"; 4514 noq_fmt = "rx_hw_%s"; 4515 4516 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) { 4517 desc = &virtnet_stats_rx_basic_desc[0]; 4518 num = ARRAY_SIZE(virtnet_stats_rx_basic_desc); 4519 4520 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc); 4521 } 4522 4523 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) { 4524 desc = &virtnet_stats_rx_csum_desc[0]; 4525 num = ARRAY_SIZE(virtnet_stats_rx_csum_desc); 4526 4527 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc); 4528 } 4529 4530 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED) { 4531 desc = &virtnet_stats_rx_speed_desc[0]; 4532 num = ARRAY_SIZE(virtnet_stats_rx_speed_desc); 4533 4534 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc); 4535 } 4536 } 4537 4538 if (type == VIRTNET_Q_TYPE_TX) { 4539 fmt = "tx%u_%s"; 4540 noq_fmt = "tx_%s"; 4541 4542 desc = &virtnet_sq_stats_desc[0]; 4543 num = ARRAY_SIZE(virtnet_sq_stats_desc); 4544 4545 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc); 4546 4547 fmt = "tx%u_hw_%s"; 4548 noq_fmt = "tx_hw_%s"; 4549 4550 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) { 4551 desc = &virtnet_stats_tx_basic_desc[0]; 4552 num = ARRAY_SIZE(virtnet_stats_tx_basic_desc); 4553 4554 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc); 4555 } 4556 4557 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) { 4558 desc = &virtnet_stats_tx_gso_desc[0]; 4559 num = ARRAY_SIZE(virtnet_stats_tx_gso_desc); 4560 4561 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc); 4562 } 4563 4564 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED) { 4565 desc = &virtnet_stats_tx_speed_desc[0]; 4566 num = ARRAY_SIZE(virtnet_stats_tx_speed_desc); 4567 4568 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc); 4569 } 4570 } 4571 4572 *data = p; 4573 } 4574 4575 struct virtnet_stats_ctx { 4576 /* The stats are write to qstats or ethtool -S */ 4577 bool to_qstat; 4578 4579 /* Used to calculate the offset inside the output buffer. */ 4580 u32 desc_num[3]; 4581 4582 /* The actual supported stat types. */ 4583 u64 bitmap[3]; 4584 4585 /* Used to calculate the reply buffer size. */ 4586 u32 size[3]; 4587 4588 /* Record the output buffer. */ 4589 u64 *data; 4590 }; 4591 4592 static void virtnet_stats_ctx_init(struct virtnet_info *vi, 4593 struct virtnet_stats_ctx *ctx, 4594 u64 *data, bool to_qstat) 4595 { 4596 u32 queue_type; 4597 4598 ctx->data = data; 4599 ctx->to_qstat = to_qstat; 4600 4601 if (to_qstat) { 4602 ctx->desc_num[VIRTNET_Q_TYPE_RX] = ARRAY_SIZE(virtnet_rq_stats_desc_qstat); 4603 ctx->desc_num[VIRTNET_Q_TYPE_TX] = ARRAY_SIZE(virtnet_sq_stats_desc_qstat); 4604 4605 queue_type = VIRTNET_Q_TYPE_RX; 4606 4607 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) { 4608 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_BASIC; 4609 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_basic_desc_qstat); 4610 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_basic); 4611 } 4612 4613 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) { 4614 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_CSUM; 4615 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_csum_desc_qstat); 4616 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_csum); 4617 } 4618 4619 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_GSO) { 4620 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_GSO; 4621 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_gso_desc_qstat); 4622 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_gso); 4623 } 4624 4625 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED) { 4626 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_SPEED; 4627 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_speed_desc_qstat); 4628 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_speed); 4629 } 4630 4631 queue_type = VIRTNET_Q_TYPE_TX; 4632 4633 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) { 4634 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_BASIC; 4635 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_basic_desc_qstat); 4636 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_basic); 4637 } 4638 4639 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_CSUM) { 4640 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_CSUM; 4641 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_csum_desc_qstat); 4642 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_csum); 4643 } 4644 4645 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) { 4646 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_GSO; 4647 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_gso_desc_qstat); 4648 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_gso); 4649 } 4650 4651 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED) { 4652 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_SPEED; 4653 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_speed_desc_qstat); 4654 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_speed); 4655 } 4656 4657 return; 4658 } 4659 4660 ctx->desc_num[VIRTNET_Q_TYPE_RX] = ARRAY_SIZE(virtnet_rq_stats_desc); 4661 ctx->desc_num[VIRTNET_Q_TYPE_TX] = ARRAY_SIZE(virtnet_sq_stats_desc); 4662 4663 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_CVQ) { 4664 queue_type = VIRTNET_Q_TYPE_CQ; 4665 4666 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_CVQ; 4667 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_cvq_desc); 4668 ctx->size[queue_type] += sizeof(struct virtio_net_stats_cvq); 4669 } 4670 4671 queue_type = VIRTNET_Q_TYPE_RX; 4672 4673 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) { 4674 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_BASIC; 4675 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_basic_desc); 4676 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_basic); 4677 } 4678 4679 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) { 4680 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_CSUM; 4681 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_csum_desc); 4682 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_csum); 4683 } 4684 4685 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED) { 4686 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_SPEED; 4687 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_speed_desc); 4688 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_speed); 4689 } 4690 4691 queue_type = VIRTNET_Q_TYPE_TX; 4692 4693 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) { 4694 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_BASIC; 4695 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_basic_desc); 4696 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_basic); 4697 } 4698 4699 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) { 4700 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_GSO; 4701 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_gso_desc); 4702 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_gso); 4703 } 4704 4705 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED) { 4706 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_SPEED; 4707 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_speed_desc); 4708 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_speed); 4709 } 4710 } 4711 4712 /* stats_sum_queue - Calculate the sum of the same fields in sq or rq. 4713 * @sum: the position to store the sum values 4714 * @num: field num 4715 * @q_value: the first queue fields 4716 * @q_num: number of the queues 4717 */ 4718 static void stats_sum_queue(u64 *sum, u32 num, u64 *q_value, u32 q_num) 4719 { 4720 u32 step = num; 4721 int i, j; 4722 u64 *p; 4723 4724 for (i = 0; i < num; ++i) { 4725 p = sum + i; 4726 *p = 0; 4727 4728 for (j = 0; j < q_num; ++j) 4729 *p += *(q_value + i + j * step); 4730 } 4731 } 4732 4733 static void virtnet_fill_total_fields(struct virtnet_info *vi, 4734 struct virtnet_stats_ctx *ctx) 4735 { 4736 u64 *data, *first_rx_q, *first_tx_q; 4737 u32 num_cq, num_rx, num_tx; 4738 4739 num_cq = ctx->desc_num[VIRTNET_Q_TYPE_CQ]; 4740 num_rx = ctx->desc_num[VIRTNET_Q_TYPE_RX]; 4741 num_tx = ctx->desc_num[VIRTNET_Q_TYPE_TX]; 4742 4743 first_rx_q = ctx->data + num_rx + num_tx + num_cq; 4744 first_tx_q = first_rx_q + vi->curr_queue_pairs * num_rx; 4745 4746 data = ctx->data; 4747 4748 stats_sum_queue(data, num_rx, first_rx_q, vi->curr_queue_pairs); 4749 4750 data = ctx->data + num_rx; 4751 4752 stats_sum_queue(data, num_tx, first_tx_q, vi->curr_queue_pairs); 4753 } 4754 4755 static void virtnet_fill_stats_qstat(struct virtnet_info *vi, u32 qid, 4756 struct virtnet_stats_ctx *ctx, 4757 const u8 *base, bool drv_stats, u8 reply_type) 4758 { 4759 const struct virtnet_stat_desc *desc; 4760 const u64_stats_t *v_stat; 4761 u64 offset, bitmap; 4762 const __le64 *v; 4763 u32 queue_type; 4764 int i, num; 4765 4766 queue_type = vq_type(vi, qid); 4767 bitmap = ctx->bitmap[queue_type]; 4768 4769 if (drv_stats) { 4770 if (queue_type == VIRTNET_Q_TYPE_RX) { 4771 desc = &virtnet_rq_stats_desc_qstat[0]; 4772 num = ARRAY_SIZE(virtnet_rq_stats_desc_qstat); 4773 } else { 4774 desc = &virtnet_sq_stats_desc_qstat[0]; 4775 num = ARRAY_SIZE(virtnet_sq_stats_desc_qstat); 4776 } 4777 4778 for (i = 0; i < num; ++i) { 4779 offset = desc[i].qstat_offset / sizeof(*ctx->data); 4780 v_stat = (const u64_stats_t *)(base + desc[i].offset); 4781 ctx->data[offset] = u64_stats_read(v_stat); 4782 } 4783 return; 4784 } 4785 4786 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_BASIC) { 4787 desc = &virtnet_stats_rx_basic_desc_qstat[0]; 4788 num = ARRAY_SIZE(virtnet_stats_rx_basic_desc_qstat); 4789 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_BASIC) 4790 goto found; 4791 } 4792 4793 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_CSUM) { 4794 desc = &virtnet_stats_rx_csum_desc_qstat[0]; 4795 num = ARRAY_SIZE(virtnet_stats_rx_csum_desc_qstat); 4796 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_CSUM) 4797 goto found; 4798 } 4799 4800 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_GSO) { 4801 desc = &virtnet_stats_rx_gso_desc_qstat[0]; 4802 num = ARRAY_SIZE(virtnet_stats_rx_gso_desc_qstat); 4803 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_GSO) 4804 goto found; 4805 } 4806 4807 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_SPEED) { 4808 desc = &virtnet_stats_rx_speed_desc_qstat[0]; 4809 num = ARRAY_SIZE(virtnet_stats_rx_speed_desc_qstat); 4810 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_SPEED) 4811 goto found; 4812 } 4813 4814 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_BASIC) { 4815 desc = &virtnet_stats_tx_basic_desc_qstat[0]; 4816 num = ARRAY_SIZE(virtnet_stats_tx_basic_desc_qstat); 4817 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_BASIC) 4818 goto found; 4819 } 4820 4821 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_CSUM) { 4822 desc = &virtnet_stats_tx_csum_desc_qstat[0]; 4823 num = ARRAY_SIZE(virtnet_stats_tx_csum_desc_qstat); 4824 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_CSUM) 4825 goto found; 4826 } 4827 4828 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_GSO) { 4829 desc = &virtnet_stats_tx_gso_desc_qstat[0]; 4830 num = ARRAY_SIZE(virtnet_stats_tx_gso_desc_qstat); 4831 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_GSO) 4832 goto found; 4833 } 4834 4835 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_SPEED) { 4836 desc = &virtnet_stats_tx_speed_desc_qstat[0]; 4837 num = ARRAY_SIZE(virtnet_stats_tx_speed_desc_qstat); 4838 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_SPEED) 4839 goto found; 4840 } 4841 4842 return; 4843 4844 found: 4845 for (i = 0; i < num; ++i) { 4846 offset = desc[i].qstat_offset / sizeof(*ctx->data); 4847 v = (const __le64 *)(base + desc[i].offset); 4848 ctx->data[offset] = le64_to_cpu(*v); 4849 } 4850 } 4851 4852 /* virtnet_fill_stats - copy the stats to qstats or ethtool -S 4853 * The stats source is the device or the driver. 4854 * 4855 * @vi: virtio net info 4856 * @qid: the vq id 4857 * @ctx: stats ctx (initiated by virtnet_stats_ctx_init()) 4858 * @base: pointer to the device reply or the driver stats structure. 4859 * @drv_stats: designate the base type (device reply, driver stats) 4860 * @type: the type of the device reply (if drv_stats is true, this must be zero) 4861 */ 4862 static void virtnet_fill_stats(struct virtnet_info *vi, u32 qid, 4863 struct virtnet_stats_ctx *ctx, 4864 const u8 *base, bool drv_stats, u8 reply_type) 4865 { 4866 u32 queue_type, num_rx, num_tx, num_cq; 4867 const struct virtnet_stat_desc *desc; 4868 const u64_stats_t *v_stat; 4869 u64 offset, bitmap; 4870 const __le64 *v; 4871 int i, num; 4872 4873 if (ctx->to_qstat) 4874 return virtnet_fill_stats_qstat(vi, qid, ctx, base, drv_stats, reply_type); 4875 4876 num_cq = ctx->desc_num[VIRTNET_Q_TYPE_CQ]; 4877 num_rx = ctx->desc_num[VIRTNET_Q_TYPE_RX]; 4878 num_tx = ctx->desc_num[VIRTNET_Q_TYPE_TX]; 4879 4880 queue_type = vq_type(vi, qid); 4881 bitmap = ctx->bitmap[queue_type]; 4882 4883 /* skip the total fields of pairs */ 4884 offset = num_rx + num_tx; 4885 4886 if (queue_type == VIRTNET_Q_TYPE_TX) { 4887 offset += num_cq + num_rx * vi->curr_queue_pairs + num_tx * (qid / 2); 4888 4889 num = ARRAY_SIZE(virtnet_sq_stats_desc); 4890 if (drv_stats) { 4891 desc = &virtnet_sq_stats_desc[0]; 4892 goto drv_stats; 4893 } 4894 4895 offset += num; 4896 4897 } else if (queue_type == VIRTNET_Q_TYPE_RX) { 4898 offset += num_cq + num_rx * (qid / 2); 4899 4900 num = ARRAY_SIZE(virtnet_rq_stats_desc); 4901 if (drv_stats) { 4902 desc = &virtnet_rq_stats_desc[0]; 4903 goto drv_stats; 4904 } 4905 4906 offset += num; 4907 } 4908 4909 if (bitmap & VIRTIO_NET_STATS_TYPE_CVQ) { 4910 desc = &virtnet_stats_cvq_desc[0]; 4911 num = ARRAY_SIZE(virtnet_stats_cvq_desc); 4912 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_CVQ) 4913 goto found; 4914 4915 offset += num; 4916 } 4917 4918 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_BASIC) { 4919 desc = &virtnet_stats_rx_basic_desc[0]; 4920 num = ARRAY_SIZE(virtnet_stats_rx_basic_desc); 4921 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_BASIC) 4922 goto found; 4923 4924 offset += num; 4925 } 4926 4927 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_CSUM) { 4928 desc = &virtnet_stats_rx_csum_desc[0]; 4929 num = ARRAY_SIZE(virtnet_stats_rx_csum_desc); 4930 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_CSUM) 4931 goto found; 4932 4933 offset += num; 4934 } 4935 4936 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_SPEED) { 4937 desc = &virtnet_stats_rx_speed_desc[0]; 4938 num = ARRAY_SIZE(virtnet_stats_rx_speed_desc); 4939 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_SPEED) 4940 goto found; 4941 4942 offset += num; 4943 } 4944 4945 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_BASIC) { 4946 desc = &virtnet_stats_tx_basic_desc[0]; 4947 num = ARRAY_SIZE(virtnet_stats_tx_basic_desc); 4948 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_BASIC) 4949 goto found; 4950 4951 offset += num; 4952 } 4953 4954 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_GSO) { 4955 desc = &virtnet_stats_tx_gso_desc[0]; 4956 num = ARRAY_SIZE(virtnet_stats_tx_gso_desc); 4957 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_GSO) 4958 goto found; 4959 4960 offset += num; 4961 } 4962 4963 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_SPEED) { 4964 desc = &virtnet_stats_tx_speed_desc[0]; 4965 num = ARRAY_SIZE(virtnet_stats_tx_speed_desc); 4966 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_SPEED) 4967 goto found; 4968 4969 offset += num; 4970 } 4971 4972 return; 4973 4974 found: 4975 for (i = 0; i < num; ++i) { 4976 v = (const __le64 *)(base + desc[i].offset); 4977 ctx->data[offset + i] = le64_to_cpu(*v); 4978 } 4979 4980 return; 4981 4982 drv_stats: 4983 for (i = 0; i < num; ++i) { 4984 v_stat = (const u64_stats_t *)(base + desc[i].offset); 4985 ctx->data[offset + i] = u64_stats_read(v_stat); 4986 } 4987 } 4988 4989 static int __virtnet_get_hw_stats(struct virtnet_info *vi, 4990 struct virtnet_stats_ctx *ctx, 4991 struct virtio_net_ctrl_queue_stats *req, 4992 int req_size, void *reply, int res_size) 4993 { 4994 struct virtio_net_stats_reply_hdr *hdr; 4995 struct scatterlist sgs_in, sgs_out; 4996 void *p; 4997 u32 qid; 4998 int ok; 4999 5000 sg_init_one(&sgs_out, req, req_size); 5001 sg_init_one(&sgs_in, reply, res_size); 5002 5003 ok = virtnet_send_command_reply(vi, VIRTIO_NET_CTRL_STATS, 5004 VIRTIO_NET_CTRL_STATS_GET, 5005 &sgs_out, &sgs_in); 5006 5007 if (!ok) 5008 return ok; 5009 5010 for (p = reply; p - reply < res_size; p += le16_to_cpu(hdr->size)) { 5011 hdr = p; 5012 qid = le16_to_cpu(hdr->vq_index); 5013 virtnet_fill_stats(vi, qid, ctx, p, false, hdr->type); 5014 } 5015 5016 return 0; 5017 } 5018 5019 static void virtnet_make_stat_req(struct virtnet_info *vi, 5020 struct virtnet_stats_ctx *ctx, 5021 struct virtio_net_ctrl_queue_stats *req, 5022 int qid, int *idx) 5023 { 5024 int qtype = vq_type(vi, qid); 5025 u64 bitmap = ctx->bitmap[qtype]; 5026 5027 if (!bitmap) 5028 return; 5029 5030 req->stats[*idx].vq_index = cpu_to_le16(qid); 5031 req->stats[*idx].types_bitmap[0] = cpu_to_le64(bitmap); 5032 *idx += 1; 5033 } 5034 5035 /* qid: -1: get stats of all vq. 5036 * > 0: get the stats for the special vq. This must not be cvq. 5037 */ 5038 static int virtnet_get_hw_stats(struct virtnet_info *vi, 5039 struct virtnet_stats_ctx *ctx, int qid) 5040 { 5041 int qnum, i, j, res_size, qtype, last_vq, first_vq; 5042 struct virtio_net_ctrl_queue_stats *req; 5043 bool enable_cvq; 5044 void *reply; 5045 int ok; 5046 5047 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_DEVICE_STATS)) 5048 return 0; 5049 5050 if (qid == -1) { 5051 last_vq = vi->curr_queue_pairs * 2 - 1; 5052 first_vq = 0; 5053 enable_cvq = true; 5054 } else { 5055 last_vq = qid; 5056 first_vq = qid; 5057 enable_cvq = false; 5058 } 5059 5060 qnum = 0; 5061 res_size = 0; 5062 for (i = first_vq; i <= last_vq ; ++i) { 5063 qtype = vq_type(vi, i); 5064 if (ctx->bitmap[qtype]) { 5065 ++qnum; 5066 res_size += ctx->size[qtype]; 5067 } 5068 } 5069 5070 if (enable_cvq && ctx->bitmap[VIRTNET_Q_TYPE_CQ]) { 5071 res_size += ctx->size[VIRTNET_Q_TYPE_CQ]; 5072 qnum += 1; 5073 } 5074 5075 req = kcalloc(qnum, sizeof(*req), GFP_KERNEL); 5076 if (!req) 5077 return -ENOMEM; 5078 5079 reply = kmalloc(res_size, GFP_KERNEL); 5080 if (!reply) { 5081 kfree(req); 5082 return -ENOMEM; 5083 } 5084 5085 j = 0; 5086 for (i = first_vq; i <= last_vq ; ++i) 5087 virtnet_make_stat_req(vi, ctx, req, i, &j); 5088 5089 if (enable_cvq) 5090 virtnet_make_stat_req(vi, ctx, req, vi->max_queue_pairs * 2, &j); 5091 5092 ok = __virtnet_get_hw_stats(vi, ctx, req, sizeof(*req) * j, reply, res_size); 5093 5094 kfree(req); 5095 kfree(reply); 5096 5097 return ok; 5098 } 5099 5100 static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data) 5101 { 5102 struct virtnet_info *vi = netdev_priv(dev); 5103 unsigned int i; 5104 u8 *p = data; 5105 5106 switch (stringset) { 5107 case ETH_SS_STATS: 5108 /* Generate the total field names. */ 5109 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_RX, -1, &p); 5110 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_TX, -1, &p); 5111 5112 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_CQ, 0, &p); 5113 5114 for (i = 0; i < vi->curr_queue_pairs; ++i) 5115 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_RX, i, &p); 5116 5117 for (i = 0; i < vi->curr_queue_pairs; ++i) 5118 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_TX, i, &p); 5119 break; 5120 } 5121 } 5122 5123 static int virtnet_get_sset_count(struct net_device *dev, int sset) 5124 { 5125 struct virtnet_info *vi = netdev_priv(dev); 5126 struct virtnet_stats_ctx ctx = {0}; 5127 u32 pair_count; 5128 5129 switch (sset) { 5130 case ETH_SS_STATS: 5131 virtnet_stats_ctx_init(vi, &ctx, NULL, false); 5132 5133 pair_count = ctx.desc_num[VIRTNET_Q_TYPE_RX] + ctx.desc_num[VIRTNET_Q_TYPE_TX]; 5134 5135 return pair_count + ctx.desc_num[VIRTNET_Q_TYPE_CQ] + 5136 vi->curr_queue_pairs * pair_count; 5137 default: 5138 return -EOPNOTSUPP; 5139 } 5140 } 5141 5142 static void virtnet_get_ethtool_stats(struct net_device *dev, 5143 struct ethtool_stats *stats, u64 *data) 5144 { 5145 struct virtnet_info *vi = netdev_priv(dev); 5146 struct virtnet_stats_ctx ctx = {0}; 5147 unsigned int start, i; 5148 const u8 *stats_base; 5149 5150 virtnet_stats_ctx_init(vi, &ctx, data, false); 5151 if (virtnet_get_hw_stats(vi, &ctx, -1)) 5152 dev_warn(&vi->dev->dev, "Failed to get hw stats.\n"); 5153 5154 for (i = 0; i < vi->curr_queue_pairs; i++) { 5155 struct receive_queue *rq = &vi->rq[i]; 5156 struct send_queue *sq = &vi->sq[i]; 5157 5158 stats_base = (const u8 *)&rq->stats; 5159 do { 5160 start = u64_stats_fetch_begin(&rq->stats.syncp); 5161 virtnet_fill_stats(vi, i * 2, &ctx, stats_base, true, 0); 5162 } while (u64_stats_fetch_retry(&rq->stats.syncp, start)); 5163 5164 stats_base = (const u8 *)&sq->stats; 5165 do { 5166 start = u64_stats_fetch_begin(&sq->stats.syncp); 5167 virtnet_fill_stats(vi, i * 2 + 1, &ctx, stats_base, true, 0); 5168 } while (u64_stats_fetch_retry(&sq->stats.syncp, start)); 5169 } 5170 5171 virtnet_fill_total_fields(vi, &ctx); 5172 } 5173 5174 static void virtnet_get_channels(struct net_device *dev, 5175 struct ethtool_channels *channels) 5176 { 5177 struct virtnet_info *vi = netdev_priv(dev); 5178 5179 channels->combined_count = vi->curr_queue_pairs; 5180 channels->max_combined = vi->max_queue_pairs; 5181 channels->max_other = 0; 5182 channels->rx_count = 0; 5183 channels->tx_count = 0; 5184 channels->other_count = 0; 5185 } 5186 5187 static int virtnet_set_link_ksettings(struct net_device *dev, 5188 const struct ethtool_link_ksettings *cmd) 5189 { 5190 struct virtnet_info *vi = netdev_priv(dev); 5191 5192 return ethtool_virtdev_set_link_ksettings(dev, cmd, 5193 &vi->speed, &vi->duplex); 5194 } 5195 5196 static int virtnet_get_link_ksettings(struct net_device *dev, 5197 struct ethtool_link_ksettings *cmd) 5198 { 5199 struct virtnet_info *vi = netdev_priv(dev); 5200 5201 cmd->base.speed = vi->speed; 5202 cmd->base.duplex = vi->duplex; 5203 cmd->base.port = PORT_OTHER; 5204 5205 return 0; 5206 } 5207 5208 static int virtnet_send_tx_notf_coal_cmds(struct virtnet_info *vi, 5209 struct ethtool_coalesce *ec) 5210 { 5211 struct virtio_net_ctrl_coal_tx *coal_tx __free(kfree) = NULL; 5212 struct scatterlist sgs_tx; 5213 int i; 5214 5215 coal_tx = kzalloc(sizeof(*coal_tx), GFP_KERNEL); 5216 if (!coal_tx) 5217 return -ENOMEM; 5218 5219 coal_tx->tx_usecs = cpu_to_le32(ec->tx_coalesce_usecs); 5220 coal_tx->tx_max_packets = cpu_to_le32(ec->tx_max_coalesced_frames); 5221 sg_init_one(&sgs_tx, coal_tx, sizeof(*coal_tx)); 5222 5223 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL, 5224 VIRTIO_NET_CTRL_NOTF_COAL_TX_SET, 5225 &sgs_tx)) 5226 return -EINVAL; 5227 5228 vi->intr_coal_tx.max_usecs = ec->tx_coalesce_usecs; 5229 vi->intr_coal_tx.max_packets = ec->tx_max_coalesced_frames; 5230 for (i = 0; i < vi->max_queue_pairs; i++) { 5231 vi->sq[i].intr_coal.max_usecs = ec->tx_coalesce_usecs; 5232 vi->sq[i].intr_coal.max_packets = ec->tx_max_coalesced_frames; 5233 } 5234 5235 return 0; 5236 } 5237 5238 static int virtnet_send_rx_notf_coal_cmds(struct virtnet_info *vi, 5239 struct ethtool_coalesce *ec) 5240 { 5241 struct virtio_net_ctrl_coal_rx *coal_rx __free(kfree) = NULL; 5242 bool rx_ctrl_dim_on = !!ec->use_adaptive_rx_coalesce; 5243 struct scatterlist sgs_rx; 5244 int i; 5245 5246 if (rx_ctrl_dim_on && !virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) 5247 return -EOPNOTSUPP; 5248 5249 if (rx_ctrl_dim_on && (ec->rx_coalesce_usecs != vi->intr_coal_rx.max_usecs || 5250 ec->rx_max_coalesced_frames != vi->intr_coal_rx.max_packets)) 5251 return -EINVAL; 5252 5253 if (rx_ctrl_dim_on && !vi->rx_dim_enabled) { 5254 vi->rx_dim_enabled = true; 5255 for (i = 0; i < vi->max_queue_pairs; i++) { 5256 mutex_lock(&vi->rq[i].dim_lock); 5257 vi->rq[i].dim_enabled = true; 5258 mutex_unlock(&vi->rq[i].dim_lock); 5259 } 5260 return 0; 5261 } 5262 5263 coal_rx = kzalloc(sizeof(*coal_rx), GFP_KERNEL); 5264 if (!coal_rx) 5265 return -ENOMEM; 5266 5267 if (!rx_ctrl_dim_on && vi->rx_dim_enabled) { 5268 vi->rx_dim_enabled = false; 5269 for (i = 0; i < vi->max_queue_pairs; i++) { 5270 mutex_lock(&vi->rq[i].dim_lock); 5271 vi->rq[i].dim_enabled = false; 5272 mutex_unlock(&vi->rq[i].dim_lock); 5273 } 5274 } 5275 5276 /* Since the per-queue coalescing params can be set, 5277 * we need apply the global new params even if they 5278 * are not updated. 5279 */ 5280 coal_rx->rx_usecs = cpu_to_le32(ec->rx_coalesce_usecs); 5281 coal_rx->rx_max_packets = cpu_to_le32(ec->rx_max_coalesced_frames); 5282 sg_init_one(&sgs_rx, coal_rx, sizeof(*coal_rx)); 5283 5284 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL, 5285 VIRTIO_NET_CTRL_NOTF_COAL_RX_SET, 5286 &sgs_rx)) 5287 return -EINVAL; 5288 5289 vi->intr_coal_rx.max_usecs = ec->rx_coalesce_usecs; 5290 vi->intr_coal_rx.max_packets = ec->rx_max_coalesced_frames; 5291 for (i = 0; i < vi->max_queue_pairs; i++) { 5292 mutex_lock(&vi->rq[i].dim_lock); 5293 vi->rq[i].intr_coal.max_usecs = ec->rx_coalesce_usecs; 5294 vi->rq[i].intr_coal.max_packets = ec->rx_max_coalesced_frames; 5295 mutex_unlock(&vi->rq[i].dim_lock); 5296 } 5297 5298 return 0; 5299 } 5300 5301 static int virtnet_send_notf_coal_cmds(struct virtnet_info *vi, 5302 struct ethtool_coalesce *ec) 5303 { 5304 int err; 5305 5306 err = virtnet_send_tx_notf_coal_cmds(vi, ec); 5307 if (err) 5308 return err; 5309 5310 err = virtnet_send_rx_notf_coal_cmds(vi, ec); 5311 if (err) 5312 return err; 5313 5314 return 0; 5315 } 5316 5317 static int virtnet_send_rx_notf_coal_vq_cmds(struct virtnet_info *vi, 5318 struct ethtool_coalesce *ec, 5319 u16 queue) 5320 { 5321 bool rx_ctrl_dim_on = !!ec->use_adaptive_rx_coalesce; 5322 u32 max_usecs, max_packets; 5323 bool cur_rx_dim; 5324 int err; 5325 5326 mutex_lock(&vi->rq[queue].dim_lock); 5327 cur_rx_dim = vi->rq[queue].dim_enabled; 5328 max_usecs = vi->rq[queue].intr_coal.max_usecs; 5329 max_packets = vi->rq[queue].intr_coal.max_packets; 5330 5331 if (rx_ctrl_dim_on && (ec->rx_coalesce_usecs != max_usecs || 5332 ec->rx_max_coalesced_frames != max_packets)) { 5333 mutex_unlock(&vi->rq[queue].dim_lock); 5334 return -EINVAL; 5335 } 5336 5337 if (rx_ctrl_dim_on && !cur_rx_dim) { 5338 vi->rq[queue].dim_enabled = true; 5339 mutex_unlock(&vi->rq[queue].dim_lock); 5340 return 0; 5341 } 5342 5343 if (!rx_ctrl_dim_on && cur_rx_dim) 5344 vi->rq[queue].dim_enabled = false; 5345 5346 /* If no params are updated, userspace ethtool will 5347 * reject the modification. 5348 */ 5349 err = virtnet_send_rx_ctrl_coal_vq_cmd(vi, queue, 5350 ec->rx_coalesce_usecs, 5351 ec->rx_max_coalesced_frames); 5352 mutex_unlock(&vi->rq[queue].dim_lock); 5353 return err; 5354 } 5355 5356 static int virtnet_send_notf_coal_vq_cmds(struct virtnet_info *vi, 5357 struct ethtool_coalesce *ec, 5358 u16 queue) 5359 { 5360 int err; 5361 5362 err = virtnet_send_rx_notf_coal_vq_cmds(vi, ec, queue); 5363 if (err) 5364 return err; 5365 5366 err = virtnet_send_tx_ctrl_coal_vq_cmd(vi, queue, 5367 ec->tx_coalesce_usecs, 5368 ec->tx_max_coalesced_frames); 5369 if (err) 5370 return err; 5371 5372 return 0; 5373 } 5374 5375 static void virtnet_rx_dim_work(struct work_struct *work) 5376 { 5377 struct dim *dim = container_of(work, struct dim, work); 5378 struct receive_queue *rq = container_of(dim, 5379 struct receive_queue, dim); 5380 struct virtnet_info *vi = rq->vq->vdev->priv; 5381 struct net_device *dev = vi->dev; 5382 struct dim_cq_moder update_moder; 5383 int qnum, err; 5384 5385 qnum = rq - vi->rq; 5386 5387 mutex_lock(&rq->dim_lock); 5388 if (!rq->dim_enabled) 5389 goto out; 5390 5391 update_moder = net_dim_get_rx_irq_moder(dev, dim); 5392 if (update_moder.usec != rq->intr_coal.max_usecs || 5393 update_moder.pkts != rq->intr_coal.max_packets) { 5394 err = virtnet_send_rx_ctrl_coal_vq_cmd(vi, qnum, 5395 update_moder.usec, 5396 update_moder.pkts); 5397 if (err) 5398 pr_debug("%s: Failed to send dim parameters on rxq%d\n", 5399 dev->name, qnum); 5400 } 5401 out: 5402 dim->state = DIM_START_MEASURE; 5403 mutex_unlock(&rq->dim_lock); 5404 } 5405 5406 static int virtnet_coal_params_supported(struct ethtool_coalesce *ec) 5407 { 5408 /* usecs coalescing is supported only if VIRTIO_NET_F_NOTF_COAL 5409 * or VIRTIO_NET_F_VQ_NOTF_COAL feature is negotiated. 5410 */ 5411 if (ec->rx_coalesce_usecs || ec->tx_coalesce_usecs) 5412 return -EOPNOTSUPP; 5413 5414 if (ec->tx_max_coalesced_frames > 1 || 5415 ec->rx_max_coalesced_frames != 1) 5416 return -EINVAL; 5417 5418 return 0; 5419 } 5420 5421 static int virtnet_should_update_vq_weight(int dev_flags, int weight, 5422 int vq_weight, bool *should_update) 5423 { 5424 if (weight ^ vq_weight) { 5425 if (dev_flags & IFF_UP) 5426 return -EBUSY; 5427 *should_update = true; 5428 } 5429 5430 return 0; 5431 } 5432 5433 static int virtnet_set_coalesce(struct net_device *dev, 5434 struct ethtool_coalesce *ec, 5435 struct kernel_ethtool_coalesce *kernel_coal, 5436 struct netlink_ext_ack *extack) 5437 { 5438 struct virtnet_info *vi = netdev_priv(dev); 5439 int ret, queue_number, napi_weight, i; 5440 bool update_napi = false; 5441 5442 /* Can't change NAPI weight if the link is up */ 5443 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0; 5444 for (queue_number = 0; queue_number < vi->max_queue_pairs; queue_number++) { 5445 ret = virtnet_should_update_vq_weight(dev->flags, napi_weight, 5446 vi->sq[queue_number].napi.weight, 5447 &update_napi); 5448 if (ret) 5449 return ret; 5450 5451 if (update_napi) { 5452 /* All queues that belong to [queue_number, vi->max_queue_pairs] will be 5453 * updated for the sake of simplicity, which might not be necessary 5454 */ 5455 break; 5456 } 5457 } 5458 5459 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) 5460 ret = virtnet_send_notf_coal_cmds(vi, ec); 5461 else 5462 ret = virtnet_coal_params_supported(ec); 5463 5464 if (ret) 5465 return ret; 5466 5467 if (update_napi) { 5468 /* xsk xmit depends on the tx napi. So if xsk is active, 5469 * prevent modifications to tx napi. 5470 */ 5471 for (i = queue_number; i < vi->max_queue_pairs; i++) { 5472 if (vi->sq[i].xsk_pool) 5473 return -EBUSY; 5474 } 5475 5476 for (; queue_number < vi->max_queue_pairs; queue_number++) 5477 vi->sq[queue_number].napi.weight = napi_weight; 5478 } 5479 5480 return ret; 5481 } 5482 5483 static int virtnet_get_coalesce(struct net_device *dev, 5484 struct ethtool_coalesce *ec, 5485 struct kernel_ethtool_coalesce *kernel_coal, 5486 struct netlink_ext_ack *extack) 5487 { 5488 struct virtnet_info *vi = netdev_priv(dev); 5489 5490 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) { 5491 ec->rx_coalesce_usecs = vi->intr_coal_rx.max_usecs; 5492 ec->tx_coalesce_usecs = vi->intr_coal_tx.max_usecs; 5493 ec->tx_max_coalesced_frames = vi->intr_coal_tx.max_packets; 5494 ec->rx_max_coalesced_frames = vi->intr_coal_rx.max_packets; 5495 ec->use_adaptive_rx_coalesce = vi->rx_dim_enabled; 5496 } else { 5497 ec->rx_max_coalesced_frames = 1; 5498 5499 if (vi->sq[0].napi.weight) 5500 ec->tx_max_coalesced_frames = 1; 5501 } 5502 5503 return 0; 5504 } 5505 5506 static int virtnet_set_per_queue_coalesce(struct net_device *dev, 5507 u32 queue, 5508 struct ethtool_coalesce *ec) 5509 { 5510 struct virtnet_info *vi = netdev_priv(dev); 5511 int ret, napi_weight; 5512 bool update_napi = false; 5513 5514 if (queue >= vi->max_queue_pairs) 5515 return -EINVAL; 5516 5517 /* Can't change NAPI weight if the link is up */ 5518 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0; 5519 ret = virtnet_should_update_vq_weight(dev->flags, napi_weight, 5520 vi->sq[queue].napi.weight, 5521 &update_napi); 5522 if (ret) 5523 return ret; 5524 5525 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) 5526 ret = virtnet_send_notf_coal_vq_cmds(vi, ec, queue); 5527 else 5528 ret = virtnet_coal_params_supported(ec); 5529 5530 if (ret) 5531 return ret; 5532 5533 if (update_napi) 5534 vi->sq[queue].napi.weight = napi_weight; 5535 5536 return 0; 5537 } 5538 5539 static int virtnet_get_per_queue_coalesce(struct net_device *dev, 5540 u32 queue, 5541 struct ethtool_coalesce *ec) 5542 { 5543 struct virtnet_info *vi = netdev_priv(dev); 5544 5545 if (queue >= vi->max_queue_pairs) 5546 return -EINVAL; 5547 5548 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) { 5549 mutex_lock(&vi->rq[queue].dim_lock); 5550 ec->rx_coalesce_usecs = vi->rq[queue].intr_coal.max_usecs; 5551 ec->tx_coalesce_usecs = vi->sq[queue].intr_coal.max_usecs; 5552 ec->tx_max_coalesced_frames = vi->sq[queue].intr_coal.max_packets; 5553 ec->rx_max_coalesced_frames = vi->rq[queue].intr_coal.max_packets; 5554 ec->use_adaptive_rx_coalesce = vi->rq[queue].dim_enabled; 5555 mutex_unlock(&vi->rq[queue].dim_lock); 5556 } else { 5557 ec->rx_max_coalesced_frames = 1; 5558 5559 if (vi->sq[queue].napi.weight) 5560 ec->tx_max_coalesced_frames = 1; 5561 } 5562 5563 return 0; 5564 } 5565 5566 static void virtnet_init_settings(struct net_device *dev) 5567 { 5568 struct virtnet_info *vi = netdev_priv(dev); 5569 5570 vi->speed = SPEED_UNKNOWN; 5571 vi->duplex = DUPLEX_UNKNOWN; 5572 } 5573 5574 static u32 virtnet_get_rxfh_key_size(struct net_device *dev) 5575 { 5576 return ((struct virtnet_info *)netdev_priv(dev))->rss_key_size; 5577 } 5578 5579 static u32 virtnet_get_rxfh_indir_size(struct net_device *dev) 5580 { 5581 return ((struct virtnet_info *)netdev_priv(dev))->rss_indir_table_size; 5582 } 5583 5584 static int virtnet_get_rxfh(struct net_device *dev, 5585 struct ethtool_rxfh_param *rxfh) 5586 { 5587 struct virtnet_info *vi = netdev_priv(dev); 5588 int i; 5589 5590 if (rxfh->indir) { 5591 for (i = 0; i < vi->rss_indir_table_size; ++i) 5592 rxfh->indir[i] = le16_to_cpu(vi->rss_hdr->indirection_table[i]); 5593 } 5594 5595 if (rxfh->key) 5596 memcpy(rxfh->key, vi->rss_hash_key_data, vi->rss_key_size); 5597 5598 rxfh->hfunc = ETH_RSS_HASH_TOP; 5599 5600 return 0; 5601 } 5602 5603 static int virtnet_set_rxfh(struct net_device *dev, 5604 struct ethtool_rxfh_param *rxfh, 5605 struct netlink_ext_ack *extack) 5606 { 5607 struct virtnet_info *vi = netdev_priv(dev); 5608 bool update = false; 5609 int i; 5610 5611 if (rxfh->hfunc != ETH_RSS_HASH_NO_CHANGE && 5612 rxfh->hfunc != ETH_RSS_HASH_TOP) 5613 return -EOPNOTSUPP; 5614 5615 if (rxfh->indir) { 5616 if (!vi->has_rss) 5617 return -EOPNOTSUPP; 5618 5619 for (i = 0; i < vi->rss_indir_table_size; ++i) 5620 vi->rss_hdr->indirection_table[i] = cpu_to_le16(rxfh->indir[i]); 5621 update = true; 5622 } 5623 5624 if (rxfh->key) { 5625 /* If either _F_HASH_REPORT or _F_RSS are negotiated, the 5626 * device provides hash calculation capabilities, that is, 5627 * hash_key is configured. 5628 */ 5629 if (!vi->has_rss && !vi->has_rss_hash_report) 5630 return -EOPNOTSUPP; 5631 5632 memcpy(vi->rss_hash_key_data, rxfh->key, vi->rss_key_size); 5633 update = true; 5634 } 5635 5636 if (update) 5637 virtnet_commit_rss_command(vi); 5638 5639 return 0; 5640 } 5641 5642 static u32 virtnet_get_rx_ring_count(struct net_device *dev) 5643 { 5644 struct virtnet_info *vi = netdev_priv(dev); 5645 5646 return vi->curr_queue_pairs; 5647 } 5648 5649 static const struct ethtool_ops virtnet_ethtool_ops = { 5650 .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES | 5651 ETHTOOL_COALESCE_USECS | ETHTOOL_COALESCE_USE_ADAPTIVE_RX, 5652 .get_drvinfo = virtnet_get_drvinfo, 5653 .get_link = ethtool_op_get_link, 5654 .get_ringparam = virtnet_get_ringparam, 5655 .set_ringparam = virtnet_set_ringparam, 5656 .get_strings = virtnet_get_strings, 5657 .get_sset_count = virtnet_get_sset_count, 5658 .get_ethtool_stats = virtnet_get_ethtool_stats, 5659 .set_channels = virtnet_set_channels, 5660 .get_channels = virtnet_get_channels, 5661 .get_ts_info = ethtool_op_get_ts_info, 5662 .get_link_ksettings = virtnet_get_link_ksettings, 5663 .set_link_ksettings = virtnet_set_link_ksettings, 5664 .set_coalesce = virtnet_set_coalesce, 5665 .get_coalesce = virtnet_get_coalesce, 5666 .set_per_queue_coalesce = virtnet_set_per_queue_coalesce, 5667 .get_per_queue_coalesce = virtnet_get_per_queue_coalesce, 5668 .get_rxfh_key_size = virtnet_get_rxfh_key_size, 5669 .get_rxfh_indir_size = virtnet_get_rxfh_indir_size, 5670 .get_rxfh = virtnet_get_rxfh, 5671 .set_rxfh = virtnet_set_rxfh, 5672 .get_rxfh_fields = virtnet_get_hashflow, 5673 .set_rxfh_fields = virtnet_set_hashflow, 5674 .get_rx_ring_count = virtnet_get_rx_ring_count, 5675 }; 5676 5677 static void virtnet_get_queue_stats_rx(struct net_device *dev, int i, 5678 struct netdev_queue_stats_rx *stats) 5679 { 5680 struct virtnet_info *vi = netdev_priv(dev); 5681 struct receive_queue *rq = &vi->rq[i]; 5682 struct virtnet_stats_ctx ctx = {0}; 5683 5684 virtnet_stats_ctx_init(vi, &ctx, (void *)stats, true); 5685 5686 virtnet_get_hw_stats(vi, &ctx, i * 2); 5687 virtnet_fill_stats(vi, i * 2, &ctx, (void *)&rq->stats, true, 0); 5688 } 5689 5690 static void virtnet_get_queue_stats_tx(struct net_device *dev, int i, 5691 struct netdev_queue_stats_tx *stats) 5692 { 5693 struct virtnet_info *vi = netdev_priv(dev); 5694 struct send_queue *sq = &vi->sq[i]; 5695 struct virtnet_stats_ctx ctx = {0}; 5696 5697 virtnet_stats_ctx_init(vi, &ctx, (void *)stats, true); 5698 5699 virtnet_get_hw_stats(vi, &ctx, i * 2 + 1); 5700 virtnet_fill_stats(vi, i * 2 + 1, &ctx, (void *)&sq->stats, true, 0); 5701 } 5702 5703 static void virtnet_get_base_stats(struct net_device *dev, 5704 struct netdev_queue_stats_rx *rx, 5705 struct netdev_queue_stats_tx *tx) 5706 { 5707 struct virtnet_info *vi = netdev_priv(dev); 5708 5709 /* The queue stats of the virtio-net will not be reset. So here we 5710 * return 0. 5711 */ 5712 rx->bytes = 0; 5713 rx->packets = 0; 5714 5715 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) { 5716 rx->hw_drops = 0; 5717 rx->hw_drop_overruns = 0; 5718 } 5719 5720 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) { 5721 rx->csum_unnecessary = 0; 5722 rx->csum_none = 0; 5723 rx->csum_bad = 0; 5724 } 5725 5726 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_GSO) { 5727 rx->hw_gro_packets = 0; 5728 rx->hw_gro_bytes = 0; 5729 rx->hw_gro_wire_packets = 0; 5730 rx->hw_gro_wire_bytes = 0; 5731 } 5732 5733 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED) 5734 rx->hw_drop_ratelimits = 0; 5735 5736 tx->bytes = 0; 5737 tx->packets = 0; 5738 tx->stop = 0; 5739 tx->wake = 0; 5740 5741 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) { 5742 tx->hw_drops = 0; 5743 tx->hw_drop_errors = 0; 5744 } 5745 5746 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_CSUM) { 5747 tx->csum_none = 0; 5748 tx->needs_csum = 0; 5749 } 5750 5751 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) { 5752 tx->hw_gso_packets = 0; 5753 tx->hw_gso_bytes = 0; 5754 tx->hw_gso_wire_packets = 0; 5755 tx->hw_gso_wire_bytes = 0; 5756 } 5757 5758 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED) 5759 tx->hw_drop_ratelimits = 0; 5760 5761 netdev_stat_queue_sum(dev, 5762 dev->real_num_rx_queues, vi->max_queue_pairs, rx, 5763 dev->real_num_tx_queues, vi->max_queue_pairs, tx); 5764 } 5765 5766 static const struct netdev_stat_ops virtnet_stat_ops = { 5767 .get_queue_stats_rx = virtnet_get_queue_stats_rx, 5768 .get_queue_stats_tx = virtnet_get_queue_stats_tx, 5769 .get_base_stats = virtnet_get_base_stats, 5770 }; 5771 5772 static void virtnet_freeze_down(struct virtio_device *vdev) 5773 { 5774 struct virtnet_info *vi = vdev->priv; 5775 5776 /* Make sure no work handler is accessing the device */ 5777 flush_work(&vi->config_work); 5778 disable_rx_mode_work(vi); 5779 flush_work(&vi->rx_mode_work); 5780 5781 if (netif_running(vi->dev)) { 5782 rtnl_lock(); 5783 virtnet_close(vi->dev); 5784 rtnl_unlock(); 5785 } 5786 5787 netif_tx_lock_bh(vi->dev); 5788 netif_device_detach(vi->dev); 5789 netif_tx_unlock_bh(vi->dev); 5790 } 5791 5792 static int init_vqs(struct virtnet_info *vi); 5793 5794 static int virtnet_restore_up(struct virtio_device *vdev) 5795 { 5796 struct virtnet_info *vi = vdev->priv; 5797 int err; 5798 5799 err = init_vqs(vi); 5800 if (err) 5801 return err; 5802 5803 virtio_device_ready(vdev); 5804 5805 enable_delayed_refill(vi); 5806 enable_rx_mode_work(vi); 5807 5808 if (netif_running(vi->dev)) { 5809 rtnl_lock(); 5810 err = virtnet_open(vi->dev); 5811 rtnl_unlock(); 5812 if (err) 5813 return err; 5814 } 5815 5816 netif_tx_lock_bh(vi->dev); 5817 netif_device_attach(vi->dev); 5818 netif_tx_unlock_bh(vi->dev); 5819 return err; 5820 } 5821 5822 static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads) 5823 { 5824 __virtio64 *_offloads __free(kfree) = NULL; 5825 struct scatterlist sg; 5826 5827 _offloads = kzalloc(sizeof(*_offloads), GFP_KERNEL); 5828 if (!_offloads) 5829 return -ENOMEM; 5830 5831 *_offloads = cpu_to_virtio64(vi->vdev, offloads); 5832 5833 sg_init_one(&sg, _offloads, sizeof(*_offloads)); 5834 5835 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS, 5836 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) { 5837 dev_warn(&vi->dev->dev, "Fail to set guest offload.\n"); 5838 return -EINVAL; 5839 } 5840 5841 return 0; 5842 } 5843 5844 static int virtnet_clear_guest_offloads(struct virtnet_info *vi) 5845 { 5846 u64 offloads = 0; 5847 5848 if (!vi->guest_offloads) 5849 return 0; 5850 5851 return virtnet_set_guest_offloads(vi, offloads); 5852 } 5853 5854 static int virtnet_restore_guest_offloads(struct virtnet_info *vi) 5855 { 5856 u64 offloads = vi->guest_offloads; 5857 5858 if (!vi->guest_offloads) 5859 return 0; 5860 5861 return virtnet_set_guest_offloads(vi, offloads); 5862 } 5863 5864 static int virtnet_rq_bind_xsk_pool(struct virtnet_info *vi, struct receive_queue *rq, 5865 struct xsk_buff_pool *pool) 5866 { 5867 int err, qindex; 5868 5869 qindex = rq - vi->rq; 5870 5871 if (pool) { 5872 err = xdp_rxq_info_reg(&rq->xsk_rxq_info, vi->dev, qindex, rq->napi.napi_id); 5873 if (err < 0) 5874 return err; 5875 5876 err = xdp_rxq_info_reg_mem_model(&rq->xsk_rxq_info, 5877 MEM_TYPE_XSK_BUFF_POOL, NULL); 5878 if (err < 0) 5879 goto unreg; 5880 5881 xsk_pool_set_rxq_info(pool, &rq->xsk_rxq_info); 5882 } 5883 5884 virtnet_rx_pause(vi, rq); 5885 5886 err = virtqueue_reset(rq->vq, virtnet_rq_unmap_free_buf, NULL); 5887 if (err) { 5888 netdev_err(vi->dev, "reset rx fail: rx queue index: %d err: %d\n", qindex, err); 5889 5890 pool = NULL; 5891 } 5892 5893 rq->xsk_pool = pool; 5894 5895 virtnet_rx_resume(vi, rq); 5896 5897 if (pool) 5898 return 0; 5899 5900 unreg: 5901 xdp_rxq_info_unreg(&rq->xsk_rxq_info); 5902 return err; 5903 } 5904 5905 static int virtnet_sq_bind_xsk_pool(struct virtnet_info *vi, 5906 struct send_queue *sq, 5907 struct xsk_buff_pool *pool) 5908 { 5909 int err, qindex; 5910 5911 qindex = sq - vi->sq; 5912 5913 virtnet_tx_pause(vi, sq); 5914 5915 err = virtqueue_reset(sq->vq, virtnet_sq_free_unused_buf, 5916 virtnet_sq_free_unused_buf_done); 5917 if (err) { 5918 netdev_err(vi->dev, "reset tx fail: tx queue index: %d err: %d\n", qindex, err); 5919 pool = NULL; 5920 } 5921 5922 sq->xsk_pool = pool; 5923 5924 virtnet_tx_resume(vi, sq); 5925 5926 return err; 5927 } 5928 5929 static int virtnet_xsk_pool_enable(struct net_device *dev, 5930 struct xsk_buff_pool *pool, 5931 u16 qid) 5932 { 5933 struct virtnet_info *vi = netdev_priv(dev); 5934 struct receive_queue *rq; 5935 struct device *dma_dev; 5936 struct send_queue *sq; 5937 dma_addr_t hdr_dma; 5938 int err, size; 5939 5940 if (vi->hdr_len > xsk_pool_get_headroom(pool)) 5941 return -EINVAL; 5942 5943 /* In big_packets mode, xdp cannot work, so there is no need to 5944 * initialize xsk of rq. 5945 */ 5946 if (vi->big_packets && !vi->mergeable_rx_bufs) 5947 return -ENOENT; 5948 5949 if (qid >= vi->curr_queue_pairs) 5950 return -EINVAL; 5951 5952 sq = &vi->sq[qid]; 5953 rq = &vi->rq[qid]; 5954 5955 /* xsk assumes that tx and rx must have the same dma device. The af-xdp 5956 * may use one buffer to receive from the rx and reuse this buffer to 5957 * send by the tx. So the dma dev of sq and rq must be the same one. 5958 * 5959 * But vq->dma_dev allows every vq has the respective dma dev. So I 5960 * check the dma dev of vq and sq is the same dev. 5961 */ 5962 if (virtqueue_dma_dev(rq->vq) != virtqueue_dma_dev(sq->vq)) 5963 return -EINVAL; 5964 5965 dma_dev = virtqueue_dma_dev(rq->vq); 5966 if (!dma_dev) 5967 return -EINVAL; 5968 5969 size = virtqueue_get_vring_size(rq->vq); 5970 5971 rq->xsk_buffs = kvcalloc(size, sizeof(*rq->xsk_buffs), GFP_KERNEL); 5972 if (!rq->xsk_buffs) 5973 return -ENOMEM; 5974 5975 hdr_dma = virtqueue_map_single_attrs(sq->vq, &xsk_hdr, vi->hdr_len, 5976 DMA_TO_DEVICE, 0); 5977 if (virtqueue_map_mapping_error(sq->vq, hdr_dma)) { 5978 err = -ENOMEM; 5979 goto err_free_buffs; 5980 } 5981 5982 err = xsk_pool_dma_map(pool, dma_dev, 0); 5983 if (err) 5984 goto err_xsk_map; 5985 5986 err = virtnet_rq_bind_xsk_pool(vi, rq, pool); 5987 if (err) 5988 goto err_rq; 5989 5990 err = virtnet_sq_bind_xsk_pool(vi, sq, pool); 5991 if (err) 5992 goto err_sq; 5993 5994 /* Now, we do not support tx offload(such as tx csum), so all the tx 5995 * virtnet hdr is zero. So all the tx packets can share a single hdr. 5996 */ 5997 sq->xsk_hdr_dma_addr = hdr_dma; 5998 5999 return 0; 6000 6001 err_sq: 6002 virtnet_rq_bind_xsk_pool(vi, rq, NULL); 6003 err_rq: 6004 xsk_pool_dma_unmap(pool, 0); 6005 err_xsk_map: 6006 virtqueue_unmap_single_attrs(rq->vq, hdr_dma, vi->hdr_len, 6007 DMA_TO_DEVICE, 0); 6008 err_free_buffs: 6009 kvfree(rq->xsk_buffs); 6010 return err; 6011 } 6012 6013 static int virtnet_xsk_pool_disable(struct net_device *dev, u16 qid) 6014 { 6015 struct virtnet_info *vi = netdev_priv(dev); 6016 struct xsk_buff_pool *pool; 6017 struct receive_queue *rq; 6018 struct send_queue *sq; 6019 int err; 6020 6021 if (qid >= vi->curr_queue_pairs) 6022 return -EINVAL; 6023 6024 sq = &vi->sq[qid]; 6025 rq = &vi->rq[qid]; 6026 6027 pool = rq->xsk_pool; 6028 6029 err = virtnet_rq_bind_xsk_pool(vi, rq, NULL); 6030 err |= virtnet_sq_bind_xsk_pool(vi, sq, NULL); 6031 6032 xsk_pool_dma_unmap(pool, 0); 6033 6034 virtqueue_unmap_single_attrs(sq->vq, sq->xsk_hdr_dma_addr, 6035 vi->hdr_len, DMA_TO_DEVICE, 0); 6036 kvfree(rq->xsk_buffs); 6037 6038 return err; 6039 } 6040 6041 static int virtnet_xsk_pool_setup(struct net_device *dev, struct netdev_bpf *xdp) 6042 { 6043 if (xdp->xsk.pool) 6044 return virtnet_xsk_pool_enable(dev, xdp->xsk.pool, 6045 xdp->xsk.queue_id); 6046 else 6047 return virtnet_xsk_pool_disable(dev, xdp->xsk.queue_id); 6048 } 6049 6050 static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog, 6051 struct netlink_ext_ack *extack) 6052 { 6053 unsigned int room = SKB_DATA_ALIGN(XDP_PACKET_HEADROOM + 6054 sizeof(struct skb_shared_info)); 6055 unsigned int max_sz = PAGE_SIZE - room - ETH_HLEN; 6056 struct virtnet_info *vi = netdev_priv(dev); 6057 struct bpf_prog *old_prog; 6058 u16 xdp_qp = 0, curr_qp; 6059 int i, err; 6060 6061 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) 6062 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) || 6063 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) || 6064 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) || 6065 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) || 6066 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM) || 6067 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) || 6068 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6))) { 6069 NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing GRO_HW/CSUM, disable GRO_HW/CSUM first"); 6070 return -EOPNOTSUPP; 6071 } 6072 6073 if (vi->mergeable_rx_bufs && !vi->any_header_sg) { 6074 NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required"); 6075 return -EINVAL; 6076 } 6077 6078 if (prog && !prog->aux->xdp_has_frags && dev->mtu > max_sz) { 6079 NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP without frags"); 6080 netdev_warn(dev, "single-buffer XDP requires MTU less than %u\n", max_sz); 6081 return -EINVAL; 6082 } 6083 6084 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs; 6085 if (prog) 6086 xdp_qp = nr_cpu_ids; 6087 6088 /* XDP requires extra queues for XDP_TX */ 6089 if (curr_qp + xdp_qp > vi->max_queue_pairs) { 6090 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", 6091 curr_qp + xdp_qp, vi->max_queue_pairs); 6092 xdp_qp = 0; 6093 } 6094 6095 old_prog = rtnl_dereference(vi->rq[0].xdp_prog); 6096 if (!prog && !old_prog) 6097 return 0; 6098 6099 if (prog) 6100 bpf_prog_add(prog, vi->max_queue_pairs - 1); 6101 6102 virtnet_rx_pause_all(vi); 6103 6104 /* Make sure NAPI is not using any XDP TX queues for RX. */ 6105 if (netif_running(dev)) { 6106 for (i = 0; i < vi->max_queue_pairs; i++) 6107 virtnet_napi_tx_disable(&vi->sq[i]); 6108 } 6109 6110 if (!prog) { 6111 for (i = 0; i < vi->max_queue_pairs; i++) { 6112 rcu_assign_pointer(vi->rq[i].xdp_prog, prog); 6113 if (i == 0) 6114 virtnet_restore_guest_offloads(vi); 6115 } 6116 synchronize_net(); 6117 } 6118 6119 err = virtnet_set_queues(vi, curr_qp + xdp_qp); 6120 if (err) 6121 goto err; 6122 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp); 6123 vi->xdp_queue_pairs = xdp_qp; 6124 6125 if (prog) { 6126 vi->xdp_enabled = true; 6127 for (i = 0; i < vi->max_queue_pairs; i++) { 6128 rcu_assign_pointer(vi->rq[i].xdp_prog, prog); 6129 if (i == 0 && !old_prog) 6130 virtnet_clear_guest_offloads(vi); 6131 } 6132 if (!old_prog) 6133 xdp_features_set_redirect_target(dev, true); 6134 } else { 6135 xdp_features_clear_redirect_target(dev); 6136 vi->xdp_enabled = false; 6137 } 6138 6139 virtnet_rx_resume_all(vi); 6140 for (i = 0; i < vi->max_queue_pairs; i++) { 6141 if (old_prog) 6142 bpf_prog_put(old_prog); 6143 if (netif_running(dev)) 6144 virtnet_napi_tx_enable(&vi->sq[i]); 6145 } 6146 6147 return 0; 6148 6149 err: 6150 if (!prog) { 6151 virtnet_clear_guest_offloads(vi); 6152 for (i = 0; i < vi->max_queue_pairs; i++) 6153 rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog); 6154 } 6155 6156 virtnet_rx_resume_all(vi); 6157 if (netif_running(dev)) { 6158 for (i = 0; i < vi->max_queue_pairs; i++) 6159 virtnet_napi_tx_enable(&vi->sq[i]); 6160 } 6161 if (prog) 6162 bpf_prog_sub(prog, vi->max_queue_pairs - 1); 6163 return err; 6164 } 6165 6166 static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp) 6167 { 6168 switch (xdp->command) { 6169 case XDP_SETUP_PROG: 6170 return virtnet_xdp_set(dev, xdp->prog, xdp->extack); 6171 case XDP_SETUP_XSK_POOL: 6172 return virtnet_xsk_pool_setup(dev, xdp); 6173 default: 6174 return -EINVAL; 6175 } 6176 } 6177 6178 static int virtnet_get_phys_port_name(struct net_device *dev, char *buf, 6179 size_t len) 6180 { 6181 struct virtnet_info *vi = netdev_priv(dev); 6182 int ret; 6183 6184 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY)) 6185 return -EOPNOTSUPP; 6186 6187 ret = snprintf(buf, len, "sby"); 6188 if (ret >= len) 6189 return -EOPNOTSUPP; 6190 6191 return 0; 6192 } 6193 6194 static int virtnet_set_features(struct net_device *dev, 6195 netdev_features_t features) 6196 { 6197 struct virtnet_info *vi = netdev_priv(dev); 6198 u64 offloads; 6199 int err; 6200 6201 if ((dev->features ^ features) & NETIF_F_GRO_HW) { 6202 if (vi->xdp_enabled) 6203 return -EBUSY; 6204 6205 if (features & NETIF_F_GRO_HW) 6206 offloads = vi->guest_offloads_capable; 6207 else 6208 offloads = vi->guest_offloads_capable & 6209 ~GUEST_OFFLOAD_GRO_HW_MASK; 6210 6211 err = virtnet_set_guest_offloads(vi, offloads); 6212 if (err) 6213 return err; 6214 vi->guest_offloads = offloads; 6215 } 6216 6217 if ((dev->features ^ features) & NETIF_F_RXHASH) { 6218 if (features & NETIF_F_RXHASH) 6219 vi->rss_hdr->hash_types = cpu_to_le32(vi->rss_hash_types_saved); 6220 else 6221 vi->rss_hdr->hash_types = cpu_to_le32(VIRTIO_NET_HASH_REPORT_NONE); 6222 6223 if (!virtnet_commit_rss_command(vi)) 6224 return -EINVAL; 6225 } 6226 6227 return 0; 6228 } 6229 6230 static void virtnet_tx_timeout(struct net_device *dev, unsigned int txqueue) 6231 { 6232 struct virtnet_info *priv = netdev_priv(dev); 6233 struct send_queue *sq = &priv->sq[txqueue]; 6234 struct netdev_queue *txq = netdev_get_tx_queue(dev, txqueue); 6235 6236 u64_stats_update_begin(&sq->stats.syncp); 6237 u64_stats_inc(&sq->stats.tx_timeouts); 6238 u64_stats_update_end(&sq->stats.syncp); 6239 6240 netdev_err(dev, "TX timeout on queue: %u, sq: %s, vq: 0x%x, name: %s, %u usecs ago\n", 6241 txqueue, sq->name, sq->vq->index, sq->vq->name, 6242 jiffies_to_usecs(jiffies - READ_ONCE(txq->trans_start))); 6243 } 6244 6245 static int virtnet_init_irq_moder(struct virtnet_info *vi) 6246 { 6247 u8 profile_flags = 0, coal_flags = 0; 6248 int ret, i; 6249 6250 profile_flags |= DIM_PROFILE_RX; 6251 coal_flags |= DIM_COALESCE_USEC | DIM_COALESCE_PKTS; 6252 ret = net_dim_init_irq_moder(vi->dev, profile_flags, coal_flags, 6253 DIM_CQ_PERIOD_MODE_START_FROM_EQE, 6254 0, virtnet_rx_dim_work, NULL); 6255 6256 if (ret) 6257 return ret; 6258 6259 for (i = 0; i < vi->max_queue_pairs; i++) 6260 net_dim_setting(vi->dev, &vi->rq[i].dim, false); 6261 6262 return 0; 6263 } 6264 6265 static void virtnet_free_irq_moder(struct virtnet_info *vi) 6266 { 6267 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) 6268 return; 6269 6270 rtnl_lock(); 6271 net_dim_free_irq_moder(vi->dev); 6272 rtnl_unlock(); 6273 } 6274 6275 static const struct net_device_ops virtnet_netdev = { 6276 .ndo_open = virtnet_open, 6277 .ndo_stop = virtnet_close, 6278 .ndo_start_xmit = start_xmit, 6279 .ndo_validate_addr = eth_validate_addr, 6280 .ndo_set_mac_address = virtnet_set_mac_address, 6281 .ndo_set_rx_mode = virtnet_set_rx_mode, 6282 .ndo_get_stats64 = virtnet_stats, 6283 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, 6284 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, 6285 .ndo_bpf = virtnet_xdp, 6286 .ndo_xdp_xmit = virtnet_xdp_xmit, 6287 .ndo_xsk_wakeup = virtnet_xsk_wakeup, 6288 .ndo_features_check = passthru_features_check, 6289 .ndo_get_phys_port_name = virtnet_get_phys_port_name, 6290 .ndo_set_features = virtnet_set_features, 6291 .ndo_tx_timeout = virtnet_tx_timeout, 6292 }; 6293 6294 static void virtnet_config_changed_work(struct work_struct *work) 6295 { 6296 struct virtnet_info *vi = 6297 container_of(work, struct virtnet_info, config_work); 6298 u16 v; 6299 6300 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS, 6301 struct virtio_net_config, status, &v) < 0) 6302 return; 6303 6304 if (v & VIRTIO_NET_S_ANNOUNCE) { 6305 netdev_notify_peers(vi->dev); 6306 virtnet_ack_link_announce(vi); 6307 } 6308 6309 /* Ignore unknown (future) status bits */ 6310 v &= VIRTIO_NET_S_LINK_UP; 6311 6312 if (vi->status == v) 6313 return; 6314 6315 vi->status = v; 6316 6317 if (vi->status & VIRTIO_NET_S_LINK_UP) { 6318 virtnet_update_settings(vi); 6319 netif_carrier_on(vi->dev); 6320 netif_tx_wake_all_queues(vi->dev); 6321 } else { 6322 netif_carrier_off(vi->dev); 6323 netif_tx_stop_all_queues(vi->dev); 6324 } 6325 } 6326 6327 static void virtnet_config_changed(struct virtio_device *vdev) 6328 { 6329 struct virtnet_info *vi = vdev->priv; 6330 6331 schedule_work(&vi->config_work); 6332 } 6333 6334 static void virtnet_free_queues(struct virtnet_info *vi) 6335 { 6336 int i; 6337 6338 for (i = 0; i < vi->max_queue_pairs; i++) { 6339 __netif_napi_del(&vi->rq[i].napi); 6340 __netif_napi_del(&vi->sq[i].napi); 6341 } 6342 6343 /* We called __netif_napi_del(), 6344 * we need to respect an RCU grace period before freeing vi->rq 6345 */ 6346 synchronize_net(); 6347 6348 kfree(vi->rq); 6349 kfree(vi->sq); 6350 kfree(vi->ctrl); 6351 } 6352 6353 static void _free_receive_bufs(struct virtnet_info *vi) 6354 { 6355 struct bpf_prog *old_prog; 6356 int i; 6357 6358 for (i = 0; i < vi->max_queue_pairs; i++) { 6359 while (vi->rq[i].pages) 6360 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0); 6361 6362 old_prog = rtnl_dereference(vi->rq[i].xdp_prog); 6363 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL); 6364 if (old_prog) 6365 bpf_prog_put(old_prog); 6366 } 6367 } 6368 6369 static void free_receive_bufs(struct virtnet_info *vi) 6370 { 6371 rtnl_lock(); 6372 _free_receive_bufs(vi); 6373 rtnl_unlock(); 6374 } 6375 6376 static void free_receive_page_frags(struct virtnet_info *vi) 6377 { 6378 int i; 6379 for (i = 0; i < vi->max_queue_pairs; i++) 6380 if (vi->rq[i].alloc_frag.page) { 6381 if (vi->rq[i].last_dma) 6382 virtnet_rq_unmap(&vi->rq[i], vi->rq[i].last_dma, 0); 6383 put_page(vi->rq[i].alloc_frag.page); 6384 } 6385 } 6386 6387 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf) 6388 { 6389 struct virtnet_info *vi = vq->vdev->priv; 6390 struct send_queue *sq; 6391 int i = vq2txq(vq); 6392 6393 sq = &vi->sq[i]; 6394 6395 switch (virtnet_xmit_ptr_unpack(&buf)) { 6396 case VIRTNET_XMIT_TYPE_SKB: 6397 case VIRTNET_XMIT_TYPE_SKB_ORPHAN: 6398 dev_kfree_skb(buf); 6399 break; 6400 6401 case VIRTNET_XMIT_TYPE_XDP: 6402 xdp_return_frame(buf); 6403 break; 6404 6405 case VIRTNET_XMIT_TYPE_XSK: 6406 xsk_tx_completed(sq->xsk_pool, 1); 6407 break; 6408 } 6409 } 6410 6411 static void virtnet_sq_free_unused_buf_done(struct virtqueue *vq) 6412 { 6413 struct virtnet_info *vi = vq->vdev->priv; 6414 int i = vq2txq(vq); 6415 6416 netdev_tx_reset_queue(netdev_get_tx_queue(vi->dev, i)); 6417 } 6418 6419 static void free_unused_bufs(struct virtnet_info *vi) 6420 { 6421 void *buf; 6422 int i; 6423 6424 for (i = 0; i < vi->max_queue_pairs; i++) { 6425 struct virtqueue *vq = vi->sq[i].vq; 6426 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) 6427 virtnet_sq_free_unused_buf(vq, buf); 6428 cond_resched(); 6429 } 6430 6431 for (i = 0; i < vi->max_queue_pairs; i++) { 6432 struct virtqueue *vq = vi->rq[i].vq; 6433 6434 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) 6435 virtnet_rq_unmap_free_buf(vq, buf); 6436 cond_resched(); 6437 } 6438 } 6439 6440 static void virtnet_del_vqs(struct virtnet_info *vi) 6441 { 6442 struct virtio_device *vdev = vi->vdev; 6443 6444 virtnet_clean_affinity(vi); 6445 6446 vdev->config->del_vqs(vdev); 6447 6448 virtnet_free_queues(vi); 6449 } 6450 6451 /* How large should a single buffer be so a queue full of these can fit at 6452 * least one full packet? 6453 * Logic below assumes the mergeable buffer header is used. 6454 */ 6455 static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq) 6456 { 6457 const unsigned int hdr_len = vi->hdr_len; 6458 unsigned int rq_size = virtqueue_get_vring_size(vq); 6459 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu; 6460 unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len; 6461 unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size); 6462 6463 return max(max(min_buf_len, hdr_len) - hdr_len, 6464 (unsigned int)GOOD_PACKET_LEN); 6465 } 6466 6467 static int virtnet_find_vqs(struct virtnet_info *vi) 6468 { 6469 struct virtqueue_info *vqs_info; 6470 struct virtqueue **vqs; 6471 int ret = -ENOMEM; 6472 int total_vqs; 6473 bool *ctx; 6474 u16 i; 6475 6476 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by 6477 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by 6478 * possible control vq. 6479 */ 6480 total_vqs = vi->max_queue_pairs * 2 + 6481 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ); 6482 6483 /* Allocate space for find_vqs parameters */ 6484 vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL); 6485 if (!vqs) 6486 goto err_vq; 6487 vqs_info = kcalloc(total_vqs, sizeof(*vqs_info), GFP_KERNEL); 6488 if (!vqs_info) 6489 goto err_vqs_info; 6490 if (!vi->big_packets || vi->mergeable_rx_bufs) { 6491 ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL); 6492 if (!ctx) 6493 goto err_ctx; 6494 } else { 6495 ctx = NULL; 6496 } 6497 6498 /* Parameters for control virtqueue, if any */ 6499 if (vi->has_cvq) { 6500 vqs_info[total_vqs - 1].name = "control"; 6501 } 6502 6503 /* Allocate/initialize parameters for send/receive virtqueues */ 6504 for (i = 0; i < vi->max_queue_pairs; i++) { 6505 vqs_info[rxq2vq(i)].callback = skb_recv_done; 6506 vqs_info[txq2vq(i)].callback = skb_xmit_done; 6507 sprintf(vi->rq[i].name, "input.%u", i); 6508 sprintf(vi->sq[i].name, "output.%u", i); 6509 vqs_info[rxq2vq(i)].name = vi->rq[i].name; 6510 vqs_info[txq2vq(i)].name = vi->sq[i].name; 6511 if (ctx) 6512 vqs_info[rxq2vq(i)].ctx = true; 6513 } 6514 6515 ret = virtio_find_vqs(vi->vdev, total_vqs, vqs, vqs_info, NULL); 6516 if (ret) 6517 goto err_find; 6518 6519 if (vi->has_cvq) { 6520 vi->cvq = vqs[total_vqs - 1]; 6521 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) 6522 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 6523 } 6524 6525 for (i = 0; i < vi->max_queue_pairs; i++) { 6526 vi->rq[i].vq = vqs[rxq2vq(i)]; 6527 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq); 6528 vi->sq[i].vq = vqs[txq2vq(i)]; 6529 } 6530 6531 /* run here: ret == 0. */ 6532 6533 6534 err_find: 6535 kfree(ctx); 6536 err_ctx: 6537 kfree(vqs_info); 6538 err_vqs_info: 6539 kfree(vqs); 6540 err_vq: 6541 return ret; 6542 } 6543 6544 static int virtnet_alloc_queues(struct virtnet_info *vi) 6545 { 6546 int i; 6547 6548 if (vi->has_cvq) { 6549 vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL); 6550 if (!vi->ctrl) 6551 goto err_ctrl; 6552 } else { 6553 vi->ctrl = NULL; 6554 } 6555 vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL); 6556 if (!vi->sq) 6557 goto err_sq; 6558 vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL); 6559 if (!vi->rq) 6560 goto err_rq; 6561 6562 INIT_DELAYED_WORK(&vi->refill, refill_work); 6563 for (i = 0; i < vi->max_queue_pairs; i++) { 6564 vi->rq[i].pages = NULL; 6565 netif_napi_add_config(vi->dev, &vi->rq[i].napi, virtnet_poll, 6566 i); 6567 vi->rq[i].napi.weight = napi_weight; 6568 netif_napi_add_tx_weight(vi->dev, &vi->sq[i].napi, 6569 virtnet_poll_tx, 6570 napi_tx ? napi_weight : 0); 6571 6572 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg)); 6573 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len); 6574 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg)); 6575 6576 u64_stats_init(&vi->rq[i].stats.syncp); 6577 u64_stats_init(&vi->sq[i].stats.syncp); 6578 mutex_init(&vi->rq[i].dim_lock); 6579 } 6580 6581 return 0; 6582 6583 err_rq: 6584 kfree(vi->sq); 6585 err_sq: 6586 kfree(vi->ctrl); 6587 err_ctrl: 6588 return -ENOMEM; 6589 } 6590 6591 static int init_vqs(struct virtnet_info *vi) 6592 { 6593 int ret; 6594 6595 /* Allocate send & receive queues */ 6596 ret = virtnet_alloc_queues(vi); 6597 if (ret) 6598 goto err; 6599 6600 ret = virtnet_find_vqs(vi); 6601 if (ret) 6602 goto err_free; 6603 6604 cpus_read_lock(); 6605 virtnet_set_affinity(vi); 6606 cpus_read_unlock(); 6607 6608 return 0; 6609 6610 err_free: 6611 virtnet_free_queues(vi); 6612 err: 6613 return ret; 6614 } 6615 6616 #ifdef CONFIG_SYSFS 6617 static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue, 6618 char *buf) 6619 { 6620 struct virtnet_info *vi = netdev_priv(queue->dev); 6621 unsigned int queue_index = get_netdev_rx_queue_index(queue); 6622 unsigned int headroom = virtnet_get_headroom(vi); 6623 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 6624 struct ewma_pkt_len *avg; 6625 6626 BUG_ON(queue_index >= vi->max_queue_pairs); 6627 avg = &vi->rq[queue_index].mrg_avg_pkt_len; 6628 return sprintf(buf, "%u\n", 6629 get_mergeable_buf_len(&vi->rq[queue_index], avg, 6630 SKB_DATA_ALIGN(headroom + tailroom))); 6631 } 6632 6633 static struct rx_queue_attribute mergeable_rx_buffer_size_attribute = 6634 __ATTR_RO(mergeable_rx_buffer_size); 6635 6636 static struct attribute *virtio_net_mrg_rx_attrs[] = { 6637 &mergeable_rx_buffer_size_attribute.attr, 6638 NULL 6639 }; 6640 6641 static const struct attribute_group virtio_net_mrg_rx_group = { 6642 .name = "virtio_net", 6643 .attrs = virtio_net_mrg_rx_attrs 6644 }; 6645 #endif 6646 6647 static bool virtnet_fail_on_feature(struct virtio_device *vdev, 6648 unsigned int fbit, 6649 const char *fname, const char *dname) 6650 { 6651 if (!virtio_has_feature(vdev, fbit)) 6652 return false; 6653 6654 dev_err(&vdev->dev, "device advertises feature %s but not %s", 6655 fname, dname); 6656 6657 return true; 6658 } 6659 6660 #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \ 6661 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit) 6662 6663 static bool virtnet_validate_features(struct virtio_device *vdev) 6664 { 6665 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) && 6666 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX, 6667 "VIRTIO_NET_F_CTRL_VQ") || 6668 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN, 6669 "VIRTIO_NET_F_CTRL_VQ") || 6670 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE, 6671 "VIRTIO_NET_F_CTRL_VQ") || 6672 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") || 6673 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR, 6674 "VIRTIO_NET_F_CTRL_VQ") || 6675 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_RSS, 6676 "VIRTIO_NET_F_CTRL_VQ") || 6677 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_HASH_REPORT, 6678 "VIRTIO_NET_F_CTRL_VQ") || 6679 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_NOTF_COAL, 6680 "VIRTIO_NET_F_CTRL_VQ") || 6681 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_VQ_NOTF_COAL, 6682 "VIRTIO_NET_F_CTRL_VQ"))) { 6683 return false; 6684 } 6685 6686 return true; 6687 } 6688 6689 #define MIN_MTU ETH_MIN_MTU 6690 #define MAX_MTU ETH_MAX_MTU 6691 6692 static int virtnet_validate(struct virtio_device *vdev) 6693 { 6694 if (!vdev->config->get) { 6695 dev_err(&vdev->dev, "%s failure: config access disabled\n", 6696 __func__); 6697 return -EINVAL; 6698 } 6699 6700 if (!virtnet_validate_features(vdev)) 6701 return -EINVAL; 6702 6703 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 6704 int mtu = virtio_cread16(vdev, 6705 offsetof(struct virtio_net_config, 6706 mtu)); 6707 if (mtu < MIN_MTU) 6708 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU); 6709 } 6710 6711 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY) && 6712 !virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) { 6713 dev_warn(&vdev->dev, "device advertises feature VIRTIO_NET_F_STANDBY but not VIRTIO_NET_F_MAC, disabling standby"); 6714 __virtio_clear_bit(vdev, VIRTIO_NET_F_STANDBY); 6715 } 6716 6717 return 0; 6718 } 6719 6720 static bool virtnet_check_guest_gso(const struct virtnet_info *vi) 6721 { 6722 return virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) || 6723 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) || 6724 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) || 6725 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) || 6726 (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) && 6727 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6)); 6728 } 6729 6730 static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu) 6731 { 6732 bool guest_gso = virtnet_check_guest_gso(vi); 6733 6734 /* If device can receive ANY guest GSO packets, regardless of mtu, 6735 * allocate packets of maximum size, otherwise limit it to only 6736 * mtu size worth only. 6737 */ 6738 if (mtu > ETH_DATA_LEN || guest_gso) { 6739 vi->big_packets = true; 6740 vi->big_packets_num_skbfrags = guest_gso ? MAX_SKB_FRAGS : DIV_ROUND_UP(mtu, PAGE_SIZE); 6741 } 6742 } 6743 6744 #define VIRTIO_NET_HASH_REPORT_MAX_TABLE 10 6745 static enum xdp_rss_hash_type 6746 virtnet_xdp_rss_type[VIRTIO_NET_HASH_REPORT_MAX_TABLE] = { 6747 [VIRTIO_NET_HASH_REPORT_NONE] = XDP_RSS_TYPE_NONE, 6748 [VIRTIO_NET_HASH_REPORT_IPv4] = XDP_RSS_TYPE_L3_IPV4, 6749 [VIRTIO_NET_HASH_REPORT_TCPv4] = XDP_RSS_TYPE_L4_IPV4_TCP, 6750 [VIRTIO_NET_HASH_REPORT_UDPv4] = XDP_RSS_TYPE_L4_IPV4_UDP, 6751 [VIRTIO_NET_HASH_REPORT_IPv6] = XDP_RSS_TYPE_L3_IPV6, 6752 [VIRTIO_NET_HASH_REPORT_TCPv6] = XDP_RSS_TYPE_L4_IPV6_TCP, 6753 [VIRTIO_NET_HASH_REPORT_UDPv6] = XDP_RSS_TYPE_L4_IPV6_UDP, 6754 [VIRTIO_NET_HASH_REPORT_IPv6_EX] = XDP_RSS_TYPE_L3_IPV6_EX, 6755 [VIRTIO_NET_HASH_REPORT_TCPv6_EX] = XDP_RSS_TYPE_L4_IPV6_TCP_EX, 6756 [VIRTIO_NET_HASH_REPORT_UDPv6_EX] = XDP_RSS_TYPE_L4_IPV6_UDP_EX 6757 }; 6758 6759 static int virtnet_xdp_rx_hash(const struct xdp_md *_ctx, u32 *hash, 6760 enum xdp_rss_hash_type *rss_type) 6761 { 6762 const struct xdp_buff *xdp = (void *)_ctx; 6763 struct virtio_net_hdr_v1_hash *hdr_hash; 6764 struct virtnet_info *vi; 6765 u16 hash_report; 6766 6767 if (!(xdp->rxq->dev->features & NETIF_F_RXHASH)) 6768 return -ENODATA; 6769 6770 vi = netdev_priv(xdp->rxq->dev); 6771 hdr_hash = (struct virtio_net_hdr_v1_hash *)(xdp->data - vi->hdr_len); 6772 hash_report = __le16_to_cpu(hdr_hash->hash_report); 6773 6774 if (hash_report >= VIRTIO_NET_HASH_REPORT_MAX_TABLE) 6775 hash_report = VIRTIO_NET_HASH_REPORT_NONE; 6776 6777 *rss_type = virtnet_xdp_rss_type[hash_report]; 6778 *hash = virtio_net_hash_value(hdr_hash); 6779 return 0; 6780 } 6781 6782 static const struct xdp_metadata_ops virtnet_xdp_metadata_ops = { 6783 .xmo_rx_hash = virtnet_xdp_rx_hash, 6784 }; 6785 6786 static int virtnet_probe(struct virtio_device *vdev) 6787 { 6788 int i, err = -ENOMEM; 6789 struct net_device *dev; 6790 struct virtnet_info *vi; 6791 u16 max_queue_pairs; 6792 int mtu = 0; 6793 6794 /* Find if host supports multiqueue/rss virtio_net device */ 6795 max_queue_pairs = 1; 6796 if (virtio_has_feature(vdev, VIRTIO_NET_F_MQ) || virtio_has_feature(vdev, VIRTIO_NET_F_RSS)) 6797 max_queue_pairs = 6798 virtio_cread16(vdev, offsetof(struct virtio_net_config, max_virtqueue_pairs)); 6799 6800 /* We need at least 2 queue's */ 6801 if (max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 6802 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 6803 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 6804 max_queue_pairs = 1; 6805 6806 /* Allocate ourselves a network device with room for our info */ 6807 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs); 6808 if (!dev) 6809 return -ENOMEM; 6810 6811 /* Set up network device as normal. */ 6812 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE | 6813 IFF_TX_SKB_NO_LINEAR; 6814 dev->netdev_ops = &virtnet_netdev; 6815 dev->stat_ops = &virtnet_stat_ops; 6816 dev->features = NETIF_F_HIGHDMA; 6817 6818 dev->ethtool_ops = &virtnet_ethtool_ops; 6819 SET_NETDEV_DEV(dev, &vdev->dev); 6820 6821 /* Do we support "hardware" checksums? */ 6822 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { 6823 /* This opens up the world of extra features. */ 6824 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG; 6825 if (csum) 6826 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG; 6827 6828 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { 6829 dev->hw_features |= NETIF_F_TSO 6830 | NETIF_F_TSO_ECN | NETIF_F_TSO6; 6831 } 6832 /* Individual feature bits: what can host handle? */ 6833 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) 6834 dev->hw_features |= NETIF_F_TSO; 6835 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) 6836 dev->hw_features |= NETIF_F_TSO6; 6837 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) 6838 dev->hw_features |= NETIF_F_TSO_ECN; 6839 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_USO)) 6840 dev->hw_features |= NETIF_F_GSO_UDP_L4; 6841 6842 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO)) { 6843 dev->hw_features |= NETIF_F_GSO_UDP_TUNNEL; 6844 dev->hw_enc_features = dev->hw_features; 6845 } 6846 if (dev->hw_features & NETIF_F_GSO_UDP_TUNNEL && 6847 virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO_CSUM)) { 6848 dev->hw_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM; 6849 dev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM; 6850 } 6851 6852 dev->features |= NETIF_F_GSO_ROBUST; 6853 6854 if (gso) 6855 dev->features |= dev->hw_features; 6856 /* (!csum && gso) case will be fixed by register_netdev() */ 6857 } 6858 6859 /* 1. With VIRTIO_NET_F_GUEST_CSUM negotiation, the driver doesn't 6860 * need to calculate checksums for partially checksummed packets, 6861 * as they're considered valid by the upper layer. 6862 * 2. Without VIRTIO_NET_F_GUEST_CSUM negotiation, the driver only 6863 * receives fully checksummed packets. The device may assist in 6864 * validating these packets' checksums, so the driver won't have to. 6865 */ 6866 dev->features |= NETIF_F_RXCSUM; 6867 6868 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || 6869 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)) 6870 dev->features |= NETIF_F_GRO_HW; 6871 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) 6872 dev->hw_features |= NETIF_F_GRO_HW; 6873 6874 dev->vlan_features = dev->features; 6875 dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT | 6876 NETDEV_XDP_ACT_XSK_ZEROCOPY; 6877 6878 /* MTU range: 68 - 65535 */ 6879 dev->min_mtu = MIN_MTU; 6880 dev->max_mtu = MAX_MTU; 6881 6882 /* Configuration may specify what MAC to use. Otherwise random. */ 6883 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) { 6884 u8 addr[ETH_ALEN]; 6885 6886 virtio_cread_bytes(vdev, 6887 offsetof(struct virtio_net_config, mac), 6888 addr, ETH_ALEN); 6889 eth_hw_addr_set(dev, addr); 6890 } else { 6891 eth_hw_addr_random(dev); 6892 dev_info(&vdev->dev, "Assigned random MAC address %pM\n", 6893 dev->dev_addr); 6894 } 6895 6896 /* Set up our device-specific information */ 6897 vi = netdev_priv(dev); 6898 vi->dev = dev; 6899 vi->vdev = vdev; 6900 vdev->priv = vi; 6901 6902 INIT_WORK(&vi->config_work, virtnet_config_changed_work); 6903 INIT_WORK(&vi->rx_mode_work, virtnet_rx_mode_work); 6904 spin_lock_init(&vi->refill_lock); 6905 6906 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) { 6907 vi->mergeable_rx_bufs = true; 6908 dev->xdp_features |= NETDEV_XDP_ACT_RX_SG; 6909 } 6910 6911 if (virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT)) 6912 vi->has_rss_hash_report = true; 6913 6914 if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS)) { 6915 vi->has_rss = true; 6916 6917 vi->rss_indir_table_size = 6918 virtio_cread16(vdev, offsetof(struct virtio_net_config, 6919 rss_max_indirection_table_length)); 6920 } 6921 vi->rss_hdr = devm_kzalloc(&vdev->dev, virtnet_rss_hdr_size(vi), GFP_KERNEL); 6922 if (!vi->rss_hdr) { 6923 err = -ENOMEM; 6924 goto free; 6925 } 6926 6927 if (vi->has_rss || vi->has_rss_hash_report) { 6928 vi->rss_key_size = 6929 virtio_cread8(vdev, offsetof(struct virtio_net_config, rss_max_key_size)); 6930 if (vi->rss_key_size > VIRTIO_NET_RSS_MAX_KEY_SIZE) { 6931 dev_err(&vdev->dev, "rss_max_key_size=%u exceeds the limit %u.\n", 6932 vi->rss_key_size, VIRTIO_NET_RSS_MAX_KEY_SIZE); 6933 err = -EINVAL; 6934 goto free; 6935 } 6936 6937 vi->rss_hash_types_supported = 6938 virtio_cread32(vdev, offsetof(struct virtio_net_config, supported_hash_types)); 6939 vi->rss_hash_types_supported &= 6940 ~(VIRTIO_NET_RSS_HASH_TYPE_IP_EX | 6941 VIRTIO_NET_RSS_HASH_TYPE_TCP_EX | 6942 VIRTIO_NET_RSS_HASH_TYPE_UDP_EX); 6943 6944 dev->hw_features |= NETIF_F_RXHASH; 6945 dev->xdp_metadata_ops = &virtnet_xdp_metadata_ops; 6946 } 6947 6948 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO) || 6949 virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO)) 6950 vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash_tunnel); 6951 else if (vi->has_rss_hash_report) 6952 vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash); 6953 else if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) || 6954 virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 6955 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 6956 else 6957 vi->hdr_len = sizeof(struct virtio_net_hdr); 6958 6959 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO_CSUM)) 6960 vi->rx_tnl_csum = true; 6961 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO)) 6962 vi->rx_tnl = true; 6963 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO)) 6964 vi->tx_tnl = true; 6965 6966 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) || 6967 virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 6968 vi->any_header_sg = true; 6969 6970 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 6971 vi->has_cvq = true; 6972 6973 mutex_init(&vi->cvq_lock); 6974 6975 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 6976 mtu = virtio_cread16(vdev, 6977 offsetof(struct virtio_net_config, 6978 mtu)); 6979 if (mtu < dev->min_mtu) { 6980 /* Should never trigger: MTU was previously validated 6981 * in virtnet_validate. 6982 */ 6983 dev_err(&vdev->dev, 6984 "device MTU appears to have changed it is now %d < %d", 6985 mtu, dev->min_mtu); 6986 err = -EINVAL; 6987 goto free; 6988 } 6989 6990 dev->mtu = mtu; 6991 dev->max_mtu = mtu; 6992 } 6993 6994 virtnet_set_big_packets(vi, mtu); 6995 6996 if (vi->any_header_sg) 6997 dev->needed_headroom = vi->hdr_len; 6998 6999 /* Enable multiqueue by default */ 7000 if (num_online_cpus() >= max_queue_pairs) 7001 vi->curr_queue_pairs = max_queue_pairs; 7002 else 7003 vi->curr_queue_pairs = num_online_cpus(); 7004 vi->max_queue_pairs = max_queue_pairs; 7005 7006 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */ 7007 err = init_vqs(vi); 7008 if (err) 7009 goto free; 7010 7011 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) { 7012 vi->intr_coal_rx.max_usecs = 0; 7013 vi->intr_coal_tx.max_usecs = 0; 7014 vi->intr_coal_rx.max_packets = 0; 7015 7016 /* Keep the default values of the coalescing parameters 7017 * aligned with the default napi_tx state. 7018 */ 7019 if (vi->sq[0].napi.weight) 7020 vi->intr_coal_tx.max_packets = 1; 7021 else 7022 vi->intr_coal_tx.max_packets = 0; 7023 } 7024 7025 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) { 7026 /* The reason is the same as VIRTIO_NET_F_NOTF_COAL. */ 7027 for (i = 0; i < vi->max_queue_pairs; i++) 7028 if (vi->sq[i].napi.weight) 7029 vi->sq[i].intr_coal.max_packets = 1; 7030 7031 err = virtnet_init_irq_moder(vi); 7032 if (err) 7033 goto free; 7034 } 7035 7036 #ifdef CONFIG_SYSFS 7037 if (vi->mergeable_rx_bufs) 7038 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group; 7039 #endif 7040 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs); 7041 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs); 7042 7043 virtnet_init_settings(dev); 7044 7045 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) { 7046 vi->failover = net_failover_create(vi->dev); 7047 if (IS_ERR(vi->failover)) { 7048 err = PTR_ERR(vi->failover); 7049 goto free_vqs; 7050 } 7051 } 7052 7053 if (vi->has_rss || vi->has_rss_hash_report) 7054 virtnet_init_default_rss(vi); 7055 7056 enable_rx_mode_work(vi); 7057 7058 /* serialize netdev register + virtio_device_ready() with ndo_open() */ 7059 rtnl_lock(); 7060 7061 err = register_netdevice(dev); 7062 if (err) { 7063 pr_debug("virtio_net: registering device failed\n"); 7064 rtnl_unlock(); 7065 goto free_failover; 7066 } 7067 7068 /* Disable config change notification until ndo_open. */ 7069 virtio_config_driver_disable(vi->vdev); 7070 7071 virtio_device_ready(vdev); 7072 7073 if (vi->has_rss || vi->has_rss_hash_report) { 7074 if (!virtnet_commit_rss_command(vi)) { 7075 dev_warn(&vdev->dev, "RSS disabled because committing failed.\n"); 7076 dev->hw_features &= ~NETIF_F_RXHASH; 7077 vi->has_rss_hash_report = false; 7078 vi->has_rss = false; 7079 } 7080 } 7081 7082 virtnet_set_queues(vi, vi->curr_queue_pairs); 7083 7084 /* a random MAC address has been assigned, notify the device. 7085 * We don't fail probe if VIRTIO_NET_F_CTRL_MAC_ADDR is not there 7086 * because many devices work fine without getting MAC explicitly 7087 */ 7088 if (!virtio_has_feature(vdev, VIRTIO_NET_F_MAC) && 7089 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 7090 struct scatterlist sg; 7091 7092 sg_init_one(&sg, dev->dev_addr, dev->addr_len); 7093 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 7094 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) { 7095 pr_debug("virtio_net: setting MAC address failed\n"); 7096 rtnl_unlock(); 7097 err = -EINVAL; 7098 goto free_unregister_netdev; 7099 } 7100 } 7101 7102 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_DEVICE_STATS)) { 7103 struct virtio_net_stats_capabilities *stats_cap __free(kfree) = NULL; 7104 struct scatterlist sg; 7105 __le64 v; 7106 7107 stats_cap = kzalloc(sizeof(*stats_cap), GFP_KERNEL); 7108 if (!stats_cap) { 7109 rtnl_unlock(); 7110 err = -ENOMEM; 7111 goto free_unregister_netdev; 7112 } 7113 7114 sg_init_one(&sg, stats_cap, sizeof(*stats_cap)); 7115 7116 if (!virtnet_send_command_reply(vi, VIRTIO_NET_CTRL_STATS, 7117 VIRTIO_NET_CTRL_STATS_QUERY, 7118 NULL, &sg)) { 7119 pr_debug("virtio_net: fail to get stats capability\n"); 7120 rtnl_unlock(); 7121 err = -EINVAL; 7122 goto free_unregister_netdev; 7123 } 7124 7125 v = stats_cap->supported_stats_types[0]; 7126 vi->device_stats_cap = le64_to_cpu(v); 7127 } 7128 7129 /* Assume link up if device can't report link status, 7130 otherwise get link status from config. */ 7131 netif_carrier_off(dev); 7132 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) { 7133 virtio_config_changed(vi->vdev); 7134 } else { 7135 vi->status = VIRTIO_NET_S_LINK_UP; 7136 virtnet_update_settings(vi); 7137 netif_carrier_on(dev); 7138 } 7139 7140 for (i = 0; i < ARRAY_SIZE(guest_offloads); i++) { 7141 unsigned int fbit; 7142 7143 fbit = virtio_offload_to_feature(guest_offloads[i]); 7144 if (virtio_has_feature(vi->vdev, fbit)) 7145 set_bit(guest_offloads[i], &vi->guest_offloads); 7146 } 7147 vi->guest_offloads_capable = vi->guest_offloads; 7148 7149 rtnl_unlock(); 7150 7151 err = virtnet_cpu_notif_add(vi); 7152 if (err) { 7153 pr_debug("virtio_net: registering cpu notifier failed\n"); 7154 goto free_unregister_netdev; 7155 } 7156 7157 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n", 7158 dev->name, max_queue_pairs); 7159 7160 return 0; 7161 7162 free_unregister_netdev: 7163 unregister_netdev(dev); 7164 free_failover: 7165 net_failover_destroy(vi->failover); 7166 free_vqs: 7167 virtio_reset_device(vdev); 7168 cancel_delayed_work_sync(&vi->refill); 7169 free_receive_page_frags(vi); 7170 virtnet_del_vqs(vi); 7171 free: 7172 free_netdev(dev); 7173 return err; 7174 } 7175 7176 static void remove_vq_common(struct virtnet_info *vi) 7177 { 7178 int i; 7179 7180 virtio_reset_device(vi->vdev); 7181 7182 /* Free unused buffers in both send and recv, if any. */ 7183 free_unused_bufs(vi); 7184 7185 /* 7186 * Rule of thumb is netdev_tx_reset_queue() should follow any 7187 * skb freeing not followed by netdev_tx_completed_queue() 7188 */ 7189 for (i = 0; i < vi->max_queue_pairs; i++) 7190 netdev_tx_reset_queue(netdev_get_tx_queue(vi->dev, i)); 7191 7192 free_receive_bufs(vi); 7193 7194 free_receive_page_frags(vi); 7195 7196 virtnet_del_vqs(vi); 7197 } 7198 7199 static void virtnet_remove(struct virtio_device *vdev) 7200 { 7201 struct virtnet_info *vi = vdev->priv; 7202 7203 virtnet_cpu_notif_remove(vi); 7204 7205 /* Make sure no work handler is accessing the device. */ 7206 flush_work(&vi->config_work); 7207 disable_rx_mode_work(vi); 7208 flush_work(&vi->rx_mode_work); 7209 7210 virtnet_free_irq_moder(vi); 7211 7212 unregister_netdev(vi->dev); 7213 7214 net_failover_destroy(vi->failover); 7215 7216 remove_vq_common(vi); 7217 7218 free_netdev(vi->dev); 7219 } 7220 7221 static __maybe_unused int virtnet_freeze(struct virtio_device *vdev) 7222 { 7223 struct virtnet_info *vi = vdev->priv; 7224 7225 virtnet_cpu_notif_remove(vi); 7226 virtnet_freeze_down(vdev); 7227 remove_vq_common(vi); 7228 7229 return 0; 7230 } 7231 7232 static __maybe_unused int virtnet_restore(struct virtio_device *vdev) 7233 { 7234 struct virtnet_info *vi = vdev->priv; 7235 int err; 7236 7237 err = virtnet_restore_up(vdev); 7238 if (err) 7239 return err; 7240 virtnet_set_queues(vi, vi->curr_queue_pairs); 7241 7242 err = virtnet_cpu_notif_add(vi); 7243 if (err) { 7244 virtnet_freeze_down(vdev); 7245 remove_vq_common(vi); 7246 return err; 7247 } 7248 7249 return 0; 7250 } 7251 7252 static struct virtio_device_id id_table[] = { 7253 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 7254 { 0 }, 7255 }; 7256 7257 #define VIRTNET_FEATURES \ 7258 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \ 7259 VIRTIO_NET_F_MAC, \ 7260 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \ 7261 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \ 7262 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \ 7263 VIRTIO_NET_F_HOST_USO, VIRTIO_NET_F_GUEST_USO4, VIRTIO_NET_F_GUEST_USO6, \ 7264 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \ 7265 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \ 7266 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \ 7267 VIRTIO_NET_F_CTRL_MAC_ADDR, \ 7268 VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \ 7269 VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY, \ 7270 VIRTIO_NET_F_RSS, VIRTIO_NET_F_HASH_REPORT, VIRTIO_NET_F_NOTF_COAL, \ 7271 VIRTIO_NET_F_VQ_NOTF_COAL, \ 7272 VIRTIO_NET_F_GUEST_HDRLEN, VIRTIO_NET_F_DEVICE_STATS 7273 7274 static unsigned int features[] = { 7275 VIRTNET_FEATURES, 7276 VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO, 7277 VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO_CSUM, 7278 VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO, 7279 VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO_CSUM, 7280 }; 7281 7282 static unsigned int features_legacy[] = { 7283 VIRTNET_FEATURES, 7284 VIRTIO_NET_F_GSO, 7285 VIRTIO_F_ANY_LAYOUT, 7286 }; 7287 7288 static struct virtio_driver virtio_net_driver = { 7289 .feature_table = features, 7290 .feature_table_size = ARRAY_SIZE(features), 7291 .feature_table_legacy = features_legacy, 7292 .feature_table_size_legacy = ARRAY_SIZE(features_legacy), 7293 .driver.name = KBUILD_MODNAME, 7294 .id_table = id_table, 7295 .validate = virtnet_validate, 7296 .probe = virtnet_probe, 7297 .remove = virtnet_remove, 7298 .config_changed = virtnet_config_changed, 7299 #ifdef CONFIG_PM_SLEEP 7300 .freeze = virtnet_freeze, 7301 .restore = virtnet_restore, 7302 #endif 7303 }; 7304 7305 static __init int virtio_net_driver_init(void) 7306 { 7307 int ret; 7308 7309 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online", 7310 virtnet_cpu_online, 7311 virtnet_cpu_down_prep); 7312 if (ret < 0) 7313 goto out; 7314 virtionet_online = ret; 7315 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead", 7316 NULL, virtnet_cpu_dead); 7317 if (ret) 7318 goto err_dead; 7319 ret = register_virtio_driver(&virtio_net_driver); 7320 if (ret) 7321 goto err_virtio; 7322 return 0; 7323 err_virtio: 7324 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 7325 err_dead: 7326 cpuhp_remove_multi_state(virtionet_online); 7327 out: 7328 return ret; 7329 } 7330 module_init(virtio_net_driver_init); 7331 7332 static __exit void virtio_net_driver_exit(void) 7333 { 7334 unregister_virtio_driver(&virtio_net_driver); 7335 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 7336 cpuhp_remove_multi_state(virtionet_online); 7337 } 7338 module_exit(virtio_net_driver_exit); 7339 7340 MODULE_DEVICE_TABLE(virtio, id_table); 7341 MODULE_DESCRIPTION("Virtio network driver"); 7342 MODULE_LICENSE("GPL"); 7343