xref: /freebsd/sys/dev/netmap/netmap.c (revision f196ce3869a446952d62f57ea71b9e0663af43a5)
168b8534bSLuigi Rizzo /*
2*f196ce38SLuigi 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 
26*f196ce38SLuigi Rizzo #define NM_BRIDGE
27*f196ce38SLuigi 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 
57*f196ce38SLuigi Rizzo #ifdef linux
58*f196ce38SLuigi Rizzo #include "bsd_glue.h"
59*f196ce38SLuigi Rizzo static netdev_tx_t netmap_start_linux(struct sk_buff *skb, struct net_device *dev);
60*f196ce38SLuigi Rizzo #endif /* linux */
61*f196ce38SLuigi Rizzo #ifdef __APPLE__
62*f196ce38SLuigi Rizzo #include "osx_glue.h"
63*f196ce38SLuigi Rizzo #endif
64*f196ce38SLuigi Rizzo #ifdef __FreeBSD__
6568b8534bSLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */
6668b8534bSLuigi Rizzo __FBSDID("$FreeBSD$");
6768b8534bSLuigi Rizzo 
6868b8534bSLuigi Rizzo #include <sys/types.h>
6968b8534bSLuigi Rizzo #include <sys/module.h>
7068b8534bSLuigi Rizzo #include <sys/errno.h>
7168b8534bSLuigi Rizzo #include <sys/param.h>	/* defines used in kernel.h */
72506cc70cSLuigi Rizzo #include <sys/jail.h>
7368b8534bSLuigi Rizzo #include <sys/kernel.h>	/* types used in module initialization */
7468b8534bSLuigi Rizzo #include <sys/conf.h>	/* cdevsw struct */
7568b8534bSLuigi Rizzo #include <sys/uio.h>	/* uio struct */
7668b8534bSLuigi Rizzo #include <sys/sockio.h>
7768b8534bSLuigi Rizzo #include <sys/socketvar.h>	/* struct socket */
7868b8534bSLuigi Rizzo #include <sys/malloc.h>
7968b8534bSLuigi Rizzo #include <sys/mman.h>	/* PROT_EXEC */
8068b8534bSLuigi Rizzo #include <sys/poll.h>
81506cc70cSLuigi Rizzo #include <sys/proc.h>
8268b8534bSLuigi Rizzo #include <vm/vm.h>	/* vtophys */
8368b8534bSLuigi Rizzo #include <vm/pmap.h>	/* vtophys */
8468b8534bSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
8568b8534bSLuigi Rizzo #include <machine/bus.h>
8668b8534bSLuigi Rizzo #include <sys/selinfo.h>
8768b8534bSLuigi Rizzo #include <sys/sysctl.h>
8868b8534bSLuigi Rizzo #include <net/if.h>
8968b8534bSLuigi Rizzo #include <net/bpf.h>		/* BIOCIMMEDIATE */
90506cc70cSLuigi Rizzo #include <net/vnet.h>
9168b8534bSLuigi Rizzo #include <net/netmap.h>
9268b8534bSLuigi Rizzo #include <dev/netmap/netmap_kern.h>
9368b8534bSLuigi Rizzo #include <machine/bus.h>	/* bus_dmamap_* */
9468b8534bSLuigi Rizzo 
9568b8534bSLuigi Rizzo MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map");
96*f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */
9768b8534bSLuigi Rizzo 
9868b8534bSLuigi Rizzo /*
9968b8534bSLuigi Rizzo  * lock and unlock for the netmap memory allocator
10068b8534bSLuigi Rizzo  */
10164ae02c3SLuigi Rizzo #define NMA_LOCK()	mtx_lock(&nm_mem->nm_mtx);
10264ae02c3SLuigi Rizzo #define NMA_UNLOCK()	mtx_unlock(&nm_mem->nm_mtx);
1035819da83SLuigi Rizzo struct netmap_mem_d;
10464ae02c3SLuigi Rizzo static struct netmap_mem_d *nm_mem;	/* Our memory allocator. */
1055819da83SLuigi Rizzo 
1065819da83SLuigi Rizzo u_int netmap_total_buffers;
1075819da83SLuigi Rizzo char *netmap_buffer_base;	/* address of an invalid buffer */
1085819da83SLuigi Rizzo 
1095819da83SLuigi Rizzo /* user-controlled variables */
1105819da83SLuigi Rizzo int netmap_verbose;
1115819da83SLuigi Rizzo 
1125819da83SLuigi Rizzo static int netmap_no_timestamp; /* don't timestamp on rxsync */
1135819da83SLuigi Rizzo 
1145819da83SLuigi Rizzo SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args");
1155819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose,
1165819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode");
1175819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp,
1185819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp");
1195819da83SLuigi Rizzo int netmap_buf_size = 2048;
1205819da83SLuigi Rizzo TUNABLE_INT("hw.netmap.buf_size", &netmap_buf_size);
1215819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, buf_size,
1225819da83SLuigi Rizzo     CTLFLAG_RD, &netmap_buf_size, 0, "Size of packet buffers");
1235819da83SLuigi Rizzo int netmap_mitigate = 1;
1245819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mitigate, CTLFLAG_RW, &netmap_mitigate, 0, "");
125c85cb1a0SLuigi Rizzo int netmap_no_pendintr = 1;
1265819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr,
1275819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_pendintr, 0, "Always look for new received packets.");
1285819da83SLuigi Rizzo 
129*f196ce38SLuigi Rizzo int netmap_drop = 0;	/* debugging */
130*f196ce38SLuigi Rizzo int netmap_flags = 0;	/* debug flags */
131*f196ce38SLuigi Rizzo int netmap_copy = 0;	/* debugging, copy content */
132*f196ce38SLuigi Rizzo 
133*f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, drop, CTLFLAG_RW, &netmap_drop, 0 , "");
134*f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, flags, CTLFLAG_RW, &netmap_flags, 0 , "");
135*f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, copy, CTLFLAG_RW, &netmap_copy, 0 , "");
136*f196ce38SLuigi Rizzo 
137*f196ce38SLuigi Rizzo #ifdef NM_BRIDGE /* support for netmap bridge */
138*f196ce38SLuigi Rizzo 
139*f196ce38SLuigi Rizzo /*
140*f196ce38SLuigi Rizzo  * system parameters.
141*f196ce38SLuigi Rizzo  *
142*f196ce38SLuigi Rizzo  * All switched ports have prefix NM_NAME.
143*f196ce38SLuigi Rizzo  * The switch has a max of NM_BDG_MAXPORTS ports (often stored in a bitmap,
144*f196ce38SLuigi Rizzo  * so a practical upper bound is 64).
145*f196ce38SLuigi Rizzo  * Each tx ring is read-write, whereas rx rings are readonly (XXX not done yet).
146*f196ce38SLuigi Rizzo  * The virtual interfaces use per-queue lock instead of core lock.
147*f196ce38SLuigi Rizzo  * In the tx loop, we aggregate traffic in batches to make all operations
148*f196ce38SLuigi Rizzo  * faster. The batch size is NM_BDG_BATCH
149*f196ce38SLuigi Rizzo  */
150*f196ce38SLuigi Rizzo #define	NM_NAME			"vale"	/* prefix for the interface */
151*f196ce38SLuigi Rizzo #define NM_BDG_MAXPORTS		16	/* up to 64 ? */
152*f196ce38SLuigi Rizzo #define NM_BRIDGE_RINGSIZE	1024	/* in the device */
153*f196ce38SLuigi Rizzo #define NM_BDG_HASH		1024	/* forwarding table entries */
154*f196ce38SLuigi Rizzo #define NM_BDG_BATCH		1024	/* entries in the forwarding buffer */
155*f196ce38SLuigi Rizzo #define	NM_BRIDGES		4	/* number of bridges */
156*f196ce38SLuigi Rizzo int netmap_bridge = NM_BDG_BATCH; /* bridge batch size */
157*f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, bridge, CTLFLAG_RW, &netmap_bridge, 0 , "");
158*f196ce38SLuigi Rizzo #ifdef linux
159*f196ce38SLuigi Rizzo #define	ADD_BDG_REF(ifp)	(NA(ifp)->if_refcount++)
160*f196ce38SLuigi Rizzo #define	DROP_BDG_REF(ifp)	(NA(ifp)->if_refcount-- <= 1)
161*f196ce38SLuigi Rizzo #else /* !linux */
162*f196ce38SLuigi Rizzo #define	ADD_BDG_REF(ifp)	(ifp)->if_refcount++
163*f196ce38SLuigi Rizzo #define	DROP_BDG_REF(ifp)	refcount_release(&(ifp)->if_refcount)
164*f196ce38SLuigi Rizzo #ifdef __FreeBSD__
165*f196ce38SLuigi Rizzo #include <sys/endian.h>
166*f196ce38SLuigi Rizzo #include <sys/refcount.h>
167*f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */
168*f196ce38SLuigi Rizzo #endif /* !linux */
169*f196ce38SLuigi Rizzo 
170*f196ce38SLuigi Rizzo static void bdg_netmap_attach(struct ifnet *ifp);
171*f196ce38SLuigi Rizzo static int bdg_netmap_reg(struct ifnet *ifp, int onoff);
172*f196ce38SLuigi Rizzo /* per-tx-queue entry */
173*f196ce38SLuigi Rizzo struct nm_bdg_fwd {	/* forwarding entry for a bridge */
174*f196ce38SLuigi Rizzo 	void *buf;
175*f196ce38SLuigi Rizzo 	uint64_t dst;	/* dst mask */
176*f196ce38SLuigi Rizzo 	uint32_t src;	/* src index ? */
177*f196ce38SLuigi Rizzo 	uint16_t len;	/* src len */
178*f196ce38SLuigi Rizzo #if 0
179*f196ce38SLuigi Rizzo 	uint64_t src_mac;	/* ignore 2 MSBytes */
180*f196ce38SLuigi Rizzo 	uint64_t dst_mac;	/* ignore 2 MSBytes */
181*f196ce38SLuigi Rizzo 	uint32_t dst_idx;	/* dst index in fwd table */
182*f196ce38SLuigi Rizzo 	uint32_t dst_buf;	/* where we copy to */
183*f196ce38SLuigi Rizzo #endif
184*f196ce38SLuigi Rizzo };
185*f196ce38SLuigi Rizzo 
186*f196ce38SLuigi Rizzo struct nm_hash_ent {
187*f196ce38SLuigi Rizzo 	uint64_t	mac;	/* the top 2 bytes are the epoch */
188*f196ce38SLuigi Rizzo 	uint64_t	ports;
189*f196ce38SLuigi Rizzo };
190*f196ce38SLuigi Rizzo 
191*f196ce38SLuigi Rizzo /*
192*f196ce38SLuigi Rizzo  * Interfaces for a bridge are all in ports[].
193*f196ce38SLuigi Rizzo  * The array has fixed size, an empty entry does not terminate
194*f196ce38SLuigi Rizzo  * the search.
195*f196ce38SLuigi Rizzo  */
196*f196ce38SLuigi Rizzo struct nm_bridge {
197*f196ce38SLuigi Rizzo 	struct ifnet *bdg_ports[NM_BDG_MAXPORTS];
198*f196ce38SLuigi Rizzo 	int n_ports;
199*f196ce38SLuigi Rizzo 	uint64_t act_ports;
200*f196ce38SLuigi Rizzo 	int freelist;	/* first buffer index */
201*f196ce38SLuigi Rizzo 	NM_SELINFO_T si;	/* poll/select wait queue */
202*f196ce38SLuigi Rizzo 	NM_LOCK_T bdg_lock;	/* protect the selinfo ? */
203*f196ce38SLuigi Rizzo 
204*f196ce38SLuigi Rizzo 	/* the forwarding table, MAC+ports */
205*f196ce38SLuigi Rizzo 	struct nm_hash_ent ht[NM_BDG_HASH];
206*f196ce38SLuigi Rizzo 
207*f196ce38SLuigi Rizzo 	int namelen;	/* 0 means free */
208*f196ce38SLuigi Rizzo 	char basename[IFNAMSIZ];
209*f196ce38SLuigi Rizzo };
210*f196ce38SLuigi Rizzo 
211*f196ce38SLuigi Rizzo struct nm_bridge nm_bridges[NM_BRIDGES];
212*f196ce38SLuigi Rizzo 
213*f196ce38SLuigi Rizzo #define BDG_LOCK(b)	mtx_lock(&(b)->bdg_lock)
214*f196ce38SLuigi Rizzo #define BDG_UNLOCK(b)	mtx_unlock(&(b)->bdg_lock)
215*f196ce38SLuigi Rizzo 
216*f196ce38SLuigi Rizzo /*
217*f196ce38SLuigi Rizzo  * NA(ifp)->bdg_port	port index
218*f196ce38SLuigi Rizzo  */
219*f196ce38SLuigi Rizzo 
220*f196ce38SLuigi Rizzo #ifndef linux
221*f196ce38SLuigi Rizzo static inline void prefetch (const void *x)
222*f196ce38SLuigi Rizzo {
223*f196ce38SLuigi Rizzo         __asm volatile("prefetcht0 %0" :: "m" (*(const unsigned long *)x));
224*f196ce38SLuigi Rizzo }
225*f196ce38SLuigi Rizzo #endif /* !linux */
226*f196ce38SLuigi Rizzo 
227*f196ce38SLuigi Rizzo // XXX only for multiples of 64 bytes, non overlapped.
228*f196ce38SLuigi Rizzo static inline void
229*f196ce38SLuigi Rizzo pkt_copy(void *_src, void *_dst, int l)
230*f196ce38SLuigi Rizzo {
231*f196ce38SLuigi Rizzo         uint64_t *src = _src;
232*f196ce38SLuigi Rizzo         uint64_t *dst = _dst;
233*f196ce38SLuigi Rizzo         if (unlikely(l >= 1024)) {
234*f196ce38SLuigi Rizzo                 bcopy(src, dst, l);
235*f196ce38SLuigi Rizzo                 return;
236*f196ce38SLuigi Rizzo         }
237*f196ce38SLuigi Rizzo         for (; likely(l > 0); l-=64) {
238*f196ce38SLuigi Rizzo                 *dst++ = *src++;
239*f196ce38SLuigi Rizzo                 *dst++ = *src++;
240*f196ce38SLuigi Rizzo                 *dst++ = *src++;
241*f196ce38SLuigi Rizzo                 *dst++ = *src++;
242*f196ce38SLuigi Rizzo                 *dst++ = *src++;
243*f196ce38SLuigi Rizzo                 *dst++ = *src++;
244*f196ce38SLuigi Rizzo                 *dst++ = *src++;
245*f196ce38SLuigi Rizzo                 *dst++ = *src++;
246*f196ce38SLuigi Rizzo         }
247*f196ce38SLuigi Rizzo }
248*f196ce38SLuigi Rizzo 
249*f196ce38SLuigi Rizzo /*
250*f196ce38SLuigi Rizzo  * locate a bridge among the existing ones.
251*f196ce38SLuigi Rizzo  * a ':' in the name terminates the bridge name. Otherwise, just NM_NAME.
252*f196ce38SLuigi Rizzo  * We assume that this is called with a name of at least NM_NAME chars.
253*f196ce38SLuigi Rizzo  */
254*f196ce38SLuigi Rizzo static struct nm_bridge *
255*f196ce38SLuigi Rizzo nm_find_bridge(const char *name)
256*f196ce38SLuigi Rizzo {
257*f196ce38SLuigi Rizzo 	int i, l, namelen, e;
258*f196ce38SLuigi Rizzo 	struct nm_bridge *b = NULL;
259*f196ce38SLuigi Rizzo 
260*f196ce38SLuigi Rizzo 	namelen = strlen(NM_NAME);	/* base length */
261*f196ce38SLuigi Rizzo 	l = strlen(name);		/* actual length */
262*f196ce38SLuigi Rizzo 	for (i = namelen + 1; i < l; i++) {
263*f196ce38SLuigi Rizzo 		if (name[i] == ':') {
264*f196ce38SLuigi Rizzo 			namelen = i;
265*f196ce38SLuigi Rizzo 			break;
266*f196ce38SLuigi Rizzo 		}
267*f196ce38SLuigi Rizzo 	}
268*f196ce38SLuigi Rizzo 	if (namelen >= IFNAMSIZ)
269*f196ce38SLuigi Rizzo 		namelen = IFNAMSIZ;
270*f196ce38SLuigi Rizzo 	ND("--- prefix is '%.*s' ---", namelen, name);
271*f196ce38SLuigi Rizzo 
272*f196ce38SLuigi Rizzo 	/* use the first entry for locking */
273*f196ce38SLuigi Rizzo 	BDG_LOCK(nm_bridges); // XXX do better
274*f196ce38SLuigi Rizzo 	for (e = -1, i = 1; i < NM_BRIDGES; i++) {
275*f196ce38SLuigi Rizzo 		b = nm_bridges + i;
276*f196ce38SLuigi Rizzo 		if (b->namelen == 0)
277*f196ce38SLuigi Rizzo 			e = i;	/* record empty slot */
278*f196ce38SLuigi Rizzo 		else if (strncmp(name, b->basename, namelen) == 0) {
279*f196ce38SLuigi Rizzo 			ND("found '%.*s' at %d", namelen, name, i);
280*f196ce38SLuigi Rizzo 			break;
281*f196ce38SLuigi Rizzo 		}
282*f196ce38SLuigi Rizzo 	}
283*f196ce38SLuigi Rizzo 	if (i == NM_BRIDGES) { /* all full */
284*f196ce38SLuigi Rizzo 		if (e == -1) { /* no empty slot */
285*f196ce38SLuigi Rizzo 			b = NULL;
286*f196ce38SLuigi Rizzo 		} else {
287*f196ce38SLuigi Rizzo 			b = nm_bridges + e;
288*f196ce38SLuigi Rizzo 			strncpy(b->basename, name, namelen);
289*f196ce38SLuigi Rizzo 			b->namelen = namelen;
290*f196ce38SLuigi Rizzo 		}
291*f196ce38SLuigi Rizzo 	}
292*f196ce38SLuigi Rizzo 	BDG_UNLOCK(nm_bridges);
293*f196ce38SLuigi Rizzo 	return b;
294*f196ce38SLuigi Rizzo }
295*f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */
2965819da83SLuigi Rizzo 
2973c0caf6cSLuigi Rizzo /*------------- memory allocator -----------------*/
2983c0caf6cSLuigi Rizzo #ifdef NETMAP_MEM2
2993c0caf6cSLuigi Rizzo #include "netmap_mem2.c"
3003c0caf6cSLuigi Rizzo #else /* !NETMAP_MEM2 */
3013c0caf6cSLuigi Rizzo #include "netmap_mem1.c"
3023c0caf6cSLuigi Rizzo #endif /* !NETMAP_MEM2 */
3033c0caf6cSLuigi Rizzo /*------------ end of memory allocator ----------*/
30468b8534bSLuigi Rizzo 
30568b8534bSLuigi Rizzo /* Structure associated to each thread which registered an interface. */
30668b8534bSLuigi Rizzo struct netmap_priv_d {
30768b8534bSLuigi Rizzo 	struct netmap_if *np_nifp;	/* netmap interface descriptor. */
30868b8534bSLuigi Rizzo 
30968b8534bSLuigi Rizzo 	struct ifnet	*np_ifp;	/* device for which we hold a reference */
31068b8534bSLuigi Rizzo 	int		np_ringid;	/* from the ioctl */
31168b8534bSLuigi Rizzo 	u_int		np_qfirst, np_qlast;	/* range of rings to scan */
31268b8534bSLuigi Rizzo 	uint16_t	np_txpoll;
31368b8534bSLuigi Rizzo };
31468b8534bSLuigi Rizzo 
31568b8534bSLuigi Rizzo 
31668b8534bSLuigi Rizzo /*
31768b8534bSLuigi Rizzo  * File descriptor's private data destructor.
31868b8534bSLuigi Rizzo  *
31968b8534bSLuigi Rizzo  * Call nm_register(ifp,0) to stop netmap mode on the interface and
32068b8534bSLuigi Rizzo  * revert to normal operation. We expect that np_ifp has not gone.
32168b8534bSLuigi Rizzo  */
32268b8534bSLuigi Rizzo static void
3235819da83SLuigi Rizzo netmap_dtor_locked(void *data)
32468b8534bSLuigi Rizzo {
32568b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = data;
32668b8534bSLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
32768b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
32868b8534bSLuigi Rizzo 	struct netmap_if *nifp = priv->np_nifp;
32968b8534bSLuigi Rizzo 
33068b8534bSLuigi Rizzo 	na->refcount--;
33168b8534bSLuigi Rizzo 	if (na->refcount <= 0) {	/* last instance */
33264ae02c3SLuigi Rizzo 		u_int i, j, lim;
33368b8534bSLuigi Rizzo 
33468b8534bSLuigi Rizzo 		D("deleting last netmap instance for %s", ifp->if_xname);
33568b8534bSLuigi Rizzo 		/*
33668b8534bSLuigi Rizzo 		 * there is a race here with *_netmap_task() and
3371a26580eSLuigi Rizzo 		 * netmap_poll(), which don't run under NETMAP_REG_LOCK.
33868b8534bSLuigi Rizzo 		 * na->refcount == 0 && na->ifp->if_capenable & IFCAP_NETMAP
33968b8534bSLuigi Rizzo 		 * (aka NETMAP_DELETING(na)) are a unique marker that the
34068b8534bSLuigi Rizzo 		 * device is dying.
34168b8534bSLuigi Rizzo 		 * Before destroying stuff we sleep a bit, and then complete
34268b8534bSLuigi Rizzo 		 * the job. NIOCREG should realize the condition and
34368b8534bSLuigi Rizzo 		 * loop until they can continue; the other routines
34468b8534bSLuigi Rizzo 		 * should check the condition at entry and quit if
34568b8534bSLuigi Rizzo 		 * they cannot run.
34668b8534bSLuigi Rizzo 		 */
3471a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
34868b8534bSLuigi Rizzo 		tsleep(na, 0, "NIOCUNREG", 4);
3491a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
35068b8534bSLuigi Rizzo 		na->nm_register(ifp, 0); /* off, clear IFCAP_NETMAP */
35168b8534bSLuigi Rizzo 		/* Wake up any sleeping threads. netmap_poll will
35268b8534bSLuigi Rizzo 		 * then return POLLERR
35368b8534bSLuigi Rizzo 		 */
354d76bf4ffSLuigi Rizzo 		for (i = 0; i < na->num_tx_rings + 1; i++)
35568b8534bSLuigi Rizzo 			selwakeuppri(&na->tx_rings[i].si, PI_NET);
356d76bf4ffSLuigi Rizzo 		for (i = 0; i < na->num_rx_rings + 1; i++)
35768b8534bSLuigi Rizzo 			selwakeuppri(&na->rx_rings[i].si, PI_NET);
35864ae02c3SLuigi Rizzo 		selwakeuppri(&na->tx_si, PI_NET);
35964ae02c3SLuigi Rizzo 		selwakeuppri(&na->rx_si, PI_NET);
36068b8534bSLuigi Rizzo 		/* release all buffers */
36168b8534bSLuigi Rizzo 		NMA_LOCK();
362d76bf4ffSLuigi Rizzo 		for (i = 0; i < na->num_tx_rings + 1; i++) {
36364ae02c3SLuigi Rizzo 			struct netmap_ring *ring = na->tx_rings[i].ring;
36468b8534bSLuigi Rizzo 			lim = na->tx_rings[i].nkr_num_slots;
36568b8534bSLuigi Rizzo 			for (j = 0; j < lim; j++)
366446ee301SLuigi Rizzo 				netmap_free_buf(nifp, ring->slot[j].buf_idx);
36764ae02c3SLuigi Rizzo 		}
368d76bf4ffSLuigi Rizzo 		for (i = 0; i < na->num_rx_rings + 1; i++) {
36964ae02c3SLuigi Rizzo 			struct netmap_ring *ring = na->rx_rings[i].ring;
37068b8534bSLuigi Rizzo 			lim = na->rx_rings[i].nkr_num_slots;
37168b8534bSLuigi Rizzo 			for (j = 0; j < lim; j++)
372446ee301SLuigi Rizzo 				netmap_free_buf(nifp, ring->slot[j].buf_idx);
37368b8534bSLuigi Rizzo 		}
37468b8534bSLuigi Rizzo 		NMA_UNLOCK();
3756e10c8b8SLuigi Rizzo 		netmap_free_rings(na);
37668b8534bSLuigi Rizzo 		wakeup(na);
37768b8534bSLuigi Rizzo 	}
3786e10c8b8SLuigi Rizzo 	netmap_if_free(nifp);
3795819da83SLuigi Rizzo }
38068b8534bSLuigi Rizzo 
381*f196ce38SLuigi Rizzo static void
382*f196ce38SLuigi Rizzo nm_if_rele(struct ifnet *ifp)
383*f196ce38SLuigi Rizzo {
384*f196ce38SLuigi Rizzo #ifndef NM_BRIDGE
385*f196ce38SLuigi Rizzo 	if_rele(ifp);
386*f196ce38SLuigi Rizzo #else /* NM_BRIDGE */
387*f196ce38SLuigi Rizzo 	int i, full;
388*f196ce38SLuigi Rizzo 	struct nm_bridge *b;
389*f196ce38SLuigi Rizzo 
390*f196ce38SLuigi Rizzo 	if (strncmp(ifp->if_xname, NM_NAME, sizeof(NM_NAME) - 1)) {
391*f196ce38SLuigi Rizzo 		if_rele(ifp);
392*f196ce38SLuigi Rizzo 		return;
393*f196ce38SLuigi Rizzo 	}
394*f196ce38SLuigi Rizzo 	if (!DROP_BDG_REF(ifp))
395*f196ce38SLuigi Rizzo 		return;
396*f196ce38SLuigi Rizzo 	b = ifp->if_bridge;
397*f196ce38SLuigi Rizzo 	BDG_LOCK(nm_bridges);
398*f196ce38SLuigi Rizzo 	BDG_LOCK(b);
399*f196ce38SLuigi Rizzo 	ND("want to disconnect %s from the bridge", ifp->if_xname);
400*f196ce38SLuigi Rizzo 	full = 0;
401*f196ce38SLuigi Rizzo 	for (i = 0; i < NM_BDG_MAXPORTS; i++) {
402*f196ce38SLuigi Rizzo 		if (b->bdg_ports[i] == ifp) {
403*f196ce38SLuigi Rizzo 			b->bdg_ports[i] = NULL;
404*f196ce38SLuigi Rizzo 			bzero(ifp, sizeof(*ifp));
405*f196ce38SLuigi Rizzo 			free(ifp, M_DEVBUF);
406*f196ce38SLuigi Rizzo 			break;
407*f196ce38SLuigi Rizzo 		}
408*f196ce38SLuigi Rizzo 		else if (b->bdg_ports[i] != NULL)
409*f196ce38SLuigi Rizzo 			full = 1;
410*f196ce38SLuigi Rizzo 	}
411*f196ce38SLuigi Rizzo 	BDG_UNLOCK(b);
412*f196ce38SLuigi Rizzo 	if (full == 0) {
413*f196ce38SLuigi Rizzo 		ND("freeing bridge %d", b - nm_bridges);
414*f196ce38SLuigi Rizzo 		b->namelen = 0;
415*f196ce38SLuigi Rizzo 	}
416*f196ce38SLuigi Rizzo 	BDG_UNLOCK(nm_bridges);
417*f196ce38SLuigi Rizzo 	if (i == NM_BDG_MAXPORTS)
418*f196ce38SLuigi Rizzo 		D("ouch, cannot find ifp to remove");
419*f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */
420*f196ce38SLuigi Rizzo }
4215819da83SLuigi Rizzo 
4225819da83SLuigi Rizzo static void
4235819da83SLuigi Rizzo netmap_dtor(void *data)
4245819da83SLuigi Rizzo {
4255819da83SLuigi Rizzo 	struct netmap_priv_d *priv = data;
4265819da83SLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
4275819da83SLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
4285819da83SLuigi Rizzo 
4291a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
4305819da83SLuigi Rizzo 	netmap_dtor_locked(data);
4311a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
43268b8534bSLuigi Rizzo 
433*f196ce38SLuigi Rizzo 	nm_if_rele(ifp);
43468b8534bSLuigi Rizzo 	bzero(priv, sizeof(*priv));	/* XXX for safety */
43568b8534bSLuigi Rizzo 	free(priv, M_DEVBUF);
43668b8534bSLuigi Rizzo }
43768b8534bSLuigi Rizzo 
43868b8534bSLuigi Rizzo 
43968b8534bSLuigi Rizzo /*
44068b8534bSLuigi Rizzo  * mmap(2) support for the "netmap" device.
44168b8534bSLuigi Rizzo  *
44268b8534bSLuigi Rizzo  * Expose all the memory previously allocated by our custom memory
44368b8534bSLuigi Rizzo  * allocator: this way the user has only to issue a single mmap(2), and
44468b8534bSLuigi Rizzo  * can work on all the data structures flawlessly.
44568b8534bSLuigi Rizzo  *
44668b8534bSLuigi Rizzo  * Return 0 on success, -1 otherwise.
44768b8534bSLuigi Rizzo  */
448babc7c12SLuigi Rizzo 
449*f196ce38SLuigi Rizzo #ifdef __FreeBSD__
45068b8534bSLuigi Rizzo static int
451babc7c12SLuigi Rizzo netmap_mmap(__unused struct cdev *dev,
45268b8534bSLuigi Rizzo #if __FreeBSD_version < 900000
453babc7c12SLuigi Rizzo 		vm_offset_t offset, vm_paddr_t *paddr, int nprot
45468b8534bSLuigi Rizzo #else
455babc7c12SLuigi Rizzo 		vm_ooffset_t offset, vm_paddr_t *paddr, int nprot,
456babc7c12SLuigi Rizzo 		__unused vm_memattr_t *memattr
45768b8534bSLuigi Rizzo #endif
458babc7c12SLuigi Rizzo 	)
45968b8534bSLuigi Rizzo {
46068b8534bSLuigi Rizzo 	if (nprot & PROT_EXEC)
46168b8534bSLuigi Rizzo 		return (-1);	// XXX -1 or EINVAL ?
462446ee301SLuigi Rizzo 
46368b8534bSLuigi Rizzo 	ND("request for offset 0x%x", (uint32_t)offset);
464446ee301SLuigi Rizzo 	*paddr = netmap_ofstophys(offset);
46568b8534bSLuigi Rizzo 
46668b8534bSLuigi Rizzo 	return (0);
46768b8534bSLuigi Rizzo }
468*f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */
46968b8534bSLuigi Rizzo 
47068b8534bSLuigi Rizzo 
47168b8534bSLuigi Rizzo /*
47202ad4083SLuigi Rizzo  * Handlers for synchronization of the queues from/to the host.
47302ad4083SLuigi Rizzo  *
47402ad4083SLuigi Rizzo  * netmap_sync_to_host() passes packets up. We are called from a
47502ad4083SLuigi Rizzo  * system call in user process context, and the only contention
47602ad4083SLuigi Rizzo  * can be among multiple user threads erroneously calling
47702ad4083SLuigi Rizzo  * this routine concurrently. In principle we should not even
47802ad4083SLuigi Rizzo  * need to lock.
47968b8534bSLuigi Rizzo  */
48068b8534bSLuigi Rizzo static void
48168b8534bSLuigi Rizzo netmap_sync_to_host(struct netmap_adapter *na)
48268b8534bSLuigi Rizzo {
483d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->tx_rings[na->num_tx_rings];
48468b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
48568b8534bSLuigi Rizzo 	struct mbuf *head = NULL, *tail = NULL, *m;
48602ad4083SLuigi Rizzo 	u_int k, n, lim = kring->nkr_num_slots - 1;
48768b8534bSLuigi Rizzo 
48802ad4083SLuigi Rizzo 	k = ring->cur;
48902ad4083SLuigi Rizzo 	if (k > lim) {
49002ad4083SLuigi Rizzo 		netmap_ring_reinit(kring);
49102ad4083SLuigi Rizzo 		return;
49202ad4083SLuigi Rizzo 	}
4931a26580eSLuigi Rizzo 	// na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0);
49468b8534bSLuigi Rizzo 
49568b8534bSLuigi Rizzo 	/* Take packets from hwcur to cur and pass them up.
49668b8534bSLuigi Rizzo 	 * In case of no buffers we give up. At the end of the loop,
49768b8534bSLuigi Rizzo 	 * the queue is drained in all cases.
49868b8534bSLuigi Rizzo 	 */
49902ad4083SLuigi Rizzo 	for (n = kring->nr_hwcur; n != k;) {
50068b8534bSLuigi Rizzo 		struct netmap_slot *slot = &ring->slot[n];
50168b8534bSLuigi Rizzo 
50268b8534bSLuigi Rizzo 		n = (n == lim) ? 0 : n + 1;
50368b8534bSLuigi Rizzo 		if (slot->len < 14 || slot->len > NETMAP_BUF_SIZE) {
50468b8534bSLuigi Rizzo 			D("bad pkt at %d len %d", n, slot->len);
50568b8534bSLuigi Rizzo 			continue;
50668b8534bSLuigi Rizzo 		}
50768b8534bSLuigi Rizzo 		m = m_devget(NMB(slot), slot->len, 0, na->ifp, NULL);
50868b8534bSLuigi Rizzo 
50968b8534bSLuigi Rizzo 		if (m == NULL)
51068b8534bSLuigi Rizzo 			break;
51168b8534bSLuigi Rizzo 		if (tail)
51268b8534bSLuigi Rizzo 			tail->m_nextpkt = m;
51368b8534bSLuigi Rizzo 		else
51468b8534bSLuigi Rizzo 			head = m;
51568b8534bSLuigi Rizzo 		tail = m;
51668b8534bSLuigi Rizzo 		m->m_nextpkt = NULL;
51768b8534bSLuigi Rizzo 	}
51802ad4083SLuigi Rizzo 	kring->nr_hwcur = k;
51968b8534bSLuigi Rizzo 	kring->nr_hwavail = ring->avail = lim;
5201a26580eSLuigi Rizzo 	// na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0);
52168b8534bSLuigi Rizzo 
52268b8534bSLuigi Rizzo 	/* send packets up, outside the lock */
52368b8534bSLuigi Rizzo 	while ((m = head) != NULL) {
52468b8534bSLuigi Rizzo 		head = head->m_nextpkt;
52568b8534bSLuigi Rizzo 		m->m_nextpkt = NULL;
52668b8534bSLuigi Rizzo 		if (netmap_verbose & NM_VERB_HOST)
5271a26580eSLuigi Rizzo 			D("sending up pkt %p size %d", m, MBUF_LEN(m));
5281a26580eSLuigi Rizzo 		NM_SEND_UP(na->ifp, m);
52968b8534bSLuigi Rizzo 	}
53068b8534bSLuigi Rizzo }
53168b8534bSLuigi Rizzo 
53268b8534bSLuigi Rizzo /*
53302ad4083SLuigi Rizzo  * rxsync backend for packets coming from the host stack.
53402ad4083SLuigi Rizzo  * They have been put in the queue by netmap_start() so we
53502ad4083SLuigi Rizzo  * need to protect access to the kring using a lock.
53602ad4083SLuigi Rizzo  *
53768b8534bSLuigi Rizzo  * This routine also does the selrecord if called from the poll handler
53868b8534bSLuigi Rizzo  * (we know because td != NULL).
53968b8534bSLuigi Rizzo  */
54068b8534bSLuigi Rizzo static void
54168b8534bSLuigi Rizzo netmap_sync_from_host(struct netmap_adapter *na, struct thread *td)
54268b8534bSLuigi Rizzo {
543d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
54468b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
54564ae02c3SLuigi Rizzo 	u_int j, n, lim = kring->nkr_num_slots;
54664ae02c3SLuigi Rizzo 	u_int k = ring->cur, resvd = ring->reserved;
54768b8534bSLuigi Rizzo 
5481a26580eSLuigi Rizzo 	na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0);
54964ae02c3SLuigi Rizzo 	if (k >= lim) {
55064ae02c3SLuigi Rizzo 		netmap_ring_reinit(kring);
55164ae02c3SLuigi Rizzo 		return;
55264ae02c3SLuigi Rizzo 	}
55364ae02c3SLuigi Rizzo 	/* new packets are already set in nr_hwavail */
55464ae02c3SLuigi Rizzo 	/* skip past packets that userspace has released */
55564ae02c3SLuigi Rizzo 	j = kring->nr_hwcur;
55664ae02c3SLuigi Rizzo 	if (resvd > 0) {
55764ae02c3SLuigi Rizzo 		if (resvd + ring->avail >= lim + 1) {
55864ae02c3SLuigi Rizzo 			D("XXX invalid reserve/avail %d %d", resvd, ring->avail);
55964ae02c3SLuigi Rizzo 			ring->reserved = resvd = 0; // XXX panic...
56064ae02c3SLuigi Rizzo 		}
56164ae02c3SLuigi Rizzo 		k = (k >= resvd) ? k - resvd : k + lim - resvd;
56264ae02c3SLuigi Rizzo         }
56364ae02c3SLuigi Rizzo 	if (j != k) {
56464ae02c3SLuigi Rizzo 		n = k >= j ? k - j : k + lim - j;
56564ae02c3SLuigi Rizzo 		kring->nr_hwavail -= n;
56602ad4083SLuigi Rizzo 		kring->nr_hwcur = k;
56764ae02c3SLuigi Rizzo 	}
56864ae02c3SLuigi Rizzo 	k = ring->avail = kring->nr_hwavail - resvd;
56902ad4083SLuigi Rizzo 	if (k == 0 && td)
57068b8534bSLuigi Rizzo 		selrecord(td, &kring->si);
57102ad4083SLuigi Rizzo 	if (k && (netmap_verbose & NM_VERB_HOST))
57202ad4083SLuigi Rizzo 		D("%d pkts from stack", k);
5731a26580eSLuigi Rizzo 	na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0);
57468b8534bSLuigi Rizzo }
57568b8534bSLuigi Rizzo 
57668b8534bSLuigi Rizzo 
57768b8534bSLuigi Rizzo /*
57868b8534bSLuigi Rizzo  * get a refcounted reference to an interface.
57968b8534bSLuigi Rizzo  * Return ENXIO if the interface does not exist, EINVAL if netmap
58068b8534bSLuigi Rizzo  * is not supported by the interface.
58168b8534bSLuigi Rizzo  * If successful, hold a reference.
58268b8534bSLuigi Rizzo  */
58368b8534bSLuigi Rizzo static int
58468b8534bSLuigi Rizzo get_ifp(const char *name, struct ifnet **ifp)
58568b8534bSLuigi Rizzo {
586*f196ce38SLuigi Rizzo #ifdef NM_BRIDGE
587*f196ce38SLuigi Rizzo 	struct ifnet *iter = NULL;
588*f196ce38SLuigi Rizzo 
589*f196ce38SLuigi Rizzo 	do {
590*f196ce38SLuigi Rizzo 		struct nm_bridge *b;
591*f196ce38SLuigi Rizzo 		int i, l, cand = -1;
592*f196ce38SLuigi Rizzo 
593*f196ce38SLuigi Rizzo 		if (strncmp(name, NM_NAME, sizeof(NM_NAME) - 1))
594*f196ce38SLuigi Rizzo 			break;
595*f196ce38SLuigi Rizzo 		b = nm_find_bridge(name);
596*f196ce38SLuigi Rizzo 		if (b == NULL) {
597*f196ce38SLuigi Rizzo 			D("no bridges available for '%s'", name);
598*f196ce38SLuigi Rizzo 			return (ENXIO);
599*f196ce38SLuigi Rizzo 		}
600*f196ce38SLuigi Rizzo 		/* XXX locking */
601*f196ce38SLuigi Rizzo 		BDG_LOCK(b);
602*f196ce38SLuigi Rizzo 		/* lookup in the local list of ports */
603*f196ce38SLuigi Rizzo 		for (i = 0; i < NM_BDG_MAXPORTS; i++) {
604*f196ce38SLuigi Rizzo 			iter = b->bdg_ports[i];
605*f196ce38SLuigi Rizzo 			if (iter == NULL) {
606*f196ce38SLuigi Rizzo 				if (cand == -1)
607*f196ce38SLuigi Rizzo 					cand = i; /* potential insert point */
608*f196ce38SLuigi Rizzo 				continue;
609*f196ce38SLuigi Rizzo 			}
610*f196ce38SLuigi Rizzo 			if (!strcmp(iter->if_xname, name)) {
611*f196ce38SLuigi Rizzo 				ADD_BDG_REF(iter);
612*f196ce38SLuigi Rizzo 				ND("found existing interface");
613*f196ce38SLuigi Rizzo 				BDG_UNLOCK(b);
614*f196ce38SLuigi Rizzo 				break;
615*f196ce38SLuigi Rizzo 			}
616*f196ce38SLuigi Rizzo 		}
617*f196ce38SLuigi Rizzo 		if (i < NM_BDG_MAXPORTS) /* already unlocked */
618*f196ce38SLuigi Rizzo 			break;
619*f196ce38SLuigi Rizzo 		if (cand == -1) {
620*f196ce38SLuigi Rizzo 			D("bridge full, cannot create new port");
621*f196ce38SLuigi Rizzo no_port:
622*f196ce38SLuigi Rizzo 			BDG_UNLOCK(b);
623*f196ce38SLuigi Rizzo 			*ifp = NULL;
624*f196ce38SLuigi Rizzo 			return EINVAL;
625*f196ce38SLuigi Rizzo 		}
626*f196ce38SLuigi Rizzo 		ND("create new bridge port %s", name);
627*f196ce38SLuigi Rizzo 		/* space for forwarding list after the ifnet */
628*f196ce38SLuigi Rizzo 		l = sizeof(*iter) +
629*f196ce38SLuigi Rizzo 			 sizeof(struct nm_bdg_fwd)*NM_BDG_BATCH ;
630*f196ce38SLuigi Rizzo 		iter = malloc(l, M_DEVBUF, M_NOWAIT | M_ZERO);
631*f196ce38SLuigi Rizzo 		if (!iter)
632*f196ce38SLuigi Rizzo 			goto no_port;
633*f196ce38SLuigi Rizzo 		strcpy(iter->if_xname, name);
634*f196ce38SLuigi Rizzo 		bdg_netmap_attach(iter);
635*f196ce38SLuigi Rizzo 		b->bdg_ports[cand] = iter;
636*f196ce38SLuigi Rizzo 		iter->if_bridge = b;
637*f196ce38SLuigi Rizzo 		ADD_BDG_REF(iter);
638*f196ce38SLuigi Rizzo 		BDG_UNLOCK(b);
639*f196ce38SLuigi Rizzo 		ND("attaching virtual bridge %p", b);
640*f196ce38SLuigi Rizzo 	} while (0);
641*f196ce38SLuigi Rizzo 	*ifp = iter;
642*f196ce38SLuigi Rizzo 	if (! *ifp)
643*f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */
64468b8534bSLuigi Rizzo 	*ifp = ifunit_ref(name);
64568b8534bSLuigi Rizzo 	if (*ifp == NULL)
64668b8534bSLuigi Rizzo 		return (ENXIO);
64768b8534bSLuigi Rizzo 	/* can do this if the capability exists and if_pspare[0]
64868b8534bSLuigi Rizzo 	 * points to the netmap descriptor.
64968b8534bSLuigi Rizzo 	 */
65068b8534bSLuigi Rizzo 	if ((*ifp)->if_capabilities & IFCAP_NETMAP && NA(*ifp))
65168b8534bSLuigi Rizzo 		return 0;	/* valid pointer, we hold the refcount */
652*f196ce38SLuigi Rizzo 	nm_if_rele(*ifp);
65368b8534bSLuigi Rizzo 	return EINVAL;	// not NETMAP capable
65468b8534bSLuigi Rizzo }
65568b8534bSLuigi Rizzo 
65668b8534bSLuigi Rizzo 
65768b8534bSLuigi Rizzo /*
65868b8534bSLuigi Rizzo  * Error routine called when txsync/rxsync detects an error.
65968b8534bSLuigi Rizzo  * Can't do much more than resetting cur = hwcur, avail = hwavail.
66068b8534bSLuigi Rizzo  * Return 1 on reinit.
661506cc70cSLuigi Rizzo  *
662506cc70cSLuigi Rizzo  * This routine is only called by the upper half of the kernel.
663506cc70cSLuigi Rizzo  * It only reads hwcur (which is changed only by the upper half, too)
664506cc70cSLuigi Rizzo  * and hwavail (which may be changed by the lower half, but only on
665506cc70cSLuigi Rizzo  * a tx ring and only to increase it, so any error will be recovered
666506cc70cSLuigi Rizzo  * on the next call). For the above, we don't strictly need to call
667506cc70cSLuigi Rizzo  * it under lock.
66868b8534bSLuigi Rizzo  */
66968b8534bSLuigi Rizzo int
67068b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring)
67168b8534bSLuigi Rizzo {
67268b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
67368b8534bSLuigi Rizzo 	u_int i, lim = kring->nkr_num_slots - 1;
67468b8534bSLuigi Rizzo 	int errors = 0;
67568b8534bSLuigi Rizzo 
67668b8534bSLuigi Rizzo 	D("called for %s", kring->na->ifp->if_xname);
67768b8534bSLuigi Rizzo 	if (ring->cur > lim)
67868b8534bSLuigi Rizzo 		errors++;
67968b8534bSLuigi Rizzo 	for (i = 0; i <= lim; i++) {
68068b8534bSLuigi Rizzo 		u_int idx = ring->slot[i].buf_idx;
68168b8534bSLuigi Rizzo 		u_int len = ring->slot[i].len;
68268b8534bSLuigi Rizzo 		if (idx < 2 || idx >= netmap_total_buffers) {
68368b8534bSLuigi Rizzo 			if (!errors++)
68468b8534bSLuigi Rizzo 				D("bad buffer at slot %d idx %d len %d ", i, idx, len);
68568b8534bSLuigi Rizzo 			ring->slot[i].buf_idx = 0;
68668b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
68768b8534bSLuigi Rizzo 		} else if (len > NETMAP_BUF_SIZE) {
68868b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
68968b8534bSLuigi Rizzo 			if (!errors++)
69068b8534bSLuigi Rizzo 				D("bad len %d at slot %d idx %d",
69168b8534bSLuigi Rizzo 					len, i, idx);
69268b8534bSLuigi Rizzo 		}
69368b8534bSLuigi Rizzo 	}
69468b8534bSLuigi Rizzo 	if (errors) {
69568b8534bSLuigi Rizzo 		int pos = kring - kring->na->tx_rings;
696d76bf4ffSLuigi Rizzo 		int n = kring->na->num_tx_rings + 1;
69768b8534bSLuigi Rizzo 
69868b8534bSLuigi Rizzo 		D("total %d errors", errors);
69968b8534bSLuigi Rizzo 		errors++;
70068b8534bSLuigi Rizzo 		D("%s %s[%d] reinit, cur %d -> %d avail %d -> %d",
70168b8534bSLuigi Rizzo 			kring->na->ifp->if_xname,
70268b8534bSLuigi Rizzo 			pos < n ?  "TX" : "RX", pos < n ? pos : pos - n,
70368b8534bSLuigi Rizzo 			ring->cur, kring->nr_hwcur,
70468b8534bSLuigi Rizzo 			ring->avail, kring->nr_hwavail);
70568b8534bSLuigi Rizzo 		ring->cur = kring->nr_hwcur;
70668b8534bSLuigi Rizzo 		ring->avail = kring->nr_hwavail;
70768b8534bSLuigi Rizzo 	}
70868b8534bSLuigi Rizzo 	return (errors ? 1 : 0);
70968b8534bSLuigi Rizzo }
71068b8534bSLuigi Rizzo 
71168b8534bSLuigi Rizzo 
71268b8534bSLuigi Rizzo /*
71368b8534bSLuigi Rizzo  * Set the ring ID. For devices with a single queue, a request
71468b8534bSLuigi Rizzo  * for all rings is the same as a single ring.
71568b8534bSLuigi Rizzo  */
71668b8534bSLuigi Rizzo static int
71768b8534bSLuigi Rizzo netmap_set_ringid(struct netmap_priv_d *priv, u_int ringid)
71868b8534bSLuigi Rizzo {
71968b8534bSLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
72068b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
72168b8534bSLuigi Rizzo 	u_int i = ringid & NETMAP_RING_MASK;
72264ae02c3SLuigi Rizzo 	/* initially (np_qfirst == np_qlast) we don't want to lock */
72368b8534bSLuigi Rizzo 	int need_lock = (priv->np_qfirst != priv->np_qlast);
724d76bf4ffSLuigi Rizzo 	int lim = na->num_rx_rings;
72568b8534bSLuigi Rizzo 
726d76bf4ffSLuigi Rizzo 	if (na->num_tx_rings > lim)
727d76bf4ffSLuigi Rizzo 		lim = na->num_tx_rings;
72864ae02c3SLuigi Rizzo 	if ( (ringid & NETMAP_HW_RING) && i >= lim) {
72968b8534bSLuigi Rizzo 		D("invalid ring id %d", i);
73068b8534bSLuigi Rizzo 		return (EINVAL);
73168b8534bSLuigi Rizzo 	}
73268b8534bSLuigi Rizzo 	if (need_lock)
7331a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
73468b8534bSLuigi Rizzo 	priv->np_ringid = ringid;
73568b8534bSLuigi Rizzo 	if (ringid & NETMAP_SW_RING) {
73664ae02c3SLuigi Rizzo 		priv->np_qfirst = NETMAP_SW_RING;
73764ae02c3SLuigi Rizzo 		priv->np_qlast = 0;
73868b8534bSLuigi Rizzo 	} else if (ringid & NETMAP_HW_RING) {
73968b8534bSLuigi Rizzo 		priv->np_qfirst = i;
74068b8534bSLuigi Rizzo 		priv->np_qlast = i + 1;
74168b8534bSLuigi Rizzo 	} else {
74268b8534bSLuigi Rizzo 		priv->np_qfirst = 0;
74364ae02c3SLuigi Rizzo 		priv->np_qlast = NETMAP_HW_RING ;
74468b8534bSLuigi Rizzo 	}
74568b8534bSLuigi Rizzo 	priv->np_txpoll = (ringid & NETMAP_NO_TX_POLL) ? 0 : 1;
74668b8534bSLuigi Rizzo 	if (need_lock)
7471a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
74868b8534bSLuigi Rizzo 	if (ringid & NETMAP_SW_RING)
74968b8534bSLuigi Rizzo 		D("ringid %s set to SW RING", ifp->if_xname);
75068b8534bSLuigi Rizzo 	else if (ringid & NETMAP_HW_RING)
75168b8534bSLuigi Rizzo 		D("ringid %s set to HW RING %d", ifp->if_xname,
75268b8534bSLuigi Rizzo 			priv->np_qfirst);
75368b8534bSLuigi Rizzo 	else
75464ae02c3SLuigi Rizzo 		D("ringid %s set to all %d HW RINGS", ifp->if_xname, lim);
75568b8534bSLuigi Rizzo 	return 0;
75668b8534bSLuigi Rizzo }
75768b8534bSLuigi Rizzo 
75868b8534bSLuigi Rizzo /*
75968b8534bSLuigi Rizzo  * ioctl(2) support for the "netmap" device.
76068b8534bSLuigi Rizzo  *
76168b8534bSLuigi Rizzo  * Following a list of accepted commands:
76268b8534bSLuigi Rizzo  * - NIOCGINFO
76368b8534bSLuigi Rizzo  * - SIOCGIFADDR	just for convenience
76468b8534bSLuigi Rizzo  * - NIOCREGIF
76568b8534bSLuigi Rizzo  * - NIOCUNREGIF
76668b8534bSLuigi Rizzo  * - NIOCTXSYNC
76768b8534bSLuigi Rizzo  * - NIOCRXSYNC
76868b8534bSLuigi Rizzo  *
76968b8534bSLuigi Rizzo  * Return 0 on success, errno otherwise.
77068b8534bSLuigi Rizzo  */
77168b8534bSLuigi Rizzo static int
77268b8534bSLuigi Rizzo netmap_ioctl(__unused struct cdev *dev, u_long cmd, caddr_t data,
773506cc70cSLuigi Rizzo 	__unused int fflag, struct thread *td)
77468b8534bSLuigi Rizzo {
77568b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
77668b8534bSLuigi Rizzo 	struct ifnet *ifp;
77768b8534bSLuigi Rizzo 	struct nmreq *nmr = (struct nmreq *) data;
77868b8534bSLuigi Rizzo 	struct netmap_adapter *na;
77968b8534bSLuigi Rizzo 	int error;
78064ae02c3SLuigi Rizzo 	u_int i, lim;
78168b8534bSLuigi Rizzo 	struct netmap_if *nifp;
78268b8534bSLuigi Rizzo 
783*f196ce38SLuigi Rizzo #ifdef linux
784*f196ce38SLuigi Rizzo #define devfs_get_cdevpriv(pp)				\
785*f196ce38SLuigi Rizzo 	({ *(struct netmap_priv_d **)pp = ((struct file *)td)->private_data; 	\
786*f196ce38SLuigi Rizzo 		(*pp ? 0 : ENOENT); })
787*f196ce38SLuigi Rizzo 
788*f196ce38SLuigi Rizzo /* devfs_set_cdevpriv cannot fail on linux */
789*f196ce38SLuigi Rizzo #define devfs_set_cdevpriv(p, fn)				\
790*f196ce38SLuigi Rizzo 	({ ((struct file *)td)->private_data = p; (p ? 0 : EINVAL); })
791*f196ce38SLuigi Rizzo 
792*f196ce38SLuigi Rizzo 
793*f196ce38SLuigi Rizzo #define devfs_clear_cdevpriv()	do {				\
794*f196ce38SLuigi Rizzo 		netmap_dtor(priv); ((struct file *)td)->private_data = 0;	\
795*f196ce38SLuigi Rizzo 	} while (0)
796*f196ce38SLuigi Rizzo #endif /* linux */
797*f196ce38SLuigi Rizzo 
798506cc70cSLuigi Rizzo 	CURVNET_SET(TD_TO_VNET(td));
799506cc70cSLuigi Rizzo 
80068b8534bSLuigi Rizzo 	error = devfs_get_cdevpriv((void **)&priv);
801506cc70cSLuigi Rizzo 	if (error != ENOENT && error != 0) {
802506cc70cSLuigi Rizzo 		CURVNET_RESTORE();
80368b8534bSLuigi Rizzo 		return (error);
804506cc70cSLuigi Rizzo 	}
80568b8534bSLuigi Rizzo 
80668b8534bSLuigi Rizzo 	error = 0;	/* Could be ENOENT */
807*f196ce38SLuigi Rizzo 	nmr->nr_name[sizeof(nmr->nr_name) - 1] = '\0';	/* truncate name */
80868b8534bSLuigi Rizzo 	switch (cmd) {
80968b8534bSLuigi Rizzo 	case NIOCGINFO:		/* return capabilities etc */
81068b8534bSLuigi Rizzo 		/* memsize is always valid */
81164ae02c3SLuigi Rizzo 		nmr->nr_memsize = nm_mem->nm_totalsize;
81268b8534bSLuigi Rizzo 		nmr->nr_offset = 0;
81364ae02c3SLuigi Rizzo 		nmr->nr_rx_rings = nmr->nr_tx_rings = 0;
81464ae02c3SLuigi Rizzo 		nmr->nr_rx_slots = nmr->nr_tx_slots = 0;
81564ae02c3SLuigi Rizzo 		if (nmr->nr_version != NETMAP_API) {
81664ae02c3SLuigi Rizzo 			D("API mismatch got %d have %d",
81764ae02c3SLuigi Rizzo 				nmr->nr_version, NETMAP_API);
81864ae02c3SLuigi Rizzo 			nmr->nr_version = NETMAP_API;
81964ae02c3SLuigi Rizzo 			error = EINVAL;
82064ae02c3SLuigi Rizzo 			break;
82164ae02c3SLuigi Rizzo 		}
82268b8534bSLuigi Rizzo 		if (nmr->nr_name[0] == '\0')	/* just get memory info */
82368b8534bSLuigi Rizzo 			break;
82468b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* get a refcount */
82568b8534bSLuigi Rizzo 		if (error)
82668b8534bSLuigi Rizzo 			break;
82768b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap_adapter */
828d76bf4ffSLuigi Rizzo 		nmr->nr_rx_rings = na->num_rx_rings;
829d76bf4ffSLuigi Rizzo 		nmr->nr_tx_rings = na->num_tx_rings;
83064ae02c3SLuigi Rizzo 		nmr->nr_rx_slots = na->num_rx_desc;
83164ae02c3SLuigi Rizzo 		nmr->nr_tx_slots = na->num_tx_desc;
832*f196ce38SLuigi Rizzo 		nm_if_rele(ifp);	/* return the refcount */
83368b8534bSLuigi Rizzo 		break;
83468b8534bSLuigi Rizzo 
83568b8534bSLuigi Rizzo 	case NIOCREGIF:
83664ae02c3SLuigi Rizzo 		if (nmr->nr_version != NETMAP_API) {
83764ae02c3SLuigi Rizzo 			nmr->nr_version = NETMAP_API;
83864ae02c3SLuigi Rizzo 			error = EINVAL;
83964ae02c3SLuigi Rizzo 			break;
84064ae02c3SLuigi Rizzo 		}
841506cc70cSLuigi Rizzo 		if (priv != NULL) {	/* thread already registered */
842506cc70cSLuigi Rizzo 			error = netmap_set_ringid(priv, nmr->nr_ringid);
843506cc70cSLuigi Rizzo 			break;
844506cc70cSLuigi Rizzo 		}
84568b8534bSLuigi Rizzo 		/* find the interface and a reference */
84668b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* keep reference */
84768b8534bSLuigi Rizzo 		if (error)
84868b8534bSLuigi Rizzo 			break;
84968b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap adapter */
85068b8534bSLuigi Rizzo 		/*
85168b8534bSLuigi Rizzo 		 * Allocate the private per-thread structure.
85268b8534bSLuigi Rizzo 		 * XXX perhaps we can use a blocking malloc ?
85368b8534bSLuigi Rizzo 		 */
85468b8534bSLuigi Rizzo 		priv = malloc(sizeof(struct netmap_priv_d), M_DEVBUF,
85568b8534bSLuigi Rizzo 			      M_NOWAIT | M_ZERO);
85668b8534bSLuigi Rizzo 		if (priv == NULL) {
85768b8534bSLuigi Rizzo 			error = ENOMEM;
858*f196ce38SLuigi Rizzo 			nm_if_rele(ifp);   /* return the refcount */
85968b8534bSLuigi Rizzo 			break;
86068b8534bSLuigi Rizzo 		}
86168b8534bSLuigi Rizzo 
86268b8534bSLuigi Rizzo 		for (i = 10; i > 0; i--) {
8631a26580eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
86468b8534bSLuigi Rizzo 			if (!NETMAP_DELETING(na))
86568b8534bSLuigi Rizzo 				break;
8661a26580eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
86768b8534bSLuigi Rizzo 			tsleep(na, 0, "NIOCREGIF", hz/10);
86868b8534bSLuigi Rizzo 		}
86968b8534bSLuigi Rizzo 		if (i == 0) {
87068b8534bSLuigi Rizzo 			D("too many NIOCREGIF attempts, give up");
87168b8534bSLuigi Rizzo 			error = EINVAL;
87268b8534bSLuigi Rizzo 			free(priv, M_DEVBUF);
873*f196ce38SLuigi Rizzo 			nm_if_rele(ifp);	/* return the refcount */
87468b8534bSLuigi Rizzo 			break;
87568b8534bSLuigi Rizzo 		}
87668b8534bSLuigi Rizzo 
87768b8534bSLuigi Rizzo 		priv->np_ifp = ifp;	/* store the reference */
87868b8534bSLuigi Rizzo 		error = netmap_set_ringid(priv, nmr->nr_ringid);
87968b8534bSLuigi Rizzo 		if (error)
88068b8534bSLuigi Rizzo 			goto error;
88168b8534bSLuigi Rizzo 		priv->np_nifp = nifp = netmap_if_new(nmr->nr_name, na);
88268b8534bSLuigi Rizzo 		if (nifp == NULL) { /* allocation failed */
88368b8534bSLuigi Rizzo 			error = ENOMEM;
88468b8534bSLuigi Rizzo 		} else if (ifp->if_capenable & IFCAP_NETMAP) {
88568b8534bSLuigi Rizzo 			/* was already set */
88668b8534bSLuigi Rizzo 		} else {
88768b8534bSLuigi Rizzo 			/* Otherwise set the card in netmap mode
88868b8534bSLuigi Rizzo 			 * and make it use the shared buffers.
88968b8534bSLuigi Rizzo 			 */
890*f196ce38SLuigi Rizzo 			for (i = 0 ; i < na->num_tx_rings + 1; i++)
891*f196ce38SLuigi Rizzo 				mtx_init(&na->tx_rings[i].q_lock, "nm_txq_lock", MTX_NETWORK_LOCK, MTX_DEF);
892*f196ce38SLuigi Rizzo 			for (i = 0 ; i < na->num_rx_rings + 1; i++) {
893*f196ce38SLuigi Rizzo 				mtx_init(&na->rx_rings[i].q_lock, "nm_rxq_lock", MTX_NETWORK_LOCK, MTX_DEF);
894*f196ce38SLuigi Rizzo 			}
89568b8534bSLuigi Rizzo 			error = na->nm_register(ifp, 1); /* mode on */
8965819da83SLuigi Rizzo 			if (error)
8975819da83SLuigi Rizzo 				netmap_dtor_locked(priv);
89868b8534bSLuigi Rizzo 		}
89968b8534bSLuigi Rizzo 
90068b8534bSLuigi Rizzo 		if (error) {	/* reg. failed, release priv and ref */
90168b8534bSLuigi Rizzo error:
9021a26580eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
903*f196ce38SLuigi Rizzo 			nm_if_rele(ifp);	/* return the refcount */
9045819da83SLuigi Rizzo 			bzero(priv, sizeof(*priv));
9055819da83SLuigi Rizzo 			free(priv, M_DEVBUF);
90668b8534bSLuigi Rizzo 			break;
90768b8534bSLuigi Rizzo 		}
90868b8534bSLuigi Rizzo 
9091a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
91068b8534bSLuigi Rizzo 		error = devfs_set_cdevpriv(priv, netmap_dtor);
91168b8534bSLuigi Rizzo 
91268b8534bSLuigi Rizzo 		if (error != 0) {
91368b8534bSLuigi Rizzo 			/* could not assign the private storage for the
91468b8534bSLuigi Rizzo 			 * thread, call the destructor explicitly.
91568b8534bSLuigi Rizzo 			 */
91668b8534bSLuigi Rizzo 			netmap_dtor(priv);
91768b8534bSLuigi Rizzo 			break;
91868b8534bSLuigi Rizzo 		}
91968b8534bSLuigi Rizzo 
92068b8534bSLuigi Rizzo 		/* return the offset of the netmap_if object */
921d76bf4ffSLuigi Rizzo 		nmr->nr_rx_rings = na->num_rx_rings;
922d76bf4ffSLuigi Rizzo 		nmr->nr_tx_rings = na->num_tx_rings;
92364ae02c3SLuigi Rizzo 		nmr->nr_rx_slots = na->num_rx_desc;
92464ae02c3SLuigi Rizzo 		nmr->nr_tx_slots = na->num_tx_desc;
92564ae02c3SLuigi Rizzo 		nmr->nr_memsize = nm_mem->nm_totalsize;
926446ee301SLuigi Rizzo 		nmr->nr_offset = netmap_if_offset(nifp);
92768b8534bSLuigi Rizzo 		break;
92868b8534bSLuigi Rizzo 
92968b8534bSLuigi Rizzo 	case NIOCUNREGIF:
930506cc70cSLuigi Rizzo 		if (priv == NULL) {
931506cc70cSLuigi Rizzo 			error = ENXIO;
932506cc70cSLuigi Rizzo 			break;
933506cc70cSLuigi Rizzo 		}
93468b8534bSLuigi Rizzo 
93568b8534bSLuigi Rizzo 		/* the interface is unregistered inside the
93668b8534bSLuigi Rizzo 		   destructor of the private data. */
93768b8534bSLuigi Rizzo 		devfs_clear_cdevpriv();
93868b8534bSLuigi Rizzo 		break;
93968b8534bSLuigi Rizzo 
94068b8534bSLuigi Rizzo 	case NIOCTXSYNC:
94168b8534bSLuigi Rizzo         case NIOCRXSYNC:
942506cc70cSLuigi Rizzo 		if (priv == NULL) {
943506cc70cSLuigi Rizzo 			error = ENXIO;
944506cc70cSLuigi Rizzo 			break;
945506cc70cSLuigi Rizzo 		}
94668b8534bSLuigi Rizzo 		ifp = priv->np_ifp;	/* we have a reference */
94768b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap adapter */
94864ae02c3SLuigi Rizzo 		if (priv->np_qfirst == NETMAP_SW_RING) { /* host rings */
94968b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC)
95068b8534bSLuigi Rizzo 				netmap_sync_to_host(na);
95168b8534bSLuigi Rizzo 			else
95268b8534bSLuigi Rizzo 				netmap_sync_from_host(na, NULL);
953506cc70cSLuigi Rizzo 			break;
95468b8534bSLuigi Rizzo 		}
95564ae02c3SLuigi Rizzo 		/* find the last ring to scan */
95664ae02c3SLuigi Rizzo 		lim = priv->np_qlast;
95764ae02c3SLuigi Rizzo 		if (lim == NETMAP_HW_RING)
9583c0caf6cSLuigi Rizzo 			lim = (cmd == NIOCTXSYNC) ?
959d76bf4ffSLuigi Rizzo 			    na->num_tx_rings : na->num_rx_rings;
96068b8534bSLuigi Rizzo 
96164ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim; i++) {
96268b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC) {
96368b8534bSLuigi Rizzo 				struct netmap_kring *kring = &na->tx_rings[i];
96468b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
9653c0caf6cSLuigi Rizzo 					D("pre txsync ring %d cur %d hwcur %d",
96668b8534bSLuigi Rizzo 					    i, kring->ring->cur,
96768b8534bSLuigi Rizzo 					    kring->nr_hwcur);
9681a26580eSLuigi Rizzo 				na->nm_txsync(ifp, i, 1 /* do lock */);
96968b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
9703c0caf6cSLuigi Rizzo 					D("post txsync ring %d cur %d hwcur %d",
97168b8534bSLuigi Rizzo 					    i, kring->ring->cur,
97268b8534bSLuigi Rizzo 					    kring->nr_hwcur);
97368b8534bSLuigi Rizzo 			} else {
9741a26580eSLuigi Rizzo 				na->nm_rxsync(ifp, i, 1 /* do lock */);
97568b8534bSLuigi Rizzo 				microtime(&na->rx_rings[i].ring->ts);
97668b8534bSLuigi Rizzo 			}
97768b8534bSLuigi Rizzo 		}
97868b8534bSLuigi Rizzo 
97968b8534bSLuigi Rizzo 		break;
98068b8534bSLuigi Rizzo 
981*f196ce38SLuigi Rizzo #ifdef __FreeBSD__
98268b8534bSLuigi Rizzo 	case BIOCIMMEDIATE:
98368b8534bSLuigi Rizzo 	case BIOCGHDRCMPLT:
98468b8534bSLuigi Rizzo 	case BIOCSHDRCMPLT:
98568b8534bSLuigi Rizzo 	case BIOCSSEESENT:
98668b8534bSLuigi Rizzo 		D("ignore BIOCIMMEDIATE/BIOCSHDRCMPLT/BIOCSHDRCMPLT/BIOCSSEESENT");
98768b8534bSLuigi Rizzo 		break;
98868b8534bSLuigi Rizzo 
989babc7c12SLuigi Rizzo 	default:	/* allow device-specific ioctls */
99068b8534bSLuigi Rizzo 	    {
99168b8534bSLuigi Rizzo 		struct socket so;
99268b8534bSLuigi Rizzo 		bzero(&so, sizeof(so));
99368b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* keep reference */
99468b8534bSLuigi Rizzo 		if (error)
99568b8534bSLuigi Rizzo 			break;
99668b8534bSLuigi Rizzo 		so.so_vnet = ifp->if_vnet;
99768b8534bSLuigi Rizzo 		// so->so_proto not null.
99868b8534bSLuigi Rizzo 		error = ifioctl(&so, cmd, data, td);
999*f196ce38SLuigi Rizzo 		nm_if_rele(ifp);
1000babc7c12SLuigi Rizzo 		break;
100168b8534bSLuigi Rizzo 	    }
1002*f196ce38SLuigi Rizzo 
1003*f196ce38SLuigi Rizzo #else /* linux */
1004*f196ce38SLuigi Rizzo 	default:
1005*f196ce38SLuigi Rizzo 		error = EOPNOTSUPP;
1006*f196ce38SLuigi Rizzo #endif /* linux */
100768b8534bSLuigi Rizzo 	}
100868b8534bSLuigi Rizzo 
1009506cc70cSLuigi Rizzo 	CURVNET_RESTORE();
101068b8534bSLuigi Rizzo 	return (error);
101168b8534bSLuigi Rizzo }
101268b8534bSLuigi Rizzo 
101368b8534bSLuigi Rizzo 
101468b8534bSLuigi Rizzo /*
101568b8534bSLuigi Rizzo  * select(2) and poll(2) handlers for the "netmap" device.
101668b8534bSLuigi Rizzo  *
101768b8534bSLuigi Rizzo  * Can be called for one or more queues.
101868b8534bSLuigi Rizzo  * Return true the event mask corresponding to ready events.
101968b8534bSLuigi Rizzo  * If there are no ready events, do a selrecord on either individual
102068b8534bSLuigi Rizzo  * selfd or on the global one.
102168b8534bSLuigi Rizzo  * Device-dependent parts (locking and sync of tx/rx rings)
102268b8534bSLuigi Rizzo  * are done through callbacks.
1023*f196ce38SLuigi Rizzo  *
1024*f196ce38SLuigi Rizzo  * On linux, pwait is the poll table.
1025*f196ce38SLuigi Rizzo  * If pwait == NULL someone else already woke up before. We can report
1026*f196ce38SLuigi Rizzo  * events but they are filtered upstream.
1027*f196ce38SLuigi Rizzo  * If pwait != NULL, then pwait->key contains the list of events.
102868b8534bSLuigi Rizzo  */
102968b8534bSLuigi Rizzo static int
103068b8534bSLuigi Rizzo netmap_poll(__unused struct cdev *dev, int events, struct thread *td)
103168b8534bSLuigi Rizzo {
103268b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
103368b8534bSLuigi Rizzo 	struct netmap_adapter *na;
103468b8534bSLuigi Rizzo 	struct ifnet *ifp;
103568b8534bSLuigi Rizzo 	struct netmap_kring *kring;
1036d0c7b075SLuigi Rizzo 	u_int core_lock, i, check_all, want_tx, want_rx, revents = 0;
103764ae02c3SLuigi Rizzo 	u_int lim_tx, lim_rx;
1038bcda432eSLuigi Rizzo 	enum {NO_CL, NEED_CL, LOCKED_CL }; /* see below */
103968b8534bSLuigi Rizzo 
104068b8534bSLuigi Rizzo 	if (devfs_get_cdevpriv((void **)&priv) != 0 || priv == NULL)
104168b8534bSLuigi Rizzo 		return POLLERR;
104268b8534bSLuigi Rizzo 
104368b8534bSLuigi Rizzo 	ifp = priv->np_ifp;
104468b8534bSLuigi Rizzo 	// XXX check for deleting() ?
104568b8534bSLuigi Rizzo 	if ( (ifp->if_capenable & IFCAP_NETMAP) == 0)
104668b8534bSLuigi Rizzo 		return POLLERR;
104768b8534bSLuigi Rizzo 
104868b8534bSLuigi Rizzo 	if (netmap_verbose & 0x8000)
104968b8534bSLuigi Rizzo 		D("device %s events 0x%x", ifp->if_xname, events);
105068b8534bSLuigi Rizzo 	want_tx = events & (POLLOUT | POLLWRNORM);
105168b8534bSLuigi Rizzo 	want_rx = events & (POLLIN | POLLRDNORM);
105268b8534bSLuigi Rizzo 
105368b8534bSLuigi Rizzo 	na = NA(ifp); /* retrieve netmap adapter */
105468b8534bSLuigi Rizzo 
1055d76bf4ffSLuigi Rizzo 	lim_tx = na->num_tx_rings;
1056d76bf4ffSLuigi Rizzo 	lim_rx = na->num_rx_rings;
105768b8534bSLuigi Rizzo 	/* how many queues we are scanning */
105864ae02c3SLuigi Rizzo 	if (priv->np_qfirst == NETMAP_SW_RING) {
105968b8534bSLuigi Rizzo 		if (priv->np_txpoll || want_tx) {
106068b8534bSLuigi Rizzo 			/* push any packets up, then we are always ready */
106164ae02c3SLuigi Rizzo 			kring = &na->tx_rings[lim_tx];
106268b8534bSLuigi Rizzo 			netmap_sync_to_host(na);
106368b8534bSLuigi Rizzo 			revents |= want_tx;
106468b8534bSLuigi Rizzo 		}
106568b8534bSLuigi Rizzo 		if (want_rx) {
106664ae02c3SLuigi Rizzo 			kring = &na->rx_rings[lim_rx];
106768b8534bSLuigi Rizzo 			if (kring->ring->avail == 0)
106868b8534bSLuigi Rizzo 				netmap_sync_from_host(na, td);
106968b8534bSLuigi Rizzo 			if (kring->ring->avail > 0) {
107068b8534bSLuigi Rizzo 				revents |= want_rx;
107168b8534bSLuigi Rizzo 			}
107268b8534bSLuigi Rizzo 		}
107368b8534bSLuigi Rizzo 		return (revents);
107468b8534bSLuigi Rizzo 	}
107568b8534bSLuigi Rizzo 
107668b8534bSLuigi Rizzo 	/*
107768b8534bSLuigi Rizzo 	 * check_all is set if the card has more than one queue and
107868b8534bSLuigi Rizzo 	 * the client is polling all of them. If true, we sleep on
107968b8534bSLuigi Rizzo 	 * the "global" selfd, otherwise we sleep on individual selfd
108068b8534bSLuigi Rizzo 	 * (we can only sleep on one of them per direction).
108168b8534bSLuigi Rizzo 	 * The interrupt routine in the driver should always wake on
108268b8534bSLuigi Rizzo 	 * the individual selfd, and also on the global one if the card
108368b8534bSLuigi Rizzo 	 * has more than one ring.
108468b8534bSLuigi Rizzo 	 *
108568b8534bSLuigi Rizzo 	 * If the card has only one lock, we just use that.
108668b8534bSLuigi Rizzo 	 * If the card has separate ring locks, we just use those
108768b8534bSLuigi Rizzo 	 * unless we are doing check_all, in which case the whole
108868b8534bSLuigi Rizzo 	 * loop is wrapped by the global lock.
108968b8534bSLuigi Rizzo 	 * We acquire locks only when necessary: if poll is called
109068b8534bSLuigi Rizzo 	 * when buffers are available, we can just return without locks.
109168b8534bSLuigi Rizzo 	 *
109268b8534bSLuigi Rizzo 	 * rxsync() is only called if we run out of buffers on a POLLIN.
109368b8534bSLuigi Rizzo 	 * txsync() is called if we run out of buffers on POLLOUT, or
109468b8534bSLuigi Rizzo 	 * there are pending packets to send. The latter can be disabled
109568b8534bSLuigi Rizzo 	 * passing NETMAP_NO_TX_POLL in the NIOCREG call.
109668b8534bSLuigi Rizzo 	 */
109764ae02c3SLuigi Rizzo 	check_all = (priv->np_qlast == NETMAP_HW_RING) && (lim_tx > 1 || lim_rx > 1);
109868b8534bSLuigi Rizzo 
109968b8534bSLuigi Rizzo 	/*
110068b8534bSLuigi Rizzo 	 * core_lock indicates what to do with the core lock.
110168b8534bSLuigi Rizzo 	 * The core lock is used when either the card has no individual
110268b8534bSLuigi Rizzo 	 * locks, or it has individual locks but we are cheking all
110368b8534bSLuigi Rizzo 	 * rings so we need the core lock to avoid missing wakeup events.
110468b8534bSLuigi Rizzo 	 *
110568b8534bSLuigi Rizzo 	 * It has three possible states:
110668b8534bSLuigi Rizzo 	 * NO_CL	we don't need to use the core lock, e.g.
110768b8534bSLuigi Rizzo 	 *		because we are protected by individual locks.
110868b8534bSLuigi Rizzo 	 * NEED_CL	we need the core lock. In this case, when we
110968b8534bSLuigi Rizzo 	 *		call the lock routine, move to LOCKED_CL
111068b8534bSLuigi Rizzo 	 *		to remember to release the lock once done.
111168b8534bSLuigi Rizzo 	 * LOCKED_CL	core lock is set, so we need to release it.
111268b8534bSLuigi Rizzo 	 */
1113d0c7b075SLuigi Rizzo 	core_lock = (check_all || !na->separate_locks) ? NEED_CL : NO_CL;
1114*f196ce38SLuigi Rizzo #ifdef NM_BRIDGE
1115*f196ce38SLuigi Rizzo 	/* the bridge uses separate locks */
1116*f196ce38SLuigi Rizzo 	if (na->nm_register == bdg_netmap_reg) {
1117*f196ce38SLuigi Rizzo 		ND("not using core lock for %s", ifp->if_xname);
1118*f196ce38SLuigi Rizzo 		core_lock = NO_CL;
1119*f196ce38SLuigi Rizzo 	}
1120*f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */
112164ae02c3SLuigi Rizzo 	if (priv->np_qlast != NETMAP_HW_RING) {
112264ae02c3SLuigi Rizzo 		lim_tx = lim_rx = priv->np_qlast;
112364ae02c3SLuigi Rizzo 	}
112464ae02c3SLuigi Rizzo 
112568b8534bSLuigi Rizzo 	/*
112668b8534bSLuigi Rizzo 	 * We start with a lock free round which is good if we have
112768b8534bSLuigi Rizzo 	 * data available. If this fails, then lock and call the sync
112868b8534bSLuigi Rizzo 	 * routines.
112968b8534bSLuigi Rizzo 	 */
113064ae02c3SLuigi Rizzo 	for (i = priv->np_qfirst; want_rx && i < lim_rx; i++) {
113168b8534bSLuigi Rizzo 		kring = &na->rx_rings[i];
113268b8534bSLuigi Rizzo 		if (kring->ring->avail > 0) {
113368b8534bSLuigi Rizzo 			revents |= want_rx;
113468b8534bSLuigi Rizzo 			want_rx = 0;	/* also breaks the loop */
113568b8534bSLuigi Rizzo 		}
113668b8534bSLuigi Rizzo 	}
113764ae02c3SLuigi Rizzo 	for (i = priv->np_qfirst; want_tx && i < lim_tx; i++) {
113868b8534bSLuigi Rizzo 		kring = &na->tx_rings[i];
113968b8534bSLuigi Rizzo 		if (kring->ring->avail > 0) {
114068b8534bSLuigi Rizzo 			revents |= want_tx;
114168b8534bSLuigi Rizzo 			want_tx = 0;	/* also breaks the loop */
114268b8534bSLuigi Rizzo 		}
114368b8534bSLuigi Rizzo 	}
114468b8534bSLuigi Rizzo 
114568b8534bSLuigi Rizzo 	/*
114668b8534bSLuigi Rizzo 	 * If we to push packets out (priv->np_txpoll) or want_tx is
114768b8534bSLuigi Rizzo 	 * still set, we do need to run the txsync calls (on all rings,
114868b8534bSLuigi Rizzo 	 * to avoid that the tx rings stall).
114968b8534bSLuigi Rizzo 	 */
115068b8534bSLuigi Rizzo 	if (priv->np_txpoll || want_tx) {
115164ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim_tx; i++) {
115268b8534bSLuigi Rizzo 			kring = &na->tx_rings[i];
11535819da83SLuigi Rizzo 			/*
11545819da83SLuigi Rizzo 			 * Skip the current ring if want_tx == 0
11555819da83SLuigi Rizzo 			 * (we have already done a successful sync on
11565819da83SLuigi Rizzo 			 * a previous ring) AND kring->cur == kring->hwcur
11575819da83SLuigi Rizzo 			 * (there are no pending transmissions for this ring).
11585819da83SLuigi Rizzo 			 */
115968b8534bSLuigi Rizzo 			if (!want_tx && kring->ring->cur == kring->nr_hwcur)
116068b8534bSLuigi Rizzo 				continue;
116168b8534bSLuigi Rizzo 			if (core_lock == NEED_CL) {
11621a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
116368b8534bSLuigi Rizzo 				core_lock = LOCKED_CL;
116468b8534bSLuigi Rizzo 			}
116568b8534bSLuigi Rizzo 			if (na->separate_locks)
11661a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_TX_LOCK, i);
116768b8534bSLuigi Rizzo 			if (netmap_verbose & NM_VERB_TXSYNC)
116868b8534bSLuigi Rizzo 				D("send %d on %s %d",
116968b8534bSLuigi Rizzo 					kring->ring->cur,
117068b8534bSLuigi Rizzo 					ifp->if_xname, i);
11711a26580eSLuigi Rizzo 			if (na->nm_txsync(ifp, i, 0 /* no lock */))
117268b8534bSLuigi Rizzo 				revents |= POLLERR;
117368b8534bSLuigi Rizzo 
11745819da83SLuigi Rizzo 			/* Check avail/call selrecord only if called with POLLOUT */
117568b8534bSLuigi Rizzo 			if (want_tx) {
117668b8534bSLuigi Rizzo 				if (kring->ring->avail > 0) {
117768b8534bSLuigi Rizzo 					/* stop at the first ring. We don't risk
117868b8534bSLuigi Rizzo 					 * starvation.
117968b8534bSLuigi Rizzo 					 */
118068b8534bSLuigi Rizzo 					revents |= want_tx;
118168b8534bSLuigi Rizzo 					want_tx = 0;
118268b8534bSLuigi Rizzo 				} else if (!check_all)
118368b8534bSLuigi Rizzo 					selrecord(td, &kring->si);
118468b8534bSLuigi Rizzo 			}
118568b8534bSLuigi Rizzo 			if (na->separate_locks)
11861a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_TX_UNLOCK, i);
118768b8534bSLuigi Rizzo 		}
118868b8534bSLuigi Rizzo 	}
118968b8534bSLuigi Rizzo 
119068b8534bSLuigi Rizzo 	/*
119168b8534bSLuigi Rizzo 	 * now if want_rx is still set we need to lock and rxsync.
119268b8534bSLuigi Rizzo 	 * Do it on all rings because otherwise we starve.
119368b8534bSLuigi Rizzo 	 */
119468b8534bSLuigi Rizzo 	if (want_rx) {
119564ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim_rx; i++) {
119668b8534bSLuigi Rizzo 			kring = &na->rx_rings[i];
119768b8534bSLuigi Rizzo 			if (core_lock == NEED_CL) {
11981a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
119968b8534bSLuigi Rizzo 				core_lock = LOCKED_CL;
120068b8534bSLuigi Rizzo 			}
120168b8534bSLuigi Rizzo 			if (na->separate_locks)
12021a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_RX_LOCK, i);
120368b8534bSLuigi Rizzo 
12041a26580eSLuigi Rizzo 			if (na->nm_rxsync(ifp, i, 0 /* no lock */))
120568b8534bSLuigi Rizzo 				revents |= POLLERR;
12065819da83SLuigi Rizzo 			if (netmap_no_timestamp == 0 ||
12075819da83SLuigi Rizzo 					kring->ring->flags & NR_TIMESTAMP) {
120868b8534bSLuigi Rizzo 				microtime(&kring->ring->ts);
12095819da83SLuigi Rizzo 			}
121068b8534bSLuigi Rizzo 
121168b8534bSLuigi Rizzo 			if (kring->ring->avail > 0)
121268b8534bSLuigi Rizzo 				revents |= want_rx;
121368b8534bSLuigi Rizzo 			else if (!check_all)
121468b8534bSLuigi Rizzo 				selrecord(td, &kring->si);
121568b8534bSLuigi Rizzo 			if (na->separate_locks)
12161a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_RX_UNLOCK, i);
121768b8534bSLuigi Rizzo 		}
121868b8534bSLuigi Rizzo 	}
121964ae02c3SLuigi Rizzo 	if (check_all && revents == 0) { /* signal on the global queue */
122068b8534bSLuigi Rizzo 		if (want_tx)
122164ae02c3SLuigi Rizzo 			selrecord(td, &na->tx_si);
122268b8534bSLuigi Rizzo 		if (want_rx)
122364ae02c3SLuigi Rizzo 			selrecord(td, &na->rx_si);
122468b8534bSLuigi Rizzo 	}
122568b8534bSLuigi Rizzo 	if (core_lock == LOCKED_CL)
12261a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
122768b8534bSLuigi Rizzo 
122868b8534bSLuigi Rizzo 	return (revents);
122968b8534bSLuigi Rizzo }
123068b8534bSLuigi Rizzo 
123168b8534bSLuigi Rizzo /*------- driver support routines ------*/
123268b8534bSLuigi Rizzo 
123368b8534bSLuigi Rizzo /*
1234babc7c12SLuigi Rizzo  * default lock wrapper.
12351a26580eSLuigi Rizzo  */
12361a26580eSLuigi Rizzo static void
1237babc7c12SLuigi Rizzo netmap_lock_wrapper(struct ifnet *dev, int what, u_int queueid)
12381a26580eSLuigi Rizzo {
1239babc7c12SLuigi Rizzo 	struct netmap_adapter *na = NA(dev);
12401a26580eSLuigi Rizzo 
12411a26580eSLuigi Rizzo 	switch (what) {
1242babc7c12SLuigi Rizzo #ifdef linux	/* some system do not need lock on register */
12431a26580eSLuigi Rizzo 	case NETMAP_REG_LOCK:
12441a26580eSLuigi Rizzo 	case NETMAP_REG_UNLOCK:
12451a26580eSLuigi Rizzo 		break;
1246babc7c12SLuigi Rizzo #endif /* linux */
12471a26580eSLuigi Rizzo 
12481a26580eSLuigi Rizzo 	case NETMAP_CORE_LOCK:
12491a26580eSLuigi Rizzo 		mtx_lock(&na->core_lock);
12501a26580eSLuigi Rizzo 		break;
12511a26580eSLuigi Rizzo 
12521a26580eSLuigi Rizzo 	case NETMAP_CORE_UNLOCK:
12531a26580eSLuigi Rizzo 		mtx_unlock(&na->core_lock);
12541a26580eSLuigi Rizzo 		break;
12551a26580eSLuigi Rizzo 
12561a26580eSLuigi Rizzo 	case NETMAP_TX_LOCK:
12571a26580eSLuigi Rizzo 		mtx_lock(&na->tx_rings[queueid].q_lock);
12581a26580eSLuigi Rizzo 		break;
12591a26580eSLuigi Rizzo 
12601a26580eSLuigi Rizzo 	case NETMAP_TX_UNLOCK:
12611a26580eSLuigi Rizzo 		mtx_unlock(&na->tx_rings[queueid].q_lock);
12621a26580eSLuigi Rizzo 		break;
12631a26580eSLuigi Rizzo 
12641a26580eSLuigi Rizzo 	case NETMAP_RX_LOCK:
12651a26580eSLuigi Rizzo 		mtx_lock(&na->rx_rings[queueid].q_lock);
12661a26580eSLuigi Rizzo 		break;
12671a26580eSLuigi Rizzo 
12681a26580eSLuigi Rizzo 	case NETMAP_RX_UNLOCK:
12691a26580eSLuigi Rizzo 		mtx_unlock(&na->rx_rings[queueid].q_lock);
12701a26580eSLuigi Rizzo 		break;
12711a26580eSLuigi Rizzo 	}
12721a26580eSLuigi Rizzo }
12731a26580eSLuigi Rizzo 
12741a26580eSLuigi Rizzo 
12751a26580eSLuigi Rizzo /*
127668b8534bSLuigi Rizzo  * Initialize a ``netmap_adapter`` object created by driver on attach.
127768b8534bSLuigi Rizzo  * We allocate a block of memory with room for a struct netmap_adapter
127868b8534bSLuigi Rizzo  * plus two sets of N+2 struct netmap_kring (where N is the number
127968b8534bSLuigi Rizzo  * of hardware rings):
128068b8534bSLuigi Rizzo  * krings	0..N-1	are for the hardware queues.
128168b8534bSLuigi Rizzo  * kring	N	is for the host stack queue
128268b8534bSLuigi Rizzo  * kring	N+1	is only used for the selinfo for all queues.
128368b8534bSLuigi Rizzo  * Return 0 on success, ENOMEM otherwise.
128464ae02c3SLuigi Rizzo  *
1285d76bf4ffSLuigi Rizzo  * na->num_tx_rings can be set for cards with different tx/rx setups
128668b8534bSLuigi Rizzo  */
128768b8534bSLuigi Rizzo int
128868b8534bSLuigi Rizzo netmap_attach(struct netmap_adapter *na, int num_queues)
128968b8534bSLuigi Rizzo {
1290*f196ce38SLuigi Rizzo 	int n, size;
129168b8534bSLuigi Rizzo 	void *buf;
129268b8534bSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
129368b8534bSLuigi Rizzo 
129468b8534bSLuigi Rizzo 	if (ifp == NULL) {
129568b8534bSLuigi Rizzo 		D("ifp not set, giving up");
129668b8534bSLuigi Rizzo 		return EINVAL;
129768b8534bSLuigi Rizzo 	}
129864ae02c3SLuigi Rizzo 	/* clear other fields ? */
129968b8534bSLuigi Rizzo 	na->refcount = 0;
1300d76bf4ffSLuigi Rizzo 	if (na->num_tx_rings == 0)
1301d76bf4ffSLuigi Rizzo 		na->num_tx_rings = num_queues;
1302d76bf4ffSLuigi Rizzo 	na->num_rx_rings = num_queues;
130364ae02c3SLuigi Rizzo 	/* on each direction we have N+1 resources
130464ae02c3SLuigi Rizzo 	 * 0..n-1	are the hardware rings
130564ae02c3SLuigi Rizzo 	 * n		is the ring attached to the stack.
130664ae02c3SLuigi Rizzo 	 */
1307d76bf4ffSLuigi Rizzo 	n = na->num_rx_rings + na->num_tx_rings + 2;
130864ae02c3SLuigi Rizzo 	size = sizeof(*na) + n * sizeof(struct netmap_kring);
130968b8534bSLuigi Rizzo 
131068b8534bSLuigi Rizzo 	buf = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
131168b8534bSLuigi Rizzo 	if (buf) {
1312d0c7b075SLuigi Rizzo 		WNA(ifp) = buf;
131368b8534bSLuigi Rizzo 		na->tx_rings = (void *)((char *)buf + sizeof(*na));
1314d76bf4ffSLuigi Rizzo 		na->rx_rings = na->tx_rings + na->num_tx_rings + 1;
131568b8534bSLuigi Rizzo 		bcopy(na, buf, sizeof(*na));
131668b8534bSLuigi Rizzo 		ifp->if_capabilities |= IFCAP_NETMAP;
13171a26580eSLuigi Rizzo 
13181a26580eSLuigi Rizzo 		na = buf;
1319*f196ce38SLuigi Rizzo 		if (na->nm_lock == NULL) {
1320*f196ce38SLuigi Rizzo 			ND("using default locks for %s", ifp->if_xname);
13211a26580eSLuigi Rizzo 			na->nm_lock = netmap_lock_wrapper;
1322*f196ce38SLuigi Rizzo 			/* core lock initialized here.
1323*f196ce38SLuigi Rizzo 			 * others initialized after netmap_if_new
1324*f196ce38SLuigi Rizzo 			 */
1325*f196ce38SLuigi Rizzo 			mtx_init(&na->core_lock, "netmap core lock", MTX_NETWORK_LOCK, MTX_DEF);
1326*f196ce38SLuigi Rizzo 		}
132768b8534bSLuigi Rizzo 	}
132864ae02c3SLuigi Rizzo #ifdef linux
1329*f196ce38SLuigi Rizzo 	if (ifp->netdev_ops) {
133064ae02c3SLuigi Rizzo 		D("netdev_ops %p", ifp->netdev_ops);
133164ae02c3SLuigi Rizzo 		/* prepare a clone of the netdev ops */
133264ae02c3SLuigi Rizzo 		na->nm_ndo = *ifp->netdev_ops;
1333*f196ce38SLuigi Rizzo 	}
133464ae02c3SLuigi Rizzo 	na->nm_ndo.ndo_start_xmit = netmap_start_linux;
133564ae02c3SLuigi Rizzo #endif
133668b8534bSLuigi Rizzo 	D("%s for %s", buf ? "ok" : "failed", ifp->if_xname);
133768b8534bSLuigi Rizzo 
133868b8534bSLuigi Rizzo 	return (buf ? 0 : ENOMEM);
133968b8534bSLuigi Rizzo }
134068b8534bSLuigi Rizzo 
134168b8534bSLuigi Rizzo 
134268b8534bSLuigi Rizzo /*
134368b8534bSLuigi Rizzo  * Free the allocated memory linked to the given ``netmap_adapter``
134468b8534bSLuigi Rizzo  * object.
134568b8534bSLuigi Rizzo  */
134668b8534bSLuigi Rizzo void
134768b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp)
134868b8534bSLuigi Rizzo {
134968b8534bSLuigi Rizzo 	u_int i;
135068b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
135168b8534bSLuigi Rizzo 
135268b8534bSLuigi Rizzo 	if (!na)
135368b8534bSLuigi Rizzo 		return;
135468b8534bSLuigi Rizzo 
1355d76bf4ffSLuigi Rizzo 	for (i = 0; i < na->num_tx_rings + 1; i++) {
135668b8534bSLuigi Rizzo 		knlist_destroy(&na->tx_rings[i].si.si_note);
135764ae02c3SLuigi Rizzo 		mtx_destroy(&na->tx_rings[i].q_lock);
135868b8534bSLuigi Rizzo 	}
1359d76bf4ffSLuigi Rizzo 	for (i = 0; i < na->num_rx_rings + 1; i++) {
136064ae02c3SLuigi Rizzo 		knlist_destroy(&na->rx_rings[i].si.si_note);
136164ae02c3SLuigi Rizzo 		mtx_destroy(&na->rx_rings[i].q_lock);
136264ae02c3SLuigi Rizzo 	}
136364ae02c3SLuigi Rizzo 	knlist_destroy(&na->tx_si.si_note);
136464ae02c3SLuigi Rizzo 	knlist_destroy(&na->rx_si.si_note);
136568b8534bSLuigi Rizzo 	bzero(na, sizeof(*na));
1366d0c7b075SLuigi Rizzo 	WNA(ifp) = NULL;
136768b8534bSLuigi Rizzo 	free(na, M_DEVBUF);
136868b8534bSLuigi Rizzo }
136968b8534bSLuigi Rizzo 
137068b8534bSLuigi Rizzo 
137168b8534bSLuigi Rizzo /*
137202ad4083SLuigi Rizzo  * Intercept packets from the network stack and pass them
137302ad4083SLuigi Rizzo  * to netmap as incoming packets on the 'software' ring.
137468b8534bSLuigi Rizzo  * We are not locked when called.
137568b8534bSLuigi Rizzo  */
137668b8534bSLuigi Rizzo int
137768b8534bSLuigi Rizzo netmap_start(struct ifnet *ifp, struct mbuf *m)
137868b8534bSLuigi Rizzo {
137968b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
1380d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
13811a26580eSLuigi Rizzo 	u_int i, len = MBUF_LEN(m);
138202ad4083SLuigi Rizzo 	int error = EBUSY, lim = kring->nkr_num_slots - 1;
138368b8534bSLuigi Rizzo 	struct netmap_slot *slot;
138468b8534bSLuigi Rizzo 
138568b8534bSLuigi Rizzo 	if (netmap_verbose & NM_VERB_HOST)
138668b8534bSLuigi Rizzo 		D("%s packet %d len %d from the stack", ifp->if_xname,
138768b8534bSLuigi Rizzo 			kring->nr_hwcur + kring->nr_hwavail, len);
13881a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
138902ad4083SLuigi Rizzo 	if (kring->nr_hwavail >= lim) {
13905b248374SLuigi Rizzo 		if (netmap_verbose)
139168b8534bSLuigi Rizzo 			D("stack ring %s full\n", ifp->if_xname);
139268b8534bSLuigi Rizzo 		goto done;	/* no space */
139368b8534bSLuigi Rizzo 	}
139464ae02c3SLuigi Rizzo 	if (len > NETMAP_BUF_SIZE) {
139564ae02c3SLuigi Rizzo 		D("drop packet size %d > %d", len, NETMAP_BUF_SIZE);
139668b8534bSLuigi Rizzo 		goto done;	/* too long for us */
139768b8534bSLuigi Rizzo 	}
139868b8534bSLuigi Rizzo 
139968b8534bSLuigi Rizzo 	/* compute the insert position */
140068b8534bSLuigi Rizzo 	i = kring->nr_hwcur + kring->nr_hwavail;
140102ad4083SLuigi Rizzo 	if (i > lim)
140202ad4083SLuigi Rizzo 		i -= lim + 1;
140368b8534bSLuigi Rizzo 	slot = &kring->ring->slot[i];
140468b8534bSLuigi Rizzo 	m_copydata(m, 0, len, NMB(slot));
140568b8534bSLuigi Rizzo 	slot->len = len;
140668b8534bSLuigi Rizzo 	kring->nr_hwavail++;
140768b8534bSLuigi Rizzo 	if (netmap_verbose  & NM_VERB_HOST)
1408d76bf4ffSLuigi Rizzo 		D("wake up host ring %s %d", na->ifp->if_xname, na->num_rx_rings);
140968b8534bSLuigi Rizzo 	selwakeuppri(&kring->si, PI_NET);
141068b8534bSLuigi Rizzo 	error = 0;
141168b8534bSLuigi Rizzo done:
14121a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
141368b8534bSLuigi Rizzo 
141468b8534bSLuigi Rizzo 	/* release the mbuf in either cases of success or failure. As an
141568b8534bSLuigi Rizzo 	 * alternative, put the mbuf in a free list and free the list
141668b8534bSLuigi Rizzo 	 * only when really necessary.
141768b8534bSLuigi Rizzo 	 */
141868b8534bSLuigi Rizzo 	m_freem(m);
141968b8534bSLuigi Rizzo 
142068b8534bSLuigi Rizzo 	return (error);
142168b8534bSLuigi Rizzo }
142268b8534bSLuigi Rizzo 
142368b8534bSLuigi Rizzo 
142468b8534bSLuigi Rizzo /*
142568b8534bSLuigi Rizzo  * netmap_reset() is called by the driver routines when reinitializing
142668b8534bSLuigi Rizzo  * a ring. The driver is in charge of locking to protect the kring.
142768b8534bSLuigi Rizzo  * If netmap mode is not set just return NULL.
142868b8534bSLuigi Rizzo  */
142968b8534bSLuigi Rizzo struct netmap_slot *
143068b8534bSLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, int n,
143168b8534bSLuigi Rizzo 	u_int new_cur)
143268b8534bSLuigi Rizzo {
143368b8534bSLuigi Rizzo 	struct netmap_kring *kring;
1434506cc70cSLuigi Rizzo 	int new_hwofs, lim;
143568b8534bSLuigi Rizzo 
143668b8534bSLuigi Rizzo 	if (na == NULL)
143768b8534bSLuigi Rizzo 		return NULL;	/* no netmap support here */
143868b8534bSLuigi Rizzo 	if (!(na->ifp->if_capenable & IFCAP_NETMAP))
143968b8534bSLuigi Rizzo 		return NULL;	/* nothing to reinitialize */
144068b8534bSLuigi Rizzo 
144164ae02c3SLuigi Rizzo 	if (tx == NR_TX) {
144264ae02c3SLuigi Rizzo 		kring = na->tx_rings + n;
1443506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur - new_cur;
144464ae02c3SLuigi Rizzo 	} else {
144564ae02c3SLuigi Rizzo 		kring = na->rx_rings + n;
1446506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur + kring->nr_hwavail - new_cur;
144764ae02c3SLuigi Rizzo 	}
144864ae02c3SLuigi Rizzo 	lim = kring->nkr_num_slots - 1;
1449506cc70cSLuigi Rizzo 	if (new_hwofs > lim)
1450506cc70cSLuigi Rizzo 		new_hwofs -= lim + 1;
1451506cc70cSLuigi Rizzo 
1452506cc70cSLuigi Rizzo 	/* Alwayws set the new offset value and realign the ring. */
1453506cc70cSLuigi Rizzo 	kring->nkr_hwofs = new_hwofs;
1454506cc70cSLuigi Rizzo 	if (tx == NR_TX)
1455506cc70cSLuigi Rizzo 		kring->nr_hwavail = kring->nkr_num_slots - 1;
1456506cc70cSLuigi Rizzo 	D("new hwofs %d on %s %s[%d]",
1457506cc70cSLuigi Rizzo 			kring->nkr_hwofs, na->ifp->if_xname,
1458506cc70cSLuigi Rizzo 			tx == NR_TX ? "TX" : "RX", n);
1459506cc70cSLuigi Rizzo 
1460*f196ce38SLuigi Rizzo #if 0 // def linux
1461*f196ce38SLuigi Rizzo 	/* XXX check that the mappings are correct */
1462*f196ce38SLuigi Rizzo 	/* need ring_nr, adapter->pdev, direction */
1463*f196ce38SLuigi Rizzo 	buffer_info->dma = dma_map_single(&pdev->dev, addr, adapter->rx_buffer_len, DMA_FROM_DEVICE);
1464*f196ce38SLuigi Rizzo 	if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
1465*f196ce38SLuigi Rizzo 		D("error mapping rx netmap buffer %d", i);
1466*f196ce38SLuigi Rizzo 		// XXX fix error handling
1467*f196ce38SLuigi Rizzo 	}
1468*f196ce38SLuigi Rizzo 
1469*f196ce38SLuigi Rizzo #endif /* linux */
147068b8534bSLuigi Rizzo 	/*
147164ae02c3SLuigi Rizzo 	 * Wakeup on the individual and global lock
1472506cc70cSLuigi Rizzo 	 * We do the wakeup here, but the ring is not yet reconfigured.
1473506cc70cSLuigi Rizzo 	 * However, we are under lock so there are no races.
147468b8534bSLuigi Rizzo 	 */
147568b8534bSLuigi Rizzo 	selwakeuppri(&kring->si, PI_NET);
147664ae02c3SLuigi Rizzo 	selwakeuppri(tx == NR_TX ? &na->tx_si : &na->rx_si, PI_NET);
147768b8534bSLuigi Rizzo 	return kring->ring->slot;
147868b8534bSLuigi Rizzo }
147968b8534bSLuigi Rizzo 
148068b8534bSLuigi Rizzo 
148168b8534bSLuigi Rizzo /*
14821a26580eSLuigi Rizzo  * Default functions to handle rx/tx interrupts
14831a26580eSLuigi Rizzo  * we have 4 cases:
14841a26580eSLuigi Rizzo  * 1 ring, single lock:
14851a26580eSLuigi Rizzo  *	lock(core); wake(i=0); unlock(core)
14861a26580eSLuigi Rizzo  * N rings, single lock:
14871a26580eSLuigi Rizzo  *	lock(core); wake(i); wake(N+1) unlock(core)
14881a26580eSLuigi Rizzo  * 1 ring, separate locks: (i=0)
14891a26580eSLuigi Rizzo  *	lock(i); wake(i); unlock(i)
14901a26580eSLuigi Rizzo  * N rings, separate locks:
14911a26580eSLuigi Rizzo  *	lock(i); wake(i); unlock(i); lock(core) wake(N+1) unlock(core)
149264ae02c3SLuigi Rizzo  * work_done is non-null on the RX path.
14931a26580eSLuigi Rizzo  */
1494babc7c12SLuigi Rizzo int
1495babc7c12SLuigi Rizzo netmap_rx_irq(struct ifnet *ifp, int q, int *work_done)
14961a26580eSLuigi Rizzo {
14971a26580eSLuigi Rizzo 	struct netmap_adapter *na;
14981a26580eSLuigi Rizzo 	struct netmap_kring *r;
149964ae02c3SLuigi Rizzo 	NM_SELINFO_T *main_wq;
15001a26580eSLuigi Rizzo 
15011a26580eSLuigi Rizzo 	if (!(ifp->if_capenable & IFCAP_NETMAP))
15021a26580eSLuigi Rizzo 		return 0;
15031a26580eSLuigi Rizzo 	na = NA(ifp);
150464ae02c3SLuigi Rizzo 	if (work_done) { /* RX path */
150564ae02c3SLuigi Rizzo 		r = na->rx_rings + q;
150664ae02c3SLuigi Rizzo 		r->nr_kflags |= NKR_PENDINTR;
1507d76bf4ffSLuigi Rizzo 		main_wq = (na->num_rx_rings > 1) ? &na->rx_si : NULL;
150864ae02c3SLuigi Rizzo 	} else { /* tx path */
150964ae02c3SLuigi Rizzo 		r = na->tx_rings + q;
1510d76bf4ffSLuigi Rizzo 		main_wq = (na->num_tx_rings > 1) ? &na->tx_si : NULL;
151164ae02c3SLuigi Rizzo 		work_done = &q; /* dummy */
151264ae02c3SLuigi Rizzo 	}
15131a26580eSLuigi Rizzo 	if (na->separate_locks) {
151464ae02c3SLuigi Rizzo 		mtx_lock(&r->q_lock);
151564ae02c3SLuigi Rizzo 		selwakeuppri(&r->si, PI_NET);
151664ae02c3SLuigi Rizzo 		mtx_unlock(&r->q_lock);
151764ae02c3SLuigi Rizzo 		if (main_wq) {
15181a26580eSLuigi Rizzo 			mtx_lock(&na->core_lock);
151964ae02c3SLuigi Rizzo 			selwakeuppri(main_wq, PI_NET);
15201a26580eSLuigi Rizzo 			mtx_unlock(&na->core_lock);
15211a26580eSLuigi Rizzo 		}
15221a26580eSLuigi Rizzo 	} else {
15231a26580eSLuigi Rizzo 		mtx_lock(&na->core_lock);
152464ae02c3SLuigi Rizzo 		selwakeuppri(&r->si, PI_NET);
152564ae02c3SLuigi Rizzo 		if (main_wq)
152664ae02c3SLuigi Rizzo 			selwakeuppri(main_wq, PI_NET);
15271a26580eSLuigi Rizzo 		mtx_unlock(&na->core_lock);
15281a26580eSLuigi Rizzo 	}
15291a26580eSLuigi Rizzo 	*work_done = 1; /* do not fire napi again */
15301a26580eSLuigi Rizzo 	return 1;
15311a26580eSLuigi Rizzo }
15321a26580eSLuigi Rizzo 
153364ae02c3SLuigi Rizzo 
1534babc7c12SLuigi Rizzo static struct cdevsw netmap_cdevsw = {
1535babc7c12SLuigi Rizzo 	.d_version = D_VERSION,
1536babc7c12SLuigi Rizzo 	.d_name = "netmap",
1537babc7c12SLuigi Rizzo 	.d_mmap = netmap_mmap,
1538babc7c12SLuigi Rizzo 	.d_ioctl = netmap_ioctl,
1539babc7c12SLuigi Rizzo 	.d_poll = netmap_poll,
1540babc7c12SLuigi Rizzo };
1541babc7c12SLuigi Rizzo 
1542*f196ce38SLuigi Rizzo #ifdef NM_BRIDGE
1543*f196ce38SLuigi Rizzo /*
1544*f196ce38SLuigi Rizzo  *---- support for virtual bridge -----
1545*f196ce38SLuigi Rizzo  */
1546*f196ce38SLuigi Rizzo 
1547*f196ce38SLuigi Rizzo /* ----- FreeBSD if_bridge hash function ------- */
1548*f196ce38SLuigi Rizzo 
1549*f196ce38SLuigi Rizzo /*
1550*f196ce38SLuigi Rizzo  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
1551*f196ce38SLuigi Rizzo  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
1552*f196ce38SLuigi Rizzo  *
1553*f196ce38SLuigi Rizzo  * http://www.burtleburtle.net/bob/hash/spooky.html
1554*f196ce38SLuigi Rizzo  */
1555*f196ce38SLuigi Rizzo #define mix(a, b, c)                                                    \
1556*f196ce38SLuigi Rizzo do {                                                                    \
1557*f196ce38SLuigi Rizzo         a -= b; a -= c; a ^= (c >> 13);                                 \
1558*f196ce38SLuigi Rizzo         b -= c; b -= a; b ^= (a << 8);                                  \
1559*f196ce38SLuigi Rizzo         c -= a; c -= b; c ^= (b >> 13);                                 \
1560*f196ce38SLuigi Rizzo         a -= b; a -= c; a ^= (c >> 12);                                 \
1561*f196ce38SLuigi Rizzo         b -= c; b -= a; b ^= (a << 16);                                 \
1562*f196ce38SLuigi Rizzo         c -= a; c -= b; c ^= (b >> 5);                                  \
1563*f196ce38SLuigi Rizzo         a -= b; a -= c; a ^= (c >> 3);                                  \
1564*f196ce38SLuigi Rizzo         b -= c; b -= a; b ^= (a << 10);                                 \
1565*f196ce38SLuigi Rizzo         c -= a; c -= b; c ^= (b >> 15);                                 \
1566*f196ce38SLuigi Rizzo } while (/*CONSTCOND*/0)
1567*f196ce38SLuigi Rizzo 
1568*f196ce38SLuigi Rizzo static __inline uint32_t
1569*f196ce38SLuigi Rizzo nm_bridge_rthash(const uint8_t *addr)
1570*f196ce38SLuigi Rizzo {
1571*f196ce38SLuigi Rizzo         uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = 0; // hask key
1572*f196ce38SLuigi Rizzo 
1573*f196ce38SLuigi Rizzo         b += addr[5] << 8;
1574*f196ce38SLuigi Rizzo         b += addr[4];
1575*f196ce38SLuigi Rizzo         a += addr[3] << 24;
1576*f196ce38SLuigi Rizzo         a += addr[2] << 16;
1577*f196ce38SLuigi Rizzo         a += addr[1] << 8;
1578*f196ce38SLuigi Rizzo         a += addr[0];
1579*f196ce38SLuigi Rizzo 
1580*f196ce38SLuigi Rizzo         mix(a, b, c);
1581*f196ce38SLuigi Rizzo #define BRIDGE_RTHASH_MASK	(NM_BDG_HASH-1)
1582*f196ce38SLuigi Rizzo         return (c & BRIDGE_RTHASH_MASK);
1583*f196ce38SLuigi Rizzo }
1584*f196ce38SLuigi Rizzo 
1585*f196ce38SLuigi Rizzo #undef mix
1586*f196ce38SLuigi Rizzo 
1587*f196ce38SLuigi Rizzo 
1588*f196ce38SLuigi Rizzo static int
1589*f196ce38SLuigi Rizzo bdg_netmap_reg(struct ifnet *ifp, int onoff)
1590*f196ce38SLuigi Rizzo {
1591*f196ce38SLuigi Rizzo 	int i, err = 0;
1592*f196ce38SLuigi Rizzo 	struct nm_bridge *b = ifp->if_bridge;
1593*f196ce38SLuigi Rizzo 
1594*f196ce38SLuigi Rizzo 	BDG_LOCK(b);
1595*f196ce38SLuigi Rizzo 	if (onoff) {
1596*f196ce38SLuigi Rizzo 		/* the interface must be already in the list.
1597*f196ce38SLuigi Rizzo 		 * only need to mark the port as active
1598*f196ce38SLuigi Rizzo 		 */
1599*f196ce38SLuigi Rizzo 		ND("should attach %s to the bridge", ifp->if_xname);
1600*f196ce38SLuigi Rizzo 		for (i=0; i < NM_BDG_MAXPORTS; i++)
1601*f196ce38SLuigi Rizzo 			if (b->bdg_ports[i] == ifp)
1602*f196ce38SLuigi Rizzo 				break;
1603*f196ce38SLuigi Rizzo 		if (i == NM_BDG_MAXPORTS) {
1604*f196ce38SLuigi Rizzo 			D("no more ports available");
1605*f196ce38SLuigi Rizzo 			err = EINVAL;
1606*f196ce38SLuigi Rizzo 			goto done;
1607*f196ce38SLuigi Rizzo 		}
1608*f196ce38SLuigi Rizzo 		ND("setting %s in netmap mode", ifp->if_xname);
1609*f196ce38SLuigi Rizzo 		ifp->if_capenable |= IFCAP_NETMAP;
1610*f196ce38SLuigi Rizzo 		NA(ifp)->bdg_port = i;
1611*f196ce38SLuigi Rizzo 		b->act_ports |= (1<<i);
1612*f196ce38SLuigi Rizzo 		b->bdg_ports[i] = ifp;
1613*f196ce38SLuigi Rizzo 	} else {
1614*f196ce38SLuigi Rizzo 		/* should be in the list, too -- remove from the mask */
1615*f196ce38SLuigi Rizzo 		ND("removing %s from netmap mode", ifp->if_xname);
1616*f196ce38SLuigi Rizzo 		ifp->if_capenable &= ~IFCAP_NETMAP;
1617*f196ce38SLuigi Rizzo 		i = NA(ifp)->bdg_port;
1618*f196ce38SLuigi Rizzo 		b->act_ports &= ~(1<<i);
1619*f196ce38SLuigi Rizzo 	}
1620*f196ce38SLuigi Rizzo done:
1621*f196ce38SLuigi Rizzo 	BDG_UNLOCK(b);
1622*f196ce38SLuigi Rizzo 	return err;
1623*f196ce38SLuigi Rizzo }
1624*f196ce38SLuigi Rizzo 
1625*f196ce38SLuigi Rizzo 
1626*f196ce38SLuigi Rizzo static int
1627*f196ce38SLuigi Rizzo nm_bdg_flush(struct nm_bdg_fwd *ft, int n, struct ifnet *ifp)
1628*f196ce38SLuigi Rizzo {
1629*f196ce38SLuigi Rizzo 	int i, ifn;
1630*f196ce38SLuigi Rizzo 	uint64_t all_dst, dst;
1631*f196ce38SLuigi Rizzo 	uint32_t sh, dh;
1632*f196ce38SLuigi Rizzo 	uint64_t mysrc = 1 << NA(ifp)->bdg_port;
1633*f196ce38SLuigi Rizzo 	uint64_t smac, dmac;
1634*f196ce38SLuigi Rizzo 	struct netmap_slot *slot;
1635*f196ce38SLuigi Rizzo 	struct nm_bridge *b = ifp->if_bridge;
1636*f196ce38SLuigi Rizzo 
1637*f196ce38SLuigi Rizzo 	ND("prepare to send %d packets, act_ports 0x%x", n, b->act_ports);
1638*f196ce38SLuigi Rizzo 	/* only consider valid destinations */
1639*f196ce38SLuigi Rizzo 	all_dst = (b->act_ports & ~mysrc);
1640*f196ce38SLuigi Rizzo 	/* first pass: hash and find destinations */
1641*f196ce38SLuigi Rizzo 	for (i = 0; likely(i < n); i++) {
1642*f196ce38SLuigi Rizzo 		uint8_t *buf = ft[i].buf;
1643*f196ce38SLuigi Rizzo 		dmac = le64toh(*(uint64_t *)(buf)) & 0xffffffffffff;
1644*f196ce38SLuigi Rizzo 		smac = le64toh(*(uint64_t *)(buf + 4));
1645*f196ce38SLuigi Rizzo 		smac >>= 16;
1646*f196ce38SLuigi Rizzo 		if (unlikely(netmap_verbose)) {
1647*f196ce38SLuigi Rizzo 		    uint8_t *s = buf+6, *d = buf;
1648*f196ce38SLuigi Rizzo 		    D("%d len %4d %02x:%02x:%02x:%02x:%02x:%02x -> %02x:%02x:%02x:%02x:%02x:%02x",
1649*f196ce38SLuigi Rizzo 			i,
1650*f196ce38SLuigi Rizzo 			ft[i].len,
1651*f196ce38SLuigi Rizzo 			s[0], s[1], s[2], s[3], s[4], s[5],
1652*f196ce38SLuigi Rizzo 			d[0], d[1], d[2], d[3], d[4], d[5]);
1653*f196ce38SLuigi Rizzo 		}
1654*f196ce38SLuigi Rizzo 		/*
1655*f196ce38SLuigi Rizzo 		 * The hash is somewhat expensive, there might be some
1656*f196ce38SLuigi Rizzo 		 * worthwhile optimizations here.
1657*f196ce38SLuigi Rizzo 		 */
1658*f196ce38SLuigi Rizzo 		if ((buf[6] & 1) == 0) { /* valid src */
1659*f196ce38SLuigi Rizzo 		    	uint8_t *s = buf+6;
1660*f196ce38SLuigi Rizzo 			sh = nm_bridge_rthash(buf+6); // XXX hash of source
1661*f196ce38SLuigi Rizzo 			/* update source port forwarding entry */
1662*f196ce38SLuigi Rizzo 			b->ht[sh].mac = smac;	/* XXX expire ? */
1663*f196ce38SLuigi Rizzo 			b->ht[sh].ports = mysrc;
1664*f196ce38SLuigi Rizzo 			if (netmap_verbose)
1665*f196ce38SLuigi Rizzo 			    D("src %02x:%02x:%02x:%02x:%02x:%02x on port %d",
1666*f196ce38SLuigi Rizzo 				s[0], s[1], s[2], s[3], s[4], s[5], NA(ifp)->bdg_port);
1667*f196ce38SLuigi Rizzo 		}
1668*f196ce38SLuigi Rizzo 		dst = 0;
1669*f196ce38SLuigi Rizzo 		if ( (buf[0] & 1) == 0) { /* unicast */
1670*f196ce38SLuigi Rizzo 		    	uint8_t *d = buf;
1671*f196ce38SLuigi Rizzo 			dh = nm_bridge_rthash(buf); // XXX hash of dst
1672*f196ce38SLuigi Rizzo 			if (b->ht[dh].mac == dmac) {	/* found dst */
1673*f196ce38SLuigi Rizzo 				dst = b->ht[dh].ports;
1674*f196ce38SLuigi Rizzo 				if (netmap_verbose)
1675*f196ce38SLuigi Rizzo 				    D("dst %02x:%02x:%02x:%02x:%02x:%02x to port %x",
1676*f196ce38SLuigi Rizzo 					d[0], d[1], d[2], d[3], d[4], d[5], (uint32_t)(dst >> 16));
1677*f196ce38SLuigi Rizzo 			}
1678*f196ce38SLuigi Rizzo 		}
1679*f196ce38SLuigi Rizzo 		if (dst == 0)
1680*f196ce38SLuigi Rizzo 			dst = all_dst;
1681*f196ce38SLuigi Rizzo 		dst &= all_dst; /* only consider valid ports */
1682*f196ce38SLuigi Rizzo 		if (unlikely(netmap_verbose))
1683*f196ce38SLuigi Rizzo 			D("pkt goes to ports 0x%x", (uint32_t)dst);
1684*f196ce38SLuigi Rizzo 		ft[i].dst = dst;
1685*f196ce38SLuigi Rizzo 	}
1686*f196ce38SLuigi Rizzo 
1687*f196ce38SLuigi Rizzo 	/* second pass, scan interfaces and forward */
1688*f196ce38SLuigi Rizzo 	all_dst = (b->act_ports & ~mysrc);
1689*f196ce38SLuigi Rizzo 	for (ifn = 0; all_dst; ifn++) {
1690*f196ce38SLuigi Rizzo 		struct ifnet *dst_ifp = b->bdg_ports[ifn];
1691*f196ce38SLuigi Rizzo 		struct netmap_adapter *na;
1692*f196ce38SLuigi Rizzo 		struct netmap_kring *kring;
1693*f196ce38SLuigi Rizzo 		struct netmap_ring *ring;
1694*f196ce38SLuigi Rizzo 		int j, lim, sent, locked;
1695*f196ce38SLuigi Rizzo 
1696*f196ce38SLuigi Rizzo 		if (!dst_ifp)
1697*f196ce38SLuigi Rizzo 			continue;
1698*f196ce38SLuigi Rizzo 		ND("scan port %d %s", ifn, dst_ifp->if_xname);
1699*f196ce38SLuigi Rizzo 		dst = 1 << ifn;
1700*f196ce38SLuigi Rizzo 		if ((dst & all_dst) == 0)	/* skip if not set */
1701*f196ce38SLuigi Rizzo 			continue;
1702*f196ce38SLuigi Rizzo 		all_dst &= ~dst;	/* clear current node */
1703*f196ce38SLuigi Rizzo 		na = NA(dst_ifp);
1704*f196ce38SLuigi Rizzo 
1705*f196ce38SLuigi Rizzo 		ring = NULL;
1706*f196ce38SLuigi Rizzo 		kring = NULL;
1707*f196ce38SLuigi Rizzo 		lim = sent = locked = 0;
1708*f196ce38SLuigi Rizzo 		/* inside, scan slots */
1709*f196ce38SLuigi Rizzo 		for (i = 0; likely(i < n); i++) {
1710*f196ce38SLuigi Rizzo 			if ((ft[i].dst & dst) == 0)
1711*f196ce38SLuigi Rizzo 				continue;	/* not here */
1712*f196ce38SLuigi Rizzo 			if (!locked) {
1713*f196ce38SLuigi Rizzo 				kring = &na->rx_rings[0];
1714*f196ce38SLuigi Rizzo 				ring = kring->ring;
1715*f196ce38SLuigi Rizzo 				lim = kring->nkr_num_slots - 1;
1716*f196ce38SLuigi Rizzo 				na->nm_lock(dst_ifp, NETMAP_RX_LOCK, 0);
1717*f196ce38SLuigi Rizzo 				locked = 1;
1718*f196ce38SLuigi Rizzo 			}
1719*f196ce38SLuigi Rizzo 			if (unlikely(kring->nr_hwavail >= lim)) {
1720*f196ce38SLuigi Rizzo 				if (netmap_verbose)
1721*f196ce38SLuigi Rizzo 					D("rx ring full on %s", ifp->if_xname);
1722*f196ce38SLuigi Rizzo 				break;
1723*f196ce38SLuigi Rizzo 			}
1724*f196ce38SLuigi Rizzo 			j = kring->nr_hwcur + kring->nr_hwavail;
1725*f196ce38SLuigi Rizzo 			if (j > lim)
1726*f196ce38SLuigi Rizzo 				j -= kring->nkr_num_slots;
1727*f196ce38SLuigi Rizzo 			slot = &ring->slot[j];
1728*f196ce38SLuigi Rizzo 			ND("send %d %d bytes at %s:%d", i, ft[i].len, dst_ifp->if_xname, j);
1729*f196ce38SLuigi Rizzo 			pkt_copy(ft[i].buf, NMB(slot), ft[i].len);
1730*f196ce38SLuigi Rizzo 			slot->len = ft[i].len;
1731*f196ce38SLuigi Rizzo 			kring->nr_hwavail++;
1732*f196ce38SLuigi Rizzo 			sent++;
1733*f196ce38SLuigi Rizzo 		}
1734*f196ce38SLuigi Rizzo 		if (locked) {
1735*f196ce38SLuigi Rizzo 			ND("sent %d on %s", sent, dst_ifp->if_xname);
1736*f196ce38SLuigi Rizzo 			if (sent)
1737*f196ce38SLuigi Rizzo 				selwakeuppri(&kring->si, PI_NET);
1738*f196ce38SLuigi Rizzo 			na->nm_lock(dst_ifp, NETMAP_RX_UNLOCK, 0);
1739*f196ce38SLuigi Rizzo 		}
1740*f196ce38SLuigi Rizzo 	}
1741*f196ce38SLuigi Rizzo 	return 0;
1742*f196ce38SLuigi Rizzo }
1743*f196ce38SLuigi Rizzo 
1744*f196ce38SLuigi Rizzo /*
1745*f196ce38SLuigi Rizzo  * main dispatch routine
1746*f196ce38SLuigi Rizzo  */
1747*f196ce38SLuigi Rizzo static int
1748*f196ce38SLuigi Rizzo bdg_netmap_txsync(struct ifnet *ifp, u_int ring_nr, int do_lock)
1749*f196ce38SLuigi Rizzo {
1750*f196ce38SLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
1751*f196ce38SLuigi Rizzo 	struct netmap_kring *kring = &na->tx_rings[ring_nr];
1752*f196ce38SLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
1753*f196ce38SLuigi Rizzo 	int i, j, k, lim = kring->nkr_num_slots - 1;
1754*f196ce38SLuigi Rizzo 	struct nm_bdg_fwd *ft = (struct nm_bdg_fwd *)(ifp + 1);
1755*f196ce38SLuigi Rizzo 	int ft_i;	/* position in the forwarding table */
1756*f196ce38SLuigi Rizzo 
1757*f196ce38SLuigi Rizzo 	k = ring->cur;
1758*f196ce38SLuigi Rizzo 	if (k > lim)
1759*f196ce38SLuigi Rizzo 		return netmap_ring_reinit(kring);
1760*f196ce38SLuigi Rizzo 	if (do_lock)
1761*f196ce38SLuigi Rizzo 		na->nm_lock(ifp, NETMAP_TX_LOCK, ring_nr);
1762*f196ce38SLuigi Rizzo 
1763*f196ce38SLuigi Rizzo 	if (netmap_bridge <= 0) { /* testing only */
1764*f196ce38SLuigi Rizzo 		j = k; // used all
1765*f196ce38SLuigi Rizzo 		goto done;
1766*f196ce38SLuigi Rizzo 	}
1767*f196ce38SLuigi Rizzo 	if (netmap_bridge > NM_BDG_BATCH)
1768*f196ce38SLuigi Rizzo 		netmap_bridge = NM_BDG_BATCH;
1769*f196ce38SLuigi Rizzo 
1770*f196ce38SLuigi Rizzo 	ft_i = 0;	/* start from 0 */
1771*f196ce38SLuigi Rizzo 	for (j = kring->nr_hwcur; likely(j != k); j = unlikely(j == lim) ? 0 : j+1) {
1772*f196ce38SLuigi Rizzo 		struct netmap_slot *slot = &ring->slot[j];
1773*f196ce38SLuigi Rizzo 		int len = ft[ft_i].len = slot->len;
1774*f196ce38SLuigi Rizzo 		char *buf = ft[ft_i].buf = NMB(slot);
1775*f196ce38SLuigi Rizzo 
1776*f196ce38SLuigi Rizzo 		prefetch(buf);
1777*f196ce38SLuigi Rizzo 		if (unlikely(len < 14))
1778*f196ce38SLuigi Rizzo 			continue;
1779*f196ce38SLuigi Rizzo 		if (unlikely(++ft_i == netmap_bridge))
1780*f196ce38SLuigi Rizzo 			ft_i = nm_bdg_flush(ft, ft_i, ifp);
1781*f196ce38SLuigi Rizzo 	}
1782*f196ce38SLuigi Rizzo 	if (ft_i)
1783*f196ce38SLuigi Rizzo 		ft_i = nm_bdg_flush(ft, ft_i, ifp);
1784*f196ce38SLuigi Rizzo 	/* count how many packets we sent */
1785*f196ce38SLuigi Rizzo 	i = k - j;
1786*f196ce38SLuigi Rizzo 	if (i < 0)
1787*f196ce38SLuigi Rizzo 		i += kring->nkr_num_slots;
1788*f196ce38SLuigi Rizzo 	kring->nr_hwavail = kring->nkr_num_slots - 1 - i;
1789*f196ce38SLuigi Rizzo 	if (j != k)
1790*f196ce38SLuigi Rizzo 		D("early break at %d/ %d, avail %d", j, k, kring->nr_hwavail);
1791*f196ce38SLuigi Rizzo 
1792*f196ce38SLuigi Rizzo done:
1793*f196ce38SLuigi Rizzo 	kring->nr_hwcur = j;
1794*f196ce38SLuigi Rizzo 	ring->avail = kring->nr_hwavail;
1795*f196ce38SLuigi Rizzo 	if (do_lock)
1796*f196ce38SLuigi Rizzo 		na->nm_lock(ifp, NETMAP_TX_UNLOCK, ring_nr);
1797*f196ce38SLuigi Rizzo 
1798*f196ce38SLuigi Rizzo 	if (netmap_verbose)
1799*f196ce38SLuigi Rizzo 		D("%s ring %d lock %d", ifp->if_xname, ring_nr, do_lock);
1800*f196ce38SLuigi Rizzo 	return 0;
1801*f196ce38SLuigi Rizzo }
1802*f196ce38SLuigi Rizzo 
1803*f196ce38SLuigi Rizzo static int
1804*f196ce38SLuigi Rizzo bdg_netmap_rxsync(struct ifnet *ifp, u_int ring_nr, int do_lock)
1805*f196ce38SLuigi Rizzo {
1806*f196ce38SLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
1807*f196ce38SLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[ring_nr];
1808*f196ce38SLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
1809*f196ce38SLuigi Rizzo 	int j, n, lim = kring->nkr_num_slots - 1;
1810*f196ce38SLuigi Rizzo 	u_int k = ring->cur, resvd = ring->reserved;
1811*f196ce38SLuigi Rizzo 
1812*f196ce38SLuigi Rizzo 	ND("%s ring %d lock %d avail %d",
1813*f196ce38SLuigi Rizzo 		ifp->if_xname, ring_nr, do_lock, kring->nr_hwavail);
1814*f196ce38SLuigi Rizzo 
1815*f196ce38SLuigi Rizzo 	if (k > lim)
1816*f196ce38SLuigi Rizzo 		return netmap_ring_reinit(kring);
1817*f196ce38SLuigi Rizzo 	if (do_lock)
1818*f196ce38SLuigi Rizzo 		na->nm_lock(ifp, NETMAP_RX_LOCK, ring_nr);
1819*f196ce38SLuigi Rizzo 
1820*f196ce38SLuigi Rizzo 	/* skip past packets that userspace has released */
1821*f196ce38SLuigi Rizzo 	j = kring->nr_hwcur;    /* netmap ring index */
1822*f196ce38SLuigi Rizzo 	if (resvd > 0) {
1823*f196ce38SLuigi Rizzo 		if (resvd + ring->avail >= lim + 1) {
1824*f196ce38SLuigi Rizzo 			D("XXX invalid reserve/avail %d %d", resvd, ring->avail);
1825*f196ce38SLuigi Rizzo 			ring->reserved = resvd = 0; // XXX panic...
1826*f196ce38SLuigi Rizzo 		}
1827*f196ce38SLuigi Rizzo 		k = (k >= resvd) ? k - resvd : k + lim + 1 - resvd;
1828*f196ce38SLuigi Rizzo 	}
1829*f196ce38SLuigi Rizzo 
1830*f196ce38SLuigi Rizzo 	if (j != k) { /* userspace has released some packets. */
1831*f196ce38SLuigi Rizzo 		n = k - j;
1832*f196ce38SLuigi Rizzo 		if (n < 0)
1833*f196ce38SLuigi Rizzo 			n += kring->nkr_num_slots;
1834*f196ce38SLuigi Rizzo 		ND("userspace releases %d packets", n);
1835*f196ce38SLuigi Rizzo                 for (n = 0; likely(j != k); n++) {
1836*f196ce38SLuigi Rizzo                         struct netmap_slot *slot = &ring->slot[j];
1837*f196ce38SLuigi Rizzo                         void *addr = NMB(slot);
1838*f196ce38SLuigi Rizzo 
1839*f196ce38SLuigi Rizzo                         if (addr == netmap_buffer_base) { /* bad buf */
1840*f196ce38SLuigi Rizzo                                 if (do_lock)
1841*f196ce38SLuigi Rizzo                                         na->nm_lock(ifp, NETMAP_RX_UNLOCK, ring_nr);
1842*f196ce38SLuigi Rizzo                                 return netmap_ring_reinit(kring);
1843*f196ce38SLuigi Rizzo                         }
1844*f196ce38SLuigi Rizzo 			/* decrease refcount for buffer */
1845*f196ce38SLuigi Rizzo 
1846*f196ce38SLuigi Rizzo 			slot->flags &= ~NS_BUF_CHANGED;
1847*f196ce38SLuigi Rizzo                         j = unlikely(j == lim) ? 0 : j + 1;
1848*f196ce38SLuigi Rizzo                 }
1849*f196ce38SLuigi Rizzo                 kring->nr_hwavail -= n;
1850*f196ce38SLuigi Rizzo                 kring->nr_hwcur = k;
1851*f196ce38SLuigi Rizzo         }
1852*f196ce38SLuigi Rizzo         /* tell userspace that there are new packets */
1853*f196ce38SLuigi Rizzo         ring->avail = kring->nr_hwavail - resvd;
1854*f196ce38SLuigi Rizzo 
1855*f196ce38SLuigi Rizzo 	if (do_lock)
1856*f196ce38SLuigi Rizzo 		na->nm_lock(ifp, NETMAP_RX_UNLOCK, ring_nr);
1857*f196ce38SLuigi Rizzo 	return 0;
1858*f196ce38SLuigi Rizzo }
1859*f196ce38SLuigi Rizzo 
1860*f196ce38SLuigi Rizzo static void
1861*f196ce38SLuigi Rizzo bdg_netmap_attach(struct ifnet *ifp)
1862*f196ce38SLuigi Rizzo {
1863*f196ce38SLuigi Rizzo 	struct netmap_adapter na;
1864*f196ce38SLuigi Rizzo 
1865*f196ce38SLuigi Rizzo 	ND("attaching virtual bridge");
1866*f196ce38SLuigi Rizzo 	bzero(&na, sizeof(na));
1867*f196ce38SLuigi Rizzo 
1868*f196ce38SLuigi Rizzo 	na.ifp = ifp;
1869*f196ce38SLuigi Rizzo 	na.separate_locks = 1;
1870*f196ce38SLuigi Rizzo 	na.num_tx_desc = NM_BRIDGE_RINGSIZE;
1871*f196ce38SLuigi Rizzo 	na.num_rx_desc = NM_BRIDGE_RINGSIZE;
1872*f196ce38SLuigi Rizzo 	na.nm_txsync = bdg_netmap_txsync;
1873*f196ce38SLuigi Rizzo 	na.nm_rxsync = bdg_netmap_rxsync;
1874*f196ce38SLuigi Rizzo 	na.nm_register = bdg_netmap_reg;
1875*f196ce38SLuigi Rizzo 	netmap_attach(&na, 1);
1876*f196ce38SLuigi Rizzo }
1877*f196ce38SLuigi Rizzo 
1878*f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */
1879babc7c12SLuigi Rizzo 
1880babc7c12SLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */
1881babc7c12SLuigi Rizzo 
1882babc7c12SLuigi Rizzo 
18831a26580eSLuigi Rizzo /*
188468b8534bSLuigi Rizzo  * Module loader.
188568b8534bSLuigi Rizzo  *
188668b8534bSLuigi Rizzo  * Create the /dev/netmap device and initialize all global
188768b8534bSLuigi Rizzo  * variables.
188868b8534bSLuigi Rizzo  *
188968b8534bSLuigi Rizzo  * Return 0 on success, errno on failure.
189068b8534bSLuigi Rizzo  */
189168b8534bSLuigi Rizzo static int
189268b8534bSLuigi Rizzo netmap_init(void)
189368b8534bSLuigi Rizzo {
189468b8534bSLuigi Rizzo 	int error;
189568b8534bSLuigi Rizzo 
189668b8534bSLuigi Rizzo 	error = netmap_memory_init();
189768b8534bSLuigi Rizzo 	if (error != 0) {
189868b8534bSLuigi Rizzo 		printf("netmap: unable to initialize the memory allocator.");
189968b8534bSLuigi Rizzo 		return (error);
190068b8534bSLuigi Rizzo 	}
190168b8534bSLuigi Rizzo 	printf("netmap: loaded module with %d Mbytes\n",
190264ae02c3SLuigi Rizzo 		(int)(nm_mem->nm_totalsize >> 20));
190368b8534bSLuigi Rizzo 	netmap_dev = make_dev(&netmap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660,
190468b8534bSLuigi Rizzo 			      "netmap");
1905*f196ce38SLuigi Rizzo 
1906*f196ce38SLuigi Rizzo #ifdef NM_BRIDGE
1907*f196ce38SLuigi Rizzo 	{
1908*f196ce38SLuigi Rizzo 	int i;
1909*f196ce38SLuigi Rizzo 	for (i = 0; i < NM_BRIDGES; i++)
1910*f196ce38SLuigi Rizzo 		mtx_init(&nm_bridges[i].bdg_lock, "bdg lock", "bdg_lock", MTX_DEF);
1911*f196ce38SLuigi Rizzo 	}
1912*f196ce38SLuigi Rizzo #endif
1913babc7c12SLuigi Rizzo 	return (error);
191468b8534bSLuigi Rizzo }
191568b8534bSLuigi Rizzo 
191668b8534bSLuigi Rizzo 
191768b8534bSLuigi Rizzo /*
191868b8534bSLuigi Rizzo  * Module unloader.
191968b8534bSLuigi Rizzo  *
192068b8534bSLuigi Rizzo  * Free all the memory, and destroy the ``/dev/netmap`` device.
192168b8534bSLuigi Rizzo  */
192268b8534bSLuigi Rizzo static void
192368b8534bSLuigi Rizzo netmap_fini(void)
192468b8534bSLuigi Rizzo {
192568b8534bSLuigi Rizzo 	destroy_dev(netmap_dev);
192668b8534bSLuigi Rizzo 	netmap_memory_fini();
192768b8534bSLuigi Rizzo 	printf("netmap: unloaded module.\n");
192868b8534bSLuigi Rizzo }
192968b8534bSLuigi Rizzo 
193068b8534bSLuigi Rizzo 
1931*f196ce38SLuigi Rizzo #ifdef __FreeBSD__
193268b8534bSLuigi Rizzo /*
193368b8534bSLuigi Rizzo  * Kernel entry point.
193468b8534bSLuigi Rizzo  *
193568b8534bSLuigi Rizzo  * Initialize/finalize the module and return.
193668b8534bSLuigi Rizzo  *
193768b8534bSLuigi Rizzo  * Return 0 on success, errno on failure.
193868b8534bSLuigi Rizzo  */
193968b8534bSLuigi Rizzo static int
194068b8534bSLuigi Rizzo netmap_loader(__unused struct module *module, int event, __unused void *arg)
194168b8534bSLuigi Rizzo {
194268b8534bSLuigi Rizzo 	int error = 0;
194368b8534bSLuigi Rizzo 
194468b8534bSLuigi Rizzo 	switch (event) {
194568b8534bSLuigi Rizzo 	case MOD_LOAD:
194668b8534bSLuigi Rizzo 		error = netmap_init();
194768b8534bSLuigi Rizzo 		break;
194868b8534bSLuigi Rizzo 
194968b8534bSLuigi Rizzo 	case MOD_UNLOAD:
195068b8534bSLuigi Rizzo 		netmap_fini();
195168b8534bSLuigi Rizzo 		break;
195268b8534bSLuigi Rizzo 
195368b8534bSLuigi Rizzo 	default:
195468b8534bSLuigi Rizzo 		error = EOPNOTSUPP;
195568b8534bSLuigi Rizzo 		break;
195668b8534bSLuigi Rizzo 	}
195768b8534bSLuigi Rizzo 
195868b8534bSLuigi Rizzo 	return (error);
195968b8534bSLuigi Rizzo }
196068b8534bSLuigi Rizzo 
196168b8534bSLuigi Rizzo 
196268b8534bSLuigi Rizzo DEV_MODULE(netmap, netmap_loader, NULL);
1963*f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */
1964