xref: /freebsd/sys/dev/netmap/netmap.c (revision 3b1f7d9e5d6f44b50ff07fde6fd0e1135f213762)
1 /*
2  * Copyright (C) 2011-2013 Matteo Landi, Luigi Rizzo. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *   1. Redistributions of source code must retain the above copyright
8  *      notice, this list of conditions and the following disclaimer.
9  *   2. Redistributions in binary form must reproduce the above copyright
10  *      notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #define NM_BRIDGE
27 
28 /*
29  * This module supports memory mapped access to network devices,
30  * see netmap(4).
31  *
32  * The module uses a large, memory pool allocated by the kernel
33  * and accessible as mmapped memory by multiple userspace threads/processes.
34  * The memory pool contains packet buffers and "netmap rings",
35  * i.e. user-accessible copies of the interface's queues.
36  *
37  * Access to the network card works like this:
38  * 1. a process/thread issues one or more open() on /dev/netmap, to create
39  *    select()able file descriptor on which events are reported.
40  * 2. on each descriptor, the process issues an ioctl() to identify
41  *    the interface that should report events to the file descriptor.
42  * 3. on each descriptor, the process issues an mmap() request to
43  *    map the shared memory region within the process' address space.
44  *    The list of interesting queues is indicated by a location in
45  *    the shared memory region.
46  * 4. using the functions in the netmap(4) userspace API, a process
47  *    can look up the occupation state of a queue, access memory buffers,
48  *    and retrieve received packets or enqueue packets to transmit.
49  * 5. using some ioctl()s the process can synchronize the userspace view
50  *    of the queue with the actual status in the kernel. This includes both
51  *    receiving the notification of new packets, and transmitting new
52  *    packets on the output interface.
53  * 6. select() or poll() can be used to wait for events on individual
54  *    transmit or receive queues (or all queues for a given interface).
55  */
56 
57 #ifdef linux
58 #include "bsd_glue.h"
59 static netdev_tx_t linux_netmap_start(struct sk_buff *skb, struct net_device *dev);
60 #endif /* linux */
61 
62 #ifdef __APPLE__
63 #include "osx_glue.h"
64 #endif /* __APPLE__ */
65 
66 #ifdef __FreeBSD__
67 #include <sys/cdefs.h> /* prerequisite */
68 __FBSDID("$FreeBSD$");
69 
70 #include <sys/types.h>
71 #include <sys/module.h>
72 #include <sys/errno.h>
73 #include <sys/param.h>	/* defines used in kernel.h */
74 #include <sys/jail.h>
75 #include <sys/kernel.h>	/* types used in module initialization */
76 #include <sys/conf.h>	/* cdevsw struct */
77 #include <sys/uio.h>	/* uio struct */
78 #include <sys/sockio.h>
79 #include <sys/socketvar.h>	/* struct socket */
80 #include <sys/malloc.h>
81 #include <sys/mman.h>	/* PROT_EXEC */
82 #include <sys/poll.h>
83 #include <sys/proc.h>
84 #include <sys/rwlock.h>
85 #include <vm/vm.h>	/* vtophys */
86 #include <vm/pmap.h>	/* vtophys */
87 #include <sys/socket.h> /* sockaddrs */
88 #include <machine/bus.h>
89 #include <sys/selinfo.h>
90 #include <sys/sysctl.h>
91 #include <net/if.h>
92 #include <net/bpf.h>		/* BIOCIMMEDIATE */
93 #include <net/vnet.h>
94 #include <machine/bus.h>	/* bus_dmamap_* */
95 
96 MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map");
97 #endif /* __FreeBSD__ */
98 
99 #include <net/netmap.h>
100 #include <dev/netmap/netmap_kern.h>
101 
102 /* XXX the following variables must be deprecated and included in nm_mem */
103 u_int netmap_total_buffers;
104 u_int netmap_buf_size;
105 char *netmap_buffer_base;	/* address of an invalid buffer */
106 
107 /* user-controlled variables */
108 int netmap_verbose;
109 
110 static int netmap_no_timestamp; /* don't timestamp on rxsync */
111 
112 SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args");
113 SYSCTL_INT(_dev_netmap, OID_AUTO, verbose,
114     CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode");
115 SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp,
116     CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp");
117 int netmap_mitigate = 1;
118 SYSCTL_INT(_dev_netmap, OID_AUTO, mitigate, CTLFLAG_RW, &netmap_mitigate, 0, "");
119 int netmap_no_pendintr = 1;
120 SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr,
121     CTLFLAG_RW, &netmap_no_pendintr, 0, "Always look for new received packets.");
122 int netmap_txsync_retry = 2;
123 SYSCTL_INT(_dev_netmap, OID_AUTO, txsync_retry, CTLFLAG_RW,
124     &netmap_txsync_retry, 0 , "Number of txsync loops in bridge's flush.");
125 
126 int netmap_drop = 0;	/* debugging */
127 int netmap_flags = 0;	/* debug flags */
128 int netmap_fwd = 0;	/* force transparent mode */
129 
130 SYSCTL_INT(_dev_netmap, OID_AUTO, drop, CTLFLAG_RW, &netmap_drop, 0 , "");
131 SYSCTL_INT(_dev_netmap, OID_AUTO, flags, CTLFLAG_RW, &netmap_flags, 0 , "");
132 SYSCTL_INT(_dev_netmap, OID_AUTO, fwd, CTLFLAG_RW, &netmap_fwd, 0 , "");
133 
134 #ifdef NM_BRIDGE /* support for netmap virtual switch, called VALE */
135 
136 /*
137  * system parameters (most of them in netmap_kern.h)
138  * NM_NAME	prefix for switch port names, default "vale"
139  * NM_MAXPORTS	number of ports
140  * NM_BRIDGES	max number of switches in the system.
141  *	XXX should become a sysctl or tunable
142  *
143  * Switch ports are named valeX:Y where X is the switch name and Y
144  * is the port. If Y matches a physical interface name, the port is
145  * connected to a physical device.
146  *
147  * Unlike physical interfaces, switch ports use their own memory region
148  * for rings and buffers.
149  * The virtual interfaces use per-queue lock instead of core lock.
150  * In the tx loop, we aggregate traffic in batches to make all operations
151  * faster. The batch size is NM_BDG_BATCH
152  */
153 #define NM_BDG_MAXRINGS		16	/* XXX unclear how many. */
154 #define NM_BRIDGE_RINGSIZE	1024	/* in the device */
155 #define NM_BDG_HASH		1024	/* forwarding table entries */
156 #define NM_BDG_BATCH		1024	/* entries in the forwarding buffer */
157 #define	NM_BRIDGES		8	/* number of bridges */
158 
159 
160 int netmap_bridge = NM_BDG_BATCH; /* bridge batch size */
161 SYSCTL_INT(_dev_netmap, OID_AUTO, bridge, CTLFLAG_RW, &netmap_bridge, 0 , "");
162 
163 #ifdef linux
164 
165 #define	refcount_acquire(_a)	atomic_add(1, (atomic_t *)_a)
166 #define	refcount_release(_a)	atomic_dec_and_test((atomic_t *)_a)
167 
168 #else /* !linux */
169 
170 #ifdef __FreeBSD__
171 #include <sys/endian.h>
172 #include <sys/refcount.h>
173 #endif /* __FreeBSD__ */
174 
175 #define prefetch(x)	__builtin_prefetch(x)
176 
177 #endif /* !linux */
178 
179 /*
180  * These are used to handle reference counters for bridge ports.
181  */
182 #define	ADD_BDG_REF(ifp)	refcount_acquire(&NA(ifp)->na_bdg_refcount)
183 #define	DROP_BDG_REF(ifp)	refcount_release(&NA(ifp)->na_bdg_refcount)
184 
185 static void bdg_netmap_attach(struct netmap_adapter *);
186 static int bdg_netmap_reg(struct ifnet *ifp, int onoff);
187 static int kern_netmap_regif(struct nmreq *nmr);
188 
189 /* per-tx-queue entry */
190 struct nm_bdg_fwd {	/* forwarding entry for a bridge */
191 	void *ft_buf;
192 	uint16_t _ft_dst;	/* dst port, unused */
193 	uint16_t ft_flags;	/* flags, e.g. indirect */
194 	uint16_t ft_len;	/* src len */
195 	uint16_t ft_next;	/* next packet to same destination */
196 };
197 
198 /* We need to build a list of buffers going to each destination.
199  * Each buffer is in one entry of struct nm_bdg_fwd, we use ft_next
200  * to build the list, and struct nm_bdg_q below for the queue.
201  * The structure should compact because potentially we have a lot
202  * of destinations.
203  */
204 struct nm_bdg_q {
205 	uint16_t bq_head;
206 	uint16_t bq_tail;
207 };
208 
209 struct nm_hash_ent {
210 	uint64_t	mac;	/* the top 2 bytes are the epoch */
211 	uint64_t	ports;
212 };
213 
214 /*
215  * Interfaces for a bridge are all in bdg_ports[].
216  * The array has fixed size, an empty entry does not terminate
217  * the search. But lookups only occur on attach/detach so we
218  * don't mind if they are slow.
219  *
220  * The bridge is non blocking on the transmit ports.
221  *
222  * bdg_lock protects accesses to the bdg_ports array.
223  * This is a rw lock (or equivalent).
224  */
225 struct nm_bridge {
226 	int namelen;	/* 0 means free */
227 
228 	/* XXX what is the proper alignment/layout ? */
229 	NM_RWLOCK_T bdg_lock;	/* protects bdg_ports */
230 	struct netmap_adapter *bdg_ports[NM_BDG_MAXPORTS];
231 
232 	char basename[IFNAMSIZ];
233 	/*
234 	 * The function to decide the destination port.
235 	 * It returns either of an index of the destination port,
236 	 * NM_BDG_BROADCAST to broadcast this packet, or NM_BDG_NOPORT not to
237 	 * forward this packet.  ring_nr is the source ring index, and the
238 	 * function may overwrite this value to forward this packet to a
239 	 * different ring index.
240 	 * This function must be set by netmap_bdgctl().
241 	 */
242 	bdg_lookup_fn_t nm_bdg_lookup;
243 
244 	/* the forwarding table, MAC+ports */
245 	struct nm_hash_ent ht[NM_BDG_HASH];
246 };
247 
248 struct nm_bridge nm_bridges[NM_BRIDGES];
249 NM_LOCK_T	netmap_bridge_mutex;
250 
251 /* other OS will have these macros defined in their own glue code. */
252 
253 #ifdef __FreeBSD__
254 #define BDG_LOCK()		mtx_lock(&netmap_bridge_mutex)
255 #define BDG_UNLOCK()		mtx_unlock(&netmap_bridge_mutex)
256 #define BDG_WLOCK(b)		rw_wlock(&(b)->bdg_lock)
257 #define BDG_WUNLOCK(b)		rw_wunlock(&(b)->bdg_lock)
258 #define BDG_RLOCK(b)		rw_rlock(&(b)->bdg_lock)
259 #define BDG_RUNLOCK(b)		rw_runlock(&(b)->bdg_lock)
260 
261 /* set/get variables. OS-specific macros may wrap these
262  * assignments into read/write lock or similar
263  */
264 #define BDG_SET_VAR(lval, p)	(lval = p)
265 #define BDG_GET_VAR(lval)	(lval)
266 #define BDG_FREE(p)		free(p, M_DEVBUF)
267 #endif /* __FreeBSD__ */
268 
269 static __inline int
270 nma_is_vp(struct netmap_adapter *na)
271 {
272 	return na->nm_register == bdg_netmap_reg;
273 }
274 static __inline int
275 nma_is_host(struct netmap_adapter *na)
276 {
277 	return na->nm_register == NULL;
278 }
279 static __inline int
280 nma_is_hw(struct netmap_adapter *na)
281 {
282 	/* In case of sw adapter, nm_register is NULL */
283 	return !nma_is_vp(na) && !nma_is_host(na);
284 }
285 
286 /*
287  * Regarding holding a NIC, if the NIC is owned by the kernel
288  * (i.e., bridge), neither another bridge nor user can use it;
289  * if the NIC is owned by a user, only users can share it.
290  * Evaluation must be done under NMA_LOCK().
291  */
292 #define NETMAP_OWNED_BY_KERN(ifp)	(!nma_is_vp(NA(ifp)) && NA(ifp)->na_bdg)
293 #define NETMAP_OWNED_BY_ANY(ifp) \
294 	(NETMAP_OWNED_BY_KERN(ifp) || (NA(ifp)->refcount > 0))
295 
296 /*
297  * NA(ifp)->bdg_port	port index
298  */
299 
300 // XXX only for multiples of 64 bytes, non overlapped.
301 static inline void
302 pkt_copy(void *_src, void *_dst, int l)
303 {
304         uint64_t *src = _src;
305         uint64_t *dst = _dst;
306         if (unlikely(l >= 1024)) {
307                 bcopy(src, dst, l);
308                 return;
309         }
310         for (; likely(l > 0); l-=64) {
311                 *dst++ = *src++;
312                 *dst++ = *src++;
313                 *dst++ = *src++;
314                 *dst++ = *src++;
315                 *dst++ = *src++;
316                 *dst++ = *src++;
317                 *dst++ = *src++;
318                 *dst++ = *src++;
319         }
320 }
321 
322 
323 /*
324  * locate a bridge among the existing ones.
325  * a ':' in the name terminates the bridge name. Otherwise, just NM_NAME.
326  * We assume that this is called with a name of at least NM_NAME chars.
327  */
328 static struct nm_bridge *
329 nm_find_bridge(const char *name, int create)
330 {
331 	int i, l, namelen;
332 	struct nm_bridge *b = NULL;
333 
334 	namelen = strlen(NM_NAME);	/* base length */
335 	l = strlen(name);		/* actual length */
336 	for (i = namelen + 1; i < l; i++) {
337 		if (name[i] == ':') {
338 			namelen = i;
339 			break;
340 		}
341 	}
342 	if (namelen >= IFNAMSIZ)
343 		namelen = IFNAMSIZ;
344 	ND("--- prefix is '%.*s' ---", namelen, name);
345 
346 	BDG_LOCK();
347 	/* lookup the name, remember empty slot if there is one */
348 	for (i = 0; i < NM_BRIDGES; i++) {
349 		struct nm_bridge *x = nm_bridges + i;
350 
351 		if (x->namelen == 0) {
352 			if (create && b == NULL)
353 				b = x;	/* record empty slot */
354 		} else if (x->namelen != namelen) {
355 			continue;
356 		} else if (strncmp(name, x->basename, namelen) == 0) {
357 			ND("found '%.*s' at %d", namelen, name, i);
358 			b = x;
359 			break;
360 		}
361 	}
362 	if (i == NM_BRIDGES && b) { /* name not found, can create entry */
363 		strncpy(b->basename, name, namelen);
364 		b->namelen = namelen;
365 		/* set the default function */
366 		b->nm_bdg_lookup = netmap_bdg_learning;
367 		/* reset the MAC address table */
368 		bzero(b->ht, sizeof(struct nm_hash_ent) * NM_BDG_HASH);
369 	}
370 	BDG_UNLOCK();
371 	return b;
372 }
373 
374 
375 /*
376  * Free the forwarding tables for rings attached to switch ports.
377  */
378 static void
379 nm_free_bdgfwd(struct netmap_adapter *na)
380 {
381 	int nrings, i;
382 	struct netmap_kring *kring;
383 
384 	nrings = nma_is_vp(na) ? na->num_tx_rings : na->num_rx_rings;
385 	kring = nma_is_vp(na) ? na->tx_rings : na->rx_rings;
386 	for (i = 0; i < nrings; i++) {
387 		if (kring[i].nkr_ft) {
388 			free(kring[i].nkr_ft, M_DEVBUF);
389 			kring[i].nkr_ft = NULL; /* protect from freeing twice */
390 		}
391 	}
392 	if (nma_is_hw(na))
393 		nm_free_bdgfwd(SWNA(na->ifp));
394 }
395 
396 
397 /*
398  * Allocate the forwarding tables for the rings attached to the bridge ports.
399  */
400 static int
401 nm_alloc_bdgfwd(struct netmap_adapter *na)
402 {
403 	int nrings, l, i, num_dstq;
404 	struct netmap_kring *kring;
405 
406 	/* all port:rings + broadcast */
407 	num_dstq = NM_BDG_MAXPORTS * NM_BDG_MAXRINGS + 1;
408 	l = sizeof(struct nm_bdg_fwd) * NM_BDG_BATCH;
409 	l += sizeof(struct nm_bdg_q) * num_dstq;
410 	l += sizeof(uint16_t) * NM_BDG_BATCH;
411 
412 	nrings = nma_is_vp(na) ? na->num_tx_rings : na->num_rx_rings;
413 	kring = nma_is_vp(na) ? na->tx_rings : na->rx_rings;
414 	for (i = 0; i < nrings; i++) {
415 		struct nm_bdg_fwd *ft;
416 		struct nm_bdg_q *dstq;
417 		int j;
418 
419 		ft = malloc(l, M_DEVBUF, M_NOWAIT | M_ZERO);
420 		if (!ft) {
421 			nm_free_bdgfwd(na);
422 			return ENOMEM;
423 		}
424 		dstq = (struct nm_bdg_q *)(ft + NM_BDG_BATCH);
425 		for (j = 0; j < num_dstq; j++)
426 			dstq[j].bq_head = dstq[j].bq_tail = NM_BDG_BATCH;
427 		kring[i].nkr_ft = ft;
428 	}
429 	if (nma_is_hw(na))
430 		nm_alloc_bdgfwd(SWNA(na->ifp));
431 	return 0;
432 }
433 
434 #endif /* NM_BRIDGE */
435 
436 
437 /*
438  * Fetch configuration from the device, to cope with dynamic
439  * reconfigurations after loading the module.
440  */
441 static int
442 netmap_update_config(struct netmap_adapter *na)
443 {
444 	struct ifnet *ifp = na->ifp;
445 	u_int txr, txd, rxr, rxd;
446 
447 	txr = txd = rxr = rxd = 0;
448 	if (na->nm_config) {
449 		na->nm_config(ifp, &txr, &txd, &rxr, &rxd);
450 	} else {
451 		/* take whatever we had at init time */
452 		txr = na->num_tx_rings;
453 		txd = na->num_tx_desc;
454 		rxr = na->num_rx_rings;
455 		rxd = na->num_rx_desc;
456 	}
457 
458 	if (na->num_tx_rings == txr && na->num_tx_desc == txd &&
459 	    na->num_rx_rings == rxr && na->num_rx_desc == rxd)
460 		return 0; /* nothing changed */
461 	if (netmap_verbose || na->refcount > 0) {
462 		D("stored config %s: txring %d x %d, rxring %d x %d",
463 			ifp->if_xname,
464 			na->num_tx_rings, na->num_tx_desc,
465 			na->num_rx_rings, na->num_rx_desc);
466 		D("new config %s: txring %d x %d, rxring %d x %d",
467 			ifp->if_xname, txr, txd, rxr, rxd);
468 	}
469 	if (na->refcount == 0) {
470 		D("configuration changed (but fine)");
471 		na->num_tx_rings = txr;
472 		na->num_tx_desc = txd;
473 		na->num_rx_rings = rxr;
474 		na->num_rx_desc = rxd;
475 		return 0;
476 	}
477 	D("configuration changed while active, this is bad...");
478 	return 1;
479 }
480 
481 /*------------- memory allocator -----------------*/
482 #include "netmap_mem2.c"
483 /*------------ end of memory allocator ----------*/
484 
485 
486 /* Structure associated to each thread which registered an interface.
487  *
488  * The first 4 fields of this structure are written by NIOCREGIF and
489  * read by poll() and NIOC?XSYNC.
490  * There is low contention among writers (actually, a correct user program
491  * should have no contention among writers) and among writers and readers,
492  * so we use a single global lock to protect the structure initialization.
493  * Since initialization involves the allocation of memory, we reuse the memory
494  * allocator lock.
495  * Read access to the structure is lock free. Readers must check that
496  * np_nifp is not NULL before using the other fields.
497  * If np_nifp is NULL initialization has not been performed, so they should
498  * return an error to userlevel.
499  *
500  * The ref_done field is used to regulate access to the refcount in the
501  * memory allocator. The refcount must be incremented at most once for
502  * each open("/dev/netmap"). The increment is performed by the first
503  * function that calls netmap_get_memory() (currently called by
504  * mmap(), NIOCGINFO and NIOCREGIF).
505  * If the refcount is incremented, it is then decremented when the
506  * private structure is destroyed.
507  */
508 struct netmap_priv_d {
509 	struct netmap_if * volatile np_nifp;	/* netmap interface descriptor. */
510 
511 	struct ifnet	*np_ifp;	/* device for which we hold a reference */
512 	int		np_ringid;	/* from the ioctl */
513 	u_int		np_qfirst, np_qlast;	/* range of rings to scan */
514 	uint16_t	np_txpoll;
515 
516 	unsigned long	ref_done;	/* use with NMA_LOCK held */
517 };
518 
519 
520 static int
521 netmap_get_memory(struct netmap_priv_d* p)
522 {
523 	int error = 0;
524 	NMA_LOCK();
525 	if (!p->ref_done) {
526 		error = netmap_memory_finalize();
527 		if (!error)
528 			p->ref_done = 1;
529 	}
530 	NMA_UNLOCK();
531 	return error;
532 }
533 
534 /*
535  * File descriptor's private data destructor.
536  *
537  * Call nm_register(ifp,0) to stop netmap mode on the interface and
538  * revert to normal operation. We expect that np_ifp has not gone.
539  */
540 /* call with NMA_LOCK held */
541 static void
542 netmap_dtor_locked(void *data)
543 {
544 	struct netmap_priv_d *priv = data;
545 	struct ifnet *ifp = priv->np_ifp;
546 	struct netmap_adapter *na = NA(ifp);
547 	struct netmap_if *nifp = priv->np_nifp;
548 
549 	na->refcount--;
550 	if (na->refcount <= 0) {	/* last instance */
551 		u_int i, j, lim;
552 
553 		if (netmap_verbose)
554 			D("deleting last instance for %s", ifp->if_xname);
555 		/*
556 		 * (TO CHECK) This function is only called
557 		 * when the last reference to this file descriptor goes
558 		 * away. This means we cannot have any pending poll()
559 		 * or interrupt routine operating on the structure.
560 		 */
561 		na->nm_register(ifp, 0); /* off, clear IFCAP_NETMAP */
562 		/* Wake up any sleeping threads. netmap_poll will
563 		 * then return POLLERR
564 		 */
565 		for (i = 0; i < na->num_tx_rings + 1; i++)
566 			selwakeuppri(&na->tx_rings[i].si, PI_NET);
567 		for (i = 0; i < na->num_rx_rings + 1; i++)
568 			selwakeuppri(&na->rx_rings[i].si, PI_NET);
569 		selwakeuppri(&na->tx_si, PI_NET);
570 		selwakeuppri(&na->rx_si, PI_NET);
571 #ifdef NM_BRIDGE
572 		nm_free_bdgfwd(na);
573 #endif /* NM_BRIDGE */
574 		/* release all buffers */
575 		for (i = 0; i < na->num_tx_rings + 1; i++) {
576 			struct netmap_ring *ring = na->tx_rings[i].ring;
577 			lim = na->tx_rings[i].nkr_num_slots;
578 			for (j = 0; j < lim; j++)
579 				netmap_free_buf(nifp, ring->slot[j].buf_idx);
580 			/* knlist_destroy(&na->tx_rings[i].si.si_note); */
581 			mtx_destroy(&na->tx_rings[i].q_lock);
582 		}
583 		for (i = 0; i < na->num_rx_rings + 1; i++) {
584 			struct netmap_ring *ring = na->rx_rings[i].ring;
585 			lim = na->rx_rings[i].nkr_num_slots;
586 			for (j = 0; j < lim; j++)
587 				netmap_free_buf(nifp, ring->slot[j].buf_idx);
588 			/* knlist_destroy(&na->rx_rings[i].si.si_note); */
589 			mtx_destroy(&na->rx_rings[i].q_lock);
590 		}
591 		/* XXX kqueue(9) needed; these will mirror knlist_init. */
592 		/* knlist_destroy(&na->tx_si.si_note); */
593 		/* knlist_destroy(&na->rx_si.si_note); */
594 		netmap_free_rings(na);
595 		if (nma_is_hw(na))
596 			SWNA(ifp)->tx_rings = SWNA(ifp)->rx_rings = NULL;
597 	}
598 	netmap_if_free(nifp);
599 }
600 
601 
602 /* we assume netmap adapter exists */
603 static void
604 nm_if_rele(struct ifnet *ifp)
605 {
606 #ifndef NM_BRIDGE
607 	if_rele(ifp);
608 #else /* NM_BRIDGE */
609 	int i, full = 0, is_hw;
610 	struct nm_bridge *b;
611 	struct netmap_adapter *na;
612 
613 	/* I can be called not only for get_ifp()-ed references where netmap's
614 	 * capability is guaranteed, but also for non-netmap-capable NICs.
615 	 */
616 	if (!NETMAP_CAPABLE(ifp) || !NA(ifp)->na_bdg) {
617 		if_rele(ifp);
618 		return;
619 	}
620 	if (!DROP_BDG_REF(ifp))
621 		return;
622 
623 	na = NA(ifp);
624 	b = na->na_bdg;
625 	is_hw = nma_is_hw(na);
626 
627 	BDG_WLOCK(b);
628 	ND("want to disconnect %s from the bridge", ifp->if_xname);
629 	full = 0;
630 	/* remove the entry from the bridge, also check
631 	 * if there are any leftover interfaces
632 	 * XXX we should optimize this code, e.g. going directly
633 	 * to na->bdg_port, and having a counter of ports that
634 	 * are connected. But it is not in a critical path.
635 	 * In NIC's case, index of sw na is always higher than hw na
636 	 */
637 	for (i = 0; i < NM_BDG_MAXPORTS; i++) {
638 		struct netmap_adapter *tmp = BDG_GET_VAR(b->bdg_ports[i]);
639 
640 		if (tmp == na) {
641 			/* disconnect from bridge */
642 			BDG_SET_VAR(b->bdg_ports[i], NULL);
643 			na->na_bdg = NULL;
644 			if (is_hw && SWNA(ifp)->na_bdg) {
645 				/* disconnect sw adapter too */
646 				int j = SWNA(ifp)->bdg_port;
647 				BDG_SET_VAR(b->bdg_ports[j], NULL);
648 				SWNA(ifp)->na_bdg = NULL;
649 			}
650 		} else if (tmp != NULL) {
651 			full = 1;
652 		}
653 	}
654 	BDG_WUNLOCK(b);
655 	if (full == 0) {
656 		ND("marking bridge %d as free", b - nm_bridges);
657 		b->namelen = 0;
658 		b->nm_bdg_lookup = NULL;
659 	}
660 	if (na->na_bdg) { /* still attached to the bridge */
661 		D("ouch, cannot find ifp to remove");
662 	} else if (is_hw) {
663 		if_rele(ifp);
664 	} else {
665 		bzero(na, sizeof(*na));
666 		free(na, M_DEVBUF);
667 		bzero(ifp, sizeof(*ifp));
668 		free(ifp, M_DEVBUF);
669 	}
670 #endif /* NM_BRIDGE */
671 }
672 
673 static void
674 netmap_dtor(void *data)
675 {
676 	struct netmap_priv_d *priv = data;
677 	struct ifnet *ifp = priv->np_ifp;
678 
679 	NMA_LOCK();
680 	if (ifp) {
681 		struct netmap_adapter *na = NA(ifp);
682 
683 		if (na->na_bdg)
684 			BDG_WLOCK(na->na_bdg);
685 		na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
686 		netmap_dtor_locked(data);
687 		na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
688 		if (na->na_bdg)
689 			BDG_WUNLOCK(na->na_bdg);
690 
691 		nm_if_rele(ifp); /* might also destroy *na */
692 	}
693 	if (priv->ref_done) {
694 		netmap_memory_deref();
695 	}
696 	NMA_UNLOCK();
697 	bzero(priv, sizeof(*priv));	/* XXX for safety */
698 	free(priv, M_DEVBUF);
699 }
700 
701 
702 #ifdef __FreeBSD__
703 #include <vm/vm.h>
704 #include <vm/vm_param.h>
705 #include <vm/vm_object.h>
706 #include <vm/vm_page.h>
707 #include <vm/vm_pager.h>
708 #include <vm/uma.h>
709 
710 /*
711  * In order to track whether pages are still mapped, we hook into
712  * the standard cdev_pager and intercept the constructor and
713  * destructor.
714  * XXX but then ? Do we really use the information ?
715  * Need to investigate.
716  */
717 static struct cdev_pager_ops saved_cdev_pager_ops;
718 
719 
720 static int
721 netmap_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
722     vm_ooffset_t foff, struct ucred *cred, u_short *color)
723 {
724 	if (netmap_verbose)
725 		D("first mmap for %p", handle);
726 	return saved_cdev_pager_ops.cdev_pg_ctor(handle,
727 			size, prot, foff, cred, color);
728 }
729 
730 
731 static void
732 netmap_dev_pager_dtor(void *handle)
733 {
734 	saved_cdev_pager_ops.cdev_pg_dtor(handle);
735 	ND("ready to release memory for %p", handle);
736 }
737 
738 
739 static struct cdev_pager_ops netmap_cdev_pager_ops = {
740         .cdev_pg_ctor = netmap_dev_pager_ctor,
741         .cdev_pg_dtor = netmap_dev_pager_dtor,
742         .cdev_pg_fault = NULL,
743 };
744 
745 
746 // XXX check whether we need netmap_mmap_single _and_ netmap_mmap
747 static int
748 netmap_mmap_single(struct cdev *cdev, vm_ooffset_t *foff,
749 	vm_size_t objsize,  vm_object_t *objp, int prot)
750 {
751 	vm_object_t obj;
752 
753 	ND("cdev %p foff %jd size %jd objp %p prot %d", cdev,
754 	    (intmax_t )*foff, (intmax_t )objsize, objp, prot);
755 	obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
756             curthread->td_ucred);
757 	ND("returns obj %p", obj);
758 	if (obj == NULL)
759 		return EINVAL;
760 	if (saved_cdev_pager_ops.cdev_pg_fault == NULL) {
761 		ND("initialize cdev_pager_ops");
762 		saved_cdev_pager_ops = *(obj->un_pager.devp.ops);
763 		netmap_cdev_pager_ops.cdev_pg_fault =
764 			saved_cdev_pager_ops.cdev_pg_fault;
765 	};
766 	obj->un_pager.devp.ops = &netmap_cdev_pager_ops;
767 	*objp = obj;
768 	return 0;
769 }
770 #endif /* __FreeBSD__ */
771 
772 
773 /*
774  * mmap(2) support for the "netmap" device.
775  *
776  * Expose all the memory previously allocated by our custom memory
777  * allocator: this way the user has only to issue a single mmap(2), and
778  * can work on all the data structures flawlessly.
779  *
780  * Return 0 on success, -1 otherwise.
781  */
782 
783 #ifdef __FreeBSD__
784 static int
785 netmap_mmap(__unused struct cdev *dev,
786 #if __FreeBSD_version < 900000
787 		vm_offset_t offset, vm_paddr_t *paddr, int nprot
788 #else
789 		vm_ooffset_t offset, vm_paddr_t *paddr, int nprot,
790 		__unused vm_memattr_t *memattr
791 #endif
792 	)
793 {
794 	int error = 0;
795 	struct netmap_priv_d *priv;
796 
797 	if (nprot & PROT_EXEC)
798 		return (-1);	// XXX -1 or EINVAL ?
799 
800 	error = devfs_get_cdevpriv((void **)&priv);
801 	if (error == EBADF) {	/* called on fault, memory is initialized */
802 		ND(5, "handling fault at ofs 0x%x", offset);
803 		error = 0;
804 	} else if (error == 0)	/* make sure memory is set */
805 		error = netmap_get_memory(priv);
806 	if (error)
807 		return (error);
808 
809 	ND("request for offset 0x%x", (uint32_t)offset);
810 	*paddr = netmap_ofstophys(offset);
811 
812 	return (*paddr ? 0 : ENOMEM);
813 }
814 
815 
816 static int
817 netmap_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
818 {
819 	if (netmap_verbose)
820 		D("dev %p fflag 0x%x devtype %d td %p",
821 			dev, fflag, devtype, td);
822 	return 0;
823 }
824 
825 
826 static int
827 netmap_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
828 {
829 	struct netmap_priv_d *priv;
830 	int error;
831 
832 	priv = malloc(sizeof(struct netmap_priv_d), M_DEVBUF,
833 			      M_NOWAIT | M_ZERO);
834 	if (priv == NULL)
835 		return ENOMEM;
836 
837 	error = devfs_set_cdevpriv(priv, netmap_dtor);
838 	if (error)
839 	        return error;
840 
841 	return 0;
842 }
843 #endif /* __FreeBSD__ */
844 
845 
846 /*
847  * Handlers for synchronization of the queues from/to the host.
848  * Netmap has two operating modes:
849  * - in the default mode, the rings connected to the host stack are
850  *   just another ring pair managed by userspace;
851  * - in transparent mode (XXX to be defined) incoming packets
852  *   (from the host or the NIC) are marked as NS_FORWARD upon
853  *   arrival, and the user application has a chance to reset the
854  *   flag for packets that should be dropped.
855  *   On the RXSYNC or poll(), packets in RX rings between
856  *   kring->nr_kcur and ring->cur with NS_FORWARD still set are moved
857  *   to the other side.
858  * The transfer NIC --> host is relatively easy, just encapsulate
859  * into mbufs and we are done. The host --> NIC side is slightly
860  * harder because there might not be room in the tx ring so it
861  * might take a while before releasing the buffer.
862  */
863 
864 
865 /*
866  * pass a chain of buffers to the host stack as coming from 'dst'
867  */
868 static void
869 netmap_send_up(struct ifnet *dst, struct mbuf *head)
870 {
871 	struct mbuf *m;
872 
873 	/* send packets up, outside the lock */
874 	while ((m = head) != NULL) {
875 		head = head->m_nextpkt;
876 		m->m_nextpkt = NULL;
877 		if (netmap_verbose & NM_VERB_HOST)
878 			D("sending up pkt %p size %d", m, MBUF_LEN(m));
879 		NM_SEND_UP(dst, m);
880 	}
881 }
882 
883 struct mbq {
884 	struct mbuf *head;
885 	struct mbuf *tail;
886 	int count;
887 };
888 
889 
890 /*
891  * put a copy of the buffers marked NS_FORWARD into an mbuf chain.
892  * Run from hwcur to cur - reserved
893  */
894 static void
895 netmap_grab_packets(struct netmap_kring *kring, struct mbq *q, int force)
896 {
897 	/* Take packets from hwcur to cur-reserved and pass them up.
898 	 * In case of no buffers we give up. At the end of the loop,
899 	 * the queue is drained in all cases.
900 	 * XXX handle reserved
901 	 */
902 	int k = kring->ring->cur - kring->ring->reserved;
903 	u_int n, lim = kring->nkr_num_slots - 1;
904 	struct mbuf *m, *tail = q->tail;
905 
906 	if (k < 0)
907 		k = k + kring->nkr_num_slots;
908 	for (n = kring->nr_hwcur; n != k;) {
909 		struct netmap_slot *slot = &kring->ring->slot[n];
910 
911 		n = (n == lim) ? 0 : n + 1;
912 		if ((slot->flags & NS_FORWARD) == 0 && !force)
913 			continue;
914 		if (slot->len < 14 || slot->len > NETMAP_BUF_SIZE) {
915 			D("bad pkt at %d len %d", n, slot->len);
916 			continue;
917 		}
918 		slot->flags &= ~NS_FORWARD; // XXX needed ?
919 		m = m_devget(NMB(slot), slot->len, 0, kring->na->ifp, NULL);
920 
921 		if (m == NULL)
922 			break;
923 		if (tail)
924 			tail->m_nextpkt = m;
925 		else
926 			q->head = m;
927 		tail = m;
928 		q->count++;
929 		m->m_nextpkt = NULL;
930 	}
931 	q->tail = tail;
932 }
933 
934 
935 /*
936  * called under main lock to send packets from the host to the NIC
937  * The host ring has packets from nr_hwcur to (cur - reserved)
938  * to be sent down. We scan the tx rings, which have just been
939  * flushed so nr_hwcur == cur. Pushing packets down means
940  * increment cur and decrement avail.
941  * XXX to be verified
942  */
943 static void
944 netmap_sw_to_nic(struct netmap_adapter *na)
945 {
946 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
947 	struct netmap_kring *k1 = &na->tx_rings[0];
948 	int i, howmany, src_lim, dst_lim;
949 
950 	howmany = kring->nr_hwavail;	/* XXX otherwise cur - reserved - nr_hwcur */
951 
952 	src_lim = kring->nkr_num_slots;
953 	for (i = 0; howmany > 0 && i < na->num_tx_rings; i++, k1++) {
954 		ND("%d packets left to ring %d (space %d)", howmany, i, k1->nr_hwavail);
955 		dst_lim = k1->nkr_num_slots;
956 		while (howmany > 0 && k1->ring->avail > 0) {
957 			struct netmap_slot *src, *dst, tmp;
958 			src = &kring->ring->slot[kring->nr_hwcur];
959 			dst = &k1->ring->slot[k1->ring->cur];
960 			tmp = *src;
961 			src->buf_idx = dst->buf_idx;
962 			src->flags = NS_BUF_CHANGED;
963 
964 			dst->buf_idx = tmp.buf_idx;
965 			dst->len = tmp.len;
966 			dst->flags = NS_BUF_CHANGED;
967 			ND("out len %d buf %d from %d to %d",
968 				dst->len, dst->buf_idx,
969 				kring->nr_hwcur, k1->ring->cur);
970 
971 			if (++kring->nr_hwcur >= src_lim)
972 				kring->nr_hwcur = 0;
973 			howmany--;
974 			kring->nr_hwavail--;
975 			if (++k1->ring->cur >= dst_lim)
976 				k1->ring->cur = 0;
977 			k1->ring->avail--;
978 		}
979 		kring->ring->cur = kring->nr_hwcur; // XXX
980 		k1++;
981 	}
982 }
983 
984 
985 /*
986  * netmap_sync_to_host() passes packets up. We are called from a
987  * system call in user process context, and the only contention
988  * can be among multiple user threads erroneously calling
989  * this routine concurrently.
990  */
991 static void
992 netmap_sync_to_host(struct netmap_adapter *na)
993 {
994 	struct netmap_kring *kring = &na->tx_rings[na->num_tx_rings];
995 	struct netmap_ring *ring = kring->ring;
996 	u_int k, lim = kring->nkr_num_slots - 1;
997 	struct mbq q = { NULL, NULL };
998 
999 	k = ring->cur;
1000 	if (k > lim) {
1001 		netmap_ring_reinit(kring);
1002 		return;
1003 	}
1004 	// na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0);
1005 
1006 	/* Take packets from hwcur to cur and pass them up.
1007 	 * In case of no buffers we give up. At the end of the loop,
1008 	 * the queue is drained in all cases.
1009 	 */
1010 	netmap_grab_packets(kring, &q, 1);
1011 	kring->nr_hwcur = k;
1012 	kring->nr_hwavail = ring->avail = lim;
1013 	// na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0);
1014 
1015 	netmap_send_up(na->ifp, q.head);
1016 }
1017 
1018 
1019 /* SWNA(ifp)->txrings[0] is always NA(ifp)->txrings[NA(ifp)->num_txrings] */
1020 static int
1021 netmap_bdg_to_host(struct ifnet *ifp, u_int ring_nr, int do_lock)
1022 {
1023 	(void)ring_nr;
1024 	(void)do_lock;
1025 	netmap_sync_to_host(NA(ifp));
1026 	return 0;
1027 }
1028 
1029 
1030 /*
1031  * rxsync backend for packets coming from the host stack.
1032  * They have been put in the queue by netmap_start() so we
1033  * need to protect access to the kring using a lock.
1034  *
1035  * This routine also does the selrecord if called from the poll handler
1036  * (we know because td != NULL).
1037  *
1038  * NOTE: on linux, selrecord() is defined as a macro and uses pwait
1039  *     as an additional hidden argument.
1040  */
1041 static void
1042 netmap_sync_from_host(struct netmap_adapter *na, struct thread *td, void *pwait)
1043 {
1044 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
1045 	struct netmap_ring *ring = kring->ring;
1046 	u_int j, n, lim = kring->nkr_num_slots;
1047 	u_int k = ring->cur, resvd = ring->reserved;
1048 
1049 	(void)pwait;	/* disable unused warnings */
1050 	na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0);
1051 	if (k >= lim) {
1052 		netmap_ring_reinit(kring);
1053 		return;
1054 	}
1055 	/* new packets are already set in nr_hwavail */
1056 	/* skip past packets that userspace has released */
1057 	j = kring->nr_hwcur;
1058 	if (resvd > 0) {
1059 		if (resvd + ring->avail >= lim + 1) {
1060 			D("XXX invalid reserve/avail %d %d", resvd, ring->avail);
1061 			ring->reserved = resvd = 0; // XXX panic...
1062 		}
1063 		k = (k >= resvd) ? k - resvd : k + lim - resvd;
1064         }
1065 	if (j != k) {
1066 		n = k >= j ? k - j : k + lim - j;
1067 		kring->nr_hwavail -= n;
1068 		kring->nr_hwcur = k;
1069 	}
1070 	k = ring->avail = kring->nr_hwavail - resvd;
1071 	if (k == 0 && td)
1072 		selrecord(td, &kring->si);
1073 	if (k && (netmap_verbose & NM_VERB_HOST))
1074 		D("%d pkts from stack", k);
1075 	na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0);
1076 }
1077 
1078 
1079 /*
1080  * get a refcounted reference to an interface.
1081  * Return ENXIO if the interface does not exist, EINVAL if netmap
1082  * is not supported by the interface.
1083  * If successful, hold a reference.
1084  *
1085  * During the NIC is attached to a bridge, reference is managed
1086  * at na->na_bdg_refcount using ADD/DROP_BDG_REF() as well as
1087  * virtual ports.  Hence, on the final DROP_BDG_REF(), the NIC
1088  * is detached from the bridge, then ifp's refcount is dropped (this
1089  * is equivalent to that ifp is destroyed in case of virtual ports.
1090  *
1091  * This function uses if_rele() when we want to prevent the NIC from
1092  * being detached from the bridge in error handling.  But once refcount
1093  * is acquired by this function, it must be released using nm_if_rele().
1094  */
1095 static int
1096 get_ifp(struct nmreq *nmr, struct ifnet **ifp)
1097 {
1098 	const char *name = nmr->nr_name;
1099 	int namelen = strlen(name);
1100 #ifdef NM_BRIDGE
1101 	struct ifnet *iter = NULL;
1102 	int no_prefix = 0;
1103 
1104 	do {
1105 		struct nm_bridge *b;
1106 		struct netmap_adapter *na;
1107 		int i, cand = -1, cand2 = -1;
1108 
1109 		if (strncmp(name, NM_NAME, sizeof(NM_NAME) - 1)) {
1110 			no_prefix = 1;
1111 			break;
1112 		}
1113 		b = nm_find_bridge(name, 1 /* create a new one if no exist */ );
1114 		if (b == NULL) {
1115 			D("no bridges available for '%s'", name);
1116 			return (ENXIO);
1117 		}
1118 		/* Now we are sure that name starts with the bridge's name */
1119 		BDG_WLOCK(b);
1120 		/* lookup in the local list of ports */
1121 		for (i = 0; i < NM_BDG_MAXPORTS; i++) {
1122 			na = BDG_GET_VAR(b->bdg_ports[i]);
1123 			if (na == NULL) {
1124 				if (cand == -1)
1125 					cand = i; /* potential insert point */
1126 				else if (cand2 == -1)
1127 					cand2 = i; /* for host stack */
1128 				continue;
1129 			}
1130 			iter = na->ifp;
1131 			/* XXX make sure the name only contains one : */
1132 			if (!strcmp(iter->if_xname, name) /* virtual port */ ||
1133 			    (namelen > b->namelen && !strcmp(iter->if_xname,
1134 			    name + b->namelen + 1)) /* NIC */) {
1135 				ADD_BDG_REF(iter);
1136 				ND("found existing interface");
1137 				BDG_WUNLOCK(b);
1138 				break;
1139 			}
1140 		}
1141 		if (i < NM_BDG_MAXPORTS) /* already unlocked */
1142 			break;
1143 		if (cand == -1) {
1144 			D("bridge full, cannot create new port");
1145 no_port:
1146 			BDG_WUNLOCK(b);
1147 			*ifp = NULL;
1148 			return EINVAL;
1149 		}
1150 		ND("create new bridge port %s", name);
1151 		/*
1152 		 * create a struct ifnet for the new port.
1153 		 * The forwarding table is attached to the kring(s).
1154 		 */
1155 		/*
1156 		 * try see if there is a matching NIC with this name
1157 		 * (after the bridge's name)
1158 		 */
1159 		iter = ifunit_ref(name + b->namelen + 1);
1160 		if (!iter) { /* this is a virtual port */
1161 			/* Create a temporary NA with arguments, then
1162 			 * bdg_netmap_attach() will allocate the real one
1163 			 * and attach it to the ifp
1164 			 */
1165 			struct netmap_adapter tmp_na;
1166 
1167 			if (nmr->nr_cmd) /* nr_cmd must be for a NIC */
1168 				goto no_port;
1169 			bzero(&tmp_na, sizeof(tmp_na));
1170 			/* bound checking */
1171 			if (nmr->nr_tx_rings < 1)
1172 				nmr->nr_tx_rings = 1;
1173 			if (nmr->nr_tx_rings > NM_BDG_MAXRINGS)
1174 				nmr->nr_tx_rings = NM_BDG_MAXRINGS;
1175 			tmp_na.num_tx_rings = nmr->nr_tx_rings;
1176 			if (nmr->nr_rx_rings < 1)
1177 				nmr->nr_rx_rings = 1;
1178 			if (nmr->nr_rx_rings > NM_BDG_MAXRINGS)
1179 				nmr->nr_rx_rings = NM_BDG_MAXRINGS;
1180 			tmp_na.num_rx_rings = nmr->nr_rx_rings;
1181 
1182 			iter = malloc(sizeof(*iter), M_DEVBUF, M_NOWAIT | M_ZERO);
1183 			if (!iter)
1184 				goto no_port;
1185 			strcpy(iter->if_xname, name);
1186 			tmp_na.ifp = iter;
1187 			/* bdg_netmap_attach creates a struct netmap_adapter */
1188 			bdg_netmap_attach(&tmp_na);
1189 		} else if (NETMAP_CAPABLE(iter)) { /* this is a NIC */
1190 			/* cannot attach the NIC that any user or another
1191 			 * bridge already holds.
1192 			 */
1193 			if (NETMAP_OWNED_BY_ANY(iter) || cand2 == -1) {
1194 ifunit_rele:
1195 				if_rele(iter); /* don't detach from bridge */
1196 				goto no_port;
1197 			}
1198 			/* bind the host stack to the bridge */
1199 			if (nmr->nr_arg1 == NETMAP_BDG_HOST) {
1200 				BDG_SET_VAR(b->bdg_ports[cand2], SWNA(iter));
1201 				SWNA(iter)->bdg_port = cand2;
1202 				SWNA(iter)->na_bdg = b;
1203 			}
1204 		} else /* not a netmap-capable NIC */
1205 			goto ifunit_rele;
1206 		na = NA(iter);
1207 		na->bdg_port = cand;
1208 		/* bind the port to the bridge (virtual ports are not active) */
1209 		BDG_SET_VAR(b->bdg_ports[cand], na);
1210 		na->na_bdg = b;
1211 		ADD_BDG_REF(iter);
1212 		BDG_WUNLOCK(b);
1213 		ND("attaching virtual bridge %p", b);
1214 	} while (0);
1215 	*ifp = iter;
1216 	if (! *ifp)
1217 #endif /* NM_BRIDGE */
1218 	*ifp = ifunit_ref(name);
1219 	if (*ifp == NULL)
1220 		return (ENXIO);
1221 	/* can do this if the capability exists and if_pspare[0]
1222 	 * points to the netmap descriptor.
1223 	 */
1224 	if (NETMAP_CAPABLE(*ifp)) {
1225 #ifdef NM_BRIDGE
1226 		/* Users cannot use the NIC attached to a bridge directly */
1227 		if (no_prefix && NETMAP_OWNED_BY_KERN(*ifp)) {
1228 			if_rele(*ifp); /* don't detach from bridge */
1229 			return EINVAL;
1230 		} else
1231 #endif /* NM_BRIDGE */
1232 		return 0;	/* valid pointer, we hold the refcount */
1233 	}
1234 	nm_if_rele(*ifp);
1235 	return EINVAL;	// not NETMAP capable
1236 }
1237 
1238 
1239 /*
1240  * Error routine called when txsync/rxsync detects an error.
1241  * Can't do much more than resetting cur = hwcur, avail = hwavail.
1242  * Return 1 on reinit.
1243  *
1244  * This routine is only called by the upper half of the kernel.
1245  * It only reads hwcur (which is changed only by the upper half, too)
1246  * and hwavail (which may be changed by the lower half, but only on
1247  * a tx ring and only to increase it, so any error will be recovered
1248  * on the next call). For the above, we don't strictly need to call
1249  * it under lock.
1250  */
1251 int
1252 netmap_ring_reinit(struct netmap_kring *kring)
1253 {
1254 	struct netmap_ring *ring = kring->ring;
1255 	u_int i, lim = kring->nkr_num_slots - 1;
1256 	int errors = 0;
1257 
1258 	RD(10, "called for %s", kring->na->ifp->if_xname);
1259 	if (ring->cur > lim)
1260 		errors++;
1261 	for (i = 0; i <= lim; i++) {
1262 		u_int idx = ring->slot[i].buf_idx;
1263 		u_int len = ring->slot[i].len;
1264 		if (idx < 2 || idx >= netmap_total_buffers) {
1265 			if (!errors++)
1266 				D("bad buffer at slot %d idx %d len %d ", i, idx, len);
1267 			ring->slot[i].buf_idx = 0;
1268 			ring->slot[i].len = 0;
1269 		} else if (len > NETMAP_BUF_SIZE) {
1270 			ring->slot[i].len = 0;
1271 			if (!errors++)
1272 				D("bad len %d at slot %d idx %d",
1273 					len, i, idx);
1274 		}
1275 	}
1276 	if (errors) {
1277 		int pos = kring - kring->na->tx_rings;
1278 		int n = kring->na->num_tx_rings + 1;
1279 
1280 		RD(10, "total %d errors", errors);
1281 		errors++;
1282 		RD(10, "%s %s[%d] reinit, cur %d -> %d avail %d -> %d",
1283 			kring->na->ifp->if_xname,
1284 			pos < n ?  "TX" : "RX", pos < n ? pos : pos - n,
1285 			ring->cur, kring->nr_hwcur,
1286 			ring->avail, kring->nr_hwavail);
1287 		ring->cur = kring->nr_hwcur;
1288 		ring->avail = kring->nr_hwavail;
1289 	}
1290 	return (errors ? 1 : 0);
1291 }
1292 
1293 
1294 /*
1295  * Set the ring ID. For devices with a single queue, a request
1296  * for all rings is the same as a single ring.
1297  */
1298 static int
1299 netmap_set_ringid(struct netmap_priv_d *priv, u_int ringid)
1300 {
1301 	struct ifnet *ifp = priv->np_ifp;
1302 	struct netmap_adapter *na = NA(ifp);
1303 	u_int i = ringid & NETMAP_RING_MASK;
1304 	/* initially (np_qfirst == np_qlast) we don't want to lock */
1305 	int need_lock = (priv->np_qfirst != priv->np_qlast);
1306 	int lim = na->num_rx_rings;
1307 
1308 	if (na->num_tx_rings > lim)
1309 		lim = na->num_tx_rings;
1310 	if ( (ringid & NETMAP_HW_RING) && i >= lim) {
1311 		D("invalid ring id %d", i);
1312 		return (EINVAL);
1313 	}
1314 	if (need_lock)
1315 		na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
1316 	priv->np_ringid = ringid;
1317 	if (ringid & NETMAP_SW_RING) {
1318 		priv->np_qfirst = NETMAP_SW_RING;
1319 		priv->np_qlast = 0;
1320 	} else if (ringid & NETMAP_HW_RING) {
1321 		priv->np_qfirst = i;
1322 		priv->np_qlast = i + 1;
1323 	} else {
1324 		priv->np_qfirst = 0;
1325 		priv->np_qlast = NETMAP_HW_RING ;
1326 	}
1327 	priv->np_txpoll = (ringid & NETMAP_NO_TX_POLL) ? 0 : 1;
1328 	if (need_lock)
1329 		na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
1330     if (netmap_verbose) {
1331 	if (ringid & NETMAP_SW_RING)
1332 		D("ringid %s set to SW RING", ifp->if_xname);
1333 	else if (ringid & NETMAP_HW_RING)
1334 		D("ringid %s set to HW RING %d", ifp->if_xname,
1335 			priv->np_qfirst);
1336 	else
1337 		D("ringid %s set to all %d HW RINGS", ifp->if_xname, lim);
1338     }
1339 	return 0;
1340 }
1341 
1342 
1343 /*
1344  * possibly move the interface to netmap-mode.
1345  * If success it returns a pointer to netmap_if, otherwise NULL.
1346  * This must be called with NMA_LOCK held.
1347  */
1348 static struct netmap_if *
1349 netmap_do_regif(struct netmap_priv_d *priv, struct ifnet *ifp,
1350 	uint16_t ringid, int *err)
1351 {
1352 	struct netmap_adapter *na = NA(ifp);
1353 	struct netmap_if *nifp = NULL;
1354 	int i, error;
1355 
1356 	if (na->na_bdg)
1357 		BDG_WLOCK(na->na_bdg);
1358 	na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
1359 
1360 	/* ring configuration may have changed, fetch from the card */
1361 	netmap_update_config(na);
1362 	priv->np_ifp = ifp;     /* store the reference */
1363 	error = netmap_set_ringid(priv, ringid);
1364 	if (error)
1365 		goto out;
1366 	nifp = netmap_if_new(ifp->if_xname, na);
1367 	if (nifp == NULL) { /* allocation failed */
1368 		error = ENOMEM;
1369 	} else if (ifp->if_capenable & IFCAP_NETMAP) {
1370 		/* was already set */
1371 	} else {
1372 		/* Otherwise set the card in netmap mode
1373 		 * and make it use the shared buffers.
1374 		 */
1375 		for (i = 0 ; i < na->num_tx_rings + 1; i++)
1376 			mtx_init(&na->tx_rings[i].q_lock, "nm_txq_lock",
1377 			    MTX_NETWORK_LOCK, MTX_DEF);
1378 		for (i = 0 ; i < na->num_rx_rings + 1; i++) {
1379 			mtx_init(&na->rx_rings[i].q_lock, "nm_rxq_lock",
1380 			    MTX_NETWORK_LOCK, MTX_DEF);
1381 		}
1382 		if (nma_is_hw(na)) {
1383 			SWNA(ifp)->tx_rings = &na->tx_rings[na->num_tx_rings];
1384 			SWNA(ifp)->rx_rings = &na->rx_rings[na->num_rx_rings];
1385 		}
1386 		error = na->nm_register(ifp, 1); /* mode on */
1387 #ifdef NM_BRIDGE
1388 		if (!error)
1389 			error = nm_alloc_bdgfwd(na);
1390 #endif /* NM_BRIDGE */
1391 		if (error) {
1392 			netmap_dtor_locked(priv);
1393 			/* nifp is not yet in priv, so free it separately */
1394 			netmap_if_free(nifp);
1395 			nifp = NULL;
1396 		}
1397 
1398 	}
1399 out:
1400 	*err = error;
1401 	na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
1402 	if (na->na_bdg)
1403 		BDG_WUNLOCK(na->na_bdg);
1404 	return nifp;
1405 }
1406 
1407 
1408 /* Process NETMAP_BDG_ATTACH and NETMAP_BDG_DETACH */
1409 static int
1410 kern_netmap_regif(struct nmreq *nmr)
1411 {
1412 	struct ifnet *ifp;
1413 	struct netmap_if *nifp;
1414 	struct netmap_priv_d *npriv;
1415 	int error;
1416 
1417 	npriv = malloc(sizeof(*npriv), M_DEVBUF, M_NOWAIT|M_ZERO);
1418 	if (npriv == NULL)
1419 		return ENOMEM;
1420 	error = netmap_get_memory(npriv);
1421 	if (error) {
1422 free_exit:
1423 		bzero(npriv, sizeof(*npriv));
1424 		free(npriv, M_DEVBUF);
1425 		return error;
1426 	}
1427 
1428 	NMA_LOCK();
1429 	error = get_ifp(nmr, &ifp);
1430 	if (error) { /* no device, or another bridge or user owns the device */
1431 		NMA_UNLOCK();
1432 		goto free_exit;
1433 	} else if (!NETMAP_OWNED_BY_KERN(ifp)) {
1434 		/* got reference to a virtual port or direct access to a NIC.
1435 		 * perhaps specified no bridge's prefix or wrong NIC's name
1436 		 */
1437 		error = EINVAL;
1438 unref_exit:
1439 		nm_if_rele(ifp);
1440 		NMA_UNLOCK();
1441 		goto free_exit;
1442 	}
1443 
1444 	if (nmr->nr_cmd == NETMAP_BDG_DETACH) {
1445 		if (NA(ifp)->refcount == 0) { /* not registered */
1446 			error = EINVAL;
1447 			goto unref_exit;
1448 		}
1449 		NMA_UNLOCK();
1450 
1451 		netmap_dtor(NA(ifp)->na_kpriv); /* unregister */
1452 		NA(ifp)->na_kpriv = NULL;
1453 		nm_if_rele(ifp); /* detach from the bridge */
1454 		goto free_exit;
1455 	} else if (NA(ifp)->refcount > 0) { /* already registered */
1456 		error = EINVAL;
1457 		goto unref_exit;
1458 	}
1459 
1460 	nifp = netmap_do_regif(npriv, ifp, nmr->nr_ringid, &error);
1461 	if (!nifp)
1462 		goto unref_exit;
1463 	wmb(); // XXX do we need it ?
1464 	npriv->np_nifp = nifp;
1465 	NA(ifp)->na_kpriv = npriv;
1466 	NMA_UNLOCK();
1467 	D("registered %s to netmap-mode", ifp->if_xname);
1468 	return 0;
1469 }
1470 
1471 
1472 /* CORE_LOCK is not necessary */
1473 static void
1474 netmap_swlock_wrapper(struct ifnet *dev, int what, u_int queueid)
1475 {
1476 	struct netmap_adapter *na = SWNA(dev);
1477 
1478 	switch (what) {
1479 	case NETMAP_TX_LOCK:
1480 		mtx_lock(&na->tx_rings[queueid].q_lock);
1481 		break;
1482 
1483 	case NETMAP_TX_UNLOCK:
1484 		mtx_unlock(&na->tx_rings[queueid].q_lock);
1485 		break;
1486 
1487 	case NETMAP_RX_LOCK:
1488 		mtx_lock(&na->rx_rings[queueid].q_lock);
1489 		break;
1490 
1491 	case NETMAP_RX_UNLOCK:
1492 		mtx_unlock(&na->rx_rings[queueid].q_lock);
1493 		break;
1494 	}
1495 }
1496 
1497 
1498 /* Initialize necessary fields of sw adapter located in right after hw's
1499  * one.  sw adapter attaches a pair of sw rings of the netmap-mode NIC.
1500  * It is always activated and deactivated at the same tie with the hw's one.
1501  * Thus we don't need refcounting on the sw adapter.
1502  * Regardless of NIC's feature we use separate lock so that anybody can lock
1503  * me independently from the hw adapter.
1504  * Make sure nm_register is NULL to be handled as FALSE in nma_is_hw
1505  */
1506 static void
1507 netmap_attach_sw(struct ifnet *ifp)
1508 {
1509 	struct netmap_adapter *hw_na = NA(ifp);
1510 	struct netmap_adapter *na = SWNA(ifp);
1511 
1512 	na->ifp = ifp;
1513 	na->separate_locks = 1;
1514 	na->nm_lock = netmap_swlock_wrapper;
1515 	na->num_rx_rings = na->num_tx_rings = 1;
1516 	na->num_tx_desc = hw_na->num_tx_desc;
1517 	na->num_rx_desc = hw_na->num_rx_desc;
1518 	na->nm_txsync = netmap_bdg_to_host;
1519 }
1520 
1521 
1522 /* exported to kernel callers */
1523 int
1524 netmap_bdg_ctl(struct nmreq *nmr, bdg_lookup_fn_t func)
1525 {
1526 	struct nm_bridge *b;
1527 	struct netmap_adapter *na;
1528 	struct ifnet *iter;
1529 	char *name = nmr->nr_name;
1530 	int cmd = nmr->nr_cmd, namelen = strlen(name);
1531 	int error = 0, i, j;
1532 
1533 	switch (cmd) {
1534 	case NETMAP_BDG_ATTACH:
1535 	case NETMAP_BDG_DETACH:
1536 		error = kern_netmap_regif(nmr);
1537 		break;
1538 
1539 	case NETMAP_BDG_LIST:
1540 		/* this is used to enumerate bridges and ports */
1541 		if (namelen) { /* look up indexes of bridge and port */
1542 			if (strncmp(name, NM_NAME, strlen(NM_NAME))) {
1543 				error = EINVAL;
1544 				break;
1545 			}
1546 			b = nm_find_bridge(name, 0 /* don't create */);
1547 			if (!b) {
1548 				error = ENOENT;
1549 				break;
1550 			}
1551 
1552 			BDG_RLOCK(b);
1553 			error = ENOENT;
1554 			for (i = 0; i < NM_BDG_MAXPORTS; i++) {
1555 				na = BDG_GET_VAR(b->bdg_ports[i]);
1556 				if (na == NULL)
1557 					continue;
1558 				iter = na->ifp;
1559 				/* the former and the latter identify a
1560 				 * virtual port and a NIC, respectively
1561 				 */
1562 				if (!strcmp(iter->if_xname, name) ||
1563 				    (namelen > b->namelen &&
1564 				    !strcmp(iter->if_xname,
1565 				    name + b->namelen + 1))) {
1566 					/* bridge index */
1567 					nmr->nr_arg1 = b - nm_bridges;
1568 					nmr->nr_arg2 = i; /* port index */
1569 					error = 0;
1570 					break;
1571 				}
1572 			}
1573 			BDG_RUNLOCK(b);
1574 		} else {
1575 			/* return the first non-empty entry starting from
1576 			 * bridge nr_arg1 and port nr_arg2.
1577 			 *
1578 			 * Users can detect the end of the same bridge by
1579 			 * seeing the new and old value of nr_arg1, and can
1580 			 * detect the end of all the bridge by error != 0
1581 			 */
1582 			i = nmr->nr_arg1;
1583 			j = nmr->nr_arg2;
1584 
1585 			for (error = ENOENT; error && i < NM_BRIDGES; i++) {
1586 				b = nm_bridges + i;
1587 				BDG_RLOCK(b);
1588 				for (; j < NM_BDG_MAXPORTS; j++) {
1589 					na = BDG_GET_VAR(b->bdg_ports[j]);
1590 					if (na == NULL)
1591 						continue;
1592 					iter = na->ifp;
1593 					nmr->nr_arg1 = i;
1594 					nmr->nr_arg2 = j;
1595 					strncpy(name, iter->if_xname, IFNAMSIZ);
1596 					error = 0;
1597 					break;
1598 				}
1599 				BDG_RUNLOCK(b);
1600 				j = 0; /* following bridges scan from 0 */
1601 			}
1602 		}
1603 		break;
1604 
1605 	case NETMAP_BDG_LOOKUP_REG:
1606 		/* register a lookup function to the given bridge.
1607 		 * nmr->nr_name may be just bridge's name (including ':'
1608 		 * if it is not just NM_NAME).
1609 		 */
1610 		if (!func) {
1611 			error = EINVAL;
1612 			break;
1613 		}
1614 		b = nm_find_bridge(name, 0 /* don't create */);
1615 		if (!b) {
1616 			error = EINVAL;
1617 			break;
1618 		}
1619 		BDG_WLOCK(b);
1620 		b->nm_bdg_lookup = func;
1621 		BDG_WUNLOCK(b);
1622 		break;
1623 	default:
1624 		D("invalid cmd (nmr->nr_cmd) (0x%x)", cmd);
1625 		error = EINVAL;
1626 		break;
1627 	}
1628 	return error;
1629 }
1630 
1631 
1632 /*
1633  * ioctl(2) support for the "netmap" device.
1634  *
1635  * Following a list of accepted commands:
1636  * - NIOCGINFO
1637  * - SIOCGIFADDR	just for convenience
1638  * - NIOCREGIF
1639  * - NIOCUNREGIF
1640  * - NIOCTXSYNC
1641  * - NIOCRXSYNC
1642  *
1643  * Return 0 on success, errno otherwise.
1644  */
1645 static int
1646 netmap_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
1647 	int fflag, struct thread *td)
1648 {
1649 	struct netmap_priv_d *priv = NULL;
1650 	struct ifnet *ifp;
1651 	struct nmreq *nmr = (struct nmreq *) data;
1652 	struct netmap_adapter *na;
1653 	int error;
1654 	u_int i, lim;
1655 	struct netmap_if *nifp;
1656 
1657 	(void)dev;	/* UNUSED */
1658 	(void)fflag;	/* UNUSED */
1659 #ifdef linux
1660 #define devfs_get_cdevpriv(pp)				\
1661 	({ *(struct netmap_priv_d **)pp = ((struct file *)td)->private_data; 	\
1662 		(*pp ? 0 : ENOENT); })
1663 
1664 /* devfs_set_cdevpriv cannot fail on linux */
1665 #define devfs_set_cdevpriv(p, fn)				\
1666 	({ ((struct file *)td)->private_data = p; (p ? 0 : EINVAL); })
1667 
1668 
1669 #define devfs_clear_cdevpriv()	do {				\
1670 		netmap_dtor(priv); ((struct file *)td)->private_data = 0;	\
1671 	} while (0)
1672 #endif /* linux */
1673 
1674 	CURVNET_SET(TD_TO_VNET(td));
1675 
1676 	error = devfs_get_cdevpriv((void **)&priv);
1677 	if (error) {
1678 		CURVNET_RESTORE();
1679 		/* XXX ENOENT should be impossible, since the priv
1680 		 * is now created in the open */
1681 		return (error == ENOENT ? ENXIO : error);
1682 	}
1683 
1684 	nmr->nr_name[sizeof(nmr->nr_name) - 1] = '\0';	/* truncate name */
1685 	switch (cmd) {
1686 	case NIOCGINFO:		/* return capabilities etc */
1687 		if (nmr->nr_version != NETMAP_API) {
1688 			D("API mismatch got %d have %d",
1689 				nmr->nr_version, NETMAP_API);
1690 			nmr->nr_version = NETMAP_API;
1691 			error = EINVAL;
1692 			break;
1693 		}
1694 		if (nmr->nr_cmd == NETMAP_BDG_LIST) {
1695 			error = netmap_bdg_ctl(nmr, NULL);
1696 			break;
1697 		}
1698 		/* update configuration */
1699 		error = netmap_get_memory(priv);
1700 		ND("get_memory returned %d", error);
1701 		if (error)
1702 			break;
1703 		/* memsize is always valid */
1704 		nmr->nr_memsize = nm_mem.nm_totalsize;
1705 		nmr->nr_offset = 0;
1706 		nmr->nr_rx_slots = nmr->nr_tx_slots = 0;
1707 		if (nmr->nr_name[0] == '\0')	/* just get memory info */
1708 			break;
1709 		/* lock because get_ifp and update_config see na->refcount */
1710 		NMA_LOCK();
1711 		error = get_ifp(nmr, &ifp); /* get a refcount */
1712 		if (error) {
1713 			NMA_UNLOCK();
1714 			break;
1715 		}
1716 		na = NA(ifp); /* retrieve netmap_adapter */
1717 		netmap_update_config(na);
1718 		NMA_UNLOCK();
1719 		nmr->nr_rx_rings = na->num_rx_rings;
1720 		nmr->nr_tx_rings = na->num_tx_rings;
1721 		nmr->nr_rx_slots = na->num_rx_desc;
1722 		nmr->nr_tx_slots = na->num_tx_desc;
1723 		nm_if_rele(ifp);	/* return the refcount */
1724 		break;
1725 
1726 	case NIOCREGIF:
1727 		if (nmr->nr_version != NETMAP_API) {
1728 			nmr->nr_version = NETMAP_API;
1729 			error = EINVAL;
1730 			break;
1731 		}
1732 		/* possibly attach/detach NIC and VALE switch */
1733 		i = nmr->nr_cmd;
1734 		if (i == NETMAP_BDG_ATTACH || i == NETMAP_BDG_DETACH) {
1735 			error = netmap_bdg_ctl(nmr, NULL);
1736 			break;
1737 		} else if (i != 0) {
1738 			D("nr_cmd must be 0 not %d", i);
1739 			error = EINVAL;
1740 			break;
1741 		}
1742 
1743 		/* ensure allocators are ready */
1744 		error = netmap_get_memory(priv);
1745 		ND("get_memory returned %d", error);
1746 		if (error)
1747 			break;
1748 
1749 		/* protect access to priv from concurrent NIOCREGIF */
1750 		NMA_LOCK();
1751 		if (priv->np_ifp != NULL) {	/* thread already registered */
1752 			error = netmap_set_ringid(priv, nmr->nr_ringid);
1753 unlock_out:
1754 			NMA_UNLOCK();
1755 			break;
1756 		}
1757 		/* find the interface and a reference */
1758 		error = get_ifp(nmr, &ifp); /* keep reference */
1759 		if (error)
1760 			goto unlock_out;
1761 		else if (NETMAP_OWNED_BY_KERN(ifp)) {
1762 			nm_if_rele(ifp);
1763 			goto unlock_out;
1764 		}
1765 		nifp = netmap_do_regif(priv, ifp, nmr->nr_ringid, &error);
1766 		if (!nifp) {    /* reg. failed, release priv and ref */
1767 			nm_if_rele(ifp);        /* return the refcount */
1768 			priv->np_ifp = NULL;
1769 			priv->np_nifp = NULL;
1770 			goto unlock_out;
1771 		}
1772 
1773 		/* the following assignment is a commitment.
1774 		 * Readers (i.e., poll and *SYNC) check for
1775 		 * np_nifp != NULL without locking
1776 		 */
1777 		wmb(); /* make sure previous writes are visible to all CPUs */
1778 		priv->np_nifp = nifp;
1779 		NMA_UNLOCK();
1780 
1781 		/* return the offset of the netmap_if object */
1782 		na = NA(ifp); /* retrieve netmap adapter */
1783 		nmr->nr_rx_rings = na->num_rx_rings;
1784 		nmr->nr_tx_rings = na->num_tx_rings;
1785 		nmr->nr_rx_slots = na->num_rx_desc;
1786 		nmr->nr_tx_slots = na->num_tx_desc;
1787 		nmr->nr_memsize = nm_mem.nm_totalsize;
1788 		nmr->nr_offset = netmap_if_offset(nifp);
1789 		break;
1790 
1791 	case NIOCUNREGIF:
1792 		// XXX we have no data here ?
1793 		D("deprecated, data is %p", nmr);
1794 		error = EINVAL;
1795 		break;
1796 
1797 	case NIOCTXSYNC:
1798 	case NIOCRXSYNC:
1799 		nifp = priv->np_nifp;
1800 
1801 		if (nifp == NULL) {
1802 			error = ENXIO;
1803 			break;
1804 		}
1805 		rmb(); /* make sure following reads are not from cache */
1806 
1807 
1808 		ifp = priv->np_ifp;	/* we have a reference */
1809 
1810 		if (ifp == NULL) {
1811 			D("Internal error: nifp != NULL && ifp == NULL");
1812 			error = ENXIO;
1813 			break;
1814 		}
1815 
1816 		na = NA(ifp); /* retrieve netmap adapter */
1817 		if (priv->np_qfirst == NETMAP_SW_RING) { /* host rings */
1818 			if (cmd == NIOCTXSYNC)
1819 				netmap_sync_to_host(na);
1820 			else
1821 				netmap_sync_from_host(na, NULL, NULL);
1822 			break;
1823 		}
1824 		/* find the last ring to scan */
1825 		lim = priv->np_qlast;
1826 		if (lim == NETMAP_HW_RING)
1827 			lim = (cmd == NIOCTXSYNC) ?
1828 			    na->num_tx_rings : na->num_rx_rings;
1829 
1830 		for (i = priv->np_qfirst; i < lim; i++) {
1831 			if (cmd == NIOCTXSYNC) {
1832 				struct netmap_kring *kring = &na->tx_rings[i];
1833 				if (netmap_verbose & NM_VERB_TXSYNC)
1834 					D("pre txsync ring %d cur %d hwcur %d",
1835 					    i, kring->ring->cur,
1836 					    kring->nr_hwcur);
1837 				na->nm_txsync(ifp, i, 1 /* do lock */);
1838 				if (netmap_verbose & NM_VERB_TXSYNC)
1839 					D("post txsync ring %d cur %d hwcur %d",
1840 					    i, kring->ring->cur,
1841 					    kring->nr_hwcur);
1842 			} else {
1843 				na->nm_rxsync(ifp, i, 1 /* do lock */);
1844 				microtime(&na->rx_rings[i].ring->ts);
1845 			}
1846 		}
1847 
1848 		break;
1849 
1850 #ifdef __FreeBSD__
1851 	case BIOCIMMEDIATE:
1852 	case BIOCGHDRCMPLT:
1853 	case BIOCSHDRCMPLT:
1854 	case BIOCSSEESENT:
1855 		D("ignore BIOCIMMEDIATE/BIOCSHDRCMPLT/BIOCSHDRCMPLT/BIOCSSEESENT");
1856 		break;
1857 
1858 	default:	/* allow device-specific ioctls */
1859 	    {
1860 		struct socket so;
1861 		bzero(&so, sizeof(so));
1862 		error = get_ifp(nmr, &ifp); /* keep reference */
1863 		if (error)
1864 			break;
1865 		so.so_vnet = ifp->if_vnet;
1866 		// so->so_proto not null.
1867 		error = ifioctl(&so, cmd, data, td);
1868 		nm_if_rele(ifp);
1869 		break;
1870 	    }
1871 
1872 #else /* linux */
1873 	default:
1874 		error = EOPNOTSUPP;
1875 #endif /* linux */
1876 	}
1877 
1878 	CURVNET_RESTORE();
1879 	return (error);
1880 }
1881 
1882 
1883 /*
1884  * select(2) and poll(2) handlers for the "netmap" device.
1885  *
1886  * Can be called for one or more queues.
1887  * Return true the event mask corresponding to ready events.
1888  * If there are no ready events, do a selrecord on either individual
1889  * selfd or on the global one.
1890  * Device-dependent parts (locking and sync of tx/rx rings)
1891  * are done through callbacks.
1892  *
1893  * On linux, arguments are really pwait, the poll table, and 'td' is struct file *
1894  * The first one is remapped to pwait as selrecord() uses the name as an
1895  * hidden argument.
1896  */
1897 static int
1898 netmap_poll(struct cdev *dev, int events, struct thread *td)
1899 {
1900 	struct netmap_priv_d *priv = NULL;
1901 	struct netmap_adapter *na;
1902 	struct ifnet *ifp;
1903 	struct netmap_kring *kring;
1904 	u_int core_lock, i, check_all, want_tx, want_rx, revents = 0;
1905 	u_int lim_tx, lim_rx, host_forwarded = 0;
1906 	struct mbq q = { NULL, NULL, 0 };
1907 	enum {NO_CL, NEED_CL, LOCKED_CL }; /* see below */
1908 	void *pwait = dev;	/* linux compatibility */
1909 
1910 	(void)pwait;
1911 
1912 	if (devfs_get_cdevpriv((void **)&priv) != 0 || priv == NULL)
1913 		return POLLERR;
1914 
1915 	if (priv->np_nifp == NULL) {
1916 		D("No if registered");
1917 		return POLLERR;
1918 	}
1919 	rmb(); /* make sure following reads are not from cache */
1920 
1921 	ifp = priv->np_ifp;
1922 	// XXX check for deleting() ?
1923 	if ( (ifp->if_capenable & IFCAP_NETMAP) == 0)
1924 		return POLLERR;
1925 
1926 	if (netmap_verbose & 0x8000)
1927 		D("device %s events 0x%x", ifp->if_xname, events);
1928 	want_tx = events & (POLLOUT | POLLWRNORM);
1929 	want_rx = events & (POLLIN | POLLRDNORM);
1930 
1931 	na = NA(ifp); /* retrieve netmap adapter */
1932 
1933 	lim_tx = na->num_tx_rings;
1934 	lim_rx = na->num_rx_rings;
1935 	/* how many queues we are scanning */
1936 	if (priv->np_qfirst == NETMAP_SW_RING) {
1937 		if (priv->np_txpoll || want_tx) {
1938 			/* push any packets up, then we are always ready */
1939 			netmap_sync_to_host(na);
1940 			revents |= want_tx;
1941 		}
1942 		if (want_rx) {
1943 			kring = &na->rx_rings[lim_rx];
1944 			if (kring->ring->avail == 0)
1945 				netmap_sync_from_host(na, td, dev);
1946 			if (kring->ring->avail > 0) {
1947 				revents |= want_rx;
1948 			}
1949 		}
1950 		return (revents);
1951 	}
1952 
1953 	/* if we are in transparent mode, check also the host rx ring */
1954 	kring = &na->rx_rings[lim_rx];
1955 	if ( (priv->np_qlast == NETMAP_HW_RING) // XXX check_all
1956 			&& want_rx
1957 			&& (netmap_fwd || kring->ring->flags & NR_FORWARD) ) {
1958 		if (kring->ring->avail == 0)
1959 			netmap_sync_from_host(na, td, dev);
1960 		if (kring->ring->avail > 0)
1961 			revents |= want_rx;
1962 	}
1963 
1964 	/*
1965 	 * check_all is set if the card has more than one queue and
1966 	 * the client is polling all of them. If true, we sleep on
1967 	 * the "global" selfd, otherwise we sleep on individual selfd
1968 	 * (we can only sleep on one of them per direction).
1969 	 * The interrupt routine in the driver should always wake on
1970 	 * the individual selfd, and also on the global one if the card
1971 	 * has more than one ring.
1972 	 *
1973 	 * If the card has only one lock, we just use that.
1974 	 * If the card has separate ring locks, we just use those
1975 	 * unless we are doing check_all, in which case the whole
1976 	 * loop is wrapped by the global lock.
1977 	 * We acquire locks only when necessary: if poll is called
1978 	 * when buffers are available, we can just return without locks.
1979 	 *
1980 	 * rxsync() is only called if we run out of buffers on a POLLIN.
1981 	 * txsync() is called if we run out of buffers on POLLOUT, or
1982 	 * there are pending packets to send. The latter can be disabled
1983 	 * passing NETMAP_NO_TX_POLL in the NIOCREG call.
1984 	 */
1985 	check_all = (priv->np_qlast == NETMAP_HW_RING) && (lim_tx > 1 || lim_rx > 1);
1986 
1987 	/*
1988 	 * core_lock indicates what to do with the core lock.
1989 	 * The core lock is used when either the card has no individual
1990 	 * locks, or it has individual locks but we are cheking all
1991 	 * rings so we need the core lock to avoid missing wakeup events.
1992 	 *
1993 	 * It has three possible states:
1994 	 * NO_CL	we don't need to use the core lock, e.g.
1995 	 *		because we are protected by individual locks.
1996 	 * NEED_CL	we need the core lock. In this case, when we
1997 	 *		call the lock routine, move to LOCKED_CL
1998 	 *		to remember to release the lock once done.
1999 	 * LOCKED_CL	core lock is set, so we need to release it.
2000 	 */
2001 	core_lock = (check_all || !na->separate_locks) ? NEED_CL : NO_CL;
2002 #ifdef NM_BRIDGE
2003 	/* the bridge uses separate locks */
2004 	if (na->nm_register == bdg_netmap_reg) {
2005 		ND("not using core lock for %s", ifp->if_xname);
2006 		core_lock = NO_CL;
2007 	}
2008 #endif /* NM_BRIDGE */
2009 	if (priv->np_qlast != NETMAP_HW_RING) {
2010 		lim_tx = lim_rx = priv->np_qlast;
2011 	}
2012 
2013 	/*
2014 	 * We start with a lock free round which is good if we have
2015 	 * data available. If this fails, then lock and call the sync
2016 	 * routines.
2017 	 */
2018 	for (i = priv->np_qfirst; want_rx && i < lim_rx; i++) {
2019 		kring = &na->rx_rings[i];
2020 		if (kring->ring->avail > 0) {
2021 			revents |= want_rx;
2022 			want_rx = 0;	/* also breaks the loop */
2023 		}
2024 	}
2025 	for (i = priv->np_qfirst; want_tx && i < lim_tx; i++) {
2026 		kring = &na->tx_rings[i];
2027 		if (kring->ring->avail > 0) {
2028 			revents |= want_tx;
2029 			want_tx = 0;	/* also breaks the loop */
2030 		}
2031 	}
2032 
2033 	/*
2034 	 * If we to push packets out (priv->np_txpoll) or want_tx is
2035 	 * still set, we do need to run the txsync calls (on all rings,
2036 	 * to avoid that the tx rings stall).
2037 	 */
2038 	if (priv->np_txpoll || want_tx) {
2039 flush_tx:
2040 		for (i = priv->np_qfirst; i < lim_tx; i++) {
2041 			kring = &na->tx_rings[i];
2042 			/*
2043 			 * Skip the current ring if want_tx == 0
2044 			 * (we have already done a successful sync on
2045 			 * a previous ring) AND kring->cur == kring->hwcur
2046 			 * (there are no pending transmissions for this ring).
2047 			 */
2048 			if (!want_tx && kring->ring->cur == kring->nr_hwcur)
2049 				continue;
2050 			if (core_lock == NEED_CL) {
2051 				na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
2052 				core_lock = LOCKED_CL;
2053 			}
2054 			if (na->separate_locks)
2055 				na->nm_lock(ifp, NETMAP_TX_LOCK, i);
2056 			if (netmap_verbose & NM_VERB_TXSYNC)
2057 				D("send %d on %s %d",
2058 					kring->ring->cur,
2059 					ifp->if_xname, i);
2060 			if (na->nm_txsync(ifp, i, 0 /* no lock */))
2061 				revents |= POLLERR;
2062 
2063 			/* Check avail/call selrecord only if called with POLLOUT */
2064 			if (want_tx) {
2065 				if (kring->ring->avail > 0) {
2066 					/* stop at the first ring. We don't risk
2067 					 * starvation.
2068 					 */
2069 					revents |= want_tx;
2070 					want_tx = 0;
2071 				} else if (!check_all)
2072 					selrecord(td, &kring->si);
2073 			}
2074 			if (na->separate_locks)
2075 				na->nm_lock(ifp, NETMAP_TX_UNLOCK, i);
2076 		}
2077 	}
2078 
2079 	/*
2080 	 * now if want_rx is still set we need to lock and rxsync.
2081 	 * Do it on all rings because otherwise we starve.
2082 	 */
2083 	if (want_rx) {
2084 		for (i = priv->np_qfirst; i < lim_rx; i++) {
2085 			kring = &na->rx_rings[i];
2086 			if (core_lock == NEED_CL) {
2087 				na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
2088 				core_lock = LOCKED_CL;
2089 			}
2090 			if (na->separate_locks)
2091 				na->nm_lock(ifp, NETMAP_RX_LOCK, i);
2092 			if (netmap_fwd ||kring->ring->flags & NR_FORWARD) {
2093 				ND(10, "forwarding some buffers up %d to %d",
2094 				    kring->nr_hwcur, kring->ring->cur);
2095 				netmap_grab_packets(kring, &q, netmap_fwd);
2096 			}
2097 
2098 			if (na->nm_rxsync(ifp, i, 0 /* no lock */))
2099 				revents |= POLLERR;
2100 			if (netmap_no_timestamp == 0 ||
2101 					kring->ring->flags & NR_TIMESTAMP) {
2102 				microtime(&kring->ring->ts);
2103 			}
2104 
2105 			if (kring->ring->avail > 0)
2106 				revents |= want_rx;
2107 			else if (!check_all)
2108 				selrecord(td, &kring->si);
2109 			if (na->separate_locks)
2110 				na->nm_lock(ifp, NETMAP_RX_UNLOCK, i);
2111 		}
2112 	}
2113 	if (check_all && revents == 0) { /* signal on the global queue */
2114 		if (want_tx)
2115 			selrecord(td, &na->tx_si);
2116 		if (want_rx)
2117 			selrecord(td, &na->rx_si);
2118 	}
2119 
2120 	/* forward host to the netmap ring */
2121 	kring = &na->rx_rings[lim_rx];
2122 	if (kring->nr_hwavail > 0)
2123 		ND("host rx %d has %d packets", lim_rx, kring->nr_hwavail);
2124 	if ( (priv->np_qlast == NETMAP_HW_RING) // XXX check_all
2125 			&& (netmap_fwd || kring->ring->flags & NR_FORWARD)
2126 			 && kring->nr_hwavail > 0 && !host_forwarded) {
2127 		if (core_lock == NEED_CL) {
2128 			na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
2129 			core_lock = LOCKED_CL;
2130 		}
2131 		netmap_sw_to_nic(na);
2132 		host_forwarded = 1; /* prevent another pass */
2133 		want_rx = 0;
2134 		goto flush_tx;
2135 	}
2136 
2137 	if (core_lock == LOCKED_CL)
2138 		na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
2139 	if (q.head)
2140 		netmap_send_up(na->ifp, q.head);
2141 
2142 	return (revents);
2143 }
2144 
2145 /*------- driver support routines ------*/
2146 
2147 
2148 /*
2149  * default lock wrapper.
2150  */
2151 static void
2152 netmap_lock_wrapper(struct ifnet *dev, int what, u_int queueid)
2153 {
2154 	struct netmap_adapter *na = NA(dev);
2155 
2156 	switch (what) {
2157 #ifdef linux	/* some system do not need lock on register */
2158 	case NETMAP_REG_LOCK:
2159 	case NETMAP_REG_UNLOCK:
2160 		break;
2161 #endif /* linux */
2162 
2163 	case NETMAP_CORE_LOCK:
2164 		mtx_lock(&na->core_lock);
2165 		break;
2166 
2167 	case NETMAP_CORE_UNLOCK:
2168 		mtx_unlock(&na->core_lock);
2169 		break;
2170 
2171 	case NETMAP_TX_LOCK:
2172 		mtx_lock(&na->tx_rings[queueid].q_lock);
2173 		break;
2174 
2175 	case NETMAP_TX_UNLOCK:
2176 		mtx_unlock(&na->tx_rings[queueid].q_lock);
2177 		break;
2178 
2179 	case NETMAP_RX_LOCK:
2180 		mtx_lock(&na->rx_rings[queueid].q_lock);
2181 		break;
2182 
2183 	case NETMAP_RX_UNLOCK:
2184 		mtx_unlock(&na->rx_rings[queueid].q_lock);
2185 		break;
2186 	}
2187 }
2188 
2189 
2190 /*
2191  * Initialize a ``netmap_adapter`` object created by driver on attach.
2192  * We allocate a block of memory with room for a struct netmap_adapter
2193  * plus two sets of N+2 struct netmap_kring (where N is the number
2194  * of hardware rings):
2195  * krings	0..N-1	are for the hardware queues.
2196  * kring	N	is for the host stack queue
2197  * kring	N+1	is only used for the selinfo for all queues.
2198  * Return 0 on success, ENOMEM otherwise.
2199  *
2200  * By default the receive and transmit adapter ring counts are both initialized
2201  * to num_queues.  na->num_tx_rings can be set for cards with different tx/rx
2202  * setups.
2203  */
2204 int
2205 netmap_attach(struct netmap_adapter *arg, int num_queues)
2206 {
2207 	struct netmap_adapter *na = NULL;
2208 	struct ifnet *ifp = arg ? arg->ifp : NULL;
2209 	int len;
2210 
2211 	if (arg == NULL || ifp == NULL)
2212 		goto fail;
2213 	len = nma_is_vp(arg) ? sizeof(*na) : sizeof(*na) * 2;
2214 	na = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
2215 	if (na == NULL)
2216 		goto fail;
2217 	WNA(ifp) = na;
2218 	*na = *arg; /* copy everything, trust the driver to not pass junk */
2219 	NETMAP_SET_CAPABLE(ifp);
2220 	if (na->num_tx_rings == 0)
2221 		na->num_tx_rings = num_queues;
2222 	na->num_rx_rings = num_queues;
2223 	na->refcount = na->na_single = na->na_multi = 0;
2224 	/* Core lock initialized here, others after netmap_if_new. */
2225 	mtx_init(&na->core_lock, "netmap core lock", MTX_NETWORK_LOCK, MTX_DEF);
2226 	if (na->nm_lock == NULL) {
2227 		ND("using default locks for %s", ifp->if_xname);
2228 		na->nm_lock = netmap_lock_wrapper;
2229 	}
2230 #ifdef linux
2231 	if (ifp->netdev_ops) {
2232 		ND("netdev_ops %p", ifp->netdev_ops);
2233 		/* prepare a clone of the netdev ops */
2234 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28)
2235 		na->nm_ndo.ndo_start_xmit = ifp->netdev_ops;
2236 #else
2237 		na->nm_ndo = *ifp->netdev_ops;
2238 #endif
2239 	}
2240 	na->nm_ndo.ndo_start_xmit = linux_netmap_start;
2241 #endif
2242 	if (!nma_is_vp(arg))
2243 		netmap_attach_sw(ifp);
2244 	D("success for %s", ifp->if_xname);
2245 	return 0;
2246 
2247 fail:
2248 	D("fail, arg %p ifp %p na %p", arg, ifp, na);
2249 	netmap_detach(ifp);
2250 	return (na ? EINVAL : ENOMEM);
2251 }
2252 
2253 
2254 /*
2255  * Free the allocated memory linked to the given ``netmap_adapter``
2256  * object.
2257  */
2258 void
2259 netmap_detach(struct ifnet *ifp)
2260 {
2261 	struct netmap_adapter *na = NA(ifp);
2262 
2263 	if (!na)
2264 		return;
2265 
2266 	mtx_destroy(&na->core_lock);
2267 
2268 	if (na->tx_rings) { /* XXX should not happen */
2269 		D("freeing leftover tx_rings");
2270 		free(na->tx_rings, M_DEVBUF);
2271 	}
2272 	bzero(na, sizeof(*na));
2273 	WNA(ifp) = NULL;
2274 	free(na, M_DEVBUF);
2275 }
2276 
2277 
2278 int
2279 nm_bdg_flush(struct nm_bdg_fwd *ft, int n, struct netmap_adapter *na, u_int ring_nr);
2280 
2281 /* we don't need to lock myself */
2282 static int
2283 bdg_netmap_start(struct ifnet *ifp, struct mbuf *m)
2284 {
2285 	struct netmap_adapter *na = SWNA(ifp);
2286 	struct nm_bdg_fwd *ft = na->rx_rings[0].nkr_ft;
2287 	char *buf = NMB(&na->rx_rings[0].ring->slot[0]);
2288 	u_int len = MBUF_LEN(m);
2289 
2290 	if (!na->na_bdg) /* SWNA is not configured to be attached */
2291 		return EBUSY;
2292 	m_copydata(m, 0, len, buf);
2293 	ft->ft_flags = 0;	// XXX could be indirect ?
2294 	ft->ft_len = len;
2295 	ft->ft_buf = buf;
2296 	ft->ft_next = NM_BDG_BATCH; // XXX is it needed ?
2297 	nm_bdg_flush(ft, 1, na, 0);
2298 
2299 	/* release the mbuf in either cases of success or failure. As an
2300 	 * alternative, put the mbuf in a free list and free the list
2301 	 * only when really necessary.
2302 	 */
2303 	m_freem(m);
2304 
2305 	return (0);
2306 }
2307 
2308 
2309 /*
2310  * Intercept packets from the network stack and pass them
2311  * to netmap as incoming packets on the 'software' ring.
2312  * We are not locked when called.
2313  */
2314 int
2315 netmap_start(struct ifnet *ifp, struct mbuf *m)
2316 {
2317 	struct netmap_adapter *na = NA(ifp);
2318 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
2319 	u_int i, len = MBUF_LEN(m);
2320 	u_int error = EBUSY, lim = kring->nkr_num_slots - 1;
2321 	struct netmap_slot *slot;
2322 
2323 	if (netmap_verbose & NM_VERB_HOST)
2324 		D("%s packet %d len %d from the stack", ifp->if_xname,
2325 			kring->nr_hwcur + kring->nr_hwavail, len);
2326 	if (len > NETMAP_BUF_SIZE) { /* too long for us */
2327 		D("%s from_host, drop packet size %d > %d", ifp->if_xname,
2328 			len, NETMAP_BUF_SIZE);
2329 		m_freem(m);
2330 		return EINVAL;
2331 	}
2332 	if (na->na_bdg)
2333 		return bdg_netmap_start(ifp, m);
2334 
2335 	na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
2336 	if (kring->nr_hwavail >= lim) {
2337 		if (netmap_verbose)
2338 			D("stack ring %s full\n", ifp->if_xname);
2339 		goto done;	/* no space */
2340 	}
2341 
2342 	/* compute the insert position */
2343 	i = kring->nr_hwcur + kring->nr_hwavail;
2344 	if (i > lim)
2345 		i -= lim + 1;
2346 	slot = &kring->ring->slot[i];
2347 	m_copydata(m, 0, len, NMB(slot));
2348 	slot->len = len;
2349 	slot->flags = kring->nkr_slot_flags;
2350 	kring->nr_hwavail++;
2351 	if (netmap_verbose  & NM_VERB_HOST)
2352 		D("wake up host ring %s %d", na->ifp->if_xname, na->num_rx_rings);
2353 	selwakeuppri(&kring->si, PI_NET);
2354 	error = 0;
2355 done:
2356 	na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
2357 
2358 	/* release the mbuf in either cases of success or failure. As an
2359 	 * alternative, put the mbuf in a free list and free the list
2360 	 * only when really necessary.
2361 	 */
2362 	m_freem(m);
2363 
2364 	return (error);
2365 }
2366 
2367 
2368 /*
2369  * netmap_reset() is called by the driver routines when reinitializing
2370  * a ring. The driver is in charge of locking to protect the kring.
2371  * If netmap mode is not set just return NULL.
2372  */
2373 struct netmap_slot *
2374 netmap_reset(struct netmap_adapter *na, enum txrx tx, int n,
2375 	u_int new_cur)
2376 {
2377 	struct netmap_kring *kring;
2378 	int new_hwofs, lim;
2379 
2380 	if (na == NULL)
2381 		return NULL;	/* no netmap support here */
2382 	if (!(na->ifp->if_capenable & IFCAP_NETMAP))
2383 		return NULL;	/* nothing to reinitialize */
2384 
2385 	if (tx == NR_TX) {
2386 		if (n >= na->num_tx_rings)
2387 			return NULL;
2388 		kring = na->tx_rings + n;
2389 		new_hwofs = kring->nr_hwcur - new_cur;
2390 	} else {
2391 		if (n >= na->num_rx_rings)
2392 			return NULL;
2393 		kring = na->rx_rings + n;
2394 		new_hwofs = kring->nr_hwcur + kring->nr_hwavail - new_cur;
2395 	}
2396 	lim = kring->nkr_num_slots - 1;
2397 	if (new_hwofs > lim)
2398 		new_hwofs -= lim + 1;
2399 
2400 	/* Alwayws set the new offset value and realign the ring. */
2401 	kring->nkr_hwofs = new_hwofs;
2402 	if (tx == NR_TX)
2403 		kring->nr_hwavail = kring->nkr_num_slots - 1;
2404 	ND(10, "new hwofs %d on %s %s[%d]",
2405 			kring->nkr_hwofs, na->ifp->if_xname,
2406 			tx == NR_TX ? "TX" : "RX", n);
2407 
2408 #if 0 // def linux
2409 	/* XXX check that the mappings are correct */
2410 	/* need ring_nr, adapter->pdev, direction */
2411 	buffer_info->dma = dma_map_single(&pdev->dev, addr, adapter->rx_buffer_len, DMA_FROM_DEVICE);
2412 	if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
2413 		D("error mapping rx netmap buffer %d", i);
2414 		// XXX fix error handling
2415 	}
2416 
2417 #endif /* linux */
2418 	/*
2419 	 * Wakeup on the individual and global lock
2420 	 * We do the wakeup here, but the ring is not yet reconfigured.
2421 	 * However, we are under lock so there are no races.
2422 	 */
2423 	selwakeuppri(&kring->si, PI_NET);
2424 	selwakeuppri(tx == NR_TX ? &na->tx_si : &na->rx_si, PI_NET);
2425 	return kring->ring->slot;
2426 }
2427 
2428 
2429 /* returns the next position in the ring */
2430 static int
2431 nm_bdg_preflush(struct netmap_adapter *na, u_int ring_nr,
2432 	struct netmap_kring *kring, u_int end)
2433 {
2434 	struct netmap_ring *ring = kring->ring;
2435 	struct nm_bdg_fwd *ft = kring->nkr_ft;
2436 	u_int j = kring->nr_hwcur, lim = kring->nkr_num_slots - 1;
2437 	u_int ft_i = 0;	/* start from 0 */
2438 
2439 	for (; likely(j != end); j = unlikely(j == lim) ? 0 : j+1) {
2440 		struct netmap_slot *slot = &ring->slot[j];
2441 		char *buf = NMB(slot);
2442 		int len = ft[ft_i].ft_len = slot->len;
2443 
2444 		ft[ft_i].ft_flags = slot->flags;
2445 
2446 		ND("flags is 0x%x", slot->flags);
2447 		/* this slot goes into a list so initialize the link field */
2448 		ft[ft_i].ft_next = NM_BDG_BATCH; /* equivalent to NULL */
2449 		if (unlikely(len < 14))
2450 			continue;
2451 		buf = ft[ft_i].ft_buf = (slot->flags & NS_INDIRECT) ?
2452 			*((void **)buf) : buf;
2453 		prefetch(buf);
2454 		if (unlikely(++ft_i == netmap_bridge))
2455 			ft_i = nm_bdg_flush(ft, ft_i, na, ring_nr);
2456 	}
2457 	if (ft_i)
2458 		ft_i = nm_bdg_flush(ft, ft_i, na, ring_nr);
2459 	return j;
2460 }
2461 
2462 
2463 /*
2464  * Pass packets from nic to the bridge. Must be called with
2465  * proper locks on the source interface.
2466  * Note, no user process can access this NIC so we can ignore
2467  * the info in the 'ring'.
2468  */
2469 static void
2470 netmap_nic_to_bdg(struct ifnet *ifp, u_int ring_nr)
2471 {
2472 	struct netmap_adapter *na = NA(ifp);
2473 	struct netmap_kring *kring = &na->rx_rings[ring_nr];
2474 	struct netmap_ring *ring = kring->ring;
2475 	int j, k, lim = kring->nkr_num_slots - 1;
2476 
2477 	/* fetch packets that have arrived */
2478 	na->nm_rxsync(ifp, ring_nr, 0);
2479 	/* XXX we don't count reserved, but it should be 0 */
2480 	j = kring->nr_hwcur;
2481 	k = j + kring->nr_hwavail;
2482 	if (k > lim)
2483 		k -= lim + 1;
2484 	if (k == j && netmap_verbose) {
2485 		D("how strange, interrupt with no packets on %s",
2486 			ifp->if_xname);
2487 		return;
2488 	}
2489 
2490 	j = nm_bdg_preflush(na, ring_nr, kring, k);
2491 
2492 	/* we consume everything, but we cannot update kring directly
2493 	 * because the nic may have destroyed the info in the NIC ring.
2494 	 * So we need to call rxsync again to restore it.
2495 	 */
2496 	ring->cur = j;
2497 	ring->avail = 0;
2498 	na->nm_rxsync(ifp, ring_nr, 0);
2499 	return;
2500 }
2501 
2502 
2503 /*
2504  * Default functions to handle rx/tx interrupts
2505  * we have 4 cases:
2506  * 1 ring, single lock:
2507  *	lock(core); wake(i=0); unlock(core)
2508  * N rings, single lock:
2509  *	lock(core); wake(i); wake(N+1) unlock(core)
2510  * 1 ring, separate locks: (i=0)
2511  *	lock(i); wake(i); unlock(i)
2512  * N rings, separate locks:
2513  *	lock(i); wake(i); unlock(i); lock(core) wake(N+1) unlock(core)
2514  * work_done is non-null on the RX path.
2515  *
2516  * The 'q' argument also includes flag to tell whether the queue is
2517  * already locked on enter, and whether it should remain locked on exit.
2518  * This helps adapting to different defaults in drivers and OSes.
2519  */
2520 int
2521 netmap_rx_irq(struct ifnet *ifp, int q, int *work_done)
2522 {
2523 	struct netmap_adapter *na;
2524 	struct netmap_kring *r;
2525 	NM_SELINFO_T *main_wq;
2526 	int locktype, unlocktype, nic_to_bridge, lock;
2527 
2528 	if (!(ifp->if_capenable & IFCAP_NETMAP))
2529 		return 0;
2530 
2531 	lock = q & (NETMAP_LOCKED_ENTER | NETMAP_LOCKED_EXIT);
2532 	q = q & NETMAP_RING_MASK;
2533 
2534 	ND(5, "received %s queue %d", work_done ? "RX" : "TX" , q);
2535 	na = NA(ifp);
2536 	if (na->na_flags & NAF_SKIP_INTR) {
2537 		ND("use regular interrupt");
2538 		return 0;
2539 	}
2540 
2541 	if (work_done) { /* RX path */
2542 		if (q >= na->num_rx_rings)
2543 			return 0;	// not a physical queue
2544 		r = na->rx_rings + q;
2545 		r->nr_kflags |= NKR_PENDINTR;
2546 		main_wq = (na->num_rx_rings > 1) ? &na->rx_si : NULL;
2547 		/* set a flag if the NIC is attached to a VALE switch */
2548 		nic_to_bridge = (na->na_bdg != NULL);
2549 		locktype = NETMAP_RX_LOCK;
2550 		unlocktype = NETMAP_RX_UNLOCK;
2551 	} else { /* TX path */
2552 		if (q >= na->num_tx_rings)
2553 			return 0;	// not a physical queue
2554 		r = na->tx_rings + q;
2555 		main_wq = (na->num_tx_rings > 1) ? &na->tx_si : NULL;
2556 		work_done = &q; /* dummy */
2557 		nic_to_bridge = 0;
2558 		locktype = NETMAP_TX_LOCK;
2559 		unlocktype = NETMAP_TX_UNLOCK;
2560 	}
2561 	if (na->separate_locks) {
2562 		if (!(lock & NETMAP_LOCKED_ENTER))
2563 			na->nm_lock(ifp, locktype, q);
2564 		/* If a NIC is attached to a bridge, flush packets
2565 		 * (and no need to wakeup anyone). Otherwise, wakeup
2566 		 * possible processes waiting for packets.
2567 		 */
2568 		if (nic_to_bridge)
2569 			netmap_nic_to_bdg(ifp, q);
2570 		else
2571 			selwakeuppri(&r->si, PI_NET);
2572 		na->nm_lock(ifp, unlocktype, q);
2573 		if (main_wq && !nic_to_bridge) {
2574 			na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
2575 			selwakeuppri(main_wq, PI_NET);
2576 			na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
2577 		}
2578 		/* lock the queue again if requested */
2579 		if (lock & NETMAP_LOCKED_EXIT)
2580 			na->nm_lock(ifp, locktype, q);
2581 	} else {
2582 		if (!(lock & NETMAP_LOCKED_ENTER))
2583 			na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
2584 		if (nic_to_bridge)
2585 			netmap_nic_to_bdg(ifp, q);
2586 		else {
2587 			selwakeuppri(&r->si, PI_NET);
2588 			if (main_wq)
2589 				selwakeuppri(main_wq, PI_NET);
2590 		}
2591 		if (!(lock & NETMAP_LOCKED_EXIT))
2592 			na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
2593 	}
2594 	*work_done = 1; /* do not fire napi again */
2595 	return 1;
2596 }
2597 
2598 
2599 #ifdef linux	/* linux-specific routines */
2600 
2601 
2602 /*
2603  * Remap linux arguments into the FreeBSD call.
2604  * - pwait is the poll table, passed as 'dev';
2605  *   If pwait == NULL someone else already woke up before. We can report
2606  *   events but they are filtered upstream.
2607  *   If pwait != NULL, then pwait->key contains the list of events.
2608  * - events is computed from pwait as above.
2609  * - file is passed as 'td';
2610  */
2611 static u_int
2612 linux_netmap_poll(struct file * file, struct poll_table_struct *pwait)
2613 {
2614 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
2615 	int events = POLLIN | POLLOUT; /* XXX maybe... */
2616 #elif LINUX_VERSION_CODE < KERNEL_VERSION(3,4,0)
2617 	int events = pwait ? pwait->key : POLLIN | POLLOUT;
2618 #else /* in 3.4.0 field 'key' was renamed to '_key' */
2619 	int events = pwait ? pwait->_key : POLLIN | POLLOUT;
2620 #endif
2621 	return netmap_poll((void *)pwait, events, (void *)file);
2622 }
2623 
2624 
2625 static int
2626 linux_netmap_mmap(struct file *f, struct vm_area_struct *vma)
2627 {
2628 	int lut_skip, i, j;
2629 	int user_skip = 0;
2630 	struct lut_entry *l_entry;
2631 	int error = 0;
2632 	unsigned long off, tomap;
2633 	/*
2634 	 * vma->vm_start: start of mapping user address space
2635 	 * vma->vm_end: end of the mapping user address space
2636 	 * vma->vm_pfoff: offset of first page in the device
2637 	 */
2638 
2639 	// XXX security checks
2640 
2641 	error = netmap_get_memory(f->private_data);
2642 	ND("get_memory returned %d", error);
2643 	if (error)
2644 	    return -error;
2645 
2646 	off = vma->vm_pgoff << PAGE_SHIFT; /* offset in bytes */
2647 	tomap = vma->vm_end - vma->vm_start;
2648 	for (i = 0; i < NETMAP_POOLS_NR; i++) {  /* loop through obj_pools */
2649 		const struct netmap_obj_pool *p = &nm_mem.pools[i];
2650 		/*
2651 		 * In each pool memory is allocated in clusters
2652 		 * of size _clustsize, each containing clustentries
2653 		 * entries. For each object k we already store the
2654 		 * vtophys mapping in lut[k] so we use that, scanning
2655 		 * the lut[] array in steps of clustentries,
2656 		 * and we map each cluster (not individual pages,
2657 		 * it would be overkill -- XXX slow ? 20130415).
2658 		 */
2659 
2660 		/*
2661 		 * We interpret vm_pgoff as an offset into the whole
2662 		 * netmap memory, as if all clusters where contiguous.
2663 		 */
2664 		for (lut_skip = 0, j = 0; j < p->_numclusters; j++, lut_skip += p->clustentries) {
2665 			unsigned long paddr, mapsize;
2666 			if (p->_clustsize <= off) {
2667 				off -= p->_clustsize;
2668 				continue;
2669 			}
2670 			l_entry = &p->lut[lut_skip]; /* first obj in the cluster */
2671 			paddr = l_entry->paddr + off;
2672 			mapsize = p->_clustsize - off;
2673 			off = 0;
2674 			if (mapsize > tomap)
2675 				mapsize = tomap;
2676 			ND("remap_pfn_range(%lx, %lx, %lx)",
2677 				vma->vm_start + user_skip,
2678 				paddr >> PAGE_SHIFT, mapsize);
2679 			if (remap_pfn_range(vma, vma->vm_start + user_skip,
2680 					paddr >> PAGE_SHIFT, mapsize,
2681 					vma->vm_page_prot))
2682 				return -EAGAIN; // XXX check return value
2683 			user_skip += mapsize;
2684 			tomap -= mapsize;
2685 			if (tomap == 0)
2686 				goto done;
2687 		}
2688 	}
2689 done:
2690 
2691 	return 0;
2692 }
2693 
2694 
2695 static netdev_tx_t
2696 linux_netmap_start(struct sk_buff *skb, struct net_device *dev)
2697 {
2698 	netmap_start(dev, skb);
2699 	return (NETDEV_TX_OK);
2700 }
2701 
2702 
2703 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)	// XXX was 38
2704 #define LIN_IOCTL_NAME	.ioctl
2705 int
2706 linux_netmap_ioctl(struct inode *inode, struct file *file, u_int cmd, u_long data /* arg */)
2707 #else
2708 #define LIN_IOCTL_NAME	.unlocked_ioctl
2709 long
2710 linux_netmap_ioctl(struct file *file, u_int cmd, u_long data /* arg */)
2711 #endif
2712 {
2713 	int ret;
2714 	struct nmreq nmr;
2715 	bzero(&nmr, sizeof(nmr));
2716 
2717 	if (data && copy_from_user(&nmr, (void *)data, sizeof(nmr) ) != 0)
2718 		return -EFAULT;
2719 	ret = netmap_ioctl(NULL, cmd, (caddr_t)&nmr, 0, (void *)file);
2720 	if (data && copy_to_user((void*)data, &nmr, sizeof(nmr) ) != 0)
2721 		return -EFAULT;
2722 	return -ret;
2723 }
2724 
2725 
2726 static int
2727 netmap_release(struct inode *inode, struct file *file)
2728 {
2729 	(void)inode;	/* UNUSED */
2730 	if (file->private_data)
2731 		netmap_dtor(file->private_data);
2732 	return (0);
2733 }
2734 
2735 
2736 static int
2737 linux_netmap_open(struct inode *inode, struct file *file)
2738 {
2739 	struct netmap_priv_d *priv;
2740 	(void)inode;	/* UNUSED */
2741 
2742 	priv = malloc(sizeof(struct netmap_priv_d), M_DEVBUF,
2743 			      M_NOWAIT | M_ZERO);
2744 	if (priv == NULL)
2745 		return -ENOMEM;
2746 
2747 	file->private_data = priv;
2748 
2749 	return (0);
2750 }
2751 
2752 
2753 static struct file_operations netmap_fops = {
2754     .owner = THIS_MODULE,
2755     .open = linux_netmap_open,
2756     .mmap = linux_netmap_mmap,
2757     LIN_IOCTL_NAME = linux_netmap_ioctl,
2758     .poll = linux_netmap_poll,
2759     .release = netmap_release,
2760 };
2761 
2762 
2763 static struct miscdevice netmap_cdevsw = {	/* same name as FreeBSD */
2764 	MISC_DYNAMIC_MINOR,
2765 	"netmap",
2766 	&netmap_fops,
2767 };
2768 
2769 static int netmap_init(void);
2770 static void netmap_fini(void);
2771 
2772 
2773 /* Errors have negative values on linux */
2774 static int linux_netmap_init(void)
2775 {
2776 	return -netmap_init();
2777 }
2778 
2779 module_init(linux_netmap_init);
2780 module_exit(netmap_fini);
2781 /* export certain symbols to other modules */
2782 EXPORT_SYMBOL(netmap_attach);		// driver attach routines
2783 EXPORT_SYMBOL(netmap_detach);		// driver detach routines
2784 EXPORT_SYMBOL(netmap_ring_reinit);	// ring init on error
2785 EXPORT_SYMBOL(netmap_buffer_lut);
2786 EXPORT_SYMBOL(netmap_total_buffers);	// index check
2787 EXPORT_SYMBOL(netmap_buffer_base);
2788 EXPORT_SYMBOL(netmap_reset);		// ring init routines
2789 EXPORT_SYMBOL(netmap_buf_size);
2790 EXPORT_SYMBOL(netmap_rx_irq);		// default irq handler
2791 EXPORT_SYMBOL(netmap_no_pendintr);	// XXX mitigation - should go away
2792 EXPORT_SYMBOL(netmap_bdg_ctl);		// bridge configuration routine
2793 EXPORT_SYMBOL(netmap_bdg_learning);	// the default lookup function
2794 
2795 
2796 MODULE_AUTHOR("http://info.iet.unipi.it/~luigi/netmap/");
2797 MODULE_DESCRIPTION("The netmap packet I/O framework");
2798 MODULE_LICENSE("Dual BSD/GPL"); /* the code here is all BSD. */
2799 
2800 #else /* __FreeBSD__ */
2801 
2802 
2803 static struct cdevsw netmap_cdevsw = {
2804 	.d_version = D_VERSION,
2805 	.d_name = "netmap",
2806 	.d_open = netmap_open,
2807 	.d_mmap = netmap_mmap,
2808 	.d_mmap_single = netmap_mmap_single,
2809 	.d_ioctl = netmap_ioctl,
2810 	.d_poll = netmap_poll,
2811 	.d_close = netmap_close,
2812 };
2813 #endif /* __FreeBSD__ */
2814 
2815 #ifdef NM_BRIDGE
2816 /*
2817  *---- support for virtual bridge -----
2818  */
2819 
2820 /* ----- FreeBSD if_bridge hash function ------- */
2821 
2822 /*
2823  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2824  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2825  *
2826  * http://www.burtleburtle.net/bob/hash/spooky.html
2827  */
2828 #define mix(a, b, c)                                                    \
2829 do {                                                                    \
2830         a -= b; a -= c; a ^= (c >> 13);                                 \
2831         b -= c; b -= a; b ^= (a << 8);                                  \
2832         c -= a; c -= b; c ^= (b >> 13);                                 \
2833         a -= b; a -= c; a ^= (c >> 12);                                 \
2834         b -= c; b -= a; b ^= (a << 16);                                 \
2835         c -= a; c -= b; c ^= (b >> 5);                                  \
2836         a -= b; a -= c; a ^= (c >> 3);                                  \
2837         b -= c; b -= a; b ^= (a << 10);                                 \
2838         c -= a; c -= b; c ^= (b >> 15);                                 \
2839 } while (/*CONSTCOND*/0)
2840 
2841 static __inline uint32_t
2842 nm_bridge_rthash(const uint8_t *addr)
2843 {
2844         uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = 0; // hask key
2845 
2846         b += addr[5] << 8;
2847         b += addr[4];
2848         a += addr[3] << 24;
2849         a += addr[2] << 16;
2850         a += addr[1] << 8;
2851         a += addr[0];
2852 
2853         mix(a, b, c);
2854 #define BRIDGE_RTHASH_MASK	(NM_BDG_HASH-1)
2855         return (c & BRIDGE_RTHASH_MASK);
2856 }
2857 
2858 #undef mix
2859 
2860 
2861 static int
2862 bdg_netmap_reg(struct ifnet *ifp, int onoff)
2863 {
2864 	// struct nm_bridge *b = NA(ifp)->na_bdg;
2865 
2866 	/* the interface is already attached to the bridge,
2867 	 * so we only need to toggle IFCAP_NETMAP.
2868 	 * Locking is not necessary (we are already under
2869 	 * NMA_LOCK, and the port is not in use during this call).
2870 	 */
2871 	/* BDG_WLOCK(b); */
2872 	if (onoff) {
2873 		ifp->if_capenable |= IFCAP_NETMAP;
2874 	} else {
2875 		ifp->if_capenable &= ~IFCAP_NETMAP;
2876 	}
2877 	/* BDG_WUNLOCK(b); */
2878 	return 0;
2879 }
2880 
2881 
2882 /*
2883  * Lookup function for a learning bridge.
2884  * Update the hash table with the source address,
2885  * and then returns the destination port index, and the
2886  * ring in *dst_ring (at the moment, always use ring 0)
2887  */
2888 u_int
2889 netmap_bdg_learning(char *buf, u_int len, uint8_t *dst_ring,
2890 		struct netmap_adapter *na)
2891 {
2892 	struct nm_hash_ent *ht = na->na_bdg->ht;
2893 	uint32_t sh, dh;
2894 	u_int dst, mysrc = na->bdg_port;
2895 	uint64_t smac, dmac;
2896 
2897 	dmac = le64toh(*(uint64_t *)(buf)) & 0xffffffffffff;
2898 	smac = le64toh(*(uint64_t *)(buf + 4));
2899 	smac >>= 16;
2900 
2901 	/*
2902 	 * The hash is somewhat expensive, there might be some
2903 	 * worthwhile optimizations here.
2904 	 */
2905 	if ((buf[6] & 1) == 0) { /* valid src */
2906 		uint8_t *s = buf+6;
2907 		sh = nm_bridge_rthash(buf+6); // XXX hash of source
2908 		/* update source port forwarding entry */
2909 		ht[sh].mac = smac;	/* XXX expire ? */
2910 		ht[sh].ports = mysrc;
2911 		if (netmap_verbose)
2912 		    D("src %02x:%02x:%02x:%02x:%02x:%02x on port %d",
2913 			s[0], s[1], s[2], s[3], s[4], s[5], mysrc);
2914 	}
2915 	dst = NM_BDG_BROADCAST;
2916 	if ((buf[0] & 1) == 0) { /* unicast */
2917 		dh = nm_bridge_rthash(buf); // XXX hash of dst
2918 		if (ht[dh].mac == dmac) {	/* found dst */
2919 			dst = ht[dh].ports;
2920 		}
2921 		/* XXX otherwise return NM_BDG_UNKNOWN ? */
2922 	}
2923 	*dst_ring = 0;
2924 	return dst;
2925 }
2926 
2927 
2928 /*
2929  * This flush routine supports only unicast and broadcast but a large
2930  * number of ports, and lets us replace the learn and dispatch functions.
2931  */
2932 int
2933 nm_bdg_flush(struct nm_bdg_fwd *ft, int n, struct netmap_adapter *na,
2934 		u_int ring_nr)
2935 {
2936 	struct nm_bdg_q *dst_ents, *brddst;
2937 	uint16_t num_dsts = 0, *dsts;
2938 	struct nm_bridge *b = na->na_bdg;
2939 	u_int i, me = na->bdg_port;
2940 
2941 	dst_ents = (struct nm_bdg_q *)(ft + NM_BDG_BATCH);
2942 	dsts = (uint16_t *)(dst_ents + NM_BDG_MAXPORTS * NM_BDG_MAXRINGS + 1);
2943 
2944 	BDG_RLOCK(b);
2945 
2946 	/* first pass: find a destination */
2947 	for (i = 0; likely(i < n); i++) {
2948 		uint8_t *buf = ft[i].ft_buf;
2949 		uint8_t dst_ring = ring_nr;
2950 		uint16_t dst_port, d_i;
2951 		struct nm_bdg_q *d;
2952 
2953 		dst_port = b->nm_bdg_lookup(buf, ft[i].ft_len, &dst_ring, na);
2954 		if (dst_port == NM_BDG_NOPORT) {
2955 			continue; /* this packet is identified to be dropped */
2956 		} else if (unlikely(dst_port > NM_BDG_MAXPORTS)) {
2957 			continue;
2958 		} else if (dst_port == NM_BDG_BROADCAST) {
2959 			dst_ring = 0; /* broadcasts always go to ring 0 */
2960 		} else if (unlikely(dst_port == me ||
2961 		    !BDG_GET_VAR(b->bdg_ports[dst_port]))) {
2962 			continue;
2963 		}
2964 
2965 		/* get a position in the scratch pad */
2966 		d_i = dst_port * NM_BDG_MAXRINGS + dst_ring;
2967 		d = dst_ents + d_i;
2968 		if (d->bq_head == NM_BDG_BATCH) { /* new destination */
2969 			d->bq_head = d->bq_tail = i;
2970 			/* remember this position to be scanned later */
2971 			if (dst_port != NM_BDG_BROADCAST)
2972 				dsts[num_dsts++] = d_i;
2973 		} else {
2974 			ft[d->bq_tail].ft_next = i;
2975 			d->bq_tail = i;
2976 		}
2977 	}
2978 
2979 	/* if there is a broadcast, set ring 0 of all ports to be scanned
2980 	 * XXX This would be optimized by recording the highest index of active
2981 	 * ports.
2982 	 */
2983 	brddst = dst_ents + NM_BDG_BROADCAST * NM_BDG_MAXRINGS;
2984 	if (brddst->bq_head != NM_BDG_BATCH) {
2985 		for (i = 0; likely(i < NM_BDG_MAXPORTS); i++) {
2986 			uint16_t d_i = i * NM_BDG_MAXRINGS;
2987 			if (unlikely(i == me) || !BDG_GET_VAR(b->bdg_ports[i]))
2988 				continue;
2989 			else if (dst_ents[d_i].bq_head == NM_BDG_BATCH)
2990 				dsts[num_dsts++] = d_i;
2991 		}
2992 	}
2993 
2994 	/* second pass: scan destinations (XXX will be modular somehow) */
2995 	for (i = 0; i < num_dsts; i++) {
2996 		struct ifnet *dst_ifp;
2997 		struct netmap_adapter *dst_na;
2998 		struct netmap_kring *kring;
2999 		struct netmap_ring *ring;
3000 		u_int dst_nr, is_vp, lim, j, sent = 0, d_i, next, brd_next;
3001 		int howmany, retry = netmap_txsync_retry;
3002 		struct nm_bdg_q *d;
3003 
3004 		d_i = dsts[i];
3005 		d = dst_ents + d_i;
3006 		dst_na = BDG_GET_VAR(b->bdg_ports[d_i/NM_BDG_MAXRINGS]);
3007 		/* protect from the lookup function returning an inactive
3008 		 * destination port
3009 		 */
3010 		if (unlikely(dst_na == NULL))
3011 			continue;
3012 		else if (dst_na->na_flags & NAF_SW_ONLY)
3013 			continue;
3014 		dst_ifp = dst_na->ifp;
3015 		/*
3016 		 * The interface may be in !netmap mode in two cases:
3017 		 * - when na is attached but not activated yet;
3018 		 * - when na is being deactivated but is still attached.
3019 		 */
3020 		if (unlikely(!(dst_ifp->if_capenable & IFCAP_NETMAP)))
3021 			continue;
3022 
3023 		/* there is at least one either unicast or broadcast packet */
3024 		brd_next = brddst->bq_head;
3025 		next = d->bq_head;
3026 
3027 		is_vp = nma_is_vp(dst_na);
3028 		dst_nr = d_i & (NM_BDG_MAXRINGS-1);
3029 		if (is_vp) { /* virtual port */
3030 			if (dst_nr >= dst_na->num_rx_rings)
3031 				dst_nr = dst_nr % dst_na->num_rx_rings;
3032 			kring = &dst_na->rx_rings[dst_nr];
3033 			ring = kring->ring;
3034 			lim = kring->nkr_num_slots - 1;
3035 			dst_na->nm_lock(dst_ifp, NETMAP_RX_LOCK, dst_nr);
3036 			j = kring->nr_hwcur + kring->nr_hwavail;
3037 			if (j > lim)
3038 				j -= kring->nkr_num_slots;
3039 			howmany = lim - kring->nr_hwavail;
3040 		} else { /* hw or sw adapter */
3041 			if (dst_nr >= dst_na->num_tx_rings)
3042 				dst_nr = dst_nr % dst_na->num_tx_rings;
3043 			kring = &dst_na->tx_rings[dst_nr];
3044 			ring = kring->ring;
3045 			lim = kring->nkr_num_slots - 1;
3046 			dst_na->nm_lock(dst_ifp, NETMAP_TX_LOCK, dst_nr);
3047 retry:
3048 			dst_na->nm_txsync(dst_ifp, dst_nr, 0);
3049 			/* see nm_bdg_flush() */
3050 			j = kring->nr_hwcur;
3051 			howmany = kring->nr_hwavail;
3052 		}
3053 		while (howmany-- > 0) {
3054 			struct netmap_slot *slot;
3055 			struct nm_bdg_fwd *ft_p;
3056 
3057 			/* our 'NULL' is always higher than valid indexes
3058 			 * so we never dereference it if the other list
3059 			 * has packets (and if both are NULL we never
3060 			 * get here).
3061 			 */
3062 			if (next < brd_next) {
3063 				ft_p = ft + next;
3064 				next = ft_p->ft_next;
3065 				ND("j %d uni %d next %d %d",
3066 					j, ft_p - ft, next, brd_next);
3067 			} else { /* insert broadcast */
3068 				ft_p = ft + brd_next;
3069 				brd_next = ft_p->ft_next;
3070 				ND("j %d brd %d next %d %d",
3071 					j, ft_p - ft, next, brd_next);
3072 			}
3073 			slot = &ring->slot[j];
3074 			ND("send %d %d bytes at %s:%d", i, ft_p->ft_len, dst_ifp->if_xname, j);
3075 		    if (ft_p->ft_flags & NS_INDIRECT) {
3076 			ND("copying from INDIRECT source");
3077 			copyin(ft_p->ft_buf, NMB(slot),
3078 				(ft_p->ft_len + 63) & ~63);
3079 		    } else {
3080 			pkt_copy(ft_p->ft_buf, NMB(slot), ft_p->ft_len);
3081 		    }
3082 			slot->len = ft_p->ft_len;
3083 			j = unlikely(j == lim) ? 0: j + 1; /* XXX to be macro-ed */
3084 			sent++;
3085 			/* are we done ? */
3086 			if (next == NM_BDG_BATCH && brd_next == NM_BDG_BATCH)
3087 				break;
3088 		}
3089 		if (netmap_verbose && (howmany < 0))
3090 			D("rx ring full on %s", dst_ifp->if_xname);
3091 		if (is_vp) {
3092 			if (sent) {
3093 				kring->nr_hwavail += sent;
3094 				selwakeuppri(&kring->si, PI_NET);
3095 			}
3096 			dst_na->nm_lock(dst_ifp, NETMAP_RX_UNLOCK, dst_nr);
3097 		} else {
3098 			if (sent) {
3099 				ring->avail -= sent;
3100 				ring->cur = j;
3101 				dst_na->nm_txsync(dst_ifp, dst_nr, 0);
3102 			}
3103 			/* retry to send more packets */
3104 			if (nma_is_hw(dst_na) && howmany < 0 && retry--)
3105 				goto retry;
3106 			dst_na->nm_lock(dst_ifp, NETMAP_TX_UNLOCK, dst_nr);
3107 		}
3108 		/* NM_BDG_BATCH means 'no packet' */
3109 		d->bq_head = d->bq_tail = NM_BDG_BATCH; /* cleanup */
3110 	}
3111 	brddst->bq_head = brddst->bq_tail = NM_BDG_BATCH; /* cleanup */
3112 	BDG_RUNLOCK(b);
3113 	return 0;
3114 }
3115 
3116 
3117 /*
3118  * main dispatch routine
3119  */
3120 static int
3121 bdg_netmap_txsync(struct ifnet *ifp, u_int ring_nr, int do_lock)
3122 {
3123 	struct netmap_adapter *na = NA(ifp);
3124 	struct netmap_kring *kring = &na->tx_rings[ring_nr];
3125 	struct netmap_ring *ring = kring->ring;
3126 	int i, j, k, lim = kring->nkr_num_slots - 1;
3127 
3128 	k = ring->cur;
3129 	if (k > lim)
3130 		return netmap_ring_reinit(kring);
3131 	if (do_lock)
3132 		na->nm_lock(ifp, NETMAP_TX_LOCK, ring_nr);
3133 
3134 	if (netmap_bridge <= 0) { /* testing only */
3135 		j = k; // used all
3136 		goto done;
3137 	}
3138 	if (netmap_bridge > NM_BDG_BATCH)
3139 		netmap_bridge = NM_BDG_BATCH;
3140 
3141 	j = nm_bdg_preflush(na, ring_nr, kring, k);
3142 	i = k - j;
3143 	if (i < 0)
3144 		i += kring->nkr_num_slots;
3145 	kring->nr_hwavail = kring->nkr_num_slots - 1 - i;
3146 	if (j != k)
3147 		D("early break at %d/ %d, avail %d", j, k, kring->nr_hwavail);
3148 
3149 done:
3150 	kring->nr_hwcur = j;
3151 	ring->avail = kring->nr_hwavail;
3152 	if (do_lock)
3153 		na->nm_lock(ifp, NETMAP_TX_UNLOCK, ring_nr);
3154 
3155 	if (netmap_verbose)
3156 		D("%s ring %d lock %d", ifp->if_xname, ring_nr, do_lock);
3157 	return 0;
3158 }
3159 
3160 
3161 static int
3162 bdg_netmap_rxsync(struct ifnet *ifp, u_int ring_nr, int do_lock)
3163 {
3164 	struct netmap_adapter *na = NA(ifp);
3165 	struct netmap_kring *kring = &na->rx_rings[ring_nr];
3166 	struct netmap_ring *ring = kring->ring;
3167 	u_int j, lim = kring->nkr_num_slots - 1;
3168 	u_int k = ring->cur, resvd = ring->reserved;
3169 	int n;
3170 
3171 	ND("%s ring %d lock %d avail %d",
3172 		ifp->if_xname, ring_nr, do_lock, kring->nr_hwavail);
3173 
3174 	if (k > lim)
3175 		return netmap_ring_reinit(kring);
3176 	if (do_lock)
3177 		na->nm_lock(ifp, NETMAP_RX_LOCK, ring_nr);
3178 
3179 	/* skip past packets that userspace has released */
3180 	j = kring->nr_hwcur;    /* netmap ring index */
3181 	if (resvd > 0) {
3182 		if (resvd + ring->avail >= lim + 1) {
3183 			D("XXX invalid reserve/avail %d %d", resvd, ring->avail);
3184 			ring->reserved = resvd = 0; // XXX panic...
3185 		}
3186 		k = (k >= resvd) ? k - resvd : k + lim + 1 - resvd;
3187 	}
3188 
3189 	if (j != k) { /* userspace has released some packets. */
3190 		n = k - j;
3191 		if (n < 0)
3192 			n += kring->nkr_num_slots;
3193 		ND("userspace releases %d packets", n);
3194                 for (n = 0; likely(j != k); n++) {
3195                         struct netmap_slot *slot = &ring->slot[j];
3196                         void *addr = NMB(slot);
3197 
3198                         if (addr == netmap_buffer_base) { /* bad buf */
3199                                 if (do_lock)
3200                                         na->nm_lock(ifp, NETMAP_RX_UNLOCK, ring_nr);
3201                                 return netmap_ring_reinit(kring);
3202                         }
3203 			/* decrease refcount for buffer */
3204 
3205 			slot->flags &= ~NS_BUF_CHANGED;
3206                         j = unlikely(j == lim) ? 0 : j + 1;
3207                 }
3208                 kring->nr_hwavail -= n;
3209                 kring->nr_hwcur = k;
3210         }
3211         /* tell userspace that there are new packets */
3212         ring->avail = kring->nr_hwavail - resvd;
3213 
3214 	if (do_lock)
3215 		na->nm_lock(ifp, NETMAP_RX_UNLOCK, ring_nr);
3216 	return 0;
3217 }
3218 
3219 
3220 static void
3221 bdg_netmap_attach(struct netmap_adapter *arg)
3222 {
3223 	struct netmap_adapter na;
3224 
3225 	ND("attaching virtual bridge");
3226 	bzero(&na, sizeof(na));
3227 
3228 	na.ifp = arg->ifp;
3229 	na.separate_locks = 1;
3230 	na.num_tx_rings = arg->num_tx_rings;
3231 	na.num_rx_rings = arg->num_rx_rings;
3232 	na.num_tx_desc = NM_BRIDGE_RINGSIZE;
3233 	na.num_rx_desc = NM_BRIDGE_RINGSIZE;
3234 	na.nm_txsync = bdg_netmap_txsync;
3235 	na.nm_rxsync = bdg_netmap_rxsync;
3236 	na.nm_register = bdg_netmap_reg;
3237 	netmap_attach(&na, na.num_tx_rings);
3238 }
3239 
3240 #endif /* NM_BRIDGE */
3241 
3242 static struct cdev *netmap_dev; /* /dev/netmap character device. */
3243 
3244 
3245 /*
3246  * Module loader.
3247  *
3248  * Create the /dev/netmap device and initialize all global
3249  * variables.
3250  *
3251  * Return 0 on success, errno on failure.
3252  */
3253 static int
3254 netmap_init(void)
3255 {
3256 	int error;
3257 
3258 	error = netmap_memory_init();
3259 	if (error != 0) {
3260 		printf("netmap: unable to initialize the memory allocator.\n");
3261 		return (error);
3262 	}
3263 	printf("netmap: loaded module\n");
3264 	netmap_dev = make_dev(&netmap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660,
3265 			      "netmap");
3266 
3267 #ifdef NM_BRIDGE
3268 	{
3269 	int i;
3270 	mtx_init(&netmap_bridge_mutex, "netmap_bridge_mutex",
3271 		MTX_NETWORK_LOCK, MTX_DEF);
3272 	bzero(nm_bridges, sizeof(struct nm_bridge) * NM_BRIDGES); /* safety */
3273 	for (i = 0; i < NM_BRIDGES; i++)
3274 		rw_init(&nm_bridges[i].bdg_lock, "bdg lock");
3275 	}
3276 #endif
3277 	return (error);
3278 }
3279 
3280 
3281 /*
3282  * Module unloader.
3283  *
3284  * Free all the memory, and destroy the ``/dev/netmap`` device.
3285  */
3286 static void
3287 netmap_fini(void)
3288 {
3289 	destroy_dev(netmap_dev);
3290 	netmap_memory_fini();
3291 	printf("netmap: unloaded module.\n");
3292 }
3293 
3294 
3295 #ifdef __FreeBSD__
3296 /*
3297  * Kernel entry point.
3298  *
3299  * Initialize/finalize the module and return.
3300  *
3301  * Return 0 on success, errno on failure.
3302  */
3303 static int
3304 netmap_loader(__unused struct module *module, int event, __unused void *arg)
3305 {
3306 	int error = 0;
3307 
3308 	switch (event) {
3309 	case MOD_LOAD:
3310 		error = netmap_init();
3311 		break;
3312 
3313 	case MOD_UNLOAD:
3314 		netmap_fini();
3315 		break;
3316 
3317 	default:
3318 		error = EOPNOTSUPP;
3319 		break;
3320 	}
3321 
3322 	return (error);
3323 }
3324 
3325 
3326 DEV_MODULE(netmap, netmap_loader, NULL);
3327 #endif /* __FreeBSD__ */
3328