xref: /freebsd/sys/dev/netmap/netmap.c (revision f26375266853d6a1037cadc43ee75d3f71a18403)
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 */
159f9790aebSLuigi Rizzo #define init_waitqueue_head(x)	// only needed in linux
160ce3ee1e7SLuigi Rizzo 
161ce3ee1e7SLuigi Rizzo 
162ce3ee1e7SLuigi Rizzo 
163ce3ee1e7SLuigi Rizzo #elif defined(linux)
164ce3ee1e7SLuigi Rizzo 
165ce3ee1e7SLuigi Rizzo #include "bsd_glue.h"
166ce3ee1e7SLuigi Rizzo 
167ce3ee1e7SLuigi Rizzo 
168ce3ee1e7SLuigi Rizzo 
169ce3ee1e7SLuigi Rizzo #elif defined(__APPLE__)
170ce3ee1e7SLuigi Rizzo 
171ce3ee1e7SLuigi Rizzo #warning OSX support is only partial
172ce3ee1e7SLuigi Rizzo #include "osx_glue.h"
173ce3ee1e7SLuigi Rizzo 
174ce3ee1e7SLuigi Rizzo #else
175ce3ee1e7SLuigi Rizzo 
176ce3ee1e7SLuigi Rizzo #error	Unsupported platform
177ce3ee1e7SLuigi Rizzo 
178ce3ee1e7SLuigi Rizzo #endif /* unsupported */
179ce3ee1e7SLuigi Rizzo 
180ce3ee1e7SLuigi Rizzo /*
181ce3ee1e7SLuigi Rizzo  * common headers
182ce3ee1e7SLuigi Rizzo  */
1830b8ed8e0SLuigi Rizzo #include <net/netmap.h>
1840b8ed8e0SLuigi Rizzo #include <dev/netmap/netmap_kern.h>
185ce3ee1e7SLuigi Rizzo #include <dev/netmap/netmap_mem2.h>
1860b8ed8e0SLuigi Rizzo 
187ce3ee1e7SLuigi Rizzo 
188ce3ee1e7SLuigi Rizzo MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map");
189ce3ee1e7SLuigi Rizzo 
190ce3ee1e7SLuigi Rizzo /*
191ce3ee1e7SLuigi Rizzo  * The following variables are used by the drivers and replicate
192ce3ee1e7SLuigi Rizzo  * fields in the global memory pool. They only refer to buffers
193ce3ee1e7SLuigi Rizzo  * used by physical interfaces.
194ce3ee1e7SLuigi Rizzo  */
1955819da83SLuigi Rizzo u_int netmap_total_buffers;
1968241616dSLuigi Rizzo u_int netmap_buf_size;
197ce3ee1e7SLuigi Rizzo char *netmap_buffer_base;	/* also address of an invalid buffer */
1985819da83SLuigi Rizzo 
1995819da83SLuigi Rizzo /* user-controlled variables */
2005819da83SLuigi Rizzo int netmap_verbose;
2015819da83SLuigi Rizzo 
2025819da83SLuigi Rizzo static int netmap_no_timestamp; /* don't timestamp on rxsync */
2035819da83SLuigi Rizzo 
2045819da83SLuigi Rizzo SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args");
2055819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose,
2065819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode");
2075819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp,
2085819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp");
2095819da83SLuigi Rizzo int netmap_mitigate = 1;
2105819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mitigate, CTLFLAG_RW, &netmap_mitigate, 0, "");
211c85cb1a0SLuigi Rizzo int netmap_no_pendintr = 1;
2125819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr,
2135819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_pendintr, 0, "Always look for new received packets.");
214f18be576SLuigi Rizzo int netmap_txsync_retry = 2;
215f18be576SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, txsync_retry, CTLFLAG_RW,
216f18be576SLuigi Rizzo     &netmap_txsync_retry, 0 , "Number of txsync loops in bridge's flush.");
2175819da83SLuigi Rizzo 
218f196ce38SLuigi Rizzo int netmap_flags = 0;	/* debug flags */
219091fd0abSLuigi Rizzo int netmap_fwd = 0;	/* force transparent mode */
220ce3ee1e7SLuigi Rizzo int netmap_mmap_unreg = 0; /* allow mmap of unregistered fds */
221f196ce38SLuigi Rizzo 
222f9790aebSLuigi Rizzo /*
223f9790aebSLuigi Rizzo  * netmap_admode selects the netmap mode to use.
224f9790aebSLuigi Rizzo  * Invalid values are reset to NETMAP_ADMODE_BEST
225f9790aebSLuigi Rizzo  */
226f9790aebSLuigi Rizzo enum { NETMAP_ADMODE_BEST = 0,	/* use native, fallback to generic */
227f9790aebSLuigi Rizzo 	NETMAP_ADMODE_NATIVE,	/* either native or none */
228f9790aebSLuigi Rizzo 	NETMAP_ADMODE_GENERIC,	/* force generic */
229f9790aebSLuigi Rizzo 	NETMAP_ADMODE_LAST };
230f9790aebSLuigi Rizzo static int netmap_admode = NETMAP_ADMODE_BEST;
231f9790aebSLuigi Rizzo 
232f9790aebSLuigi Rizzo int netmap_generic_mit = 100*1000;   /* Generic mitigation interval in nanoseconds. */
233f9790aebSLuigi Rizzo int netmap_generic_ringsize = 1024;   /* Generic ringsize. */
234f9790aebSLuigi Rizzo 
235f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, flags, CTLFLAG_RW, &netmap_flags, 0 , "");
236091fd0abSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, fwd, CTLFLAG_RW, &netmap_fwd, 0 , "");
237ce3ee1e7SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mmap_unreg, CTLFLAG_RW, &netmap_mmap_unreg, 0, "");
238f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, admode, CTLFLAG_RW, &netmap_admode, 0 , "");
239f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_mit, CTLFLAG_RW, &netmap_generic_mit, 0 , "");
240f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_ringsize, CTLFLAG_RW, &netmap_generic_ringsize, 0 , "");
241f196ce38SLuigi Rizzo 
242ce3ee1e7SLuigi Rizzo NMG_LOCK_T	netmap_global_lock;
243ce3ee1e7SLuigi Rizzo 
244ce3ee1e7SLuigi Rizzo 
245f9790aebSLuigi Rizzo static void
246f9790aebSLuigi Rizzo nm_kr_get(struct netmap_kring *kr)
247ce3ee1e7SLuigi Rizzo {
248ce3ee1e7SLuigi Rizzo 	while (NM_ATOMIC_TEST_AND_SET(&kr->nr_busy))
249ce3ee1e7SLuigi Rizzo 		tsleep(kr, 0, "NM_KR_GET", 4);
250ce3ee1e7SLuigi Rizzo }
251ce3ee1e7SLuigi Rizzo 
252f9790aebSLuigi Rizzo 
25317885a7bSLuigi Rizzo /*
25417885a7bSLuigi Rizzo  * mark the ring as stopped, and run through the locks
25517885a7bSLuigi Rizzo  * to make sure other users get to see it.
25617885a7bSLuigi Rizzo  */
257f9790aebSLuigi Rizzo void
258f9790aebSLuigi Rizzo netmap_disable_ring(struct netmap_kring *kr)
259ce3ee1e7SLuigi Rizzo {
260ce3ee1e7SLuigi Rizzo 	kr->nkr_stopped = 1;
261ce3ee1e7SLuigi Rizzo 	nm_kr_get(kr);
262ce3ee1e7SLuigi Rizzo 	mtx_lock(&kr->q_lock);
263ce3ee1e7SLuigi Rizzo 	mtx_unlock(&kr->q_lock);
264ce3ee1e7SLuigi Rizzo 	nm_kr_put(kr);
265ce3ee1e7SLuigi Rizzo }
266ce3ee1e7SLuigi Rizzo 
267f9790aebSLuigi Rizzo 
268f9790aebSLuigi Rizzo static void
269f9790aebSLuigi Rizzo netmap_set_all_rings(struct ifnet *ifp, int stopped)
270ce3ee1e7SLuigi Rizzo {
271ce3ee1e7SLuigi Rizzo 	struct netmap_adapter *na;
272ce3ee1e7SLuigi Rizzo 	int i;
273ce3ee1e7SLuigi Rizzo 
274ce3ee1e7SLuigi Rizzo 	if (!(ifp->if_capenable & IFCAP_NETMAP))
275ce3ee1e7SLuigi Rizzo 		return;
276ce3ee1e7SLuigi Rizzo 
277ce3ee1e7SLuigi Rizzo 	na = NA(ifp);
278ce3ee1e7SLuigi Rizzo 
279f9790aebSLuigi Rizzo 	for (i = 0; i <= na->num_tx_rings; i++) {
280f9790aebSLuigi Rizzo 		if (stopped)
281f9790aebSLuigi Rizzo 			netmap_disable_ring(na->tx_rings + i);
282f9790aebSLuigi Rizzo 		else
283ce3ee1e7SLuigi Rizzo 			na->tx_rings[i].nkr_stopped = 0;
284f9790aebSLuigi Rizzo 		na->nm_notify(na, i, NR_TX, NAF_DISABLE_NOTIFY |
285f9790aebSLuigi Rizzo 			(i == na->num_tx_rings ? NAF_GLOBAL_NOTIFY: 0));
286ce3ee1e7SLuigi Rizzo 	}
287f9790aebSLuigi Rizzo 
288f9790aebSLuigi Rizzo 	for (i = 0; i <= na->num_rx_rings; i++) {
289f9790aebSLuigi Rizzo 		if (stopped)
290f9790aebSLuigi Rizzo 			netmap_disable_ring(na->rx_rings + i);
291f9790aebSLuigi Rizzo 		else
292ce3ee1e7SLuigi Rizzo 			na->rx_rings[i].nkr_stopped = 0;
293f9790aebSLuigi Rizzo 		na->nm_notify(na, i, NR_RX, NAF_DISABLE_NOTIFY |
294f9790aebSLuigi Rizzo 			(i == na->num_rx_rings ? NAF_GLOBAL_NOTIFY: 0));
295ce3ee1e7SLuigi Rizzo 	}
296ce3ee1e7SLuigi Rizzo }
297ce3ee1e7SLuigi Rizzo 
298ce3ee1e7SLuigi Rizzo 
299f9790aebSLuigi Rizzo void
300f9790aebSLuigi Rizzo netmap_disable_all_rings(struct ifnet *ifp)
301f9790aebSLuigi Rizzo {
302f9790aebSLuigi Rizzo 	netmap_set_all_rings(ifp, 1 /* stopped */);
303f9790aebSLuigi Rizzo }
304f9790aebSLuigi Rizzo 
305f9790aebSLuigi Rizzo 
306f9790aebSLuigi Rizzo void
307f9790aebSLuigi Rizzo netmap_enable_all_rings(struct ifnet *ifp)
308f9790aebSLuigi Rizzo {
309f9790aebSLuigi Rizzo 	netmap_set_all_rings(ifp, 0 /* enabled */);
310f9790aebSLuigi Rizzo }
311f9790aebSLuigi Rizzo 
312f9790aebSLuigi Rizzo 
313ce3ee1e7SLuigi Rizzo /*
314ce3ee1e7SLuigi Rizzo  * generic bound_checking function
315ce3ee1e7SLuigi Rizzo  */
316ce3ee1e7SLuigi Rizzo u_int
317ce3ee1e7SLuigi Rizzo nm_bound_var(u_int *v, u_int dflt, u_int lo, u_int hi, const char *msg)
318ce3ee1e7SLuigi Rizzo {
319ce3ee1e7SLuigi Rizzo 	u_int oldv = *v;
320ce3ee1e7SLuigi Rizzo 	const char *op = NULL;
321ce3ee1e7SLuigi Rizzo 
322ce3ee1e7SLuigi Rizzo 	if (dflt < lo)
323ce3ee1e7SLuigi Rizzo 		dflt = lo;
324ce3ee1e7SLuigi Rizzo 	if (dflt > hi)
325ce3ee1e7SLuigi Rizzo 		dflt = hi;
326ce3ee1e7SLuigi Rizzo 	if (oldv < lo) {
327ce3ee1e7SLuigi Rizzo 		*v = dflt;
328ce3ee1e7SLuigi Rizzo 		op = "Bump";
329ce3ee1e7SLuigi Rizzo 	} else if (oldv > hi) {
330ce3ee1e7SLuigi Rizzo 		*v = hi;
331ce3ee1e7SLuigi Rizzo 		op = "Clamp";
332ce3ee1e7SLuigi Rizzo 	}
333ce3ee1e7SLuigi Rizzo 	if (op && msg)
334ce3ee1e7SLuigi Rizzo 		printf("%s %s to %d (was %d)\n", op, msg, *v, oldv);
335ce3ee1e7SLuigi Rizzo 	return *v;
336ce3ee1e7SLuigi Rizzo }
337ce3ee1e7SLuigi Rizzo 
338f9790aebSLuigi Rizzo 
339ce3ee1e7SLuigi Rizzo /*
340ce3ee1e7SLuigi Rizzo  * packet-dump function, user-supplied or static buffer.
341ce3ee1e7SLuigi Rizzo  * The destination buffer must be at least 30+4*len
342ce3ee1e7SLuigi Rizzo  */
343ce3ee1e7SLuigi Rizzo const char *
344ce3ee1e7SLuigi Rizzo nm_dump_buf(char *p, int len, int lim, char *dst)
345ce3ee1e7SLuigi Rizzo {
346ce3ee1e7SLuigi Rizzo 	static char _dst[8192];
347ce3ee1e7SLuigi Rizzo 	int i, j, i0;
348ce3ee1e7SLuigi Rizzo 	static char hex[] ="0123456789abcdef";
349ce3ee1e7SLuigi Rizzo 	char *o;	/* output position */
350ce3ee1e7SLuigi Rizzo 
351ce3ee1e7SLuigi Rizzo #define P_HI(x)	hex[((x) & 0xf0)>>4]
352ce3ee1e7SLuigi Rizzo #define P_LO(x)	hex[((x) & 0xf)]
353ce3ee1e7SLuigi Rizzo #define P_C(x)	((x) >= 0x20 && (x) <= 0x7e ? (x) : '.')
354ce3ee1e7SLuigi Rizzo 	if (!dst)
355ce3ee1e7SLuigi Rizzo 		dst = _dst;
356ce3ee1e7SLuigi Rizzo 	if (lim <= 0 || lim > len)
357ce3ee1e7SLuigi Rizzo 		lim = len;
358ce3ee1e7SLuigi Rizzo 	o = dst;
359ce3ee1e7SLuigi Rizzo 	sprintf(o, "buf 0x%p len %d lim %d\n", p, len, lim);
360ce3ee1e7SLuigi Rizzo 	o += strlen(o);
361ce3ee1e7SLuigi Rizzo 	/* hexdump routine */
362ce3ee1e7SLuigi Rizzo 	for (i = 0; i < lim; ) {
363ce3ee1e7SLuigi Rizzo 		sprintf(o, "%5d: ", i);
364ce3ee1e7SLuigi Rizzo 		o += strlen(o);
365ce3ee1e7SLuigi Rizzo 		memset(o, ' ', 48);
366ce3ee1e7SLuigi Rizzo 		i0 = i;
367ce3ee1e7SLuigi Rizzo 		for (j=0; j < 16 && i < lim; i++, j++) {
368ce3ee1e7SLuigi Rizzo 			o[j*3] = P_HI(p[i]);
369ce3ee1e7SLuigi Rizzo 			o[j*3+1] = P_LO(p[i]);
370ce3ee1e7SLuigi Rizzo 		}
371ce3ee1e7SLuigi Rizzo 		i = i0;
372ce3ee1e7SLuigi Rizzo 		for (j=0; j < 16 && i < lim; i++, j++)
373ce3ee1e7SLuigi Rizzo 			o[j + 48] = P_C(p[i]);
374ce3ee1e7SLuigi Rizzo 		o[j+48] = '\n';
375ce3ee1e7SLuigi Rizzo 		o += j+49;
376ce3ee1e7SLuigi Rizzo 	}
377ce3ee1e7SLuigi Rizzo 	*o = '\0';
378ce3ee1e7SLuigi Rizzo #undef P_HI
379ce3ee1e7SLuigi Rizzo #undef P_LO
380ce3ee1e7SLuigi Rizzo #undef P_C
381ce3ee1e7SLuigi Rizzo 	return dst;
382ce3ee1e7SLuigi Rizzo }
383f196ce38SLuigi Rizzo 
384f18be576SLuigi Rizzo 
385ae10d1afSLuigi Rizzo /*
386ae10d1afSLuigi Rizzo  * Fetch configuration from the device, to cope with dynamic
387ae10d1afSLuigi Rizzo  * reconfigurations after loading the module.
388ae10d1afSLuigi Rizzo  */
389f9790aebSLuigi Rizzo int
390ae10d1afSLuigi Rizzo netmap_update_config(struct netmap_adapter *na)
391ae10d1afSLuigi Rizzo {
392ae10d1afSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
393ae10d1afSLuigi Rizzo 	u_int txr, txd, rxr, rxd;
394ae10d1afSLuigi Rizzo 
395ae10d1afSLuigi Rizzo 	txr = txd = rxr = rxd = 0;
396ae10d1afSLuigi Rizzo 	if (na->nm_config) {
397f9790aebSLuigi Rizzo 		na->nm_config(na, &txr, &txd, &rxr, &rxd);
398ae10d1afSLuigi Rizzo 	} else {
399ae10d1afSLuigi Rizzo 		/* take whatever we had at init time */
400ae10d1afSLuigi Rizzo 		txr = na->num_tx_rings;
401ae10d1afSLuigi Rizzo 		txd = na->num_tx_desc;
402ae10d1afSLuigi Rizzo 		rxr = na->num_rx_rings;
403ae10d1afSLuigi Rizzo 		rxd = na->num_rx_desc;
404ae10d1afSLuigi Rizzo 	}
405ae10d1afSLuigi Rizzo 
406ae10d1afSLuigi Rizzo 	if (na->num_tx_rings == txr && na->num_tx_desc == txd &&
407ae10d1afSLuigi Rizzo 	    na->num_rx_rings == rxr && na->num_rx_desc == rxd)
408ae10d1afSLuigi Rizzo 		return 0; /* nothing changed */
409f9790aebSLuigi Rizzo 	if (netmap_verbose || na->active_fds > 0) {
410ae10d1afSLuigi Rizzo 		D("stored config %s: txring %d x %d, rxring %d x %d",
411f9790aebSLuigi Rizzo 			NM_IFPNAME(ifp),
412ae10d1afSLuigi Rizzo 			na->num_tx_rings, na->num_tx_desc,
413ae10d1afSLuigi Rizzo 			na->num_rx_rings, na->num_rx_desc);
414ae10d1afSLuigi Rizzo 		D("new config %s: txring %d x %d, rxring %d x %d",
415f9790aebSLuigi Rizzo 			NM_IFPNAME(ifp), txr, txd, rxr, rxd);
416ae10d1afSLuigi Rizzo 	}
417f9790aebSLuigi Rizzo 	if (na->active_fds == 0) {
418ae10d1afSLuigi Rizzo 		D("configuration changed (but fine)");
419ae10d1afSLuigi Rizzo 		na->num_tx_rings = txr;
420ae10d1afSLuigi Rizzo 		na->num_tx_desc = txd;
421ae10d1afSLuigi Rizzo 		na->num_rx_rings = rxr;
422ae10d1afSLuigi Rizzo 		na->num_rx_desc = rxd;
423ae10d1afSLuigi Rizzo 		return 0;
424ae10d1afSLuigi Rizzo 	}
425ae10d1afSLuigi Rizzo 	D("configuration changed while active, this is bad...");
426ae10d1afSLuigi Rizzo 	return 1;
427ae10d1afSLuigi Rizzo }
428ae10d1afSLuigi Rizzo 
429f9790aebSLuigi Rizzo 
430f9790aebSLuigi Rizzo int
431f9790aebSLuigi Rizzo netmap_krings_create(struct netmap_adapter *na, u_int ntx, u_int nrx, u_int tailroom)
432f9790aebSLuigi Rizzo {
433f9790aebSLuigi Rizzo 	u_int i, len, ndesc;
434f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
435f9790aebSLuigi Rizzo 
43617885a7bSLuigi Rizzo 	// XXX additional space for extra rings ?
437f9790aebSLuigi Rizzo 	len = (ntx + nrx) * sizeof(struct netmap_kring) + tailroom;
438f9790aebSLuigi Rizzo 
439f9790aebSLuigi Rizzo 	na->tx_rings = malloc((size_t)len, M_DEVBUF, M_NOWAIT | M_ZERO);
440f9790aebSLuigi Rizzo 	if (na->tx_rings == NULL) {
441f9790aebSLuigi Rizzo 		D("Cannot allocate krings");
442f9790aebSLuigi Rizzo 		return ENOMEM;
443f9790aebSLuigi Rizzo 	}
444f9790aebSLuigi Rizzo 	na->rx_rings = na->tx_rings + ntx;
445f9790aebSLuigi Rizzo 
44617885a7bSLuigi Rizzo 	/*
44717885a7bSLuigi Rizzo 	 * All fields in krings are 0 except the one initialized below.
44817885a7bSLuigi Rizzo 	 * but better be explicit on important kring fields.
44917885a7bSLuigi Rizzo 	 */
450f9790aebSLuigi Rizzo 	ndesc = na->num_tx_desc;
451f9790aebSLuigi Rizzo 	for (i = 0; i < ntx; i++) { /* Transmit rings */
452f9790aebSLuigi Rizzo 		kring = &na->tx_rings[i];
453f9790aebSLuigi Rizzo 		bzero(kring, sizeof(*kring));
454f9790aebSLuigi Rizzo 		kring->na = na;
45517885a7bSLuigi Rizzo 		kring->ring_id = i;
456f9790aebSLuigi Rizzo 		kring->nkr_num_slots = ndesc;
457f9790aebSLuigi Rizzo 		/*
45817885a7bSLuigi Rizzo 		 * IMPORTANT: Always keep one slot empty.
459f9790aebSLuigi Rizzo 		 */
46017885a7bSLuigi Rizzo 		kring->rhead = kring->rcur = kring->nr_hwcur = 0;
46117885a7bSLuigi Rizzo 		kring->rtail = kring->nr_hwtail = ndesc - 1;
46217885a7bSLuigi Rizzo 		snprintf(kring->name, sizeof(kring->name) - 1, "%s TX%d", NM_IFPNAME(na->ifp), i);
463f9790aebSLuigi Rizzo 		mtx_init(&kring->q_lock, "nm_txq_lock", NULL, MTX_DEF);
464f9790aebSLuigi Rizzo 		init_waitqueue_head(&kring->si);
465f9790aebSLuigi Rizzo 	}
466f9790aebSLuigi Rizzo 
467f9790aebSLuigi Rizzo 	ndesc = na->num_rx_desc;
468f9790aebSLuigi Rizzo 	for (i = 0; i < nrx; i++) { /* Receive rings */
469f9790aebSLuigi Rizzo 		kring = &na->rx_rings[i];
470f9790aebSLuigi Rizzo 		bzero(kring, sizeof(*kring));
471f9790aebSLuigi Rizzo 		kring->na = na;
47217885a7bSLuigi Rizzo 		kring->ring_id = i;
473f9790aebSLuigi Rizzo 		kring->nkr_num_slots = ndesc;
47417885a7bSLuigi Rizzo 		kring->rhead = kring->rcur = kring->nr_hwcur = 0;
47517885a7bSLuigi Rizzo 		kring->rtail = kring->nr_hwtail = 0;
47617885a7bSLuigi Rizzo 		snprintf(kring->name, sizeof(kring->name) - 1, "%s RX%d", NM_IFPNAME(na->ifp), i);
477f9790aebSLuigi Rizzo 		mtx_init(&kring->q_lock, "nm_rxq_lock", NULL, MTX_DEF);
478f9790aebSLuigi Rizzo 		init_waitqueue_head(&kring->si);
479f9790aebSLuigi Rizzo 	}
480f9790aebSLuigi Rizzo 	init_waitqueue_head(&na->tx_si);
481f9790aebSLuigi Rizzo 	init_waitqueue_head(&na->rx_si);
482f9790aebSLuigi Rizzo 
483f9790aebSLuigi Rizzo 	na->tailroom = na->rx_rings + nrx;
484f9790aebSLuigi Rizzo 
485f9790aebSLuigi Rizzo 	return 0;
486f9790aebSLuigi Rizzo }
487f9790aebSLuigi Rizzo 
488f9790aebSLuigi Rizzo 
48917885a7bSLuigi Rizzo /* XXX check boundaries */
490f9790aebSLuigi Rizzo void
491f9790aebSLuigi Rizzo netmap_krings_delete(struct netmap_adapter *na)
492f9790aebSLuigi Rizzo {
493f9790aebSLuigi Rizzo 	int i;
494f9790aebSLuigi Rizzo 
495f9790aebSLuigi Rizzo 	for (i = 0; i < na->num_tx_rings + 1; i++) {
496f9790aebSLuigi Rizzo 		mtx_destroy(&na->tx_rings[i].q_lock);
497f9790aebSLuigi Rizzo 	}
498f9790aebSLuigi Rizzo 	for (i = 0; i < na->num_rx_rings + 1; i++) {
499f9790aebSLuigi Rizzo 		mtx_destroy(&na->rx_rings[i].q_lock);
500f9790aebSLuigi Rizzo 	}
501f9790aebSLuigi Rizzo 	free(na->tx_rings, M_DEVBUF);
502f9790aebSLuigi Rizzo 	na->tx_rings = na->rx_rings = na->tailroom = NULL;
503f9790aebSLuigi Rizzo }
504f9790aebSLuigi Rizzo 
505f9790aebSLuigi Rizzo 
50617885a7bSLuigi Rizzo /*
50717885a7bSLuigi Rizzo  * Destructor for NIC ports. They also have an mbuf queue
50817885a7bSLuigi Rizzo  * on the rings connected to the host so we need to purge
50917885a7bSLuigi Rizzo  * them first.
51017885a7bSLuigi Rizzo  */
51117885a7bSLuigi Rizzo static void
51217885a7bSLuigi Rizzo netmap_hw_krings_delete(struct netmap_adapter *na)
51317885a7bSLuigi Rizzo {
51417885a7bSLuigi Rizzo 	struct mbq *q = &na->rx_rings[na->num_rx_rings].rx_queue;
51517885a7bSLuigi Rizzo 
51617885a7bSLuigi Rizzo 	ND("destroy sw mbq with len %d", mbq_len(q));
51717885a7bSLuigi Rizzo 	mbq_purge(q);
51817885a7bSLuigi Rizzo 	mbq_safe_destroy(q);
51917885a7bSLuigi Rizzo 	netmap_krings_delete(na);
52017885a7bSLuigi Rizzo }
52117885a7bSLuigi Rizzo 
52217885a7bSLuigi Rizzo 
523ce3ee1e7SLuigi Rizzo static struct netmap_if*
524ce3ee1e7SLuigi Rizzo netmap_if_new(const char *ifname, struct netmap_adapter *na)
525ce3ee1e7SLuigi Rizzo {
526f9790aebSLuigi Rizzo 	struct netmap_if *nifp;
527f9790aebSLuigi Rizzo 
528ce3ee1e7SLuigi Rizzo 	if (netmap_update_config(na)) {
529ce3ee1e7SLuigi Rizzo 		/* configuration mismatch, report and fail */
530ce3ee1e7SLuigi Rizzo 		return NULL;
531ce3ee1e7SLuigi Rizzo 	}
532f9790aebSLuigi Rizzo 
533f9790aebSLuigi Rizzo 	if (na->active_fds)
534f9790aebSLuigi Rizzo 		goto final;
535f9790aebSLuigi Rizzo 
536f9790aebSLuigi Rizzo 	if (na->nm_krings_create(na))
537f9790aebSLuigi Rizzo 		goto cleanup;
538f9790aebSLuigi Rizzo 
539f9790aebSLuigi Rizzo 	if (netmap_mem_rings_create(na))
540f9790aebSLuigi Rizzo 		goto cleanup;
541f9790aebSLuigi Rizzo 
542f9790aebSLuigi Rizzo final:
543f9790aebSLuigi Rizzo 
544f9790aebSLuigi Rizzo 	nifp = netmap_mem_if_new(ifname, na);
545f9790aebSLuigi Rizzo 	if (nifp == NULL)
546f9790aebSLuigi Rizzo 		goto cleanup;
547f9790aebSLuigi Rizzo 
548f9790aebSLuigi Rizzo 	return (nifp);
549f9790aebSLuigi Rizzo 
550f9790aebSLuigi Rizzo cleanup:
551f9790aebSLuigi Rizzo 
552f9790aebSLuigi Rizzo 	if (na->active_fds == 0) {
553f9790aebSLuigi Rizzo 		netmap_mem_rings_delete(na);
554f9790aebSLuigi Rizzo 		na->nm_krings_delete(na);
555ce3ee1e7SLuigi Rizzo 	}
55668b8534bSLuigi Rizzo 
557f9790aebSLuigi Rizzo 	return NULL;
558f9790aebSLuigi Rizzo }
5598241616dSLuigi Rizzo 
56068b8534bSLuigi Rizzo 
561ce3ee1e7SLuigi Rizzo /* grab a reference to the memory allocator, if we don't have one already.  The
562ce3ee1e7SLuigi Rizzo  * reference is taken from the netmap_adapter registered with the priv.
563ce3ee1e7SLuigi Rizzo  *
564ce3ee1e7SLuigi Rizzo  */
565ce3ee1e7SLuigi Rizzo static int
566ce3ee1e7SLuigi Rizzo netmap_get_memory_locked(struct netmap_priv_d* p)
567ce3ee1e7SLuigi Rizzo {
568ce3ee1e7SLuigi Rizzo 	struct netmap_mem_d *nmd;
569ce3ee1e7SLuigi Rizzo 	int error = 0;
570ce3ee1e7SLuigi Rizzo 
571f9790aebSLuigi Rizzo 	if (p->np_na == NULL) {
572ce3ee1e7SLuigi Rizzo 		if (!netmap_mmap_unreg)
573ce3ee1e7SLuigi Rizzo 			return ENODEV;
574ce3ee1e7SLuigi Rizzo 		/* for compatibility with older versions of the API
575ce3ee1e7SLuigi Rizzo  		 * we use the global allocator when no interface has been
576ce3ee1e7SLuigi Rizzo  		 * registered
577ce3ee1e7SLuigi Rizzo  		 */
578ce3ee1e7SLuigi Rizzo 		nmd = &nm_mem;
579ce3ee1e7SLuigi Rizzo 	} else {
580f9790aebSLuigi Rizzo 		nmd = p->np_na->nm_mem;
581ce3ee1e7SLuigi Rizzo 	}
582ce3ee1e7SLuigi Rizzo 	if (p->np_mref == NULL) {
583ce3ee1e7SLuigi Rizzo 		error = netmap_mem_finalize(nmd);
584ce3ee1e7SLuigi Rizzo 		if (!error)
585ce3ee1e7SLuigi Rizzo 			p->np_mref = nmd;
586ce3ee1e7SLuigi Rizzo 	} else if (p->np_mref != nmd) {
587ce3ee1e7SLuigi Rizzo 		/* a virtual port has been registered, but previous
588ce3ee1e7SLuigi Rizzo  		 * syscalls already used the global allocator.
589ce3ee1e7SLuigi Rizzo  		 * We cannot continue
590ce3ee1e7SLuigi Rizzo  		 */
591ce3ee1e7SLuigi Rizzo 		error = ENODEV;
592ce3ee1e7SLuigi Rizzo 	}
593ce3ee1e7SLuigi Rizzo 	return error;
594ce3ee1e7SLuigi Rizzo }
59568b8534bSLuigi Rizzo 
596f9790aebSLuigi Rizzo 
597f9790aebSLuigi Rizzo int
5988241616dSLuigi Rizzo netmap_get_memory(struct netmap_priv_d* p)
5998241616dSLuigi Rizzo {
600ce3ee1e7SLuigi Rizzo 	int error;
601ce3ee1e7SLuigi Rizzo 	NMG_LOCK();
602ce3ee1e7SLuigi Rizzo 	error = netmap_get_memory_locked(p);
603ce3ee1e7SLuigi Rizzo 	NMG_UNLOCK();
6048241616dSLuigi Rizzo 	return error;
6058241616dSLuigi Rizzo }
6068241616dSLuigi Rizzo 
607f9790aebSLuigi Rizzo 
608ce3ee1e7SLuigi Rizzo static int
609ce3ee1e7SLuigi Rizzo netmap_have_memory_locked(struct netmap_priv_d* p)
610ce3ee1e7SLuigi Rizzo {
611ce3ee1e7SLuigi Rizzo 	return p->np_mref != NULL;
612ce3ee1e7SLuigi Rizzo }
613ce3ee1e7SLuigi Rizzo 
614f9790aebSLuigi Rizzo 
615ce3ee1e7SLuigi Rizzo static void
616ce3ee1e7SLuigi Rizzo netmap_drop_memory_locked(struct netmap_priv_d* p)
617ce3ee1e7SLuigi Rizzo {
618ce3ee1e7SLuigi Rizzo 	if (p->np_mref) {
619ce3ee1e7SLuigi Rizzo 		netmap_mem_deref(p->np_mref);
620ce3ee1e7SLuigi Rizzo 		p->np_mref = NULL;
621ce3ee1e7SLuigi Rizzo 	}
622ce3ee1e7SLuigi Rizzo }
623ce3ee1e7SLuigi Rizzo 
624f9790aebSLuigi Rizzo 
62568b8534bSLuigi Rizzo /*
62668b8534bSLuigi Rizzo  * File descriptor's private data destructor.
62768b8534bSLuigi Rizzo  *
62868b8534bSLuigi Rizzo  * Call nm_register(ifp,0) to stop netmap mode on the interface and
629f9790aebSLuigi Rizzo  * revert to normal operation. We expect that np_na->ifp has not gone.
630ce3ee1e7SLuigi Rizzo  * The second argument is the nifp to work on. In some cases it is
631ce3ee1e7SLuigi Rizzo  * not attached yet to the netmap_priv_d so we need to pass it as
632ce3ee1e7SLuigi Rizzo  * a separate argument.
63368b8534bSLuigi Rizzo  */
634ce3ee1e7SLuigi Rizzo /* call with NMG_LOCK held */
63568b8534bSLuigi Rizzo static void
636ce3ee1e7SLuigi Rizzo netmap_do_unregif(struct netmap_priv_d *priv, struct netmap_if *nifp)
63768b8534bSLuigi Rizzo {
638f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
639f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
64068b8534bSLuigi Rizzo 
641ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
642f9790aebSLuigi Rizzo 	na->active_fds--;
643f9790aebSLuigi Rizzo 	if (na->active_fds <= 0) {	/* last instance */
64468b8534bSLuigi Rizzo 
645ae10d1afSLuigi Rizzo 		if (netmap_verbose)
646f9790aebSLuigi Rizzo 			D("deleting last instance for %s", NM_IFPNAME(ifp));
64768b8534bSLuigi Rizzo 		/*
648f18be576SLuigi Rizzo 		 * (TO CHECK) This function is only called
649f18be576SLuigi Rizzo 		 * when the last reference to this file descriptor goes
650f18be576SLuigi Rizzo 		 * away. This means we cannot have any pending poll()
651f18be576SLuigi Rizzo 		 * or interrupt routine operating on the structure.
652ce3ee1e7SLuigi Rizzo 		 * XXX The file may be closed in a thread while
653ce3ee1e7SLuigi Rizzo 		 * another thread is using it.
654ce3ee1e7SLuigi Rizzo 		 * Linux keeps the file opened until the last reference
655ce3ee1e7SLuigi Rizzo 		 * by any outstanding ioctl/poll or mmap is gone.
656ce3ee1e7SLuigi Rizzo 		 * FreeBSD does not track mmap()s (but we do) and
657ce3ee1e7SLuigi Rizzo 		 * wakes up any sleeping poll(). Need to check what
658ce3ee1e7SLuigi Rizzo 		 * happens if the close() occurs while a concurrent
659ce3ee1e7SLuigi Rizzo 		 * syscall is running.
66068b8534bSLuigi Rizzo 		 */
661f9790aebSLuigi Rizzo 		if (ifp)
662f9790aebSLuigi Rizzo 			na->nm_register(na, 0); /* off, clear flags */
66368b8534bSLuigi Rizzo 		/* Wake up any sleeping threads. netmap_poll will
66468b8534bSLuigi Rizzo 		 * then return POLLERR
665ce3ee1e7SLuigi Rizzo 		 * XXX The wake up now must happen during *_down(), when
666ce3ee1e7SLuigi Rizzo 		 * we order all activities to stop. -gl
66768b8534bSLuigi Rizzo 		 */
6682f70fca5SEd Maste 		/* XXX kqueue(9) needed; these will mirror knlist_init. */
6692f70fca5SEd Maste 		/* knlist_destroy(&na->tx_si.si_note); */
6702f70fca5SEd Maste 		/* knlist_destroy(&na->rx_si.si_note); */
671f9790aebSLuigi Rizzo 
672f9790aebSLuigi Rizzo 		/* delete rings and buffers */
673f9790aebSLuigi Rizzo 		netmap_mem_rings_delete(na);
674f9790aebSLuigi Rizzo 		na->nm_krings_delete(na);
67568b8534bSLuigi Rizzo 	}
676f9790aebSLuigi Rizzo 	/* delete the nifp */
677ce3ee1e7SLuigi Rizzo 	netmap_mem_if_delete(na, nifp);
6785819da83SLuigi Rizzo }
67968b8534bSLuigi Rizzo 
680f18be576SLuigi Rizzo 
681ce3ee1e7SLuigi Rizzo /*
682ce3ee1e7SLuigi Rizzo  * returns 1 if this is the last instance and we can free priv
683ce3ee1e7SLuigi Rizzo  */
684f9790aebSLuigi Rizzo int
685ce3ee1e7SLuigi Rizzo netmap_dtor_locked(struct netmap_priv_d *priv)
686ce3ee1e7SLuigi Rizzo {
687f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
688ce3ee1e7SLuigi Rizzo 
689ce3ee1e7SLuigi Rizzo #ifdef __FreeBSD__
690ce3ee1e7SLuigi Rizzo 	/*
691ce3ee1e7SLuigi Rizzo 	 * np_refcount is the number of active mmaps on
692ce3ee1e7SLuigi Rizzo 	 * this file descriptor
693ce3ee1e7SLuigi Rizzo 	 */
694ce3ee1e7SLuigi Rizzo 	if (--priv->np_refcount > 0) {
695ce3ee1e7SLuigi Rizzo 		return 0;
696ce3ee1e7SLuigi Rizzo 	}
697ce3ee1e7SLuigi Rizzo #endif /* __FreeBSD__ */
698f9790aebSLuigi Rizzo 	if (!na) {
699f9790aebSLuigi Rizzo 	    return 1; //XXX is it correct?
700ce3ee1e7SLuigi Rizzo 	}
701f9790aebSLuigi Rizzo 	netmap_do_unregif(priv, priv->np_nifp);
702f9790aebSLuigi Rizzo 	priv->np_nifp = NULL;
703ce3ee1e7SLuigi Rizzo 	netmap_drop_memory_locked(priv);
704f9790aebSLuigi Rizzo 	if (priv->np_na) {
705f9790aebSLuigi Rizzo 		netmap_adapter_put(na);
706f9790aebSLuigi Rizzo 		priv->np_na = NULL;
707ce3ee1e7SLuigi Rizzo 	}
708ce3ee1e7SLuigi Rizzo 	return 1;
709f196ce38SLuigi Rizzo }
7105819da83SLuigi Rizzo 
711f9790aebSLuigi Rizzo 
712f9790aebSLuigi Rizzo void
7135819da83SLuigi Rizzo netmap_dtor(void *data)
7145819da83SLuigi Rizzo {
7155819da83SLuigi Rizzo 	struct netmap_priv_d *priv = data;
716ce3ee1e7SLuigi Rizzo 	int last_instance;
7175819da83SLuigi Rizzo 
718ce3ee1e7SLuigi Rizzo 	NMG_LOCK();
719ce3ee1e7SLuigi Rizzo 	last_instance = netmap_dtor_locked(priv);
720ce3ee1e7SLuigi Rizzo 	NMG_UNLOCK();
721ce3ee1e7SLuigi Rizzo 	if (last_instance) {
722ce3ee1e7SLuigi Rizzo 		bzero(priv, sizeof(*priv));	/* for safety */
72368b8534bSLuigi Rizzo 		free(priv, M_DEVBUF);
72468b8534bSLuigi Rizzo 	}
725ce3ee1e7SLuigi Rizzo }
72668b8534bSLuigi Rizzo 
727f18be576SLuigi Rizzo 
72868b8534bSLuigi Rizzo 
72968b8534bSLuigi Rizzo 
73068b8534bSLuigi Rizzo /*
73102ad4083SLuigi Rizzo  * Handlers for synchronization of the queues from/to the host.
732091fd0abSLuigi Rizzo  * Netmap has two operating modes:
733091fd0abSLuigi Rizzo  * - in the default mode, the rings connected to the host stack are
734091fd0abSLuigi Rizzo  *   just another ring pair managed by userspace;
735091fd0abSLuigi Rizzo  * - in transparent mode (XXX to be defined) incoming packets
736091fd0abSLuigi Rizzo  *   (from the host or the NIC) are marked as NS_FORWARD upon
737091fd0abSLuigi Rizzo  *   arrival, and the user application has a chance to reset the
738091fd0abSLuigi Rizzo  *   flag for packets that should be dropped.
739091fd0abSLuigi Rizzo  *   On the RXSYNC or poll(), packets in RX rings between
740091fd0abSLuigi Rizzo  *   kring->nr_kcur and ring->cur with NS_FORWARD still set are moved
741091fd0abSLuigi Rizzo  *   to the other side.
742091fd0abSLuigi Rizzo  * The transfer NIC --> host is relatively easy, just encapsulate
743091fd0abSLuigi Rizzo  * into mbufs and we are done. The host --> NIC side is slightly
744091fd0abSLuigi Rizzo  * harder because there might not be room in the tx ring so it
745091fd0abSLuigi Rizzo  * might take a while before releasing the buffer.
746091fd0abSLuigi Rizzo  */
747091fd0abSLuigi Rizzo 
748f18be576SLuigi Rizzo 
749091fd0abSLuigi Rizzo /*
750091fd0abSLuigi Rizzo  * pass a chain of buffers to the host stack as coming from 'dst'
75117885a7bSLuigi Rizzo  * We do not need to lock because the queue is private.
752091fd0abSLuigi Rizzo  */
753091fd0abSLuigi Rizzo static void
754f9790aebSLuigi Rizzo netmap_send_up(struct ifnet *dst, struct mbq *q)
755091fd0abSLuigi Rizzo {
756091fd0abSLuigi Rizzo 	struct mbuf *m;
757091fd0abSLuigi Rizzo 
758091fd0abSLuigi Rizzo 	/* send packets up, outside the lock */
759f9790aebSLuigi Rizzo 	while ((m = mbq_dequeue(q)) != NULL) {
760091fd0abSLuigi Rizzo 		if (netmap_verbose & NM_VERB_HOST)
761091fd0abSLuigi Rizzo 			D("sending up pkt %p size %d", m, MBUF_LEN(m));
762091fd0abSLuigi Rizzo 		NM_SEND_UP(dst, m);
763091fd0abSLuigi Rizzo 	}
764f9790aebSLuigi Rizzo 	mbq_destroy(q);
765091fd0abSLuigi Rizzo }
766091fd0abSLuigi Rizzo 
767f18be576SLuigi Rizzo 
768091fd0abSLuigi Rizzo /*
769091fd0abSLuigi Rizzo  * put a copy of the buffers marked NS_FORWARD into an mbuf chain.
77017885a7bSLuigi Rizzo  * Take packets from hwcur to ring->head marked NS_FORWARD (or forced)
77117885a7bSLuigi Rizzo  * and pass them up. Drop remaining packets in the unlikely event
77217885a7bSLuigi Rizzo  * of an mbuf shortage.
773091fd0abSLuigi Rizzo  */
774091fd0abSLuigi Rizzo static void
775091fd0abSLuigi Rizzo netmap_grab_packets(struct netmap_kring *kring, struct mbq *q, int force)
776091fd0abSLuigi Rizzo {
77717885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
77817885a7bSLuigi Rizzo 	u_int const head = kring->ring->head;
77917885a7bSLuigi Rizzo 	u_int n;
780f9790aebSLuigi Rizzo 	struct netmap_adapter *na = kring->na;
781091fd0abSLuigi Rizzo 
78217885a7bSLuigi Rizzo 	for (n = kring->nr_hwcur; n != head; n = nm_next(n, lim)) {
78317885a7bSLuigi Rizzo 		struct mbuf *m;
784091fd0abSLuigi Rizzo 		struct netmap_slot *slot = &kring->ring->slot[n];
785091fd0abSLuigi Rizzo 
786091fd0abSLuigi Rizzo 		if ((slot->flags & NS_FORWARD) == 0 && !force)
787091fd0abSLuigi Rizzo 			continue;
788f9790aebSLuigi Rizzo 		if (slot->len < 14 || slot->len > NETMAP_BDG_BUF_SIZE(na->nm_mem)) {
78917885a7bSLuigi Rizzo 			RD(5, "bad pkt at %d len %d", n, slot->len);
790091fd0abSLuigi Rizzo 			continue;
791091fd0abSLuigi Rizzo 		}
792091fd0abSLuigi Rizzo 		slot->flags &= ~NS_FORWARD; // XXX needed ?
79317885a7bSLuigi Rizzo 		/* XXX TODO: adapt to the case of a multisegment packet */
794f9790aebSLuigi Rizzo 		m = m_devget(BDG_NMB(na, slot), slot->len, 0, na->ifp, NULL);
795091fd0abSLuigi Rizzo 
796091fd0abSLuigi Rizzo 		if (m == NULL)
797091fd0abSLuigi Rizzo 			break;
798f9790aebSLuigi Rizzo 		mbq_enqueue(q, m);
799091fd0abSLuigi Rizzo 	}
800091fd0abSLuigi Rizzo }
801091fd0abSLuigi Rizzo 
802f18be576SLuigi Rizzo 
803091fd0abSLuigi Rizzo /*
80417885a7bSLuigi Rizzo  * Send to the NIC rings packets marked NS_FORWARD between
80517885a7bSLuigi Rizzo  * kring->nr_hwcur and kring->rhead
80617885a7bSLuigi Rizzo  * Called under kring->rx_queue.lock on the sw rx ring,
807091fd0abSLuigi Rizzo  */
80817885a7bSLuigi Rizzo static u_int
809091fd0abSLuigi Rizzo netmap_sw_to_nic(struct netmap_adapter *na)
810091fd0abSLuigi Rizzo {
811091fd0abSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
81217885a7bSLuigi Rizzo 	struct netmap_slot *rxslot = kring->ring->slot;
81317885a7bSLuigi Rizzo 	u_int i, rxcur = kring->nr_hwcur;
81417885a7bSLuigi Rizzo 	u_int const head = kring->rhead;
81517885a7bSLuigi Rizzo 	u_int const src_lim = kring->nkr_num_slots - 1;
81617885a7bSLuigi Rizzo 	u_int sent = 0;
817ce3ee1e7SLuigi Rizzo 
81817885a7bSLuigi Rizzo 	/* scan rings to find space, then fill as much as possible */
81917885a7bSLuigi Rizzo 	for (i = 0; i < na->num_tx_rings; i++) {
82017885a7bSLuigi Rizzo 		struct netmap_kring *kdst = &na->tx_rings[i];
82117885a7bSLuigi Rizzo 		struct netmap_ring *rdst = kdst->ring;
82217885a7bSLuigi Rizzo 		u_int const dst_lim = kdst->nkr_num_slots - 1;
823ce3ee1e7SLuigi Rizzo 
82417885a7bSLuigi Rizzo 		/* XXX do we trust ring or kring->rcur,rtail ? */
82517885a7bSLuigi Rizzo 		for (; rxcur != head && !nm_ring_empty(rdst);
82617885a7bSLuigi Rizzo 		     rxcur = nm_next(rxcur, src_lim) ) {
827091fd0abSLuigi Rizzo 			struct netmap_slot *src, *dst, tmp;
82817885a7bSLuigi Rizzo 			u_int dst_cur = rdst->cur;
82917885a7bSLuigi Rizzo 
83017885a7bSLuigi Rizzo 			src = &rxslot[rxcur];
83117885a7bSLuigi Rizzo 			if ((src->flags & NS_FORWARD) == 0 && !netmap_fwd)
83217885a7bSLuigi Rizzo 				continue;
83317885a7bSLuigi Rizzo 
83417885a7bSLuigi Rizzo 			sent++;
83517885a7bSLuigi Rizzo 
83617885a7bSLuigi Rizzo 			dst = &rdst->slot[dst_cur];
83717885a7bSLuigi Rizzo 
838091fd0abSLuigi Rizzo 			tmp = *src;
83917885a7bSLuigi Rizzo 
840091fd0abSLuigi Rizzo 			src->buf_idx = dst->buf_idx;
841091fd0abSLuigi Rizzo 			src->flags = NS_BUF_CHANGED;
842091fd0abSLuigi Rizzo 
843091fd0abSLuigi Rizzo 			dst->buf_idx = tmp.buf_idx;
844091fd0abSLuigi Rizzo 			dst->len = tmp.len;
845091fd0abSLuigi Rizzo 			dst->flags = NS_BUF_CHANGED;
846091fd0abSLuigi Rizzo 
84717885a7bSLuigi Rizzo 			rdst->cur = nm_next(dst_cur, dst_lim);
848091fd0abSLuigi Rizzo 		}
84917885a7bSLuigi Rizzo 		/* if (sent) XXX txsync ? */
850091fd0abSLuigi Rizzo 	}
85117885a7bSLuigi Rizzo 	return sent;
852091fd0abSLuigi Rizzo }
853091fd0abSLuigi Rizzo 
854f18be576SLuigi Rizzo 
855091fd0abSLuigi Rizzo /*
856ce3ee1e7SLuigi Rizzo  * netmap_txsync_to_host() passes packets up. We are called from a
85702ad4083SLuigi Rizzo  * system call in user process context, and the only contention
85802ad4083SLuigi Rizzo  * can be among multiple user threads erroneously calling
859091fd0abSLuigi Rizzo  * this routine concurrently.
86068b8534bSLuigi Rizzo  */
861f9790aebSLuigi Rizzo void
862ce3ee1e7SLuigi Rizzo netmap_txsync_to_host(struct netmap_adapter *na)
86368b8534bSLuigi Rizzo {
864d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->tx_rings[na->num_tx_rings];
86568b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
86617885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
86717885a7bSLuigi Rizzo 	u_int const head = nm_txsync_prologue(kring);
868f9790aebSLuigi Rizzo 	struct mbq q;
869f9790aebSLuigi Rizzo 	int error;
87068b8534bSLuigi Rizzo 
871f9790aebSLuigi Rizzo 	error = nm_kr_tryget(kring);
872f9790aebSLuigi Rizzo 	if (error) {
873f9790aebSLuigi Rizzo 		if (error == NM_KR_BUSY)
874ce3ee1e7SLuigi Rizzo 			D("ring %p busy (user error)", kring);
87502ad4083SLuigi Rizzo 		return;
87602ad4083SLuigi Rizzo 	}
87717885a7bSLuigi Rizzo 	if (head > lim) {
878ce3ee1e7SLuigi Rizzo 		D("invalid ring index in stack TX kring %p", kring);
879ce3ee1e7SLuigi Rizzo 		netmap_ring_reinit(kring);
880ce3ee1e7SLuigi Rizzo 		nm_kr_put(kring);
881ce3ee1e7SLuigi Rizzo 		return;
882ce3ee1e7SLuigi Rizzo 	}
88368b8534bSLuigi Rizzo 
88417885a7bSLuigi Rizzo 	/* Take packets from hwcur to head and pass them up.
88517885a7bSLuigi Rizzo 	 * force head = cur since netmap_grab_packets() stops at head
88668b8534bSLuigi Rizzo 	 * In case of no buffers we give up. At the end of the loop,
88768b8534bSLuigi Rizzo 	 * the queue is drained in all cases.
88868b8534bSLuigi Rizzo 	 */
889f9790aebSLuigi Rizzo 	mbq_init(&q);
89017885a7bSLuigi Rizzo 	ring->cur = head;
89117885a7bSLuigi Rizzo 	netmap_grab_packets(kring, &q, 1 /* force */);
89217885a7bSLuigi Rizzo 	ND("have %d pkts in queue", mbq_len(&q));
89317885a7bSLuigi Rizzo 	kring->nr_hwcur = head;
89417885a7bSLuigi Rizzo 	kring->nr_hwtail = head + lim;
89517885a7bSLuigi Rizzo 	if (kring->nr_hwtail > lim)
89617885a7bSLuigi Rizzo 		kring->nr_hwtail -= lim + 1;
89717885a7bSLuigi Rizzo 	nm_txsync_finalize(kring);
89868b8534bSLuigi Rizzo 
899ce3ee1e7SLuigi Rizzo 	nm_kr_put(kring);
900f9790aebSLuigi Rizzo 	netmap_send_up(na->ifp, &q);
901f18be576SLuigi Rizzo }
902f18be576SLuigi Rizzo 
903f18be576SLuigi Rizzo 
90468b8534bSLuigi Rizzo /*
90502ad4083SLuigi Rizzo  * rxsync backend for packets coming from the host stack.
90617885a7bSLuigi Rizzo  * They have been put in kring->rx_queue by netmap_transmit().
90717885a7bSLuigi Rizzo  * We protect access to the kring using kring->rx_queue.lock
90802ad4083SLuigi Rizzo  *
90968b8534bSLuigi Rizzo  * This routine also does the selrecord if called from the poll handler
91068b8534bSLuigi Rizzo  * (we know because td != NULL).
91101c7d25fSLuigi Rizzo  *
91201c7d25fSLuigi Rizzo  * NOTE: on linux, selrecord() is defined as a macro and uses pwait
91301c7d25fSLuigi Rizzo  *     as an additional hidden argument.
91417885a7bSLuigi Rizzo  * returns the number of packets delivered to tx queues in
91517885a7bSLuigi Rizzo  * transparent mode, or a negative value if error
91668b8534bSLuigi Rizzo  */
91717885a7bSLuigi Rizzo int
918ce3ee1e7SLuigi Rizzo netmap_rxsync_from_host(struct netmap_adapter *na, struct thread *td, void *pwait)
91968b8534bSLuigi Rizzo {
920d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
92168b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
92217885a7bSLuigi Rizzo 	u_int nm_i, n;
92317885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
92417885a7bSLuigi Rizzo 	u_int const head = nm_rxsync_prologue(kring);
92517885a7bSLuigi Rizzo 	int ret = 0;
92617885a7bSLuigi Rizzo 	struct mbq *q = &kring->rx_queue;
92768b8534bSLuigi Rizzo 
92801c7d25fSLuigi Rizzo 	(void)pwait;	/* disable unused warnings */
929ce3ee1e7SLuigi Rizzo 
93017885a7bSLuigi Rizzo 	if (head > lim) {
93164ae02c3SLuigi Rizzo 		netmap_ring_reinit(kring);
93217885a7bSLuigi Rizzo 		return EINVAL;
93317885a7bSLuigi Rizzo 	}
93417885a7bSLuigi Rizzo 
93517885a7bSLuigi Rizzo 	if (kring->nkr_stopped) /* check a first time without lock */
93617885a7bSLuigi Rizzo 		return EBUSY;
93717885a7bSLuigi Rizzo 
93817885a7bSLuigi Rizzo 	mtx_lock(&q->lock);
93917885a7bSLuigi Rizzo 
94017885a7bSLuigi Rizzo 	if (kring->nkr_stopped) {  /* check again with lock held */
94117885a7bSLuigi Rizzo 		ret = EBUSY;
942ce3ee1e7SLuigi Rizzo 		goto unlock_out;
94364ae02c3SLuigi Rizzo 	}
94417885a7bSLuigi Rizzo 
94517885a7bSLuigi Rizzo 	/* First part: import newly received packets */
94617885a7bSLuigi Rizzo 	n = mbq_len(q);
94717885a7bSLuigi Rizzo 	if (n) { /* grab packets from the queue */
94817885a7bSLuigi Rizzo 		struct mbuf *m;
94917885a7bSLuigi Rizzo 		uint32_t stop_i;
95017885a7bSLuigi Rizzo 
95117885a7bSLuigi Rizzo 		nm_i = kring->nr_hwtail;
95217885a7bSLuigi Rizzo 		stop_i = nm_prev(nm_i, lim);
95317885a7bSLuigi Rizzo 		while ( nm_i != stop_i && (m = mbq_dequeue(q)) != NULL ) {
95417885a7bSLuigi Rizzo 			int len = MBUF_LEN(m);
95517885a7bSLuigi Rizzo 			struct netmap_slot *slot = &ring->slot[nm_i];
95617885a7bSLuigi Rizzo 
95717885a7bSLuigi Rizzo 			m_copydata(m, 0, len, BDG_NMB(na, slot));
95817885a7bSLuigi Rizzo 			ND("nm %d len %d", nm_i, len);
95917885a7bSLuigi Rizzo 			if (netmap_verbose)
96017885a7bSLuigi Rizzo                                 D("%s", nm_dump_buf(BDG_NMB(na, slot),len, 128, NULL));
96117885a7bSLuigi Rizzo 
96217885a7bSLuigi Rizzo 			slot->len = len;
96317885a7bSLuigi Rizzo 			slot->flags = kring->nkr_slot_flags;
96417885a7bSLuigi Rizzo 			nm_i = nm_next(nm_i, lim);
96564ae02c3SLuigi Rizzo 		}
96617885a7bSLuigi Rizzo 		kring->nr_hwtail = nm_i;
96764ae02c3SLuigi Rizzo 	}
96817885a7bSLuigi Rizzo 
96917885a7bSLuigi Rizzo 	/*
97017885a7bSLuigi Rizzo 	 * Second part: skip past packets that userspace has released.
97117885a7bSLuigi Rizzo 	 */
97217885a7bSLuigi Rizzo 	nm_i = kring->nr_hwcur;
97317885a7bSLuigi Rizzo 	if (nm_i != head) { /* something was released */
97417885a7bSLuigi Rizzo 		if (netmap_fwd || kring->ring->flags & NR_FORWARD)
97517885a7bSLuigi Rizzo 			ret = netmap_sw_to_nic(na);
97617885a7bSLuigi Rizzo 		kring->nr_hwcur = head;
97764ae02c3SLuigi Rizzo 	}
97817885a7bSLuigi Rizzo 
97917885a7bSLuigi Rizzo 	nm_rxsync_finalize(kring);
98017885a7bSLuigi Rizzo 
98117885a7bSLuigi Rizzo 	/* access copies of cur,tail in the kring */
98217885a7bSLuigi Rizzo 	if (kring->rcur == kring->rtail && td) /* no bufs available */
98368b8534bSLuigi Rizzo 		selrecord(td, &kring->si);
98417885a7bSLuigi Rizzo 
985ce3ee1e7SLuigi Rizzo unlock_out:
986ce3ee1e7SLuigi Rizzo 
98717885a7bSLuigi Rizzo 	mtx_unlock(&q->lock);
98817885a7bSLuigi Rizzo 	return ret;
98968b8534bSLuigi Rizzo }
99068b8534bSLuigi Rizzo 
99168b8534bSLuigi Rizzo 
992f9790aebSLuigi Rizzo /* Get a netmap adapter for the port.
993f9790aebSLuigi Rizzo  *
994f9790aebSLuigi Rizzo  * If it is possible to satisfy the request, return 0
995f9790aebSLuigi Rizzo  * with *na containing the netmap adapter found.
996f9790aebSLuigi Rizzo  * Otherwise return an error code, with *na containing NULL.
997f9790aebSLuigi Rizzo  *
998f9790aebSLuigi Rizzo  * When the port is attached to a bridge, we always return
999f9790aebSLuigi Rizzo  * EBUSY.
1000f9790aebSLuigi Rizzo  * Otherwise, if the port is already bound to a file descriptor,
1001f9790aebSLuigi Rizzo  * then we unconditionally return the existing adapter into *na.
1002f9790aebSLuigi Rizzo  * In all the other cases, we return (into *na) either native,
1003f9790aebSLuigi Rizzo  * generic or NULL, according to the following table:
1004f9790aebSLuigi Rizzo  *
1005f9790aebSLuigi Rizzo  *					native_support
1006f9790aebSLuigi Rizzo  * active_fds   dev.netmap.admode         YES     NO
1007f9790aebSLuigi Rizzo  * -------------------------------------------------------
1008f9790aebSLuigi Rizzo  *    >0              *                 NA(ifp) NA(ifp)
1009f9790aebSLuigi Rizzo  *
1010f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_BEST      NATIVE  GENERIC
1011f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_NATIVE    NATIVE   NULL
1012f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_GENERIC   GENERIC GENERIC
1013f9790aebSLuigi Rizzo  *
1014f9790aebSLuigi Rizzo  */
1015f9790aebSLuigi Rizzo 
1016f9790aebSLuigi Rizzo int
1017f9790aebSLuigi Rizzo netmap_get_hw_na(struct ifnet *ifp, struct netmap_adapter **na)
1018f9790aebSLuigi Rizzo {
1019f9790aebSLuigi Rizzo 	/* generic support */
1020f9790aebSLuigi Rizzo 	int i = netmap_admode;	/* Take a snapshot. */
1021f9790aebSLuigi Rizzo 	int error = 0;
1022f9790aebSLuigi Rizzo 	struct netmap_adapter *prev_na;
1023f9790aebSLuigi Rizzo 	struct netmap_generic_adapter *gna;
1024f9790aebSLuigi Rizzo 
1025f9790aebSLuigi Rizzo 	*na = NULL; /* default */
1026f9790aebSLuigi Rizzo 
1027f9790aebSLuigi Rizzo 	/* reset in case of invalid value */
1028f9790aebSLuigi Rizzo 	if (i < NETMAP_ADMODE_BEST || i >= NETMAP_ADMODE_LAST)
1029f9790aebSLuigi Rizzo 		i = netmap_admode = NETMAP_ADMODE_BEST;
1030f9790aebSLuigi Rizzo 
1031f9790aebSLuigi Rizzo 	if (NETMAP_CAPABLE(ifp)) {
1032f9790aebSLuigi Rizzo 		/* If an adapter already exists, but is
1033f9790aebSLuigi Rizzo 		 * attached to a vale port, we report that the
1034f9790aebSLuigi Rizzo 		 * port is busy.
1035f9790aebSLuigi Rizzo 		 */
1036f9790aebSLuigi Rizzo 		if (NETMAP_OWNED_BY_KERN(NA(ifp)))
1037f9790aebSLuigi Rizzo 			return EBUSY;
1038f9790aebSLuigi Rizzo 
1039f9790aebSLuigi Rizzo 		/* If an adapter already exists, return it if
1040f9790aebSLuigi Rizzo 		 * there are active file descriptors or if
1041f9790aebSLuigi Rizzo 		 * netmap is not forced to use generic
1042f9790aebSLuigi Rizzo 		 * adapters.
1043f9790aebSLuigi Rizzo 		 */
1044f9790aebSLuigi Rizzo 		if (NA(ifp)->active_fds > 0 ||
1045f9790aebSLuigi Rizzo 				i != NETMAP_ADMODE_GENERIC) {
1046f9790aebSLuigi Rizzo 			*na = NA(ifp);
1047f9790aebSLuigi Rizzo 			return 0;
1048f9790aebSLuigi Rizzo 		}
1049f9790aebSLuigi Rizzo 	}
1050f9790aebSLuigi Rizzo 
1051f9790aebSLuigi Rizzo 	/* If there isn't native support and netmap is not allowed
1052f9790aebSLuigi Rizzo 	 * to use generic adapters, we cannot satisfy the request.
1053f9790aebSLuigi Rizzo 	 */
1054f9790aebSLuigi Rizzo 	if (!NETMAP_CAPABLE(ifp) && i == NETMAP_ADMODE_NATIVE)
1055*f2637526SLuigi Rizzo 		return EOPNOTSUPP;
1056f9790aebSLuigi Rizzo 
1057f9790aebSLuigi Rizzo 	/* Otherwise, create a generic adapter and return it,
1058f9790aebSLuigi Rizzo 	 * saving the previously used netmap adapter, if any.
1059f9790aebSLuigi Rizzo 	 *
1060f9790aebSLuigi Rizzo 	 * Note that here 'prev_na', if not NULL, MUST be a
1061f9790aebSLuigi Rizzo 	 * native adapter, and CANNOT be a generic one. This is
1062f9790aebSLuigi Rizzo 	 * true because generic adapters are created on demand, and
1063f9790aebSLuigi Rizzo 	 * destroyed when not used anymore. Therefore, if the adapter
1064f9790aebSLuigi Rizzo 	 * currently attached to an interface 'ifp' is generic, it
1065f9790aebSLuigi Rizzo 	 * must be that
1066f9790aebSLuigi Rizzo 	 * (NA(ifp)->active_fds > 0 || NETMAP_OWNED_BY_KERN(NA(ifp))).
1067f9790aebSLuigi Rizzo 	 * Consequently, if NA(ifp) is generic, we will enter one of
1068f9790aebSLuigi Rizzo 	 * the branches above. This ensures that we never override
1069f9790aebSLuigi Rizzo 	 * a generic adapter with another generic adapter.
1070f9790aebSLuigi Rizzo 	 */
1071f9790aebSLuigi Rizzo 	prev_na = NA(ifp);
1072f9790aebSLuigi Rizzo 	error = generic_netmap_attach(ifp);
1073f9790aebSLuigi Rizzo 	if (error)
1074f9790aebSLuigi Rizzo 		return error;
1075f9790aebSLuigi Rizzo 
1076f9790aebSLuigi Rizzo 	*na = NA(ifp);
1077f9790aebSLuigi Rizzo 	gna = (struct netmap_generic_adapter*)NA(ifp);
1078f9790aebSLuigi Rizzo 	gna->prev = prev_na; /* save old na */
1079f9790aebSLuigi Rizzo 	if (prev_na != NULL) {
1080f9790aebSLuigi Rizzo 		ifunit_ref(ifp->if_xname);
1081f9790aebSLuigi Rizzo 		// XXX add a refcount ?
1082f9790aebSLuigi Rizzo 		netmap_adapter_get(prev_na);
1083f9790aebSLuigi Rizzo 	}
108417885a7bSLuigi Rizzo 	ND("Created generic NA %p (prev %p)", gna, gna->prev);
1085f9790aebSLuigi Rizzo 
1086f9790aebSLuigi Rizzo 	return 0;
1087f9790aebSLuigi Rizzo }
1088f9790aebSLuigi Rizzo 
1089f9790aebSLuigi Rizzo 
109068b8534bSLuigi Rizzo /*
1091ce3ee1e7SLuigi Rizzo  * MUST BE CALLED UNDER NMG_LOCK()
1092ce3ee1e7SLuigi Rizzo  *
1093*f2637526SLuigi Rizzo  * Get a refcounted reference to a netmap adapter attached
1094*f2637526SLuigi Rizzo  * to the interface specified by nmr.
1095ce3ee1e7SLuigi Rizzo  * This is always called in the execution of an ioctl().
1096ce3ee1e7SLuigi Rizzo  *
1097*f2637526SLuigi Rizzo  * Return ENXIO if the interface specified by the request does
1098*f2637526SLuigi Rizzo  * not exist, ENOTSUP if netmap is not supported by the interface,
1099*f2637526SLuigi Rizzo  * EBUSY if the interface is already attached to a bridge,
1100*f2637526SLuigi Rizzo  * EINVAL if parameters are invalid, ENOMEM if needed resources
1101*f2637526SLuigi Rizzo  * could not be allocated.
1102*f2637526SLuigi Rizzo  * If successful, hold a reference to the netmap adapter.
1103f18be576SLuigi Rizzo  *
1104*f2637526SLuigi Rizzo  * No reference is kept on the real interface, which may then
1105*f2637526SLuigi Rizzo  * disappear at any time.
110668b8534bSLuigi Rizzo  */
1107f9790aebSLuigi Rizzo int
1108f9790aebSLuigi Rizzo netmap_get_na(struct nmreq *nmr, struct netmap_adapter **na, int create)
110968b8534bSLuigi Rizzo {
1110f9790aebSLuigi Rizzo 	struct ifnet *ifp;
1111f9790aebSLuigi Rizzo 	int error = 0;
1112f9790aebSLuigi Rizzo 	struct netmap_adapter *ret;
1113f9790aebSLuigi Rizzo 
1114f9790aebSLuigi Rizzo 	*na = NULL;     /* default return value */
1115f196ce38SLuigi Rizzo 
1116ce3ee1e7SLuigi Rizzo 	/* first try to see if this is a bridge port. */
1117ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
1118ce3ee1e7SLuigi Rizzo 
1119f9790aebSLuigi Rizzo 	error = netmap_get_bdg_na(nmr, na, create);
1120f9790aebSLuigi Rizzo 	if (error || *na != NULL) /* valid match in netmap_get_bdg_na() */
1121f9790aebSLuigi Rizzo 		return error;
1122ce3ee1e7SLuigi Rizzo 
1123f9790aebSLuigi Rizzo 	ifp = ifunit_ref(nmr->nr_name);
1124f9790aebSLuigi Rizzo 	if (ifp == NULL) {
1125ce3ee1e7SLuigi Rizzo 	        return ENXIO;
1126f196ce38SLuigi Rizzo 	}
1127ce3ee1e7SLuigi Rizzo 
1128f9790aebSLuigi Rizzo 	error = netmap_get_hw_na(ifp, &ret);
1129f9790aebSLuigi Rizzo 	if (error)
1130f9790aebSLuigi Rizzo 		goto out;
1131f18be576SLuigi Rizzo 
1132f9790aebSLuigi Rizzo 	if (ret != NULL) {
1133f9790aebSLuigi Rizzo 		/* Users cannot use the NIC attached to a bridge directly */
1134f9790aebSLuigi Rizzo 		if (NETMAP_OWNED_BY_KERN(ret)) {
1135*f2637526SLuigi Rizzo 			error = EBUSY;
1136f9790aebSLuigi Rizzo 			goto out;
1137ce3ee1e7SLuigi Rizzo 		}
1138f9790aebSLuigi Rizzo 		error = 0;
1139f9790aebSLuigi Rizzo 		*na = ret;
1140f9790aebSLuigi Rizzo 		netmap_adapter_get(ret);
1141f9790aebSLuigi Rizzo 	}
1142f9790aebSLuigi Rizzo out:
1143f9790aebSLuigi Rizzo 	if_rele(ifp);
1144f18be576SLuigi Rizzo 
11455ab0d24dSLuigi Rizzo 	return error;
11465ab0d24dSLuigi Rizzo }
1147ce3ee1e7SLuigi Rizzo 
1148ce3ee1e7SLuigi Rizzo 
1149f9790aebSLuigi Rizzo /*
1150f9790aebSLuigi Rizzo  * validate parameters on entry for *_txsync()
1151f9790aebSLuigi Rizzo  * Returns ring->cur if ok, or something >= kring->nkr_num_slots
115217885a7bSLuigi Rizzo  * in case of error.
1153f9790aebSLuigi Rizzo  *
115417885a7bSLuigi Rizzo  * rhead, rcur and rtail=hwtail are stored from previous round.
115517885a7bSLuigi Rizzo  * hwcur is the next packet to send to the ring.
1156f9790aebSLuigi Rizzo  *
115717885a7bSLuigi Rizzo  * We want
115817885a7bSLuigi Rizzo  *    hwcur <= *rhead <= head <= cur <= tail = *rtail <= hwtail
1159f9790aebSLuigi Rizzo  *
116017885a7bSLuigi Rizzo  * hwcur, rhead, rtail and hwtail are reliable
1161f9790aebSLuigi Rizzo  */
1162f9790aebSLuigi Rizzo u_int
116317885a7bSLuigi Rizzo nm_txsync_prologue(struct netmap_kring *kring)
1164f9790aebSLuigi Rizzo {
1165f9790aebSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
116617885a7bSLuigi Rizzo 	u_int head = ring->head; /* read only once */
1167f9790aebSLuigi Rizzo 	u_int cur = ring->cur; /* read only once */
1168f9790aebSLuigi Rizzo 	u_int n = kring->nkr_num_slots;
1169ce3ee1e7SLuigi Rizzo 
117017885a7bSLuigi Rizzo 	ND(5, "%s kcur %d ktail %d head %d cur %d tail %d",
117117885a7bSLuigi Rizzo 		kring->name,
117217885a7bSLuigi Rizzo 		kring->nr_hwcur, kring->nr_hwtail,
117317885a7bSLuigi Rizzo 		ring->head, ring->cur, ring->tail);
117417885a7bSLuigi Rizzo #if 1 /* kernel sanity checks; but we can trust the kring. */
117517885a7bSLuigi Rizzo 	if (kring->nr_hwcur >= n || kring->rhead >= n ||
117617885a7bSLuigi Rizzo 	    kring->rtail >= n ||  kring->nr_hwtail >= n)
1177f9790aebSLuigi Rizzo 		goto error;
1178f9790aebSLuigi Rizzo #endif /* kernel sanity checks */
117917885a7bSLuigi Rizzo 	/*
118017885a7bSLuigi Rizzo 	 * user sanity checks. We only use 'cur',
118117885a7bSLuigi Rizzo 	 * A, B, ... are possible positions for cur:
118217885a7bSLuigi Rizzo 	 *
118317885a7bSLuigi Rizzo 	 *  0    A  cur   B  tail  C  n-1
118417885a7bSLuigi Rizzo 	 *  0    D  tail  E  cur   F  n-1
118517885a7bSLuigi Rizzo 	 *
118617885a7bSLuigi Rizzo 	 * B, F, D are valid. A, C, E are wrong
118717885a7bSLuigi Rizzo 	 */
118817885a7bSLuigi Rizzo 	if (kring->rtail >= kring->rhead) {
118917885a7bSLuigi Rizzo 		/* want rhead <= head <= rtail */
119017885a7bSLuigi Rizzo 		if (head < kring->rhead || head > kring->rtail)
1191f9790aebSLuigi Rizzo 			goto error;
119217885a7bSLuigi Rizzo 		/* and also head <= cur <= rtail */
119317885a7bSLuigi Rizzo 		if (cur < head || cur > kring->rtail)
1194f9790aebSLuigi Rizzo 			goto error;
119517885a7bSLuigi Rizzo 	} else { /* here rtail < rhead */
119617885a7bSLuigi Rizzo 		/* we need head outside rtail .. rhead */
119717885a7bSLuigi Rizzo 		if (head > kring->rtail && head < kring->rhead)
119817885a7bSLuigi Rizzo 			goto error;
119917885a7bSLuigi Rizzo 
120017885a7bSLuigi Rizzo 		/* two cases now: head <= rtail or head >= rhead  */
120117885a7bSLuigi Rizzo 		if (head <= kring->rtail) {
120217885a7bSLuigi Rizzo 			/* want head <= cur <= rtail */
120317885a7bSLuigi Rizzo 			if (cur < head || cur > kring->rtail)
120417885a7bSLuigi Rizzo 				goto error;
120517885a7bSLuigi Rizzo 		} else { /* head >= rhead */
120617885a7bSLuigi Rizzo 			/* cur must be outside rtail..head */
120717885a7bSLuigi Rizzo 			if (cur > kring->rtail && cur < head)
120817885a7bSLuigi Rizzo 				goto error;
1209f18be576SLuigi Rizzo 		}
1210f9790aebSLuigi Rizzo 	}
121117885a7bSLuigi Rizzo 	if (ring->tail != kring->rtail) {
121217885a7bSLuigi Rizzo 		RD(5, "tail overwritten was %d need %d",
121317885a7bSLuigi Rizzo 			ring->tail, kring->rtail);
121417885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
121517885a7bSLuigi Rizzo 	}
121617885a7bSLuigi Rizzo 	kring->rhead = head;
121717885a7bSLuigi Rizzo 	kring->rcur = cur;
121817885a7bSLuigi Rizzo 	return head;
1219f9790aebSLuigi Rizzo 
1220f9790aebSLuigi Rizzo error:
122117885a7bSLuigi Rizzo 	RD(5, "%s kring error: hwcur %d rcur %d hwtail %d cur %d tail %d",
122217885a7bSLuigi Rizzo 		kring->name,
1223f9790aebSLuigi Rizzo 		kring->nr_hwcur,
122417885a7bSLuigi Rizzo 		kring->rcur, kring->nr_hwtail,
122517885a7bSLuigi Rizzo 		cur, ring->tail);
1226f9790aebSLuigi Rizzo 	return n;
122768b8534bSLuigi Rizzo }
122868b8534bSLuigi Rizzo 
122968b8534bSLuigi Rizzo 
123068b8534bSLuigi Rizzo /*
1231f9790aebSLuigi Rizzo  * validate parameters on entry for *_rxsync()
123217885a7bSLuigi Rizzo  * Returns ring->head if ok, kring->nkr_num_slots on error.
1233f9790aebSLuigi Rizzo  *
123417885a7bSLuigi Rizzo  * For a valid configuration,
123517885a7bSLuigi Rizzo  * hwcur <= head <= cur <= tail <= hwtail
1236f9790aebSLuigi Rizzo  *
123717885a7bSLuigi Rizzo  * We only consider head and cur.
123817885a7bSLuigi Rizzo  * hwcur and hwtail are reliable.
1239f9790aebSLuigi Rizzo  *
1240f9790aebSLuigi Rizzo  */
1241f9790aebSLuigi Rizzo u_int
124217885a7bSLuigi Rizzo nm_rxsync_prologue(struct netmap_kring *kring)
1243f9790aebSLuigi Rizzo {
1244f9790aebSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
124517885a7bSLuigi Rizzo 	uint32_t const n = kring->nkr_num_slots;
124617885a7bSLuigi Rizzo 	uint32_t head, cur;
1247f9790aebSLuigi Rizzo 
124817885a7bSLuigi Rizzo 	ND("%s kc %d kt %d h %d c %d t %d",
124917885a7bSLuigi Rizzo 		kring->name,
125017885a7bSLuigi Rizzo 		kring->nr_hwcur, kring->nr_hwtail,
125117885a7bSLuigi Rizzo 		ring->head, ring->cur, ring->tail);
125217885a7bSLuigi Rizzo 	/*
125317885a7bSLuigi Rizzo 	 * Before storing the new values, we should check they do not
125417885a7bSLuigi Rizzo 	 * move backwards. However:
125517885a7bSLuigi Rizzo 	 * - head is not an issue because the previous value is hwcur;
125617885a7bSLuigi Rizzo 	 * - cur could in principle go back, however it does not matter
125717885a7bSLuigi Rizzo 	 *   because we are processing a brand new rxsync()
125817885a7bSLuigi Rizzo 	 */
125917885a7bSLuigi Rizzo 	cur = kring->rcur = ring->cur;	/* read only once */
126017885a7bSLuigi Rizzo 	head = kring->rhead = ring->head;	/* read only once */
1261f9790aebSLuigi Rizzo #if 1 /* kernel sanity checks */
126217885a7bSLuigi Rizzo 	if (kring->nr_hwcur >= n || kring->nr_hwtail >= n)
1263f9790aebSLuigi Rizzo 		goto error;
1264f9790aebSLuigi Rizzo #endif /* kernel sanity checks */
1265f9790aebSLuigi Rizzo 	/* user sanity checks */
126617885a7bSLuigi Rizzo 	if (kring->nr_hwtail >= kring->nr_hwcur) {
126717885a7bSLuigi Rizzo 		/* want hwcur <= rhead <= hwtail */
126817885a7bSLuigi Rizzo 		if (head < kring->nr_hwcur || head > kring->nr_hwtail)
1269f9790aebSLuigi Rizzo 			goto error;
127017885a7bSLuigi Rizzo 		/* and also rhead <= rcur <= hwtail */
127117885a7bSLuigi Rizzo 		if (cur < head || cur > kring->nr_hwtail)
1272f9790aebSLuigi Rizzo 			goto error;
1273f9790aebSLuigi Rizzo 	} else {
127417885a7bSLuigi Rizzo 		/* we need rhead outside hwtail..hwcur */
127517885a7bSLuigi Rizzo 		if (head < kring->nr_hwcur && head > kring->nr_hwtail)
1276f9790aebSLuigi Rizzo 			goto error;
127717885a7bSLuigi Rizzo 		/* two cases now: head <= hwtail or head >= hwcur  */
127817885a7bSLuigi Rizzo 		if (head <= kring->nr_hwtail) {
127917885a7bSLuigi Rizzo 			/* want head <= cur <= hwtail */
128017885a7bSLuigi Rizzo 			if (cur < head || cur > kring->nr_hwtail)
1281f9790aebSLuigi Rizzo 				goto error;
128217885a7bSLuigi Rizzo 		} else {
128317885a7bSLuigi Rizzo 			/* cur must be outside hwtail..head */
128417885a7bSLuigi Rizzo 			if (cur < head && cur > kring->nr_hwtail)
1285f9790aebSLuigi Rizzo 				goto error;
1286f9790aebSLuigi Rizzo 		}
1287f9790aebSLuigi Rizzo 	}
128817885a7bSLuigi Rizzo 	if (ring->tail != kring->rtail) {
128917885a7bSLuigi Rizzo 		RD(5, "%s tail overwritten was %d need %d",
129017885a7bSLuigi Rizzo 			kring->name,
129117885a7bSLuigi Rizzo 			ring->tail, kring->rtail);
129217885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
129317885a7bSLuigi Rizzo 	}
129417885a7bSLuigi Rizzo 	return head;
1295f9790aebSLuigi Rizzo 
1296f9790aebSLuigi Rizzo error:
129717885a7bSLuigi Rizzo 	RD(5, "kring error: hwcur %d rcur %d hwtail %d head %d cur %d tail %d",
1298f9790aebSLuigi Rizzo 		kring->nr_hwcur,
129917885a7bSLuigi Rizzo 		kring->rcur, kring->nr_hwtail,
130017885a7bSLuigi Rizzo 		kring->rhead, kring->rcur, ring->tail);
1301f9790aebSLuigi Rizzo 	return n;
1302f9790aebSLuigi Rizzo }
1303f9790aebSLuigi Rizzo 
130417885a7bSLuigi Rizzo 
1305f9790aebSLuigi Rizzo /*
130668b8534bSLuigi Rizzo  * Error routine called when txsync/rxsync detects an error.
130717885a7bSLuigi Rizzo  * Can't do much more than resetting head =cur = hwcur, tail = hwtail
130868b8534bSLuigi Rizzo  * Return 1 on reinit.
1309506cc70cSLuigi Rizzo  *
1310506cc70cSLuigi Rizzo  * This routine is only called by the upper half of the kernel.
1311506cc70cSLuigi Rizzo  * It only reads hwcur (which is changed only by the upper half, too)
131217885a7bSLuigi Rizzo  * and hwtail (which may be changed by the lower half, but only on
1313506cc70cSLuigi Rizzo  * a tx ring and only to increase it, so any error will be recovered
1314506cc70cSLuigi Rizzo  * on the next call). For the above, we don't strictly need to call
1315506cc70cSLuigi Rizzo  * it under lock.
131668b8534bSLuigi Rizzo  */
131768b8534bSLuigi Rizzo int
131868b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring)
131968b8534bSLuigi Rizzo {
132068b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
132168b8534bSLuigi Rizzo 	u_int i, lim = kring->nkr_num_slots - 1;
132268b8534bSLuigi Rizzo 	int errors = 0;
132368b8534bSLuigi Rizzo 
1324ce3ee1e7SLuigi Rizzo 	// XXX KASSERT nm_kr_tryget
1325f9790aebSLuigi Rizzo 	RD(10, "called for %s", NM_IFPNAME(kring->na->ifp));
132617885a7bSLuigi Rizzo 	// XXX probably wrong to trust userspace
132717885a7bSLuigi Rizzo 	kring->rhead = ring->head;
132817885a7bSLuigi Rizzo 	kring->rcur  = ring->cur;
132917885a7bSLuigi Rizzo 	kring->rtail = ring->tail;
133017885a7bSLuigi Rizzo 
133168b8534bSLuigi Rizzo 	if (ring->cur > lim)
133268b8534bSLuigi Rizzo 		errors++;
133317885a7bSLuigi Rizzo 	if (ring->head > lim)
133417885a7bSLuigi Rizzo 		errors++;
133517885a7bSLuigi Rizzo 	if (ring->tail > lim)
133617885a7bSLuigi Rizzo 		errors++;
133768b8534bSLuigi Rizzo 	for (i = 0; i <= lim; i++) {
133868b8534bSLuigi Rizzo 		u_int idx = ring->slot[i].buf_idx;
133968b8534bSLuigi Rizzo 		u_int len = ring->slot[i].len;
134068b8534bSLuigi Rizzo 		if (idx < 2 || idx >= netmap_total_buffers) {
134117885a7bSLuigi Rizzo 			RD(5, "bad index at slot %d idx %d len %d ", i, idx, len);
134268b8534bSLuigi Rizzo 			ring->slot[i].buf_idx = 0;
134368b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
1344ce3ee1e7SLuigi Rizzo 		} else if (len > NETMAP_BDG_BUF_SIZE(kring->na->nm_mem)) {
134568b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
134617885a7bSLuigi Rizzo 			RD(5, "bad len at slot %d idx %d len %d", i, idx, len);
134768b8534bSLuigi Rizzo 		}
134868b8534bSLuigi Rizzo 	}
134968b8534bSLuigi Rizzo 	if (errors) {
13508241616dSLuigi Rizzo 		RD(10, "total %d errors", errors);
135117885a7bSLuigi Rizzo 		RD(10, "%s reinit, cur %d -> %d tail %d -> %d",
135217885a7bSLuigi Rizzo 			kring->name,
135368b8534bSLuigi Rizzo 			ring->cur, kring->nr_hwcur,
135417885a7bSLuigi Rizzo 			ring->tail, kring->nr_hwtail);
135517885a7bSLuigi Rizzo 		ring->head = kring->rhead = kring->nr_hwcur;
135617885a7bSLuigi Rizzo 		ring->cur  = kring->rcur  = kring->nr_hwcur;
135717885a7bSLuigi Rizzo 		ring->tail = kring->rtail = kring->nr_hwtail;
135868b8534bSLuigi Rizzo 	}
135968b8534bSLuigi Rizzo 	return (errors ? 1 : 0);
136068b8534bSLuigi Rizzo }
136168b8534bSLuigi Rizzo 
136268b8534bSLuigi Rizzo 
136368b8534bSLuigi Rizzo /*
136468b8534bSLuigi Rizzo  * Set the ring ID. For devices with a single queue, a request
136568b8534bSLuigi Rizzo  * for all rings is the same as a single ring.
136668b8534bSLuigi Rizzo  */
136768b8534bSLuigi Rizzo static int
136868b8534bSLuigi Rizzo netmap_set_ringid(struct netmap_priv_d *priv, u_int ringid)
136968b8534bSLuigi Rizzo {
1370f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1371f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
137268b8534bSLuigi Rizzo 	u_int i = ringid & NETMAP_RING_MASK;
137364ae02c3SLuigi Rizzo 	/* initially (np_qfirst == np_qlast) we don't want to lock */
1374ce3ee1e7SLuigi Rizzo 	u_int lim = na->num_rx_rings;
137568b8534bSLuigi Rizzo 
1376d76bf4ffSLuigi Rizzo 	if (na->num_tx_rings > lim)
1377d76bf4ffSLuigi Rizzo 		lim = na->num_tx_rings;
137864ae02c3SLuigi Rizzo 	if ( (ringid & NETMAP_HW_RING) && i >= lim) {
137968b8534bSLuigi Rizzo 		D("invalid ring id %d", i);
138068b8534bSLuigi Rizzo 		return (EINVAL);
138168b8534bSLuigi Rizzo 	}
138268b8534bSLuigi Rizzo 	priv->np_ringid = ringid;
138368b8534bSLuigi Rizzo 	if (ringid & NETMAP_SW_RING) {
138464ae02c3SLuigi Rizzo 		priv->np_qfirst = NETMAP_SW_RING;
138564ae02c3SLuigi Rizzo 		priv->np_qlast = 0;
138668b8534bSLuigi Rizzo 	} else if (ringid & NETMAP_HW_RING) {
138768b8534bSLuigi Rizzo 		priv->np_qfirst = i;
138868b8534bSLuigi Rizzo 		priv->np_qlast = i + 1;
138968b8534bSLuigi Rizzo 	} else {
139068b8534bSLuigi Rizzo 		priv->np_qfirst = 0;
139164ae02c3SLuigi Rizzo 		priv->np_qlast = NETMAP_HW_RING ;
139268b8534bSLuigi Rizzo 	}
139368b8534bSLuigi Rizzo 	priv->np_txpoll = (ringid & NETMAP_NO_TX_POLL) ? 0 : 1;
1394ae10d1afSLuigi Rizzo     if (netmap_verbose) {
139568b8534bSLuigi Rizzo 	if (ringid & NETMAP_SW_RING)
1396f9790aebSLuigi Rizzo 		D("ringid %s set to SW RING", NM_IFPNAME(ifp));
139768b8534bSLuigi Rizzo 	else if (ringid & NETMAP_HW_RING)
1398f9790aebSLuigi Rizzo 		D("ringid %s set to HW RING %d", NM_IFPNAME(ifp),
139968b8534bSLuigi Rizzo 			priv->np_qfirst);
140068b8534bSLuigi Rizzo 	else
1401f9790aebSLuigi Rizzo 		D("ringid %s set to all %d HW RINGS", NM_IFPNAME(ifp), lim);
1402ae10d1afSLuigi Rizzo     }
140368b8534bSLuigi Rizzo 	return 0;
140468b8534bSLuigi Rizzo }
140568b8534bSLuigi Rizzo 
1406f18be576SLuigi Rizzo 
1407f18be576SLuigi Rizzo /*
1408f18be576SLuigi Rizzo  * possibly move the interface to netmap-mode.
1409f18be576SLuigi Rizzo  * If success it returns a pointer to netmap_if, otherwise NULL.
1410ce3ee1e7SLuigi Rizzo  * This must be called with NMG_LOCK held.
1411f18be576SLuigi Rizzo  */
1412f9790aebSLuigi Rizzo struct netmap_if *
1413f9790aebSLuigi Rizzo netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na,
1414f18be576SLuigi Rizzo 	uint16_t ringid, int *err)
1415f18be576SLuigi Rizzo {
1416f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
1417f18be576SLuigi Rizzo 	struct netmap_if *nifp = NULL;
1418f9790aebSLuigi Rizzo 	int error, need_mem = 0;
1419f18be576SLuigi Rizzo 
1420ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
1421f18be576SLuigi Rizzo 	/* ring configuration may have changed, fetch from the card */
1422f18be576SLuigi Rizzo 	netmap_update_config(na);
1423f9790aebSLuigi Rizzo 	priv->np_na = na;     /* store the reference */
1424f18be576SLuigi Rizzo 	error = netmap_set_ringid(priv, ringid);
1425f18be576SLuigi Rizzo 	if (error)
1426f18be576SLuigi Rizzo 		goto out;
1427ce3ee1e7SLuigi Rizzo 	/* ensure allocators are ready */
1428ce3ee1e7SLuigi Rizzo 	need_mem = !netmap_have_memory_locked(priv);
1429ce3ee1e7SLuigi Rizzo 	if (need_mem) {
1430ce3ee1e7SLuigi Rizzo 		error = netmap_get_memory_locked(priv);
1431ce3ee1e7SLuigi Rizzo 		ND("get_memory returned %d", error);
1432ce3ee1e7SLuigi Rizzo 		if (error)
1433ce3ee1e7SLuigi Rizzo 			goto out;
1434ce3ee1e7SLuigi Rizzo 	}
1435f9790aebSLuigi Rizzo 	nifp = netmap_if_new(NM_IFPNAME(ifp), na);
1436f18be576SLuigi Rizzo 	if (nifp == NULL) { /* allocation failed */
1437ce3ee1e7SLuigi Rizzo 		/* we should drop the allocator, but only
1438ce3ee1e7SLuigi Rizzo 		 * if we were the ones who grabbed it
1439ce3ee1e7SLuigi Rizzo 		 */
1440f18be576SLuigi Rizzo 		error = ENOMEM;
1441ce3ee1e7SLuigi Rizzo 		goto out;
1442ce3ee1e7SLuigi Rizzo 	}
1443f9790aebSLuigi Rizzo 	na->active_fds++;
1444ce3ee1e7SLuigi Rizzo 	if (ifp->if_capenable & IFCAP_NETMAP) {
1445f18be576SLuigi Rizzo 		/* was already set */
1446f18be576SLuigi Rizzo 	} else {
1447f18be576SLuigi Rizzo 		/* Otherwise set the card in netmap mode
1448f18be576SLuigi Rizzo 		 * and make it use the shared buffers.
1449ce3ee1e7SLuigi Rizzo 		 *
1450ce3ee1e7SLuigi Rizzo 		 * do not core lock because the race is harmless here,
1451ce3ee1e7SLuigi Rizzo 		 * there cannot be any traffic to netmap_transmit()
1452ce3ee1e7SLuigi Rizzo 		 */
1453f9790aebSLuigi Rizzo 		na->na_lut = na->nm_mem->pools[NETMAP_BUF_POOL].lut;
1454f9790aebSLuigi Rizzo 		ND("%p->na_lut == %p", na, na->na_lut);
1455f9790aebSLuigi Rizzo 		na->na_lut_objtotal = na->nm_mem->pools[NETMAP_BUF_POOL].objtotal;
1456f9790aebSLuigi Rizzo 		error = na->nm_register(na, 1); /* mode on */
1457f18be576SLuigi Rizzo 		if (error) {
1458ce3ee1e7SLuigi Rizzo 			netmap_do_unregif(priv, nifp);
1459f18be576SLuigi Rizzo 			nifp = NULL;
1460f18be576SLuigi Rizzo 		}
1461f18be576SLuigi Rizzo 	}
1462f18be576SLuigi Rizzo out:
1463f18be576SLuigi Rizzo 	*err = error;
1464f9790aebSLuigi Rizzo 	if (error) {
1465f9790aebSLuigi Rizzo 		priv->np_na = NULL;
1466f9790aebSLuigi Rizzo 		if (need_mem)
1467f9790aebSLuigi Rizzo 			netmap_drop_memory_locked(priv);
1468f9790aebSLuigi Rizzo 	}
1469ce3ee1e7SLuigi Rizzo 	if (nifp != NULL) {
1470ce3ee1e7SLuigi Rizzo 		/*
1471ce3ee1e7SLuigi Rizzo 		 * advertise that the interface is ready bt setting ni_nifp.
1472ce3ee1e7SLuigi Rizzo 		 * The barrier is needed because readers (poll and *SYNC)
1473ce3ee1e7SLuigi Rizzo 		 * check for priv->np_nifp != NULL without locking
1474ce3ee1e7SLuigi Rizzo 		 */
1475ce3ee1e7SLuigi Rizzo 		wmb(); /* make sure previous writes are visible to all CPUs */
1476ce3ee1e7SLuigi Rizzo 		priv->np_nifp = nifp;
1477ce3ee1e7SLuigi Rizzo 	}
1478f18be576SLuigi Rizzo 	return nifp;
1479f18be576SLuigi Rizzo }
1480f18be576SLuigi Rizzo 
1481f18be576SLuigi Rizzo 
1482f18be576SLuigi Rizzo 
148368b8534bSLuigi Rizzo /*
148468b8534bSLuigi Rizzo  * ioctl(2) support for the "netmap" device.
148568b8534bSLuigi Rizzo  *
148668b8534bSLuigi Rizzo  * Following a list of accepted commands:
148768b8534bSLuigi Rizzo  * - NIOCGINFO
148868b8534bSLuigi Rizzo  * - SIOCGIFADDR	just for convenience
148968b8534bSLuigi Rizzo  * - NIOCREGIF
149068b8534bSLuigi Rizzo  * - NIOCTXSYNC
149168b8534bSLuigi Rizzo  * - NIOCRXSYNC
149268b8534bSLuigi Rizzo  *
149368b8534bSLuigi Rizzo  * Return 0 on success, errno otherwise.
149468b8534bSLuigi Rizzo  */
1495f9790aebSLuigi Rizzo int
14960b8ed8e0SLuigi Rizzo netmap_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
14970b8ed8e0SLuigi Rizzo 	int fflag, struct thread *td)
149868b8534bSLuigi Rizzo {
149968b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
1500ce3ee1e7SLuigi Rizzo 	struct ifnet *ifp = NULL;
150168b8534bSLuigi Rizzo 	struct nmreq *nmr = (struct nmreq *) data;
1502ce3ee1e7SLuigi Rizzo 	struct netmap_adapter *na = NULL;
150368b8534bSLuigi Rizzo 	int error;
150464ae02c3SLuigi Rizzo 	u_int i, lim;
150568b8534bSLuigi Rizzo 	struct netmap_if *nifp;
1506ce3ee1e7SLuigi Rizzo 	struct netmap_kring *krings;
150768b8534bSLuigi Rizzo 
15080b8ed8e0SLuigi Rizzo 	(void)dev;	/* UNUSED */
15090b8ed8e0SLuigi Rizzo 	(void)fflag;	/* UNUSED */
1510f196ce38SLuigi Rizzo #ifdef linux
1511f196ce38SLuigi Rizzo #define devfs_get_cdevpriv(pp)				\
1512f196ce38SLuigi Rizzo 	({ *(struct netmap_priv_d **)pp = ((struct file *)td)->private_data; 	\
1513f196ce38SLuigi Rizzo 		(*pp ? 0 : ENOENT); })
1514f196ce38SLuigi Rizzo 
1515f196ce38SLuigi Rizzo /* devfs_set_cdevpriv cannot fail on linux */
1516f196ce38SLuigi Rizzo #define devfs_set_cdevpriv(p, fn)				\
1517f196ce38SLuigi Rizzo 	({ ((struct file *)td)->private_data = p; (p ? 0 : EINVAL); })
1518f196ce38SLuigi Rizzo 
1519f196ce38SLuigi Rizzo 
1520f196ce38SLuigi Rizzo #define devfs_clear_cdevpriv()	do {				\
1521f196ce38SLuigi Rizzo 		netmap_dtor(priv); ((struct file *)td)->private_data = 0;	\
1522f196ce38SLuigi Rizzo 	} while (0)
1523f196ce38SLuigi Rizzo #endif /* linux */
1524f196ce38SLuigi Rizzo 
152517885a7bSLuigi Rizzo 	if (cmd == NIOCGINFO || cmd == NIOCREGIF) {
152617885a7bSLuigi Rizzo 		/* truncate name */
152717885a7bSLuigi Rizzo 		nmr->nr_name[sizeof(nmr->nr_name) - 1] = '\0';
152817885a7bSLuigi Rizzo 		if (nmr->nr_version != NETMAP_API) {
152917885a7bSLuigi Rizzo 			D("API mismatch for %s got %d need %d",
153017885a7bSLuigi Rizzo 				nmr->nr_name,
153117885a7bSLuigi Rizzo 				nmr->nr_version, NETMAP_API);
153217885a7bSLuigi Rizzo 			nmr->nr_version = NETMAP_API;
153317885a7bSLuigi Rizzo 			return EINVAL;
153417885a7bSLuigi Rizzo 		}
153517885a7bSLuigi Rizzo 	}
1536506cc70cSLuigi Rizzo 	CURVNET_SET(TD_TO_VNET(td));
1537506cc70cSLuigi Rizzo 
153868b8534bSLuigi Rizzo 	error = devfs_get_cdevpriv((void **)&priv);
15398241616dSLuigi Rizzo 	if (error) {
1540506cc70cSLuigi Rizzo 		CURVNET_RESTORE();
15418241616dSLuigi Rizzo 		/* XXX ENOENT should be impossible, since the priv
15428241616dSLuigi Rizzo 		 * is now created in the open */
15438241616dSLuigi Rizzo 		return (error == ENOENT ? ENXIO : error);
1544506cc70cSLuigi Rizzo 	}
154568b8534bSLuigi Rizzo 
154668b8534bSLuigi Rizzo 	switch (cmd) {
154768b8534bSLuigi Rizzo 	case NIOCGINFO:		/* return capabilities etc */
1548f18be576SLuigi Rizzo 		if (nmr->nr_cmd == NETMAP_BDG_LIST) {
1549f18be576SLuigi Rizzo 			error = netmap_bdg_ctl(nmr, NULL);
1550f18be576SLuigi Rizzo 			break;
1551f18be576SLuigi Rizzo 		}
1552ce3ee1e7SLuigi Rizzo 
1553ce3ee1e7SLuigi Rizzo 		NMG_LOCK();
1554ce3ee1e7SLuigi Rizzo 		do {
1555ce3ee1e7SLuigi Rizzo 			/* memsize is always valid */
1556ce3ee1e7SLuigi Rizzo 			struct netmap_mem_d *nmd = &nm_mem;
1557ce3ee1e7SLuigi Rizzo 			u_int memflags;
1558ce3ee1e7SLuigi Rizzo 
1559ce3ee1e7SLuigi Rizzo 			if (nmr->nr_name[0] != '\0') {
1560ce3ee1e7SLuigi Rizzo 				/* get a refcount */
1561f9790aebSLuigi Rizzo 				error = netmap_get_na(nmr, &na, 1 /* create */);
15628241616dSLuigi Rizzo 				if (error)
15638241616dSLuigi Rizzo 					break;
1564f9790aebSLuigi Rizzo 				nmd = na->nm_mem; /* get memory allocator */
1565ce3ee1e7SLuigi Rizzo 			}
1566ce3ee1e7SLuigi Rizzo 
1567ce3ee1e7SLuigi Rizzo 			error = netmap_mem_get_info(nmd, &nmr->nr_memsize, &memflags);
1568ce3ee1e7SLuigi Rizzo 			if (error)
1569ce3ee1e7SLuigi Rizzo 				break;
1570ce3ee1e7SLuigi Rizzo 			if (na == NULL) /* only memory info */
1571ce3ee1e7SLuigi Rizzo 				break;
15728241616dSLuigi Rizzo 			nmr->nr_offset = 0;
15738241616dSLuigi Rizzo 			nmr->nr_rx_slots = nmr->nr_tx_slots = 0;
1574ae10d1afSLuigi Rizzo 			netmap_update_config(na);
1575d76bf4ffSLuigi Rizzo 			nmr->nr_rx_rings = na->num_rx_rings;
1576d76bf4ffSLuigi Rizzo 			nmr->nr_tx_rings = na->num_tx_rings;
157764ae02c3SLuigi Rizzo 			nmr->nr_rx_slots = na->num_rx_desc;
157864ae02c3SLuigi Rizzo 			nmr->nr_tx_slots = na->num_tx_desc;
1579ce3ee1e7SLuigi Rizzo 			if (memflags & NETMAP_MEM_PRIVATE)
1580ce3ee1e7SLuigi Rizzo 				nmr->nr_ringid |= NETMAP_PRIV_MEM;
1581f9790aebSLuigi Rizzo 			netmap_adapter_put(na);
1582ce3ee1e7SLuigi Rizzo 		} while (0);
1583ce3ee1e7SLuigi Rizzo 		NMG_UNLOCK();
158468b8534bSLuigi Rizzo 		break;
158568b8534bSLuigi Rizzo 
158668b8534bSLuigi Rizzo 	case NIOCREGIF:
1587f18be576SLuigi Rizzo 		/* possibly attach/detach NIC and VALE switch */
1588f18be576SLuigi Rizzo 		i = nmr->nr_cmd;
1589f9790aebSLuigi Rizzo 		if (i == NETMAP_BDG_ATTACH || i == NETMAP_BDG_DETACH
1590f9790aebSLuigi Rizzo 				|| i == NETMAP_BDG_OFFSET) {
1591f18be576SLuigi Rizzo 			error = netmap_bdg_ctl(nmr, NULL);
1592f18be576SLuigi Rizzo 			break;
1593f18be576SLuigi Rizzo 		} else if (i != 0) {
1594f18be576SLuigi Rizzo 			D("nr_cmd must be 0 not %d", i);
1595f18be576SLuigi Rizzo 			error = EINVAL;
1596f18be576SLuigi Rizzo 			break;
1597f18be576SLuigi Rizzo 		}
1598f18be576SLuigi Rizzo 
15998241616dSLuigi Rizzo 		/* protect access to priv from concurrent NIOCREGIF */
1600ce3ee1e7SLuigi Rizzo 		NMG_LOCK();
1601ce3ee1e7SLuigi Rizzo 		do {
1602ce3ee1e7SLuigi Rizzo 			u_int memflags;
1603ce3ee1e7SLuigi Rizzo 
1604f9790aebSLuigi Rizzo 			if (priv->np_na != NULL) {	/* thread already registered */
1605506cc70cSLuigi Rizzo 				error = netmap_set_ringid(priv, nmr->nr_ringid);
1606506cc70cSLuigi Rizzo 				break;
1607506cc70cSLuigi Rizzo 			}
160868b8534bSLuigi Rizzo 			/* find the interface and a reference */
1609f9790aebSLuigi Rizzo 			error = netmap_get_na(nmr, &na, 1 /* create */); /* keep reference */
161068b8534bSLuigi Rizzo 			if (error)
1611ce3ee1e7SLuigi Rizzo 				break;
1612f9790aebSLuigi Rizzo 			ifp = na->ifp;
1613f9790aebSLuigi Rizzo 			if (NETMAP_OWNED_BY_KERN(na)) {
1614f9790aebSLuigi Rizzo 				netmap_adapter_put(na);
1615ce3ee1e7SLuigi Rizzo 				error = EBUSY;
1616ce3ee1e7SLuigi Rizzo 				break;
1617f196ce38SLuigi Rizzo 			}
1618f9790aebSLuigi Rizzo 			nifp = netmap_do_regif(priv, na, nmr->nr_ringid, &error);
1619f18be576SLuigi Rizzo 			if (!nifp) {    /* reg. failed, release priv and ref */
1620f9790aebSLuigi Rizzo 				netmap_adapter_put(na);
16218241616dSLuigi Rizzo 				priv->np_nifp = NULL;
1622ce3ee1e7SLuigi Rizzo 				break;
162368b8534bSLuigi Rizzo 			}
162468b8534bSLuigi Rizzo 
162568b8534bSLuigi Rizzo 			/* return the offset of the netmap_if object */
1626d76bf4ffSLuigi Rizzo 			nmr->nr_rx_rings = na->num_rx_rings;
1627d76bf4ffSLuigi Rizzo 			nmr->nr_tx_rings = na->num_tx_rings;
162864ae02c3SLuigi Rizzo 			nmr->nr_rx_slots = na->num_rx_desc;
162964ae02c3SLuigi Rizzo 			nmr->nr_tx_slots = na->num_tx_desc;
1630ce3ee1e7SLuigi Rizzo 			error = netmap_mem_get_info(na->nm_mem, &nmr->nr_memsize, &memflags);
1631ce3ee1e7SLuigi Rizzo 			if (error) {
1632f9790aebSLuigi Rizzo 				netmap_adapter_put(na);
1633ce3ee1e7SLuigi Rizzo 				break;
1634ce3ee1e7SLuigi Rizzo 			}
1635ce3ee1e7SLuigi Rizzo 			if (memflags & NETMAP_MEM_PRIVATE) {
1636ce3ee1e7SLuigi Rizzo 				nmr->nr_ringid |= NETMAP_PRIV_MEM;
16373d819cb6SLuigi Rizzo 				*(uint32_t *)(uintptr_t)&nifp->ni_flags |= NI_PRIV_MEM;
1638ce3ee1e7SLuigi Rizzo 			}
1639ce3ee1e7SLuigi Rizzo 			nmr->nr_offset = netmap_mem_if_offset(na->nm_mem, nifp);
1640ce3ee1e7SLuigi Rizzo 		} while (0);
1641ce3ee1e7SLuigi Rizzo 		NMG_UNLOCK();
164268b8534bSLuigi Rizzo 		break;
164368b8534bSLuigi Rizzo 
164468b8534bSLuigi Rizzo 	case NIOCTXSYNC:
164568b8534bSLuigi Rizzo 	case NIOCRXSYNC:
16468241616dSLuigi Rizzo 		nifp = priv->np_nifp;
16478241616dSLuigi Rizzo 
16488241616dSLuigi Rizzo 		if (nifp == NULL) {
1649506cc70cSLuigi Rizzo 			error = ENXIO;
1650506cc70cSLuigi Rizzo 			break;
1651506cc70cSLuigi Rizzo 		}
16528241616dSLuigi Rizzo 		rmb(); /* make sure following reads are not from cache */
16538241616dSLuigi Rizzo 
1654f9790aebSLuigi Rizzo 		na = priv->np_na;      /* we have a reference */
16558241616dSLuigi Rizzo 
1656f9790aebSLuigi Rizzo 		if (na == NULL) {
1657f9790aebSLuigi Rizzo 			D("Internal error: nifp != NULL && na == NULL");
16588241616dSLuigi Rizzo 			error = ENXIO;
16598241616dSLuigi Rizzo 			break;
16608241616dSLuigi Rizzo 		}
16618241616dSLuigi Rizzo 
1662f9790aebSLuigi Rizzo 		ifp = na->ifp;
1663f9790aebSLuigi Rizzo 		if (ifp == NULL) {
1664f9790aebSLuigi Rizzo 			RD(1, "the ifp is gone");
1665f9790aebSLuigi Rizzo 			error = ENXIO;
1666f9790aebSLuigi Rizzo 			break;
1667f9790aebSLuigi Rizzo 		}
1668f9790aebSLuigi Rizzo 
166964ae02c3SLuigi Rizzo 		if (priv->np_qfirst == NETMAP_SW_RING) { /* host rings */
167068b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC)
1671ce3ee1e7SLuigi Rizzo 				netmap_txsync_to_host(na);
167268b8534bSLuigi Rizzo 			else
1673ce3ee1e7SLuigi Rizzo 				netmap_rxsync_from_host(na, NULL, NULL);
1674506cc70cSLuigi Rizzo 			break;
167568b8534bSLuigi Rizzo 		}
167664ae02c3SLuigi Rizzo 		/* find the last ring to scan */
167764ae02c3SLuigi Rizzo 		lim = priv->np_qlast;
167864ae02c3SLuigi Rizzo 		if (lim == NETMAP_HW_RING)
16793c0caf6cSLuigi Rizzo 			lim = (cmd == NIOCTXSYNC) ?
1680d76bf4ffSLuigi Rizzo 			    na->num_tx_rings : na->num_rx_rings;
168168b8534bSLuigi Rizzo 
1682ce3ee1e7SLuigi Rizzo 		krings = (cmd == NIOCTXSYNC) ? na->tx_rings : na->rx_rings;
168364ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim; i++) {
1684ce3ee1e7SLuigi Rizzo 			struct netmap_kring *kring = krings + i;
1685ce3ee1e7SLuigi Rizzo 			if (nm_kr_tryget(kring)) {
1686ce3ee1e7SLuigi Rizzo 				error = EBUSY;
1687ce3ee1e7SLuigi Rizzo 				goto out;
1688ce3ee1e7SLuigi Rizzo 			}
168968b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC) {
169068b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
16913c0caf6cSLuigi Rizzo 					D("pre txsync ring %d cur %d hwcur %d",
169268b8534bSLuigi Rizzo 					    i, kring->ring->cur,
169368b8534bSLuigi Rizzo 					    kring->nr_hwcur);
169417885a7bSLuigi Rizzo 				if (nm_txsync_prologue(kring) >= kring->nkr_num_slots) {
169517885a7bSLuigi Rizzo 					netmap_ring_reinit(kring);
169617885a7bSLuigi Rizzo 				} else {
1697f9790aebSLuigi Rizzo 					na->nm_txsync(na, i, NAF_FORCE_RECLAIM);
169817885a7bSLuigi Rizzo 				}
169968b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
17003c0caf6cSLuigi Rizzo 					D("post txsync ring %d cur %d hwcur %d",
170168b8534bSLuigi Rizzo 					    i, kring->ring->cur,
170268b8534bSLuigi Rizzo 					    kring->nr_hwcur);
170368b8534bSLuigi Rizzo 			} else {
1704f9790aebSLuigi Rizzo 				na->nm_rxsync(na, i, NAF_FORCE_READ);
170568b8534bSLuigi Rizzo 				microtime(&na->rx_rings[i].ring->ts);
170668b8534bSLuigi Rizzo 			}
1707ce3ee1e7SLuigi Rizzo 			nm_kr_put(kring);
170868b8534bSLuigi Rizzo 		}
170968b8534bSLuigi Rizzo 
171068b8534bSLuigi Rizzo 		break;
171168b8534bSLuigi Rizzo 
1712f196ce38SLuigi Rizzo #ifdef __FreeBSD__
171368b8534bSLuigi Rizzo 	case BIOCIMMEDIATE:
171468b8534bSLuigi Rizzo 	case BIOCGHDRCMPLT:
171568b8534bSLuigi Rizzo 	case BIOCSHDRCMPLT:
171668b8534bSLuigi Rizzo 	case BIOCSSEESENT:
171768b8534bSLuigi Rizzo 		D("ignore BIOCIMMEDIATE/BIOCSHDRCMPLT/BIOCSHDRCMPLT/BIOCSSEESENT");
171868b8534bSLuigi Rizzo 		break;
171968b8534bSLuigi Rizzo 
1720babc7c12SLuigi Rizzo 	default:	/* allow device-specific ioctls */
172168b8534bSLuigi Rizzo 	    {
172268b8534bSLuigi Rizzo 		struct socket so;
1723ce3ee1e7SLuigi Rizzo 
172468b8534bSLuigi Rizzo 		bzero(&so, sizeof(so));
1725ce3ee1e7SLuigi Rizzo 		NMG_LOCK();
1726f9790aebSLuigi Rizzo 		error = netmap_get_na(nmr, &na, 0 /* don't create */); /* keep reference */
1727ce3ee1e7SLuigi Rizzo 		if (error) {
1728f9790aebSLuigi Rizzo 			netmap_adapter_put(na);
1729ce3ee1e7SLuigi Rizzo 			NMG_UNLOCK();
173068b8534bSLuigi Rizzo 			break;
1731ce3ee1e7SLuigi Rizzo 		}
1732f9790aebSLuigi Rizzo 		ifp = na->ifp;
173368b8534bSLuigi Rizzo 		so.so_vnet = ifp->if_vnet;
173468b8534bSLuigi Rizzo 		// so->so_proto not null.
173568b8534bSLuigi Rizzo 		error = ifioctl(&so, cmd, data, td);
1736f9790aebSLuigi Rizzo 		netmap_adapter_put(na);
1737ce3ee1e7SLuigi Rizzo 		NMG_UNLOCK();
1738babc7c12SLuigi Rizzo 		break;
173968b8534bSLuigi Rizzo 	    }
1740f196ce38SLuigi Rizzo 
1741f196ce38SLuigi Rizzo #else /* linux */
1742f196ce38SLuigi Rizzo 	default:
1743f196ce38SLuigi Rizzo 		error = EOPNOTSUPP;
1744f196ce38SLuigi Rizzo #endif /* linux */
174568b8534bSLuigi Rizzo 	}
1746ce3ee1e7SLuigi Rizzo out:
174768b8534bSLuigi Rizzo 
1748506cc70cSLuigi Rizzo 	CURVNET_RESTORE();
174968b8534bSLuigi Rizzo 	return (error);
175068b8534bSLuigi Rizzo }
175168b8534bSLuigi Rizzo 
175268b8534bSLuigi Rizzo 
175368b8534bSLuigi Rizzo /*
175468b8534bSLuigi Rizzo  * select(2) and poll(2) handlers for the "netmap" device.
175568b8534bSLuigi Rizzo  *
175668b8534bSLuigi Rizzo  * Can be called for one or more queues.
175768b8534bSLuigi Rizzo  * Return true the event mask corresponding to ready events.
175868b8534bSLuigi Rizzo  * If there are no ready events, do a selrecord on either individual
1759ce3ee1e7SLuigi Rizzo  * selinfo or on the global one.
176068b8534bSLuigi Rizzo  * Device-dependent parts (locking and sync of tx/rx rings)
176168b8534bSLuigi Rizzo  * are done through callbacks.
1762f196ce38SLuigi Rizzo  *
176301c7d25fSLuigi Rizzo  * On linux, arguments are really pwait, the poll table, and 'td' is struct file *
176401c7d25fSLuigi Rizzo  * The first one is remapped to pwait as selrecord() uses the name as an
176501c7d25fSLuigi Rizzo  * hidden argument.
176668b8534bSLuigi Rizzo  */
1767f9790aebSLuigi Rizzo int
176801c7d25fSLuigi Rizzo netmap_poll(struct cdev *dev, int events, struct thread *td)
176968b8534bSLuigi Rizzo {
177068b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
177168b8534bSLuigi Rizzo 	struct netmap_adapter *na;
177268b8534bSLuigi Rizzo 	struct ifnet *ifp;
177368b8534bSLuigi Rizzo 	struct netmap_kring *kring;
1774954dca4cSLuigi Rizzo 	u_int i, check_all_tx, check_all_rx, want_tx, want_rx, revents = 0;
177517885a7bSLuigi Rizzo 	u_int lim_tx, lim_rx;
177617885a7bSLuigi Rizzo 	struct mbq q;		/* packets from hw queues to host stack */
177701c7d25fSLuigi Rizzo 	void *pwait = dev;	/* linux compatibility */
177801c7d25fSLuigi Rizzo 
1779f9790aebSLuigi Rizzo 	/*
1780f9790aebSLuigi Rizzo 	 * In order to avoid nested locks, we need to "double check"
1781f9790aebSLuigi Rizzo 	 * txsync and rxsync if we decide to do a selrecord().
1782f9790aebSLuigi Rizzo 	 * retry_tx (and retry_rx, later) prevent looping forever.
1783f9790aebSLuigi Rizzo 	 */
178417885a7bSLuigi Rizzo 	int retry_tx = 1, retry_rx = 1;
1785ce3ee1e7SLuigi Rizzo 
178601c7d25fSLuigi Rizzo 	(void)pwait;
1787f9790aebSLuigi Rizzo 	mbq_init(&q);
178868b8534bSLuigi Rizzo 
178968b8534bSLuigi Rizzo 	if (devfs_get_cdevpriv((void **)&priv) != 0 || priv == NULL)
179068b8534bSLuigi Rizzo 		return POLLERR;
179168b8534bSLuigi Rizzo 
17928241616dSLuigi Rizzo 	if (priv->np_nifp == NULL) {
17938241616dSLuigi Rizzo 		D("No if registered");
17948241616dSLuigi Rizzo 		return POLLERR;
17958241616dSLuigi Rizzo 	}
17968241616dSLuigi Rizzo 	rmb(); /* make sure following reads are not from cache */
17978241616dSLuigi Rizzo 
1798f9790aebSLuigi Rizzo 	na = priv->np_na;
1799f9790aebSLuigi Rizzo 	ifp = na->ifp;
1800f9790aebSLuigi Rizzo 	// check for deleted
1801f9790aebSLuigi Rizzo 	if (ifp == NULL) {
1802f9790aebSLuigi Rizzo 		RD(1, "the ifp is gone");
1803f9790aebSLuigi Rizzo 		return POLLERR;
1804f9790aebSLuigi Rizzo 	}
1805f9790aebSLuigi Rizzo 
180668b8534bSLuigi Rizzo 	if ( (ifp->if_capenable & IFCAP_NETMAP) == 0)
180768b8534bSLuigi Rizzo 		return POLLERR;
180868b8534bSLuigi Rizzo 
180968b8534bSLuigi Rizzo 	if (netmap_verbose & 0x8000)
1810f9790aebSLuigi Rizzo 		D("device %s events 0x%x", NM_IFPNAME(ifp), events);
181168b8534bSLuigi Rizzo 	want_tx = events & (POLLOUT | POLLWRNORM);
181268b8534bSLuigi Rizzo 	want_rx = events & (POLLIN | POLLRDNORM);
181368b8534bSLuigi Rizzo 
1814d76bf4ffSLuigi Rizzo 	lim_tx = na->num_tx_rings;
1815d76bf4ffSLuigi Rizzo 	lim_rx = na->num_rx_rings;
1816ce3ee1e7SLuigi Rizzo 
181764ae02c3SLuigi Rizzo 	if (priv->np_qfirst == NETMAP_SW_RING) {
181817885a7bSLuigi Rizzo 		// XXX locking ?
1819ce3ee1e7SLuigi Rizzo 		/* handle the host stack ring */
182068b8534bSLuigi Rizzo 		if (priv->np_txpoll || want_tx) {
182168b8534bSLuigi Rizzo 			/* push any packets up, then we are always ready */
1822ce3ee1e7SLuigi Rizzo 			netmap_txsync_to_host(na);
182368b8534bSLuigi Rizzo 			revents |= want_tx;
182468b8534bSLuigi Rizzo 		}
182568b8534bSLuigi Rizzo 		if (want_rx) {
182664ae02c3SLuigi Rizzo 			kring = &na->rx_rings[lim_rx];
182717885a7bSLuigi Rizzo 			/* XXX replace with rxprologue etc. */
182817885a7bSLuigi Rizzo 			if (nm_ring_empty(kring->ring))
1829ce3ee1e7SLuigi Rizzo 				netmap_rxsync_from_host(na, td, dev);
183017885a7bSLuigi Rizzo 			if (!nm_ring_empty(kring->ring))
183168b8534bSLuigi Rizzo 				revents |= want_rx;
183268b8534bSLuigi Rizzo 		}
183368b8534bSLuigi Rizzo 		return (revents);
183468b8534bSLuigi Rizzo 	}
183568b8534bSLuigi Rizzo 
1836091fd0abSLuigi Rizzo 
183768b8534bSLuigi Rizzo 	/*
1838f9790aebSLuigi Rizzo 	 * check_all_{tx|rx} are set if the card has more than one queue AND
1839f9790aebSLuigi Rizzo 	 * the file descriptor is bound to all of them. If so, we sleep on
1840ce3ee1e7SLuigi Rizzo 	 * the "global" selinfo, otherwise we sleep on individual selinfo
1841ce3ee1e7SLuigi Rizzo 	 * (FreeBSD only allows two selinfo's per file descriptor).
1842ce3ee1e7SLuigi Rizzo 	 * The interrupt routine in the driver wake one or the other
1843ce3ee1e7SLuigi Rizzo 	 * (or both) depending on which clients are active.
184468b8534bSLuigi Rizzo 	 *
184568b8534bSLuigi Rizzo 	 * rxsync() is only called if we run out of buffers on a POLLIN.
184668b8534bSLuigi Rizzo 	 * txsync() is called if we run out of buffers on POLLOUT, or
184768b8534bSLuigi Rizzo 	 * there are pending packets to send. The latter can be disabled
184868b8534bSLuigi Rizzo 	 * passing NETMAP_NO_TX_POLL in the NIOCREG call.
184968b8534bSLuigi Rizzo 	 */
1850954dca4cSLuigi Rizzo 	check_all_tx = (priv->np_qlast == NETMAP_HW_RING) && (lim_tx > 1);
1851954dca4cSLuigi Rizzo 	check_all_rx = (priv->np_qlast == NETMAP_HW_RING) && (lim_rx > 1);
185268b8534bSLuigi Rizzo 
185364ae02c3SLuigi Rizzo 	if (priv->np_qlast != NETMAP_HW_RING) {
185464ae02c3SLuigi Rizzo 		lim_tx = lim_rx = priv->np_qlast;
185564ae02c3SLuigi Rizzo 	}
185664ae02c3SLuigi Rizzo 
185768b8534bSLuigi Rizzo 	/*
1858f9790aebSLuigi Rizzo 	 * We start with a lock free round which is cheap if we have
1859f9790aebSLuigi Rizzo 	 * slots available. If this fails, then lock and call the sync
186068b8534bSLuigi Rizzo 	 * routines.
186168b8534bSLuigi Rizzo 	 */
186264ae02c3SLuigi Rizzo 	for (i = priv->np_qfirst; want_rx && i < lim_rx; i++) {
186368b8534bSLuigi Rizzo 		kring = &na->rx_rings[i];
186417885a7bSLuigi Rizzo 		/* XXX compare ring->cur and kring->tail */
186517885a7bSLuigi Rizzo 		if (!nm_ring_empty(kring->ring)) {
186668b8534bSLuigi Rizzo 			revents |= want_rx;
186768b8534bSLuigi Rizzo 			want_rx = 0;	/* also breaks the loop */
186868b8534bSLuigi Rizzo 		}
186968b8534bSLuigi Rizzo 	}
187064ae02c3SLuigi Rizzo 	for (i = priv->np_qfirst; want_tx && i < lim_tx; i++) {
187168b8534bSLuigi Rizzo 		kring = &na->tx_rings[i];
187217885a7bSLuigi Rizzo 		/* XXX compare ring->cur and kring->tail */
187317885a7bSLuigi Rizzo 		if (!nm_ring_empty(kring->ring)) {
187468b8534bSLuigi Rizzo 			revents |= want_tx;
187568b8534bSLuigi Rizzo 			want_tx = 0;	/* also breaks the loop */
187668b8534bSLuigi Rizzo 		}
187768b8534bSLuigi Rizzo 	}
187868b8534bSLuigi Rizzo 
187968b8534bSLuigi Rizzo 	/*
188017885a7bSLuigi Rizzo 	 * If we want to push packets out (priv->np_txpoll) or
188117885a7bSLuigi Rizzo 	 * want_tx is still set, we must issue txsync calls
188217885a7bSLuigi Rizzo 	 * (on all rings, to avoid that the tx rings stall).
1883f9790aebSLuigi Rizzo 	 * XXX should also check cur != hwcur on the tx rings.
1884f9790aebSLuigi Rizzo 	 * Fortunately, normal tx mode has np_txpoll set.
188568b8534bSLuigi Rizzo 	 */
188668b8534bSLuigi Rizzo 	if (priv->np_txpoll || want_tx) {
188717885a7bSLuigi Rizzo 		/*
188817885a7bSLuigi Rizzo 		 * The first round checks if anyone is ready, if not
188917885a7bSLuigi Rizzo 		 * do a selrecord and another round to handle races.
189017885a7bSLuigi Rizzo 		 * want_tx goes to 0 if any space is found, and is
189117885a7bSLuigi Rizzo 		 * used to skip rings with no pending transmissions.
1892ce3ee1e7SLuigi Rizzo 		 */
1893091fd0abSLuigi Rizzo flush_tx:
189464ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim_tx; i++) {
189517885a7bSLuigi Rizzo 			int found = 0;
189617885a7bSLuigi Rizzo 
189768b8534bSLuigi Rizzo 			kring = &na->tx_rings[i];
189868b8534bSLuigi Rizzo 			if (!want_tx && kring->ring->cur == kring->nr_hwcur)
189968b8534bSLuigi Rizzo 				continue;
190017885a7bSLuigi Rizzo 			/* only one thread does txsync */
1901ce3ee1e7SLuigi Rizzo 			if (nm_kr_tryget(kring)) {
190217885a7bSLuigi Rizzo 				D("%p lost race on txring %d, ok", priv, i);
190317885a7bSLuigi Rizzo 				continue;
190468b8534bSLuigi Rizzo 			}
190517885a7bSLuigi Rizzo 			if (nm_txsync_prologue(kring) >= kring->nkr_num_slots) {
190617885a7bSLuigi Rizzo 				netmap_ring_reinit(kring);
190717885a7bSLuigi Rizzo 				revents |= POLLERR;
190817885a7bSLuigi Rizzo 			} else {
1909f9790aebSLuigi Rizzo 				if (na->nm_txsync(na, i, 0))
191068b8534bSLuigi Rizzo 					revents |= POLLERR;
191117885a7bSLuigi Rizzo 			}
191268b8534bSLuigi Rizzo 
191317885a7bSLuigi Rizzo 			/*
191417885a7bSLuigi Rizzo 			 * If we found new slots, notify potential
191517885a7bSLuigi Rizzo 			 * listeners on the same ring.
191617885a7bSLuigi Rizzo 			 * Since we just did a txsync, look at the copies
191717885a7bSLuigi Rizzo 			 * of cur,tail in the kring.
1918f9790aebSLuigi Rizzo 			 */
191917885a7bSLuigi Rizzo 			found = kring->rcur != kring->rtail;
192017885a7bSLuigi Rizzo 			nm_kr_put(kring);
192117885a7bSLuigi Rizzo 			if (found) { /* notify other listeners */
192268b8534bSLuigi Rizzo 				revents |= want_tx;
192368b8534bSLuigi Rizzo 				want_tx = 0;
192417885a7bSLuigi Rizzo 				na->nm_notify(na, i, NR_TX, NAF_GLOBAL_NOTIFY);
192568b8534bSLuigi Rizzo 			}
1926ce3ee1e7SLuigi Rizzo 		}
1927ce3ee1e7SLuigi Rizzo 		if (want_tx && retry_tx) {
1928954dca4cSLuigi Rizzo 			selrecord(td, check_all_tx ?
1929ce3ee1e7SLuigi Rizzo 			    &na->tx_si : &na->tx_rings[priv->np_qfirst].si);
1930ce3ee1e7SLuigi Rizzo 			retry_tx = 0;
1931ce3ee1e7SLuigi Rizzo 			goto flush_tx;
193268b8534bSLuigi Rizzo 		}
193368b8534bSLuigi Rizzo 	}
193468b8534bSLuigi Rizzo 
193568b8534bSLuigi Rizzo 	/*
193617885a7bSLuigi Rizzo 	 * If want_rx is still set scan receive rings.
193768b8534bSLuigi Rizzo 	 * Do it on all rings because otherwise we starve.
193868b8534bSLuigi Rizzo 	 */
193968b8534bSLuigi Rizzo 	if (want_rx) {
194017885a7bSLuigi Rizzo 		int send_down = 0; /* transparent mode */
194117885a7bSLuigi Rizzo 		/* two rounds here to for race avoidance */
1942ce3ee1e7SLuigi Rizzo do_retry_rx:
194364ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim_rx; i++) {
194417885a7bSLuigi Rizzo 			int found = 0;
194517885a7bSLuigi Rizzo 
194668b8534bSLuigi Rizzo 			kring = &na->rx_rings[i];
1947ce3ee1e7SLuigi Rizzo 
1948ce3ee1e7SLuigi Rizzo 			if (nm_kr_tryget(kring)) {
194917885a7bSLuigi Rizzo 				D("%p lost race on rxring %d, ok", priv, i);
195017885a7bSLuigi Rizzo 				continue;
195168b8534bSLuigi Rizzo 			}
1952ce3ee1e7SLuigi Rizzo 
195317885a7bSLuigi Rizzo 			/*
195417885a7bSLuigi Rizzo 			 * transparent mode support: collect packets
195517885a7bSLuigi Rizzo 			 * from the rxring(s).
195617885a7bSLuigi Rizzo 			 * XXX NR_FORWARD should only be read on
1957ce3ee1e7SLuigi Rizzo 			 * physical or NIC ports
1958ce3ee1e7SLuigi Rizzo 			 */
1959091fd0abSLuigi Rizzo 			if (netmap_fwd ||kring->ring->flags & NR_FORWARD) {
1960091fd0abSLuigi Rizzo 				ND(10, "forwarding some buffers up %d to %d",
1961091fd0abSLuigi Rizzo 				    kring->nr_hwcur, kring->ring->cur);
1962091fd0abSLuigi Rizzo 				netmap_grab_packets(kring, &q, netmap_fwd);
1963091fd0abSLuigi Rizzo 			}
196468b8534bSLuigi Rizzo 
1965f9790aebSLuigi Rizzo 			if (na->nm_rxsync(na, i, 0))
196668b8534bSLuigi Rizzo 				revents |= POLLERR;
19675819da83SLuigi Rizzo 			if (netmap_no_timestamp == 0 ||
19685819da83SLuigi Rizzo 					kring->ring->flags & NR_TIMESTAMP) {
196968b8534bSLuigi Rizzo 				microtime(&kring->ring->ts);
19705819da83SLuigi Rizzo 			}
197117885a7bSLuigi Rizzo 			/* after an rxsync we can use kring->rcur, rtail */
197217885a7bSLuigi Rizzo 			found = kring->rcur != kring->rtail;
197317885a7bSLuigi Rizzo 			nm_kr_put(kring);
197417885a7bSLuigi Rizzo 			if (found) {
197568b8534bSLuigi Rizzo 				revents |= want_rx;
1976ce3ee1e7SLuigi Rizzo 				retry_rx = 0;
197717885a7bSLuigi Rizzo 				na->nm_notify(na, i, NR_RX, NAF_GLOBAL_NOTIFY);
197868b8534bSLuigi Rizzo 			}
197968b8534bSLuigi Rizzo 		}
198017885a7bSLuigi Rizzo 
198117885a7bSLuigi Rizzo 		/* transparent mode XXX only during first pass ? */
198217885a7bSLuigi Rizzo 		kring = &na->rx_rings[lim_rx];
198317885a7bSLuigi Rizzo 		if (check_all_rx
198417885a7bSLuigi Rizzo 		    && (netmap_fwd || kring->ring->flags & NR_FORWARD)) {
198517885a7bSLuigi Rizzo 			/* XXX fix to use kring fields */
198617885a7bSLuigi Rizzo 			if (nm_ring_empty(kring->ring))
198717885a7bSLuigi Rizzo 				send_down = netmap_rxsync_from_host(na, td, dev);
198817885a7bSLuigi Rizzo 			if (!nm_ring_empty(kring->ring))
198917885a7bSLuigi Rizzo 				revents |= want_rx;
199017885a7bSLuigi Rizzo 		}
199117885a7bSLuigi Rizzo 
199217885a7bSLuigi Rizzo 		if (retry_rx)
1993954dca4cSLuigi Rizzo 			selrecord(td, check_all_rx ?
1994ce3ee1e7SLuigi Rizzo 			    &na->rx_si : &na->rx_rings[priv->np_qfirst].si);
199517885a7bSLuigi Rizzo 		if (send_down > 0 || retry_rx) {
199617885a7bSLuigi Rizzo 			retry_rx = 0;
199717885a7bSLuigi Rizzo 			if (send_down)
199817885a7bSLuigi Rizzo 				goto flush_tx; /* and retry_rx */
199917885a7bSLuigi Rizzo 			else
2000ce3ee1e7SLuigi Rizzo 				goto do_retry_rx;
2001ce3ee1e7SLuigi Rizzo 		}
200268b8534bSLuigi Rizzo 	}
2003091fd0abSLuigi Rizzo 
200417885a7bSLuigi Rizzo 	/*
200517885a7bSLuigi Rizzo 	 * Transparent mode: marked bufs on rx rings between
200617885a7bSLuigi Rizzo 	 * kring->nr_hwcur and ring->head
200717885a7bSLuigi Rizzo 	 * are passed to the other endpoint.
200817885a7bSLuigi Rizzo 	 *
200917885a7bSLuigi Rizzo 	 * In this mode we also scan the sw rxring, which in
201017885a7bSLuigi Rizzo 	 * turn passes packets up.
201117885a7bSLuigi Rizzo 	 *
201217885a7bSLuigi Rizzo 	 * XXX Transparent mode at the moment requires to bind all
201317885a7bSLuigi Rizzo  	 * rings to a single file descriptor.
2014ce3ee1e7SLuigi Rizzo 	 */
2015091fd0abSLuigi Rizzo 
2016091fd0abSLuigi Rizzo 	if (q.head)
2017f9790aebSLuigi Rizzo 		netmap_send_up(na->ifp, &q);
201868b8534bSLuigi Rizzo 
201968b8534bSLuigi Rizzo 	return (revents);
202068b8534bSLuigi Rizzo }
202168b8534bSLuigi Rizzo 
202217885a7bSLuigi Rizzo 
202317885a7bSLuigi Rizzo /*-------------------- driver support routines -------------------*/
202468b8534bSLuigi Rizzo 
2025f9790aebSLuigi Rizzo static int netmap_hw_krings_create(struct netmap_adapter *);
2026f9790aebSLuigi Rizzo 
2027f9790aebSLuigi Rizzo static int
202817885a7bSLuigi Rizzo netmap_notify(struct netmap_adapter *na, u_int n_ring,
202917885a7bSLuigi Rizzo 	enum txrx tx, int flags)
2030f9790aebSLuigi Rizzo {
2031f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
2032f9790aebSLuigi Rizzo 
2033f9790aebSLuigi Rizzo 	if (tx == NR_TX) {
2034f9790aebSLuigi Rizzo 		kring = na->tx_rings + n_ring;
2035f9790aebSLuigi Rizzo 		selwakeuppri(&kring->si, PI_NET);
2036f9790aebSLuigi Rizzo 		if (flags & NAF_GLOBAL_NOTIFY)
2037f9790aebSLuigi Rizzo 			selwakeuppri(&na->tx_si, PI_NET);
2038f9790aebSLuigi Rizzo 	} else {
2039f9790aebSLuigi Rizzo 		kring = na->rx_rings + n_ring;
2040f9790aebSLuigi Rizzo 		selwakeuppri(&kring->si, PI_NET);
2041f9790aebSLuigi Rizzo 		if (flags & NAF_GLOBAL_NOTIFY)
2042f9790aebSLuigi Rizzo 			selwakeuppri(&na->rx_si, PI_NET);
2043f9790aebSLuigi Rizzo 	}
2044f9790aebSLuigi Rizzo 	return 0;
2045f9790aebSLuigi Rizzo }
2046f9790aebSLuigi Rizzo 
2047f9790aebSLuigi Rizzo 
2048f9790aebSLuigi Rizzo // XXX check handling of failures
2049f9790aebSLuigi Rizzo int
2050f9790aebSLuigi Rizzo netmap_attach_common(struct netmap_adapter *na)
2051f9790aebSLuigi Rizzo {
2052f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
2053f9790aebSLuigi Rizzo 
2054f9790aebSLuigi Rizzo 	if (na->num_tx_rings == 0 || na->num_rx_rings == 0) {
2055f9790aebSLuigi Rizzo 		D("%s: invalid rings tx %d rx %d",
2056f9790aebSLuigi Rizzo 			ifp->if_xname, na->num_tx_rings, na->num_rx_rings);
2057f9790aebSLuigi Rizzo 		return EINVAL;
2058f9790aebSLuigi Rizzo 	}
2059f9790aebSLuigi Rizzo 	WNA(ifp) = na;
206017885a7bSLuigi Rizzo 
206117885a7bSLuigi Rizzo 	/* the following is only needed for na that use the host port.
206217885a7bSLuigi Rizzo 	 * XXX do we have something similar for linux ?
206317885a7bSLuigi Rizzo 	 */
206417885a7bSLuigi Rizzo #ifdef __FreeBSD__
206517885a7bSLuigi Rizzo 	na->if_input = ifp->if_input; /* for netmap_send_up */
206617885a7bSLuigi Rizzo #endif /* __FreeBSD__ */
206717885a7bSLuigi Rizzo 
2068f9790aebSLuigi Rizzo 	NETMAP_SET_CAPABLE(ifp);
2069f9790aebSLuigi Rizzo 	if (na->nm_krings_create == NULL) {
2070f9790aebSLuigi Rizzo 		na->nm_krings_create = netmap_hw_krings_create;
207117885a7bSLuigi Rizzo 		na->nm_krings_delete = netmap_hw_krings_delete;
2072f9790aebSLuigi Rizzo 	}
2073f9790aebSLuigi Rizzo 	if (na->nm_notify == NULL)
2074f9790aebSLuigi Rizzo 		na->nm_notify = netmap_notify;
2075f9790aebSLuigi Rizzo 	na->active_fds = 0;
2076f9790aebSLuigi Rizzo 
2077f9790aebSLuigi Rizzo 	if (na->nm_mem == NULL)
2078f9790aebSLuigi Rizzo 		na->nm_mem = &nm_mem;
2079f9790aebSLuigi Rizzo 	return 0;
2080f9790aebSLuigi Rizzo }
2081f9790aebSLuigi Rizzo 
2082f9790aebSLuigi Rizzo 
2083f9790aebSLuigi Rizzo void
2084f9790aebSLuigi Rizzo netmap_detach_common(struct netmap_adapter *na)
2085f9790aebSLuigi Rizzo {
2086f9790aebSLuigi Rizzo 	if (na->ifp)
2087f9790aebSLuigi Rizzo 		WNA(na->ifp) = NULL; /* XXX do we need this? */
2088f9790aebSLuigi Rizzo 
2089f9790aebSLuigi Rizzo 	if (na->tx_rings) { /* XXX should not happen */
2090f9790aebSLuigi Rizzo 		D("freeing leftover tx_rings");
2091f9790aebSLuigi Rizzo 		na->nm_krings_delete(na);
2092f9790aebSLuigi Rizzo 	}
2093f9790aebSLuigi Rizzo 	if (na->na_flags & NAF_MEM_OWNER)
2094f9790aebSLuigi Rizzo 		netmap_mem_private_delete(na->nm_mem);
2095f9790aebSLuigi Rizzo 	bzero(na, sizeof(*na));
2096f9790aebSLuigi Rizzo 	free(na, M_DEVBUF);
2097f9790aebSLuigi Rizzo }
2098f9790aebSLuigi Rizzo 
2099f18be576SLuigi Rizzo 
210068b8534bSLuigi Rizzo /*
210168b8534bSLuigi Rizzo  * Initialize a ``netmap_adapter`` object created by driver on attach.
210268b8534bSLuigi Rizzo  * We allocate a block of memory with room for a struct netmap_adapter
210368b8534bSLuigi Rizzo  * plus two sets of N+2 struct netmap_kring (where N is the number
210468b8534bSLuigi Rizzo  * of hardware rings):
210568b8534bSLuigi Rizzo  * krings	0..N-1	are for the hardware queues.
210668b8534bSLuigi Rizzo  * kring	N	is for the host stack queue
210717885a7bSLuigi Rizzo  * kring	N+1	is only used for the selinfo for all queues. // XXX still true ?
210868b8534bSLuigi Rizzo  * Return 0 on success, ENOMEM otherwise.
210968b8534bSLuigi Rizzo  */
211068b8534bSLuigi Rizzo int
2111f9790aebSLuigi Rizzo netmap_attach(struct netmap_adapter *arg)
211268b8534bSLuigi Rizzo {
2113f9790aebSLuigi Rizzo 	struct netmap_hw_adapter *hwna = NULL;
2114f9790aebSLuigi Rizzo 	// XXX when is arg == NULL ?
2115ae10d1afSLuigi Rizzo 	struct ifnet *ifp = arg ? arg->ifp : NULL;
211668b8534bSLuigi Rizzo 
2117ae10d1afSLuigi Rizzo 	if (arg == NULL || ifp == NULL)
2118ae10d1afSLuigi Rizzo 		goto fail;
2119f9790aebSLuigi Rizzo 	hwna = malloc(sizeof(*hwna), M_DEVBUF, M_NOWAIT | M_ZERO);
2120f9790aebSLuigi Rizzo 	if (hwna == NULL)
2121ae10d1afSLuigi Rizzo 		goto fail;
2122f9790aebSLuigi Rizzo 	hwna->up = *arg;
2123f9790aebSLuigi Rizzo 	if (netmap_attach_common(&hwna->up)) {
2124f9790aebSLuigi Rizzo 		free(hwna, M_DEVBUF);
2125f9790aebSLuigi Rizzo 		goto fail;
2126f9790aebSLuigi Rizzo 	}
2127f9790aebSLuigi Rizzo 	netmap_adapter_get(&hwna->up);
2128f9790aebSLuigi Rizzo 
212964ae02c3SLuigi Rizzo #ifdef linux
2130f18be576SLuigi Rizzo 	if (ifp->netdev_ops) {
2131f18be576SLuigi Rizzo 		/* prepare a clone of the netdev ops */
2132f18be576SLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28)
2133f9790aebSLuigi Rizzo 		hwna->nm_ndo.ndo_start_xmit = ifp->netdev_ops;
2134f18be576SLuigi Rizzo #else
2135f9790aebSLuigi Rizzo 		hwna->nm_ndo = *ifp->netdev_ops;
2136f18be576SLuigi Rizzo #endif
2137f18be576SLuigi Rizzo 	}
2138f9790aebSLuigi Rizzo 	hwna->nm_ndo.ndo_start_xmit = linux_netmap_start_xmit;
2139ce3ee1e7SLuigi Rizzo #endif /* linux */
2140f9790aebSLuigi Rizzo 
2141f9790aebSLuigi Rizzo 	D("success for %s", NM_IFPNAME(ifp));
2142ae10d1afSLuigi Rizzo 	return 0;
214368b8534bSLuigi Rizzo 
2144ae10d1afSLuigi Rizzo fail:
2145f9790aebSLuigi Rizzo 	D("fail, arg %p ifp %p na %p", arg, ifp, hwna);
2146849bec0eSLuigi Rizzo 	netmap_detach(ifp);
2147f9790aebSLuigi Rizzo 	return (hwna ? EINVAL : ENOMEM);
214868b8534bSLuigi Rizzo }
214968b8534bSLuigi Rizzo 
215068b8534bSLuigi Rizzo 
2151f9790aebSLuigi Rizzo void
2152f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_get)(struct netmap_adapter *na)
2153f9790aebSLuigi Rizzo {
2154f9790aebSLuigi Rizzo 	if (!na) {
2155f9790aebSLuigi Rizzo 		return;
2156f9790aebSLuigi Rizzo 	}
2157f9790aebSLuigi Rizzo 
2158f9790aebSLuigi Rizzo 	refcount_acquire(&na->na_refcount);
2159f9790aebSLuigi Rizzo }
2160f9790aebSLuigi Rizzo 
2161f9790aebSLuigi Rizzo 
2162f9790aebSLuigi Rizzo /* returns 1 iff the netmap_adapter is destroyed */
2163f9790aebSLuigi Rizzo int
2164f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_put)(struct netmap_adapter *na)
2165f9790aebSLuigi Rizzo {
2166f9790aebSLuigi Rizzo 	if (!na)
2167f9790aebSLuigi Rizzo 		return 1;
2168f9790aebSLuigi Rizzo 
2169f9790aebSLuigi Rizzo 	if (!refcount_release(&na->na_refcount))
2170f9790aebSLuigi Rizzo 		return 0;
2171f9790aebSLuigi Rizzo 
2172f9790aebSLuigi Rizzo 	if (na->nm_dtor)
2173f9790aebSLuigi Rizzo 		na->nm_dtor(na);
2174f9790aebSLuigi Rizzo 
2175f9790aebSLuigi Rizzo 	netmap_detach_common(na);
2176f9790aebSLuigi Rizzo 
2177f9790aebSLuigi Rizzo 	return 1;
2178f9790aebSLuigi Rizzo }
2179f9790aebSLuigi Rizzo 
2180f9790aebSLuigi Rizzo 
2181f9790aebSLuigi Rizzo int
2182f9790aebSLuigi Rizzo netmap_hw_krings_create(struct netmap_adapter *na)
2183f9790aebSLuigi Rizzo {
218417885a7bSLuigi Rizzo 	int ret = netmap_krings_create(na,
2185f9790aebSLuigi Rizzo 		na->num_tx_rings + 1, na->num_rx_rings + 1, 0);
218617885a7bSLuigi Rizzo 	if (ret == 0) {
218717885a7bSLuigi Rizzo 		/* initialize the mbq for the sw rx ring */
218817885a7bSLuigi Rizzo 		mbq_safe_init(&na->rx_rings[na->num_rx_rings].rx_queue);
218917885a7bSLuigi Rizzo 		ND("initialized sw rx queue %d", na->num_rx_rings);
219017885a7bSLuigi Rizzo 	}
219117885a7bSLuigi Rizzo 	return ret;
2192f9790aebSLuigi Rizzo }
2193f9790aebSLuigi Rizzo 
2194f9790aebSLuigi Rizzo 
2195f9790aebSLuigi Rizzo 
219668b8534bSLuigi Rizzo /*
219768b8534bSLuigi Rizzo  * Free the allocated memory linked to the given ``netmap_adapter``
219868b8534bSLuigi Rizzo  * object.
219968b8534bSLuigi Rizzo  */
220068b8534bSLuigi Rizzo void
220168b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp)
220268b8534bSLuigi Rizzo {
220368b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
220468b8534bSLuigi Rizzo 
220568b8534bSLuigi Rizzo 	if (!na)
220668b8534bSLuigi Rizzo 		return;
220768b8534bSLuigi Rizzo 
2208f9790aebSLuigi Rizzo 	NMG_LOCK();
2209f9790aebSLuigi Rizzo 	netmap_disable_all_rings(ifp);
2210fb25194fSLuigi Rizzo 	if (!netmap_adapter_put(na)) {
2211fb25194fSLuigi Rizzo 		/* someone is still using the adapter,
2212fb25194fSLuigi Rizzo 		 * tell them that the interface is gone
2213fb25194fSLuigi Rizzo 		 */
2214f9790aebSLuigi Rizzo 		na->ifp = NULL;
2215fb25194fSLuigi Rizzo 		/* give them a chance to notice */
2216f9790aebSLuigi Rizzo 		netmap_enable_all_rings(ifp);
2217fb25194fSLuigi Rizzo 	}
2218f9790aebSLuigi Rizzo 	NMG_UNLOCK();
2219ae10d1afSLuigi Rizzo }
2220f18be576SLuigi Rizzo 
2221f18be576SLuigi Rizzo 
222268b8534bSLuigi Rizzo /*
222302ad4083SLuigi Rizzo  * Intercept packets from the network stack and pass them
222402ad4083SLuigi Rizzo  * to netmap as incoming packets on the 'software' ring.
222517885a7bSLuigi Rizzo  *
222617885a7bSLuigi Rizzo  * We only store packets in a bounded mbq and then copy them
222717885a7bSLuigi Rizzo  * in the relevant rxsync routine.
222817885a7bSLuigi Rizzo  *
2229ce3ee1e7SLuigi Rizzo  * We rely on the OS to make sure that the ifp and na do not go
2230ce3ee1e7SLuigi Rizzo  * away (typically the caller checks for IFF_DRV_RUNNING or the like).
2231ce3ee1e7SLuigi Rizzo  * In nm_register() or whenever there is a reinitialization,
2232f9790aebSLuigi Rizzo  * we make sure to make the mode change visible here.
223368b8534bSLuigi Rizzo  */
223468b8534bSLuigi Rizzo int
2235ce3ee1e7SLuigi Rizzo netmap_transmit(struct ifnet *ifp, struct mbuf *m)
223668b8534bSLuigi Rizzo {
223768b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
2238ce3ee1e7SLuigi Rizzo 	struct netmap_kring *kring;
223917885a7bSLuigi Rizzo 	u_int len = MBUF_LEN(m);
224017885a7bSLuigi Rizzo 	u_int error = ENOBUFS;
224117885a7bSLuigi Rizzo 	struct mbq *q;
224217885a7bSLuigi Rizzo 	int space;
224368b8534bSLuigi Rizzo 
2244ce3ee1e7SLuigi Rizzo 	// XXX [Linux] we do not need this lock
2245ce3ee1e7SLuigi Rizzo 	// if we follow the down/configure/up protocol -gl
2246ce3ee1e7SLuigi Rizzo 	// mtx_lock(&na->core_lock);
224717885a7bSLuigi Rizzo 
2248ce3ee1e7SLuigi Rizzo 	if ( (ifp->if_capenable & IFCAP_NETMAP) == 0) {
224917885a7bSLuigi Rizzo 		D("%s not in netmap mode anymore", NM_IFPNAME(ifp));
2250ce3ee1e7SLuigi Rizzo 		error = ENXIO;
2251ce3ee1e7SLuigi Rizzo 		goto done;
2252ce3ee1e7SLuigi Rizzo 	}
2253ce3ee1e7SLuigi Rizzo 
2254ce3ee1e7SLuigi Rizzo 	kring = &na->rx_rings[na->num_rx_rings];
225517885a7bSLuigi Rizzo 	q = &kring->rx_queue;
225617885a7bSLuigi Rizzo 
2257ce3ee1e7SLuigi Rizzo 	// XXX reconsider long packets if we handle fragments
2258ce3ee1e7SLuigi Rizzo 	if (len > NETMAP_BDG_BUF_SIZE(na->nm_mem)) { /* too long for us */
2259f9790aebSLuigi Rizzo 		D("%s from_host, drop packet size %d > %d", NM_IFPNAME(ifp),
2260ce3ee1e7SLuigi Rizzo 			len, NETMAP_BDG_BUF_SIZE(na->nm_mem));
2261ce3ee1e7SLuigi Rizzo 		goto done;
2262849bec0eSLuigi Rizzo 	}
226317885a7bSLuigi Rizzo 
226417885a7bSLuigi Rizzo 	/* protect against rxsync_from_host(), netmap_sw_to_nic()
226517885a7bSLuigi Rizzo 	 * and maybe other instances of netmap_transmit (the latter
226617885a7bSLuigi Rizzo 	 * not possible on Linux).
226717885a7bSLuigi Rizzo 	 * Also avoid overflowing the queue.
2268ce3ee1e7SLuigi Rizzo 	 */
226917885a7bSLuigi Rizzo 	mtx_lock(&q->lock);
227017885a7bSLuigi Rizzo 
227117885a7bSLuigi Rizzo         space = kring->nr_hwtail - kring->nr_hwcur;
227217885a7bSLuigi Rizzo         if (space < 0)
227317885a7bSLuigi Rizzo                 space += kring->nkr_num_slots;
227417885a7bSLuigi Rizzo 	if (space + mbq_len(q) >= kring->nkr_num_slots - 1) { // XXX
227517885a7bSLuigi Rizzo 		RD(10, "%s full hwcur %d hwtail %d qlen %d len %d m %p",
227617885a7bSLuigi Rizzo 			 NM_IFPNAME(ifp), kring->nr_hwcur, kring->nr_hwtail, mbq_len(q),
227717885a7bSLuigi Rizzo 			len, m);
2278ce3ee1e7SLuigi Rizzo 	} else {
227917885a7bSLuigi Rizzo 		mbq_enqueue(q, m);
228017885a7bSLuigi Rizzo 		ND(10, "%s %d bufs in queue len %d m %p",
228117885a7bSLuigi Rizzo 			NM_IFPNAME(ifp), mbq_len(q), len, m);
228217885a7bSLuigi Rizzo 		/* notify outside the lock */
228317885a7bSLuigi Rizzo 		m = NULL;
228468b8534bSLuigi Rizzo 		error = 0;
2285ce3ee1e7SLuigi Rizzo 	}
228617885a7bSLuigi Rizzo 	mtx_unlock(&q->lock);
2287ce3ee1e7SLuigi Rizzo 
228868b8534bSLuigi Rizzo done:
228917885a7bSLuigi Rizzo 	if (m)
229068b8534bSLuigi Rizzo 		m_freem(m);
229117885a7bSLuigi Rizzo 	/* unconditionally wake up listeners */
229217885a7bSLuigi Rizzo 	na->nm_notify(na, na->num_rx_rings, NR_RX, 0);
229368b8534bSLuigi Rizzo 
229468b8534bSLuigi Rizzo 	return (error);
229568b8534bSLuigi Rizzo }
229668b8534bSLuigi Rizzo 
229768b8534bSLuigi Rizzo 
229868b8534bSLuigi Rizzo /*
229968b8534bSLuigi Rizzo  * netmap_reset() is called by the driver routines when reinitializing
230068b8534bSLuigi Rizzo  * a ring. The driver is in charge of locking to protect the kring.
2301f9790aebSLuigi Rizzo  * If native netmap mode is not set just return NULL.
230268b8534bSLuigi Rizzo  */
230368b8534bSLuigi Rizzo struct netmap_slot *
2304ce3ee1e7SLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, u_int n,
230568b8534bSLuigi Rizzo 	u_int new_cur)
230668b8534bSLuigi Rizzo {
230768b8534bSLuigi Rizzo 	struct netmap_kring *kring;
2308506cc70cSLuigi Rizzo 	int new_hwofs, lim;
230968b8534bSLuigi Rizzo 
2310ce3ee1e7SLuigi Rizzo 	if (na == NULL) {
2311ce3ee1e7SLuigi Rizzo 		D("NULL na, should not happen");
231268b8534bSLuigi Rizzo 		return NULL;	/* no netmap support here */
2313ce3ee1e7SLuigi Rizzo 	}
2314ce3ee1e7SLuigi Rizzo 	if (!(na->ifp->if_capenable & IFCAP_NETMAP)) {
23155864b3a5SLuigi Rizzo 		ND("interface not in netmap mode");
231668b8534bSLuigi Rizzo 		return NULL;	/* nothing to reinitialize */
2317ce3ee1e7SLuigi Rizzo 	}
231868b8534bSLuigi Rizzo 
2319ce3ee1e7SLuigi Rizzo 	/* XXX note- in the new scheme, we are not guaranteed to be
2320ce3ee1e7SLuigi Rizzo 	 * under lock (e.g. when called on a device reset).
2321ce3ee1e7SLuigi Rizzo 	 * In this case, we should set a flag and do not trust too
2322ce3ee1e7SLuigi Rizzo 	 * much the values. In practice: TODO
2323ce3ee1e7SLuigi Rizzo 	 * - set a RESET flag somewhere in the kring
2324ce3ee1e7SLuigi Rizzo 	 * - do the processing in a conservative way
2325ce3ee1e7SLuigi Rizzo 	 * - let the *sync() fixup at the end.
2326ce3ee1e7SLuigi Rizzo 	 */
232764ae02c3SLuigi Rizzo 	if (tx == NR_TX) {
23288241616dSLuigi Rizzo 		if (n >= na->num_tx_rings)
23298241616dSLuigi Rizzo 			return NULL;
233064ae02c3SLuigi Rizzo 		kring = na->tx_rings + n;
233117885a7bSLuigi Rizzo 		// XXX check whether we should use hwcur or rcur
2332506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur - new_cur;
233364ae02c3SLuigi Rizzo 	} else {
23348241616dSLuigi Rizzo 		if (n >= na->num_rx_rings)
23358241616dSLuigi Rizzo 			return NULL;
233664ae02c3SLuigi Rizzo 		kring = na->rx_rings + n;
233717885a7bSLuigi Rizzo 		new_hwofs = kring->nr_hwtail - new_cur;
233864ae02c3SLuigi Rizzo 	}
233964ae02c3SLuigi Rizzo 	lim = kring->nkr_num_slots - 1;
2340506cc70cSLuigi Rizzo 	if (new_hwofs > lim)
2341506cc70cSLuigi Rizzo 		new_hwofs -= lim + 1;
2342506cc70cSLuigi Rizzo 
2343ce3ee1e7SLuigi Rizzo 	/* Always set the new offset value and realign the ring. */
234417885a7bSLuigi Rizzo 	if (netmap_verbose)
234517885a7bSLuigi Rizzo 	    D("%s %s%d hwofs %d -> %d, hwtail %d -> %d",
234617885a7bSLuigi Rizzo 		NM_IFPNAME(na->ifp),
234717885a7bSLuigi Rizzo 		tx == NR_TX ? "TX" : "RX", n,
2348ce3ee1e7SLuigi Rizzo 		kring->nkr_hwofs, new_hwofs,
234917885a7bSLuigi Rizzo 		kring->nr_hwtail,
235017885a7bSLuigi Rizzo 		tx == NR_TX ? lim : kring->nr_hwtail);
2351506cc70cSLuigi Rizzo 	kring->nkr_hwofs = new_hwofs;
235217885a7bSLuigi Rizzo 	if (tx == NR_TX) {
235317885a7bSLuigi Rizzo 		kring->nr_hwtail = kring->nr_hwcur + lim;
235417885a7bSLuigi Rizzo 		if (kring->nr_hwtail > lim)
235517885a7bSLuigi Rizzo 			kring->nr_hwtail -= lim + 1;
235617885a7bSLuigi Rizzo 	}
2357506cc70cSLuigi Rizzo 
2358f196ce38SLuigi Rizzo #if 0 // def linux
2359f196ce38SLuigi Rizzo 	/* XXX check that the mappings are correct */
2360f196ce38SLuigi Rizzo 	/* need ring_nr, adapter->pdev, direction */
2361f196ce38SLuigi Rizzo 	buffer_info->dma = dma_map_single(&pdev->dev, addr, adapter->rx_buffer_len, DMA_FROM_DEVICE);
2362f196ce38SLuigi Rizzo 	if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
2363f196ce38SLuigi Rizzo 		D("error mapping rx netmap buffer %d", i);
2364f196ce38SLuigi Rizzo 		// XXX fix error handling
2365f196ce38SLuigi Rizzo 	}
2366f196ce38SLuigi Rizzo 
2367f196ce38SLuigi Rizzo #endif /* linux */
236868b8534bSLuigi Rizzo 	/*
2369ce3ee1e7SLuigi Rizzo 	 * Wakeup on the individual and global selwait
2370506cc70cSLuigi Rizzo 	 * We do the wakeup here, but the ring is not yet reconfigured.
2371506cc70cSLuigi Rizzo 	 * However, we are under lock so there are no races.
237268b8534bSLuigi Rizzo 	 */
2373f9790aebSLuigi Rizzo 	na->nm_notify(na, n, tx, NAF_GLOBAL_NOTIFY);
237468b8534bSLuigi Rizzo 	return kring->ring->slot;
237568b8534bSLuigi Rizzo }
237668b8534bSLuigi Rizzo 
237768b8534bSLuigi Rizzo 
2378ce3ee1e7SLuigi Rizzo /*
2379f9790aebSLuigi Rizzo  * Dispatch rx/tx interrupts to the netmap rings.
2380ce3ee1e7SLuigi Rizzo  *
2381ce3ee1e7SLuigi Rizzo  * "work_done" is non-null on the RX path, NULL for the TX path.
2382ce3ee1e7SLuigi Rizzo  * We rely on the OS to make sure that there is only one active
2383ce3ee1e7SLuigi Rizzo  * instance per queue, and that there is appropriate locking.
2384849bec0eSLuigi Rizzo  *
2385f9790aebSLuigi Rizzo  * The 'notify' routine depends on what the ring is attached to.
2386f9790aebSLuigi Rizzo  * - for a netmap file descriptor, do a selwakeup on the individual
2387f9790aebSLuigi Rizzo  *   waitqueue, plus one on the global one if needed
2388f9790aebSLuigi Rizzo  * - for a switch, call the proper forwarding routine
2389f9790aebSLuigi Rizzo  * - XXX more ?
2390f9790aebSLuigi Rizzo  */
2391f9790aebSLuigi Rizzo void
2392f9790aebSLuigi Rizzo netmap_common_irq(struct ifnet *ifp, u_int q, u_int *work_done)
2393f9790aebSLuigi Rizzo {
2394f9790aebSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
2395f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
2396f9790aebSLuigi Rizzo 
2397f9790aebSLuigi Rizzo 	q &= NETMAP_RING_MASK;
2398f9790aebSLuigi Rizzo 
2399f9790aebSLuigi Rizzo 	if (netmap_verbose) {
2400f9790aebSLuigi Rizzo 	        RD(5, "received %s queue %d", work_done ? "RX" : "TX" , q);
2401f9790aebSLuigi Rizzo 	}
2402f9790aebSLuigi Rizzo 
2403f9790aebSLuigi Rizzo 	if (work_done) { /* RX path */
2404f9790aebSLuigi Rizzo 		if (q >= na->num_rx_rings)
2405f9790aebSLuigi Rizzo 			return;	// not a physical queue
2406f9790aebSLuigi Rizzo 		kring = na->rx_rings + q;
2407f9790aebSLuigi Rizzo 		kring->nr_kflags |= NKR_PENDINTR;	// XXX atomic ?
2408f9790aebSLuigi Rizzo 		na->nm_notify(na, q, NR_RX,
2409f9790aebSLuigi Rizzo 			(na->num_rx_rings > 1 ? NAF_GLOBAL_NOTIFY : 0));
2410f9790aebSLuigi Rizzo 		*work_done = 1; /* do not fire napi again */
2411f9790aebSLuigi Rizzo 	} else { /* TX path */
2412f9790aebSLuigi Rizzo 		if (q >= na->num_tx_rings)
2413f9790aebSLuigi Rizzo 			return;	// not a physical queue
2414f9790aebSLuigi Rizzo 		kring = na->tx_rings + q;
2415f9790aebSLuigi Rizzo 		na->nm_notify(na, q, NR_TX,
2416f9790aebSLuigi Rizzo 			(na->num_tx_rings > 1 ? NAF_GLOBAL_NOTIFY : 0));
2417f9790aebSLuigi Rizzo 	}
2418f9790aebSLuigi Rizzo }
2419f9790aebSLuigi Rizzo 
242017885a7bSLuigi Rizzo 
2421f9790aebSLuigi Rizzo /*
2422f9790aebSLuigi Rizzo  * Default functions to handle rx/tx interrupts from a physical device.
2423f9790aebSLuigi Rizzo  * "work_done" is non-null on the RX path, NULL for the TX path.
2424f9790aebSLuigi Rizzo  *
2425ce3ee1e7SLuigi Rizzo  * If the card is not in netmap mode, simply return 0,
2426ce3ee1e7SLuigi Rizzo  * so that the caller proceeds with regular processing.
2427f9790aebSLuigi Rizzo  * Otherwise call netmap_common_irq() and return 1.
2428ce3ee1e7SLuigi Rizzo  *
2429ce3ee1e7SLuigi Rizzo  * If the card is connected to a netmap file descriptor,
2430ce3ee1e7SLuigi Rizzo  * do a selwakeup on the individual queue, plus one on the global one
2431ce3ee1e7SLuigi Rizzo  * if needed (multiqueue card _and_ there are multiqueue listeners),
2432ce3ee1e7SLuigi Rizzo  * and return 1.
2433ce3ee1e7SLuigi Rizzo  *
2434ce3ee1e7SLuigi Rizzo  * Finally, if called on rx from an interface connected to a switch,
2435ce3ee1e7SLuigi Rizzo  * calls the proper forwarding routine, and return 1.
24361a26580eSLuigi Rizzo  */
2437babc7c12SLuigi Rizzo int
2438ce3ee1e7SLuigi Rizzo netmap_rx_irq(struct ifnet *ifp, u_int q, u_int *work_done)
24391a26580eSLuigi Rizzo {
2440f9790aebSLuigi Rizzo 	// XXX could we check NAF_NATIVE_ON ?
24411a26580eSLuigi Rizzo 	if (!(ifp->if_capenable & IFCAP_NETMAP))
24421a26580eSLuigi Rizzo 		return 0;
2443849bec0eSLuigi Rizzo 
2444f9790aebSLuigi Rizzo 	if (NA(ifp)->na_flags & NAF_SKIP_INTR) {
24458241616dSLuigi Rizzo 		ND("use regular interrupt");
24468241616dSLuigi Rizzo 		return 0;
24478241616dSLuigi Rizzo 	}
24488241616dSLuigi Rizzo 
2449f9790aebSLuigi Rizzo 	netmap_common_irq(ifp, q, work_done);
24501a26580eSLuigi Rizzo 	return 1;
24511a26580eSLuigi Rizzo }
24521a26580eSLuigi Rizzo 
245364ae02c3SLuigi Rizzo 
245401c7d25fSLuigi Rizzo /*
2455f9790aebSLuigi Rizzo  * Module loader and unloader
2456f196ce38SLuigi Rizzo  *
2457f9790aebSLuigi Rizzo  * netmap_init() creates the /dev/netmap device and initializes
2458f9790aebSLuigi Rizzo  * all global variables. Returns 0 on success, errno on failure
2459f9790aebSLuigi Rizzo  * (but there is no chance)
2460f9790aebSLuigi Rizzo  *
2461f9790aebSLuigi Rizzo  * netmap_fini() destroys everything.
2462f196ce38SLuigi Rizzo  */
2463babc7c12SLuigi Rizzo 
2464babc7c12SLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */
2465f9790aebSLuigi Rizzo extern struct cdevsw netmap_cdevsw;
2466babc7c12SLuigi Rizzo 
246717885a7bSLuigi Rizzo 
2468f9790aebSLuigi Rizzo void
246968b8534bSLuigi Rizzo netmap_fini(void)
247068b8534bSLuigi Rizzo {
2471f9790aebSLuigi Rizzo 	// XXX destroy_bridges() ?
2472f9790aebSLuigi Rizzo 	if (netmap_dev)
247368b8534bSLuigi Rizzo 		destroy_dev(netmap_dev);
2474ce3ee1e7SLuigi Rizzo 	netmap_mem_fini();
2475ce3ee1e7SLuigi Rizzo 	NMG_LOCK_DESTROY();
247668b8534bSLuigi Rizzo 	printf("netmap: unloaded module.\n");
247768b8534bSLuigi Rizzo }
247868b8534bSLuigi Rizzo 
247917885a7bSLuigi Rizzo 
2480f9790aebSLuigi Rizzo int
2481f9790aebSLuigi Rizzo netmap_init(void)
248268b8534bSLuigi Rizzo {
2483f9790aebSLuigi Rizzo 	int error;
248468b8534bSLuigi Rizzo 
2485f9790aebSLuigi Rizzo 	NMG_LOCK_INIT();
248668b8534bSLuigi Rizzo 
2487f9790aebSLuigi Rizzo 	error = netmap_mem_init();
2488f9790aebSLuigi Rizzo 	if (error != 0)
2489f9790aebSLuigi Rizzo 		goto fail;
2490f9790aebSLuigi Rizzo 	/* XXX could use make_dev_credv() to get error number */
2491f9790aebSLuigi Rizzo 	netmap_dev = make_dev(&netmap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660,
2492f9790aebSLuigi Rizzo 			      "netmap");
2493f9790aebSLuigi Rizzo 	if (!netmap_dev)
2494f9790aebSLuigi Rizzo 		goto fail;
2495f9790aebSLuigi Rizzo 
2496f9790aebSLuigi Rizzo 	netmap_init_bridges();
2497f9790aebSLuigi Rizzo 	printf("netmap: loaded module\n");
2498f9790aebSLuigi Rizzo 	return (0);
2499f9790aebSLuigi Rizzo fail:
250068b8534bSLuigi Rizzo 	netmap_fini();
2501f9790aebSLuigi Rizzo 	return (EINVAL); /* may be incorrect */
250268b8534bSLuigi Rizzo }
2503