xref: /freebsd/sys/dev/netmap/netmap.c (revision 8241616dc534f6cf0c95abe6b61bc2bb0378fc91)
168b8534bSLuigi Rizzo /*
2f196ce38SLuigi Rizzo  * Copyright (C) 2011-2012 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 
26f196ce38SLuigi Rizzo #define NM_BRIDGE
27f196ce38SLuigi Rizzo 
2868b8534bSLuigi Rizzo /*
2968b8534bSLuigi Rizzo  * This module supports memory mapped access to network devices,
3068b8534bSLuigi Rizzo  * see netmap(4).
3168b8534bSLuigi Rizzo  *
3268b8534bSLuigi Rizzo  * The module uses a large, memory pool allocated by the kernel
3368b8534bSLuigi Rizzo  * and accessible as mmapped memory by multiple userspace threads/processes.
3468b8534bSLuigi Rizzo  * The memory pool contains packet buffers and "netmap rings",
3568b8534bSLuigi Rizzo  * i.e. user-accessible copies of the interface's queues.
3668b8534bSLuigi Rizzo  *
3768b8534bSLuigi Rizzo  * Access to the network card works like this:
3868b8534bSLuigi Rizzo  * 1. a process/thread issues one or more open() on /dev/netmap, to create
3968b8534bSLuigi Rizzo  *    select()able file descriptor on which events are reported.
4068b8534bSLuigi Rizzo  * 2. on each descriptor, the process issues an ioctl() to identify
4168b8534bSLuigi Rizzo  *    the interface that should report events to the file descriptor.
4268b8534bSLuigi Rizzo  * 3. on each descriptor, the process issues an mmap() request to
4368b8534bSLuigi Rizzo  *    map the shared memory region within the process' address space.
4468b8534bSLuigi Rizzo  *    The list of interesting queues is indicated by a location in
4568b8534bSLuigi Rizzo  *    the shared memory region.
4668b8534bSLuigi Rizzo  * 4. using the functions in the netmap(4) userspace API, a process
4768b8534bSLuigi Rizzo  *    can look up the occupation state of a queue, access memory buffers,
4868b8534bSLuigi Rizzo  *    and retrieve received packets or enqueue packets to transmit.
4968b8534bSLuigi Rizzo  * 5. using some ioctl()s the process can synchronize the userspace view
5068b8534bSLuigi Rizzo  *    of the queue with the actual status in the kernel. This includes both
5168b8534bSLuigi Rizzo  *    receiving the notification of new packets, and transmitting new
5268b8534bSLuigi Rizzo  *    packets on the output interface.
5368b8534bSLuigi Rizzo  * 6. select() or poll() can be used to wait for events on individual
5468b8534bSLuigi Rizzo  *    transmit or receive queues (or all queues for a given interface).
5568b8534bSLuigi Rizzo  */
5668b8534bSLuigi Rizzo 
57f196ce38SLuigi Rizzo #ifdef linux
58f196ce38SLuigi Rizzo #include "bsd_glue.h"
5942a3a5bdSLuigi Rizzo static netdev_tx_t linux_netmap_start(struct sk_buff *skb, struct net_device *dev);
60f196ce38SLuigi Rizzo #endif /* linux */
6101c7d25fSLuigi Rizzo 
62f196ce38SLuigi Rizzo #ifdef __APPLE__
63f196ce38SLuigi Rizzo #include "osx_glue.h"
6401c7d25fSLuigi Rizzo #endif /* __APPLE__ */
6501c7d25fSLuigi Rizzo 
66f196ce38SLuigi Rizzo #ifdef __FreeBSD__
6768b8534bSLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */
6868b8534bSLuigi Rizzo __FBSDID("$FreeBSD$");
6968b8534bSLuigi Rizzo 
7068b8534bSLuigi Rizzo #include <sys/types.h>
7168b8534bSLuigi Rizzo #include <sys/module.h>
7268b8534bSLuigi Rizzo #include <sys/errno.h>
7368b8534bSLuigi Rizzo #include <sys/param.h>	/* defines used in kernel.h */
74506cc70cSLuigi Rizzo #include <sys/jail.h>
7568b8534bSLuigi Rizzo #include <sys/kernel.h>	/* types used in module initialization */
7668b8534bSLuigi Rizzo #include <sys/conf.h>	/* cdevsw struct */
7768b8534bSLuigi Rizzo #include <sys/uio.h>	/* uio struct */
7868b8534bSLuigi Rizzo #include <sys/sockio.h>
7968b8534bSLuigi Rizzo #include <sys/socketvar.h>	/* struct socket */
8068b8534bSLuigi Rizzo #include <sys/malloc.h>
8168b8534bSLuigi Rizzo #include <sys/mman.h>	/* PROT_EXEC */
8268b8534bSLuigi Rizzo #include <sys/poll.h>
83506cc70cSLuigi Rizzo #include <sys/proc.h>
8468b8534bSLuigi Rizzo #include <vm/vm.h>	/* vtophys */
8568b8534bSLuigi Rizzo #include <vm/pmap.h>	/* vtophys */
8668b8534bSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
8768b8534bSLuigi Rizzo #include <machine/bus.h>
8868b8534bSLuigi Rizzo #include <sys/selinfo.h>
8968b8534bSLuigi Rizzo #include <sys/sysctl.h>
9068b8534bSLuigi Rizzo #include <net/if.h>
9168b8534bSLuigi Rizzo #include <net/bpf.h>		/* BIOCIMMEDIATE */
92506cc70cSLuigi Rizzo #include <net/vnet.h>
9368b8534bSLuigi Rizzo #include <machine/bus.h>	/* bus_dmamap_* */
9468b8534bSLuigi Rizzo 
9568b8534bSLuigi Rizzo MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map");
96f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */
9768b8534bSLuigi Rizzo 
980b8ed8e0SLuigi Rizzo #include <net/netmap.h>
990b8ed8e0SLuigi Rizzo #include <dev/netmap/netmap_kern.h>
1000b8ed8e0SLuigi Rizzo 
1015819da83SLuigi Rizzo u_int netmap_total_buffers;
102*8241616dSLuigi Rizzo u_int netmap_buf_size;
1035819da83SLuigi Rizzo char *netmap_buffer_base;	/* address of an invalid buffer */
1045819da83SLuigi Rizzo 
1055819da83SLuigi Rizzo /* user-controlled variables */
1065819da83SLuigi Rizzo int netmap_verbose;
1075819da83SLuigi Rizzo 
1085819da83SLuigi Rizzo static int netmap_no_timestamp; /* don't timestamp on rxsync */
1095819da83SLuigi Rizzo 
1105819da83SLuigi Rizzo SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args");
1115819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose,
1125819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode");
1135819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp,
1145819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp");
1155819da83SLuigi Rizzo int netmap_mitigate = 1;
1165819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mitigate, CTLFLAG_RW, &netmap_mitigate, 0, "");
117c85cb1a0SLuigi Rizzo int netmap_no_pendintr = 1;
1185819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr,
1195819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_pendintr, 0, "Always look for new received packets.");
1205819da83SLuigi Rizzo 
121f196ce38SLuigi Rizzo int netmap_drop = 0;	/* debugging */
122f196ce38SLuigi Rizzo int netmap_flags = 0;	/* debug flags */
123f196ce38SLuigi Rizzo int netmap_copy = 0;	/* debugging, copy content */
124f196ce38SLuigi Rizzo 
125f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, drop, CTLFLAG_RW, &netmap_drop, 0 , "");
126f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, flags, CTLFLAG_RW, &netmap_flags, 0 , "");
127f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, copy, CTLFLAG_RW, &netmap_copy, 0 , "");
128f196ce38SLuigi Rizzo 
129f196ce38SLuigi Rizzo #ifdef NM_BRIDGE /* support for netmap bridge */
130f196ce38SLuigi Rizzo 
131f196ce38SLuigi Rizzo /*
132f196ce38SLuigi Rizzo  * system parameters.
133f196ce38SLuigi Rizzo  *
134f196ce38SLuigi Rizzo  * All switched ports have prefix NM_NAME.
135f196ce38SLuigi Rizzo  * The switch has a max of NM_BDG_MAXPORTS ports (often stored in a bitmap,
136f196ce38SLuigi Rizzo  * so a practical upper bound is 64).
137f196ce38SLuigi Rizzo  * Each tx ring is read-write, whereas rx rings are readonly (XXX not done yet).
138f196ce38SLuigi Rizzo  * The virtual interfaces use per-queue lock instead of core lock.
139f196ce38SLuigi Rizzo  * In the tx loop, we aggregate traffic in batches to make all operations
140f196ce38SLuigi Rizzo  * faster. The batch size is NM_BDG_BATCH
141f196ce38SLuigi Rizzo  */
142f196ce38SLuigi Rizzo #define	NM_NAME			"vale"	/* prefix for the interface */
143f196ce38SLuigi Rizzo #define NM_BDG_MAXPORTS		16	/* up to 64 ? */
144f196ce38SLuigi Rizzo #define NM_BRIDGE_RINGSIZE	1024	/* in the device */
145f196ce38SLuigi Rizzo #define NM_BDG_HASH		1024	/* forwarding table entries */
146f196ce38SLuigi Rizzo #define NM_BDG_BATCH		1024	/* entries in the forwarding buffer */
147f196ce38SLuigi Rizzo #define	NM_BRIDGES		4	/* number of bridges */
148f196ce38SLuigi Rizzo int netmap_bridge = NM_BDG_BATCH; /* bridge batch size */
149f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, bridge, CTLFLAG_RW, &netmap_bridge, 0 , "");
15001c7d25fSLuigi Rizzo 
151f196ce38SLuigi Rizzo #ifdef linux
152f196ce38SLuigi Rizzo #define	ADD_BDG_REF(ifp)	(NA(ifp)->if_refcount++)
153f196ce38SLuigi Rizzo #define	DROP_BDG_REF(ifp)	(NA(ifp)->if_refcount-- <= 1)
154f196ce38SLuigi Rizzo #else /* !linux */
155f196ce38SLuigi Rizzo #define	ADD_BDG_REF(ifp)	(ifp)->if_refcount++
156f196ce38SLuigi Rizzo #define	DROP_BDG_REF(ifp)	refcount_release(&(ifp)->if_refcount)
157f196ce38SLuigi Rizzo #ifdef __FreeBSD__
158f196ce38SLuigi Rizzo #include <sys/endian.h>
159f196ce38SLuigi Rizzo #include <sys/refcount.h>
160f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */
16101c7d25fSLuigi Rizzo #define prefetch(x)	__builtin_prefetch(x)
162f196ce38SLuigi Rizzo #endif /* !linux */
163f196ce38SLuigi Rizzo 
164f196ce38SLuigi Rizzo static void bdg_netmap_attach(struct ifnet *ifp);
165f196ce38SLuigi Rizzo static int bdg_netmap_reg(struct ifnet *ifp, int onoff);
166f196ce38SLuigi Rizzo /* per-tx-queue entry */
167f196ce38SLuigi Rizzo struct nm_bdg_fwd {	/* forwarding entry for a bridge */
168f196ce38SLuigi Rizzo 	void *buf;
169f196ce38SLuigi Rizzo 	uint64_t dst;	/* dst mask */
170f196ce38SLuigi Rizzo 	uint32_t src;	/* src index ? */
171f196ce38SLuigi Rizzo 	uint16_t len;	/* src len */
172f196ce38SLuigi Rizzo };
173f196ce38SLuigi Rizzo 
174f196ce38SLuigi Rizzo struct nm_hash_ent {
175f196ce38SLuigi Rizzo 	uint64_t	mac;	/* the top 2 bytes are the epoch */
176f196ce38SLuigi Rizzo 	uint64_t	ports;
177f196ce38SLuigi Rizzo };
178f196ce38SLuigi Rizzo 
179f196ce38SLuigi Rizzo /*
180f196ce38SLuigi Rizzo  * Interfaces for a bridge are all in ports[].
181f196ce38SLuigi Rizzo  * The array has fixed size, an empty entry does not terminate
182f196ce38SLuigi Rizzo  * the search.
183f196ce38SLuigi Rizzo  */
184f196ce38SLuigi Rizzo struct nm_bridge {
185f196ce38SLuigi Rizzo 	struct ifnet *bdg_ports[NM_BDG_MAXPORTS];
186f196ce38SLuigi Rizzo 	int n_ports;
187f196ce38SLuigi Rizzo 	uint64_t act_ports;
188f196ce38SLuigi Rizzo 	int freelist;	/* first buffer index */
189f196ce38SLuigi Rizzo 	NM_SELINFO_T si;	/* poll/select wait queue */
190f196ce38SLuigi Rizzo 	NM_LOCK_T bdg_lock;	/* protect the selinfo ? */
191f196ce38SLuigi Rizzo 
192f196ce38SLuigi Rizzo 	/* the forwarding table, MAC+ports */
193f196ce38SLuigi Rizzo 	struct nm_hash_ent ht[NM_BDG_HASH];
194f196ce38SLuigi Rizzo 
195f196ce38SLuigi Rizzo 	int namelen;	/* 0 means free */
196f196ce38SLuigi Rizzo 	char basename[IFNAMSIZ];
197f196ce38SLuigi Rizzo };
198f196ce38SLuigi Rizzo 
199f196ce38SLuigi Rizzo struct nm_bridge nm_bridges[NM_BRIDGES];
200f196ce38SLuigi Rizzo 
201f196ce38SLuigi Rizzo #define BDG_LOCK(b)	mtx_lock(&(b)->bdg_lock)
202f196ce38SLuigi Rizzo #define BDG_UNLOCK(b)	mtx_unlock(&(b)->bdg_lock)
203f196ce38SLuigi Rizzo 
204f196ce38SLuigi Rizzo /*
205f196ce38SLuigi Rizzo  * NA(ifp)->bdg_port	port index
206f196ce38SLuigi Rizzo  */
207f196ce38SLuigi Rizzo 
208f196ce38SLuigi Rizzo // XXX only for multiples of 64 bytes, non overlapped.
209f196ce38SLuigi Rizzo static inline void
210f196ce38SLuigi Rizzo pkt_copy(void *_src, void *_dst, int l)
211f196ce38SLuigi Rizzo {
212f196ce38SLuigi Rizzo         uint64_t *src = _src;
213f196ce38SLuigi Rizzo         uint64_t *dst = _dst;
214f196ce38SLuigi Rizzo         if (unlikely(l >= 1024)) {
215f196ce38SLuigi Rizzo                 bcopy(src, dst, l);
216f196ce38SLuigi Rizzo                 return;
217f196ce38SLuigi Rizzo         }
218f196ce38SLuigi Rizzo         for (; likely(l > 0); l-=64) {
219f196ce38SLuigi Rizzo                 *dst++ = *src++;
220f196ce38SLuigi Rizzo                 *dst++ = *src++;
221f196ce38SLuigi Rizzo                 *dst++ = *src++;
222f196ce38SLuigi Rizzo                 *dst++ = *src++;
223f196ce38SLuigi Rizzo                 *dst++ = *src++;
224f196ce38SLuigi Rizzo                 *dst++ = *src++;
225f196ce38SLuigi Rizzo                 *dst++ = *src++;
226f196ce38SLuigi Rizzo                 *dst++ = *src++;
227f196ce38SLuigi Rizzo         }
228f196ce38SLuigi Rizzo }
229f196ce38SLuigi Rizzo 
230f196ce38SLuigi Rizzo /*
231f196ce38SLuigi Rizzo  * locate a bridge among the existing ones.
232f196ce38SLuigi Rizzo  * a ':' in the name terminates the bridge name. Otherwise, just NM_NAME.
233f196ce38SLuigi Rizzo  * We assume that this is called with a name of at least NM_NAME chars.
234f196ce38SLuigi Rizzo  */
235f196ce38SLuigi Rizzo static struct nm_bridge *
236f196ce38SLuigi Rizzo nm_find_bridge(const char *name)
237f196ce38SLuigi Rizzo {
238f196ce38SLuigi Rizzo 	int i, l, namelen, e;
239f196ce38SLuigi Rizzo 	struct nm_bridge *b = NULL;
240f196ce38SLuigi Rizzo 
241f196ce38SLuigi Rizzo 	namelen = strlen(NM_NAME);	/* base length */
242f196ce38SLuigi Rizzo 	l = strlen(name);		/* actual length */
243f196ce38SLuigi Rizzo 	for (i = namelen + 1; i < l; i++) {
244f196ce38SLuigi Rizzo 		if (name[i] == ':') {
245f196ce38SLuigi Rizzo 			namelen = i;
246f196ce38SLuigi Rizzo 			break;
247f196ce38SLuigi Rizzo 		}
248f196ce38SLuigi Rizzo 	}
249f196ce38SLuigi Rizzo 	if (namelen >= IFNAMSIZ)
250f196ce38SLuigi Rizzo 		namelen = IFNAMSIZ;
251f196ce38SLuigi Rizzo 	ND("--- prefix is '%.*s' ---", namelen, name);
252f196ce38SLuigi Rizzo 
253f196ce38SLuigi Rizzo 	/* use the first entry for locking */
254f196ce38SLuigi Rizzo 	BDG_LOCK(nm_bridges); // XXX do better
255f196ce38SLuigi Rizzo 	for (e = -1, i = 1; i < NM_BRIDGES; i++) {
256f196ce38SLuigi Rizzo 		b = nm_bridges + i;
257f196ce38SLuigi Rizzo 		if (b->namelen == 0)
258f196ce38SLuigi Rizzo 			e = i;	/* record empty slot */
259f196ce38SLuigi Rizzo 		else if (strncmp(name, b->basename, namelen) == 0) {
260f196ce38SLuigi Rizzo 			ND("found '%.*s' at %d", namelen, name, i);
261f196ce38SLuigi Rizzo 			break;
262f196ce38SLuigi Rizzo 		}
263f196ce38SLuigi Rizzo 	}
264f196ce38SLuigi Rizzo 	if (i == NM_BRIDGES) { /* all full */
265f196ce38SLuigi Rizzo 		if (e == -1) { /* no empty slot */
266f196ce38SLuigi Rizzo 			b = NULL;
267f196ce38SLuigi Rizzo 		} else {
268f196ce38SLuigi Rizzo 			b = nm_bridges + e;
269f196ce38SLuigi Rizzo 			strncpy(b->basename, name, namelen);
270f196ce38SLuigi Rizzo 			b->namelen = namelen;
271f196ce38SLuigi Rizzo 		}
272f196ce38SLuigi Rizzo 	}
273f196ce38SLuigi Rizzo 	BDG_UNLOCK(nm_bridges);
274f196ce38SLuigi Rizzo 	return b;
275f196ce38SLuigi Rizzo }
276f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */
2775819da83SLuigi Rizzo 
2783c0caf6cSLuigi Rizzo /*------------- memory allocator -----------------*/
2793c0caf6cSLuigi Rizzo #ifdef NETMAP_MEM2
2803c0caf6cSLuigi Rizzo #include "netmap_mem2.c"
2813c0caf6cSLuigi Rizzo #else /* !NETMAP_MEM2 */
2823c0caf6cSLuigi Rizzo #include "netmap_mem1.c"
2833c0caf6cSLuigi Rizzo #endif /* !NETMAP_MEM2 */
2843c0caf6cSLuigi Rizzo /*------------ end of memory allocator ----------*/
28568b8534bSLuigi Rizzo 
286*8241616dSLuigi Rizzo 
287*8241616dSLuigi Rizzo /* Structure associated to each thread which registered an interface.
288*8241616dSLuigi Rizzo  *
289*8241616dSLuigi Rizzo  * The first 4 fields of this structure are written by NIOCREGIF and
290*8241616dSLuigi Rizzo  * read by poll() and NIOC?XSYNC.
291*8241616dSLuigi Rizzo  * There is low contention among writers (actually, a correct user program
292*8241616dSLuigi Rizzo  * should have no contention among writers) and among writers and readers,
293*8241616dSLuigi Rizzo  * so we use a single global lock to protect the structure initialization.
294*8241616dSLuigi Rizzo  * Since initialization involves the allocation of memory, we reuse the memory
295*8241616dSLuigi Rizzo  * allocator lock.
296*8241616dSLuigi Rizzo  * Read access to the structure is lock free. Readers must check that
297*8241616dSLuigi Rizzo  * np_nifp is not NULL before using the other fields.
298*8241616dSLuigi Rizzo  * If np_nifp is NULL initialization has not been performed, so they should
299*8241616dSLuigi Rizzo  * return an error to userlevel.
300*8241616dSLuigi Rizzo  *
301*8241616dSLuigi Rizzo  * The ref_done field is used to regulate access to the refcount in the
302*8241616dSLuigi Rizzo  * memory allocator. The refcount must be incremented at most once for
303*8241616dSLuigi Rizzo  * each open("/dev/netmap"). The increment is performed by the first
304*8241616dSLuigi Rizzo  * function that calls netmap_get_memory() (currently called by
305*8241616dSLuigi Rizzo  * mmap(), NIOCGINFO and NIOCREGIF).
306*8241616dSLuigi Rizzo  * If the refcount is incremented, it is then decremented when the
307*8241616dSLuigi Rizzo  * private structure is destroyed.
308*8241616dSLuigi Rizzo  */
30968b8534bSLuigi Rizzo struct netmap_priv_d {
310*8241616dSLuigi Rizzo 	struct netmap_if * volatile np_nifp;	/* netmap interface descriptor. */
31168b8534bSLuigi Rizzo 
31268b8534bSLuigi Rizzo 	struct ifnet	*np_ifp;	/* device for which we hold a reference */
31368b8534bSLuigi Rizzo 	int		np_ringid;	/* from the ioctl */
31468b8534bSLuigi Rizzo 	u_int		np_qfirst, np_qlast;	/* range of rings to scan */
31568b8534bSLuigi Rizzo 	uint16_t	np_txpoll;
316*8241616dSLuigi Rizzo 
317*8241616dSLuigi Rizzo 	unsigned long	ref_done;	/* use with NMA_LOCK held */
31868b8534bSLuigi Rizzo };
31968b8534bSLuigi Rizzo 
32068b8534bSLuigi Rizzo 
321*8241616dSLuigi Rizzo static int
322*8241616dSLuigi Rizzo netmap_get_memory(struct netmap_priv_d* p)
323*8241616dSLuigi Rizzo {
324*8241616dSLuigi Rizzo 	int error = 0;
325*8241616dSLuigi Rizzo 	NMA_LOCK();
326*8241616dSLuigi Rizzo 	if (!p->ref_done) {
327*8241616dSLuigi Rizzo 		error = netmap_memory_finalize();
328*8241616dSLuigi Rizzo 		if (!error)
329*8241616dSLuigi Rizzo 			p->ref_done = 1;
330*8241616dSLuigi Rizzo 	}
331*8241616dSLuigi Rizzo 	NMA_UNLOCK();
332*8241616dSLuigi Rizzo 	return error;
333*8241616dSLuigi Rizzo }
334*8241616dSLuigi Rizzo 
33568b8534bSLuigi Rizzo /*
33668b8534bSLuigi Rizzo  * File descriptor's private data destructor.
33768b8534bSLuigi Rizzo  *
33868b8534bSLuigi Rizzo  * Call nm_register(ifp,0) to stop netmap mode on the interface and
33968b8534bSLuigi Rizzo  * revert to normal operation. We expect that np_ifp has not gone.
34068b8534bSLuigi Rizzo  */
341*8241616dSLuigi Rizzo /* call with NMA_LOCK held */
34268b8534bSLuigi Rizzo static void
3435819da83SLuigi Rizzo netmap_dtor_locked(void *data)
34468b8534bSLuigi Rizzo {
34568b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = data;
34668b8534bSLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
34768b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
34868b8534bSLuigi Rizzo 	struct netmap_if *nifp = priv->np_nifp;
34968b8534bSLuigi Rizzo 
35068b8534bSLuigi Rizzo 	na->refcount--;
35168b8534bSLuigi Rizzo 	if (na->refcount <= 0) {	/* last instance */
35264ae02c3SLuigi Rizzo 		u_int i, j, lim;
35368b8534bSLuigi Rizzo 
35468b8534bSLuigi Rizzo 		D("deleting last netmap instance for %s", ifp->if_xname);
35568b8534bSLuigi Rizzo 		/*
35668b8534bSLuigi Rizzo 		 * there is a race here with *_netmap_task() and
3571a26580eSLuigi Rizzo 		 * netmap_poll(), which don't run under NETMAP_REG_LOCK.
35868b8534bSLuigi Rizzo 		 * na->refcount == 0 && na->ifp->if_capenable & IFCAP_NETMAP
35968b8534bSLuigi Rizzo 		 * (aka NETMAP_DELETING(na)) are a unique marker that the
36068b8534bSLuigi Rizzo 		 * device is dying.
36168b8534bSLuigi Rizzo 		 * Before destroying stuff we sleep a bit, and then complete
36268b8534bSLuigi Rizzo 		 * the job. NIOCREG should realize the condition and
36368b8534bSLuigi Rizzo 		 * loop until they can continue; the other routines
36468b8534bSLuigi Rizzo 		 * should check the condition at entry and quit if
36568b8534bSLuigi Rizzo 		 * they cannot run.
36668b8534bSLuigi Rizzo 		 */
3671a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
36868b8534bSLuigi Rizzo 		tsleep(na, 0, "NIOCUNREG", 4);
3691a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
37068b8534bSLuigi Rizzo 		na->nm_register(ifp, 0); /* off, clear IFCAP_NETMAP */
37168b8534bSLuigi Rizzo 		/* Wake up any sleeping threads. netmap_poll will
37268b8534bSLuigi Rizzo 		 * then return POLLERR
37368b8534bSLuigi Rizzo 		 */
374d76bf4ffSLuigi Rizzo 		for (i = 0; i < na->num_tx_rings + 1; i++)
37568b8534bSLuigi Rizzo 			selwakeuppri(&na->tx_rings[i].si, PI_NET);
376d76bf4ffSLuigi Rizzo 		for (i = 0; i < na->num_rx_rings + 1; i++)
37768b8534bSLuigi Rizzo 			selwakeuppri(&na->rx_rings[i].si, PI_NET);
37864ae02c3SLuigi Rizzo 		selwakeuppri(&na->tx_si, PI_NET);
37964ae02c3SLuigi Rizzo 		selwakeuppri(&na->rx_si, PI_NET);
38068b8534bSLuigi Rizzo 		/* release all buffers */
381d76bf4ffSLuigi Rizzo 		for (i = 0; i < na->num_tx_rings + 1; i++) {
38264ae02c3SLuigi Rizzo 			struct netmap_ring *ring = na->tx_rings[i].ring;
38368b8534bSLuigi Rizzo 			lim = na->tx_rings[i].nkr_num_slots;
38468b8534bSLuigi Rizzo 			for (j = 0; j < lim; j++)
385446ee301SLuigi Rizzo 				netmap_free_buf(nifp, ring->slot[j].buf_idx);
3862f70fca5SEd Maste 			/* knlist_destroy(&na->tx_rings[i].si.si_note); */
3872f70fca5SEd Maste 			mtx_destroy(&na->tx_rings[i].q_lock);
38864ae02c3SLuigi Rizzo 		}
389d76bf4ffSLuigi Rizzo 		for (i = 0; i < na->num_rx_rings + 1; i++) {
39064ae02c3SLuigi Rizzo 			struct netmap_ring *ring = na->rx_rings[i].ring;
39168b8534bSLuigi Rizzo 			lim = na->rx_rings[i].nkr_num_slots;
39268b8534bSLuigi Rizzo 			for (j = 0; j < lim; j++)
393446ee301SLuigi Rizzo 				netmap_free_buf(nifp, ring->slot[j].buf_idx);
3942f70fca5SEd Maste 			/* knlist_destroy(&na->rx_rings[i].si.si_note); */
3952f70fca5SEd Maste 			mtx_destroy(&na->rx_rings[i].q_lock);
39668b8534bSLuigi Rizzo 		}
3972f70fca5SEd Maste 		/* XXX kqueue(9) needed; these will mirror knlist_init. */
3982f70fca5SEd Maste 		/* knlist_destroy(&na->tx_si.si_note); */
3992f70fca5SEd Maste 		/* knlist_destroy(&na->rx_si.si_note); */
4006e10c8b8SLuigi Rizzo 		netmap_free_rings(na);
40168b8534bSLuigi Rizzo 		wakeup(na);
40268b8534bSLuigi Rizzo 	}
4036e10c8b8SLuigi Rizzo 	netmap_if_free(nifp);
4045819da83SLuigi Rizzo }
40568b8534bSLuigi Rizzo 
406f196ce38SLuigi Rizzo static void
407f196ce38SLuigi Rizzo nm_if_rele(struct ifnet *ifp)
408f196ce38SLuigi Rizzo {
409f196ce38SLuigi Rizzo #ifndef NM_BRIDGE
410f196ce38SLuigi Rizzo 	if_rele(ifp);
411f196ce38SLuigi Rizzo #else /* NM_BRIDGE */
412f196ce38SLuigi Rizzo 	int i, full;
413f196ce38SLuigi Rizzo 	struct nm_bridge *b;
414f196ce38SLuigi Rizzo 
415f196ce38SLuigi Rizzo 	if (strncmp(ifp->if_xname, NM_NAME, sizeof(NM_NAME) - 1)) {
416f196ce38SLuigi Rizzo 		if_rele(ifp);
417f196ce38SLuigi Rizzo 		return;
418f196ce38SLuigi Rizzo 	}
419f196ce38SLuigi Rizzo 	if (!DROP_BDG_REF(ifp))
420f196ce38SLuigi Rizzo 		return;
421f196ce38SLuigi Rizzo 	b = ifp->if_bridge;
422f196ce38SLuigi Rizzo 	BDG_LOCK(nm_bridges);
423f196ce38SLuigi Rizzo 	BDG_LOCK(b);
424f196ce38SLuigi Rizzo 	ND("want to disconnect %s from the bridge", ifp->if_xname);
425f196ce38SLuigi Rizzo 	full = 0;
426f196ce38SLuigi Rizzo 	for (i = 0; i < NM_BDG_MAXPORTS; i++) {
427f196ce38SLuigi Rizzo 		if (b->bdg_ports[i] == ifp) {
428f196ce38SLuigi Rizzo 			b->bdg_ports[i] = NULL;
429f196ce38SLuigi Rizzo 			bzero(ifp, sizeof(*ifp));
430f196ce38SLuigi Rizzo 			free(ifp, M_DEVBUF);
431f196ce38SLuigi Rizzo 			break;
432f196ce38SLuigi Rizzo 		}
433f196ce38SLuigi Rizzo 		else if (b->bdg_ports[i] != NULL)
434f196ce38SLuigi Rizzo 			full = 1;
435f196ce38SLuigi Rizzo 	}
436f196ce38SLuigi Rizzo 	BDG_UNLOCK(b);
437f196ce38SLuigi Rizzo 	if (full == 0) {
438f196ce38SLuigi Rizzo 		ND("freeing bridge %d", b - nm_bridges);
439f196ce38SLuigi Rizzo 		b->namelen = 0;
440f196ce38SLuigi Rizzo 	}
441f196ce38SLuigi Rizzo 	BDG_UNLOCK(nm_bridges);
442f196ce38SLuigi Rizzo 	if (i == NM_BDG_MAXPORTS)
443f196ce38SLuigi Rizzo 		D("ouch, cannot find ifp to remove");
444f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */
445f196ce38SLuigi Rizzo }
4465819da83SLuigi Rizzo 
4475819da83SLuigi Rizzo static void
4485819da83SLuigi Rizzo netmap_dtor(void *data)
4495819da83SLuigi Rizzo {
4505819da83SLuigi Rizzo 	struct netmap_priv_d *priv = data;
4515819da83SLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
452*8241616dSLuigi Rizzo 	struct netmap_adapter *na;
4535819da83SLuigi Rizzo 
454*8241616dSLuigi Rizzo 	NMA_LOCK();
455*8241616dSLuigi Rizzo 	if (ifp) {
456*8241616dSLuigi Rizzo 		na = NA(ifp);
4571a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
4585819da83SLuigi Rizzo 		netmap_dtor_locked(data);
4591a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
46068b8534bSLuigi Rizzo 
461f196ce38SLuigi Rizzo 		nm_if_rele(ifp);
462*8241616dSLuigi Rizzo 	}
463*8241616dSLuigi Rizzo 	if (priv->ref_done) {
464*8241616dSLuigi Rizzo 		netmap_memory_deref();
465*8241616dSLuigi Rizzo 	}
466*8241616dSLuigi Rizzo 	NMA_UNLOCK();
46768b8534bSLuigi Rizzo 	bzero(priv, sizeof(*priv));	/* XXX for safety */
46868b8534bSLuigi Rizzo 	free(priv, M_DEVBUF);
46968b8534bSLuigi Rizzo }
47068b8534bSLuigi Rizzo 
471*8241616dSLuigi Rizzo #ifdef __FreeBSD__
472*8241616dSLuigi Rizzo #include <vm/vm.h>
473*8241616dSLuigi Rizzo #include <vm/vm_param.h>
474*8241616dSLuigi Rizzo #include <vm/vm_object.h>
475*8241616dSLuigi Rizzo #include <vm/vm_page.h>
476*8241616dSLuigi Rizzo #include <vm/vm_pager.h>
477*8241616dSLuigi Rizzo #include <vm/uma.h>
478*8241616dSLuigi Rizzo 
479*8241616dSLuigi Rizzo static struct cdev_pager_ops saved_cdev_pager_ops;
480*8241616dSLuigi Rizzo 
481*8241616dSLuigi Rizzo static int
482*8241616dSLuigi Rizzo netmap_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
483*8241616dSLuigi Rizzo     vm_ooffset_t foff, struct ucred *cred, u_short *color)
484*8241616dSLuigi Rizzo {
485*8241616dSLuigi Rizzo 	D("first mmap for %p", handle);
486*8241616dSLuigi Rizzo 	return saved_cdev_pager_ops.cdev_pg_ctor(handle,
487*8241616dSLuigi Rizzo 			size, prot, foff, cred, color);
488*8241616dSLuigi Rizzo }
489*8241616dSLuigi Rizzo 
490*8241616dSLuigi Rizzo static void
491*8241616dSLuigi Rizzo netmap_dev_pager_dtor(void *handle)
492*8241616dSLuigi Rizzo {
493*8241616dSLuigi Rizzo 	saved_cdev_pager_ops.cdev_pg_dtor(handle);
494*8241616dSLuigi Rizzo 	D("ready to release memory for %p", handle);
495*8241616dSLuigi Rizzo }
496*8241616dSLuigi Rizzo 
497*8241616dSLuigi Rizzo 
498*8241616dSLuigi Rizzo static struct cdev_pager_ops netmap_cdev_pager_ops = {
499*8241616dSLuigi Rizzo         .cdev_pg_ctor = netmap_dev_pager_ctor,
500*8241616dSLuigi Rizzo         .cdev_pg_dtor = netmap_dev_pager_dtor,
501*8241616dSLuigi Rizzo         .cdev_pg_fault = NULL,
502*8241616dSLuigi Rizzo };
503*8241616dSLuigi Rizzo 
504*8241616dSLuigi Rizzo static int
505*8241616dSLuigi Rizzo netmap_mmap_single(struct cdev *cdev, vm_ooffset_t *foff,
506*8241616dSLuigi Rizzo 	vm_size_t objsize,  vm_object_t *objp, int prot)
507*8241616dSLuigi Rizzo {
508*8241616dSLuigi Rizzo 	vm_object_t obj;
509*8241616dSLuigi Rizzo 
510*8241616dSLuigi Rizzo 	D("cdev %p foff %d size %d objp %p prot %d", cdev, *foff,
511*8241616dSLuigi Rizzo 		objsize, objp, prot);
512*8241616dSLuigi Rizzo 	obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
513*8241616dSLuigi Rizzo             curthread->td_ucred);
514*8241616dSLuigi Rizzo 	ND("returns obj %p", obj);
515*8241616dSLuigi Rizzo 	if (obj == NULL)
516*8241616dSLuigi Rizzo 		return EINVAL;
517*8241616dSLuigi Rizzo 	if (saved_cdev_pager_ops.cdev_pg_fault == NULL) {
518*8241616dSLuigi Rizzo 		D("initialize cdev_pager_ops");
519*8241616dSLuigi Rizzo 		saved_cdev_pager_ops = *(obj->un_pager.devp.ops);
520*8241616dSLuigi Rizzo 		netmap_cdev_pager_ops.cdev_pg_fault =
521*8241616dSLuigi Rizzo 			saved_cdev_pager_ops.cdev_pg_fault;
522*8241616dSLuigi Rizzo 	};
523*8241616dSLuigi Rizzo 	obj->un_pager.devp.ops = &netmap_cdev_pager_ops;
524*8241616dSLuigi Rizzo 	*objp = obj;
525*8241616dSLuigi Rizzo 	return 0;
526*8241616dSLuigi Rizzo }
527*8241616dSLuigi Rizzo #endif /* __FreeBSD__ */
528*8241616dSLuigi Rizzo 
52968b8534bSLuigi Rizzo 
53068b8534bSLuigi Rizzo /*
53168b8534bSLuigi Rizzo  * mmap(2) support for the "netmap" device.
53268b8534bSLuigi Rizzo  *
53368b8534bSLuigi Rizzo  * Expose all the memory previously allocated by our custom memory
53468b8534bSLuigi Rizzo  * allocator: this way the user has only to issue a single mmap(2), and
53568b8534bSLuigi Rizzo  * can work on all the data structures flawlessly.
53668b8534bSLuigi Rizzo  *
53768b8534bSLuigi Rizzo  * Return 0 on success, -1 otherwise.
53868b8534bSLuigi Rizzo  */
539babc7c12SLuigi Rizzo 
540f196ce38SLuigi Rizzo #ifdef __FreeBSD__
54168b8534bSLuigi Rizzo static int
542babc7c12SLuigi Rizzo netmap_mmap(__unused struct cdev *dev,
54368b8534bSLuigi Rizzo #if __FreeBSD_version < 900000
544babc7c12SLuigi Rizzo 		vm_offset_t offset, vm_paddr_t *paddr, int nprot
54568b8534bSLuigi Rizzo #else
546babc7c12SLuigi Rizzo 		vm_ooffset_t offset, vm_paddr_t *paddr, int nprot,
547babc7c12SLuigi Rizzo 		__unused vm_memattr_t *memattr
54868b8534bSLuigi Rizzo #endif
549babc7c12SLuigi Rizzo 	)
55068b8534bSLuigi Rizzo {
551*8241616dSLuigi Rizzo 	int error = 0;
552*8241616dSLuigi Rizzo 	struct netmap_priv_d *priv;
553*8241616dSLuigi Rizzo 
55468b8534bSLuigi Rizzo 	if (nprot & PROT_EXEC)
55568b8534bSLuigi Rizzo 		return (-1);	// XXX -1 or EINVAL ?
556446ee301SLuigi Rizzo 
557*8241616dSLuigi Rizzo 	error = devfs_get_cdevpriv((void **)&priv);
558*8241616dSLuigi Rizzo 	if (error == EBADF) {	/* called on fault, memory is initialized */
559*8241616dSLuigi Rizzo 		ND(5, "handling fault at ofs 0x%x", offset);
560*8241616dSLuigi Rizzo 		error = 0;
561*8241616dSLuigi Rizzo 	} else if (error == 0)	/* make sure memory is set */
562*8241616dSLuigi Rizzo 		error = netmap_get_memory(priv);
563*8241616dSLuigi Rizzo 	if (error)
564*8241616dSLuigi Rizzo 		return (error);
565*8241616dSLuigi Rizzo 
56668b8534bSLuigi Rizzo 	ND("request for offset 0x%x", (uint32_t)offset);
567446ee301SLuigi Rizzo 	*paddr = netmap_ofstophys(offset);
56868b8534bSLuigi Rizzo 
569*8241616dSLuigi Rizzo 	return (*paddr ? 0 : ENOMEM);
570*8241616dSLuigi Rizzo }
571*8241616dSLuigi Rizzo 
572*8241616dSLuigi Rizzo static int
573*8241616dSLuigi Rizzo netmap_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
574*8241616dSLuigi Rizzo {
575*8241616dSLuigi Rizzo 	D("dev %p fflag 0x%x devtype %d td %p", dev, fflag, devtype, td);
576*8241616dSLuigi Rizzo 	return 0;
577*8241616dSLuigi Rizzo }
578*8241616dSLuigi Rizzo 
579*8241616dSLuigi Rizzo static int
580*8241616dSLuigi Rizzo netmap_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
581*8241616dSLuigi Rizzo {
582*8241616dSLuigi Rizzo 	struct netmap_priv_d *priv;
583*8241616dSLuigi Rizzo 	int error;
584*8241616dSLuigi Rizzo 
585*8241616dSLuigi Rizzo 	priv = malloc(sizeof(struct netmap_priv_d), M_DEVBUF,
586*8241616dSLuigi Rizzo 			      M_NOWAIT | M_ZERO);
587*8241616dSLuigi Rizzo 	if (priv == NULL)
588*8241616dSLuigi Rizzo 		return ENOMEM;
589*8241616dSLuigi Rizzo 
590*8241616dSLuigi Rizzo 	error = devfs_set_cdevpriv(priv, netmap_dtor);
591*8241616dSLuigi Rizzo 	if (error)
592*8241616dSLuigi Rizzo 	        return error;
593*8241616dSLuigi Rizzo 
594*8241616dSLuigi Rizzo 	return 0;
59568b8534bSLuigi Rizzo }
596f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */
59768b8534bSLuigi Rizzo 
59868b8534bSLuigi Rizzo 
59968b8534bSLuigi Rizzo /*
60002ad4083SLuigi Rizzo  * Handlers for synchronization of the queues from/to the host.
60102ad4083SLuigi Rizzo  *
60202ad4083SLuigi Rizzo  * netmap_sync_to_host() passes packets up. We are called from a
60302ad4083SLuigi Rizzo  * system call in user process context, and the only contention
60402ad4083SLuigi Rizzo  * can be among multiple user threads erroneously calling
60502ad4083SLuigi Rizzo  * this routine concurrently. In principle we should not even
60602ad4083SLuigi Rizzo  * need to lock.
60768b8534bSLuigi Rizzo  */
60868b8534bSLuigi Rizzo static void
60968b8534bSLuigi Rizzo netmap_sync_to_host(struct netmap_adapter *na)
61068b8534bSLuigi Rizzo {
611d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->tx_rings[na->num_tx_rings];
61268b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
61368b8534bSLuigi Rizzo 	struct mbuf *head = NULL, *tail = NULL, *m;
61402ad4083SLuigi Rizzo 	u_int k, n, lim = kring->nkr_num_slots - 1;
61568b8534bSLuigi Rizzo 
61602ad4083SLuigi Rizzo 	k = ring->cur;
61702ad4083SLuigi Rizzo 	if (k > lim) {
61802ad4083SLuigi Rizzo 		netmap_ring_reinit(kring);
61902ad4083SLuigi Rizzo 		return;
62002ad4083SLuigi Rizzo 	}
6211a26580eSLuigi Rizzo 	// na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0);
62268b8534bSLuigi Rizzo 
62368b8534bSLuigi Rizzo 	/* Take packets from hwcur to cur and pass them up.
62468b8534bSLuigi Rizzo 	 * In case of no buffers we give up. At the end of the loop,
62568b8534bSLuigi Rizzo 	 * the queue is drained in all cases.
62668b8534bSLuigi Rizzo 	 */
62702ad4083SLuigi Rizzo 	for (n = kring->nr_hwcur; n != k;) {
62868b8534bSLuigi Rizzo 		struct netmap_slot *slot = &ring->slot[n];
62968b8534bSLuigi Rizzo 
63068b8534bSLuigi Rizzo 		n = (n == lim) ? 0 : n + 1;
63168b8534bSLuigi Rizzo 		if (slot->len < 14 || slot->len > NETMAP_BUF_SIZE) {
63268b8534bSLuigi Rizzo 			D("bad pkt at %d len %d", n, slot->len);
63368b8534bSLuigi Rizzo 			continue;
63468b8534bSLuigi Rizzo 		}
63568b8534bSLuigi Rizzo 		m = m_devget(NMB(slot), slot->len, 0, na->ifp, NULL);
63668b8534bSLuigi Rizzo 
63768b8534bSLuigi Rizzo 		if (m == NULL)
63868b8534bSLuigi Rizzo 			break;
63968b8534bSLuigi Rizzo 		if (tail)
64068b8534bSLuigi Rizzo 			tail->m_nextpkt = m;
64168b8534bSLuigi Rizzo 		else
64268b8534bSLuigi Rizzo 			head = m;
64368b8534bSLuigi Rizzo 		tail = m;
64468b8534bSLuigi Rizzo 		m->m_nextpkt = NULL;
64568b8534bSLuigi Rizzo 	}
64602ad4083SLuigi Rizzo 	kring->nr_hwcur = k;
64768b8534bSLuigi Rizzo 	kring->nr_hwavail = ring->avail = lim;
6481a26580eSLuigi Rizzo 	// na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0);
64968b8534bSLuigi Rizzo 
65068b8534bSLuigi Rizzo 	/* send packets up, outside the lock */
65168b8534bSLuigi Rizzo 	while ((m = head) != NULL) {
65268b8534bSLuigi Rizzo 		head = head->m_nextpkt;
65368b8534bSLuigi Rizzo 		m->m_nextpkt = NULL;
65468b8534bSLuigi Rizzo 		if (netmap_verbose & NM_VERB_HOST)
6551a26580eSLuigi Rizzo 			D("sending up pkt %p size %d", m, MBUF_LEN(m));
6561a26580eSLuigi Rizzo 		NM_SEND_UP(na->ifp, m);
65768b8534bSLuigi Rizzo 	}
65868b8534bSLuigi Rizzo }
65968b8534bSLuigi Rizzo 
66068b8534bSLuigi Rizzo /*
66102ad4083SLuigi Rizzo  * rxsync backend for packets coming from the host stack.
66202ad4083SLuigi Rizzo  * They have been put in the queue by netmap_start() so we
66302ad4083SLuigi Rizzo  * need to protect access to the kring using a lock.
66402ad4083SLuigi Rizzo  *
66568b8534bSLuigi Rizzo  * This routine also does the selrecord if called from the poll handler
66668b8534bSLuigi Rizzo  * (we know because td != NULL).
66701c7d25fSLuigi Rizzo  *
66801c7d25fSLuigi Rizzo  * NOTE: on linux, selrecord() is defined as a macro and uses pwait
66901c7d25fSLuigi Rizzo  *     as an additional hidden argument.
67068b8534bSLuigi Rizzo  */
67168b8534bSLuigi Rizzo static void
67201c7d25fSLuigi Rizzo netmap_sync_from_host(struct netmap_adapter *na, struct thread *td, void *pwait)
67368b8534bSLuigi Rizzo {
674d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
67568b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
67664ae02c3SLuigi Rizzo 	u_int j, n, lim = kring->nkr_num_slots;
67764ae02c3SLuigi Rizzo 	u_int k = ring->cur, resvd = ring->reserved;
67868b8534bSLuigi Rizzo 
67901c7d25fSLuigi Rizzo 	(void)pwait;	/* disable unused warnings */
6801a26580eSLuigi Rizzo 	na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0);
68164ae02c3SLuigi Rizzo 	if (k >= lim) {
68264ae02c3SLuigi Rizzo 		netmap_ring_reinit(kring);
68364ae02c3SLuigi Rizzo 		return;
68464ae02c3SLuigi Rizzo 	}
68564ae02c3SLuigi Rizzo 	/* new packets are already set in nr_hwavail */
68664ae02c3SLuigi Rizzo 	/* skip past packets that userspace has released */
68764ae02c3SLuigi Rizzo 	j = kring->nr_hwcur;
68864ae02c3SLuigi Rizzo 	if (resvd > 0) {
68964ae02c3SLuigi Rizzo 		if (resvd + ring->avail >= lim + 1) {
69064ae02c3SLuigi Rizzo 			D("XXX invalid reserve/avail %d %d", resvd, ring->avail);
69164ae02c3SLuigi Rizzo 			ring->reserved = resvd = 0; // XXX panic...
69264ae02c3SLuigi Rizzo 		}
69364ae02c3SLuigi Rizzo 		k = (k >= resvd) ? k - resvd : k + lim - resvd;
69464ae02c3SLuigi Rizzo         }
69564ae02c3SLuigi Rizzo 	if (j != k) {
69664ae02c3SLuigi Rizzo 		n = k >= j ? k - j : k + lim - j;
69764ae02c3SLuigi Rizzo 		kring->nr_hwavail -= n;
69802ad4083SLuigi Rizzo 		kring->nr_hwcur = k;
69964ae02c3SLuigi Rizzo 	}
70064ae02c3SLuigi Rizzo 	k = ring->avail = kring->nr_hwavail - resvd;
70102ad4083SLuigi Rizzo 	if (k == 0 && td)
70268b8534bSLuigi Rizzo 		selrecord(td, &kring->si);
70302ad4083SLuigi Rizzo 	if (k && (netmap_verbose & NM_VERB_HOST))
70402ad4083SLuigi Rizzo 		D("%d pkts from stack", k);
7051a26580eSLuigi Rizzo 	na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0);
70668b8534bSLuigi Rizzo }
70768b8534bSLuigi Rizzo 
70868b8534bSLuigi Rizzo 
70968b8534bSLuigi Rizzo /*
71068b8534bSLuigi Rizzo  * get a refcounted reference to an interface.
71168b8534bSLuigi Rizzo  * Return ENXIO if the interface does not exist, EINVAL if netmap
71268b8534bSLuigi Rizzo  * is not supported by the interface.
71368b8534bSLuigi Rizzo  * If successful, hold a reference.
71468b8534bSLuigi Rizzo  */
71568b8534bSLuigi Rizzo static int
71668b8534bSLuigi Rizzo get_ifp(const char *name, struct ifnet **ifp)
71768b8534bSLuigi Rizzo {
718f196ce38SLuigi Rizzo #ifdef NM_BRIDGE
719f196ce38SLuigi Rizzo 	struct ifnet *iter = NULL;
720f196ce38SLuigi Rizzo 
721f196ce38SLuigi Rizzo 	do {
722f196ce38SLuigi Rizzo 		struct nm_bridge *b;
723f196ce38SLuigi Rizzo 		int i, l, cand = -1;
724f196ce38SLuigi Rizzo 
725f196ce38SLuigi Rizzo 		if (strncmp(name, NM_NAME, sizeof(NM_NAME) - 1))
726f196ce38SLuigi Rizzo 			break;
727f196ce38SLuigi Rizzo 		b = nm_find_bridge(name);
728f196ce38SLuigi Rizzo 		if (b == NULL) {
729f196ce38SLuigi Rizzo 			D("no bridges available for '%s'", name);
730f196ce38SLuigi Rizzo 			return (ENXIO);
731f196ce38SLuigi Rizzo 		}
732f196ce38SLuigi Rizzo 		/* XXX locking */
733f196ce38SLuigi Rizzo 		BDG_LOCK(b);
734f196ce38SLuigi Rizzo 		/* lookup in the local list of ports */
735f196ce38SLuigi Rizzo 		for (i = 0; i < NM_BDG_MAXPORTS; i++) {
736f196ce38SLuigi Rizzo 			iter = b->bdg_ports[i];
737f196ce38SLuigi Rizzo 			if (iter == NULL) {
738f196ce38SLuigi Rizzo 				if (cand == -1)
739f196ce38SLuigi Rizzo 					cand = i; /* potential insert point */
740f196ce38SLuigi Rizzo 				continue;
741f196ce38SLuigi Rizzo 			}
742f196ce38SLuigi Rizzo 			if (!strcmp(iter->if_xname, name)) {
743f196ce38SLuigi Rizzo 				ADD_BDG_REF(iter);
744f196ce38SLuigi Rizzo 				ND("found existing interface");
745f196ce38SLuigi Rizzo 				BDG_UNLOCK(b);
746f196ce38SLuigi Rizzo 				break;
747f196ce38SLuigi Rizzo 			}
748f196ce38SLuigi Rizzo 		}
749f196ce38SLuigi Rizzo 		if (i < NM_BDG_MAXPORTS) /* already unlocked */
750f196ce38SLuigi Rizzo 			break;
751f196ce38SLuigi Rizzo 		if (cand == -1) {
752f196ce38SLuigi Rizzo 			D("bridge full, cannot create new port");
753f196ce38SLuigi Rizzo no_port:
754f196ce38SLuigi Rizzo 			BDG_UNLOCK(b);
755f196ce38SLuigi Rizzo 			*ifp = NULL;
756f196ce38SLuigi Rizzo 			return EINVAL;
757f196ce38SLuigi Rizzo 		}
758f196ce38SLuigi Rizzo 		ND("create new bridge port %s", name);
759f196ce38SLuigi Rizzo 		/* space for forwarding list after the ifnet */
760f196ce38SLuigi Rizzo 		l = sizeof(*iter) +
761f196ce38SLuigi Rizzo 			 sizeof(struct nm_bdg_fwd)*NM_BDG_BATCH ;
762f196ce38SLuigi Rizzo 		iter = malloc(l, M_DEVBUF, M_NOWAIT | M_ZERO);
763f196ce38SLuigi Rizzo 		if (!iter)
764f196ce38SLuigi Rizzo 			goto no_port;
765f196ce38SLuigi Rizzo 		strcpy(iter->if_xname, name);
766f196ce38SLuigi Rizzo 		bdg_netmap_attach(iter);
767f196ce38SLuigi Rizzo 		b->bdg_ports[cand] = iter;
768f196ce38SLuigi Rizzo 		iter->if_bridge = b;
769f196ce38SLuigi Rizzo 		ADD_BDG_REF(iter);
770f196ce38SLuigi Rizzo 		BDG_UNLOCK(b);
771f196ce38SLuigi Rizzo 		ND("attaching virtual bridge %p", b);
772f196ce38SLuigi Rizzo 	} while (0);
773f196ce38SLuigi Rizzo 	*ifp = iter;
774f196ce38SLuigi Rizzo 	if (! *ifp)
775f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */
77668b8534bSLuigi Rizzo 	*ifp = ifunit_ref(name);
77768b8534bSLuigi Rizzo 	if (*ifp == NULL)
77868b8534bSLuigi Rizzo 		return (ENXIO);
77968b8534bSLuigi Rizzo 	/* can do this if the capability exists and if_pspare[0]
78068b8534bSLuigi Rizzo 	 * points to the netmap descriptor.
78168b8534bSLuigi Rizzo 	 */
782*8241616dSLuigi Rizzo 	if (NETMAP_CAPABLE(*ifp))
78368b8534bSLuigi Rizzo 		return 0;	/* valid pointer, we hold the refcount */
784f196ce38SLuigi Rizzo 	nm_if_rele(*ifp);
78568b8534bSLuigi Rizzo 	return EINVAL;	// not NETMAP capable
78668b8534bSLuigi Rizzo }
78768b8534bSLuigi Rizzo 
78868b8534bSLuigi Rizzo 
78968b8534bSLuigi Rizzo /*
79068b8534bSLuigi Rizzo  * Error routine called when txsync/rxsync detects an error.
79168b8534bSLuigi Rizzo  * Can't do much more than resetting cur = hwcur, avail = hwavail.
79268b8534bSLuigi Rizzo  * Return 1 on reinit.
793506cc70cSLuigi Rizzo  *
794506cc70cSLuigi Rizzo  * This routine is only called by the upper half of the kernel.
795506cc70cSLuigi Rizzo  * It only reads hwcur (which is changed only by the upper half, too)
796506cc70cSLuigi Rizzo  * and hwavail (which may be changed by the lower half, but only on
797506cc70cSLuigi Rizzo  * a tx ring and only to increase it, so any error will be recovered
798506cc70cSLuigi Rizzo  * on the next call). For the above, we don't strictly need to call
799506cc70cSLuigi Rizzo  * it under lock.
80068b8534bSLuigi Rizzo  */
80168b8534bSLuigi Rizzo int
80268b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring)
80368b8534bSLuigi Rizzo {
80468b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
80568b8534bSLuigi Rizzo 	u_int i, lim = kring->nkr_num_slots - 1;
80668b8534bSLuigi Rizzo 	int errors = 0;
80768b8534bSLuigi Rizzo 
808*8241616dSLuigi Rizzo 	RD(10, "called for %s", kring->na->ifp->if_xname);
80968b8534bSLuigi Rizzo 	if (ring->cur > lim)
81068b8534bSLuigi Rizzo 		errors++;
81168b8534bSLuigi Rizzo 	for (i = 0; i <= lim; i++) {
81268b8534bSLuigi Rizzo 		u_int idx = ring->slot[i].buf_idx;
81368b8534bSLuigi Rizzo 		u_int len = ring->slot[i].len;
81468b8534bSLuigi Rizzo 		if (idx < 2 || idx >= netmap_total_buffers) {
81568b8534bSLuigi Rizzo 			if (!errors++)
81668b8534bSLuigi Rizzo 				D("bad buffer at slot %d idx %d len %d ", i, idx, len);
81768b8534bSLuigi Rizzo 			ring->slot[i].buf_idx = 0;
81868b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
81968b8534bSLuigi Rizzo 		} else if (len > NETMAP_BUF_SIZE) {
82068b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
82168b8534bSLuigi Rizzo 			if (!errors++)
82268b8534bSLuigi Rizzo 				D("bad len %d at slot %d idx %d",
82368b8534bSLuigi Rizzo 					len, i, idx);
82468b8534bSLuigi Rizzo 		}
82568b8534bSLuigi Rizzo 	}
82668b8534bSLuigi Rizzo 	if (errors) {
82768b8534bSLuigi Rizzo 		int pos = kring - kring->na->tx_rings;
828d76bf4ffSLuigi Rizzo 		int n = kring->na->num_tx_rings + 1;
82968b8534bSLuigi Rizzo 
830*8241616dSLuigi Rizzo 		RD(10, "total %d errors", errors);
83168b8534bSLuigi Rizzo 		errors++;
832*8241616dSLuigi Rizzo 		RD(10, "%s %s[%d] reinit, cur %d -> %d avail %d -> %d",
83368b8534bSLuigi Rizzo 			kring->na->ifp->if_xname,
83468b8534bSLuigi Rizzo 			pos < n ?  "TX" : "RX", pos < n ? pos : pos - n,
83568b8534bSLuigi Rizzo 			ring->cur, kring->nr_hwcur,
83668b8534bSLuigi Rizzo 			ring->avail, kring->nr_hwavail);
83768b8534bSLuigi Rizzo 		ring->cur = kring->nr_hwcur;
83868b8534bSLuigi Rizzo 		ring->avail = kring->nr_hwavail;
83968b8534bSLuigi Rizzo 	}
84068b8534bSLuigi Rizzo 	return (errors ? 1 : 0);
84168b8534bSLuigi Rizzo }
84268b8534bSLuigi Rizzo 
84368b8534bSLuigi Rizzo 
84468b8534bSLuigi Rizzo /*
84568b8534bSLuigi Rizzo  * Set the ring ID. For devices with a single queue, a request
84668b8534bSLuigi Rizzo  * for all rings is the same as a single ring.
84768b8534bSLuigi Rizzo  */
84868b8534bSLuigi Rizzo static int
84968b8534bSLuigi Rizzo netmap_set_ringid(struct netmap_priv_d *priv, u_int ringid)
85068b8534bSLuigi Rizzo {
85168b8534bSLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
85268b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
85368b8534bSLuigi Rizzo 	u_int i = ringid & NETMAP_RING_MASK;
85464ae02c3SLuigi Rizzo 	/* initially (np_qfirst == np_qlast) we don't want to lock */
85568b8534bSLuigi Rizzo 	int need_lock = (priv->np_qfirst != priv->np_qlast);
856d76bf4ffSLuigi Rizzo 	int lim = na->num_rx_rings;
85768b8534bSLuigi Rizzo 
858d76bf4ffSLuigi Rizzo 	if (na->num_tx_rings > lim)
859d76bf4ffSLuigi Rizzo 		lim = na->num_tx_rings;
86064ae02c3SLuigi Rizzo 	if ( (ringid & NETMAP_HW_RING) && i >= lim) {
86168b8534bSLuigi Rizzo 		D("invalid ring id %d", i);
86268b8534bSLuigi Rizzo 		return (EINVAL);
86368b8534bSLuigi Rizzo 	}
86468b8534bSLuigi Rizzo 	if (need_lock)
8651a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
86668b8534bSLuigi Rizzo 	priv->np_ringid = ringid;
86768b8534bSLuigi Rizzo 	if (ringid & NETMAP_SW_RING) {
86864ae02c3SLuigi Rizzo 		priv->np_qfirst = NETMAP_SW_RING;
86964ae02c3SLuigi Rizzo 		priv->np_qlast = 0;
87068b8534bSLuigi Rizzo 	} else if (ringid & NETMAP_HW_RING) {
87168b8534bSLuigi Rizzo 		priv->np_qfirst = i;
87268b8534bSLuigi Rizzo 		priv->np_qlast = i + 1;
87368b8534bSLuigi Rizzo 	} else {
87468b8534bSLuigi Rizzo 		priv->np_qfirst = 0;
87564ae02c3SLuigi Rizzo 		priv->np_qlast = NETMAP_HW_RING ;
87668b8534bSLuigi Rizzo 	}
87768b8534bSLuigi Rizzo 	priv->np_txpoll = (ringid & NETMAP_NO_TX_POLL) ? 0 : 1;
87868b8534bSLuigi Rizzo 	if (need_lock)
8791a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
88068b8534bSLuigi Rizzo 	if (ringid & NETMAP_SW_RING)
88168b8534bSLuigi Rizzo 		D("ringid %s set to SW RING", ifp->if_xname);
88268b8534bSLuigi Rizzo 	else if (ringid & NETMAP_HW_RING)
88368b8534bSLuigi Rizzo 		D("ringid %s set to HW RING %d", ifp->if_xname,
88468b8534bSLuigi Rizzo 			priv->np_qfirst);
88568b8534bSLuigi Rizzo 	else
88664ae02c3SLuigi Rizzo 		D("ringid %s set to all %d HW RINGS", ifp->if_xname, lim);
88768b8534bSLuigi Rizzo 	return 0;
88868b8534bSLuigi Rizzo }
88968b8534bSLuigi Rizzo 
89068b8534bSLuigi Rizzo /*
89168b8534bSLuigi Rizzo  * ioctl(2) support for the "netmap" device.
89268b8534bSLuigi Rizzo  *
89368b8534bSLuigi Rizzo  * Following a list of accepted commands:
89468b8534bSLuigi Rizzo  * - NIOCGINFO
89568b8534bSLuigi Rizzo  * - SIOCGIFADDR	just for convenience
89668b8534bSLuigi Rizzo  * - NIOCREGIF
89768b8534bSLuigi Rizzo  * - NIOCUNREGIF
89868b8534bSLuigi Rizzo  * - NIOCTXSYNC
89968b8534bSLuigi Rizzo  * - NIOCRXSYNC
90068b8534bSLuigi Rizzo  *
90168b8534bSLuigi Rizzo  * Return 0 on success, errno otherwise.
90268b8534bSLuigi Rizzo  */
90368b8534bSLuigi Rizzo static int
9040b8ed8e0SLuigi Rizzo netmap_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
9050b8ed8e0SLuigi Rizzo 	int fflag, struct thread *td)
90668b8534bSLuigi Rizzo {
90768b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
90868b8534bSLuigi Rizzo 	struct ifnet *ifp;
90968b8534bSLuigi Rizzo 	struct nmreq *nmr = (struct nmreq *) data;
91068b8534bSLuigi Rizzo 	struct netmap_adapter *na;
91168b8534bSLuigi Rizzo 	int error;
91264ae02c3SLuigi Rizzo 	u_int i, lim;
91368b8534bSLuigi Rizzo 	struct netmap_if *nifp;
91468b8534bSLuigi Rizzo 
9150b8ed8e0SLuigi Rizzo 	(void)dev;	/* UNUSED */
9160b8ed8e0SLuigi Rizzo 	(void)fflag;	/* UNUSED */
917f196ce38SLuigi Rizzo #ifdef linux
918f196ce38SLuigi Rizzo #define devfs_get_cdevpriv(pp)				\
919f196ce38SLuigi Rizzo 	({ *(struct netmap_priv_d **)pp = ((struct file *)td)->private_data; 	\
920f196ce38SLuigi Rizzo 		(*pp ? 0 : ENOENT); })
921f196ce38SLuigi Rizzo 
922f196ce38SLuigi Rizzo /* devfs_set_cdevpriv cannot fail on linux */
923f196ce38SLuigi Rizzo #define devfs_set_cdevpriv(p, fn)				\
924f196ce38SLuigi Rizzo 	({ ((struct file *)td)->private_data = p; (p ? 0 : EINVAL); })
925f196ce38SLuigi Rizzo 
926f196ce38SLuigi Rizzo 
927f196ce38SLuigi Rizzo #define devfs_clear_cdevpriv()	do {				\
928f196ce38SLuigi Rizzo 		netmap_dtor(priv); ((struct file *)td)->private_data = 0;	\
929f196ce38SLuigi Rizzo 	} while (0)
930f196ce38SLuigi Rizzo #endif /* linux */
931f196ce38SLuigi Rizzo 
932506cc70cSLuigi Rizzo 	CURVNET_SET(TD_TO_VNET(td));
933506cc70cSLuigi Rizzo 
93468b8534bSLuigi Rizzo 	error = devfs_get_cdevpriv((void **)&priv);
935*8241616dSLuigi Rizzo 	if (error) {
936506cc70cSLuigi Rizzo 		CURVNET_RESTORE();
937*8241616dSLuigi Rizzo 		/* XXX ENOENT should be impossible, since the priv
938*8241616dSLuigi Rizzo 		 * is now created in the open */
939*8241616dSLuigi Rizzo 		return (error == ENOENT ? ENXIO : error);
940506cc70cSLuigi Rizzo 	}
94168b8534bSLuigi Rizzo 
942f196ce38SLuigi Rizzo 	nmr->nr_name[sizeof(nmr->nr_name) - 1] = '\0';	/* truncate name */
94368b8534bSLuigi Rizzo 	switch (cmd) {
94468b8534bSLuigi Rizzo 	case NIOCGINFO:		/* return capabilities etc */
94564ae02c3SLuigi Rizzo 		if (nmr->nr_version != NETMAP_API) {
94664ae02c3SLuigi Rizzo 			D("API mismatch got %d have %d",
94764ae02c3SLuigi Rizzo 				nmr->nr_version, NETMAP_API);
94864ae02c3SLuigi Rizzo 			nmr->nr_version = NETMAP_API;
94964ae02c3SLuigi Rizzo 			error = EINVAL;
95064ae02c3SLuigi Rizzo 			break;
95164ae02c3SLuigi Rizzo 		}
952*8241616dSLuigi Rizzo 		/* update configuration */
953*8241616dSLuigi Rizzo 		error = netmap_get_memory(priv);
954*8241616dSLuigi Rizzo 		ND("get_memory returned %d", error);
955*8241616dSLuigi Rizzo 		if (error)
956*8241616dSLuigi Rizzo 			break;
957*8241616dSLuigi Rizzo 		/* memsize is always valid */
958*8241616dSLuigi Rizzo 		nmr->nr_memsize = nm_mem.nm_totalsize;
959*8241616dSLuigi Rizzo 		nmr->nr_offset = 0;
960*8241616dSLuigi Rizzo 		nmr->nr_rx_rings = nmr->nr_tx_rings = 0;
961*8241616dSLuigi Rizzo 		nmr->nr_rx_slots = nmr->nr_tx_slots = 0;
96268b8534bSLuigi Rizzo 		if (nmr->nr_name[0] == '\0')	/* just get memory info */
96368b8534bSLuigi Rizzo 			break;
96468b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* get a refcount */
96568b8534bSLuigi Rizzo 		if (error)
96668b8534bSLuigi Rizzo 			break;
96768b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap_adapter */
968d76bf4ffSLuigi Rizzo 		nmr->nr_rx_rings = na->num_rx_rings;
969d76bf4ffSLuigi Rizzo 		nmr->nr_tx_rings = na->num_tx_rings;
97064ae02c3SLuigi Rizzo 		nmr->nr_rx_slots = na->num_rx_desc;
97164ae02c3SLuigi Rizzo 		nmr->nr_tx_slots = na->num_tx_desc;
972f196ce38SLuigi Rizzo 		nm_if_rele(ifp);	/* return the refcount */
97368b8534bSLuigi Rizzo 		break;
97468b8534bSLuigi Rizzo 
97568b8534bSLuigi Rizzo 	case NIOCREGIF:
97664ae02c3SLuigi Rizzo 		if (nmr->nr_version != NETMAP_API) {
97764ae02c3SLuigi Rizzo 			nmr->nr_version = NETMAP_API;
97864ae02c3SLuigi Rizzo 			error = EINVAL;
97964ae02c3SLuigi Rizzo 			break;
98064ae02c3SLuigi Rizzo 		}
981*8241616dSLuigi Rizzo 		/* ensure allocators are ready */
982*8241616dSLuigi Rizzo 		error = netmap_get_memory(priv);
983*8241616dSLuigi Rizzo 		ND("get_memory returned %d", error);
984*8241616dSLuigi Rizzo 		if (error)
985*8241616dSLuigi Rizzo 			break;
986*8241616dSLuigi Rizzo 
987*8241616dSLuigi Rizzo 		/* protect access to priv from concurrent NIOCREGIF */
988*8241616dSLuigi Rizzo 		NMA_LOCK();
989*8241616dSLuigi Rizzo 		if (priv->np_ifp != NULL) {	/* thread already registered */
990506cc70cSLuigi Rizzo 			error = netmap_set_ringid(priv, nmr->nr_ringid);
991*8241616dSLuigi Rizzo 			NMA_UNLOCK();
992506cc70cSLuigi Rizzo 			break;
993506cc70cSLuigi Rizzo 		}
99468b8534bSLuigi Rizzo 		/* find the interface and a reference */
99568b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* keep reference */
996*8241616dSLuigi Rizzo 		if (error) {
997*8241616dSLuigi Rizzo 			NMA_UNLOCK();
99868b8534bSLuigi Rizzo 			break;
99968b8534bSLuigi Rizzo 		}
1000*8241616dSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap adapter */
100168b8534bSLuigi Rizzo 
100268b8534bSLuigi Rizzo 		for (i = 10; i > 0; i--) {
10031a26580eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
100468b8534bSLuigi Rizzo 			if (!NETMAP_DELETING(na))
100568b8534bSLuigi Rizzo 				break;
10061a26580eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
100768b8534bSLuigi Rizzo 			tsleep(na, 0, "NIOCREGIF", hz/10);
100868b8534bSLuigi Rizzo 		}
100968b8534bSLuigi Rizzo 		if (i == 0) {
101068b8534bSLuigi Rizzo 			D("too many NIOCREGIF attempts, give up");
101168b8534bSLuigi Rizzo 			error = EINVAL;
1012f196ce38SLuigi Rizzo 			nm_if_rele(ifp);	/* return the refcount */
1013*8241616dSLuigi Rizzo 			NMA_UNLOCK();
101468b8534bSLuigi Rizzo 			break;
101568b8534bSLuigi Rizzo 		}
101668b8534bSLuigi Rizzo 
101768b8534bSLuigi Rizzo 		priv->np_ifp = ifp;	/* store the reference */
101868b8534bSLuigi Rizzo 		error = netmap_set_ringid(priv, nmr->nr_ringid);
101968b8534bSLuigi Rizzo 		if (error)
102068b8534bSLuigi Rizzo 			goto error;
1021*8241616dSLuigi Rizzo 		nifp = netmap_if_new(nmr->nr_name, na);
102268b8534bSLuigi Rizzo 		if (nifp == NULL) { /* allocation failed */
102368b8534bSLuigi Rizzo 			error = ENOMEM;
102468b8534bSLuigi Rizzo 		} else if (ifp->if_capenable & IFCAP_NETMAP) {
102568b8534bSLuigi Rizzo 			/* was already set */
102668b8534bSLuigi Rizzo 		} else {
102768b8534bSLuigi Rizzo 			/* Otherwise set the card in netmap mode
102868b8534bSLuigi Rizzo 			 * and make it use the shared buffers.
102968b8534bSLuigi Rizzo 			 */
1030f196ce38SLuigi Rizzo 			for (i = 0 ; i < na->num_tx_rings + 1; i++)
1031f196ce38SLuigi Rizzo 				mtx_init(&na->tx_rings[i].q_lock, "nm_txq_lock", MTX_NETWORK_LOCK, MTX_DEF);
1032f196ce38SLuigi Rizzo 			for (i = 0 ; i < na->num_rx_rings + 1; i++) {
1033f196ce38SLuigi Rizzo 				mtx_init(&na->rx_rings[i].q_lock, "nm_rxq_lock", MTX_NETWORK_LOCK, MTX_DEF);
1034f196ce38SLuigi Rizzo 			}
103568b8534bSLuigi Rizzo 			error = na->nm_register(ifp, 1); /* mode on */
1036*8241616dSLuigi Rizzo 			if (error) {
10375819da83SLuigi Rizzo 				netmap_dtor_locked(priv);
1038*8241616dSLuigi Rizzo 				netmap_if_free(nifp);
1039*8241616dSLuigi Rizzo 			}
104068b8534bSLuigi Rizzo 		}
104168b8534bSLuigi Rizzo 
104268b8534bSLuigi Rizzo 		if (error) {	/* reg. failed, release priv and ref */
104368b8534bSLuigi Rizzo error:
10441a26580eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
1045f196ce38SLuigi Rizzo 			nm_if_rele(ifp);	/* return the refcount */
1046*8241616dSLuigi Rizzo 			priv->np_ifp = NULL;
1047*8241616dSLuigi Rizzo 			priv->np_nifp = NULL;
1048*8241616dSLuigi Rizzo 			NMA_UNLOCK();
104968b8534bSLuigi Rizzo 			break;
105068b8534bSLuigi Rizzo 		}
105168b8534bSLuigi Rizzo 
10521a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
105368b8534bSLuigi Rizzo 
1054*8241616dSLuigi Rizzo 		/* the following assignment is a commitment.
1055*8241616dSLuigi Rizzo 		 * Readers (i.e., poll and *SYNC) check for
1056*8241616dSLuigi Rizzo 		 * np_nifp != NULL without locking
105768b8534bSLuigi Rizzo 		 */
1058*8241616dSLuigi Rizzo 		wmb(); /* make sure previous writes are visible to all CPUs */
1059*8241616dSLuigi Rizzo 		priv->np_nifp = nifp;
1060*8241616dSLuigi Rizzo 		NMA_UNLOCK();
106168b8534bSLuigi Rizzo 
106268b8534bSLuigi Rizzo 		/* return the offset of the netmap_if object */
1063d76bf4ffSLuigi Rizzo 		nmr->nr_rx_rings = na->num_rx_rings;
1064d76bf4ffSLuigi Rizzo 		nmr->nr_tx_rings = na->num_tx_rings;
106564ae02c3SLuigi Rizzo 		nmr->nr_rx_slots = na->num_rx_desc;
106664ae02c3SLuigi Rizzo 		nmr->nr_tx_slots = na->num_tx_desc;
1067*8241616dSLuigi Rizzo 		nmr->nr_memsize = nm_mem.nm_totalsize;
1068446ee301SLuigi Rizzo 		nmr->nr_offset = netmap_if_offset(nifp);
106968b8534bSLuigi Rizzo 		break;
107068b8534bSLuigi Rizzo 
107168b8534bSLuigi Rizzo 	case NIOCUNREGIF:
1072*8241616dSLuigi Rizzo 		// XXX we have no data here ?
1073*8241616dSLuigi Rizzo 		D("deprecated, data is %p", nmr);
1074*8241616dSLuigi Rizzo 		error = EINVAL;
107568b8534bSLuigi Rizzo 		break;
107668b8534bSLuigi Rizzo 
107768b8534bSLuigi Rizzo 	case NIOCTXSYNC:
107868b8534bSLuigi Rizzo 	case NIOCRXSYNC:
1079*8241616dSLuigi Rizzo 		nifp = priv->np_nifp;
1080*8241616dSLuigi Rizzo 
1081*8241616dSLuigi Rizzo 		if (nifp == NULL) {
1082506cc70cSLuigi Rizzo 			error = ENXIO;
1083506cc70cSLuigi Rizzo 			break;
1084506cc70cSLuigi Rizzo 		}
1085*8241616dSLuigi Rizzo 		rmb(); /* make sure following reads are not from cache */
1086*8241616dSLuigi Rizzo 
1087*8241616dSLuigi Rizzo 
108868b8534bSLuigi Rizzo 		ifp = priv->np_ifp;	/* we have a reference */
1089*8241616dSLuigi Rizzo 
1090*8241616dSLuigi Rizzo 		if (ifp == NULL) {
1091*8241616dSLuigi Rizzo 			D("Internal error: nifp != NULL && ifp == NULL");
1092*8241616dSLuigi Rizzo 			error = ENXIO;
1093*8241616dSLuigi Rizzo 			break;
1094*8241616dSLuigi Rizzo 		}
1095*8241616dSLuigi Rizzo 
109668b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap adapter */
109764ae02c3SLuigi Rizzo 		if (priv->np_qfirst == NETMAP_SW_RING) { /* host rings */
109868b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC)
109968b8534bSLuigi Rizzo 				netmap_sync_to_host(na);
110068b8534bSLuigi Rizzo 			else
110101c7d25fSLuigi Rizzo 				netmap_sync_from_host(na, NULL, NULL);
1102506cc70cSLuigi Rizzo 			break;
110368b8534bSLuigi Rizzo 		}
110464ae02c3SLuigi Rizzo 		/* find the last ring to scan */
110564ae02c3SLuigi Rizzo 		lim = priv->np_qlast;
110664ae02c3SLuigi Rizzo 		if (lim == NETMAP_HW_RING)
11073c0caf6cSLuigi Rizzo 			lim = (cmd == NIOCTXSYNC) ?
1108d76bf4ffSLuigi Rizzo 			    na->num_tx_rings : na->num_rx_rings;
110968b8534bSLuigi Rizzo 
111064ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim; i++) {
111168b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC) {
111268b8534bSLuigi Rizzo 				struct netmap_kring *kring = &na->tx_rings[i];
111368b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
11143c0caf6cSLuigi Rizzo 					D("pre txsync ring %d cur %d hwcur %d",
111568b8534bSLuigi Rizzo 					    i, kring->ring->cur,
111668b8534bSLuigi Rizzo 					    kring->nr_hwcur);
11171a26580eSLuigi Rizzo 				na->nm_txsync(ifp, i, 1 /* do lock */);
111868b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
11193c0caf6cSLuigi Rizzo 					D("post txsync ring %d cur %d hwcur %d",
112068b8534bSLuigi Rizzo 					    i, kring->ring->cur,
112168b8534bSLuigi Rizzo 					    kring->nr_hwcur);
112268b8534bSLuigi Rizzo 			} else {
11231a26580eSLuigi Rizzo 				na->nm_rxsync(ifp, i, 1 /* do lock */);
112468b8534bSLuigi Rizzo 				microtime(&na->rx_rings[i].ring->ts);
112568b8534bSLuigi Rizzo 			}
112668b8534bSLuigi Rizzo 		}
112768b8534bSLuigi Rizzo 
112868b8534bSLuigi Rizzo 		break;
112968b8534bSLuigi Rizzo 
1130f196ce38SLuigi Rizzo #ifdef __FreeBSD__
113168b8534bSLuigi Rizzo 	case BIOCIMMEDIATE:
113268b8534bSLuigi Rizzo 	case BIOCGHDRCMPLT:
113368b8534bSLuigi Rizzo 	case BIOCSHDRCMPLT:
113468b8534bSLuigi Rizzo 	case BIOCSSEESENT:
113568b8534bSLuigi Rizzo 		D("ignore BIOCIMMEDIATE/BIOCSHDRCMPLT/BIOCSHDRCMPLT/BIOCSSEESENT");
113668b8534bSLuigi Rizzo 		break;
113768b8534bSLuigi Rizzo 
1138babc7c12SLuigi Rizzo 	default:	/* allow device-specific ioctls */
113968b8534bSLuigi Rizzo 	    {
114068b8534bSLuigi Rizzo 		struct socket so;
114168b8534bSLuigi Rizzo 		bzero(&so, sizeof(so));
114268b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* keep reference */
114368b8534bSLuigi Rizzo 		if (error)
114468b8534bSLuigi Rizzo 			break;
114568b8534bSLuigi Rizzo 		so.so_vnet = ifp->if_vnet;
114668b8534bSLuigi Rizzo 		// so->so_proto not null.
114768b8534bSLuigi Rizzo 		error = ifioctl(&so, cmd, data, td);
1148f196ce38SLuigi Rizzo 		nm_if_rele(ifp);
1149babc7c12SLuigi Rizzo 		break;
115068b8534bSLuigi Rizzo 	    }
1151f196ce38SLuigi Rizzo 
1152f196ce38SLuigi Rizzo #else /* linux */
1153f196ce38SLuigi Rizzo 	default:
1154f196ce38SLuigi Rizzo 		error = EOPNOTSUPP;
1155f196ce38SLuigi Rizzo #endif /* linux */
115668b8534bSLuigi Rizzo 	}
115768b8534bSLuigi Rizzo 
1158506cc70cSLuigi Rizzo 	CURVNET_RESTORE();
115968b8534bSLuigi Rizzo 	return (error);
116068b8534bSLuigi Rizzo }
116168b8534bSLuigi Rizzo 
116268b8534bSLuigi Rizzo 
116368b8534bSLuigi Rizzo /*
116468b8534bSLuigi Rizzo  * select(2) and poll(2) handlers for the "netmap" device.
116568b8534bSLuigi Rizzo  *
116668b8534bSLuigi Rizzo  * Can be called for one or more queues.
116768b8534bSLuigi Rizzo  * Return true the event mask corresponding to ready events.
116868b8534bSLuigi Rizzo  * If there are no ready events, do a selrecord on either individual
116968b8534bSLuigi Rizzo  * selfd or on the global one.
117068b8534bSLuigi Rizzo  * Device-dependent parts (locking and sync of tx/rx rings)
117168b8534bSLuigi Rizzo  * are done through callbacks.
1172f196ce38SLuigi Rizzo  *
117301c7d25fSLuigi Rizzo  * On linux, arguments are really pwait, the poll table, and 'td' is struct file *
117401c7d25fSLuigi Rizzo  * The first one is remapped to pwait as selrecord() uses the name as an
117501c7d25fSLuigi Rizzo  * hidden argument.
117668b8534bSLuigi Rizzo  */
117768b8534bSLuigi Rizzo static int
117801c7d25fSLuigi Rizzo netmap_poll(struct cdev *dev, int events, struct thread *td)
117968b8534bSLuigi Rizzo {
118068b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
118168b8534bSLuigi Rizzo 	struct netmap_adapter *na;
118268b8534bSLuigi Rizzo 	struct ifnet *ifp;
118368b8534bSLuigi Rizzo 	struct netmap_kring *kring;
1184d0c7b075SLuigi Rizzo 	u_int core_lock, i, check_all, want_tx, want_rx, revents = 0;
118564ae02c3SLuigi Rizzo 	u_int lim_tx, lim_rx;
1186bcda432eSLuigi Rizzo 	enum {NO_CL, NEED_CL, LOCKED_CL }; /* see below */
118701c7d25fSLuigi Rizzo 	void *pwait = dev;	/* linux compatibility */
118801c7d25fSLuigi Rizzo 
118901c7d25fSLuigi Rizzo 	(void)pwait;
119068b8534bSLuigi Rizzo 
119168b8534bSLuigi Rizzo 	if (devfs_get_cdevpriv((void **)&priv) != 0 || priv == NULL)
119268b8534bSLuigi Rizzo 		return POLLERR;
119368b8534bSLuigi Rizzo 
1194*8241616dSLuigi Rizzo 	if (priv->np_nifp == NULL) {
1195*8241616dSLuigi Rizzo 		D("No if registered");
1196*8241616dSLuigi Rizzo 		return POLLERR;
1197*8241616dSLuigi Rizzo 	}
1198*8241616dSLuigi Rizzo 	rmb(); /* make sure following reads are not from cache */
1199*8241616dSLuigi Rizzo 
120068b8534bSLuigi Rizzo 	ifp = priv->np_ifp;
120168b8534bSLuigi Rizzo 	// XXX check for deleting() ?
120268b8534bSLuigi Rizzo 	if ( (ifp->if_capenable & IFCAP_NETMAP) == 0)
120368b8534bSLuigi Rizzo 		return POLLERR;
120468b8534bSLuigi Rizzo 
120568b8534bSLuigi Rizzo 	if (netmap_verbose & 0x8000)
120668b8534bSLuigi Rizzo 		D("device %s events 0x%x", ifp->if_xname, events);
120768b8534bSLuigi Rizzo 	want_tx = events & (POLLOUT | POLLWRNORM);
120868b8534bSLuigi Rizzo 	want_rx = events & (POLLIN | POLLRDNORM);
120968b8534bSLuigi Rizzo 
121068b8534bSLuigi Rizzo 	na = NA(ifp); /* retrieve netmap adapter */
121168b8534bSLuigi Rizzo 
1212d76bf4ffSLuigi Rizzo 	lim_tx = na->num_tx_rings;
1213d76bf4ffSLuigi Rizzo 	lim_rx = na->num_rx_rings;
121468b8534bSLuigi Rizzo 	/* how many queues we are scanning */
121564ae02c3SLuigi Rizzo 	if (priv->np_qfirst == NETMAP_SW_RING) {
121668b8534bSLuigi Rizzo 		if (priv->np_txpoll || want_tx) {
121768b8534bSLuigi Rizzo 			/* push any packets up, then we are always ready */
121864ae02c3SLuigi Rizzo 			kring = &na->tx_rings[lim_tx];
121968b8534bSLuigi Rizzo 			netmap_sync_to_host(na);
122068b8534bSLuigi Rizzo 			revents |= want_tx;
122168b8534bSLuigi Rizzo 		}
122268b8534bSLuigi Rizzo 		if (want_rx) {
122364ae02c3SLuigi Rizzo 			kring = &na->rx_rings[lim_rx];
122468b8534bSLuigi Rizzo 			if (kring->ring->avail == 0)
122501c7d25fSLuigi Rizzo 				netmap_sync_from_host(na, td, dev);
122668b8534bSLuigi Rizzo 			if (kring->ring->avail > 0) {
122768b8534bSLuigi Rizzo 				revents |= want_rx;
122868b8534bSLuigi Rizzo 			}
122968b8534bSLuigi Rizzo 		}
123068b8534bSLuigi Rizzo 		return (revents);
123168b8534bSLuigi Rizzo 	}
123268b8534bSLuigi Rizzo 
123368b8534bSLuigi Rizzo 	/*
123468b8534bSLuigi Rizzo 	 * check_all is set if the card has more than one queue and
123568b8534bSLuigi Rizzo 	 * the client is polling all of them. If true, we sleep on
123668b8534bSLuigi Rizzo 	 * the "global" selfd, otherwise we sleep on individual selfd
123768b8534bSLuigi Rizzo 	 * (we can only sleep on one of them per direction).
123868b8534bSLuigi Rizzo 	 * The interrupt routine in the driver should always wake on
123968b8534bSLuigi Rizzo 	 * the individual selfd, and also on the global one if the card
124068b8534bSLuigi Rizzo 	 * has more than one ring.
124168b8534bSLuigi Rizzo 	 *
124268b8534bSLuigi Rizzo 	 * If the card has only one lock, we just use that.
124368b8534bSLuigi Rizzo 	 * If the card has separate ring locks, we just use those
124468b8534bSLuigi Rizzo 	 * unless we are doing check_all, in which case the whole
124568b8534bSLuigi Rizzo 	 * loop is wrapped by the global lock.
124668b8534bSLuigi Rizzo 	 * We acquire locks only when necessary: if poll is called
124768b8534bSLuigi Rizzo 	 * when buffers are available, we can just return without locks.
124868b8534bSLuigi Rizzo 	 *
124968b8534bSLuigi Rizzo 	 * rxsync() is only called if we run out of buffers on a POLLIN.
125068b8534bSLuigi Rizzo 	 * txsync() is called if we run out of buffers on POLLOUT, or
125168b8534bSLuigi Rizzo 	 * there are pending packets to send. The latter can be disabled
125268b8534bSLuigi Rizzo 	 * passing NETMAP_NO_TX_POLL in the NIOCREG call.
125368b8534bSLuigi Rizzo 	 */
125464ae02c3SLuigi Rizzo 	check_all = (priv->np_qlast == NETMAP_HW_RING) && (lim_tx > 1 || lim_rx > 1);
125568b8534bSLuigi Rizzo 
125668b8534bSLuigi Rizzo 	/*
125768b8534bSLuigi Rizzo 	 * core_lock indicates what to do with the core lock.
125868b8534bSLuigi Rizzo 	 * The core lock is used when either the card has no individual
125968b8534bSLuigi Rizzo 	 * locks, or it has individual locks but we are cheking all
126068b8534bSLuigi Rizzo 	 * rings so we need the core lock to avoid missing wakeup events.
126168b8534bSLuigi Rizzo 	 *
126268b8534bSLuigi Rizzo 	 * It has three possible states:
126368b8534bSLuigi Rizzo 	 * NO_CL	we don't need to use the core lock, e.g.
126468b8534bSLuigi Rizzo 	 *		because we are protected by individual locks.
126568b8534bSLuigi Rizzo 	 * NEED_CL	we need the core lock. In this case, when we
126668b8534bSLuigi Rizzo 	 *		call the lock routine, move to LOCKED_CL
126768b8534bSLuigi Rizzo 	 *		to remember to release the lock once done.
126868b8534bSLuigi Rizzo 	 * LOCKED_CL	core lock is set, so we need to release it.
126968b8534bSLuigi Rizzo 	 */
1270d0c7b075SLuigi Rizzo 	core_lock = (check_all || !na->separate_locks) ? NEED_CL : NO_CL;
1271f196ce38SLuigi Rizzo #ifdef NM_BRIDGE
1272f196ce38SLuigi Rizzo 	/* the bridge uses separate locks */
1273f196ce38SLuigi Rizzo 	if (na->nm_register == bdg_netmap_reg) {
1274f196ce38SLuigi Rizzo 		ND("not using core lock for %s", ifp->if_xname);
1275f196ce38SLuigi Rizzo 		core_lock = NO_CL;
1276f196ce38SLuigi Rizzo 	}
1277f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */
127864ae02c3SLuigi Rizzo 	if (priv->np_qlast != NETMAP_HW_RING) {
127964ae02c3SLuigi Rizzo 		lim_tx = lim_rx = priv->np_qlast;
128064ae02c3SLuigi Rizzo 	}
128164ae02c3SLuigi Rizzo 
128268b8534bSLuigi Rizzo 	/*
128368b8534bSLuigi Rizzo 	 * We start with a lock free round which is good if we have
128468b8534bSLuigi Rizzo 	 * data available. If this fails, then lock and call the sync
128568b8534bSLuigi Rizzo 	 * routines.
128668b8534bSLuigi Rizzo 	 */
128764ae02c3SLuigi Rizzo 	for (i = priv->np_qfirst; want_rx && i < lim_rx; i++) {
128868b8534bSLuigi Rizzo 		kring = &na->rx_rings[i];
128968b8534bSLuigi Rizzo 		if (kring->ring->avail > 0) {
129068b8534bSLuigi Rizzo 			revents |= want_rx;
129168b8534bSLuigi Rizzo 			want_rx = 0;	/* also breaks the loop */
129268b8534bSLuigi Rizzo 		}
129368b8534bSLuigi Rizzo 	}
129464ae02c3SLuigi Rizzo 	for (i = priv->np_qfirst; want_tx && i < lim_tx; i++) {
129568b8534bSLuigi Rizzo 		kring = &na->tx_rings[i];
129668b8534bSLuigi Rizzo 		if (kring->ring->avail > 0) {
129768b8534bSLuigi Rizzo 			revents |= want_tx;
129868b8534bSLuigi Rizzo 			want_tx = 0;	/* also breaks the loop */
129968b8534bSLuigi Rizzo 		}
130068b8534bSLuigi Rizzo 	}
130168b8534bSLuigi Rizzo 
130268b8534bSLuigi Rizzo 	/*
130368b8534bSLuigi Rizzo 	 * If we to push packets out (priv->np_txpoll) or want_tx is
130468b8534bSLuigi Rizzo 	 * still set, we do need to run the txsync calls (on all rings,
130568b8534bSLuigi Rizzo 	 * to avoid that the tx rings stall).
130668b8534bSLuigi Rizzo 	 */
130768b8534bSLuigi Rizzo 	if (priv->np_txpoll || want_tx) {
130864ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim_tx; i++) {
130968b8534bSLuigi Rizzo 			kring = &na->tx_rings[i];
13105819da83SLuigi Rizzo 			/*
13115819da83SLuigi Rizzo 			 * Skip the current ring if want_tx == 0
13125819da83SLuigi Rizzo 			 * (we have already done a successful sync on
13135819da83SLuigi Rizzo 			 * a previous ring) AND kring->cur == kring->hwcur
13145819da83SLuigi Rizzo 			 * (there are no pending transmissions for this ring).
13155819da83SLuigi Rizzo 			 */
131668b8534bSLuigi Rizzo 			if (!want_tx && kring->ring->cur == kring->nr_hwcur)
131768b8534bSLuigi Rizzo 				continue;
131868b8534bSLuigi Rizzo 			if (core_lock == NEED_CL) {
13191a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
132068b8534bSLuigi Rizzo 				core_lock = LOCKED_CL;
132168b8534bSLuigi Rizzo 			}
132268b8534bSLuigi Rizzo 			if (na->separate_locks)
13231a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_TX_LOCK, i);
132468b8534bSLuigi Rizzo 			if (netmap_verbose & NM_VERB_TXSYNC)
132568b8534bSLuigi Rizzo 				D("send %d on %s %d",
132668b8534bSLuigi Rizzo 					kring->ring->cur,
132768b8534bSLuigi Rizzo 					ifp->if_xname, i);
13281a26580eSLuigi Rizzo 			if (na->nm_txsync(ifp, i, 0 /* no lock */))
132968b8534bSLuigi Rizzo 				revents |= POLLERR;
133068b8534bSLuigi Rizzo 
13315819da83SLuigi Rizzo 			/* Check avail/call selrecord only if called with POLLOUT */
133268b8534bSLuigi Rizzo 			if (want_tx) {
133368b8534bSLuigi Rizzo 				if (kring->ring->avail > 0) {
133468b8534bSLuigi Rizzo 					/* stop at the first ring. We don't risk
133568b8534bSLuigi Rizzo 					 * starvation.
133668b8534bSLuigi Rizzo 					 */
133768b8534bSLuigi Rizzo 					revents |= want_tx;
133868b8534bSLuigi Rizzo 					want_tx = 0;
133968b8534bSLuigi Rizzo 				} else if (!check_all)
134068b8534bSLuigi Rizzo 					selrecord(td, &kring->si);
134168b8534bSLuigi Rizzo 			}
134268b8534bSLuigi Rizzo 			if (na->separate_locks)
13431a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_TX_UNLOCK, i);
134468b8534bSLuigi Rizzo 		}
134568b8534bSLuigi Rizzo 	}
134668b8534bSLuigi Rizzo 
134768b8534bSLuigi Rizzo 	/*
134868b8534bSLuigi Rizzo 	 * now if want_rx is still set we need to lock and rxsync.
134968b8534bSLuigi Rizzo 	 * Do it on all rings because otherwise we starve.
135068b8534bSLuigi Rizzo 	 */
135168b8534bSLuigi Rizzo 	if (want_rx) {
135264ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim_rx; i++) {
135368b8534bSLuigi Rizzo 			kring = &na->rx_rings[i];
135468b8534bSLuigi Rizzo 			if (core_lock == NEED_CL) {
13551a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
135668b8534bSLuigi Rizzo 				core_lock = LOCKED_CL;
135768b8534bSLuigi Rizzo 			}
135868b8534bSLuigi Rizzo 			if (na->separate_locks)
13591a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_RX_LOCK, i);
136068b8534bSLuigi Rizzo 
13611a26580eSLuigi Rizzo 			if (na->nm_rxsync(ifp, i, 0 /* no lock */))
136268b8534bSLuigi Rizzo 				revents |= POLLERR;
13635819da83SLuigi Rizzo 			if (netmap_no_timestamp == 0 ||
13645819da83SLuigi Rizzo 					kring->ring->flags & NR_TIMESTAMP) {
136568b8534bSLuigi Rizzo 				microtime(&kring->ring->ts);
13665819da83SLuigi Rizzo 			}
136768b8534bSLuigi Rizzo 
136868b8534bSLuigi Rizzo 			if (kring->ring->avail > 0)
136968b8534bSLuigi Rizzo 				revents |= want_rx;
137068b8534bSLuigi Rizzo 			else if (!check_all)
137168b8534bSLuigi Rizzo 				selrecord(td, &kring->si);
137268b8534bSLuigi Rizzo 			if (na->separate_locks)
13731a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_RX_UNLOCK, i);
137468b8534bSLuigi Rizzo 		}
137568b8534bSLuigi Rizzo 	}
137664ae02c3SLuigi Rizzo 	if (check_all && revents == 0) { /* signal on the global queue */
137768b8534bSLuigi Rizzo 		if (want_tx)
137864ae02c3SLuigi Rizzo 			selrecord(td, &na->tx_si);
137968b8534bSLuigi Rizzo 		if (want_rx)
138064ae02c3SLuigi Rizzo 			selrecord(td, &na->rx_si);
138168b8534bSLuigi Rizzo 	}
138268b8534bSLuigi Rizzo 	if (core_lock == LOCKED_CL)
13831a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
138468b8534bSLuigi Rizzo 
138568b8534bSLuigi Rizzo 	return (revents);
138668b8534bSLuigi Rizzo }
138768b8534bSLuigi Rizzo 
138868b8534bSLuigi Rizzo /*------- driver support routines ------*/
138968b8534bSLuigi Rizzo 
139068b8534bSLuigi Rizzo /*
1391babc7c12SLuigi Rizzo  * default lock wrapper.
13921a26580eSLuigi Rizzo  */
13931a26580eSLuigi Rizzo static void
1394babc7c12SLuigi Rizzo netmap_lock_wrapper(struct ifnet *dev, int what, u_int queueid)
13951a26580eSLuigi Rizzo {
1396babc7c12SLuigi Rizzo 	struct netmap_adapter *na = NA(dev);
13971a26580eSLuigi Rizzo 
13981a26580eSLuigi Rizzo 	switch (what) {
1399babc7c12SLuigi Rizzo #ifdef linux	/* some system do not need lock on register */
14001a26580eSLuigi Rizzo 	case NETMAP_REG_LOCK:
14011a26580eSLuigi Rizzo 	case NETMAP_REG_UNLOCK:
14021a26580eSLuigi Rizzo 		break;
1403babc7c12SLuigi Rizzo #endif /* linux */
14041a26580eSLuigi Rizzo 
14051a26580eSLuigi Rizzo 	case NETMAP_CORE_LOCK:
14061a26580eSLuigi Rizzo 		mtx_lock(&na->core_lock);
14071a26580eSLuigi Rizzo 		break;
14081a26580eSLuigi Rizzo 
14091a26580eSLuigi Rizzo 	case NETMAP_CORE_UNLOCK:
14101a26580eSLuigi Rizzo 		mtx_unlock(&na->core_lock);
14111a26580eSLuigi Rizzo 		break;
14121a26580eSLuigi Rizzo 
14131a26580eSLuigi Rizzo 	case NETMAP_TX_LOCK:
14141a26580eSLuigi Rizzo 		mtx_lock(&na->tx_rings[queueid].q_lock);
14151a26580eSLuigi Rizzo 		break;
14161a26580eSLuigi Rizzo 
14171a26580eSLuigi Rizzo 	case NETMAP_TX_UNLOCK:
14181a26580eSLuigi Rizzo 		mtx_unlock(&na->tx_rings[queueid].q_lock);
14191a26580eSLuigi Rizzo 		break;
14201a26580eSLuigi Rizzo 
14211a26580eSLuigi Rizzo 	case NETMAP_RX_LOCK:
14221a26580eSLuigi Rizzo 		mtx_lock(&na->rx_rings[queueid].q_lock);
14231a26580eSLuigi Rizzo 		break;
14241a26580eSLuigi Rizzo 
14251a26580eSLuigi Rizzo 	case NETMAP_RX_UNLOCK:
14261a26580eSLuigi Rizzo 		mtx_unlock(&na->rx_rings[queueid].q_lock);
14271a26580eSLuigi Rizzo 		break;
14281a26580eSLuigi Rizzo 	}
14291a26580eSLuigi Rizzo }
14301a26580eSLuigi Rizzo 
14311a26580eSLuigi Rizzo 
14321a26580eSLuigi Rizzo /*
143368b8534bSLuigi Rizzo  * Initialize a ``netmap_adapter`` object created by driver on attach.
143468b8534bSLuigi Rizzo  * We allocate a block of memory with room for a struct netmap_adapter
143568b8534bSLuigi Rizzo  * plus two sets of N+2 struct netmap_kring (where N is the number
143668b8534bSLuigi Rizzo  * of hardware rings):
143768b8534bSLuigi Rizzo  * krings	0..N-1	are for the hardware queues.
143868b8534bSLuigi Rizzo  * kring	N	is for the host stack queue
143968b8534bSLuigi Rizzo  * kring	N+1	is only used for the selinfo for all queues.
144068b8534bSLuigi Rizzo  * Return 0 on success, ENOMEM otherwise.
144164ae02c3SLuigi Rizzo  *
14420bf88954SEd Maste  * By default the receive and transmit adapter ring counts are both initialized
14430bf88954SEd Maste  * to num_queues.  na->num_tx_rings can be set for cards with different tx/rx
144424e57ec9SEd Maste  * setups.
144568b8534bSLuigi Rizzo  */
144668b8534bSLuigi Rizzo int
144768b8534bSLuigi Rizzo netmap_attach(struct netmap_adapter *na, int num_queues)
144868b8534bSLuigi Rizzo {
1449f196ce38SLuigi Rizzo 	int n, size;
145068b8534bSLuigi Rizzo 	void *buf;
145168b8534bSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
145268b8534bSLuigi Rizzo 
145368b8534bSLuigi Rizzo 	if (ifp == NULL) {
145468b8534bSLuigi Rizzo 		D("ifp not set, giving up");
145568b8534bSLuigi Rizzo 		return EINVAL;
145668b8534bSLuigi Rizzo 	}
145764ae02c3SLuigi Rizzo 	/* clear other fields ? */
145868b8534bSLuigi Rizzo 	na->refcount = 0;
1459d76bf4ffSLuigi Rizzo 	if (na->num_tx_rings == 0)
1460d76bf4ffSLuigi Rizzo 		na->num_tx_rings = num_queues;
1461d76bf4ffSLuigi Rizzo 	na->num_rx_rings = num_queues;
146264ae02c3SLuigi Rizzo 	/* on each direction we have N+1 resources
146364ae02c3SLuigi Rizzo 	 * 0..n-1	are the hardware rings
146464ae02c3SLuigi Rizzo 	 * n		is the ring attached to the stack.
146564ae02c3SLuigi Rizzo 	 */
1466d76bf4ffSLuigi Rizzo 	n = na->num_rx_rings + na->num_tx_rings + 2;
146764ae02c3SLuigi Rizzo 	size = sizeof(*na) + n * sizeof(struct netmap_kring);
146868b8534bSLuigi Rizzo 
146968b8534bSLuigi Rizzo 	buf = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
147068b8534bSLuigi Rizzo 	if (buf) {
1471d0c7b075SLuigi Rizzo 		WNA(ifp) = buf;
147268b8534bSLuigi Rizzo 		na->tx_rings = (void *)((char *)buf + sizeof(*na));
1473d76bf4ffSLuigi Rizzo 		na->rx_rings = na->tx_rings + na->num_tx_rings + 1;
147468b8534bSLuigi Rizzo 		bcopy(na, buf, sizeof(*na));
1475*8241616dSLuigi Rizzo 		NETMAP_SET_CAPABLE(ifp);
14761a26580eSLuigi Rizzo 
14771a26580eSLuigi Rizzo 		na = buf;
14782f70fca5SEd Maste 		/* Core lock initialized here.  Others are initialized after
14792f70fca5SEd Maste 		 * netmap_if_new.
14802f70fca5SEd Maste 		 */
14812f70fca5SEd Maste 		mtx_init(&na->core_lock, "netmap core lock", MTX_NETWORK_LOCK,
14822f70fca5SEd Maste 		    MTX_DEF);
1483f196ce38SLuigi Rizzo 		if (na->nm_lock == NULL) {
1484f196ce38SLuigi Rizzo 			ND("using default locks for %s", ifp->if_xname);
14851a26580eSLuigi Rizzo 			na->nm_lock = netmap_lock_wrapper;
1486f196ce38SLuigi Rizzo 		}
148768b8534bSLuigi Rizzo 	}
148864ae02c3SLuigi Rizzo #ifdef linux
1489f196ce38SLuigi Rizzo 	if (ifp->netdev_ops) {
1490*8241616dSLuigi Rizzo 		ND("netdev_ops %p", ifp->netdev_ops);
149164ae02c3SLuigi Rizzo 		/* prepare a clone of the netdev ops */
149264ae02c3SLuigi Rizzo 		na->nm_ndo = *ifp->netdev_ops;
1493f196ce38SLuigi Rizzo 	}
149442a3a5bdSLuigi Rizzo 	na->nm_ndo.ndo_start_xmit = linux_netmap_start;
149564ae02c3SLuigi Rizzo #endif
149668b8534bSLuigi Rizzo 	D("%s for %s", buf ? "ok" : "failed", ifp->if_xname);
149768b8534bSLuigi Rizzo 
149868b8534bSLuigi Rizzo 	return (buf ? 0 : ENOMEM);
149968b8534bSLuigi Rizzo }
150068b8534bSLuigi Rizzo 
150168b8534bSLuigi Rizzo 
150268b8534bSLuigi Rizzo /*
150368b8534bSLuigi Rizzo  * Free the allocated memory linked to the given ``netmap_adapter``
150468b8534bSLuigi Rizzo  * object.
150568b8534bSLuigi Rizzo  */
150668b8534bSLuigi Rizzo void
150768b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp)
150868b8534bSLuigi Rizzo {
150968b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
151068b8534bSLuigi Rizzo 
151168b8534bSLuigi Rizzo 	if (!na)
151268b8534bSLuigi Rizzo 		return;
151368b8534bSLuigi Rizzo 
15142f70fca5SEd Maste 	mtx_destroy(&na->core_lock);
15152f70fca5SEd Maste 
151668b8534bSLuigi Rizzo 	bzero(na, sizeof(*na));
1517d0c7b075SLuigi Rizzo 	WNA(ifp) = NULL;
151868b8534bSLuigi Rizzo 	free(na, M_DEVBUF);
151968b8534bSLuigi Rizzo }
152068b8534bSLuigi Rizzo 
152168b8534bSLuigi Rizzo 
152268b8534bSLuigi Rizzo /*
152302ad4083SLuigi Rizzo  * Intercept packets from the network stack and pass them
152402ad4083SLuigi Rizzo  * to netmap as incoming packets on the 'software' ring.
152568b8534bSLuigi Rizzo  * We are not locked when called.
152668b8534bSLuigi Rizzo  */
152768b8534bSLuigi Rizzo int
152868b8534bSLuigi Rizzo netmap_start(struct ifnet *ifp, struct mbuf *m)
152968b8534bSLuigi Rizzo {
153068b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
1531d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
15321a26580eSLuigi Rizzo 	u_int i, len = MBUF_LEN(m);
1533b3d53016SLuigi Rizzo 	u_int error = EBUSY, lim = kring->nkr_num_slots - 1;
153468b8534bSLuigi Rizzo 	struct netmap_slot *slot;
153568b8534bSLuigi Rizzo 
153668b8534bSLuigi Rizzo 	if (netmap_verbose & NM_VERB_HOST)
153768b8534bSLuigi Rizzo 		D("%s packet %d len %d from the stack", ifp->if_xname,
153868b8534bSLuigi Rizzo 			kring->nr_hwcur + kring->nr_hwavail, len);
15391a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
154002ad4083SLuigi Rizzo 	if (kring->nr_hwavail >= lim) {
15415b248374SLuigi Rizzo 		if (netmap_verbose)
154268b8534bSLuigi Rizzo 			D("stack ring %s full\n", ifp->if_xname);
154368b8534bSLuigi Rizzo 		goto done;	/* no space */
154468b8534bSLuigi Rizzo 	}
154564ae02c3SLuigi Rizzo 	if (len > NETMAP_BUF_SIZE) {
154664ae02c3SLuigi Rizzo 		D("drop packet size %d > %d", len, NETMAP_BUF_SIZE);
154768b8534bSLuigi Rizzo 		goto done;	/* too long for us */
154868b8534bSLuigi Rizzo 	}
154968b8534bSLuigi Rizzo 
155068b8534bSLuigi Rizzo 	/* compute the insert position */
155168b8534bSLuigi Rizzo 	i = kring->nr_hwcur + kring->nr_hwavail;
155202ad4083SLuigi Rizzo 	if (i > lim)
155302ad4083SLuigi Rizzo 		i -= lim + 1;
155468b8534bSLuigi Rizzo 	slot = &kring->ring->slot[i];
155568b8534bSLuigi Rizzo 	m_copydata(m, 0, len, NMB(slot));
155668b8534bSLuigi Rizzo 	slot->len = len;
155768b8534bSLuigi Rizzo 	kring->nr_hwavail++;
155868b8534bSLuigi Rizzo 	if (netmap_verbose  & NM_VERB_HOST)
1559d76bf4ffSLuigi Rizzo 		D("wake up host ring %s %d", na->ifp->if_xname, na->num_rx_rings);
156068b8534bSLuigi Rizzo 	selwakeuppri(&kring->si, PI_NET);
156168b8534bSLuigi Rizzo 	error = 0;
156268b8534bSLuigi Rizzo done:
15631a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
156468b8534bSLuigi Rizzo 
156568b8534bSLuigi Rizzo 	/* release the mbuf in either cases of success or failure. As an
156668b8534bSLuigi Rizzo 	 * alternative, put the mbuf in a free list and free the list
156768b8534bSLuigi Rizzo 	 * only when really necessary.
156868b8534bSLuigi Rizzo 	 */
156968b8534bSLuigi Rizzo 	m_freem(m);
157068b8534bSLuigi Rizzo 
157168b8534bSLuigi Rizzo 	return (error);
157268b8534bSLuigi Rizzo }
157368b8534bSLuigi Rizzo 
157468b8534bSLuigi Rizzo 
157568b8534bSLuigi Rizzo /*
157668b8534bSLuigi Rizzo  * netmap_reset() is called by the driver routines when reinitializing
157768b8534bSLuigi Rizzo  * a ring. The driver is in charge of locking to protect the kring.
157868b8534bSLuigi Rizzo  * If netmap mode is not set just return NULL.
157968b8534bSLuigi Rizzo  */
158068b8534bSLuigi Rizzo struct netmap_slot *
158168b8534bSLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, int n,
158268b8534bSLuigi Rizzo 	u_int new_cur)
158368b8534bSLuigi Rizzo {
158468b8534bSLuigi Rizzo 	struct netmap_kring *kring;
1585506cc70cSLuigi Rizzo 	int new_hwofs, lim;
158668b8534bSLuigi Rizzo 
158768b8534bSLuigi Rizzo 	if (na == NULL)
158868b8534bSLuigi Rizzo 		return NULL;	/* no netmap support here */
158968b8534bSLuigi Rizzo 	if (!(na->ifp->if_capenable & IFCAP_NETMAP))
159068b8534bSLuigi Rizzo 		return NULL;	/* nothing to reinitialize */
159168b8534bSLuigi Rizzo 
159264ae02c3SLuigi Rizzo 	if (tx == NR_TX) {
1593*8241616dSLuigi Rizzo 		if (n >= na->num_tx_rings)
1594*8241616dSLuigi Rizzo 			return NULL;
159564ae02c3SLuigi Rizzo 		kring = na->tx_rings + n;
1596506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur - new_cur;
159764ae02c3SLuigi Rizzo 	} else {
1598*8241616dSLuigi Rizzo 		if (n >= na->num_rx_rings)
1599*8241616dSLuigi Rizzo 			return NULL;
160064ae02c3SLuigi Rizzo 		kring = na->rx_rings + n;
1601506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur + kring->nr_hwavail - new_cur;
160264ae02c3SLuigi Rizzo 	}
160364ae02c3SLuigi Rizzo 	lim = kring->nkr_num_slots - 1;
1604506cc70cSLuigi Rizzo 	if (new_hwofs > lim)
1605506cc70cSLuigi Rizzo 		new_hwofs -= lim + 1;
1606506cc70cSLuigi Rizzo 
1607506cc70cSLuigi Rizzo 	/* Alwayws set the new offset value and realign the ring. */
1608506cc70cSLuigi Rizzo 	kring->nkr_hwofs = new_hwofs;
1609506cc70cSLuigi Rizzo 	if (tx == NR_TX)
1610506cc70cSLuigi Rizzo 		kring->nr_hwavail = kring->nkr_num_slots - 1;
1611*8241616dSLuigi Rizzo 	ND(10, "new hwofs %d on %s %s[%d]",
1612506cc70cSLuigi Rizzo 			kring->nkr_hwofs, na->ifp->if_xname,
1613506cc70cSLuigi Rizzo 			tx == NR_TX ? "TX" : "RX", n);
1614506cc70cSLuigi Rizzo 
1615f196ce38SLuigi Rizzo #if 0 // def linux
1616f196ce38SLuigi Rizzo 	/* XXX check that the mappings are correct */
1617f196ce38SLuigi Rizzo 	/* need ring_nr, adapter->pdev, direction */
1618f196ce38SLuigi Rizzo 	buffer_info->dma = dma_map_single(&pdev->dev, addr, adapter->rx_buffer_len, DMA_FROM_DEVICE);
1619f196ce38SLuigi Rizzo 	if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
1620f196ce38SLuigi Rizzo 		D("error mapping rx netmap buffer %d", i);
1621f196ce38SLuigi Rizzo 		// XXX fix error handling
1622f196ce38SLuigi Rizzo 	}
1623f196ce38SLuigi Rizzo 
1624f196ce38SLuigi Rizzo #endif /* linux */
162568b8534bSLuigi Rizzo 	/*
162664ae02c3SLuigi Rizzo 	 * Wakeup on the individual and global lock
1627506cc70cSLuigi Rizzo 	 * We do the wakeup here, but the ring is not yet reconfigured.
1628506cc70cSLuigi Rizzo 	 * However, we are under lock so there are no races.
162968b8534bSLuigi Rizzo 	 */
163068b8534bSLuigi Rizzo 	selwakeuppri(&kring->si, PI_NET);
163164ae02c3SLuigi Rizzo 	selwakeuppri(tx == NR_TX ? &na->tx_si : &na->rx_si, PI_NET);
163268b8534bSLuigi Rizzo 	return kring->ring->slot;
163368b8534bSLuigi Rizzo }
163468b8534bSLuigi Rizzo 
163568b8534bSLuigi Rizzo 
163668b8534bSLuigi Rizzo /*
16371a26580eSLuigi Rizzo  * Default functions to handle rx/tx interrupts
16381a26580eSLuigi Rizzo  * we have 4 cases:
16391a26580eSLuigi Rizzo  * 1 ring, single lock:
16401a26580eSLuigi Rizzo  *	lock(core); wake(i=0); unlock(core)
16411a26580eSLuigi Rizzo  * N rings, single lock:
16421a26580eSLuigi Rizzo  *	lock(core); wake(i); wake(N+1) unlock(core)
16431a26580eSLuigi Rizzo  * 1 ring, separate locks: (i=0)
16441a26580eSLuigi Rizzo  *	lock(i); wake(i); unlock(i)
16451a26580eSLuigi Rizzo  * N rings, separate locks:
16461a26580eSLuigi Rizzo  *	lock(i); wake(i); unlock(i); lock(core) wake(N+1) unlock(core)
164764ae02c3SLuigi Rizzo  * work_done is non-null on the RX path.
16481a26580eSLuigi Rizzo  */
1649babc7c12SLuigi Rizzo int
1650babc7c12SLuigi Rizzo netmap_rx_irq(struct ifnet *ifp, int q, int *work_done)
16511a26580eSLuigi Rizzo {
16521a26580eSLuigi Rizzo 	struct netmap_adapter *na;
16531a26580eSLuigi Rizzo 	struct netmap_kring *r;
165464ae02c3SLuigi Rizzo 	NM_SELINFO_T *main_wq;
16551a26580eSLuigi Rizzo 
16561a26580eSLuigi Rizzo 	if (!(ifp->if_capenable & IFCAP_NETMAP))
16571a26580eSLuigi Rizzo 		return 0;
1658*8241616dSLuigi Rizzo 	ND(5, "received %s queue %d", work_done ? "RX" : "TX" , q);
16591a26580eSLuigi Rizzo 	na = NA(ifp);
1660*8241616dSLuigi Rizzo 	if (na->na_flags & NAF_SKIP_INTR) {
1661*8241616dSLuigi Rizzo 		ND("use regular interrupt");
1662*8241616dSLuigi Rizzo 		return 0;
1663*8241616dSLuigi Rizzo 	}
1664*8241616dSLuigi Rizzo 
166564ae02c3SLuigi Rizzo 	if (work_done) { /* RX path */
1666*8241616dSLuigi Rizzo 		if (q >= na->num_rx_rings)
1667*8241616dSLuigi Rizzo 			return 0;	// regular queue
166864ae02c3SLuigi Rizzo 		r = na->rx_rings + q;
166964ae02c3SLuigi Rizzo 		r->nr_kflags |= NKR_PENDINTR;
1670d76bf4ffSLuigi Rizzo 		main_wq = (na->num_rx_rings > 1) ? &na->rx_si : NULL;
167164ae02c3SLuigi Rizzo 	} else { /* tx path */
1672*8241616dSLuigi Rizzo 		if (q >= na->num_tx_rings)
1673*8241616dSLuigi Rizzo 			return 0;	// regular queue
167464ae02c3SLuigi Rizzo 		r = na->tx_rings + q;
1675d76bf4ffSLuigi Rizzo 		main_wq = (na->num_tx_rings > 1) ? &na->tx_si : NULL;
167664ae02c3SLuigi Rizzo 		work_done = &q; /* dummy */
167764ae02c3SLuigi Rizzo 	}
16781a26580eSLuigi Rizzo 	if (na->separate_locks) {
167964ae02c3SLuigi Rizzo 		mtx_lock(&r->q_lock);
168064ae02c3SLuigi Rizzo 		selwakeuppri(&r->si, PI_NET);
168164ae02c3SLuigi Rizzo 		mtx_unlock(&r->q_lock);
168264ae02c3SLuigi Rizzo 		if (main_wq) {
16831a26580eSLuigi Rizzo 			mtx_lock(&na->core_lock);
168464ae02c3SLuigi Rizzo 			selwakeuppri(main_wq, PI_NET);
16851a26580eSLuigi Rizzo 			mtx_unlock(&na->core_lock);
16861a26580eSLuigi Rizzo 		}
16871a26580eSLuigi Rizzo 	} else {
16881a26580eSLuigi Rizzo 		mtx_lock(&na->core_lock);
168964ae02c3SLuigi Rizzo 		selwakeuppri(&r->si, PI_NET);
169064ae02c3SLuigi Rizzo 		if (main_wq)
169164ae02c3SLuigi Rizzo 			selwakeuppri(main_wq, PI_NET);
16921a26580eSLuigi Rizzo 		mtx_unlock(&na->core_lock);
16931a26580eSLuigi Rizzo 	}
16941a26580eSLuigi Rizzo 	*work_done = 1; /* do not fire napi again */
16951a26580eSLuigi Rizzo 	return 1;
16961a26580eSLuigi Rizzo }
16971a26580eSLuigi Rizzo 
169864ae02c3SLuigi Rizzo 
169901c7d25fSLuigi Rizzo #ifdef linux	/* linux-specific routines */
170001c7d25fSLuigi Rizzo 
170101c7d25fSLuigi Rizzo /*
170201c7d25fSLuigi Rizzo  * Remap linux arguments into the FreeBSD call.
170301c7d25fSLuigi Rizzo  * - pwait is the poll table, passed as 'dev';
170401c7d25fSLuigi Rizzo  *   If pwait == NULL someone else already woke up before. We can report
170501c7d25fSLuigi Rizzo  *   events but they are filtered upstream.
170601c7d25fSLuigi Rizzo  *   If pwait != NULL, then pwait->key contains the list of events.
170701c7d25fSLuigi Rizzo  * - events is computed from pwait as above.
170801c7d25fSLuigi Rizzo  * - file is passed as 'td';
170901c7d25fSLuigi Rizzo  */
171001c7d25fSLuigi Rizzo static u_int
171101c7d25fSLuigi Rizzo linux_netmap_poll(struct file * file, struct poll_table_struct *pwait)
171201c7d25fSLuigi Rizzo {
171301c7d25fSLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(3,4,0)
171401c7d25fSLuigi Rizzo 	int events = pwait ? pwait->key : POLLIN | POLLOUT;
171501c7d25fSLuigi Rizzo #else /* in 3.4.0 field 'key' was renamed to '_key' */
171601c7d25fSLuigi Rizzo 	int events = pwait ? pwait->_key : POLLIN | POLLOUT;
171701c7d25fSLuigi Rizzo #endif
171801c7d25fSLuigi Rizzo 	return netmap_poll((void *)pwait, events, (void *)file);
171901c7d25fSLuigi Rizzo }
172001c7d25fSLuigi Rizzo 
172101c7d25fSLuigi Rizzo static int
172242a3a5bdSLuigi Rizzo linux_netmap_mmap(struct file *f, struct vm_area_struct *vma)
172301c7d25fSLuigi Rizzo {
172401c7d25fSLuigi Rizzo 	int lut_skip, i, j;
172501c7d25fSLuigi Rizzo 	int user_skip = 0;
172601c7d25fSLuigi Rizzo 	struct lut_entry *l_entry;
1727*8241616dSLuigi Rizzo 	int error = 0;
1728*8241616dSLuigi Rizzo 	unsigned long off, tomap;
172901c7d25fSLuigi Rizzo 	/*
173001c7d25fSLuigi Rizzo 	 * vma->vm_start: start of mapping user address space
173101c7d25fSLuigi Rizzo 	 * vma->vm_end: end of the mapping user address space
1732*8241616dSLuigi Rizzo 	 * vma->vm_pfoff: offset of first page in the device
173301c7d25fSLuigi Rizzo 	 */
173401c7d25fSLuigi Rizzo 
173501c7d25fSLuigi Rizzo 	// XXX security checks
173601c7d25fSLuigi Rizzo 
1737*8241616dSLuigi Rizzo 	error = netmap_get_memory(f->private_data);
1738*8241616dSLuigi Rizzo 	ND("get_memory returned %d", error);
1739*8241616dSLuigi Rizzo 	if (error)
1740*8241616dSLuigi Rizzo 	    return -error;
1741*8241616dSLuigi Rizzo 
1742*8241616dSLuigi Rizzo 	off = vma->vm_pgoff << PAGE_SHIFT; /* offset in bytes */
1743*8241616dSLuigi Rizzo 	tomap = vma->vm_end - vma->vm_start;
1744*8241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {  /* loop through obj_pools */
1745*8241616dSLuigi Rizzo 		const struct netmap_obj_pool *p = &nm_mem.pools[i];
174601c7d25fSLuigi Rizzo 		/*
174701c7d25fSLuigi Rizzo 		 * In each pool memory is allocated in clusters
174801c7d25fSLuigi Rizzo 		 * of size _clustsize, each containing clustentries
174901c7d25fSLuigi Rizzo 		 * entries. For each object k we already store the
1750*8241616dSLuigi Rizzo 		 * vtophys mapping in lut[k] so we use that, scanning
175101c7d25fSLuigi Rizzo 		 * the lut[] array in steps of clustentries,
175201c7d25fSLuigi Rizzo 		 * and we map each cluster (not individual pages,
175301c7d25fSLuigi Rizzo 		 * it would be overkill).
175401c7d25fSLuigi Rizzo 		 */
1755*8241616dSLuigi Rizzo 
1756*8241616dSLuigi Rizzo 		/*
1757*8241616dSLuigi Rizzo 		 * We interpret vm_pgoff as an offset into the whole
1758*8241616dSLuigi Rizzo 		 * netmap memory, as if all clusters where contiguous.
1759*8241616dSLuigi Rizzo 		 */
1760*8241616dSLuigi Rizzo 		for (lut_skip = 0, j = 0; j < p->_numclusters; j++, lut_skip += p->clustentries) {
1761*8241616dSLuigi Rizzo 			unsigned long paddr, mapsize;
1762*8241616dSLuigi Rizzo 			if (p->_clustsize <= off) {
1763*8241616dSLuigi Rizzo 				off -= p->_clustsize;
1764*8241616dSLuigi Rizzo 				continue;
1765*8241616dSLuigi Rizzo 			}
1766*8241616dSLuigi Rizzo 			l_entry = &p->lut[lut_skip]; /* first obj in the cluster */
1767*8241616dSLuigi Rizzo 			paddr = l_entry->paddr + off;
1768*8241616dSLuigi Rizzo 			mapsize = p->_clustsize - off;
1769*8241616dSLuigi Rizzo 			off = 0;
1770*8241616dSLuigi Rizzo 			if (mapsize > tomap)
1771*8241616dSLuigi Rizzo 				mapsize = tomap;
1772*8241616dSLuigi Rizzo 			ND("remap_pfn_range(%lx, %lx, %lx)",
1773*8241616dSLuigi Rizzo 				vma->vm_start + user_skip,
1774*8241616dSLuigi Rizzo 				paddr >> PAGE_SHIFT, mapsize);
177501c7d25fSLuigi Rizzo 			if (remap_pfn_range(vma, vma->vm_start + user_skip,
1776*8241616dSLuigi Rizzo 					paddr >> PAGE_SHIFT, mapsize,
177701c7d25fSLuigi Rizzo 					vma->vm_page_prot))
177801c7d25fSLuigi Rizzo 				return -EAGAIN; // XXX check return value
1779*8241616dSLuigi Rizzo 			user_skip += mapsize;
1780*8241616dSLuigi Rizzo 			tomap -= mapsize;
1781*8241616dSLuigi Rizzo 			if (tomap == 0)
1782*8241616dSLuigi Rizzo 				goto done;
178301c7d25fSLuigi Rizzo 		}
178401c7d25fSLuigi Rizzo 	}
1785*8241616dSLuigi Rizzo done:
178601c7d25fSLuigi Rizzo 
178701c7d25fSLuigi Rizzo 	return 0;
178801c7d25fSLuigi Rizzo }
178901c7d25fSLuigi Rizzo 
179001c7d25fSLuigi Rizzo static netdev_tx_t
179142a3a5bdSLuigi Rizzo linux_netmap_start(struct sk_buff *skb, struct net_device *dev)
179201c7d25fSLuigi Rizzo {
179301c7d25fSLuigi Rizzo 	netmap_start(dev, skb);
179401c7d25fSLuigi Rizzo 	return (NETDEV_TX_OK);
179501c7d25fSLuigi Rizzo }
179601c7d25fSLuigi Rizzo 
179701c7d25fSLuigi Rizzo 
17980b8ed8e0SLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)	// XXX was 38
179901c7d25fSLuigi Rizzo #define LIN_IOCTL_NAME	.ioctl
180001c7d25fSLuigi Rizzo int
180101c7d25fSLuigi Rizzo linux_netmap_ioctl(struct inode *inode, struct file *file, u_int cmd, u_long data /* arg */)
180201c7d25fSLuigi Rizzo #else
180301c7d25fSLuigi Rizzo #define LIN_IOCTL_NAME	.unlocked_ioctl
180401c7d25fSLuigi Rizzo long
180501c7d25fSLuigi Rizzo linux_netmap_ioctl(struct file *file, u_int cmd, u_long data /* arg */)
180601c7d25fSLuigi Rizzo #endif
180701c7d25fSLuigi Rizzo {
180801c7d25fSLuigi Rizzo 	int ret;
180901c7d25fSLuigi Rizzo 	struct nmreq nmr;
181001c7d25fSLuigi Rizzo 	bzero(&nmr, sizeof(nmr));
181101c7d25fSLuigi Rizzo 
181201c7d25fSLuigi Rizzo 	if (data && copy_from_user(&nmr, (void *)data, sizeof(nmr) ) != 0)
181301c7d25fSLuigi Rizzo 		return -EFAULT;
181401c7d25fSLuigi Rizzo 	ret = netmap_ioctl(NULL, cmd, (caddr_t)&nmr, 0, (void *)file);
181501c7d25fSLuigi Rizzo 	if (data && copy_to_user((void*)data, &nmr, sizeof(nmr) ) != 0)
181601c7d25fSLuigi Rizzo 		return -EFAULT;
181701c7d25fSLuigi Rizzo 	return -ret;
181801c7d25fSLuigi Rizzo }
181901c7d25fSLuigi Rizzo 
182001c7d25fSLuigi Rizzo 
182101c7d25fSLuigi Rizzo static int
18220b8ed8e0SLuigi Rizzo netmap_release(struct inode *inode, struct file *file)
182301c7d25fSLuigi Rizzo {
18240b8ed8e0SLuigi Rizzo 	(void)inode;	/* UNUSED */
182501c7d25fSLuigi Rizzo 	if (file->private_data)
182601c7d25fSLuigi Rizzo 		netmap_dtor(file->private_data);
182701c7d25fSLuigi Rizzo 	return (0);
182801c7d25fSLuigi Rizzo }
182901c7d25fSLuigi Rizzo 
1830*8241616dSLuigi Rizzo static int
1831*8241616dSLuigi Rizzo linux_netmap_open(struct inode *inode, struct file *file)
1832*8241616dSLuigi Rizzo {
1833*8241616dSLuigi Rizzo 	struct netmap_priv_d *priv;
1834*8241616dSLuigi Rizzo 	(void)inode;	/* UNUSED */
1835*8241616dSLuigi Rizzo 
1836*8241616dSLuigi Rizzo 	priv = malloc(sizeof(struct netmap_priv_d), M_DEVBUF,
1837*8241616dSLuigi Rizzo 			      M_NOWAIT | M_ZERO);
1838*8241616dSLuigi Rizzo 	if (priv == NULL)
1839*8241616dSLuigi Rizzo 		return -ENOMEM;
1840*8241616dSLuigi Rizzo 
1841*8241616dSLuigi Rizzo 	file->private_data = priv;
1842*8241616dSLuigi Rizzo 
1843*8241616dSLuigi Rizzo 	return (0);
1844*8241616dSLuigi Rizzo }
184501c7d25fSLuigi Rizzo 
184601c7d25fSLuigi Rizzo static struct file_operations netmap_fops = {
1847*8241616dSLuigi Rizzo     .open = linux_netmap_open,
184842a3a5bdSLuigi Rizzo     .mmap = linux_netmap_mmap,
184901c7d25fSLuigi Rizzo     LIN_IOCTL_NAME = linux_netmap_ioctl,
185001c7d25fSLuigi Rizzo     .poll = linux_netmap_poll,
185101c7d25fSLuigi Rizzo     .release = netmap_release,
185201c7d25fSLuigi Rizzo };
185301c7d25fSLuigi Rizzo 
185401c7d25fSLuigi Rizzo static struct miscdevice netmap_cdevsw = {	/* same name as FreeBSD */
185501c7d25fSLuigi Rizzo 	MISC_DYNAMIC_MINOR,
185601c7d25fSLuigi Rizzo 	"netmap",
185701c7d25fSLuigi Rizzo 	&netmap_fops,
185801c7d25fSLuigi Rizzo };
185901c7d25fSLuigi Rizzo 
186001c7d25fSLuigi Rizzo static int netmap_init(void);
186101c7d25fSLuigi Rizzo static void netmap_fini(void);
186201c7d25fSLuigi Rizzo 
186342a3a5bdSLuigi Rizzo /* Errors have negative values on linux */
186442a3a5bdSLuigi Rizzo static int linux_netmap_init(void)
186542a3a5bdSLuigi Rizzo {
186642a3a5bdSLuigi Rizzo 	return -netmap_init();
186742a3a5bdSLuigi Rizzo }
186842a3a5bdSLuigi Rizzo 
186942a3a5bdSLuigi Rizzo module_init(linux_netmap_init);
187001c7d25fSLuigi Rizzo module_exit(netmap_fini);
187101c7d25fSLuigi Rizzo /* export certain symbols to other modules */
187201c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_attach);		// driver attach routines
187301c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_detach);		// driver detach routines
187401c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_ring_reinit);	// ring init on error
187501c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_buffer_lut);
187601c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_total_buffers);	// index check
187701c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_buffer_base);
187801c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_reset);		// ring init routines
187901c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_buf_size);
188001c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_rx_irq);		// default irq handler
188101c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_no_pendintr);	// XXX mitigation - should go away
188201c7d25fSLuigi Rizzo 
188301c7d25fSLuigi Rizzo 
188401c7d25fSLuigi Rizzo MODULE_AUTHOR("http://info.iet.unipi.it/~luigi/netmap/");
188501c7d25fSLuigi Rizzo MODULE_DESCRIPTION("The netmap packet I/O framework");
188601c7d25fSLuigi Rizzo MODULE_LICENSE("Dual BSD/GPL"); /* the code here is all BSD. */
188701c7d25fSLuigi Rizzo 
188801c7d25fSLuigi Rizzo #else /* __FreeBSD__ */
188901c7d25fSLuigi Rizzo 
1890babc7c12SLuigi Rizzo static struct cdevsw netmap_cdevsw = {
1891babc7c12SLuigi Rizzo 	.d_version = D_VERSION,
1892babc7c12SLuigi Rizzo 	.d_name = "netmap",
1893*8241616dSLuigi Rizzo 	.d_open = netmap_open,
1894babc7c12SLuigi Rizzo 	.d_mmap = netmap_mmap,
1895*8241616dSLuigi Rizzo 	.d_mmap_single = netmap_mmap_single,
1896babc7c12SLuigi Rizzo 	.d_ioctl = netmap_ioctl,
1897babc7c12SLuigi Rizzo 	.d_poll = netmap_poll,
1898*8241616dSLuigi Rizzo 	.d_close = netmap_close,
1899babc7c12SLuigi Rizzo };
190001c7d25fSLuigi Rizzo #endif /* __FreeBSD__ */
1901babc7c12SLuigi Rizzo 
1902f196ce38SLuigi Rizzo #ifdef NM_BRIDGE
1903f196ce38SLuigi Rizzo /*
1904f196ce38SLuigi Rizzo  *---- support for virtual bridge -----
1905f196ce38SLuigi Rizzo  */
1906f196ce38SLuigi Rizzo 
1907f196ce38SLuigi Rizzo /* ----- FreeBSD if_bridge hash function ------- */
1908f196ce38SLuigi Rizzo 
1909f196ce38SLuigi Rizzo /*
1910f196ce38SLuigi Rizzo  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
1911f196ce38SLuigi Rizzo  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
1912f196ce38SLuigi Rizzo  *
1913f196ce38SLuigi Rizzo  * http://www.burtleburtle.net/bob/hash/spooky.html
1914f196ce38SLuigi Rizzo  */
1915f196ce38SLuigi Rizzo #define mix(a, b, c)                                                    \
1916f196ce38SLuigi Rizzo do {                                                                    \
1917f196ce38SLuigi Rizzo         a -= b; a -= c; a ^= (c >> 13);                                 \
1918f196ce38SLuigi Rizzo         b -= c; b -= a; b ^= (a << 8);                                  \
1919f196ce38SLuigi Rizzo         c -= a; c -= b; c ^= (b >> 13);                                 \
1920f196ce38SLuigi Rizzo         a -= b; a -= c; a ^= (c >> 12);                                 \
1921f196ce38SLuigi Rizzo         b -= c; b -= a; b ^= (a << 16);                                 \
1922f196ce38SLuigi Rizzo         c -= a; c -= b; c ^= (b >> 5);                                  \
1923f196ce38SLuigi Rizzo         a -= b; a -= c; a ^= (c >> 3);                                  \
1924f196ce38SLuigi Rizzo         b -= c; b -= a; b ^= (a << 10);                                 \
1925f196ce38SLuigi Rizzo         c -= a; c -= b; c ^= (b >> 15);                                 \
1926f196ce38SLuigi Rizzo } while (/*CONSTCOND*/0)
1927f196ce38SLuigi Rizzo 
1928f196ce38SLuigi Rizzo static __inline uint32_t
1929f196ce38SLuigi Rizzo nm_bridge_rthash(const uint8_t *addr)
1930f196ce38SLuigi Rizzo {
1931f196ce38SLuigi Rizzo         uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = 0; // hask key
1932f196ce38SLuigi Rizzo 
1933f196ce38SLuigi Rizzo         b += addr[5] << 8;
1934f196ce38SLuigi Rizzo         b += addr[4];
1935f196ce38SLuigi Rizzo         a += addr[3] << 24;
1936f196ce38SLuigi Rizzo         a += addr[2] << 16;
1937f196ce38SLuigi Rizzo         a += addr[1] << 8;
1938f196ce38SLuigi Rizzo         a += addr[0];
1939f196ce38SLuigi Rizzo 
1940f196ce38SLuigi Rizzo         mix(a, b, c);
1941f196ce38SLuigi Rizzo #define BRIDGE_RTHASH_MASK	(NM_BDG_HASH-1)
1942f196ce38SLuigi Rizzo         return (c & BRIDGE_RTHASH_MASK);
1943f196ce38SLuigi Rizzo }
1944f196ce38SLuigi Rizzo 
1945f196ce38SLuigi Rizzo #undef mix
1946f196ce38SLuigi Rizzo 
1947f196ce38SLuigi Rizzo 
1948f196ce38SLuigi Rizzo static int
1949f196ce38SLuigi Rizzo bdg_netmap_reg(struct ifnet *ifp, int onoff)
1950f196ce38SLuigi Rizzo {
1951f196ce38SLuigi Rizzo 	int i, err = 0;
1952f196ce38SLuigi Rizzo 	struct nm_bridge *b = ifp->if_bridge;
1953f196ce38SLuigi Rizzo 
1954f196ce38SLuigi Rizzo 	BDG_LOCK(b);
1955f196ce38SLuigi Rizzo 	if (onoff) {
1956f196ce38SLuigi Rizzo 		/* the interface must be already in the list.
1957f196ce38SLuigi Rizzo 		 * only need to mark the port as active
1958f196ce38SLuigi Rizzo 		 */
1959f196ce38SLuigi Rizzo 		ND("should attach %s to the bridge", ifp->if_xname);
1960f196ce38SLuigi Rizzo 		for (i=0; i < NM_BDG_MAXPORTS; i++)
1961f196ce38SLuigi Rizzo 			if (b->bdg_ports[i] == ifp)
1962f196ce38SLuigi Rizzo 				break;
1963f196ce38SLuigi Rizzo 		if (i == NM_BDG_MAXPORTS) {
1964f196ce38SLuigi Rizzo 			D("no more ports available");
1965f196ce38SLuigi Rizzo 			err = EINVAL;
1966f196ce38SLuigi Rizzo 			goto done;
1967f196ce38SLuigi Rizzo 		}
1968f196ce38SLuigi Rizzo 		ND("setting %s in netmap mode", ifp->if_xname);
1969f196ce38SLuigi Rizzo 		ifp->if_capenable |= IFCAP_NETMAP;
1970f196ce38SLuigi Rizzo 		NA(ifp)->bdg_port = i;
1971f196ce38SLuigi Rizzo 		b->act_ports |= (1<<i);
1972f196ce38SLuigi Rizzo 		b->bdg_ports[i] = ifp;
1973f196ce38SLuigi Rizzo 	} else {
1974f196ce38SLuigi Rizzo 		/* should be in the list, too -- remove from the mask */
1975f196ce38SLuigi Rizzo 		ND("removing %s from netmap mode", ifp->if_xname);
1976f196ce38SLuigi Rizzo 		ifp->if_capenable &= ~IFCAP_NETMAP;
1977f196ce38SLuigi Rizzo 		i = NA(ifp)->bdg_port;
1978f196ce38SLuigi Rizzo 		b->act_ports &= ~(1<<i);
1979f196ce38SLuigi Rizzo 	}
1980f196ce38SLuigi Rizzo done:
1981f196ce38SLuigi Rizzo 	BDG_UNLOCK(b);
1982f196ce38SLuigi Rizzo 	return err;
1983f196ce38SLuigi Rizzo }
1984f196ce38SLuigi Rizzo 
1985f196ce38SLuigi Rizzo 
1986f196ce38SLuigi Rizzo static int
1987f196ce38SLuigi Rizzo nm_bdg_flush(struct nm_bdg_fwd *ft, int n, struct ifnet *ifp)
1988f196ce38SLuigi Rizzo {
1989f196ce38SLuigi Rizzo 	int i, ifn;
1990f196ce38SLuigi Rizzo 	uint64_t all_dst, dst;
1991f196ce38SLuigi Rizzo 	uint32_t sh, dh;
1992f196ce38SLuigi Rizzo 	uint64_t mysrc = 1 << NA(ifp)->bdg_port;
1993f196ce38SLuigi Rizzo 	uint64_t smac, dmac;
1994f196ce38SLuigi Rizzo 	struct netmap_slot *slot;
1995f196ce38SLuigi Rizzo 	struct nm_bridge *b = ifp->if_bridge;
1996f196ce38SLuigi Rizzo 
1997f196ce38SLuigi Rizzo 	ND("prepare to send %d packets, act_ports 0x%x", n, b->act_ports);
1998f196ce38SLuigi Rizzo 	/* only consider valid destinations */
1999f196ce38SLuigi Rizzo 	all_dst = (b->act_ports & ~mysrc);
2000f196ce38SLuigi Rizzo 	/* first pass: hash and find destinations */
2001f196ce38SLuigi Rizzo 	for (i = 0; likely(i < n); i++) {
2002f196ce38SLuigi Rizzo 		uint8_t *buf = ft[i].buf;
2003f196ce38SLuigi Rizzo 		dmac = le64toh(*(uint64_t *)(buf)) & 0xffffffffffff;
2004f196ce38SLuigi Rizzo 		smac = le64toh(*(uint64_t *)(buf + 4));
2005f196ce38SLuigi Rizzo 		smac >>= 16;
2006f196ce38SLuigi Rizzo 		if (unlikely(netmap_verbose)) {
2007f196ce38SLuigi Rizzo 		    uint8_t *s = buf+6, *d = buf;
2008f196ce38SLuigi Rizzo 		    D("%d len %4d %02x:%02x:%02x:%02x:%02x:%02x -> %02x:%02x:%02x:%02x:%02x:%02x",
2009f196ce38SLuigi Rizzo 			i,
2010f196ce38SLuigi Rizzo 			ft[i].len,
2011f196ce38SLuigi Rizzo 			s[0], s[1], s[2], s[3], s[4], s[5],
2012f196ce38SLuigi Rizzo 			d[0], d[1], d[2], d[3], d[4], d[5]);
2013f196ce38SLuigi Rizzo 		}
2014f196ce38SLuigi Rizzo 		/*
2015f196ce38SLuigi Rizzo 		 * The hash is somewhat expensive, there might be some
2016f196ce38SLuigi Rizzo 		 * worthwhile optimizations here.
2017f196ce38SLuigi Rizzo 		 */
2018f196ce38SLuigi Rizzo 		if ((buf[6] & 1) == 0) { /* valid src */
2019f196ce38SLuigi Rizzo 		    	uint8_t *s = buf+6;
2020f196ce38SLuigi Rizzo 			sh = nm_bridge_rthash(buf+6); // XXX hash of source
2021f196ce38SLuigi Rizzo 			/* update source port forwarding entry */
2022f196ce38SLuigi Rizzo 			b->ht[sh].mac = smac;	/* XXX expire ? */
2023f196ce38SLuigi Rizzo 			b->ht[sh].ports = mysrc;
2024f196ce38SLuigi Rizzo 			if (netmap_verbose)
2025f196ce38SLuigi Rizzo 			    D("src %02x:%02x:%02x:%02x:%02x:%02x on port %d",
2026f196ce38SLuigi Rizzo 				s[0], s[1], s[2], s[3], s[4], s[5], NA(ifp)->bdg_port);
2027f196ce38SLuigi Rizzo 		}
2028f196ce38SLuigi Rizzo 		dst = 0;
2029f196ce38SLuigi Rizzo 		if ( (buf[0] & 1) == 0) { /* unicast */
2030f196ce38SLuigi Rizzo 		    	uint8_t *d = buf;
2031f196ce38SLuigi Rizzo 			dh = nm_bridge_rthash(buf); // XXX hash of dst
2032f196ce38SLuigi Rizzo 			if (b->ht[dh].mac == dmac) {	/* found dst */
2033f196ce38SLuigi Rizzo 				dst = b->ht[dh].ports;
2034f196ce38SLuigi Rizzo 				if (netmap_verbose)
2035f196ce38SLuigi Rizzo 				    D("dst %02x:%02x:%02x:%02x:%02x:%02x to port %x",
2036f196ce38SLuigi Rizzo 					d[0], d[1], d[2], d[3], d[4], d[5], (uint32_t)(dst >> 16));
2037f196ce38SLuigi Rizzo 			}
2038f196ce38SLuigi Rizzo 		}
2039f196ce38SLuigi Rizzo 		if (dst == 0)
2040f196ce38SLuigi Rizzo 			dst = all_dst;
2041f196ce38SLuigi Rizzo 		dst &= all_dst; /* only consider valid ports */
2042f196ce38SLuigi Rizzo 		if (unlikely(netmap_verbose))
2043f196ce38SLuigi Rizzo 			D("pkt goes to ports 0x%x", (uint32_t)dst);
2044f196ce38SLuigi Rizzo 		ft[i].dst = dst;
2045f196ce38SLuigi Rizzo 	}
2046f196ce38SLuigi Rizzo 
2047f196ce38SLuigi Rizzo 	/* second pass, scan interfaces and forward */
2048f196ce38SLuigi Rizzo 	all_dst = (b->act_ports & ~mysrc);
2049f196ce38SLuigi Rizzo 	for (ifn = 0; all_dst; ifn++) {
2050f196ce38SLuigi Rizzo 		struct ifnet *dst_ifp = b->bdg_ports[ifn];
2051f196ce38SLuigi Rizzo 		struct netmap_adapter *na;
2052f196ce38SLuigi Rizzo 		struct netmap_kring *kring;
2053f196ce38SLuigi Rizzo 		struct netmap_ring *ring;
2054f196ce38SLuigi Rizzo 		int j, lim, sent, locked;
2055f196ce38SLuigi Rizzo 
2056f196ce38SLuigi Rizzo 		if (!dst_ifp)
2057f196ce38SLuigi Rizzo 			continue;
2058f196ce38SLuigi Rizzo 		ND("scan port %d %s", ifn, dst_ifp->if_xname);
2059f196ce38SLuigi Rizzo 		dst = 1 << ifn;
2060f196ce38SLuigi Rizzo 		if ((dst & all_dst) == 0)	/* skip if not set */
2061f196ce38SLuigi Rizzo 			continue;
2062f196ce38SLuigi Rizzo 		all_dst &= ~dst;	/* clear current node */
2063f196ce38SLuigi Rizzo 		na = NA(dst_ifp);
2064f196ce38SLuigi Rizzo 
2065f196ce38SLuigi Rizzo 		ring = NULL;
2066f196ce38SLuigi Rizzo 		kring = NULL;
2067f196ce38SLuigi Rizzo 		lim = sent = locked = 0;
2068f196ce38SLuigi Rizzo 		/* inside, scan slots */
2069f196ce38SLuigi Rizzo 		for (i = 0; likely(i < n); i++) {
2070f196ce38SLuigi Rizzo 			if ((ft[i].dst & dst) == 0)
2071f196ce38SLuigi Rizzo 				continue;	/* not here */
2072f196ce38SLuigi Rizzo 			if (!locked) {
2073f196ce38SLuigi Rizzo 				kring = &na->rx_rings[0];
2074f196ce38SLuigi Rizzo 				ring = kring->ring;
2075f196ce38SLuigi Rizzo 				lim = kring->nkr_num_slots - 1;
2076f196ce38SLuigi Rizzo 				na->nm_lock(dst_ifp, NETMAP_RX_LOCK, 0);
2077f196ce38SLuigi Rizzo 				locked = 1;
2078f196ce38SLuigi Rizzo 			}
2079f196ce38SLuigi Rizzo 			if (unlikely(kring->nr_hwavail >= lim)) {
2080f196ce38SLuigi Rizzo 				if (netmap_verbose)
2081f196ce38SLuigi Rizzo 					D("rx ring full on %s", ifp->if_xname);
2082f196ce38SLuigi Rizzo 				break;
2083f196ce38SLuigi Rizzo 			}
2084f196ce38SLuigi Rizzo 			j = kring->nr_hwcur + kring->nr_hwavail;
2085f196ce38SLuigi Rizzo 			if (j > lim)
2086f196ce38SLuigi Rizzo 				j -= kring->nkr_num_slots;
2087f196ce38SLuigi Rizzo 			slot = &ring->slot[j];
2088f196ce38SLuigi Rizzo 			ND("send %d %d bytes at %s:%d", i, ft[i].len, dst_ifp->if_xname, j);
2089f196ce38SLuigi Rizzo 			pkt_copy(ft[i].buf, NMB(slot), ft[i].len);
2090f196ce38SLuigi Rizzo 			slot->len = ft[i].len;
2091f196ce38SLuigi Rizzo 			kring->nr_hwavail++;
2092f196ce38SLuigi Rizzo 			sent++;
2093f196ce38SLuigi Rizzo 		}
2094f196ce38SLuigi Rizzo 		if (locked) {
2095f196ce38SLuigi Rizzo 			ND("sent %d on %s", sent, dst_ifp->if_xname);
2096f196ce38SLuigi Rizzo 			if (sent)
2097f196ce38SLuigi Rizzo 				selwakeuppri(&kring->si, PI_NET);
2098f196ce38SLuigi Rizzo 			na->nm_lock(dst_ifp, NETMAP_RX_UNLOCK, 0);
2099f196ce38SLuigi Rizzo 		}
2100f196ce38SLuigi Rizzo 	}
2101f196ce38SLuigi Rizzo 	return 0;
2102f196ce38SLuigi Rizzo }
2103f196ce38SLuigi Rizzo 
2104f196ce38SLuigi Rizzo /*
2105f196ce38SLuigi Rizzo  * main dispatch routine
2106f196ce38SLuigi Rizzo  */
2107f196ce38SLuigi Rizzo static int
2108f196ce38SLuigi Rizzo bdg_netmap_txsync(struct ifnet *ifp, u_int ring_nr, int do_lock)
2109f196ce38SLuigi Rizzo {
2110f196ce38SLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
2111f196ce38SLuigi Rizzo 	struct netmap_kring *kring = &na->tx_rings[ring_nr];
2112f196ce38SLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
2113f196ce38SLuigi Rizzo 	int i, j, k, lim = kring->nkr_num_slots - 1;
2114f196ce38SLuigi Rizzo 	struct nm_bdg_fwd *ft = (struct nm_bdg_fwd *)(ifp + 1);
2115f196ce38SLuigi Rizzo 	int ft_i;	/* position in the forwarding table */
2116f196ce38SLuigi Rizzo 
2117f196ce38SLuigi Rizzo 	k = ring->cur;
2118f196ce38SLuigi Rizzo 	if (k > lim)
2119f196ce38SLuigi Rizzo 		return netmap_ring_reinit(kring);
2120f196ce38SLuigi Rizzo 	if (do_lock)
2121f196ce38SLuigi Rizzo 		na->nm_lock(ifp, NETMAP_TX_LOCK, ring_nr);
2122f196ce38SLuigi Rizzo 
2123f196ce38SLuigi Rizzo 	if (netmap_bridge <= 0) { /* testing only */
2124f196ce38SLuigi Rizzo 		j = k; // used all
2125f196ce38SLuigi Rizzo 		goto done;
2126f196ce38SLuigi Rizzo 	}
2127f196ce38SLuigi Rizzo 	if (netmap_bridge > NM_BDG_BATCH)
2128f196ce38SLuigi Rizzo 		netmap_bridge = NM_BDG_BATCH;
2129f196ce38SLuigi Rizzo 
2130f196ce38SLuigi Rizzo 	ft_i = 0;	/* start from 0 */
2131f196ce38SLuigi Rizzo 	for (j = kring->nr_hwcur; likely(j != k); j = unlikely(j == lim) ? 0 : j+1) {
2132f196ce38SLuigi Rizzo 		struct netmap_slot *slot = &ring->slot[j];
2133f196ce38SLuigi Rizzo 		int len = ft[ft_i].len = slot->len;
2134f196ce38SLuigi Rizzo 		char *buf = ft[ft_i].buf = NMB(slot);
2135f196ce38SLuigi Rizzo 
2136f196ce38SLuigi Rizzo 		prefetch(buf);
2137f196ce38SLuigi Rizzo 		if (unlikely(len < 14))
2138f196ce38SLuigi Rizzo 			continue;
2139f196ce38SLuigi Rizzo 		if (unlikely(++ft_i == netmap_bridge))
2140f196ce38SLuigi Rizzo 			ft_i = nm_bdg_flush(ft, ft_i, ifp);
2141f196ce38SLuigi Rizzo 	}
2142f196ce38SLuigi Rizzo 	if (ft_i)
2143f196ce38SLuigi Rizzo 		ft_i = nm_bdg_flush(ft, ft_i, ifp);
2144f196ce38SLuigi Rizzo 	/* count how many packets we sent */
2145f196ce38SLuigi Rizzo 	i = k - j;
2146f196ce38SLuigi Rizzo 	if (i < 0)
2147f196ce38SLuigi Rizzo 		i += kring->nkr_num_slots;
2148f196ce38SLuigi Rizzo 	kring->nr_hwavail = kring->nkr_num_slots - 1 - i;
2149f196ce38SLuigi Rizzo 	if (j != k)
2150f196ce38SLuigi Rizzo 		D("early break at %d/ %d, avail %d", j, k, kring->nr_hwavail);
2151f196ce38SLuigi Rizzo 
2152f196ce38SLuigi Rizzo done:
2153f196ce38SLuigi Rizzo 	kring->nr_hwcur = j;
2154f196ce38SLuigi Rizzo 	ring->avail = kring->nr_hwavail;
2155f196ce38SLuigi Rizzo 	if (do_lock)
2156f196ce38SLuigi Rizzo 		na->nm_lock(ifp, NETMAP_TX_UNLOCK, ring_nr);
2157f196ce38SLuigi Rizzo 
2158f196ce38SLuigi Rizzo 	if (netmap_verbose)
2159f196ce38SLuigi Rizzo 		D("%s ring %d lock %d", ifp->if_xname, ring_nr, do_lock);
2160f196ce38SLuigi Rizzo 	return 0;
2161f196ce38SLuigi Rizzo }
2162f196ce38SLuigi Rizzo 
2163f196ce38SLuigi Rizzo static int
2164f196ce38SLuigi Rizzo bdg_netmap_rxsync(struct ifnet *ifp, u_int ring_nr, int do_lock)
2165f196ce38SLuigi Rizzo {
2166f196ce38SLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
2167f196ce38SLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[ring_nr];
2168f196ce38SLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
2169b3d53016SLuigi Rizzo 	u_int j, n, lim = kring->nkr_num_slots - 1;
2170f196ce38SLuigi Rizzo 	u_int k = ring->cur, resvd = ring->reserved;
2171f196ce38SLuigi Rizzo 
2172f196ce38SLuigi Rizzo 	ND("%s ring %d lock %d avail %d",
2173f196ce38SLuigi Rizzo 		ifp->if_xname, ring_nr, do_lock, kring->nr_hwavail);
2174f196ce38SLuigi Rizzo 
2175f196ce38SLuigi Rizzo 	if (k > lim)
2176f196ce38SLuigi Rizzo 		return netmap_ring_reinit(kring);
2177f196ce38SLuigi Rizzo 	if (do_lock)
2178f196ce38SLuigi Rizzo 		na->nm_lock(ifp, NETMAP_RX_LOCK, ring_nr);
2179f196ce38SLuigi Rizzo 
2180f196ce38SLuigi Rizzo 	/* skip past packets that userspace has released */
2181f196ce38SLuigi Rizzo 	j = kring->nr_hwcur;    /* netmap ring index */
2182f196ce38SLuigi Rizzo 	if (resvd > 0) {
2183f196ce38SLuigi Rizzo 		if (resvd + ring->avail >= lim + 1) {
2184f196ce38SLuigi Rizzo 			D("XXX invalid reserve/avail %d %d", resvd, ring->avail);
2185f196ce38SLuigi Rizzo 			ring->reserved = resvd = 0; // XXX panic...
2186f196ce38SLuigi Rizzo 		}
2187f196ce38SLuigi Rizzo 		k = (k >= resvd) ? k - resvd : k + lim + 1 - resvd;
2188f196ce38SLuigi Rizzo 	}
2189f196ce38SLuigi Rizzo 
2190f196ce38SLuigi Rizzo 	if (j != k) { /* userspace has released some packets. */
2191f196ce38SLuigi Rizzo 		n = k - j;
2192f196ce38SLuigi Rizzo 		if (n < 0)
2193f196ce38SLuigi Rizzo 			n += kring->nkr_num_slots;
2194f196ce38SLuigi Rizzo 		ND("userspace releases %d packets", n);
2195f196ce38SLuigi Rizzo                 for (n = 0; likely(j != k); n++) {
2196f196ce38SLuigi Rizzo                         struct netmap_slot *slot = &ring->slot[j];
2197f196ce38SLuigi Rizzo                         void *addr = NMB(slot);
2198f196ce38SLuigi Rizzo 
2199f196ce38SLuigi Rizzo                         if (addr == netmap_buffer_base) { /* bad buf */
2200f196ce38SLuigi Rizzo                                 if (do_lock)
2201f196ce38SLuigi Rizzo                                         na->nm_lock(ifp, NETMAP_RX_UNLOCK, ring_nr);
2202f196ce38SLuigi Rizzo                                 return netmap_ring_reinit(kring);
2203f196ce38SLuigi Rizzo                         }
2204f196ce38SLuigi Rizzo 			/* decrease refcount for buffer */
2205f196ce38SLuigi Rizzo 
2206f196ce38SLuigi Rizzo 			slot->flags &= ~NS_BUF_CHANGED;
2207f196ce38SLuigi Rizzo                         j = unlikely(j == lim) ? 0 : j + 1;
2208f196ce38SLuigi Rizzo                 }
2209f196ce38SLuigi Rizzo                 kring->nr_hwavail -= n;
2210f196ce38SLuigi Rizzo                 kring->nr_hwcur = k;
2211f196ce38SLuigi Rizzo         }
2212f196ce38SLuigi Rizzo         /* tell userspace that there are new packets */
2213f196ce38SLuigi Rizzo         ring->avail = kring->nr_hwavail - resvd;
2214f196ce38SLuigi Rizzo 
2215f196ce38SLuigi Rizzo 	if (do_lock)
2216f196ce38SLuigi Rizzo 		na->nm_lock(ifp, NETMAP_RX_UNLOCK, ring_nr);
2217f196ce38SLuigi Rizzo 	return 0;
2218f196ce38SLuigi Rizzo }
2219f196ce38SLuigi Rizzo 
2220f196ce38SLuigi Rizzo static void
2221f196ce38SLuigi Rizzo bdg_netmap_attach(struct ifnet *ifp)
2222f196ce38SLuigi Rizzo {
2223f196ce38SLuigi Rizzo 	struct netmap_adapter na;
2224f196ce38SLuigi Rizzo 
2225f196ce38SLuigi Rizzo 	ND("attaching virtual bridge");
2226f196ce38SLuigi Rizzo 	bzero(&na, sizeof(na));
2227f196ce38SLuigi Rizzo 
2228f196ce38SLuigi Rizzo 	na.ifp = ifp;
2229f196ce38SLuigi Rizzo 	na.separate_locks = 1;
2230f196ce38SLuigi Rizzo 	na.num_tx_desc = NM_BRIDGE_RINGSIZE;
2231f196ce38SLuigi Rizzo 	na.num_rx_desc = NM_BRIDGE_RINGSIZE;
2232f196ce38SLuigi Rizzo 	na.nm_txsync = bdg_netmap_txsync;
2233f196ce38SLuigi Rizzo 	na.nm_rxsync = bdg_netmap_rxsync;
2234f196ce38SLuigi Rizzo 	na.nm_register = bdg_netmap_reg;
2235f196ce38SLuigi Rizzo 	netmap_attach(&na, 1);
2236f196ce38SLuigi Rizzo }
2237f196ce38SLuigi Rizzo 
2238f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */
2239babc7c12SLuigi Rizzo 
2240babc7c12SLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */
2241babc7c12SLuigi Rizzo 
2242babc7c12SLuigi Rizzo 
22431a26580eSLuigi Rizzo /*
224468b8534bSLuigi Rizzo  * Module loader.
224568b8534bSLuigi Rizzo  *
224668b8534bSLuigi Rizzo  * Create the /dev/netmap device and initialize all global
224768b8534bSLuigi Rizzo  * variables.
224868b8534bSLuigi Rizzo  *
224968b8534bSLuigi Rizzo  * Return 0 on success, errno on failure.
225068b8534bSLuigi Rizzo  */
225168b8534bSLuigi Rizzo static int
225268b8534bSLuigi Rizzo netmap_init(void)
225368b8534bSLuigi Rizzo {
225468b8534bSLuigi Rizzo 	int error;
225568b8534bSLuigi Rizzo 
225668b8534bSLuigi Rizzo 	error = netmap_memory_init();
225768b8534bSLuigi Rizzo 	if (error != 0) {
225842a3a5bdSLuigi Rizzo 		printf("netmap: unable to initialize the memory allocator.\n");
225968b8534bSLuigi Rizzo 		return (error);
226068b8534bSLuigi Rizzo 	}
2261*8241616dSLuigi Rizzo 	printf("netmap: loaded module\n");
226268b8534bSLuigi Rizzo 	netmap_dev = make_dev(&netmap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660,
226368b8534bSLuigi Rizzo 			      "netmap");
2264f196ce38SLuigi Rizzo 
2265f196ce38SLuigi Rizzo #ifdef NM_BRIDGE
2266f196ce38SLuigi Rizzo 	{
2267f196ce38SLuigi Rizzo 	int i;
2268f196ce38SLuigi Rizzo 	for (i = 0; i < NM_BRIDGES; i++)
2269f196ce38SLuigi Rizzo 		mtx_init(&nm_bridges[i].bdg_lock, "bdg lock", "bdg_lock", MTX_DEF);
2270f196ce38SLuigi Rizzo 	}
2271f196ce38SLuigi Rizzo #endif
2272babc7c12SLuigi Rizzo 	return (error);
227368b8534bSLuigi Rizzo }
227468b8534bSLuigi Rizzo 
227568b8534bSLuigi Rizzo 
227668b8534bSLuigi Rizzo /*
227768b8534bSLuigi Rizzo  * Module unloader.
227868b8534bSLuigi Rizzo  *
227968b8534bSLuigi Rizzo  * Free all the memory, and destroy the ``/dev/netmap`` device.
228068b8534bSLuigi Rizzo  */
228168b8534bSLuigi Rizzo static void
228268b8534bSLuigi Rizzo netmap_fini(void)
228368b8534bSLuigi Rizzo {
228468b8534bSLuigi Rizzo 	destroy_dev(netmap_dev);
228568b8534bSLuigi Rizzo 	netmap_memory_fini();
228668b8534bSLuigi Rizzo 	printf("netmap: unloaded module.\n");
228768b8534bSLuigi Rizzo }
228868b8534bSLuigi Rizzo 
228968b8534bSLuigi Rizzo 
2290f196ce38SLuigi Rizzo #ifdef __FreeBSD__
229168b8534bSLuigi Rizzo /*
229268b8534bSLuigi Rizzo  * Kernel entry point.
229368b8534bSLuigi Rizzo  *
229468b8534bSLuigi Rizzo  * Initialize/finalize the module and return.
229568b8534bSLuigi Rizzo  *
229668b8534bSLuigi Rizzo  * Return 0 on success, errno on failure.
229768b8534bSLuigi Rizzo  */
229868b8534bSLuigi Rizzo static int
229968b8534bSLuigi Rizzo netmap_loader(__unused struct module *module, int event, __unused void *arg)
230068b8534bSLuigi Rizzo {
230168b8534bSLuigi Rizzo 	int error = 0;
230268b8534bSLuigi Rizzo 
230368b8534bSLuigi Rizzo 	switch (event) {
230468b8534bSLuigi Rizzo 	case MOD_LOAD:
230568b8534bSLuigi Rizzo 		error = netmap_init();
230668b8534bSLuigi Rizzo 		break;
230768b8534bSLuigi Rizzo 
230868b8534bSLuigi Rizzo 	case MOD_UNLOAD:
230968b8534bSLuigi Rizzo 		netmap_fini();
231068b8534bSLuigi Rizzo 		break;
231168b8534bSLuigi Rizzo 
231268b8534bSLuigi Rizzo 	default:
231368b8534bSLuigi Rizzo 		error = EOPNOTSUPP;
231468b8534bSLuigi Rizzo 		break;
231568b8534bSLuigi Rizzo 	}
231668b8534bSLuigi Rizzo 
231768b8534bSLuigi Rizzo 	return (error);
231868b8534bSLuigi Rizzo }
231968b8534bSLuigi Rizzo 
232068b8534bSLuigi Rizzo 
232168b8534bSLuigi Rizzo DEV_MODULE(netmap, netmap_loader, NULL);
2322f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */
2323