1 /* 2 * Virtual network driver for conversing with remote driver backends. 3 * 4 * Copyright (c) 2002-2005, K A Fraser 5 * Copyright (c) 2005, XenSource Ltd 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License version 2 9 * as published by the Free Software Foundation; or, when distributed 10 * separately from the Linux kernel or incorporated into other 11 * software packages, subject to the following license: 12 * 13 * Permission is hereby granted, free of charge, to any person obtaining a copy 14 * of this source file (the "Software"), to deal in the Software without 15 * restriction, including without limitation the rights to use, copy, modify, 16 * merge, publish, distribute, sublicense, and/or sell copies of the Software, 17 * and to permit persons to whom the Software is furnished to do so, subject to 18 * the following conditions: 19 * 20 * The above copyright notice and this permission notice shall be included in 21 * all copies or substantial portions of the Software. 22 * 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 29 * IN THE SOFTWARE. 30 */ 31 32 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 33 34 #include <linux/module.h> 35 #include <linux/kernel.h> 36 #include <linux/netdevice.h> 37 #include <linux/etherdevice.h> 38 #include <linux/skbuff.h> 39 #include <linux/ethtool.h> 40 #include <linux/if_ether.h> 41 #include <net/tcp.h> 42 #include <linux/udp.h> 43 #include <linux/moduleparam.h> 44 #include <linux/mm.h> 45 #include <linux/slab.h> 46 #include <net/ip.h> 47 #include <linux/bpf.h> 48 #include <net/page_pool.h> 49 #include <linux/bpf_trace.h> 50 51 #include <xen/xen.h> 52 #include <xen/xenbus.h> 53 #include <xen/events.h> 54 #include <xen/page.h> 55 #include <xen/platform_pci.h> 56 #include <xen/grant_table.h> 57 58 #include <xen/interface/io/netif.h> 59 #include <xen/interface/memory.h> 60 #include <xen/interface/grant_table.h> 61 62 /* Module parameters */ 63 #define MAX_QUEUES_DEFAULT 8 64 static unsigned int xennet_max_queues; 65 module_param_named(max_queues, xennet_max_queues, uint, 0644); 66 MODULE_PARM_DESC(max_queues, 67 "Maximum number of queues per virtual interface"); 68 69 #define XENNET_TIMEOUT (5 * HZ) 70 71 static const struct ethtool_ops xennet_ethtool_ops; 72 73 struct netfront_cb { 74 int pull_to; 75 }; 76 77 #define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb)) 78 79 #define RX_COPY_THRESHOLD 256 80 81 #define NET_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, XEN_PAGE_SIZE) 82 #define NET_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, XEN_PAGE_SIZE) 83 84 /* Minimum number of Rx slots (includes slot for GSO metadata). */ 85 #define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1) 86 87 /* Queue name is interface name with "-qNNN" appended */ 88 #define QUEUE_NAME_SIZE (IFNAMSIZ + 6) 89 90 /* IRQ name is queue name with "-tx" or "-rx" appended */ 91 #define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3) 92 93 static DECLARE_WAIT_QUEUE_HEAD(module_wq); 94 95 struct netfront_stats { 96 u64 packets; 97 u64 bytes; 98 struct u64_stats_sync syncp; 99 }; 100 101 struct netfront_info; 102 103 struct netfront_queue { 104 unsigned int id; /* Queue ID, 0-based */ 105 char name[QUEUE_NAME_SIZE]; /* DEVNAME-qN */ 106 struct netfront_info *info; 107 108 struct bpf_prog __rcu *xdp_prog; 109 110 struct napi_struct napi; 111 112 /* Split event channels support, tx_* == rx_* when using 113 * single event channel. 114 */ 115 unsigned int tx_evtchn, rx_evtchn; 116 unsigned int tx_irq, rx_irq; 117 /* Only used when split event channels support is enabled */ 118 char tx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-tx */ 119 char rx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-rx */ 120 121 spinlock_t tx_lock; 122 struct xen_netif_tx_front_ring tx; 123 int tx_ring_ref; 124 125 /* 126 * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries 127 * are linked from tx_skb_freelist through tx_link. 128 */ 129 struct sk_buff *tx_skbs[NET_TX_RING_SIZE]; 130 unsigned short tx_link[NET_TX_RING_SIZE]; 131 #define TX_LINK_NONE 0xffff 132 #define TX_PENDING 0xfffe 133 grant_ref_t gref_tx_head; 134 grant_ref_t grant_tx_ref[NET_TX_RING_SIZE]; 135 struct page *grant_tx_page[NET_TX_RING_SIZE]; 136 unsigned tx_skb_freelist; 137 unsigned int tx_pend_queue; 138 139 spinlock_t rx_lock ____cacheline_aligned_in_smp; 140 struct xen_netif_rx_front_ring rx; 141 int rx_ring_ref; 142 143 struct timer_list rx_refill_timer; 144 145 struct sk_buff *rx_skbs[NET_RX_RING_SIZE]; 146 grant_ref_t gref_rx_head; 147 grant_ref_t grant_rx_ref[NET_RX_RING_SIZE]; 148 149 unsigned int rx_rsp_unconsumed; 150 spinlock_t rx_cons_lock; 151 152 struct page_pool *page_pool; 153 struct xdp_rxq_info xdp_rxq; 154 }; 155 156 struct netfront_info { 157 struct list_head list; 158 struct net_device *netdev; 159 160 struct xenbus_device *xbdev; 161 162 /* Multi-queue support */ 163 struct netfront_queue *queues; 164 165 /* Statistics */ 166 struct netfront_stats __percpu *rx_stats; 167 struct netfront_stats __percpu *tx_stats; 168 169 /* XDP state */ 170 bool netback_has_xdp_headroom; 171 bool netfront_xdp_enabled; 172 173 /* Is device behaving sane? */ 174 bool broken; 175 176 atomic_t rx_gso_checksum_fixup; 177 }; 178 179 struct netfront_rx_info { 180 struct xen_netif_rx_response rx; 181 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1]; 182 }; 183 184 /* 185 * Access macros for acquiring freeing slots in tx_skbs[]. 186 */ 187 188 static void add_id_to_list(unsigned *head, unsigned short *list, 189 unsigned short id) 190 { 191 list[id] = *head; 192 *head = id; 193 } 194 195 static unsigned short get_id_from_list(unsigned *head, unsigned short *list) 196 { 197 unsigned int id = *head; 198 199 if (id != TX_LINK_NONE) { 200 *head = list[id]; 201 list[id] = TX_LINK_NONE; 202 } 203 return id; 204 } 205 206 static int xennet_rxidx(RING_IDX idx) 207 { 208 return idx & (NET_RX_RING_SIZE - 1); 209 } 210 211 static struct sk_buff *xennet_get_rx_skb(struct netfront_queue *queue, 212 RING_IDX ri) 213 { 214 int i = xennet_rxidx(ri); 215 struct sk_buff *skb = queue->rx_skbs[i]; 216 queue->rx_skbs[i] = NULL; 217 return skb; 218 } 219 220 static grant_ref_t xennet_get_rx_ref(struct netfront_queue *queue, 221 RING_IDX ri) 222 { 223 int i = xennet_rxidx(ri); 224 grant_ref_t ref = queue->grant_rx_ref[i]; 225 queue->grant_rx_ref[i] = INVALID_GRANT_REF; 226 return ref; 227 } 228 229 #ifdef CONFIG_SYSFS 230 static const struct attribute_group xennet_dev_group; 231 #endif 232 233 static bool xennet_can_sg(struct net_device *dev) 234 { 235 return dev->features & NETIF_F_SG; 236 } 237 238 239 static void rx_refill_timeout(struct timer_list *t) 240 { 241 struct netfront_queue *queue = from_timer(queue, t, rx_refill_timer); 242 napi_schedule(&queue->napi); 243 } 244 245 static int netfront_tx_slot_available(struct netfront_queue *queue) 246 { 247 return (queue->tx.req_prod_pvt - queue->tx.rsp_cons) < 248 (NET_TX_RING_SIZE - XEN_NETIF_NR_SLOTS_MIN - 1); 249 } 250 251 static void xennet_maybe_wake_tx(struct netfront_queue *queue) 252 { 253 struct net_device *dev = queue->info->netdev; 254 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, queue->id); 255 256 if (unlikely(netif_tx_queue_stopped(dev_queue)) && 257 netfront_tx_slot_available(queue) && 258 likely(netif_running(dev))) 259 netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id)); 260 } 261 262 263 static struct sk_buff *xennet_alloc_one_rx_buffer(struct netfront_queue *queue) 264 { 265 struct sk_buff *skb; 266 struct page *page; 267 268 skb = __netdev_alloc_skb(queue->info->netdev, 269 RX_COPY_THRESHOLD + NET_IP_ALIGN, 270 GFP_ATOMIC | __GFP_NOWARN); 271 if (unlikely(!skb)) 272 return NULL; 273 274 page = page_pool_dev_alloc_pages(queue->page_pool); 275 if (unlikely(!page)) { 276 kfree_skb(skb); 277 return NULL; 278 } 279 skb_add_rx_frag(skb, 0, page, 0, 0, PAGE_SIZE); 280 281 /* Align ip header to a 16 bytes boundary */ 282 skb_reserve(skb, NET_IP_ALIGN); 283 skb->dev = queue->info->netdev; 284 285 return skb; 286 } 287 288 289 static void xennet_alloc_rx_buffers(struct netfront_queue *queue) 290 { 291 RING_IDX req_prod = queue->rx.req_prod_pvt; 292 int notify; 293 int err = 0; 294 295 if (unlikely(!netif_carrier_ok(queue->info->netdev))) 296 return; 297 298 for (req_prod = queue->rx.req_prod_pvt; 299 req_prod - queue->rx.rsp_cons < NET_RX_RING_SIZE; 300 req_prod++) { 301 struct sk_buff *skb; 302 unsigned short id; 303 grant_ref_t ref; 304 struct page *page; 305 struct xen_netif_rx_request *req; 306 307 skb = xennet_alloc_one_rx_buffer(queue); 308 if (!skb) { 309 err = -ENOMEM; 310 break; 311 } 312 313 id = xennet_rxidx(req_prod); 314 315 BUG_ON(queue->rx_skbs[id]); 316 queue->rx_skbs[id] = skb; 317 318 ref = gnttab_claim_grant_reference(&queue->gref_rx_head); 319 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref)); 320 queue->grant_rx_ref[id] = ref; 321 322 page = skb_frag_page(&skb_shinfo(skb)->frags[0]); 323 324 req = RING_GET_REQUEST(&queue->rx, req_prod); 325 gnttab_page_grant_foreign_access_ref_one(ref, 326 queue->info->xbdev->otherend_id, 327 page, 328 0); 329 req->id = id; 330 req->gref = ref; 331 } 332 333 queue->rx.req_prod_pvt = req_prod; 334 335 /* Try again later if there are not enough requests or skb allocation 336 * failed. 337 * Enough requests is quantified as the sum of newly created slots and 338 * the unconsumed slots at the backend. 339 */ 340 if (req_prod - queue->rx.rsp_cons < NET_RX_SLOTS_MIN || 341 unlikely(err)) { 342 mod_timer(&queue->rx_refill_timer, jiffies + (HZ/10)); 343 return; 344 } 345 346 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->rx, notify); 347 if (notify) 348 notify_remote_via_irq(queue->rx_irq); 349 } 350 351 static int xennet_open(struct net_device *dev) 352 { 353 struct netfront_info *np = netdev_priv(dev); 354 unsigned int num_queues = dev->real_num_tx_queues; 355 unsigned int i = 0; 356 struct netfront_queue *queue = NULL; 357 358 if (!np->queues || np->broken) 359 return -ENODEV; 360 361 for (i = 0; i < num_queues; ++i) { 362 queue = &np->queues[i]; 363 napi_enable(&queue->napi); 364 365 spin_lock_bh(&queue->rx_lock); 366 if (netif_carrier_ok(dev)) { 367 xennet_alloc_rx_buffers(queue); 368 queue->rx.sring->rsp_event = queue->rx.rsp_cons + 1; 369 if (RING_HAS_UNCONSUMED_RESPONSES(&queue->rx)) 370 napi_schedule(&queue->napi); 371 } 372 spin_unlock_bh(&queue->rx_lock); 373 } 374 375 netif_tx_start_all_queues(dev); 376 377 return 0; 378 } 379 380 static bool xennet_tx_buf_gc(struct netfront_queue *queue) 381 { 382 RING_IDX cons, prod; 383 unsigned short id; 384 struct sk_buff *skb; 385 bool more_to_do; 386 bool work_done = false; 387 const struct device *dev = &queue->info->netdev->dev; 388 389 BUG_ON(!netif_carrier_ok(queue->info->netdev)); 390 391 do { 392 prod = queue->tx.sring->rsp_prod; 393 if (RING_RESPONSE_PROD_OVERFLOW(&queue->tx, prod)) { 394 dev_alert(dev, "Illegal number of responses %u\n", 395 prod - queue->tx.rsp_cons); 396 goto err; 397 } 398 rmb(); /* Ensure we see responses up to 'rp'. */ 399 400 for (cons = queue->tx.rsp_cons; cons != prod; cons++) { 401 struct xen_netif_tx_response txrsp; 402 403 work_done = true; 404 405 RING_COPY_RESPONSE(&queue->tx, cons, &txrsp); 406 if (txrsp.status == XEN_NETIF_RSP_NULL) 407 continue; 408 409 id = txrsp.id; 410 if (id >= RING_SIZE(&queue->tx)) { 411 dev_alert(dev, 412 "Response has incorrect id (%u)\n", 413 id); 414 goto err; 415 } 416 if (queue->tx_link[id] != TX_PENDING) { 417 dev_alert(dev, 418 "Response for inactive request\n"); 419 goto err; 420 } 421 422 queue->tx_link[id] = TX_LINK_NONE; 423 skb = queue->tx_skbs[id]; 424 queue->tx_skbs[id] = NULL; 425 if (unlikely(!gnttab_end_foreign_access_ref( 426 queue->grant_tx_ref[id]))) { 427 dev_alert(dev, 428 "Grant still in use by backend domain\n"); 429 goto err; 430 } 431 gnttab_release_grant_reference( 432 &queue->gref_tx_head, queue->grant_tx_ref[id]); 433 queue->grant_tx_ref[id] = INVALID_GRANT_REF; 434 queue->grant_tx_page[id] = NULL; 435 add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, id); 436 dev_kfree_skb_irq(skb); 437 } 438 439 queue->tx.rsp_cons = prod; 440 441 RING_FINAL_CHECK_FOR_RESPONSES(&queue->tx, more_to_do); 442 } while (more_to_do); 443 444 xennet_maybe_wake_tx(queue); 445 446 return work_done; 447 448 err: 449 queue->info->broken = true; 450 dev_alert(dev, "Disabled for further use\n"); 451 452 return work_done; 453 } 454 455 struct xennet_gnttab_make_txreq { 456 struct netfront_queue *queue; 457 struct sk_buff *skb; 458 struct page *page; 459 struct xen_netif_tx_request *tx; /* Last request on ring page */ 460 struct xen_netif_tx_request tx_local; /* Last request local copy*/ 461 unsigned int size; 462 }; 463 464 static void xennet_tx_setup_grant(unsigned long gfn, unsigned int offset, 465 unsigned int len, void *data) 466 { 467 struct xennet_gnttab_make_txreq *info = data; 468 unsigned int id; 469 struct xen_netif_tx_request *tx; 470 grant_ref_t ref; 471 /* convenient aliases */ 472 struct page *page = info->page; 473 struct netfront_queue *queue = info->queue; 474 struct sk_buff *skb = info->skb; 475 476 id = get_id_from_list(&queue->tx_skb_freelist, queue->tx_link); 477 tx = RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++); 478 ref = gnttab_claim_grant_reference(&queue->gref_tx_head); 479 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref)); 480 481 gnttab_grant_foreign_access_ref(ref, queue->info->xbdev->otherend_id, 482 gfn, GNTMAP_readonly); 483 484 queue->tx_skbs[id] = skb; 485 queue->grant_tx_page[id] = page; 486 queue->grant_tx_ref[id] = ref; 487 488 info->tx_local.id = id; 489 info->tx_local.gref = ref; 490 info->tx_local.offset = offset; 491 info->tx_local.size = len; 492 info->tx_local.flags = 0; 493 494 *tx = info->tx_local; 495 496 /* 497 * Put the request in the pending queue, it will be set to be pending 498 * when the producer index is about to be raised. 499 */ 500 add_id_to_list(&queue->tx_pend_queue, queue->tx_link, id); 501 502 info->tx = tx; 503 info->size += info->tx_local.size; 504 } 505 506 static struct xen_netif_tx_request *xennet_make_first_txreq( 507 struct xennet_gnttab_make_txreq *info, 508 unsigned int offset, unsigned int len) 509 { 510 info->size = 0; 511 512 gnttab_for_one_grant(info->page, offset, len, xennet_tx_setup_grant, info); 513 514 return info->tx; 515 } 516 517 static void xennet_make_one_txreq(unsigned long gfn, unsigned int offset, 518 unsigned int len, void *data) 519 { 520 struct xennet_gnttab_make_txreq *info = data; 521 522 info->tx->flags |= XEN_NETTXF_more_data; 523 skb_get(info->skb); 524 xennet_tx_setup_grant(gfn, offset, len, data); 525 } 526 527 static void xennet_make_txreqs( 528 struct xennet_gnttab_make_txreq *info, 529 struct page *page, 530 unsigned int offset, unsigned int len) 531 { 532 /* Skip unused frames from start of page */ 533 page += offset >> PAGE_SHIFT; 534 offset &= ~PAGE_MASK; 535 536 while (len) { 537 info->page = page; 538 info->size = 0; 539 540 gnttab_foreach_grant_in_range(page, offset, len, 541 xennet_make_one_txreq, 542 info); 543 544 page++; 545 offset = 0; 546 len -= info->size; 547 } 548 } 549 550 /* 551 * Count how many ring slots are required to send this skb. Each frag 552 * might be a compound page. 553 */ 554 static int xennet_count_skb_slots(struct sk_buff *skb) 555 { 556 int i, frags = skb_shinfo(skb)->nr_frags; 557 int slots; 558 559 slots = gnttab_count_grant(offset_in_page(skb->data), 560 skb_headlen(skb)); 561 562 for (i = 0; i < frags; i++) { 563 skb_frag_t *frag = skb_shinfo(skb)->frags + i; 564 unsigned long size = skb_frag_size(frag); 565 unsigned long offset = skb_frag_off(frag); 566 567 /* Skip unused frames from start of page */ 568 offset &= ~PAGE_MASK; 569 570 slots += gnttab_count_grant(offset, size); 571 } 572 573 return slots; 574 } 575 576 static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb, 577 struct net_device *sb_dev) 578 { 579 unsigned int num_queues = dev->real_num_tx_queues; 580 u32 hash; 581 u16 queue_idx; 582 583 /* First, check if there is only one queue */ 584 if (num_queues == 1) { 585 queue_idx = 0; 586 } else { 587 hash = skb_get_hash(skb); 588 queue_idx = hash % num_queues; 589 } 590 591 return queue_idx; 592 } 593 594 static void xennet_mark_tx_pending(struct netfront_queue *queue) 595 { 596 unsigned int i; 597 598 while ((i = get_id_from_list(&queue->tx_pend_queue, queue->tx_link)) != 599 TX_LINK_NONE) 600 queue->tx_link[i] = TX_PENDING; 601 } 602 603 static int xennet_xdp_xmit_one(struct net_device *dev, 604 struct netfront_queue *queue, 605 struct xdp_frame *xdpf) 606 { 607 struct netfront_info *np = netdev_priv(dev); 608 struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats); 609 struct xennet_gnttab_make_txreq info = { 610 .queue = queue, 611 .skb = NULL, 612 .page = virt_to_page(xdpf->data), 613 }; 614 int notify; 615 616 xennet_make_first_txreq(&info, 617 offset_in_page(xdpf->data), 618 xdpf->len); 619 620 xennet_mark_tx_pending(queue); 621 622 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify); 623 if (notify) 624 notify_remote_via_irq(queue->tx_irq); 625 626 u64_stats_update_begin(&tx_stats->syncp); 627 tx_stats->bytes += xdpf->len; 628 tx_stats->packets++; 629 u64_stats_update_end(&tx_stats->syncp); 630 631 xennet_tx_buf_gc(queue); 632 633 return 0; 634 } 635 636 static int xennet_xdp_xmit(struct net_device *dev, int n, 637 struct xdp_frame **frames, u32 flags) 638 { 639 unsigned int num_queues = dev->real_num_tx_queues; 640 struct netfront_info *np = netdev_priv(dev); 641 struct netfront_queue *queue = NULL; 642 unsigned long irq_flags; 643 int nxmit = 0; 644 int i; 645 646 if (unlikely(np->broken)) 647 return -ENODEV; 648 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) 649 return -EINVAL; 650 651 queue = &np->queues[smp_processor_id() % num_queues]; 652 653 spin_lock_irqsave(&queue->tx_lock, irq_flags); 654 for (i = 0; i < n; i++) { 655 struct xdp_frame *xdpf = frames[i]; 656 657 if (!xdpf) 658 continue; 659 if (xennet_xdp_xmit_one(dev, queue, xdpf)) 660 break; 661 nxmit++; 662 } 663 spin_unlock_irqrestore(&queue->tx_lock, irq_flags); 664 665 return nxmit; 666 } 667 668 669 #define MAX_XEN_SKB_FRAGS (65536 / XEN_PAGE_SIZE + 1) 670 671 static netdev_tx_t xennet_start_xmit(struct sk_buff *skb, struct net_device *dev) 672 { 673 struct netfront_info *np = netdev_priv(dev); 674 struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats); 675 struct xen_netif_tx_request *first_tx; 676 unsigned int i; 677 int notify; 678 int slots; 679 struct page *page; 680 unsigned int offset; 681 unsigned int len; 682 unsigned long flags; 683 struct netfront_queue *queue = NULL; 684 struct xennet_gnttab_make_txreq info = { }; 685 unsigned int num_queues = dev->real_num_tx_queues; 686 u16 queue_index; 687 struct sk_buff *nskb; 688 689 /* Drop the packet if no queues are set up */ 690 if (num_queues < 1) 691 goto drop; 692 if (unlikely(np->broken)) 693 goto drop; 694 /* Determine which queue to transmit this SKB on */ 695 queue_index = skb_get_queue_mapping(skb); 696 queue = &np->queues[queue_index]; 697 698 /* If skb->len is too big for wire format, drop skb and alert 699 * user about misconfiguration. 700 */ 701 if (unlikely(skb->len > XEN_NETIF_MAX_TX_SIZE)) { 702 net_alert_ratelimited( 703 "xennet: skb->len = %u, too big for wire format\n", 704 skb->len); 705 goto drop; 706 } 707 708 slots = xennet_count_skb_slots(skb); 709 if (unlikely(slots > MAX_XEN_SKB_FRAGS + 1)) { 710 net_dbg_ratelimited("xennet: skb rides the rocket: %d slots, %d bytes\n", 711 slots, skb->len); 712 if (skb_linearize(skb)) 713 goto drop; 714 } 715 716 page = virt_to_page(skb->data); 717 offset = offset_in_page(skb->data); 718 719 /* The first req should be at least ETH_HLEN size or the packet will be 720 * dropped by netback. 721 */ 722 if (unlikely(PAGE_SIZE - offset < ETH_HLEN)) { 723 nskb = skb_copy(skb, GFP_ATOMIC); 724 if (!nskb) 725 goto drop; 726 dev_consume_skb_any(skb); 727 skb = nskb; 728 page = virt_to_page(skb->data); 729 offset = offset_in_page(skb->data); 730 } 731 732 len = skb_headlen(skb); 733 734 spin_lock_irqsave(&queue->tx_lock, flags); 735 736 if (unlikely(!netif_carrier_ok(dev) || 737 (slots > 1 && !xennet_can_sg(dev)) || 738 netif_needs_gso(skb, netif_skb_features(skb)))) { 739 spin_unlock_irqrestore(&queue->tx_lock, flags); 740 goto drop; 741 } 742 743 /* First request for the linear area. */ 744 info.queue = queue; 745 info.skb = skb; 746 info.page = page; 747 first_tx = xennet_make_first_txreq(&info, offset, len); 748 offset += info.tx_local.size; 749 if (offset == PAGE_SIZE) { 750 page++; 751 offset = 0; 752 } 753 len -= info.tx_local.size; 754 755 if (skb->ip_summed == CHECKSUM_PARTIAL) 756 /* local packet? */ 757 first_tx->flags |= XEN_NETTXF_csum_blank | 758 XEN_NETTXF_data_validated; 759 else if (skb->ip_summed == CHECKSUM_UNNECESSARY) 760 /* remote but checksummed. */ 761 first_tx->flags |= XEN_NETTXF_data_validated; 762 763 /* Optional extra info after the first request. */ 764 if (skb_shinfo(skb)->gso_size) { 765 struct xen_netif_extra_info *gso; 766 767 gso = (struct xen_netif_extra_info *) 768 RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++); 769 770 first_tx->flags |= XEN_NETTXF_extra_info; 771 772 gso->u.gso.size = skb_shinfo(skb)->gso_size; 773 gso->u.gso.type = (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) ? 774 XEN_NETIF_GSO_TYPE_TCPV6 : 775 XEN_NETIF_GSO_TYPE_TCPV4; 776 gso->u.gso.pad = 0; 777 gso->u.gso.features = 0; 778 779 gso->type = XEN_NETIF_EXTRA_TYPE_GSO; 780 gso->flags = 0; 781 } 782 783 /* Requests for the rest of the linear area. */ 784 xennet_make_txreqs(&info, page, offset, len); 785 786 /* Requests for all the frags. */ 787 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 788 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 789 xennet_make_txreqs(&info, skb_frag_page(frag), 790 skb_frag_off(frag), 791 skb_frag_size(frag)); 792 } 793 794 /* First request has the packet length. */ 795 first_tx->size = skb->len; 796 797 /* timestamp packet in software */ 798 skb_tx_timestamp(skb); 799 800 xennet_mark_tx_pending(queue); 801 802 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify); 803 if (notify) 804 notify_remote_via_irq(queue->tx_irq); 805 806 u64_stats_update_begin(&tx_stats->syncp); 807 tx_stats->bytes += skb->len; 808 tx_stats->packets++; 809 u64_stats_update_end(&tx_stats->syncp); 810 811 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */ 812 xennet_tx_buf_gc(queue); 813 814 if (!netfront_tx_slot_available(queue)) 815 netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id)); 816 817 spin_unlock_irqrestore(&queue->tx_lock, flags); 818 819 return NETDEV_TX_OK; 820 821 drop: 822 dev->stats.tx_dropped++; 823 dev_kfree_skb_any(skb); 824 return NETDEV_TX_OK; 825 } 826 827 static int xennet_close(struct net_device *dev) 828 { 829 struct netfront_info *np = netdev_priv(dev); 830 unsigned int num_queues = dev->real_num_tx_queues; 831 unsigned int i; 832 struct netfront_queue *queue; 833 netif_tx_stop_all_queues(np->netdev); 834 for (i = 0; i < num_queues; ++i) { 835 queue = &np->queues[i]; 836 napi_disable(&queue->napi); 837 } 838 return 0; 839 } 840 841 static void xennet_destroy_queues(struct netfront_info *info) 842 { 843 unsigned int i; 844 845 for (i = 0; i < info->netdev->real_num_tx_queues; i++) { 846 struct netfront_queue *queue = &info->queues[i]; 847 848 if (netif_running(info->netdev)) 849 napi_disable(&queue->napi); 850 netif_napi_del(&queue->napi); 851 } 852 853 kfree(info->queues); 854 info->queues = NULL; 855 } 856 857 static void xennet_uninit(struct net_device *dev) 858 { 859 struct netfront_info *np = netdev_priv(dev); 860 xennet_destroy_queues(np); 861 } 862 863 static void xennet_set_rx_rsp_cons(struct netfront_queue *queue, RING_IDX val) 864 { 865 unsigned long flags; 866 867 spin_lock_irqsave(&queue->rx_cons_lock, flags); 868 queue->rx.rsp_cons = val; 869 queue->rx_rsp_unconsumed = XEN_RING_NR_UNCONSUMED_RESPONSES(&queue->rx); 870 spin_unlock_irqrestore(&queue->rx_cons_lock, flags); 871 } 872 873 static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb, 874 grant_ref_t ref) 875 { 876 int new = xennet_rxidx(queue->rx.req_prod_pvt); 877 878 BUG_ON(queue->rx_skbs[new]); 879 queue->rx_skbs[new] = skb; 880 queue->grant_rx_ref[new] = ref; 881 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->id = new; 882 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->gref = ref; 883 queue->rx.req_prod_pvt++; 884 } 885 886 static int xennet_get_extras(struct netfront_queue *queue, 887 struct xen_netif_extra_info *extras, 888 RING_IDX rp) 889 890 { 891 struct xen_netif_extra_info extra; 892 struct device *dev = &queue->info->netdev->dev; 893 RING_IDX cons = queue->rx.rsp_cons; 894 int err = 0; 895 896 do { 897 struct sk_buff *skb; 898 grant_ref_t ref; 899 900 if (unlikely(cons + 1 == rp)) { 901 if (net_ratelimit()) 902 dev_warn(dev, "Missing extra info\n"); 903 err = -EBADR; 904 break; 905 } 906 907 RING_COPY_RESPONSE(&queue->rx, ++cons, &extra); 908 909 if (unlikely(!extra.type || 910 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) { 911 if (net_ratelimit()) 912 dev_warn(dev, "Invalid extra type: %d\n", 913 extra.type); 914 err = -EINVAL; 915 } else { 916 extras[extra.type - 1] = extra; 917 } 918 919 skb = xennet_get_rx_skb(queue, cons); 920 ref = xennet_get_rx_ref(queue, cons); 921 xennet_move_rx_slot(queue, skb, ref); 922 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE); 923 924 xennet_set_rx_rsp_cons(queue, cons); 925 return err; 926 } 927 928 static u32 xennet_run_xdp(struct netfront_queue *queue, struct page *pdata, 929 struct xen_netif_rx_response *rx, struct bpf_prog *prog, 930 struct xdp_buff *xdp, bool *need_xdp_flush) 931 { 932 struct xdp_frame *xdpf; 933 u32 len = rx->status; 934 u32 act; 935 int err; 936 937 xdp_init_buff(xdp, XEN_PAGE_SIZE - XDP_PACKET_HEADROOM, 938 &queue->xdp_rxq); 939 xdp_prepare_buff(xdp, page_address(pdata), XDP_PACKET_HEADROOM, 940 len, false); 941 942 act = bpf_prog_run_xdp(prog, xdp); 943 switch (act) { 944 case XDP_TX: 945 get_page(pdata); 946 xdpf = xdp_convert_buff_to_frame(xdp); 947 err = xennet_xdp_xmit(queue->info->netdev, 1, &xdpf, 0); 948 if (unlikely(!err)) 949 xdp_return_frame_rx_napi(xdpf); 950 else if (unlikely(err < 0)) 951 trace_xdp_exception(queue->info->netdev, prog, act); 952 break; 953 case XDP_REDIRECT: 954 get_page(pdata); 955 err = xdp_do_redirect(queue->info->netdev, xdp, prog); 956 *need_xdp_flush = true; 957 if (unlikely(err)) 958 trace_xdp_exception(queue->info->netdev, prog, act); 959 break; 960 case XDP_PASS: 961 case XDP_DROP: 962 break; 963 964 case XDP_ABORTED: 965 trace_xdp_exception(queue->info->netdev, prog, act); 966 break; 967 968 default: 969 bpf_warn_invalid_xdp_action(queue->info->netdev, prog, act); 970 } 971 972 return act; 973 } 974 975 static int xennet_get_responses(struct netfront_queue *queue, 976 struct netfront_rx_info *rinfo, RING_IDX rp, 977 struct sk_buff_head *list, 978 bool *need_xdp_flush) 979 { 980 struct xen_netif_rx_response *rx = &rinfo->rx, rx_local; 981 int max = XEN_NETIF_NR_SLOTS_MIN + (rx->status <= RX_COPY_THRESHOLD); 982 RING_IDX cons = queue->rx.rsp_cons; 983 struct sk_buff *skb = xennet_get_rx_skb(queue, cons); 984 struct xen_netif_extra_info *extras = rinfo->extras; 985 grant_ref_t ref = xennet_get_rx_ref(queue, cons); 986 struct device *dev = &queue->info->netdev->dev; 987 struct bpf_prog *xdp_prog; 988 struct xdp_buff xdp; 989 int slots = 1; 990 int err = 0; 991 u32 verdict; 992 993 if (rx->flags & XEN_NETRXF_extra_info) { 994 err = xennet_get_extras(queue, extras, rp); 995 if (!err) { 996 if (extras[XEN_NETIF_EXTRA_TYPE_XDP - 1].type) { 997 struct xen_netif_extra_info *xdp; 998 999 xdp = &extras[XEN_NETIF_EXTRA_TYPE_XDP - 1]; 1000 rx->offset = xdp->u.xdp.headroom; 1001 } 1002 } 1003 cons = queue->rx.rsp_cons; 1004 } 1005 1006 for (;;) { 1007 if (unlikely(rx->status < 0 || 1008 rx->offset + rx->status > XEN_PAGE_SIZE)) { 1009 if (net_ratelimit()) 1010 dev_warn(dev, "rx->offset: %u, size: %d\n", 1011 rx->offset, rx->status); 1012 xennet_move_rx_slot(queue, skb, ref); 1013 err = -EINVAL; 1014 goto next; 1015 } 1016 1017 /* 1018 * This definitely indicates a bug, either in this driver or in 1019 * the backend driver. In future this should flag the bad 1020 * situation to the system controller to reboot the backend. 1021 */ 1022 if (ref == INVALID_GRANT_REF) { 1023 if (net_ratelimit()) 1024 dev_warn(dev, "Bad rx response id %d.\n", 1025 rx->id); 1026 err = -EINVAL; 1027 goto next; 1028 } 1029 1030 if (!gnttab_end_foreign_access_ref(ref)) { 1031 dev_alert(dev, 1032 "Grant still in use by backend domain\n"); 1033 queue->info->broken = true; 1034 dev_alert(dev, "Disabled for further use\n"); 1035 return -EINVAL; 1036 } 1037 1038 gnttab_release_grant_reference(&queue->gref_rx_head, ref); 1039 1040 rcu_read_lock(); 1041 xdp_prog = rcu_dereference(queue->xdp_prog); 1042 if (xdp_prog) { 1043 if (!(rx->flags & XEN_NETRXF_more_data)) { 1044 /* currently only a single page contains data */ 1045 verdict = xennet_run_xdp(queue, 1046 skb_frag_page(&skb_shinfo(skb)->frags[0]), 1047 rx, xdp_prog, &xdp, need_xdp_flush); 1048 if (verdict != XDP_PASS) 1049 err = -EINVAL; 1050 } else { 1051 /* drop the frame */ 1052 err = -EINVAL; 1053 } 1054 } 1055 rcu_read_unlock(); 1056 next: 1057 __skb_queue_tail(list, skb); 1058 if (!(rx->flags & XEN_NETRXF_more_data)) 1059 break; 1060 1061 if (cons + slots == rp) { 1062 if (net_ratelimit()) 1063 dev_warn(dev, "Need more slots\n"); 1064 err = -ENOENT; 1065 break; 1066 } 1067 1068 RING_COPY_RESPONSE(&queue->rx, cons + slots, &rx_local); 1069 rx = &rx_local; 1070 skb = xennet_get_rx_skb(queue, cons + slots); 1071 ref = xennet_get_rx_ref(queue, cons + slots); 1072 slots++; 1073 } 1074 1075 if (unlikely(slots > max)) { 1076 if (net_ratelimit()) 1077 dev_warn(dev, "Too many slots\n"); 1078 err = -E2BIG; 1079 } 1080 1081 if (unlikely(err)) 1082 xennet_set_rx_rsp_cons(queue, cons + slots); 1083 1084 return err; 1085 } 1086 1087 static int xennet_set_skb_gso(struct sk_buff *skb, 1088 struct xen_netif_extra_info *gso) 1089 { 1090 if (!gso->u.gso.size) { 1091 if (net_ratelimit()) 1092 pr_warn("GSO size must not be zero\n"); 1093 return -EINVAL; 1094 } 1095 1096 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 && 1097 gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) { 1098 if (net_ratelimit()) 1099 pr_warn("Bad GSO type %d\n", gso->u.gso.type); 1100 return -EINVAL; 1101 } 1102 1103 skb_shinfo(skb)->gso_size = gso->u.gso.size; 1104 skb_shinfo(skb)->gso_type = 1105 (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ? 1106 SKB_GSO_TCPV4 : 1107 SKB_GSO_TCPV6; 1108 1109 /* Header must be checked, and gso_segs computed. */ 1110 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; 1111 skb_shinfo(skb)->gso_segs = 0; 1112 1113 return 0; 1114 } 1115 1116 static int xennet_fill_frags(struct netfront_queue *queue, 1117 struct sk_buff *skb, 1118 struct sk_buff_head *list) 1119 { 1120 RING_IDX cons = queue->rx.rsp_cons; 1121 struct sk_buff *nskb; 1122 1123 while ((nskb = __skb_dequeue(list))) { 1124 struct xen_netif_rx_response rx; 1125 skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0]; 1126 1127 RING_COPY_RESPONSE(&queue->rx, ++cons, &rx); 1128 1129 if (skb_shinfo(skb)->nr_frags == MAX_SKB_FRAGS) { 1130 unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to; 1131 1132 BUG_ON(pull_to < skb_headlen(skb)); 1133 __pskb_pull_tail(skb, pull_to - skb_headlen(skb)); 1134 } 1135 if (unlikely(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS)) { 1136 xennet_set_rx_rsp_cons(queue, 1137 ++cons + skb_queue_len(list)); 1138 kfree_skb(nskb); 1139 return -ENOENT; 1140 } 1141 1142 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, 1143 skb_frag_page(nfrag), 1144 rx.offset, rx.status, PAGE_SIZE); 1145 1146 skb_shinfo(nskb)->nr_frags = 0; 1147 kfree_skb(nskb); 1148 } 1149 1150 xennet_set_rx_rsp_cons(queue, cons); 1151 1152 return 0; 1153 } 1154 1155 static int checksum_setup(struct net_device *dev, struct sk_buff *skb) 1156 { 1157 bool recalculate_partial_csum = false; 1158 1159 /* 1160 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy 1161 * peers can fail to set NETRXF_csum_blank when sending a GSO 1162 * frame. In this case force the SKB to CHECKSUM_PARTIAL and 1163 * recalculate the partial checksum. 1164 */ 1165 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) { 1166 struct netfront_info *np = netdev_priv(dev); 1167 atomic_inc(&np->rx_gso_checksum_fixup); 1168 skb->ip_summed = CHECKSUM_PARTIAL; 1169 recalculate_partial_csum = true; 1170 } 1171 1172 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */ 1173 if (skb->ip_summed != CHECKSUM_PARTIAL) 1174 return 0; 1175 1176 return skb_checksum_setup(skb, recalculate_partial_csum); 1177 } 1178 1179 static int handle_incoming_queue(struct netfront_queue *queue, 1180 struct sk_buff_head *rxq) 1181 { 1182 struct netfront_stats *rx_stats = this_cpu_ptr(queue->info->rx_stats); 1183 int packets_dropped = 0; 1184 struct sk_buff *skb; 1185 1186 while ((skb = __skb_dequeue(rxq)) != NULL) { 1187 int pull_to = NETFRONT_SKB_CB(skb)->pull_to; 1188 1189 if (pull_to > skb_headlen(skb)) 1190 __pskb_pull_tail(skb, pull_to - skb_headlen(skb)); 1191 1192 /* Ethernet work: Delayed to here as it peeks the header. */ 1193 skb->protocol = eth_type_trans(skb, queue->info->netdev); 1194 skb_reset_network_header(skb); 1195 1196 if (checksum_setup(queue->info->netdev, skb)) { 1197 kfree_skb(skb); 1198 packets_dropped++; 1199 queue->info->netdev->stats.rx_errors++; 1200 continue; 1201 } 1202 1203 u64_stats_update_begin(&rx_stats->syncp); 1204 rx_stats->packets++; 1205 rx_stats->bytes += skb->len; 1206 u64_stats_update_end(&rx_stats->syncp); 1207 1208 /* Pass it up. */ 1209 napi_gro_receive(&queue->napi, skb); 1210 } 1211 1212 return packets_dropped; 1213 } 1214 1215 static int xennet_poll(struct napi_struct *napi, int budget) 1216 { 1217 struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi); 1218 struct net_device *dev = queue->info->netdev; 1219 struct sk_buff *skb; 1220 struct netfront_rx_info rinfo; 1221 struct xen_netif_rx_response *rx = &rinfo.rx; 1222 struct xen_netif_extra_info *extras = rinfo.extras; 1223 RING_IDX i, rp; 1224 int work_done; 1225 struct sk_buff_head rxq; 1226 struct sk_buff_head errq; 1227 struct sk_buff_head tmpq; 1228 int err; 1229 bool need_xdp_flush = false; 1230 1231 spin_lock(&queue->rx_lock); 1232 1233 skb_queue_head_init(&rxq); 1234 skb_queue_head_init(&errq); 1235 skb_queue_head_init(&tmpq); 1236 1237 rp = queue->rx.sring->rsp_prod; 1238 if (RING_RESPONSE_PROD_OVERFLOW(&queue->rx, rp)) { 1239 dev_alert(&dev->dev, "Illegal number of responses %u\n", 1240 rp - queue->rx.rsp_cons); 1241 queue->info->broken = true; 1242 spin_unlock(&queue->rx_lock); 1243 return 0; 1244 } 1245 rmb(); /* Ensure we see queued responses up to 'rp'. */ 1246 1247 i = queue->rx.rsp_cons; 1248 work_done = 0; 1249 while ((i != rp) && (work_done < budget)) { 1250 RING_COPY_RESPONSE(&queue->rx, i, rx); 1251 memset(extras, 0, sizeof(rinfo.extras)); 1252 1253 err = xennet_get_responses(queue, &rinfo, rp, &tmpq, 1254 &need_xdp_flush); 1255 1256 if (unlikely(err)) { 1257 if (queue->info->broken) { 1258 spin_unlock(&queue->rx_lock); 1259 return 0; 1260 } 1261 err: 1262 while ((skb = __skb_dequeue(&tmpq))) 1263 __skb_queue_tail(&errq, skb); 1264 dev->stats.rx_errors++; 1265 i = queue->rx.rsp_cons; 1266 continue; 1267 } 1268 1269 skb = __skb_dequeue(&tmpq); 1270 1271 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) { 1272 struct xen_netif_extra_info *gso; 1273 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1]; 1274 1275 if (unlikely(xennet_set_skb_gso(skb, gso))) { 1276 __skb_queue_head(&tmpq, skb); 1277 xennet_set_rx_rsp_cons(queue, 1278 queue->rx.rsp_cons + 1279 skb_queue_len(&tmpq)); 1280 goto err; 1281 } 1282 } 1283 1284 NETFRONT_SKB_CB(skb)->pull_to = rx->status; 1285 if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD) 1286 NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD; 1287 1288 skb_frag_off_set(&skb_shinfo(skb)->frags[0], rx->offset); 1289 skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status); 1290 skb->data_len = rx->status; 1291 skb->len += rx->status; 1292 1293 if (unlikely(xennet_fill_frags(queue, skb, &tmpq))) 1294 goto err; 1295 1296 if (rx->flags & XEN_NETRXF_csum_blank) 1297 skb->ip_summed = CHECKSUM_PARTIAL; 1298 else if (rx->flags & XEN_NETRXF_data_validated) 1299 skb->ip_summed = CHECKSUM_UNNECESSARY; 1300 1301 __skb_queue_tail(&rxq, skb); 1302 1303 i = queue->rx.rsp_cons + 1; 1304 xennet_set_rx_rsp_cons(queue, i); 1305 work_done++; 1306 } 1307 if (need_xdp_flush) 1308 xdp_do_flush(); 1309 1310 __skb_queue_purge(&errq); 1311 1312 work_done -= handle_incoming_queue(queue, &rxq); 1313 1314 xennet_alloc_rx_buffers(queue); 1315 1316 if (work_done < budget) { 1317 int more_to_do = 0; 1318 1319 napi_complete_done(napi, work_done); 1320 1321 RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do); 1322 if (more_to_do) 1323 napi_schedule(napi); 1324 } 1325 1326 spin_unlock(&queue->rx_lock); 1327 1328 return work_done; 1329 } 1330 1331 static int xennet_change_mtu(struct net_device *dev, int mtu) 1332 { 1333 int max = xennet_can_sg(dev) ? XEN_NETIF_MAX_TX_SIZE : ETH_DATA_LEN; 1334 1335 if (mtu > max) 1336 return -EINVAL; 1337 dev->mtu = mtu; 1338 return 0; 1339 } 1340 1341 static void xennet_get_stats64(struct net_device *dev, 1342 struct rtnl_link_stats64 *tot) 1343 { 1344 struct netfront_info *np = netdev_priv(dev); 1345 int cpu; 1346 1347 for_each_possible_cpu(cpu) { 1348 struct netfront_stats *rx_stats = per_cpu_ptr(np->rx_stats, cpu); 1349 struct netfront_stats *tx_stats = per_cpu_ptr(np->tx_stats, cpu); 1350 u64 rx_packets, rx_bytes, tx_packets, tx_bytes; 1351 unsigned int start; 1352 1353 do { 1354 start = u64_stats_fetch_begin_irq(&tx_stats->syncp); 1355 tx_packets = tx_stats->packets; 1356 tx_bytes = tx_stats->bytes; 1357 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start)); 1358 1359 do { 1360 start = u64_stats_fetch_begin_irq(&rx_stats->syncp); 1361 rx_packets = rx_stats->packets; 1362 rx_bytes = rx_stats->bytes; 1363 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start)); 1364 1365 tot->rx_packets += rx_packets; 1366 tot->tx_packets += tx_packets; 1367 tot->rx_bytes += rx_bytes; 1368 tot->tx_bytes += tx_bytes; 1369 } 1370 1371 tot->rx_errors = dev->stats.rx_errors; 1372 tot->tx_dropped = dev->stats.tx_dropped; 1373 } 1374 1375 static void xennet_release_tx_bufs(struct netfront_queue *queue) 1376 { 1377 struct sk_buff *skb; 1378 int i; 1379 1380 for (i = 0; i < NET_TX_RING_SIZE; i++) { 1381 /* Skip over entries which are actually freelist references */ 1382 if (!queue->tx_skbs[i]) 1383 continue; 1384 1385 skb = queue->tx_skbs[i]; 1386 queue->tx_skbs[i] = NULL; 1387 get_page(queue->grant_tx_page[i]); 1388 gnttab_end_foreign_access(queue->grant_tx_ref[i], 1389 (unsigned long)page_address(queue->grant_tx_page[i])); 1390 queue->grant_tx_page[i] = NULL; 1391 queue->grant_tx_ref[i] = INVALID_GRANT_REF; 1392 add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, i); 1393 dev_kfree_skb_irq(skb); 1394 } 1395 } 1396 1397 static void xennet_release_rx_bufs(struct netfront_queue *queue) 1398 { 1399 int id, ref; 1400 1401 spin_lock_bh(&queue->rx_lock); 1402 1403 for (id = 0; id < NET_RX_RING_SIZE; id++) { 1404 struct sk_buff *skb; 1405 struct page *page; 1406 1407 skb = queue->rx_skbs[id]; 1408 if (!skb) 1409 continue; 1410 1411 ref = queue->grant_rx_ref[id]; 1412 if (ref == INVALID_GRANT_REF) 1413 continue; 1414 1415 page = skb_frag_page(&skb_shinfo(skb)->frags[0]); 1416 1417 /* gnttab_end_foreign_access() needs a page ref until 1418 * foreign access is ended (which may be deferred). 1419 */ 1420 get_page(page); 1421 gnttab_end_foreign_access(ref, 1422 (unsigned long)page_address(page)); 1423 queue->grant_rx_ref[id] = INVALID_GRANT_REF; 1424 1425 kfree_skb(skb); 1426 } 1427 1428 spin_unlock_bh(&queue->rx_lock); 1429 } 1430 1431 static netdev_features_t xennet_fix_features(struct net_device *dev, 1432 netdev_features_t features) 1433 { 1434 struct netfront_info *np = netdev_priv(dev); 1435 1436 if (features & NETIF_F_SG && 1437 !xenbus_read_unsigned(np->xbdev->otherend, "feature-sg", 0)) 1438 features &= ~NETIF_F_SG; 1439 1440 if (features & NETIF_F_IPV6_CSUM && 1441 !xenbus_read_unsigned(np->xbdev->otherend, 1442 "feature-ipv6-csum-offload", 0)) 1443 features &= ~NETIF_F_IPV6_CSUM; 1444 1445 if (features & NETIF_F_TSO && 1446 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv4", 0)) 1447 features &= ~NETIF_F_TSO; 1448 1449 if (features & NETIF_F_TSO6 && 1450 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv6", 0)) 1451 features &= ~NETIF_F_TSO6; 1452 1453 return features; 1454 } 1455 1456 static int xennet_set_features(struct net_device *dev, 1457 netdev_features_t features) 1458 { 1459 if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) { 1460 netdev_info(dev, "Reducing MTU because no SG offload"); 1461 dev->mtu = ETH_DATA_LEN; 1462 } 1463 1464 return 0; 1465 } 1466 1467 static bool xennet_handle_tx(struct netfront_queue *queue, unsigned int *eoi) 1468 { 1469 unsigned long flags; 1470 1471 if (unlikely(queue->info->broken)) 1472 return false; 1473 1474 spin_lock_irqsave(&queue->tx_lock, flags); 1475 if (xennet_tx_buf_gc(queue)) 1476 *eoi = 0; 1477 spin_unlock_irqrestore(&queue->tx_lock, flags); 1478 1479 return true; 1480 } 1481 1482 static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id) 1483 { 1484 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS; 1485 1486 if (likely(xennet_handle_tx(dev_id, &eoiflag))) 1487 xen_irq_lateeoi(irq, eoiflag); 1488 1489 return IRQ_HANDLED; 1490 } 1491 1492 static bool xennet_handle_rx(struct netfront_queue *queue, unsigned int *eoi) 1493 { 1494 unsigned int work_queued; 1495 unsigned long flags; 1496 1497 if (unlikely(queue->info->broken)) 1498 return false; 1499 1500 spin_lock_irqsave(&queue->rx_cons_lock, flags); 1501 work_queued = XEN_RING_NR_UNCONSUMED_RESPONSES(&queue->rx); 1502 if (work_queued > queue->rx_rsp_unconsumed) { 1503 queue->rx_rsp_unconsumed = work_queued; 1504 *eoi = 0; 1505 } else if (unlikely(work_queued < queue->rx_rsp_unconsumed)) { 1506 const struct device *dev = &queue->info->netdev->dev; 1507 1508 spin_unlock_irqrestore(&queue->rx_cons_lock, flags); 1509 dev_alert(dev, "RX producer index going backwards\n"); 1510 dev_alert(dev, "Disabled for further use\n"); 1511 queue->info->broken = true; 1512 return false; 1513 } 1514 spin_unlock_irqrestore(&queue->rx_cons_lock, flags); 1515 1516 if (likely(netif_carrier_ok(queue->info->netdev) && work_queued)) 1517 napi_schedule(&queue->napi); 1518 1519 return true; 1520 } 1521 1522 static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id) 1523 { 1524 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS; 1525 1526 if (likely(xennet_handle_rx(dev_id, &eoiflag))) 1527 xen_irq_lateeoi(irq, eoiflag); 1528 1529 return IRQ_HANDLED; 1530 } 1531 1532 static irqreturn_t xennet_interrupt(int irq, void *dev_id) 1533 { 1534 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS; 1535 1536 if (xennet_handle_tx(dev_id, &eoiflag) && 1537 xennet_handle_rx(dev_id, &eoiflag)) 1538 xen_irq_lateeoi(irq, eoiflag); 1539 1540 return IRQ_HANDLED; 1541 } 1542 1543 #ifdef CONFIG_NET_POLL_CONTROLLER 1544 static void xennet_poll_controller(struct net_device *dev) 1545 { 1546 /* Poll each queue */ 1547 struct netfront_info *info = netdev_priv(dev); 1548 unsigned int num_queues = dev->real_num_tx_queues; 1549 unsigned int i; 1550 1551 if (info->broken) 1552 return; 1553 1554 for (i = 0; i < num_queues; ++i) 1555 xennet_interrupt(0, &info->queues[i]); 1556 } 1557 #endif 1558 1559 #define NETBACK_XDP_HEADROOM_DISABLE 0 1560 #define NETBACK_XDP_HEADROOM_ENABLE 1 1561 1562 static int talk_to_netback_xdp(struct netfront_info *np, int xdp) 1563 { 1564 int err; 1565 unsigned short headroom; 1566 1567 headroom = xdp ? XDP_PACKET_HEADROOM : 0; 1568 err = xenbus_printf(XBT_NIL, np->xbdev->nodename, 1569 "xdp-headroom", "%hu", 1570 headroom); 1571 if (err) 1572 pr_warn("Error writing xdp-headroom\n"); 1573 1574 return err; 1575 } 1576 1577 static int xennet_xdp_set(struct net_device *dev, struct bpf_prog *prog, 1578 struct netlink_ext_ack *extack) 1579 { 1580 unsigned long max_mtu = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM; 1581 struct netfront_info *np = netdev_priv(dev); 1582 struct bpf_prog *old_prog; 1583 unsigned int i, err; 1584 1585 if (dev->mtu > max_mtu) { 1586 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_mtu); 1587 return -EINVAL; 1588 } 1589 1590 if (!np->netback_has_xdp_headroom) 1591 return 0; 1592 1593 xenbus_switch_state(np->xbdev, XenbusStateReconfiguring); 1594 1595 err = talk_to_netback_xdp(np, prog ? NETBACK_XDP_HEADROOM_ENABLE : 1596 NETBACK_XDP_HEADROOM_DISABLE); 1597 if (err) 1598 return err; 1599 1600 /* avoid the race with XDP headroom adjustment */ 1601 wait_event(module_wq, 1602 xenbus_read_driver_state(np->xbdev->otherend) == 1603 XenbusStateReconfigured); 1604 np->netfront_xdp_enabled = true; 1605 1606 old_prog = rtnl_dereference(np->queues[0].xdp_prog); 1607 1608 if (prog) 1609 bpf_prog_add(prog, dev->real_num_tx_queues); 1610 1611 for (i = 0; i < dev->real_num_tx_queues; ++i) 1612 rcu_assign_pointer(np->queues[i].xdp_prog, prog); 1613 1614 if (old_prog) 1615 for (i = 0; i < dev->real_num_tx_queues; ++i) 1616 bpf_prog_put(old_prog); 1617 1618 xenbus_switch_state(np->xbdev, XenbusStateConnected); 1619 1620 return 0; 1621 } 1622 1623 static int xennet_xdp(struct net_device *dev, struct netdev_bpf *xdp) 1624 { 1625 struct netfront_info *np = netdev_priv(dev); 1626 1627 if (np->broken) 1628 return -ENODEV; 1629 1630 switch (xdp->command) { 1631 case XDP_SETUP_PROG: 1632 return xennet_xdp_set(dev, xdp->prog, xdp->extack); 1633 default: 1634 return -EINVAL; 1635 } 1636 } 1637 1638 static const struct net_device_ops xennet_netdev_ops = { 1639 .ndo_uninit = xennet_uninit, 1640 .ndo_open = xennet_open, 1641 .ndo_stop = xennet_close, 1642 .ndo_start_xmit = xennet_start_xmit, 1643 .ndo_change_mtu = xennet_change_mtu, 1644 .ndo_get_stats64 = xennet_get_stats64, 1645 .ndo_set_mac_address = eth_mac_addr, 1646 .ndo_validate_addr = eth_validate_addr, 1647 .ndo_fix_features = xennet_fix_features, 1648 .ndo_set_features = xennet_set_features, 1649 .ndo_select_queue = xennet_select_queue, 1650 .ndo_bpf = xennet_xdp, 1651 .ndo_xdp_xmit = xennet_xdp_xmit, 1652 #ifdef CONFIG_NET_POLL_CONTROLLER 1653 .ndo_poll_controller = xennet_poll_controller, 1654 #endif 1655 }; 1656 1657 static void xennet_free_netdev(struct net_device *netdev) 1658 { 1659 struct netfront_info *np = netdev_priv(netdev); 1660 1661 free_percpu(np->rx_stats); 1662 free_percpu(np->tx_stats); 1663 free_netdev(netdev); 1664 } 1665 1666 static struct net_device *xennet_create_dev(struct xenbus_device *dev) 1667 { 1668 int err; 1669 struct net_device *netdev; 1670 struct netfront_info *np; 1671 1672 netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues); 1673 if (!netdev) 1674 return ERR_PTR(-ENOMEM); 1675 1676 np = netdev_priv(netdev); 1677 np->xbdev = dev; 1678 1679 np->queues = NULL; 1680 1681 err = -ENOMEM; 1682 np->rx_stats = netdev_alloc_pcpu_stats(struct netfront_stats); 1683 if (np->rx_stats == NULL) 1684 goto exit; 1685 np->tx_stats = netdev_alloc_pcpu_stats(struct netfront_stats); 1686 if (np->tx_stats == NULL) 1687 goto exit; 1688 1689 netdev->netdev_ops = &xennet_netdev_ops; 1690 1691 netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM | 1692 NETIF_F_GSO_ROBUST; 1693 netdev->hw_features = NETIF_F_SG | 1694 NETIF_F_IPV6_CSUM | 1695 NETIF_F_TSO | NETIF_F_TSO6; 1696 1697 /* 1698 * Assume that all hw features are available for now. This set 1699 * will be adjusted by the call to netdev_update_features() in 1700 * xennet_connect() which is the earliest point where we can 1701 * negotiate with the backend regarding supported features. 1702 */ 1703 netdev->features |= netdev->hw_features; 1704 1705 netdev->ethtool_ops = &xennet_ethtool_ops; 1706 netdev->min_mtu = ETH_MIN_MTU; 1707 netdev->max_mtu = XEN_NETIF_MAX_TX_SIZE; 1708 SET_NETDEV_DEV(netdev, &dev->dev); 1709 1710 np->netdev = netdev; 1711 np->netfront_xdp_enabled = false; 1712 1713 netif_carrier_off(netdev); 1714 1715 do { 1716 xenbus_switch_state(dev, XenbusStateInitialising); 1717 err = wait_event_timeout(module_wq, 1718 xenbus_read_driver_state(dev->otherend) != 1719 XenbusStateClosed && 1720 xenbus_read_driver_state(dev->otherend) != 1721 XenbusStateUnknown, XENNET_TIMEOUT); 1722 } while (!err); 1723 1724 return netdev; 1725 1726 exit: 1727 xennet_free_netdev(netdev); 1728 return ERR_PTR(err); 1729 } 1730 1731 /* 1732 * Entry point to this code when a new device is created. Allocate the basic 1733 * structures and the ring buffers for communication with the backend, and 1734 * inform the backend of the appropriate details for those. 1735 */ 1736 static int netfront_probe(struct xenbus_device *dev, 1737 const struct xenbus_device_id *id) 1738 { 1739 int err; 1740 struct net_device *netdev; 1741 struct netfront_info *info; 1742 1743 netdev = xennet_create_dev(dev); 1744 if (IS_ERR(netdev)) { 1745 err = PTR_ERR(netdev); 1746 xenbus_dev_fatal(dev, err, "creating netdev"); 1747 return err; 1748 } 1749 1750 info = netdev_priv(netdev); 1751 dev_set_drvdata(&dev->dev, info); 1752 #ifdef CONFIG_SYSFS 1753 info->netdev->sysfs_groups[0] = &xennet_dev_group; 1754 #endif 1755 1756 return 0; 1757 } 1758 1759 static void xennet_end_access(int ref, void *page) 1760 { 1761 /* This frees the page as a side-effect */ 1762 if (ref != INVALID_GRANT_REF) 1763 gnttab_end_foreign_access(ref, (unsigned long)page); 1764 } 1765 1766 static void xennet_disconnect_backend(struct netfront_info *info) 1767 { 1768 unsigned int i = 0; 1769 unsigned int num_queues = info->netdev->real_num_tx_queues; 1770 1771 netif_carrier_off(info->netdev); 1772 1773 for (i = 0; i < num_queues && info->queues; ++i) { 1774 struct netfront_queue *queue = &info->queues[i]; 1775 1776 del_timer_sync(&queue->rx_refill_timer); 1777 1778 if (queue->tx_irq && (queue->tx_irq == queue->rx_irq)) 1779 unbind_from_irqhandler(queue->tx_irq, queue); 1780 if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) { 1781 unbind_from_irqhandler(queue->tx_irq, queue); 1782 unbind_from_irqhandler(queue->rx_irq, queue); 1783 } 1784 queue->tx_evtchn = queue->rx_evtchn = 0; 1785 queue->tx_irq = queue->rx_irq = 0; 1786 1787 if (netif_running(info->netdev)) 1788 napi_synchronize(&queue->napi); 1789 1790 xennet_release_tx_bufs(queue); 1791 xennet_release_rx_bufs(queue); 1792 gnttab_free_grant_references(queue->gref_tx_head); 1793 gnttab_free_grant_references(queue->gref_rx_head); 1794 1795 /* End access and free the pages */ 1796 xennet_end_access(queue->tx_ring_ref, queue->tx.sring); 1797 xennet_end_access(queue->rx_ring_ref, queue->rx.sring); 1798 1799 queue->tx_ring_ref = INVALID_GRANT_REF; 1800 queue->rx_ring_ref = INVALID_GRANT_REF; 1801 queue->tx.sring = NULL; 1802 queue->rx.sring = NULL; 1803 1804 page_pool_destroy(queue->page_pool); 1805 } 1806 } 1807 1808 /* 1809 * We are reconnecting to the backend, due to a suspend/resume, or a backend 1810 * driver restart. We tear down our netif structure and recreate it, but 1811 * leave the device-layer structures intact so that this is transparent to the 1812 * rest of the kernel. 1813 */ 1814 static int netfront_resume(struct xenbus_device *dev) 1815 { 1816 struct netfront_info *info = dev_get_drvdata(&dev->dev); 1817 1818 dev_dbg(&dev->dev, "%s\n", dev->nodename); 1819 1820 netif_tx_lock_bh(info->netdev); 1821 netif_device_detach(info->netdev); 1822 netif_tx_unlock_bh(info->netdev); 1823 1824 xennet_disconnect_backend(info); 1825 return 0; 1826 } 1827 1828 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[]) 1829 { 1830 char *s, *e, *macstr; 1831 int i; 1832 1833 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL); 1834 if (IS_ERR(macstr)) 1835 return PTR_ERR(macstr); 1836 1837 for (i = 0; i < ETH_ALEN; i++) { 1838 mac[i] = simple_strtoul(s, &e, 16); 1839 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) { 1840 kfree(macstr); 1841 return -ENOENT; 1842 } 1843 s = e+1; 1844 } 1845 1846 kfree(macstr); 1847 return 0; 1848 } 1849 1850 static int setup_netfront_single(struct netfront_queue *queue) 1851 { 1852 int err; 1853 1854 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn); 1855 if (err < 0) 1856 goto fail; 1857 1858 err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn, 1859 xennet_interrupt, 0, 1860 queue->info->netdev->name, 1861 queue); 1862 if (err < 0) 1863 goto bind_fail; 1864 queue->rx_evtchn = queue->tx_evtchn; 1865 queue->rx_irq = queue->tx_irq = err; 1866 1867 return 0; 1868 1869 bind_fail: 1870 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn); 1871 queue->tx_evtchn = 0; 1872 fail: 1873 return err; 1874 } 1875 1876 static int setup_netfront_split(struct netfront_queue *queue) 1877 { 1878 int err; 1879 1880 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn); 1881 if (err < 0) 1882 goto fail; 1883 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn); 1884 if (err < 0) 1885 goto alloc_rx_evtchn_fail; 1886 1887 snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name), 1888 "%s-tx", queue->name); 1889 err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn, 1890 xennet_tx_interrupt, 0, 1891 queue->tx_irq_name, queue); 1892 if (err < 0) 1893 goto bind_tx_fail; 1894 queue->tx_irq = err; 1895 1896 snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name), 1897 "%s-rx", queue->name); 1898 err = bind_evtchn_to_irqhandler_lateeoi(queue->rx_evtchn, 1899 xennet_rx_interrupt, 0, 1900 queue->rx_irq_name, queue); 1901 if (err < 0) 1902 goto bind_rx_fail; 1903 queue->rx_irq = err; 1904 1905 return 0; 1906 1907 bind_rx_fail: 1908 unbind_from_irqhandler(queue->tx_irq, queue); 1909 queue->tx_irq = 0; 1910 bind_tx_fail: 1911 xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn); 1912 queue->rx_evtchn = 0; 1913 alloc_rx_evtchn_fail: 1914 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn); 1915 queue->tx_evtchn = 0; 1916 fail: 1917 return err; 1918 } 1919 1920 static int setup_netfront(struct xenbus_device *dev, 1921 struct netfront_queue *queue, unsigned int feature_split_evtchn) 1922 { 1923 struct xen_netif_tx_sring *txs; 1924 struct xen_netif_rx_sring *rxs; 1925 int err; 1926 1927 queue->tx_ring_ref = INVALID_GRANT_REF; 1928 queue->rx_ring_ref = INVALID_GRANT_REF; 1929 queue->rx.sring = NULL; 1930 queue->tx.sring = NULL; 1931 1932 err = xenbus_setup_ring(dev, GFP_NOIO | __GFP_HIGH, (void **)&txs, 1933 1, &queue->tx_ring_ref); 1934 if (err) 1935 goto fail; 1936 1937 XEN_FRONT_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE); 1938 1939 err = xenbus_setup_ring(dev, GFP_NOIO | __GFP_HIGH, (void **)&rxs, 1940 1, &queue->rx_ring_ref); 1941 if (err) 1942 goto fail; 1943 1944 XEN_FRONT_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE); 1945 1946 if (feature_split_evtchn) 1947 err = setup_netfront_split(queue); 1948 /* setup single event channel if 1949 * a) feature-split-event-channels == 0 1950 * b) feature-split-event-channels == 1 but failed to setup 1951 */ 1952 if (!feature_split_evtchn || err) 1953 err = setup_netfront_single(queue); 1954 1955 if (err) 1956 goto fail; 1957 1958 return 0; 1959 1960 fail: 1961 xenbus_teardown_ring((void **)&queue->rx.sring, 1, &queue->rx_ring_ref); 1962 xenbus_teardown_ring((void **)&queue->tx.sring, 1, &queue->tx_ring_ref); 1963 1964 return err; 1965 } 1966 1967 /* Queue-specific initialisation 1968 * This used to be done in xennet_create_dev() but must now 1969 * be run per-queue. 1970 */ 1971 static int xennet_init_queue(struct netfront_queue *queue) 1972 { 1973 unsigned short i; 1974 int err = 0; 1975 char *devid; 1976 1977 spin_lock_init(&queue->tx_lock); 1978 spin_lock_init(&queue->rx_lock); 1979 spin_lock_init(&queue->rx_cons_lock); 1980 1981 timer_setup(&queue->rx_refill_timer, rx_refill_timeout, 0); 1982 1983 devid = strrchr(queue->info->xbdev->nodename, '/') + 1; 1984 snprintf(queue->name, sizeof(queue->name), "vif%s-q%u", 1985 devid, queue->id); 1986 1987 /* Initialise tx_skb_freelist as a free chain containing every entry. */ 1988 queue->tx_skb_freelist = 0; 1989 queue->tx_pend_queue = TX_LINK_NONE; 1990 for (i = 0; i < NET_TX_RING_SIZE; i++) { 1991 queue->tx_link[i] = i + 1; 1992 queue->grant_tx_ref[i] = INVALID_GRANT_REF; 1993 queue->grant_tx_page[i] = NULL; 1994 } 1995 queue->tx_link[NET_TX_RING_SIZE - 1] = TX_LINK_NONE; 1996 1997 /* Clear out rx_skbs */ 1998 for (i = 0; i < NET_RX_RING_SIZE; i++) { 1999 queue->rx_skbs[i] = NULL; 2000 queue->grant_rx_ref[i] = INVALID_GRANT_REF; 2001 } 2002 2003 /* A grant for every tx ring slot */ 2004 if (gnttab_alloc_grant_references(NET_TX_RING_SIZE, 2005 &queue->gref_tx_head) < 0) { 2006 pr_alert("can't alloc tx grant refs\n"); 2007 err = -ENOMEM; 2008 goto exit; 2009 } 2010 2011 /* A grant for every rx ring slot */ 2012 if (gnttab_alloc_grant_references(NET_RX_RING_SIZE, 2013 &queue->gref_rx_head) < 0) { 2014 pr_alert("can't alloc rx grant refs\n"); 2015 err = -ENOMEM; 2016 goto exit_free_tx; 2017 } 2018 2019 return 0; 2020 2021 exit_free_tx: 2022 gnttab_free_grant_references(queue->gref_tx_head); 2023 exit: 2024 return err; 2025 } 2026 2027 static int write_queue_xenstore_keys(struct netfront_queue *queue, 2028 struct xenbus_transaction *xbt, int write_hierarchical) 2029 { 2030 /* Write the queue-specific keys into XenStore in the traditional 2031 * way for a single queue, or in a queue subkeys for multiple 2032 * queues. 2033 */ 2034 struct xenbus_device *dev = queue->info->xbdev; 2035 int err; 2036 const char *message; 2037 char *path; 2038 size_t pathsize; 2039 2040 /* Choose the correct place to write the keys */ 2041 if (write_hierarchical) { 2042 pathsize = strlen(dev->nodename) + 10; 2043 path = kzalloc(pathsize, GFP_KERNEL); 2044 if (!path) { 2045 err = -ENOMEM; 2046 message = "out of memory while writing ring references"; 2047 goto error; 2048 } 2049 snprintf(path, pathsize, "%s/queue-%u", 2050 dev->nodename, queue->id); 2051 } else { 2052 path = (char *)dev->nodename; 2053 } 2054 2055 /* Write ring references */ 2056 err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u", 2057 queue->tx_ring_ref); 2058 if (err) { 2059 message = "writing tx-ring-ref"; 2060 goto error; 2061 } 2062 2063 err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u", 2064 queue->rx_ring_ref); 2065 if (err) { 2066 message = "writing rx-ring-ref"; 2067 goto error; 2068 } 2069 2070 /* Write event channels; taking into account both shared 2071 * and split event channel scenarios. 2072 */ 2073 if (queue->tx_evtchn == queue->rx_evtchn) { 2074 /* Shared event channel */ 2075 err = xenbus_printf(*xbt, path, 2076 "event-channel", "%u", queue->tx_evtchn); 2077 if (err) { 2078 message = "writing event-channel"; 2079 goto error; 2080 } 2081 } else { 2082 /* Split event channels */ 2083 err = xenbus_printf(*xbt, path, 2084 "event-channel-tx", "%u", queue->tx_evtchn); 2085 if (err) { 2086 message = "writing event-channel-tx"; 2087 goto error; 2088 } 2089 2090 err = xenbus_printf(*xbt, path, 2091 "event-channel-rx", "%u", queue->rx_evtchn); 2092 if (err) { 2093 message = "writing event-channel-rx"; 2094 goto error; 2095 } 2096 } 2097 2098 if (write_hierarchical) 2099 kfree(path); 2100 return 0; 2101 2102 error: 2103 if (write_hierarchical) 2104 kfree(path); 2105 xenbus_dev_fatal(dev, err, "%s", message); 2106 return err; 2107 } 2108 2109 2110 2111 static int xennet_create_page_pool(struct netfront_queue *queue) 2112 { 2113 int err; 2114 struct page_pool_params pp_params = { 2115 .order = 0, 2116 .flags = 0, 2117 .pool_size = NET_RX_RING_SIZE, 2118 .nid = NUMA_NO_NODE, 2119 .dev = &queue->info->netdev->dev, 2120 .offset = XDP_PACKET_HEADROOM, 2121 .max_len = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM, 2122 }; 2123 2124 queue->page_pool = page_pool_create(&pp_params); 2125 if (IS_ERR(queue->page_pool)) { 2126 err = PTR_ERR(queue->page_pool); 2127 queue->page_pool = NULL; 2128 return err; 2129 } 2130 2131 err = xdp_rxq_info_reg(&queue->xdp_rxq, queue->info->netdev, 2132 queue->id, 0); 2133 if (err) { 2134 netdev_err(queue->info->netdev, "xdp_rxq_info_reg failed\n"); 2135 goto err_free_pp; 2136 } 2137 2138 err = xdp_rxq_info_reg_mem_model(&queue->xdp_rxq, 2139 MEM_TYPE_PAGE_POOL, queue->page_pool); 2140 if (err) { 2141 netdev_err(queue->info->netdev, "xdp_rxq_info_reg_mem_model failed\n"); 2142 goto err_unregister_rxq; 2143 } 2144 return 0; 2145 2146 err_unregister_rxq: 2147 xdp_rxq_info_unreg(&queue->xdp_rxq); 2148 err_free_pp: 2149 page_pool_destroy(queue->page_pool); 2150 queue->page_pool = NULL; 2151 return err; 2152 } 2153 2154 static int xennet_create_queues(struct netfront_info *info, 2155 unsigned int *num_queues) 2156 { 2157 unsigned int i; 2158 int ret; 2159 2160 info->queues = kcalloc(*num_queues, sizeof(struct netfront_queue), 2161 GFP_KERNEL); 2162 if (!info->queues) 2163 return -ENOMEM; 2164 2165 for (i = 0; i < *num_queues; i++) { 2166 struct netfront_queue *queue = &info->queues[i]; 2167 2168 queue->id = i; 2169 queue->info = info; 2170 2171 ret = xennet_init_queue(queue); 2172 if (ret < 0) { 2173 dev_warn(&info->xbdev->dev, 2174 "only created %d queues\n", i); 2175 *num_queues = i; 2176 break; 2177 } 2178 2179 /* use page pool recycling instead of buddy allocator */ 2180 ret = xennet_create_page_pool(queue); 2181 if (ret < 0) { 2182 dev_err(&info->xbdev->dev, "can't allocate page pool\n"); 2183 *num_queues = i; 2184 return ret; 2185 } 2186 2187 netif_napi_add(queue->info->netdev, &queue->napi, 2188 xennet_poll, 64); 2189 if (netif_running(info->netdev)) 2190 napi_enable(&queue->napi); 2191 } 2192 2193 netif_set_real_num_tx_queues(info->netdev, *num_queues); 2194 2195 if (*num_queues == 0) { 2196 dev_err(&info->xbdev->dev, "no queues\n"); 2197 return -EINVAL; 2198 } 2199 return 0; 2200 } 2201 2202 /* Common code used when first setting up, and when resuming. */ 2203 static int talk_to_netback(struct xenbus_device *dev, 2204 struct netfront_info *info) 2205 { 2206 const char *message; 2207 struct xenbus_transaction xbt; 2208 int err; 2209 unsigned int feature_split_evtchn; 2210 unsigned int i = 0; 2211 unsigned int max_queues = 0; 2212 struct netfront_queue *queue = NULL; 2213 unsigned int num_queues = 1; 2214 u8 addr[ETH_ALEN]; 2215 2216 info->netdev->irq = 0; 2217 2218 /* Check if backend supports multiple queues */ 2219 max_queues = xenbus_read_unsigned(info->xbdev->otherend, 2220 "multi-queue-max-queues", 1); 2221 num_queues = min(max_queues, xennet_max_queues); 2222 2223 /* Check feature-split-event-channels */ 2224 feature_split_evtchn = xenbus_read_unsigned(info->xbdev->otherend, 2225 "feature-split-event-channels", 0); 2226 2227 /* Read mac addr. */ 2228 err = xen_net_read_mac(dev, addr); 2229 if (err) { 2230 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename); 2231 goto out_unlocked; 2232 } 2233 eth_hw_addr_set(info->netdev, addr); 2234 2235 info->netback_has_xdp_headroom = xenbus_read_unsigned(info->xbdev->otherend, 2236 "feature-xdp-headroom", 0); 2237 if (info->netback_has_xdp_headroom) { 2238 /* set the current xen-netfront xdp state */ 2239 err = talk_to_netback_xdp(info, info->netfront_xdp_enabled ? 2240 NETBACK_XDP_HEADROOM_ENABLE : 2241 NETBACK_XDP_HEADROOM_DISABLE); 2242 if (err) 2243 goto out_unlocked; 2244 } 2245 2246 rtnl_lock(); 2247 if (info->queues) 2248 xennet_destroy_queues(info); 2249 2250 /* For the case of a reconnect reset the "broken" indicator. */ 2251 info->broken = false; 2252 2253 err = xennet_create_queues(info, &num_queues); 2254 if (err < 0) { 2255 xenbus_dev_fatal(dev, err, "creating queues"); 2256 kfree(info->queues); 2257 info->queues = NULL; 2258 goto out; 2259 } 2260 rtnl_unlock(); 2261 2262 /* Create shared ring, alloc event channel -- for each queue */ 2263 for (i = 0; i < num_queues; ++i) { 2264 queue = &info->queues[i]; 2265 err = setup_netfront(dev, queue, feature_split_evtchn); 2266 if (err) 2267 goto destroy_ring; 2268 } 2269 2270 again: 2271 err = xenbus_transaction_start(&xbt); 2272 if (err) { 2273 xenbus_dev_fatal(dev, err, "starting transaction"); 2274 goto destroy_ring; 2275 } 2276 2277 if (xenbus_exists(XBT_NIL, 2278 info->xbdev->otherend, "multi-queue-max-queues")) { 2279 /* Write the number of queues */ 2280 err = xenbus_printf(xbt, dev->nodename, 2281 "multi-queue-num-queues", "%u", num_queues); 2282 if (err) { 2283 message = "writing multi-queue-num-queues"; 2284 goto abort_transaction_no_dev_fatal; 2285 } 2286 } 2287 2288 if (num_queues == 1) { 2289 err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */ 2290 if (err) 2291 goto abort_transaction_no_dev_fatal; 2292 } else { 2293 /* Write the keys for each queue */ 2294 for (i = 0; i < num_queues; ++i) { 2295 queue = &info->queues[i]; 2296 err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */ 2297 if (err) 2298 goto abort_transaction_no_dev_fatal; 2299 } 2300 } 2301 2302 /* The remaining keys are not queue-specific */ 2303 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u", 2304 1); 2305 if (err) { 2306 message = "writing request-rx-copy"; 2307 goto abort_transaction; 2308 } 2309 2310 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1); 2311 if (err) { 2312 message = "writing feature-rx-notify"; 2313 goto abort_transaction; 2314 } 2315 2316 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1); 2317 if (err) { 2318 message = "writing feature-sg"; 2319 goto abort_transaction; 2320 } 2321 2322 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1); 2323 if (err) { 2324 message = "writing feature-gso-tcpv4"; 2325 goto abort_transaction; 2326 } 2327 2328 err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1"); 2329 if (err) { 2330 message = "writing feature-gso-tcpv6"; 2331 goto abort_transaction; 2332 } 2333 2334 err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload", 2335 "1"); 2336 if (err) { 2337 message = "writing feature-ipv6-csum-offload"; 2338 goto abort_transaction; 2339 } 2340 2341 err = xenbus_transaction_end(xbt, 0); 2342 if (err) { 2343 if (err == -EAGAIN) 2344 goto again; 2345 xenbus_dev_fatal(dev, err, "completing transaction"); 2346 goto destroy_ring; 2347 } 2348 2349 return 0; 2350 2351 abort_transaction: 2352 xenbus_dev_fatal(dev, err, "%s", message); 2353 abort_transaction_no_dev_fatal: 2354 xenbus_transaction_end(xbt, 1); 2355 destroy_ring: 2356 xennet_disconnect_backend(info); 2357 rtnl_lock(); 2358 xennet_destroy_queues(info); 2359 out: 2360 rtnl_unlock(); 2361 out_unlocked: 2362 device_unregister(&dev->dev); 2363 return err; 2364 } 2365 2366 static int xennet_connect(struct net_device *dev) 2367 { 2368 struct netfront_info *np = netdev_priv(dev); 2369 unsigned int num_queues = 0; 2370 int err; 2371 unsigned int j = 0; 2372 struct netfront_queue *queue = NULL; 2373 2374 if (!xenbus_read_unsigned(np->xbdev->otherend, "feature-rx-copy", 0)) { 2375 dev_info(&dev->dev, 2376 "backend does not support copying receive path\n"); 2377 return -ENODEV; 2378 } 2379 2380 err = talk_to_netback(np->xbdev, np); 2381 if (err) 2382 return err; 2383 if (np->netback_has_xdp_headroom) 2384 pr_info("backend supports XDP headroom\n"); 2385 2386 /* talk_to_netback() sets the correct number of queues */ 2387 num_queues = dev->real_num_tx_queues; 2388 2389 if (dev->reg_state == NETREG_UNINITIALIZED) { 2390 err = register_netdev(dev); 2391 if (err) { 2392 pr_warn("%s: register_netdev err=%d\n", __func__, err); 2393 device_unregister(&np->xbdev->dev); 2394 return err; 2395 } 2396 } 2397 2398 rtnl_lock(); 2399 netdev_update_features(dev); 2400 rtnl_unlock(); 2401 2402 /* 2403 * All public and private state should now be sane. Get 2404 * ready to start sending and receiving packets and give the driver 2405 * domain a kick because we've probably just requeued some 2406 * packets. 2407 */ 2408 netif_tx_lock_bh(np->netdev); 2409 netif_device_attach(np->netdev); 2410 netif_tx_unlock_bh(np->netdev); 2411 2412 netif_carrier_on(np->netdev); 2413 for (j = 0; j < num_queues; ++j) { 2414 queue = &np->queues[j]; 2415 2416 notify_remote_via_irq(queue->tx_irq); 2417 if (queue->tx_irq != queue->rx_irq) 2418 notify_remote_via_irq(queue->rx_irq); 2419 2420 spin_lock_irq(&queue->tx_lock); 2421 xennet_tx_buf_gc(queue); 2422 spin_unlock_irq(&queue->tx_lock); 2423 2424 spin_lock_bh(&queue->rx_lock); 2425 xennet_alloc_rx_buffers(queue); 2426 spin_unlock_bh(&queue->rx_lock); 2427 } 2428 2429 return 0; 2430 } 2431 2432 /* 2433 * Callback received when the backend's state changes. 2434 */ 2435 static void netback_changed(struct xenbus_device *dev, 2436 enum xenbus_state backend_state) 2437 { 2438 struct netfront_info *np = dev_get_drvdata(&dev->dev); 2439 struct net_device *netdev = np->netdev; 2440 2441 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state)); 2442 2443 wake_up_all(&module_wq); 2444 2445 switch (backend_state) { 2446 case XenbusStateInitialising: 2447 case XenbusStateInitialised: 2448 case XenbusStateReconfiguring: 2449 case XenbusStateReconfigured: 2450 case XenbusStateUnknown: 2451 break; 2452 2453 case XenbusStateInitWait: 2454 if (dev->state != XenbusStateInitialising) 2455 break; 2456 if (xennet_connect(netdev) != 0) 2457 break; 2458 xenbus_switch_state(dev, XenbusStateConnected); 2459 break; 2460 2461 case XenbusStateConnected: 2462 netdev_notify_peers(netdev); 2463 break; 2464 2465 case XenbusStateClosed: 2466 if (dev->state == XenbusStateClosed) 2467 break; 2468 fallthrough; /* Missed the backend's CLOSING state */ 2469 case XenbusStateClosing: 2470 xenbus_frontend_closed(dev); 2471 break; 2472 } 2473 } 2474 2475 static const struct xennet_stat { 2476 char name[ETH_GSTRING_LEN]; 2477 u16 offset; 2478 } xennet_stats[] = { 2479 { 2480 "rx_gso_checksum_fixup", 2481 offsetof(struct netfront_info, rx_gso_checksum_fixup) 2482 }, 2483 }; 2484 2485 static int xennet_get_sset_count(struct net_device *dev, int string_set) 2486 { 2487 switch (string_set) { 2488 case ETH_SS_STATS: 2489 return ARRAY_SIZE(xennet_stats); 2490 default: 2491 return -EINVAL; 2492 } 2493 } 2494 2495 static void xennet_get_ethtool_stats(struct net_device *dev, 2496 struct ethtool_stats *stats, u64 * data) 2497 { 2498 void *np = netdev_priv(dev); 2499 int i; 2500 2501 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++) 2502 data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset)); 2503 } 2504 2505 static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data) 2506 { 2507 int i; 2508 2509 switch (stringset) { 2510 case ETH_SS_STATS: 2511 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++) 2512 memcpy(data + i * ETH_GSTRING_LEN, 2513 xennet_stats[i].name, ETH_GSTRING_LEN); 2514 break; 2515 } 2516 } 2517 2518 static const struct ethtool_ops xennet_ethtool_ops = 2519 { 2520 .get_link = ethtool_op_get_link, 2521 2522 .get_sset_count = xennet_get_sset_count, 2523 .get_ethtool_stats = xennet_get_ethtool_stats, 2524 .get_strings = xennet_get_strings, 2525 .get_ts_info = ethtool_op_get_ts_info, 2526 }; 2527 2528 #ifdef CONFIG_SYSFS 2529 static ssize_t show_rxbuf(struct device *dev, 2530 struct device_attribute *attr, char *buf) 2531 { 2532 return sprintf(buf, "%lu\n", NET_RX_RING_SIZE); 2533 } 2534 2535 static ssize_t store_rxbuf(struct device *dev, 2536 struct device_attribute *attr, 2537 const char *buf, size_t len) 2538 { 2539 char *endp; 2540 2541 if (!capable(CAP_NET_ADMIN)) 2542 return -EPERM; 2543 2544 simple_strtoul(buf, &endp, 0); 2545 if (endp == buf) 2546 return -EBADMSG; 2547 2548 /* rxbuf_min and rxbuf_max are no longer configurable. */ 2549 2550 return len; 2551 } 2552 2553 static DEVICE_ATTR(rxbuf_min, 0644, show_rxbuf, store_rxbuf); 2554 static DEVICE_ATTR(rxbuf_max, 0644, show_rxbuf, store_rxbuf); 2555 static DEVICE_ATTR(rxbuf_cur, 0444, show_rxbuf, NULL); 2556 2557 static struct attribute *xennet_dev_attrs[] = { 2558 &dev_attr_rxbuf_min.attr, 2559 &dev_attr_rxbuf_max.attr, 2560 &dev_attr_rxbuf_cur.attr, 2561 NULL 2562 }; 2563 2564 static const struct attribute_group xennet_dev_group = { 2565 .attrs = xennet_dev_attrs 2566 }; 2567 #endif /* CONFIG_SYSFS */ 2568 2569 static void xennet_bus_close(struct xenbus_device *dev) 2570 { 2571 int ret; 2572 2573 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed) 2574 return; 2575 do { 2576 xenbus_switch_state(dev, XenbusStateClosing); 2577 ret = wait_event_timeout(module_wq, 2578 xenbus_read_driver_state(dev->otherend) == 2579 XenbusStateClosing || 2580 xenbus_read_driver_state(dev->otherend) == 2581 XenbusStateClosed || 2582 xenbus_read_driver_state(dev->otherend) == 2583 XenbusStateUnknown, 2584 XENNET_TIMEOUT); 2585 } while (!ret); 2586 2587 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed) 2588 return; 2589 2590 do { 2591 xenbus_switch_state(dev, XenbusStateClosed); 2592 ret = wait_event_timeout(module_wq, 2593 xenbus_read_driver_state(dev->otherend) == 2594 XenbusStateClosed || 2595 xenbus_read_driver_state(dev->otherend) == 2596 XenbusStateUnknown, 2597 XENNET_TIMEOUT); 2598 } while (!ret); 2599 } 2600 2601 static int xennet_remove(struct xenbus_device *dev) 2602 { 2603 struct netfront_info *info = dev_get_drvdata(&dev->dev); 2604 2605 xennet_bus_close(dev); 2606 xennet_disconnect_backend(info); 2607 2608 if (info->netdev->reg_state == NETREG_REGISTERED) 2609 unregister_netdev(info->netdev); 2610 2611 if (info->queues) { 2612 rtnl_lock(); 2613 xennet_destroy_queues(info); 2614 rtnl_unlock(); 2615 } 2616 xennet_free_netdev(info->netdev); 2617 2618 return 0; 2619 } 2620 2621 static const struct xenbus_device_id netfront_ids[] = { 2622 { "vif" }, 2623 { "" } 2624 }; 2625 2626 static struct xenbus_driver netfront_driver = { 2627 .ids = netfront_ids, 2628 .probe = netfront_probe, 2629 .remove = xennet_remove, 2630 .resume = netfront_resume, 2631 .otherend_changed = netback_changed, 2632 }; 2633 2634 static int __init netif_init(void) 2635 { 2636 if (!xen_domain()) 2637 return -ENODEV; 2638 2639 if (!xen_has_pv_nic_devices()) 2640 return -ENODEV; 2641 2642 pr_info("Initialising Xen virtual ethernet driver\n"); 2643 2644 /* Allow as many queues as there are CPUs inut max. 8 if user has not 2645 * specified a value. 2646 */ 2647 if (xennet_max_queues == 0) 2648 xennet_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT, 2649 num_online_cpus()); 2650 2651 return xenbus_register_frontend(&netfront_driver); 2652 } 2653 module_init(netif_init); 2654 2655 2656 static void __exit netif_exit(void) 2657 { 2658 xenbus_unregister_driver(&netfront_driver); 2659 } 2660 module_exit(netif_exit); 2661 2662 MODULE_DESCRIPTION("Xen virtual network device frontend"); 2663 MODULE_LICENSE("GPL"); 2664 MODULE_ALIAS("xen:vif"); 2665 MODULE_ALIAS("xennet"); 2666