1718cf2ccSPedro F. Giffuni /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
71f9790aebSLuigi Rizzo 
72f9790aebSLuigi Rizzo #include <sys/types.h>
73f9790aebSLuigi Rizzo #include <sys/errno.h>
74f9790aebSLuigi Rizzo #include <sys/malloc.h>
75f9790aebSLuigi Rizzo #include <sys/lock.h>   /* PROT_EXEC */
76f9790aebSLuigi Rizzo #include <sys/rwlock.h>
77f9790aebSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
78f9790aebSLuigi Rizzo #include <sys/selinfo.h>
79f9790aebSLuigi Rizzo #include <net/if.h>
80a02dbe4cSLuiz Otavio O Souza #include <net/if_types.h>
81f9790aebSLuigi Rizzo #include <net/if_var.h>
82f9790aebSLuigi Rizzo #include <machine/bus.h>        /* bus_dmamap_* in netmap_kern.h */
83f9790aebSLuigi Rizzo 
84f9790aebSLuigi Rizzo // XXX temporary - D() defined here
85f9790aebSLuigi Rizzo #include <net/netmap.h>
86f9790aebSLuigi Rizzo #include <dev/netmap/netmap_kern.h>
87f9790aebSLuigi Rizzo #include <dev/netmap/netmap_mem2.h>
88f9790aebSLuigi Rizzo 
89f0ea3689SLuigi Rizzo #define MBUF_RXQ(m)	((m)->m_pkthdr.flowid)
90f9790aebSLuigi Rizzo #define smp_mb()
91f9790aebSLuigi Rizzo 
9237e3a6d3SLuigi Rizzo #elif defined _WIN32
9337e3a6d3SLuigi Rizzo 
9437e3a6d3SLuigi Rizzo #include "win_glue.h"
9537e3a6d3SLuigi Rizzo 
9637e3a6d3SLuigi Rizzo #define MBUF_TXQ(m) 	0//((m)->m_pkthdr.flowid)
9737e3a6d3SLuigi Rizzo #define MBUF_RXQ(m)	    0//((m)->m_pkthdr.flowid)
9837e3a6d3SLuigi Rizzo #define smp_mb()		//XXX: to be correctly defined
99f9790aebSLuigi Rizzo 
100f9790aebSLuigi Rizzo #else /* linux */
101f9790aebSLuigi Rizzo 
102f9790aebSLuigi Rizzo #include "bsd_glue.h"
103f9790aebSLuigi Rizzo 
104f9790aebSLuigi Rizzo #include <linux/ethtool.h>      /* struct ethtool_ops, get_ringparam */
105f9790aebSLuigi Rizzo #include <linux/hrtimer.h>
106f9790aebSLuigi Rizzo 
10737e3a6d3SLuigi Rizzo static inline struct mbuf *
10837e3a6d3SLuigi Rizzo nm_os_get_mbuf(struct ifnet *ifp, int len)
10937e3a6d3SLuigi Rizzo {
11037e3a6d3SLuigi Rizzo 	return alloc_skb(ifp->needed_headroom + len +
11137e3a6d3SLuigi Rizzo 			 ifp->needed_tailroom, GFP_ATOMIC);
11237e3a6d3SLuigi Rizzo }
113f9790aebSLuigi Rizzo 
114f9790aebSLuigi Rizzo #endif /* linux */
115f9790aebSLuigi Rizzo 
116f9790aebSLuigi Rizzo 
117f9790aebSLuigi Rizzo /* Common headers. */
118f9790aebSLuigi Rizzo #include <net/netmap.h>
119f9790aebSLuigi Rizzo #include <dev/netmap/netmap_kern.h>
120f9790aebSLuigi Rizzo #include <dev/netmap/netmap_mem2.h>
121f9790aebSLuigi Rizzo 
122f9790aebSLuigi Rizzo 
12337e3a6d3SLuigi Rizzo #define for_each_kring_n(_i, _k, _karr, _n) \
1242ff91c17SVincenzo Maffione 	for ((_k)=*(_karr), (_i) = 0; (_i) < (_n); (_i)++, (_k) = (_karr)[(_i)])
125f9790aebSLuigi Rizzo 
12637e3a6d3SLuigi Rizzo #define for_each_tx_kring(_i, _k, _na) \
12737e3a6d3SLuigi Rizzo 		for_each_kring_n(_i, _k, (_na)->tx_rings, (_na)->num_tx_rings)
12837e3a6d3SLuigi Rizzo #define for_each_tx_kring_h(_i, _k, _na) \
12937e3a6d3SLuigi Rizzo 		for_each_kring_n(_i, _k, (_na)->tx_rings, (_na)->num_tx_rings + 1)
13037e3a6d3SLuigi Rizzo 
13137e3a6d3SLuigi Rizzo #define for_each_rx_kring(_i, _k, _na) \
13237e3a6d3SLuigi Rizzo 		for_each_kring_n(_i, _k, (_na)->rx_rings, (_na)->num_rx_rings)
13337e3a6d3SLuigi Rizzo #define for_each_rx_kring_h(_i, _k, _na) \
13437e3a6d3SLuigi Rizzo 		for_each_kring_n(_i, _k, (_na)->rx_rings, (_na)->num_rx_rings + 1)
13537e3a6d3SLuigi Rizzo 
13637e3a6d3SLuigi Rizzo 
13737e3a6d3SLuigi Rizzo /* ======================== PERFORMANCE STATISTICS =========================== */
138f9790aebSLuigi Rizzo 
1394bf50f18SLuigi Rizzo #ifdef RATE_GENERIC
140f9790aebSLuigi Rizzo #define IFRATE(x) x
141f9790aebSLuigi Rizzo struct rate_stats {
142f9790aebSLuigi Rizzo 	unsigned long txpkt;
143f9790aebSLuigi Rizzo 	unsigned long txsync;
144f9790aebSLuigi Rizzo 	unsigned long txirq;
14537e3a6d3SLuigi Rizzo 	unsigned long txrepl;
14637e3a6d3SLuigi Rizzo 	unsigned long txdrop;
147f9790aebSLuigi Rizzo 	unsigned long rxpkt;
148f9790aebSLuigi Rizzo 	unsigned long rxirq;
149f9790aebSLuigi Rizzo 	unsigned long rxsync;
150f9790aebSLuigi Rizzo };
151f9790aebSLuigi Rizzo 
152f9790aebSLuigi Rizzo struct rate_context {
153f9790aebSLuigi Rizzo 	unsigned refcount;
154f9790aebSLuigi Rizzo 	struct timer_list timer;
155f9790aebSLuigi Rizzo 	struct rate_stats new;
156f9790aebSLuigi Rizzo 	struct rate_stats old;
157f9790aebSLuigi Rizzo };
158f9790aebSLuigi Rizzo 
159f9790aebSLuigi Rizzo #define RATE_PRINTK(_NAME_) \
160f9790aebSLuigi Rizzo 	printk( #_NAME_ " = %lu Hz\n", (cur._NAME_ - ctx->old._NAME_)/RATE_PERIOD);
161f9790aebSLuigi Rizzo #define RATE_PERIOD  2
162f9790aebSLuigi Rizzo static void rate_callback(unsigned long arg)
163f9790aebSLuigi Rizzo {
164f9790aebSLuigi Rizzo 	struct rate_context * ctx = (struct rate_context *)arg;
165f9790aebSLuigi Rizzo 	struct rate_stats cur = ctx->new;
166f9790aebSLuigi Rizzo 	int r;
167f9790aebSLuigi Rizzo 
168f9790aebSLuigi Rizzo 	RATE_PRINTK(txpkt);
169f9790aebSLuigi Rizzo 	RATE_PRINTK(txsync);
170f9790aebSLuigi Rizzo 	RATE_PRINTK(txirq);
17137e3a6d3SLuigi Rizzo 	RATE_PRINTK(txrepl);
17237e3a6d3SLuigi Rizzo 	RATE_PRINTK(txdrop);
173f9790aebSLuigi Rizzo 	RATE_PRINTK(rxpkt);
174f9790aebSLuigi Rizzo 	RATE_PRINTK(rxsync);
175f9790aebSLuigi Rizzo 	RATE_PRINTK(rxirq);
176f9790aebSLuigi Rizzo 	printk("\n");
177f9790aebSLuigi Rizzo 
178f9790aebSLuigi Rizzo 	ctx->old = cur;
179f9790aebSLuigi Rizzo 	r = mod_timer(&ctx->timer, jiffies +
180f9790aebSLuigi Rizzo 			msecs_to_jiffies(RATE_PERIOD * 1000));
181f9790aebSLuigi Rizzo 	if (unlikely(r))
182f9790aebSLuigi Rizzo 		D("[v1000] Error: mod_timer()");
183f9790aebSLuigi Rizzo }
184f9790aebSLuigi Rizzo 
185f9790aebSLuigi Rizzo static struct rate_context rate_ctx;
186f9790aebSLuigi Rizzo 
1874bf50f18SLuigi Rizzo void generic_rate(int txp, int txs, int txi, int rxp, int rxs, int rxi)
1884bf50f18SLuigi Rizzo {
1894bf50f18SLuigi Rizzo 	if (txp) rate_ctx.new.txpkt++;
1904bf50f18SLuigi Rizzo 	if (txs) rate_ctx.new.txsync++;
1914bf50f18SLuigi Rizzo 	if (txi) rate_ctx.new.txirq++;
1924bf50f18SLuigi Rizzo 	if (rxp) rate_ctx.new.rxpkt++;
1934bf50f18SLuigi Rizzo 	if (rxs) rate_ctx.new.rxsync++;
1944bf50f18SLuigi Rizzo 	if (rxi) rate_ctx.new.rxirq++;
1954bf50f18SLuigi Rizzo }
1964bf50f18SLuigi Rizzo 
197f9790aebSLuigi Rizzo #else /* !RATE */
198f9790aebSLuigi Rizzo #define IFRATE(x)
199f9790aebSLuigi Rizzo #endif /* !RATE */
200f9790aebSLuigi Rizzo 
201f9790aebSLuigi Rizzo 
202c3e9b4dbSLuiz Otavio O Souza /* ========== GENERIC (EMULATED) NETMAP ADAPTER SUPPORT ============= */
203f9790aebSLuigi Rizzo 
204f9790aebSLuigi Rizzo /*
205f9790aebSLuigi Rizzo  * Wrapper used by the generic adapter layer to notify
206f9790aebSLuigi Rizzo  * the poller threads. Differently from netmap_rx_irq(), we check
2074bf50f18SLuigi Rizzo  * only NAF_NETMAP_ON instead of NAF_NATIVE_ON to enable the irq.
208f9790aebSLuigi Rizzo  */
20937e3a6d3SLuigi Rizzo void
21037e3a6d3SLuigi Rizzo netmap_generic_irq(struct netmap_adapter *na, u_int q, u_int *work_done)
211f9790aebSLuigi Rizzo {
2124bf50f18SLuigi Rizzo 	if (unlikely(!nm_netmap_on(na)))
213f9790aebSLuigi Rizzo 		return;
214f9790aebSLuigi Rizzo 
21537e3a6d3SLuigi Rizzo 	netmap_common_irq(na, q, work_done);
21637e3a6d3SLuigi Rizzo #ifdef RATE_GENERIC
21737e3a6d3SLuigi Rizzo 	if (work_done)
21837e3a6d3SLuigi Rizzo 		rate_ctx.new.rxirq++;
21937e3a6d3SLuigi Rizzo 	else
22037e3a6d3SLuigi Rizzo 		rate_ctx.new.txirq++;
22137e3a6d3SLuigi Rizzo #endif  /* RATE_GENERIC */
222f9790aebSLuigi Rizzo }
223f9790aebSLuigi Rizzo 
22437e3a6d3SLuigi Rizzo static int
22537e3a6d3SLuigi Rizzo generic_netmap_unregister(struct netmap_adapter *na)
22637e3a6d3SLuigi Rizzo {
22737e3a6d3SLuigi Rizzo 	struct netmap_generic_adapter *gna = (struct netmap_generic_adapter *)na;
22837e3a6d3SLuigi Rizzo 	struct netmap_kring *kring = NULL;
22937e3a6d3SLuigi Rizzo 	int i, r;
23037e3a6d3SLuigi Rizzo 
23137e3a6d3SLuigi Rizzo 	if (na->active_fds == 0) {
23237e3a6d3SLuigi Rizzo 		na->na_flags &= ~NAF_NETMAP_ON;
23337e3a6d3SLuigi Rizzo 
23437e3a6d3SLuigi Rizzo 		/* Stop intercepting packets on the RX path. */
23537e3a6d3SLuigi Rizzo 		nm_os_catch_rx(gna, 0);
23637e3a6d3SLuigi Rizzo 
2374f80b14cSVincenzo Maffione 		/* Release packet steering control. */
2384f80b14cSVincenzo Maffione 		nm_os_catch_tx(gna, 0);
23937e3a6d3SLuigi Rizzo 	}
24037e3a6d3SLuigi Rizzo 
24137e3a6d3SLuigi Rizzo 	for_each_rx_kring_h(r, kring, na) {
24237e3a6d3SLuigi Rizzo 		if (nm_kring_pending_off(kring)) {
243c3e9b4dbSLuiz Otavio O Souza 			D("Emulated adapter: ring '%s' deactivated", kring->name);
24437e3a6d3SLuigi Rizzo 			kring->nr_mode = NKR_NETMAP_OFF;
24537e3a6d3SLuigi Rizzo 		}
24637e3a6d3SLuigi Rizzo 	}
24737e3a6d3SLuigi Rizzo 	for_each_tx_kring_h(r, kring, na) {
24837e3a6d3SLuigi Rizzo 		if (nm_kring_pending_off(kring)) {
24937e3a6d3SLuigi Rizzo 			kring->nr_mode = NKR_NETMAP_OFF;
250c3e9b4dbSLuiz Otavio O Souza 			D("Emulated adapter: ring '%s' deactivated", kring->name);
25137e3a6d3SLuigi Rizzo 		}
25237e3a6d3SLuigi Rizzo 	}
25337e3a6d3SLuigi Rizzo 
25437e3a6d3SLuigi Rizzo 	for_each_rx_kring(r, kring, na) {
25537e3a6d3SLuigi Rizzo 		/* Free the mbufs still pending in the RX queues,
25637e3a6d3SLuigi Rizzo 		 * that did not end up into the corresponding netmap
25737e3a6d3SLuigi Rizzo 		 * RX rings. */
25837e3a6d3SLuigi Rizzo 		mbq_safe_purge(&kring->rx_queue);
25937e3a6d3SLuigi Rizzo 		nm_os_mitigation_cleanup(&gna->mit[r]);
26037e3a6d3SLuigi Rizzo 	}
26137e3a6d3SLuigi Rizzo 
26237e3a6d3SLuigi Rizzo 	/* Decrement reference counter for the mbufs in the
26337e3a6d3SLuigi Rizzo 	 * TX pools. These mbufs can be still pending in drivers,
26437e3a6d3SLuigi Rizzo 	 * (e.g. this happens with virtio-net driver, which
26537e3a6d3SLuigi Rizzo 	 * does lazy reclaiming of transmitted mbufs). */
26637e3a6d3SLuigi Rizzo 	for_each_tx_kring(r, kring, na) {
26737e3a6d3SLuigi Rizzo 		/* We must remove the destructor on the TX event,
26837e3a6d3SLuigi Rizzo 		 * because the destructor invokes netmap code, and
26937e3a6d3SLuigi Rizzo 		 * the netmap module may disappear before the
27037e3a6d3SLuigi Rizzo 		 * TX event is consumed. */
27137e3a6d3SLuigi Rizzo 		mtx_lock_spin(&kring->tx_event_lock);
27237e3a6d3SLuigi Rizzo 		if (kring->tx_event) {
273c3e9b4dbSLuiz Otavio O Souza 			SET_MBUF_DESTRUCTOR(kring->tx_event, NULL);
27437e3a6d3SLuigi Rizzo 		}
27537e3a6d3SLuigi Rizzo 		kring->tx_event = NULL;
27637e3a6d3SLuigi Rizzo 		mtx_unlock_spin(&kring->tx_event_lock);
27737e3a6d3SLuigi Rizzo 	}
27837e3a6d3SLuigi Rizzo 
27937e3a6d3SLuigi Rizzo 	if (na->active_fds == 0) {
280c3e9b4dbSLuiz Otavio O Souza 		nm_os_free(gna->mit);
28137e3a6d3SLuigi Rizzo 
28237e3a6d3SLuigi Rizzo 		for_each_rx_kring(r, kring, na) {
28337e3a6d3SLuigi Rizzo 			mbq_safe_fini(&kring->rx_queue);
28437e3a6d3SLuigi Rizzo 		}
28537e3a6d3SLuigi Rizzo 
28637e3a6d3SLuigi Rizzo 		for_each_tx_kring(r, kring, na) {
28737e3a6d3SLuigi Rizzo 			mtx_destroy(&kring->tx_event_lock);
28837e3a6d3SLuigi Rizzo 			if (kring->tx_pool == NULL) {
28937e3a6d3SLuigi Rizzo 				continue;
29037e3a6d3SLuigi Rizzo 			}
29137e3a6d3SLuigi Rizzo 
29237e3a6d3SLuigi Rizzo 			for (i=0; i<na->num_tx_desc; i++) {
29337e3a6d3SLuigi Rizzo 				if (kring->tx_pool[i]) {
29437e3a6d3SLuigi Rizzo 					m_freem(kring->tx_pool[i]);
29537e3a6d3SLuigi Rizzo 				}
29637e3a6d3SLuigi Rizzo 			}
297c3e9b4dbSLuiz Otavio O Souza 			nm_os_free(kring->tx_pool);
29837e3a6d3SLuigi Rizzo 			kring->tx_pool = NULL;
29937e3a6d3SLuigi Rizzo 		}
30037e3a6d3SLuigi Rizzo 
30137e3a6d3SLuigi Rizzo #ifdef RATE_GENERIC
30237e3a6d3SLuigi Rizzo 		if (--rate_ctx.refcount == 0) {
30337e3a6d3SLuigi Rizzo 			D("del_timer()");
30437e3a6d3SLuigi Rizzo 			del_timer(&rate_ctx.timer);
30537e3a6d3SLuigi Rizzo 		}
30637e3a6d3SLuigi Rizzo #endif
307c3e9b4dbSLuiz Otavio O Souza 		D("Emulated adapter for %s deactivated", na->name);
30837e3a6d3SLuigi Rizzo 	}
30937e3a6d3SLuigi Rizzo 
31037e3a6d3SLuigi Rizzo 	return 0;
31137e3a6d3SLuigi Rizzo }
312f9790aebSLuigi Rizzo 
313f9790aebSLuigi Rizzo /* Enable/disable netmap mode for a generic network interface. */
31417885a7bSLuigi Rizzo static int
31517885a7bSLuigi Rizzo generic_netmap_register(struct netmap_adapter *na, int enable)
316f9790aebSLuigi Rizzo {
317f9790aebSLuigi Rizzo 	struct netmap_generic_adapter *gna = (struct netmap_generic_adapter *)na;
31837e3a6d3SLuigi Rizzo 	struct netmap_kring *kring = NULL;
319f9790aebSLuigi Rizzo 	int error;
320f9790aebSLuigi Rizzo 	int i, r;
321f9790aebSLuigi Rizzo 
32237e3a6d3SLuigi Rizzo 	if (!na) {
323f9790aebSLuigi Rizzo 		return EINVAL;
324f9790aebSLuigi Rizzo 	}
325f9790aebSLuigi Rizzo 
32637e3a6d3SLuigi Rizzo 	if (!enable) {
32737e3a6d3SLuigi Rizzo 		/* This is actually an unregif. */
32837e3a6d3SLuigi Rizzo 		return generic_netmap_unregister(na);
32937e3a6d3SLuigi Rizzo 	}
33037e3a6d3SLuigi Rizzo 
33137e3a6d3SLuigi Rizzo 	if (na->active_fds == 0) {
332c3e9b4dbSLuiz Otavio O Souza 		D("Emulated adapter for %s activated", na->name);
33337e3a6d3SLuigi Rizzo 		/* Do all memory allocations when (na->active_fds == 0), to
33437e3a6d3SLuigi Rizzo 		 * simplify error management. */
33537e3a6d3SLuigi Rizzo 
33637e3a6d3SLuigi Rizzo 		/* Allocate memory for mitigation support on all the rx queues. */
337c3e9b4dbSLuiz Otavio O Souza 		gna->mit = nm_os_malloc(na->num_rx_rings * sizeof(struct nm_generic_mit));
338f0ea3689SLuigi Rizzo 		if (!gna->mit) {
339f0ea3689SLuigi Rizzo 			D("mitigation allocation failed");
340f0ea3689SLuigi Rizzo 			error = ENOMEM;
341f0ea3689SLuigi Rizzo 			goto out;
342f0ea3689SLuigi Rizzo 		}
34337e3a6d3SLuigi Rizzo 
34437e3a6d3SLuigi Rizzo 		for_each_rx_kring(r, kring, na) {
34537e3a6d3SLuigi Rizzo 			/* Init mitigation support. */
34637e3a6d3SLuigi Rizzo 			nm_os_mitigation_init(&gna->mit[r], r, na);
347f0ea3689SLuigi Rizzo 
348f9790aebSLuigi Rizzo 			/* Initialize the rx queue, as generic_rx_handler() can
34937e3a6d3SLuigi Rizzo 			 * be called as soon as nm_os_catch_rx() returns.
350f9790aebSLuigi Rizzo 			 */
35137e3a6d3SLuigi Rizzo 			mbq_safe_init(&kring->rx_queue);
352f9790aebSLuigi Rizzo 		}
353f9790aebSLuigi Rizzo 
354f9790aebSLuigi Rizzo 		/*
35537e3a6d3SLuigi Rizzo 		 * Prepare mbuf pools (parallel to the tx rings), for packet
35637e3a6d3SLuigi Rizzo 		 * transmission. Don't preallocate the mbufs here, it's simpler
35737e3a6d3SLuigi Rizzo 		 * to leave this task to txsync.
358f9790aebSLuigi Rizzo 		 */
35937e3a6d3SLuigi Rizzo 		for_each_tx_kring(r, kring, na) {
36037e3a6d3SLuigi Rizzo 			kring->tx_pool = NULL;
36137e3a6d3SLuigi Rizzo 		}
36237e3a6d3SLuigi Rizzo 		for_each_tx_kring(r, kring, na) {
36337e3a6d3SLuigi Rizzo 			kring->tx_pool =
364c3e9b4dbSLuiz Otavio O Souza 				nm_os_malloc(na->num_tx_desc * sizeof(struct mbuf *));
36537e3a6d3SLuigi Rizzo 			if (!kring->tx_pool) {
366f9790aebSLuigi Rizzo 				D("tx_pool allocation failed");
367f9790aebSLuigi Rizzo 				error = ENOMEM;
36817885a7bSLuigi Rizzo 				goto free_tx_pools;
369f9790aebSLuigi Rizzo 			}
37037e3a6d3SLuigi Rizzo 			mtx_init(&kring->tx_event_lock, "tx_event_lock",
37137e3a6d3SLuigi Rizzo 				 NULL, MTX_SPIN);
37237e3a6d3SLuigi Rizzo 		}
37337e3a6d3SLuigi Rizzo 	}
37437e3a6d3SLuigi Rizzo 
37537e3a6d3SLuigi Rizzo 	for_each_rx_kring_h(r, kring, na) {
37637e3a6d3SLuigi Rizzo 		if (nm_kring_pending_on(kring)) {
377c3e9b4dbSLuiz Otavio O Souza 			D("Emulated adapter: ring '%s' activated", kring->name);
37837e3a6d3SLuigi Rizzo 			kring->nr_mode = NKR_NETMAP_ON;
37937e3a6d3SLuigi Rizzo 		}
38037e3a6d3SLuigi Rizzo 
38137e3a6d3SLuigi Rizzo 	}
38237e3a6d3SLuigi Rizzo 	for_each_tx_kring_h(r, kring, na) {
38337e3a6d3SLuigi Rizzo 		if (nm_kring_pending_on(kring)) {
384c3e9b4dbSLuiz Otavio O Souza 			D("Emulated adapter: ring '%s' activated", kring->name);
38537e3a6d3SLuigi Rizzo 			kring->nr_mode = NKR_NETMAP_ON;
38637e3a6d3SLuigi Rizzo 		}
38737e3a6d3SLuigi Rizzo 	}
38837e3a6d3SLuigi Rizzo 
38937e3a6d3SLuigi Rizzo 	for_each_tx_kring(r, kring, na) {
39037e3a6d3SLuigi Rizzo 		/* Initialize tx_pool and tx_event. */
391f9790aebSLuigi Rizzo 		for (i=0; i<na->num_tx_desc; i++) {
39237e3a6d3SLuigi Rizzo 			kring->tx_pool[i] = NULL;
393f9790aebSLuigi Rizzo 		}
39437e3a6d3SLuigi Rizzo 
39537e3a6d3SLuigi Rizzo 		kring->tx_event = NULL;
396f9790aebSLuigi Rizzo 	}
39737e3a6d3SLuigi Rizzo 
39837e3a6d3SLuigi Rizzo 	if (na->active_fds == 0) {
399f9790aebSLuigi Rizzo 		/* Prepare to intercept incoming traffic. */
40037e3a6d3SLuigi Rizzo 		error = nm_os_catch_rx(gna, 1);
401f9790aebSLuigi Rizzo 		if (error) {
40237e3a6d3SLuigi Rizzo 			D("nm_os_catch_rx(1) failed (%d)", error);
4034f80b14cSVincenzo Maffione 			goto free_tx_pools;
404f9790aebSLuigi Rizzo 		}
405f9790aebSLuigi Rizzo 
4064f80b14cSVincenzo Maffione 		/* Let netmap control the packet steering. */
40737e3a6d3SLuigi Rizzo 		error = nm_os_catch_tx(gna, 1);
40837e3a6d3SLuigi Rizzo 		if (error) {
40937e3a6d3SLuigi Rizzo 			D("nm_os_catch_tx(1) failed (%d)", error);
41037e3a6d3SLuigi Rizzo 			goto catch_rx;
41137e3a6d3SLuigi Rizzo 		}
412f9790aebSLuigi Rizzo 
41337e3a6d3SLuigi Rizzo 		na->na_flags |= NAF_NETMAP_ON;
41437e3a6d3SLuigi Rizzo 
4154bf50f18SLuigi Rizzo #ifdef RATE_GENERIC
416f9790aebSLuigi Rizzo 		if (rate_ctx.refcount == 0) {
417f9790aebSLuigi Rizzo 			D("setup_timer()");
418f9790aebSLuigi Rizzo 			memset(&rate_ctx, 0, sizeof(rate_ctx));
419f9790aebSLuigi Rizzo 			setup_timer(&rate_ctx.timer, &rate_callback, (unsigned long)&rate_ctx);
420f9790aebSLuigi Rizzo 			if (mod_timer(&rate_ctx.timer, jiffies + msecs_to_jiffies(1500))) {
421f9790aebSLuigi Rizzo 				D("Error: mod_timer()");
422f9790aebSLuigi Rizzo 			}
423f9790aebSLuigi Rizzo 		}
424f9790aebSLuigi Rizzo 		rate_ctx.refcount++;
425f9790aebSLuigi Rizzo #endif /* RATE */
426f9790aebSLuigi Rizzo 	}
427f9790aebSLuigi Rizzo 
428f9790aebSLuigi Rizzo 	return 0;
429f9790aebSLuigi Rizzo 
43037e3a6d3SLuigi Rizzo 	/* Here (na->active_fds == 0) holds. */
43137e3a6d3SLuigi Rizzo catch_rx:
43237e3a6d3SLuigi Rizzo 	nm_os_catch_rx(gna, 0);
43317885a7bSLuigi Rizzo free_tx_pools:
43437e3a6d3SLuigi Rizzo 	for_each_tx_kring(r, kring, na) {
43537e3a6d3SLuigi Rizzo 		mtx_destroy(&kring->tx_event_lock);
43637e3a6d3SLuigi Rizzo 		if (kring->tx_pool == NULL) {
43717885a7bSLuigi Rizzo 			continue;
438f2637526SLuigi Rizzo 		}
439c3e9b4dbSLuiz Otavio O Souza 		nm_os_free(kring->tx_pool);
44037e3a6d3SLuigi Rizzo 		kring->tx_pool = NULL;
44137e3a6d3SLuigi Rizzo 	}
44237e3a6d3SLuigi Rizzo 	for_each_rx_kring(r, kring, na) {
44337e3a6d3SLuigi Rizzo 		mbq_safe_fini(&kring->rx_queue);
444f9790aebSLuigi Rizzo 	}
445c3e9b4dbSLuiz Otavio O Souza 	nm_os_free(gna->mit);
446f0ea3689SLuigi Rizzo out:
447f9790aebSLuigi Rizzo 
448f9790aebSLuigi Rizzo 	return error;
449f9790aebSLuigi Rizzo }
450f9790aebSLuigi Rizzo 
451f9790aebSLuigi Rizzo /*
452f9790aebSLuigi Rizzo  * Callback invoked when the device driver frees an mbuf used
453f9790aebSLuigi Rizzo  * by netmap to transmit a packet. This usually happens when
454f9790aebSLuigi Rizzo  * the NIC notifies the driver that transmission is completed.
455f9790aebSLuigi Rizzo  */
456f9790aebSLuigi Rizzo static void
457f9790aebSLuigi Rizzo generic_mbuf_destructor(struct mbuf *m)
458f9790aebSLuigi Rizzo {
45937e3a6d3SLuigi Rizzo 	struct netmap_adapter *na = NA(GEN_TX_MBUF_IFP(m));
46037e3a6d3SLuigi Rizzo 	struct netmap_kring *kring;
46137e3a6d3SLuigi Rizzo 	unsigned int r = MBUF_TXQ(m);
46237e3a6d3SLuigi Rizzo 	unsigned int r_orig = r;
46337e3a6d3SLuigi Rizzo 
46437e3a6d3SLuigi Rizzo 	if (unlikely(!nm_netmap_on(na) || r >= na->num_tx_rings)) {
46537e3a6d3SLuigi Rizzo 		D("Error: no netmap adapter on device %p",
46637e3a6d3SLuigi Rizzo 		  GEN_TX_MBUF_IFP(m));
46737e3a6d3SLuigi Rizzo 		return;
46837e3a6d3SLuigi Rizzo 	}
46937e3a6d3SLuigi Rizzo 
47037e3a6d3SLuigi Rizzo 	/*
47137e3a6d3SLuigi Rizzo 	 * First, clear the event mbuf.
47237e3a6d3SLuigi Rizzo 	 * In principle, the event 'm' should match the one stored
47337e3a6d3SLuigi Rizzo 	 * on ring 'r'. However we check it explicitely to stay
47437e3a6d3SLuigi Rizzo 	 * safe against lower layers (qdisc, driver, etc.) changing
47537e3a6d3SLuigi Rizzo 	 * MBUF_TXQ(m) under our feet. If the match is not found
47637e3a6d3SLuigi Rizzo 	 * on 'r', we try to see if it belongs to some other ring.
47737e3a6d3SLuigi Rizzo 	 */
47837e3a6d3SLuigi Rizzo 	for (;;) {
47937e3a6d3SLuigi Rizzo 		bool match = false;
48037e3a6d3SLuigi Rizzo 
4812ff91c17SVincenzo Maffione 		kring = na->tx_rings[r];
48237e3a6d3SLuigi Rizzo 		mtx_lock_spin(&kring->tx_event_lock);
48337e3a6d3SLuigi Rizzo 		if (kring->tx_event == m) {
48437e3a6d3SLuigi Rizzo 			kring->tx_event = NULL;
48537e3a6d3SLuigi Rizzo 			match = true;
48637e3a6d3SLuigi Rizzo 		}
48737e3a6d3SLuigi Rizzo 		mtx_unlock_spin(&kring->tx_event_lock);
48837e3a6d3SLuigi Rizzo 
48937e3a6d3SLuigi Rizzo 		if (match) {
49037e3a6d3SLuigi Rizzo 			if (r != r_orig) {
49137e3a6d3SLuigi Rizzo 				RD(1, "event %p migrated: ring %u --> %u",
49237e3a6d3SLuigi Rizzo 				      m, r_orig, r);
49337e3a6d3SLuigi Rizzo 			}
49437e3a6d3SLuigi Rizzo 			break;
49537e3a6d3SLuigi Rizzo 		}
49637e3a6d3SLuigi Rizzo 
49737e3a6d3SLuigi Rizzo 		if (++r == na->num_tx_rings) r = 0;
49837e3a6d3SLuigi Rizzo 
49937e3a6d3SLuigi Rizzo 		if (r == r_orig) {
50037e3a6d3SLuigi Rizzo 			RD(1, "Cannot match event %p", m);
50137e3a6d3SLuigi Rizzo 			return;
50237e3a6d3SLuigi Rizzo 		}
50337e3a6d3SLuigi Rizzo 	}
50437e3a6d3SLuigi Rizzo 
50537e3a6d3SLuigi Rizzo 	/* Second, wake up clients. They will reclaim the event through
50637e3a6d3SLuigi Rizzo 	 * txsync. */
50737e3a6d3SLuigi Rizzo 	netmap_generic_irq(na, r, NULL);
508f9790aebSLuigi Rizzo #ifdef __FreeBSD__
5094f80b14cSVincenzo Maffione #if __FreeBSD_version <= 1200050
5104f80b14cSVincenzo Maffione 	void_mbuf_dtor(m, NULL, NULL);
5114f80b14cSVincenzo Maffione #else  /* __FreeBSD_version >= 1200051 */
512e8fd18f3SGleb Smirnoff 	void_mbuf_dtor(m);
5134f80b14cSVincenzo Maffione #endif /* __FreeBSD_version >= 1200051 */
51437e3a6d3SLuigi Rizzo #endif
515f9790aebSLuigi Rizzo }
516f9790aebSLuigi Rizzo 
51717885a7bSLuigi Rizzo /* Record completed transmissions and update hwtail.
518f9790aebSLuigi Rizzo  *
51917885a7bSLuigi Rizzo  * The oldest tx buffer not yet completed is at nr_hwtail + 1,
520f9790aebSLuigi Rizzo  * nr_hwcur is the first unsent buffer.
521f9790aebSLuigi Rizzo  */
52217885a7bSLuigi Rizzo static u_int
52337e3a6d3SLuigi Rizzo generic_netmap_tx_clean(struct netmap_kring *kring, int txqdisc)
524f9790aebSLuigi Rizzo {
52517885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
52617885a7bSLuigi Rizzo 	u_int nm_i = nm_next(kring->nr_hwtail, lim);
527f9790aebSLuigi Rizzo 	u_int hwcur = kring->nr_hwcur;
528f9790aebSLuigi Rizzo 	u_int n = 0;
529f9790aebSLuigi Rizzo 	struct mbuf **tx_pool = kring->tx_pool;
530f9790aebSLuigi Rizzo 
53137e3a6d3SLuigi Rizzo 	ND("hwcur = %d, hwtail = %d", kring->nr_hwcur, kring->nr_hwtail);
53237e3a6d3SLuigi Rizzo 
53317885a7bSLuigi Rizzo 	while (nm_i != hwcur) { /* buffers not completed */
53417885a7bSLuigi Rizzo 		struct mbuf *m = tx_pool[nm_i];
535f9790aebSLuigi Rizzo 
53637e3a6d3SLuigi Rizzo 		if (txqdisc) {
53737e3a6d3SLuigi Rizzo 			if (m == NULL) {
53837e3a6d3SLuigi Rizzo 				/* Nothing to do, this is going
53937e3a6d3SLuigi Rizzo 				 * to be replenished. */
54037e3a6d3SLuigi Rizzo 				RD(3, "Is this happening?");
54137e3a6d3SLuigi Rizzo 
54237e3a6d3SLuigi Rizzo 			} else if (MBUF_QUEUED(m)) {
54337e3a6d3SLuigi Rizzo 				break; /* Not dequeued yet. */
54437e3a6d3SLuigi Rizzo 
54537e3a6d3SLuigi Rizzo 			} else if (MBUF_REFCNT(m) != 1) {
54637e3a6d3SLuigi Rizzo 				/* This mbuf has been dequeued but is still busy
54737e3a6d3SLuigi Rizzo 				 * (refcount is 2).
54837e3a6d3SLuigi Rizzo 				 * Leave it to the driver and replenish. */
54937e3a6d3SLuigi Rizzo 				m_freem(m);
55037e3a6d3SLuigi Rizzo 				tx_pool[nm_i] = NULL;
551f9790aebSLuigi Rizzo 			}
55237e3a6d3SLuigi Rizzo 
55337e3a6d3SLuigi Rizzo 		} else {
55437e3a6d3SLuigi Rizzo 			if (unlikely(m == NULL)) {
55537e3a6d3SLuigi Rizzo 				int event_consumed;
55637e3a6d3SLuigi Rizzo 
55737e3a6d3SLuigi Rizzo 				/* This slot was used to place an event. */
55837e3a6d3SLuigi Rizzo 				mtx_lock_spin(&kring->tx_event_lock);
55937e3a6d3SLuigi Rizzo 				event_consumed = (kring->tx_event == NULL);
56037e3a6d3SLuigi Rizzo 				mtx_unlock_spin(&kring->tx_event_lock);
56137e3a6d3SLuigi Rizzo 				if (!event_consumed) {
56237e3a6d3SLuigi Rizzo 					/* The event has not been consumed yet,
56337e3a6d3SLuigi Rizzo 					 * still busy in the driver. */
56437e3a6d3SLuigi Rizzo 					break;
565f9790aebSLuigi Rizzo 				}
56637e3a6d3SLuigi Rizzo 				/* The event has been consumed, we can go
56737e3a6d3SLuigi Rizzo 				 * ahead. */
56837e3a6d3SLuigi Rizzo 
56937e3a6d3SLuigi Rizzo 			} else if (MBUF_REFCNT(m) != 1) {
57037e3a6d3SLuigi Rizzo 				/* This mbuf is still busy: its refcnt is 2. */
57137e3a6d3SLuigi Rizzo 				break;
57237e3a6d3SLuigi Rizzo 			}
57337e3a6d3SLuigi Rizzo 		}
57437e3a6d3SLuigi Rizzo 
575f9790aebSLuigi Rizzo 		n++;
57617885a7bSLuigi Rizzo 		nm_i = nm_next(nm_i, lim);
577f9790aebSLuigi Rizzo 	}
57817885a7bSLuigi Rizzo 	kring->nr_hwtail = nm_prev(nm_i, lim);
57917885a7bSLuigi Rizzo 	ND("tx completed [%d] -> hwtail %d", n, kring->nr_hwtail);
580f9790aebSLuigi Rizzo 
581f9790aebSLuigi Rizzo 	return n;
582f9790aebSLuigi Rizzo }
583f9790aebSLuigi Rizzo 
58437e3a6d3SLuigi Rizzo /* Compute a slot index in the middle between inf and sup. */
585f9790aebSLuigi Rizzo static inline u_int
58637e3a6d3SLuigi Rizzo ring_middle(u_int inf, u_int sup, u_int lim)
587f9790aebSLuigi Rizzo {
58837e3a6d3SLuigi Rizzo 	u_int n = lim + 1;
589f9790aebSLuigi Rizzo 	u_int e;
590f9790aebSLuigi Rizzo 
59137e3a6d3SLuigi Rizzo 	if (sup >= inf) {
59237e3a6d3SLuigi Rizzo 		e = (sup + inf) / 2;
593f9790aebSLuigi Rizzo 	} else { /* wrap around */
59437e3a6d3SLuigi Rizzo 		e = (sup + n + inf) / 2;
595f9790aebSLuigi Rizzo 		if (e >= n) {
596f9790aebSLuigi Rizzo 			e -= n;
597f9790aebSLuigi Rizzo 		}
598f9790aebSLuigi Rizzo 	}
599f9790aebSLuigi Rizzo 
600f9790aebSLuigi Rizzo 	if (unlikely(e >= n)) {
601f9790aebSLuigi Rizzo 		D("This cannot happen");
602f9790aebSLuigi Rizzo 		e = 0;
603f9790aebSLuigi Rizzo 	}
604f9790aebSLuigi Rizzo 
605f9790aebSLuigi Rizzo 	return e;
606f9790aebSLuigi Rizzo }
607f9790aebSLuigi Rizzo 
608f9790aebSLuigi Rizzo static void
609f9790aebSLuigi Rizzo generic_set_tx_event(struct netmap_kring *kring, u_int hwcur)
610f9790aebSLuigi Rizzo {
61137e3a6d3SLuigi Rizzo 	u_int lim = kring->nkr_num_slots - 1;
612f9790aebSLuigi Rizzo 	struct mbuf *m;
613f9790aebSLuigi Rizzo 	u_int e;
61437e3a6d3SLuigi Rizzo 	u_int ntc = nm_next(kring->nr_hwtail, lim); /* next to clean */
615f9790aebSLuigi Rizzo 
61637e3a6d3SLuigi Rizzo 	if (ntc == hwcur) {
61717885a7bSLuigi Rizzo 		return; /* all buffers are free */
618f9790aebSLuigi Rizzo 	}
61937e3a6d3SLuigi Rizzo 
62037e3a6d3SLuigi Rizzo 	/*
62137e3a6d3SLuigi Rizzo 	 * We have pending packets in the driver between hwtail+1
62237e3a6d3SLuigi Rizzo 	 * and hwcur, and we have to chose one of these slot to
62337e3a6d3SLuigi Rizzo 	 * generate a notification.
62437e3a6d3SLuigi Rizzo 	 * There is a race but this is only called within txsync which
62537e3a6d3SLuigi Rizzo 	 * does a double check.
62637e3a6d3SLuigi Rizzo 	 */
62737e3a6d3SLuigi Rizzo #if 0
62837e3a6d3SLuigi Rizzo 	/* Choose a slot in the middle, so that we don't risk ending
62937e3a6d3SLuigi Rizzo 	 * up in a situation where the client continuously wake up,
63037e3a6d3SLuigi Rizzo 	 * fills one or a few TX slots and go to sleep again. */
63137e3a6d3SLuigi Rizzo 	e = ring_middle(ntc, hwcur, lim);
63237e3a6d3SLuigi Rizzo #else
63337e3a6d3SLuigi Rizzo 	/* Choose the first pending slot, to be safe against driver
63437e3a6d3SLuigi Rizzo 	 * reordering mbuf transmissions. */
63537e3a6d3SLuigi Rizzo 	e = ntc;
63637e3a6d3SLuigi Rizzo #endif
637f9790aebSLuigi Rizzo 
638f9790aebSLuigi Rizzo 	m = kring->tx_pool[e];
639f9790aebSLuigi Rizzo 	if (m == NULL) {
64037e3a6d3SLuigi Rizzo 		/* An event is already in place. */
641f9790aebSLuigi Rizzo 		return;
642f9790aebSLuigi Rizzo 	}
643f9790aebSLuigi Rizzo 
64437e3a6d3SLuigi Rizzo 	mtx_lock_spin(&kring->tx_event_lock);
64537e3a6d3SLuigi Rizzo 	if (kring->tx_event) {
64637e3a6d3SLuigi Rizzo 		/* An event is already in place. */
64737e3a6d3SLuigi Rizzo 		mtx_unlock_spin(&kring->tx_event_lock);
64837e3a6d3SLuigi Rizzo 		return;
64937e3a6d3SLuigi Rizzo 	}
65037e3a6d3SLuigi Rizzo 
651c3e9b4dbSLuiz Otavio O Souza 	SET_MBUF_DESTRUCTOR(m, generic_mbuf_destructor);
65237e3a6d3SLuigi Rizzo 	kring->tx_event = m;
65337e3a6d3SLuigi Rizzo 	mtx_unlock_spin(&kring->tx_event_lock);
65437e3a6d3SLuigi Rizzo 
65537e3a6d3SLuigi Rizzo 	kring->tx_pool[e] = NULL;
65637e3a6d3SLuigi Rizzo 
65737e3a6d3SLuigi Rizzo 	ND(5, "Request Event at %d mbuf %p refcnt %d", e, m, m ? MBUF_REFCNT(m) : -2 );
65837e3a6d3SLuigi Rizzo 
65937e3a6d3SLuigi Rizzo 	/* Decrement the refcount. This will free it if we lose the race
66037e3a6d3SLuigi Rizzo 	 * with the driver. */
661f9790aebSLuigi Rizzo 	m_freem(m);
662f9790aebSLuigi Rizzo 	smp_mb();
663f9790aebSLuigi Rizzo }
664f9790aebSLuigi Rizzo 
665f9790aebSLuigi Rizzo 
666f9790aebSLuigi Rizzo /*
667f9790aebSLuigi Rizzo  * generic_netmap_txsync() transforms netmap buffers into mbufs
668f9790aebSLuigi Rizzo  * and passes them to the standard device driver
669f9790aebSLuigi Rizzo  * (ndo_start_xmit() or ifp->if_transmit() ).
670f9790aebSLuigi Rizzo  * On linux this is not done directly, but using dev_queue_xmit(),
671f9790aebSLuigi Rizzo  * since it implements the TX flow control (and takes some locks).
672f9790aebSLuigi Rizzo  */
673f9790aebSLuigi Rizzo static int
6744bf50f18SLuigi Rizzo generic_netmap_txsync(struct netmap_kring *kring, int flags)
675f9790aebSLuigi Rizzo {
6764bf50f18SLuigi Rizzo 	struct netmap_adapter *na = kring->na;
67737e3a6d3SLuigi Rizzo 	struct netmap_generic_adapter *gna = (struct netmap_generic_adapter *)na;
678f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
679f9790aebSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
68017885a7bSLuigi Rizzo 	u_int nm_i;	/* index into the netmap ring */ // j
68117885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
68217885a7bSLuigi Rizzo 	u_int const head = kring->rhead;
6834bf50f18SLuigi Rizzo 	u_int ring_nr = kring->ring_id;
684f9790aebSLuigi Rizzo 
685f9790aebSLuigi Rizzo 	IFRATE(rate_ctx.new.txsync++);
686f9790aebSLuigi Rizzo 
687f9790aebSLuigi Rizzo 	rmb();
68817885a7bSLuigi Rizzo 
689f9790aebSLuigi Rizzo 	/*
69017885a7bSLuigi Rizzo 	 * First part: process new packets to send.
691f9790aebSLuigi Rizzo 	 */
69217885a7bSLuigi Rizzo 	nm_i = kring->nr_hwcur;
69317885a7bSLuigi Rizzo 	if (nm_i != head) {	/* we have new packets to send */
69437e3a6d3SLuigi Rizzo 		struct nm_os_gen_arg a;
69537e3a6d3SLuigi Rizzo 		u_int event = -1;
69637e3a6d3SLuigi Rizzo 
69737e3a6d3SLuigi Rizzo 		if (gna->txqdisc && nm_kr_txempty(kring)) {
69837e3a6d3SLuigi Rizzo 			/* In txqdisc mode, we ask for a delayed notification,
69937e3a6d3SLuigi Rizzo 			 * but only when cur == hwtail, which means that the
70037e3a6d3SLuigi Rizzo 			 * client is going to block. */
70137e3a6d3SLuigi Rizzo 			event = ring_middle(nm_i, head, lim);
70237e3a6d3SLuigi Rizzo 			ND(3, "Place txqdisc event (hwcur=%u,event=%u,"
70337e3a6d3SLuigi Rizzo 			      "head=%u,hwtail=%u)", nm_i, event, head,
70437e3a6d3SLuigi Rizzo 			      kring->nr_hwtail);
70537e3a6d3SLuigi Rizzo 		}
70637e3a6d3SLuigi Rizzo 
70737e3a6d3SLuigi Rizzo 		a.ifp = ifp;
70837e3a6d3SLuigi Rizzo 		a.ring_nr = ring_nr;
70937e3a6d3SLuigi Rizzo 		a.head = a.tail = NULL;
71037e3a6d3SLuigi Rizzo 
71117885a7bSLuigi Rizzo 		while (nm_i != head) {
71217885a7bSLuigi Rizzo 			struct netmap_slot *slot = &ring->slot[nm_i];
713f9790aebSLuigi Rizzo 			u_int len = slot->len;
7144bf50f18SLuigi Rizzo 			void *addr = NMB(na, slot);
71517885a7bSLuigi Rizzo 			/* device-specific */
716f9790aebSLuigi Rizzo 			struct mbuf *m;
717f9790aebSLuigi Rizzo 			int tx_ret;
718f9790aebSLuigi Rizzo 
7194bf50f18SLuigi Rizzo 			NM_CHECK_ADDR_LEN(na, addr, len);
72017885a7bSLuigi Rizzo 
72137e3a6d3SLuigi Rizzo 			/* Tale a mbuf from the tx pool (replenishing the pool
72237e3a6d3SLuigi Rizzo 			 * entry if necessary) and copy in the user packet. */
72317885a7bSLuigi Rizzo 			m = kring->tx_pool[nm_i];
724f9790aebSLuigi Rizzo 			if (unlikely(m == NULL)) {
72537e3a6d3SLuigi Rizzo 				kring->tx_pool[nm_i] = m =
72637e3a6d3SLuigi Rizzo 					nm_os_get_mbuf(ifp, NETMAP_BUF_SIZE(na));
72737e3a6d3SLuigi Rizzo 				if (m == NULL) {
72837e3a6d3SLuigi Rizzo 					RD(2, "Failed to replenish mbuf");
72937e3a6d3SLuigi Rizzo 					/* Here we could schedule a timer which
73037e3a6d3SLuigi Rizzo 					 * retries to replenish after a while,
73137e3a6d3SLuigi Rizzo 					 * and notifies the client when it
73237e3a6d3SLuigi Rizzo 					 * manages to replenish some slots. In
73337e3a6d3SLuigi Rizzo 					 * any case we break early to avoid
73437e3a6d3SLuigi Rizzo 					 * crashes. */
735f9790aebSLuigi Rizzo 					break;
736f9790aebSLuigi Rizzo 				}
73737e3a6d3SLuigi Rizzo 				IFRATE(rate_ctx.new.txrepl++);
738f9790aebSLuigi Rizzo 			}
73937e3a6d3SLuigi Rizzo 
74037e3a6d3SLuigi Rizzo 			a.m = m;
74137e3a6d3SLuigi Rizzo 			a.addr = addr;
74237e3a6d3SLuigi Rizzo 			a.len = len;
74337e3a6d3SLuigi Rizzo 			a.qevent = (nm_i == event);
74437e3a6d3SLuigi Rizzo 			/* When not in txqdisc mode, we should ask
74537e3a6d3SLuigi Rizzo 			 * notifications when NS_REPORT is set, or roughly
74637e3a6d3SLuigi Rizzo 			 * every half ring. To optimize this, we set a
74737e3a6d3SLuigi Rizzo 			 * notification event when the client runs out of
74837e3a6d3SLuigi Rizzo 			 * TX ring space, or when transmission fails. In
74937e3a6d3SLuigi Rizzo 			 * the latter case we also break early.
750f9790aebSLuigi Rizzo 			 */
75137e3a6d3SLuigi Rizzo 			tx_ret = nm_os_generic_xmit_frame(&a);
752f9790aebSLuigi Rizzo 			if (unlikely(tx_ret)) {
75337e3a6d3SLuigi Rizzo 				if (!gna->txqdisc) {
754f9790aebSLuigi Rizzo 					/*
755f9790aebSLuigi Rizzo 					 * No room for this mbuf in the device driver.
756f9790aebSLuigi Rizzo 					 * Request a notification FOR A PREVIOUS MBUF,
757f9790aebSLuigi Rizzo 					 * then call generic_netmap_tx_clean(kring) to do the
758f9790aebSLuigi Rizzo 					 * double check and see if we can free more buffers.
759f9790aebSLuigi Rizzo 					 * If there is space continue, else break;
760f9790aebSLuigi Rizzo 					 * NOTE: the double check is necessary if the problem
761f9790aebSLuigi Rizzo 					 * occurs in the txsync call after selrecord().
762f9790aebSLuigi Rizzo 					 * Also, we need some way to tell the caller that not
763f9790aebSLuigi Rizzo 					 * all buffers were queued onto the device (this was
764f9790aebSLuigi Rizzo 					 * not a problem with native netmap driver where space
765f9790aebSLuigi Rizzo 					 * is preallocated). The bridge has a similar problem
766f9790aebSLuigi Rizzo 					 * and we solve it there by dropping the excess packets.
767f9790aebSLuigi Rizzo 					 */
76817885a7bSLuigi Rizzo 					generic_set_tx_event(kring, nm_i);
76937e3a6d3SLuigi Rizzo 					if (generic_netmap_tx_clean(kring, gna->txqdisc)) {
77037e3a6d3SLuigi Rizzo 						/* space now available */
771f9790aebSLuigi Rizzo 						continue;
772f9790aebSLuigi Rizzo 					} else {
773f9790aebSLuigi Rizzo 						break;
774f9790aebSLuigi Rizzo 					}
775f9790aebSLuigi Rizzo 				}
77637e3a6d3SLuigi Rizzo 
77737e3a6d3SLuigi Rizzo 				/* In txqdisc mode, the netmap-aware qdisc
77837e3a6d3SLuigi Rizzo 				 * queue has the same length as the number of
77937e3a6d3SLuigi Rizzo 				 * netmap slots (N). Since tail is advanced
78037e3a6d3SLuigi Rizzo 				 * only when packets are dequeued, qdisc
78137e3a6d3SLuigi Rizzo 				 * queue overrun cannot happen, so
78237e3a6d3SLuigi Rizzo 				 * nm_os_generic_xmit_frame() did not fail
78337e3a6d3SLuigi Rizzo 				 * because of that.
78437e3a6d3SLuigi Rizzo 				 * However, packets can be dropped because
78537e3a6d3SLuigi Rizzo 				 * carrier is off, or because our qdisc is
78637e3a6d3SLuigi Rizzo 				 * being deactivated, or possibly for other
78737e3a6d3SLuigi Rizzo 				 * reasons. In these cases, we just let the
78837e3a6d3SLuigi Rizzo 				 * packet to be dropped. */
78937e3a6d3SLuigi Rizzo 				IFRATE(rate_ctx.new.txdrop++);
79037e3a6d3SLuigi Rizzo 			}
79137e3a6d3SLuigi Rizzo 
792f9790aebSLuigi Rizzo 			slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED);
79317885a7bSLuigi Rizzo 			nm_i = nm_next(nm_i, lim);
794f0ea3689SLuigi Rizzo 			IFRATE(rate_ctx.new.txpkt++);
795f9790aebSLuigi Rizzo 		}
79637e3a6d3SLuigi Rizzo 		if (a.head != NULL) {
79737e3a6d3SLuigi Rizzo 			a.addr = NULL;
79837e3a6d3SLuigi Rizzo 			nm_os_generic_xmit_frame(&a);
79937e3a6d3SLuigi Rizzo 		}
80037e3a6d3SLuigi Rizzo 		/* Update hwcur to the next slot to transmit. Here nm_i
80137e3a6d3SLuigi Rizzo 		 * is not necessarily head, we could break early. */
80237e3a6d3SLuigi Rizzo 		kring->nr_hwcur = nm_i;
80317885a7bSLuigi Rizzo 	}
804f9790aebSLuigi Rizzo 
80517885a7bSLuigi Rizzo 	/*
80617885a7bSLuigi Rizzo 	 * Second, reclaim completed buffers
80717885a7bSLuigi Rizzo 	 */
80837e3a6d3SLuigi Rizzo 	if (!gna->txqdisc && (flags & NAF_FORCE_RECLAIM || nm_kr_txempty(kring))) {
809f9790aebSLuigi Rizzo 		/* No more available slots? Set a notification event
810f9790aebSLuigi Rizzo 		 * on a netmap slot that will be cleaned in the future.
811f9790aebSLuigi Rizzo 		 * No doublecheck is performed, since txsync() will be
812f9790aebSLuigi Rizzo 		 * called twice by netmap_poll().
813f9790aebSLuigi Rizzo 		 */
81417885a7bSLuigi Rizzo 		generic_set_tx_event(kring, nm_i);
815f9790aebSLuigi Rizzo 	}
816f9790aebSLuigi Rizzo 
81737e3a6d3SLuigi Rizzo 	generic_netmap_tx_clean(kring, gna->txqdisc);
81817885a7bSLuigi Rizzo 
819f9790aebSLuigi Rizzo 	return 0;
820f9790aebSLuigi Rizzo }
821f9790aebSLuigi Rizzo 
82217885a7bSLuigi Rizzo 
823f9790aebSLuigi Rizzo /*
82437e3a6d3SLuigi Rizzo  * This handler is registered (through nm_os_catch_rx())
825f9790aebSLuigi Rizzo  * within the attached network interface
826f9790aebSLuigi Rizzo  * in the RX subsystem, so that every mbuf passed up by
827f9790aebSLuigi Rizzo  * the driver can be stolen to the network stack.
828f9790aebSLuigi Rizzo  * Stolen packets are put in a queue where the
829f9790aebSLuigi Rizzo  * generic_netmap_rxsync() callback can extract them.
83037e3a6d3SLuigi Rizzo  * Returns 1 if the packet was stolen, 0 otherwise.
831f9790aebSLuigi Rizzo  */
83237e3a6d3SLuigi Rizzo int
83317885a7bSLuigi Rizzo generic_rx_handler(struct ifnet *ifp, struct mbuf *m)
834f9790aebSLuigi Rizzo {
835f9790aebSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
836f9790aebSLuigi Rizzo 	struct netmap_generic_adapter *gna = (struct netmap_generic_adapter *)na;
83737e3a6d3SLuigi Rizzo 	struct netmap_kring *kring;
838f9790aebSLuigi Rizzo 	u_int work_done;
83937e3a6d3SLuigi Rizzo 	u_int r = MBUF_RXQ(m); /* receive ring number */
840f0ea3689SLuigi Rizzo 
84137e3a6d3SLuigi Rizzo 	if (r >= na->num_rx_rings) {
84237e3a6d3SLuigi Rizzo 		r = r % na->num_rx_rings;
84337e3a6d3SLuigi Rizzo 	}
84437e3a6d3SLuigi Rizzo 
8452ff91c17SVincenzo Maffione 	kring = na->rx_rings[r];
84637e3a6d3SLuigi Rizzo 
84737e3a6d3SLuigi Rizzo 	if (kring->nr_mode == NKR_NETMAP_OFF) {
84837e3a6d3SLuigi Rizzo 		/* We must not intercept this mbuf. */
84937e3a6d3SLuigi Rizzo 		return 0;
850f0ea3689SLuigi Rizzo 	}
851f9790aebSLuigi Rizzo 
852f9790aebSLuigi Rizzo 	/* limit the size of the queue */
85337e3a6d3SLuigi Rizzo 	if (unlikely(!gna->rxsg && MBUF_LEN(m) > NETMAP_BUF_SIZE(na))) {
85437e3a6d3SLuigi Rizzo 		/* This may happen when GRO/LRO features are enabled for
85537e3a6d3SLuigi Rizzo 		 * the NIC driver when the generic adapter does not
85637e3a6d3SLuigi Rizzo 		 * support RX scatter-gather. */
85737e3a6d3SLuigi Rizzo 		RD(2, "Warning: driver pushed up big packet "
85837e3a6d3SLuigi Rizzo 				"(size=%d)", (int)MBUF_LEN(m));
85937e3a6d3SLuigi Rizzo 		m_freem(m);
86037e3a6d3SLuigi Rizzo 	} else if (unlikely(mbq_len(&kring->rx_queue) > 1024)) {
861f9790aebSLuigi Rizzo 		m_freem(m);
862f9790aebSLuigi Rizzo 	} else {
86337e3a6d3SLuigi Rizzo 		mbq_safe_enqueue(&kring->rx_queue, m);
864f9790aebSLuigi Rizzo 	}
865f9790aebSLuigi Rizzo 
866f9790aebSLuigi Rizzo 	if (netmap_generic_mit < 32768) {
867f9790aebSLuigi Rizzo 		/* no rx mitigation, pass notification up */
86837e3a6d3SLuigi Rizzo 		netmap_generic_irq(na, r, &work_done);
869f9790aebSLuigi Rizzo 	} else {
870f9790aebSLuigi Rizzo 		/* same as send combining, filter notification if there is a
871f9790aebSLuigi Rizzo 		 * pending timer, otherwise pass it up and start a timer.
872f9790aebSLuigi Rizzo 		 */
87337e3a6d3SLuigi Rizzo 		if (likely(nm_os_mitigation_active(&gna->mit[r]))) {
874f9790aebSLuigi Rizzo 			/* Record that there is some pending work. */
87537e3a6d3SLuigi Rizzo 			gna->mit[r].mit_pending = 1;
876f9790aebSLuigi Rizzo 		} else {
87737e3a6d3SLuigi Rizzo 			netmap_generic_irq(na, r, &work_done);
87837e3a6d3SLuigi Rizzo 			nm_os_mitigation_start(&gna->mit[r]);
879f9790aebSLuigi Rizzo 		}
880f9790aebSLuigi Rizzo 	}
88137e3a6d3SLuigi Rizzo 
88237e3a6d3SLuigi Rizzo 	/* We have intercepted the mbuf. */
88337e3a6d3SLuigi Rizzo 	return 1;
884f9790aebSLuigi Rizzo }
885f9790aebSLuigi Rizzo 
886f9790aebSLuigi Rizzo /*
887f9790aebSLuigi Rizzo  * generic_netmap_rxsync() extracts mbufs from the queue filled by
888f9790aebSLuigi Rizzo  * generic_netmap_rx_handler() and puts their content in the netmap
889f9790aebSLuigi Rizzo  * receive ring.
890f9790aebSLuigi Rizzo  * Access must be protected because the rx handler is asynchronous,
891f9790aebSLuigi Rizzo  */
892f9790aebSLuigi Rizzo static int
8934bf50f18SLuigi Rizzo generic_netmap_rxsync(struct netmap_kring *kring, int flags)
894f9790aebSLuigi Rizzo {
895f9790aebSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
8964bf50f18SLuigi Rizzo 	struct netmap_adapter *na = kring->na;
89717885a7bSLuigi Rizzo 	u_int nm_i;	/* index into the netmap ring */ //j,
89817885a7bSLuigi Rizzo 	u_int n;
89917885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
900847bf383SLuigi Rizzo 	u_int const head = kring->rhead;
901f9790aebSLuigi Rizzo 	int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR;
902f9790aebSLuigi Rizzo 
90337e3a6d3SLuigi Rizzo 	/* Adapter-specific variables. */
90437e3a6d3SLuigi Rizzo 	u_int nm_buf_len = NETMAP_BUF_SIZE(na);
90537e3a6d3SLuigi Rizzo 	struct mbq tmpq;
90637e3a6d3SLuigi Rizzo 	struct mbuf *m;
90737e3a6d3SLuigi Rizzo 	int avail; /* in bytes */
90837e3a6d3SLuigi Rizzo 	int mlen;
90937e3a6d3SLuigi Rizzo 	int copy;
91037e3a6d3SLuigi Rizzo 
91117885a7bSLuigi Rizzo 	if (head > lim)
912f9790aebSLuigi Rizzo 		return netmap_ring_reinit(kring);
913f9790aebSLuigi Rizzo 
91437e3a6d3SLuigi Rizzo 	IFRATE(rate_ctx.new.rxsync++);
91517885a7bSLuigi Rizzo 
916f9790aebSLuigi Rizzo 	/*
91737e3a6d3SLuigi Rizzo 	 * First part: skip past packets that userspace has released.
91837e3a6d3SLuigi Rizzo 	 * This can possibly make room for the second part.
91917885a7bSLuigi Rizzo 	 */
92017885a7bSLuigi Rizzo 	nm_i = kring->nr_hwcur;
92117885a7bSLuigi Rizzo 	if (nm_i != head) {
922f9790aebSLuigi Rizzo 		/* Userspace has released some packets. */
92317885a7bSLuigi Rizzo 		for (n = 0; nm_i != head; n++) {
92417885a7bSLuigi Rizzo 			struct netmap_slot *slot = &ring->slot[nm_i];
925f9790aebSLuigi Rizzo 
926f9790aebSLuigi Rizzo 			slot->flags &= ~NS_BUF_CHANGED;
92717885a7bSLuigi Rizzo 			nm_i = nm_next(nm_i, lim);
928f9790aebSLuigi Rizzo 		}
92917885a7bSLuigi Rizzo 		kring->nr_hwcur = head;
930f9790aebSLuigi Rizzo 	}
93137e3a6d3SLuigi Rizzo 
93237e3a6d3SLuigi Rizzo 	/*
93337e3a6d3SLuigi Rizzo 	 * Second part: import newly received packets.
93437e3a6d3SLuigi Rizzo 	 */
93537e3a6d3SLuigi Rizzo 	if (!netmap_no_pendintr && !force_update) {
93637e3a6d3SLuigi Rizzo 		return 0;
93737e3a6d3SLuigi Rizzo 	}
93837e3a6d3SLuigi Rizzo 
93937e3a6d3SLuigi Rizzo 	nm_i = kring->nr_hwtail; /* First empty slot in the receive ring. */
94037e3a6d3SLuigi Rizzo 
94137e3a6d3SLuigi Rizzo 	/* Compute the available space (in bytes) in this netmap ring.
94237e3a6d3SLuigi Rizzo 	 * The first slot that is not considered in is the one before
94337e3a6d3SLuigi Rizzo 	 * nr_hwcur. */
94437e3a6d3SLuigi Rizzo 
94537e3a6d3SLuigi Rizzo 	avail = nm_prev(kring->nr_hwcur, lim) - nm_i;
94637e3a6d3SLuigi Rizzo 	if (avail < 0)
94737e3a6d3SLuigi Rizzo 		avail += lim + 1;
94837e3a6d3SLuigi Rizzo 	avail *= nm_buf_len;
94937e3a6d3SLuigi Rizzo 
95037e3a6d3SLuigi Rizzo 	/* First pass: While holding the lock on the RX mbuf queue,
95137e3a6d3SLuigi Rizzo 	 * extract as many mbufs as they fit the available space,
95237e3a6d3SLuigi Rizzo 	 * and put them in a temporary queue.
95337e3a6d3SLuigi Rizzo 	 * To avoid performing a per-mbuf division (mlen / nm_buf_len) to
95437e3a6d3SLuigi Rizzo 	 * to update avail, we do the update in a while loop that we
95537e3a6d3SLuigi Rizzo 	 * also use to set the RX slots, but without performing the copy. */
95637e3a6d3SLuigi Rizzo 	mbq_init(&tmpq);
95737e3a6d3SLuigi Rizzo 	mbq_lock(&kring->rx_queue);
95837e3a6d3SLuigi Rizzo 	for (n = 0;; n++) {
95937e3a6d3SLuigi Rizzo 		m = mbq_peek(&kring->rx_queue);
96037e3a6d3SLuigi Rizzo 		if (!m) {
96137e3a6d3SLuigi Rizzo 			/* No more packets from the driver. */
96237e3a6d3SLuigi Rizzo 			break;
96337e3a6d3SLuigi Rizzo 		}
96437e3a6d3SLuigi Rizzo 
96537e3a6d3SLuigi Rizzo 		mlen = MBUF_LEN(m);
96637e3a6d3SLuigi Rizzo 		if (mlen > avail) {
96737e3a6d3SLuigi Rizzo 			/* No more space in the ring. */
96837e3a6d3SLuigi Rizzo 			break;
96937e3a6d3SLuigi Rizzo 		}
97037e3a6d3SLuigi Rizzo 
97137e3a6d3SLuigi Rizzo 		mbq_dequeue(&kring->rx_queue);
97237e3a6d3SLuigi Rizzo 
97337e3a6d3SLuigi Rizzo 		while (mlen) {
97437e3a6d3SLuigi Rizzo 			copy = nm_buf_len;
97537e3a6d3SLuigi Rizzo 			if (mlen < copy) {
97637e3a6d3SLuigi Rizzo 				copy = mlen;
97737e3a6d3SLuigi Rizzo 			}
97837e3a6d3SLuigi Rizzo 			mlen -= copy;
97937e3a6d3SLuigi Rizzo 			avail -= nm_buf_len;
98037e3a6d3SLuigi Rizzo 
98137e3a6d3SLuigi Rizzo 			ring->slot[nm_i].len = copy;
9824f80b14cSVincenzo Maffione 			ring->slot[nm_i].flags = (mlen ? NS_MOREFRAG : 0);
98337e3a6d3SLuigi Rizzo 			nm_i = nm_next(nm_i, lim);
98437e3a6d3SLuigi Rizzo 		}
98537e3a6d3SLuigi Rizzo 
98637e3a6d3SLuigi Rizzo 		mbq_enqueue(&tmpq, m);
98737e3a6d3SLuigi Rizzo 	}
98837e3a6d3SLuigi Rizzo 	mbq_unlock(&kring->rx_queue);
98937e3a6d3SLuigi Rizzo 
99037e3a6d3SLuigi Rizzo 	/* Second pass: Drain the temporary queue, going over the used RX slots,
99137e3a6d3SLuigi Rizzo 	 * and perform the copy out of the RX queue lock. */
99237e3a6d3SLuigi Rizzo 	nm_i = kring->nr_hwtail;
99337e3a6d3SLuigi Rizzo 
99437e3a6d3SLuigi Rizzo 	for (;;) {
99537e3a6d3SLuigi Rizzo 		void *nmaddr;
99637e3a6d3SLuigi Rizzo 		int ofs = 0;
99737e3a6d3SLuigi Rizzo 		int morefrag;
99837e3a6d3SLuigi Rizzo 
99937e3a6d3SLuigi Rizzo 		m = mbq_dequeue(&tmpq);
100037e3a6d3SLuigi Rizzo 		if (!m)	{
100137e3a6d3SLuigi Rizzo 			break;
100237e3a6d3SLuigi Rizzo 		}
100337e3a6d3SLuigi Rizzo 
100437e3a6d3SLuigi Rizzo 		do {
100537e3a6d3SLuigi Rizzo 			nmaddr = NMB(na, &ring->slot[nm_i]);
100637e3a6d3SLuigi Rizzo 			/* We only check the address here on generic rx rings. */
100737e3a6d3SLuigi Rizzo 			if (nmaddr == NETMAP_BUF_BASE(na)) { /* Bad buffer */
100837e3a6d3SLuigi Rizzo 				m_freem(m);
100937e3a6d3SLuigi Rizzo 				mbq_purge(&tmpq);
101037e3a6d3SLuigi Rizzo 				mbq_fini(&tmpq);
101137e3a6d3SLuigi Rizzo 				return netmap_ring_reinit(kring);
101237e3a6d3SLuigi Rizzo 			}
101337e3a6d3SLuigi Rizzo 
101437e3a6d3SLuigi Rizzo 			copy = ring->slot[nm_i].len;
101537e3a6d3SLuigi Rizzo 			m_copydata(m, ofs, copy, nmaddr);
101637e3a6d3SLuigi Rizzo 			ofs += copy;
101737e3a6d3SLuigi Rizzo 			morefrag = ring->slot[nm_i].flags & NS_MOREFRAG;
101837e3a6d3SLuigi Rizzo 			nm_i = nm_next(nm_i, lim);
101937e3a6d3SLuigi Rizzo 		} while (morefrag);
102037e3a6d3SLuigi Rizzo 
102137e3a6d3SLuigi Rizzo 		m_freem(m);
102237e3a6d3SLuigi Rizzo 	}
102337e3a6d3SLuigi Rizzo 
102437e3a6d3SLuigi Rizzo 	mbq_fini(&tmpq);
102537e3a6d3SLuigi Rizzo 
102637e3a6d3SLuigi Rizzo 	if (n) {
102737e3a6d3SLuigi Rizzo 		kring->nr_hwtail = nm_i;
102837e3a6d3SLuigi Rizzo 		IFRATE(rate_ctx.new.rxpkt += n);
102937e3a6d3SLuigi Rizzo 	}
103037e3a6d3SLuigi Rizzo 	kring->nr_kflags &= ~NKR_PENDINTR;
1031f9790aebSLuigi Rizzo 
1032f9790aebSLuigi Rizzo 	return 0;
1033f9790aebSLuigi Rizzo }
1034f9790aebSLuigi Rizzo 
1035f9790aebSLuigi Rizzo static void
1036f9790aebSLuigi Rizzo generic_netmap_dtor(struct netmap_adapter *na)
1037f9790aebSLuigi Rizzo {
1038f9790aebSLuigi Rizzo 	struct netmap_generic_adapter *gna = (struct netmap_generic_adapter*)na;
1039847bf383SLuigi Rizzo 	struct ifnet *ifp = netmap_generic_getifp(gna);
1040f9790aebSLuigi Rizzo 	struct netmap_adapter *prev_na = gna->prev;
1041f9790aebSLuigi Rizzo 
1042f9790aebSLuigi Rizzo 	if (prev_na != NULL) {
1043847bf383SLuigi Rizzo 		netmap_adapter_put(prev_na);
104437e3a6d3SLuigi Rizzo 		if (nm_iszombie(na)) {
1045847bf383SLuigi Rizzo 		        /*
1046847bf383SLuigi Rizzo 		         * The driver has been removed without releasing
1047847bf383SLuigi Rizzo 		         * the reference so we need to do it here.
1048847bf383SLuigi Rizzo 		         */
1049f9790aebSLuigi Rizzo 		        netmap_adapter_put(prev_na);
1050f9790aebSLuigi Rizzo 		}
1051c3e9b4dbSLuiz Otavio O Souza 		D("Native netmap adapter %p restored", prev_na);
1052847bf383SLuigi Rizzo 	}
1053*2a7db7a6SVincenzo Maffione 	NM_RESTORE_NA(ifp, prev_na);
105437e3a6d3SLuigi Rizzo 	/*
105537e3a6d3SLuigi Rizzo 	 * netmap_detach_common(), that it's called after this function,
105637e3a6d3SLuigi Rizzo 	 * overrides WNA(ifp) if na->ifp is not NULL.
105737e3a6d3SLuigi Rizzo 	 */
1058f9790aebSLuigi Rizzo 	na->ifp = NULL;
1059c3e9b4dbSLuiz Otavio O Souza 	D("Emulated netmap adapter for %s destroyed", na->name);
1060c3e9b4dbSLuiz Otavio O Souza }
1061c3e9b4dbSLuiz Otavio O Souza 
1062c3e9b4dbSLuiz Otavio O Souza int
1063c3e9b4dbSLuiz Otavio O Souza na_is_generic(struct netmap_adapter *na)
1064c3e9b4dbSLuiz Otavio O Souza {
1065c3e9b4dbSLuiz Otavio O Souza 	return na->nm_register == generic_netmap_register;
1066f9790aebSLuigi Rizzo }
1067f9790aebSLuigi Rizzo 
1068f9790aebSLuigi Rizzo /*
1069f9790aebSLuigi Rizzo  * generic_netmap_attach() makes it possible to use netmap on
1070f9790aebSLuigi Rizzo  * a device without native netmap support.
1071f9790aebSLuigi Rizzo  * This is less performant than native support but potentially
1072f9790aebSLuigi Rizzo  * faster than raw sockets or similar schemes.
1073f9790aebSLuigi Rizzo  *
1074f9790aebSLuigi Rizzo  * In this "emulated" mode, netmap rings do not necessarily
1075f9790aebSLuigi Rizzo  * have the same size as those in the NIC. We use a default
1076f9790aebSLuigi Rizzo  * value and possibly override it if the OS has ways to fetch the
1077f9790aebSLuigi Rizzo  * actual configuration.
1078f9790aebSLuigi Rizzo  */
1079f9790aebSLuigi Rizzo int
1080f9790aebSLuigi Rizzo generic_netmap_attach(struct ifnet *ifp)
1081f9790aebSLuigi Rizzo {
1082f9790aebSLuigi Rizzo 	struct netmap_adapter *na;
1083f9790aebSLuigi Rizzo 	struct netmap_generic_adapter *gna;
1084f9790aebSLuigi Rizzo 	int retval;
1085f9790aebSLuigi Rizzo 	u_int num_tx_desc, num_rx_desc;
1086f9790aebSLuigi Rizzo 
1087a02dbe4cSLuiz Otavio O Souza #ifdef __FreeBSD__
1088a02dbe4cSLuiz Otavio O Souza 	if (ifp->if_type == IFT_LOOP) {
1089a02dbe4cSLuiz Otavio O Souza 		D("if_loop is not supported by %s", __func__);
1090a02dbe4cSLuiz Otavio O Souza 		return EINVAL;
1091a02dbe4cSLuiz Otavio O Souza 	}
1092a02dbe4cSLuiz Otavio O Souza #endif
1093a02dbe4cSLuiz Otavio O Souza 
1094*2a7db7a6SVincenzo Maffione 	if (NM_NA_CLASH(ifp)) {
10954f80b14cSVincenzo Maffione 		/* If NA(ifp) is not null but there is no valid netmap
10964f80b14cSVincenzo Maffione 		 * adapter it means that someone else is using the same
10974f80b14cSVincenzo Maffione 		 * pointer (e.g. ax25_ptr on linux). This happens for
10984f80b14cSVincenzo Maffione 		 * instance when also PF_RING is in use. */
10994f80b14cSVincenzo Maffione 		D("Error: netmap adapter hook is busy");
11004f80b14cSVincenzo Maffione 		return EBUSY;
11014f80b14cSVincenzo Maffione 	}
11024f80b14cSVincenzo Maffione 
1103f9790aebSLuigi Rizzo 	num_tx_desc = num_rx_desc = netmap_generic_ringsize; /* starting point */
1104f9790aebSLuigi Rizzo 
110537e3a6d3SLuigi Rizzo 	nm_os_generic_find_num_desc(ifp, &num_tx_desc, &num_rx_desc); /* ignore errors */
1106f9790aebSLuigi Rizzo 	ND("Netmap ring size: TX = %d, RX = %d", num_tx_desc, num_rx_desc);
1107e4166283SLuigi Rizzo 	if (num_tx_desc == 0 || num_rx_desc == 0) {
1108e4166283SLuigi Rizzo 		D("Device has no hw slots (tx %u, rx %u)", num_tx_desc, num_rx_desc);
1109e4166283SLuigi Rizzo 		return EINVAL;
1110e4166283SLuigi Rizzo 	}
1111f9790aebSLuigi Rizzo 
1112c3e9b4dbSLuiz Otavio O Souza 	gna = nm_os_malloc(sizeof(*gna));
1113f9790aebSLuigi Rizzo 	if (gna == NULL) {
1114f9790aebSLuigi Rizzo 		D("no memory on attach, give up");
1115f9790aebSLuigi Rizzo 		return ENOMEM;
1116f9790aebSLuigi Rizzo 	}
1117f9790aebSLuigi Rizzo 	na = (struct netmap_adapter *)gna;
1118847bf383SLuigi Rizzo 	strncpy(na->name, ifp->if_xname, sizeof(na->name));
1119f9790aebSLuigi Rizzo 	na->ifp = ifp;
1120f9790aebSLuigi Rizzo 	na->num_tx_desc = num_tx_desc;
1121f9790aebSLuigi Rizzo 	na->num_rx_desc = num_rx_desc;
1122*2a7db7a6SVincenzo Maffione 	na->rx_buf_maxsize = 32768;
1123f9790aebSLuigi Rizzo 	na->nm_register = &generic_netmap_register;
1124f9790aebSLuigi Rizzo 	na->nm_txsync = &generic_netmap_txsync;
1125f9790aebSLuigi Rizzo 	na->nm_rxsync = &generic_netmap_rxsync;
1126f9790aebSLuigi Rizzo 	na->nm_dtor = &generic_netmap_dtor;
11274bf50f18SLuigi Rizzo 	/* when using generic, NAF_NETMAP_ON is set so we force
1128f9790aebSLuigi Rizzo 	 * NAF_SKIP_INTR to use the regular interrupt handler
1129f9790aebSLuigi Rizzo 	 */
1130f0ea3689SLuigi Rizzo 	na->na_flags = NAF_SKIP_INTR | NAF_HOST_RINGS;
1131f9790aebSLuigi Rizzo 
1132f9790aebSLuigi Rizzo 	ND("[GNA] num_tx_queues(%d), real_num_tx_queues(%d), len(%lu)",
1133f9790aebSLuigi Rizzo 			ifp->num_tx_queues, ifp->real_num_tx_queues,
1134f9790aebSLuigi Rizzo 			ifp->tx_queue_len);
1135f9790aebSLuigi Rizzo 	ND("[GNA] num_rx_queues(%d), real_num_rx_queues(%d)",
1136f9790aebSLuigi Rizzo 			ifp->num_rx_queues, ifp->real_num_rx_queues);
1137f9790aebSLuigi Rizzo 
113837e3a6d3SLuigi Rizzo 	nm_os_generic_find_num_queues(ifp, &na->num_tx_rings, &na->num_rx_rings);
1139f9790aebSLuigi Rizzo 
1140f9790aebSLuigi Rizzo 	retval = netmap_attach_common(na);
1141f9790aebSLuigi Rizzo 	if (retval) {
1142c3e9b4dbSLuiz Otavio O Souza 		nm_os_free(gna);
114337e3a6d3SLuigi Rizzo 		return retval;
1144f9790aebSLuigi Rizzo 	}
1145f9790aebSLuigi Rizzo 
1146*2a7db7a6SVincenzo Maffione 	if (NM_NA_VALID(ifp)) {
114737e3a6d3SLuigi Rizzo 		gna->prev = NA(ifp); /* save old na */
114837e3a6d3SLuigi Rizzo 		netmap_adapter_get(gna->prev);
114937e3a6d3SLuigi Rizzo 	}
115037e3a6d3SLuigi Rizzo 	NM_ATTACH_NA(ifp, na);
115137e3a6d3SLuigi Rizzo 
115237e3a6d3SLuigi Rizzo 	nm_os_generic_set_features(gna);
115337e3a6d3SLuigi Rizzo 
1154c3e9b4dbSLuiz Otavio O Souza 	D("Emulated adapter for %s created (prev was %p)", na->name, gna->prev);
115537e3a6d3SLuigi Rizzo 
1156f9790aebSLuigi Rizzo 	return retval;
1157f9790aebSLuigi Rizzo }
1158