1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (C) 2013-2016 Vincenzo Maffione 5 * Copyright (C) 2013-2016 Luigi Rizzo 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 /* 31 * This module implements netmap support on top of standard, 32 * unmodified device drivers. 33 * 34 * A NIOCREGIF request is handled here if the device does not 35 * have native support. TX and RX rings are emulated as follows: 36 * 37 * NIOCREGIF 38 * We preallocate a block of TX mbufs (roughly as many as 39 * tx descriptors; the number is not critical) to speed up 40 * operation during transmissions. The refcount on most of 41 * these buffers is artificially bumped up so we can recycle 42 * them more easily. Also, the destructor is intercepted 43 * so we use it as an interrupt notification to wake up 44 * processes blocked on a poll(). 45 * 46 * For each receive ring we allocate one "struct mbq" 47 * (an mbuf tailq plus a spinlock). We intercept packets 48 * (through if_input) 49 * on the receive path and put them in the mbq from which 50 * netmap receive routines can grab them. 51 * 52 * TX: 53 * in the generic_txsync() routine, netmap buffers are copied 54 * (or linked, in a future) to the preallocated mbufs 55 * and pushed to the transmit queue. Some of these mbufs 56 * (those with NS_REPORT, or otherwise every half ring) 57 * have the refcount=1, others have refcount=2. 58 * When the destructor is invoked, we take that as 59 * a notification that all mbufs up to that one in 60 * the specific ring have been completed, and generate 61 * the equivalent of a transmit interrupt. 62 * 63 * RX: 64 * 65 */ 66 67 #ifdef __FreeBSD__ 68 69 #include <sys/cdefs.h> /* prerequisite */ 70 __FBSDID("$FreeBSD$"); 71 72 #include <sys/types.h> 73 #include <sys/errno.h> 74 #include <sys/malloc.h> 75 #include <sys/lock.h> /* PROT_EXEC */ 76 #include <sys/rwlock.h> 77 #include <sys/socket.h> /* sockaddrs */ 78 #include <sys/selinfo.h> 79 #include <net/if.h> 80 #include <net/if_types.h> 81 #include <net/if_var.h> 82 #include <machine/bus.h> /* bus_dmamap_* in netmap_kern.h */ 83 84 #include <net/netmap.h> 85 #include <dev/netmap/netmap_kern.h> 86 #include <dev/netmap/netmap_mem2.h> 87 88 #define MBUF_RXQ(m) ((m)->m_pkthdr.flowid) 89 #define smp_mb() 90 91 #elif defined _WIN32 92 93 #include "win_glue.h" 94 95 #define MBUF_TXQ(m) 0//((m)->m_pkthdr.flowid) 96 #define MBUF_RXQ(m) 0//((m)->m_pkthdr.flowid) 97 #define smp_mb() //XXX: to be correctly defined 98 99 #else /* linux */ 100 101 #include "bsd_glue.h" 102 103 #include <linux/ethtool.h> /* struct ethtool_ops, get_ringparam */ 104 #include <linux/hrtimer.h> 105 106 static inline struct mbuf * 107 nm_os_get_mbuf(struct ifnet *ifp, int len) 108 { 109 return alloc_skb(ifp->needed_headroom + len + 110 ifp->needed_tailroom, GFP_ATOMIC); 111 } 112 113 #endif /* linux */ 114 115 116 /* Common headers. */ 117 #include <net/netmap.h> 118 #include <dev/netmap/netmap_kern.h> 119 #include <dev/netmap/netmap_mem2.h> 120 121 122 #define for_each_kring_n(_i, _k, _karr, _n) \ 123 for ((_k)=*(_karr), (_i) = 0; (_i) < (_n); (_i)++, (_k) = (_karr)[(_i)]) 124 125 #define for_each_tx_kring(_i, _k, _na) \ 126 for_each_kring_n(_i, _k, (_na)->tx_rings, (_na)->num_tx_rings) 127 #define for_each_tx_kring_h(_i, _k, _na) \ 128 for_each_kring_n(_i, _k, (_na)->tx_rings, (_na)->num_tx_rings + 1) 129 130 #define for_each_rx_kring(_i, _k, _na) \ 131 for_each_kring_n(_i, _k, (_na)->rx_rings, (_na)->num_rx_rings) 132 #define for_each_rx_kring_h(_i, _k, _na) \ 133 for_each_kring_n(_i, _k, (_na)->rx_rings, (_na)->num_rx_rings + 1) 134 135 136 /* ======================== PERFORMANCE STATISTICS =========================== */ 137 138 #ifdef RATE_GENERIC 139 #define IFRATE(x) x 140 struct rate_stats { 141 unsigned long txpkt; 142 unsigned long txsync; 143 unsigned long txirq; 144 unsigned long txrepl; 145 unsigned long txdrop; 146 unsigned long rxpkt; 147 unsigned long rxirq; 148 unsigned long rxsync; 149 }; 150 151 struct rate_context { 152 unsigned refcount; 153 struct timer_list timer; 154 struct rate_stats new; 155 struct rate_stats old; 156 }; 157 158 #define RATE_PRINTK(_NAME_) \ 159 printk( #_NAME_ " = %lu Hz\n", (cur._NAME_ - ctx->old._NAME_)/RATE_PERIOD); 160 #define RATE_PERIOD 2 161 static void rate_callback(unsigned long arg) 162 { 163 struct rate_context * ctx = (struct rate_context *)arg; 164 struct rate_stats cur = ctx->new; 165 int r; 166 167 RATE_PRINTK(txpkt); 168 RATE_PRINTK(txsync); 169 RATE_PRINTK(txirq); 170 RATE_PRINTK(txrepl); 171 RATE_PRINTK(txdrop); 172 RATE_PRINTK(rxpkt); 173 RATE_PRINTK(rxsync); 174 RATE_PRINTK(rxirq); 175 printk("\n"); 176 177 ctx->old = cur; 178 r = mod_timer(&ctx->timer, jiffies + 179 msecs_to_jiffies(RATE_PERIOD * 1000)); 180 if (unlikely(r)) 181 nm_prerr("mod_timer() failed"); 182 } 183 184 static struct rate_context rate_ctx; 185 186 void generic_rate(int txp, int txs, int txi, int rxp, int rxs, int rxi) 187 { 188 if (txp) rate_ctx.new.txpkt++; 189 if (txs) rate_ctx.new.txsync++; 190 if (txi) rate_ctx.new.txirq++; 191 if (rxp) rate_ctx.new.rxpkt++; 192 if (rxs) rate_ctx.new.rxsync++; 193 if (rxi) rate_ctx.new.rxirq++; 194 } 195 196 #else /* !RATE */ 197 #define IFRATE(x) 198 #endif /* !RATE */ 199 200 201 /* ========== GENERIC (EMULATED) NETMAP ADAPTER SUPPORT ============= */ 202 203 /* 204 * Wrapper used by the generic adapter layer to notify 205 * the poller threads. Differently from netmap_rx_irq(), we check 206 * only NAF_NETMAP_ON instead of NAF_NATIVE_ON to enable the irq. 207 */ 208 void 209 netmap_generic_irq(struct netmap_adapter *na, u_int q, u_int *work_done) 210 { 211 if (unlikely(!nm_netmap_on(na))) 212 return; 213 214 netmap_common_irq(na, q, work_done); 215 #ifdef RATE_GENERIC 216 if (work_done) 217 rate_ctx.new.rxirq++; 218 else 219 rate_ctx.new.txirq++; 220 #endif /* RATE_GENERIC */ 221 } 222 223 static int 224 generic_netmap_unregister(struct netmap_adapter *na) 225 { 226 struct netmap_generic_adapter *gna = (struct netmap_generic_adapter *)na; 227 struct netmap_kring *kring = NULL; 228 int i, r; 229 230 if (na->active_fds == 0) { 231 na->na_flags &= ~NAF_NETMAP_ON; 232 233 /* Stop intercepting packets on the RX path. */ 234 nm_os_catch_rx(gna, 0); 235 236 /* Release packet steering control. */ 237 nm_os_catch_tx(gna, 0); 238 } 239 240 for_each_rx_kring_h(r, kring, na) { 241 if (nm_kring_pending_off(kring)) { 242 nm_prinf("Emulated adapter: ring '%s' deactivated", kring->name); 243 kring->nr_mode = NKR_NETMAP_OFF; 244 } 245 } 246 for_each_tx_kring_h(r, kring, na) { 247 if (nm_kring_pending_off(kring)) { 248 kring->nr_mode = NKR_NETMAP_OFF; 249 nm_prinf("Emulated adapter: ring '%s' deactivated", kring->name); 250 } 251 } 252 253 for_each_rx_kring(r, kring, na) { 254 /* Free the mbufs still pending in the RX queues, 255 * that did not end up into the corresponding netmap 256 * RX rings. */ 257 mbq_safe_purge(&kring->rx_queue); 258 nm_os_mitigation_cleanup(&gna->mit[r]); 259 } 260 261 /* Decrement reference counter for the mbufs in the 262 * TX pools. These mbufs can be still pending in drivers, 263 * (e.g. this happens with virtio-net driver, which 264 * does lazy reclaiming of transmitted mbufs). */ 265 for_each_tx_kring(r, kring, na) { 266 /* We must remove the destructor on the TX event, 267 * because the destructor invokes netmap code, and 268 * the netmap module may disappear before the 269 * TX event is consumed. */ 270 mtx_lock_spin(&kring->tx_event_lock); 271 if (kring->tx_event) { 272 SET_MBUF_DESTRUCTOR(kring->tx_event, NULL); 273 } 274 kring->tx_event = NULL; 275 mtx_unlock_spin(&kring->tx_event_lock); 276 } 277 278 if (na->active_fds == 0) { 279 nm_os_free(gna->mit); 280 281 for_each_rx_kring(r, kring, na) { 282 mbq_safe_fini(&kring->rx_queue); 283 } 284 285 for_each_tx_kring(r, kring, na) { 286 mtx_destroy(&kring->tx_event_lock); 287 if (kring->tx_pool == NULL) { 288 continue; 289 } 290 291 for (i=0; i<na->num_tx_desc; i++) { 292 if (kring->tx_pool[i]) { 293 m_freem(kring->tx_pool[i]); 294 } 295 } 296 nm_os_free(kring->tx_pool); 297 kring->tx_pool = NULL; 298 } 299 300 #ifdef RATE_GENERIC 301 if (--rate_ctx.refcount == 0) { 302 nm_prinf("del_timer()"); 303 del_timer(&rate_ctx.timer); 304 } 305 #endif 306 nm_prinf("Emulated adapter for %s deactivated", na->name); 307 } 308 309 return 0; 310 } 311 312 /* Enable/disable netmap mode for a generic network interface. */ 313 static int 314 generic_netmap_register(struct netmap_adapter *na, int enable) 315 { 316 struct netmap_generic_adapter *gna = (struct netmap_generic_adapter *)na; 317 struct netmap_kring *kring = NULL; 318 int error; 319 int i, r; 320 321 if (!na) { 322 return EINVAL; 323 } 324 325 if (!enable) { 326 /* This is actually an unregif. */ 327 return generic_netmap_unregister(na); 328 } 329 330 if (na->active_fds == 0) { 331 nm_prinf("Emulated adapter for %s activated", na->name); 332 /* Do all memory allocations when (na->active_fds == 0), to 333 * simplify error management. */ 334 335 /* Allocate memory for mitigation support on all the rx queues. */ 336 gna->mit = nm_os_malloc(na->num_rx_rings * sizeof(struct nm_generic_mit)); 337 if (!gna->mit) { 338 nm_prerr("mitigation allocation failed"); 339 error = ENOMEM; 340 goto out; 341 } 342 343 for_each_rx_kring(r, kring, na) { 344 /* Init mitigation support. */ 345 nm_os_mitigation_init(&gna->mit[r], r, na); 346 347 /* Initialize the rx queue, as generic_rx_handler() can 348 * be called as soon as nm_os_catch_rx() returns. 349 */ 350 mbq_safe_init(&kring->rx_queue); 351 } 352 353 /* 354 * Prepare mbuf pools (parallel to the tx rings), for packet 355 * transmission. Don't preallocate the mbufs here, it's simpler 356 * to leave this task to txsync. 357 */ 358 for_each_tx_kring(r, kring, na) { 359 kring->tx_pool = NULL; 360 } 361 for_each_tx_kring(r, kring, na) { 362 kring->tx_pool = 363 nm_os_malloc(na->num_tx_desc * sizeof(struct mbuf *)); 364 if (!kring->tx_pool) { 365 nm_prerr("tx_pool allocation failed"); 366 error = ENOMEM; 367 goto free_tx_pools; 368 } 369 mtx_init(&kring->tx_event_lock, "tx_event_lock", 370 NULL, MTX_SPIN); 371 } 372 } 373 374 for_each_rx_kring_h(r, kring, na) { 375 if (nm_kring_pending_on(kring)) { 376 nm_prinf("Emulated adapter: ring '%s' activated", kring->name); 377 kring->nr_mode = NKR_NETMAP_ON; 378 } 379 380 } 381 for_each_tx_kring_h(r, kring, na) { 382 if (nm_kring_pending_on(kring)) { 383 nm_prinf("Emulated adapter: ring '%s' activated", kring->name); 384 kring->nr_mode = NKR_NETMAP_ON; 385 } 386 } 387 388 for_each_tx_kring(r, kring, na) { 389 /* Initialize tx_pool and tx_event. */ 390 for (i=0; i<na->num_tx_desc; i++) { 391 kring->tx_pool[i] = NULL; 392 } 393 394 kring->tx_event = NULL; 395 } 396 397 if (na->active_fds == 0) { 398 /* Prepare to intercept incoming traffic. */ 399 error = nm_os_catch_rx(gna, 1); 400 if (error) { 401 nm_prerr("nm_os_catch_rx(1) failed (%d)", error); 402 goto free_tx_pools; 403 } 404 405 /* Let netmap control the packet steering. */ 406 error = nm_os_catch_tx(gna, 1); 407 if (error) { 408 nm_prerr("nm_os_catch_tx(1) failed (%d)", error); 409 goto catch_rx; 410 } 411 412 na->na_flags |= NAF_NETMAP_ON; 413 414 #ifdef RATE_GENERIC 415 if (rate_ctx.refcount == 0) { 416 nm_prinf("setup_timer()"); 417 memset(&rate_ctx, 0, sizeof(rate_ctx)); 418 setup_timer(&rate_ctx.timer, &rate_callback, (unsigned long)&rate_ctx); 419 if (mod_timer(&rate_ctx.timer, jiffies + msecs_to_jiffies(1500))) { 420 nm_prerr("Error: mod_timer()"); 421 } 422 } 423 rate_ctx.refcount++; 424 #endif /* RATE */ 425 } 426 427 return 0; 428 429 /* Here (na->active_fds == 0) holds. */ 430 catch_rx: 431 nm_os_catch_rx(gna, 0); 432 free_tx_pools: 433 for_each_tx_kring(r, kring, na) { 434 mtx_destroy(&kring->tx_event_lock); 435 if (kring->tx_pool == NULL) { 436 continue; 437 } 438 nm_os_free(kring->tx_pool); 439 kring->tx_pool = NULL; 440 } 441 for_each_rx_kring(r, kring, na) { 442 mbq_safe_fini(&kring->rx_queue); 443 } 444 nm_os_free(gna->mit); 445 out: 446 447 return error; 448 } 449 450 /* 451 * Callback invoked when the device driver frees an mbuf used 452 * by netmap to transmit a packet. This usually happens when 453 * the NIC notifies the driver that transmission is completed. 454 */ 455 static void 456 generic_mbuf_destructor(struct mbuf *m) 457 { 458 struct netmap_adapter *na = NA(GEN_TX_MBUF_IFP(m)); 459 struct netmap_kring *kring; 460 unsigned int r = MBUF_TXQ(m); 461 unsigned int r_orig = r; 462 463 if (unlikely(!nm_netmap_on(na) || r >= na->num_tx_rings)) { 464 nm_prerr("Error: no netmap adapter on device %p", 465 GEN_TX_MBUF_IFP(m)); 466 return; 467 } 468 469 /* 470 * First, clear the event mbuf. 471 * In principle, the event 'm' should match the one stored 472 * on ring 'r'. However we check it explicitely to stay 473 * safe against lower layers (qdisc, driver, etc.) changing 474 * MBUF_TXQ(m) under our feet. If the match is not found 475 * on 'r', we try to see if it belongs to some other ring. 476 */ 477 for (;;) { 478 bool match = false; 479 480 kring = na->tx_rings[r]; 481 mtx_lock_spin(&kring->tx_event_lock); 482 if (kring->tx_event == m) { 483 kring->tx_event = NULL; 484 match = true; 485 } 486 mtx_unlock_spin(&kring->tx_event_lock); 487 488 if (match) { 489 if (r != r_orig) { 490 nm_prlim(1, "event %p migrated: ring %u --> %u", 491 m, r_orig, r); 492 } 493 break; 494 } 495 496 if (++r == na->num_tx_rings) r = 0; 497 498 if (r == r_orig) { 499 nm_prlim(1, "Cannot match event %p", m); 500 return; 501 } 502 } 503 504 /* Second, wake up clients. They will reclaim the event through 505 * txsync. */ 506 netmap_generic_irq(na, r, NULL); 507 #ifdef __FreeBSD__ 508 #if __FreeBSD_version <= 1200050 509 void_mbuf_dtor(m, NULL, NULL); 510 #else /* __FreeBSD_version >= 1200051 */ 511 void_mbuf_dtor(m); 512 #endif /* __FreeBSD_version >= 1200051 */ 513 #endif 514 } 515 516 /* Record completed transmissions and update hwtail. 517 * 518 * The oldest tx buffer not yet completed is at nr_hwtail + 1, 519 * nr_hwcur is the first unsent buffer. 520 */ 521 static u_int 522 generic_netmap_tx_clean(struct netmap_kring *kring, int txqdisc) 523 { 524 u_int const lim = kring->nkr_num_slots - 1; 525 u_int nm_i = nm_next(kring->nr_hwtail, lim); 526 u_int hwcur = kring->nr_hwcur; 527 u_int n = 0; 528 struct mbuf **tx_pool = kring->tx_pool; 529 530 nm_prdis("hwcur = %d, hwtail = %d", kring->nr_hwcur, kring->nr_hwtail); 531 532 while (nm_i != hwcur) { /* buffers not completed */ 533 struct mbuf *m = tx_pool[nm_i]; 534 535 if (txqdisc) { 536 if (m == NULL) { 537 /* Nothing to do, this is going 538 * to be replenished. */ 539 nm_prlim(3, "Is this happening?"); 540 541 } else if (MBUF_QUEUED(m)) { 542 break; /* Not dequeued yet. */ 543 544 } else if (MBUF_REFCNT(m) != 1) { 545 /* This mbuf has been dequeued but is still busy 546 * (refcount is 2). 547 * Leave it to the driver and replenish. */ 548 m_freem(m); 549 tx_pool[nm_i] = NULL; 550 } 551 552 } else { 553 if (unlikely(m == NULL)) { 554 int event_consumed; 555 556 /* This slot was used to place an event. */ 557 mtx_lock_spin(&kring->tx_event_lock); 558 event_consumed = (kring->tx_event == NULL); 559 mtx_unlock_spin(&kring->tx_event_lock); 560 if (!event_consumed) { 561 /* The event has not been consumed yet, 562 * still busy in the driver. */ 563 break; 564 } 565 /* The event has been consumed, we can go 566 * ahead. */ 567 568 } else if (MBUF_REFCNT(m) != 1) { 569 /* This mbuf is still busy: its refcnt is 2. */ 570 break; 571 } 572 } 573 574 n++; 575 nm_i = nm_next(nm_i, lim); 576 } 577 kring->nr_hwtail = nm_prev(nm_i, lim); 578 nm_prdis("tx completed [%d] -> hwtail %d", n, kring->nr_hwtail); 579 580 return n; 581 } 582 583 /* Compute a slot index in the middle between inf and sup. */ 584 static inline u_int 585 ring_middle(u_int inf, u_int sup, u_int lim) 586 { 587 u_int n = lim + 1; 588 u_int e; 589 590 if (sup >= inf) { 591 e = (sup + inf) / 2; 592 } else { /* wrap around */ 593 e = (sup + n + inf) / 2; 594 if (e >= n) { 595 e -= n; 596 } 597 } 598 599 if (unlikely(e >= n)) { 600 nm_prerr("This cannot happen"); 601 e = 0; 602 } 603 604 return e; 605 } 606 607 static void 608 generic_set_tx_event(struct netmap_kring *kring, u_int hwcur) 609 { 610 u_int lim = kring->nkr_num_slots - 1; 611 struct mbuf *m; 612 u_int e; 613 u_int ntc = nm_next(kring->nr_hwtail, lim); /* next to clean */ 614 615 if (ntc == hwcur) { 616 return; /* all buffers are free */ 617 } 618 619 /* 620 * We have pending packets in the driver between hwtail+1 621 * and hwcur, and we have to chose one of these slot to 622 * generate a notification. 623 * There is a race but this is only called within txsync which 624 * does a double check. 625 */ 626 #if 0 627 /* Choose a slot in the middle, so that we don't risk ending 628 * up in a situation where the client continuously wake up, 629 * fills one or a few TX slots and go to sleep again. */ 630 e = ring_middle(ntc, hwcur, lim); 631 #else 632 /* Choose the first pending slot, to be safe against driver 633 * reordering mbuf transmissions. */ 634 e = ntc; 635 #endif 636 637 m = kring->tx_pool[e]; 638 if (m == NULL) { 639 /* An event is already in place. */ 640 return; 641 } 642 643 mtx_lock_spin(&kring->tx_event_lock); 644 if (kring->tx_event) { 645 /* An event is already in place. */ 646 mtx_unlock_spin(&kring->tx_event_lock); 647 return; 648 } 649 650 SET_MBUF_DESTRUCTOR(m, generic_mbuf_destructor); 651 kring->tx_event = m; 652 mtx_unlock_spin(&kring->tx_event_lock); 653 654 kring->tx_pool[e] = NULL; 655 656 nm_prdis("Request Event at %d mbuf %p refcnt %d", e, m, m ? MBUF_REFCNT(m) : -2 ); 657 658 /* Decrement the refcount. This will free it if we lose the race 659 * with the driver. */ 660 m_freem(m); 661 smp_mb(); 662 } 663 664 665 /* 666 * generic_netmap_txsync() transforms netmap buffers into mbufs 667 * and passes them to the standard device driver 668 * (ndo_start_xmit() or ifp->if_transmit() ). 669 * On linux this is not done directly, but using dev_queue_xmit(), 670 * since it implements the TX flow control (and takes some locks). 671 */ 672 static int 673 generic_netmap_txsync(struct netmap_kring *kring, int flags) 674 { 675 struct netmap_adapter *na = kring->na; 676 struct netmap_generic_adapter *gna = (struct netmap_generic_adapter *)na; 677 struct ifnet *ifp = na->ifp; 678 struct netmap_ring *ring = kring->ring; 679 u_int nm_i; /* index into the netmap ring */ // j 680 u_int const lim = kring->nkr_num_slots - 1; 681 u_int const head = kring->rhead; 682 u_int ring_nr = kring->ring_id; 683 684 IFRATE(rate_ctx.new.txsync++); 685 686 rmb(); 687 688 /* 689 * First part: process new packets to send. 690 */ 691 nm_i = kring->nr_hwcur; 692 if (nm_i != head) { /* we have new packets to send */ 693 struct nm_os_gen_arg a; 694 u_int event = -1; 695 696 if (gna->txqdisc && nm_kr_txempty(kring)) { 697 /* In txqdisc mode, we ask for a delayed notification, 698 * but only when cur == hwtail, which means that the 699 * client is going to block. */ 700 event = ring_middle(nm_i, head, lim); 701 nm_prdis("Place txqdisc event (hwcur=%u,event=%u," 702 "head=%u,hwtail=%u)", nm_i, event, head, 703 kring->nr_hwtail); 704 } 705 706 a.ifp = ifp; 707 a.ring_nr = ring_nr; 708 a.head = a.tail = NULL; 709 710 while (nm_i != head) { 711 struct netmap_slot *slot = &ring->slot[nm_i]; 712 u_int len = slot->len; 713 void *addr = NMB(na, slot); 714 /* device-specific */ 715 struct mbuf *m; 716 int tx_ret; 717 718 NM_CHECK_ADDR_LEN(na, addr, len); 719 720 /* Tale a mbuf from the tx pool (replenishing the pool 721 * entry if necessary) and copy in the user packet. */ 722 m = kring->tx_pool[nm_i]; 723 if (unlikely(m == NULL)) { 724 kring->tx_pool[nm_i] = m = 725 nm_os_get_mbuf(ifp, NETMAP_BUF_SIZE(na)); 726 if (m == NULL) { 727 nm_prlim(2, "Failed to replenish mbuf"); 728 /* Here we could schedule a timer which 729 * retries to replenish after a while, 730 * and notifies the client when it 731 * manages to replenish some slots. In 732 * any case we break early to avoid 733 * crashes. */ 734 break; 735 } 736 IFRATE(rate_ctx.new.txrepl++); 737 } 738 739 a.m = m; 740 a.addr = addr; 741 a.len = len; 742 a.qevent = (nm_i == event); 743 /* When not in txqdisc mode, we should ask 744 * notifications when NS_REPORT is set, or roughly 745 * every half ring. To optimize this, we set a 746 * notification event when the client runs out of 747 * TX ring space, or when transmission fails. In 748 * the latter case we also break early. 749 */ 750 tx_ret = nm_os_generic_xmit_frame(&a); 751 if (unlikely(tx_ret)) { 752 if (!gna->txqdisc) { 753 /* 754 * No room for this mbuf in the device driver. 755 * Request a notification FOR A PREVIOUS MBUF, 756 * then call generic_netmap_tx_clean(kring) to do the 757 * double check and see if we can free more buffers. 758 * If there is space continue, else break; 759 * NOTE: the double check is necessary if the problem 760 * occurs in the txsync call after selrecord(). 761 * Also, we need some way to tell the caller that not 762 * all buffers were queued onto the device (this was 763 * not a problem with native netmap driver where space 764 * is preallocated). The bridge has a similar problem 765 * and we solve it there by dropping the excess packets. 766 */ 767 generic_set_tx_event(kring, nm_i); 768 if (generic_netmap_tx_clean(kring, gna->txqdisc)) { 769 /* space now available */ 770 continue; 771 } else { 772 break; 773 } 774 } 775 776 /* In txqdisc mode, the netmap-aware qdisc 777 * queue has the same length as the number of 778 * netmap slots (N). Since tail is advanced 779 * only when packets are dequeued, qdisc 780 * queue overrun cannot happen, so 781 * nm_os_generic_xmit_frame() did not fail 782 * because of that. 783 * However, packets can be dropped because 784 * carrier is off, or because our qdisc is 785 * being deactivated, or possibly for other 786 * reasons. In these cases, we just let the 787 * packet to be dropped. */ 788 IFRATE(rate_ctx.new.txdrop++); 789 } 790 791 slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED); 792 nm_i = nm_next(nm_i, lim); 793 IFRATE(rate_ctx.new.txpkt++); 794 } 795 if (a.head != NULL) { 796 a.addr = NULL; 797 nm_os_generic_xmit_frame(&a); 798 } 799 /* Update hwcur to the next slot to transmit. Here nm_i 800 * is not necessarily head, we could break early. */ 801 kring->nr_hwcur = nm_i; 802 } 803 804 /* 805 * Second, reclaim completed buffers 806 */ 807 if (!gna->txqdisc && (flags & NAF_FORCE_RECLAIM || nm_kr_txempty(kring))) { 808 /* No more available slots? Set a notification event 809 * on a netmap slot that will be cleaned in the future. 810 * No doublecheck is performed, since txsync() will be 811 * called twice by netmap_poll(). 812 */ 813 generic_set_tx_event(kring, nm_i); 814 } 815 816 generic_netmap_tx_clean(kring, gna->txqdisc); 817 818 return 0; 819 } 820 821 822 /* 823 * This handler is registered (through nm_os_catch_rx()) 824 * within the attached network interface 825 * in the RX subsystem, so that every mbuf passed up by 826 * the driver can be stolen to the network stack. 827 * Stolen packets are put in a queue where the 828 * generic_netmap_rxsync() callback can extract them. 829 * Returns 1 if the packet was stolen, 0 otherwise. 830 */ 831 int 832 generic_rx_handler(struct ifnet *ifp, struct mbuf *m) 833 { 834 struct netmap_adapter *na = NA(ifp); 835 struct netmap_generic_adapter *gna = (struct netmap_generic_adapter *)na; 836 struct netmap_kring *kring; 837 u_int work_done; 838 u_int r = MBUF_RXQ(m); /* receive ring number */ 839 840 if (r >= na->num_rx_rings) { 841 r = r % na->num_rx_rings; 842 } 843 844 kring = na->rx_rings[r]; 845 846 if (kring->nr_mode == NKR_NETMAP_OFF) { 847 /* We must not intercept this mbuf. */ 848 return 0; 849 } 850 851 /* limit the size of the queue */ 852 if (unlikely(!gna->rxsg && MBUF_LEN(m) > NETMAP_BUF_SIZE(na))) { 853 /* This may happen when GRO/LRO features are enabled for 854 * the NIC driver when the generic adapter does not 855 * support RX scatter-gather. */ 856 nm_prlim(2, "Warning: driver pushed up big packet " 857 "(size=%d)", (int)MBUF_LEN(m)); 858 m_freem(m); 859 } else if (unlikely(mbq_len(&kring->rx_queue) > 1024)) { 860 m_freem(m); 861 } else { 862 mbq_safe_enqueue(&kring->rx_queue, m); 863 } 864 865 if (netmap_generic_mit < 32768) { 866 /* no rx mitigation, pass notification up */ 867 netmap_generic_irq(na, r, &work_done); 868 } else { 869 /* same as send combining, filter notification if there is a 870 * pending timer, otherwise pass it up and start a timer. 871 */ 872 if (likely(nm_os_mitigation_active(&gna->mit[r]))) { 873 /* Record that there is some pending work. */ 874 gna->mit[r].mit_pending = 1; 875 } else { 876 netmap_generic_irq(na, r, &work_done); 877 nm_os_mitigation_start(&gna->mit[r]); 878 } 879 } 880 881 /* We have intercepted the mbuf. */ 882 return 1; 883 } 884 885 /* 886 * generic_netmap_rxsync() extracts mbufs from the queue filled by 887 * generic_netmap_rx_handler() and puts their content in the netmap 888 * receive ring. 889 * Access must be protected because the rx handler is asynchronous, 890 */ 891 static int 892 generic_netmap_rxsync(struct netmap_kring *kring, int flags) 893 { 894 struct netmap_ring *ring = kring->ring; 895 struct netmap_adapter *na = kring->na; 896 u_int nm_i; /* index into the netmap ring */ //j, 897 u_int n; 898 u_int const lim = kring->nkr_num_slots - 1; 899 u_int const head = kring->rhead; 900 int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR; 901 902 /* Adapter-specific variables. */ 903 u_int nm_buf_len = NETMAP_BUF_SIZE(na); 904 struct mbq tmpq; 905 struct mbuf *m; 906 int avail; /* in bytes */ 907 int mlen; 908 int copy; 909 910 if (head > lim) 911 return netmap_ring_reinit(kring); 912 913 IFRATE(rate_ctx.new.rxsync++); 914 915 /* 916 * First part: skip past packets that userspace has released. 917 * This can possibly make room for the second part. 918 */ 919 nm_i = kring->nr_hwcur; 920 if (nm_i != head) { 921 /* Userspace has released some packets. */ 922 for (n = 0; nm_i != head; n++) { 923 struct netmap_slot *slot = &ring->slot[nm_i]; 924 925 slot->flags &= ~NS_BUF_CHANGED; 926 nm_i = nm_next(nm_i, lim); 927 } 928 kring->nr_hwcur = head; 929 } 930 931 /* 932 * Second part: import newly received packets. 933 */ 934 if (!netmap_no_pendintr && !force_update) { 935 return 0; 936 } 937 938 nm_i = kring->nr_hwtail; /* First empty slot in the receive ring. */ 939 940 /* Compute the available space (in bytes) in this netmap ring. 941 * The first slot that is not considered in is the one before 942 * nr_hwcur. */ 943 944 avail = nm_prev(kring->nr_hwcur, lim) - nm_i; 945 if (avail < 0) 946 avail += lim + 1; 947 avail *= nm_buf_len; 948 949 /* First pass: While holding the lock on the RX mbuf queue, 950 * extract as many mbufs as they fit the available space, 951 * and put them in a temporary queue. 952 * To avoid performing a per-mbuf division (mlen / nm_buf_len) to 953 * to update avail, we do the update in a while loop that we 954 * also use to set the RX slots, but without performing the copy. */ 955 mbq_init(&tmpq); 956 mbq_lock(&kring->rx_queue); 957 for (n = 0;; n++) { 958 m = mbq_peek(&kring->rx_queue); 959 if (!m) { 960 /* No more packets from the driver. */ 961 break; 962 } 963 964 mlen = MBUF_LEN(m); 965 if (mlen > avail) { 966 /* No more space in the ring. */ 967 break; 968 } 969 970 mbq_dequeue(&kring->rx_queue); 971 972 while (mlen) { 973 copy = nm_buf_len; 974 if (mlen < copy) { 975 copy = mlen; 976 } 977 mlen -= copy; 978 avail -= nm_buf_len; 979 980 ring->slot[nm_i].len = copy; 981 ring->slot[nm_i].flags = (mlen ? NS_MOREFRAG : 0); 982 nm_i = nm_next(nm_i, lim); 983 } 984 985 mbq_enqueue(&tmpq, m); 986 } 987 mbq_unlock(&kring->rx_queue); 988 989 /* Second pass: Drain the temporary queue, going over the used RX slots, 990 * and perform the copy out of the RX queue lock. */ 991 nm_i = kring->nr_hwtail; 992 993 for (;;) { 994 void *nmaddr; 995 int ofs = 0; 996 int morefrag; 997 998 m = mbq_dequeue(&tmpq); 999 if (!m) { 1000 break; 1001 } 1002 1003 do { 1004 nmaddr = NMB(na, &ring->slot[nm_i]); 1005 /* We only check the address here on generic rx rings. */ 1006 if (nmaddr == NETMAP_BUF_BASE(na)) { /* Bad buffer */ 1007 m_freem(m); 1008 mbq_purge(&tmpq); 1009 mbq_fini(&tmpq); 1010 return netmap_ring_reinit(kring); 1011 } 1012 1013 copy = ring->slot[nm_i].len; 1014 m_copydata(m, ofs, copy, nmaddr); 1015 ofs += copy; 1016 morefrag = ring->slot[nm_i].flags & NS_MOREFRAG; 1017 nm_i = nm_next(nm_i, lim); 1018 } while (morefrag); 1019 1020 m_freem(m); 1021 } 1022 1023 mbq_fini(&tmpq); 1024 1025 if (n) { 1026 kring->nr_hwtail = nm_i; 1027 IFRATE(rate_ctx.new.rxpkt += n); 1028 } 1029 kring->nr_kflags &= ~NKR_PENDINTR; 1030 1031 return 0; 1032 } 1033 1034 static void 1035 generic_netmap_dtor(struct netmap_adapter *na) 1036 { 1037 struct netmap_generic_adapter *gna = (struct netmap_generic_adapter*)na; 1038 struct ifnet *ifp = netmap_generic_getifp(gna); 1039 struct netmap_adapter *prev_na = gna->prev; 1040 1041 if (prev_na != NULL) { 1042 netmap_adapter_put(prev_na); 1043 if (nm_iszombie(na)) { 1044 /* 1045 * The driver has been removed without releasing 1046 * the reference so we need to do it here. 1047 */ 1048 netmap_adapter_put(prev_na); 1049 } 1050 nm_prinf("Native netmap adapter %p restored", prev_na); 1051 } 1052 NM_RESTORE_NA(ifp, prev_na); 1053 /* 1054 * netmap_detach_common(), that it's called after this function, 1055 * overrides WNA(ifp) if na->ifp is not NULL. 1056 */ 1057 na->ifp = NULL; 1058 nm_prinf("Emulated netmap adapter for %s destroyed", na->name); 1059 } 1060 1061 int 1062 na_is_generic(struct netmap_adapter *na) 1063 { 1064 return na->nm_register == generic_netmap_register; 1065 } 1066 1067 /* 1068 * generic_netmap_attach() makes it possible to use netmap on 1069 * a device without native netmap support. 1070 * This is less performant than native support but potentially 1071 * faster than raw sockets or similar schemes. 1072 * 1073 * In this "emulated" mode, netmap rings do not necessarily 1074 * have the same size as those in the NIC. We use a default 1075 * value and possibly override it if the OS has ways to fetch the 1076 * actual configuration. 1077 */ 1078 int 1079 generic_netmap_attach(struct ifnet *ifp) 1080 { 1081 struct netmap_adapter *na; 1082 struct netmap_generic_adapter *gna; 1083 int retval; 1084 u_int num_tx_desc, num_rx_desc; 1085 1086 #ifdef __FreeBSD__ 1087 if (ifp->if_type == IFT_LOOP) { 1088 nm_prerr("if_loop is not supported by %s", __func__); 1089 return EINVAL; 1090 } 1091 #endif 1092 1093 if (NM_NA_CLASH(ifp)) { 1094 /* If NA(ifp) is not null but there is no valid netmap 1095 * adapter it means that someone else is using the same 1096 * pointer (e.g. ax25_ptr on linux). This happens for 1097 * instance when also PF_RING is in use. */ 1098 nm_prerr("Error: netmap adapter hook is busy"); 1099 return EBUSY; 1100 } 1101 1102 num_tx_desc = num_rx_desc = netmap_generic_ringsize; /* starting point */ 1103 1104 nm_os_generic_find_num_desc(ifp, &num_tx_desc, &num_rx_desc); /* ignore errors */ 1105 if (num_tx_desc == 0 || num_rx_desc == 0) { 1106 nm_prerr("Device has no hw slots (tx %u, rx %u)", num_tx_desc, num_rx_desc); 1107 return EINVAL; 1108 } 1109 1110 gna = nm_os_malloc(sizeof(*gna)); 1111 if (gna == NULL) { 1112 nm_prerr("no memory on attach, give up"); 1113 return ENOMEM; 1114 } 1115 na = (struct netmap_adapter *)gna; 1116 strlcpy(na->name, ifp->if_xname, sizeof(na->name)); 1117 na->ifp = ifp; 1118 na->num_tx_desc = num_tx_desc; 1119 na->num_rx_desc = num_rx_desc; 1120 na->rx_buf_maxsize = 32768; 1121 na->nm_register = &generic_netmap_register; 1122 na->nm_txsync = &generic_netmap_txsync; 1123 na->nm_rxsync = &generic_netmap_rxsync; 1124 na->nm_dtor = &generic_netmap_dtor; 1125 /* when using generic, NAF_NETMAP_ON is set so we force 1126 * NAF_SKIP_INTR to use the regular interrupt handler 1127 */ 1128 na->na_flags = NAF_SKIP_INTR | NAF_HOST_RINGS; 1129 1130 nm_prdis("[GNA] num_tx_queues(%d), real_num_tx_queues(%d), len(%lu)", 1131 ifp->num_tx_queues, ifp->real_num_tx_queues, 1132 ifp->tx_queue_len); 1133 nm_prdis("[GNA] num_rx_queues(%d), real_num_rx_queues(%d)", 1134 ifp->num_rx_queues, ifp->real_num_rx_queues); 1135 1136 nm_os_generic_find_num_queues(ifp, &na->num_tx_rings, &na->num_rx_rings); 1137 1138 retval = netmap_attach_common(na); 1139 if (retval) { 1140 nm_os_free(gna); 1141 return retval; 1142 } 1143 1144 if (NM_NA_VALID(ifp)) { 1145 gna->prev = NA(ifp); /* save old na */ 1146 netmap_adapter_get(gna->prev); 1147 } 1148 NM_ATTACH_NA(ifp, na); 1149 1150 nm_os_generic_set_features(gna); 1151 1152 nm_prinf("Emulated adapter for %s created (prev was %p)", na->name, gna->prev); 1153 1154 return retval; 1155 } 1156