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