1 /* 2 * Copyright (C) 2013-2014 Universita` di Pisa. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 /* 27 * This module implements netmap support on top of standard, 28 * unmodified device drivers. 29 * 30 * A NIOCREGIF request is handled here if the device does not 31 * have native support. TX and RX rings are emulated as follows: 32 * 33 * NIOCREGIF 34 * We preallocate a block of TX mbufs (roughly as many as 35 * tx descriptors; the number is not critical) to speed up 36 * operation during transmissions. The refcount on most of 37 * these buffers is artificially bumped up so we can recycle 38 * them more easily. Also, the destructor is intercepted 39 * so we use it as an interrupt notification to wake up 40 * processes blocked on a poll(). 41 * 42 * For each receive ring we allocate one "struct mbq" 43 * (an mbuf tailq plus a spinlock). We intercept packets 44 * (through if_input) 45 * on the receive path and put them in the mbq from which 46 * netmap receive routines can grab them. 47 * 48 * TX: 49 * in the generic_txsync() routine, netmap buffers are copied 50 * (or linked, in a future) to the preallocated mbufs 51 * and pushed to the transmit queue. Some of these mbufs 52 * (those with NS_REPORT, or otherwise every half ring) 53 * have the refcount=1, others have refcount=2. 54 * When the destructor is invoked, we take that as 55 * a notification that all mbufs up to that one in 56 * the specific ring have been completed, and generate 57 * the equivalent of a transmit interrupt. 58 * 59 * RX: 60 * 61 */ 62 63 #ifdef __FreeBSD__ 64 65 #include <sys/cdefs.h> /* prerequisite */ 66 __FBSDID("$FreeBSD$"); 67 68 #include <sys/types.h> 69 #include <sys/errno.h> 70 #include <sys/malloc.h> 71 #include <sys/lock.h> /* PROT_EXEC */ 72 #include <sys/rwlock.h> 73 #include <sys/socket.h> /* sockaddrs */ 74 #include <sys/selinfo.h> 75 #include <net/if.h> 76 #include <net/if_var.h> 77 #include <machine/bus.h> /* bus_dmamap_* in netmap_kern.h */ 78 79 // XXX temporary - D() defined here 80 #include <net/netmap.h> 81 #include <dev/netmap/netmap_kern.h> 82 #include <dev/netmap/netmap_mem2.h> 83 84 #define rtnl_lock() ND("rtnl_lock called"); 85 #define rtnl_unlock() ND("rtnl_unlock called"); 86 #define MBUF_TXQ(m) ((m)->m_pkthdr.flowid) 87 #define MBUF_RXQ(m) ((m)->m_pkthdr.flowid) 88 #define smp_mb() 89 90 /* 91 * FreeBSD mbuf allocator/deallocator in emulation mode: 92 * 93 * We allocate EXT_PACKET mbuf+clusters, but need to set M_NOFREE 94 * so that the destructor, if invoked, will not free the packet. 95 * In principle we should set the destructor only on demand, 96 * but since there might be a race we better do it on allocation. 97 * As a consequence, we also need to set the destructor or we 98 * would leak buffers. 99 */ 100 101 /* 102 * mbuf wrappers 103 */ 104 #define netmap_get_mbuf(len) m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR|M_NOFREE) 105 106 /* mbuf destructor, also need to change the type to EXT_EXTREF, 107 * add an M_NOFREE flag, and then clear the flag and 108 * chain into uma_zfree(zone_pack, mf) 109 * (or reinstall the buffer ?) 110 */ 111 #define SET_MBUF_DESTRUCTOR(m, fn) do { \ 112 (m)->m_ext.ext_free = (void *)fn; \ 113 (m)->m_ext.ext_type = EXT_EXTREF; \ 114 } while (0) 115 116 117 #define GET_MBUF_REFCNT(m) ((m)->m_ext.ref_cnt ? *(m)->m_ext.ref_cnt : -1) 118 119 120 121 #else /* linux */ 122 123 #include "bsd_glue.h" 124 125 #include <linux/rtnetlink.h> /* rtnl_[un]lock() */ 126 #include <linux/ethtool.h> /* struct ethtool_ops, get_ringparam */ 127 #include <linux/hrtimer.h> 128 129 //#define RATE /* Enables communication statistics. */ 130 131 //#define REG_RESET 132 133 #endif /* linux */ 134 135 136 /* Common headers. */ 137 #include <net/netmap.h> 138 #include <dev/netmap/netmap_kern.h> 139 #include <dev/netmap/netmap_mem2.h> 140 141 142 143 /* ======================== usage stats =========================== */ 144 145 #ifdef RATE 146 #define IFRATE(x) x 147 struct rate_stats { 148 unsigned long txpkt; 149 unsigned long txsync; 150 unsigned long txirq; 151 unsigned long rxpkt; 152 unsigned long rxirq; 153 unsigned long rxsync; 154 }; 155 156 struct rate_context { 157 unsigned refcount; 158 struct timer_list timer; 159 struct rate_stats new; 160 struct rate_stats old; 161 }; 162 163 #define RATE_PRINTK(_NAME_) \ 164 printk( #_NAME_ " = %lu Hz\n", (cur._NAME_ - ctx->old._NAME_)/RATE_PERIOD); 165 #define RATE_PERIOD 2 166 static void rate_callback(unsigned long arg) 167 { 168 struct rate_context * ctx = (struct rate_context *)arg; 169 struct rate_stats cur = ctx->new; 170 int r; 171 172 RATE_PRINTK(txpkt); 173 RATE_PRINTK(txsync); 174 RATE_PRINTK(txirq); 175 RATE_PRINTK(rxpkt); 176 RATE_PRINTK(rxsync); 177 RATE_PRINTK(rxirq); 178 printk("\n"); 179 180 ctx->old = cur; 181 r = mod_timer(&ctx->timer, jiffies + 182 msecs_to_jiffies(RATE_PERIOD * 1000)); 183 if (unlikely(r)) 184 D("[v1000] Error: mod_timer()"); 185 } 186 187 static struct rate_context rate_ctx; 188 189 #else /* !RATE */ 190 #define IFRATE(x) 191 #endif /* !RATE */ 192 193 194 /* =============== GENERIC NETMAP ADAPTER SUPPORT ================= */ 195 #define GENERIC_BUF_SIZE netmap_buf_size /* Size of the mbufs in the Tx pool. */ 196 197 /* 198 * Wrapper used by the generic adapter layer to notify 199 * the poller threads. Differently from netmap_rx_irq(), we check 200 * only IFCAP_NETMAP instead of NAF_NATIVE_ON to enable the irq. 201 */ 202 static void 203 netmap_generic_irq(struct ifnet *ifp, u_int q, u_int *work_done) 204 { 205 if (unlikely(!(ifp->if_capenable & IFCAP_NETMAP))) 206 return; 207 208 netmap_common_irq(ifp, q, work_done); 209 } 210 211 212 /* Enable/disable netmap mode for a generic network interface. */ 213 static int 214 generic_netmap_register(struct netmap_adapter *na, int enable) 215 { 216 struct ifnet *ifp = na->ifp; 217 struct netmap_generic_adapter *gna = (struct netmap_generic_adapter *)na; 218 struct mbuf *m; 219 int error; 220 int i, r; 221 222 if (!na) 223 return EINVAL; 224 225 #ifdef REG_RESET 226 error = ifp->netdev_ops->ndo_stop(ifp); 227 if (error) { 228 return error; 229 } 230 #endif /* REG_RESET */ 231 232 if (enable) { /* Enable netmap mode. */ 233 /* Init the mitigation support. */ 234 gna->mit = malloc(na->num_rx_rings * sizeof(struct nm_generic_mit), 235 M_DEVBUF, M_NOWAIT | M_ZERO); 236 if (!gna->mit) { 237 D("mitigation allocation failed"); 238 error = ENOMEM; 239 goto out; 240 } 241 for (r=0; r<na->num_rx_rings; r++) 242 netmap_mitigation_init(&gna->mit[r], na); 243 244 /* Initialize the rx queue, as generic_rx_handler() can 245 * be called as soon as netmap_catch_rx() returns. 246 */ 247 for (r=0; r<na->num_rx_rings; r++) { 248 mbq_safe_init(&na->rx_rings[r].rx_queue); 249 } 250 251 /* 252 * Preallocate packet buffers for the tx rings. 253 */ 254 for (r=0; r<na->num_tx_rings; r++) 255 na->tx_rings[r].tx_pool = NULL; 256 for (r=0; r<na->num_tx_rings; r++) { 257 na->tx_rings[r].tx_pool = malloc(na->num_tx_desc * sizeof(struct mbuf *), 258 M_DEVBUF, M_NOWAIT | M_ZERO); 259 if (!na->tx_rings[r].tx_pool) { 260 D("tx_pool allocation failed"); 261 error = ENOMEM; 262 goto free_tx_pools; 263 } 264 for (i=0; i<na->num_tx_desc; i++) 265 na->tx_rings[r].tx_pool[i] = NULL; 266 for (i=0; i<na->num_tx_desc; i++) { 267 m = netmap_get_mbuf(GENERIC_BUF_SIZE); 268 if (!m) { 269 D("tx_pool[%d] allocation failed", i); 270 error = ENOMEM; 271 goto free_tx_pools; 272 } 273 na->tx_rings[r].tx_pool[i] = m; 274 } 275 } 276 rtnl_lock(); 277 /* Prepare to intercept incoming traffic. */ 278 error = netmap_catch_rx(na, 1); 279 if (error) { 280 D("netdev_rx_handler_register() failed (%d)", error); 281 goto register_handler; 282 } 283 ifp->if_capenable |= IFCAP_NETMAP; 284 285 /* Make netmap control the packet steering. */ 286 netmap_catch_tx(gna, 1); 287 288 rtnl_unlock(); 289 290 #ifdef RATE 291 if (rate_ctx.refcount == 0) { 292 D("setup_timer()"); 293 memset(&rate_ctx, 0, sizeof(rate_ctx)); 294 setup_timer(&rate_ctx.timer, &rate_callback, (unsigned long)&rate_ctx); 295 if (mod_timer(&rate_ctx.timer, jiffies + msecs_to_jiffies(1500))) { 296 D("Error: mod_timer()"); 297 } 298 } 299 rate_ctx.refcount++; 300 #endif /* RATE */ 301 302 } else if (na->tx_rings[0].tx_pool) { 303 /* Disable netmap mode. We enter here only if the previous 304 generic_netmap_register(na, 1) was successfull. 305 If it was not, na->tx_rings[0].tx_pool was set to NULL by the 306 error handling code below. */ 307 rtnl_lock(); 308 309 ifp->if_capenable &= ~IFCAP_NETMAP; 310 311 /* Release packet steering control. */ 312 netmap_catch_tx(gna, 0); 313 314 /* Do not intercept packets on the rx path. */ 315 netmap_catch_rx(na, 0); 316 317 rtnl_unlock(); 318 319 /* Free the mbufs going to the netmap rings */ 320 for (r=0; r<na->num_rx_rings; r++) { 321 mbq_safe_purge(&na->rx_rings[r].rx_queue); 322 mbq_safe_destroy(&na->rx_rings[r].rx_queue); 323 } 324 325 for (r=0; r<na->num_rx_rings; r++) 326 netmap_mitigation_cleanup(&gna->mit[r]); 327 free(gna->mit, M_DEVBUF); 328 329 for (r=0; r<na->num_tx_rings; r++) { 330 for (i=0; i<na->num_tx_desc; i++) { 331 m_freem(na->tx_rings[r].tx_pool[i]); 332 } 333 free(na->tx_rings[r].tx_pool, M_DEVBUF); 334 } 335 336 #ifdef RATE 337 if (--rate_ctx.refcount == 0) { 338 D("del_timer()"); 339 del_timer(&rate_ctx.timer); 340 } 341 #endif 342 } 343 344 #ifdef REG_RESET 345 error = ifp->netdev_ops->ndo_open(ifp); 346 if (error) { 347 goto free_tx_pools; 348 } 349 #endif 350 351 return 0; 352 353 register_handler: 354 rtnl_unlock(); 355 free_tx_pools: 356 for (r=0; r<na->num_tx_rings; r++) { 357 if (na->tx_rings[r].tx_pool == NULL) 358 continue; 359 for (i=0; i<na->num_tx_desc; i++) 360 if (na->tx_rings[r].tx_pool[i]) 361 m_freem(na->tx_rings[r].tx_pool[i]); 362 free(na->tx_rings[r].tx_pool, M_DEVBUF); 363 na->tx_rings[r].tx_pool = NULL; 364 } 365 for (r=0; r<na->num_rx_rings; r++) { 366 netmap_mitigation_cleanup(&gna->mit[r]); 367 mbq_safe_destroy(&na->rx_rings[r].rx_queue); 368 } 369 free(gna->mit, M_DEVBUF); 370 out: 371 372 return error; 373 } 374 375 /* 376 * Callback invoked when the device driver frees an mbuf used 377 * by netmap to transmit a packet. This usually happens when 378 * the NIC notifies the driver that transmission is completed. 379 */ 380 static void 381 generic_mbuf_destructor(struct mbuf *m) 382 { 383 if (netmap_verbose) 384 D("Tx irq (%p) queue %d", m, MBUF_TXQ(m)); 385 netmap_generic_irq(MBUF_IFP(m), MBUF_TXQ(m), NULL); 386 #ifdef __FreeBSD__ 387 m->m_ext.ext_type = EXT_PACKET; 388 m->m_ext.ext_free = NULL; 389 if (*(m->m_ext.ref_cnt) == 0) 390 *(m->m_ext.ref_cnt) = 1; 391 uma_zfree(zone_pack, m); 392 #endif /* __FreeBSD__ */ 393 IFRATE(rate_ctx.new.txirq++); 394 } 395 396 /* Record completed transmissions and update hwtail. 397 * 398 * The oldest tx buffer not yet completed is at nr_hwtail + 1, 399 * nr_hwcur is the first unsent buffer. 400 */ 401 static u_int 402 generic_netmap_tx_clean(struct netmap_kring *kring) 403 { 404 u_int const lim = kring->nkr_num_slots - 1; 405 u_int nm_i = nm_next(kring->nr_hwtail, lim); 406 u_int hwcur = kring->nr_hwcur; 407 u_int n = 0; 408 struct mbuf **tx_pool = kring->tx_pool; 409 410 while (nm_i != hwcur) { /* buffers not completed */ 411 struct mbuf *m = tx_pool[nm_i]; 412 413 if (unlikely(m == NULL)) { 414 /* this is done, try to replenish the entry */ 415 tx_pool[nm_i] = m = netmap_get_mbuf(GENERIC_BUF_SIZE); 416 if (unlikely(m == NULL)) { 417 D("mbuf allocation failed, XXX error"); 418 // XXX how do we proceed ? break ? 419 return -ENOMEM; 420 } 421 } else if (GET_MBUF_REFCNT(m) != 1) { 422 break; /* This mbuf is still busy: its refcnt is 2. */ 423 } 424 n++; 425 nm_i = nm_next(nm_i, lim); 426 } 427 kring->nr_hwtail = nm_prev(nm_i, lim); 428 ND("tx completed [%d] -> hwtail %d", n, kring->nr_hwtail); 429 430 return n; 431 } 432 433 434 /* 435 * We have pending packets in the driver between nr_hwtail +1 and hwcur. 436 * Compute a position in the middle, to be used to generate 437 * a notification. 438 */ 439 static inline u_int 440 generic_tx_event_middle(struct netmap_kring *kring, u_int hwcur) 441 { 442 u_int n = kring->nkr_num_slots; 443 u_int ntc = nm_next(kring->nr_hwtail, n-1); 444 u_int e; 445 446 if (hwcur >= ntc) { 447 e = (hwcur + ntc) / 2; 448 } else { /* wrap around */ 449 e = (hwcur + n + ntc) / 2; 450 if (e >= n) { 451 e -= n; 452 } 453 } 454 455 if (unlikely(e >= n)) { 456 D("This cannot happen"); 457 e = 0; 458 } 459 460 return e; 461 } 462 463 /* 464 * We have pending packets in the driver between nr_hwtail+1 and hwcur. 465 * Schedule a notification approximately in the middle of the two. 466 * There is a race but this is only called within txsync which does 467 * a double check. 468 */ 469 static void 470 generic_set_tx_event(struct netmap_kring *kring, u_int hwcur) 471 { 472 struct mbuf *m; 473 u_int e; 474 475 if (nm_next(kring->nr_hwtail, kring->nkr_num_slots -1) == hwcur) { 476 return; /* all buffers are free */ 477 } 478 e = generic_tx_event_middle(kring, hwcur); 479 480 m = kring->tx_pool[e]; 481 if (m == NULL) { 482 /* This can happen if there is already an event on the netmap 483 slot 'e': There is nothing to do. */ 484 return; 485 } 486 ND("Event at %d mbuf %p refcnt %d", e, m, GET_MBUF_REFCNT(m)); 487 kring->tx_pool[e] = NULL; 488 SET_MBUF_DESTRUCTOR(m, generic_mbuf_destructor); 489 490 // XXX wmb() ? 491 /* Decrement the refcount an free it if we have the last one. */ 492 m_freem(m); 493 smp_mb(); 494 } 495 496 497 /* 498 * generic_netmap_txsync() transforms netmap buffers into mbufs 499 * and passes them to the standard device driver 500 * (ndo_start_xmit() or ifp->if_transmit() ). 501 * On linux this is not done directly, but using dev_queue_xmit(), 502 * since it implements the TX flow control (and takes some locks). 503 */ 504 static int 505 generic_netmap_txsync(struct netmap_adapter *na, u_int ring_nr, int flags) 506 { 507 struct ifnet *ifp = na->ifp; 508 struct netmap_kring *kring = &na->tx_rings[ring_nr]; 509 struct netmap_ring *ring = kring->ring; 510 u_int nm_i; /* index into the netmap ring */ // j 511 u_int const lim = kring->nkr_num_slots - 1; 512 u_int const head = kring->rhead; 513 514 IFRATE(rate_ctx.new.txsync++); 515 516 // TODO: handle the case of mbuf allocation failure 517 518 rmb(); 519 520 /* 521 * First part: process new packets to send. 522 */ 523 nm_i = kring->nr_hwcur; 524 if (nm_i != head) { /* we have new packets to send */ 525 while (nm_i != head) { 526 struct netmap_slot *slot = &ring->slot[nm_i]; 527 u_int len = slot->len; 528 void *addr = NMB(slot); 529 530 /* device-specific */ 531 struct mbuf *m; 532 int tx_ret; 533 534 NM_CHECK_ADDR_LEN(addr, len); 535 536 /* Tale a mbuf from the tx pool and copy in the user packet. */ 537 m = kring->tx_pool[nm_i]; 538 if (unlikely(!m)) { 539 RD(5, "This should never happen"); 540 kring->tx_pool[nm_i] = m = netmap_get_mbuf(GENERIC_BUF_SIZE); 541 if (unlikely(m == NULL)) { 542 D("mbuf allocation failed"); 543 break; 544 } 545 } 546 /* XXX we should ask notifications when NS_REPORT is set, 547 * or roughly every half frame. We can optimize this 548 * by lazily requesting notifications only when a 549 * transmission fails. Probably the best way is to 550 * break on failures and set notifications when 551 * ring->cur == ring->tail || nm_i != cur 552 */ 553 tx_ret = generic_xmit_frame(ifp, m, addr, len, ring_nr); 554 if (unlikely(tx_ret)) { 555 RD(5, "start_xmit failed: err %d [nm_i %u, head %u, hwtail %u]", 556 tx_ret, nm_i, head, kring->nr_hwtail); 557 /* 558 * No room for this mbuf in the device driver. 559 * Request a notification FOR A PREVIOUS MBUF, 560 * then call generic_netmap_tx_clean(kring) to do the 561 * double check and see if we can free more buffers. 562 * If there is space continue, else break; 563 * NOTE: the double check is necessary if the problem 564 * occurs in the txsync call after selrecord(). 565 * Also, we need some way to tell the caller that not 566 * all buffers were queued onto the device (this was 567 * not a problem with native netmap driver where space 568 * is preallocated). The bridge has a similar problem 569 * and we solve it there by dropping the excess packets. 570 */ 571 generic_set_tx_event(kring, nm_i); 572 if (generic_netmap_tx_clean(kring)) { /* space now available */ 573 continue; 574 } else { 575 break; 576 } 577 } 578 slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED); 579 nm_i = nm_next(nm_i, lim); 580 IFRATE(rate_ctx.new.txpkt ++); 581 } 582 583 /* Update hwcur to the next slot to transmit. */ 584 kring->nr_hwcur = nm_i; /* not head, we could break early */ 585 } 586 587 /* 588 * Second, reclaim completed buffers 589 */ 590 if (flags & NAF_FORCE_RECLAIM || nm_kr_txempty(kring)) { 591 /* No more available slots? Set a notification event 592 * on a netmap slot that will be cleaned in the future. 593 * No doublecheck is performed, since txsync() will be 594 * called twice by netmap_poll(). 595 */ 596 generic_set_tx_event(kring, nm_i); 597 } 598 ND("tx #%d, hwtail = %d", n, kring->nr_hwtail); 599 600 generic_netmap_tx_clean(kring); 601 602 nm_txsync_finalize(kring); 603 604 return 0; 605 } 606 607 608 /* 609 * This handler is registered (through netmap_catch_rx()) 610 * within the attached network interface 611 * in the RX subsystem, so that every mbuf passed up by 612 * the driver can be stolen to the network stack. 613 * Stolen packets are put in a queue where the 614 * generic_netmap_rxsync() callback can extract them. 615 */ 616 void 617 generic_rx_handler(struct ifnet *ifp, struct mbuf *m) 618 { 619 struct netmap_adapter *na = NA(ifp); 620 struct netmap_generic_adapter *gna = (struct netmap_generic_adapter *)na; 621 u_int work_done; 622 u_int rr = MBUF_RXQ(m); // receive ring number 623 624 if (rr >= na->num_rx_rings) { 625 rr = rr % na->num_rx_rings; // XXX expensive... 626 } 627 628 /* limit the size of the queue */ 629 if (unlikely(mbq_len(&na->rx_rings[rr].rx_queue) > 1024)) { 630 m_freem(m); 631 } else { 632 mbq_safe_enqueue(&na->rx_rings[rr].rx_queue, m); 633 } 634 635 if (netmap_generic_mit < 32768) { 636 /* no rx mitigation, pass notification up */ 637 netmap_generic_irq(na->ifp, rr, &work_done); 638 IFRATE(rate_ctx.new.rxirq++); 639 } else { 640 /* same as send combining, filter notification if there is a 641 * pending timer, otherwise pass it up and start a timer. 642 */ 643 if (likely(netmap_mitigation_active(&gna->mit[rr]))) { 644 /* Record that there is some pending work. */ 645 gna->mit[rr].mit_pending = 1; 646 } else { 647 netmap_generic_irq(na->ifp, rr, &work_done); 648 IFRATE(rate_ctx.new.rxirq++); 649 netmap_mitigation_start(&gna->mit[rr]); 650 } 651 } 652 } 653 654 /* 655 * generic_netmap_rxsync() extracts mbufs from the queue filled by 656 * generic_netmap_rx_handler() and puts their content in the netmap 657 * receive ring. 658 * Access must be protected because the rx handler is asynchronous, 659 */ 660 static int 661 generic_netmap_rxsync(struct netmap_adapter *na, u_int ring_nr, int flags) 662 { 663 struct netmap_kring *kring = &na->rx_rings[ring_nr]; 664 struct netmap_ring *ring = kring->ring; 665 u_int nm_i; /* index into the netmap ring */ //j, 666 u_int n; 667 u_int const lim = kring->nkr_num_slots - 1; 668 u_int const head = nm_rxsync_prologue(kring); 669 int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR; 670 671 if (head > lim) 672 return netmap_ring_reinit(kring); 673 674 /* 675 * First part: import newly received packets. 676 */ 677 if (netmap_no_pendintr || force_update) { 678 /* extract buffers from the rx queue, stop at most one 679 * slot before nr_hwcur (stop_i) 680 */ 681 uint16_t slot_flags = kring->nkr_slot_flags; 682 u_int stop_i = nm_prev(kring->nr_hwcur, lim); 683 684 nm_i = kring->nr_hwtail; /* first empty slot in the receive ring */ 685 for (n = 0; nm_i != stop_i; n++) { 686 int len; 687 void *addr = NMB(&ring->slot[nm_i]); 688 struct mbuf *m; 689 690 /* we only check the address here on generic rx rings */ 691 if (addr == netmap_buffer_base) { /* Bad buffer */ 692 return netmap_ring_reinit(kring); 693 } 694 /* 695 * Call the locked version of the function. 696 * XXX Ideally we could grab a batch of mbufs at once 697 * and save some locking overhead. 698 */ 699 m = mbq_safe_dequeue(&kring->rx_queue); 700 if (!m) /* no more data */ 701 break; 702 len = MBUF_LEN(m); 703 m_copydata(m, 0, len, addr); 704 ring->slot[nm_i].len = len; 705 ring->slot[nm_i].flags = slot_flags; 706 m_freem(m); 707 nm_i = nm_next(nm_i, lim); 708 } 709 if (n) { 710 kring->nr_hwtail = nm_i; 711 IFRATE(rate_ctx.new.rxpkt += n); 712 } 713 kring->nr_kflags &= ~NKR_PENDINTR; 714 } 715 716 // XXX should we invert the order ? 717 /* 718 * Second part: skip past packets that userspace has released. 719 */ 720 nm_i = kring->nr_hwcur; 721 if (nm_i != head) { 722 /* Userspace has released some packets. */ 723 for (n = 0; nm_i != head; n++) { 724 struct netmap_slot *slot = &ring->slot[nm_i]; 725 726 slot->flags &= ~NS_BUF_CHANGED; 727 nm_i = nm_next(nm_i, lim); 728 } 729 kring->nr_hwcur = head; 730 } 731 /* tell userspace that there might be new packets. */ 732 nm_rxsync_finalize(kring); 733 IFRATE(rate_ctx.new.rxsync++); 734 735 return 0; 736 } 737 738 static void 739 generic_netmap_dtor(struct netmap_adapter *na) 740 { 741 struct ifnet *ifp = na->ifp; 742 struct netmap_generic_adapter *gna = (struct netmap_generic_adapter*)na; 743 struct netmap_adapter *prev_na = gna->prev; 744 745 if (prev_na != NULL) { 746 D("Released generic NA %p", gna); 747 if_rele(na->ifp); 748 netmap_adapter_put(prev_na); 749 } 750 if (ifp != NULL) { 751 WNA(ifp) = prev_na; 752 D("Restored native NA %p", prev_na); 753 na->ifp = NULL; 754 } 755 } 756 757 /* 758 * generic_netmap_attach() makes it possible to use netmap on 759 * a device without native netmap support. 760 * This is less performant than native support but potentially 761 * faster than raw sockets or similar schemes. 762 * 763 * In this "emulated" mode, netmap rings do not necessarily 764 * have the same size as those in the NIC. We use a default 765 * value and possibly override it if the OS has ways to fetch the 766 * actual configuration. 767 */ 768 int 769 generic_netmap_attach(struct ifnet *ifp) 770 { 771 struct netmap_adapter *na; 772 struct netmap_generic_adapter *gna; 773 int retval; 774 u_int num_tx_desc, num_rx_desc; 775 776 num_tx_desc = num_rx_desc = netmap_generic_ringsize; /* starting point */ 777 778 generic_find_num_desc(ifp, &num_tx_desc, &num_rx_desc); 779 ND("Netmap ring size: TX = %d, RX = %d", num_tx_desc, num_rx_desc); 780 781 gna = malloc(sizeof(*gna), M_DEVBUF, M_NOWAIT | M_ZERO); 782 if (gna == NULL) { 783 D("no memory on attach, give up"); 784 return ENOMEM; 785 } 786 na = (struct netmap_adapter *)gna; 787 na->ifp = ifp; 788 na->num_tx_desc = num_tx_desc; 789 na->num_rx_desc = num_rx_desc; 790 na->nm_register = &generic_netmap_register; 791 na->nm_txsync = &generic_netmap_txsync; 792 na->nm_rxsync = &generic_netmap_rxsync; 793 na->nm_dtor = &generic_netmap_dtor; 794 /* when using generic, IFCAP_NETMAP is set so we force 795 * NAF_SKIP_INTR to use the regular interrupt handler 796 */ 797 na->na_flags = NAF_SKIP_INTR | NAF_HOST_RINGS; 798 799 ND("[GNA] num_tx_queues(%d), real_num_tx_queues(%d), len(%lu)", 800 ifp->num_tx_queues, ifp->real_num_tx_queues, 801 ifp->tx_queue_len); 802 ND("[GNA] num_rx_queues(%d), real_num_rx_queues(%d)", 803 ifp->num_rx_queues, ifp->real_num_rx_queues); 804 805 generic_find_num_queues(ifp, &na->num_tx_rings, &na->num_rx_rings); 806 807 retval = netmap_attach_common(na); 808 if (retval) { 809 free(gna, M_DEVBUF); 810 } 811 812 return retval; 813 } 814