xref: /freebsd/sys/dev/netmap/netmap.c (revision f0ea3689a9c1c27067145ed902811149e78cc4fa)
168b8534bSLuigi Rizzo /*
217885a7bSLuigi Rizzo  * Copyright (C) 2011-2014 Matteo Landi, Luigi Rizzo. All rights reserved.
368b8534bSLuigi Rizzo  *
468b8534bSLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
568b8534bSLuigi Rizzo  * modification, are permitted provided that the following conditions
668b8534bSLuigi Rizzo  * are met:
768b8534bSLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
868b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
968b8534bSLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
1068b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
1168b8534bSLuigi Rizzo  *      documentation and/or other materials provided with the distribution.
1268b8534bSLuigi Rizzo  *
1368b8534bSLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1468b8534bSLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1568b8534bSLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1668b8534bSLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1768b8534bSLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1868b8534bSLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1968b8534bSLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2068b8534bSLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2168b8534bSLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2268b8534bSLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2368b8534bSLuigi Rizzo  * SUCH DAMAGE.
2468b8534bSLuigi Rizzo  */
2568b8534bSLuigi Rizzo 
26ce3ee1e7SLuigi Rizzo 
2768b8534bSLuigi Rizzo /*
28f9790aebSLuigi Rizzo  * $FreeBSD$
29f9790aebSLuigi Rizzo  *
3068b8534bSLuigi Rizzo  * This module supports memory mapped access to network devices,
3168b8534bSLuigi Rizzo  * see netmap(4).
3268b8534bSLuigi Rizzo  *
3368b8534bSLuigi Rizzo  * The module uses a large, memory pool allocated by the kernel
3468b8534bSLuigi Rizzo  * and accessible as mmapped memory by multiple userspace threads/processes.
3568b8534bSLuigi Rizzo  * The memory pool contains packet buffers and "netmap rings",
3668b8534bSLuigi Rizzo  * i.e. user-accessible copies of the interface's queues.
3768b8534bSLuigi Rizzo  *
3868b8534bSLuigi Rizzo  * Access to the network card works like this:
3968b8534bSLuigi Rizzo  * 1. a process/thread issues one or more open() on /dev/netmap, to create
4068b8534bSLuigi Rizzo  *    select()able file descriptor on which events are reported.
4168b8534bSLuigi Rizzo  * 2. on each descriptor, the process issues an ioctl() to identify
4268b8534bSLuigi Rizzo  *    the interface that should report events to the file descriptor.
4368b8534bSLuigi Rizzo  * 3. on each descriptor, the process issues an mmap() request to
4468b8534bSLuigi Rizzo  *    map the shared memory region within the process' address space.
4568b8534bSLuigi Rizzo  *    The list of interesting queues is indicated by a location in
4668b8534bSLuigi Rizzo  *    the shared memory region.
4768b8534bSLuigi Rizzo  * 4. using the functions in the netmap(4) userspace API, a process
4868b8534bSLuigi Rizzo  *    can look up the occupation state of a queue, access memory buffers,
4968b8534bSLuigi Rizzo  *    and retrieve received packets or enqueue packets to transmit.
5068b8534bSLuigi Rizzo  * 5. using some ioctl()s the process can synchronize the userspace view
5168b8534bSLuigi Rizzo  *    of the queue with the actual status in the kernel. This includes both
5268b8534bSLuigi Rizzo  *    receiving the notification of new packets, and transmitting new
5368b8534bSLuigi Rizzo  *    packets on the output interface.
5468b8534bSLuigi Rizzo  * 6. select() or poll() can be used to wait for events on individual
5568b8534bSLuigi Rizzo  *    transmit or receive queues (or all queues for a given interface).
56ce3ee1e7SLuigi Rizzo  *
57ce3ee1e7SLuigi Rizzo 
58ce3ee1e7SLuigi Rizzo 		SYNCHRONIZATION (USER)
59ce3ee1e7SLuigi Rizzo 
60ce3ee1e7SLuigi Rizzo The netmap rings and data structures may be shared among multiple
61ce3ee1e7SLuigi Rizzo user threads or even independent processes.
62ce3ee1e7SLuigi Rizzo Any synchronization among those threads/processes is delegated
63ce3ee1e7SLuigi Rizzo to the threads themselves. Only one thread at a time can be in
64ce3ee1e7SLuigi Rizzo a system call on the same netmap ring. The OS does not enforce
65ce3ee1e7SLuigi Rizzo this and only guarantees against system crashes in case of
66ce3ee1e7SLuigi Rizzo invalid usage.
67ce3ee1e7SLuigi Rizzo 
68ce3ee1e7SLuigi Rizzo 		LOCKING (INTERNAL)
69ce3ee1e7SLuigi Rizzo 
70ce3ee1e7SLuigi Rizzo Within the kernel, access to the netmap rings is protected as follows:
71ce3ee1e7SLuigi Rizzo 
72ce3ee1e7SLuigi Rizzo - a spinlock on each ring, to handle producer/consumer races on
73ce3ee1e7SLuigi Rizzo   RX rings attached to the host stack (against multiple host
74ce3ee1e7SLuigi Rizzo   threads writing from the host stack to the same ring),
75ce3ee1e7SLuigi Rizzo   and on 'destination' rings attached to a VALE switch
76ce3ee1e7SLuigi Rizzo   (i.e. RX rings in VALE ports, and TX rings in NIC/host ports)
77ce3ee1e7SLuigi Rizzo   protecting multiple active senders for the same destination)
78ce3ee1e7SLuigi Rizzo 
79ce3ee1e7SLuigi Rizzo - an atomic variable to guarantee that there is at most one
80ce3ee1e7SLuigi Rizzo   instance of *_*xsync() on the ring at any time.
81ce3ee1e7SLuigi Rizzo   For rings connected to user file
82ce3ee1e7SLuigi Rizzo   descriptors, an atomic_test_and_set() protects this, and the
83ce3ee1e7SLuigi Rizzo   lock on the ring is not actually used.
84ce3ee1e7SLuigi Rizzo   For NIC RX rings connected to a VALE switch, an atomic_test_and_set()
85ce3ee1e7SLuigi Rizzo   is also used to prevent multiple executions (the driver might indeed
86ce3ee1e7SLuigi Rizzo   already guarantee this).
87ce3ee1e7SLuigi Rizzo   For NIC TX rings connected to a VALE switch, the lock arbitrates
88ce3ee1e7SLuigi Rizzo   access to the queue (both when allocating buffers and when pushing
89ce3ee1e7SLuigi Rizzo   them out).
90ce3ee1e7SLuigi Rizzo 
91ce3ee1e7SLuigi Rizzo - *xsync() should be protected against initializations of the card.
92ce3ee1e7SLuigi Rizzo   On FreeBSD most devices have the reset routine protected by
93ce3ee1e7SLuigi Rizzo   a RING lock (ixgbe, igb, em) or core lock (re). lem is missing
94ce3ee1e7SLuigi Rizzo   the RING protection on rx_reset(), this should be added.
95ce3ee1e7SLuigi Rizzo 
96ce3ee1e7SLuigi Rizzo   On linux there is an external lock on the tx path, which probably
97ce3ee1e7SLuigi Rizzo   also arbitrates access to the reset routine. XXX to be revised
98ce3ee1e7SLuigi Rizzo 
99ce3ee1e7SLuigi Rizzo - a per-interface core_lock protecting access from the host stack
100ce3ee1e7SLuigi Rizzo   while interfaces may be detached from netmap mode.
101ce3ee1e7SLuigi Rizzo   XXX there should be no need for this lock if we detach the interfaces
102ce3ee1e7SLuigi Rizzo   only while they are down.
103ce3ee1e7SLuigi Rizzo 
104ce3ee1e7SLuigi Rizzo 
105ce3ee1e7SLuigi Rizzo --- VALE SWITCH ---
106ce3ee1e7SLuigi Rizzo 
107ce3ee1e7SLuigi Rizzo NMG_LOCK() serializes all modifications to switches and ports.
108ce3ee1e7SLuigi Rizzo A switch cannot be deleted until all ports are gone.
109ce3ee1e7SLuigi Rizzo 
110ce3ee1e7SLuigi Rizzo For each switch, an SX lock (RWlock on linux) protects
111ce3ee1e7SLuigi Rizzo deletion of ports. When configuring or deleting a new port, the
112ce3ee1e7SLuigi Rizzo lock is acquired in exclusive mode (after holding NMG_LOCK).
113ce3ee1e7SLuigi Rizzo When forwarding, the lock is acquired in shared mode (without NMG_LOCK).
114ce3ee1e7SLuigi Rizzo The lock is held throughout the entire forwarding cycle,
115ce3ee1e7SLuigi Rizzo during which the thread may incur in a page fault.
116ce3ee1e7SLuigi Rizzo Hence it is important that sleepable shared locks are used.
117ce3ee1e7SLuigi Rizzo 
118ce3ee1e7SLuigi Rizzo On the rx ring, the per-port lock is grabbed initially to reserve
119ce3ee1e7SLuigi Rizzo a number of slot in the ring, then the lock is released,
120ce3ee1e7SLuigi Rizzo packets are copied from source to destination, and then
121ce3ee1e7SLuigi Rizzo the lock is acquired again and the receive ring is updated.
122ce3ee1e7SLuigi Rizzo (A similar thing is done on the tx ring for NIC and host stack
123ce3ee1e7SLuigi Rizzo ports attached to the switch)
124ce3ee1e7SLuigi Rizzo 
12568b8534bSLuigi Rizzo  */
12668b8534bSLuigi Rizzo 
127ce3ee1e7SLuigi Rizzo /*
128ce3ee1e7SLuigi Rizzo  * OS-specific code that is used only within this file.
129ce3ee1e7SLuigi Rizzo  * Other OS-specific code that must be accessed by drivers
130ce3ee1e7SLuigi Rizzo  * is present in netmap_kern.h
131ce3ee1e7SLuigi Rizzo  */
13201c7d25fSLuigi Rizzo 
133ce3ee1e7SLuigi Rizzo #if defined(__FreeBSD__)
13468b8534bSLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */
13568b8534bSLuigi Rizzo #include <sys/types.h>
13668b8534bSLuigi Rizzo #include <sys/errno.h>
13768b8534bSLuigi Rizzo #include <sys/param.h>	/* defines used in kernel.h */
13868b8534bSLuigi Rizzo #include <sys/kernel.h>	/* types used in module initialization */
139f9790aebSLuigi Rizzo #include <sys/conf.h>	/* cdevsw struct, UID, GID */
14068b8534bSLuigi Rizzo #include <sys/sockio.h>
14168b8534bSLuigi Rizzo #include <sys/socketvar.h>	/* struct socket */
14268b8534bSLuigi Rizzo #include <sys/malloc.h>
14368b8534bSLuigi Rizzo #include <sys/poll.h>
14489f6b863SAttilio Rao #include <sys/rwlock.h>
14568b8534bSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
14668b8534bSLuigi Rizzo #include <sys/selinfo.h>
14768b8534bSLuigi Rizzo #include <sys/sysctl.h>
148339f59c0SGleb Smirnoff #include <sys/jail.h>
149339f59c0SGleb Smirnoff #include <net/vnet.h>
15068b8534bSLuigi Rizzo #include <net/if.h>
15176039bc8SGleb Smirnoff #include <net/if_var.h>
15268b8534bSLuigi Rizzo #include <net/bpf.h>		/* BIOCIMMEDIATE */
15368b8534bSLuigi Rizzo #include <machine/bus.h>	/* bus_dmamap_* */
154ce3ee1e7SLuigi Rizzo #include <sys/endian.h>
155ce3ee1e7SLuigi Rizzo #include <sys/refcount.h>
15668b8534bSLuigi Rizzo 
15768b8534bSLuigi Rizzo 
158f9790aebSLuigi Rizzo /* reduce conditional code */
159*f0ea3689SLuigi Rizzo // linux API, use for the knlist in FreeBSD
160*f0ea3689SLuigi Rizzo #define init_waitqueue_head(x)	knlist_init_mtx(&(x)->si_note, NULL)
161ce3ee1e7SLuigi Rizzo 
162*f0ea3689SLuigi Rizzo void freebsd_selwakeup(struct selinfo *si, int pri);
163*f0ea3689SLuigi Rizzo #define OS_selwakeup(a, b)	freebsd_selwakeup(a, b)
164ce3ee1e7SLuigi Rizzo 
165ce3ee1e7SLuigi Rizzo #elif defined(linux)
166ce3ee1e7SLuigi Rizzo 
167ce3ee1e7SLuigi Rizzo #include "bsd_glue.h"
168ce3ee1e7SLuigi Rizzo 
169ce3ee1e7SLuigi Rizzo 
170ce3ee1e7SLuigi Rizzo 
171ce3ee1e7SLuigi Rizzo #elif defined(__APPLE__)
172ce3ee1e7SLuigi Rizzo 
173ce3ee1e7SLuigi Rizzo #warning OSX support is only partial
174ce3ee1e7SLuigi Rizzo #include "osx_glue.h"
175ce3ee1e7SLuigi Rizzo 
176ce3ee1e7SLuigi Rizzo #else
177ce3ee1e7SLuigi Rizzo 
178ce3ee1e7SLuigi Rizzo #error	Unsupported platform
179ce3ee1e7SLuigi Rizzo 
180ce3ee1e7SLuigi Rizzo #endif /* unsupported */
181ce3ee1e7SLuigi Rizzo 
182ce3ee1e7SLuigi Rizzo /*
183ce3ee1e7SLuigi Rizzo  * common headers
184ce3ee1e7SLuigi Rizzo  */
1850b8ed8e0SLuigi Rizzo #include <net/netmap.h>
1860b8ed8e0SLuigi Rizzo #include <dev/netmap/netmap_kern.h>
187ce3ee1e7SLuigi Rizzo #include <dev/netmap/netmap_mem2.h>
1880b8ed8e0SLuigi Rizzo 
189ce3ee1e7SLuigi Rizzo 
190ce3ee1e7SLuigi Rizzo MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map");
191ce3ee1e7SLuigi Rizzo 
192ce3ee1e7SLuigi Rizzo /*
193ce3ee1e7SLuigi Rizzo  * The following variables are used by the drivers and replicate
194ce3ee1e7SLuigi Rizzo  * fields in the global memory pool. They only refer to buffers
195ce3ee1e7SLuigi Rizzo  * used by physical interfaces.
196ce3ee1e7SLuigi Rizzo  */
1975819da83SLuigi Rizzo u_int netmap_total_buffers;
1988241616dSLuigi Rizzo u_int netmap_buf_size;
199ce3ee1e7SLuigi Rizzo char *netmap_buffer_base;	/* also address of an invalid buffer */
2005819da83SLuigi Rizzo 
2015819da83SLuigi Rizzo /* user-controlled variables */
2025819da83SLuigi Rizzo int netmap_verbose;
2035819da83SLuigi Rizzo 
2045819da83SLuigi Rizzo static int netmap_no_timestamp; /* don't timestamp on rxsync */
2055819da83SLuigi Rizzo 
2065819da83SLuigi Rizzo SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args");
2075819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose,
2085819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode");
2095819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp,
2105819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp");
2115819da83SLuigi Rizzo int netmap_mitigate = 1;
2125819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mitigate, CTLFLAG_RW, &netmap_mitigate, 0, "");
213c85cb1a0SLuigi Rizzo int netmap_no_pendintr = 1;
2145819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr,
2155819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_pendintr, 0, "Always look for new received packets.");
216f18be576SLuigi Rizzo int netmap_txsync_retry = 2;
217f18be576SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, txsync_retry, CTLFLAG_RW,
218f18be576SLuigi Rizzo     &netmap_txsync_retry, 0 , "Number of txsync loops in bridge's flush.");
2195819da83SLuigi Rizzo 
220f196ce38SLuigi Rizzo int netmap_flags = 0;	/* debug flags */
221091fd0abSLuigi Rizzo int netmap_fwd = 0;	/* force transparent mode */
222ce3ee1e7SLuigi Rizzo int netmap_mmap_unreg = 0; /* allow mmap of unregistered fds */
223f196ce38SLuigi Rizzo 
224f9790aebSLuigi Rizzo /*
225f9790aebSLuigi Rizzo  * netmap_admode selects the netmap mode to use.
226f9790aebSLuigi Rizzo  * Invalid values are reset to NETMAP_ADMODE_BEST
227f9790aebSLuigi Rizzo  */
228f9790aebSLuigi Rizzo enum { NETMAP_ADMODE_BEST = 0,	/* use native, fallback to generic */
229f9790aebSLuigi Rizzo 	NETMAP_ADMODE_NATIVE,	/* either native or none */
230f9790aebSLuigi Rizzo 	NETMAP_ADMODE_GENERIC,	/* force generic */
231f9790aebSLuigi Rizzo 	NETMAP_ADMODE_LAST };
232f9790aebSLuigi Rizzo static int netmap_admode = NETMAP_ADMODE_BEST;
233f9790aebSLuigi Rizzo 
234f9790aebSLuigi Rizzo int netmap_generic_mit = 100*1000;   /* Generic mitigation interval in nanoseconds. */
235f9790aebSLuigi Rizzo int netmap_generic_ringsize = 1024;   /* Generic ringsize. */
236*f0ea3689SLuigi Rizzo int netmap_generic_rings = 1;   /* number of queues in generic. */
237f9790aebSLuigi Rizzo 
238f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, flags, CTLFLAG_RW, &netmap_flags, 0 , "");
239091fd0abSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, fwd, CTLFLAG_RW, &netmap_fwd, 0 , "");
240ce3ee1e7SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mmap_unreg, CTLFLAG_RW, &netmap_mmap_unreg, 0, "");
241f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, admode, CTLFLAG_RW, &netmap_admode, 0 , "");
242f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_mit, CTLFLAG_RW, &netmap_generic_mit, 0 , "");
243f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_ringsize, CTLFLAG_RW, &netmap_generic_ringsize, 0 , "");
244*f0ea3689SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_rings, CTLFLAG_RW, &netmap_generic_rings, 0 , "");
245f196ce38SLuigi Rizzo 
246ce3ee1e7SLuigi Rizzo NMG_LOCK_T	netmap_global_lock;
247ce3ee1e7SLuigi Rizzo 
248ce3ee1e7SLuigi Rizzo 
249f9790aebSLuigi Rizzo static void
250f9790aebSLuigi Rizzo nm_kr_get(struct netmap_kring *kr)
251ce3ee1e7SLuigi Rizzo {
252ce3ee1e7SLuigi Rizzo 	while (NM_ATOMIC_TEST_AND_SET(&kr->nr_busy))
253ce3ee1e7SLuigi Rizzo 		tsleep(kr, 0, "NM_KR_GET", 4);
254ce3ee1e7SLuigi Rizzo }
255ce3ee1e7SLuigi Rizzo 
256f9790aebSLuigi Rizzo 
25717885a7bSLuigi Rizzo /*
25817885a7bSLuigi Rizzo  * mark the ring as stopped, and run through the locks
25917885a7bSLuigi Rizzo  * to make sure other users get to see it.
26017885a7bSLuigi Rizzo  */
261f9790aebSLuigi Rizzo void
262f9790aebSLuigi Rizzo netmap_disable_ring(struct netmap_kring *kr)
263ce3ee1e7SLuigi Rizzo {
264ce3ee1e7SLuigi Rizzo 	kr->nkr_stopped = 1;
265ce3ee1e7SLuigi Rizzo 	nm_kr_get(kr);
266ce3ee1e7SLuigi Rizzo 	mtx_lock(&kr->q_lock);
267ce3ee1e7SLuigi Rizzo 	mtx_unlock(&kr->q_lock);
268ce3ee1e7SLuigi Rizzo 	nm_kr_put(kr);
269ce3ee1e7SLuigi Rizzo }
270ce3ee1e7SLuigi Rizzo 
271f9790aebSLuigi Rizzo 
272f9790aebSLuigi Rizzo static void
273f9790aebSLuigi Rizzo netmap_set_all_rings(struct ifnet *ifp, int stopped)
274ce3ee1e7SLuigi Rizzo {
275ce3ee1e7SLuigi Rizzo 	struct netmap_adapter *na;
276ce3ee1e7SLuigi Rizzo 	int i;
277*f0ea3689SLuigi Rizzo 	u_int ntx, nrx;
278ce3ee1e7SLuigi Rizzo 
279ce3ee1e7SLuigi Rizzo 	if (!(ifp->if_capenable & IFCAP_NETMAP))
280ce3ee1e7SLuigi Rizzo 		return;
281ce3ee1e7SLuigi Rizzo 
282ce3ee1e7SLuigi Rizzo 	na = NA(ifp);
283ce3ee1e7SLuigi Rizzo 
284*f0ea3689SLuigi Rizzo 	ntx = netmap_real_tx_rings(na);
285*f0ea3689SLuigi Rizzo 	nrx = netmap_real_rx_rings(na);
286*f0ea3689SLuigi Rizzo 
287*f0ea3689SLuigi Rizzo 	for (i = 0; i < ntx; i++) {
288f9790aebSLuigi Rizzo 		if (stopped)
289f9790aebSLuigi Rizzo 			netmap_disable_ring(na->tx_rings + i);
290f9790aebSLuigi Rizzo 		else
291ce3ee1e7SLuigi Rizzo 			na->tx_rings[i].nkr_stopped = 0;
292*f0ea3689SLuigi Rizzo 		na->nm_notify(na, i, NR_TX, NAF_DISABLE_NOTIFY);
293ce3ee1e7SLuigi Rizzo 	}
294f9790aebSLuigi Rizzo 
295*f0ea3689SLuigi Rizzo 	for (i = 0; i < nrx; i++) {
296f9790aebSLuigi Rizzo 		if (stopped)
297f9790aebSLuigi Rizzo 			netmap_disable_ring(na->rx_rings + i);
298f9790aebSLuigi Rizzo 		else
299ce3ee1e7SLuigi Rizzo 			na->rx_rings[i].nkr_stopped = 0;
300*f0ea3689SLuigi Rizzo 		na->nm_notify(na, i, NR_RX, NAF_DISABLE_NOTIFY);
301ce3ee1e7SLuigi Rizzo 	}
302ce3ee1e7SLuigi Rizzo }
303ce3ee1e7SLuigi Rizzo 
304ce3ee1e7SLuigi Rizzo 
305f9790aebSLuigi Rizzo void
306f9790aebSLuigi Rizzo netmap_disable_all_rings(struct ifnet *ifp)
307f9790aebSLuigi Rizzo {
308f9790aebSLuigi Rizzo 	netmap_set_all_rings(ifp, 1 /* stopped */);
309f9790aebSLuigi Rizzo }
310f9790aebSLuigi Rizzo 
311f9790aebSLuigi Rizzo 
312f9790aebSLuigi Rizzo void
313f9790aebSLuigi Rizzo netmap_enable_all_rings(struct ifnet *ifp)
314f9790aebSLuigi Rizzo {
315f9790aebSLuigi Rizzo 	netmap_set_all_rings(ifp, 0 /* enabled */);
316f9790aebSLuigi Rizzo }
317f9790aebSLuigi Rizzo 
318f9790aebSLuigi Rizzo 
319ce3ee1e7SLuigi Rizzo /*
320ce3ee1e7SLuigi Rizzo  * generic bound_checking function
321ce3ee1e7SLuigi Rizzo  */
322ce3ee1e7SLuigi Rizzo u_int
323ce3ee1e7SLuigi Rizzo nm_bound_var(u_int *v, u_int dflt, u_int lo, u_int hi, const char *msg)
324ce3ee1e7SLuigi Rizzo {
325ce3ee1e7SLuigi Rizzo 	u_int oldv = *v;
326ce3ee1e7SLuigi Rizzo 	const char *op = NULL;
327ce3ee1e7SLuigi Rizzo 
328ce3ee1e7SLuigi Rizzo 	if (dflt < lo)
329ce3ee1e7SLuigi Rizzo 		dflt = lo;
330ce3ee1e7SLuigi Rizzo 	if (dflt > hi)
331ce3ee1e7SLuigi Rizzo 		dflt = hi;
332ce3ee1e7SLuigi Rizzo 	if (oldv < lo) {
333ce3ee1e7SLuigi Rizzo 		*v = dflt;
334ce3ee1e7SLuigi Rizzo 		op = "Bump";
335ce3ee1e7SLuigi Rizzo 	} else if (oldv > hi) {
336ce3ee1e7SLuigi Rizzo 		*v = hi;
337ce3ee1e7SLuigi Rizzo 		op = "Clamp";
338ce3ee1e7SLuigi Rizzo 	}
339ce3ee1e7SLuigi Rizzo 	if (op && msg)
340ce3ee1e7SLuigi Rizzo 		printf("%s %s to %d (was %d)\n", op, msg, *v, oldv);
341ce3ee1e7SLuigi Rizzo 	return *v;
342ce3ee1e7SLuigi Rizzo }
343ce3ee1e7SLuigi Rizzo 
344f9790aebSLuigi Rizzo 
345ce3ee1e7SLuigi Rizzo /*
346ce3ee1e7SLuigi Rizzo  * packet-dump function, user-supplied or static buffer.
347ce3ee1e7SLuigi Rizzo  * The destination buffer must be at least 30+4*len
348ce3ee1e7SLuigi Rizzo  */
349ce3ee1e7SLuigi Rizzo const char *
350ce3ee1e7SLuigi Rizzo nm_dump_buf(char *p, int len, int lim, char *dst)
351ce3ee1e7SLuigi Rizzo {
352ce3ee1e7SLuigi Rizzo 	static char _dst[8192];
353ce3ee1e7SLuigi Rizzo 	int i, j, i0;
354ce3ee1e7SLuigi Rizzo 	static char hex[] ="0123456789abcdef";
355ce3ee1e7SLuigi Rizzo 	char *o;	/* output position */
356ce3ee1e7SLuigi Rizzo 
357ce3ee1e7SLuigi Rizzo #define P_HI(x)	hex[((x) & 0xf0)>>4]
358ce3ee1e7SLuigi Rizzo #define P_LO(x)	hex[((x) & 0xf)]
359ce3ee1e7SLuigi Rizzo #define P_C(x)	((x) >= 0x20 && (x) <= 0x7e ? (x) : '.')
360ce3ee1e7SLuigi Rizzo 	if (!dst)
361ce3ee1e7SLuigi Rizzo 		dst = _dst;
362ce3ee1e7SLuigi Rizzo 	if (lim <= 0 || lim > len)
363ce3ee1e7SLuigi Rizzo 		lim = len;
364ce3ee1e7SLuigi Rizzo 	o = dst;
365ce3ee1e7SLuigi Rizzo 	sprintf(o, "buf 0x%p len %d lim %d\n", p, len, lim);
366ce3ee1e7SLuigi Rizzo 	o += strlen(o);
367ce3ee1e7SLuigi Rizzo 	/* hexdump routine */
368ce3ee1e7SLuigi Rizzo 	for (i = 0; i < lim; ) {
369ce3ee1e7SLuigi Rizzo 		sprintf(o, "%5d: ", i);
370ce3ee1e7SLuigi Rizzo 		o += strlen(o);
371ce3ee1e7SLuigi Rizzo 		memset(o, ' ', 48);
372ce3ee1e7SLuigi Rizzo 		i0 = i;
373ce3ee1e7SLuigi Rizzo 		for (j=0; j < 16 && i < lim; i++, j++) {
374ce3ee1e7SLuigi Rizzo 			o[j*3] = P_HI(p[i]);
375ce3ee1e7SLuigi Rizzo 			o[j*3+1] = P_LO(p[i]);
376ce3ee1e7SLuigi Rizzo 		}
377ce3ee1e7SLuigi Rizzo 		i = i0;
378ce3ee1e7SLuigi Rizzo 		for (j=0; j < 16 && i < lim; i++, j++)
379ce3ee1e7SLuigi Rizzo 			o[j + 48] = P_C(p[i]);
380ce3ee1e7SLuigi Rizzo 		o[j+48] = '\n';
381ce3ee1e7SLuigi Rizzo 		o += j+49;
382ce3ee1e7SLuigi Rizzo 	}
383ce3ee1e7SLuigi Rizzo 	*o = '\0';
384ce3ee1e7SLuigi Rizzo #undef P_HI
385ce3ee1e7SLuigi Rizzo #undef P_LO
386ce3ee1e7SLuigi Rizzo #undef P_C
387ce3ee1e7SLuigi Rizzo 	return dst;
388ce3ee1e7SLuigi Rizzo }
389f196ce38SLuigi Rizzo 
390f18be576SLuigi Rizzo 
391ae10d1afSLuigi Rizzo /*
392ae10d1afSLuigi Rizzo  * Fetch configuration from the device, to cope with dynamic
393ae10d1afSLuigi Rizzo  * reconfigurations after loading the module.
394ae10d1afSLuigi Rizzo  */
395f9790aebSLuigi Rizzo int
396ae10d1afSLuigi Rizzo netmap_update_config(struct netmap_adapter *na)
397ae10d1afSLuigi Rizzo {
398ae10d1afSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
399ae10d1afSLuigi Rizzo 	u_int txr, txd, rxr, rxd;
400ae10d1afSLuigi Rizzo 
401ae10d1afSLuigi Rizzo 	txr = txd = rxr = rxd = 0;
402ae10d1afSLuigi Rizzo 	if (na->nm_config) {
403f9790aebSLuigi Rizzo 		na->nm_config(na, &txr, &txd, &rxr, &rxd);
404ae10d1afSLuigi Rizzo 	} else {
405ae10d1afSLuigi Rizzo 		/* take whatever we had at init time */
406ae10d1afSLuigi Rizzo 		txr = na->num_tx_rings;
407ae10d1afSLuigi Rizzo 		txd = na->num_tx_desc;
408ae10d1afSLuigi Rizzo 		rxr = na->num_rx_rings;
409ae10d1afSLuigi Rizzo 		rxd = na->num_rx_desc;
410ae10d1afSLuigi Rizzo 	}
411ae10d1afSLuigi Rizzo 
412ae10d1afSLuigi Rizzo 	if (na->num_tx_rings == txr && na->num_tx_desc == txd &&
413ae10d1afSLuigi Rizzo 	    na->num_rx_rings == rxr && na->num_rx_desc == rxd)
414ae10d1afSLuigi Rizzo 		return 0; /* nothing changed */
415f9790aebSLuigi Rizzo 	if (netmap_verbose || na->active_fds > 0) {
416ae10d1afSLuigi Rizzo 		D("stored config %s: txring %d x %d, rxring %d x %d",
417f9790aebSLuigi Rizzo 			NM_IFPNAME(ifp),
418ae10d1afSLuigi Rizzo 			na->num_tx_rings, na->num_tx_desc,
419ae10d1afSLuigi Rizzo 			na->num_rx_rings, na->num_rx_desc);
420ae10d1afSLuigi Rizzo 		D("new config %s: txring %d x %d, rxring %d x %d",
421f9790aebSLuigi Rizzo 			NM_IFPNAME(ifp), txr, txd, rxr, rxd);
422ae10d1afSLuigi Rizzo 	}
423f9790aebSLuigi Rizzo 	if (na->active_fds == 0) {
424ae10d1afSLuigi Rizzo 		D("configuration changed (but fine)");
425ae10d1afSLuigi Rizzo 		na->num_tx_rings = txr;
426ae10d1afSLuigi Rizzo 		na->num_tx_desc = txd;
427ae10d1afSLuigi Rizzo 		na->num_rx_rings = rxr;
428ae10d1afSLuigi Rizzo 		na->num_rx_desc = rxd;
429ae10d1afSLuigi Rizzo 		return 0;
430ae10d1afSLuigi Rizzo 	}
431ae10d1afSLuigi Rizzo 	D("configuration changed while active, this is bad...");
432ae10d1afSLuigi Rizzo 	return 1;
433ae10d1afSLuigi Rizzo }
434ae10d1afSLuigi Rizzo 
435*f0ea3689SLuigi Rizzo static int
436*f0ea3689SLuigi Rizzo netmap_txsync_compat(struct netmap_kring *kring, int flags)
437*f0ea3689SLuigi Rizzo {
438*f0ea3689SLuigi Rizzo 	struct netmap_adapter *na = kring->na;
439*f0ea3689SLuigi Rizzo 	return na->nm_txsync(na, kring->ring_id, flags);
440*f0ea3689SLuigi Rizzo }
441f9790aebSLuigi Rizzo 
442*f0ea3689SLuigi Rizzo static int
443*f0ea3689SLuigi Rizzo netmap_rxsync_compat(struct netmap_kring *kring, int flags)
444*f0ea3689SLuigi Rizzo {
445*f0ea3689SLuigi Rizzo 	struct netmap_adapter *na = kring->na;
446*f0ea3689SLuigi Rizzo 	return na->nm_rxsync(na, kring->ring_id, flags);
447*f0ea3689SLuigi Rizzo }
448*f0ea3689SLuigi Rizzo 
449*f0ea3689SLuigi Rizzo static int
450*f0ea3689SLuigi Rizzo netmap_txsync_to_host_compat(struct netmap_kring *kring, int flags)
451*f0ea3689SLuigi Rizzo {
452*f0ea3689SLuigi Rizzo 	(void)flags;
453*f0ea3689SLuigi Rizzo 	netmap_txsync_to_host(kring->na);
454*f0ea3689SLuigi Rizzo 	return 0;
455*f0ea3689SLuigi Rizzo }
456*f0ea3689SLuigi Rizzo 
457*f0ea3689SLuigi Rizzo static int
458*f0ea3689SLuigi Rizzo netmap_rxsync_from_host_compat(struct netmap_kring *kring, int flags)
459*f0ea3689SLuigi Rizzo {
460*f0ea3689SLuigi Rizzo 	(void)flags;
461*f0ea3689SLuigi Rizzo 	netmap_rxsync_from_host(kring->na, NULL, NULL);
462*f0ea3689SLuigi Rizzo 	return 0;
463*f0ea3689SLuigi Rizzo }
464*f0ea3689SLuigi Rizzo 
465*f0ea3689SLuigi Rizzo 
466*f0ea3689SLuigi Rizzo 
467*f0ea3689SLuigi Rizzo /* create the krings array and initialize the fields common to all adapters.
468*f0ea3689SLuigi Rizzo  * The array layout is this:
469*f0ea3689SLuigi Rizzo  *
470*f0ea3689SLuigi Rizzo  *                    +----------+
471*f0ea3689SLuigi Rizzo  * na->tx_rings ----->|          | \
472*f0ea3689SLuigi Rizzo  *                    |          |  } na->num_tx_ring
473*f0ea3689SLuigi Rizzo  *                    |          | /
474*f0ea3689SLuigi Rizzo  *                    +----------+
475*f0ea3689SLuigi Rizzo  *                    |          |    host tx kring
476*f0ea3689SLuigi Rizzo  * na->rx_rings ----> +----------+
477*f0ea3689SLuigi Rizzo  *                    |          | \
478*f0ea3689SLuigi Rizzo  *                    |          |  } na->num_rx_rings
479*f0ea3689SLuigi Rizzo  *                    |          | /
480*f0ea3689SLuigi Rizzo  *                    +----------+
481*f0ea3689SLuigi Rizzo  *                    |          |    host rx kring
482*f0ea3689SLuigi Rizzo  *                    +----------+
483*f0ea3689SLuigi Rizzo  * na->tailroom ----->|          | \
484*f0ea3689SLuigi Rizzo  *                    |          |  } tailroom bytes
485*f0ea3689SLuigi Rizzo  *                    |          | /
486*f0ea3689SLuigi Rizzo  *                    +----------+
487*f0ea3689SLuigi Rizzo  *
488*f0ea3689SLuigi Rizzo  * Note: for compatibility, host krings are created even when not needed.
489*f0ea3689SLuigi Rizzo  * The tailroom space is currently used by vale ports for allocating leases.
490*f0ea3689SLuigi Rizzo  */
491f9790aebSLuigi Rizzo int
492*f0ea3689SLuigi Rizzo netmap_krings_create(struct netmap_adapter *na, u_int tailroom)
493f9790aebSLuigi Rizzo {
494f9790aebSLuigi Rizzo 	u_int i, len, ndesc;
495f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
496*f0ea3689SLuigi Rizzo 	u_int ntx, nrx;
497f9790aebSLuigi Rizzo 
498*f0ea3689SLuigi Rizzo 	/* account for the (possibly fake) host rings */
499*f0ea3689SLuigi Rizzo 	ntx = na->num_tx_rings + 1;
500*f0ea3689SLuigi Rizzo 	nrx = na->num_rx_rings + 1;
501*f0ea3689SLuigi Rizzo 
502f9790aebSLuigi Rizzo 	len = (ntx + nrx) * sizeof(struct netmap_kring) + tailroom;
503f9790aebSLuigi Rizzo 
504f9790aebSLuigi Rizzo 	na->tx_rings = malloc((size_t)len, M_DEVBUF, M_NOWAIT | M_ZERO);
505f9790aebSLuigi Rizzo 	if (na->tx_rings == NULL) {
506f9790aebSLuigi Rizzo 		D("Cannot allocate krings");
507f9790aebSLuigi Rizzo 		return ENOMEM;
508f9790aebSLuigi Rizzo 	}
509f9790aebSLuigi Rizzo 	na->rx_rings = na->tx_rings + ntx;
510f9790aebSLuigi Rizzo 
51117885a7bSLuigi Rizzo 	/*
51217885a7bSLuigi Rizzo 	 * All fields in krings are 0 except the one initialized below.
51317885a7bSLuigi Rizzo 	 * but better be explicit on important kring fields.
51417885a7bSLuigi Rizzo 	 */
515f9790aebSLuigi Rizzo 	ndesc = na->num_tx_desc;
516f9790aebSLuigi Rizzo 	for (i = 0; i < ntx; i++) { /* Transmit rings */
517f9790aebSLuigi Rizzo 		kring = &na->tx_rings[i];
518f9790aebSLuigi Rizzo 		bzero(kring, sizeof(*kring));
519f9790aebSLuigi Rizzo 		kring->na = na;
52017885a7bSLuigi Rizzo 		kring->ring_id = i;
521f9790aebSLuigi Rizzo 		kring->nkr_num_slots = ndesc;
522*f0ea3689SLuigi Rizzo 		if (i < na->num_tx_rings) {
523*f0ea3689SLuigi Rizzo 			kring->nm_sync = netmap_txsync_compat; // XXX
524*f0ea3689SLuigi Rizzo 		} else if (i == na->num_tx_rings) {
525*f0ea3689SLuigi Rizzo 			kring->nm_sync = netmap_txsync_to_host_compat;
526*f0ea3689SLuigi Rizzo 		}
527f9790aebSLuigi Rizzo 		/*
52817885a7bSLuigi Rizzo 		 * IMPORTANT: Always keep one slot empty.
529f9790aebSLuigi Rizzo 		 */
53017885a7bSLuigi Rizzo 		kring->rhead = kring->rcur = kring->nr_hwcur = 0;
53117885a7bSLuigi Rizzo 		kring->rtail = kring->nr_hwtail = ndesc - 1;
53217885a7bSLuigi Rizzo 		snprintf(kring->name, sizeof(kring->name) - 1, "%s TX%d", NM_IFPNAME(na->ifp), i);
533*f0ea3689SLuigi Rizzo 		ND("ktx %s h %d c %d t %d",
534*f0ea3689SLuigi Rizzo 			kring->name, kring->rhead, kring->rcur, kring->rtail);
535f9790aebSLuigi Rizzo 		mtx_init(&kring->q_lock, "nm_txq_lock", NULL, MTX_DEF);
536f9790aebSLuigi Rizzo 		init_waitqueue_head(&kring->si);
537f9790aebSLuigi Rizzo 	}
538f9790aebSLuigi Rizzo 
539f9790aebSLuigi Rizzo 	ndesc = na->num_rx_desc;
540f9790aebSLuigi Rizzo 	for (i = 0; i < nrx; i++) { /* Receive rings */
541f9790aebSLuigi Rizzo 		kring = &na->rx_rings[i];
542f9790aebSLuigi Rizzo 		bzero(kring, sizeof(*kring));
543f9790aebSLuigi Rizzo 		kring->na = na;
54417885a7bSLuigi Rizzo 		kring->ring_id = i;
545f9790aebSLuigi Rizzo 		kring->nkr_num_slots = ndesc;
546*f0ea3689SLuigi Rizzo 		if (i < na->num_rx_rings) {
547*f0ea3689SLuigi Rizzo 			kring->nm_sync = netmap_rxsync_compat; // XXX
548*f0ea3689SLuigi Rizzo 		} else if (i == na->num_rx_rings) {
549*f0ea3689SLuigi Rizzo 			kring->nm_sync = netmap_rxsync_from_host_compat;
550*f0ea3689SLuigi Rizzo 		}
55117885a7bSLuigi Rizzo 		kring->rhead = kring->rcur = kring->nr_hwcur = 0;
55217885a7bSLuigi Rizzo 		kring->rtail = kring->nr_hwtail = 0;
55317885a7bSLuigi Rizzo 		snprintf(kring->name, sizeof(kring->name) - 1, "%s RX%d", NM_IFPNAME(na->ifp), i);
554*f0ea3689SLuigi Rizzo 		ND("krx %s h %d c %d t %d",
555*f0ea3689SLuigi Rizzo 			kring->name, kring->rhead, kring->rcur, kring->rtail);
556f9790aebSLuigi Rizzo 		mtx_init(&kring->q_lock, "nm_rxq_lock", NULL, MTX_DEF);
557f9790aebSLuigi Rizzo 		init_waitqueue_head(&kring->si);
558f9790aebSLuigi Rizzo 	}
559f9790aebSLuigi Rizzo 	init_waitqueue_head(&na->tx_si);
560f9790aebSLuigi Rizzo 	init_waitqueue_head(&na->rx_si);
561f9790aebSLuigi Rizzo 
562f9790aebSLuigi Rizzo 	na->tailroom = na->rx_rings + nrx;
563f9790aebSLuigi Rizzo 
564f9790aebSLuigi Rizzo 	return 0;
565f9790aebSLuigi Rizzo }
566f9790aebSLuigi Rizzo 
567f9790aebSLuigi Rizzo 
568*f0ea3689SLuigi Rizzo /* undo the actions performed by netmap_krings_create */
569f9790aebSLuigi Rizzo void
570f9790aebSLuigi Rizzo netmap_krings_delete(struct netmap_adapter *na)
571f9790aebSLuigi Rizzo {
572*f0ea3689SLuigi Rizzo 	struct netmap_kring *kring = na->tx_rings;
573f9790aebSLuigi Rizzo 
574*f0ea3689SLuigi Rizzo 	/* we rely on the krings layout described above */
575*f0ea3689SLuigi Rizzo 	for ( ; kring != na->tailroom; kring++) {
576*f0ea3689SLuigi Rizzo 		mtx_destroy(&kring->q_lock);
577f9790aebSLuigi Rizzo 	}
578f9790aebSLuigi Rizzo 	free(na->tx_rings, M_DEVBUF);
579f9790aebSLuigi Rizzo 	na->tx_rings = na->rx_rings = na->tailroom = NULL;
580f9790aebSLuigi Rizzo }
581f9790aebSLuigi Rizzo 
582f9790aebSLuigi Rizzo 
58317885a7bSLuigi Rizzo /*
58417885a7bSLuigi Rizzo  * Destructor for NIC ports. They also have an mbuf queue
58517885a7bSLuigi Rizzo  * on the rings connected to the host so we need to purge
58617885a7bSLuigi Rizzo  * them first.
58717885a7bSLuigi Rizzo  */
58817885a7bSLuigi Rizzo static void
58917885a7bSLuigi Rizzo netmap_hw_krings_delete(struct netmap_adapter *na)
59017885a7bSLuigi Rizzo {
59117885a7bSLuigi Rizzo 	struct mbq *q = &na->rx_rings[na->num_rx_rings].rx_queue;
59217885a7bSLuigi Rizzo 
59317885a7bSLuigi Rizzo 	ND("destroy sw mbq with len %d", mbq_len(q));
59417885a7bSLuigi Rizzo 	mbq_purge(q);
59517885a7bSLuigi Rizzo 	mbq_safe_destroy(q);
59617885a7bSLuigi Rizzo 	netmap_krings_delete(na);
59717885a7bSLuigi Rizzo }
59817885a7bSLuigi Rizzo 
59917885a7bSLuigi Rizzo 
600ce3ee1e7SLuigi Rizzo static struct netmap_if*
601ce3ee1e7SLuigi Rizzo netmap_if_new(const char *ifname, struct netmap_adapter *na)
602ce3ee1e7SLuigi Rizzo {
603f9790aebSLuigi Rizzo 	struct netmap_if *nifp;
604f9790aebSLuigi Rizzo 
605ce3ee1e7SLuigi Rizzo 	if (netmap_update_config(na)) {
606ce3ee1e7SLuigi Rizzo 		/* configuration mismatch, report and fail */
607ce3ee1e7SLuigi Rizzo 		return NULL;
608ce3ee1e7SLuigi Rizzo 	}
609f9790aebSLuigi Rizzo 
610f9790aebSLuigi Rizzo 	if (na->active_fds)
611f9790aebSLuigi Rizzo 		goto final;
612f9790aebSLuigi Rizzo 
613f9790aebSLuigi Rizzo 	if (na->nm_krings_create(na))
614f9790aebSLuigi Rizzo 		goto cleanup;
615f9790aebSLuigi Rizzo 
616f9790aebSLuigi Rizzo 	if (netmap_mem_rings_create(na))
617f9790aebSLuigi Rizzo 		goto cleanup;
618f9790aebSLuigi Rizzo 
619f9790aebSLuigi Rizzo final:
620f9790aebSLuigi Rizzo 
621f9790aebSLuigi Rizzo 	nifp = netmap_mem_if_new(ifname, na);
622f9790aebSLuigi Rizzo 	if (nifp == NULL)
623f9790aebSLuigi Rizzo 		goto cleanup;
624f9790aebSLuigi Rizzo 
625f9790aebSLuigi Rizzo 	return (nifp);
626f9790aebSLuigi Rizzo 
627f9790aebSLuigi Rizzo cleanup:
628f9790aebSLuigi Rizzo 
629f9790aebSLuigi Rizzo 	if (na->active_fds == 0) {
630f9790aebSLuigi Rizzo 		netmap_mem_rings_delete(na);
631f9790aebSLuigi Rizzo 		na->nm_krings_delete(na);
632ce3ee1e7SLuigi Rizzo 	}
63368b8534bSLuigi Rizzo 
634f9790aebSLuigi Rizzo 	return NULL;
635f9790aebSLuigi Rizzo }
6368241616dSLuigi Rizzo 
63768b8534bSLuigi Rizzo 
638ce3ee1e7SLuigi Rizzo /* grab a reference to the memory allocator, if we don't have one already.  The
639ce3ee1e7SLuigi Rizzo  * reference is taken from the netmap_adapter registered with the priv.
640ce3ee1e7SLuigi Rizzo  *
641ce3ee1e7SLuigi Rizzo  */
642ce3ee1e7SLuigi Rizzo static int
643ce3ee1e7SLuigi Rizzo netmap_get_memory_locked(struct netmap_priv_d* p)
644ce3ee1e7SLuigi Rizzo {
645ce3ee1e7SLuigi Rizzo 	struct netmap_mem_d *nmd;
646ce3ee1e7SLuigi Rizzo 	int error = 0;
647ce3ee1e7SLuigi Rizzo 
648f9790aebSLuigi Rizzo 	if (p->np_na == NULL) {
649ce3ee1e7SLuigi Rizzo 		if (!netmap_mmap_unreg)
650ce3ee1e7SLuigi Rizzo 			return ENODEV;
651ce3ee1e7SLuigi Rizzo 		/* for compatibility with older versions of the API
652ce3ee1e7SLuigi Rizzo  		 * we use the global allocator when no interface has been
653ce3ee1e7SLuigi Rizzo  		 * registered
654ce3ee1e7SLuigi Rizzo  		 */
655ce3ee1e7SLuigi Rizzo 		nmd = &nm_mem;
656ce3ee1e7SLuigi Rizzo 	} else {
657f9790aebSLuigi Rizzo 		nmd = p->np_na->nm_mem;
658ce3ee1e7SLuigi Rizzo 	}
659ce3ee1e7SLuigi Rizzo 	if (p->np_mref == NULL) {
660ce3ee1e7SLuigi Rizzo 		error = netmap_mem_finalize(nmd);
661ce3ee1e7SLuigi Rizzo 		if (!error)
662ce3ee1e7SLuigi Rizzo 			p->np_mref = nmd;
663ce3ee1e7SLuigi Rizzo 	} else if (p->np_mref != nmd) {
664ce3ee1e7SLuigi Rizzo 		/* a virtual port has been registered, but previous
665ce3ee1e7SLuigi Rizzo  		 * syscalls already used the global allocator.
666ce3ee1e7SLuigi Rizzo  		 * We cannot continue
667ce3ee1e7SLuigi Rizzo  		 */
668ce3ee1e7SLuigi Rizzo 		error = ENODEV;
669ce3ee1e7SLuigi Rizzo 	}
670ce3ee1e7SLuigi Rizzo 	return error;
671ce3ee1e7SLuigi Rizzo }
67268b8534bSLuigi Rizzo 
673f9790aebSLuigi Rizzo 
674f9790aebSLuigi Rizzo int
6758241616dSLuigi Rizzo netmap_get_memory(struct netmap_priv_d* p)
6768241616dSLuigi Rizzo {
677ce3ee1e7SLuigi Rizzo 	int error;
678ce3ee1e7SLuigi Rizzo 	NMG_LOCK();
679ce3ee1e7SLuigi Rizzo 	error = netmap_get_memory_locked(p);
680ce3ee1e7SLuigi Rizzo 	NMG_UNLOCK();
6818241616dSLuigi Rizzo 	return error;
6828241616dSLuigi Rizzo }
6838241616dSLuigi Rizzo 
684f9790aebSLuigi Rizzo 
685ce3ee1e7SLuigi Rizzo static int
686ce3ee1e7SLuigi Rizzo netmap_have_memory_locked(struct netmap_priv_d* p)
687ce3ee1e7SLuigi Rizzo {
688ce3ee1e7SLuigi Rizzo 	return p->np_mref != NULL;
689ce3ee1e7SLuigi Rizzo }
690ce3ee1e7SLuigi Rizzo 
691f9790aebSLuigi Rizzo 
692ce3ee1e7SLuigi Rizzo static void
693ce3ee1e7SLuigi Rizzo netmap_drop_memory_locked(struct netmap_priv_d* p)
694ce3ee1e7SLuigi Rizzo {
695ce3ee1e7SLuigi Rizzo 	if (p->np_mref) {
696ce3ee1e7SLuigi Rizzo 		netmap_mem_deref(p->np_mref);
697ce3ee1e7SLuigi Rizzo 		p->np_mref = NULL;
698ce3ee1e7SLuigi Rizzo 	}
699ce3ee1e7SLuigi Rizzo }
700ce3ee1e7SLuigi Rizzo 
701f9790aebSLuigi Rizzo 
70268b8534bSLuigi Rizzo /*
70368b8534bSLuigi Rizzo  * File descriptor's private data destructor.
70468b8534bSLuigi Rizzo  *
70568b8534bSLuigi Rizzo  * Call nm_register(ifp,0) to stop netmap mode on the interface and
706f9790aebSLuigi Rizzo  * revert to normal operation. We expect that np_na->ifp has not gone.
707ce3ee1e7SLuigi Rizzo  * The second argument is the nifp to work on. In some cases it is
708ce3ee1e7SLuigi Rizzo  * not attached yet to the netmap_priv_d so we need to pass it as
709ce3ee1e7SLuigi Rizzo  * a separate argument.
71068b8534bSLuigi Rizzo  */
711ce3ee1e7SLuigi Rizzo /* call with NMG_LOCK held */
71268b8534bSLuigi Rizzo static void
713ce3ee1e7SLuigi Rizzo netmap_do_unregif(struct netmap_priv_d *priv, struct netmap_if *nifp)
71468b8534bSLuigi Rizzo {
715f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
716f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
71768b8534bSLuigi Rizzo 
718ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
719f9790aebSLuigi Rizzo 	na->active_fds--;
720f9790aebSLuigi Rizzo 	if (na->active_fds <= 0) {	/* last instance */
72168b8534bSLuigi Rizzo 
722ae10d1afSLuigi Rizzo 		if (netmap_verbose)
723f9790aebSLuigi Rizzo 			D("deleting last instance for %s", NM_IFPNAME(ifp));
72468b8534bSLuigi Rizzo 		/*
725f18be576SLuigi Rizzo 		 * (TO CHECK) This function is only called
726f18be576SLuigi Rizzo 		 * when the last reference to this file descriptor goes
727f18be576SLuigi Rizzo 		 * away. This means we cannot have any pending poll()
728f18be576SLuigi Rizzo 		 * or interrupt routine operating on the structure.
729ce3ee1e7SLuigi Rizzo 		 * XXX The file may be closed in a thread while
730ce3ee1e7SLuigi Rizzo 		 * another thread is using it.
731ce3ee1e7SLuigi Rizzo 		 * Linux keeps the file opened until the last reference
732ce3ee1e7SLuigi Rizzo 		 * by any outstanding ioctl/poll or mmap is gone.
733ce3ee1e7SLuigi Rizzo 		 * FreeBSD does not track mmap()s (but we do) and
734ce3ee1e7SLuigi Rizzo 		 * wakes up any sleeping poll(). Need to check what
735ce3ee1e7SLuigi Rizzo 		 * happens if the close() occurs while a concurrent
736ce3ee1e7SLuigi Rizzo 		 * syscall is running.
73768b8534bSLuigi Rizzo 		 */
738f9790aebSLuigi Rizzo 		if (ifp)
739f9790aebSLuigi Rizzo 			na->nm_register(na, 0); /* off, clear flags */
74068b8534bSLuigi Rizzo 		/* Wake up any sleeping threads. netmap_poll will
74168b8534bSLuigi Rizzo 		 * then return POLLERR
742ce3ee1e7SLuigi Rizzo 		 * XXX The wake up now must happen during *_down(), when
743ce3ee1e7SLuigi Rizzo 		 * we order all activities to stop. -gl
74468b8534bSLuigi Rizzo 		 */
7452f70fca5SEd Maste 		/* XXX kqueue(9) needed; these will mirror knlist_init. */
7462f70fca5SEd Maste 		/* knlist_destroy(&na->tx_si.si_note); */
7472f70fca5SEd Maste 		/* knlist_destroy(&na->rx_si.si_note); */
748f9790aebSLuigi Rizzo 
749f9790aebSLuigi Rizzo 		/* delete rings and buffers */
750f9790aebSLuigi Rizzo 		netmap_mem_rings_delete(na);
751f9790aebSLuigi Rizzo 		na->nm_krings_delete(na);
75268b8534bSLuigi Rizzo 	}
753f9790aebSLuigi Rizzo 	/* delete the nifp */
754ce3ee1e7SLuigi Rizzo 	netmap_mem_if_delete(na, nifp);
7555819da83SLuigi Rizzo }
75668b8534bSLuigi Rizzo 
757*f0ea3689SLuigi Rizzo static __inline int
758*f0ea3689SLuigi Rizzo nm_tx_si_user(struct netmap_priv_d *priv)
759*f0ea3689SLuigi Rizzo {
760*f0ea3689SLuigi Rizzo 	return (priv->np_na != NULL &&
761*f0ea3689SLuigi Rizzo 		(priv->np_txqlast - priv->np_txqfirst > 1));
762*f0ea3689SLuigi Rizzo }
763*f0ea3689SLuigi Rizzo 
764*f0ea3689SLuigi Rizzo static __inline int
765*f0ea3689SLuigi Rizzo nm_rx_si_user(struct netmap_priv_d *priv)
766*f0ea3689SLuigi Rizzo {
767*f0ea3689SLuigi Rizzo 	return (priv->np_na != NULL &&
768*f0ea3689SLuigi Rizzo 		(priv->np_rxqlast - priv->np_rxqfirst > 1));
769*f0ea3689SLuigi Rizzo }
770*f0ea3689SLuigi Rizzo 
771f18be576SLuigi Rizzo 
772ce3ee1e7SLuigi Rizzo /*
773ce3ee1e7SLuigi Rizzo  * returns 1 if this is the last instance and we can free priv
774ce3ee1e7SLuigi Rizzo  */
775f9790aebSLuigi Rizzo int
776ce3ee1e7SLuigi Rizzo netmap_dtor_locked(struct netmap_priv_d *priv)
777ce3ee1e7SLuigi Rizzo {
778f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
779ce3ee1e7SLuigi Rizzo 
780ce3ee1e7SLuigi Rizzo #ifdef __FreeBSD__
781ce3ee1e7SLuigi Rizzo 	/*
782ce3ee1e7SLuigi Rizzo 	 * np_refcount is the number of active mmaps on
783ce3ee1e7SLuigi Rizzo 	 * this file descriptor
784ce3ee1e7SLuigi Rizzo 	 */
785ce3ee1e7SLuigi Rizzo 	if (--priv->np_refcount > 0) {
786ce3ee1e7SLuigi Rizzo 		return 0;
787ce3ee1e7SLuigi Rizzo 	}
788ce3ee1e7SLuigi Rizzo #endif /* __FreeBSD__ */
789f9790aebSLuigi Rizzo 	if (!na) {
790f9790aebSLuigi Rizzo 	    return 1; //XXX is it correct?
791ce3ee1e7SLuigi Rizzo 	}
792f9790aebSLuigi Rizzo 	netmap_do_unregif(priv, priv->np_nifp);
793f9790aebSLuigi Rizzo 	priv->np_nifp = NULL;
794ce3ee1e7SLuigi Rizzo 	netmap_drop_memory_locked(priv);
795f9790aebSLuigi Rizzo 	if (priv->np_na) {
796*f0ea3689SLuigi Rizzo 		if (nm_tx_si_user(priv))
797*f0ea3689SLuigi Rizzo 			na->tx_si_users--;
798*f0ea3689SLuigi Rizzo 		if (nm_rx_si_user(priv))
799*f0ea3689SLuigi Rizzo 			na->rx_si_users--;
800f9790aebSLuigi Rizzo 		netmap_adapter_put(na);
801f9790aebSLuigi Rizzo 		priv->np_na = NULL;
802ce3ee1e7SLuigi Rizzo 	}
803ce3ee1e7SLuigi Rizzo 	return 1;
804f196ce38SLuigi Rizzo }
8055819da83SLuigi Rizzo 
806f9790aebSLuigi Rizzo 
807f9790aebSLuigi Rizzo void
8085819da83SLuigi Rizzo netmap_dtor(void *data)
8095819da83SLuigi Rizzo {
8105819da83SLuigi Rizzo 	struct netmap_priv_d *priv = data;
811ce3ee1e7SLuigi Rizzo 	int last_instance;
8125819da83SLuigi Rizzo 
813ce3ee1e7SLuigi Rizzo 	NMG_LOCK();
814ce3ee1e7SLuigi Rizzo 	last_instance = netmap_dtor_locked(priv);
815ce3ee1e7SLuigi Rizzo 	NMG_UNLOCK();
816ce3ee1e7SLuigi Rizzo 	if (last_instance) {
817ce3ee1e7SLuigi Rizzo 		bzero(priv, sizeof(*priv));	/* for safety */
81868b8534bSLuigi Rizzo 		free(priv, M_DEVBUF);
81968b8534bSLuigi Rizzo 	}
820ce3ee1e7SLuigi Rizzo }
82168b8534bSLuigi Rizzo 
822f18be576SLuigi Rizzo 
82368b8534bSLuigi Rizzo 
82468b8534bSLuigi Rizzo 
82568b8534bSLuigi Rizzo /*
82602ad4083SLuigi Rizzo  * Handlers for synchronization of the queues from/to the host.
827091fd0abSLuigi Rizzo  * Netmap has two operating modes:
828091fd0abSLuigi Rizzo  * - in the default mode, the rings connected to the host stack are
829091fd0abSLuigi Rizzo  *   just another ring pair managed by userspace;
830091fd0abSLuigi Rizzo  * - in transparent mode (XXX to be defined) incoming packets
831091fd0abSLuigi Rizzo  *   (from the host or the NIC) are marked as NS_FORWARD upon
832091fd0abSLuigi Rizzo  *   arrival, and the user application has a chance to reset the
833091fd0abSLuigi Rizzo  *   flag for packets that should be dropped.
834091fd0abSLuigi Rizzo  *   On the RXSYNC or poll(), packets in RX rings between
835091fd0abSLuigi Rizzo  *   kring->nr_kcur and ring->cur with NS_FORWARD still set are moved
836091fd0abSLuigi Rizzo  *   to the other side.
837091fd0abSLuigi Rizzo  * The transfer NIC --> host is relatively easy, just encapsulate
838091fd0abSLuigi Rizzo  * into mbufs and we are done. The host --> NIC side is slightly
839091fd0abSLuigi Rizzo  * harder because there might not be room in the tx ring so it
840091fd0abSLuigi Rizzo  * might take a while before releasing the buffer.
841091fd0abSLuigi Rizzo  */
842091fd0abSLuigi Rizzo 
843f18be576SLuigi Rizzo 
844091fd0abSLuigi Rizzo /*
845091fd0abSLuigi Rizzo  * pass a chain of buffers to the host stack as coming from 'dst'
84617885a7bSLuigi Rizzo  * We do not need to lock because the queue is private.
847091fd0abSLuigi Rizzo  */
848091fd0abSLuigi Rizzo static void
849f9790aebSLuigi Rizzo netmap_send_up(struct ifnet *dst, struct mbq *q)
850091fd0abSLuigi Rizzo {
851091fd0abSLuigi Rizzo 	struct mbuf *m;
852091fd0abSLuigi Rizzo 
853091fd0abSLuigi Rizzo 	/* send packets up, outside the lock */
854f9790aebSLuigi Rizzo 	while ((m = mbq_dequeue(q)) != NULL) {
855091fd0abSLuigi Rizzo 		if (netmap_verbose & NM_VERB_HOST)
856091fd0abSLuigi Rizzo 			D("sending up pkt %p size %d", m, MBUF_LEN(m));
857091fd0abSLuigi Rizzo 		NM_SEND_UP(dst, m);
858091fd0abSLuigi Rizzo 	}
859f9790aebSLuigi Rizzo 	mbq_destroy(q);
860091fd0abSLuigi Rizzo }
861091fd0abSLuigi Rizzo 
862f18be576SLuigi Rizzo 
863091fd0abSLuigi Rizzo /*
864091fd0abSLuigi Rizzo  * put a copy of the buffers marked NS_FORWARD into an mbuf chain.
86517885a7bSLuigi Rizzo  * Take packets from hwcur to ring->head marked NS_FORWARD (or forced)
86617885a7bSLuigi Rizzo  * and pass them up. Drop remaining packets in the unlikely event
86717885a7bSLuigi Rizzo  * of an mbuf shortage.
868091fd0abSLuigi Rizzo  */
869091fd0abSLuigi Rizzo static void
870091fd0abSLuigi Rizzo netmap_grab_packets(struct netmap_kring *kring, struct mbq *q, int force)
871091fd0abSLuigi Rizzo {
87217885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
87317885a7bSLuigi Rizzo 	u_int const head = kring->ring->head;
87417885a7bSLuigi Rizzo 	u_int n;
875f9790aebSLuigi Rizzo 	struct netmap_adapter *na = kring->na;
876091fd0abSLuigi Rizzo 
87717885a7bSLuigi Rizzo 	for (n = kring->nr_hwcur; n != head; n = nm_next(n, lim)) {
87817885a7bSLuigi Rizzo 		struct mbuf *m;
879091fd0abSLuigi Rizzo 		struct netmap_slot *slot = &kring->ring->slot[n];
880091fd0abSLuigi Rizzo 
881091fd0abSLuigi Rizzo 		if ((slot->flags & NS_FORWARD) == 0 && !force)
882091fd0abSLuigi Rizzo 			continue;
883f9790aebSLuigi Rizzo 		if (slot->len < 14 || slot->len > NETMAP_BDG_BUF_SIZE(na->nm_mem)) {
88417885a7bSLuigi Rizzo 			RD(5, "bad pkt at %d len %d", n, slot->len);
885091fd0abSLuigi Rizzo 			continue;
886091fd0abSLuigi Rizzo 		}
887091fd0abSLuigi Rizzo 		slot->flags &= ~NS_FORWARD; // XXX needed ?
88817885a7bSLuigi Rizzo 		/* XXX TODO: adapt to the case of a multisegment packet */
889f9790aebSLuigi Rizzo 		m = m_devget(BDG_NMB(na, slot), slot->len, 0, na->ifp, NULL);
890091fd0abSLuigi Rizzo 
891091fd0abSLuigi Rizzo 		if (m == NULL)
892091fd0abSLuigi Rizzo 			break;
893f9790aebSLuigi Rizzo 		mbq_enqueue(q, m);
894091fd0abSLuigi Rizzo 	}
895091fd0abSLuigi Rizzo }
896091fd0abSLuigi Rizzo 
897f18be576SLuigi Rizzo 
898091fd0abSLuigi Rizzo /*
89917885a7bSLuigi Rizzo  * Send to the NIC rings packets marked NS_FORWARD between
90017885a7bSLuigi Rizzo  * kring->nr_hwcur and kring->rhead
90117885a7bSLuigi Rizzo  * Called under kring->rx_queue.lock on the sw rx ring,
902091fd0abSLuigi Rizzo  */
90317885a7bSLuigi Rizzo static u_int
904091fd0abSLuigi Rizzo netmap_sw_to_nic(struct netmap_adapter *na)
905091fd0abSLuigi Rizzo {
906091fd0abSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
90717885a7bSLuigi Rizzo 	struct netmap_slot *rxslot = kring->ring->slot;
90817885a7bSLuigi Rizzo 	u_int i, rxcur = kring->nr_hwcur;
90917885a7bSLuigi Rizzo 	u_int const head = kring->rhead;
91017885a7bSLuigi Rizzo 	u_int const src_lim = kring->nkr_num_slots - 1;
91117885a7bSLuigi Rizzo 	u_int sent = 0;
912ce3ee1e7SLuigi Rizzo 
91317885a7bSLuigi Rizzo 	/* scan rings to find space, then fill as much as possible */
91417885a7bSLuigi Rizzo 	for (i = 0; i < na->num_tx_rings; i++) {
91517885a7bSLuigi Rizzo 		struct netmap_kring *kdst = &na->tx_rings[i];
91617885a7bSLuigi Rizzo 		struct netmap_ring *rdst = kdst->ring;
91717885a7bSLuigi Rizzo 		u_int const dst_lim = kdst->nkr_num_slots - 1;
918ce3ee1e7SLuigi Rizzo 
91917885a7bSLuigi Rizzo 		/* XXX do we trust ring or kring->rcur,rtail ? */
92017885a7bSLuigi Rizzo 		for (; rxcur != head && !nm_ring_empty(rdst);
92117885a7bSLuigi Rizzo 		     rxcur = nm_next(rxcur, src_lim) ) {
922091fd0abSLuigi Rizzo 			struct netmap_slot *src, *dst, tmp;
92317885a7bSLuigi Rizzo 			u_int dst_cur = rdst->cur;
92417885a7bSLuigi Rizzo 
92517885a7bSLuigi Rizzo 			src = &rxslot[rxcur];
92617885a7bSLuigi Rizzo 			if ((src->flags & NS_FORWARD) == 0 && !netmap_fwd)
92717885a7bSLuigi Rizzo 				continue;
92817885a7bSLuigi Rizzo 
92917885a7bSLuigi Rizzo 			sent++;
93017885a7bSLuigi Rizzo 
93117885a7bSLuigi Rizzo 			dst = &rdst->slot[dst_cur];
93217885a7bSLuigi Rizzo 
933091fd0abSLuigi Rizzo 			tmp = *src;
93417885a7bSLuigi Rizzo 
935091fd0abSLuigi Rizzo 			src->buf_idx = dst->buf_idx;
936091fd0abSLuigi Rizzo 			src->flags = NS_BUF_CHANGED;
937091fd0abSLuigi Rizzo 
938091fd0abSLuigi Rizzo 			dst->buf_idx = tmp.buf_idx;
939091fd0abSLuigi Rizzo 			dst->len = tmp.len;
940091fd0abSLuigi Rizzo 			dst->flags = NS_BUF_CHANGED;
941091fd0abSLuigi Rizzo 
94217885a7bSLuigi Rizzo 			rdst->cur = nm_next(dst_cur, dst_lim);
943091fd0abSLuigi Rizzo 		}
94417885a7bSLuigi Rizzo 		/* if (sent) XXX txsync ? */
945091fd0abSLuigi Rizzo 	}
94617885a7bSLuigi Rizzo 	return sent;
947091fd0abSLuigi Rizzo }
948091fd0abSLuigi Rizzo 
949f18be576SLuigi Rizzo 
950091fd0abSLuigi Rizzo /*
951ce3ee1e7SLuigi Rizzo  * netmap_txsync_to_host() passes packets up. We are called from a
95202ad4083SLuigi Rizzo  * system call in user process context, and the only contention
95302ad4083SLuigi Rizzo  * can be among multiple user threads erroneously calling
954091fd0abSLuigi Rizzo  * this routine concurrently.
95568b8534bSLuigi Rizzo  */
956f9790aebSLuigi Rizzo void
957ce3ee1e7SLuigi Rizzo netmap_txsync_to_host(struct netmap_adapter *na)
95868b8534bSLuigi Rizzo {
959d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->tx_rings[na->num_tx_rings];
96068b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
96117885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
962*f0ea3689SLuigi Rizzo 	u_int const head = kring->rhead;
963f9790aebSLuigi Rizzo 	struct mbq q;
96468b8534bSLuigi Rizzo 
96517885a7bSLuigi Rizzo 	/* Take packets from hwcur to head and pass them up.
96617885a7bSLuigi Rizzo 	 * force head = cur since netmap_grab_packets() stops at head
96768b8534bSLuigi Rizzo 	 * In case of no buffers we give up. At the end of the loop,
96868b8534bSLuigi Rizzo 	 * the queue is drained in all cases.
96968b8534bSLuigi Rizzo 	 */
970f9790aebSLuigi Rizzo 	mbq_init(&q);
97117885a7bSLuigi Rizzo 	ring->cur = head;
97217885a7bSLuigi Rizzo 	netmap_grab_packets(kring, &q, 1 /* force */);
97317885a7bSLuigi Rizzo 	ND("have %d pkts in queue", mbq_len(&q));
97417885a7bSLuigi Rizzo 	kring->nr_hwcur = head;
97517885a7bSLuigi Rizzo 	kring->nr_hwtail = head + lim;
97617885a7bSLuigi Rizzo 	if (kring->nr_hwtail > lim)
97717885a7bSLuigi Rizzo 		kring->nr_hwtail -= lim + 1;
97817885a7bSLuigi Rizzo 	nm_txsync_finalize(kring);
97968b8534bSLuigi Rizzo 
980f9790aebSLuigi Rizzo 	netmap_send_up(na->ifp, &q);
981f18be576SLuigi Rizzo }
982f18be576SLuigi Rizzo 
983f18be576SLuigi Rizzo 
98468b8534bSLuigi Rizzo /*
98502ad4083SLuigi Rizzo  * rxsync backend for packets coming from the host stack.
98617885a7bSLuigi Rizzo  * They have been put in kring->rx_queue by netmap_transmit().
98717885a7bSLuigi Rizzo  * We protect access to the kring using kring->rx_queue.lock
98802ad4083SLuigi Rizzo  *
98968b8534bSLuigi Rizzo  * This routine also does the selrecord if called from the poll handler
99068b8534bSLuigi Rizzo  * (we know because td != NULL).
99101c7d25fSLuigi Rizzo  *
99201c7d25fSLuigi Rizzo  * NOTE: on linux, selrecord() is defined as a macro and uses pwait
99301c7d25fSLuigi Rizzo  *     as an additional hidden argument.
99417885a7bSLuigi Rizzo  * returns the number of packets delivered to tx queues in
99517885a7bSLuigi Rizzo  * transparent mode, or a negative value if error
99668b8534bSLuigi Rizzo  */
99717885a7bSLuigi Rizzo int
998ce3ee1e7SLuigi Rizzo netmap_rxsync_from_host(struct netmap_adapter *na, struct thread *td, void *pwait)
99968b8534bSLuigi Rizzo {
1000d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
100168b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
100217885a7bSLuigi Rizzo 	u_int nm_i, n;
100317885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
1004*f0ea3689SLuigi Rizzo 	u_int const head = kring->rhead;
100517885a7bSLuigi Rizzo 	int ret = 0;
100617885a7bSLuigi Rizzo 	struct mbq *q = &kring->rx_queue;
100768b8534bSLuigi Rizzo 
100801c7d25fSLuigi Rizzo 	(void)pwait;	/* disable unused warnings */
1009*f0ea3689SLuigi Rizzo 	(void)td;
101017885a7bSLuigi Rizzo 
101117885a7bSLuigi Rizzo 	mtx_lock(&q->lock);
101217885a7bSLuigi Rizzo 
101317885a7bSLuigi Rizzo 	/* First part: import newly received packets */
101417885a7bSLuigi Rizzo 	n = mbq_len(q);
101517885a7bSLuigi Rizzo 	if (n) { /* grab packets from the queue */
101617885a7bSLuigi Rizzo 		struct mbuf *m;
101717885a7bSLuigi Rizzo 		uint32_t stop_i;
101817885a7bSLuigi Rizzo 
101917885a7bSLuigi Rizzo 		nm_i = kring->nr_hwtail;
102017885a7bSLuigi Rizzo 		stop_i = nm_prev(nm_i, lim);
102117885a7bSLuigi Rizzo 		while ( nm_i != stop_i && (m = mbq_dequeue(q)) != NULL ) {
102217885a7bSLuigi Rizzo 			int len = MBUF_LEN(m);
102317885a7bSLuigi Rizzo 			struct netmap_slot *slot = &ring->slot[nm_i];
102417885a7bSLuigi Rizzo 
102517885a7bSLuigi Rizzo 			m_copydata(m, 0, len, BDG_NMB(na, slot));
102617885a7bSLuigi Rizzo 			ND("nm %d len %d", nm_i, len);
102717885a7bSLuigi Rizzo 			if (netmap_verbose)
102817885a7bSLuigi Rizzo                                 D("%s", nm_dump_buf(BDG_NMB(na, slot),len, 128, NULL));
102917885a7bSLuigi Rizzo 
103017885a7bSLuigi Rizzo 			slot->len = len;
103117885a7bSLuigi Rizzo 			slot->flags = kring->nkr_slot_flags;
103217885a7bSLuigi Rizzo 			nm_i = nm_next(nm_i, lim);
103364ae02c3SLuigi Rizzo 		}
103417885a7bSLuigi Rizzo 		kring->nr_hwtail = nm_i;
103564ae02c3SLuigi Rizzo 	}
103617885a7bSLuigi Rizzo 
103717885a7bSLuigi Rizzo 	/*
103817885a7bSLuigi Rizzo 	 * Second part: skip past packets that userspace has released.
103917885a7bSLuigi Rizzo 	 */
104017885a7bSLuigi Rizzo 	nm_i = kring->nr_hwcur;
104117885a7bSLuigi Rizzo 	if (nm_i != head) { /* something was released */
104217885a7bSLuigi Rizzo 		if (netmap_fwd || kring->ring->flags & NR_FORWARD)
104317885a7bSLuigi Rizzo 			ret = netmap_sw_to_nic(na);
104417885a7bSLuigi Rizzo 		kring->nr_hwcur = head;
104564ae02c3SLuigi Rizzo 	}
104617885a7bSLuigi Rizzo 
104717885a7bSLuigi Rizzo 	nm_rxsync_finalize(kring);
104817885a7bSLuigi Rizzo 
104917885a7bSLuigi Rizzo 	/* access copies of cur,tail in the kring */
105017885a7bSLuigi Rizzo 	if (kring->rcur == kring->rtail && td) /* no bufs available */
105168b8534bSLuigi Rizzo 		selrecord(td, &kring->si);
105217885a7bSLuigi Rizzo 
105317885a7bSLuigi Rizzo 	mtx_unlock(&q->lock);
105417885a7bSLuigi Rizzo 	return ret;
105568b8534bSLuigi Rizzo }
105668b8534bSLuigi Rizzo 
105768b8534bSLuigi Rizzo 
1058f9790aebSLuigi Rizzo /* Get a netmap adapter for the port.
1059f9790aebSLuigi Rizzo  *
1060f9790aebSLuigi Rizzo  * If it is possible to satisfy the request, return 0
1061f9790aebSLuigi Rizzo  * with *na containing the netmap adapter found.
1062f9790aebSLuigi Rizzo  * Otherwise return an error code, with *na containing NULL.
1063f9790aebSLuigi Rizzo  *
1064f9790aebSLuigi Rizzo  * When the port is attached to a bridge, we always return
1065f9790aebSLuigi Rizzo  * EBUSY.
1066f9790aebSLuigi Rizzo  * Otherwise, if the port is already bound to a file descriptor,
1067f9790aebSLuigi Rizzo  * then we unconditionally return the existing adapter into *na.
1068f9790aebSLuigi Rizzo  * In all the other cases, we return (into *na) either native,
1069f9790aebSLuigi Rizzo  * generic or NULL, according to the following table:
1070f9790aebSLuigi Rizzo  *
1071f9790aebSLuigi Rizzo  *					native_support
1072f9790aebSLuigi Rizzo  * active_fds   dev.netmap.admode         YES     NO
1073f9790aebSLuigi Rizzo  * -------------------------------------------------------
1074f9790aebSLuigi Rizzo  *    >0              *                 NA(ifp) NA(ifp)
1075f9790aebSLuigi Rizzo  *
1076f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_BEST      NATIVE  GENERIC
1077f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_NATIVE    NATIVE   NULL
1078f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_GENERIC   GENERIC GENERIC
1079f9790aebSLuigi Rizzo  *
1080f9790aebSLuigi Rizzo  */
1081f9790aebSLuigi Rizzo 
1082f9790aebSLuigi Rizzo int
1083f9790aebSLuigi Rizzo netmap_get_hw_na(struct ifnet *ifp, struct netmap_adapter **na)
1084f9790aebSLuigi Rizzo {
1085f9790aebSLuigi Rizzo 	/* generic support */
1086f9790aebSLuigi Rizzo 	int i = netmap_admode;	/* Take a snapshot. */
1087f9790aebSLuigi Rizzo 	int error = 0;
1088f9790aebSLuigi Rizzo 	struct netmap_adapter *prev_na;
1089f9790aebSLuigi Rizzo 	struct netmap_generic_adapter *gna;
1090f9790aebSLuigi Rizzo 
1091f9790aebSLuigi Rizzo 	*na = NULL; /* default */
1092f9790aebSLuigi Rizzo 
1093f9790aebSLuigi Rizzo 	/* reset in case of invalid value */
1094f9790aebSLuigi Rizzo 	if (i < NETMAP_ADMODE_BEST || i >= NETMAP_ADMODE_LAST)
1095f9790aebSLuigi Rizzo 		i = netmap_admode = NETMAP_ADMODE_BEST;
1096f9790aebSLuigi Rizzo 
1097f9790aebSLuigi Rizzo 	if (NETMAP_CAPABLE(ifp)) {
1098f9790aebSLuigi Rizzo 		/* If an adapter already exists, but is
1099f9790aebSLuigi Rizzo 		 * attached to a vale port, we report that the
1100f9790aebSLuigi Rizzo 		 * port is busy.
1101f9790aebSLuigi Rizzo 		 */
1102f9790aebSLuigi Rizzo 		if (NETMAP_OWNED_BY_KERN(NA(ifp)))
1103f9790aebSLuigi Rizzo 			return EBUSY;
1104f9790aebSLuigi Rizzo 
1105f9790aebSLuigi Rizzo 		/* If an adapter already exists, return it if
1106f9790aebSLuigi Rizzo 		 * there are active file descriptors or if
1107f9790aebSLuigi Rizzo 		 * netmap is not forced to use generic
1108f9790aebSLuigi Rizzo 		 * adapters.
1109f9790aebSLuigi Rizzo 		 */
1110f9790aebSLuigi Rizzo 		if (NA(ifp)->active_fds > 0 ||
1111f9790aebSLuigi Rizzo 				i != NETMAP_ADMODE_GENERIC) {
1112f9790aebSLuigi Rizzo 			*na = NA(ifp);
1113f9790aebSLuigi Rizzo 			return 0;
1114f9790aebSLuigi Rizzo 		}
1115f9790aebSLuigi Rizzo 	}
1116f9790aebSLuigi Rizzo 
1117f9790aebSLuigi Rizzo 	/* If there isn't native support and netmap is not allowed
1118f9790aebSLuigi Rizzo 	 * to use generic adapters, we cannot satisfy the request.
1119f9790aebSLuigi Rizzo 	 */
1120f9790aebSLuigi Rizzo 	if (!NETMAP_CAPABLE(ifp) && i == NETMAP_ADMODE_NATIVE)
1121f2637526SLuigi Rizzo 		return EOPNOTSUPP;
1122f9790aebSLuigi Rizzo 
1123f9790aebSLuigi Rizzo 	/* Otherwise, create a generic adapter and return it,
1124f9790aebSLuigi Rizzo 	 * saving the previously used netmap adapter, if any.
1125f9790aebSLuigi Rizzo 	 *
1126f9790aebSLuigi Rizzo 	 * Note that here 'prev_na', if not NULL, MUST be a
1127f9790aebSLuigi Rizzo 	 * native adapter, and CANNOT be a generic one. This is
1128f9790aebSLuigi Rizzo 	 * true because generic adapters are created on demand, and
1129f9790aebSLuigi Rizzo 	 * destroyed when not used anymore. Therefore, if the adapter
1130f9790aebSLuigi Rizzo 	 * currently attached to an interface 'ifp' is generic, it
1131f9790aebSLuigi Rizzo 	 * must be that
1132f9790aebSLuigi Rizzo 	 * (NA(ifp)->active_fds > 0 || NETMAP_OWNED_BY_KERN(NA(ifp))).
1133f9790aebSLuigi Rizzo 	 * Consequently, if NA(ifp) is generic, we will enter one of
1134f9790aebSLuigi Rizzo 	 * the branches above. This ensures that we never override
1135f9790aebSLuigi Rizzo 	 * a generic adapter with another generic adapter.
1136f9790aebSLuigi Rizzo 	 */
1137f9790aebSLuigi Rizzo 	prev_na = NA(ifp);
1138f9790aebSLuigi Rizzo 	error = generic_netmap_attach(ifp);
1139f9790aebSLuigi Rizzo 	if (error)
1140f9790aebSLuigi Rizzo 		return error;
1141f9790aebSLuigi Rizzo 
1142f9790aebSLuigi Rizzo 	*na = NA(ifp);
1143f9790aebSLuigi Rizzo 	gna = (struct netmap_generic_adapter*)NA(ifp);
1144f9790aebSLuigi Rizzo 	gna->prev = prev_na; /* save old na */
1145f9790aebSLuigi Rizzo 	if (prev_na != NULL) {
1146f9790aebSLuigi Rizzo 		ifunit_ref(ifp->if_xname);
1147f9790aebSLuigi Rizzo 		// XXX add a refcount ?
1148f9790aebSLuigi Rizzo 		netmap_adapter_get(prev_na);
1149f9790aebSLuigi Rizzo 	}
115017885a7bSLuigi Rizzo 	ND("Created generic NA %p (prev %p)", gna, gna->prev);
1151f9790aebSLuigi Rizzo 
1152f9790aebSLuigi Rizzo 	return 0;
1153f9790aebSLuigi Rizzo }
1154f9790aebSLuigi Rizzo 
1155f9790aebSLuigi Rizzo 
115668b8534bSLuigi Rizzo /*
1157ce3ee1e7SLuigi Rizzo  * MUST BE CALLED UNDER NMG_LOCK()
1158ce3ee1e7SLuigi Rizzo  *
1159f2637526SLuigi Rizzo  * Get a refcounted reference to a netmap adapter attached
1160f2637526SLuigi Rizzo  * to the interface specified by nmr.
1161ce3ee1e7SLuigi Rizzo  * This is always called in the execution of an ioctl().
1162ce3ee1e7SLuigi Rizzo  *
1163f2637526SLuigi Rizzo  * Return ENXIO if the interface specified by the request does
1164f2637526SLuigi Rizzo  * not exist, ENOTSUP if netmap is not supported by the interface,
1165f2637526SLuigi Rizzo  * EBUSY if the interface is already attached to a bridge,
1166f2637526SLuigi Rizzo  * EINVAL if parameters are invalid, ENOMEM if needed resources
1167f2637526SLuigi Rizzo  * could not be allocated.
1168f2637526SLuigi Rizzo  * If successful, hold a reference to the netmap adapter.
1169f18be576SLuigi Rizzo  *
1170f2637526SLuigi Rizzo  * No reference is kept on the real interface, which may then
1171f2637526SLuigi Rizzo  * disappear at any time.
117268b8534bSLuigi Rizzo  */
1173f9790aebSLuigi Rizzo int
1174f9790aebSLuigi Rizzo netmap_get_na(struct nmreq *nmr, struct netmap_adapter **na, int create)
117568b8534bSLuigi Rizzo {
1176*f0ea3689SLuigi Rizzo 	struct ifnet *ifp = NULL;
1177f9790aebSLuigi Rizzo 	int error = 0;
1178*f0ea3689SLuigi Rizzo 	struct netmap_adapter *ret = NULL;
1179f9790aebSLuigi Rizzo 
1180f9790aebSLuigi Rizzo 	*na = NULL;     /* default return value */
1181f196ce38SLuigi Rizzo 
1182ce3ee1e7SLuigi Rizzo 	/* first try to see if this is a bridge port. */
1183ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
1184ce3ee1e7SLuigi Rizzo 
1185*f0ea3689SLuigi Rizzo 	error = netmap_get_pipe_na(nmr, na, create);
1186*f0ea3689SLuigi Rizzo 	if (error || *na != NULL)
1187f9790aebSLuigi Rizzo 		return error;
1188ce3ee1e7SLuigi Rizzo 
1189*f0ea3689SLuigi Rizzo 	error = netmap_get_bdg_na(nmr, na, create);
1190*f0ea3689SLuigi Rizzo 	if (error)
1191*f0ea3689SLuigi Rizzo 		return error;
1192*f0ea3689SLuigi Rizzo 
1193*f0ea3689SLuigi Rizzo 	if (*na != NULL) /* valid match in netmap_get_bdg_na() */
1194*f0ea3689SLuigi Rizzo 		goto pipes;
1195*f0ea3689SLuigi Rizzo 
1196f9790aebSLuigi Rizzo 	ifp = ifunit_ref(nmr->nr_name);
1197f9790aebSLuigi Rizzo 	if (ifp == NULL) {
1198ce3ee1e7SLuigi Rizzo 	        return ENXIO;
1199f196ce38SLuigi Rizzo 	}
1200ce3ee1e7SLuigi Rizzo 
1201f9790aebSLuigi Rizzo 	error = netmap_get_hw_na(ifp, &ret);
1202f9790aebSLuigi Rizzo 	if (error)
1203f9790aebSLuigi Rizzo 		goto out;
1204f18be576SLuigi Rizzo 
1205f9790aebSLuigi Rizzo 	/* Users cannot use the NIC attached to a bridge directly */
1206f9790aebSLuigi Rizzo 	if (NETMAP_OWNED_BY_KERN(ret)) {
1207f2637526SLuigi Rizzo 		error = EBUSY;
1208f9790aebSLuigi Rizzo 		goto out;
1209ce3ee1e7SLuigi Rizzo 	}
1210f9790aebSLuigi Rizzo 	*na = ret;
1211f9790aebSLuigi Rizzo 	netmap_adapter_get(ret);
1212*f0ea3689SLuigi Rizzo 
1213*f0ea3689SLuigi Rizzo pipes:
1214*f0ea3689SLuigi Rizzo 	error = netmap_pipe_alloc(*na, nmr);
1215*f0ea3689SLuigi Rizzo 
1216f9790aebSLuigi Rizzo out:
1217*f0ea3689SLuigi Rizzo 	if (error && ret != NULL)
1218*f0ea3689SLuigi Rizzo 		netmap_adapter_put(ret);
1219*f0ea3689SLuigi Rizzo 
1220*f0ea3689SLuigi Rizzo 	if (ifp)
1221f9790aebSLuigi Rizzo 		if_rele(ifp);
1222f18be576SLuigi Rizzo 
12235ab0d24dSLuigi Rizzo 	return error;
12245ab0d24dSLuigi Rizzo }
1225ce3ee1e7SLuigi Rizzo 
1226ce3ee1e7SLuigi Rizzo 
1227f9790aebSLuigi Rizzo /*
1228f9790aebSLuigi Rizzo  * validate parameters on entry for *_txsync()
1229f9790aebSLuigi Rizzo  * Returns ring->cur if ok, or something >= kring->nkr_num_slots
123017885a7bSLuigi Rizzo  * in case of error.
1231f9790aebSLuigi Rizzo  *
123217885a7bSLuigi Rizzo  * rhead, rcur and rtail=hwtail are stored from previous round.
123317885a7bSLuigi Rizzo  * hwcur is the next packet to send to the ring.
1234f9790aebSLuigi Rizzo  *
123517885a7bSLuigi Rizzo  * We want
123617885a7bSLuigi Rizzo  *    hwcur <= *rhead <= head <= cur <= tail = *rtail <= hwtail
1237f9790aebSLuigi Rizzo  *
123817885a7bSLuigi Rizzo  * hwcur, rhead, rtail and hwtail are reliable
1239f9790aebSLuigi Rizzo  */
1240f9790aebSLuigi Rizzo u_int
124117885a7bSLuigi Rizzo nm_txsync_prologue(struct netmap_kring *kring)
1242f9790aebSLuigi Rizzo {
1243f9790aebSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
124417885a7bSLuigi Rizzo 	u_int head = ring->head; /* read only once */
1245f9790aebSLuigi Rizzo 	u_int cur = ring->cur; /* read only once */
1246f9790aebSLuigi Rizzo 	u_int n = kring->nkr_num_slots;
1247ce3ee1e7SLuigi Rizzo 
124817885a7bSLuigi Rizzo 	ND(5, "%s kcur %d ktail %d head %d cur %d tail %d",
124917885a7bSLuigi Rizzo 		kring->name,
125017885a7bSLuigi Rizzo 		kring->nr_hwcur, kring->nr_hwtail,
125117885a7bSLuigi Rizzo 		ring->head, ring->cur, ring->tail);
125217885a7bSLuigi Rizzo #if 1 /* kernel sanity checks; but we can trust the kring. */
125317885a7bSLuigi Rizzo 	if (kring->nr_hwcur >= n || kring->rhead >= n ||
125417885a7bSLuigi Rizzo 	    kring->rtail >= n ||  kring->nr_hwtail >= n)
1255f9790aebSLuigi Rizzo 		goto error;
1256f9790aebSLuigi Rizzo #endif /* kernel sanity checks */
125717885a7bSLuigi Rizzo 	/*
125817885a7bSLuigi Rizzo 	 * user sanity checks. We only use 'cur',
125917885a7bSLuigi Rizzo 	 * A, B, ... are possible positions for cur:
126017885a7bSLuigi Rizzo 	 *
126117885a7bSLuigi Rizzo 	 *  0    A  cur   B  tail  C  n-1
126217885a7bSLuigi Rizzo 	 *  0    D  tail  E  cur   F  n-1
126317885a7bSLuigi Rizzo 	 *
126417885a7bSLuigi Rizzo 	 * B, F, D are valid. A, C, E are wrong
126517885a7bSLuigi Rizzo 	 */
126617885a7bSLuigi Rizzo 	if (kring->rtail >= kring->rhead) {
126717885a7bSLuigi Rizzo 		/* want rhead <= head <= rtail */
126817885a7bSLuigi Rizzo 		if (head < kring->rhead || head > kring->rtail)
1269f9790aebSLuigi Rizzo 			goto error;
127017885a7bSLuigi Rizzo 		/* and also head <= cur <= rtail */
127117885a7bSLuigi Rizzo 		if (cur < head || cur > kring->rtail)
1272f9790aebSLuigi Rizzo 			goto error;
127317885a7bSLuigi Rizzo 	} else { /* here rtail < rhead */
127417885a7bSLuigi Rizzo 		/* we need head outside rtail .. rhead */
127517885a7bSLuigi Rizzo 		if (head > kring->rtail && head < kring->rhead)
127617885a7bSLuigi Rizzo 			goto error;
127717885a7bSLuigi Rizzo 
127817885a7bSLuigi Rizzo 		/* two cases now: head <= rtail or head >= rhead  */
127917885a7bSLuigi Rizzo 		if (head <= kring->rtail) {
128017885a7bSLuigi Rizzo 			/* want head <= cur <= rtail */
128117885a7bSLuigi Rizzo 			if (cur < head || cur > kring->rtail)
128217885a7bSLuigi Rizzo 				goto error;
128317885a7bSLuigi Rizzo 		} else { /* head >= rhead */
128417885a7bSLuigi Rizzo 			/* cur must be outside rtail..head */
128517885a7bSLuigi Rizzo 			if (cur > kring->rtail && cur < head)
128617885a7bSLuigi Rizzo 				goto error;
1287f18be576SLuigi Rizzo 		}
1288f9790aebSLuigi Rizzo 	}
128917885a7bSLuigi Rizzo 	if (ring->tail != kring->rtail) {
129017885a7bSLuigi Rizzo 		RD(5, "tail overwritten was %d need %d",
129117885a7bSLuigi Rizzo 			ring->tail, kring->rtail);
129217885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
129317885a7bSLuigi Rizzo 	}
129417885a7bSLuigi Rizzo 	kring->rhead = head;
129517885a7bSLuigi Rizzo 	kring->rcur = cur;
129617885a7bSLuigi Rizzo 	return head;
1297f9790aebSLuigi Rizzo 
1298f9790aebSLuigi Rizzo error:
129917885a7bSLuigi Rizzo 	RD(5, "%s kring error: hwcur %d rcur %d hwtail %d cur %d tail %d",
130017885a7bSLuigi Rizzo 		kring->name,
1301f9790aebSLuigi Rizzo 		kring->nr_hwcur,
130217885a7bSLuigi Rizzo 		kring->rcur, kring->nr_hwtail,
130317885a7bSLuigi Rizzo 		cur, ring->tail);
1304f9790aebSLuigi Rizzo 	return n;
130568b8534bSLuigi Rizzo }
130668b8534bSLuigi Rizzo 
130768b8534bSLuigi Rizzo 
130868b8534bSLuigi Rizzo /*
1309f9790aebSLuigi Rizzo  * validate parameters on entry for *_rxsync()
131017885a7bSLuigi Rizzo  * Returns ring->head if ok, kring->nkr_num_slots on error.
1311f9790aebSLuigi Rizzo  *
131217885a7bSLuigi Rizzo  * For a valid configuration,
131317885a7bSLuigi Rizzo  * hwcur <= head <= cur <= tail <= hwtail
1314f9790aebSLuigi Rizzo  *
131517885a7bSLuigi Rizzo  * We only consider head and cur.
131617885a7bSLuigi Rizzo  * hwcur and hwtail are reliable.
1317f9790aebSLuigi Rizzo  *
1318f9790aebSLuigi Rizzo  */
1319f9790aebSLuigi Rizzo u_int
132017885a7bSLuigi Rizzo nm_rxsync_prologue(struct netmap_kring *kring)
1321f9790aebSLuigi Rizzo {
1322f9790aebSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
132317885a7bSLuigi Rizzo 	uint32_t const n = kring->nkr_num_slots;
132417885a7bSLuigi Rizzo 	uint32_t head, cur;
1325f9790aebSLuigi Rizzo 
132617885a7bSLuigi Rizzo 	ND("%s kc %d kt %d h %d c %d t %d",
132717885a7bSLuigi Rizzo 		kring->name,
132817885a7bSLuigi Rizzo 		kring->nr_hwcur, kring->nr_hwtail,
132917885a7bSLuigi Rizzo 		ring->head, ring->cur, ring->tail);
133017885a7bSLuigi Rizzo 	/*
133117885a7bSLuigi Rizzo 	 * Before storing the new values, we should check they do not
133217885a7bSLuigi Rizzo 	 * move backwards. However:
133317885a7bSLuigi Rizzo 	 * - head is not an issue because the previous value is hwcur;
133417885a7bSLuigi Rizzo 	 * - cur could in principle go back, however it does not matter
133517885a7bSLuigi Rizzo 	 *   because we are processing a brand new rxsync()
133617885a7bSLuigi Rizzo 	 */
133717885a7bSLuigi Rizzo 	cur = kring->rcur = ring->cur;	/* read only once */
133817885a7bSLuigi Rizzo 	head = kring->rhead = ring->head;	/* read only once */
1339f9790aebSLuigi Rizzo #if 1 /* kernel sanity checks */
134017885a7bSLuigi Rizzo 	if (kring->nr_hwcur >= n || kring->nr_hwtail >= n)
1341f9790aebSLuigi Rizzo 		goto error;
1342f9790aebSLuigi Rizzo #endif /* kernel sanity checks */
1343f9790aebSLuigi Rizzo 	/* user sanity checks */
134417885a7bSLuigi Rizzo 	if (kring->nr_hwtail >= kring->nr_hwcur) {
134517885a7bSLuigi Rizzo 		/* want hwcur <= rhead <= hwtail */
134617885a7bSLuigi Rizzo 		if (head < kring->nr_hwcur || head > kring->nr_hwtail)
1347f9790aebSLuigi Rizzo 			goto error;
134817885a7bSLuigi Rizzo 		/* and also rhead <= rcur <= hwtail */
134917885a7bSLuigi Rizzo 		if (cur < head || cur > kring->nr_hwtail)
1350f9790aebSLuigi Rizzo 			goto error;
1351f9790aebSLuigi Rizzo 	} else {
135217885a7bSLuigi Rizzo 		/* we need rhead outside hwtail..hwcur */
135317885a7bSLuigi Rizzo 		if (head < kring->nr_hwcur && head > kring->nr_hwtail)
1354f9790aebSLuigi Rizzo 			goto error;
135517885a7bSLuigi Rizzo 		/* two cases now: head <= hwtail or head >= hwcur  */
135617885a7bSLuigi Rizzo 		if (head <= kring->nr_hwtail) {
135717885a7bSLuigi Rizzo 			/* want head <= cur <= hwtail */
135817885a7bSLuigi Rizzo 			if (cur < head || cur > kring->nr_hwtail)
1359f9790aebSLuigi Rizzo 				goto error;
136017885a7bSLuigi Rizzo 		} else {
136117885a7bSLuigi Rizzo 			/* cur must be outside hwtail..head */
136217885a7bSLuigi Rizzo 			if (cur < head && cur > kring->nr_hwtail)
1363f9790aebSLuigi Rizzo 				goto error;
1364f9790aebSLuigi Rizzo 		}
1365f9790aebSLuigi Rizzo 	}
136617885a7bSLuigi Rizzo 	if (ring->tail != kring->rtail) {
136717885a7bSLuigi Rizzo 		RD(5, "%s tail overwritten was %d need %d",
136817885a7bSLuigi Rizzo 			kring->name,
136917885a7bSLuigi Rizzo 			ring->tail, kring->rtail);
137017885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
137117885a7bSLuigi Rizzo 	}
137217885a7bSLuigi Rizzo 	return head;
1373f9790aebSLuigi Rizzo 
1374f9790aebSLuigi Rizzo error:
137517885a7bSLuigi Rizzo 	RD(5, "kring error: hwcur %d rcur %d hwtail %d head %d cur %d tail %d",
1376f9790aebSLuigi Rizzo 		kring->nr_hwcur,
137717885a7bSLuigi Rizzo 		kring->rcur, kring->nr_hwtail,
137817885a7bSLuigi Rizzo 		kring->rhead, kring->rcur, ring->tail);
1379f9790aebSLuigi Rizzo 	return n;
1380f9790aebSLuigi Rizzo }
1381f9790aebSLuigi Rizzo 
138217885a7bSLuigi Rizzo 
1383f9790aebSLuigi Rizzo /*
138468b8534bSLuigi Rizzo  * Error routine called when txsync/rxsync detects an error.
138517885a7bSLuigi Rizzo  * Can't do much more than resetting head =cur = hwcur, tail = hwtail
138668b8534bSLuigi Rizzo  * Return 1 on reinit.
1387506cc70cSLuigi Rizzo  *
1388506cc70cSLuigi Rizzo  * This routine is only called by the upper half of the kernel.
1389506cc70cSLuigi Rizzo  * It only reads hwcur (which is changed only by the upper half, too)
139017885a7bSLuigi Rizzo  * and hwtail (which may be changed by the lower half, but only on
1391506cc70cSLuigi Rizzo  * a tx ring and only to increase it, so any error will be recovered
1392506cc70cSLuigi Rizzo  * on the next call). For the above, we don't strictly need to call
1393506cc70cSLuigi Rizzo  * it under lock.
139468b8534bSLuigi Rizzo  */
139568b8534bSLuigi Rizzo int
139668b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring)
139768b8534bSLuigi Rizzo {
139868b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
139968b8534bSLuigi Rizzo 	u_int i, lim = kring->nkr_num_slots - 1;
140068b8534bSLuigi Rizzo 	int errors = 0;
140168b8534bSLuigi Rizzo 
1402ce3ee1e7SLuigi Rizzo 	// XXX KASSERT nm_kr_tryget
1403f9790aebSLuigi Rizzo 	RD(10, "called for %s", NM_IFPNAME(kring->na->ifp));
140417885a7bSLuigi Rizzo 	// XXX probably wrong to trust userspace
140517885a7bSLuigi Rizzo 	kring->rhead = ring->head;
140617885a7bSLuigi Rizzo 	kring->rcur  = ring->cur;
140717885a7bSLuigi Rizzo 	kring->rtail = ring->tail;
140817885a7bSLuigi Rizzo 
140968b8534bSLuigi Rizzo 	if (ring->cur > lim)
141068b8534bSLuigi Rizzo 		errors++;
141117885a7bSLuigi Rizzo 	if (ring->head > lim)
141217885a7bSLuigi Rizzo 		errors++;
141317885a7bSLuigi Rizzo 	if (ring->tail > lim)
141417885a7bSLuigi Rizzo 		errors++;
141568b8534bSLuigi Rizzo 	for (i = 0; i <= lim; i++) {
141668b8534bSLuigi Rizzo 		u_int idx = ring->slot[i].buf_idx;
141768b8534bSLuigi Rizzo 		u_int len = ring->slot[i].len;
141868b8534bSLuigi Rizzo 		if (idx < 2 || idx >= netmap_total_buffers) {
141917885a7bSLuigi Rizzo 			RD(5, "bad index at slot %d idx %d len %d ", i, idx, len);
142068b8534bSLuigi Rizzo 			ring->slot[i].buf_idx = 0;
142168b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
1422ce3ee1e7SLuigi Rizzo 		} else if (len > NETMAP_BDG_BUF_SIZE(kring->na->nm_mem)) {
142368b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
142417885a7bSLuigi Rizzo 			RD(5, "bad len at slot %d idx %d len %d", i, idx, len);
142568b8534bSLuigi Rizzo 		}
142668b8534bSLuigi Rizzo 	}
142768b8534bSLuigi Rizzo 	if (errors) {
14288241616dSLuigi Rizzo 		RD(10, "total %d errors", errors);
142917885a7bSLuigi Rizzo 		RD(10, "%s reinit, cur %d -> %d tail %d -> %d",
143017885a7bSLuigi Rizzo 			kring->name,
143168b8534bSLuigi Rizzo 			ring->cur, kring->nr_hwcur,
143217885a7bSLuigi Rizzo 			ring->tail, kring->nr_hwtail);
143317885a7bSLuigi Rizzo 		ring->head = kring->rhead = kring->nr_hwcur;
143417885a7bSLuigi Rizzo 		ring->cur  = kring->rcur  = kring->nr_hwcur;
143517885a7bSLuigi Rizzo 		ring->tail = kring->rtail = kring->nr_hwtail;
143668b8534bSLuigi Rizzo 	}
143768b8534bSLuigi Rizzo 	return (errors ? 1 : 0);
143868b8534bSLuigi Rizzo }
143968b8534bSLuigi Rizzo 
144068b8534bSLuigi Rizzo 
144168b8534bSLuigi Rizzo /*
144268b8534bSLuigi Rizzo  * Set the ring ID. For devices with a single queue, a request
144368b8534bSLuigi Rizzo  * for all rings is the same as a single ring.
144468b8534bSLuigi Rizzo  */
144568b8534bSLuigi Rizzo static int
1446*f0ea3689SLuigi Rizzo netmap_set_ringid(struct netmap_priv_d *priv, uint16_t ringid, uint32_t flags)
144768b8534bSLuigi Rizzo {
1448f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1449*f0ea3689SLuigi Rizzo 	u_int j, i = ringid & NETMAP_RING_MASK;
1450*f0ea3689SLuigi Rizzo 	u_int reg = flags & NR_REG_MASK;
145168b8534bSLuigi Rizzo 
1452*f0ea3689SLuigi Rizzo 	if (reg == NR_REG_DEFAULT) {
1453*f0ea3689SLuigi Rizzo 		/* convert from old ringid to flags */
145468b8534bSLuigi Rizzo 		if (ringid & NETMAP_SW_RING) {
1455*f0ea3689SLuigi Rizzo 			reg = NR_REG_SW;
145668b8534bSLuigi Rizzo 		} else if (ringid & NETMAP_HW_RING) {
1457*f0ea3689SLuigi Rizzo 			reg = NR_REG_ONE_NIC;
145868b8534bSLuigi Rizzo 		} else {
1459*f0ea3689SLuigi Rizzo 			reg = NR_REG_ALL_NIC;
1460*f0ea3689SLuigi Rizzo 		}
1461*f0ea3689SLuigi Rizzo 		D("deprecated API, old ringid 0x%x -> ringid %x reg %d", ringid, i, reg);
1462*f0ea3689SLuigi Rizzo 	}
1463*f0ea3689SLuigi Rizzo 	switch (reg) {
1464*f0ea3689SLuigi Rizzo 	case NR_REG_ALL_NIC:
1465*f0ea3689SLuigi Rizzo 	case NR_REG_PIPE_MASTER:
1466*f0ea3689SLuigi Rizzo 	case NR_REG_PIPE_SLAVE:
1467*f0ea3689SLuigi Rizzo 		priv->np_txqfirst = 0;
1468*f0ea3689SLuigi Rizzo 		priv->np_txqlast = na->num_tx_rings;
1469*f0ea3689SLuigi Rizzo 		priv->np_rxqfirst = 0;
1470*f0ea3689SLuigi Rizzo 		priv->np_rxqlast = na->num_rx_rings;
1471*f0ea3689SLuigi Rizzo 		ND("%s %d %d", "ALL/PIPE",
1472*f0ea3689SLuigi Rizzo 			priv->np_rxqfirst, priv->np_rxqlast);
1473*f0ea3689SLuigi Rizzo 		break;
1474*f0ea3689SLuigi Rizzo 	case NR_REG_SW:
1475*f0ea3689SLuigi Rizzo 	case NR_REG_NIC_SW:
1476*f0ea3689SLuigi Rizzo 		if (!(na->na_flags & NAF_HOST_RINGS)) {
1477*f0ea3689SLuigi Rizzo 			D("host rings not supported");
1478*f0ea3689SLuigi Rizzo 			return EINVAL;
1479*f0ea3689SLuigi Rizzo 		}
1480*f0ea3689SLuigi Rizzo 		priv->np_txqfirst = (reg == NR_REG_SW ?
1481*f0ea3689SLuigi Rizzo 			na->num_tx_rings : 0);
1482*f0ea3689SLuigi Rizzo 		priv->np_txqlast = na->num_tx_rings + 1;
1483*f0ea3689SLuigi Rizzo 		priv->np_rxqfirst = (reg == NR_REG_SW ?
1484*f0ea3689SLuigi Rizzo 			na->num_rx_rings : 0);
1485*f0ea3689SLuigi Rizzo 		priv->np_rxqlast = na->num_rx_rings + 1;
1486*f0ea3689SLuigi Rizzo 		ND("%s %d %d", reg == NR_REG_SW ? "SW" : "NIC+SW",
1487*f0ea3689SLuigi Rizzo 			priv->np_rxqfirst, priv->np_rxqlast);
1488*f0ea3689SLuigi Rizzo 		break;
1489*f0ea3689SLuigi Rizzo 	case NR_REG_ONE_NIC:
1490*f0ea3689SLuigi Rizzo 		if (i >= na->num_tx_rings && i >= na->num_rx_rings) {
1491*f0ea3689SLuigi Rizzo 			D("invalid ring id %d", i);
1492*f0ea3689SLuigi Rizzo 			return EINVAL;
1493*f0ea3689SLuigi Rizzo 		}
1494*f0ea3689SLuigi Rizzo 		/* if not enough rings, use the first one */
1495*f0ea3689SLuigi Rizzo 		j = i;
1496*f0ea3689SLuigi Rizzo 		if (j >= na->num_tx_rings)
1497*f0ea3689SLuigi Rizzo 			j = 0;
1498*f0ea3689SLuigi Rizzo 		priv->np_txqfirst = j;
1499*f0ea3689SLuigi Rizzo 		priv->np_txqlast = j + 1;
1500*f0ea3689SLuigi Rizzo 		j = i;
1501*f0ea3689SLuigi Rizzo 		if (j >= na->num_rx_rings)
1502*f0ea3689SLuigi Rizzo 			j = 0;
1503*f0ea3689SLuigi Rizzo 		priv->np_rxqfirst = j;
1504*f0ea3689SLuigi Rizzo 		priv->np_rxqlast = j + 1;
1505*f0ea3689SLuigi Rizzo 		break;
1506*f0ea3689SLuigi Rizzo 	default:
1507*f0ea3689SLuigi Rizzo 		D("invalid regif type %d", reg);
1508*f0ea3689SLuigi Rizzo 		return EINVAL;
150968b8534bSLuigi Rizzo 	}
151068b8534bSLuigi Rizzo 	priv->np_txpoll = (ringid & NETMAP_NO_TX_POLL) ? 0 : 1;
1511*f0ea3689SLuigi Rizzo 	priv->np_flags = (flags & ~NR_REG_MASK) | reg;
1512*f0ea3689SLuigi Rizzo 	if (nm_tx_si_user(priv))
1513*f0ea3689SLuigi Rizzo 		na->tx_si_users++;
1514*f0ea3689SLuigi Rizzo 	if (nm_rx_si_user(priv))
1515*f0ea3689SLuigi Rizzo 		na->rx_si_users++;
1516ae10d1afSLuigi Rizzo 	if (netmap_verbose) {
1517*f0ea3689SLuigi Rizzo 		D("%s: tx [%d,%d) rx [%d,%d) id %d",
1518*f0ea3689SLuigi Rizzo 			NM_IFPNAME(na->ifp),
1519*f0ea3689SLuigi Rizzo 			priv->np_txqfirst,
1520*f0ea3689SLuigi Rizzo 			priv->np_txqlast,
1521*f0ea3689SLuigi Rizzo 			priv->np_rxqfirst,
1522*f0ea3689SLuigi Rizzo 			priv->np_rxqlast,
1523*f0ea3689SLuigi Rizzo 			i);
1524ae10d1afSLuigi Rizzo 	}
152568b8534bSLuigi Rizzo 	return 0;
152668b8534bSLuigi Rizzo }
152768b8534bSLuigi Rizzo 
1528f18be576SLuigi Rizzo /*
1529f18be576SLuigi Rizzo  * possibly move the interface to netmap-mode.
1530f18be576SLuigi Rizzo  * If success it returns a pointer to netmap_if, otherwise NULL.
1531ce3ee1e7SLuigi Rizzo  * This must be called with NMG_LOCK held.
1532f18be576SLuigi Rizzo  */
1533f9790aebSLuigi Rizzo struct netmap_if *
1534f9790aebSLuigi Rizzo netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na,
1535*f0ea3689SLuigi Rizzo 	uint16_t ringid, uint32_t flags, int *err)
1536f18be576SLuigi Rizzo {
1537f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
1538f18be576SLuigi Rizzo 	struct netmap_if *nifp = NULL;
1539f9790aebSLuigi Rizzo 	int error, need_mem = 0;
1540f18be576SLuigi Rizzo 
1541ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
1542f18be576SLuigi Rizzo 	/* ring configuration may have changed, fetch from the card */
1543f18be576SLuigi Rizzo 	netmap_update_config(na);
1544f9790aebSLuigi Rizzo 	priv->np_na = na;     /* store the reference */
1545*f0ea3689SLuigi Rizzo 	error = netmap_set_ringid(priv, ringid, flags);
1546f18be576SLuigi Rizzo 	if (error)
1547f18be576SLuigi Rizzo 		goto out;
1548ce3ee1e7SLuigi Rizzo 	/* ensure allocators are ready */
1549ce3ee1e7SLuigi Rizzo 	need_mem = !netmap_have_memory_locked(priv);
1550ce3ee1e7SLuigi Rizzo 	if (need_mem) {
1551ce3ee1e7SLuigi Rizzo 		error = netmap_get_memory_locked(priv);
1552ce3ee1e7SLuigi Rizzo 		ND("get_memory returned %d", error);
1553ce3ee1e7SLuigi Rizzo 		if (error)
1554ce3ee1e7SLuigi Rizzo 			goto out;
1555ce3ee1e7SLuigi Rizzo 	}
1556f9790aebSLuigi Rizzo 	nifp = netmap_if_new(NM_IFPNAME(ifp), na);
1557f18be576SLuigi Rizzo 	if (nifp == NULL) { /* allocation failed */
1558ce3ee1e7SLuigi Rizzo 		/* we should drop the allocator, but only
1559ce3ee1e7SLuigi Rizzo 		 * if we were the ones who grabbed it
1560ce3ee1e7SLuigi Rizzo 		 */
1561f18be576SLuigi Rizzo 		error = ENOMEM;
1562ce3ee1e7SLuigi Rizzo 		goto out;
1563ce3ee1e7SLuigi Rizzo 	}
1564f9790aebSLuigi Rizzo 	na->active_fds++;
1565ce3ee1e7SLuigi Rizzo 	if (ifp->if_capenable & IFCAP_NETMAP) {
1566f18be576SLuigi Rizzo 		/* was already set */
1567f18be576SLuigi Rizzo 	} else {
1568f18be576SLuigi Rizzo 		/* Otherwise set the card in netmap mode
1569f18be576SLuigi Rizzo 		 * and make it use the shared buffers.
1570ce3ee1e7SLuigi Rizzo 		 *
1571ce3ee1e7SLuigi Rizzo 		 * do not core lock because the race is harmless here,
1572ce3ee1e7SLuigi Rizzo 		 * there cannot be any traffic to netmap_transmit()
1573ce3ee1e7SLuigi Rizzo 		 */
1574f9790aebSLuigi Rizzo 		na->na_lut = na->nm_mem->pools[NETMAP_BUF_POOL].lut;
1575f9790aebSLuigi Rizzo 		ND("%p->na_lut == %p", na, na->na_lut);
1576f9790aebSLuigi Rizzo 		na->na_lut_objtotal = na->nm_mem->pools[NETMAP_BUF_POOL].objtotal;
1577f9790aebSLuigi Rizzo 		error = na->nm_register(na, 1); /* mode on */
1578f18be576SLuigi Rizzo 		if (error) {
1579ce3ee1e7SLuigi Rizzo 			netmap_do_unregif(priv, nifp);
1580f18be576SLuigi Rizzo 			nifp = NULL;
1581f18be576SLuigi Rizzo 		}
1582f18be576SLuigi Rizzo 	}
1583f18be576SLuigi Rizzo out:
1584f18be576SLuigi Rizzo 	*err = error;
1585f9790aebSLuigi Rizzo 	if (error) {
1586f9790aebSLuigi Rizzo 		priv->np_na = NULL;
1587f9790aebSLuigi Rizzo 		if (need_mem)
1588f9790aebSLuigi Rizzo 			netmap_drop_memory_locked(priv);
1589f9790aebSLuigi Rizzo 	}
1590ce3ee1e7SLuigi Rizzo 	if (nifp != NULL) {
1591ce3ee1e7SLuigi Rizzo 		/*
1592ce3ee1e7SLuigi Rizzo 		 * advertise that the interface is ready bt setting ni_nifp.
1593ce3ee1e7SLuigi Rizzo 		 * The barrier is needed because readers (poll and *SYNC)
1594ce3ee1e7SLuigi Rizzo 		 * check for priv->np_nifp != NULL without locking
1595ce3ee1e7SLuigi Rizzo 		 */
1596ce3ee1e7SLuigi Rizzo 		wmb(); /* make sure previous writes are visible to all CPUs */
1597ce3ee1e7SLuigi Rizzo 		priv->np_nifp = nifp;
1598ce3ee1e7SLuigi Rizzo 	}
1599f18be576SLuigi Rizzo 	return nifp;
1600f18be576SLuigi Rizzo }
1601f18be576SLuigi Rizzo 
1602f18be576SLuigi Rizzo 
1603f18be576SLuigi Rizzo 
160468b8534bSLuigi Rizzo /*
160568b8534bSLuigi Rizzo  * ioctl(2) support for the "netmap" device.
160668b8534bSLuigi Rizzo  *
160768b8534bSLuigi Rizzo  * Following a list of accepted commands:
160868b8534bSLuigi Rizzo  * - NIOCGINFO
160968b8534bSLuigi Rizzo  * - SIOCGIFADDR	just for convenience
161068b8534bSLuigi Rizzo  * - NIOCREGIF
161168b8534bSLuigi Rizzo  * - NIOCTXSYNC
161268b8534bSLuigi Rizzo  * - NIOCRXSYNC
161368b8534bSLuigi Rizzo  *
161468b8534bSLuigi Rizzo  * Return 0 on success, errno otherwise.
161568b8534bSLuigi Rizzo  */
1616f9790aebSLuigi Rizzo int
16170b8ed8e0SLuigi Rizzo netmap_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
16180b8ed8e0SLuigi Rizzo 	int fflag, struct thread *td)
161968b8534bSLuigi Rizzo {
162068b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
1621ce3ee1e7SLuigi Rizzo 	struct ifnet *ifp = NULL;
162268b8534bSLuigi Rizzo 	struct nmreq *nmr = (struct nmreq *) data;
1623ce3ee1e7SLuigi Rizzo 	struct netmap_adapter *na = NULL;
162468b8534bSLuigi Rizzo 	int error;
1625*f0ea3689SLuigi Rizzo 	u_int i, qfirst, qlast;
162668b8534bSLuigi Rizzo 	struct netmap_if *nifp;
1627ce3ee1e7SLuigi Rizzo 	struct netmap_kring *krings;
162868b8534bSLuigi Rizzo 
16290b8ed8e0SLuigi Rizzo 	(void)dev;	/* UNUSED */
16300b8ed8e0SLuigi Rizzo 	(void)fflag;	/* UNUSED */
1631f196ce38SLuigi Rizzo 
163217885a7bSLuigi Rizzo 	if (cmd == NIOCGINFO || cmd == NIOCREGIF) {
163317885a7bSLuigi Rizzo 		/* truncate name */
163417885a7bSLuigi Rizzo 		nmr->nr_name[sizeof(nmr->nr_name) - 1] = '\0';
163517885a7bSLuigi Rizzo 		if (nmr->nr_version != NETMAP_API) {
163617885a7bSLuigi Rizzo 			D("API mismatch for %s got %d need %d",
163717885a7bSLuigi Rizzo 				nmr->nr_name,
163817885a7bSLuigi Rizzo 				nmr->nr_version, NETMAP_API);
163917885a7bSLuigi Rizzo 			nmr->nr_version = NETMAP_API;
1640*f0ea3689SLuigi Rizzo 		}
1641*f0ea3689SLuigi Rizzo 		if (nmr->nr_version < NETMAP_MIN_API ||
1642*f0ea3689SLuigi Rizzo 		    nmr->nr_version > NETMAP_MAX_API) {
164317885a7bSLuigi Rizzo 			return EINVAL;
164417885a7bSLuigi Rizzo 		}
164517885a7bSLuigi Rizzo 	}
1646506cc70cSLuigi Rizzo 	CURVNET_SET(TD_TO_VNET(td));
1647506cc70cSLuigi Rizzo 
164868b8534bSLuigi Rizzo 	error = devfs_get_cdevpriv((void **)&priv);
16498241616dSLuigi Rizzo 	if (error) {
1650506cc70cSLuigi Rizzo 		CURVNET_RESTORE();
16518241616dSLuigi Rizzo 		/* XXX ENOENT should be impossible, since the priv
16528241616dSLuigi Rizzo 		 * is now created in the open */
16538241616dSLuigi Rizzo 		return (error == ENOENT ? ENXIO : error);
1654506cc70cSLuigi Rizzo 	}
165568b8534bSLuigi Rizzo 
165668b8534bSLuigi Rizzo 	switch (cmd) {
165768b8534bSLuigi Rizzo 	case NIOCGINFO:		/* return capabilities etc */
1658f18be576SLuigi Rizzo 		if (nmr->nr_cmd == NETMAP_BDG_LIST) {
1659f18be576SLuigi Rizzo 			error = netmap_bdg_ctl(nmr, NULL);
1660f18be576SLuigi Rizzo 			break;
1661f18be576SLuigi Rizzo 		}
1662ce3ee1e7SLuigi Rizzo 
1663ce3ee1e7SLuigi Rizzo 		NMG_LOCK();
1664ce3ee1e7SLuigi Rizzo 		do {
1665ce3ee1e7SLuigi Rizzo 			/* memsize is always valid */
1666ce3ee1e7SLuigi Rizzo 			struct netmap_mem_d *nmd = &nm_mem;
1667ce3ee1e7SLuigi Rizzo 			u_int memflags;
1668ce3ee1e7SLuigi Rizzo 
1669ce3ee1e7SLuigi Rizzo 			if (nmr->nr_name[0] != '\0') {
1670ce3ee1e7SLuigi Rizzo 				/* get a refcount */
1671f9790aebSLuigi Rizzo 				error = netmap_get_na(nmr, &na, 1 /* create */);
16728241616dSLuigi Rizzo 				if (error)
16738241616dSLuigi Rizzo 					break;
1674f9790aebSLuigi Rizzo 				nmd = na->nm_mem; /* get memory allocator */
1675ce3ee1e7SLuigi Rizzo 			}
1676ce3ee1e7SLuigi Rizzo 
1677*f0ea3689SLuigi Rizzo 			error = netmap_mem_get_info(nmd, &nmr->nr_memsize, &memflags,
1678*f0ea3689SLuigi Rizzo 				&nmr->nr_arg2);
1679ce3ee1e7SLuigi Rizzo 			if (error)
1680ce3ee1e7SLuigi Rizzo 				break;
1681ce3ee1e7SLuigi Rizzo 			if (na == NULL) /* only memory info */
1682ce3ee1e7SLuigi Rizzo 				break;
16838241616dSLuigi Rizzo 			nmr->nr_offset = 0;
16848241616dSLuigi Rizzo 			nmr->nr_rx_slots = nmr->nr_tx_slots = 0;
1685ae10d1afSLuigi Rizzo 			netmap_update_config(na);
1686d76bf4ffSLuigi Rizzo 			nmr->nr_rx_rings = na->num_rx_rings;
1687d76bf4ffSLuigi Rizzo 			nmr->nr_tx_rings = na->num_tx_rings;
168864ae02c3SLuigi Rizzo 			nmr->nr_rx_slots = na->num_rx_desc;
168964ae02c3SLuigi Rizzo 			nmr->nr_tx_slots = na->num_tx_desc;
1690f9790aebSLuigi Rizzo 			netmap_adapter_put(na);
1691ce3ee1e7SLuigi Rizzo 		} while (0);
1692ce3ee1e7SLuigi Rizzo 		NMG_UNLOCK();
169368b8534bSLuigi Rizzo 		break;
169468b8534bSLuigi Rizzo 
169568b8534bSLuigi Rizzo 	case NIOCREGIF:
1696f18be576SLuigi Rizzo 		/* possibly attach/detach NIC and VALE switch */
1697f18be576SLuigi Rizzo 		i = nmr->nr_cmd;
1698f9790aebSLuigi Rizzo 		if (i == NETMAP_BDG_ATTACH || i == NETMAP_BDG_DETACH
1699*f0ea3689SLuigi Rizzo 				|| i == NETMAP_BDG_VNET_HDR) {
1700f18be576SLuigi Rizzo 			error = netmap_bdg_ctl(nmr, NULL);
1701f18be576SLuigi Rizzo 			break;
1702f18be576SLuigi Rizzo 		} else if (i != 0) {
1703f18be576SLuigi Rizzo 			D("nr_cmd must be 0 not %d", i);
1704f18be576SLuigi Rizzo 			error = EINVAL;
1705f18be576SLuigi Rizzo 			break;
1706f18be576SLuigi Rizzo 		}
1707f18be576SLuigi Rizzo 
17088241616dSLuigi Rizzo 		/* protect access to priv from concurrent NIOCREGIF */
1709ce3ee1e7SLuigi Rizzo 		NMG_LOCK();
1710ce3ee1e7SLuigi Rizzo 		do {
1711ce3ee1e7SLuigi Rizzo 			u_int memflags;
1712ce3ee1e7SLuigi Rizzo 
1713f9790aebSLuigi Rizzo 			if (priv->np_na != NULL) {	/* thread already registered */
1714*f0ea3689SLuigi Rizzo 				error = EBUSY;
1715506cc70cSLuigi Rizzo 				break;
1716506cc70cSLuigi Rizzo 			}
171768b8534bSLuigi Rizzo 			/* find the interface and a reference */
1718f9790aebSLuigi Rizzo 			error = netmap_get_na(nmr, &na, 1 /* create */); /* keep reference */
171968b8534bSLuigi Rizzo 			if (error)
1720ce3ee1e7SLuigi Rizzo 				break;
1721f9790aebSLuigi Rizzo 			ifp = na->ifp;
1722f9790aebSLuigi Rizzo 			if (NETMAP_OWNED_BY_KERN(na)) {
1723f9790aebSLuigi Rizzo 				netmap_adapter_put(na);
1724ce3ee1e7SLuigi Rizzo 				error = EBUSY;
1725ce3ee1e7SLuigi Rizzo 				break;
1726f196ce38SLuigi Rizzo 			}
1727*f0ea3689SLuigi Rizzo 			nifp = netmap_do_regif(priv, na, nmr->nr_ringid, nmr->nr_flags, &error);
1728f18be576SLuigi Rizzo 			if (!nifp) {    /* reg. failed, release priv and ref */
1729f9790aebSLuigi Rizzo 				netmap_adapter_put(na);
17308241616dSLuigi Rizzo 				priv->np_nifp = NULL;
1731ce3ee1e7SLuigi Rizzo 				break;
173268b8534bSLuigi Rizzo 			}
1733*f0ea3689SLuigi Rizzo 			priv->np_td = td; // XXX kqueue, debugging only
173468b8534bSLuigi Rizzo 
173568b8534bSLuigi Rizzo 			/* return the offset of the netmap_if object */
1736d76bf4ffSLuigi Rizzo 			nmr->nr_rx_rings = na->num_rx_rings;
1737d76bf4ffSLuigi Rizzo 			nmr->nr_tx_rings = na->num_tx_rings;
173864ae02c3SLuigi Rizzo 			nmr->nr_rx_slots = na->num_rx_desc;
173964ae02c3SLuigi Rizzo 			nmr->nr_tx_slots = na->num_tx_desc;
1740*f0ea3689SLuigi Rizzo 			error = netmap_mem_get_info(na->nm_mem, &nmr->nr_memsize, &memflags,
1741*f0ea3689SLuigi Rizzo 				&nmr->nr_arg2);
1742ce3ee1e7SLuigi Rizzo 			if (error) {
1743f9790aebSLuigi Rizzo 				netmap_adapter_put(na);
1744ce3ee1e7SLuigi Rizzo 				break;
1745ce3ee1e7SLuigi Rizzo 			}
1746ce3ee1e7SLuigi Rizzo 			if (memflags & NETMAP_MEM_PRIVATE) {
17473d819cb6SLuigi Rizzo 				*(uint32_t *)(uintptr_t)&nifp->ni_flags |= NI_PRIV_MEM;
1748ce3ee1e7SLuigi Rizzo 			}
1749*f0ea3689SLuigi Rizzo 			priv->np_txsi = (priv->np_txqlast - priv->np_txqfirst > 1) ?
1750*f0ea3689SLuigi Rizzo 				&na->tx_si : &na->tx_rings[priv->np_txqfirst].si;
1751*f0ea3689SLuigi Rizzo 			priv->np_rxsi = (priv->np_rxqlast - priv->np_rxqfirst > 1) ?
1752*f0ea3689SLuigi Rizzo 				&na->rx_si : &na->rx_rings[priv->np_rxqfirst].si;
1753*f0ea3689SLuigi Rizzo 
1754*f0ea3689SLuigi Rizzo 			if (nmr->nr_arg3) {
1755*f0ea3689SLuigi Rizzo 				D("requested %d extra buffers", nmr->nr_arg3);
1756*f0ea3689SLuigi Rizzo 				nmr->nr_arg3 = netmap_extra_alloc(na,
1757*f0ea3689SLuigi Rizzo 					&nifp->ni_bufs_head, nmr->nr_arg3);
1758*f0ea3689SLuigi Rizzo 				D("got %d extra buffers", nmr->nr_arg3);
1759*f0ea3689SLuigi Rizzo 			}
1760ce3ee1e7SLuigi Rizzo 			nmr->nr_offset = netmap_mem_if_offset(na->nm_mem, nifp);
1761ce3ee1e7SLuigi Rizzo 		} while (0);
1762ce3ee1e7SLuigi Rizzo 		NMG_UNLOCK();
176368b8534bSLuigi Rizzo 		break;
176468b8534bSLuigi Rizzo 
176568b8534bSLuigi Rizzo 	case NIOCTXSYNC:
176668b8534bSLuigi Rizzo 	case NIOCRXSYNC:
17678241616dSLuigi Rizzo 		nifp = priv->np_nifp;
17688241616dSLuigi Rizzo 
17698241616dSLuigi Rizzo 		if (nifp == NULL) {
1770506cc70cSLuigi Rizzo 			error = ENXIO;
1771506cc70cSLuigi Rizzo 			break;
1772506cc70cSLuigi Rizzo 		}
17738241616dSLuigi Rizzo 		rmb(); /* make sure following reads are not from cache */
17748241616dSLuigi Rizzo 
1775f9790aebSLuigi Rizzo 		na = priv->np_na;      /* we have a reference */
17768241616dSLuigi Rizzo 
1777f9790aebSLuigi Rizzo 		if (na == NULL) {
1778f9790aebSLuigi Rizzo 			D("Internal error: nifp != NULL && na == NULL");
17798241616dSLuigi Rizzo 			error = ENXIO;
17808241616dSLuigi Rizzo 			break;
17818241616dSLuigi Rizzo 		}
17828241616dSLuigi Rizzo 
1783f9790aebSLuigi Rizzo 		ifp = na->ifp;
1784f9790aebSLuigi Rizzo 		if (ifp == NULL) {
1785f9790aebSLuigi Rizzo 			RD(1, "the ifp is gone");
1786f9790aebSLuigi Rizzo 			error = ENXIO;
1787f9790aebSLuigi Rizzo 			break;
1788f9790aebSLuigi Rizzo 		}
1789f9790aebSLuigi Rizzo 
1790*f0ea3689SLuigi Rizzo 		if (cmd == NIOCTXSYNC) {
1791*f0ea3689SLuigi Rizzo 			krings = na->tx_rings;
1792*f0ea3689SLuigi Rizzo 			qfirst = priv->np_txqfirst;
1793*f0ea3689SLuigi Rizzo 			qlast = priv->np_txqlast;
1794*f0ea3689SLuigi Rizzo 		} else {
1795*f0ea3689SLuigi Rizzo 			krings = na->rx_rings;
1796*f0ea3689SLuigi Rizzo 			qfirst = priv->np_rxqfirst;
1797*f0ea3689SLuigi Rizzo 			qlast = priv->np_rxqlast;
179868b8534bSLuigi Rizzo 		}
179968b8534bSLuigi Rizzo 
1800*f0ea3689SLuigi Rizzo 		for (i = qfirst; i < qlast; i++) {
1801ce3ee1e7SLuigi Rizzo 			struct netmap_kring *kring = krings + i;
1802ce3ee1e7SLuigi Rizzo 			if (nm_kr_tryget(kring)) {
1803ce3ee1e7SLuigi Rizzo 				error = EBUSY;
1804ce3ee1e7SLuigi Rizzo 				goto out;
1805ce3ee1e7SLuigi Rizzo 			}
180668b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC) {
180768b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
18083c0caf6cSLuigi Rizzo 					D("pre txsync ring %d cur %d hwcur %d",
180968b8534bSLuigi Rizzo 					    i, kring->ring->cur,
181068b8534bSLuigi Rizzo 					    kring->nr_hwcur);
181117885a7bSLuigi Rizzo 				if (nm_txsync_prologue(kring) >= kring->nkr_num_slots) {
181217885a7bSLuigi Rizzo 					netmap_ring_reinit(kring);
181317885a7bSLuigi Rizzo 				} else {
1814*f0ea3689SLuigi Rizzo 					kring->nm_sync(kring, NAF_FORCE_RECLAIM);
181517885a7bSLuigi Rizzo 				}
181668b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
18173c0caf6cSLuigi Rizzo 					D("post txsync ring %d cur %d hwcur %d",
181868b8534bSLuigi Rizzo 					    i, kring->ring->cur,
181968b8534bSLuigi Rizzo 					    kring->nr_hwcur);
182068b8534bSLuigi Rizzo 			} else {
1821*f0ea3689SLuigi Rizzo 				kring->nm_sync(kring, NAF_FORCE_READ);
182268b8534bSLuigi Rizzo 				microtime(&na->rx_rings[i].ring->ts);
182368b8534bSLuigi Rizzo 			}
1824ce3ee1e7SLuigi Rizzo 			nm_kr_put(kring);
182568b8534bSLuigi Rizzo 		}
182668b8534bSLuigi Rizzo 
182768b8534bSLuigi Rizzo 		break;
182868b8534bSLuigi Rizzo 
1829f196ce38SLuigi Rizzo #ifdef __FreeBSD__
183068b8534bSLuigi Rizzo 	case BIOCIMMEDIATE:
183168b8534bSLuigi Rizzo 	case BIOCGHDRCMPLT:
183268b8534bSLuigi Rizzo 	case BIOCSHDRCMPLT:
183368b8534bSLuigi Rizzo 	case BIOCSSEESENT:
183468b8534bSLuigi Rizzo 		D("ignore BIOCIMMEDIATE/BIOCSHDRCMPLT/BIOCSHDRCMPLT/BIOCSSEESENT");
183568b8534bSLuigi Rizzo 		break;
183668b8534bSLuigi Rizzo 
1837babc7c12SLuigi Rizzo 	default:	/* allow device-specific ioctls */
183868b8534bSLuigi Rizzo 	    {
183968b8534bSLuigi Rizzo 		struct socket so;
1840ce3ee1e7SLuigi Rizzo 
184168b8534bSLuigi Rizzo 		bzero(&so, sizeof(so));
1842ce3ee1e7SLuigi Rizzo 		NMG_LOCK();
1843f9790aebSLuigi Rizzo 		error = netmap_get_na(nmr, &na, 0 /* don't create */); /* keep reference */
1844ce3ee1e7SLuigi Rizzo 		if (error) {
1845f9790aebSLuigi Rizzo 			netmap_adapter_put(na);
1846ce3ee1e7SLuigi Rizzo 			NMG_UNLOCK();
184768b8534bSLuigi Rizzo 			break;
1848ce3ee1e7SLuigi Rizzo 		}
1849f9790aebSLuigi Rizzo 		ifp = na->ifp;
185068b8534bSLuigi Rizzo 		so.so_vnet = ifp->if_vnet;
185168b8534bSLuigi Rizzo 		// so->so_proto not null.
185268b8534bSLuigi Rizzo 		error = ifioctl(&so, cmd, data, td);
1853f9790aebSLuigi Rizzo 		netmap_adapter_put(na);
1854ce3ee1e7SLuigi Rizzo 		NMG_UNLOCK();
1855babc7c12SLuigi Rizzo 		break;
185668b8534bSLuigi Rizzo 	    }
1857f196ce38SLuigi Rizzo 
1858f196ce38SLuigi Rizzo #else /* linux */
1859f196ce38SLuigi Rizzo 	default:
1860f196ce38SLuigi Rizzo 		error = EOPNOTSUPP;
1861f196ce38SLuigi Rizzo #endif /* linux */
186268b8534bSLuigi Rizzo 	}
1863ce3ee1e7SLuigi Rizzo out:
186468b8534bSLuigi Rizzo 
1865506cc70cSLuigi Rizzo 	CURVNET_RESTORE();
186668b8534bSLuigi Rizzo 	return (error);
186768b8534bSLuigi Rizzo }
186868b8534bSLuigi Rizzo 
186968b8534bSLuigi Rizzo 
187068b8534bSLuigi Rizzo /*
187168b8534bSLuigi Rizzo  * select(2) and poll(2) handlers for the "netmap" device.
187268b8534bSLuigi Rizzo  *
187368b8534bSLuigi Rizzo  * Can be called for one or more queues.
187468b8534bSLuigi Rizzo  * Return true the event mask corresponding to ready events.
187568b8534bSLuigi Rizzo  * If there are no ready events, do a selrecord on either individual
1876ce3ee1e7SLuigi Rizzo  * selinfo or on the global one.
187768b8534bSLuigi Rizzo  * Device-dependent parts (locking and sync of tx/rx rings)
187868b8534bSLuigi Rizzo  * are done through callbacks.
1879f196ce38SLuigi Rizzo  *
188001c7d25fSLuigi Rizzo  * On linux, arguments are really pwait, the poll table, and 'td' is struct file *
188101c7d25fSLuigi Rizzo  * The first one is remapped to pwait as selrecord() uses the name as an
188201c7d25fSLuigi Rizzo  * hidden argument.
188368b8534bSLuigi Rizzo  */
1884f9790aebSLuigi Rizzo int
188501c7d25fSLuigi Rizzo netmap_poll(struct cdev *dev, int events, struct thread *td)
188668b8534bSLuigi Rizzo {
188768b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
188868b8534bSLuigi Rizzo 	struct netmap_adapter *na;
188968b8534bSLuigi Rizzo 	struct ifnet *ifp;
189068b8534bSLuigi Rizzo 	struct netmap_kring *kring;
1891954dca4cSLuigi Rizzo 	u_int i, check_all_tx, check_all_rx, want_tx, want_rx, revents = 0;
189217885a7bSLuigi Rizzo 	struct mbq q;		/* packets from hw queues to host stack */
189301c7d25fSLuigi Rizzo 	void *pwait = dev;	/* linux compatibility */
1894*f0ea3689SLuigi Rizzo 	int is_kevent = 0;
189501c7d25fSLuigi Rizzo 
1896f9790aebSLuigi Rizzo 	/*
1897f9790aebSLuigi Rizzo 	 * In order to avoid nested locks, we need to "double check"
1898f9790aebSLuigi Rizzo 	 * txsync and rxsync if we decide to do a selrecord().
1899f9790aebSLuigi Rizzo 	 * retry_tx (and retry_rx, later) prevent looping forever.
1900f9790aebSLuigi Rizzo 	 */
190117885a7bSLuigi Rizzo 	int retry_tx = 1, retry_rx = 1;
1902ce3ee1e7SLuigi Rizzo 
190301c7d25fSLuigi Rizzo 	(void)pwait;
1904f9790aebSLuigi Rizzo 	mbq_init(&q);
190568b8534bSLuigi Rizzo 
1906*f0ea3689SLuigi Rizzo 	/*
1907*f0ea3689SLuigi Rizzo 	 * XXX kevent has curthread->tp_fop == NULL,
1908*f0ea3689SLuigi Rizzo 	 * so devfs_get_cdevpriv() fails. We circumvent this by passing
1909*f0ea3689SLuigi Rizzo 	 * priv as the first argument, which is also useful to avoid
1910*f0ea3689SLuigi Rizzo 	 * the selrecord() which are not necessary in that case.
1911*f0ea3689SLuigi Rizzo 	 */
1912*f0ea3689SLuigi Rizzo 	if (devfs_get_cdevpriv((void **)&priv) != 0) {
1913*f0ea3689SLuigi Rizzo 		is_kevent = 1;
1914*f0ea3689SLuigi Rizzo 		if (netmap_verbose)
1915*f0ea3689SLuigi Rizzo 			D("called from kevent");
1916*f0ea3689SLuigi Rizzo 		priv = (struct netmap_priv_d *)dev;
1917*f0ea3689SLuigi Rizzo 	}
1918*f0ea3689SLuigi Rizzo 	if (priv == NULL)
191968b8534bSLuigi Rizzo 		return POLLERR;
192068b8534bSLuigi Rizzo 
19218241616dSLuigi Rizzo 	if (priv->np_nifp == NULL) {
19228241616dSLuigi Rizzo 		D("No if registered");
19238241616dSLuigi Rizzo 		return POLLERR;
19248241616dSLuigi Rizzo 	}
19258241616dSLuigi Rizzo 	rmb(); /* make sure following reads are not from cache */
19268241616dSLuigi Rizzo 
1927f9790aebSLuigi Rizzo 	na = priv->np_na;
1928f9790aebSLuigi Rizzo 	ifp = na->ifp;
1929f9790aebSLuigi Rizzo 	// check for deleted
1930f9790aebSLuigi Rizzo 	if (ifp == NULL) {
1931f9790aebSLuigi Rizzo 		RD(1, "the ifp is gone");
1932f9790aebSLuigi Rizzo 		return POLLERR;
1933f9790aebSLuigi Rizzo 	}
1934f9790aebSLuigi Rizzo 
193568b8534bSLuigi Rizzo 	if ( (ifp->if_capenable & IFCAP_NETMAP) == 0)
193668b8534bSLuigi Rizzo 		return POLLERR;
193768b8534bSLuigi Rizzo 
193868b8534bSLuigi Rizzo 	if (netmap_verbose & 0x8000)
1939f9790aebSLuigi Rizzo 		D("device %s events 0x%x", NM_IFPNAME(ifp), events);
194068b8534bSLuigi Rizzo 	want_tx = events & (POLLOUT | POLLWRNORM);
194168b8534bSLuigi Rizzo 	want_rx = events & (POLLIN | POLLRDNORM);
194268b8534bSLuigi Rizzo 
1943091fd0abSLuigi Rizzo 
194468b8534bSLuigi Rizzo 	/*
1945f9790aebSLuigi Rizzo 	 * check_all_{tx|rx} are set if the card has more than one queue AND
1946f9790aebSLuigi Rizzo 	 * the file descriptor is bound to all of them. If so, we sleep on
1947ce3ee1e7SLuigi Rizzo 	 * the "global" selinfo, otherwise we sleep on individual selinfo
1948ce3ee1e7SLuigi Rizzo 	 * (FreeBSD only allows two selinfo's per file descriptor).
1949ce3ee1e7SLuigi Rizzo 	 * The interrupt routine in the driver wake one or the other
1950ce3ee1e7SLuigi Rizzo 	 * (or both) depending on which clients are active.
195168b8534bSLuigi Rizzo 	 *
195268b8534bSLuigi Rizzo 	 * rxsync() is only called if we run out of buffers on a POLLIN.
195368b8534bSLuigi Rizzo 	 * txsync() is called if we run out of buffers on POLLOUT, or
195468b8534bSLuigi Rizzo 	 * there are pending packets to send. The latter can be disabled
195568b8534bSLuigi Rizzo 	 * passing NETMAP_NO_TX_POLL in the NIOCREG call.
195668b8534bSLuigi Rizzo 	 */
1957*f0ea3689SLuigi Rizzo 	check_all_tx = nm_tx_si_user(priv);
1958*f0ea3689SLuigi Rizzo 	check_all_rx = nm_rx_si_user(priv);
195964ae02c3SLuigi Rizzo 
196068b8534bSLuigi Rizzo 	/*
1961f9790aebSLuigi Rizzo 	 * We start with a lock free round which is cheap if we have
1962f9790aebSLuigi Rizzo 	 * slots available. If this fails, then lock and call the sync
196368b8534bSLuigi Rizzo 	 * routines.
196468b8534bSLuigi Rizzo 	 */
1965*f0ea3689SLuigi Rizzo 	for (i = priv->np_rxqfirst; want_rx && i < priv->np_rxqlast; i++) {
196668b8534bSLuigi Rizzo 		kring = &na->rx_rings[i];
196717885a7bSLuigi Rizzo 		/* XXX compare ring->cur and kring->tail */
196817885a7bSLuigi Rizzo 		if (!nm_ring_empty(kring->ring)) {
196968b8534bSLuigi Rizzo 			revents |= want_rx;
197068b8534bSLuigi Rizzo 			want_rx = 0;	/* also breaks the loop */
197168b8534bSLuigi Rizzo 		}
197268b8534bSLuigi Rizzo 	}
1973*f0ea3689SLuigi Rizzo 	for (i = priv->np_txqfirst; want_tx && i < priv->np_txqlast; i++) {
197468b8534bSLuigi Rizzo 		kring = &na->tx_rings[i];
197517885a7bSLuigi Rizzo 		/* XXX compare ring->cur and kring->tail */
197617885a7bSLuigi Rizzo 		if (!nm_ring_empty(kring->ring)) {
197768b8534bSLuigi Rizzo 			revents |= want_tx;
197868b8534bSLuigi Rizzo 			want_tx = 0;	/* also breaks the loop */
197968b8534bSLuigi Rizzo 		}
198068b8534bSLuigi Rizzo 	}
198168b8534bSLuigi Rizzo 
198268b8534bSLuigi Rizzo 	/*
198317885a7bSLuigi Rizzo 	 * If we want to push packets out (priv->np_txpoll) or
198417885a7bSLuigi Rizzo 	 * want_tx is still set, we must issue txsync calls
198517885a7bSLuigi Rizzo 	 * (on all rings, to avoid that the tx rings stall).
1986f9790aebSLuigi Rizzo 	 * XXX should also check cur != hwcur on the tx rings.
1987f9790aebSLuigi Rizzo 	 * Fortunately, normal tx mode has np_txpoll set.
198868b8534bSLuigi Rizzo 	 */
198968b8534bSLuigi Rizzo 	if (priv->np_txpoll || want_tx) {
199017885a7bSLuigi Rizzo 		/*
199117885a7bSLuigi Rizzo 		 * The first round checks if anyone is ready, if not
199217885a7bSLuigi Rizzo 		 * do a selrecord and another round to handle races.
199317885a7bSLuigi Rizzo 		 * want_tx goes to 0 if any space is found, and is
199417885a7bSLuigi Rizzo 		 * used to skip rings with no pending transmissions.
1995ce3ee1e7SLuigi Rizzo 		 */
1996091fd0abSLuigi Rizzo flush_tx:
1997*f0ea3689SLuigi Rizzo 		for (i = priv->np_txqfirst; i < priv->np_txqlast; i++) {
199817885a7bSLuigi Rizzo 			int found = 0;
199917885a7bSLuigi Rizzo 
200068b8534bSLuigi Rizzo 			kring = &na->tx_rings[i];
200168b8534bSLuigi Rizzo 			if (!want_tx && kring->ring->cur == kring->nr_hwcur)
200268b8534bSLuigi Rizzo 				continue;
200317885a7bSLuigi Rizzo 			/* only one thread does txsync */
2004ce3ee1e7SLuigi Rizzo 			if (nm_kr_tryget(kring)) {
200517885a7bSLuigi Rizzo 				D("%p lost race on txring %d, ok", priv, i);
200617885a7bSLuigi Rizzo 				continue;
200768b8534bSLuigi Rizzo 			}
200817885a7bSLuigi Rizzo 			if (nm_txsync_prologue(kring) >= kring->nkr_num_slots) {
200917885a7bSLuigi Rizzo 				netmap_ring_reinit(kring);
201017885a7bSLuigi Rizzo 				revents |= POLLERR;
201117885a7bSLuigi Rizzo 			} else {
2012*f0ea3689SLuigi Rizzo 				if (kring->nm_sync(kring, 0))
201368b8534bSLuigi Rizzo 					revents |= POLLERR;
201417885a7bSLuigi Rizzo 			}
201568b8534bSLuigi Rizzo 
201617885a7bSLuigi Rizzo 			/*
201717885a7bSLuigi Rizzo 			 * If we found new slots, notify potential
201817885a7bSLuigi Rizzo 			 * listeners on the same ring.
201917885a7bSLuigi Rizzo 			 * Since we just did a txsync, look at the copies
202017885a7bSLuigi Rizzo 			 * of cur,tail in the kring.
2021f9790aebSLuigi Rizzo 			 */
202217885a7bSLuigi Rizzo 			found = kring->rcur != kring->rtail;
202317885a7bSLuigi Rizzo 			nm_kr_put(kring);
202417885a7bSLuigi Rizzo 			if (found) { /* notify other listeners */
202568b8534bSLuigi Rizzo 				revents |= want_tx;
202668b8534bSLuigi Rizzo 				want_tx = 0;
2027*f0ea3689SLuigi Rizzo 				na->nm_notify(na, i, NR_TX, 0);
202868b8534bSLuigi Rizzo 			}
2029ce3ee1e7SLuigi Rizzo 		}
2030*f0ea3689SLuigi Rizzo 		if (want_tx && retry_tx && !is_kevent) {
2031954dca4cSLuigi Rizzo 			selrecord(td, check_all_tx ?
2032*f0ea3689SLuigi Rizzo 			    &na->tx_si : &na->tx_rings[priv->np_txqfirst].si);
2033ce3ee1e7SLuigi Rizzo 			retry_tx = 0;
2034ce3ee1e7SLuigi Rizzo 			goto flush_tx;
203568b8534bSLuigi Rizzo 		}
203668b8534bSLuigi Rizzo 	}
203768b8534bSLuigi Rizzo 
203868b8534bSLuigi Rizzo 	/*
203917885a7bSLuigi Rizzo 	 * If want_rx is still set scan receive rings.
204068b8534bSLuigi Rizzo 	 * Do it on all rings because otherwise we starve.
204168b8534bSLuigi Rizzo 	 */
204268b8534bSLuigi Rizzo 	if (want_rx) {
204317885a7bSLuigi Rizzo 		int send_down = 0; /* transparent mode */
204417885a7bSLuigi Rizzo 		/* two rounds here to for race avoidance */
2045ce3ee1e7SLuigi Rizzo do_retry_rx:
2046*f0ea3689SLuigi Rizzo 		for (i = priv->np_rxqfirst; i < priv->np_rxqlast; i++) {
204717885a7bSLuigi Rizzo 			int found = 0;
204817885a7bSLuigi Rizzo 
204968b8534bSLuigi Rizzo 			kring = &na->rx_rings[i];
2050ce3ee1e7SLuigi Rizzo 
2051ce3ee1e7SLuigi Rizzo 			if (nm_kr_tryget(kring)) {
205217885a7bSLuigi Rizzo 				D("%p lost race on rxring %d, ok", priv, i);
205317885a7bSLuigi Rizzo 				continue;
205468b8534bSLuigi Rizzo 			}
2055ce3ee1e7SLuigi Rizzo 
205617885a7bSLuigi Rizzo 			/*
205717885a7bSLuigi Rizzo 			 * transparent mode support: collect packets
205817885a7bSLuigi Rizzo 			 * from the rxring(s).
205917885a7bSLuigi Rizzo 			 * XXX NR_FORWARD should only be read on
2060ce3ee1e7SLuigi Rizzo 			 * physical or NIC ports
2061ce3ee1e7SLuigi Rizzo 			 */
2062091fd0abSLuigi Rizzo 			if (netmap_fwd ||kring->ring->flags & NR_FORWARD) {
2063091fd0abSLuigi Rizzo 				ND(10, "forwarding some buffers up %d to %d",
2064091fd0abSLuigi Rizzo 				    kring->nr_hwcur, kring->ring->cur);
2065091fd0abSLuigi Rizzo 				netmap_grab_packets(kring, &q, netmap_fwd);
2066091fd0abSLuigi Rizzo 			}
206768b8534bSLuigi Rizzo 
2068*f0ea3689SLuigi Rizzo 			if (kring->nm_sync(kring, 0))
206968b8534bSLuigi Rizzo 				revents |= POLLERR;
20705819da83SLuigi Rizzo 			if (netmap_no_timestamp == 0 ||
20715819da83SLuigi Rizzo 					kring->ring->flags & NR_TIMESTAMP) {
207268b8534bSLuigi Rizzo 				microtime(&kring->ring->ts);
20735819da83SLuigi Rizzo 			}
207417885a7bSLuigi Rizzo 			/* after an rxsync we can use kring->rcur, rtail */
207517885a7bSLuigi Rizzo 			found = kring->rcur != kring->rtail;
207617885a7bSLuigi Rizzo 			nm_kr_put(kring);
207717885a7bSLuigi Rizzo 			if (found) {
207868b8534bSLuigi Rizzo 				revents |= want_rx;
2079ce3ee1e7SLuigi Rizzo 				retry_rx = 0;
2080*f0ea3689SLuigi Rizzo 				na->nm_notify(na, i, NR_RX, 0);
208168b8534bSLuigi Rizzo 			}
208268b8534bSLuigi Rizzo 		}
208317885a7bSLuigi Rizzo 
208417885a7bSLuigi Rizzo 		/* transparent mode XXX only during first pass ? */
2085*f0ea3689SLuigi Rizzo 		if (na->na_flags & NAF_HOST_RINGS) {
2086*f0ea3689SLuigi Rizzo 			kring = &na->rx_rings[na->num_rx_rings];
208717885a7bSLuigi Rizzo 			if (check_all_rx
208817885a7bSLuigi Rizzo 			    && (netmap_fwd || kring->ring->flags & NR_FORWARD)) {
208917885a7bSLuigi Rizzo 				/* XXX fix to use kring fields */
209017885a7bSLuigi Rizzo 				if (nm_ring_empty(kring->ring))
209117885a7bSLuigi Rizzo 					send_down = netmap_rxsync_from_host(na, td, dev);
209217885a7bSLuigi Rizzo 				if (!nm_ring_empty(kring->ring))
209317885a7bSLuigi Rizzo 					revents |= want_rx;
209417885a7bSLuigi Rizzo 			}
2095*f0ea3689SLuigi Rizzo 		}
209617885a7bSLuigi Rizzo 
2097*f0ea3689SLuigi Rizzo 		if (retry_rx && !is_kevent)
2098954dca4cSLuigi Rizzo 			selrecord(td, check_all_rx ?
2099*f0ea3689SLuigi Rizzo 			    &na->rx_si : &na->rx_rings[priv->np_rxqfirst].si);
210017885a7bSLuigi Rizzo 		if (send_down > 0 || retry_rx) {
210117885a7bSLuigi Rizzo 			retry_rx = 0;
210217885a7bSLuigi Rizzo 			if (send_down)
210317885a7bSLuigi Rizzo 				goto flush_tx; /* and retry_rx */
210417885a7bSLuigi Rizzo 			else
2105ce3ee1e7SLuigi Rizzo 				goto do_retry_rx;
2106ce3ee1e7SLuigi Rizzo 		}
210768b8534bSLuigi Rizzo 	}
2108091fd0abSLuigi Rizzo 
210917885a7bSLuigi Rizzo 	/*
211017885a7bSLuigi Rizzo 	 * Transparent mode: marked bufs on rx rings between
211117885a7bSLuigi Rizzo 	 * kring->nr_hwcur and ring->head
211217885a7bSLuigi Rizzo 	 * are passed to the other endpoint.
211317885a7bSLuigi Rizzo 	 *
211417885a7bSLuigi Rizzo 	 * In this mode we also scan the sw rxring, which in
211517885a7bSLuigi Rizzo 	 * turn passes packets up.
211617885a7bSLuigi Rizzo 	 *
211717885a7bSLuigi Rizzo 	 * XXX Transparent mode at the moment requires to bind all
211817885a7bSLuigi Rizzo  	 * rings to a single file descriptor.
2119ce3ee1e7SLuigi Rizzo 	 */
2120091fd0abSLuigi Rizzo 
2121091fd0abSLuigi Rizzo 	if (q.head)
2122f9790aebSLuigi Rizzo 		netmap_send_up(na->ifp, &q);
212368b8534bSLuigi Rizzo 
212468b8534bSLuigi Rizzo 	return (revents);
212568b8534bSLuigi Rizzo }
212668b8534bSLuigi Rizzo 
212717885a7bSLuigi Rizzo 
212817885a7bSLuigi Rizzo /*-------------------- driver support routines -------------------*/
212968b8534bSLuigi Rizzo 
2130f9790aebSLuigi Rizzo static int netmap_hw_krings_create(struct netmap_adapter *);
2131f9790aebSLuigi Rizzo 
2132f9790aebSLuigi Rizzo static int
213317885a7bSLuigi Rizzo netmap_notify(struct netmap_adapter *na, u_int n_ring,
213417885a7bSLuigi Rizzo 	enum txrx tx, int flags)
2135f9790aebSLuigi Rizzo {
2136f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
2137f9790aebSLuigi Rizzo 
2138f9790aebSLuigi Rizzo 	if (tx == NR_TX) {
2139f9790aebSLuigi Rizzo 		kring = na->tx_rings + n_ring;
2140*f0ea3689SLuigi Rizzo 		OS_selwakeup(&kring->si, PI_NET);
2141*f0ea3689SLuigi Rizzo 		if (na->tx_si_users > 0)
2142*f0ea3689SLuigi Rizzo 			OS_selwakeup(&na->tx_si, PI_NET);
2143f9790aebSLuigi Rizzo 	} else {
2144f9790aebSLuigi Rizzo 		kring = na->rx_rings + n_ring;
2145*f0ea3689SLuigi Rizzo 		OS_selwakeup(&kring->si, PI_NET);
2146*f0ea3689SLuigi Rizzo 		if (na->rx_si_users > 0)
2147*f0ea3689SLuigi Rizzo 			OS_selwakeup(&na->rx_si, PI_NET);
2148f9790aebSLuigi Rizzo 	}
2149f9790aebSLuigi Rizzo 	return 0;
2150f9790aebSLuigi Rizzo }
2151f9790aebSLuigi Rizzo 
2152f9790aebSLuigi Rizzo 
2153f9790aebSLuigi Rizzo // XXX check handling of failures
2154f9790aebSLuigi Rizzo int
2155f9790aebSLuigi Rizzo netmap_attach_common(struct netmap_adapter *na)
2156f9790aebSLuigi Rizzo {
2157f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
2158f9790aebSLuigi Rizzo 
2159f9790aebSLuigi Rizzo 	if (na->num_tx_rings == 0 || na->num_rx_rings == 0) {
2160f9790aebSLuigi Rizzo 		D("%s: invalid rings tx %d rx %d",
2161f9790aebSLuigi Rizzo 			ifp->if_xname, na->num_tx_rings, na->num_rx_rings);
2162f9790aebSLuigi Rizzo 		return EINVAL;
2163f9790aebSLuigi Rizzo 	}
2164f9790aebSLuigi Rizzo 	WNA(ifp) = na;
216517885a7bSLuigi Rizzo 
216617885a7bSLuigi Rizzo 	/* the following is only needed for na that use the host port.
216717885a7bSLuigi Rizzo 	 * XXX do we have something similar for linux ?
216817885a7bSLuigi Rizzo 	 */
216917885a7bSLuigi Rizzo #ifdef __FreeBSD__
217017885a7bSLuigi Rizzo 	na->if_input = ifp->if_input; /* for netmap_send_up */
217117885a7bSLuigi Rizzo #endif /* __FreeBSD__ */
217217885a7bSLuigi Rizzo 
2173f9790aebSLuigi Rizzo 	NETMAP_SET_CAPABLE(ifp);
2174f9790aebSLuigi Rizzo 	if (na->nm_krings_create == NULL) {
2175f9790aebSLuigi Rizzo 		na->nm_krings_create = netmap_hw_krings_create;
217617885a7bSLuigi Rizzo 		na->nm_krings_delete = netmap_hw_krings_delete;
2177f9790aebSLuigi Rizzo 	}
2178f9790aebSLuigi Rizzo 	if (na->nm_notify == NULL)
2179f9790aebSLuigi Rizzo 		na->nm_notify = netmap_notify;
2180f9790aebSLuigi Rizzo 	na->active_fds = 0;
2181f9790aebSLuigi Rizzo 
2182f9790aebSLuigi Rizzo 	if (na->nm_mem == NULL)
2183f9790aebSLuigi Rizzo 		na->nm_mem = &nm_mem;
2184f9790aebSLuigi Rizzo 	return 0;
2185f9790aebSLuigi Rizzo }
2186f9790aebSLuigi Rizzo 
2187f9790aebSLuigi Rizzo 
2188f9790aebSLuigi Rizzo void
2189f9790aebSLuigi Rizzo netmap_detach_common(struct netmap_adapter *na)
2190f9790aebSLuigi Rizzo {
2191f9790aebSLuigi Rizzo 	if (na->ifp)
2192f9790aebSLuigi Rizzo 		WNA(na->ifp) = NULL; /* XXX do we need this? */
2193f9790aebSLuigi Rizzo 
2194f9790aebSLuigi Rizzo 	if (na->tx_rings) { /* XXX should not happen */
2195f9790aebSLuigi Rizzo 		D("freeing leftover tx_rings");
2196f9790aebSLuigi Rizzo 		na->nm_krings_delete(na);
2197f9790aebSLuigi Rizzo 	}
2198*f0ea3689SLuigi Rizzo 	netmap_pipe_dealloc(na);
2199f9790aebSLuigi Rizzo 	if (na->na_flags & NAF_MEM_OWNER)
2200f9790aebSLuigi Rizzo 		netmap_mem_private_delete(na->nm_mem);
2201f9790aebSLuigi Rizzo 	bzero(na, sizeof(*na));
2202f9790aebSLuigi Rizzo 	free(na, M_DEVBUF);
2203f9790aebSLuigi Rizzo }
2204f9790aebSLuigi Rizzo 
2205f18be576SLuigi Rizzo 
220668b8534bSLuigi Rizzo /*
220768b8534bSLuigi Rizzo  * Initialize a ``netmap_adapter`` object created by driver on attach.
220868b8534bSLuigi Rizzo  * We allocate a block of memory with room for a struct netmap_adapter
220968b8534bSLuigi Rizzo  * plus two sets of N+2 struct netmap_kring (where N is the number
221068b8534bSLuigi Rizzo  * of hardware rings):
221168b8534bSLuigi Rizzo  * krings	0..N-1	are for the hardware queues.
221268b8534bSLuigi Rizzo  * kring	N	is for the host stack queue
221317885a7bSLuigi Rizzo  * kring	N+1	is only used for the selinfo for all queues. // XXX still true ?
221468b8534bSLuigi Rizzo  * Return 0 on success, ENOMEM otherwise.
221568b8534bSLuigi Rizzo  */
221668b8534bSLuigi Rizzo int
2217f9790aebSLuigi Rizzo netmap_attach(struct netmap_adapter *arg)
221868b8534bSLuigi Rizzo {
2219f9790aebSLuigi Rizzo 	struct netmap_hw_adapter *hwna = NULL;
2220f9790aebSLuigi Rizzo 	// XXX when is arg == NULL ?
2221ae10d1afSLuigi Rizzo 	struct ifnet *ifp = arg ? arg->ifp : NULL;
222268b8534bSLuigi Rizzo 
2223ae10d1afSLuigi Rizzo 	if (arg == NULL || ifp == NULL)
2224ae10d1afSLuigi Rizzo 		goto fail;
2225f9790aebSLuigi Rizzo 	hwna = malloc(sizeof(*hwna), M_DEVBUF, M_NOWAIT | M_ZERO);
2226f9790aebSLuigi Rizzo 	if (hwna == NULL)
2227ae10d1afSLuigi Rizzo 		goto fail;
2228f9790aebSLuigi Rizzo 	hwna->up = *arg;
2229*f0ea3689SLuigi Rizzo 	hwna->up.na_flags |= NAF_HOST_RINGS;
2230f9790aebSLuigi Rizzo 	if (netmap_attach_common(&hwna->up)) {
2231f9790aebSLuigi Rizzo 		free(hwna, M_DEVBUF);
2232f9790aebSLuigi Rizzo 		goto fail;
2233f9790aebSLuigi Rizzo 	}
2234f9790aebSLuigi Rizzo 	netmap_adapter_get(&hwna->up);
2235f9790aebSLuigi Rizzo 
223664ae02c3SLuigi Rizzo #ifdef linux
2237f18be576SLuigi Rizzo 	if (ifp->netdev_ops) {
2238f18be576SLuigi Rizzo 		/* prepare a clone of the netdev ops */
2239f18be576SLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28)
2240f9790aebSLuigi Rizzo 		hwna->nm_ndo.ndo_start_xmit = ifp->netdev_ops;
2241f18be576SLuigi Rizzo #else
2242f9790aebSLuigi Rizzo 		hwna->nm_ndo = *ifp->netdev_ops;
2243f18be576SLuigi Rizzo #endif
2244f18be576SLuigi Rizzo 	}
2245f9790aebSLuigi Rizzo 	hwna->nm_ndo.ndo_start_xmit = linux_netmap_start_xmit;
2246ce3ee1e7SLuigi Rizzo #endif /* linux */
2247f9790aebSLuigi Rizzo 
2248f9790aebSLuigi Rizzo 	D("success for %s", NM_IFPNAME(ifp));
2249ae10d1afSLuigi Rizzo 	return 0;
225068b8534bSLuigi Rizzo 
2251ae10d1afSLuigi Rizzo fail:
2252f9790aebSLuigi Rizzo 	D("fail, arg %p ifp %p na %p", arg, ifp, hwna);
2253849bec0eSLuigi Rizzo 	netmap_detach(ifp);
2254f9790aebSLuigi Rizzo 	return (hwna ? EINVAL : ENOMEM);
225568b8534bSLuigi Rizzo }
225668b8534bSLuigi Rizzo 
225768b8534bSLuigi Rizzo 
2258f9790aebSLuigi Rizzo void
2259f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_get)(struct netmap_adapter *na)
2260f9790aebSLuigi Rizzo {
2261f9790aebSLuigi Rizzo 	if (!na) {
2262f9790aebSLuigi Rizzo 		return;
2263f9790aebSLuigi Rizzo 	}
2264f9790aebSLuigi Rizzo 
2265f9790aebSLuigi Rizzo 	refcount_acquire(&na->na_refcount);
2266f9790aebSLuigi Rizzo }
2267f9790aebSLuigi Rizzo 
2268f9790aebSLuigi Rizzo 
2269f9790aebSLuigi Rizzo /* returns 1 iff the netmap_adapter is destroyed */
2270f9790aebSLuigi Rizzo int
2271f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_put)(struct netmap_adapter *na)
2272f9790aebSLuigi Rizzo {
2273f9790aebSLuigi Rizzo 	if (!na)
2274f9790aebSLuigi Rizzo 		return 1;
2275f9790aebSLuigi Rizzo 
2276f9790aebSLuigi Rizzo 	if (!refcount_release(&na->na_refcount))
2277f9790aebSLuigi Rizzo 		return 0;
2278f9790aebSLuigi Rizzo 
2279f9790aebSLuigi Rizzo 	if (na->nm_dtor)
2280f9790aebSLuigi Rizzo 		na->nm_dtor(na);
2281f9790aebSLuigi Rizzo 
2282f9790aebSLuigi Rizzo 	netmap_detach_common(na);
2283f9790aebSLuigi Rizzo 
2284f9790aebSLuigi Rizzo 	return 1;
2285f9790aebSLuigi Rizzo }
2286f9790aebSLuigi Rizzo 
2287f9790aebSLuigi Rizzo int
2288f9790aebSLuigi Rizzo netmap_hw_krings_create(struct netmap_adapter *na)
2289f9790aebSLuigi Rizzo {
2290*f0ea3689SLuigi Rizzo 	int ret = netmap_krings_create(na, 0);
229117885a7bSLuigi Rizzo 	if (ret == 0) {
229217885a7bSLuigi Rizzo 		/* initialize the mbq for the sw rx ring */
229317885a7bSLuigi Rizzo 		mbq_safe_init(&na->rx_rings[na->num_rx_rings].rx_queue);
229417885a7bSLuigi Rizzo 		ND("initialized sw rx queue %d", na->num_rx_rings);
229517885a7bSLuigi Rizzo 	}
229617885a7bSLuigi Rizzo 	return ret;
2297f9790aebSLuigi Rizzo }
2298f9790aebSLuigi Rizzo 
2299f9790aebSLuigi Rizzo 
2300f9790aebSLuigi Rizzo 
230168b8534bSLuigi Rizzo /*
230268b8534bSLuigi Rizzo  * Free the allocated memory linked to the given ``netmap_adapter``
230368b8534bSLuigi Rizzo  * object.
230468b8534bSLuigi Rizzo  */
230568b8534bSLuigi Rizzo void
230668b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp)
230768b8534bSLuigi Rizzo {
230868b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
230968b8534bSLuigi Rizzo 
231068b8534bSLuigi Rizzo 	if (!na)
231168b8534bSLuigi Rizzo 		return;
231268b8534bSLuigi Rizzo 
2313f9790aebSLuigi Rizzo 	NMG_LOCK();
2314f9790aebSLuigi Rizzo 	netmap_disable_all_rings(ifp);
2315fb25194fSLuigi Rizzo 	if (!netmap_adapter_put(na)) {
2316fb25194fSLuigi Rizzo 		/* someone is still using the adapter,
2317fb25194fSLuigi Rizzo 		 * tell them that the interface is gone
2318fb25194fSLuigi Rizzo 		 */
2319f9790aebSLuigi Rizzo 		na->ifp = NULL;
2320fb25194fSLuigi Rizzo 		/* give them a chance to notice */
2321f9790aebSLuigi Rizzo 		netmap_enable_all_rings(ifp);
2322fb25194fSLuigi Rizzo 	}
2323f9790aebSLuigi Rizzo 	NMG_UNLOCK();
2324ae10d1afSLuigi Rizzo }
2325f18be576SLuigi Rizzo 
2326f18be576SLuigi Rizzo 
232768b8534bSLuigi Rizzo /*
232802ad4083SLuigi Rizzo  * Intercept packets from the network stack and pass them
232902ad4083SLuigi Rizzo  * to netmap as incoming packets on the 'software' ring.
233017885a7bSLuigi Rizzo  *
233117885a7bSLuigi Rizzo  * We only store packets in a bounded mbq and then copy them
233217885a7bSLuigi Rizzo  * in the relevant rxsync routine.
233317885a7bSLuigi Rizzo  *
2334ce3ee1e7SLuigi Rizzo  * We rely on the OS to make sure that the ifp and na do not go
2335ce3ee1e7SLuigi Rizzo  * away (typically the caller checks for IFF_DRV_RUNNING or the like).
2336ce3ee1e7SLuigi Rizzo  * In nm_register() or whenever there is a reinitialization,
2337f9790aebSLuigi Rizzo  * we make sure to make the mode change visible here.
233868b8534bSLuigi Rizzo  */
233968b8534bSLuigi Rizzo int
2340ce3ee1e7SLuigi Rizzo netmap_transmit(struct ifnet *ifp, struct mbuf *m)
234168b8534bSLuigi Rizzo {
234268b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
2343ce3ee1e7SLuigi Rizzo 	struct netmap_kring *kring;
234417885a7bSLuigi Rizzo 	u_int len = MBUF_LEN(m);
234517885a7bSLuigi Rizzo 	u_int error = ENOBUFS;
234617885a7bSLuigi Rizzo 	struct mbq *q;
234717885a7bSLuigi Rizzo 	int space;
234868b8534bSLuigi Rizzo 
2349ce3ee1e7SLuigi Rizzo 	// XXX [Linux] we do not need this lock
2350ce3ee1e7SLuigi Rizzo 	// if we follow the down/configure/up protocol -gl
2351ce3ee1e7SLuigi Rizzo 	// mtx_lock(&na->core_lock);
235217885a7bSLuigi Rizzo 
2353ce3ee1e7SLuigi Rizzo 	if ( (ifp->if_capenable & IFCAP_NETMAP) == 0) {
235417885a7bSLuigi Rizzo 		D("%s not in netmap mode anymore", NM_IFPNAME(ifp));
2355ce3ee1e7SLuigi Rizzo 		error = ENXIO;
2356ce3ee1e7SLuigi Rizzo 		goto done;
2357ce3ee1e7SLuigi Rizzo 	}
2358ce3ee1e7SLuigi Rizzo 
2359ce3ee1e7SLuigi Rizzo 	kring = &na->rx_rings[na->num_rx_rings];
236017885a7bSLuigi Rizzo 	q = &kring->rx_queue;
236117885a7bSLuigi Rizzo 
2362ce3ee1e7SLuigi Rizzo 	// XXX reconsider long packets if we handle fragments
2363ce3ee1e7SLuigi Rizzo 	if (len > NETMAP_BDG_BUF_SIZE(na->nm_mem)) { /* too long for us */
2364f9790aebSLuigi Rizzo 		D("%s from_host, drop packet size %d > %d", NM_IFPNAME(ifp),
2365ce3ee1e7SLuigi Rizzo 			len, NETMAP_BDG_BUF_SIZE(na->nm_mem));
2366ce3ee1e7SLuigi Rizzo 		goto done;
2367849bec0eSLuigi Rizzo 	}
236817885a7bSLuigi Rizzo 
236917885a7bSLuigi Rizzo 	/* protect against rxsync_from_host(), netmap_sw_to_nic()
237017885a7bSLuigi Rizzo 	 * and maybe other instances of netmap_transmit (the latter
237117885a7bSLuigi Rizzo 	 * not possible on Linux).
237217885a7bSLuigi Rizzo 	 * Also avoid overflowing the queue.
2373ce3ee1e7SLuigi Rizzo 	 */
237417885a7bSLuigi Rizzo 	mtx_lock(&q->lock);
237517885a7bSLuigi Rizzo 
237617885a7bSLuigi Rizzo         space = kring->nr_hwtail - kring->nr_hwcur;
237717885a7bSLuigi Rizzo         if (space < 0)
237817885a7bSLuigi Rizzo                 space += kring->nkr_num_slots;
237917885a7bSLuigi Rizzo 	if (space + mbq_len(q) >= kring->nkr_num_slots - 1) { // XXX
238017885a7bSLuigi Rizzo 		RD(10, "%s full hwcur %d hwtail %d qlen %d len %d m %p",
238117885a7bSLuigi Rizzo 			 NM_IFPNAME(ifp), kring->nr_hwcur, kring->nr_hwtail, mbq_len(q),
238217885a7bSLuigi Rizzo 			len, m);
2383ce3ee1e7SLuigi Rizzo 	} else {
238417885a7bSLuigi Rizzo 		mbq_enqueue(q, m);
238517885a7bSLuigi Rizzo 		ND(10, "%s %d bufs in queue len %d m %p",
238617885a7bSLuigi Rizzo 			NM_IFPNAME(ifp), mbq_len(q), len, m);
238717885a7bSLuigi Rizzo 		/* notify outside the lock */
238817885a7bSLuigi Rizzo 		m = NULL;
238968b8534bSLuigi Rizzo 		error = 0;
2390ce3ee1e7SLuigi Rizzo 	}
239117885a7bSLuigi Rizzo 	mtx_unlock(&q->lock);
2392ce3ee1e7SLuigi Rizzo 
239368b8534bSLuigi Rizzo done:
239417885a7bSLuigi Rizzo 	if (m)
239568b8534bSLuigi Rizzo 		m_freem(m);
239617885a7bSLuigi Rizzo 	/* unconditionally wake up listeners */
239717885a7bSLuigi Rizzo 	na->nm_notify(na, na->num_rx_rings, NR_RX, 0);
239868b8534bSLuigi Rizzo 
239968b8534bSLuigi Rizzo 	return (error);
240068b8534bSLuigi Rizzo }
240168b8534bSLuigi Rizzo 
240268b8534bSLuigi Rizzo 
240368b8534bSLuigi Rizzo /*
240468b8534bSLuigi Rizzo  * netmap_reset() is called by the driver routines when reinitializing
240568b8534bSLuigi Rizzo  * a ring. The driver is in charge of locking to protect the kring.
2406f9790aebSLuigi Rizzo  * If native netmap mode is not set just return NULL.
240768b8534bSLuigi Rizzo  */
240868b8534bSLuigi Rizzo struct netmap_slot *
2409ce3ee1e7SLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, u_int n,
241068b8534bSLuigi Rizzo 	u_int new_cur)
241168b8534bSLuigi Rizzo {
241268b8534bSLuigi Rizzo 	struct netmap_kring *kring;
2413506cc70cSLuigi Rizzo 	int new_hwofs, lim;
241468b8534bSLuigi Rizzo 
2415ce3ee1e7SLuigi Rizzo 	if (na == NULL) {
2416ce3ee1e7SLuigi Rizzo 		D("NULL na, should not happen");
241768b8534bSLuigi Rizzo 		return NULL;	/* no netmap support here */
2418ce3ee1e7SLuigi Rizzo 	}
2419ce3ee1e7SLuigi Rizzo 	if (!(na->ifp->if_capenable & IFCAP_NETMAP)) {
24205864b3a5SLuigi Rizzo 		ND("interface not in netmap mode");
242168b8534bSLuigi Rizzo 		return NULL;	/* nothing to reinitialize */
2422ce3ee1e7SLuigi Rizzo 	}
242368b8534bSLuigi Rizzo 
2424ce3ee1e7SLuigi Rizzo 	/* XXX note- in the new scheme, we are not guaranteed to be
2425ce3ee1e7SLuigi Rizzo 	 * under lock (e.g. when called on a device reset).
2426ce3ee1e7SLuigi Rizzo 	 * In this case, we should set a flag and do not trust too
2427ce3ee1e7SLuigi Rizzo 	 * much the values. In practice: TODO
2428ce3ee1e7SLuigi Rizzo 	 * - set a RESET flag somewhere in the kring
2429ce3ee1e7SLuigi Rizzo 	 * - do the processing in a conservative way
2430ce3ee1e7SLuigi Rizzo 	 * - let the *sync() fixup at the end.
2431ce3ee1e7SLuigi Rizzo 	 */
243264ae02c3SLuigi Rizzo 	if (tx == NR_TX) {
24338241616dSLuigi Rizzo 		if (n >= na->num_tx_rings)
24348241616dSLuigi Rizzo 			return NULL;
243564ae02c3SLuigi Rizzo 		kring = na->tx_rings + n;
243617885a7bSLuigi Rizzo 		// XXX check whether we should use hwcur or rcur
2437506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur - new_cur;
243864ae02c3SLuigi Rizzo 	} else {
24398241616dSLuigi Rizzo 		if (n >= na->num_rx_rings)
24408241616dSLuigi Rizzo 			return NULL;
244164ae02c3SLuigi Rizzo 		kring = na->rx_rings + n;
244217885a7bSLuigi Rizzo 		new_hwofs = kring->nr_hwtail - new_cur;
244364ae02c3SLuigi Rizzo 	}
244464ae02c3SLuigi Rizzo 	lim = kring->nkr_num_slots - 1;
2445506cc70cSLuigi Rizzo 	if (new_hwofs > lim)
2446506cc70cSLuigi Rizzo 		new_hwofs -= lim + 1;
2447506cc70cSLuigi Rizzo 
2448ce3ee1e7SLuigi Rizzo 	/* Always set the new offset value and realign the ring. */
244917885a7bSLuigi Rizzo 	if (netmap_verbose)
245017885a7bSLuigi Rizzo 	    D("%s %s%d hwofs %d -> %d, hwtail %d -> %d",
245117885a7bSLuigi Rizzo 		NM_IFPNAME(na->ifp),
245217885a7bSLuigi Rizzo 		tx == NR_TX ? "TX" : "RX", n,
2453ce3ee1e7SLuigi Rizzo 		kring->nkr_hwofs, new_hwofs,
245417885a7bSLuigi Rizzo 		kring->nr_hwtail,
245517885a7bSLuigi Rizzo 		tx == NR_TX ? lim : kring->nr_hwtail);
2456506cc70cSLuigi Rizzo 	kring->nkr_hwofs = new_hwofs;
245717885a7bSLuigi Rizzo 	if (tx == NR_TX) {
245817885a7bSLuigi Rizzo 		kring->nr_hwtail = kring->nr_hwcur + lim;
245917885a7bSLuigi Rizzo 		if (kring->nr_hwtail > lim)
246017885a7bSLuigi Rizzo 			kring->nr_hwtail -= lim + 1;
246117885a7bSLuigi Rizzo 	}
2462506cc70cSLuigi Rizzo 
2463f196ce38SLuigi Rizzo #if 0 // def linux
2464f196ce38SLuigi Rizzo 	/* XXX check that the mappings are correct */
2465f196ce38SLuigi Rizzo 	/* need ring_nr, adapter->pdev, direction */
2466f196ce38SLuigi Rizzo 	buffer_info->dma = dma_map_single(&pdev->dev, addr, adapter->rx_buffer_len, DMA_FROM_DEVICE);
2467f196ce38SLuigi Rizzo 	if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
2468f196ce38SLuigi Rizzo 		D("error mapping rx netmap buffer %d", i);
2469f196ce38SLuigi Rizzo 		// XXX fix error handling
2470f196ce38SLuigi Rizzo 	}
2471f196ce38SLuigi Rizzo 
2472f196ce38SLuigi Rizzo #endif /* linux */
247368b8534bSLuigi Rizzo 	/*
2474ce3ee1e7SLuigi Rizzo 	 * Wakeup on the individual and global selwait
2475506cc70cSLuigi Rizzo 	 * We do the wakeup here, but the ring is not yet reconfigured.
2476506cc70cSLuigi Rizzo 	 * However, we are under lock so there are no races.
247768b8534bSLuigi Rizzo 	 */
2478*f0ea3689SLuigi Rizzo 	na->nm_notify(na, n, tx, 0);
247968b8534bSLuigi Rizzo 	return kring->ring->slot;
248068b8534bSLuigi Rizzo }
248168b8534bSLuigi Rizzo 
248268b8534bSLuigi Rizzo 
2483ce3ee1e7SLuigi Rizzo /*
2484f9790aebSLuigi Rizzo  * Dispatch rx/tx interrupts to the netmap rings.
2485ce3ee1e7SLuigi Rizzo  *
2486ce3ee1e7SLuigi Rizzo  * "work_done" is non-null on the RX path, NULL for the TX path.
2487ce3ee1e7SLuigi Rizzo  * We rely on the OS to make sure that there is only one active
2488ce3ee1e7SLuigi Rizzo  * instance per queue, and that there is appropriate locking.
2489849bec0eSLuigi Rizzo  *
2490f9790aebSLuigi Rizzo  * The 'notify' routine depends on what the ring is attached to.
2491f9790aebSLuigi Rizzo  * - for a netmap file descriptor, do a selwakeup on the individual
2492f9790aebSLuigi Rizzo  *   waitqueue, plus one on the global one if needed
2493f9790aebSLuigi Rizzo  * - for a switch, call the proper forwarding routine
2494f9790aebSLuigi Rizzo  * - XXX more ?
2495f9790aebSLuigi Rizzo  */
2496f9790aebSLuigi Rizzo void
2497f9790aebSLuigi Rizzo netmap_common_irq(struct ifnet *ifp, u_int q, u_int *work_done)
2498f9790aebSLuigi Rizzo {
2499f9790aebSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
2500f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
2501f9790aebSLuigi Rizzo 
2502f9790aebSLuigi Rizzo 	q &= NETMAP_RING_MASK;
2503f9790aebSLuigi Rizzo 
2504f9790aebSLuigi Rizzo 	if (netmap_verbose) {
2505f9790aebSLuigi Rizzo 	        RD(5, "received %s queue %d", work_done ? "RX" : "TX" , q);
2506f9790aebSLuigi Rizzo 	}
2507f9790aebSLuigi Rizzo 
2508f9790aebSLuigi Rizzo 	if (work_done) { /* RX path */
2509f9790aebSLuigi Rizzo 		if (q >= na->num_rx_rings)
2510f9790aebSLuigi Rizzo 			return;	// not a physical queue
2511f9790aebSLuigi Rizzo 		kring = na->rx_rings + q;
2512f9790aebSLuigi Rizzo 		kring->nr_kflags |= NKR_PENDINTR;	// XXX atomic ?
2513*f0ea3689SLuigi Rizzo 		na->nm_notify(na, q, NR_RX, 0);
2514f9790aebSLuigi Rizzo 		*work_done = 1; /* do not fire napi again */
2515f9790aebSLuigi Rizzo 	} else { /* TX path */
2516f9790aebSLuigi Rizzo 		if (q >= na->num_tx_rings)
2517f9790aebSLuigi Rizzo 			return;	// not a physical queue
2518f9790aebSLuigi Rizzo 		kring = na->tx_rings + q;
2519*f0ea3689SLuigi Rizzo 		na->nm_notify(na, q, NR_TX, 0);
2520f9790aebSLuigi Rizzo 	}
2521f9790aebSLuigi Rizzo }
2522f9790aebSLuigi Rizzo 
252317885a7bSLuigi Rizzo 
2524f9790aebSLuigi Rizzo /*
2525f9790aebSLuigi Rizzo  * Default functions to handle rx/tx interrupts from a physical device.
2526f9790aebSLuigi Rizzo  * "work_done" is non-null on the RX path, NULL for the TX path.
2527f9790aebSLuigi Rizzo  *
2528ce3ee1e7SLuigi Rizzo  * If the card is not in netmap mode, simply return 0,
2529ce3ee1e7SLuigi Rizzo  * so that the caller proceeds with regular processing.
2530f9790aebSLuigi Rizzo  * Otherwise call netmap_common_irq() and return 1.
2531ce3ee1e7SLuigi Rizzo  *
2532ce3ee1e7SLuigi Rizzo  * If the card is connected to a netmap file descriptor,
2533ce3ee1e7SLuigi Rizzo  * do a selwakeup on the individual queue, plus one on the global one
2534ce3ee1e7SLuigi Rizzo  * if needed (multiqueue card _and_ there are multiqueue listeners),
2535ce3ee1e7SLuigi Rizzo  * and return 1.
2536ce3ee1e7SLuigi Rizzo  *
2537ce3ee1e7SLuigi Rizzo  * Finally, if called on rx from an interface connected to a switch,
2538ce3ee1e7SLuigi Rizzo  * calls the proper forwarding routine, and return 1.
25391a26580eSLuigi Rizzo  */
2540babc7c12SLuigi Rizzo int
2541ce3ee1e7SLuigi Rizzo netmap_rx_irq(struct ifnet *ifp, u_int q, u_int *work_done)
25421a26580eSLuigi Rizzo {
2543f9790aebSLuigi Rizzo 	// XXX could we check NAF_NATIVE_ON ?
25441a26580eSLuigi Rizzo 	if (!(ifp->if_capenable & IFCAP_NETMAP))
25451a26580eSLuigi Rizzo 		return 0;
2546849bec0eSLuigi Rizzo 
2547f9790aebSLuigi Rizzo 	if (NA(ifp)->na_flags & NAF_SKIP_INTR) {
25488241616dSLuigi Rizzo 		ND("use regular interrupt");
25498241616dSLuigi Rizzo 		return 0;
25508241616dSLuigi Rizzo 	}
25518241616dSLuigi Rizzo 
2552f9790aebSLuigi Rizzo 	netmap_common_irq(ifp, q, work_done);
25531a26580eSLuigi Rizzo 	return 1;
25541a26580eSLuigi Rizzo }
25551a26580eSLuigi Rizzo 
255664ae02c3SLuigi Rizzo 
255701c7d25fSLuigi Rizzo /*
2558f9790aebSLuigi Rizzo  * Module loader and unloader
2559f196ce38SLuigi Rizzo  *
2560f9790aebSLuigi Rizzo  * netmap_init() creates the /dev/netmap device and initializes
2561f9790aebSLuigi Rizzo  * all global variables. Returns 0 on success, errno on failure
2562f9790aebSLuigi Rizzo  * (but there is no chance)
2563f9790aebSLuigi Rizzo  *
2564f9790aebSLuigi Rizzo  * netmap_fini() destroys everything.
2565f196ce38SLuigi Rizzo  */
2566babc7c12SLuigi Rizzo 
2567babc7c12SLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */
2568f9790aebSLuigi Rizzo extern struct cdevsw netmap_cdevsw;
2569babc7c12SLuigi Rizzo 
257017885a7bSLuigi Rizzo 
2571f9790aebSLuigi Rizzo void
257268b8534bSLuigi Rizzo netmap_fini(void)
257368b8534bSLuigi Rizzo {
2574f9790aebSLuigi Rizzo 	// XXX destroy_bridges() ?
2575f9790aebSLuigi Rizzo 	if (netmap_dev)
257668b8534bSLuigi Rizzo 		destroy_dev(netmap_dev);
2577ce3ee1e7SLuigi Rizzo 	netmap_mem_fini();
2578ce3ee1e7SLuigi Rizzo 	NMG_LOCK_DESTROY();
257968b8534bSLuigi Rizzo 	printf("netmap: unloaded module.\n");
258068b8534bSLuigi Rizzo }
258168b8534bSLuigi Rizzo 
258217885a7bSLuigi Rizzo 
2583f9790aebSLuigi Rizzo int
2584f9790aebSLuigi Rizzo netmap_init(void)
258568b8534bSLuigi Rizzo {
2586f9790aebSLuigi Rizzo 	int error;
258768b8534bSLuigi Rizzo 
2588f9790aebSLuigi Rizzo 	NMG_LOCK_INIT();
258968b8534bSLuigi Rizzo 
2590f9790aebSLuigi Rizzo 	error = netmap_mem_init();
2591f9790aebSLuigi Rizzo 	if (error != 0)
2592f9790aebSLuigi Rizzo 		goto fail;
2593f9790aebSLuigi Rizzo 	/* XXX could use make_dev_credv() to get error number */
2594f9790aebSLuigi Rizzo 	netmap_dev = make_dev(&netmap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660,
2595f9790aebSLuigi Rizzo 			      "netmap");
2596f9790aebSLuigi Rizzo 	if (!netmap_dev)
2597f9790aebSLuigi Rizzo 		goto fail;
2598f9790aebSLuigi Rizzo 
2599f9790aebSLuigi Rizzo 	netmap_init_bridges();
2600f9790aebSLuigi Rizzo 	printf("netmap: loaded module\n");
2601f9790aebSLuigi Rizzo 	return (0);
2602f9790aebSLuigi Rizzo fail:
260368b8534bSLuigi Rizzo 	netmap_fini();
2604f9790aebSLuigi Rizzo 	return (EINVAL); /* may be incorrect */
260568b8534bSLuigi Rizzo }
2606