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