xref: /freebsd-14.2/sys/dev/netmap/netmap.c (revision 2f70fca5)
168b8534bSLuigi Rizzo /*
2f196ce38SLuigi Rizzo  * Copyright (C) 2011-2012 Matteo Landi, Luigi Rizzo. All rights reserved.
368b8534bSLuigi Rizzo  *
468b8534bSLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
568b8534bSLuigi Rizzo  * modification, are permitted provided that the following conditions
668b8534bSLuigi Rizzo  * are met:
768b8534bSLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
868b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
968b8534bSLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
1068b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
1168b8534bSLuigi Rizzo  *    documentation and/or other materials provided with the distribution.
1268b8534bSLuigi Rizzo  *
1368b8534bSLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1468b8534bSLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1568b8534bSLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1668b8534bSLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1768b8534bSLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1868b8534bSLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1968b8534bSLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2068b8534bSLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2168b8534bSLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2268b8534bSLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2368b8534bSLuigi Rizzo  * SUCH DAMAGE.
2468b8534bSLuigi Rizzo  */
2568b8534bSLuigi Rizzo 
26f196ce38SLuigi Rizzo #define NM_BRIDGE
27f196ce38SLuigi Rizzo 
2868b8534bSLuigi Rizzo /*
2968b8534bSLuigi Rizzo  * This module supports memory mapped access to network devices,
3068b8534bSLuigi Rizzo  * see netmap(4).
3168b8534bSLuigi Rizzo  *
3268b8534bSLuigi Rizzo  * The module uses a large, memory pool allocated by the kernel
3368b8534bSLuigi Rizzo  * and accessible as mmapped memory by multiple userspace threads/processes.
3468b8534bSLuigi Rizzo  * The memory pool contains packet buffers and "netmap rings",
3568b8534bSLuigi Rizzo  * i.e. user-accessible copies of the interface's queues.
3668b8534bSLuigi Rizzo  *
3768b8534bSLuigi Rizzo  * Access to the network card works like this:
3868b8534bSLuigi Rizzo  * 1. a process/thread issues one or more open() on /dev/netmap, to create
3968b8534bSLuigi Rizzo  *    select()able file descriptor on which events are reported.
4068b8534bSLuigi Rizzo  * 2. on each descriptor, the process issues an ioctl() to identify
4168b8534bSLuigi Rizzo  *    the interface that should report events to the file descriptor.
4268b8534bSLuigi Rizzo  * 3. on each descriptor, the process issues an mmap() request to
4368b8534bSLuigi Rizzo  *    map the shared memory region within the process' address space.
4468b8534bSLuigi Rizzo  *    The list of interesting queues is indicated by a location in
4568b8534bSLuigi Rizzo  *    the shared memory region.
4668b8534bSLuigi Rizzo  * 4. using the functions in the netmap(4) userspace API, a process
4768b8534bSLuigi Rizzo  *    can look up the occupation state of a queue, access memory buffers,
4868b8534bSLuigi Rizzo  *    and retrieve received packets or enqueue packets to transmit.
4968b8534bSLuigi Rizzo  * 5. using some ioctl()s the process can synchronize the userspace view
5068b8534bSLuigi Rizzo  *    of the queue with the actual status in the kernel. This includes both
5168b8534bSLuigi Rizzo  *    receiving the notification of new packets, and transmitting new
5268b8534bSLuigi Rizzo  *    packets on the output interface.
5368b8534bSLuigi Rizzo  * 6. select() or poll() can be used to wait for events on individual
5468b8534bSLuigi Rizzo  *    transmit or receive queues (or all queues for a given interface).
5568b8534bSLuigi Rizzo  */
5668b8534bSLuigi Rizzo 
57f196ce38SLuigi Rizzo #ifdef linux
58f196ce38SLuigi Rizzo #include "bsd_glue.h"
5942a3a5bdSLuigi Rizzo static netdev_tx_t linux_netmap_start(struct sk_buff *skb, struct net_device *dev);
60f196ce38SLuigi Rizzo #endif /* linux */
6101c7d25fSLuigi Rizzo 
62f196ce38SLuigi Rizzo #ifdef __APPLE__
63f196ce38SLuigi Rizzo #include "osx_glue.h"
6401c7d25fSLuigi Rizzo #endif /* __APPLE__ */
6501c7d25fSLuigi Rizzo 
66f196ce38SLuigi Rizzo #ifdef __FreeBSD__
6768b8534bSLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */
6868b8534bSLuigi Rizzo __FBSDID("$FreeBSD$");
6968b8534bSLuigi Rizzo 
7068b8534bSLuigi Rizzo #include <sys/types.h>
7168b8534bSLuigi Rizzo #include <sys/module.h>
7268b8534bSLuigi Rizzo #include <sys/errno.h>
7368b8534bSLuigi Rizzo #include <sys/param.h>	/* defines used in kernel.h */
74506cc70cSLuigi Rizzo #include <sys/jail.h>
7568b8534bSLuigi Rizzo #include <sys/kernel.h>	/* types used in module initialization */
7668b8534bSLuigi Rizzo #include <sys/conf.h>	/* cdevsw struct */
7768b8534bSLuigi Rizzo #include <sys/uio.h>	/* uio struct */
7868b8534bSLuigi Rizzo #include <sys/sockio.h>
7968b8534bSLuigi Rizzo #include <sys/socketvar.h>	/* struct socket */
8068b8534bSLuigi Rizzo #include <sys/malloc.h>
8168b8534bSLuigi Rizzo #include <sys/mman.h>	/* PROT_EXEC */
8268b8534bSLuigi Rizzo #include <sys/poll.h>
83506cc70cSLuigi Rizzo #include <sys/proc.h>
8468b8534bSLuigi Rizzo #include <vm/vm.h>	/* vtophys */
8568b8534bSLuigi Rizzo #include <vm/pmap.h>	/* vtophys */
8668b8534bSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
8768b8534bSLuigi Rizzo #include <machine/bus.h>
8868b8534bSLuigi Rizzo #include <sys/selinfo.h>
8968b8534bSLuigi Rizzo #include <sys/sysctl.h>
9068b8534bSLuigi Rizzo #include <net/if.h>
9168b8534bSLuigi Rizzo #include <net/bpf.h>		/* BIOCIMMEDIATE */
92506cc70cSLuigi Rizzo #include <net/vnet.h>
9368b8534bSLuigi Rizzo #include <machine/bus.h>	/* bus_dmamap_* */
9468b8534bSLuigi Rizzo 
9568b8534bSLuigi Rizzo MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map");
96f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */
9768b8534bSLuigi Rizzo 
980b8ed8e0SLuigi Rizzo #include <net/netmap.h>
990b8ed8e0SLuigi Rizzo #include <dev/netmap/netmap_kern.h>
1000b8ed8e0SLuigi Rizzo 
10168b8534bSLuigi Rizzo /*
10268b8534bSLuigi Rizzo  * lock and unlock for the netmap memory allocator
10368b8534bSLuigi Rizzo  */
10464ae02c3SLuigi Rizzo #define NMA_LOCK()	mtx_lock(&nm_mem->nm_mtx);
10564ae02c3SLuigi Rizzo #define NMA_UNLOCK()	mtx_unlock(&nm_mem->nm_mtx);
1065819da83SLuigi Rizzo struct netmap_mem_d;
10764ae02c3SLuigi Rizzo static struct netmap_mem_d *nm_mem;	/* Our memory allocator. */
1085819da83SLuigi Rizzo 
1095819da83SLuigi Rizzo u_int netmap_total_buffers;
1105819da83SLuigi Rizzo char *netmap_buffer_base;	/* address of an invalid buffer */
1115819da83SLuigi Rizzo 
1125819da83SLuigi Rizzo /* user-controlled variables */
1135819da83SLuigi Rizzo int netmap_verbose;
1145819da83SLuigi Rizzo 
1155819da83SLuigi Rizzo static int netmap_no_timestamp; /* don't timestamp on rxsync */
1165819da83SLuigi Rizzo 
1175819da83SLuigi Rizzo SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args");
1185819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose,
1195819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode");
1205819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp,
1215819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp");
122b3d53016SLuigi Rizzo u_int netmap_buf_size = 2048;
123b3d53016SLuigi Rizzo TUNABLE_INT("hw.netmap.buf_size", (u_int *)&netmap_buf_size);
1245819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, buf_size,
1255819da83SLuigi Rizzo     CTLFLAG_RD, &netmap_buf_size, 0, "Size of packet buffers");
1265819da83SLuigi Rizzo int netmap_mitigate = 1;
1275819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mitigate, CTLFLAG_RW, &netmap_mitigate, 0, "");
128c85cb1a0SLuigi Rizzo int netmap_no_pendintr = 1;
1295819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr,
1305819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_pendintr, 0, "Always look for new received packets.");
1315819da83SLuigi Rizzo 
132f196ce38SLuigi Rizzo int netmap_drop = 0;	/* debugging */
133f196ce38SLuigi Rizzo int netmap_flags = 0;	/* debug flags */
134f196ce38SLuigi Rizzo int netmap_copy = 0;	/* debugging, copy content */
135f196ce38SLuigi Rizzo 
136f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, drop, CTLFLAG_RW, &netmap_drop, 0 , "");
137f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, flags, CTLFLAG_RW, &netmap_flags, 0 , "");
138f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, copy, CTLFLAG_RW, &netmap_copy, 0 , "");
139f196ce38SLuigi Rizzo 
140f196ce38SLuigi Rizzo #ifdef NM_BRIDGE /* support for netmap bridge */
141f196ce38SLuigi Rizzo 
142f196ce38SLuigi Rizzo /*
143f196ce38SLuigi Rizzo  * system parameters.
144f196ce38SLuigi Rizzo  *
145f196ce38SLuigi Rizzo  * All switched ports have prefix NM_NAME.
146f196ce38SLuigi Rizzo  * The switch has a max of NM_BDG_MAXPORTS ports (often stored in a bitmap,
147f196ce38SLuigi Rizzo  * so a practical upper bound is 64).
148f196ce38SLuigi Rizzo  * Each tx ring is read-write, whereas rx rings are readonly (XXX not done yet).
149f196ce38SLuigi Rizzo  * The virtual interfaces use per-queue lock instead of core lock.
150f196ce38SLuigi Rizzo  * In the tx loop, we aggregate traffic in batches to make all operations
151f196ce38SLuigi Rizzo  * faster. The batch size is NM_BDG_BATCH
152f196ce38SLuigi Rizzo  */
153f196ce38SLuigi Rizzo #define	NM_NAME			"vale"	/* prefix for the interface */
154f196ce38SLuigi Rizzo #define NM_BDG_MAXPORTS		16	/* up to 64 ? */
155f196ce38SLuigi Rizzo #define NM_BRIDGE_RINGSIZE	1024	/* in the device */
156f196ce38SLuigi Rizzo #define NM_BDG_HASH		1024	/* forwarding table entries */
157f196ce38SLuigi Rizzo #define NM_BDG_BATCH		1024	/* entries in the forwarding buffer */
158f196ce38SLuigi Rizzo #define	NM_BRIDGES		4	/* number of bridges */
159f196ce38SLuigi Rizzo int netmap_bridge = NM_BDG_BATCH; /* bridge batch size */
160f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, bridge, CTLFLAG_RW, &netmap_bridge, 0 , "");
16101c7d25fSLuigi Rizzo 
162f196ce38SLuigi Rizzo #ifdef linux
163f196ce38SLuigi Rizzo #define	ADD_BDG_REF(ifp)	(NA(ifp)->if_refcount++)
164f196ce38SLuigi Rizzo #define	DROP_BDG_REF(ifp)	(NA(ifp)->if_refcount-- <= 1)
165f196ce38SLuigi Rizzo #else /* !linux */
166f196ce38SLuigi Rizzo #define	ADD_BDG_REF(ifp)	(ifp)->if_refcount++
167f196ce38SLuigi Rizzo #define	DROP_BDG_REF(ifp)	refcount_release(&(ifp)->if_refcount)
168f196ce38SLuigi Rizzo #ifdef __FreeBSD__
169f196ce38SLuigi Rizzo #include <sys/endian.h>
170f196ce38SLuigi Rizzo #include <sys/refcount.h>
171f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */
17201c7d25fSLuigi Rizzo #define prefetch(x)	__builtin_prefetch(x)
173f196ce38SLuigi Rizzo #endif /* !linux */
174f196ce38SLuigi Rizzo 
175f196ce38SLuigi Rizzo static void bdg_netmap_attach(struct ifnet *ifp);
176f196ce38SLuigi Rizzo static int bdg_netmap_reg(struct ifnet *ifp, int onoff);
177f196ce38SLuigi Rizzo /* per-tx-queue entry */
178f196ce38SLuigi Rizzo struct nm_bdg_fwd {	/* forwarding entry for a bridge */
179f196ce38SLuigi Rizzo 	void *buf;
180f196ce38SLuigi Rizzo 	uint64_t dst;	/* dst mask */
181f196ce38SLuigi Rizzo 	uint32_t src;	/* src index ? */
182f196ce38SLuigi Rizzo 	uint16_t len;	/* src len */
183f196ce38SLuigi Rizzo };
184f196ce38SLuigi Rizzo 
185f196ce38SLuigi Rizzo struct nm_hash_ent {
186f196ce38SLuigi Rizzo 	uint64_t	mac;	/* the top 2 bytes are the epoch */
187f196ce38SLuigi Rizzo 	uint64_t	ports;
188f196ce38SLuigi Rizzo };
189f196ce38SLuigi Rizzo 
190f196ce38SLuigi Rizzo /*
191f196ce38SLuigi Rizzo  * Interfaces for a bridge are all in ports[].
192f196ce38SLuigi Rizzo  * The array has fixed size, an empty entry does not terminate
193f196ce38SLuigi Rizzo  * the search.
194f196ce38SLuigi Rizzo  */
195f196ce38SLuigi Rizzo struct nm_bridge {
196f196ce38SLuigi Rizzo 	struct ifnet *bdg_ports[NM_BDG_MAXPORTS];
197f196ce38SLuigi Rizzo 	int n_ports;
198f196ce38SLuigi Rizzo 	uint64_t act_ports;
199f196ce38SLuigi Rizzo 	int freelist;	/* first buffer index */
200f196ce38SLuigi Rizzo 	NM_SELINFO_T si;	/* poll/select wait queue */
201f196ce38SLuigi Rizzo 	NM_LOCK_T bdg_lock;	/* protect the selinfo ? */
202f196ce38SLuigi Rizzo 
203f196ce38SLuigi Rizzo 	/* the forwarding table, MAC+ports */
204f196ce38SLuigi Rizzo 	struct nm_hash_ent ht[NM_BDG_HASH];
205f196ce38SLuigi Rizzo 
206f196ce38SLuigi Rizzo 	int namelen;	/* 0 means free */
207f196ce38SLuigi Rizzo 	char basename[IFNAMSIZ];
208f196ce38SLuigi Rizzo };
209f196ce38SLuigi Rizzo 
210f196ce38SLuigi Rizzo struct nm_bridge nm_bridges[NM_BRIDGES];
211f196ce38SLuigi Rizzo 
212f196ce38SLuigi Rizzo #define BDG_LOCK(b)	mtx_lock(&(b)->bdg_lock)
213f196ce38SLuigi Rizzo #define BDG_UNLOCK(b)	mtx_unlock(&(b)->bdg_lock)
214f196ce38SLuigi Rizzo 
215f196ce38SLuigi Rizzo /*
216f196ce38SLuigi Rizzo  * NA(ifp)->bdg_port	port index
217f196ce38SLuigi Rizzo  */
218f196ce38SLuigi Rizzo 
219f196ce38SLuigi Rizzo // XXX only for multiples of 64 bytes, non overlapped.
220f196ce38SLuigi Rizzo static inline void
221f196ce38SLuigi Rizzo pkt_copy(void *_src, void *_dst, int l)
222f196ce38SLuigi Rizzo {
223f196ce38SLuigi Rizzo         uint64_t *src = _src;
224f196ce38SLuigi Rizzo         uint64_t *dst = _dst;
225f196ce38SLuigi Rizzo         if (unlikely(l >= 1024)) {
226f196ce38SLuigi Rizzo                 bcopy(src, dst, l);
227f196ce38SLuigi Rizzo                 return;
228f196ce38SLuigi Rizzo         }
229f196ce38SLuigi Rizzo         for (; likely(l > 0); l-=64) {
230f196ce38SLuigi Rizzo                 *dst++ = *src++;
231f196ce38SLuigi Rizzo                 *dst++ = *src++;
232f196ce38SLuigi Rizzo                 *dst++ = *src++;
233f196ce38SLuigi Rizzo                 *dst++ = *src++;
234f196ce38SLuigi Rizzo                 *dst++ = *src++;
235f196ce38SLuigi Rizzo                 *dst++ = *src++;
236f196ce38SLuigi Rizzo                 *dst++ = *src++;
237f196ce38SLuigi Rizzo                 *dst++ = *src++;
238f196ce38SLuigi Rizzo         }
239f196ce38SLuigi Rizzo }
240f196ce38SLuigi Rizzo 
241f196ce38SLuigi Rizzo /*
242f196ce38SLuigi Rizzo  * locate a bridge among the existing ones.
243f196ce38SLuigi Rizzo  * a ':' in the name terminates the bridge name. Otherwise, just NM_NAME.
244f196ce38SLuigi Rizzo  * We assume that this is called with a name of at least NM_NAME chars.
245f196ce38SLuigi Rizzo  */
246f196ce38SLuigi Rizzo static struct nm_bridge *
247f196ce38SLuigi Rizzo nm_find_bridge(const char *name)
248f196ce38SLuigi Rizzo {
249f196ce38SLuigi Rizzo 	int i, l, namelen, e;
250f196ce38SLuigi Rizzo 	struct nm_bridge *b = NULL;
251f196ce38SLuigi Rizzo 
252f196ce38SLuigi Rizzo 	namelen = strlen(NM_NAME);	/* base length */
253f196ce38SLuigi Rizzo 	l = strlen(name);		/* actual length */
254f196ce38SLuigi Rizzo 	for (i = namelen + 1; i < l; i++) {
255f196ce38SLuigi Rizzo 		if (name[i] == ':') {
256f196ce38SLuigi Rizzo 			namelen = i;
257f196ce38SLuigi Rizzo 			break;
258f196ce38SLuigi Rizzo 		}
259f196ce38SLuigi Rizzo 	}
260f196ce38SLuigi Rizzo 	if (namelen >= IFNAMSIZ)
261f196ce38SLuigi Rizzo 		namelen = IFNAMSIZ;
262f196ce38SLuigi Rizzo 	ND("--- prefix is '%.*s' ---", namelen, name);
263f196ce38SLuigi Rizzo 
264f196ce38SLuigi Rizzo 	/* use the first entry for locking */
265f196ce38SLuigi Rizzo 	BDG_LOCK(nm_bridges); // XXX do better
266f196ce38SLuigi Rizzo 	for (e = -1, i = 1; i < NM_BRIDGES; i++) {
267f196ce38SLuigi Rizzo 		b = nm_bridges + i;
268f196ce38SLuigi Rizzo 		if (b->namelen == 0)
269f196ce38SLuigi Rizzo 			e = i;	/* record empty slot */
270f196ce38SLuigi Rizzo 		else if (strncmp(name, b->basename, namelen) == 0) {
271f196ce38SLuigi Rizzo 			ND("found '%.*s' at %d", namelen, name, i);
272f196ce38SLuigi Rizzo 			break;
273f196ce38SLuigi Rizzo 		}
274f196ce38SLuigi Rizzo 	}
275f196ce38SLuigi Rizzo 	if (i == NM_BRIDGES) { /* all full */
276f196ce38SLuigi Rizzo 		if (e == -1) { /* no empty slot */
277f196ce38SLuigi Rizzo 			b = NULL;
278f196ce38SLuigi Rizzo 		} else {
279f196ce38SLuigi Rizzo 			b = nm_bridges + e;
280f196ce38SLuigi Rizzo 			strncpy(b->basename, name, namelen);
281f196ce38SLuigi Rizzo 			b->namelen = namelen;
282f196ce38SLuigi Rizzo 		}
283f196ce38SLuigi Rizzo 	}
284f196ce38SLuigi Rizzo 	BDG_UNLOCK(nm_bridges);
285f196ce38SLuigi Rizzo 	return b;
286f196ce38SLuigi Rizzo }
287f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */
2885819da83SLuigi Rizzo 
2893c0caf6cSLuigi Rizzo /*------------- memory allocator -----------------*/
2903c0caf6cSLuigi Rizzo #ifdef NETMAP_MEM2
2913c0caf6cSLuigi Rizzo #include "netmap_mem2.c"
2923c0caf6cSLuigi Rizzo #else /* !NETMAP_MEM2 */
2933c0caf6cSLuigi Rizzo #include "netmap_mem1.c"
2943c0caf6cSLuigi Rizzo #endif /* !NETMAP_MEM2 */
2953c0caf6cSLuigi Rizzo /*------------ end of memory allocator ----------*/
29668b8534bSLuigi Rizzo 
29768b8534bSLuigi Rizzo /* Structure associated to each thread which registered an interface. */
29868b8534bSLuigi Rizzo struct netmap_priv_d {
29968b8534bSLuigi Rizzo 	struct netmap_if *np_nifp;	/* netmap interface descriptor. */
30068b8534bSLuigi Rizzo 
30168b8534bSLuigi Rizzo 	struct ifnet	*np_ifp;	/* device for which we hold a reference */
30268b8534bSLuigi Rizzo 	int		np_ringid;	/* from the ioctl */
30368b8534bSLuigi Rizzo 	u_int		np_qfirst, np_qlast;	/* range of rings to scan */
30468b8534bSLuigi Rizzo 	uint16_t	np_txpoll;
30568b8534bSLuigi Rizzo };
30668b8534bSLuigi Rizzo 
30768b8534bSLuigi Rizzo 
30868b8534bSLuigi Rizzo /*
30968b8534bSLuigi Rizzo  * File descriptor's private data destructor.
31068b8534bSLuigi Rizzo  *
31168b8534bSLuigi Rizzo  * Call nm_register(ifp,0) to stop netmap mode on the interface and
31268b8534bSLuigi Rizzo  * revert to normal operation. We expect that np_ifp has not gone.
31368b8534bSLuigi Rizzo  */
31468b8534bSLuigi Rizzo static void
3155819da83SLuigi Rizzo netmap_dtor_locked(void *data)
31668b8534bSLuigi Rizzo {
31768b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = data;
31868b8534bSLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
31968b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
32068b8534bSLuigi Rizzo 	struct netmap_if *nifp = priv->np_nifp;
32168b8534bSLuigi Rizzo 
32268b8534bSLuigi Rizzo 	na->refcount--;
32368b8534bSLuigi Rizzo 	if (na->refcount <= 0) {	/* last instance */
32464ae02c3SLuigi Rizzo 		u_int i, j, lim;
32568b8534bSLuigi Rizzo 
32668b8534bSLuigi Rizzo 		D("deleting last netmap instance for %s", ifp->if_xname);
32768b8534bSLuigi Rizzo 		/*
32868b8534bSLuigi Rizzo 		 * there is a race here with *_netmap_task() and
3291a26580eSLuigi Rizzo 		 * netmap_poll(), which don't run under NETMAP_REG_LOCK.
33068b8534bSLuigi Rizzo 		 * na->refcount == 0 && na->ifp->if_capenable & IFCAP_NETMAP
33168b8534bSLuigi Rizzo 		 * (aka NETMAP_DELETING(na)) are a unique marker that the
33268b8534bSLuigi Rizzo 		 * device is dying.
33368b8534bSLuigi Rizzo 		 * Before destroying stuff we sleep a bit, and then complete
33468b8534bSLuigi Rizzo 		 * the job. NIOCREG should realize the condition and
33568b8534bSLuigi Rizzo 		 * loop until they can continue; the other routines
33668b8534bSLuigi Rizzo 		 * should check the condition at entry and quit if
33768b8534bSLuigi Rizzo 		 * they cannot run.
33868b8534bSLuigi Rizzo 		 */
3391a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
34068b8534bSLuigi Rizzo 		tsleep(na, 0, "NIOCUNREG", 4);
3411a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
34268b8534bSLuigi Rizzo 		na->nm_register(ifp, 0); /* off, clear IFCAP_NETMAP */
34368b8534bSLuigi Rizzo 		/* Wake up any sleeping threads. netmap_poll will
34468b8534bSLuigi Rizzo 		 * then return POLLERR
34568b8534bSLuigi Rizzo 		 */
346d76bf4ffSLuigi Rizzo 		for (i = 0; i < na->num_tx_rings + 1; i++)
34768b8534bSLuigi Rizzo 			selwakeuppri(&na->tx_rings[i].si, PI_NET);
348d76bf4ffSLuigi Rizzo 		for (i = 0; i < na->num_rx_rings + 1; i++)
34968b8534bSLuigi Rizzo 			selwakeuppri(&na->rx_rings[i].si, PI_NET);
35064ae02c3SLuigi Rizzo 		selwakeuppri(&na->tx_si, PI_NET);
35164ae02c3SLuigi Rizzo 		selwakeuppri(&na->rx_si, PI_NET);
35268b8534bSLuigi Rizzo 		/* release all buffers */
35368b8534bSLuigi Rizzo 		NMA_LOCK();
354d76bf4ffSLuigi Rizzo 		for (i = 0; i < na->num_tx_rings + 1; i++) {
35564ae02c3SLuigi Rizzo 			struct netmap_ring *ring = na->tx_rings[i].ring;
35668b8534bSLuigi Rizzo 			lim = na->tx_rings[i].nkr_num_slots;
35768b8534bSLuigi Rizzo 			for (j = 0; j < lim; j++)
358446ee301SLuigi Rizzo 				netmap_free_buf(nifp, ring->slot[j].buf_idx);
359*2f70fca5SEd Maste 			/* knlist_destroy(&na->tx_rings[i].si.si_note); */
360*2f70fca5SEd Maste 			mtx_destroy(&na->tx_rings[i].q_lock);
36164ae02c3SLuigi Rizzo 		}
362d76bf4ffSLuigi Rizzo 		for (i = 0; i < na->num_rx_rings + 1; i++) {
36364ae02c3SLuigi Rizzo 			struct netmap_ring *ring = na->rx_rings[i].ring;
36468b8534bSLuigi Rizzo 			lim = na->rx_rings[i].nkr_num_slots;
36568b8534bSLuigi Rizzo 			for (j = 0; j < lim; j++)
366446ee301SLuigi Rizzo 				netmap_free_buf(nifp, ring->slot[j].buf_idx);
367*2f70fca5SEd Maste 			/* knlist_destroy(&na->rx_rings[i].si.si_note); */
368*2f70fca5SEd Maste 			mtx_destroy(&na->rx_rings[i].q_lock);
36968b8534bSLuigi Rizzo 		}
370*2f70fca5SEd Maste 		/* XXX kqueue(9) needed; these will mirror knlist_init. */
371*2f70fca5SEd Maste 		/* knlist_destroy(&na->tx_si.si_note); */
372*2f70fca5SEd Maste 		/* knlist_destroy(&na->rx_si.si_note); */
37368b8534bSLuigi Rizzo 		NMA_UNLOCK();
3746e10c8b8SLuigi Rizzo 		netmap_free_rings(na);
37568b8534bSLuigi Rizzo 		wakeup(na);
37668b8534bSLuigi Rizzo 	}
3776e10c8b8SLuigi Rizzo 	netmap_if_free(nifp);
3785819da83SLuigi Rizzo }
37968b8534bSLuigi Rizzo 
380f196ce38SLuigi Rizzo static void
381f196ce38SLuigi Rizzo nm_if_rele(struct ifnet *ifp)
382f196ce38SLuigi Rizzo {
383f196ce38SLuigi Rizzo #ifndef NM_BRIDGE
384f196ce38SLuigi Rizzo 	if_rele(ifp);
385f196ce38SLuigi Rizzo #else /* NM_BRIDGE */
386f196ce38SLuigi Rizzo 	int i, full;
387f196ce38SLuigi Rizzo 	struct nm_bridge *b;
388f196ce38SLuigi Rizzo 
389f196ce38SLuigi Rizzo 	if (strncmp(ifp->if_xname, NM_NAME, sizeof(NM_NAME) - 1)) {
390f196ce38SLuigi Rizzo 		if_rele(ifp);
391f196ce38SLuigi Rizzo 		return;
392f196ce38SLuigi Rizzo 	}
393f196ce38SLuigi Rizzo 	if (!DROP_BDG_REF(ifp))
394f196ce38SLuigi Rizzo 		return;
395f196ce38SLuigi Rizzo 	b = ifp->if_bridge;
396f196ce38SLuigi Rizzo 	BDG_LOCK(nm_bridges);
397f196ce38SLuigi Rizzo 	BDG_LOCK(b);
398f196ce38SLuigi Rizzo 	ND("want to disconnect %s from the bridge", ifp->if_xname);
399f196ce38SLuigi Rizzo 	full = 0;
400f196ce38SLuigi Rizzo 	for (i = 0; i < NM_BDG_MAXPORTS; i++) {
401f196ce38SLuigi Rizzo 		if (b->bdg_ports[i] == ifp) {
402f196ce38SLuigi Rizzo 			b->bdg_ports[i] = NULL;
403f196ce38SLuigi Rizzo 			bzero(ifp, sizeof(*ifp));
404f196ce38SLuigi Rizzo 			free(ifp, M_DEVBUF);
405f196ce38SLuigi Rizzo 			break;
406f196ce38SLuigi Rizzo 		}
407f196ce38SLuigi Rizzo 		else if (b->bdg_ports[i] != NULL)
408f196ce38SLuigi Rizzo 			full = 1;
409f196ce38SLuigi Rizzo 	}
410f196ce38SLuigi Rizzo 	BDG_UNLOCK(b);
411f196ce38SLuigi Rizzo 	if (full == 0) {
412f196ce38SLuigi Rizzo 		ND("freeing bridge %d", b - nm_bridges);
413f196ce38SLuigi Rizzo 		b->namelen = 0;
414f196ce38SLuigi Rizzo 	}
415f196ce38SLuigi Rizzo 	BDG_UNLOCK(nm_bridges);
416f196ce38SLuigi Rizzo 	if (i == NM_BDG_MAXPORTS)
417f196ce38SLuigi Rizzo 		D("ouch, cannot find ifp to remove");
418f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */
419f196ce38SLuigi Rizzo }
4205819da83SLuigi Rizzo 
4215819da83SLuigi Rizzo static void
4225819da83SLuigi Rizzo netmap_dtor(void *data)
4235819da83SLuigi Rizzo {
4245819da83SLuigi Rizzo 	struct netmap_priv_d *priv = data;
4255819da83SLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
4265819da83SLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
4275819da83SLuigi Rizzo 
4281a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
4295819da83SLuigi Rizzo 	netmap_dtor_locked(data);
4301a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
43168b8534bSLuigi Rizzo 
432f196ce38SLuigi Rizzo 	nm_if_rele(ifp);
43368b8534bSLuigi Rizzo 	bzero(priv, sizeof(*priv));	/* XXX for safety */
43468b8534bSLuigi Rizzo 	free(priv, M_DEVBUF);
43568b8534bSLuigi Rizzo }
43668b8534bSLuigi Rizzo 
43768b8534bSLuigi Rizzo 
43868b8534bSLuigi Rizzo /*
43968b8534bSLuigi Rizzo  * mmap(2) support for the "netmap" device.
44068b8534bSLuigi Rizzo  *
44168b8534bSLuigi Rizzo  * Expose all the memory previously allocated by our custom memory
44268b8534bSLuigi Rizzo  * allocator: this way the user has only to issue a single mmap(2), and
44368b8534bSLuigi Rizzo  * can work on all the data structures flawlessly.
44468b8534bSLuigi Rizzo  *
44568b8534bSLuigi Rizzo  * Return 0 on success, -1 otherwise.
44668b8534bSLuigi Rizzo  */
447babc7c12SLuigi Rizzo 
448f196ce38SLuigi Rizzo #ifdef __FreeBSD__
44968b8534bSLuigi Rizzo static int
450babc7c12SLuigi Rizzo netmap_mmap(__unused struct cdev *dev,
45168b8534bSLuigi Rizzo #if __FreeBSD_version < 900000
452babc7c12SLuigi Rizzo 		vm_offset_t offset, vm_paddr_t *paddr, int nprot
45368b8534bSLuigi Rizzo #else
454babc7c12SLuigi Rizzo 		vm_ooffset_t offset, vm_paddr_t *paddr, int nprot,
455babc7c12SLuigi Rizzo 		__unused vm_memattr_t *memattr
45668b8534bSLuigi Rizzo #endif
457babc7c12SLuigi Rizzo 	)
45868b8534bSLuigi Rizzo {
45968b8534bSLuigi Rizzo 	if (nprot & PROT_EXEC)
46068b8534bSLuigi Rizzo 		return (-1);	// XXX -1 or EINVAL ?
461446ee301SLuigi Rizzo 
46268b8534bSLuigi Rizzo 	ND("request for offset 0x%x", (uint32_t)offset);
463446ee301SLuigi Rizzo 	*paddr = netmap_ofstophys(offset);
46468b8534bSLuigi Rizzo 
46568b8534bSLuigi Rizzo 	return (0);
46668b8534bSLuigi Rizzo }
467f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */
46868b8534bSLuigi Rizzo 
46968b8534bSLuigi Rizzo 
47068b8534bSLuigi Rizzo /*
47102ad4083SLuigi Rizzo  * Handlers for synchronization of the queues from/to the host.
47202ad4083SLuigi Rizzo  *
47302ad4083SLuigi Rizzo  * netmap_sync_to_host() passes packets up. We are called from a
47402ad4083SLuigi Rizzo  * system call in user process context, and the only contention
47502ad4083SLuigi Rizzo  * can be among multiple user threads erroneously calling
47602ad4083SLuigi Rizzo  * this routine concurrently. In principle we should not even
47702ad4083SLuigi Rizzo  * need to lock.
47868b8534bSLuigi Rizzo  */
47968b8534bSLuigi Rizzo static void
48068b8534bSLuigi Rizzo netmap_sync_to_host(struct netmap_adapter *na)
48168b8534bSLuigi Rizzo {
482d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->tx_rings[na->num_tx_rings];
48368b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
48468b8534bSLuigi Rizzo 	struct mbuf *head = NULL, *tail = NULL, *m;
48502ad4083SLuigi Rizzo 	u_int k, n, lim = kring->nkr_num_slots - 1;
48668b8534bSLuigi Rizzo 
48702ad4083SLuigi Rizzo 	k = ring->cur;
48802ad4083SLuigi Rizzo 	if (k > lim) {
48902ad4083SLuigi Rizzo 		netmap_ring_reinit(kring);
49002ad4083SLuigi Rizzo 		return;
49102ad4083SLuigi Rizzo 	}
4921a26580eSLuigi Rizzo 	// na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0);
49368b8534bSLuigi Rizzo 
49468b8534bSLuigi Rizzo 	/* Take packets from hwcur to cur and pass them up.
49568b8534bSLuigi Rizzo 	 * In case of no buffers we give up. At the end of the loop,
49668b8534bSLuigi Rizzo 	 * the queue is drained in all cases.
49768b8534bSLuigi Rizzo 	 */
49802ad4083SLuigi Rizzo 	for (n = kring->nr_hwcur; n != k;) {
49968b8534bSLuigi Rizzo 		struct netmap_slot *slot = &ring->slot[n];
50068b8534bSLuigi Rizzo 
50168b8534bSLuigi Rizzo 		n = (n == lim) ? 0 : n + 1;
50268b8534bSLuigi Rizzo 		if (slot->len < 14 || slot->len > NETMAP_BUF_SIZE) {
50368b8534bSLuigi Rizzo 			D("bad pkt at %d len %d", n, slot->len);
50468b8534bSLuigi Rizzo 			continue;
50568b8534bSLuigi Rizzo 		}
50668b8534bSLuigi Rizzo 		m = m_devget(NMB(slot), slot->len, 0, na->ifp, NULL);
50768b8534bSLuigi Rizzo 
50868b8534bSLuigi Rizzo 		if (m == NULL)
50968b8534bSLuigi Rizzo 			break;
51068b8534bSLuigi Rizzo 		if (tail)
51168b8534bSLuigi Rizzo 			tail->m_nextpkt = m;
51268b8534bSLuigi Rizzo 		else
51368b8534bSLuigi Rizzo 			head = m;
51468b8534bSLuigi Rizzo 		tail = m;
51568b8534bSLuigi Rizzo 		m->m_nextpkt = NULL;
51668b8534bSLuigi Rizzo 	}
51702ad4083SLuigi Rizzo 	kring->nr_hwcur = k;
51868b8534bSLuigi Rizzo 	kring->nr_hwavail = ring->avail = lim;
5191a26580eSLuigi Rizzo 	// na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0);
52068b8534bSLuigi Rizzo 
52168b8534bSLuigi Rizzo 	/* send packets up, outside the lock */
52268b8534bSLuigi Rizzo 	while ((m = head) != NULL) {
52368b8534bSLuigi Rizzo 		head = head->m_nextpkt;
52468b8534bSLuigi Rizzo 		m->m_nextpkt = NULL;
52568b8534bSLuigi Rizzo 		if (netmap_verbose & NM_VERB_HOST)
5261a26580eSLuigi Rizzo 			D("sending up pkt %p size %d", m, MBUF_LEN(m));
5271a26580eSLuigi Rizzo 		NM_SEND_UP(na->ifp, m);
52868b8534bSLuigi Rizzo 	}
52968b8534bSLuigi Rizzo }
53068b8534bSLuigi Rizzo 
53168b8534bSLuigi Rizzo /*
53202ad4083SLuigi Rizzo  * rxsync backend for packets coming from the host stack.
53302ad4083SLuigi Rizzo  * They have been put in the queue by netmap_start() so we
53402ad4083SLuigi Rizzo  * need to protect access to the kring using a lock.
53502ad4083SLuigi Rizzo  *
53668b8534bSLuigi Rizzo  * This routine also does the selrecord if called from the poll handler
53768b8534bSLuigi Rizzo  * (we know because td != NULL).
53801c7d25fSLuigi Rizzo  *
53901c7d25fSLuigi Rizzo  * NOTE: on linux, selrecord() is defined as a macro and uses pwait
54001c7d25fSLuigi Rizzo  *     as an additional hidden argument.
54168b8534bSLuigi Rizzo  */
54268b8534bSLuigi Rizzo static void
54301c7d25fSLuigi Rizzo netmap_sync_from_host(struct netmap_adapter *na, struct thread *td, void *pwait)
54468b8534bSLuigi Rizzo {
545d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
54668b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
54764ae02c3SLuigi Rizzo 	u_int j, n, lim = kring->nkr_num_slots;
54864ae02c3SLuigi Rizzo 	u_int k = ring->cur, resvd = ring->reserved;
54968b8534bSLuigi Rizzo 
55001c7d25fSLuigi Rizzo 	(void)pwait;	/* disable unused warnings */
5511a26580eSLuigi Rizzo 	na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0);
55264ae02c3SLuigi Rizzo 	if (k >= lim) {
55364ae02c3SLuigi Rizzo 		netmap_ring_reinit(kring);
55464ae02c3SLuigi Rizzo 		return;
55564ae02c3SLuigi Rizzo 	}
55664ae02c3SLuigi Rizzo 	/* new packets are already set in nr_hwavail */
55764ae02c3SLuigi Rizzo 	/* skip past packets that userspace has released */
55864ae02c3SLuigi Rizzo 	j = kring->nr_hwcur;
55964ae02c3SLuigi Rizzo 	if (resvd > 0) {
56064ae02c3SLuigi Rizzo 		if (resvd + ring->avail >= lim + 1) {
56164ae02c3SLuigi Rizzo 			D("XXX invalid reserve/avail %d %d", resvd, ring->avail);
56264ae02c3SLuigi Rizzo 			ring->reserved = resvd = 0; // XXX panic...
56364ae02c3SLuigi Rizzo 		}
56464ae02c3SLuigi Rizzo 		k = (k >= resvd) ? k - resvd : k + lim - resvd;
56564ae02c3SLuigi Rizzo         }
56664ae02c3SLuigi Rizzo 	if (j != k) {
56764ae02c3SLuigi Rizzo 		n = k >= j ? k - j : k + lim - j;
56864ae02c3SLuigi Rizzo 		kring->nr_hwavail -= n;
56902ad4083SLuigi Rizzo 		kring->nr_hwcur = k;
57064ae02c3SLuigi Rizzo 	}
57164ae02c3SLuigi Rizzo 	k = ring->avail = kring->nr_hwavail - resvd;
57202ad4083SLuigi Rizzo 	if (k == 0 && td)
57368b8534bSLuigi Rizzo 		selrecord(td, &kring->si);
57402ad4083SLuigi Rizzo 	if (k && (netmap_verbose & NM_VERB_HOST))
57502ad4083SLuigi Rizzo 		D("%d pkts from stack", k);
5761a26580eSLuigi Rizzo 	na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0);
57768b8534bSLuigi Rizzo }
57868b8534bSLuigi Rizzo 
57968b8534bSLuigi Rizzo 
58068b8534bSLuigi Rizzo /*
58168b8534bSLuigi Rizzo  * get a refcounted reference to an interface.
58268b8534bSLuigi Rizzo  * Return ENXIO if the interface does not exist, EINVAL if netmap
58368b8534bSLuigi Rizzo  * is not supported by the interface.
58468b8534bSLuigi Rizzo  * If successful, hold a reference.
58568b8534bSLuigi Rizzo  */
58668b8534bSLuigi Rizzo static int
58768b8534bSLuigi Rizzo get_ifp(const char *name, struct ifnet **ifp)
58868b8534bSLuigi Rizzo {
589f196ce38SLuigi Rizzo #ifdef NM_BRIDGE
590f196ce38SLuigi Rizzo 	struct ifnet *iter = NULL;
591f196ce38SLuigi Rizzo 
592f196ce38SLuigi Rizzo 	do {
593f196ce38SLuigi Rizzo 		struct nm_bridge *b;
594f196ce38SLuigi Rizzo 		int i, l, cand = -1;
595f196ce38SLuigi Rizzo 
596f196ce38SLuigi Rizzo 		if (strncmp(name, NM_NAME, sizeof(NM_NAME) - 1))
597f196ce38SLuigi Rizzo 			break;
598f196ce38SLuigi Rizzo 		b = nm_find_bridge(name);
599f196ce38SLuigi Rizzo 		if (b == NULL) {
600f196ce38SLuigi Rizzo 			D("no bridges available for '%s'", name);
601f196ce38SLuigi Rizzo 			return (ENXIO);
602f196ce38SLuigi Rizzo 		}
603f196ce38SLuigi Rizzo 		/* XXX locking */
604f196ce38SLuigi Rizzo 		BDG_LOCK(b);
605f196ce38SLuigi Rizzo 		/* lookup in the local list of ports */
606f196ce38SLuigi Rizzo 		for (i = 0; i < NM_BDG_MAXPORTS; i++) {
607f196ce38SLuigi Rizzo 			iter = b->bdg_ports[i];
608f196ce38SLuigi Rizzo 			if (iter == NULL) {
609f196ce38SLuigi Rizzo 				if (cand == -1)
610f196ce38SLuigi Rizzo 					cand = i; /* potential insert point */
611f196ce38SLuigi Rizzo 				continue;
612f196ce38SLuigi Rizzo 			}
613f196ce38SLuigi Rizzo 			if (!strcmp(iter->if_xname, name)) {
614f196ce38SLuigi Rizzo 				ADD_BDG_REF(iter);
615f196ce38SLuigi Rizzo 				ND("found existing interface");
616f196ce38SLuigi Rizzo 				BDG_UNLOCK(b);
617f196ce38SLuigi Rizzo 				break;
618f196ce38SLuigi Rizzo 			}
619f196ce38SLuigi Rizzo 		}
620f196ce38SLuigi Rizzo 		if (i < NM_BDG_MAXPORTS) /* already unlocked */
621f196ce38SLuigi Rizzo 			break;
622f196ce38SLuigi Rizzo 		if (cand == -1) {
623f196ce38SLuigi Rizzo 			D("bridge full, cannot create new port");
624f196ce38SLuigi Rizzo no_port:
625f196ce38SLuigi Rizzo 			BDG_UNLOCK(b);
626f196ce38SLuigi Rizzo 			*ifp = NULL;
627f196ce38SLuigi Rizzo 			return EINVAL;
628f196ce38SLuigi Rizzo 		}
629f196ce38SLuigi Rizzo 		ND("create new bridge port %s", name);
630f196ce38SLuigi Rizzo 		/* space for forwarding list after the ifnet */
631f196ce38SLuigi Rizzo 		l = sizeof(*iter) +
632f196ce38SLuigi Rizzo 			 sizeof(struct nm_bdg_fwd)*NM_BDG_BATCH ;
633f196ce38SLuigi Rizzo 		iter = malloc(l, M_DEVBUF, M_NOWAIT | M_ZERO);
634f196ce38SLuigi Rizzo 		if (!iter)
635f196ce38SLuigi Rizzo 			goto no_port;
636f196ce38SLuigi Rizzo 		strcpy(iter->if_xname, name);
637f196ce38SLuigi Rizzo 		bdg_netmap_attach(iter);
638f196ce38SLuigi Rizzo 		b->bdg_ports[cand] = iter;
639f196ce38SLuigi Rizzo 		iter->if_bridge = b;
640f196ce38SLuigi Rizzo 		ADD_BDG_REF(iter);
641f196ce38SLuigi Rizzo 		BDG_UNLOCK(b);
642f196ce38SLuigi Rizzo 		ND("attaching virtual bridge %p", b);
643f196ce38SLuigi Rizzo 	} while (0);
644f196ce38SLuigi Rizzo 	*ifp = iter;
645f196ce38SLuigi Rizzo 	if (! *ifp)
646f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */
64768b8534bSLuigi Rizzo 	*ifp = ifunit_ref(name);
64868b8534bSLuigi Rizzo 	if (*ifp == NULL)
64968b8534bSLuigi Rizzo 		return (ENXIO);
65068b8534bSLuigi Rizzo 	/* can do this if the capability exists and if_pspare[0]
65168b8534bSLuigi Rizzo 	 * points to the netmap descriptor.
65268b8534bSLuigi Rizzo 	 */
65368b8534bSLuigi Rizzo 	if ((*ifp)->if_capabilities & IFCAP_NETMAP && NA(*ifp))
65468b8534bSLuigi Rizzo 		return 0;	/* valid pointer, we hold the refcount */
655f196ce38SLuigi Rizzo 	nm_if_rele(*ifp);
65668b8534bSLuigi Rizzo 	return EINVAL;	// not NETMAP capable
65768b8534bSLuigi Rizzo }
65868b8534bSLuigi Rizzo 
65968b8534bSLuigi Rizzo 
66068b8534bSLuigi Rizzo /*
66168b8534bSLuigi Rizzo  * Error routine called when txsync/rxsync detects an error.
66268b8534bSLuigi Rizzo  * Can't do much more than resetting cur = hwcur, avail = hwavail.
66368b8534bSLuigi Rizzo  * Return 1 on reinit.
664506cc70cSLuigi Rizzo  *
665506cc70cSLuigi Rizzo  * This routine is only called by the upper half of the kernel.
666506cc70cSLuigi Rizzo  * It only reads hwcur (which is changed only by the upper half, too)
667506cc70cSLuigi Rizzo  * and hwavail (which may be changed by the lower half, but only on
668506cc70cSLuigi Rizzo  * a tx ring and only to increase it, so any error will be recovered
669506cc70cSLuigi Rizzo  * on the next call). For the above, we don't strictly need to call
670506cc70cSLuigi Rizzo  * it under lock.
67168b8534bSLuigi Rizzo  */
67268b8534bSLuigi Rizzo int
67368b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring)
67468b8534bSLuigi Rizzo {
67568b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
67668b8534bSLuigi Rizzo 	u_int i, lim = kring->nkr_num_slots - 1;
67768b8534bSLuigi Rizzo 	int errors = 0;
67868b8534bSLuigi Rizzo 
67968b8534bSLuigi Rizzo 	D("called for %s", kring->na->ifp->if_xname);
68068b8534bSLuigi Rizzo 	if (ring->cur > lim)
68168b8534bSLuigi Rizzo 		errors++;
68268b8534bSLuigi Rizzo 	for (i = 0; i <= lim; i++) {
68368b8534bSLuigi Rizzo 		u_int idx = ring->slot[i].buf_idx;
68468b8534bSLuigi Rizzo 		u_int len = ring->slot[i].len;
68568b8534bSLuigi Rizzo 		if (idx < 2 || idx >= netmap_total_buffers) {
68668b8534bSLuigi Rizzo 			if (!errors++)
68768b8534bSLuigi Rizzo 				D("bad buffer at slot %d idx %d len %d ", i, idx, len);
68868b8534bSLuigi Rizzo 			ring->slot[i].buf_idx = 0;
68968b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
69068b8534bSLuigi Rizzo 		} else if (len > NETMAP_BUF_SIZE) {
69168b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
69268b8534bSLuigi Rizzo 			if (!errors++)
69368b8534bSLuigi Rizzo 				D("bad len %d at slot %d idx %d",
69468b8534bSLuigi Rizzo 					len, i, idx);
69568b8534bSLuigi Rizzo 		}
69668b8534bSLuigi Rizzo 	}
69768b8534bSLuigi Rizzo 	if (errors) {
69868b8534bSLuigi Rizzo 		int pos = kring - kring->na->tx_rings;
699d76bf4ffSLuigi Rizzo 		int n = kring->na->num_tx_rings + 1;
70068b8534bSLuigi Rizzo 
70168b8534bSLuigi Rizzo 		D("total %d errors", errors);
70268b8534bSLuigi Rizzo 		errors++;
70368b8534bSLuigi Rizzo 		D("%s %s[%d] reinit, cur %d -> %d avail %d -> %d",
70468b8534bSLuigi Rizzo 			kring->na->ifp->if_xname,
70568b8534bSLuigi Rizzo 			pos < n ?  "TX" : "RX", pos < n ? pos : pos - n,
70668b8534bSLuigi Rizzo 			ring->cur, kring->nr_hwcur,
70768b8534bSLuigi Rizzo 			ring->avail, kring->nr_hwavail);
70868b8534bSLuigi Rizzo 		ring->cur = kring->nr_hwcur;
70968b8534bSLuigi Rizzo 		ring->avail = kring->nr_hwavail;
71068b8534bSLuigi Rizzo 	}
71168b8534bSLuigi Rizzo 	return (errors ? 1 : 0);
71268b8534bSLuigi Rizzo }
71368b8534bSLuigi Rizzo 
71468b8534bSLuigi Rizzo 
71568b8534bSLuigi Rizzo /*
71668b8534bSLuigi Rizzo  * Set the ring ID. For devices with a single queue, a request
71768b8534bSLuigi Rizzo  * for all rings is the same as a single ring.
71868b8534bSLuigi Rizzo  */
71968b8534bSLuigi Rizzo static int
72068b8534bSLuigi Rizzo netmap_set_ringid(struct netmap_priv_d *priv, u_int ringid)
72168b8534bSLuigi Rizzo {
72268b8534bSLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
72368b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
72468b8534bSLuigi Rizzo 	u_int i = ringid & NETMAP_RING_MASK;
72564ae02c3SLuigi Rizzo 	/* initially (np_qfirst == np_qlast) we don't want to lock */
72668b8534bSLuigi Rizzo 	int need_lock = (priv->np_qfirst != priv->np_qlast);
727d76bf4ffSLuigi Rizzo 	int lim = na->num_rx_rings;
72868b8534bSLuigi Rizzo 
729d76bf4ffSLuigi Rizzo 	if (na->num_tx_rings > lim)
730d76bf4ffSLuigi Rizzo 		lim = na->num_tx_rings;
73164ae02c3SLuigi Rizzo 	if ( (ringid & NETMAP_HW_RING) && i >= lim) {
73268b8534bSLuigi Rizzo 		D("invalid ring id %d", i);
73368b8534bSLuigi Rizzo 		return (EINVAL);
73468b8534bSLuigi Rizzo 	}
73568b8534bSLuigi Rizzo 	if (need_lock)
7361a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
73768b8534bSLuigi Rizzo 	priv->np_ringid = ringid;
73868b8534bSLuigi Rizzo 	if (ringid & NETMAP_SW_RING) {
73964ae02c3SLuigi Rizzo 		priv->np_qfirst = NETMAP_SW_RING;
74064ae02c3SLuigi Rizzo 		priv->np_qlast = 0;
74168b8534bSLuigi Rizzo 	} else if (ringid & NETMAP_HW_RING) {
74268b8534bSLuigi Rizzo 		priv->np_qfirst = i;
74368b8534bSLuigi Rizzo 		priv->np_qlast = i + 1;
74468b8534bSLuigi Rizzo 	} else {
74568b8534bSLuigi Rizzo 		priv->np_qfirst = 0;
74664ae02c3SLuigi Rizzo 		priv->np_qlast = NETMAP_HW_RING ;
74768b8534bSLuigi Rizzo 	}
74868b8534bSLuigi Rizzo 	priv->np_txpoll = (ringid & NETMAP_NO_TX_POLL) ? 0 : 1;
74968b8534bSLuigi Rizzo 	if (need_lock)
7501a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
75168b8534bSLuigi Rizzo 	if (ringid & NETMAP_SW_RING)
75268b8534bSLuigi Rizzo 		D("ringid %s set to SW RING", ifp->if_xname);
75368b8534bSLuigi Rizzo 	else if (ringid & NETMAP_HW_RING)
75468b8534bSLuigi Rizzo 		D("ringid %s set to HW RING %d", ifp->if_xname,
75568b8534bSLuigi Rizzo 			priv->np_qfirst);
75668b8534bSLuigi Rizzo 	else
75764ae02c3SLuigi Rizzo 		D("ringid %s set to all %d HW RINGS", ifp->if_xname, lim);
75868b8534bSLuigi Rizzo 	return 0;
75968b8534bSLuigi Rizzo }
76068b8534bSLuigi Rizzo 
76168b8534bSLuigi Rizzo /*
76268b8534bSLuigi Rizzo  * ioctl(2) support for the "netmap" device.
76368b8534bSLuigi Rizzo  *
76468b8534bSLuigi Rizzo  * Following a list of accepted commands:
76568b8534bSLuigi Rizzo  * - NIOCGINFO
76668b8534bSLuigi Rizzo  * - SIOCGIFADDR	just for convenience
76768b8534bSLuigi Rizzo  * - NIOCREGIF
76868b8534bSLuigi Rizzo  * - NIOCUNREGIF
76968b8534bSLuigi Rizzo  * - NIOCTXSYNC
77068b8534bSLuigi Rizzo  * - NIOCRXSYNC
77168b8534bSLuigi Rizzo  *
77268b8534bSLuigi Rizzo  * Return 0 on success, errno otherwise.
77368b8534bSLuigi Rizzo  */
77468b8534bSLuigi Rizzo static int
7750b8ed8e0SLuigi Rizzo netmap_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
7760b8ed8e0SLuigi Rizzo 	int fflag, struct thread *td)
77768b8534bSLuigi Rizzo {
77868b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
77968b8534bSLuigi Rizzo 	struct ifnet *ifp;
78068b8534bSLuigi Rizzo 	struct nmreq *nmr = (struct nmreq *) data;
78168b8534bSLuigi Rizzo 	struct netmap_adapter *na;
78268b8534bSLuigi Rizzo 	int error;
78364ae02c3SLuigi Rizzo 	u_int i, lim;
78468b8534bSLuigi Rizzo 	struct netmap_if *nifp;
78568b8534bSLuigi Rizzo 
7860b8ed8e0SLuigi Rizzo 	(void)dev;	/* UNUSED */
7870b8ed8e0SLuigi Rizzo 	(void)fflag;	/* UNUSED */
788f196ce38SLuigi Rizzo #ifdef linux
789f196ce38SLuigi Rizzo #define devfs_get_cdevpriv(pp)				\
790f196ce38SLuigi Rizzo 	({ *(struct netmap_priv_d **)pp = ((struct file *)td)->private_data; 	\
791f196ce38SLuigi Rizzo 		(*pp ? 0 : ENOENT); })
792f196ce38SLuigi Rizzo 
793f196ce38SLuigi Rizzo /* devfs_set_cdevpriv cannot fail on linux */
794f196ce38SLuigi Rizzo #define devfs_set_cdevpriv(p, fn)				\
795f196ce38SLuigi Rizzo 	({ ((struct file *)td)->private_data = p; (p ? 0 : EINVAL); })
796f196ce38SLuigi Rizzo 
797f196ce38SLuigi Rizzo 
798f196ce38SLuigi Rizzo #define devfs_clear_cdevpriv()	do {				\
799f196ce38SLuigi Rizzo 		netmap_dtor(priv); ((struct file *)td)->private_data = 0;	\
800f196ce38SLuigi Rizzo 	} while (0)
801f196ce38SLuigi Rizzo #endif /* linux */
802f196ce38SLuigi Rizzo 
803506cc70cSLuigi Rizzo 	CURVNET_SET(TD_TO_VNET(td));
804506cc70cSLuigi Rizzo 
80568b8534bSLuigi Rizzo 	error = devfs_get_cdevpriv((void **)&priv);
806506cc70cSLuigi Rizzo 	if (error != ENOENT && error != 0) {
807506cc70cSLuigi Rizzo 		CURVNET_RESTORE();
80868b8534bSLuigi Rizzo 		return (error);
809506cc70cSLuigi Rizzo 	}
81068b8534bSLuigi Rizzo 
81168b8534bSLuigi Rizzo 	error = 0;	/* Could be ENOENT */
812f196ce38SLuigi Rizzo 	nmr->nr_name[sizeof(nmr->nr_name) - 1] = '\0';	/* truncate name */
81368b8534bSLuigi Rizzo 	switch (cmd) {
81468b8534bSLuigi Rizzo 	case NIOCGINFO:		/* return capabilities etc */
81568b8534bSLuigi Rizzo 		/* memsize is always valid */
81664ae02c3SLuigi Rizzo 		nmr->nr_memsize = nm_mem->nm_totalsize;
81768b8534bSLuigi Rizzo 		nmr->nr_offset = 0;
81864ae02c3SLuigi Rizzo 		nmr->nr_rx_rings = nmr->nr_tx_rings = 0;
81964ae02c3SLuigi Rizzo 		nmr->nr_rx_slots = nmr->nr_tx_slots = 0;
82064ae02c3SLuigi Rizzo 		if (nmr->nr_version != NETMAP_API) {
82164ae02c3SLuigi Rizzo 			D("API mismatch got %d have %d",
82264ae02c3SLuigi Rizzo 				nmr->nr_version, NETMAP_API);
82364ae02c3SLuigi Rizzo 			nmr->nr_version = NETMAP_API;
82464ae02c3SLuigi Rizzo 			error = EINVAL;
82564ae02c3SLuigi Rizzo 			break;
82664ae02c3SLuigi Rizzo 		}
82768b8534bSLuigi Rizzo 		if (nmr->nr_name[0] == '\0')	/* just get memory info */
82868b8534bSLuigi Rizzo 			break;
82968b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* get a refcount */
83068b8534bSLuigi Rizzo 		if (error)
83168b8534bSLuigi Rizzo 			break;
83268b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap_adapter */
833d76bf4ffSLuigi Rizzo 		nmr->nr_rx_rings = na->num_rx_rings;
834d76bf4ffSLuigi Rizzo 		nmr->nr_tx_rings = na->num_tx_rings;
83564ae02c3SLuigi Rizzo 		nmr->nr_rx_slots = na->num_rx_desc;
83664ae02c3SLuigi Rizzo 		nmr->nr_tx_slots = na->num_tx_desc;
837f196ce38SLuigi Rizzo 		nm_if_rele(ifp);	/* return the refcount */
83868b8534bSLuigi Rizzo 		break;
83968b8534bSLuigi Rizzo 
84068b8534bSLuigi Rizzo 	case NIOCREGIF:
84164ae02c3SLuigi Rizzo 		if (nmr->nr_version != NETMAP_API) {
84264ae02c3SLuigi Rizzo 			nmr->nr_version = NETMAP_API;
84364ae02c3SLuigi Rizzo 			error = EINVAL;
84464ae02c3SLuigi Rizzo 			break;
84564ae02c3SLuigi Rizzo 		}
846506cc70cSLuigi Rizzo 		if (priv != NULL) {	/* thread already registered */
847506cc70cSLuigi Rizzo 			error = netmap_set_ringid(priv, nmr->nr_ringid);
848506cc70cSLuigi Rizzo 			break;
849506cc70cSLuigi Rizzo 		}
85068b8534bSLuigi Rizzo 		/* find the interface and a reference */
85168b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* keep reference */
85268b8534bSLuigi Rizzo 		if (error)
85368b8534bSLuigi Rizzo 			break;
85468b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap adapter */
85568b8534bSLuigi Rizzo 		/*
85668b8534bSLuigi Rizzo 		 * Allocate the private per-thread structure.
85768b8534bSLuigi Rizzo 		 * XXX perhaps we can use a blocking malloc ?
85868b8534bSLuigi Rizzo 		 */
85968b8534bSLuigi Rizzo 		priv = malloc(sizeof(struct netmap_priv_d), M_DEVBUF,
86068b8534bSLuigi Rizzo 			      M_NOWAIT | M_ZERO);
86168b8534bSLuigi Rizzo 		if (priv == NULL) {
86268b8534bSLuigi Rizzo 			error = ENOMEM;
863f196ce38SLuigi Rizzo 			nm_if_rele(ifp);   /* return the refcount */
86468b8534bSLuigi Rizzo 			break;
86568b8534bSLuigi Rizzo 		}
86668b8534bSLuigi Rizzo 
86768b8534bSLuigi Rizzo 		for (i = 10; i > 0; i--) {
8681a26580eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
86968b8534bSLuigi Rizzo 			if (!NETMAP_DELETING(na))
87068b8534bSLuigi Rizzo 				break;
8711a26580eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
87268b8534bSLuigi Rizzo 			tsleep(na, 0, "NIOCREGIF", hz/10);
87368b8534bSLuigi Rizzo 		}
87468b8534bSLuigi Rizzo 		if (i == 0) {
87568b8534bSLuigi Rizzo 			D("too many NIOCREGIF attempts, give up");
87668b8534bSLuigi Rizzo 			error = EINVAL;
87768b8534bSLuigi Rizzo 			free(priv, M_DEVBUF);
878f196ce38SLuigi Rizzo 			nm_if_rele(ifp);	/* return the refcount */
87968b8534bSLuigi Rizzo 			break;
88068b8534bSLuigi Rizzo 		}
88168b8534bSLuigi Rizzo 
88268b8534bSLuigi Rizzo 		priv->np_ifp = ifp;	/* store the reference */
88368b8534bSLuigi Rizzo 		error = netmap_set_ringid(priv, nmr->nr_ringid);
88468b8534bSLuigi Rizzo 		if (error)
88568b8534bSLuigi Rizzo 			goto error;
88668b8534bSLuigi Rizzo 		priv->np_nifp = nifp = netmap_if_new(nmr->nr_name, na);
88768b8534bSLuigi Rizzo 		if (nifp == NULL) { /* allocation failed */
88868b8534bSLuigi Rizzo 			error = ENOMEM;
88968b8534bSLuigi Rizzo 		} else if (ifp->if_capenable & IFCAP_NETMAP) {
89068b8534bSLuigi Rizzo 			/* was already set */
89168b8534bSLuigi Rizzo 		} else {
89268b8534bSLuigi Rizzo 			/* Otherwise set the card in netmap mode
89368b8534bSLuigi Rizzo 			 * and make it use the shared buffers.
89468b8534bSLuigi Rizzo 			 */
895f196ce38SLuigi Rizzo 			for (i = 0 ; i < na->num_tx_rings + 1; i++)
896f196ce38SLuigi Rizzo 				mtx_init(&na->tx_rings[i].q_lock, "nm_txq_lock", MTX_NETWORK_LOCK, MTX_DEF);
897f196ce38SLuigi Rizzo 			for (i = 0 ; i < na->num_rx_rings + 1; i++) {
898f196ce38SLuigi Rizzo 				mtx_init(&na->rx_rings[i].q_lock, "nm_rxq_lock", MTX_NETWORK_LOCK, MTX_DEF);
899f196ce38SLuigi Rizzo 			}
90068b8534bSLuigi Rizzo 			error = na->nm_register(ifp, 1); /* mode on */
9015819da83SLuigi Rizzo 			if (error)
9025819da83SLuigi Rizzo 				netmap_dtor_locked(priv);
90368b8534bSLuigi Rizzo 		}
90468b8534bSLuigi Rizzo 
90568b8534bSLuigi Rizzo 		if (error) {	/* reg. failed, release priv and ref */
90668b8534bSLuigi Rizzo error:
9071a26580eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
908f196ce38SLuigi Rizzo 			nm_if_rele(ifp);	/* return the refcount */
9095819da83SLuigi Rizzo 			bzero(priv, sizeof(*priv));
9105819da83SLuigi Rizzo 			free(priv, M_DEVBUF);
91168b8534bSLuigi Rizzo 			break;
91268b8534bSLuigi Rizzo 		}
91368b8534bSLuigi Rizzo 
9141a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
91568b8534bSLuigi Rizzo 		error = devfs_set_cdevpriv(priv, netmap_dtor);
91668b8534bSLuigi Rizzo 
91768b8534bSLuigi Rizzo 		if (error != 0) {
91868b8534bSLuigi Rizzo 			/* could not assign the private storage for the
91968b8534bSLuigi Rizzo 			 * thread, call the destructor explicitly.
92068b8534bSLuigi Rizzo 			 */
92168b8534bSLuigi Rizzo 			netmap_dtor(priv);
92268b8534bSLuigi Rizzo 			break;
92368b8534bSLuigi Rizzo 		}
92468b8534bSLuigi Rizzo 
92568b8534bSLuigi Rizzo 		/* return the offset of the netmap_if object */
926d76bf4ffSLuigi Rizzo 		nmr->nr_rx_rings = na->num_rx_rings;
927d76bf4ffSLuigi Rizzo 		nmr->nr_tx_rings = na->num_tx_rings;
92864ae02c3SLuigi Rizzo 		nmr->nr_rx_slots = na->num_rx_desc;
92964ae02c3SLuigi Rizzo 		nmr->nr_tx_slots = na->num_tx_desc;
93064ae02c3SLuigi Rizzo 		nmr->nr_memsize = nm_mem->nm_totalsize;
931446ee301SLuigi Rizzo 		nmr->nr_offset = netmap_if_offset(nifp);
93268b8534bSLuigi Rizzo 		break;
93368b8534bSLuigi Rizzo 
93468b8534bSLuigi Rizzo 	case NIOCUNREGIF:
935506cc70cSLuigi Rizzo 		if (priv == NULL) {
936506cc70cSLuigi Rizzo 			error = ENXIO;
937506cc70cSLuigi Rizzo 			break;
938506cc70cSLuigi Rizzo 		}
93968b8534bSLuigi Rizzo 
94068b8534bSLuigi Rizzo 		/* the interface is unregistered inside the
94168b8534bSLuigi Rizzo 		   destructor of the private data. */
94268b8534bSLuigi Rizzo 		devfs_clear_cdevpriv();
94368b8534bSLuigi Rizzo 		break;
94468b8534bSLuigi Rizzo 
94568b8534bSLuigi Rizzo 	case NIOCTXSYNC:
94668b8534bSLuigi Rizzo         case NIOCRXSYNC:
947506cc70cSLuigi Rizzo 		if (priv == NULL) {
948506cc70cSLuigi Rizzo 			error = ENXIO;
949506cc70cSLuigi Rizzo 			break;
950506cc70cSLuigi Rizzo 		}
95168b8534bSLuigi Rizzo 		ifp = priv->np_ifp;	/* we have a reference */
95268b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap adapter */
95364ae02c3SLuigi Rizzo 		if (priv->np_qfirst == NETMAP_SW_RING) { /* host rings */
95468b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC)
95568b8534bSLuigi Rizzo 				netmap_sync_to_host(na);
95668b8534bSLuigi Rizzo 			else
95701c7d25fSLuigi Rizzo 				netmap_sync_from_host(na, NULL, NULL);
958506cc70cSLuigi Rizzo 			break;
95968b8534bSLuigi Rizzo 		}
96064ae02c3SLuigi Rizzo 		/* find the last ring to scan */
96164ae02c3SLuigi Rizzo 		lim = priv->np_qlast;
96264ae02c3SLuigi Rizzo 		if (lim == NETMAP_HW_RING)
9633c0caf6cSLuigi Rizzo 			lim = (cmd == NIOCTXSYNC) ?
964d76bf4ffSLuigi Rizzo 			    na->num_tx_rings : na->num_rx_rings;
96568b8534bSLuigi Rizzo 
96664ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim; i++) {
96768b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC) {
96868b8534bSLuigi Rizzo 				struct netmap_kring *kring = &na->tx_rings[i];
96968b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
9703c0caf6cSLuigi Rizzo 					D("pre txsync ring %d cur %d hwcur %d",
97168b8534bSLuigi Rizzo 					    i, kring->ring->cur,
97268b8534bSLuigi Rizzo 					    kring->nr_hwcur);
9731a26580eSLuigi Rizzo 				na->nm_txsync(ifp, i, 1 /* do lock */);
97468b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
9753c0caf6cSLuigi Rizzo 					D("post txsync ring %d cur %d hwcur %d",
97668b8534bSLuigi Rizzo 					    i, kring->ring->cur,
97768b8534bSLuigi Rizzo 					    kring->nr_hwcur);
97868b8534bSLuigi Rizzo 			} else {
9791a26580eSLuigi Rizzo 				na->nm_rxsync(ifp, i, 1 /* do lock */);
98068b8534bSLuigi Rizzo 				microtime(&na->rx_rings[i].ring->ts);
98168b8534bSLuigi Rizzo 			}
98268b8534bSLuigi Rizzo 		}
98368b8534bSLuigi Rizzo 
98468b8534bSLuigi Rizzo 		break;
98568b8534bSLuigi Rizzo 
986f196ce38SLuigi Rizzo #ifdef __FreeBSD__
98768b8534bSLuigi Rizzo 	case BIOCIMMEDIATE:
98868b8534bSLuigi Rizzo 	case BIOCGHDRCMPLT:
98968b8534bSLuigi Rizzo 	case BIOCSHDRCMPLT:
99068b8534bSLuigi Rizzo 	case BIOCSSEESENT:
99168b8534bSLuigi Rizzo 		D("ignore BIOCIMMEDIATE/BIOCSHDRCMPLT/BIOCSHDRCMPLT/BIOCSSEESENT");
99268b8534bSLuigi Rizzo 		break;
99368b8534bSLuigi Rizzo 
994babc7c12SLuigi Rizzo 	default:	/* allow device-specific ioctls */
99568b8534bSLuigi Rizzo 	    {
99668b8534bSLuigi Rizzo 		struct socket so;
99768b8534bSLuigi Rizzo 		bzero(&so, sizeof(so));
99868b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* keep reference */
99968b8534bSLuigi Rizzo 		if (error)
100068b8534bSLuigi Rizzo 			break;
100168b8534bSLuigi Rizzo 		so.so_vnet = ifp->if_vnet;
100268b8534bSLuigi Rizzo 		// so->so_proto not null.
100368b8534bSLuigi Rizzo 		error = ifioctl(&so, cmd, data, td);
1004f196ce38SLuigi Rizzo 		nm_if_rele(ifp);
1005babc7c12SLuigi Rizzo 		break;
100668b8534bSLuigi Rizzo 	    }
1007f196ce38SLuigi Rizzo 
1008f196ce38SLuigi Rizzo #else /* linux */
1009f196ce38SLuigi Rizzo 	default:
1010f196ce38SLuigi Rizzo 		error = EOPNOTSUPP;
1011f196ce38SLuigi Rizzo #endif /* linux */
101268b8534bSLuigi Rizzo 	}
101368b8534bSLuigi Rizzo 
1014506cc70cSLuigi Rizzo 	CURVNET_RESTORE();
101568b8534bSLuigi Rizzo 	return (error);
101668b8534bSLuigi Rizzo }
101768b8534bSLuigi Rizzo 
101868b8534bSLuigi Rizzo 
101968b8534bSLuigi Rizzo /*
102068b8534bSLuigi Rizzo  * select(2) and poll(2) handlers for the "netmap" device.
102168b8534bSLuigi Rizzo  *
102268b8534bSLuigi Rizzo  * Can be called for one or more queues.
102368b8534bSLuigi Rizzo  * Return true the event mask corresponding to ready events.
102468b8534bSLuigi Rizzo  * If there are no ready events, do a selrecord on either individual
102568b8534bSLuigi Rizzo  * selfd or on the global one.
102668b8534bSLuigi Rizzo  * Device-dependent parts (locking and sync of tx/rx rings)
102768b8534bSLuigi Rizzo  * are done through callbacks.
1028f196ce38SLuigi Rizzo  *
102901c7d25fSLuigi Rizzo  * On linux, arguments are really pwait, the poll table, and 'td' is struct file *
103001c7d25fSLuigi Rizzo  * The first one is remapped to pwait as selrecord() uses the name as an
103101c7d25fSLuigi Rizzo  * hidden argument.
103268b8534bSLuigi Rizzo  */
103368b8534bSLuigi Rizzo static int
103401c7d25fSLuigi Rizzo netmap_poll(struct cdev *dev, int events, struct thread *td)
103568b8534bSLuigi Rizzo {
103668b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
103768b8534bSLuigi Rizzo 	struct netmap_adapter *na;
103868b8534bSLuigi Rizzo 	struct ifnet *ifp;
103968b8534bSLuigi Rizzo 	struct netmap_kring *kring;
1040d0c7b075SLuigi Rizzo 	u_int core_lock, i, check_all, want_tx, want_rx, revents = 0;
104164ae02c3SLuigi Rizzo 	u_int lim_tx, lim_rx;
1042bcda432eSLuigi Rizzo 	enum {NO_CL, NEED_CL, LOCKED_CL }; /* see below */
104301c7d25fSLuigi Rizzo 	void *pwait = dev;	/* linux compatibility */
104401c7d25fSLuigi Rizzo 
104501c7d25fSLuigi Rizzo 	(void)pwait;
104668b8534bSLuigi Rizzo 
104768b8534bSLuigi Rizzo 	if (devfs_get_cdevpriv((void **)&priv) != 0 || priv == NULL)
104868b8534bSLuigi Rizzo 		return POLLERR;
104968b8534bSLuigi Rizzo 
105068b8534bSLuigi Rizzo 	ifp = priv->np_ifp;
105168b8534bSLuigi Rizzo 	// XXX check for deleting() ?
105268b8534bSLuigi Rizzo 	if ( (ifp->if_capenable & IFCAP_NETMAP) == 0)
105368b8534bSLuigi Rizzo 		return POLLERR;
105468b8534bSLuigi Rizzo 
105568b8534bSLuigi Rizzo 	if (netmap_verbose & 0x8000)
105668b8534bSLuigi Rizzo 		D("device %s events 0x%x", ifp->if_xname, events);
105768b8534bSLuigi Rizzo 	want_tx = events & (POLLOUT | POLLWRNORM);
105868b8534bSLuigi Rizzo 	want_rx = events & (POLLIN | POLLRDNORM);
105968b8534bSLuigi Rizzo 
106068b8534bSLuigi Rizzo 	na = NA(ifp); /* retrieve netmap adapter */
106168b8534bSLuigi Rizzo 
1062d76bf4ffSLuigi Rizzo 	lim_tx = na->num_tx_rings;
1063d76bf4ffSLuigi Rizzo 	lim_rx = na->num_rx_rings;
106468b8534bSLuigi Rizzo 	/* how many queues we are scanning */
106564ae02c3SLuigi Rizzo 	if (priv->np_qfirst == NETMAP_SW_RING) {
106668b8534bSLuigi Rizzo 		if (priv->np_txpoll || want_tx) {
106768b8534bSLuigi Rizzo 			/* push any packets up, then we are always ready */
106864ae02c3SLuigi Rizzo 			kring = &na->tx_rings[lim_tx];
106968b8534bSLuigi Rizzo 			netmap_sync_to_host(na);
107068b8534bSLuigi Rizzo 			revents |= want_tx;
107168b8534bSLuigi Rizzo 		}
107268b8534bSLuigi Rizzo 		if (want_rx) {
107364ae02c3SLuigi Rizzo 			kring = &na->rx_rings[lim_rx];
107468b8534bSLuigi Rizzo 			if (kring->ring->avail == 0)
107501c7d25fSLuigi Rizzo 				netmap_sync_from_host(na, td, dev);
107668b8534bSLuigi Rizzo 			if (kring->ring->avail > 0) {
107768b8534bSLuigi Rizzo 				revents |= want_rx;
107868b8534bSLuigi Rizzo 			}
107968b8534bSLuigi Rizzo 		}
108068b8534bSLuigi Rizzo 		return (revents);
108168b8534bSLuigi Rizzo 	}
108268b8534bSLuigi Rizzo 
108368b8534bSLuigi Rizzo 	/*
108468b8534bSLuigi Rizzo 	 * check_all is set if the card has more than one queue and
108568b8534bSLuigi Rizzo 	 * the client is polling all of them. If true, we sleep on
108668b8534bSLuigi Rizzo 	 * the "global" selfd, otherwise we sleep on individual selfd
108768b8534bSLuigi Rizzo 	 * (we can only sleep on one of them per direction).
108868b8534bSLuigi Rizzo 	 * The interrupt routine in the driver should always wake on
108968b8534bSLuigi Rizzo 	 * the individual selfd, and also on the global one if the card
109068b8534bSLuigi Rizzo 	 * has more than one ring.
109168b8534bSLuigi Rizzo 	 *
109268b8534bSLuigi Rizzo 	 * If the card has only one lock, we just use that.
109368b8534bSLuigi Rizzo 	 * If the card has separate ring locks, we just use those
109468b8534bSLuigi Rizzo 	 * unless we are doing check_all, in which case the whole
109568b8534bSLuigi Rizzo 	 * loop is wrapped by the global lock.
109668b8534bSLuigi Rizzo 	 * We acquire locks only when necessary: if poll is called
109768b8534bSLuigi Rizzo 	 * when buffers are available, we can just return without locks.
109868b8534bSLuigi Rizzo 	 *
109968b8534bSLuigi Rizzo 	 * rxsync() is only called if we run out of buffers on a POLLIN.
110068b8534bSLuigi Rizzo 	 * txsync() is called if we run out of buffers on POLLOUT, or
110168b8534bSLuigi Rizzo 	 * there are pending packets to send. The latter can be disabled
110268b8534bSLuigi Rizzo 	 * passing NETMAP_NO_TX_POLL in the NIOCREG call.
110368b8534bSLuigi Rizzo 	 */
110464ae02c3SLuigi Rizzo 	check_all = (priv->np_qlast == NETMAP_HW_RING) && (lim_tx > 1 || lim_rx > 1);
110568b8534bSLuigi Rizzo 
110668b8534bSLuigi Rizzo 	/*
110768b8534bSLuigi Rizzo 	 * core_lock indicates what to do with the core lock.
110868b8534bSLuigi Rizzo 	 * The core lock is used when either the card has no individual
110968b8534bSLuigi Rizzo 	 * locks, or it has individual locks but we are cheking all
111068b8534bSLuigi Rizzo 	 * rings so we need the core lock to avoid missing wakeup events.
111168b8534bSLuigi Rizzo 	 *
111268b8534bSLuigi Rizzo 	 * It has three possible states:
111368b8534bSLuigi Rizzo 	 * NO_CL	we don't need to use the core lock, e.g.
111468b8534bSLuigi Rizzo 	 *		because we are protected by individual locks.
111568b8534bSLuigi Rizzo 	 * NEED_CL	we need the core lock. In this case, when we
111668b8534bSLuigi Rizzo 	 *		call the lock routine, move to LOCKED_CL
111768b8534bSLuigi Rizzo 	 *		to remember to release the lock once done.
111868b8534bSLuigi Rizzo 	 * LOCKED_CL	core lock is set, so we need to release it.
111968b8534bSLuigi Rizzo 	 */
1120d0c7b075SLuigi Rizzo 	core_lock = (check_all || !na->separate_locks) ? NEED_CL : NO_CL;
1121f196ce38SLuigi Rizzo #ifdef NM_BRIDGE
1122f196ce38SLuigi Rizzo 	/* the bridge uses separate locks */
1123f196ce38SLuigi Rizzo 	if (na->nm_register == bdg_netmap_reg) {
1124f196ce38SLuigi Rizzo 		ND("not using core lock for %s", ifp->if_xname);
1125f196ce38SLuigi Rizzo 		core_lock = NO_CL;
1126f196ce38SLuigi Rizzo 	}
1127f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */
112864ae02c3SLuigi Rizzo 	if (priv->np_qlast != NETMAP_HW_RING) {
112964ae02c3SLuigi Rizzo 		lim_tx = lim_rx = priv->np_qlast;
113064ae02c3SLuigi Rizzo 	}
113164ae02c3SLuigi Rizzo 
113268b8534bSLuigi Rizzo 	/*
113368b8534bSLuigi Rizzo 	 * We start with a lock free round which is good if we have
113468b8534bSLuigi Rizzo 	 * data available. If this fails, then lock and call the sync
113568b8534bSLuigi Rizzo 	 * routines.
113668b8534bSLuigi Rizzo 	 */
113764ae02c3SLuigi Rizzo 	for (i = priv->np_qfirst; want_rx && i < lim_rx; i++) {
113868b8534bSLuigi Rizzo 		kring = &na->rx_rings[i];
113968b8534bSLuigi Rizzo 		if (kring->ring->avail > 0) {
114068b8534bSLuigi Rizzo 			revents |= want_rx;
114168b8534bSLuigi Rizzo 			want_rx = 0;	/* also breaks the loop */
114268b8534bSLuigi Rizzo 		}
114368b8534bSLuigi Rizzo 	}
114464ae02c3SLuigi Rizzo 	for (i = priv->np_qfirst; want_tx && i < lim_tx; i++) {
114568b8534bSLuigi Rizzo 		kring = &na->tx_rings[i];
114668b8534bSLuigi Rizzo 		if (kring->ring->avail > 0) {
114768b8534bSLuigi Rizzo 			revents |= want_tx;
114868b8534bSLuigi Rizzo 			want_tx = 0;	/* also breaks the loop */
114968b8534bSLuigi Rizzo 		}
115068b8534bSLuigi Rizzo 	}
115168b8534bSLuigi Rizzo 
115268b8534bSLuigi Rizzo 	/*
115368b8534bSLuigi Rizzo 	 * If we to push packets out (priv->np_txpoll) or want_tx is
115468b8534bSLuigi Rizzo 	 * still set, we do need to run the txsync calls (on all rings,
115568b8534bSLuigi Rizzo 	 * to avoid that the tx rings stall).
115668b8534bSLuigi Rizzo 	 */
115768b8534bSLuigi Rizzo 	if (priv->np_txpoll || want_tx) {
115864ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim_tx; i++) {
115968b8534bSLuigi Rizzo 			kring = &na->tx_rings[i];
11605819da83SLuigi Rizzo 			/*
11615819da83SLuigi Rizzo 			 * Skip the current ring if want_tx == 0
11625819da83SLuigi Rizzo 			 * (we have already done a successful sync on
11635819da83SLuigi Rizzo 			 * a previous ring) AND kring->cur == kring->hwcur
11645819da83SLuigi Rizzo 			 * (there are no pending transmissions for this ring).
11655819da83SLuigi Rizzo 			 */
116668b8534bSLuigi Rizzo 			if (!want_tx && kring->ring->cur == kring->nr_hwcur)
116768b8534bSLuigi Rizzo 				continue;
116868b8534bSLuigi Rizzo 			if (core_lock == NEED_CL) {
11691a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
117068b8534bSLuigi Rizzo 				core_lock = LOCKED_CL;
117168b8534bSLuigi Rizzo 			}
117268b8534bSLuigi Rizzo 			if (na->separate_locks)
11731a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_TX_LOCK, i);
117468b8534bSLuigi Rizzo 			if (netmap_verbose & NM_VERB_TXSYNC)
117568b8534bSLuigi Rizzo 				D("send %d on %s %d",
117668b8534bSLuigi Rizzo 					kring->ring->cur,
117768b8534bSLuigi Rizzo 					ifp->if_xname, i);
11781a26580eSLuigi Rizzo 			if (na->nm_txsync(ifp, i, 0 /* no lock */))
117968b8534bSLuigi Rizzo 				revents |= POLLERR;
118068b8534bSLuigi Rizzo 
11815819da83SLuigi Rizzo 			/* Check avail/call selrecord only if called with POLLOUT */
118268b8534bSLuigi Rizzo 			if (want_tx) {
118368b8534bSLuigi Rizzo 				if (kring->ring->avail > 0) {
118468b8534bSLuigi Rizzo 					/* stop at the first ring. We don't risk
118568b8534bSLuigi Rizzo 					 * starvation.
118668b8534bSLuigi Rizzo 					 */
118768b8534bSLuigi Rizzo 					revents |= want_tx;
118868b8534bSLuigi Rizzo 					want_tx = 0;
118968b8534bSLuigi Rizzo 				} else if (!check_all)
119068b8534bSLuigi Rizzo 					selrecord(td, &kring->si);
119168b8534bSLuigi Rizzo 			}
119268b8534bSLuigi Rizzo 			if (na->separate_locks)
11931a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_TX_UNLOCK, i);
119468b8534bSLuigi Rizzo 		}
119568b8534bSLuigi Rizzo 	}
119668b8534bSLuigi Rizzo 
119768b8534bSLuigi Rizzo 	/*
119868b8534bSLuigi Rizzo 	 * now if want_rx is still set we need to lock and rxsync.
119968b8534bSLuigi Rizzo 	 * Do it on all rings because otherwise we starve.
120068b8534bSLuigi Rizzo 	 */
120168b8534bSLuigi Rizzo 	if (want_rx) {
120264ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim_rx; i++) {
120368b8534bSLuigi Rizzo 			kring = &na->rx_rings[i];
120468b8534bSLuigi Rizzo 			if (core_lock == NEED_CL) {
12051a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
120668b8534bSLuigi Rizzo 				core_lock = LOCKED_CL;
120768b8534bSLuigi Rizzo 			}
120868b8534bSLuigi Rizzo 			if (na->separate_locks)
12091a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_RX_LOCK, i);
121068b8534bSLuigi Rizzo 
12111a26580eSLuigi Rizzo 			if (na->nm_rxsync(ifp, i, 0 /* no lock */))
121268b8534bSLuigi Rizzo 				revents |= POLLERR;
12135819da83SLuigi Rizzo 			if (netmap_no_timestamp == 0 ||
12145819da83SLuigi Rizzo 					kring->ring->flags & NR_TIMESTAMP) {
121568b8534bSLuigi Rizzo 				microtime(&kring->ring->ts);
12165819da83SLuigi Rizzo 			}
121768b8534bSLuigi Rizzo 
121868b8534bSLuigi Rizzo 			if (kring->ring->avail > 0)
121968b8534bSLuigi Rizzo 				revents |= want_rx;
122068b8534bSLuigi Rizzo 			else if (!check_all)
122168b8534bSLuigi Rizzo 				selrecord(td, &kring->si);
122268b8534bSLuigi Rizzo 			if (na->separate_locks)
12231a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_RX_UNLOCK, i);
122468b8534bSLuigi Rizzo 		}
122568b8534bSLuigi Rizzo 	}
122664ae02c3SLuigi Rizzo 	if (check_all && revents == 0) { /* signal on the global queue */
122768b8534bSLuigi Rizzo 		if (want_tx)
122864ae02c3SLuigi Rizzo 			selrecord(td, &na->tx_si);
122968b8534bSLuigi Rizzo 		if (want_rx)
123064ae02c3SLuigi Rizzo 			selrecord(td, &na->rx_si);
123168b8534bSLuigi Rizzo 	}
123268b8534bSLuigi Rizzo 	if (core_lock == LOCKED_CL)
12331a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
123468b8534bSLuigi Rizzo 
123568b8534bSLuigi Rizzo 	return (revents);
123668b8534bSLuigi Rizzo }
123768b8534bSLuigi Rizzo 
123868b8534bSLuigi Rizzo /*------- driver support routines ------*/
123968b8534bSLuigi Rizzo 
124068b8534bSLuigi Rizzo /*
1241babc7c12SLuigi Rizzo  * default lock wrapper.
12421a26580eSLuigi Rizzo  */
12431a26580eSLuigi Rizzo static void
1244babc7c12SLuigi Rizzo netmap_lock_wrapper(struct ifnet *dev, int what, u_int queueid)
12451a26580eSLuigi Rizzo {
1246babc7c12SLuigi Rizzo 	struct netmap_adapter *na = NA(dev);
12471a26580eSLuigi Rizzo 
12481a26580eSLuigi Rizzo 	switch (what) {
1249babc7c12SLuigi Rizzo #ifdef linux	/* some system do not need lock on register */
12501a26580eSLuigi Rizzo 	case NETMAP_REG_LOCK:
12511a26580eSLuigi Rizzo 	case NETMAP_REG_UNLOCK:
12521a26580eSLuigi Rizzo 		break;
1253babc7c12SLuigi Rizzo #endif /* linux */
12541a26580eSLuigi Rizzo 
12551a26580eSLuigi Rizzo 	case NETMAP_CORE_LOCK:
12561a26580eSLuigi Rizzo 		mtx_lock(&na->core_lock);
12571a26580eSLuigi Rizzo 		break;
12581a26580eSLuigi Rizzo 
12591a26580eSLuigi Rizzo 	case NETMAP_CORE_UNLOCK:
12601a26580eSLuigi Rizzo 		mtx_unlock(&na->core_lock);
12611a26580eSLuigi Rizzo 		break;
12621a26580eSLuigi Rizzo 
12631a26580eSLuigi Rizzo 	case NETMAP_TX_LOCK:
12641a26580eSLuigi Rizzo 		mtx_lock(&na->tx_rings[queueid].q_lock);
12651a26580eSLuigi Rizzo 		break;
12661a26580eSLuigi Rizzo 
12671a26580eSLuigi Rizzo 	case NETMAP_TX_UNLOCK:
12681a26580eSLuigi Rizzo 		mtx_unlock(&na->tx_rings[queueid].q_lock);
12691a26580eSLuigi Rizzo 		break;
12701a26580eSLuigi Rizzo 
12711a26580eSLuigi Rizzo 	case NETMAP_RX_LOCK:
12721a26580eSLuigi Rizzo 		mtx_lock(&na->rx_rings[queueid].q_lock);
12731a26580eSLuigi Rizzo 		break;
12741a26580eSLuigi Rizzo 
12751a26580eSLuigi Rizzo 	case NETMAP_RX_UNLOCK:
12761a26580eSLuigi Rizzo 		mtx_unlock(&na->rx_rings[queueid].q_lock);
12771a26580eSLuigi Rizzo 		break;
12781a26580eSLuigi Rizzo 	}
12791a26580eSLuigi Rizzo }
12801a26580eSLuigi Rizzo 
12811a26580eSLuigi Rizzo 
12821a26580eSLuigi Rizzo /*
128368b8534bSLuigi Rizzo  * Initialize a ``netmap_adapter`` object created by driver on attach.
128468b8534bSLuigi Rizzo  * We allocate a block of memory with room for a struct netmap_adapter
128568b8534bSLuigi Rizzo  * plus two sets of N+2 struct netmap_kring (where N is the number
128668b8534bSLuigi Rizzo  * of hardware rings):
128768b8534bSLuigi Rizzo  * krings	0..N-1	are for the hardware queues.
128868b8534bSLuigi Rizzo  * kring	N	is for the host stack queue
128968b8534bSLuigi Rizzo  * kring	N+1	is only used for the selinfo for all queues.
129068b8534bSLuigi Rizzo  * Return 0 on success, ENOMEM otherwise.
129164ae02c3SLuigi Rizzo  *
12920bf88954SEd Maste  * By default the receive and transmit adapter ring counts are both initialized
12930bf88954SEd Maste  * to num_queues.  na->num_tx_rings can be set for cards with different tx/rx
129424e57ec9SEd Maste  * setups.
129568b8534bSLuigi Rizzo  */
129668b8534bSLuigi Rizzo int
129768b8534bSLuigi Rizzo netmap_attach(struct netmap_adapter *na, int num_queues)
129868b8534bSLuigi Rizzo {
1299f196ce38SLuigi Rizzo 	int n, size;
130068b8534bSLuigi Rizzo 	void *buf;
130168b8534bSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
130268b8534bSLuigi Rizzo 
130368b8534bSLuigi Rizzo 	if (ifp == NULL) {
130468b8534bSLuigi Rizzo 		D("ifp not set, giving up");
130568b8534bSLuigi Rizzo 		return EINVAL;
130668b8534bSLuigi Rizzo 	}
130764ae02c3SLuigi Rizzo 	/* clear other fields ? */
130868b8534bSLuigi Rizzo 	na->refcount = 0;
1309d76bf4ffSLuigi Rizzo 	if (na->num_tx_rings == 0)
1310d76bf4ffSLuigi Rizzo 		na->num_tx_rings = num_queues;
1311d76bf4ffSLuigi Rizzo 	na->num_rx_rings = num_queues;
131264ae02c3SLuigi Rizzo 	/* on each direction we have N+1 resources
131364ae02c3SLuigi Rizzo 	 * 0..n-1	are the hardware rings
131464ae02c3SLuigi Rizzo 	 * n		is the ring attached to the stack.
131564ae02c3SLuigi Rizzo 	 */
1316d76bf4ffSLuigi Rizzo 	n = na->num_rx_rings + na->num_tx_rings + 2;
131764ae02c3SLuigi Rizzo 	size = sizeof(*na) + n * sizeof(struct netmap_kring);
131868b8534bSLuigi Rizzo 
131968b8534bSLuigi Rizzo 	buf = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
132068b8534bSLuigi Rizzo 	if (buf) {
1321d0c7b075SLuigi Rizzo 		WNA(ifp) = buf;
132268b8534bSLuigi Rizzo 		na->tx_rings = (void *)((char *)buf + sizeof(*na));
1323d76bf4ffSLuigi Rizzo 		na->rx_rings = na->tx_rings + na->num_tx_rings + 1;
132468b8534bSLuigi Rizzo 		bcopy(na, buf, sizeof(*na));
132568b8534bSLuigi Rizzo 		ifp->if_capabilities |= IFCAP_NETMAP;
13261a26580eSLuigi Rizzo 
13271a26580eSLuigi Rizzo 		na = buf;
1328*2f70fca5SEd Maste 		/* Core lock initialized here.  Others are initialized after
1329*2f70fca5SEd Maste 		 * netmap_if_new.
1330*2f70fca5SEd Maste 		 */
1331*2f70fca5SEd Maste 		mtx_init(&na->core_lock, "netmap core lock", MTX_NETWORK_LOCK,
1332*2f70fca5SEd Maste 		    MTX_DEF);
1333f196ce38SLuigi Rizzo 		if (na->nm_lock == NULL) {
1334f196ce38SLuigi Rizzo 			ND("using default locks for %s", ifp->if_xname);
13351a26580eSLuigi Rizzo 			na->nm_lock = netmap_lock_wrapper;
1336f196ce38SLuigi Rizzo 		}
133768b8534bSLuigi Rizzo 	}
133864ae02c3SLuigi Rizzo #ifdef linux
1339f196ce38SLuigi Rizzo 	if (ifp->netdev_ops) {
134064ae02c3SLuigi Rizzo 		D("netdev_ops %p", ifp->netdev_ops);
134164ae02c3SLuigi Rizzo 		/* prepare a clone of the netdev ops */
134264ae02c3SLuigi Rizzo 		na->nm_ndo = *ifp->netdev_ops;
1343f196ce38SLuigi Rizzo 	}
134442a3a5bdSLuigi Rizzo 	na->nm_ndo.ndo_start_xmit = linux_netmap_start;
134564ae02c3SLuigi Rizzo #endif
134668b8534bSLuigi Rizzo 	D("%s for %s", buf ? "ok" : "failed", ifp->if_xname);
134768b8534bSLuigi Rizzo 
134868b8534bSLuigi Rizzo 	return (buf ? 0 : ENOMEM);
134968b8534bSLuigi Rizzo }
135068b8534bSLuigi Rizzo 
135168b8534bSLuigi Rizzo 
135268b8534bSLuigi Rizzo /*
135368b8534bSLuigi Rizzo  * Free the allocated memory linked to the given ``netmap_adapter``
135468b8534bSLuigi Rizzo  * object.
135568b8534bSLuigi Rizzo  */
135668b8534bSLuigi Rizzo void
135768b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp)
135868b8534bSLuigi Rizzo {
135968b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
136068b8534bSLuigi Rizzo 
136168b8534bSLuigi Rizzo 	if (!na)
136268b8534bSLuigi Rizzo 		return;
136368b8534bSLuigi Rizzo 
1364*2f70fca5SEd Maste 	mtx_destroy(&na->core_lock);
1365*2f70fca5SEd Maste 
136668b8534bSLuigi Rizzo 	bzero(na, sizeof(*na));
1367d0c7b075SLuigi Rizzo 	WNA(ifp) = NULL;
136868b8534bSLuigi Rizzo 	free(na, M_DEVBUF);
136968b8534bSLuigi Rizzo }
137068b8534bSLuigi Rizzo 
137168b8534bSLuigi Rizzo 
137268b8534bSLuigi Rizzo /*
137302ad4083SLuigi Rizzo  * Intercept packets from the network stack and pass them
137402ad4083SLuigi Rizzo  * to netmap as incoming packets on the 'software' ring.
137568b8534bSLuigi Rizzo  * We are not locked when called.
137668b8534bSLuigi Rizzo  */
137768b8534bSLuigi Rizzo int
137868b8534bSLuigi Rizzo netmap_start(struct ifnet *ifp, struct mbuf *m)
137968b8534bSLuigi Rizzo {
138068b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
1381d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
13821a26580eSLuigi Rizzo 	u_int i, len = MBUF_LEN(m);
1383b3d53016SLuigi Rizzo 	u_int error = EBUSY, lim = kring->nkr_num_slots - 1;
138468b8534bSLuigi Rizzo 	struct netmap_slot *slot;
138568b8534bSLuigi Rizzo 
138668b8534bSLuigi Rizzo 	if (netmap_verbose & NM_VERB_HOST)
138768b8534bSLuigi Rizzo 		D("%s packet %d len %d from the stack", ifp->if_xname,
138868b8534bSLuigi Rizzo 			kring->nr_hwcur + kring->nr_hwavail, len);
13891a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
139002ad4083SLuigi Rizzo 	if (kring->nr_hwavail >= lim) {
13915b248374SLuigi Rizzo 		if (netmap_verbose)
139268b8534bSLuigi Rizzo 			D("stack ring %s full\n", ifp->if_xname);
139368b8534bSLuigi Rizzo 		goto done;	/* no space */
139468b8534bSLuigi Rizzo 	}
139564ae02c3SLuigi Rizzo 	if (len > NETMAP_BUF_SIZE) {
139664ae02c3SLuigi Rizzo 		D("drop packet size %d > %d", len, NETMAP_BUF_SIZE);
139768b8534bSLuigi Rizzo 		goto done;	/* too long for us */
139868b8534bSLuigi Rizzo 	}
139968b8534bSLuigi Rizzo 
140068b8534bSLuigi Rizzo 	/* compute the insert position */
140168b8534bSLuigi Rizzo 	i = kring->nr_hwcur + kring->nr_hwavail;
140202ad4083SLuigi Rizzo 	if (i > lim)
140302ad4083SLuigi Rizzo 		i -= lim + 1;
140468b8534bSLuigi Rizzo 	slot = &kring->ring->slot[i];
140568b8534bSLuigi Rizzo 	m_copydata(m, 0, len, NMB(slot));
140668b8534bSLuigi Rizzo 	slot->len = len;
140768b8534bSLuigi Rizzo 	kring->nr_hwavail++;
140868b8534bSLuigi Rizzo 	if (netmap_verbose  & NM_VERB_HOST)
1409d76bf4ffSLuigi Rizzo 		D("wake up host ring %s %d", na->ifp->if_xname, na->num_rx_rings);
141068b8534bSLuigi Rizzo 	selwakeuppri(&kring->si, PI_NET);
141168b8534bSLuigi Rizzo 	error = 0;
141268b8534bSLuigi Rizzo done:
14131a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
141468b8534bSLuigi Rizzo 
141568b8534bSLuigi Rizzo 	/* release the mbuf in either cases of success or failure. As an
141668b8534bSLuigi Rizzo 	 * alternative, put the mbuf in a free list and free the list
141768b8534bSLuigi Rizzo 	 * only when really necessary.
141868b8534bSLuigi Rizzo 	 */
141968b8534bSLuigi Rizzo 	m_freem(m);
142068b8534bSLuigi Rizzo 
142168b8534bSLuigi Rizzo 	return (error);
142268b8534bSLuigi Rizzo }
142368b8534bSLuigi Rizzo 
142468b8534bSLuigi Rizzo 
142568b8534bSLuigi Rizzo /*
142668b8534bSLuigi Rizzo  * netmap_reset() is called by the driver routines when reinitializing
142768b8534bSLuigi Rizzo  * a ring. The driver is in charge of locking to protect the kring.
142868b8534bSLuigi Rizzo  * If netmap mode is not set just return NULL.
142968b8534bSLuigi Rizzo  */
143068b8534bSLuigi Rizzo struct netmap_slot *
143168b8534bSLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, int n,
143268b8534bSLuigi Rizzo 	u_int new_cur)
143368b8534bSLuigi Rizzo {
143468b8534bSLuigi Rizzo 	struct netmap_kring *kring;
1435506cc70cSLuigi Rizzo 	int new_hwofs, lim;
143668b8534bSLuigi Rizzo 
143768b8534bSLuigi Rizzo 	if (na == NULL)
143868b8534bSLuigi Rizzo 		return NULL;	/* no netmap support here */
143968b8534bSLuigi Rizzo 	if (!(na->ifp->if_capenable & IFCAP_NETMAP))
144068b8534bSLuigi Rizzo 		return NULL;	/* nothing to reinitialize */
144168b8534bSLuigi Rizzo 
144264ae02c3SLuigi Rizzo 	if (tx == NR_TX) {
144364ae02c3SLuigi Rizzo 		kring = na->tx_rings + n;
1444506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur - new_cur;
144564ae02c3SLuigi Rizzo 	} else {
144664ae02c3SLuigi Rizzo 		kring = na->rx_rings + n;
1447506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur + kring->nr_hwavail - new_cur;
144864ae02c3SLuigi Rizzo 	}
144964ae02c3SLuigi Rizzo 	lim = kring->nkr_num_slots - 1;
1450506cc70cSLuigi Rizzo 	if (new_hwofs > lim)
1451506cc70cSLuigi Rizzo 		new_hwofs -= lim + 1;
1452506cc70cSLuigi Rizzo 
1453506cc70cSLuigi Rizzo 	/* Alwayws set the new offset value and realign the ring. */
1454506cc70cSLuigi Rizzo 	kring->nkr_hwofs = new_hwofs;
1455506cc70cSLuigi Rizzo 	if (tx == NR_TX)
1456506cc70cSLuigi Rizzo 		kring->nr_hwavail = kring->nkr_num_slots - 1;
1457506cc70cSLuigi Rizzo 	D("new hwofs %d on %s %s[%d]",
1458506cc70cSLuigi Rizzo 			kring->nkr_hwofs, na->ifp->if_xname,
1459506cc70cSLuigi Rizzo 			tx == NR_TX ? "TX" : "RX", n);
1460506cc70cSLuigi Rizzo 
1461f196ce38SLuigi Rizzo #if 0 // def linux
1462f196ce38SLuigi Rizzo 	/* XXX check that the mappings are correct */
1463f196ce38SLuigi Rizzo 	/* need ring_nr, adapter->pdev, direction */
1464f196ce38SLuigi Rizzo 	buffer_info->dma = dma_map_single(&pdev->dev, addr, adapter->rx_buffer_len, DMA_FROM_DEVICE);
1465f196ce38SLuigi Rizzo 	if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
1466f196ce38SLuigi Rizzo 		D("error mapping rx netmap buffer %d", i);
1467f196ce38SLuigi Rizzo 		// XXX fix error handling
1468f196ce38SLuigi Rizzo 	}
1469f196ce38SLuigi Rizzo 
1470f196ce38SLuigi Rizzo #endif /* linux */
147168b8534bSLuigi Rizzo 	/*
147264ae02c3SLuigi Rizzo 	 * Wakeup on the individual and global lock
1473506cc70cSLuigi Rizzo 	 * We do the wakeup here, but the ring is not yet reconfigured.
1474506cc70cSLuigi Rizzo 	 * However, we are under lock so there are no races.
147568b8534bSLuigi Rizzo 	 */
147668b8534bSLuigi Rizzo 	selwakeuppri(&kring->si, PI_NET);
147764ae02c3SLuigi Rizzo 	selwakeuppri(tx == NR_TX ? &na->tx_si : &na->rx_si, PI_NET);
147868b8534bSLuigi Rizzo 	return kring->ring->slot;
147968b8534bSLuigi Rizzo }
148068b8534bSLuigi Rizzo 
148168b8534bSLuigi Rizzo 
148268b8534bSLuigi Rizzo /*
14831a26580eSLuigi Rizzo  * Default functions to handle rx/tx interrupts
14841a26580eSLuigi Rizzo  * we have 4 cases:
14851a26580eSLuigi Rizzo  * 1 ring, single lock:
14861a26580eSLuigi Rizzo  *	lock(core); wake(i=0); unlock(core)
14871a26580eSLuigi Rizzo  * N rings, single lock:
14881a26580eSLuigi Rizzo  *	lock(core); wake(i); wake(N+1) unlock(core)
14891a26580eSLuigi Rizzo  * 1 ring, separate locks: (i=0)
14901a26580eSLuigi Rizzo  *	lock(i); wake(i); unlock(i)
14911a26580eSLuigi Rizzo  * N rings, separate locks:
14921a26580eSLuigi Rizzo  *	lock(i); wake(i); unlock(i); lock(core) wake(N+1) unlock(core)
149364ae02c3SLuigi Rizzo  * work_done is non-null on the RX path.
14941a26580eSLuigi Rizzo  */
1495babc7c12SLuigi Rizzo int
1496babc7c12SLuigi Rizzo netmap_rx_irq(struct ifnet *ifp, int q, int *work_done)
14971a26580eSLuigi Rizzo {
14981a26580eSLuigi Rizzo 	struct netmap_adapter *na;
14991a26580eSLuigi Rizzo 	struct netmap_kring *r;
150064ae02c3SLuigi Rizzo 	NM_SELINFO_T *main_wq;
15011a26580eSLuigi Rizzo 
15021a26580eSLuigi Rizzo 	if (!(ifp->if_capenable & IFCAP_NETMAP))
15031a26580eSLuigi Rizzo 		return 0;
15041a26580eSLuigi Rizzo 	na = NA(ifp);
150564ae02c3SLuigi Rizzo 	if (work_done) { /* RX path */
150664ae02c3SLuigi Rizzo 		r = na->rx_rings + q;
150764ae02c3SLuigi Rizzo 		r->nr_kflags |= NKR_PENDINTR;
1508d76bf4ffSLuigi Rizzo 		main_wq = (na->num_rx_rings > 1) ? &na->rx_si : NULL;
150964ae02c3SLuigi Rizzo 	} else { /* tx path */
151064ae02c3SLuigi Rizzo 		r = na->tx_rings + q;
1511d76bf4ffSLuigi Rizzo 		main_wq = (na->num_tx_rings > 1) ? &na->tx_si : NULL;
151264ae02c3SLuigi Rizzo 		work_done = &q; /* dummy */
151364ae02c3SLuigi Rizzo 	}
15141a26580eSLuigi Rizzo 	if (na->separate_locks) {
151564ae02c3SLuigi Rizzo 		mtx_lock(&r->q_lock);
151664ae02c3SLuigi Rizzo 		selwakeuppri(&r->si, PI_NET);
151764ae02c3SLuigi Rizzo 		mtx_unlock(&r->q_lock);
151864ae02c3SLuigi Rizzo 		if (main_wq) {
15191a26580eSLuigi Rizzo 			mtx_lock(&na->core_lock);
152064ae02c3SLuigi Rizzo 			selwakeuppri(main_wq, PI_NET);
15211a26580eSLuigi Rizzo 			mtx_unlock(&na->core_lock);
15221a26580eSLuigi Rizzo 		}
15231a26580eSLuigi Rizzo 	} else {
15241a26580eSLuigi Rizzo 		mtx_lock(&na->core_lock);
152564ae02c3SLuigi Rizzo 		selwakeuppri(&r->si, PI_NET);
152664ae02c3SLuigi Rizzo 		if (main_wq)
152764ae02c3SLuigi Rizzo 			selwakeuppri(main_wq, PI_NET);
15281a26580eSLuigi Rizzo 		mtx_unlock(&na->core_lock);
15291a26580eSLuigi Rizzo 	}
15301a26580eSLuigi Rizzo 	*work_done = 1; /* do not fire napi again */
15311a26580eSLuigi Rizzo 	return 1;
15321a26580eSLuigi Rizzo }
15331a26580eSLuigi Rizzo 
153464ae02c3SLuigi Rizzo 
153501c7d25fSLuigi Rizzo #ifdef linux	/* linux-specific routines */
153601c7d25fSLuigi Rizzo 
153701c7d25fSLuigi Rizzo /*
153801c7d25fSLuigi Rizzo  * Remap linux arguments into the FreeBSD call.
153901c7d25fSLuigi Rizzo  * - pwait is the poll table, passed as 'dev';
154001c7d25fSLuigi Rizzo  *   If pwait == NULL someone else already woke up before. We can report
154101c7d25fSLuigi Rizzo  *   events but they are filtered upstream.
154201c7d25fSLuigi Rizzo  *   If pwait != NULL, then pwait->key contains the list of events.
154301c7d25fSLuigi Rizzo  * - events is computed from pwait as above.
154401c7d25fSLuigi Rizzo  * - file is passed as 'td';
154501c7d25fSLuigi Rizzo  */
154601c7d25fSLuigi Rizzo static u_int
154701c7d25fSLuigi Rizzo linux_netmap_poll(struct file * file, struct poll_table_struct *pwait)
154801c7d25fSLuigi Rizzo {
154901c7d25fSLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(3,4,0)
155001c7d25fSLuigi Rizzo 	int events = pwait ? pwait->key : POLLIN | POLLOUT;
155101c7d25fSLuigi Rizzo #else /* in 3.4.0 field 'key' was renamed to '_key' */
155201c7d25fSLuigi Rizzo 	int events = pwait ? pwait->_key : POLLIN | POLLOUT;
155301c7d25fSLuigi Rizzo #endif
155401c7d25fSLuigi Rizzo 	return netmap_poll((void *)pwait, events, (void *)file);
155501c7d25fSLuigi Rizzo }
155601c7d25fSLuigi Rizzo 
155701c7d25fSLuigi Rizzo static int
155842a3a5bdSLuigi Rizzo linux_netmap_mmap(struct file *f, struct vm_area_struct *vma)
155901c7d25fSLuigi Rizzo {
156001c7d25fSLuigi Rizzo 	int lut_skip, i, j;
156101c7d25fSLuigi Rizzo 	int user_skip = 0;
156201c7d25fSLuigi Rizzo 	struct lut_entry *l_entry;
156301c7d25fSLuigi Rizzo 	const struct netmap_obj_pool *p[] = {
156401c7d25fSLuigi Rizzo 		nm_mem->nm_if_pool,
156501c7d25fSLuigi Rizzo 		nm_mem->nm_ring_pool,
156601c7d25fSLuigi Rizzo 		nm_mem->nm_buf_pool };
156701c7d25fSLuigi Rizzo 	/*
156801c7d25fSLuigi Rizzo 	 * vma->vm_start: start of mapping user address space
156901c7d25fSLuigi Rizzo 	 * vma->vm_end: end of the mapping user address space
157001c7d25fSLuigi Rizzo 	 */
157101c7d25fSLuigi Rizzo 
15720b8ed8e0SLuigi Rizzo 	(void)f;	/* UNUSED */
157301c7d25fSLuigi Rizzo 	// XXX security checks
157401c7d25fSLuigi Rizzo 
157501c7d25fSLuigi Rizzo 	for (i = 0; i < 3; i++) {  /* loop through obj_pools */
157601c7d25fSLuigi Rizzo 		/*
157701c7d25fSLuigi Rizzo 		 * In each pool memory is allocated in clusters
157801c7d25fSLuigi Rizzo 		 * of size _clustsize , each containing clustentries
157901c7d25fSLuigi Rizzo 		 * entries. For each object k we already store the
158001c7d25fSLuigi Rizzo 		 * vtophys malling in lut[k] so we use that, scanning
158101c7d25fSLuigi Rizzo 		 * the lut[] array in steps of clustentries,
158201c7d25fSLuigi Rizzo 		 * and we map each cluster (not individual pages,
158301c7d25fSLuigi Rizzo 		 * it would be overkill).
158401c7d25fSLuigi Rizzo 		 */
158501c7d25fSLuigi Rizzo 		for (lut_skip = 0, j = 0; j < p[i]->_numclusters; j++) {
158601c7d25fSLuigi Rizzo 			l_entry = &p[i]->lut[lut_skip];
158701c7d25fSLuigi Rizzo 			if (remap_pfn_range(vma, vma->vm_start + user_skip,
158801c7d25fSLuigi Rizzo 					l_entry->paddr >> PAGE_SHIFT, p[i]->_clustsize,
158901c7d25fSLuigi Rizzo 					vma->vm_page_prot))
159001c7d25fSLuigi Rizzo 				return -EAGAIN; // XXX check return value
159101c7d25fSLuigi Rizzo 			lut_skip += p[i]->clustentries;
159201c7d25fSLuigi Rizzo 			user_skip += p[i]->_clustsize;
159301c7d25fSLuigi Rizzo 		}
159401c7d25fSLuigi Rizzo 	}
159501c7d25fSLuigi Rizzo 
159601c7d25fSLuigi Rizzo 	return 0;
159701c7d25fSLuigi Rizzo }
159801c7d25fSLuigi Rizzo 
159901c7d25fSLuigi Rizzo static netdev_tx_t
160042a3a5bdSLuigi Rizzo linux_netmap_start(struct sk_buff *skb, struct net_device *dev)
160101c7d25fSLuigi Rizzo {
160201c7d25fSLuigi Rizzo 	netmap_start(dev, skb);
160301c7d25fSLuigi Rizzo 	return (NETDEV_TX_OK);
160401c7d25fSLuigi Rizzo }
160501c7d25fSLuigi Rizzo 
160601c7d25fSLuigi Rizzo 
16070b8ed8e0SLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)	// XXX was 38
160801c7d25fSLuigi Rizzo #define LIN_IOCTL_NAME	.ioctl
160901c7d25fSLuigi Rizzo int
161001c7d25fSLuigi Rizzo linux_netmap_ioctl(struct inode *inode, struct file *file, u_int cmd, u_long data /* arg */)
161101c7d25fSLuigi Rizzo #else
161201c7d25fSLuigi Rizzo #define LIN_IOCTL_NAME	.unlocked_ioctl
161301c7d25fSLuigi Rizzo long
161401c7d25fSLuigi Rizzo linux_netmap_ioctl(struct file *file, u_int cmd, u_long data /* arg */)
161501c7d25fSLuigi Rizzo #endif
161601c7d25fSLuigi Rizzo {
161701c7d25fSLuigi Rizzo 	int ret;
161801c7d25fSLuigi Rizzo 	struct nmreq nmr;
161901c7d25fSLuigi Rizzo 	bzero(&nmr, sizeof(nmr));
162001c7d25fSLuigi Rizzo 
162101c7d25fSLuigi Rizzo 	if (data && copy_from_user(&nmr, (void *)data, sizeof(nmr) ) != 0)
162201c7d25fSLuigi Rizzo 		return -EFAULT;
162301c7d25fSLuigi Rizzo 	ret = netmap_ioctl(NULL, cmd, (caddr_t)&nmr, 0, (void *)file);
162401c7d25fSLuigi Rizzo 	if (data && copy_to_user((void*)data, &nmr, sizeof(nmr) ) != 0)
162501c7d25fSLuigi Rizzo 		return -EFAULT;
162601c7d25fSLuigi Rizzo 	return -ret;
162701c7d25fSLuigi Rizzo }
162801c7d25fSLuigi Rizzo 
162901c7d25fSLuigi Rizzo 
163001c7d25fSLuigi Rizzo static int
16310b8ed8e0SLuigi Rizzo netmap_release(struct inode *inode, struct file *file)
163201c7d25fSLuigi Rizzo {
16330b8ed8e0SLuigi Rizzo 	(void)inode;	/* UNUSED */
163401c7d25fSLuigi Rizzo 	if (file->private_data)
163501c7d25fSLuigi Rizzo 		netmap_dtor(file->private_data);
163601c7d25fSLuigi Rizzo 	return (0);
163701c7d25fSLuigi Rizzo }
163801c7d25fSLuigi Rizzo 
163901c7d25fSLuigi Rizzo 
164001c7d25fSLuigi Rizzo static struct file_operations netmap_fops = {
164142a3a5bdSLuigi Rizzo     .mmap = linux_netmap_mmap,
164201c7d25fSLuigi Rizzo     LIN_IOCTL_NAME = linux_netmap_ioctl,
164301c7d25fSLuigi Rizzo     .poll = linux_netmap_poll,
164401c7d25fSLuigi Rizzo     .release = netmap_release,
164501c7d25fSLuigi Rizzo };
164601c7d25fSLuigi Rizzo 
164701c7d25fSLuigi Rizzo static struct miscdevice netmap_cdevsw = {	/* same name as FreeBSD */
164801c7d25fSLuigi Rizzo 	MISC_DYNAMIC_MINOR,
164901c7d25fSLuigi Rizzo 	"netmap",
165001c7d25fSLuigi Rizzo 	&netmap_fops,
165101c7d25fSLuigi Rizzo };
165201c7d25fSLuigi Rizzo 
165301c7d25fSLuigi Rizzo static int netmap_init(void);
165401c7d25fSLuigi Rizzo static void netmap_fini(void);
165501c7d25fSLuigi Rizzo 
165642a3a5bdSLuigi Rizzo /* Errors have negative values on linux */
165742a3a5bdSLuigi Rizzo static int linux_netmap_init(void)
165842a3a5bdSLuigi Rizzo {
165942a3a5bdSLuigi Rizzo 	return -netmap_init();
166042a3a5bdSLuigi Rizzo }
166142a3a5bdSLuigi Rizzo 
166242a3a5bdSLuigi Rizzo module_init(linux_netmap_init);
166301c7d25fSLuigi Rizzo module_exit(netmap_fini);
166401c7d25fSLuigi Rizzo /* export certain symbols to other modules */
166501c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_attach);		// driver attach routines
166601c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_detach);		// driver detach routines
166701c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_ring_reinit);	// ring init on error
166801c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_buffer_lut);
166901c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_total_buffers);	// index check
167001c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_buffer_base);
167101c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_reset);		// ring init routines
167201c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_buf_size);
167301c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_rx_irq);		// default irq handler
167401c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_no_pendintr);	// XXX mitigation - should go away
167501c7d25fSLuigi Rizzo 
167601c7d25fSLuigi Rizzo 
167701c7d25fSLuigi Rizzo MODULE_AUTHOR("http://info.iet.unipi.it/~luigi/netmap/");
167801c7d25fSLuigi Rizzo MODULE_DESCRIPTION("The netmap packet I/O framework");
167901c7d25fSLuigi Rizzo MODULE_LICENSE("Dual BSD/GPL"); /* the code here is all BSD. */
168001c7d25fSLuigi Rizzo 
168101c7d25fSLuigi Rizzo #else /* __FreeBSD__ */
168201c7d25fSLuigi Rizzo 
1683babc7c12SLuigi Rizzo static struct cdevsw netmap_cdevsw = {
1684babc7c12SLuigi Rizzo 	.d_version = D_VERSION,
1685babc7c12SLuigi Rizzo 	.d_name = "netmap",
1686babc7c12SLuigi Rizzo 	.d_mmap = netmap_mmap,
1687babc7c12SLuigi Rizzo 	.d_ioctl = netmap_ioctl,
1688babc7c12SLuigi Rizzo 	.d_poll = netmap_poll,
1689babc7c12SLuigi Rizzo };
169001c7d25fSLuigi Rizzo #endif /* __FreeBSD__ */
1691babc7c12SLuigi Rizzo 
1692f196ce38SLuigi Rizzo #ifdef NM_BRIDGE
1693f196ce38SLuigi Rizzo /*
1694f196ce38SLuigi Rizzo  *---- support for virtual bridge -----
1695f196ce38SLuigi Rizzo  */
1696f196ce38SLuigi Rizzo 
1697f196ce38SLuigi Rizzo /* ----- FreeBSD if_bridge hash function ------- */
1698f196ce38SLuigi Rizzo 
1699f196ce38SLuigi Rizzo /*
1700f196ce38SLuigi Rizzo  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
1701f196ce38SLuigi Rizzo  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
1702f196ce38SLuigi Rizzo  *
1703f196ce38SLuigi Rizzo  * http://www.burtleburtle.net/bob/hash/spooky.html
1704f196ce38SLuigi Rizzo  */
1705f196ce38SLuigi Rizzo #define mix(a, b, c)                                                    \
1706f196ce38SLuigi Rizzo do {                                                                    \
1707f196ce38SLuigi Rizzo         a -= b; a -= c; a ^= (c >> 13);                                 \
1708f196ce38SLuigi Rizzo         b -= c; b -= a; b ^= (a << 8);                                  \
1709f196ce38SLuigi Rizzo         c -= a; c -= b; c ^= (b >> 13);                                 \
1710f196ce38SLuigi Rizzo         a -= b; a -= c; a ^= (c >> 12);                                 \
1711f196ce38SLuigi Rizzo         b -= c; b -= a; b ^= (a << 16);                                 \
1712f196ce38SLuigi Rizzo         c -= a; c -= b; c ^= (b >> 5);                                  \
1713f196ce38SLuigi Rizzo         a -= b; a -= c; a ^= (c >> 3);                                  \
1714f196ce38SLuigi Rizzo         b -= c; b -= a; b ^= (a << 10);                                 \
1715f196ce38SLuigi Rizzo         c -= a; c -= b; c ^= (b >> 15);                                 \
1716f196ce38SLuigi Rizzo } while (/*CONSTCOND*/0)
1717f196ce38SLuigi Rizzo 
1718f196ce38SLuigi Rizzo static __inline uint32_t
1719f196ce38SLuigi Rizzo nm_bridge_rthash(const uint8_t *addr)
1720f196ce38SLuigi Rizzo {
1721f196ce38SLuigi Rizzo         uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = 0; // hask key
1722f196ce38SLuigi Rizzo 
1723f196ce38SLuigi Rizzo         b += addr[5] << 8;
1724f196ce38SLuigi Rizzo         b += addr[4];
1725f196ce38SLuigi Rizzo         a += addr[3] << 24;
1726f196ce38SLuigi Rizzo         a += addr[2] << 16;
1727f196ce38SLuigi Rizzo         a += addr[1] << 8;
1728f196ce38SLuigi Rizzo         a += addr[0];
1729f196ce38SLuigi Rizzo 
1730f196ce38SLuigi Rizzo         mix(a, b, c);
1731f196ce38SLuigi Rizzo #define BRIDGE_RTHASH_MASK	(NM_BDG_HASH-1)
1732f196ce38SLuigi Rizzo         return (c & BRIDGE_RTHASH_MASK);
1733f196ce38SLuigi Rizzo }
1734f196ce38SLuigi Rizzo 
1735f196ce38SLuigi Rizzo #undef mix
1736f196ce38SLuigi Rizzo 
1737f196ce38SLuigi Rizzo 
1738f196ce38SLuigi Rizzo static int
1739f196ce38SLuigi Rizzo bdg_netmap_reg(struct ifnet *ifp, int onoff)
1740f196ce38SLuigi Rizzo {
1741f196ce38SLuigi Rizzo 	int i, err = 0;
1742f196ce38SLuigi Rizzo 	struct nm_bridge *b = ifp->if_bridge;
1743f196ce38SLuigi Rizzo 
1744f196ce38SLuigi Rizzo 	BDG_LOCK(b);
1745f196ce38SLuigi Rizzo 	if (onoff) {
1746f196ce38SLuigi Rizzo 		/* the interface must be already in the list.
1747f196ce38SLuigi Rizzo 		 * only need to mark the port as active
1748f196ce38SLuigi Rizzo 		 */
1749f196ce38SLuigi Rizzo 		ND("should attach %s to the bridge", ifp->if_xname);
1750f196ce38SLuigi Rizzo 		for (i=0; i < NM_BDG_MAXPORTS; i++)
1751f196ce38SLuigi Rizzo 			if (b->bdg_ports[i] == ifp)
1752f196ce38SLuigi Rizzo 				break;
1753f196ce38SLuigi Rizzo 		if (i == NM_BDG_MAXPORTS) {
1754f196ce38SLuigi Rizzo 			D("no more ports available");
1755f196ce38SLuigi Rizzo 			err = EINVAL;
1756f196ce38SLuigi Rizzo 			goto done;
1757f196ce38SLuigi Rizzo 		}
1758f196ce38SLuigi Rizzo 		ND("setting %s in netmap mode", ifp->if_xname);
1759f196ce38SLuigi Rizzo 		ifp->if_capenable |= IFCAP_NETMAP;
1760f196ce38SLuigi Rizzo 		NA(ifp)->bdg_port = i;
1761f196ce38SLuigi Rizzo 		b->act_ports |= (1<<i);
1762f196ce38SLuigi Rizzo 		b->bdg_ports[i] = ifp;
1763f196ce38SLuigi Rizzo 	} else {
1764f196ce38SLuigi Rizzo 		/* should be in the list, too -- remove from the mask */
1765f196ce38SLuigi Rizzo 		ND("removing %s from netmap mode", ifp->if_xname);
1766f196ce38SLuigi Rizzo 		ifp->if_capenable &= ~IFCAP_NETMAP;
1767f196ce38SLuigi Rizzo 		i = NA(ifp)->bdg_port;
1768f196ce38SLuigi Rizzo 		b->act_ports &= ~(1<<i);
1769f196ce38SLuigi Rizzo 	}
1770f196ce38SLuigi Rizzo done:
1771f196ce38SLuigi Rizzo 	BDG_UNLOCK(b);
1772f196ce38SLuigi Rizzo 	return err;
1773f196ce38SLuigi Rizzo }
1774f196ce38SLuigi Rizzo 
1775f196ce38SLuigi Rizzo 
1776f196ce38SLuigi Rizzo static int
1777f196ce38SLuigi Rizzo nm_bdg_flush(struct nm_bdg_fwd *ft, int n, struct ifnet *ifp)
1778f196ce38SLuigi Rizzo {
1779f196ce38SLuigi Rizzo 	int i, ifn;
1780f196ce38SLuigi Rizzo 	uint64_t all_dst, dst;
1781f196ce38SLuigi Rizzo 	uint32_t sh, dh;
1782f196ce38SLuigi Rizzo 	uint64_t mysrc = 1 << NA(ifp)->bdg_port;
1783f196ce38SLuigi Rizzo 	uint64_t smac, dmac;
1784f196ce38SLuigi Rizzo 	struct netmap_slot *slot;
1785f196ce38SLuigi Rizzo 	struct nm_bridge *b = ifp->if_bridge;
1786f196ce38SLuigi Rizzo 
1787f196ce38SLuigi Rizzo 	ND("prepare to send %d packets, act_ports 0x%x", n, b->act_ports);
1788f196ce38SLuigi Rizzo 	/* only consider valid destinations */
1789f196ce38SLuigi Rizzo 	all_dst = (b->act_ports & ~mysrc);
1790f196ce38SLuigi Rizzo 	/* first pass: hash and find destinations */
1791f196ce38SLuigi Rizzo 	for (i = 0; likely(i < n); i++) {
1792f196ce38SLuigi Rizzo 		uint8_t *buf = ft[i].buf;
1793f196ce38SLuigi Rizzo 		dmac = le64toh(*(uint64_t *)(buf)) & 0xffffffffffff;
1794f196ce38SLuigi Rizzo 		smac = le64toh(*(uint64_t *)(buf + 4));
1795f196ce38SLuigi Rizzo 		smac >>= 16;
1796f196ce38SLuigi Rizzo 		if (unlikely(netmap_verbose)) {
1797f196ce38SLuigi Rizzo 		    uint8_t *s = buf+6, *d = buf;
1798f196ce38SLuigi Rizzo 		    D("%d len %4d %02x:%02x:%02x:%02x:%02x:%02x -> %02x:%02x:%02x:%02x:%02x:%02x",
1799f196ce38SLuigi Rizzo 			i,
1800f196ce38SLuigi Rizzo 			ft[i].len,
1801f196ce38SLuigi Rizzo 			s[0], s[1], s[2], s[3], s[4], s[5],
1802f196ce38SLuigi Rizzo 			d[0], d[1], d[2], d[3], d[4], d[5]);
1803f196ce38SLuigi Rizzo 		}
1804f196ce38SLuigi Rizzo 		/*
1805f196ce38SLuigi Rizzo 		 * The hash is somewhat expensive, there might be some
1806f196ce38SLuigi Rizzo 		 * worthwhile optimizations here.
1807f196ce38SLuigi Rizzo 		 */
1808f196ce38SLuigi Rizzo 		if ((buf[6] & 1) == 0) { /* valid src */
1809f196ce38SLuigi Rizzo 		    	uint8_t *s = buf+6;
1810f196ce38SLuigi Rizzo 			sh = nm_bridge_rthash(buf+6); // XXX hash of source
1811f196ce38SLuigi Rizzo 			/* update source port forwarding entry */
1812f196ce38SLuigi Rizzo 			b->ht[sh].mac = smac;	/* XXX expire ? */
1813f196ce38SLuigi Rizzo 			b->ht[sh].ports = mysrc;
1814f196ce38SLuigi Rizzo 			if (netmap_verbose)
1815f196ce38SLuigi Rizzo 			    D("src %02x:%02x:%02x:%02x:%02x:%02x on port %d",
1816f196ce38SLuigi Rizzo 				s[0], s[1], s[2], s[3], s[4], s[5], NA(ifp)->bdg_port);
1817f196ce38SLuigi Rizzo 		}
1818f196ce38SLuigi Rizzo 		dst = 0;
1819f196ce38SLuigi Rizzo 		if ( (buf[0] & 1) == 0) { /* unicast */
1820f196ce38SLuigi Rizzo 		    	uint8_t *d = buf;
1821f196ce38SLuigi Rizzo 			dh = nm_bridge_rthash(buf); // XXX hash of dst
1822f196ce38SLuigi Rizzo 			if (b->ht[dh].mac == dmac) {	/* found dst */
1823f196ce38SLuigi Rizzo 				dst = b->ht[dh].ports;
1824f196ce38SLuigi Rizzo 				if (netmap_verbose)
1825f196ce38SLuigi Rizzo 				    D("dst %02x:%02x:%02x:%02x:%02x:%02x to port %x",
1826f196ce38SLuigi Rizzo 					d[0], d[1], d[2], d[3], d[4], d[5], (uint32_t)(dst >> 16));
1827f196ce38SLuigi Rizzo 			}
1828f196ce38SLuigi Rizzo 		}
1829f196ce38SLuigi Rizzo 		if (dst == 0)
1830f196ce38SLuigi Rizzo 			dst = all_dst;
1831f196ce38SLuigi Rizzo 		dst &= all_dst; /* only consider valid ports */
1832f196ce38SLuigi Rizzo 		if (unlikely(netmap_verbose))
1833f196ce38SLuigi Rizzo 			D("pkt goes to ports 0x%x", (uint32_t)dst);
1834f196ce38SLuigi Rizzo 		ft[i].dst = dst;
1835f196ce38SLuigi Rizzo 	}
1836f196ce38SLuigi Rizzo 
1837f196ce38SLuigi Rizzo 	/* second pass, scan interfaces and forward */
1838f196ce38SLuigi Rizzo 	all_dst = (b->act_ports & ~mysrc);
1839f196ce38SLuigi Rizzo 	for (ifn = 0; all_dst; ifn++) {
1840f196ce38SLuigi Rizzo 		struct ifnet *dst_ifp = b->bdg_ports[ifn];
1841f196ce38SLuigi Rizzo 		struct netmap_adapter *na;
1842f196ce38SLuigi Rizzo 		struct netmap_kring *kring;
1843f196ce38SLuigi Rizzo 		struct netmap_ring *ring;
1844f196ce38SLuigi Rizzo 		int j, lim, sent, locked;
1845f196ce38SLuigi Rizzo 
1846f196ce38SLuigi Rizzo 		if (!dst_ifp)
1847f196ce38SLuigi Rizzo 			continue;
1848f196ce38SLuigi Rizzo 		ND("scan port %d %s", ifn, dst_ifp->if_xname);
1849f196ce38SLuigi Rizzo 		dst = 1 << ifn;
1850f196ce38SLuigi Rizzo 		if ((dst & all_dst) == 0)	/* skip if not set */
1851f196ce38SLuigi Rizzo 			continue;
1852f196ce38SLuigi Rizzo 		all_dst &= ~dst;	/* clear current node */
1853f196ce38SLuigi Rizzo 		na = NA(dst_ifp);
1854f196ce38SLuigi Rizzo 
1855f196ce38SLuigi Rizzo 		ring = NULL;
1856f196ce38SLuigi Rizzo 		kring = NULL;
1857f196ce38SLuigi Rizzo 		lim = sent = locked = 0;
1858f196ce38SLuigi Rizzo 		/* inside, scan slots */
1859f196ce38SLuigi Rizzo 		for (i = 0; likely(i < n); i++) {
1860f196ce38SLuigi Rizzo 			if ((ft[i].dst & dst) == 0)
1861f196ce38SLuigi Rizzo 				continue;	/* not here */
1862f196ce38SLuigi Rizzo 			if (!locked) {
1863f196ce38SLuigi Rizzo 				kring = &na->rx_rings[0];
1864f196ce38SLuigi Rizzo 				ring = kring->ring;
1865f196ce38SLuigi Rizzo 				lim = kring->nkr_num_slots - 1;
1866f196ce38SLuigi Rizzo 				na->nm_lock(dst_ifp, NETMAP_RX_LOCK, 0);
1867f196ce38SLuigi Rizzo 				locked = 1;
1868f196ce38SLuigi Rizzo 			}
1869f196ce38SLuigi Rizzo 			if (unlikely(kring->nr_hwavail >= lim)) {
1870f196ce38SLuigi Rizzo 				if (netmap_verbose)
1871f196ce38SLuigi Rizzo 					D("rx ring full on %s", ifp->if_xname);
1872f196ce38SLuigi Rizzo 				break;
1873f196ce38SLuigi Rizzo 			}
1874f196ce38SLuigi Rizzo 			j = kring->nr_hwcur + kring->nr_hwavail;
1875f196ce38SLuigi Rizzo 			if (j > lim)
1876f196ce38SLuigi Rizzo 				j -= kring->nkr_num_slots;
1877f196ce38SLuigi Rizzo 			slot = &ring->slot[j];
1878f196ce38SLuigi Rizzo 			ND("send %d %d bytes at %s:%d", i, ft[i].len, dst_ifp->if_xname, j);
1879f196ce38SLuigi Rizzo 			pkt_copy(ft[i].buf, NMB(slot), ft[i].len);
1880f196ce38SLuigi Rizzo 			slot->len = ft[i].len;
1881f196ce38SLuigi Rizzo 			kring->nr_hwavail++;
1882f196ce38SLuigi Rizzo 			sent++;
1883f196ce38SLuigi Rizzo 		}
1884f196ce38SLuigi Rizzo 		if (locked) {
1885f196ce38SLuigi Rizzo 			ND("sent %d on %s", sent, dst_ifp->if_xname);
1886f196ce38SLuigi Rizzo 			if (sent)
1887f196ce38SLuigi Rizzo 				selwakeuppri(&kring->si, PI_NET);
1888f196ce38SLuigi Rizzo 			na->nm_lock(dst_ifp, NETMAP_RX_UNLOCK, 0);
1889f196ce38SLuigi Rizzo 		}
1890f196ce38SLuigi Rizzo 	}
1891f196ce38SLuigi Rizzo 	return 0;
1892f196ce38SLuigi Rizzo }
1893f196ce38SLuigi Rizzo 
1894f196ce38SLuigi Rizzo /*
1895f196ce38SLuigi Rizzo  * main dispatch routine
1896f196ce38SLuigi Rizzo  */
1897f196ce38SLuigi Rizzo static int
1898f196ce38SLuigi Rizzo bdg_netmap_txsync(struct ifnet *ifp, u_int ring_nr, int do_lock)
1899f196ce38SLuigi Rizzo {
1900f196ce38SLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
1901f196ce38SLuigi Rizzo 	struct netmap_kring *kring = &na->tx_rings[ring_nr];
1902f196ce38SLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
1903f196ce38SLuigi Rizzo 	int i, j, k, lim = kring->nkr_num_slots - 1;
1904f196ce38SLuigi Rizzo 	struct nm_bdg_fwd *ft = (struct nm_bdg_fwd *)(ifp + 1);
1905f196ce38SLuigi Rizzo 	int ft_i;	/* position in the forwarding table */
1906f196ce38SLuigi Rizzo 
1907f196ce38SLuigi Rizzo 	k = ring->cur;
1908f196ce38SLuigi Rizzo 	if (k > lim)
1909f196ce38SLuigi Rizzo 		return netmap_ring_reinit(kring);
1910f196ce38SLuigi Rizzo 	if (do_lock)
1911f196ce38SLuigi Rizzo 		na->nm_lock(ifp, NETMAP_TX_LOCK, ring_nr);
1912f196ce38SLuigi Rizzo 
1913f196ce38SLuigi Rizzo 	if (netmap_bridge <= 0) { /* testing only */
1914f196ce38SLuigi Rizzo 		j = k; // used all
1915f196ce38SLuigi Rizzo 		goto done;
1916f196ce38SLuigi Rizzo 	}
1917f196ce38SLuigi Rizzo 	if (netmap_bridge > NM_BDG_BATCH)
1918f196ce38SLuigi Rizzo 		netmap_bridge = NM_BDG_BATCH;
1919f196ce38SLuigi Rizzo 
1920f196ce38SLuigi Rizzo 	ft_i = 0;	/* start from 0 */
1921f196ce38SLuigi Rizzo 	for (j = kring->nr_hwcur; likely(j != k); j = unlikely(j == lim) ? 0 : j+1) {
1922f196ce38SLuigi Rizzo 		struct netmap_slot *slot = &ring->slot[j];
1923f196ce38SLuigi Rizzo 		int len = ft[ft_i].len = slot->len;
1924f196ce38SLuigi Rizzo 		char *buf = ft[ft_i].buf = NMB(slot);
1925f196ce38SLuigi Rizzo 
1926f196ce38SLuigi Rizzo 		prefetch(buf);
1927f196ce38SLuigi Rizzo 		if (unlikely(len < 14))
1928f196ce38SLuigi Rizzo 			continue;
1929f196ce38SLuigi Rizzo 		if (unlikely(++ft_i == netmap_bridge))
1930f196ce38SLuigi Rizzo 			ft_i = nm_bdg_flush(ft, ft_i, ifp);
1931f196ce38SLuigi Rizzo 	}
1932f196ce38SLuigi Rizzo 	if (ft_i)
1933f196ce38SLuigi Rizzo 		ft_i = nm_bdg_flush(ft, ft_i, ifp);
1934f196ce38SLuigi Rizzo 	/* count how many packets we sent */
1935f196ce38SLuigi Rizzo 	i = k - j;
1936f196ce38SLuigi Rizzo 	if (i < 0)
1937f196ce38SLuigi Rizzo 		i += kring->nkr_num_slots;
1938f196ce38SLuigi Rizzo 	kring->nr_hwavail = kring->nkr_num_slots - 1 - i;
1939f196ce38SLuigi Rizzo 	if (j != k)
1940f196ce38SLuigi Rizzo 		D("early break at %d/ %d, avail %d", j, k, kring->nr_hwavail);
1941f196ce38SLuigi Rizzo 
1942f196ce38SLuigi Rizzo done:
1943f196ce38SLuigi Rizzo 	kring->nr_hwcur = j;
1944f196ce38SLuigi Rizzo 	ring->avail = kring->nr_hwavail;
1945f196ce38SLuigi Rizzo 	if (do_lock)
1946f196ce38SLuigi Rizzo 		na->nm_lock(ifp, NETMAP_TX_UNLOCK, ring_nr);
1947f196ce38SLuigi Rizzo 
1948f196ce38SLuigi Rizzo 	if (netmap_verbose)
1949f196ce38SLuigi Rizzo 		D("%s ring %d lock %d", ifp->if_xname, ring_nr, do_lock);
1950f196ce38SLuigi Rizzo 	return 0;
1951f196ce38SLuigi Rizzo }
1952f196ce38SLuigi Rizzo 
1953f196ce38SLuigi Rizzo static int
1954f196ce38SLuigi Rizzo bdg_netmap_rxsync(struct ifnet *ifp, u_int ring_nr, int do_lock)
1955f196ce38SLuigi Rizzo {
1956f196ce38SLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
1957f196ce38SLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[ring_nr];
1958f196ce38SLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
1959b3d53016SLuigi Rizzo 	u_int j, n, lim = kring->nkr_num_slots - 1;
1960f196ce38SLuigi Rizzo 	u_int k = ring->cur, resvd = ring->reserved;
1961f196ce38SLuigi Rizzo 
1962f196ce38SLuigi Rizzo 	ND("%s ring %d lock %d avail %d",
1963f196ce38SLuigi Rizzo 		ifp->if_xname, ring_nr, do_lock, kring->nr_hwavail);
1964f196ce38SLuigi Rizzo 
1965f196ce38SLuigi Rizzo 	if (k > lim)
1966f196ce38SLuigi Rizzo 		return netmap_ring_reinit(kring);
1967f196ce38SLuigi Rizzo 	if (do_lock)
1968f196ce38SLuigi Rizzo 		na->nm_lock(ifp, NETMAP_RX_LOCK, ring_nr);
1969f196ce38SLuigi Rizzo 
1970f196ce38SLuigi Rizzo 	/* skip past packets that userspace has released */
1971f196ce38SLuigi Rizzo 	j = kring->nr_hwcur;    /* netmap ring index */
1972f196ce38SLuigi Rizzo 	if (resvd > 0) {
1973f196ce38SLuigi Rizzo 		if (resvd + ring->avail >= lim + 1) {
1974f196ce38SLuigi Rizzo 			D("XXX invalid reserve/avail %d %d", resvd, ring->avail);
1975f196ce38SLuigi Rizzo 			ring->reserved = resvd = 0; // XXX panic...
1976f196ce38SLuigi Rizzo 		}
1977f196ce38SLuigi Rizzo 		k = (k >= resvd) ? k - resvd : k + lim + 1 - resvd;
1978f196ce38SLuigi Rizzo 	}
1979f196ce38SLuigi Rizzo 
1980f196ce38SLuigi Rizzo 	if (j != k) { /* userspace has released some packets. */
1981f196ce38SLuigi Rizzo 		n = k - j;
1982f196ce38SLuigi Rizzo 		if (n < 0)
1983f196ce38SLuigi Rizzo 			n += kring->nkr_num_slots;
1984f196ce38SLuigi Rizzo 		ND("userspace releases %d packets", n);
1985f196ce38SLuigi Rizzo                 for (n = 0; likely(j != k); n++) {
1986f196ce38SLuigi Rizzo                         struct netmap_slot *slot = &ring->slot[j];
1987f196ce38SLuigi Rizzo                         void *addr = NMB(slot);
1988f196ce38SLuigi Rizzo 
1989f196ce38SLuigi Rizzo                         if (addr == netmap_buffer_base) { /* bad buf */
1990f196ce38SLuigi Rizzo                                 if (do_lock)
1991f196ce38SLuigi Rizzo                                         na->nm_lock(ifp, NETMAP_RX_UNLOCK, ring_nr);
1992f196ce38SLuigi Rizzo                                 return netmap_ring_reinit(kring);
1993f196ce38SLuigi Rizzo                         }
1994f196ce38SLuigi Rizzo 			/* decrease refcount for buffer */
1995f196ce38SLuigi Rizzo 
1996f196ce38SLuigi Rizzo 			slot->flags &= ~NS_BUF_CHANGED;
1997f196ce38SLuigi Rizzo                         j = unlikely(j == lim) ? 0 : j + 1;
1998f196ce38SLuigi Rizzo                 }
1999f196ce38SLuigi Rizzo                 kring->nr_hwavail -= n;
2000f196ce38SLuigi Rizzo                 kring->nr_hwcur = k;
2001f196ce38SLuigi Rizzo         }
2002f196ce38SLuigi Rizzo         /* tell userspace that there are new packets */
2003f196ce38SLuigi Rizzo         ring->avail = kring->nr_hwavail - resvd;
2004f196ce38SLuigi Rizzo 
2005f196ce38SLuigi Rizzo 	if (do_lock)
2006f196ce38SLuigi Rizzo 		na->nm_lock(ifp, NETMAP_RX_UNLOCK, ring_nr);
2007f196ce38SLuigi Rizzo 	return 0;
2008f196ce38SLuigi Rizzo }
2009f196ce38SLuigi Rizzo 
2010f196ce38SLuigi Rizzo static void
2011f196ce38SLuigi Rizzo bdg_netmap_attach(struct ifnet *ifp)
2012f196ce38SLuigi Rizzo {
2013f196ce38SLuigi Rizzo 	struct netmap_adapter na;
2014f196ce38SLuigi Rizzo 
2015f196ce38SLuigi Rizzo 	ND("attaching virtual bridge");
2016f196ce38SLuigi Rizzo 	bzero(&na, sizeof(na));
2017f196ce38SLuigi Rizzo 
2018f196ce38SLuigi Rizzo 	na.ifp = ifp;
2019f196ce38SLuigi Rizzo 	na.separate_locks = 1;
2020f196ce38SLuigi Rizzo 	na.num_tx_desc = NM_BRIDGE_RINGSIZE;
2021f196ce38SLuigi Rizzo 	na.num_rx_desc = NM_BRIDGE_RINGSIZE;
2022f196ce38SLuigi Rizzo 	na.nm_txsync = bdg_netmap_txsync;
2023f196ce38SLuigi Rizzo 	na.nm_rxsync = bdg_netmap_rxsync;
2024f196ce38SLuigi Rizzo 	na.nm_register = bdg_netmap_reg;
2025f196ce38SLuigi Rizzo 	netmap_attach(&na, 1);
2026f196ce38SLuigi Rizzo }
2027f196ce38SLuigi Rizzo 
2028f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */
2029babc7c12SLuigi Rizzo 
2030babc7c12SLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */
2031babc7c12SLuigi Rizzo 
2032babc7c12SLuigi Rizzo 
20331a26580eSLuigi Rizzo /*
203468b8534bSLuigi Rizzo  * Module loader.
203568b8534bSLuigi Rizzo  *
203668b8534bSLuigi Rizzo  * Create the /dev/netmap device and initialize all global
203768b8534bSLuigi Rizzo  * variables.
203868b8534bSLuigi Rizzo  *
203968b8534bSLuigi Rizzo  * Return 0 on success, errno on failure.
204068b8534bSLuigi Rizzo  */
204168b8534bSLuigi Rizzo static int
204268b8534bSLuigi Rizzo netmap_init(void)
204368b8534bSLuigi Rizzo {
204468b8534bSLuigi Rizzo 	int error;
204568b8534bSLuigi Rizzo 
204668b8534bSLuigi Rizzo 	error = netmap_memory_init();
204768b8534bSLuigi Rizzo 	if (error != 0) {
204842a3a5bdSLuigi Rizzo 		printf("netmap: unable to initialize the memory allocator.\n");
204968b8534bSLuigi Rizzo 		return (error);
205068b8534bSLuigi Rizzo 	}
205168b8534bSLuigi Rizzo 	printf("netmap: loaded module with %d Mbytes\n",
205264ae02c3SLuigi Rizzo 		(int)(nm_mem->nm_totalsize >> 20));
205368b8534bSLuigi Rizzo 	netmap_dev = make_dev(&netmap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660,
205468b8534bSLuigi Rizzo 			      "netmap");
2055f196ce38SLuigi Rizzo 
2056f196ce38SLuigi Rizzo #ifdef NM_BRIDGE
2057f196ce38SLuigi Rizzo 	{
2058f196ce38SLuigi Rizzo 	int i;
2059f196ce38SLuigi Rizzo 	for (i = 0; i < NM_BRIDGES; i++)
2060f196ce38SLuigi Rizzo 		mtx_init(&nm_bridges[i].bdg_lock, "bdg lock", "bdg_lock", MTX_DEF);
2061f196ce38SLuigi Rizzo 	}
2062f196ce38SLuigi Rizzo #endif
2063babc7c12SLuigi Rizzo 	return (error);
206468b8534bSLuigi Rizzo }
206568b8534bSLuigi Rizzo 
206668b8534bSLuigi Rizzo 
206768b8534bSLuigi Rizzo /*
206868b8534bSLuigi Rizzo  * Module unloader.
206968b8534bSLuigi Rizzo  *
207068b8534bSLuigi Rizzo  * Free all the memory, and destroy the ``/dev/netmap`` device.
207168b8534bSLuigi Rizzo  */
207268b8534bSLuigi Rizzo static void
207368b8534bSLuigi Rizzo netmap_fini(void)
207468b8534bSLuigi Rizzo {
207568b8534bSLuigi Rizzo 	destroy_dev(netmap_dev);
207668b8534bSLuigi Rizzo 	netmap_memory_fini();
207768b8534bSLuigi Rizzo 	printf("netmap: unloaded module.\n");
207868b8534bSLuigi Rizzo }
207968b8534bSLuigi Rizzo 
208068b8534bSLuigi Rizzo 
2081f196ce38SLuigi Rizzo #ifdef __FreeBSD__
208268b8534bSLuigi Rizzo /*
208368b8534bSLuigi Rizzo  * Kernel entry point.
208468b8534bSLuigi Rizzo  *
208568b8534bSLuigi Rizzo  * Initialize/finalize the module and return.
208668b8534bSLuigi Rizzo  *
208768b8534bSLuigi Rizzo  * Return 0 on success, errno on failure.
208868b8534bSLuigi Rizzo  */
208968b8534bSLuigi Rizzo static int
209068b8534bSLuigi Rizzo netmap_loader(__unused struct module *module, int event, __unused void *arg)
209168b8534bSLuigi Rizzo {
209268b8534bSLuigi Rizzo 	int error = 0;
209368b8534bSLuigi Rizzo 
209468b8534bSLuigi Rizzo 	switch (event) {
209568b8534bSLuigi Rizzo 	case MOD_LOAD:
209668b8534bSLuigi Rizzo 		error = netmap_init();
209768b8534bSLuigi Rizzo 		break;
209868b8534bSLuigi Rizzo 
209968b8534bSLuigi Rizzo 	case MOD_UNLOAD:
210068b8534bSLuigi Rizzo 		netmap_fini();
210168b8534bSLuigi Rizzo 		break;
210268b8534bSLuigi Rizzo 
210368b8534bSLuigi Rizzo 	default:
210468b8534bSLuigi Rizzo 		error = EOPNOTSUPP;
210568b8534bSLuigi Rizzo 		break;
210668b8534bSLuigi Rizzo 	}
210768b8534bSLuigi Rizzo 
210868b8534bSLuigi Rizzo 	return (error);
210968b8534bSLuigi Rizzo }
211068b8534bSLuigi Rizzo 
211168b8534bSLuigi Rizzo 
211268b8534bSLuigi Rizzo DEV_MODULE(netmap, netmap_loader, NULL);
2113f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */
2114