1f9790aebSLuigi Rizzo /*
217885a7bSLuigi Rizzo  * Copyright (C) 2013-2014 Universita` di Pisa. All rights reserved.
3f9790aebSLuigi Rizzo  *
4f9790aebSLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
5f9790aebSLuigi Rizzo  * modification, are permitted provided that the following conditions
6f9790aebSLuigi Rizzo  * are met:
7f9790aebSLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
8f9790aebSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
9f9790aebSLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
10f9790aebSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
11f9790aebSLuigi Rizzo  *      documentation and/or other materials provided with the distribution.
12f9790aebSLuigi Rizzo  *
13f9790aebSLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14f9790aebSLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15f9790aebSLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16f9790aebSLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17f9790aebSLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18f9790aebSLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19f9790aebSLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20f9790aebSLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21f9790aebSLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22f9790aebSLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23f9790aebSLuigi Rizzo  * SUCH DAMAGE.
24f9790aebSLuigi Rizzo  */
25f9790aebSLuigi Rizzo 
26f9790aebSLuigi Rizzo /*
27f9790aebSLuigi Rizzo  * This module implements netmap support on top of standard,
28f9790aebSLuigi Rizzo  * unmodified device drivers.
29f9790aebSLuigi Rizzo  *
30f9790aebSLuigi Rizzo  * A NIOCREGIF request is handled here if the device does not
31f9790aebSLuigi Rizzo  * have native support. TX and RX rings are emulated as follows:
32f9790aebSLuigi Rizzo  *
33f9790aebSLuigi Rizzo  * NIOCREGIF
34f9790aebSLuigi Rizzo  *	We preallocate a block of TX mbufs (roughly as many as
35f9790aebSLuigi Rizzo  *	tx descriptors; the number is not critical) to speed up
36f9790aebSLuigi Rizzo  *	operation during transmissions. The refcount on most of
37f9790aebSLuigi Rizzo  *	these buffers is artificially bumped up so we can recycle
38f9790aebSLuigi Rizzo  *	them more easily. Also, the destructor is intercepted
39f9790aebSLuigi Rizzo  *	so we use it as an interrupt notification to wake up
40f9790aebSLuigi Rizzo  *	processes blocked on a poll().
41f9790aebSLuigi Rizzo  *
42f9790aebSLuigi Rizzo  *	For each receive ring we allocate one "struct mbq"
43f9790aebSLuigi Rizzo  *	(an mbuf tailq plus a spinlock). We intercept packets
44f9790aebSLuigi Rizzo  *	(through if_input)
45f9790aebSLuigi Rizzo  *	on the receive path and put them in the mbq from which
46f9790aebSLuigi Rizzo  *	netmap receive routines can grab them.
47f9790aebSLuigi Rizzo  *
48f9790aebSLuigi Rizzo  * TX:
49f9790aebSLuigi Rizzo  *	in the generic_txsync() routine, netmap buffers are copied
50f9790aebSLuigi Rizzo  *	(or linked, in a future) to the preallocated mbufs
51f9790aebSLuigi Rizzo  *	and pushed to the transmit queue. Some of these mbufs
52f9790aebSLuigi Rizzo  *	(those with NS_REPORT, or otherwise every half ring)
53f9790aebSLuigi Rizzo  *	have the refcount=1, others have refcount=2.
54f9790aebSLuigi Rizzo  *	When the destructor is invoked, we take that as
55f9790aebSLuigi Rizzo  *	a notification that all mbufs up to that one in
56f9790aebSLuigi Rizzo  *	the specific ring have been completed, and generate
57f9790aebSLuigi Rizzo  *	the equivalent of a transmit interrupt.
58f9790aebSLuigi Rizzo  *
59f9790aebSLuigi Rizzo  * RX:
60f9790aebSLuigi Rizzo  *
61f9790aebSLuigi Rizzo  */
62f9790aebSLuigi Rizzo 
63f9790aebSLuigi Rizzo #ifdef __FreeBSD__
64f9790aebSLuigi Rizzo 
65f9790aebSLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */
66f9790aebSLuigi Rizzo __FBSDID("$FreeBSD$");
67f9790aebSLuigi Rizzo 
68f9790aebSLuigi Rizzo #include <sys/types.h>
69f9790aebSLuigi Rizzo #include <sys/errno.h>
70f9790aebSLuigi Rizzo #include <sys/malloc.h>
71f9790aebSLuigi Rizzo #include <sys/lock.h>   /* PROT_EXEC */
72f9790aebSLuigi Rizzo #include <sys/rwlock.h>
73f9790aebSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
74f9790aebSLuigi Rizzo #include <sys/selinfo.h>
75f9790aebSLuigi Rizzo #include <net/if.h>
76f9790aebSLuigi Rizzo #include <net/if_var.h>
77f9790aebSLuigi Rizzo #include <machine/bus.h>        /* bus_dmamap_* in netmap_kern.h */
78f9790aebSLuigi Rizzo 
79f9790aebSLuigi Rizzo // XXX temporary - D() defined here
80f9790aebSLuigi Rizzo #include <net/netmap.h>
81f9790aebSLuigi Rizzo #include <dev/netmap/netmap_kern.h>
82f9790aebSLuigi Rizzo #include <dev/netmap/netmap_mem2.h>
83f9790aebSLuigi Rizzo 
84e4166283SLuigi Rizzo #define rtnl_lock()	ND("rtnl_lock called")
85e4166283SLuigi Rizzo #define rtnl_unlock()	ND("rtnl_unlock called")
86f9790aebSLuigi Rizzo #define MBUF_TXQ(m)	((m)->m_pkthdr.flowid)
87f0ea3689SLuigi Rizzo #define MBUF_RXQ(m)	((m)->m_pkthdr.flowid)
88f9790aebSLuigi Rizzo #define smp_mb()
89f9790aebSLuigi Rizzo 
90f9790aebSLuigi Rizzo /*
915899a007SLuigi Rizzo  * FreeBSD mbuf allocator/deallocator in emulation mode:
925899a007SLuigi Rizzo  *
935899a007SLuigi Rizzo  * We allocate EXT_PACKET mbuf+clusters, but need to set M_NOFREE
945899a007SLuigi Rizzo  * so that the destructor, if invoked, will not free the packet.
955899a007SLuigi Rizzo  *    In principle we should set the destructor only on demand,
965899a007SLuigi Rizzo  * but since there might be a race we better do it on allocation.
975899a007SLuigi Rizzo  * As a consequence, we also need to set the destructor or we
985899a007SLuigi Rizzo  * would leak buffers.
99f9790aebSLuigi Rizzo  */
100f9790aebSLuigi Rizzo 
101f9790aebSLuigi Rizzo /*
1025899a007SLuigi Rizzo  * mbuf wrappers
103f9790aebSLuigi Rizzo  */
104f9790aebSLuigi Rizzo 
1054bf50f18SLuigi Rizzo /* mbuf destructor, also need to change the type to EXT_EXTREF,
106f9790aebSLuigi Rizzo  * add an M_NOFREE flag, and then clear the flag and
107f9790aebSLuigi Rizzo  * chain into uma_zfree(zone_pack, mf)
108f9790aebSLuigi Rizzo  * (or reinstall the buffer ?)
109f9790aebSLuigi Rizzo  */
110f9790aebSLuigi Rizzo #define SET_MBUF_DESTRUCTOR(m, fn)	do {		\
111f9790aebSLuigi Rizzo 	(m)->m_ext.ext_free = (void *)fn;	\
112f9790aebSLuigi Rizzo 	(m)->m_ext.ext_type = EXT_EXTREF;	\
113f9790aebSLuigi Rizzo } while (0)
114f9790aebSLuigi Rizzo 
115e4166283SLuigi Rizzo static void
116e4166283SLuigi Rizzo netmap_default_mbuf_destructor(struct mbuf *m)
117e4166283SLuigi Rizzo {
1184bf50f18SLuigi Rizzo 	/* restore original mbuf */
1194bf50f18SLuigi Rizzo 	m->m_ext.ext_buf = m->m_data = m->m_ext.ext_arg1;
1204bf50f18SLuigi Rizzo 	m->m_ext.ext_arg1 = NULL;
121e4166283SLuigi Rizzo 	m->m_ext.ext_type = EXT_PACKET;
122e4166283SLuigi Rizzo 	m->m_ext.ext_free = NULL;
1234bf50f18SLuigi Rizzo 	if (GET_MBUF_REFCNT(m) == 0)
1244bf50f18SLuigi Rizzo 		SET_MBUF_REFCNT(m, 1);
125e4166283SLuigi Rizzo 	uma_zfree(zone_pack, m);
126e4166283SLuigi Rizzo }
127e4166283SLuigi Rizzo 
128e4166283SLuigi Rizzo static inline struct mbuf *
129e4166283SLuigi Rizzo netmap_get_mbuf(int len)
130e4166283SLuigi Rizzo {
131e4166283SLuigi Rizzo 	struct mbuf *m;
132e4166283SLuigi Rizzo 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR | M_NOFREE);
133e4166283SLuigi Rizzo 	if (m) {
1344bf50f18SLuigi Rizzo 		m->m_ext.ext_arg1 = m->m_ext.ext_buf; // XXX save
135e4166283SLuigi Rizzo 		m->m_ext.ext_free = (void *)netmap_default_mbuf_destructor;
136e4166283SLuigi Rizzo 		m->m_ext.ext_type = EXT_EXTREF;
1374bf50f18SLuigi Rizzo 		ND(5, "create m %p refcnt %d", m, GET_MBUF_REFCNT(m));
138e4166283SLuigi Rizzo 	}
139e4166283SLuigi Rizzo 	return m;
140e4166283SLuigi Rizzo }
141f9790aebSLuigi Rizzo 
142f9790aebSLuigi Rizzo 
143f9790aebSLuigi Rizzo 
144f9790aebSLuigi Rizzo #else /* linux */
145f9790aebSLuigi Rizzo 
146f9790aebSLuigi Rizzo #include "bsd_glue.h"
147f9790aebSLuigi Rizzo 
148f9790aebSLuigi Rizzo #include <linux/rtnetlink.h>    /* rtnl_[un]lock() */
149f9790aebSLuigi Rizzo #include <linux/ethtool.h>      /* struct ethtool_ops, get_ringparam */
150f9790aebSLuigi Rizzo #include <linux/hrtimer.h>
151f9790aebSLuigi Rizzo 
152f9790aebSLuigi Rizzo //#define REG_RESET
153f9790aebSLuigi Rizzo 
154f9790aebSLuigi Rizzo #endif /* linux */
155f9790aebSLuigi Rizzo 
156f9790aebSLuigi Rizzo 
157f9790aebSLuigi Rizzo /* Common headers. */
158f9790aebSLuigi Rizzo #include <net/netmap.h>
159f9790aebSLuigi Rizzo #include <dev/netmap/netmap_kern.h>
160f9790aebSLuigi Rizzo #include <dev/netmap/netmap_mem2.h>
161f9790aebSLuigi Rizzo 
162f9790aebSLuigi Rizzo 
163f9790aebSLuigi Rizzo 
164f9790aebSLuigi Rizzo /* ======================== usage stats =========================== */
165f9790aebSLuigi Rizzo 
1664bf50f18SLuigi Rizzo #ifdef RATE_GENERIC
167f9790aebSLuigi Rizzo #define IFRATE(x) x
168f9790aebSLuigi Rizzo struct rate_stats {
169f9790aebSLuigi Rizzo 	unsigned long txpkt;
170f9790aebSLuigi Rizzo 	unsigned long txsync;
171f9790aebSLuigi Rizzo 	unsigned long txirq;
172f9790aebSLuigi Rizzo 	unsigned long rxpkt;
173f9790aebSLuigi Rizzo 	unsigned long rxirq;
174f9790aebSLuigi Rizzo 	unsigned long rxsync;
175f9790aebSLuigi Rizzo };
176f9790aebSLuigi Rizzo 
177f9790aebSLuigi Rizzo struct rate_context {
178f9790aebSLuigi Rizzo 	unsigned refcount;
179f9790aebSLuigi Rizzo 	struct timer_list timer;
180f9790aebSLuigi Rizzo 	struct rate_stats new;
181f9790aebSLuigi Rizzo 	struct rate_stats old;
182f9790aebSLuigi Rizzo };
183f9790aebSLuigi Rizzo 
184f9790aebSLuigi Rizzo #define RATE_PRINTK(_NAME_) \
185f9790aebSLuigi Rizzo 	printk( #_NAME_ " = %lu Hz\n", (cur._NAME_ - ctx->old._NAME_)/RATE_PERIOD);
186f9790aebSLuigi Rizzo #define RATE_PERIOD  2
187f9790aebSLuigi Rizzo static void rate_callback(unsigned long arg)
188f9790aebSLuigi Rizzo {
189f9790aebSLuigi Rizzo 	struct rate_context * ctx = (struct rate_context *)arg;
190f9790aebSLuigi Rizzo 	struct rate_stats cur = ctx->new;
191f9790aebSLuigi Rizzo 	int r;
192f9790aebSLuigi Rizzo 
193f9790aebSLuigi Rizzo 	RATE_PRINTK(txpkt);
194f9790aebSLuigi Rizzo 	RATE_PRINTK(txsync);
195f9790aebSLuigi Rizzo 	RATE_PRINTK(txirq);
196f9790aebSLuigi Rizzo 	RATE_PRINTK(rxpkt);
197f9790aebSLuigi Rizzo 	RATE_PRINTK(rxsync);
198f9790aebSLuigi Rizzo 	RATE_PRINTK(rxirq);
199f9790aebSLuigi Rizzo 	printk("\n");
200f9790aebSLuigi Rizzo 
201f9790aebSLuigi Rizzo 	ctx->old = cur;
202f9790aebSLuigi Rizzo 	r = mod_timer(&ctx->timer, jiffies +
203f9790aebSLuigi Rizzo 			msecs_to_jiffies(RATE_PERIOD * 1000));
204f9790aebSLuigi Rizzo 	if (unlikely(r))
205f9790aebSLuigi Rizzo 		D("[v1000] Error: mod_timer()");
206f9790aebSLuigi Rizzo }
207f9790aebSLuigi Rizzo 
208f9790aebSLuigi Rizzo static struct rate_context rate_ctx;
209f9790aebSLuigi Rizzo 
2104bf50f18SLuigi Rizzo void generic_rate(int txp, int txs, int txi, int rxp, int rxs, int rxi)
2114bf50f18SLuigi Rizzo {
2124bf50f18SLuigi Rizzo     if (txp) rate_ctx.new.txpkt++;
2134bf50f18SLuigi Rizzo     if (txs) rate_ctx.new.txsync++;
2144bf50f18SLuigi Rizzo     if (txi) rate_ctx.new.txirq++;
2154bf50f18SLuigi Rizzo     if (rxp) rate_ctx.new.rxpkt++;
2164bf50f18SLuigi Rizzo     if (rxs) rate_ctx.new.rxsync++;
2174bf50f18SLuigi Rizzo     if (rxi) rate_ctx.new.rxirq++;
2184bf50f18SLuigi Rizzo }
2194bf50f18SLuigi Rizzo 
220f9790aebSLuigi Rizzo #else /* !RATE */
221f9790aebSLuigi Rizzo #define IFRATE(x)
222f9790aebSLuigi Rizzo #endif /* !RATE */
223f9790aebSLuigi Rizzo 
224f9790aebSLuigi Rizzo 
225f9790aebSLuigi Rizzo /* =============== GENERIC NETMAP ADAPTER SUPPORT ================= */
226f9790aebSLuigi Rizzo 
227f9790aebSLuigi Rizzo /*
228f9790aebSLuigi Rizzo  * Wrapper used by the generic adapter layer to notify
229f9790aebSLuigi Rizzo  * the poller threads. Differently from netmap_rx_irq(), we check
2304bf50f18SLuigi Rizzo  * only NAF_NETMAP_ON instead of NAF_NATIVE_ON to enable the irq.
231f9790aebSLuigi Rizzo  */
232f9790aebSLuigi Rizzo static void
233f9790aebSLuigi Rizzo netmap_generic_irq(struct ifnet *ifp, u_int q, u_int *work_done)
234f9790aebSLuigi Rizzo {
2354bf50f18SLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
2364bf50f18SLuigi Rizzo 	if (unlikely(!nm_netmap_on(na)))
237f9790aebSLuigi Rizzo 		return;
238f9790aebSLuigi Rizzo 
239f9790aebSLuigi Rizzo 	netmap_common_irq(ifp, q, work_done);
240f9790aebSLuigi Rizzo }
241f9790aebSLuigi Rizzo 
242f9790aebSLuigi Rizzo 
243f9790aebSLuigi Rizzo /* Enable/disable netmap mode for a generic network interface. */
24417885a7bSLuigi Rizzo static int
24517885a7bSLuigi Rizzo generic_netmap_register(struct netmap_adapter *na, int enable)
246f9790aebSLuigi Rizzo {
247f9790aebSLuigi Rizzo 	struct netmap_generic_adapter *gna = (struct netmap_generic_adapter *)na;
248f9790aebSLuigi Rizzo 	struct mbuf *m;
249f9790aebSLuigi Rizzo 	int error;
250f9790aebSLuigi Rizzo 	int i, r;
251f9790aebSLuigi Rizzo 
252f9790aebSLuigi Rizzo 	if (!na)
253f9790aebSLuigi Rizzo 		return EINVAL;
254f9790aebSLuigi Rizzo 
255f9790aebSLuigi Rizzo #ifdef REG_RESET
256f9790aebSLuigi Rizzo 	error = ifp->netdev_ops->ndo_stop(ifp);
257f9790aebSLuigi Rizzo 	if (error) {
258f9790aebSLuigi Rizzo 		return error;
259f9790aebSLuigi Rizzo 	}
260f9790aebSLuigi Rizzo #endif /* REG_RESET */
261f9790aebSLuigi Rizzo 
262f9790aebSLuigi Rizzo 	if (enable) { /* Enable netmap mode. */
263e4166283SLuigi Rizzo 		/* Init the mitigation support on all the rx queues. */
264f0ea3689SLuigi Rizzo 		gna->mit = malloc(na->num_rx_rings * sizeof(struct nm_generic_mit),
265f0ea3689SLuigi Rizzo 					M_DEVBUF, M_NOWAIT | M_ZERO);
266f0ea3689SLuigi Rizzo 		if (!gna->mit) {
267f0ea3689SLuigi Rizzo 			D("mitigation allocation failed");
268f0ea3689SLuigi Rizzo 			error = ENOMEM;
269f0ea3689SLuigi Rizzo 			goto out;
270f0ea3689SLuigi Rizzo 		}
271f0ea3689SLuigi Rizzo 		for (r=0; r<na->num_rx_rings; r++)
2724bf50f18SLuigi Rizzo 			netmap_mitigation_init(&gna->mit[r], r, na);
273f0ea3689SLuigi Rizzo 
274f9790aebSLuigi Rizzo 		/* Initialize the rx queue, as generic_rx_handler() can
275f9790aebSLuigi Rizzo 		 * be called as soon as netmap_catch_rx() returns.
276f9790aebSLuigi Rizzo 		 */
277f9790aebSLuigi Rizzo 		for (r=0; r<na->num_rx_rings; r++) {
278f9790aebSLuigi Rizzo 			mbq_safe_init(&na->rx_rings[r].rx_queue);
279f9790aebSLuigi Rizzo 		}
280f9790aebSLuigi Rizzo 
281f9790aebSLuigi Rizzo 		/*
282f9790aebSLuigi Rizzo 		 * Preallocate packet buffers for the tx rings.
283f9790aebSLuigi Rizzo 		 */
28417885a7bSLuigi Rizzo 		for (r=0; r<na->num_tx_rings; r++)
28517885a7bSLuigi Rizzo 			na->tx_rings[r].tx_pool = NULL;
286f9790aebSLuigi Rizzo 		for (r=0; r<na->num_tx_rings; r++) {
287f9790aebSLuigi Rizzo 			na->tx_rings[r].tx_pool = malloc(na->num_tx_desc * sizeof(struct mbuf *),
288f9790aebSLuigi Rizzo 					M_DEVBUF, M_NOWAIT | M_ZERO);
289f9790aebSLuigi Rizzo 			if (!na->tx_rings[r].tx_pool) {
290f9790aebSLuigi Rizzo 				D("tx_pool allocation failed");
291f9790aebSLuigi Rizzo 				error = ENOMEM;
29217885a7bSLuigi Rizzo 				goto free_tx_pools;
293f9790aebSLuigi Rizzo 			}
29417885a7bSLuigi Rizzo 			for (i=0; i<na->num_tx_desc; i++)
29517885a7bSLuigi Rizzo 				na->tx_rings[r].tx_pool[i] = NULL;
296f9790aebSLuigi Rizzo 			for (i=0; i<na->num_tx_desc; i++) {
2974bf50f18SLuigi Rizzo 				m = netmap_get_mbuf(NETMAP_BUF_SIZE(na));
298f9790aebSLuigi Rizzo 				if (!m) {
299f9790aebSLuigi Rizzo 					D("tx_pool[%d] allocation failed", i);
300f9790aebSLuigi Rizzo 					error = ENOMEM;
30117885a7bSLuigi Rizzo 					goto free_tx_pools;
302f9790aebSLuigi Rizzo 				}
303f9790aebSLuigi Rizzo 				na->tx_rings[r].tx_pool[i] = m;
304f9790aebSLuigi Rizzo 			}
305f9790aebSLuigi Rizzo 		}
306f9790aebSLuigi Rizzo 		rtnl_lock();
307f9790aebSLuigi Rizzo 		/* Prepare to intercept incoming traffic. */
308f9790aebSLuigi Rizzo 		error = netmap_catch_rx(na, 1);
309f9790aebSLuigi Rizzo 		if (error) {
310f2637526SLuigi Rizzo 			D("netdev_rx_handler_register() failed (%d)", error);
311f9790aebSLuigi Rizzo 			goto register_handler;
312f9790aebSLuigi Rizzo 		}
3134bf50f18SLuigi Rizzo 		na->na_flags |= NAF_NETMAP_ON;
314f9790aebSLuigi Rizzo 
315f9790aebSLuigi Rizzo 		/* Make netmap control the packet steering. */
31617885a7bSLuigi Rizzo 		netmap_catch_tx(gna, 1);
317f9790aebSLuigi Rizzo 
318f9790aebSLuigi Rizzo 		rtnl_unlock();
319f9790aebSLuigi Rizzo 
3204bf50f18SLuigi Rizzo #ifdef RATE_GENERIC
321f9790aebSLuigi Rizzo 		if (rate_ctx.refcount == 0) {
322f9790aebSLuigi Rizzo 			D("setup_timer()");
323f9790aebSLuigi Rizzo 			memset(&rate_ctx, 0, sizeof(rate_ctx));
324f9790aebSLuigi Rizzo 			setup_timer(&rate_ctx.timer, &rate_callback, (unsigned long)&rate_ctx);
325f9790aebSLuigi Rizzo 			if (mod_timer(&rate_ctx.timer, jiffies + msecs_to_jiffies(1500))) {
326f9790aebSLuigi Rizzo 				D("Error: mod_timer()");
327f9790aebSLuigi Rizzo 			}
328f9790aebSLuigi Rizzo 		}
329f9790aebSLuigi Rizzo 		rate_ctx.refcount++;
330f9790aebSLuigi Rizzo #endif /* RATE */
331f9790aebSLuigi Rizzo 
332f2637526SLuigi Rizzo 	} else if (na->tx_rings[0].tx_pool) {
333f2637526SLuigi Rizzo 		/* Disable netmap mode. We enter here only if the previous
334f2637526SLuigi Rizzo 		   generic_netmap_register(na, 1) was successfull.
335f2637526SLuigi Rizzo 		   If it was not, na->tx_rings[0].tx_pool was set to NULL by the
336f2637526SLuigi Rizzo 		   error handling code below. */
337f9790aebSLuigi Rizzo 		rtnl_lock();
338f9790aebSLuigi Rizzo 
3394bf50f18SLuigi Rizzo 		na->na_flags &= ~NAF_NETMAP_ON;
340f9790aebSLuigi Rizzo 
341f9790aebSLuigi Rizzo 		/* Release packet steering control. */
34217885a7bSLuigi Rizzo 		netmap_catch_tx(gna, 0);
343f9790aebSLuigi Rizzo 
344f9790aebSLuigi Rizzo 		/* Do not intercept packets on the rx path. */
345f9790aebSLuigi Rizzo 		netmap_catch_rx(na, 0);
346f9790aebSLuigi Rizzo 
347f9790aebSLuigi Rizzo 		rtnl_unlock();
348f9790aebSLuigi Rizzo 
349f9790aebSLuigi Rizzo 		/* Free the mbufs going to the netmap rings */
350f9790aebSLuigi Rizzo 		for (r=0; r<na->num_rx_rings; r++) {
351f9790aebSLuigi Rizzo 			mbq_safe_purge(&na->rx_rings[r].rx_queue);
352f9790aebSLuigi Rizzo 			mbq_safe_destroy(&na->rx_rings[r].rx_queue);
353f9790aebSLuigi Rizzo 		}
354f9790aebSLuigi Rizzo 
355f0ea3689SLuigi Rizzo 		for (r=0; r<na->num_rx_rings; r++)
356f0ea3689SLuigi Rizzo 			netmap_mitigation_cleanup(&gna->mit[r]);
357f0ea3689SLuigi Rizzo 		free(gna->mit, M_DEVBUF);
358f9790aebSLuigi Rizzo 
359f9790aebSLuigi Rizzo 		for (r=0; r<na->num_tx_rings; r++) {
360f9790aebSLuigi Rizzo 			for (i=0; i<na->num_tx_desc; i++) {
361f9790aebSLuigi Rizzo 				m_freem(na->tx_rings[r].tx_pool[i]);
362f9790aebSLuigi Rizzo 			}
363f9790aebSLuigi Rizzo 			free(na->tx_rings[r].tx_pool, M_DEVBUF);
364f9790aebSLuigi Rizzo 		}
365f9790aebSLuigi Rizzo 
3664bf50f18SLuigi Rizzo #ifdef RATE_GENERIC
367f9790aebSLuigi Rizzo 		if (--rate_ctx.refcount == 0) {
368f9790aebSLuigi Rizzo 			D("del_timer()");
369f9790aebSLuigi Rizzo 			del_timer(&rate_ctx.timer);
370f9790aebSLuigi Rizzo 		}
371f9790aebSLuigi Rizzo #endif
372f9790aebSLuigi Rizzo 	}
373f9790aebSLuigi Rizzo 
374f9790aebSLuigi Rizzo #ifdef REG_RESET
375f9790aebSLuigi Rizzo 	error = ifp->netdev_ops->ndo_open(ifp);
376f9790aebSLuigi Rizzo 	if (error) {
377f2637526SLuigi Rizzo 		goto free_tx_pools;
378f9790aebSLuigi Rizzo 	}
379f9790aebSLuigi Rizzo #endif
380f9790aebSLuigi Rizzo 
381f9790aebSLuigi Rizzo 	return 0;
382f9790aebSLuigi Rizzo 
383f9790aebSLuigi Rizzo register_handler:
384f9790aebSLuigi Rizzo 	rtnl_unlock();
38517885a7bSLuigi Rizzo free_tx_pools:
38617885a7bSLuigi Rizzo 	for (r=0; r<na->num_tx_rings; r++) {
38717885a7bSLuigi Rizzo 		if (na->tx_rings[r].tx_pool == NULL)
38817885a7bSLuigi Rizzo 			continue;
38917885a7bSLuigi Rizzo 		for (i=0; i<na->num_tx_desc; i++)
39017885a7bSLuigi Rizzo 			if (na->tx_rings[r].tx_pool[i])
391f9790aebSLuigi Rizzo 				m_freem(na->tx_rings[r].tx_pool[i]);
392f9790aebSLuigi Rizzo 		free(na->tx_rings[r].tx_pool, M_DEVBUF);
393f2637526SLuigi Rizzo 		na->tx_rings[r].tx_pool = NULL;
394f2637526SLuigi Rizzo 	}
395f2637526SLuigi Rizzo 	for (r=0; r<na->num_rx_rings; r++) {
396f0ea3689SLuigi Rizzo 		netmap_mitigation_cleanup(&gna->mit[r]);
397f2637526SLuigi Rizzo 		mbq_safe_destroy(&na->rx_rings[r].rx_queue);
398f9790aebSLuigi Rizzo 	}
399f0ea3689SLuigi Rizzo 	free(gna->mit, M_DEVBUF);
400f0ea3689SLuigi Rizzo out:
401f9790aebSLuigi Rizzo 
402f9790aebSLuigi Rizzo 	return error;
403f9790aebSLuigi Rizzo }
404f9790aebSLuigi Rizzo 
405f9790aebSLuigi Rizzo /*
406f9790aebSLuigi Rizzo  * Callback invoked when the device driver frees an mbuf used
407f9790aebSLuigi Rizzo  * by netmap to transmit a packet. This usually happens when
408f9790aebSLuigi Rizzo  * the NIC notifies the driver that transmission is completed.
409f9790aebSLuigi Rizzo  */
410f9790aebSLuigi Rizzo static void
411f9790aebSLuigi Rizzo generic_mbuf_destructor(struct mbuf *m)
412f9790aebSLuigi Rizzo {
413f9790aebSLuigi Rizzo 	netmap_generic_irq(MBUF_IFP(m), MBUF_TXQ(m), NULL);
414f9790aebSLuigi Rizzo #ifdef __FreeBSD__
415e4166283SLuigi Rizzo 	if (netmap_verbose)
416e4166283SLuigi Rizzo 		RD(5, "Tx irq (%p) queue %d index %d" , m, MBUF_TXQ(m), (int)(uintptr_t)m->m_ext.ext_arg1);
417e4166283SLuigi Rizzo 	netmap_default_mbuf_destructor(m);
418f9790aebSLuigi Rizzo #endif /* __FreeBSD__ */
419f9790aebSLuigi Rizzo 	IFRATE(rate_ctx.new.txirq++);
420f9790aebSLuigi Rizzo }
421f9790aebSLuigi Rizzo 
4224bf50f18SLuigi Rizzo extern int netmap_adaptive_io;
4234bf50f18SLuigi Rizzo 
42417885a7bSLuigi Rizzo /* Record completed transmissions and update hwtail.
425f9790aebSLuigi Rizzo  *
42617885a7bSLuigi Rizzo  * The oldest tx buffer not yet completed is at nr_hwtail + 1,
427f9790aebSLuigi Rizzo  * nr_hwcur is the first unsent buffer.
428f9790aebSLuigi Rizzo  */
42917885a7bSLuigi Rizzo static u_int
430f9790aebSLuigi Rizzo generic_netmap_tx_clean(struct netmap_kring *kring)
431f9790aebSLuigi Rizzo {
43217885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
43317885a7bSLuigi Rizzo 	u_int nm_i = nm_next(kring->nr_hwtail, lim);
434f9790aebSLuigi Rizzo 	u_int hwcur = kring->nr_hwcur;
435f9790aebSLuigi Rizzo 	u_int n = 0;
436f9790aebSLuigi Rizzo 	struct mbuf **tx_pool = kring->tx_pool;
437f9790aebSLuigi Rizzo 
43817885a7bSLuigi Rizzo 	while (nm_i != hwcur) { /* buffers not completed */
43917885a7bSLuigi Rizzo 		struct mbuf *m = tx_pool[nm_i];
440f9790aebSLuigi Rizzo 
441f9790aebSLuigi Rizzo 		if (unlikely(m == NULL)) {
44217885a7bSLuigi Rizzo 			/* this is done, try to replenish the entry */
4434bf50f18SLuigi Rizzo 			tx_pool[nm_i] = m = netmap_get_mbuf(NETMAP_BUF_SIZE(kring->na));
444f9790aebSLuigi Rizzo 			if (unlikely(m == NULL)) {
445f9790aebSLuigi Rizzo 				D("mbuf allocation failed, XXX error");
446f9790aebSLuigi Rizzo 				// XXX how do we proceed ? break ?
447f9790aebSLuigi Rizzo 				return -ENOMEM;
448f9790aebSLuigi Rizzo 			}
449f9790aebSLuigi Rizzo 		} else if (GET_MBUF_REFCNT(m) != 1) {
450f9790aebSLuigi Rizzo 			break; /* This mbuf is still busy: its refcnt is 2. */
451f9790aebSLuigi Rizzo 		}
452f9790aebSLuigi Rizzo 		n++;
45317885a7bSLuigi Rizzo 		nm_i = nm_next(nm_i, lim);
4544bf50f18SLuigi Rizzo #if 0 /* rate adaptation */
4554bf50f18SLuigi Rizzo 		if (netmap_adaptive_io > 1) {
4564bf50f18SLuigi Rizzo 			if (n >= netmap_adaptive_io)
4574bf50f18SLuigi Rizzo 				break;
4584bf50f18SLuigi Rizzo 		} else if (netmap_adaptive_io) {
4594bf50f18SLuigi Rizzo 			/* if hwcur - nm_i < lim/8 do an early break
4604bf50f18SLuigi Rizzo 			 * so we prevent the sender from stalling. See CVT.
4614bf50f18SLuigi Rizzo 			 */
4624bf50f18SLuigi Rizzo 			if (hwcur >= nm_i) {
4634bf50f18SLuigi Rizzo 				if (hwcur - nm_i < lim/2)
4644bf50f18SLuigi Rizzo 					break;
4654bf50f18SLuigi Rizzo 			} else {
4664bf50f18SLuigi Rizzo 				if (hwcur + lim + 1 - nm_i < lim/2)
4674bf50f18SLuigi Rizzo 					break;
4684bf50f18SLuigi Rizzo 			}
4694bf50f18SLuigi Rizzo 		}
4704bf50f18SLuigi Rizzo #endif
471f9790aebSLuigi Rizzo 	}
47217885a7bSLuigi Rizzo 	kring->nr_hwtail = nm_prev(nm_i, lim);
47317885a7bSLuigi Rizzo 	ND("tx completed [%d] -> hwtail %d", n, kring->nr_hwtail);
474f9790aebSLuigi Rizzo 
475f9790aebSLuigi Rizzo 	return n;
476f9790aebSLuigi Rizzo }
477f9790aebSLuigi Rizzo 
478f9790aebSLuigi Rizzo 
479f9790aebSLuigi Rizzo /*
48017885a7bSLuigi Rizzo  * We have pending packets in the driver between nr_hwtail +1 and hwcur.
481f9790aebSLuigi Rizzo  * Compute a position in the middle, to be used to generate
482f9790aebSLuigi Rizzo  * a notification.
483f9790aebSLuigi Rizzo  */
484f9790aebSLuigi Rizzo static inline u_int
485f9790aebSLuigi Rizzo generic_tx_event_middle(struct netmap_kring *kring, u_int hwcur)
486f9790aebSLuigi Rizzo {
487f9790aebSLuigi Rizzo 	u_int n = kring->nkr_num_slots;
48817885a7bSLuigi Rizzo 	u_int ntc = nm_next(kring->nr_hwtail, n-1);
489f9790aebSLuigi Rizzo 	u_int e;
490f9790aebSLuigi Rizzo 
491f9790aebSLuigi Rizzo 	if (hwcur >= ntc) {
492f9790aebSLuigi Rizzo 		e = (hwcur + ntc) / 2;
493f9790aebSLuigi Rizzo 	} else { /* wrap around */
494f9790aebSLuigi Rizzo 		e = (hwcur + n + ntc) / 2;
495f9790aebSLuigi Rizzo 		if (e >= n) {
496f9790aebSLuigi Rizzo 			e -= n;
497f9790aebSLuigi Rizzo 		}
498f9790aebSLuigi Rizzo 	}
499f9790aebSLuigi Rizzo 
500f9790aebSLuigi Rizzo 	if (unlikely(e >= n)) {
501f9790aebSLuigi Rizzo 		D("This cannot happen");
502f9790aebSLuigi Rizzo 		e = 0;
503f9790aebSLuigi Rizzo 	}
504f9790aebSLuigi Rizzo 
505f9790aebSLuigi Rizzo 	return e;
506f9790aebSLuigi Rizzo }
507f9790aebSLuigi Rizzo 
508f9790aebSLuigi Rizzo /*
50917885a7bSLuigi Rizzo  * We have pending packets in the driver between nr_hwtail+1 and hwcur.
510f9790aebSLuigi Rizzo  * Schedule a notification approximately in the middle of the two.
511f9790aebSLuigi Rizzo  * There is a race but this is only called within txsync which does
512f9790aebSLuigi Rizzo  * a double check.
513f9790aebSLuigi Rizzo  */
514f9790aebSLuigi Rizzo static void
515f9790aebSLuigi Rizzo generic_set_tx_event(struct netmap_kring *kring, u_int hwcur)
516f9790aebSLuigi Rizzo {
517f9790aebSLuigi Rizzo 	struct mbuf *m;
518f9790aebSLuigi Rizzo 	u_int e;
519f9790aebSLuigi Rizzo 
52017885a7bSLuigi Rizzo 	if (nm_next(kring->nr_hwtail, kring->nkr_num_slots -1) == hwcur) {
52117885a7bSLuigi Rizzo 		return; /* all buffers are free */
522f9790aebSLuigi Rizzo 	}
523f9790aebSLuigi Rizzo 	e = generic_tx_event_middle(kring, hwcur);
524f9790aebSLuigi Rizzo 
525f9790aebSLuigi Rizzo 	m = kring->tx_pool[e];
526e4166283SLuigi Rizzo 	ND(5, "Request Event at %d mbuf %p refcnt %d", e, m, m ? GET_MBUF_REFCNT(m) : -2 );
527f9790aebSLuigi Rizzo 	if (m == NULL) {
528f9790aebSLuigi Rizzo 		/* This can happen if there is already an event on the netmap
529f9790aebSLuigi Rizzo 		   slot 'e': There is nothing to do. */
530f9790aebSLuigi Rizzo 		return;
531f9790aebSLuigi Rizzo 	}
532f9790aebSLuigi Rizzo 	kring->tx_pool[e] = NULL;
533f9790aebSLuigi Rizzo 	SET_MBUF_DESTRUCTOR(m, generic_mbuf_destructor);
534f9790aebSLuigi Rizzo 
535f9790aebSLuigi Rizzo 	// XXX wmb() ?
536f9790aebSLuigi Rizzo 	/* Decrement the refcount an free it if we have the last one. */
537f9790aebSLuigi Rizzo 	m_freem(m);
538f9790aebSLuigi Rizzo 	smp_mb();
539f9790aebSLuigi Rizzo }
540f9790aebSLuigi Rizzo 
541f9790aebSLuigi Rizzo 
542f9790aebSLuigi Rizzo /*
543f9790aebSLuigi Rizzo  * generic_netmap_txsync() transforms netmap buffers into mbufs
544f9790aebSLuigi Rizzo  * and passes them to the standard device driver
545f9790aebSLuigi Rizzo  * (ndo_start_xmit() or ifp->if_transmit() ).
546f9790aebSLuigi Rizzo  * On linux this is not done directly, but using dev_queue_xmit(),
547f9790aebSLuigi Rizzo  * since it implements the TX flow control (and takes some locks).
548f9790aebSLuigi Rizzo  */
549f9790aebSLuigi Rizzo static int
5504bf50f18SLuigi Rizzo generic_netmap_txsync(struct netmap_kring *kring, int flags)
551f9790aebSLuigi Rizzo {
5524bf50f18SLuigi Rizzo 	struct netmap_adapter *na = kring->na;
553f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
554f9790aebSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
55517885a7bSLuigi Rizzo 	u_int nm_i;	/* index into the netmap ring */ // j
55617885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
55717885a7bSLuigi Rizzo 	u_int const head = kring->rhead;
5584bf50f18SLuigi Rizzo 	u_int ring_nr = kring->ring_id;
559f9790aebSLuigi Rizzo 
560f9790aebSLuigi Rizzo 	IFRATE(rate_ctx.new.txsync++);
561f9790aebSLuigi Rizzo 
562f9790aebSLuigi Rizzo 	// TODO: handle the case of mbuf allocation failure
563f9790aebSLuigi Rizzo 
564f9790aebSLuigi Rizzo 	rmb();
56517885a7bSLuigi Rizzo 
566f9790aebSLuigi Rizzo 	/*
56717885a7bSLuigi Rizzo 	 * First part: process new packets to send.
568f9790aebSLuigi Rizzo 	 */
56917885a7bSLuigi Rizzo 	nm_i = kring->nr_hwcur;
57017885a7bSLuigi Rizzo 	if (nm_i != head) {	/* we have new packets to send */
57117885a7bSLuigi Rizzo 		while (nm_i != head) {
57217885a7bSLuigi Rizzo 			struct netmap_slot *slot = &ring->slot[nm_i];
573f9790aebSLuigi Rizzo 			u_int len = slot->len;
5744bf50f18SLuigi Rizzo 			void *addr = NMB(na, slot);
57517885a7bSLuigi Rizzo 
57617885a7bSLuigi Rizzo 			/* device-specific */
577f9790aebSLuigi Rizzo 			struct mbuf *m;
578f9790aebSLuigi Rizzo 			int tx_ret;
579f9790aebSLuigi Rizzo 
5804bf50f18SLuigi Rizzo 			NM_CHECK_ADDR_LEN(na, addr, len);
58117885a7bSLuigi Rizzo 
582f9790aebSLuigi Rizzo 			/* Tale a mbuf from the tx pool and copy in the user packet. */
58317885a7bSLuigi Rizzo 			m = kring->tx_pool[nm_i];
584f9790aebSLuigi Rizzo 			if (unlikely(!m)) {
585f9790aebSLuigi Rizzo 				RD(5, "This should never happen");
5864bf50f18SLuigi Rizzo 				kring->tx_pool[nm_i] = m = netmap_get_mbuf(NETMAP_BUF_SIZE(na));
587f9790aebSLuigi Rizzo 				if (unlikely(m == NULL)) {
588f9790aebSLuigi Rizzo 					D("mbuf allocation failed");
589f9790aebSLuigi Rizzo 					break;
590f9790aebSLuigi Rizzo 				}
591f9790aebSLuigi Rizzo 			}
592f9790aebSLuigi Rizzo 			/* XXX we should ask notifications when NS_REPORT is set,
593f9790aebSLuigi Rizzo 			 * or roughly every half frame. We can optimize this
594f9790aebSLuigi Rizzo 			 * by lazily requesting notifications only when a
595f9790aebSLuigi Rizzo 			 * transmission fails. Probably the best way is to
596f9790aebSLuigi Rizzo 			 * break on failures and set notifications when
59717885a7bSLuigi Rizzo 			 * ring->cur == ring->tail || nm_i != cur
598f9790aebSLuigi Rizzo 			 */
599f9790aebSLuigi Rizzo 			tx_ret = generic_xmit_frame(ifp, m, addr, len, ring_nr);
600f9790aebSLuigi Rizzo 			if (unlikely(tx_ret)) {
6014bf50f18SLuigi Rizzo 				ND(5, "start_xmit failed: err %d [nm_i %u, head %u, hwtail %u]",
60217885a7bSLuigi Rizzo 						tx_ret, nm_i, head, kring->nr_hwtail);
603f9790aebSLuigi Rizzo 				/*
604f9790aebSLuigi Rizzo 				 * No room for this mbuf in the device driver.
605f9790aebSLuigi Rizzo 				 * Request a notification FOR A PREVIOUS MBUF,
606f9790aebSLuigi Rizzo 				 * then call generic_netmap_tx_clean(kring) to do the
607f9790aebSLuigi Rizzo 				 * double check and see if we can free more buffers.
608f9790aebSLuigi Rizzo 				 * If there is space continue, else break;
609f9790aebSLuigi Rizzo 				 * NOTE: the double check is necessary if the problem
610f9790aebSLuigi Rizzo 				 * occurs in the txsync call after selrecord().
611f9790aebSLuigi Rizzo 				 * Also, we need some way to tell the caller that not
612f9790aebSLuigi Rizzo 				 * all buffers were queued onto the device (this was
613f9790aebSLuigi Rizzo 				 * not a problem with native netmap driver where space
614f9790aebSLuigi Rizzo 				 * is preallocated). The bridge has a similar problem
615f9790aebSLuigi Rizzo 				 * and we solve it there by dropping the excess packets.
616f9790aebSLuigi Rizzo 				 */
61717885a7bSLuigi Rizzo 				generic_set_tx_event(kring, nm_i);
618f9790aebSLuigi Rizzo 				if (generic_netmap_tx_clean(kring)) { /* space now available */
619f9790aebSLuigi Rizzo 					continue;
620f9790aebSLuigi Rizzo 				} else {
621f9790aebSLuigi Rizzo 					break;
622f9790aebSLuigi Rizzo 				}
623f9790aebSLuigi Rizzo 			}
624f9790aebSLuigi Rizzo 			slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED);
62517885a7bSLuigi Rizzo 			nm_i = nm_next(nm_i, lim);
626f0ea3689SLuigi Rizzo 			IFRATE(rate_ctx.new.txpkt ++);
627f9790aebSLuigi Rizzo 		}
628f9790aebSLuigi Rizzo 
629f9790aebSLuigi Rizzo 		/* Update hwcur to the next slot to transmit. */
63017885a7bSLuigi Rizzo 		kring->nr_hwcur = nm_i; /* not head, we could break early */
63117885a7bSLuigi Rizzo 	}
632f9790aebSLuigi Rizzo 
63317885a7bSLuigi Rizzo 	/*
63417885a7bSLuigi Rizzo 	 * Second, reclaim completed buffers
63517885a7bSLuigi Rizzo 	 */
63617885a7bSLuigi Rizzo 	if (flags & NAF_FORCE_RECLAIM || nm_kr_txempty(kring)) {
637f9790aebSLuigi Rizzo 		/* No more available slots? Set a notification event
638f9790aebSLuigi Rizzo 		 * on a netmap slot that will be cleaned in the future.
639f9790aebSLuigi Rizzo 		 * No doublecheck is performed, since txsync() will be
640f9790aebSLuigi Rizzo 		 * called twice by netmap_poll().
641f9790aebSLuigi Rizzo 		 */
64217885a7bSLuigi Rizzo 		generic_set_tx_event(kring, nm_i);
643f9790aebSLuigi Rizzo 	}
64417885a7bSLuigi Rizzo 	ND("tx #%d, hwtail = %d", n, kring->nr_hwtail);
645f9790aebSLuigi Rizzo 
64617885a7bSLuigi Rizzo 	generic_netmap_tx_clean(kring);
64717885a7bSLuigi Rizzo 
64817885a7bSLuigi Rizzo 	nm_txsync_finalize(kring);
649f9790aebSLuigi Rizzo 
650f9790aebSLuigi Rizzo 	return 0;
651f9790aebSLuigi Rizzo }
652f9790aebSLuigi Rizzo 
65317885a7bSLuigi Rizzo 
654f9790aebSLuigi Rizzo /*
655f9790aebSLuigi Rizzo  * This handler is registered (through netmap_catch_rx())
656f9790aebSLuigi Rizzo  * within the attached network interface
657f9790aebSLuigi Rizzo  * in the RX subsystem, so that every mbuf passed up by
658f9790aebSLuigi Rizzo  * the driver can be stolen to the network stack.
659f9790aebSLuigi Rizzo  * Stolen packets are put in a queue where the
660f9790aebSLuigi Rizzo  * generic_netmap_rxsync() callback can extract them.
661f9790aebSLuigi Rizzo  */
66217885a7bSLuigi Rizzo void
66317885a7bSLuigi Rizzo generic_rx_handler(struct ifnet *ifp, struct mbuf *m)
664f9790aebSLuigi Rizzo {
665f9790aebSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
666f9790aebSLuigi Rizzo 	struct netmap_generic_adapter *gna = (struct netmap_generic_adapter *)na;
667f9790aebSLuigi Rizzo 	u_int work_done;
668f0ea3689SLuigi Rizzo 	u_int rr = MBUF_RXQ(m); // receive ring number
669f0ea3689SLuigi Rizzo 
670f0ea3689SLuigi Rizzo 	if (rr >= na->num_rx_rings) {
671f0ea3689SLuigi Rizzo 		rr = rr % na->num_rx_rings; // XXX expensive...
672f0ea3689SLuigi Rizzo 	}
673f9790aebSLuigi Rizzo 
674f9790aebSLuigi Rizzo 	/* limit the size of the queue */
675f9790aebSLuigi Rizzo 	if (unlikely(mbq_len(&na->rx_rings[rr].rx_queue) > 1024)) {
676f9790aebSLuigi Rizzo 		m_freem(m);
677f9790aebSLuigi Rizzo 	} else {
678f9790aebSLuigi Rizzo 		mbq_safe_enqueue(&na->rx_rings[rr].rx_queue, m);
679f9790aebSLuigi Rizzo 	}
680f9790aebSLuigi Rizzo 
681f9790aebSLuigi Rizzo 	if (netmap_generic_mit < 32768) {
682f9790aebSLuigi Rizzo 		/* no rx mitigation, pass notification up */
683f9790aebSLuigi Rizzo 		netmap_generic_irq(na->ifp, rr, &work_done);
684f9790aebSLuigi Rizzo 		IFRATE(rate_ctx.new.rxirq++);
685f9790aebSLuigi Rizzo 	} else {
686f9790aebSLuigi Rizzo 		/* same as send combining, filter notification if there is a
687f9790aebSLuigi Rizzo 		 * pending timer, otherwise pass it up and start a timer.
688f9790aebSLuigi Rizzo 		 */
689f0ea3689SLuigi Rizzo 		if (likely(netmap_mitigation_active(&gna->mit[rr]))) {
690f9790aebSLuigi Rizzo 			/* Record that there is some pending work. */
691f0ea3689SLuigi Rizzo 			gna->mit[rr].mit_pending = 1;
692f9790aebSLuigi Rizzo 		} else {
693f9790aebSLuigi Rizzo 			netmap_generic_irq(na->ifp, rr, &work_done);
694f9790aebSLuigi Rizzo 			IFRATE(rate_ctx.new.rxirq++);
695f0ea3689SLuigi Rizzo 			netmap_mitigation_start(&gna->mit[rr]);
696f9790aebSLuigi Rizzo 		}
697f9790aebSLuigi Rizzo 	}
698f9790aebSLuigi Rizzo }
699f9790aebSLuigi Rizzo 
700f9790aebSLuigi Rizzo /*
701f9790aebSLuigi Rizzo  * generic_netmap_rxsync() extracts mbufs from the queue filled by
702f9790aebSLuigi Rizzo  * generic_netmap_rx_handler() and puts their content in the netmap
703f9790aebSLuigi Rizzo  * receive ring.
704f9790aebSLuigi Rizzo  * Access must be protected because the rx handler is asynchronous,
705f9790aebSLuigi Rizzo  */
706f9790aebSLuigi Rizzo static int
7074bf50f18SLuigi Rizzo generic_netmap_rxsync(struct netmap_kring *kring, int flags)
708f9790aebSLuigi Rizzo {
709f9790aebSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
7104bf50f18SLuigi Rizzo 	struct netmap_adapter *na = kring->na;
71117885a7bSLuigi Rizzo 	u_int nm_i;	/* index into the netmap ring */ //j,
71217885a7bSLuigi Rizzo 	u_int n;
71317885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
71417885a7bSLuigi Rizzo 	u_int const head = nm_rxsync_prologue(kring);
715f9790aebSLuigi Rizzo 	int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR;
716f9790aebSLuigi Rizzo 
71717885a7bSLuigi Rizzo 	if (head > lim)
718f9790aebSLuigi Rizzo 		return netmap_ring_reinit(kring);
719f9790aebSLuigi Rizzo 
72017885a7bSLuigi Rizzo 	/*
72117885a7bSLuigi Rizzo 	 * First part: import newly received packets.
72217885a7bSLuigi Rizzo 	 */
723f9790aebSLuigi Rizzo 	if (netmap_no_pendintr || force_update) {
72417885a7bSLuigi Rizzo 		/* extract buffers from the rx queue, stop at most one
72517885a7bSLuigi Rizzo 		 * slot before nr_hwcur (stop_i)
72617885a7bSLuigi Rizzo 		 */
727f9790aebSLuigi Rizzo 		uint16_t slot_flags = kring->nkr_slot_flags;
72817885a7bSLuigi Rizzo 		u_int stop_i = nm_prev(kring->nr_hwcur, lim);
72917885a7bSLuigi Rizzo 
73017885a7bSLuigi Rizzo 		nm_i = kring->nr_hwtail; /* first empty slot in the receive ring */
73117885a7bSLuigi Rizzo 		for (n = 0; nm_i != stop_i; n++) {
73217885a7bSLuigi Rizzo 			int len;
7334bf50f18SLuigi Rizzo 			void *addr = NMB(na, &ring->slot[nm_i]);
734f9790aebSLuigi Rizzo 			struct mbuf *m;
735f9790aebSLuigi Rizzo 
73617885a7bSLuigi Rizzo 			/* we only check the address here on generic rx rings */
7374bf50f18SLuigi Rizzo 			if (addr == NETMAP_BUF_BASE(na)) { /* Bad buffer */
738f9790aebSLuigi Rizzo 				return netmap_ring_reinit(kring);
739f9790aebSLuigi Rizzo 			}
740f9790aebSLuigi Rizzo 			/*
741f9790aebSLuigi Rizzo 			 * Call the locked version of the function.
74217885a7bSLuigi Rizzo 			 * XXX Ideally we could grab a batch of mbufs at once
74317885a7bSLuigi Rizzo 			 * and save some locking overhead.
744f9790aebSLuigi Rizzo 			 */
745f9790aebSLuigi Rizzo 			m = mbq_safe_dequeue(&kring->rx_queue);
74617885a7bSLuigi Rizzo 			if (!m)	/* no more data */
747f9790aebSLuigi Rizzo 				break;
748f9790aebSLuigi Rizzo 			len = MBUF_LEN(m);
749f9790aebSLuigi Rizzo 			m_copydata(m, 0, len, addr);
75017885a7bSLuigi Rizzo 			ring->slot[nm_i].len = len;
75117885a7bSLuigi Rizzo 			ring->slot[nm_i].flags = slot_flags;
752f9790aebSLuigi Rizzo 			m_freem(m);
75317885a7bSLuigi Rizzo 			nm_i = nm_next(nm_i, lim);
754f9790aebSLuigi Rizzo 		}
755f9790aebSLuigi Rizzo 		if (n) {
75617885a7bSLuigi Rizzo 			kring->nr_hwtail = nm_i;
757f9790aebSLuigi Rizzo 			IFRATE(rate_ctx.new.rxpkt += n);
758f9790aebSLuigi Rizzo 		}
759f9790aebSLuigi Rizzo 		kring->nr_kflags &= ~NKR_PENDINTR;
760f9790aebSLuigi Rizzo 	}
761f9790aebSLuigi Rizzo 
762f9790aebSLuigi Rizzo 	// XXX should we invert the order ?
76317885a7bSLuigi Rizzo 	/*
76417885a7bSLuigi Rizzo 	 * Second part: skip past packets that userspace has released.
76517885a7bSLuigi Rizzo 	 */
76617885a7bSLuigi Rizzo 	nm_i = kring->nr_hwcur;
76717885a7bSLuigi Rizzo 	if (nm_i != head) {
768f9790aebSLuigi Rizzo 		/* Userspace has released some packets. */
76917885a7bSLuigi Rizzo 		for (n = 0; nm_i != head; n++) {
77017885a7bSLuigi Rizzo 			struct netmap_slot *slot = &ring->slot[nm_i];
771f9790aebSLuigi Rizzo 
772f9790aebSLuigi Rizzo 			slot->flags &= ~NS_BUF_CHANGED;
77317885a7bSLuigi Rizzo 			nm_i = nm_next(nm_i, lim);
774f9790aebSLuigi Rizzo 		}
77517885a7bSLuigi Rizzo 		kring->nr_hwcur = head;
776f9790aebSLuigi Rizzo 	}
77717885a7bSLuigi Rizzo 	/* tell userspace that there might be new packets. */
77817885a7bSLuigi Rizzo 	nm_rxsync_finalize(kring);
779f9790aebSLuigi Rizzo 	IFRATE(rate_ctx.new.rxsync++);
780f9790aebSLuigi Rizzo 
781f9790aebSLuigi Rizzo 	return 0;
782f9790aebSLuigi Rizzo }
783f9790aebSLuigi Rizzo 
784f9790aebSLuigi Rizzo static void
785f9790aebSLuigi Rizzo generic_netmap_dtor(struct netmap_adapter *na)
786f9790aebSLuigi Rizzo {
787f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
788f9790aebSLuigi Rizzo 	struct netmap_generic_adapter *gna = (struct netmap_generic_adapter*)na;
789f9790aebSLuigi Rizzo 	struct netmap_adapter *prev_na = gna->prev;
790f9790aebSLuigi Rizzo 
791f9790aebSLuigi Rizzo 	if (prev_na != NULL) {
792f9790aebSLuigi Rizzo 		D("Released generic NA %p", gna);
793f9790aebSLuigi Rizzo 		if_rele(na->ifp);
794f9790aebSLuigi Rizzo 		netmap_adapter_put(prev_na);
795f9790aebSLuigi Rizzo 	}
796f9790aebSLuigi Rizzo 	if (ifp != NULL) {
797f9790aebSLuigi Rizzo 		WNA(ifp) = prev_na;
798f9790aebSLuigi Rizzo 		D("Restored native NA %p", prev_na);
799f9790aebSLuigi Rizzo 		na->ifp = NULL;
800f9790aebSLuigi Rizzo 	}
801f9790aebSLuigi Rizzo }
802f9790aebSLuigi Rizzo 
803f9790aebSLuigi Rizzo /*
804f9790aebSLuigi Rizzo  * generic_netmap_attach() makes it possible to use netmap on
805f9790aebSLuigi Rizzo  * a device without native netmap support.
806f9790aebSLuigi Rizzo  * This is less performant than native support but potentially
807f9790aebSLuigi Rizzo  * faster than raw sockets or similar schemes.
808f9790aebSLuigi Rizzo  *
809f9790aebSLuigi Rizzo  * In this "emulated" mode, netmap rings do not necessarily
810f9790aebSLuigi Rizzo  * have the same size as those in the NIC. We use a default
811f9790aebSLuigi Rizzo  * value and possibly override it if the OS has ways to fetch the
812f9790aebSLuigi Rizzo  * actual configuration.
813f9790aebSLuigi Rizzo  */
814f9790aebSLuigi Rizzo int
815f9790aebSLuigi Rizzo generic_netmap_attach(struct ifnet *ifp)
816f9790aebSLuigi Rizzo {
817f9790aebSLuigi Rizzo 	struct netmap_adapter *na;
818f9790aebSLuigi Rizzo 	struct netmap_generic_adapter *gna;
819f9790aebSLuigi Rizzo 	int retval;
820f9790aebSLuigi Rizzo 	u_int num_tx_desc, num_rx_desc;
821f9790aebSLuigi Rizzo 
822f9790aebSLuigi Rizzo 	num_tx_desc = num_rx_desc = netmap_generic_ringsize; /* starting point */
823f9790aebSLuigi Rizzo 
824*db5cb211SLuigi Rizzo 	generic_find_num_desc(ifp, &num_tx_desc, &num_rx_desc); /* ignore errors */
825f9790aebSLuigi Rizzo 	ND("Netmap ring size: TX = %d, RX = %d", num_tx_desc, num_rx_desc);
826e4166283SLuigi Rizzo 	if (num_tx_desc == 0 || num_rx_desc == 0) {
827e4166283SLuigi Rizzo 		D("Device has no hw slots (tx %u, rx %u)", num_tx_desc, num_rx_desc);
828e4166283SLuigi Rizzo 		return EINVAL;
829e4166283SLuigi Rizzo 	}
830f9790aebSLuigi Rizzo 
831f9790aebSLuigi Rizzo 	gna = malloc(sizeof(*gna), M_DEVBUF, M_NOWAIT | M_ZERO);
832f9790aebSLuigi Rizzo 	if (gna == NULL) {
833f9790aebSLuigi Rizzo 		D("no memory on attach, give up");
834f9790aebSLuigi Rizzo 		return ENOMEM;
835f9790aebSLuigi Rizzo 	}
836f9790aebSLuigi Rizzo 	na = (struct netmap_adapter *)gna;
837f9790aebSLuigi Rizzo 	na->ifp = ifp;
838f9790aebSLuigi Rizzo 	na->num_tx_desc = num_tx_desc;
839f9790aebSLuigi Rizzo 	na->num_rx_desc = num_rx_desc;
840f9790aebSLuigi Rizzo 	na->nm_register = &generic_netmap_register;
841f9790aebSLuigi Rizzo 	na->nm_txsync = &generic_netmap_txsync;
842f9790aebSLuigi Rizzo 	na->nm_rxsync = &generic_netmap_rxsync;
843f9790aebSLuigi Rizzo 	na->nm_dtor = &generic_netmap_dtor;
8444bf50f18SLuigi Rizzo 	/* when using generic, NAF_NETMAP_ON is set so we force
845f9790aebSLuigi Rizzo 	 * NAF_SKIP_INTR to use the regular interrupt handler
846f9790aebSLuigi Rizzo 	 */
847f0ea3689SLuigi Rizzo 	na->na_flags = NAF_SKIP_INTR | NAF_HOST_RINGS;
848f9790aebSLuigi Rizzo 
849f9790aebSLuigi Rizzo 	ND("[GNA] num_tx_queues(%d), real_num_tx_queues(%d), len(%lu)",
850f9790aebSLuigi Rizzo 			ifp->num_tx_queues, ifp->real_num_tx_queues,
851f9790aebSLuigi Rizzo 			ifp->tx_queue_len);
852f9790aebSLuigi Rizzo 	ND("[GNA] num_rx_queues(%d), real_num_rx_queues(%d)",
853f9790aebSLuigi Rizzo 			ifp->num_rx_queues, ifp->real_num_rx_queues);
854f9790aebSLuigi Rizzo 
855f9790aebSLuigi Rizzo 	generic_find_num_queues(ifp, &na->num_tx_rings, &na->num_rx_rings);
856f9790aebSLuigi Rizzo 
857f9790aebSLuigi Rizzo 	retval = netmap_attach_common(na);
858f9790aebSLuigi Rizzo 	if (retval) {
859f9790aebSLuigi Rizzo 		free(gna, M_DEVBUF);
860f9790aebSLuigi Rizzo 	}
861f9790aebSLuigi Rizzo 
862f9790aebSLuigi Rizzo 	return retval;
863f9790aebSLuigi Rizzo }
864