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