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