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 <net/route.h> 23 #include <net/xdp.h> 24 #include <net/net_failover.h> 25 26 static int napi_weight = NAPI_POLL_WEIGHT; 27 module_param(napi_weight, int, 0444); 28 29 static bool csum = true, gso = true, napi_tx = true; 30 module_param(csum, bool, 0444); 31 module_param(gso, bool, 0444); 32 module_param(napi_tx, bool, 0644); 33 34 /* FIXME: MTU in config. */ 35 #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN) 36 #define GOOD_COPY_LEN 128 37 38 #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD) 39 40 /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */ 41 #define VIRTIO_XDP_HEADROOM 256 42 43 /* Separating two types of XDP xmit */ 44 #define VIRTIO_XDP_TX BIT(0) 45 #define VIRTIO_XDP_REDIR BIT(1) 46 47 #define VIRTIO_XDP_FLAG BIT(0) 48 49 /* RX packet size EWMA. The average packet size is used to determine the packet 50 * buffer size when refilling RX rings. As the entire RX ring may be refilled 51 * at once, the weight is chosen so that the EWMA will be insensitive to short- 52 * term, transient changes in packet size. 53 */ 54 DECLARE_EWMA(pkt_len, 0, 64) 55 56 #define VIRTNET_DRIVER_VERSION "1.0.0" 57 58 static const unsigned long guest_offloads[] = { 59 VIRTIO_NET_F_GUEST_TSO4, 60 VIRTIO_NET_F_GUEST_TSO6, 61 VIRTIO_NET_F_GUEST_ECN, 62 VIRTIO_NET_F_GUEST_UFO, 63 VIRTIO_NET_F_GUEST_CSUM 64 }; 65 66 #define GUEST_OFFLOAD_LRO_MASK ((1ULL << VIRTIO_NET_F_GUEST_TSO4) | \ 67 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \ 68 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \ 69 (1ULL << VIRTIO_NET_F_GUEST_UFO)) 70 71 struct virtnet_stat_desc { 72 char desc[ETH_GSTRING_LEN]; 73 size_t offset; 74 }; 75 76 struct virtnet_sq_stats { 77 struct u64_stats_sync syncp; 78 u64 packets; 79 u64 bytes; 80 u64 xdp_tx; 81 u64 xdp_tx_drops; 82 u64 kicks; 83 }; 84 85 struct virtnet_rq_stats { 86 struct u64_stats_sync syncp; 87 u64 packets; 88 u64 bytes; 89 u64 drops; 90 u64 xdp_packets; 91 u64 xdp_tx; 92 u64 xdp_redirects; 93 u64 xdp_drops; 94 u64 kicks; 95 }; 96 97 #define VIRTNET_SQ_STAT(m) offsetof(struct virtnet_sq_stats, m) 98 #define VIRTNET_RQ_STAT(m) offsetof(struct virtnet_rq_stats, m) 99 100 static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = { 101 { "packets", VIRTNET_SQ_STAT(packets) }, 102 { "bytes", VIRTNET_SQ_STAT(bytes) }, 103 { "xdp_tx", VIRTNET_SQ_STAT(xdp_tx) }, 104 { "xdp_tx_drops", VIRTNET_SQ_STAT(xdp_tx_drops) }, 105 { "kicks", VIRTNET_SQ_STAT(kicks) }, 106 }; 107 108 static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = { 109 { "packets", VIRTNET_RQ_STAT(packets) }, 110 { "bytes", VIRTNET_RQ_STAT(bytes) }, 111 { "drops", VIRTNET_RQ_STAT(drops) }, 112 { "xdp_packets", VIRTNET_RQ_STAT(xdp_packets) }, 113 { "xdp_tx", VIRTNET_RQ_STAT(xdp_tx) }, 114 { "xdp_redirects", VIRTNET_RQ_STAT(xdp_redirects) }, 115 { "xdp_drops", VIRTNET_RQ_STAT(xdp_drops) }, 116 { "kicks", VIRTNET_RQ_STAT(kicks) }, 117 }; 118 119 #define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc) 120 #define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc) 121 122 /* Internal representation of a send virtqueue */ 123 struct send_queue { 124 /* Virtqueue associated with this send _queue */ 125 struct virtqueue *vq; 126 127 /* TX: fragments + linear part + virtio header */ 128 struct scatterlist sg[MAX_SKB_FRAGS + 2]; 129 130 /* Name of the send queue: output.$index */ 131 char name[40]; 132 133 struct virtnet_sq_stats stats; 134 135 struct napi_struct napi; 136 }; 137 138 /* Internal representation of a receive virtqueue */ 139 struct receive_queue { 140 /* Virtqueue associated with this receive_queue */ 141 struct virtqueue *vq; 142 143 struct napi_struct napi; 144 145 struct bpf_prog __rcu *xdp_prog; 146 147 struct virtnet_rq_stats stats; 148 149 /* Chain pages by the private ptr. */ 150 struct page *pages; 151 152 /* Average packet length for mergeable receive buffers. */ 153 struct ewma_pkt_len mrg_avg_pkt_len; 154 155 /* Page frag for packet buffer allocation. */ 156 struct page_frag alloc_frag; 157 158 /* RX: fragments + linear part + virtio header */ 159 struct scatterlist sg[MAX_SKB_FRAGS + 2]; 160 161 /* Min single buffer size for mergeable buffers case. */ 162 unsigned int min_buf_len; 163 164 /* Name of this receive queue: input.$index */ 165 char name[40]; 166 167 struct xdp_rxq_info xdp_rxq; 168 }; 169 170 /* Control VQ buffers: protected by the rtnl lock */ 171 struct control_buf { 172 struct virtio_net_ctrl_hdr hdr; 173 virtio_net_ctrl_ack status; 174 struct virtio_net_ctrl_mq mq; 175 u8 promisc; 176 u8 allmulti; 177 __virtio16 vid; 178 __virtio64 offloads; 179 }; 180 181 struct virtnet_info { 182 struct virtio_device *vdev; 183 struct virtqueue *cvq; 184 struct net_device *dev; 185 struct send_queue *sq; 186 struct receive_queue *rq; 187 unsigned int status; 188 189 /* Max # of queue pairs supported by the device */ 190 u16 max_queue_pairs; 191 192 /* # of queue pairs currently used by the driver */ 193 u16 curr_queue_pairs; 194 195 /* # of XDP queue pairs currently used by the driver */ 196 u16 xdp_queue_pairs; 197 198 /* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */ 199 bool xdp_enabled; 200 201 /* I like... big packets and I cannot lie! */ 202 bool big_packets; 203 204 /* Host will merge rx buffers for big packets (shake it! shake it!) */ 205 bool mergeable_rx_bufs; 206 207 /* Has control virtqueue */ 208 bool has_cvq; 209 210 /* Host can handle any s/g split between our header and packet data */ 211 bool any_header_sg; 212 213 /* Packet virtio header size */ 214 u8 hdr_len; 215 216 /* Work struct for refilling if we run low on memory. */ 217 struct delayed_work refill; 218 219 /* Work struct for config space updates */ 220 struct work_struct config_work; 221 222 /* Does the affinity hint is set for virtqueues? */ 223 bool affinity_hint_set; 224 225 /* CPU hotplug instances for online & dead */ 226 struct hlist_node node; 227 struct hlist_node node_dead; 228 229 struct control_buf *ctrl; 230 231 /* Ethtool settings */ 232 u8 duplex; 233 u32 speed; 234 235 unsigned long guest_offloads; 236 unsigned long guest_offloads_capable; 237 238 /* failover when STANDBY feature enabled */ 239 struct failover *failover; 240 }; 241 242 struct padded_vnet_hdr { 243 struct virtio_net_hdr_mrg_rxbuf hdr; 244 /* 245 * hdr is in a separate sg buffer, and data sg buffer shares same page 246 * with this header sg. This padding makes next sg 16 byte aligned 247 * after the header. 248 */ 249 char padding[4]; 250 }; 251 252 static bool is_xdp_frame(void *ptr) 253 { 254 return (unsigned long)ptr & VIRTIO_XDP_FLAG; 255 } 256 257 static void *xdp_to_ptr(struct xdp_frame *ptr) 258 { 259 return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG); 260 } 261 262 static struct xdp_frame *ptr_to_xdp(void *ptr) 263 { 264 return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG); 265 } 266 267 /* Converting between virtqueue no. and kernel tx/rx queue no. 268 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq 269 */ 270 static int vq2txq(struct virtqueue *vq) 271 { 272 return (vq->index - 1) / 2; 273 } 274 275 static int txq2vq(int txq) 276 { 277 return txq * 2 + 1; 278 } 279 280 static int vq2rxq(struct virtqueue *vq) 281 { 282 return vq->index / 2; 283 } 284 285 static int rxq2vq(int rxq) 286 { 287 return rxq * 2; 288 } 289 290 static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb) 291 { 292 return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb; 293 } 294 295 /* 296 * private is used to chain pages for big packets, put the whole 297 * most recent used list in the beginning for reuse 298 */ 299 static void give_pages(struct receive_queue *rq, struct page *page) 300 { 301 struct page *end; 302 303 /* Find end of list, sew whole thing into vi->rq.pages. */ 304 for (end = page; end->private; end = (struct page *)end->private); 305 end->private = (unsigned long)rq->pages; 306 rq->pages = page; 307 } 308 309 static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask) 310 { 311 struct page *p = rq->pages; 312 313 if (p) { 314 rq->pages = (struct page *)p->private; 315 /* clear private here, it is used to chain pages */ 316 p->private = 0; 317 } else 318 p = alloc_page(gfp_mask); 319 return p; 320 } 321 322 static void virtqueue_napi_schedule(struct napi_struct *napi, 323 struct virtqueue *vq) 324 { 325 if (napi_schedule_prep(napi)) { 326 virtqueue_disable_cb(vq); 327 __napi_schedule(napi); 328 } 329 } 330 331 static void virtqueue_napi_complete(struct napi_struct *napi, 332 struct virtqueue *vq, int processed) 333 { 334 int opaque; 335 336 opaque = virtqueue_enable_cb_prepare(vq); 337 if (napi_complete_done(napi, processed)) { 338 if (unlikely(virtqueue_poll(vq, opaque))) 339 virtqueue_napi_schedule(napi, vq); 340 } else { 341 virtqueue_disable_cb(vq); 342 } 343 } 344 345 static void skb_xmit_done(struct virtqueue *vq) 346 { 347 struct virtnet_info *vi = vq->vdev->priv; 348 struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi; 349 350 /* Suppress further interrupts. */ 351 virtqueue_disable_cb(vq); 352 353 if (napi->weight) 354 virtqueue_napi_schedule(napi, vq); 355 else 356 /* We were probably waiting for more output buffers. */ 357 netif_wake_subqueue(vi->dev, vq2txq(vq)); 358 } 359 360 #define MRG_CTX_HEADER_SHIFT 22 361 static void *mergeable_len_to_ctx(unsigned int truesize, 362 unsigned int headroom) 363 { 364 return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize); 365 } 366 367 static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx) 368 { 369 return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT; 370 } 371 372 static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx) 373 { 374 return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1); 375 } 376 377 /* Called from bottom half context */ 378 static struct sk_buff *page_to_skb(struct virtnet_info *vi, 379 struct receive_queue *rq, 380 struct page *page, unsigned int offset, 381 unsigned int len, unsigned int truesize, 382 bool hdr_valid, unsigned int metasize) 383 { 384 struct sk_buff *skb; 385 struct virtio_net_hdr_mrg_rxbuf *hdr; 386 unsigned int copy, hdr_len, hdr_padded_len; 387 char *p; 388 389 p = page_address(page) + offset; 390 391 /* copy small packet so we can reuse these pages for small data */ 392 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN); 393 if (unlikely(!skb)) 394 return NULL; 395 396 hdr = skb_vnet_hdr(skb); 397 398 hdr_len = vi->hdr_len; 399 if (vi->mergeable_rx_bufs) 400 hdr_padded_len = sizeof(*hdr); 401 else 402 hdr_padded_len = sizeof(struct padded_vnet_hdr); 403 404 /* hdr_valid means no XDP, so we can copy the vnet header */ 405 if (hdr_valid) 406 memcpy(hdr, p, hdr_len); 407 408 len -= hdr_len; 409 offset += hdr_padded_len; 410 p += hdr_padded_len; 411 412 copy = len; 413 if (copy > skb_tailroom(skb)) 414 copy = skb_tailroom(skb); 415 skb_put_data(skb, p, copy); 416 417 if (metasize) { 418 __skb_pull(skb, metasize); 419 skb_metadata_set(skb, metasize); 420 } 421 422 len -= copy; 423 offset += copy; 424 425 if (vi->mergeable_rx_bufs) { 426 if (len) 427 skb_add_rx_frag(skb, 0, page, offset, len, truesize); 428 else 429 put_page(page); 430 return skb; 431 } 432 433 /* 434 * Verify that we can indeed put this data into a skb. 435 * This is here to handle cases when the device erroneously 436 * tries to receive more than is possible. This is usually 437 * the case of a broken device. 438 */ 439 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) { 440 net_dbg_ratelimited("%s: too much data\n", skb->dev->name); 441 dev_kfree_skb(skb); 442 return NULL; 443 } 444 BUG_ON(offset >= PAGE_SIZE); 445 while (len) { 446 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len); 447 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset, 448 frag_size, truesize); 449 len -= frag_size; 450 page = (struct page *)page->private; 451 offset = 0; 452 } 453 454 if (page) 455 give_pages(rq, page); 456 457 return skb; 458 } 459 460 static int __virtnet_xdp_xmit_one(struct virtnet_info *vi, 461 struct send_queue *sq, 462 struct xdp_frame *xdpf) 463 { 464 struct virtio_net_hdr_mrg_rxbuf *hdr; 465 int err; 466 467 if (unlikely(xdpf->headroom < vi->hdr_len)) 468 return -EOVERFLOW; 469 470 /* Make room for virtqueue hdr (also change xdpf->headroom?) */ 471 xdpf->data -= vi->hdr_len; 472 /* Zero header and leave csum up to XDP layers */ 473 hdr = xdpf->data; 474 memset(hdr, 0, vi->hdr_len); 475 xdpf->len += vi->hdr_len; 476 477 sg_init_one(sq->sg, xdpf->data, xdpf->len); 478 479 err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp_to_ptr(xdpf), 480 GFP_ATOMIC); 481 if (unlikely(err)) 482 return -ENOSPC; /* Caller handle free/refcnt */ 483 484 return 0; 485 } 486 487 /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on 488 * the current cpu, so it does not need to be locked. 489 * 490 * Here we use marco instead of inline functions because we have to deal with 491 * three issues at the same time: 1. the choice of sq. 2. judge and execute the 492 * lock/unlock of txq 3. make sparse happy. It is difficult for two inline 493 * functions to perfectly solve these three problems at the same time. 494 */ 495 #define virtnet_xdp_get_sq(vi) ({ \ 496 struct netdev_queue *txq; \ 497 typeof(vi) v = (vi); \ 498 unsigned int qp; \ 499 \ 500 if (v->curr_queue_pairs > nr_cpu_ids) { \ 501 qp = v->curr_queue_pairs - v->xdp_queue_pairs; \ 502 qp += smp_processor_id(); \ 503 txq = netdev_get_tx_queue(v->dev, qp); \ 504 __netif_tx_acquire(txq); \ 505 } else { \ 506 qp = smp_processor_id() % v->curr_queue_pairs; \ 507 txq = netdev_get_tx_queue(v->dev, qp); \ 508 __netif_tx_lock(txq, raw_smp_processor_id()); \ 509 } \ 510 v->sq + qp; \ 511 }) 512 513 #define virtnet_xdp_put_sq(vi, q) { \ 514 struct netdev_queue *txq; \ 515 typeof(vi) v = (vi); \ 516 \ 517 txq = netdev_get_tx_queue(v->dev, (q) - v->sq); \ 518 if (v->curr_queue_pairs > nr_cpu_ids) \ 519 __netif_tx_release(txq); \ 520 else \ 521 __netif_tx_unlock(txq); \ 522 } 523 524 static int virtnet_xdp_xmit(struct net_device *dev, 525 int n, struct xdp_frame **frames, u32 flags) 526 { 527 struct virtnet_info *vi = netdev_priv(dev); 528 struct receive_queue *rq = vi->rq; 529 struct bpf_prog *xdp_prog; 530 struct send_queue *sq; 531 unsigned int len; 532 int packets = 0; 533 int bytes = 0; 534 int nxmit = 0; 535 int kicks = 0; 536 void *ptr; 537 int ret; 538 int i; 539 540 /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this 541 * indicate XDP resources have been successfully allocated. 542 */ 543 xdp_prog = rcu_access_pointer(rq->xdp_prog); 544 if (!xdp_prog) 545 return -ENXIO; 546 547 sq = virtnet_xdp_get_sq(vi); 548 549 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) { 550 ret = -EINVAL; 551 goto out; 552 } 553 554 /* Free up any pending old buffers before queueing new ones. */ 555 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) { 556 if (likely(is_xdp_frame(ptr))) { 557 struct xdp_frame *frame = ptr_to_xdp(ptr); 558 559 bytes += frame->len; 560 xdp_return_frame(frame); 561 } else { 562 struct sk_buff *skb = ptr; 563 564 bytes += skb->len; 565 napi_consume_skb(skb, false); 566 } 567 packets++; 568 } 569 570 for (i = 0; i < n; i++) { 571 struct xdp_frame *xdpf = frames[i]; 572 573 if (__virtnet_xdp_xmit_one(vi, sq, xdpf)) 574 break; 575 nxmit++; 576 } 577 ret = nxmit; 578 579 if (flags & XDP_XMIT_FLUSH) { 580 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) 581 kicks = 1; 582 } 583 out: 584 u64_stats_update_begin(&sq->stats.syncp); 585 sq->stats.bytes += bytes; 586 sq->stats.packets += packets; 587 sq->stats.xdp_tx += n; 588 sq->stats.xdp_tx_drops += n - nxmit; 589 sq->stats.kicks += kicks; 590 u64_stats_update_end(&sq->stats.syncp); 591 592 virtnet_xdp_put_sq(vi, sq); 593 return ret; 594 } 595 596 static unsigned int virtnet_get_headroom(struct virtnet_info *vi) 597 { 598 return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0; 599 } 600 601 /* We copy the packet for XDP in the following cases: 602 * 603 * 1) Packet is scattered across multiple rx buffers. 604 * 2) Headroom space is insufficient. 605 * 606 * This is inefficient but it's a temporary condition that 607 * we hit right after XDP is enabled and until queue is refilled 608 * with large buffers with sufficient headroom - so it should affect 609 * at most queue size packets. 610 * Afterwards, the conditions to enable 611 * XDP should preclude the underlying device from sending packets 612 * across multiple buffers (num_buf > 1), and we make sure buffers 613 * have enough headroom. 614 */ 615 static struct page *xdp_linearize_page(struct receive_queue *rq, 616 u16 *num_buf, 617 struct page *p, 618 int offset, 619 int page_off, 620 unsigned int *len) 621 { 622 struct page *page = alloc_page(GFP_ATOMIC); 623 624 if (!page) 625 return NULL; 626 627 memcpy(page_address(page) + page_off, page_address(p) + offset, *len); 628 page_off += *len; 629 630 while (--*num_buf) { 631 int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 632 unsigned int buflen; 633 void *buf; 634 int off; 635 636 buf = virtqueue_get_buf(rq->vq, &buflen); 637 if (unlikely(!buf)) 638 goto err_buf; 639 640 p = virt_to_head_page(buf); 641 off = buf - page_address(p); 642 643 /* guard against a misconfigured or uncooperative backend that 644 * is sending packet larger than the MTU. 645 */ 646 if ((page_off + buflen + tailroom) > PAGE_SIZE) { 647 put_page(p); 648 goto err_buf; 649 } 650 651 memcpy(page_address(page) + page_off, 652 page_address(p) + off, buflen); 653 page_off += buflen; 654 put_page(p); 655 } 656 657 /* Headroom does not contribute to packet length */ 658 *len = page_off - VIRTIO_XDP_HEADROOM; 659 return page; 660 err_buf: 661 __free_pages(page, 0); 662 return NULL; 663 } 664 665 static struct sk_buff *receive_small(struct net_device *dev, 666 struct virtnet_info *vi, 667 struct receive_queue *rq, 668 void *buf, void *ctx, 669 unsigned int len, 670 unsigned int *xdp_xmit, 671 struct virtnet_rq_stats *stats) 672 { 673 struct sk_buff *skb; 674 struct bpf_prog *xdp_prog; 675 unsigned int xdp_headroom = (unsigned long)ctx; 676 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom; 677 unsigned int headroom = vi->hdr_len + header_offset; 678 unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) + 679 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 680 struct page *page = virt_to_head_page(buf); 681 unsigned int delta = 0; 682 struct page *xdp_page; 683 int err; 684 unsigned int metasize = 0; 685 686 len -= vi->hdr_len; 687 stats->bytes += len; 688 689 rcu_read_lock(); 690 xdp_prog = rcu_dereference(rq->xdp_prog); 691 if (xdp_prog) { 692 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset; 693 struct xdp_frame *xdpf; 694 struct xdp_buff xdp; 695 void *orig_data; 696 u32 act; 697 698 if (unlikely(hdr->hdr.gso_type)) 699 goto err_xdp; 700 701 if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) { 702 int offset = buf - page_address(page) + header_offset; 703 unsigned int tlen = len + vi->hdr_len; 704 u16 num_buf = 1; 705 706 xdp_headroom = virtnet_get_headroom(vi); 707 header_offset = VIRTNET_RX_PAD + xdp_headroom; 708 headroom = vi->hdr_len + header_offset; 709 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) + 710 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 711 xdp_page = xdp_linearize_page(rq, &num_buf, page, 712 offset, header_offset, 713 &tlen); 714 if (!xdp_page) 715 goto err_xdp; 716 717 buf = page_address(xdp_page); 718 put_page(page); 719 page = xdp_page; 720 } 721 722 xdp_init_buff(&xdp, buflen, &rq->xdp_rxq); 723 xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len, 724 xdp_headroom, len, true); 725 orig_data = xdp.data; 726 act = bpf_prog_run_xdp(xdp_prog, &xdp); 727 stats->xdp_packets++; 728 729 switch (act) { 730 case XDP_PASS: 731 /* Recalculate length in case bpf program changed it */ 732 delta = orig_data - xdp.data; 733 len = xdp.data_end - xdp.data; 734 metasize = xdp.data - xdp.data_meta; 735 break; 736 case XDP_TX: 737 stats->xdp_tx++; 738 xdpf = xdp_convert_buff_to_frame(&xdp); 739 if (unlikely(!xdpf)) 740 goto err_xdp; 741 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0); 742 if (unlikely(!err)) { 743 xdp_return_frame_rx_napi(xdpf); 744 } else if (unlikely(err < 0)) { 745 trace_xdp_exception(vi->dev, xdp_prog, act); 746 goto err_xdp; 747 } 748 *xdp_xmit |= VIRTIO_XDP_TX; 749 rcu_read_unlock(); 750 goto xdp_xmit; 751 case XDP_REDIRECT: 752 stats->xdp_redirects++; 753 err = xdp_do_redirect(dev, &xdp, xdp_prog); 754 if (err) 755 goto err_xdp; 756 *xdp_xmit |= VIRTIO_XDP_REDIR; 757 rcu_read_unlock(); 758 goto xdp_xmit; 759 default: 760 bpf_warn_invalid_xdp_action(act); 761 fallthrough; 762 case XDP_ABORTED: 763 trace_xdp_exception(vi->dev, xdp_prog, act); 764 goto err_xdp; 765 case XDP_DROP: 766 goto err_xdp; 767 } 768 } 769 rcu_read_unlock(); 770 771 skb = build_skb(buf, buflen); 772 if (!skb) { 773 put_page(page); 774 goto err; 775 } 776 skb_reserve(skb, headroom - delta); 777 skb_put(skb, len); 778 if (!xdp_prog) { 779 buf += header_offset; 780 memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len); 781 } /* keep zeroed vnet hdr since XDP is loaded */ 782 783 if (metasize) 784 skb_metadata_set(skb, metasize); 785 786 err: 787 return skb; 788 789 err_xdp: 790 rcu_read_unlock(); 791 stats->xdp_drops++; 792 stats->drops++; 793 put_page(page); 794 xdp_xmit: 795 return NULL; 796 } 797 798 static struct sk_buff *receive_big(struct net_device *dev, 799 struct virtnet_info *vi, 800 struct receive_queue *rq, 801 void *buf, 802 unsigned int len, 803 struct virtnet_rq_stats *stats) 804 { 805 struct page *page = buf; 806 struct sk_buff *skb = 807 page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, true, 0); 808 809 stats->bytes += len - vi->hdr_len; 810 if (unlikely(!skb)) 811 goto err; 812 813 return skb; 814 815 err: 816 stats->drops++; 817 give_pages(rq, page); 818 return NULL; 819 } 820 821 static struct sk_buff *receive_mergeable(struct net_device *dev, 822 struct virtnet_info *vi, 823 struct receive_queue *rq, 824 void *buf, 825 void *ctx, 826 unsigned int len, 827 unsigned int *xdp_xmit, 828 struct virtnet_rq_stats *stats) 829 { 830 struct virtio_net_hdr_mrg_rxbuf *hdr = buf; 831 u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers); 832 struct page *page = virt_to_head_page(buf); 833 int offset = buf - page_address(page); 834 struct sk_buff *head_skb, *curr_skb; 835 struct bpf_prog *xdp_prog; 836 unsigned int truesize = mergeable_ctx_to_truesize(ctx); 837 unsigned int headroom = mergeable_ctx_to_headroom(ctx); 838 unsigned int metasize = 0; 839 unsigned int frame_sz; 840 int err; 841 842 head_skb = NULL; 843 stats->bytes += len - vi->hdr_len; 844 845 rcu_read_lock(); 846 xdp_prog = rcu_dereference(rq->xdp_prog); 847 if (xdp_prog) { 848 struct xdp_frame *xdpf; 849 struct page *xdp_page; 850 struct xdp_buff xdp; 851 void *data; 852 u32 act; 853 854 /* Transient failure which in theory could occur if 855 * in-flight packets from before XDP was enabled reach 856 * the receive path after XDP is loaded. 857 */ 858 if (unlikely(hdr->hdr.gso_type)) 859 goto err_xdp; 860 861 /* Buffers with headroom use PAGE_SIZE as alloc size, 862 * see add_recvbuf_mergeable() + get_mergeable_buf_len() 863 */ 864 frame_sz = headroom ? PAGE_SIZE : truesize; 865 866 /* This happens when rx buffer size is underestimated 867 * or headroom is not enough because of the buffer 868 * was refilled before XDP is set. This should only 869 * happen for the first several packets, so we don't 870 * care much about its performance. 871 */ 872 if (unlikely(num_buf > 1 || 873 headroom < virtnet_get_headroom(vi))) { 874 /* linearize data for XDP */ 875 xdp_page = xdp_linearize_page(rq, &num_buf, 876 page, offset, 877 VIRTIO_XDP_HEADROOM, 878 &len); 879 frame_sz = PAGE_SIZE; 880 881 if (!xdp_page) 882 goto err_xdp; 883 offset = VIRTIO_XDP_HEADROOM; 884 } else { 885 xdp_page = page; 886 } 887 888 /* Allow consuming headroom but reserve enough space to push 889 * the descriptor on if we get an XDP_TX return code. 890 */ 891 data = page_address(xdp_page) + offset; 892 xdp_init_buff(&xdp, frame_sz - vi->hdr_len, &rq->xdp_rxq); 893 xdp_prepare_buff(&xdp, data - VIRTIO_XDP_HEADROOM + vi->hdr_len, 894 VIRTIO_XDP_HEADROOM, len - vi->hdr_len, true); 895 896 act = bpf_prog_run_xdp(xdp_prog, &xdp); 897 stats->xdp_packets++; 898 899 switch (act) { 900 case XDP_PASS: 901 metasize = xdp.data - xdp.data_meta; 902 903 /* recalculate offset to account for any header 904 * adjustments and minus the metasize to copy the 905 * metadata in page_to_skb(). Note other cases do not 906 * build an skb and avoid using offset 907 */ 908 offset = xdp.data - page_address(xdp_page) - 909 vi->hdr_len - metasize; 910 911 /* recalculate len if xdp.data, xdp.data_end or 912 * xdp.data_meta were adjusted 913 */ 914 len = xdp.data_end - xdp.data + vi->hdr_len + metasize; 915 /* We can only create skb based on xdp_page. */ 916 if (unlikely(xdp_page != page)) { 917 rcu_read_unlock(); 918 put_page(page); 919 head_skb = page_to_skb(vi, rq, xdp_page, offset, 920 len, PAGE_SIZE, false, 921 metasize); 922 return head_skb; 923 } 924 break; 925 case XDP_TX: 926 stats->xdp_tx++; 927 xdpf = xdp_convert_buff_to_frame(&xdp); 928 if (unlikely(!xdpf)) 929 goto err_xdp; 930 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0); 931 if (unlikely(!err)) { 932 xdp_return_frame_rx_napi(xdpf); 933 } else if (unlikely(err < 0)) { 934 trace_xdp_exception(vi->dev, xdp_prog, act); 935 if (unlikely(xdp_page != page)) 936 put_page(xdp_page); 937 goto err_xdp; 938 } 939 *xdp_xmit |= VIRTIO_XDP_TX; 940 if (unlikely(xdp_page != page)) 941 put_page(page); 942 rcu_read_unlock(); 943 goto xdp_xmit; 944 case XDP_REDIRECT: 945 stats->xdp_redirects++; 946 err = xdp_do_redirect(dev, &xdp, xdp_prog); 947 if (err) { 948 if (unlikely(xdp_page != page)) 949 put_page(xdp_page); 950 goto err_xdp; 951 } 952 *xdp_xmit |= VIRTIO_XDP_REDIR; 953 if (unlikely(xdp_page != page)) 954 put_page(page); 955 rcu_read_unlock(); 956 goto xdp_xmit; 957 default: 958 bpf_warn_invalid_xdp_action(act); 959 fallthrough; 960 case XDP_ABORTED: 961 trace_xdp_exception(vi->dev, xdp_prog, act); 962 fallthrough; 963 case XDP_DROP: 964 if (unlikely(xdp_page != page)) 965 __free_pages(xdp_page, 0); 966 goto err_xdp; 967 } 968 } 969 rcu_read_unlock(); 970 971 if (unlikely(len > truesize)) { 972 pr_debug("%s: rx error: len %u exceeds truesize %lu\n", 973 dev->name, len, (unsigned long)ctx); 974 dev->stats.rx_length_errors++; 975 goto err_skb; 976 } 977 978 head_skb = page_to_skb(vi, rq, page, offset, len, truesize, !xdp_prog, 979 metasize); 980 curr_skb = head_skb; 981 982 if (unlikely(!curr_skb)) 983 goto err_skb; 984 while (--num_buf) { 985 int num_skb_frags; 986 987 buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx); 988 if (unlikely(!buf)) { 989 pr_debug("%s: rx error: %d buffers out of %d missing\n", 990 dev->name, num_buf, 991 virtio16_to_cpu(vi->vdev, 992 hdr->num_buffers)); 993 dev->stats.rx_length_errors++; 994 goto err_buf; 995 } 996 997 stats->bytes += len; 998 page = virt_to_head_page(buf); 999 1000 truesize = mergeable_ctx_to_truesize(ctx); 1001 if (unlikely(len > truesize)) { 1002 pr_debug("%s: rx error: len %u exceeds truesize %lu\n", 1003 dev->name, len, (unsigned long)ctx); 1004 dev->stats.rx_length_errors++; 1005 goto err_skb; 1006 } 1007 1008 num_skb_frags = skb_shinfo(curr_skb)->nr_frags; 1009 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) { 1010 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC); 1011 1012 if (unlikely(!nskb)) 1013 goto err_skb; 1014 if (curr_skb == head_skb) 1015 skb_shinfo(curr_skb)->frag_list = nskb; 1016 else 1017 curr_skb->next = nskb; 1018 curr_skb = nskb; 1019 head_skb->truesize += nskb->truesize; 1020 num_skb_frags = 0; 1021 } 1022 if (curr_skb != head_skb) { 1023 head_skb->data_len += len; 1024 head_skb->len += len; 1025 head_skb->truesize += truesize; 1026 } 1027 offset = buf - page_address(page); 1028 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) { 1029 put_page(page); 1030 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1, 1031 len, truesize); 1032 } else { 1033 skb_add_rx_frag(curr_skb, num_skb_frags, page, 1034 offset, len, truesize); 1035 } 1036 } 1037 1038 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len); 1039 return head_skb; 1040 1041 err_xdp: 1042 rcu_read_unlock(); 1043 stats->xdp_drops++; 1044 err_skb: 1045 put_page(page); 1046 while (num_buf-- > 1) { 1047 buf = virtqueue_get_buf(rq->vq, &len); 1048 if (unlikely(!buf)) { 1049 pr_debug("%s: rx error: %d buffers missing\n", 1050 dev->name, num_buf); 1051 dev->stats.rx_length_errors++; 1052 break; 1053 } 1054 stats->bytes += len; 1055 page = virt_to_head_page(buf); 1056 put_page(page); 1057 } 1058 err_buf: 1059 stats->drops++; 1060 dev_kfree_skb(head_skb); 1061 xdp_xmit: 1062 return NULL; 1063 } 1064 1065 static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq, 1066 void *buf, unsigned int len, void **ctx, 1067 unsigned int *xdp_xmit, 1068 struct virtnet_rq_stats *stats) 1069 { 1070 struct net_device *dev = vi->dev; 1071 struct sk_buff *skb; 1072 struct virtio_net_hdr_mrg_rxbuf *hdr; 1073 1074 if (unlikely(len < vi->hdr_len + ETH_HLEN)) { 1075 pr_debug("%s: short packet %i\n", dev->name, len); 1076 dev->stats.rx_length_errors++; 1077 if (vi->mergeable_rx_bufs) { 1078 put_page(virt_to_head_page(buf)); 1079 } else if (vi->big_packets) { 1080 give_pages(rq, buf); 1081 } else { 1082 put_page(virt_to_head_page(buf)); 1083 } 1084 return; 1085 } 1086 1087 if (vi->mergeable_rx_bufs) 1088 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit, 1089 stats); 1090 else if (vi->big_packets) 1091 skb = receive_big(dev, vi, rq, buf, len, stats); 1092 else 1093 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats); 1094 1095 if (unlikely(!skb)) 1096 return; 1097 1098 hdr = skb_vnet_hdr(skb); 1099 1100 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) 1101 skb->ip_summed = CHECKSUM_UNNECESSARY; 1102 1103 if (virtio_net_hdr_to_skb(skb, &hdr->hdr, 1104 virtio_is_little_endian(vi->vdev))) { 1105 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n", 1106 dev->name, hdr->hdr.gso_type, 1107 hdr->hdr.gso_size); 1108 goto frame_err; 1109 } 1110 1111 skb_record_rx_queue(skb, vq2rxq(rq->vq)); 1112 skb->protocol = eth_type_trans(skb, dev); 1113 pr_debug("Receiving skb proto 0x%04x len %i type %i\n", 1114 ntohs(skb->protocol), skb->len, skb->pkt_type); 1115 1116 napi_gro_receive(&rq->napi, skb); 1117 return; 1118 1119 frame_err: 1120 dev->stats.rx_frame_errors++; 1121 dev_kfree_skb(skb); 1122 } 1123 1124 /* Unlike mergeable buffers, all buffers are allocated to the 1125 * same size, except for the headroom. For this reason we do 1126 * not need to use mergeable_len_to_ctx here - it is enough 1127 * to store the headroom as the context ignoring the truesize. 1128 */ 1129 static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq, 1130 gfp_t gfp) 1131 { 1132 struct page_frag *alloc_frag = &rq->alloc_frag; 1133 char *buf; 1134 unsigned int xdp_headroom = virtnet_get_headroom(vi); 1135 void *ctx = (void *)(unsigned long)xdp_headroom; 1136 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom; 1137 int err; 1138 1139 len = SKB_DATA_ALIGN(len) + 1140 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 1141 if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp))) 1142 return -ENOMEM; 1143 1144 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 1145 get_page(alloc_frag->page); 1146 alloc_frag->offset += len; 1147 sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom, 1148 vi->hdr_len + GOOD_PACKET_LEN); 1149 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp); 1150 if (err < 0) 1151 put_page(virt_to_head_page(buf)); 1152 return err; 1153 } 1154 1155 static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq, 1156 gfp_t gfp) 1157 { 1158 struct page *first, *list = NULL; 1159 char *p; 1160 int i, err, offset; 1161 1162 sg_init_table(rq->sg, MAX_SKB_FRAGS + 2); 1163 1164 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */ 1165 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) { 1166 first = get_a_page(rq, gfp); 1167 if (!first) { 1168 if (list) 1169 give_pages(rq, list); 1170 return -ENOMEM; 1171 } 1172 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE); 1173 1174 /* chain new page in list head to match sg */ 1175 first->private = (unsigned long)list; 1176 list = first; 1177 } 1178 1179 first = get_a_page(rq, gfp); 1180 if (!first) { 1181 give_pages(rq, list); 1182 return -ENOMEM; 1183 } 1184 p = page_address(first); 1185 1186 /* rq->sg[0], rq->sg[1] share the same page */ 1187 /* a separated rq->sg[0] for header - required in case !any_header_sg */ 1188 sg_set_buf(&rq->sg[0], p, vi->hdr_len); 1189 1190 /* rq->sg[1] for data packet, from offset */ 1191 offset = sizeof(struct padded_vnet_hdr); 1192 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset); 1193 1194 /* chain first in list head */ 1195 first->private = (unsigned long)list; 1196 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2, 1197 first, gfp); 1198 if (err < 0) 1199 give_pages(rq, first); 1200 1201 return err; 1202 } 1203 1204 static unsigned int get_mergeable_buf_len(struct receive_queue *rq, 1205 struct ewma_pkt_len *avg_pkt_len, 1206 unsigned int room) 1207 { 1208 const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 1209 unsigned int len; 1210 1211 if (room) 1212 return PAGE_SIZE - room; 1213 1214 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len), 1215 rq->min_buf_len, PAGE_SIZE - hdr_len); 1216 1217 return ALIGN(len, L1_CACHE_BYTES); 1218 } 1219 1220 static int add_recvbuf_mergeable(struct virtnet_info *vi, 1221 struct receive_queue *rq, gfp_t gfp) 1222 { 1223 struct page_frag *alloc_frag = &rq->alloc_frag; 1224 unsigned int headroom = virtnet_get_headroom(vi); 1225 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 1226 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom); 1227 char *buf; 1228 void *ctx; 1229 int err; 1230 unsigned int len, hole; 1231 1232 /* Extra tailroom is needed to satisfy XDP's assumption. This 1233 * means rx frags coalescing won't work, but consider we've 1234 * disabled GSO for XDP, it won't be a big issue. 1235 */ 1236 len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room); 1237 if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp))) 1238 return -ENOMEM; 1239 1240 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 1241 buf += headroom; /* advance address leaving hole at front of pkt */ 1242 get_page(alloc_frag->page); 1243 alloc_frag->offset += len + room; 1244 hole = alloc_frag->size - alloc_frag->offset; 1245 if (hole < len + room) { 1246 /* To avoid internal fragmentation, if there is very likely not 1247 * enough space for another buffer, add the remaining space to 1248 * the current buffer. 1249 */ 1250 len += hole; 1251 alloc_frag->offset += hole; 1252 } 1253 1254 sg_init_one(rq->sg, buf, len); 1255 ctx = mergeable_len_to_ctx(len, headroom); 1256 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp); 1257 if (err < 0) 1258 put_page(virt_to_head_page(buf)); 1259 1260 return err; 1261 } 1262 1263 /* 1264 * Returns false if we couldn't fill entirely (OOM). 1265 * 1266 * Normally run in the receive path, but can also be run from ndo_open 1267 * before we're receiving packets, or from refill_work which is 1268 * careful to disable receiving (using napi_disable). 1269 */ 1270 static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq, 1271 gfp_t gfp) 1272 { 1273 int err; 1274 bool oom; 1275 1276 do { 1277 if (vi->mergeable_rx_bufs) 1278 err = add_recvbuf_mergeable(vi, rq, gfp); 1279 else if (vi->big_packets) 1280 err = add_recvbuf_big(vi, rq, gfp); 1281 else 1282 err = add_recvbuf_small(vi, rq, gfp); 1283 1284 oom = err == -ENOMEM; 1285 if (err) 1286 break; 1287 } while (rq->vq->num_free); 1288 if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) { 1289 unsigned long flags; 1290 1291 flags = u64_stats_update_begin_irqsave(&rq->stats.syncp); 1292 rq->stats.kicks++; 1293 u64_stats_update_end_irqrestore(&rq->stats.syncp, flags); 1294 } 1295 1296 return !oom; 1297 } 1298 1299 static void skb_recv_done(struct virtqueue *rvq) 1300 { 1301 struct virtnet_info *vi = rvq->vdev->priv; 1302 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)]; 1303 1304 virtqueue_napi_schedule(&rq->napi, rvq); 1305 } 1306 1307 static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi) 1308 { 1309 napi_enable(napi); 1310 1311 /* If all buffers were filled by other side before we napi_enabled, we 1312 * won't get another interrupt, so process any outstanding packets now. 1313 * Call local_bh_enable after to trigger softIRQ processing. 1314 */ 1315 local_bh_disable(); 1316 virtqueue_napi_schedule(napi, vq); 1317 local_bh_enable(); 1318 } 1319 1320 static void virtnet_napi_tx_enable(struct virtnet_info *vi, 1321 struct virtqueue *vq, 1322 struct napi_struct *napi) 1323 { 1324 if (!napi->weight) 1325 return; 1326 1327 /* Tx napi touches cachelines on the cpu handling tx interrupts. Only 1328 * enable the feature if this is likely affine with the transmit path. 1329 */ 1330 if (!vi->affinity_hint_set) { 1331 napi->weight = 0; 1332 return; 1333 } 1334 1335 return virtnet_napi_enable(vq, napi); 1336 } 1337 1338 static void virtnet_napi_tx_disable(struct napi_struct *napi) 1339 { 1340 if (napi->weight) 1341 napi_disable(napi); 1342 } 1343 1344 static void refill_work(struct work_struct *work) 1345 { 1346 struct virtnet_info *vi = 1347 container_of(work, struct virtnet_info, refill.work); 1348 bool still_empty; 1349 int i; 1350 1351 for (i = 0; i < vi->curr_queue_pairs; i++) { 1352 struct receive_queue *rq = &vi->rq[i]; 1353 1354 napi_disable(&rq->napi); 1355 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL); 1356 virtnet_napi_enable(rq->vq, &rq->napi); 1357 1358 /* In theory, this can happen: if we don't get any buffers in 1359 * we will *never* try to fill again. 1360 */ 1361 if (still_empty) 1362 schedule_delayed_work(&vi->refill, HZ/2); 1363 } 1364 } 1365 1366 static int virtnet_receive(struct receive_queue *rq, int budget, 1367 unsigned int *xdp_xmit) 1368 { 1369 struct virtnet_info *vi = rq->vq->vdev->priv; 1370 struct virtnet_rq_stats stats = {}; 1371 unsigned int len; 1372 void *buf; 1373 int i; 1374 1375 if (!vi->big_packets || vi->mergeable_rx_bufs) { 1376 void *ctx; 1377 1378 while (stats.packets < budget && 1379 (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) { 1380 receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats); 1381 stats.packets++; 1382 } 1383 } else { 1384 while (stats.packets < budget && 1385 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) { 1386 receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats); 1387 stats.packets++; 1388 } 1389 } 1390 1391 if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) { 1392 if (!try_fill_recv(vi, rq, GFP_ATOMIC)) 1393 schedule_delayed_work(&vi->refill, 0); 1394 } 1395 1396 u64_stats_update_begin(&rq->stats.syncp); 1397 for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) { 1398 size_t offset = virtnet_rq_stats_desc[i].offset; 1399 u64 *item; 1400 1401 item = (u64 *)((u8 *)&rq->stats + offset); 1402 *item += *(u64 *)((u8 *)&stats + offset); 1403 } 1404 u64_stats_update_end(&rq->stats.syncp); 1405 1406 return stats.packets; 1407 } 1408 1409 static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi) 1410 { 1411 unsigned int len; 1412 unsigned int packets = 0; 1413 unsigned int bytes = 0; 1414 void *ptr; 1415 1416 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) { 1417 if (likely(!is_xdp_frame(ptr))) { 1418 struct sk_buff *skb = ptr; 1419 1420 pr_debug("Sent skb %p\n", skb); 1421 1422 bytes += skb->len; 1423 napi_consume_skb(skb, in_napi); 1424 } else { 1425 struct xdp_frame *frame = ptr_to_xdp(ptr); 1426 1427 bytes += frame->len; 1428 xdp_return_frame(frame); 1429 } 1430 packets++; 1431 } 1432 1433 /* Avoid overhead when no packets have been processed 1434 * happens when called speculatively from start_xmit. 1435 */ 1436 if (!packets) 1437 return; 1438 1439 u64_stats_update_begin(&sq->stats.syncp); 1440 sq->stats.bytes += bytes; 1441 sq->stats.packets += packets; 1442 u64_stats_update_end(&sq->stats.syncp); 1443 } 1444 1445 static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q) 1446 { 1447 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs)) 1448 return false; 1449 else if (q < vi->curr_queue_pairs) 1450 return true; 1451 else 1452 return false; 1453 } 1454 1455 static void virtnet_poll_cleantx(struct receive_queue *rq) 1456 { 1457 struct virtnet_info *vi = rq->vq->vdev->priv; 1458 unsigned int index = vq2rxq(rq->vq); 1459 struct send_queue *sq = &vi->sq[index]; 1460 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index); 1461 1462 if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index)) 1463 return; 1464 1465 if (__netif_tx_trylock(txq)) { 1466 free_old_xmit_skbs(sq, true); 1467 __netif_tx_unlock(txq); 1468 } 1469 1470 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) 1471 netif_tx_wake_queue(txq); 1472 } 1473 1474 static int virtnet_poll(struct napi_struct *napi, int budget) 1475 { 1476 struct receive_queue *rq = 1477 container_of(napi, struct receive_queue, napi); 1478 struct virtnet_info *vi = rq->vq->vdev->priv; 1479 struct send_queue *sq; 1480 unsigned int received; 1481 unsigned int xdp_xmit = 0; 1482 1483 virtnet_poll_cleantx(rq); 1484 1485 received = virtnet_receive(rq, budget, &xdp_xmit); 1486 1487 /* Out of packets? */ 1488 if (received < budget) 1489 virtqueue_napi_complete(napi, rq->vq, received); 1490 1491 if (xdp_xmit & VIRTIO_XDP_REDIR) 1492 xdp_do_flush(); 1493 1494 if (xdp_xmit & VIRTIO_XDP_TX) { 1495 sq = virtnet_xdp_get_sq(vi); 1496 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) { 1497 u64_stats_update_begin(&sq->stats.syncp); 1498 sq->stats.kicks++; 1499 u64_stats_update_end(&sq->stats.syncp); 1500 } 1501 virtnet_xdp_put_sq(vi, sq); 1502 } 1503 1504 return received; 1505 } 1506 1507 static int virtnet_open(struct net_device *dev) 1508 { 1509 struct virtnet_info *vi = netdev_priv(dev); 1510 int i, err; 1511 1512 for (i = 0; i < vi->max_queue_pairs; i++) { 1513 if (i < vi->curr_queue_pairs) 1514 /* Make sure we have some buffers: if oom use wq. */ 1515 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 1516 schedule_delayed_work(&vi->refill, 0); 1517 1518 err = xdp_rxq_info_reg(&vi->rq[i].xdp_rxq, dev, i, vi->rq[i].napi.napi_id); 1519 if (err < 0) 1520 return err; 1521 1522 err = xdp_rxq_info_reg_mem_model(&vi->rq[i].xdp_rxq, 1523 MEM_TYPE_PAGE_SHARED, NULL); 1524 if (err < 0) { 1525 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq); 1526 return err; 1527 } 1528 1529 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 1530 virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi); 1531 } 1532 1533 return 0; 1534 } 1535 1536 static int virtnet_poll_tx(struct napi_struct *napi, int budget) 1537 { 1538 struct send_queue *sq = container_of(napi, struct send_queue, napi); 1539 struct virtnet_info *vi = sq->vq->vdev->priv; 1540 unsigned int index = vq2txq(sq->vq); 1541 struct netdev_queue *txq; 1542 1543 if (unlikely(is_xdp_raw_buffer_queue(vi, index))) { 1544 /* We don't need to enable cb for XDP */ 1545 napi_complete_done(napi, 0); 1546 return 0; 1547 } 1548 1549 txq = netdev_get_tx_queue(vi->dev, index); 1550 __netif_tx_lock(txq, raw_smp_processor_id()); 1551 free_old_xmit_skbs(sq, true); 1552 __netif_tx_unlock(txq); 1553 1554 virtqueue_napi_complete(napi, sq->vq, 0); 1555 1556 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) 1557 netif_tx_wake_queue(txq); 1558 1559 return 0; 1560 } 1561 1562 static int xmit_skb(struct send_queue *sq, struct sk_buff *skb) 1563 { 1564 struct virtio_net_hdr_mrg_rxbuf *hdr; 1565 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; 1566 struct virtnet_info *vi = sq->vq->vdev->priv; 1567 int num_sg; 1568 unsigned hdr_len = vi->hdr_len; 1569 bool can_push; 1570 1571 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); 1572 1573 can_push = vi->any_header_sg && 1574 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) && 1575 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len; 1576 /* Even if we can, don't push here yet as this would skew 1577 * csum_start offset below. */ 1578 if (can_push) 1579 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len); 1580 else 1581 hdr = skb_vnet_hdr(skb); 1582 1583 if (virtio_net_hdr_from_skb(skb, &hdr->hdr, 1584 virtio_is_little_endian(vi->vdev), false, 1585 0)) 1586 BUG(); 1587 1588 if (vi->mergeable_rx_bufs) 1589 hdr->num_buffers = 0; 1590 1591 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2)); 1592 if (can_push) { 1593 __skb_push(skb, hdr_len); 1594 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len); 1595 if (unlikely(num_sg < 0)) 1596 return num_sg; 1597 /* Pull header back to avoid skew in tx bytes calculations. */ 1598 __skb_pull(skb, hdr_len); 1599 } else { 1600 sg_set_buf(sq->sg, hdr, hdr_len); 1601 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len); 1602 if (unlikely(num_sg < 0)) 1603 return num_sg; 1604 num_sg++; 1605 } 1606 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC); 1607 } 1608 1609 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) 1610 { 1611 struct virtnet_info *vi = netdev_priv(dev); 1612 int qnum = skb_get_queue_mapping(skb); 1613 struct send_queue *sq = &vi->sq[qnum]; 1614 int err; 1615 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum); 1616 bool kick = !netdev_xmit_more(); 1617 bool use_napi = sq->napi.weight; 1618 1619 /* Free up any pending old buffers before queueing new ones. */ 1620 free_old_xmit_skbs(sq, false); 1621 1622 if (use_napi && kick) 1623 virtqueue_enable_cb_delayed(sq->vq); 1624 1625 /* timestamp packet in software */ 1626 skb_tx_timestamp(skb); 1627 1628 /* Try to transmit */ 1629 err = xmit_skb(sq, skb); 1630 1631 /* This should not happen! */ 1632 if (unlikely(err)) { 1633 dev->stats.tx_fifo_errors++; 1634 if (net_ratelimit()) 1635 dev_warn(&dev->dev, 1636 "Unexpected TXQ (%d) queue failure: %d\n", 1637 qnum, err); 1638 dev->stats.tx_dropped++; 1639 dev_kfree_skb_any(skb); 1640 return NETDEV_TX_OK; 1641 } 1642 1643 /* Don't wait up for transmitted skbs to be freed. */ 1644 if (!use_napi) { 1645 skb_orphan(skb); 1646 nf_reset_ct(skb); 1647 } 1648 1649 /* If running out of space, stop queue to avoid getting packets that we 1650 * are then unable to transmit. 1651 * An alternative would be to force queuing layer to requeue the skb by 1652 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be 1653 * returned in a normal path of operation: it means that driver is not 1654 * maintaining the TX queue stop/start state properly, and causes 1655 * the stack to do a non-trivial amount of useless work. 1656 * Since most packets only take 1 or 2 ring slots, stopping the queue 1657 * early means 16 slots are typically wasted. 1658 */ 1659 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) { 1660 netif_stop_subqueue(dev, qnum); 1661 if (!use_napi && 1662 unlikely(!virtqueue_enable_cb_delayed(sq->vq))) { 1663 /* More just got used, free them then recheck. */ 1664 free_old_xmit_skbs(sq, false); 1665 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) { 1666 netif_start_subqueue(dev, qnum); 1667 virtqueue_disable_cb(sq->vq); 1668 } 1669 } 1670 } 1671 1672 if (kick || netif_xmit_stopped(txq)) { 1673 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) { 1674 u64_stats_update_begin(&sq->stats.syncp); 1675 sq->stats.kicks++; 1676 u64_stats_update_end(&sq->stats.syncp); 1677 } 1678 } 1679 1680 return NETDEV_TX_OK; 1681 } 1682 1683 /* 1684 * Send command via the control virtqueue and check status. Commands 1685 * supported by the hypervisor, as indicated by feature bits, should 1686 * never fail unless improperly formatted. 1687 */ 1688 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, 1689 struct scatterlist *out) 1690 { 1691 struct scatterlist *sgs[4], hdr, stat; 1692 unsigned out_num = 0, tmp; 1693 1694 /* Caller should know better */ 1695 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)); 1696 1697 vi->ctrl->status = ~0; 1698 vi->ctrl->hdr.class = class; 1699 vi->ctrl->hdr.cmd = cmd; 1700 /* Add header */ 1701 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr)); 1702 sgs[out_num++] = &hdr; 1703 1704 if (out) 1705 sgs[out_num++] = out; 1706 1707 /* Add return status. */ 1708 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status)); 1709 sgs[out_num] = &stat; 1710 1711 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs)); 1712 virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC); 1713 1714 if (unlikely(!virtqueue_kick(vi->cvq))) 1715 return vi->ctrl->status == VIRTIO_NET_OK; 1716 1717 /* Spin for a response, the kick causes an ioport write, trapping 1718 * into the hypervisor, so the request should be handled immediately. 1719 */ 1720 while (!virtqueue_get_buf(vi->cvq, &tmp) && 1721 !virtqueue_is_broken(vi->cvq)) 1722 cpu_relax(); 1723 1724 return vi->ctrl->status == VIRTIO_NET_OK; 1725 } 1726 1727 static int virtnet_set_mac_address(struct net_device *dev, void *p) 1728 { 1729 struct virtnet_info *vi = netdev_priv(dev); 1730 struct virtio_device *vdev = vi->vdev; 1731 int ret; 1732 struct sockaddr *addr; 1733 struct scatterlist sg; 1734 1735 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY)) 1736 return -EOPNOTSUPP; 1737 1738 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL); 1739 if (!addr) 1740 return -ENOMEM; 1741 1742 ret = eth_prepare_mac_addr_change(dev, addr); 1743 if (ret) 1744 goto out; 1745 1746 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 1747 sg_init_one(&sg, addr->sa_data, dev->addr_len); 1748 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 1749 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) { 1750 dev_warn(&vdev->dev, 1751 "Failed to set mac address by vq command.\n"); 1752 ret = -EINVAL; 1753 goto out; 1754 } 1755 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) && 1756 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) { 1757 unsigned int i; 1758 1759 /* Naturally, this has an atomicity problem. */ 1760 for (i = 0; i < dev->addr_len; i++) 1761 virtio_cwrite8(vdev, 1762 offsetof(struct virtio_net_config, mac) + 1763 i, addr->sa_data[i]); 1764 } 1765 1766 eth_commit_mac_addr_change(dev, p); 1767 ret = 0; 1768 1769 out: 1770 kfree(addr); 1771 return ret; 1772 } 1773 1774 static void virtnet_stats(struct net_device *dev, 1775 struct rtnl_link_stats64 *tot) 1776 { 1777 struct virtnet_info *vi = netdev_priv(dev); 1778 unsigned int start; 1779 int i; 1780 1781 for (i = 0; i < vi->max_queue_pairs; i++) { 1782 u64 tpackets, tbytes, rpackets, rbytes, rdrops; 1783 struct receive_queue *rq = &vi->rq[i]; 1784 struct send_queue *sq = &vi->sq[i]; 1785 1786 do { 1787 start = u64_stats_fetch_begin_irq(&sq->stats.syncp); 1788 tpackets = sq->stats.packets; 1789 tbytes = sq->stats.bytes; 1790 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start)); 1791 1792 do { 1793 start = u64_stats_fetch_begin_irq(&rq->stats.syncp); 1794 rpackets = rq->stats.packets; 1795 rbytes = rq->stats.bytes; 1796 rdrops = rq->stats.drops; 1797 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start)); 1798 1799 tot->rx_packets += rpackets; 1800 tot->tx_packets += tpackets; 1801 tot->rx_bytes += rbytes; 1802 tot->tx_bytes += tbytes; 1803 tot->rx_dropped += rdrops; 1804 } 1805 1806 tot->tx_dropped = dev->stats.tx_dropped; 1807 tot->tx_fifo_errors = dev->stats.tx_fifo_errors; 1808 tot->rx_length_errors = dev->stats.rx_length_errors; 1809 tot->rx_frame_errors = dev->stats.rx_frame_errors; 1810 } 1811 1812 static void virtnet_ack_link_announce(struct virtnet_info *vi) 1813 { 1814 rtnl_lock(); 1815 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE, 1816 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL)) 1817 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n"); 1818 rtnl_unlock(); 1819 } 1820 1821 static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 1822 { 1823 struct scatterlist sg; 1824 struct net_device *dev = vi->dev; 1825 1826 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ)) 1827 return 0; 1828 1829 vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs); 1830 sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq)); 1831 1832 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, 1833 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) { 1834 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n", 1835 queue_pairs); 1836 return -EINVAL; 1837 } else { 1838 vi->curr_queue_pairs = queue_pairs; 1839 /* virtnet_open() will refill when device is going to up. */ 1840 if (dev->flags & IFF_UP) 1841 schedule_delayed_work(&vi->refill, 0); 1842 } 1843 1844 return 0; 1845 } 1846 1847 static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 1848 { 1849 int err; 1850 1851 rtnl_lock(); 1852 err = _virtnet_set_queues(vi, queue_pairs); 1853 rtnl_unlock(); 1854 return err; 1855 } 1856 1857 static int virtnet_close(struct net_device *dev) 1858 { 1859 struct virtnet_info *vi = netdev_priv(dev); 1860 int i; 1861 1862 /* Make sure refill_work doesn't re-enable napi! */ 1863 cancel_delayed_work_sync(&vi->refill); 1864 1865 for (i = 0; i < vi->max_queue_pairs; i++) { 1866 xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq); 1867 napi_disable(&vi->rq[i].napi); 1868 virtnet_napi_tx_disable(&vi->sq[i].napi); 1869 } 1870 1871 return 0; 1872 } 1873 1874 static void virtnet_set_rx_mode(struct net_device *dev) 1875 { 1876 struct virtnet_info *vi = netdev_priv(dev); 1877 struct scatterlist sg[2]; 1878 struct virtio_net_ctrl_mac *mac_data; 1879 struct netdev_hw_addr *ha; 1880 int uc_count; 1881 int mc_count; 1882 void *buf; 1883 int i; 1884 1885 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */ 1886 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) 1887 return; 1888 1889 vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0); 1890 vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0); 1891 1892 sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc)); 1893 1894 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1895 VIRTIO_NET_CTRL_RX_PROMISC, sg)) 1896 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", 1897 vi->ctrl->promisc ? "en" : "dis"); 1898 1899 sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti)); 1900 1901 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1902 VIRTIO_NET_CTRL_RX_ALLMULTI, sg)) 1903 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", 1904 vi->ctrl->allmulti ? "en" : "dis"); 1905 1906 uc_count = netdev_uc_count(dev); 1907 mc_count = netdev_mc_count(dev); 1908 /* MAC filter - use one buffer for both lists */ 1909 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) + 1910 (2 * sizeof(mac_data->entries)), GFP_ATOMIC); 1911 mac_data = buf; 1912 if (!buf) 1913 return; 1914 1915 sg_init_table(sg, 2); 1916 1917 /* Store the unicast list and count in the front of the buffer */ 1918 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count); 1919 i = 0; 1920 netdev_for_each_uc_addr(ha, dev) 1921 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 1922 1923 sg_set_buf(&sg[0], mac_data, 1924 sizeof(mac_data->entries) + (uc_count * ETH_ALEN)); 1925 1926 /* multicast list and count fill the end */ 1927 mac_data = (void *)&mac_data->macs[uc_count][0]; 1928 1929 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count); 1930 i = 0; 1931 netdev_for_each_mc_addr(ha, dev) 1932 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 1933 1934 sg_set_buf(&sg[1], mac_data, 1935 sizeof(mac_data->entries) + (mc_count * ETH_ALEN)); 1936 1937 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 1938 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg)) 1939 dev_warn(&dev->dev, "Failed to set MAC filter table.\n"); 1940 1941 kfree(buf); 1942 } 1943 1944 static int virtnet_vlan_rx_add_vid(struct net_device *dev, 1945 __be16 proto, u16 vid) 1946 { 1947 struct virtnet_info *vi = netdev_priv(dev); 1948 struct scatterlist sg; 1949 1950 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid); 1951 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid)); 1952 1953 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 1954 VIRTIO_NET_CTRL_VLAN_ADD, &sg)) 1955 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); 1956 return 0; 1957 } 1958 1959 static int virtnet_vlan_rx_kill_vid(struct net_device *dev, 1960 __be16 proto, u16 vid) 1961 { 1962 struct virtnet_info *vi = netdev_priv(dev); 1963 struct scatterlist sg; 1964 1965 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid); 1966 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid)); 1967 1968 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 1969 VIRTIO_NET_CTRL_VLAN_DEL, &sg)) 1970 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); 1971 return 0; 1972 } 1973 1974 static void virtnet_clean_affinity(struct virtnet_info *vi) 1975 { 1976 int i; 1977 1978 if (vi->affinity_hint_set) { 1979 for (i = 0; i < vi->max_queue_pairs; i++) { 1980 virtqueue_set_affinity(vi->rq[i].vq, NULL); 1981 virtqueue_set_affinity(vi->sq[i].vq, NULL); 1982 } 1983 1984 vi->affinity_hint_set = false; 1985 } 1986 } 1987 1988 static void virtnet_set_affinity(struct virtnet_info *vi) 1989 { 1990 cpumask_var_t mask; 1991 int stragglers; 1992 int group_size; 1993 int i, j, cpu; 1994 int num_cpu; 1995 int stride; 1996 1997 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) { 1998 virtnet_clean_affinity(vi); 1999 return; 2000 } 2001 2002 num_cpu = num_online_cpus(); 2003 stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1); 2004 stragglers = num_cpu >= vi->curr_queue_pairs ? 2005 num_cpu % vi->curr_queue_pairs : 2006 0; 2007 cpu = cpumask_next(-1, cpu_online_mask); 2008 2009 for (i = 0; i < vi->curr_queue_pairs; i++) { 2010 group_size = stride + (i < stragglers ? 1 : 0); 2011 2012 for (j = 0; j < group_size; j++) { 2013 cpumask_set_cpu(cpu, mask); 2014 cpu = cpumask_next_wrap(cpu, cpu_online_mask, 2015 nr_cpu_ids, false); 2016 } 2017 virtqueue_set_affinity(vi->rq[i].vq, mask); 2018 virtqueue_set_affinity(vi->sq[i].vq, mask); 2019 __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS); 2020 cpumask_clear(mask); 2021 } 2022 2023 vi->affinity_hint_set = true; 2024 free_cpumask_var(mask); 2025 } 2026 2027 static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node) 2028 { 2029 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 2030 node); 2031 virtnet_set_affinity(vi); 2032 return 0; 2033 } 2034 2035 static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node) 2036 { 2037 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 2038 node_dead); 2039 virtnet_set_affinity(vi); 2040 return 0; 2041 } 2042 2043 static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node) 2044 { 2045 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 2046 node); 2047 2048 virtnet_clean_affinity(vi); 2049 return 0; 2050 } 2051 2052 static enum cpuhp_state virtionet_online; 2053 2054 static int virtnet_cpu_notif_add(struct virtnet_info *vi) 2055 { 2056 int ret; 2057 2058 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node); 2059 if (ret) 2060 return ret; 2061 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD, 2062 &vi->node_dead); 2063 if (!ret) 2064 return ret; 2065 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 2066 return ret; 2067 } 2068 2069 static void virtnet_cpu_notif_remove(struct virtnet_info *vi) 2070 { 2071 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 2072 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD, 2073 &vi->node_dead); 2074 } 2075 2076 static void virtnet_get_ringparam(struct net_device *dev, 2077 struct ethtool_ringparam *ring) 2078 { 2079 struct virtnet_info *vi = netdev_priv(dev); 2080 2081 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq); 2082 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq); 2083 ring->rx_pending = ring->rx_max_pending; 2084 ring->tx_pending = ring->tx_max_pending; 2085 } 2086 2087 2088 static void virtnet_get_drvinfo(struct net_device *dev, 2089 struct ethtool_drvinfo *info) 2090 { 2091 struct virtnet_info *vi = netdev_priv(dev); 2092 struct virtio_device *vdev = vi->vdev; 2093 2094 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); 2095 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version)); 2096 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info)); 2097 2098 } 2099 2100 /* TODO: Eliminate OOO packets during switching */ 2101 static int virtnet_set_channels(struct net_device *dev, 2102 struct ethtool_channels *channels) 2103 { 2104 struct virtnet_info *vi = netdev_priv(dev); 2105 u16 queue_pairs = channels->combined_count; 2106 int err; 2107 2108 /* We don't support separate rx/tx channels. 2109 * We don't allow setting 'other' channels. 2110 */ 2111 if (channels->rx_count || channels->tx_count || channels->other_count) 2112 return -EINVAL; 2113 2114 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0) 2115 return -EINVAL; 2116 2117 /* For now we don't support modifying channels while XDP is loaded 2118 * also when XDP is loaded all RX queues have XDP programs so we only 2119 * need to check a single RX queue. 2120 */ 2121 if (vi->rq[0].xdp_prog) 2122 return -EINVAL; 2123 2124 get_online_cpus(); 2125 err = _virtnet_set_queues(vi, queue_pairs); 2126 if (err) { 2127 put_online_cpus(); 2128 goto err; 2129 } 2130 virtnet_set_affinity(vi); 2131 put_online_cpus(); 2132 2133 netif_set_real_num_tx_queues(dev, queue_pairs); 2134 netif_set_real_num_rx_queues(dev, queue_pairs); 2135 err: 2136 return err; 2137 } 2138 2139 static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data) 2140 { 2141 struct virtnet_info *vi = netdev_priv(dev); 2142 unsigned int i, j; 2143 u8 *p = data; 2144 2145 switch (stringset) { 2146 case ETH_SS_STATS: 2147 for (i = 0; i < vi->curr_queue_pairs; i++) { 2148 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) 2149 ethtool_sprintf(&p, "rx_queue_%u_%s", i, 2150 virtnet_rq_stats_desc[j].desc); 2151 } 2152 2153 for (i = 0; i < vi->curr_queue_pairs; i++) { 2154 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) 2155 ethtool_sprintf(&p, "tx_queue_%u_%s", i, 2156 virtnet_sq_stats_desc[j].desc); 2157 } 2158 break; 2159 } 2160 } 2161 2162 static int virtnet_get_sset_count(struct net_device *dev, int sset) 2163 { 2164 struct virtnet_info *vi = netdev_priv(dev); 2165 2166 switch (sset) { 2167 case ETH_SS_STATS: 2168 return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN + 2169 VIRTNET_SQ_STATS_LEN); 2170 default: 2171 return -EOPNOTSUPP; 2172 } 2173 } 2174 2175 static void virtnet_get_ethtool_stats(struct net_device *dev, 2176 struct ethtool_stats *stats, u64 *data) 2177 { 2178 struct virtnet_info *vi = netdev_priv(dev); 2179 unsigned int idx = 0, start, i, j; 2180 const u8 *stats_base; 2181 size_t offset; 2182 2183 for (i = 0; i < vi->curr_queue_pairs; i++) { 2184 struct receive_queue *rq = &vi->rq[i]; 2185 2186 stats_base = (u8 *)&rq->stats; 2187 do { 2188 start = u64_stats_fetch_begin_irq(&rq->stats.syncp); 2189 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) { 2190 offset = virtnet_rq_stats_desc[j].offset; 2191 data[idx + j] = *(u64 *)(stats_base + offset); 2192 } 2193 } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start)); 2194 idx += VIRTNET_RQ_STATS_LEN; 2195 } 2196 2197 for (i = 0; i < vi->curr_queue_pairs; i++) { 2198 struct send_queue *sq = &vi->sq[i]; 2199 2200 stats_base = (u8 *)&sq->stats; 2201 do { 2202 start = u64_stats_fetch_begin_irq(&sq->stats.syncp); 2203 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) { 2204 offset = virtnet_sq_stats_desc[j].offset; 2205 data[idx + j] = *(u64 *)(stats_base + offset); 2206 } 2207 } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start)); 2208 idx += VIRTNET_SQ_STATS_LEN; 2209 } 2210 } 2211 2212 static void virtnet_get_channels(struct net_device *dev, 2213 struct ethtool_channels *channels) 2214 { 2215 struct virtnet_info *vi = netdev_priv(dev); 2216 2217 channels->combined_count = vi->curr_queue_pairs; 2218 channels->max_combined = vi->max_queue_pairs; 2219 channels->max_other = 0; 2220 channels->rx_count = 0; 2221 channels->tx_count = 0; 2222 channels->other_count = 0; 2223 } 2224 2225 static int virtnet_set_link_ksettings(struct net_device *dev, 2226 const struct ethtool_link_ksettings *cmd) 2227 { 2228 struct virtnet_info *vi = netdev_priv(dev); 2229 2230 return ethtool_virtdev_set_link_ksettings(dev, cmd, 2231 &vi->speed, &vi->duplex); 2232 } 2233 2234 static int virtnet_get_link_ksettings(struct net_device *dev, 2235 struct ethtool_link_ksettings *cmd) 2236 { 2237 struct virtnet_info *vi = netdev_priv(dev); 2238 2239 cmd->base.speed = vi->speed; 2240 cmd->base.duplex = vi->duplex; 2241 cmd->base.port = PORT_OTHER; 2242 2243 return 0; 2244 } 2245 2246 static int virtnet_set_coalesce(struct net_device *dev, 2247 struct ethtool_coalesce *ec) 2248 { 2249 struct virtnet_info *vi = netdev_priv(dev); 2250 int i, napi_weight; 2251 2252 if (ec->tx_max_coalesced_frames > 1 || 2253 ec->rx_max_coalesced_frames != 1) 2254 return -EINVAL; 2255 2256 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0; 2257 if (napi_weight ^ vi->sq[0].napi.weight) { 2258 if (dev->flags & IFF_UP) 2259 return -EBUSY; 2260 for (i = 0; i < vi->max_queue_pairs; i++) 2261 vi->sq[i].napi.weight = napi_weight; 2262 } 2263 2264 return 0; 2265 } 2266 2267 static int virtnet_get_coalesce(struct net_device *dev, 2268 struct ethtool_coalesce *ec) 2269 { 2270 struct ethtool_coalesce ec_default = { 2271 .cmd = ETHTOOL_GCOALESCE, 2272 .rx_max_coalesced_frames = 1, 2273 }; 2274 struct virtnet_info *vi = netdev_priv(dev); 2275 2276 memcpy(ec, &ec_default, sizeof(ec_default)); 2277 2278 if (vi->sq[0].napi.weight) 2279 ec->tx_max_coalesced_frames = 1; 2280 2281 return 0; 2282 } 2283 2284 static void virtnet_init_settings(struct net_device *dev) 2285 { 2286 struct virtnet_info *vi = netdev_priv(dev); 2287 2288 vi->speed = SPEED_UNKNOWN; 2289 vi->duplex = DUPLEX_UNKNOWN; 2290 } 2291 2292 static void virtnet_update_settings(struct virtnet_info *vi) 2293 { 2294 u32 speed; 2295 u8 duplex; 2296 2297 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX)) 2298 return; 2299 2300 virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed); 2301 2302 if (ethtool_validate_speed(speed)) 2303 vi->speed = speed; 2304 2305 virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex); 2306 2307 if (ethtool_validate_duplex(duplex)) 2308 vi->duplex = duplex; 2309 } 2310 2311 static const struct ethtool_ops virtnet_ethtool_ops = { 2312 .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES, 2313 .get_drvinfo = virtnet_get_drvinfo, 2314 .get_link = ethtool_op_get_link, 2315 .get_ringparam = virtnet_get_ringparam, 2316 .get_strings = virtnet_get_strings, 2317 .get_sset_count = virtnet_get_sset_count, 2318 .get_ethtool_stats = virtnet_get_ethtool_stats, 2319 .set_channels = virtnet_set_channels, 2320 .get_channels = virtnet_get_channels, 2321 .get_ts_info = ethtool_op_get_ts_info, 2322 .get_link_ksettings = virtnet_get_link_ksettings, 2323 .set_link_ksettings = virtnet_set_link_ksettings, 2324 .set_coalesce = virtnet_set_coalesce, 2325 .get_coalesce = virtnet_get_coalesce, 2326 }; 2327 2328 static void virtnet_freeze_down(struct virtio_device *vdev) 2329 { 2330 struct virtnet_info *vi = vdev->priv; 2331 int i; 2332 2333 /* Make sure no work handler is accessing the device */ 2334 flush_work(&vi->config_work); 2335 2336 netif_tx_lock_bh(vi->dev); 2337 netif_device_detach(vi->dev); 2338 netif_tx_unlock_bh(vi->dev); 2339 cancel_delayed_work_sync(&vi->refill); 2340 2341 if (netif_running(vi->dev)) { 2342 for (i = 0; i < vi->max_queue_pairs; i++) { 2343 napi_disable(&vi->rq[i].napi); 2344 virtnet_napi_tx_disable(&vi->sq[i].napi); 2345 } 2346 } 2347 } 2348 2349 static int init_vqs(struct virtnet_info *vi); 2350 2351 static int virtnet_restore_up(struct virtio_device *vdev) 2352 { 2353 struct virtnet_info *vi = vdev->priv; 2354 int err, i; 2355 2356 err = init_vqs(vi); 2357 if (err) 2358 return err; 2359 2360 virtio_device_ready(vdev); 2361 2362 if (netif_running(vi->dev)) { 2363 for (i = 0; i < vi->curr_queue_pairs; i++) 2364 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 2365 schedule_delayed_work(&vi->refill, 0); 2366 2367 for (i = 0; i < vi->max_queue_pairs; i++) { 2368 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 2369 virtnet_napi_tx_enable(vi, vi->sq[i].vq, 2370 &vi->sq[i].napi); 2371 } 2372 } 2373 2374 netif_tx_lock_bh(vi->dev); 2375 netif_device_attach(vi->dev); 2376 netif_tx_unlock_bh(vi->dev); 2377 return err; 2378 } 2379 2380 static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads) 2381 { 2382 struct scatterlist sg; 2383 vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads); 2384 2385 sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads)); 2386 2387 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS, 2388 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) { 2389 dev_warn(&vi->dev->dev, "Fail to set guest offload.\n"); 2390 return -EINVAL; 2391 } 2392 2393 return 0; 2394 } 2395 2396 static int virtnet_clear_guest_offloads(struct virtnet_info *vi) 2397 { 2398 u64 offloads = 0; 2399 2400 if (!vi->guest_offloads) 2401 return 0; 2402 2403 return virtnet_set_guest_offloads(vi, offloads); 2404 } 2405 2406 static int virtnet_restore_guest_offloads(struct virtnet_info *vi) 2407 { 2408 u64 offloads = vi->guest_offloads; 2409 2410 if (!vi->guest_offloads) 2411 return 0; 2412 2413 return virtnet_set_guest_offloads(vi, offloads); 2414 } 2415 2416 static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog, 2417 struct netlink_ext_ack *extack) 2418 { 2419 unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr); 2420 struct virtnet_info *vi = netdev_priv(dev); 2421 struct bpf_prog *old_prog; 2422 u16 xdp_qp = 0, curr_qp; 2423 int i, err; 2424 2425 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) 2426 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) || 2427 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) || 2428 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) || 2429 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) || 2430 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))) { 2431 NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing LRO/CSUM, disable LRO/CSUM first"); 2432 return -EOPNOTSUPP; 2433 } 2434 2435 if (vi->mergeable_rx_bufs && !vi->any_header_sg) { 2436 NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required"); 2437 return -EINVAL; 2438 } 2439 2440 if (dev->mtu > max_sz) { 2441 NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP"); 2442 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz); 2443 return -EINVAL; 2444 } 2445 2446 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs; 2447 if (prog) 2448 xdp_qp = nr_cpu_ids; 2449 2450 /* XDP requires extra queues for XDP_TX */ 2451 if (curr_qp + xdp_qp > vi->max_queue_pairs) { 2452 netdev_warn(dev, "XDP request %i queues but max is %i. XDP_TX and XDP_REDIRECT will operate in a slower locked tx mode.\n", 2453 curr_qp + xdp_qp, vi->max_queue_pairs); 2454 xdp_qp = 0; 2455 } 2456 2457 old_prog = rtnl_dereference(vi->rq[0].xdp_prog); 2458 if (!prog && !old_prog) 2459 return 0; 2460 2461 if (prog) 2462 bpf_prog_add(prog, vi->max_queue_pairs - 1); 2463 2464 /* Make sure NAPI is not using any XDP TX queues for RX. */ 2465 if (netif_running(dev)) { 2466 for (i = 0; i < vi->max_queue_pairs; i++) { 2467 napi_disable(&vi->rq[i].napi); 2468 virtnet_napi_tx_disable(&vi->sq[i].napi); 2469 } 2470 } 2471 2472 if (!prog) { 2473 for (i = 0; i < vi->max_queue_pairs; i++) { 2474 rcu_assign_pointer(vi->rq[i].xdp_prog, prog); 2475 if (i == 0) 2476 virtnet_restore_guest_offloads(vi); 2477 } 2478 synchronize_net(); 2479 } 2480 2481 err = _virtnet_set_queues(vi, curr_qp + xdp_qp); 2482 if (err) 2483 goto err; 2484 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp); 2485 vi->xdp_queue_pairs = xdp_qp; 2486 2487 if (prog) { 2488 vi->xdp_enabled = true; 2489 for (i = 0; i < vi->max_queue_pairs; i++) { 2490 rcu_assign_pointer(vi->rq[i].xdp_prog, prog); 2491 if (i == 0 && !old_prog) 2492 virtnet_clear_guest_offloads(vi); 2493 } 2494 } else { 2495 vi->xdp_enabled = false; 2496 } 2497 2498 for (i = 0; i < vi->max_queue_pairs; i++) { 2499 if (old_prog) 2500 bpf_prog_put(old_prog); 2501 if (netif_running(dev)) { 2502 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 2503 virtnet_napi_tx_enable(vi, vi->sq[i].vq, 2504 &vi->sq[i].napi); 2505 } 2506 } 2507 2508 return 0; 2509 2510 err: 2511 if (!prog) { 2512 virtnet_clear_guest_offloads(vi); 2513 for (i = 0; i < vi->max_queue_pairs; i++) 2514 rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog); 2515 } 2516 2517 if (netif_running(dev)) { 2518 for (i = 0; i < vi->max_queue_pairs; i++) { 2519 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); 2520 virtnet_napi_tx_enable(vi, vi->sq[i].vq, 2521 &vi->sq[i].napi); 2522 } 2523 } 2524 if (prog) 2525 bpf_prog_sub(prog, vi->max_queue_pairs - 1); 2526 return err; 2527 } 2528 2529 static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp) 2530 { 2531 switch (xdp->command) { 2532 case XDP_SETUP_PROG: 2533 return virtnet_xdp_set(dev, xdp->prog, xdp->extack); 2534 default: 2535 return -EINVAL; 2536 } 2537 } 2538 2539 static int virtnet_get_phys_port_name(struct net_device *dev, char *buf, 2540 size_t len) 2541 { 2542 struct virtnet_info *vi = netdev_priv(dev); 2543 int ret; 2544 2545 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY)) 2546 return -EOPNOTSUPP; 2547 2548 ret = snprintf(buf, len, "sby"); 2549 if (ret >= len) 2550 return -EOPNOTSUPP; 2551 2552 return 0; 2553 } 2554 2555 static int virtnet_set_features(struct net_device *dev, 2556 netdev_features_t features) 2557 { 2558 struct virtnet_info *vi = netdev_priv(dev); 2559 u64 offloads; 2560 int err; 2561 2562 if ((dev->features ^ features) & NETIF_F_LRO) { 2563 if (vi->xdp_enabled) 2564 return -EBUSY; 2565 2566 if (features & NETIF_F_LRO) 2567 offloads = vi->guest_offloads_capable; 2568 else 2569 offloads = vi->guest_offloads_capable & 2570 ~GUEST_OFFLOAD_LRO_MASK; 2571 2572 err = virtnet_set_guest_offloads(vi, offloads); 2573 if (err) 2574 return err; 2575 vi->guest_offloads = offloads; 2576 } 2577 2578 return 0; 2579 } 2580 2581 static const struct net_device_ops virtnet_netdev = { 2582 .ndo_open = virtnet_open, 2583 .ndo_stop = virtnet_close, 2584 .ndo_start_xmit = start_xmit, 2585 .ndo_validate_addr = eth_validate_addr, 2586 .ndo_set_mac_address = virtnet_set_mac_address, 2587 .ndo_set_rx_mode = virtnet_set_rx_mode, 2588 .ndo_get_stats64 = virtnet_stats, 2589 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, 2590 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, 2591 .ndo_bpf = virtnet_xdp, 2592 .ndo_xdp_xmit = virtnet_xdp_xmit, 2593 .ndo_features_check = passthru_features_check, 2594 .ndo_get_phys_port_name = virtnet_get_phys_port_name, 2595 .ndo_set_features = virtnet_set_features, 2596 }; 2597 2598 static void virtnet_config_changed_work(struct work_struct *work) 2599 { 2600 struct virtnet_info *vi = 2601 container_of(work, struct virtnet_info, config_work); 2602 u16 v; 2603 2604 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS, 2605 struct virtio_net_config, status, &v) < 0) 2606 return; 2607 2608 if (v & VIRTIO_NET_S_ANNOUNCE) { 2609 netdev_notify_peers(vi->dev); 2610 virtnet_ack_link_announce(vi); 2611 } 2612 2613 /* Ignore unknown (future) status bits */ 2614 v &= VIRTIO_NET_S_LINK_UP; 2615 2616 if (vi->status == v) 2617 return; 2618 2619 vi->status = v; 2620 2621 if (vi->status & VIRTIO_NET_S_LINK_UP) { 2622 virtnet_update_settings(vi); 2623 netif_carrier_on(vi->dev); 2624 netif_tx_wake_all_queues(vi->dev); 2625 } else { 2626 netif_carrier_off(vi->dev); 2627 netif_tx_stop_all_queues(vi->dev); 2628 } 2629 } 2630 2631 static void virtnet_config_changed(struct virtio_device *vdev) 2632 { 2633 struct virtnet_info *vi = vdev->priv; 2634 2635 schedule_work(&vi->config_work); 2636 } 2637 2638 static void virtnet_free_queues(struct virtnet_info *vi) 2639 { 2640 int i; 2641 2642 for (i = 0; i < vi->max_queue_pairs; i++) { 2643 __netif_napi_del(&vi->rq[i].napi); 2644 __netif_napi_del(&vi->sq[i].napi); 2645 } 2646 2647 /* We called __netif_napi_del(), 2648 * we need to respect an RCU grace period before freeing vi->rq 2649 */ 2650 synchronize_net(); 2651 2652 kfree(vi->rq); 2653 kfree(vi->sq); 2654 kfree(vi->ctrl); 2655 } 2656 2657 static void _free_receive_bufs(struct virtnet_info *vi) 2658 { 2659 struct bpf_prog *old_prog; 2660 int i; 2661 2662 for (i = 0; i < vi->max_queue_pairs; i++) { 2663 while (vi->rq[i].pages) 2664 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0); 2665 2666 old_prog = rtnl_dereference(vi->rq[i].xdp_prog); 2667 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL); 2668 if (old_prog) 2669 bpf_prog_put(old_prog); 2670 } 2671 } 2672 2673 static void free_receive_bufs(struct virtnet_info *vi) 2674 { 2675 rtnl_lock(); 2676 _free_receive_bufs(vi); 2677 rtnl_unlock(); 2678 } 2679 2680 static void free_receive_page_frags(struct virtnet_info *vi) 2681 { 2682 int i; 2683 for (i = 0; i < vi->max_queue_pairs; i++) 2684 if (vi->rq[i].alloc_frag.page) 2685 put_page(vi->rq[i].alloc_frag.page); 2686 } 2687 2688 static void free_unused_bufs(struct virtnet_info *vi) 2689 { 2690 void *buf; 2691 int i; 2692 2693 for (i = 0; i < vi->max_queue_pairs; i++) { 2694 struct virtqueue *vq = vi->sq[i].vq; 2695 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { 2696 if (!is_xdp_frame(buf)) 2697 dev_kfree_skb(buf); 2698 else 2699 xdp_return_frame(ptr_to_xdp(buf)); 2700 } 2701 } 2702 2703 for (i = 0; i < vi->max_queue_pairs; i++) { 2704 struct virtqueue *vq = vi->rq[i].vq; 2705 2706 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { 2707 if (vi->mergeable_rx_bufs) { 2708 put_page(virt_to_head_page(buf)); 2709 } else if (vi->big_packets) { 2710 give_pages(&vi->rq[i], buf); 2711 } else { 2712 put_page(virt_to_head_page(buf)); 2713 } 2714 } 2715 } 2716 } 2717 2718 static void virtnet_del_vqs(struct virtnet_info *vi) 2719 { 2720 struct virtio_device *vdev = vi->vdev; 2721 2722 virtnet_clean_affinity(vi); 2723 2724 vdev->config->del_vqs(vdev); 2725 2726 virtnet_free_queues(vi); 2727 } 2728 2729 /* How large should a single buffer be so a queue full of these can fit at 2730 * least one full packet? 2731 * Logic below assumes the mergeable buffer header is used. 2732 */ 2733 static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq) 2734 { 2735 const unsigned int hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 2736 unsigned int rq_size = virtqueue_get_vring_size(vq); 2737 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu; 2738 unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len; 2739 unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size); 2740 2741 return max(max(min_buf_len, hdr_len) - hdr_len, 2742 (unsigned int)GOOD_PACKET_LEN); 2743 } 2744 2745 static int virtnet_find_vqs(struct virtnet_info *vi) 2746 { 2747 vq_callback_t **callbacks; 2748 struct virtqueue **vqs; 2749 int ret = -ENOMEM; 2750 int i, total_vqs; 2751 const char **names; 2752 bool *ctx; 2753 2754 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by 2755 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by 2756 * possible control vq. 2757 */ 2758 total_vqs = vi->max_queue_pairs * 2 + 2759 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ); 2760 2761 /* Allocate space for find_vqs parameters */ 2762 vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL); 2763 if (!vqs) 2764 goto err_vq; 2765 callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL); 2766 if (!callbacks) 2767 goto err_callback; 2768 names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL); 2769 if (!names) 2770 goto err_names; 2771 if (!vi->big_packets || vi->mergeable_rx_bufs) { 2772 ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL); 2773 if (!ctx) 2774 goto err_ctx; 2775 } else { 2776 ctx = NULL; 2777 } 2778 2779 /* Parameters for control virtqueue, if any */ 2780 if (vi->has_cvq) { 2781 callbacks[total_vqs - 1] = NULL; 2782 names[total_vqs - 1] = "control"; 2783 } 2784 2785 /* Allocate/initialize parameters for send/receive virtqueues */ 2786 for (i = 0; i < vi->max_queue_pairs; i++) { 2787 callbacks[rxq2vq(i)] = skb_recv_done; 2788 callbacks[txq2vq(i)] = skb_xmit_done; 2789 sprintf(vi->rq[i].name, "input.%d", i); 2790 sprintf(vi->sq[i].name, "output.%d", i); 2791 names[rxq2vq(i)] = vi->rq[i].name; 2792 names[txq2vq(i)] = vi->sq[i].name; 2793 if (ctx) 2794 ctx[rxq2vq(i)] = true; 2795 } 2796 2797 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks, 2798 names, ctx, NULL); 2799 if (ret) 2800 goto err_find; 2801 2802 if (vi->has_cvq) { 2803 vi->cvq = vqs[total_vqs - 1]; 2804 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) 2805 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 2806 } 2807 2808 for (i = 0; i < vi->max_queue_pairs; i++) { 2809 vi->rq[i].vq = vqs[rxq2vq(i)]; 2810 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq); 2811 vi->sq[i].vq = vqs[txq2vq(i)]; 2812 } 2813 2814 /* run here: ret == 0. */ 2815 2816 2817 err_find: 2818 kfree(ctx); 2819 err_ctx: 2820 kfree(names); 2821 err_names: 2822 kfree(callbacks); 2823 err_callback: 2824 kfree(vqs); 2825 err_vq: 2826 return ret; 2827 } 2828 2829 static int virtnet_alloc_queues(struct virtnet_info *vi) 2830 { 2831 int i; 2832 2833 vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL); 2834 if (!vi->ctrl) 2835 goto err_ctrl; 2836 vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL); 2837 if (!vi->sq) 2838 goto err_sq; 2839 vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL); 2840 if (!vi->rq) 2841 goto err_rq; 2842 2843 INIT_DELAYED_WORK(&vi->refill, refill_work); 2844 for (i = 0; i < vi->max_queue_pairs; i++) { 2845 vi->rq[i].pages = NULL; 2846 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll, 2847 napi_weight); 2848 netif_tx_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx, 2849 napi_tx ? napi_weight : 0); 2850 2851 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg)); 2852 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len); 2853 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg)); 2854 2855 u64_stats_init(&vi->rq[i].stats.syncp); 2856 u64_stats_init(&vi->sq[i].stats.syncp); 2857 } 2858 2859 return 0; 2860 2861 err_rq: 2862 kfree(vi->sq); 2863 err_sq: 2864 kfree(vi->ctrl); 2865 err_ctrl: 2866 return -ENOMEM; 2867 } 2868 2869 static int init_vqs(struct virtnet_info *vi) 2870 { 2871 int ret; 2872 2873 /* Allocate send & receive queues */ 2874 ret = virtnet_alloc_queues(vi); 2875 if (ret) 2876 goto err; 2877 2878 ret = virtnet_find_vqs(vi); 2879 if (ret) 2880 goto err_free; 2881 2882 get_online_cpus(); 2883 virtnet_set_affinity(vi); 2884 put_online_cpus(); 2885 2886 return 0; 2887 2888 err_free: 2889 virtnet_free_queues(vi); 2890 err: 2891 return ret; 2892 } 2893 2894 #ifdef CONFIG_SYSFS 2895 static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue, 2896 char *buf) 2897 { 2898 struct virtnet_info *vi = netdev_priv(queue->dev); 2899 unsigned int queue_index = get_netdev_rx_queue_index(queue); 2900 unsigned int headroom = virtnet_get_headroom(vi); 2901 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0; 2902 struct ewma_pkt_len *avg; 2903 2904 BUG_ON(queue_index >= vi->max_queue_pairs); 2905 avg = &vi->rq[queue_index].mrg_avg_pkt_len; 2906 return sprintf(buf, "%u\n", 2907 get_mergeable_buf_len(&vi->rq[queue_index], avg, 2908 SKB_DATA_ALIGN(headroom + tailroom))); 2909 } 2910 2911 static struct rx_queue_attribute mergeable_rx_buffer_size_attribute = 2912 __ATTR_RO(mergeable_rx_buffer_size); 2913 2914 static struct attribute *virtio_net_mrg_rx_attrs[] = { 2915 &mergeable_rx_buffer_size_attribute.attr, 2916 NULL 2917 }; 2918 2919 static const struct attribute_group virtio_net_mrg_rx_group = { 2920 .name = "virtio_net", 2921 .attrs = virtio_net_mrg_rx_attrs 2922 }; 2923 #endif 2924 2925 static bool virtnet_fail_on_feature(struct virtio_device *vdev, 2926 unsigned int fbit, 2927 const char *fname, const char *dname) 2928 { 2929 if (!virtio_has_feature(vdev, fbit)) 2930 return false; 2931 2932 dev_err(&vdev->dev, "device advertises feature %s but not %s", 2933 fname, dname); 2934 2935 return true; 2936 } 2937 2938 #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \ 2939 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit) 2940 2941 static bool virtnet_validate_features(struct virtio_device *vdev) 2942 { 2943 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) && 2944 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX, 2945 "VIRTIO_NET_F_CTRL_VQ") || 2946 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN, 2947 "VIRTIO_NET_F_CTRL_VQ") || 2948 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE, 2949 "VIRTIO_NET_F_CTRL_VQ") || 2950 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") || 2951 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR, 2952 "VIRTIO_NET_F_CTRL_VQ"))) { 2953 return false; 2954 } 2955 2956 return true; 2957 } 2958 2959 #define MIN_MTU ETH_MIN_MTU 2960 #define MAX_MTU ETH_MAX_MTU 2961 2962 static int virtnet_validate(struct virtio_device *vdev) 2963 { 2964 if (!vdev->config->get) { 2965 dev_err(&vdev->dev, "%s failure: config access disabled\n", 2966 __func__); 2967 return -EINVAL; 2968 } 2969 2970 if (!virtnet_validate_features(vdev)) 2971 return -EINVAL; 2972 2973 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 2974 int mtu = virtio_cread16(vdev, 2975 offsetof(struct virtio_net_config, 2976 mtu)); 2977 if (mtu < MIN_MTU) 2978 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU); 2979 } 2980 2981 return 0; 2982 } 2983 2984 static int virtnet_probe(struct virtio_device *vdev) 2985 { 2986 int i, err = -ENOMEM; 2987 struct net_device *dev; 2988 struct virtnet_info *vi; 2989 u16 max_queue_pairs; 2990 int mtu; 2991 2992 /* Find if host supports multiqueue virtio_net device */ 2993 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ, 2994 struct virtio_net_config, 2995 max_virtqueue_pairs, &max_queue_pairs); 2996 2997 /* We need at least 2 queue's */ 2998 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 2999 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 3000 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 3001 max_queue_pairs = 1; 3002 3003 /* Allocate ourselves a network device with room for our info */ 3004 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs); 3005 if (!dev) 3006 return -ENOMEM; 3007 3008 /* Set up network device as normal. */ 3009 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE | 3010 IFF_TX_SKB_NO_LINEAR; 3011 dev->netdev_ops = &virtnet_netdev; 3012 dev->features = NETIF_F_HIGHDMA; 3013 3014 dev->ethtool_ops = &virtnet_ethtool_ops; 3015 SET_NETDEV_DEV(dev, &vdev->dev); 3016 3017 /* Do we support "hardware" checksums? */ 3018 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { 3019 /* This opens up the world of extra features. */ 3020 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG; 3021 if (csum) 3022 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG; 3023 3024 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { 3025 dev->hw_features |= NETIF_F_TSO 3026 | NETIF_F_TSO_ECN | NETIF_F_TSO6; 3027 } 3028 /* Individual feature bits: what can host handle? */ 3029 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) 3030 dev->hw_features |= NETIF_F_TSO; 3031 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) 3032 dev->hw_features |= NETIF_F_TSO6; 3033 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) 3034 dev->hw_features |= NETIF_F_TSO_ECN; 3035 3036 dev->features |= NETIF_F_GSO_ROBUST; 3037 3038 if (gso) 3039 dev->features |= dev->hw_features & NETIF_F_ALL_TSO; 3040 /* (!csum && gso) case will be fixed by register_netdev() */ 3041 } 3042 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM)) 3043 dev->features |= NETIF_F_RXCSUM; 3044 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || 3045 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)) 3046 dev->features |= NETIF_F_LRO; 3047 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) 3048 dev->hw_features |= NETIF_F_LRO; 3049 3050 dev->vlan_features = dev->features; 3051 3052 /* MTU range: 68 - 65535 */ 3053 dev->min_mtu = MIN_MTU; 3054 dev->max_mtu = MAX_MTU; 3055 3056 /* Configuration may specify what MAC to use. Otherwise random. */ 3057 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) 3058 virtio_cread_bytes(vdev, 3059 offsetof(struct virtio_net_config, mac), 3060 dev->dev_addr, dev->addr_len); 3061 else 3062 eth_hw_addr_random(dev); 3063 3064 /* Set up our device-specific information */ 3065 vi = netdev_priv(dev); 3066 vi->dev = dev; 3067 vi->vdev = vdev; 3068 vdev->priv = vi; 3069 3070 INIT_WORK(&vi->config_work, virtnet_config_changed_work); 3071 3072 /* If we can receive ANY GSO packets, we must allocate large ones. */ 3073 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || 3074 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) || 3075 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) || 3076 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO)) 3077 vi->big_packets = true; 3078 3079 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) 3080 vi->mergeable_rx_bufs = true; 3081 3082 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) || 3083 virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 3084 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 3085 else 3086 vi->hdr_len = sizeof(struct virtio_net_hdr); 3087 3088 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) || 3089 virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 3090 vi->any_header_sg = true; 3091 3092 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 3093 vi->has_cvq = true; 3094 3095 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 3096 mtu = virtio_cread16(vdev, 3097 offsetof(struct virtio_net_config, 3098 mtu)); 3099 if (mtu < dev->min_mtu) { 3100 /* Should never trigger: MTU was previously validated 3101 * in virtnet_validate. 3102 */ 3103 dev_err(&vdev->dev, 3104 "device MTU appears to have changed it is now %d < %d", 3105 mtu, dev->min_mtu); 3106 err = -EINVAL; 3107 goto free; 3108 } 3109 3110 dev->mtu = mtu; 3111 dev->max_mtu = mtu; 3112 3113 /* TODO: size buffers correctly in this case. */ 3114 if (dev->mtu > ETH_DATA_LEN) 3115 vi->big_packets = true; 3116 } 3117 3118 if (vi->any_header_sg) 3119 dev->needed_headroom = vi->hdr_len; 3120 3121 /* Enable multiqueue by default */ 3122 if (num_online_cpus() >= max_queue_pairs) 3123 vi->curr_queue_pairs = max_queue_pairs; 3124 else 3125 vi->curr_queue_pairs = num_online_cpus(); 3126 vi->max_queue_pairs = max_queue_pairs; 3127 3128 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */ 3129 err = init_vqs(vi); 3130 if (err) 3131 goto free; 3132 3133 #ifdef CONFIG_SYSFS 3134 if (vi->mergeable_rx_bufs) 3135 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group; 3136 #endif 3137 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs); 3138 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs); 3139 3140 virtnet_init_settings(dev); 3141 3142 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) { 3143 vi->failover = net_failover_create(vi->dev); 3144 if (IS_ERR(vi->failover)) { 3145 err = PTR_ERR(vi->failover); 3146 goto free_vqs; 3147 } 3148 } 3149 3150 err = register_netdev(dev); 3151 if (err) { 3152 pr_debug("virtio_net: registering device failed\n"); 3153 goto free_failover; 3154 } 3155 3156 virtio_device_ready(vdev); 3157 3158 err = virtnet_cpu_notif_add(vi); 3159 if (err) { 3160 pr_debug("virtio_net: registering cpu notifier failed\n"); 3161 goto free_unregister_netdev; 3162 } 3163 3164 virtnet_set_queues(vi, vi->curr_queue_pairs); 3165 3166 /* Assume link up if device can't report link status, 3167 otherwise get link status from config. */ 3168 netif_carrier_off(dev); 3169 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) { 3170 schedule_work(&vi->config_work); 3171 } else { 3172 vi->status = VIRTIO_NET_S_LINK_UP; 3173 virtnet_update_settings(vi); 3174 netif_carrier_on(dev); 3175 } 3176 3177 for (i = 0; i < ARRAY_SIZE(guest_offloads); i++) 3178 if (virtio_has_feature(vi->vdev, guest_offloads[i])) 3179 set_bit(guest_offloads[i], &vi->guest_offloads); 3180 vi->guest_offloads_capable = vi->guest_offloads; 3181 3182 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n", 3183 dev->name, max_queue_pairs); 3184 3185 return 0; 3186 3187 free_unregister_netdev: 3188 vi->vdev->config->reset(vdev); 3189 3190 unregister_netdev(dev); 3191 free_failover: 3192 net_failover_destroy(vi->failover); 3193 free_vqs: 3194 cancel_delayed_work_sync(&vi->refill); 3195 free_receive_page_frags(vi); 3196 virtnet_del_vqs(vi); 3197 free: 3198 free_netdev(dev); 3199 return err; 3200 } 3201 3202 static void remove_vq_common(struct virtnet_info *vi) 3203 { 3204 vi->vdev->config->reset(vi->vdev); 3205 3206 /* Free unused buffers in both send and recv, if any. */ 3207 free_unused_bufs(vi); 3208 3209 free_receive_bufs(vi); 3210 3211 free_receive_page_frags(vi); 3212 3213 virtnet_del_vqs(vi); 3214 } 3215 3216 static void virtnet_remove(struct virtio_device *vdev) 3217 { 3218 struct virtnet_info *vi = vdev->priv; 3219 3220 virtnet_cpu_notif_remove(vi); 3221 3222 /* Make sure no work handler is accessing the device. */ 3223 flush_work(&vi->config_work); 3224 3225 unregister_netdev(vi->dev); 3226 3227 net_failover_destroy(vi->failover); 3228 3229 remove_vq_common(vi); 3230 3231 free_netdev(vi->dev); 3232 } 3233 3234 static __maybe_unused int virtnet_freeze(struct virtio_device *vdev) 3235 { 3236 struct virtnet_info *vi = vdev->priv; 3237 3238 virtnet_cpu_notif_remove(vi); 3239 virtnet_freeze_down(vdev); 3240 remove_vq_common(vi); 3241 3242 return 0; 3243 } 3244 3245 static __maybe_unused int virtnet_restore(struct virtio_device *vdev) 3246 { 3247 struct virtnet_info *vi = vdev->priv; 3248 int err; 3249 3250 err = virtnet_restore_up(vdev); 3251 if (err) 3252 return err; 3253 virtnet_set_queues(vi, vi->curr_queue_pairs); 3254 3255 err = virtnet_cpu_notif_add(vi); 3256 if (err) 3257 return err; 3258 3259 return 0; 3260 } 3261 3262 static struct virtio_device_id id_table[] = { 3263 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 3264 { 0 }, 3265 }; 3266 3267 #define VIRTNET_FEATURES \ 3268 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \ 3269 VIRTIO_NET_F_MAC, \ 3270 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \ 3271 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \ 3272 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \ 3273 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \ 3274 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \ 3275 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \ 3276 VIRTIO_NET_F_CTRL_MAC_ADDR, \ 3277 VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \ 3278 VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY 3279 3280 static unsigned int features[] = { 3281 VIRTNET_FEATURES, 3282 }; 3283 3284 static unsigned int features_legacy[] = { 3285 VIRTNET_FEATURES, 3286 VIRTIO_NET_F_GSO, 3287 VIRTIO_F_ANY_LAYOUT, 3288 }; 3289 3290 static struct virtio_driver virtio_net_driver = { 3291 .feature_table = features, 3292 .feature_table_size = ARRAY_SIZE(features), 3293 .feature_table_legacy = features_legacy, 3294 .feature_table_size_legacy = ARRAY_SIZE(features_legacy), 3295 .driver.name = KBUILD_MODNAME, 3296 .driver.owner = THIS_MODULE, 3297 .id_table = id_table, 3298 .validate = virtnet_validate, 3299 .probe = virtnet_probe, 3300 .remove = virtnet_remove, 3301 .config_changed = virtnet_config_changed, 3302 #ifdef CONFIG_PM_SLEEP 3303 .freeze = virtnet_freeze, 3304 .restore = virtnet_restore, 3305 #endif 3306 }; 3307 3308 static __init int virtio_net_driver_init(void) 3309 { 3310 int ret; 3311 3312 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online", 3313 virtnet_cpu_online, 3314 virtnet_cpu_down_prep); 3315 if (ret < 0) 3316 goto out; 3317 virtionet_online = ret; 3318 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead", 3319 NULL, virtnet_cpu_dead); 3320 if (ret) 3321 goto err_dead; 3322 3323 ret = register_virtio_driver(&virtio_net_driver); 3324 if (ret) 3325 goto err_virtio; 3326 return 0; 3327 err_virtio: 3328 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 3329 err_dead: 3330 cpuhp_remove_multi_state(virtionet_online); 3331 out: 3332 return ret; 3333 } 3334 module_init(virtio_net_driver_init); 3335 3336 static __exit void virtio_net_driver_exit(void) 3337 { 3338 unregister_virtio_driver(&virtio_net_driver); 3339 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 3340 cpuhp_remove_multi_state(virtionet_online); 3341 } 3342 module_exit(virtio_net_driver_exit); 3343 3344 MODULE_DEVICE_TABLE(virtio, id_table); 3345 MODULE_DESCRIPTION("Virtio network driver"); 3346 MODULE_LICENSE("GPL"); 3347