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