1718cf2ccSPedro F. Giffuni /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
437e3a6d3SLuigi Rizzo  * Copyright (C) 2013-2016 Vincenzo Maffione
537e3a6d3SLuigi Rizzo  * Copyright (C) 2013-2016 Luigi Rizzo
637e3a6d3SLuigi Rizzo  * All rights reserved.
7f9790aebSLuigi Rizzo  *
8f9790aebSLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
9f9790aebSLuigi Rizzo  * modification, are permitted provided that the following conditions
10f9790aebSLuigi Rizzo  * are met:
11f9790aebSLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
12f9790aebSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
13f9790aebSLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
14f9790aebSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
15f9790aebSLuigi Rizzo  *      documentation and/or other materials provided with the distribution.
16f9790aebSLuigi Rizzo  *
17f9790aebSLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18f9790aebSLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19f9790aebSLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20f9790aebSLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21f9790aebSLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22f9790aebSLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23f9790aebSLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24f9790aebSLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25f9790aebSLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26f9790aebSLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27f9790aebSLuigi Rizzo  * SUCH DAMAGE.
28f9790aebSLuigi Rizzo  */
29f9790aebSLuigi Rizzo 
30f9790aebSLuigi Rizzo /*
31f9790aebSLuigi Rizzo  * This module implements netmap support on top of standard,
32f9790aebSLuigi Rizzo  * unmodified device drivers.
33f9790aebSLuigi Rizzo  *
34f9790aebSLuigi Rizzo  * A NIOCREGIF request is handled here if the device does not
35f9790aebSLuigi Rizzo  * have native support. TX and RX rings are emulated as follows:
36f9790aebSLuigi Rizzo  *
37f9790aebSLuigi Rizzo  * NIOCREGIF
38f9790aebSLuigi Rizzo  *	We preallocate a block of TX mbufs (roughly as many as
39f9790aebSLuigi Rizzo  *	tx descriptors; the number is not critical) to speed up
40f9790aebSLuigi Rizzo  *	operation during transmissions. The refcount on most of
41f9790aebSLuigi Rizzo  *	these buffers is artificially bumped up so we can recycle
42f9790aebSLuigi Rizzo  *	them more easily. Also, the destructor is intercepted
43f9790aebSLuigi Rizzo  *	so we use it as an interrupt notification to wake up
44f9790aebSLuigi Rizzo  *	processes blocked on a poll().
45f9790aebSLuigi Rizzo  *
46f9790aebSLuigi Rizzo  *	For each receive ring we allocate one "struct mbq"
47f9790aebSLuigi Rizzo  *	(an mbuf tailq plus a spinlock). We intercept packets
48f9790aebSLuigi Rizzo  *	(through if_input)
49f9790aebSLuigi Rizzo  *	on the receive path and put them in the mbq from which
50f9790aebSLuigi Rizzo  *	netmap receive routines can grab them.
51f9790aebSLuigi Rizzo  *
52f9790aebSLuigi Rizzo  * TX:
53f9790aebSLuigi Rizzo  *	in the generic_txsync() routine, netmap buffers are copied
54f9790aebSLuigi Rizzo  *	(or linked, in a future) to the preallocated mbufs
55f9790aebSLuigi Rizzo  *	and pushed to the transmit queue. Some of these mbufs
56f9790aebSLuigi Rizzo  *	(those with NS_REPORT, or otherwise every half ring)
57f9790aebSLuigi Rizzo  *	have the refcount=1, others have refcount=2.
58f9790aebSLuigi Rizzo  *	When the destructor is invoked, we take that as
59f9790aebSLuigi Rizzo  *	a notification that all mbufs up to that one in
60f9790aebSLuigi Rizzo  *	the specific ring have been completed, and generate
61f9790aebSLuigi Rizzo  *	the equivalent of a transmit interrupt.
62f9790aebSLuigi Rizzo  *
63f9790aebSLuigi Rizzo  * RX:
64f9790aebSLuigi Rizzo  *
65f9790aebSLuigi Rizzo  */
66f9790aebSLuigi Rizzo 
67f9790aebSLuigi Rizzo #ifdef __FreeBSD__
68f9790aebSLuigi Rizzo 
69f9790aebSLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */
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>
78a02dbe4cSLuiz Otavio O Souza #include <net/if_types.h>
79f9790aebSLuigi Rizzo #include <net/if_var.h>
80f9790aebSLuigi Rizzo #include <machine/bus.h>        /* bus_dmamap_* in netmap_kern.h */
81f9790aebSLuigi Rizzo 
82f9790aebSLuigi Rizzo #include <net/netmap.h>
83f9790aebSLuigi Rizzo #include <dev/netmap/netmap_kern.h>
84f9790aebSLuigi Rizzo #include <dev/netmap/netmap_mem2.h>
85f9790aebSLuigi Rizzo 
86f0ea3689SLuigi Rizzo #define MBUF_RXQ(m)	((m)->m_pkthdr.flowid)
87f9790aebSLuigi Rizzo #define smp_mb()
88f9790aebSLuigi Rizzo 
8937e3a6d3SLuigi Rizzo #elif defined _WIN32
9037e3a6d3SLuigi Rizzo 
9137e3a6d3SLuigi Rizzo #include "win_glue.h"
9237e3a6d3SLuigi Rizzo 
9337e3a6d3SLuigi Rizzo #define MBUF_TXQ(m) 	0//((m)->m_pkthdr.flowid)
9437e3a6d3SLuigi Rizzo #define MBUF_RXQ(m)	    0//((m)->m_pkthdr.flowid)
9537e3a6d3SLuigi Rizzo #define smp_mb()		//XXX: to be correctly defined
96f9790aebSLuigi Rizzo 
97f9790aebSLuigi Rizzo #else /* linux */
98f9790aebSLuigi Rizzo 
99f9790aebSLuigi Rizzo #include "bsd_glue.h"
100f9790aebSLuigi Rizzo 
101f9790aebSLuigi Rizzo #include <linux/ethtool.h>      /* struct ethtool_ops, get_ringparam */
102f9790aebSLuigi Rizzo #include <linux/hrtimer.h>
103f9790aebSLuigi Rizzo 
10437e3a6d3SLuigi Rizzo static inline struct mbuf *
nm_os_get_mbuf(struct ifnet * ifp,int len)10537e3a6d3SLuigi Rizzo nm_os_get_mbuf(struct ifnet *ifp, int len)
10637e3a6d3SLuigi Rizzo {
107a6d768d8SVincenzo Maffione 	return alloc_skb(LL_RESERVED_SPACE(ifp) + len +
10837e3a6d3SLuigi Rizzo 			 ifp->needed_tailroom, GFP_ATOMIC);
10937e3a6d3SLuigi Rizzo }
110f9790aebSLuigi Rizzo 
111f9790aebSLuigi Rizzo #endif /* linux */
112f9790aebSLuigi Rizzo 
113f9790aebSLuigi Rizzo 
114f9790aebSLuigi Rizzo /* Common headers. */
115f9790aebSLuigi Rizzo #include <net/netmap.h>
116f9790aebSLuigi Rizzo #include <dev/netmap/netmap_kern.h>
117f9790aebSLuigi Rizzo #include <dev/netmap/netmap_mem2.h>
118f9790aebSLuigi Rizzo 
119f9790aebSLuigi Rizzo 
12037e3a6d3SLuigi Rizzo #define for_each_kring_n(_i, _k, _karr, _n) \
1212ff91c17SVincenzo Maffione 	for ((_k)=*(_karr), (_i) = 0; (_i) < (_n); (_i)++, (_k) = (_karr)[(_i)])
122f9790aebSLuigi Rizzo 
12337e3a6d3SLuigi Rizzo #define for_each_tx_kring(_i, _k, _na) \
12437e3a6d3SLuigi Rizzo 		for_each_kring_n(_i, _k, (_na)->tx_rings, (_na)->num_tx_rings)
12537e3a6d3SLuigi Rizzo #define for_each_tx_kring_h(_i, _k, _na) \
12637e3a6d3SLuigi Rizzo 		for_each_kring_n(_i, _k, (_na)->tx_rings, (_na)->num_tx_rings + 1)
12737e3a6d3SLuigi Rizzo 
12837e3a6d3SLuigi Rizzo #define for_each_rx_kring(_i, _k, _na) \
12937e3a6d3SLuigi Rizzo 		for_each_kring_n(_i, _k, (_na)->rx_rings, (_na)->num_rx_rings)
13037e3a6d3SLuigi Rizzo #define for_each_rx_kring_h(_i, _k, _na) \
13137e3a6d3SLuigi Rizzo 		for_each_kring_n(_i, _k, (_na)->rx_rings, (_na)->num_rx_rings + 1)
13237e3a6d3SLuigi Rizzo 
13337e3a6d3SLuigi Rizzo 
13437e3a6d3SLuigi Rizzo /* ======================== PERFORMANCE STATISTICS =========================== */
135f9790aebSLuigi Rizzo 
1364bf50f18SLuigi Rizzo #ifdef RATE_GENERIC
137f9790aebSLuigi Rizzo #define IFRATE(x) x
138f9790aebSLuigi Rizzo struct rate_stats {
139f9790aebSLuigi Rizzo 	unsigned long txpkt;
140f9790aebSLuigi Rizzo 	unsigned long txsync;
141f9790aebSLuigi Rizzo 	unsigned long txirq;
14237e3a6d3SLuigi Rizzo 	unsigned long txrepl;
14337e3a6d3SLuigi Rizzo 	unsigned long txdrop;
144f9790aebSLuigi Rizzo 	unsigned long rxpkt;
145f9790aebSLuigi Rizzo 	unsigned long rxirq;
146f9790aebSLuigi Rizzo 	unsigned long rxsync;
147f9790aebSLuigi Rizzo };
148f9790aebSLuigi Rizzo 
149f9790aebSLuigi Rizzo struct rate_context {
150f9790aebSLuigi Rizzo 	unsigned refcount;
151f9790aebSLuigi Rizzo 	struct timer_list timer;
152f9790aebSLuigi Rizzo 	struct rate_stats new;
153f9790aebSLuigi Rizzo 	struct rate_stats old;
154f9790aebSLuigi Rizzo };
155f9790aebSLuigi Rizzo 
156f9790aebSLuigi Rizzo #define RATE_PRINTK(_NAME_) \
157f9790aebSLuigi Rizzo 	printk( #_NAME_ " = %lu Hz\n", (cur._NAME_ - ctx->old._NAME_)/RATE_PERIOD);
158f9790aebSLuigi Rizzo #define RATE_PERIOD  2
rate_callback(unsigned long arg)159f9790aebSLuigi Rizzo static void rate_callback(unsigned long arg)
160f9790aebSLuigi Rizzo {
161f9790aebSLuigi Rizzo 	struct rate_context * ctx = (struct rate_context *)arg;
162f9790aebSLuigi Rizzo 	struct rate_stats cur = ctx->new;
163f9790aebSLuigi Rizzo 	int r;
164f9790aebSLuigi Rizzo 
165f9790aebSLuigi Rizzo 	RATE_PRINTK(txpkt);
166f9790aebSLuigi Rizzo 	RATE_PRINTK(txsync);
167f9790aebSLuigi Rizzo 	RATE_PRINTK(txirq);
16837e3a6d3SLuigi Rizzo 	RATE_PRINTK(txrepl);
16937e3a6d3SLuigi Rizzo 	RATE_PRINTK(txdrop);
170f9790aebSLuigi Rizzo 	RATE_PRINTK(rxpkt);
171f9790aebSLuigi Rizzo 	RATE_PRINTK(rxsync);
172f9790aebSLuigi Rizzo 	RATE_PRINTK(rxirq);
173f9790aebSLuigi Rizzo 	printk("\n");
174f9790aebSLuigi Rizzo 
175f9790aebSLuigi Rizzo 	ctx->old = cur;
176f9790aebSLuigi Rizzo 	r = mod_timer(&ctx->timer, jiffies +
177f9790aebSLuigi Rizzo 			msecs_to_jiffies(RATE_PERIOD * 1000));
178f9790aebSLuigi Rizzo 	if (unlikely(r))
179b6e66be2SVincenzo Maffione 		nm_prerr("mod_timer() failed");
180f9790aebSLuigi Rizzo }
181f9790aebSLuigi Rizzo 
182f9790aebSLuigi Rizzo static struct rate_context rate_ctx;
183f9790aebSLuigi Rizzo 
generic_rate(int txp,int txs,int txi,int rxp,int rxs,int rxi)1844bf50f18SLuigi Rizzo void generic_rate(int txp, int txs, int txi, int rxp, int rxs, int rxi)
1854bf50f18SLuigi Rizzo {
1864bf50f18SLuigi Rizzo 	if (txp) rate_ctx.new.txpkt++;
1874bf50f18SLuigi Rizzo 	if (txs) rate_ctx.new.txsync++;
1884bf50f18SLuigi Rizzo 	if (txi) rate_ctx.new.txirq++;
1894bf50f18SLuigi Rizzo 	if (rxp) rate_ctx.new.rxpkt++;
1904bf50f18SLuigi Rizzo 	if (rxs) rate_ctx.new.rxsync++;
1914bf50f18SLuigi Rizzo 	if (rxi) rate_ctx.new.rxirq++;
1924bf50f18SLuigi Rizzo }
1934bf50f18SLuigi Rizzo 
194f9790aebSLuigi Rizzo #else /* !RATE */
195f9790aebSLuigi Rizzo #define IFRATE(x)
196f9790aebSLuigi Rizzo #endif /* !RATE */
197f9790aebSLuigi Rizzo 
198f9790aebSLuigi Rizzo 
199c3e9b4dbSLuiz Otavio O Souza /* ========== GENERIC (EMULATED) NETMAP ADAPTER SUPPORT ============= */
200f9790aebSLuigi Rizzo 
201f9790aebSLuigi Rizzo /*
202f9790aebSLuigi Rizzo  * Wrapper used by the generic adapter layer to notify
203f9790aebSLuigi Rizzo  * the poller threads. Differently from netmap_rx_irq(), we check
2044bf50f18SLuigi Rizzo  * only NAF_NETMAP_ON instead of NAF_NATIVE_ON to enable the irq.
205f9790aebSLuigi Rizzo  */
20637e3a6d3SLuigi Rizzo void
netmap_generic_irq(struct netmap_adapter * na,u_int q,u_int * work_done)20737e3a6d3SLuigi Rizzo netmap_generic_irq(struct netmap_adapter *na, u_int q, u_int *work_done)
208f9790aebSLuigi Rizzo {
2094bf50f18SLuigi Rizzo 	if (unlikely(!nm_netmap_on(na)))
210f9790aebSLuigi Rizzo 		return;
211f9790aebSLuigi Rizzo 
21237e3a6d3SLuigi Rizzo 	netmap_common_irq(na, q, work_done);
21337e3a6d3SLuigi Rizzo #ifdef RATE_GENERIC
21437e3a6d3SLuigi Rizzo 	if (work_done)
21537e3a6d3SLuigi Rizzo 		rate_ctx.new.rxirq++;
21637e3a6d3SLuigi Rizzo 	else
21737e3a6d3SLuigi Rizzo 		rate_ctx.new.txirq++;
21837e3a6d3SLuigi Rizzo #endif  /* RATE_GENERIC */
219f9790aebSLuigi Rizzo }
220f9790aebSLuigi Rizzo 
22137e3a6d3SLuigi Rizzo static int
generic_netmap_unregister(struct netmap_adapter * na)22237e3a6d3SLuigi Rizzo generic_netmap_unregister(struct netmap_adapter *na)
22337e3a6d3SLuigi Rizzo {
22437e3a6d3SLuigi Rizzo 	struct netmap_generic_adapter *gna = (struct netmap_generic_adapter *)na;
22537e3a6d3SLuigi Rizzo 	struct netmap_kring *kring = NULL;
22637e3a6d3SLuigi Rizzo 	int i, r;
22737e3a6d3SLuigi Rizzo 
22837e3a6d3SLuigi Rizzo 	if (na->active_fds == 0) {
22937e3a6d3SLuigi Rizzo 		na->na_flags &= ~NAF_NETMAP_ON;
23037e3a6d3SLuigi Rizzo 
23137e3a6d3SLuigi Rizzo 		/* Stop intercepting packets on the RX path. */
23237e3a6d3SLuigi Rizzo 		nm_os_catch_rx(gna, 0);
23337e3a6d3SLuigi Rizzo 
2344f80b14cSVincenzo Maffione 		/* Release packet steering control. */
2354f80b14cSVincenzo Maffione 		nm_os_catch_tx(gna, 0);
23637e3a6d3SLuigi Rizzo 	}
23737e3a6d3SLuigi Rizzo 
23875f4f3edSVincenzo Maffione 	netmap_krings_mode_commit(na, /*onoff=*/0);
23937e3a6d3SLuigi Rizzo 
24037e3a6d3SLuigi Rizzo 	for_each_rx_kring(r, kring, na) {
24137e3a6d3SLuigi Rizzo 		/* Free the mbufs still pending in the RX queues,
24237e3a6d3SLuigi Rizzo 		 * that did not end up into the corresponding netmap
24337e3a6d3SLuigi Rizzo 		 * RX rings. */
24437e3a6d3SLuigi Rizzo 		mbq_safe_purge(&kring->rx_queue);
24537e3a6d3SLuigi Rizzo 		nm_os_mitigation_cleanup(&gna->mit[r]);
24637e3a6d3SLuigi Rizzo 	}
24737e3a6d3SLuigi Rizzo 
24837e3a6d3SLuigi Rizzo 	/* Decrement reference counter for the mbufs in the
24937e3a6d3SLuigi Rizzo 	 * TX pools. These mbufs can be still pending in drivers,
25037e3a6d3SLuigi Rizzo 	 * (e.g. this happens with virtio-net driver, which
25137e3a6d3SLuigi Rizzo 	 * does lazy reclaiming of transmitted mbufs). */
25237e3a6d3SLuigi Rizzo 	for_each_tx_kring(r, kring, na) {
25337e3a6d3SLuigi Rizzo 		/* We must remove the destructor on the TX event,
25437e3a6d3SLuigi Rizzo 		 * because the destructor invokes netmap code, and
25537e3a6d3SLuigi Rizzo 		 * the netmap module may disappear before the
25637e3a6d3SLuigi Rizzo 		 * TX event is consumed. */
25737e3a6d3SLuigi Rizzo 		mtx_lock_spin(&kring->tx_event_lock);
25837e3a6d3SLuigi Rizzo 		if (kring->tx_event) {
259c3e9b4dbSLuiz Otavio O Souza 			SET_MBUF_DESTRUCTOR(kring->tx_event, NULL);
26037e3a6d3SLuigi Rizzo 		}
26137e3a6d3SLuigi Rizzo 		kring->tx_event = NULL;
26237e3a6d3SLuigi Rizzo 		mtx_unlock_spin(&kring->tx_event_lock);
26337e3a6d3SLuigi Rizzo 	}
26437e3a6d3SLuigi Rizzo 
26537e3a6d3SLuigi Rizzo 	if (na->active_fds == 0) {
266c3e9b4dbSLuiz Otavio O Souza 		nm_os_free(gna->mit);
26737e3a6d3SLuigi Rizzo 
26837e3a6d3SLuigi Rizzo 		for_each_rx_kring(r, kring, na) {
26937e3a6d3SLuigi Rizzo 			mbq_safe_fini(&kring->rx_queue);
27037e3a6d3SLuigi Rizzo 		}
27137e3a6d3SLuigi Rizzo 
27237e3a6d3SLuigi Rizzo 		for_each_tx_kring(r, kring, na) {
273ce12afaaSMark Johnston 			callout_drain(&kring->tx_event_callout);
27437e3a6d3SLuigi Rizzo 			mtx_destroy(&kring->tx_event_lock);
27537e3a6d3SLuigi Rizzo 			if (kring->tx_pool == NULL) {
27637e3a6d3SLuigi Rizzo 				continue;
27737e3a6d3SLuigi Rizzo 			}
27837e3a6d3SLuigi Rizzo 
27937e3a6d3SLuigi Rizzo 			for (i=0; i<na->num_tx_desc; i++) {
28037e3a6d3SLuigi Rizzo 				if (kring->tx_pool[i]) {
28137e3a6d3SLuigi Rizzo 					m_freem(kring->tx_pool[i]);
28237e3a6d3SLuigi Rizzo 				}
28337e3a6d3SLuigi Rizzo 			}
284c3e9b4dbSLuiz Otavio O Souza 			nm_os_free(kring->tx_pool);
28537e3a6d3SLuigi Rizzo 			kring->tx_pool = NULL;
28637e3a6d3SLuigi Rizzo 		}
28737e3a6d3SLuigi Rizzo 
28837e3a6d3SLuigi Rizzo #ifdef RATE_GENERIC
28937e3a6d3SLuigi Rizzo 		if (--rate_ctx.refcount == 0) {
290b6e66be2SVincenzo Maffione 			nm_prinf("del_timer()");
29137e3a6d3SLuigi Rizzo 			del_timer(&rate_ctx.timer);
29237e3a6d3SLuigi Rizzo 		}
29337e3a6d3SLuigi Rizzo #endif
294b6e66be2SVincenzo Maffione 		nm_prinf("Emulated adapter for %s deactivated", na->name);
29537e3a6d3SLuigi Rizzo 	}
29637e3a6d3SLuigi Rizzo 
29737e3a6d3SLuigi Rizzo 	return 0;
29837e3a6d3SLuigi Rizzo }
299f9790aebSLuigi Rizzo 
300f9790aebSLuigi Rizzo /* Enable/disable netmap mode for a generic network interface. */
30117885a7bSLuigi Rizzo static int
generic_netmap_register(struct netmap_adapter * na,int enable)30217885a7bSLuigi Rizzo generic_netmap_register(struct netmap_adapter *na, int enable)
303f9790aebSLuigi Rizzo {
304f9790aebSLuigi Rizzo 	struct netmap_generic_adapter *gna = (struct netmap_generic_adapter *)na;
30537e3a6d3SLuigi Rizzo 	struct netmap_kring *kring = NULL;
306f9790aebSLuigi Rizzo 	int error;
307f9790aebSLuigi Rizzo 	int i, r;
308f9790aebSLuigi Rizzo 
30937e3a6d3SLuigi Rizzo 	if (!na) {
310f9790aebSLuigi Rizzo 		return EINVAL;
311f9790aebSLuigi Rizzo 	}
312f9790aebSLuigi Rizzo 
31337e3a6d3SLuigi Rizzo 	if (!enable) {
31437e3a6d3SLuigi Rizzo 		/* This is actually an unregif. */
31537e3a6d3SLuigi Rizzo 		return generic_netmap_unregister(na);
31637e3a6d3SLuigi Rizzo 	}
31737e3a6d3SLuigi Rizzo 
31837e3a6d3SLuigi Rizzo 	if (na->active_fds == 0) {
319b6e66be2SVincenzo Maffione 		nm_prinf("Emulated adapter for %s activated", na->name);
32037e3a6d3SLuigi Rizzo 		/* Do all memory allocations when (na->active_fds == 0), to
32137e3a6d3SLuigi Rizzo 		 * simplify error management. */
32237e3a6d3SLuigi Rizzo 
32337e3a6d3SLuigi Rizzo 		/* Allocate memory for mitigation support on all the rx queues. */
324c3e9b4dbSLuiz Otavio O Souza 		gna->mit = nm_os_malloc(na->num_rx_rings * sizeof(struct nm_generic_mit));
325f0ea3689SLuigi Rizzo 		if (!gna->mit) {
326b6e66be2SVincenzo Maffione 			nm_prerr("mitigation allocation failed");
327f0ea3689SLuigi Rizzo 			error = ENOMEM;
328f0ea3689SLuigi Rizzo 			goto out;
329f0ea3689SLuigi Rizzo 		}
33037e3a6d3SLuigi Rizzo 
33137e3a6d3SLuigi Rizzo 		for_each_rx_kring(r, kring, na) {
33237e3a6d3SLuigi Rizzo 			/* Init mitigation support. */
33337e3a6d3SLuigi Rizzo 			nm_os_mitigation_init(&gna->mit[r], r, na);
334f0ea3689SLuigi Rizzo 
335f9790aebSLuigi Rizzo 			/* Initialize the rx queue, as generic_rx_handler() can
33637e3a6d3SLuigi Rizzo 			 * be called as soon as nm_os_catch_rx() returns.
337f9790aebSLuigi Rizzo 			 */
33837e3a6d3SLuigi Rizzo 			mbq_safe_init(&kring->rx_queue);
339f9790aebSLuigi Rizzo 		}
340f9790aebSLuigi Rizzo 
341f9790aebSLuigi Rizzo 		/*
34237e3a6d3SLuigi Rizzo 		 * Prepare mbuf pools (parallel to the tx rings), for packet
34337e3a6d3SLuigi Rizzo 		 * transmission. Don't preallocate the mbufs here, it's simpler
34437e3a6d3SLuigi Rizzo 		 * to leave this task to txsync.
345f9790aebSLuigi Rizzo 		 */
34637e3a6d3SLuigi Rizzo 		for_each_tx_kring(r, kring, na) {
34737e3a6d3SLuigi Rizzo 			kring->tx_pool = NULL;
34837e3a6d3SLuigi Rizzo 		}
34937e3a6d3SLuigi Rizzo 		for_each_tx_kring(r, kring, na) {
35037e3a6d3SLuigi Rizzo 			kring->tx_pool =
351c3e9b4dbSLuiz Otavio O Souza 				nm_os_malloc(na->num_tx_desc * sizeof(struct mbuf *));
35237e3a6d3SLuigi Rizzo 			if (!kring->tx_pool) {
353b6e66be2SVincenzo Maffione 				nm_prerr("tx_pool allocation failed");
354f9790aebSLuigi Rizzo 				error = ENOMEM;
35517885a7bSLuigi Rizzo 				goto free_tx_pools;
356f9790aebSLuigi Rizzo 			}
35737e3a6d3SLuigi Rizzo 			mtx_init(&kring->tx_event_lock, "tx_event_lock",
35837e3a6d3SLuigi Rizzo 				 NULL, MTX_SPIN);
359ce12afaaSMark Johnston 			callout_init_mtx(&kring->tx_event_callout,
360ce12afaaSMark Johnston 					 &kring->tx_event_lock,
361ce12afaaSMark Johnston 					 CALLOUT_RETURNUNLOCKED);
36237e3a6d3SLuigi Rizzo 		}
36337e3a6d3SLuigi Rizzo 	}
36437e3a6d3SLuigi Rizzo 
36575f4f3edSVincenzo Maffione 	netmap_krings_mode_commit(na, /*onoff=*/1);
36637e3a6d3SLuigi Rizzo 
36737e3a6d3SLuigi Rizzo 	for_each_tx_kring(r, kring, na) {
36837e3a6d3SLuigi Rizzo 		/* Initialize tx_pool and tx_event. */
369f9790aebSLuigi Rizzo 		for (i=0; i<na->num_tx_desc; i++) {
37037e3a6d3SLuigi Rizzo 			kring->tx_pool[i] = NULL;
371f9790aebSLuigi Rizzo 		}
37237e3a6d3SLuigi Rizzo 
37337e3a6d3SLuigi Rizzo 		kring->tx_event = NULL;
374f9790aebSLuigi Rizzo 	}
37537e3a6d3SLuigi Rizzo 
37637e3a6d3SLuigi Rizzo 	if (na->active_fds == 0) {
377f9790aebSLuigi Rizzo 		/* Prepare to intercept incoming traffic. */
37837e3a6d3SLuigi Rizzo 		error = nm_os_catch_rx(gna, 1);
379f9790aebSLuigi Rizzo 		if (error) {
380b6e66be2SVincenzo Maffione 			nm_prerr("nm_os_catch_rx(1) failed (%d)", error);
3814f80b14cSVincenzo Maffione 			goto free_tx_pools;
382f9790aebSLuigi Rizzo 		}
383f9790aebSLuigi Rizzo 
3844f80b14cSVincenzo Maffione 		/* Let netmap control the packet steering. */
38537e3a6d3SLuigi Rizzo 		error = nm_os_catch_tx(gna, 1);
38637e3a6d3SLuigi Rizzo 		if (error) {
387b6e66be2SVincenzo Maffione 			nm_prerr("nm_os_catch_tx(1) failed (%d)", error);
38837e3a6d3SLuigi Rizzo 			goto catch_rx;
38937e3a6d3SLuigi Rizzo 		}
390f9790aebSLuigi Rizzo 
39137e3a6d3SLuigi Rizzo 		na->na_flags |= NAF_NETMAP_ON;
39237e3a6d3SLuigi Rizzo 
3934bf50f18SLuigi Rizzo #ifdef RATE_GENERIC
394f9790aebSLuigi Rizzo 		if (rate_ctx.refcount == 0) {
395b6e66be2SVincenzo Maffione 			nm_prinf("setup_timer()");
396f9790aebSLuigi Rizzo 			memset(&rate_ctx, 0, sizeof(rate_ctx));
397f9790aebSLuigi Rizzo 			setup_timer(&rate_ctx.timer, &rate_callback, (unsigned long)&rate_ctx);
398f9790aebSLuigi Rizzo 			if (mod_timer(&rate_ctx.timer, jiffies + msecs_to_jiffies(1500))) {
399b6e66be2SVincenzo Maffione 				nm_prerr("Error: mod_timer()");
400f9790aebSLuigi Rizzo 			}
401f9790aebSLuigi Rizzo 		}
402f9790aebSLuigi Rizzo 		rate_ctx.refcount++;
403f9790aebSLuigi Rizzo #endif /* RATE */
404f9790aebSLuigi Rizzo 	}
405f9790aebSLuigi Rizzo 
406f9790aebSLuigi Rizzo 	return 0;
407f9790aebSLuigi Rizzo 
40837e3a6d3SLuigi Rizzo 	/* Here (na->active_fds == 0) holds. */
40937e3a6d3SLuigi Rizzo catch_rx:
41037e3a6d3SLuigi Rizzo 	nm_os_catch_rx(gna, 0);
41117885a7bSLuigi Rizzo free_tx_pools:
41237e3a6d3SLuigi Rizzo 	for_each_tx_kring(r, kring, na) {
41337e3a6d3SLuigi Rizzo 		mtx_destroy(&kring->tx_event_lock);
41437e3a6d3SLuigi Rizzo 		if (kring->tx_pool == NULL) {
41517885a7bSLuigi Rizzo 			continue;
416f2637526SLuigi Rizzo 		}
417c3e9b4dbSLuiz Otavio O Souza 		nm_os_free(kring->tx_pool);
41837e3a6d3SLuigi Rizzo 		kring->tx_pool = NULL;
41937e3a6d3SLuigi Rizzo 	}
42037e3a6d3SLuigi Rizzo 	for_each_rx_kring(r, kring, na) {
42137e3a6d3SLuigi Rizzo 		mbq_safe_fini(&kring->rx_queue);
422f9790aebSLuigi Rizzo 	}
423c3e9b4dbSLuiz Otavio O Souza 	nm_os_free(gna->mit);
424f0ea3689SLuigi Rizzo out:
425f9790aebSLuigi Rizzo 
426f9790aebSLuigi Rizzo 	return error;
427f9790aebSLuigi Rizzo }
428f9790aebSLuigi Rizzo 
429f9790aebSLuigi Rizzo /*
430f9790aebSLuigi Rizzo  * Callback invoked when the device driver frees an mbuf used
431f9790aebSLuigi Rizzo  * by netmap to transmit a packet. This usually happens when
432f9790aebSLuigi Rizzo  * the NIC notifies the driver that transmission is completed.
433f9790aebSLuigi Rizzo  */
434f9790aebSLuigi Rizzo static void
generic_mbuf_dtor(struct mbuf * m)435ce12afaaSMark Johnston generic_mbuf_dtor(struct mbuf *m)
436f9790aebSLuigi Rizzo {
43737e3a6d3SLuigi Rizzo 	struct netmap_adapter *na = NA(GEN_TX_MBUF_IFP(m));
43837e3a6d3SLuigi Rizzo 	struct netmap_kring *kring;
43937e3a6d3SLuigi Rizzo 	unsigned int r = MBUF_TXQ(m);
44037e3a6d3SLuigi Rizzo 	unsigned int r_orig = r;
44137e3a6d3SLuigi Rizzo 
44237e3a6d3SLuigi Rizzo 	if (unlikely(!nm_netmap_on(na) || r >= na->num_tx_rings)) {
443b6e66be2SVincenzo Maffione 		nm_prerr("Error: no netmap adapter on device %p",
44437e3a6d3SLuigi Rizzo 		  GEN_TX_MBUF_IFP(m));
44537e3a6d3SLuigi Rizzo 		return;
44637e3a6d3SLuigi Rizzo 	}
44737e3a6d3SLuigi Rizzo 
44837e3a6d3SLuigi Rizzo 	/*
44937e3a6d3SLuigi Rizzo 	 * First, clear the event mbuf.
45037e3a6d3SLuigi Rizzo 	 * In principle, the event 'm' should match the one stored
45145c67e8fSVincenzo Maffione 	 * on ring 'r'. However we check it explicitly to stay
45237e3a6d3SLuigi Rizzo 	 * safe against lower layers (qdisc, driver, etc.) changing
45337e3a6d3SLuigi Rizzo 	 * MBUF_TXQ(m) under our feet. If the match is not found
45437e3a6d3SLuigi Rizzo 	 * on 'r', we try to see if it belongs to some other ring.
45537e3a6d3SLuigi Rizzo 	 */
45637e3a6d3SLuigi Rizzo 	for (;;) {
45737e3a6d3SLuigi Rizzo 		bool match = false;
45837e3a6d3SLuigi Rizzo 
4592ff91c17SVincenzo Maffione 		kring = na->tx_rings[r];
46037e3a6d3SLuigi Rizzo 		mtx_lock_spin(&kring->tx_event_lock);
46137e3a6d3SLuigi Rizzo 		if (kring->tx_event == m) {
46237e3a6d3SLuigi Rizzo 			kring->tx_event = NULL;
46337e3a6d3SLuigi Rizzo 			match = true;
46437e3a6d3SLuigi Rizzo 		}
46537e3a6d3SLuigi Rizzo 		mtx_unlock_spin(&kring->tx_event_lock);
46637e3a6d3SLuigi Rizzo 
46737e3a6d3SLuigi Rizzo 		if (match) {
46837e3a6d3SLuigi Rizzo 			if (r != r_orig) {
469b6e66be2SVincenzo Maffione 				nm_prlim(1, "event %p migrated: ring %u --> %u",
47037e3a6d3SLuigi Rizzo 				      m, r_orig, r);
47137e3a6d3SLuigi Rizzo 			}
47237e3a6d3SLuigi Rizzo 			break;
47337e3a6d3SLuigi Rizzo 		}
47437e3a6d3SLuigi Rizzo 
47537e3a6d3SLuigi Rizzo 		if (++r == na->num_tx_rings) r = 0;
47637e3a6d3SLuigi Rizzo 
47737e3a6d3SLuigi Rizzo 		if (r == r_orig) {
478ce12afaaSMark Johnston #ifndef __FreeBSD__
479ce12afaaSMark Johnston 			/*
480ce12afaaSMark Johnston 			 * On FreeBSD this situation can arise if the tx_event
481ce12afaaSMark Johnston 			 * callout handler cleared a stuck packet.
482ce12afaaSMark Johnston 			 */
483b6e66be2SVincenzo Maffione 			nm_prlim(1, "Cannot match event %p", m);
484ce12afaaSMark Johnston #endif
485ce12afaaSMark Johnston 			nm_generic_mbuf_dtor(m);
48637e3a6d3SLuigi Rizzo 			return;
48737e3a6d3SLuigi Rizzo 		}
48837e3a6d3SLuigi Rizzo 	}
48937e3a6d3SLuigi Rizzo 
49037e3a6d3SLuigi Rizzo 	/* Second, wake up clients. They will reclaim the event through
49137e3a6d3SLuigi Rizzo 	 * txsync. */
49237e3a6d3SLuigi Rizzo 	netmap_generic_irq(na, r, NULL);
493ce12afaaSMark Johnston 	nm_generic_mbuf_dtor(m);
494f9790aebSLuigi Rizzo }
495f9790aebSLuigi Rizzo 
49617885a7bSLuigi Rizzo /* Record completed transmissions and update hwtail.
497f9790aebSLuigi Rizzo  *
49817885a7bSLuigi Rizzo  * The oldest tx buffer not yet completed is at nr_hwtail + 1,
499f9790aebSLuigi Rizzo  * nr_hwcur is the first unsent buffer.
500f9790aebSLuigi Rizzo  */
50117885a7bSLuigi Rizzo static u_int
generic_netmap_tx_clean(struct netmap_kring * kring,int txqdisc)50237e3a6d3SLuigi Rizzo generic_netmap_tx_clean(struct netmap_kring *kring, int txqdisc)
503f9790aebSLuigi Rizzo {
50417885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
50517885a7bSLuigi Rizzo 	u_int nm_i = nm_next(kring->nr_hwtail, lim);
506f9790aebSLuigi Rizzo 	u_int hwcur = kring->nr_hwcur;
507f9790aebSLuigi Rizzo 	u_int n = 0;
508f9790aebSLuigi Rizzo 	struct mbuf **tx_pool = kring->tx_pool;
509f9790aebSLuigi Rizzo 
510b6e66be2SVincenzo Maffione 	nm_prdis("hwcur = %d, hwtail = %d", kring->nr_hwcur, kring->nr_hwtail);
51137e3a6d3SLuigi Rizzo 
51217885a7bSLuigi Rizzo 	while (nm_i != hwcur) { /* buffers not completed */
51317885a7bSLuigi Rizzo 		struct mbuf *m = tx_pool[nm_i];
514f9790aebSLuigi Rizzo 
51537e3a6d3SLuigi Rizzo 		if (txqdisc) {
51637e3a6d3SLuigi Rizzo 			if (m == NULL) {
51737e3a6d3SLuigi Rizzo 				/* Nothing to do, this is going
51837e3a6d3SLuigi Rizzo 				 * to be replenished. */
519b6e66be2SVincenzo Maffione 				nm_prlim(3, "Is this happening?");
52037e3a6d3SLuigi Rizzo 
52137e3a6d3SLuigi Rizzo 			} else if (MBUF_QUEUED(m)) {
52237e3a6d3SLuigi Rizzo 				break; /* Not dequeued yet. */
52337e3a6d3SLuigi Rizzo 
52437e3a6d3SLuigi Rizzo 			} else if (MBUF_REFCNT(m) != 1) {
52537e3a6d3SLuigi Rizzo 				/* This mbuf has been dequeued but is still busy
52637e3a6d3SLuigi Rizzo 				 * (refcount is 2).
52737e3a6d3SLuigi Rizzo 				 * Leave it to the driver and replenish. */
52837e3a6d3SLuigi Rizzo 				m_freem(m);
52937e3a6d3SLuigi Rizzo 				tx_pool[nm_i] = NULL;
530f9790aebSLuigi Rizzo 			}
53137e3a6d3SLuigi Rizzo 
53237e3a6d3SLuigi Rizzo 		} else {
53337e3a6d3SLuigi Rizzo 			if (unlikely(m == NULL)) {
53437e3a6d3SLuigi Rizzo 				int event_consumed;
53537e3a6d3SLuigi Rizzo 
53637e3a6d3SLuigi Rizzo 				/* This slot was used to place an event. */
53737e3a6d3SLuigi Rizzo 				mtx_lock_spin(&kring->tx_event_lock);
53837e3a6d3SLuigi Rizzo 				event_consumed = (kring->tx_event == NULL);
53937e3a6d3SLuigi Rizzo 				mtx_unlock_spin(&kring->tx_event_lock);
54037e3a6d3SLuigi Rizzo 				if (!event_consumed) {
54137e3a6d3SLuigi Rizzo 					/* The event has not been consumed yet,
54237e3a6d3SLuigi Rizzo 					 * still busy in the driver. */
54337e3a6d3SLuigi Rizzo 					break;
544f9790aebSLuigi Rizzo 				}
54537e3a6d3SLuigi Rizzo 				/* The event has been consumed, we can go
54637e3a6d3SLuigi Rizzo 				 * ahead. */
54737e3a6d3SLuigi Rizzo 			} else if (MBUF_REFCNT(m) != 1) {
54837e3a6d3SLuigi Rizzo 				/* This mbuf is still busy: its refcnt is 2. */
54937e3a6d3SLuigi Rizzo 				break;
55037e3a6d3SLuigi Rizzo 			}
55137e3a6d3SLuigi Rizzo 		}
55237e3a6d3SLuigi Rizzo 
553f9790aebSLuigi Rizzo 		n++;
55417885a7bSLuigi Rizzo 		nm_i = nm_next(nm_i, lim);
555f9790aebSLuigi Rizzo 	}
55617885a7bSLuigi Rizzo 	kring->nr_hwtail = nm_prev(nm_i, lim);
557b6e66be2SVincenzo Maffione 	nm_prdis("tx completed [%d] -> hwtail %d", n, kring->nr_hwtail);
558f9790aebSLuigi Rizzo 
559f9790aebSLuigi Rizzo 	return n;
560f9790aebSLuigi Rizzo }
561f9790aebSLuigi Rizzo 
56237e3a6d3SLuigi Rizzo /* Compute a slot index in the middle between inf and sup. */
563f9790aebSLuigi Rizzo static inline u_int
ring_middle(u_int inf,u_int sup,u_int lim)56437e3a6d3SLuigi Rizzo ring_middle(u_int inf, u_int sup, u_int lim)
565f9790aebSLuigi Rizzo {
56637e3a6d3SLuigi Rizzo 	u_int n = lim + 1;
567f9790aebSLuigi Rizzo 	u_int e;
568f9790aebSLuigi Rizzo 
56937e3a6d3SLuigi Rizzo 	if (sup >= inf) {
57037e3a6d3SLuigi Rizzo 		e = (sup + inf) / 2;
571f9790aebSLuigi Rizzo 	} else { /* wrap around */
57237e3a6d3SLuigi Rizzo 		e = (sup + n + inf) / 2;
573f9790aebSLuigi Rizzo 		if (e >= n) {
574f9790aebSLuigi Rizzo 			e -= n;
575f9790aebSLuigi Rizzo 		}
576f9790aebSLuigi Rizzo 	}
577f9790aebSLuigi Rizzo 
578f9790aebSLuigi Rizzo 	if (unlikely(e >= n)) {
579b6e66be2SVincenzo Maffione 		nm_prerr("This cannot happen");
580f9790aebSLuigi Rizzo 		e = 0;
581f9790aebSLuigi Rizzo 	}
582f9790aebSLuigi Rizzo 
583f9790aebSLuigi Rizzo 	return e;
584f9790aebSLuigi Rizzo }
585f9790aebSLuigi Rizzo 
586ce12afaaSMark Johnston #ifdef __FreeBSD__
587ce12afaaSMark Johnston static void
generic_tx_callout(void * arg)588ce12afaaSMark Johnston generic_tx_callout(void *arg)
589ce12afaaSMark Johnston {
590ce12afaaSMark Johnston 	struct netmap_kring *kring = arg;
591ce12afaaSMark Johnston 
592ce12afaaSMark Johnston 	kring->tx_event = NULL;
593ce12afaaSMark Johnston 	mtx_unlock_spin(&kring->tx_event_lock);
594ce12afaaSMark Johnston 	netmap_generic_irq(kring->na, kring->ring_id, NULL);
595ce12afaaSMark Johnston }
596ce12afaaSMark Johnston #endif
597ce12afaaSMark Johnston 
598f9790aebSLuigi Rizzo static void
generic_set_tx_event(struct netmap_kring * kring,u_int hwcur)599f9790aebSLuigi Rizzo generic_set_tx_event(struct netmap_kring *kring, u_int hwcur)
600f9790aebSLuigi Rizzo {
60137e3a6d3SLuigi Rizzo 	u_int lim = kring->nkr_num_slots - 1;
602f9790aebSLuigi Rizzo 	struct mbuf *m;
603f9790aebSLuigi Rizzo 	u_int e;
60437e3a6d3SLuigi Rizzo 	u_int ntc = nm_next(kring->nr_hwtail, lim); /* next to clean */
605f9790aebSLuigi Rizzo 
60637e3a6d3SLuigi Rizzo 	if (ntc == hwcur) {
60717885a7bSLuigi Rizzo 		return; /* all buffers are free */
608f9790aebSLuigi Rizzo 	}
60937e3a6d3SLuigi Rizzo 
61037e3a6d3SLuigi Rizzo 	/*
61137e3a6d3SLuigi Rizzo 	 * We have pending packets in the driver between hwtail+1
61237e3a6d3SLuigi Rizzo 	 * and hwcur, and we have to chose one of these slot to
61337e3a6d3SLuigi Rizzo 	 * generate a notification.
61437e3a6d3SLuigi Rizzo 	 * There is a race but this is only called within txsync which
61537e3a6d3SLuigi Rizzo 	 * does a double check.
61637e3a6d3SLuigi Rizzo 	 */
61737e3a6d3SLuigi Rizzo #if 0
61837e3a6d3SLuigi Rizzo 	/* Choose a slot in the middle, so that we don't risk ending
61937e3a6d3SLuigi Rizzo 	 * up in a situation where the client continuously wake up,
62037e3a6d3SLuigi Rizzo 	 * fills one or a few TX slots and go to sleep again. */
62137e3a6d3SLuigi Rizzo 	e = ring_middle(ntc, hwcur, lim);
62237e3a6d3SLuigi Rizzo #else
62337e3a6d3SLuigi Rizzo 	/* Choose the first pending slot, to be safe against driver
62437e3a6d3SLuigi Rizzo 	 * reordering mbuf transmissions. */
62537e3a6d3SLuigi Rizzo 	e = ntc;
62637e3a6d3SLuigi Rizzo #endif
627f9790aebSLuigi Rizzo 
628f9790aebSLuigi Rizzo 	m = kring->tx_pool[e];
629f9790aebSLuigi Rizzo 	if (m == NULL) {
63037e3a6d3SLuigi Rizzo 		/* An event is already in place. */
631f9790aebSLuigi Rizzo 		return;
632f9790aebSLuigi Rizzo 	}
633f9790aebSLuigi Rizzo 
63437e3a6d3SLuigi Rizzo 	mtx_lock_spin(&kring->tx_event_lock);
63537e3a6d3SLuigi Rizzo 	if (kring->tx_event) {
63637e3a6d3SLuigi Rizzo 		/* An event is already in place. */
63737e3a6d3SLuigi Rizzo 		mtx_unlock_spin(&kring->tx_event_lock);
63837e3a6d3SLuigi Rizzo 		return;
63937e3a6d3SLuigi Rizzo 	}
64037e3a6d3SLuigi Rizzo 
641ce12afaaSMark Johnston 	SET_MBUF_DESTRUCTOR(m, generic_mbuf_dtor);
64237e3a6d3SLuigi Rizzo 	kring->tx_event = m;
643ce12afaaSMark Johnston #ifdef __FreeBSD__
644ce12afaaSMark Johnston 	/*
645ce12afaaSMark Johnston 	 * Handle the possibility that the transmitted buffer isn't reclaimed
646ce12afaaSMark Johnston 	 * within a bounded period of time.  This can arise when transmitting
647ce12afaaSMark Johnston 	 * out of multiple ports via a lagg or bridge interface, since the
648ce12afaaSMark Johnston 	 * member ports may legitimately only free transmitted buffers in
649ce12afaaSMark Johnston 	 * batches.
650ce12afaaSMark Johnston 	 *
651ce12afaaSMark Johnston 	 * The callout handler clears the stuck packet from the ring, allowing
652ce12afaaSMark Johnston 	 * transmission to proceed.  In the common case we let
653ce12afaaSMark Johnston 	 * generic_mbuf_dtor() unstick the ring, allowing mbufs to be
654ce12afaaSMark Johnston 	 * reused most of the time.
655ce12afaaSMark Johnston 	 */
656ce12afaaSMark Johnston 	callout_reset_sbt_curcpu(&kring->tx_event_callout, SBT_1MS, 0,
657ce12afaaSMark Johnston 	    generic_tx_callout, kring, 0);
658ce12afaaSMark Johnston #endif
65937e3a6d3SLuigi Rizzo 	mtx_unlock_spin(&kring->tx_event_lock);
66037e3a6d3SLuigi Rizzo 
66137e3a6d3SLuigi Rizzo 	kring->tx_pool[e] = NULL;
66237e3a6d3SLuigi Rizzo 
663b6e66be2SVincenzo Maffione 	nm_prdis("Request Event at %d mbuf %p refcnt %d", e, m, m ? MBUF_REFCNT(m) : -2 );
66437e3a6d3SLuigi Rizzo 
66537e3a6d3SLuigi Rizzo 	/* Decrement the refcount. This will free it if we lose the race
66637e3a6d3SLuigi Rizzo 	 * with the driver. */
667f9790aebSLuigi Rizzo 	m_freem(m);
668f9790aebSLuigi Rizzo }
669f9790aebSLuigi Rizzo 
670f9790aebSLuigi Rizzo /*
671f9790aebSLuigi Rizzo  * generic_netmap_txsync() transforms netmap buffers into mbufs
672f9790aebSLuigi Rizzo  * and passes them to the standard device driver
673f9790aebSLuigi Rizzo  * (ndo_start_xmit() or ifp->if_transmit() ).
674f9790aebSLuigi Rizzo  * On linux this is not done directly, but using dev_queue_xmit(),
675f9790aebSLuigi Rizzo  * since it implements the TX flow control (and takes some locks).
676f9790aebSLuigi Rizzo  */
677f9790aebSLuigi Rizzo static int
generic_netmap_txsync(struct netmap_kring * kring,int flags)6784bf50f18SLuigi Rizzo generic_netmap_txsync(struct netmap_kring *kring, int flags)
679f9790aebSLuigi Rizzo {
6804bf50f18SLuigi Rizzo 	struct netmap_adapter *na = kring->na;
68137e3a6d3SLuigi Rizzo 	struct netmap_generic_adapter *gna = (struct netmap_generic_adapter *)na;
682e330262fSJustin Hibbits 	if_t ifp = na->ifp;
683f9790aebSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
68417885a7bSLuigi Rizzo 	u_int nm_i;	/* index into the netmap ring */ // j
68517885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
68617885a7bSLuigi Rizzo 	u_int const head = kring->rhead;
6874bf50f18SLuigi Rizzo 	u_int ring_nr = kring->ring_id;
688f9790aebSLuigi Rizzo 
689f9790aebSLuigi Rizzo 	IFRATE(rate_ctx.new.txsync++);
690f9790aebSLuigi Rizzo 
691f9790aebSLuigi Rizzo 	rmb();
69217885a7bSLuigi Rizzo 
693f9790aebSLuigi Rizzo 	/*
69417885a7bSLuigi Rizzo 	 * First part: process new packets to send.
695f9790aebSLuigi Rizzo 	 */
69617885a7bSLuigi Rizzo 	nm_i = kring->nr_hwcur;
69717885a7bSLuigi Rizzo 	if (nm_i != head) {	/* we have new packets to send */
69837e3a6d3SLuigi Rizzo 		struct nm_os_gen_arg a;
69937e3a6d3SLuigi Rizzo 		u_int event = -1;
700484456b2SVincenzo Maffione #ifdef __FreeBSD__
701484456b2SVincenzo Maffione 		struct epoch_tracker et;
702484456b2SVincenzo Maffione 
703484456b2SVincenzo Maffione 		NET_EPOCH_ENTER(et);
704484456b2SVincenzo Maffione #endif
70537e3a6d3SLuigi Rizzo 
70637e3a6d3SLuigi Rizzo 		if (gna->txqdisc && nm_kr_txempty(kring)) {
70737e3a6d3SLuigi Rizzo 			/* In txqdisc mode, we ask for a delayed notification,
70837e3a6d3SLuigi Rizzo 			 * but only when cur == hwtail, which means that the
70937e3a6d3SLuigi Rizzo 			 * client is going to block. */
71037e3a6d3SLuigi Rizzo 			event = ring_middle(nm_i, head, lim);
711b6e66be2SVincenzo Maffione 			nm_prdis("Place txqdisc event (hwcur=%u,event=%u,"
71237e3a6d3SLuigi Rizzo 			      "head=%u,hwtail=%u)", nm_i, event, head,
71337e3a6d3SLuigi Rizzo 			      kring->nr_hwtail);
71437e3a6d3SLuigi Rizzo 		}
71537e3a6d3SLuigi Rizzo 
71637e3a6d3SLuigi Rizzo 		a.ifp = ifp;
71737e3a6d3SLuigi Rizzo 		a.ring_nr = ring_nr;
71837e3a6d3SLuigi Rizzo 		a.head = a.tail = NULL;
71937e3a6d3SLuigi Rizzo 
72017885a7bSLuigi Rizzo 		while (nm_i != head) {
72117885a7bSLuigi Rizzo 			struct netmap_slot *slot = &ring->slot[nm_i];
722f9790aebSLuigi Rizzo 			u_int len = slot->len;
7234bf50f18SLuigi Rizzo 			void *addr = NMB(na, slot);
72417885a7bSLuigi Rizzo 			/* device-specific */
725f9790aebSLuigi Rizzo 			struct mbuf *m;
726f9790aebSLuigi Rizzo 			int tx_ret;
727f9790aebSLuigi Rizzo 
7284bf50f18SLuigi Rizzo 			NM_CHECK_ADDR_LEN(na, addr, len);
72917885a7bSLuigi Rizzo 
73037e3a6d3SLuigi Rizzo 			/* Tale a mbuf from the tx pool (replenishing the pool
73137e3a6d3SLuigi Rizzo 			 * entry if necessary) and copy in the user packet. */
73217885a7bSLuigi Rizzo 			m = kring->tx_pool[nm_i];
733f9790aebSLuigi Rizzo 			if (unlikely(m == NULL)) {
73437e3a6d3SLuigi Rizzo 				kring->tx_pool[nm_i] = m =
73537e3a6d3SLuigi Rizzo 					nm_os_get_mbuf(ifp, NETMAP_BUF_SIZE(na));
73637e3a6d3SLuigi Rizzo 				if (m == NULL) {
737b6e66be2SVincenzo Maffione 					nm_prlim(2, "Failed to replenish mbuf");
73837e3a6d3SLuigi Rizzo 					/* Here we could schedule a timer which
73937e3a6d3SLuigi Rizzo 					 * retries to replenish after a while,
74037e3a6d3SLuigi Rizzo 					 * and notifies the client when it
74137e3a6d3SLuigi Rizzo 					 * manages to replenish some slots. In
74237e3a6d3SLuigi Rizzo 					 * any case we break early to avoid
74337e3a6d3SLuigi Rizzo 					 * crashes. */
744f9790aebSLuigi Rizzo 					break;
745f9790aebSLuigi Rizzo 				}
74637e3a6d3SLuigi Rizzo 				IFRATE(rate_ctx.new.txrepl++);
747ce12afaaSMark Johnston 			} else {
748ce12afaaSMark Johnston 				nm_os_mbuf_reinit(m);
749f9790aebSLuigi Rizzo 			}
75037e3a6d3SLuigi Rizzo 
75137e3a6d3SLuigi Rizzo 			a.m = m;
75237e3a6d3SLuigi Rizzo 			a.addr = addr;
75337e3a6d3SLuigi Rizzo 			a.len = len;
75437e3a6d3SLuigi Rizzo 			a.qevent = (nm_i == event);
75537e3a6d3SLuigi Rizzo 			/* When not in txqdisc mode, we should ask
75637e3a6d3SLuigi Rizzo 			 * notifications when NS_REPORT is set, or roughly
75737e3a6d3SLuigi Rizzo 			 * every half ring. To optimize this, we set a
75837e3a6d3SLuigi Rizzo 			 * notification event when the client runs out of
75937e3a6d3SLuigi Rizzo 			 * TX ring space, or when transmission fails. In
76037e3a6d3SLuigi Rizzo 			 * the latter case we also break early.
761f9790aebSLuigi Rizzo 			 */
76237e3a6d3SLuigi Rizzo 			tx_ret = nm_os_generic_xmit_frame(&a);
763f9790aebSLuigi Rizzo 			if (unlikely(tx_ret)) {
76437e3a6d3SLuigi Rizzo 				if (!gna->txqdisc) {
765f9790aebSLuigi Rizzo 					/*
766f9790aebSLuigi Rizzo 					 * No room for this mbuf in the device driver.
767f9790aebSLuigi Rizzo 					 * Request a notification FOR A PREVIOUS MBUF,
768f9790aebSLuigi Rizzo 					 * then call generic_netmap_tx_clean(kring) to do the
769f9790aebSLuigi Rizzo 					 * double check and see if we can free more buffers.
770f9790aebSLuigi Rizzo 					 * If there is space continue, else break;
771f9790aebSLuigi Rizzo 					 * NOTE: the double check is necessary if the problem
772f9790aebSLuigi Rizzo 					 * occurs in the txsync call after selrecord().
773f9790aebSLuigi Rizzo 					 * Also, we need some way to tell the caller that not
774f9790aebSLuigi Rizzo 					 * all buffers were queued onto the device (this was
775f9790aebSLuigi Rizzo 					 * not a problem with native netmap driver where space
776f9790aebSLuigi Rizzo 					 * is preallocated). The bridge has a similar problem
777f9790aebSLuigi Rizzo 					 * and we solve it there by dropping the excess packets.
778f9790aebSLuigi Rizzo 					 */
77917885a7bSLuigi Rizzo 					generic_set_tx_event(kring, nm_i);
78037e3a6d3SLuigi Rizzo 					if (generic_netmap_tx_clean(kring, gna->txqdisc)) {
78137e3a6d3SLuigi Rizzo 						/* space now available */
782f9790aebSLuigi Rizzo 						continue;
783f9790aebSLuigi Rizzo 					} else {
784f9790aebSLuigi Rizzo 						break;
785f9790aebSLuigi Rizzo 					}
786f9790aebSLuigi Rizzo 				}
78737e3a6d3SLuigi Rizzo 
78837e3a6d3SLuigi Rizzo 				/* In txqdisc mode, the netmap-aware qdisc
78937e3a6d3SLuigi Rizzo 				 * queue has the same length as the number of
79037e3a6d3SLuigi Rizzo 				 * netmap slots (N). Since tail is advanced
79137e3a6d3SLuigi Rizzo 				 * only when packets are dequeued, qdisc
79237e3a6d3SLuigi Rizzo 				 * queue overrun cannot happen, so
79337e3a6d3SLuigi Rizzo 				 * nm_os_generic_xmit_frame() did not fail
79437e3a6d3SLuigi Rizzo 				 * because of that.
79537e3a6d3SLuigi Rizzo 				 * However, packets can be dropped because
79637e3a6d3SLuigi Rizzo 				 * carrier is off, or because our qdisc is
79737e3a6d3SLuigi Rizzo 				 * being deactivated, or possibly for other
79837e3a6d3SLuigi Rizzo 				 * reasons. In these cases, we just let the
79937e3a6d3SLuigi Rizzo 				 * packet to be dropped. */
80037e3a6d3SLuigi Rizzo 				IFRATE(rate_ctx.new.txdrop++);
80137e3a6d3SLuigi Rizzo 			}
80237e3a6d3SLuigi Rizzo 
803f9790aebSLuigi Rizzo 			slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED);
80417885a7bSLuigi Rizzo 			nm_i = nm_next(nm_i, lim);
805f0ea3689SLuigi Rizzo 			IFRATE(rate_ctx.new.txpkt++);
806f9790aebSLuigi Rizzo 		}
80737e3a6d3SLuigi Rizzo 		if (a.head != NULL) {
80837e3a6d3SLuigi Rizzo 			a.addr = NULL;
80937e3a6d3SLuigi Rizzo 			nm_os_generic_xmit_frame(&a);
81037e3a6d3SLuigi Rizzo 		}
81137e3a6d3SLuigi Rizzo 		/* Update hwcur to the next slot to transmit. Here nm_i
81237e3a6d3SLuigi Rizzo 		 * is not necessarily head, we could break early. */
81337e3a6d3SLuigi Rizzo 		kring->nr_hwcur = nm_i;
814484456b2SVincenzo Maffione 
815484456b2SVincenzo Maffione #ifdef __FreeBSD__
816484456b2SVincenzo Maffione 		NET_EPOCH_EXIT(et);
817484456b2SVincenzo Maffione #endif
81817885a7bSLuigi Rizzo 	}
819f9790aebSLuigi Rizzo 
82037e3a6d3SLuigi Rizzo 	if (!gna->txqdisc && (flags & NAF_FORCE_RECLAIM || nm_kr_txempty(kring))) {
821f9790aebSLuigi Rizzo 		/* No more available slots? Set a notification event
822f9790aebSLuigi Rizzo 		 * on a netmap slot that will be cleaned in the future.
823f9790aebSLuigi Rizzo 		 * No doublecheck is performed, since txsync() will be
824f9790aebSLuigi Rizzo 		 * called twice by netmap_poll().
825f9790aebSLuigi Rizzo 		 */
82617885a7bSLuigi Rizzo 		generic_set_tx_event(kring, nm_i);
827f9790aebSLuigi Rizzo 	}
828f9790aebSLuigi Rizzo 
829ce12afaaSMark Johnston 	/*
830ce12afaaSMark Johnston 	 * Second, reclaim completed buffers
831ce12afaaSMark Johnston 	 */
83237e3a6d3SLuigi Rizzo 	generic_netmap_tx_clean(kring, gna->txqdisc);
83317885a7bSLuigi Rizzo 
834f9790aebSLuigi Rizzo 	return 0;
835f9790aebSLuigi Rizzo }
836f9790aebSLuigi Rizzo 
83717885a7bSLuigi Rizzo 
838f9790aebSLuigi Rizzo /*
83937e3a6d3SLuigi Rizzo  * This handler is registered (through nm_os_catch_rx())
840f9790aebSLuigi Rizzo  * within the attached network interface
841f9790aebSLuigi Rizzo  * in the RX subsystem, so that every mbuf passed up by
842f9790aebSLuigi Rizzo  * the driver can be stolen to the network stack.
843f9790aebSLuigi Rizzo  * Stolen packets are put in a queue where the
844f9790aebSLuigi Rizzo  * generic_netmap_rxsync() callback can extract them.
84537e3a6d3SLuigi Rizzo  * Returns 1 if the packet was stolen, 0 otherwise.
846f9790aebSLuigi Rizzo  */
84737e3a6d3SLuigi Rizzo int
generic_rx_handler(if_t ifp,struct mbuf * m)848e330262fSJustin Hibbits generic_rx_handler(if_t ifp, struct mbuf *m)
849f9790aebSLuigi Rizzo {
850f9790aebSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
851f9790aebSLuigi Rizzo 	struct netmap_generic_adapter *gna = (struct netmap_generic_adapter *)na;
85237e3a6d3SLuigi Rizzo 	struct netmap_kring *kring;
853f9790aebSLuigi Rizzo 	u_int work_done;
85437e3a6d3SLuigi Rizzo 	u_int r = MBUF_RXQ(m); /* receive ring number */
855f0ea3689SLuigi Rizzo 
85637e3a6d3SLuigi Rizzo 	if (r >= na->num_rx_rings) {
85737e3a6d3SLuigi Rizzo 		r = r % na->num_rx_rings;
85837e3a6d3SLuigi Rizzo 	}
85937e3a6d3SLuigi Rizzo 
8602ff91c17SVincenzo Maffione 	kring = na->rx_rings[r];
86137e3a6d3SLuigi Rizzo 
86237e3a6d3SLuigi Rizzo 	if (kring->nr_mode == NKR_NETMAP_OFF) {
86337e3a6d3SLuigi Rizzo 		/* We must not intercept this mbuf. */
86437e3a6d3SLuigi Rizzo 		return 0;
865f0ea3689SLuigi Rizzo 	}
866f9790aebSLuigi Rizzo 
867f9790aebSLuigi Rizzo 	/* limit the size of the queue */
86837e3a6d3SLuigi Rizzo 	if (unlikely(!gna->rxsg && MBUF_LEN(m) > NETMAP_BUF_SIZE(na))) {
86937e3a6d3SLuigi Rizzo 		/* This may happen when GRO/LRO features are enabled for
87037e3a6d3SLuigi Rizzo 		 * the NIC driver when the generic adapter does not
87137e3a6d3SLuigi Rizzo 		 * support RX scatter-gather. */
872b6e66be2SVincenzo Maffione 		nm_prlim(2, "Warning: driver pushed up big packet "
87337e3a6d3SLuigi Rizzo 				"(size=%d)", (int)MBUF_LEN(m));
874df40e30cSMark Johnston 		if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
87537e3a6d3SLuigi Rizzo 		m_freem(m);
876539437c8SMark Johnston 	} else if (unlikely(mbq_len(&kring->rx_queue) > na->num_rx_desc)) {
877df40e30cSMark Johnston 		if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
878f9790aebSLuigi Rizzo 		m_freem(m);
879f9790aebSLuigi Rizzo 	} else {
88037e3a6d3SLuigi Rizzo 		mbq_safe_enqueue(&kring->rx_queue, m);
881f9790aebSLuigi Rizzo 	}
882f9790aebSLuigi Rizzo 
883f9790aebSLuigi Rizzo 	if (netmap_generic_mit < 32768) {
884f9790aebSLuigi Rizzo 		/* no rx mitigation, pass notification up */
88537e3a6d3SLuigi Rizzo 		netmap_generic_irq(na, r, &work_done);
886f9790aebSLuigi Rizzo 	} else {
887f9790aebSLuigi Rizzo 		/* same as send combining, filter notification if there is a
888f9790aebSLuigi Rizzo 		 * pending timer, otherwise pass it up and start a timer.
889f9790aebSLuigi Rizzo 		 */
89037e3a6d3SLuigi Rizzo 		if (likely(nm_os_mitigation_active(&gna->mit[r]))) {
891f9790aebSLuigi Rizzo 			/* Record that there is some pending work. */
89237e3a6d3SLuigi Rizzo 			gna->mit[r].mit_pending = 1;
893f9790aebSLuigi Rizzo 		} else {
89437e3a6d3SLuigi Rizzo 			netmap_generic_irq(na, r, &work_done);
89537e3a6d3SLuigi Rizzo 			nm_os_mitigation_start(&gna->mit[r]);
896f9790aebSLuigi Rizzo 		}
897f9790aebSLuigi Rizzo 	}
89837e3a6d3SLuigi Rizzo 
89937e3a6d3SLuigi Rizzo 	/* We have intercepted the mbuf. */
90037e3a6d3SLuigi Rizzo 	return 1;
901f9790aebSLuigi Rizzo }
902f9790aebSLuigi Rizzo 
903f9790aebSLuigi Rizzo /*
904f9790aebSLuigi Rizzo  * generic_netmap_rxsync() extracts mbufs from the queue filled by
905f9790aebSLuigi Rizzo  * generic_netmap_rx_handler() and puts their content in the netmap
906f9790aebSLuigi Rizzo  * receive ring.
907f9790aebSLuigi Rizzo  * Access must be protected because the rx handler is asynchronous,
908f9790aebSLuigi Rizzo  */
909f9790aebSLuigi Rizzo static int
generic_netmap_rxsync(struct netmap_kring * kring,int flags)9104bf50f18SLuigi Rizzo generic_netmap_rxsync(struct netmap_kring *kring, int flags)
911f9790aebSLuigi Rizzo {
912f9790aebSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
9134bf50f18SLuigi Rizzo 	struct netmap_adapter *na = kring->na;
91417885a7bSLuigi Rizzo 	u_int nm_i;	/* index into the netmap ring */ //j,
91517885a7bSLuigi Rizzo 	u_int n;
91617885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
917847bf383SLuigi Rizzo 	u_int const head = kring->rhead;
918f9790aebSLuigi Rizzo 	int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR;
919f9790aebSLuigi Rizzo 
92037e3a6d3SLuigi Rizzo 	/* Adapter-specific variables. */
92137e3a6d3SLuigi Rizzo 	u_int nm_buf_len = NETMAP_BUF_SIZE(na);
92237e3a6d3SLuigi Rizzo 	struct mbq tmpq;
92337e3a6d3SLuigi Rizzo 	struct mbuf *m;
92437e3a6d3SLuigi Rizzo 	int avail; /* in bytes */
92537e3a6d3SLuigi Rizzo 	int mlen;
92637e3a6d3SLuigi Rizzo 	int copy;
92737e3a6d3SLuigi Rizzo 
92817885a7bSLuigi Rizzo 	if (head > lim)
929f9790aebSLuigi Rizzo 		return netmap_ring_reinit(kring);
930f9790aebSLuigi Rizzo 
93137e3a6d3SLuigi Rizzo 	IFRATE(rate_ctx.new.rxsync++);
93217885a7bSLuigi Rizzo 
933f9790aebSLuigi Rizzo 	/*
93437e3a6d3SLuigi Rizzo 	 * First part: skip past packets that userspace has released.
93537e3a6d3SLuigi Rizzo 	 * This can possibly make room for the second part.
93617885a7bSLuigi Rizzo 	 */
93717885a7bSLuigi Rizzo 	nm_i = kring->nr_hwcur;
93817885a7bSLuigi Rizzo 	if (nm_i != head) {
939f9790aebSLuigi Rizzo 		/* Userspace has released some packets. */
94017885a7bSLuigi Rizzo 		for (n = 0; nm_i != head; n++) {
94117885a7bSLuigi Rizzo 			struct netmap_slot *slot = &ring->slot[nm_i];
942f9790aebSLuigi Rizzo 
943f9790aebSLuigi Rizzo 			slot->flags &= ~NS_BUF_CHANGED;
94417885a7bSLuigi Rizzo 			nm_i = nm_next(nm_i, lim);
945f9790aebSLuigi Rizzo 		}
94617885a7bSLuigi Rizzo 		kring->nr_hwcur = head;
947f9790aebSLuigi Rizzo 	}
94837e3a6d3SLuigi Rizzo 
94937e3a6d3SLuigi Rizzo 	/*
95037e3a6d3SLuigi Rizzo 	 * Second part: import newly received packets.
95137e3a6d3SLuigi Rizzo 	 */
95237e3a6d3SLuigi Rizzo 	if (!netmap_no_pendintr && !force_update) {
95337e3a6d3SLuigi Rizzo 		return 0;
95437e3a6d3SLuigi Rizzo 	}
95537e3a6d3SLuigi Rizzo 
95637e3a6d3SLuigi Rizzo 	nm_i = kring->nr_hwtail; /* First empty slot in the receive ring. */
95737e3a6d3SLuigi Rizzo 
95837e3a6d3SLuigi Rizzo 	/* Compute the available space (in bytes) in this netmap ring.
95937e3a6d3SLuigi Rizzo 	 * The first slot that is not considered in is the one before
96037e3a6d3SLuigi Rizzo 	 * nr_hwcur. */
96137e3a6d3SLuigi Rizzo 
96237e3a6d3SLuigi Rizzo 	avail = nm_prev(kring->nr_hwcur, lim) - nm_i;
96337e3a6d3SLuigi Rizzo 	if (avail < 0)
96437e3a6d3SLuigi Rizzo 		avail += lim + 1;
96537e3a6d3SLuigi Rizzo 	avail *= nm_buf_len;
96637e3a6d3SLuigi Rizzo 
96737e3a6d3SLuigi Rizzo 	/* First pass: While holding the lock on the RX mbuf queue,
96837e3a6d3SLuigi Rizzo 	 * extract as many mbufs as they fit the available space,
96937e3a6d3SLuigi Rizzo 	 * and put them in a temporary queue.
97037e3a6d3SLuigi Rizzo 	 * To avoid performing a per-mbuf division (mlen / nm_buf_len) to
97137e3a6d3SLuigi Rizzo 	 * to update avail, we do the update in a while loop that we
97237e3a6d3SLuigi Rizzo 	 * also use to set the RX slots, but without performing the copy. */
97337e3a6d3SLuigi Rizzo 	mbq_init(&tmpq);
97437e3a6d3SLuigi Rizzo 	mbq_lock(&kring->rx_queue);
97537e3a6d3SLuigi Rizzo 	for (n = 0;; n++) {
97637e3a6d3SLuigi Rizzo 		m = mbq_peek(&kring->rx_queue);
97737e3a6d3SLuigi Rizzo 		if (!m) {
97837e3a6d3SLuigi Rizzo 			/* No more packets from the driver. */
97937e3a6d3SLuigi Rizzo 			break;
98037e3a6d3SLuigi Rizzo 		}
98137e3a6d3SLuigi Rizzo 
98237e3a6d3SLuigi Rizzo 		mlen = MBUF_LEN(m);
98337e3a6d3SLuigi Rizzo 		if (mlen > avail) {
98437e3a6d3SLuigi Rizzo 			/* No more space in the ring. */
98537e3a6d3SLuigi Rizzo 			break;
98637e3a6d3SLuigi Rizzo 		}
98737e3a6d3SLuigi Rizzo 
98837e3a6d3SLuigi Rizzo 		mbq_dequeue(&kring->rx_queue);
98937e3a6d3SLuigi Rizzo 
99037e3a6d3SLuigi Rizzo 		while (mlen) {
99137e3a6d3SLuigi Rizzo 			copy = nm_buf_len;
99237e3a6d3SLuigi Rizzo 			if (mlen < copy) {
99337e3a6d3SLuigi Rizzo 				copy = mlen;
99437e3a6d3SLuigi Rizzo 			}
99537e3a6d3SLuigi Rizzo 			mlen -= copy;
99637e3a6d3SLuigi Rizzo 			avail -= nm_buf_len;
99737e3a6d3SLuigi Rizzo 
99837e3a6d3SLuigi Rizzo 			ring->slot[nm_i].len = copy;
9994f80b14cSVincenzo Maffione 			ring->slot[nm_i].flags = (mlen ? NS_MOREFRAG : 0);
100037e3a6d3SLuigi Rizzo 			nm_i = nm_next(nm_i, lim);
100137e3a6d3SLuigi Rizzo 		}
100237e3a6d3SLuigi Rizzo 
100337e3a6d3SLuigi Rizzo 		mbq_enqueue(&tmpq, m);
100437e3a6d3SLuigi Rizzo 	}
100537e3a6d3SLuigi Rizzo 	mbq_unlock(&kring->rx_queue);
100637e3a6d3SLuigi Rizzo 
100737e3a6d3SLuigi Rizzo 	/* Second pass: Drain the temporary queue, going over the used RX slots,
100837e3a6d3SLuigi Rizzo 	 * and perform the copy out of the RX queue lock. */
100937e3a6d3SLuigi Rizzo 	nm_i = kring->nr_hwtail;
101037e3a6d3SLuigi Rizzo 
101137e3a6d3SLuigi Rizzo 	for (;;) {
101237e3a6d3SLuigi Rizzo 		void *nmaddr;
101337e3a6d3SLuigi Rizzo 		int ofs = 0;
101437e3a6d3SLuigi Rizzo 		int morefrag;
101537e3a6d3SLuigi Rizzo 
101637e3a6d3SLuigi Rizzo 		m = mbq_dequeue(&tmpq);
101737e3a6d3SLuigi Rizzo 		if (!m)	{
101837e3a6d3SLuigi Rizzo 			break;
101937e3a6d3SLuigi Rizzo 		}
102037e3a6d3SLuigi Rizzo 
102137e3a6d3SLuigi Rizzo 		do {
102237e3a6d3SLuigi Rizzo 			nmaddr = NMB(na, &ring->slot[nm_i]);
102337e3a6d3SLuigi Rizzo 			/* We only check the address here on generic rx rings. */
102437e3a6d3SLuigi Rizzo 			if (nmaddr == NETMAP_BUF_BASE(na)) { /* Bad buffer */
102537e3a6d3SLuigi Rizzo 				m_freem(m);
102637e3a6d3SLuigi Rizzo 				mbq_purge(&tmpq);
102737e3a6d3SLuigi Rizzo 				mbq_fini(&tmpq);
102837e3a6d3SLuigi Rizzo 				return netmap_ring_reinit(kring);
102937e3a6d3SLuigi Rizzo 			}
103037e3a6d3SLuigi Rizzo 
103137e3a6d3SLuigi Rizzo 			copy = ring->slot[nm_i].len;
103237e3a6d3SLuigi Rizzo 			m_copydata(m, ofs, copy, nmaddr);
103337e3a6d3SLuigi Rizzo 			ofs += copy;
103437e3a6d3SLuigi Rizzo 			morefrag = ring->slot[nm_i].flags & NS_MOREFRAG;
103537e3a6d3SLuigi Rizzo 			nm_i = nm_next(nm_i, lim);
103637e3a6d3SLuigi Rizzo 		} while (morefrag);
103737e3a6d3SLuigi Rizzo 
103837e3a6d3SLuigi Rizzo 		m_freem(m);
103937e3a6d3SLuigi Rizzo 	}
104037e3a6d3SLuigi Rizzo 
104137e3a6d3SLuigi Rizzo 	mbq_fini(&tmpq);
104237e3a6d3SLuigi Rizzo 
104337e3a6d3SLuigi Rizzo 	if (n) {
104437e3a6d3SLuigi Rizzo 		kring->nr_hwtail = nm_i;
104537e3a6d3SLuigi Rizzo 		IFRATE(rate_ctx.new.rxpkt += n);
104637e3a6d3SLuigi Rizzo 	}
104737e3a6d3SLuigi Rizzo 	kring->nr_kflags &= ~NKR_PENDINTR;
1048f9790aebSLuigi Rizzo 
1049f9790aebSLuigi Rizzo 	return 0;
1050f9790aebSLuigi Rizzo }
1051f9790aebSLuigi Rizzo 
1052f9790aebSLuigi Rizzo static void
generic_netmap_dtor(struct netmap_adapter * na)1053f9790aebSLuigi Rizzo generic_netmap_dtor(struct netmap_adapter *na)
1054f9790aebSLuigi Rizzo {
1055f9790aebSLuigi Rizzo 	struct netmap_generic_adapter *gna = (struct netmap_generic_adapter*)na;
1056e330262fSJustin Hibbits 	if_t ifp = netmap_generic_getifp(gna);
1057f9790aebSLuigi Rizzo 	struct netmap_adapter *prev_na = gna->prev;
1058f9790aebSLuigi Rizzo 
1059f9790aebSLuigi Rizzo 	if (prev_na != NULL) {
1060847bf383SLuigi Rizzo 		netmap_adapter_put(prev_na);
106137e3a6d3SLuigi Rizzo 		if (nm_iszombie(na)) {
1062847bf383SLuigi Rizzo 		        /*
1063847bf383SLuigi Rizzo 		         * The driver has been removed without releasing
1064847bf383SLuigi Rizzo 		         * the reference so we need to do it here.
1065847bf383SLuigi Rizzo 		         */
1066f9790aebSLuigi Rizzo 		        netmap_adapter_put(prev_na);
1067f9790aebSLuigi Rizzo 		}
10685fe59a51SVincenzo Maffione 		nm_prinf("Native netmap adapter for %s restored", prev_na->name);
1069847bf383SLuigi Rizzo 	}
10702a7db7a6SVincenzo Maffione 	NM_RESTORE_NA(ifp, prev_na);
1071f9790aebSLuigi Rizzo 	na->ifp = NULL;
1072b6e66be2SVincenzo Maffione 	nm_prinf("Emulated netmap adapter for %s destroyed", na->name);
1073c3e9b4dbSLuiz Otavio O Souza }
1074c3e9b4dbSLuiz Otavio O Souza 
1075c3e9b4dbSLuiz Otavio O Souza int
na_is_generic(struct netmap_adapter * na)1076c3e9b4dbSLuiz Otavio O Souza na_is_generic(struct netmap_adapter *na)
1077c3e9b4dbSLuiz Otavio O Souza {
1078c3e9b4dbSLuiz Otavio O Souza 	return na->nm_register == generic_netmap_register;
1079f9790aebSLuigi Rizzo }
1080f9790aebSLuigi Rizzo 
1081f9790aebSLuigi Rizzo /*
1082f9790aebSLuigi Rizzo  * generic_netmap_attach() makes it possible to use netmap on
1083f9790aebSLuigi Rizzo  * a device without native netmap support.
1084f9790aebSLuigi Rizzo  * This is less performant than native support but potentially
1085f9790aebSLuigi Rizzo  * faster than raw sockets or similar schemes.
1086f9790aebSLuigi Rizzo  *
1087f9790aebSLuigi Rizzo  * In this "emulated" mode, netmap rings do not necessarily
1088f9790aebSLuigi Rizzo  * have the same size as those in the NIC. We use a default
1089f9790aebSLuigi Rizzo  * value and possibly override it if the OS has ways to fetch the
1090f9790aebSLuigi Rizzo  * actual configuration.
1091f9790aebSLuigi Rizzo  */
1092f9790aebSLuigi Rizzo int
generic_netmap_attach(if_t ifp)1093e330262fSJustin Hibbits generic_netmap_attach(if_t ifp)
1094f9790aebSLuigi Rizzo {
1095f9790aebSLuigi Rizzo 	struct netmap_adapter *na;
1096f9790aebSLuigi Rizzo 	struct netmap_generic_adapter *gna;
1097f9790aebSLuigi Rizzo 	int retval;
1098f9790aebSLuigi Rizzo 	u_int num_tx_desc, num_rx_desc;
1099f9790aebSLuigi Rizzo 
1100a02dbe4cSLuiz Otavio O Souza #ifdef __FreeBSD__
1101e330262fSJustin Hibbits 	if (if_gettype(ifp) == IFT_LOOP) {
1102b6e66be2SVincenzo Maffione 		nm_prerr("if_loop is not supported by %s", __func__);
1103a02dbe4cSLuiz Otavio O Souza 		return EINVAL;
1104a02dbe4cSLuiz Otavio O Souza 	}
1105a02dbe4cSLuiz Otavio O Souza #endif
1106a02dbe4cSLuiz Otavio O Souza 
11072a7db7a6SVincenzo Maffione 	if (NM_NA_CLASH(ifp)) {
11084f80b14cSVincenzo Maffione 		/* If NA(ifp) is not null but there is no valid netmap
11094f80b14cSVincenzo Maffione 		 * adapter it means that someone else is using the same
11104f80b14cSVincenzo Maffione 		 * pointer (e.g. ax25_ptr on linux). This happens for
11114f80b14cSVincenzo Maffione 		 * instance when also PF_RING is in use. */
1112b6e66be2SVincenzo Maffione 		nm_prerr("Error: netmap adapter hook is busy");
11134f80b14cSVincenzo Maffione 		return EBUSY;
11144f80b14cSVincenzo Maffione 	}
11154f80b14cSVincenzo Maffione 
1116f9790aebSLuigi Rizzo 	num_tx_desc = num_rx_desc = netmap_generic_ringsize; /* starting point */
1117f9790aebSLuigi Rizzo 
111837e3a6d3SLuigi Rizzo 	nm_os_generic_find_num_desc(ifp, &num_tx_desc, &num_rx_desc); /* ignore errors */
1119e4166283SLuigi Rizzo 	if (num_tx_desc == 0 || num_rx_desc == 0) {
1120b6e66be2SVincenzo Maffione 		nm_prerr("Device has no hw slots (tx %u, rx %u)", num_tx_desc, num_rx_desc);
1121e4166283SLuigi Rizzo 		return EINVAL;
1122e4166283SLuigi Rizzo 	}
1123f9790aebSLuigi Rizzo 
1124c3e9b4dbSLuiz Otavio O Souza 	gna = nm_os_malloc(sizeof(*gna));
1125f9790aebSLuigi Rizzo 	if (gna == NULL) {
1126b6e66be2SVincenzo Maffione 		nm_prerr("no memory on attach, give up");
1127f9790aebSLuigi Rizzo 		return ENOMEM;
1128f9790aebSLuigi Rizzo 	}
1129f9790aebSLuigi Rizzo 	na = (struct netmap_adapter *)gna;
1130e330262fSJustin Hibbits 	strlcpy(na->name, if_name(ifp), sizeof(na->name));
1131f9790aebSLuigi Rizzo 	na->ifp = ifp;
1132f9790aebSLuigi Rizzo 	na->num_tx_desc = num_tx_desc;
1133f9790aebSLuigi Rizzo 	na->num_rx_desc = num_rx_desc;
11342a7db7a6SVincenzo Maffione 	na->rx_buf_maxsize = 32768;
1135f9790aebSLuigi Rizzo 	na->nm_register = &generic_netmap_register;
1136f9790aebSLuigi Rizzo 	na->nm_txsync = &generic_netmap_txsync;
1137f9790aebSLuigi Rizzo 	na->nm_rxsync = &generic_netmap_rxsync;
1138f9790aebSLuigi Rizzo 	na->nm_dtor = &generic_netmap_dtor;
11394bf50f18SLuigi Rizzo 	/* when using generic, NAF_NETMAP_ON is set so we force
1140f9790aebSLuigi Rizzo 	 * NAF_SKIP_INTR to use the regular interrupt handler
1141f9790aebSLuigi Rizzo 	 */
1142f0ea3689SLuigi Rizzo 	na->na_flags = NAF_SKIP_INTR | NAF_HOST_RINGS;
1143f9790aebSLuigi Rizzo 
1144b6e66be2SVincenzo Maffione 	nm_prdis("[GNA] num_tx_queues(%d), real_num_tx_queues(%d), len(%lu)",
1145f9790aebSLuigi Rizzo 			ifp->num_tx_queues, ifp->real_num_tx_queues,
1146f9790aebSLuigi Rizzo 			ifp->tx_queue_len);
1147b6e66be2SVincenzo Maffione 	nm_prdis("[GNA] num_rx_queues(%d), real_num_rx_queues(%d)",
1148f9790aebSLuigi Rizzo 			ifp->num_rx_queues, ifp->real_num_rx_queues);
1149f9790aebSLuigi Rizzo 
115037e3a6d3SLuigi Rizzo 	nm_os_generic_find_num_queues(ifp, &na->num_tx_rings, &na->num_rx_rings);
1151f9790aebSLuigi Rizzo 
1152f9790aebSLuigi Rizzo 	retval = netmap_attach_common(na);
1153f9790aebSLuigi Rizzo 	if (retval) {
1154c3e9b4dbSLuiz Otavio O Souza 		nm_os_free(gna);
115537e3a6d3SLuigi Rizzo 		return retval;
1156f9790aebSLuigi Rizzo 	}
1157f9790aebSLuigi Rizzo 
11582a7db7a6SVincenzo Maffione 	if (NM_NA_VALID(ifp)) {
115937e3a6d3SLuigi Rizzo 		gna->prev = NA(ifp); /* save old na */
116037e3a6d3SLuigi Rizzo 		netmap_adapter_get(gna->prev);
116137e3a6d3SLuigi Rizzo 	}
116237e3a6d3SLuigi Rizzo 	NM_ATTACH_NA(ifp, na);
116337e3a6d3SLuigi Rizzo 
116437e3a6d3SLuigi Rizzo 	nm_os_generic_set_features(gna);
116537e3a6d3SLuigi Rizzo 
1166d7143780SVincenzo Maffione 	nm_prinf("Emulated adapter for %s created (prev was %s)", na->name,
1167d7143780SVincenzo Maffione 	    gna->prev ? gna->prev->name : "NULL");
116837e3a6d3SLuigi Rizzo 
1169f9790aebSLuigi Rizzo 	return retval;
1170f9790aebSLuigi Rizzo }
1171