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