xref: /freebsd/sys/dev/netmap/netmap.c (revision 849bec0e760b99364545f0b4d570ed60767a1475)
168b8534bSLuigi Rizzo /*
2*849bec0eSLuigi Rizzo  * Copyright (C) 2011-2013 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>
8489f6b863SAttilio Rao #include <sys/rwlock.h>
8568b8534bSLuigi Rizzo #include <vm/vm.h>	/* vtophys */
8668b8534bSLuigi Rizzo #include <vm/pmap.h>	/* vtophys */
8768b8534bSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
8868b8534bSLuigi Rizzo #include <machine/bus.h>
8968b8534bSLuigi Rizzo #include <sys/selinfo.h>
9068b8534bSLuigi Rizzo #include <sys/sysctl.h>
9168b8534bSLuigi Rizzo #include <net/if.h>
9268b8534bSLuigi Rizzo #include <net/bpf.h>		/* BIOCIMMEDIATE */
93506cc70cSLuigi Rizzo #include <net/vnet.h>
9468b8534bSLuigi Rizzo #include <machine/bus.h>	/* bus_dmamap_* */
9568b8534bSLuigi Rizzo 
9668b8534bSLuigi Rizzo MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map");
97f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */
9868b8534bSLuigi Rizzo 
990b8ed8e0SLuigi Rizzo #include <net/netmap.h>
1000b8ed8e0SLuigi Rizzo #include <dev/netmap/netmap_kern.h>
1010b8ed8e0SLuigi Rizzo 
102d4b42e08SLuigi Rizzo /* XXX the following variables must be deprecated and included in nm_mem */
1035819da83SLuigi Rizzo u_int netmap_total_buffers;
1048241616dSLuigi Rizzo u_int netmap_buf_size;
1055819da83SLuigi Rizzo char *netmap_buffer_base;	/* address of an invalid buffer */
1065819da83SLuigi Rizzo 
1075819da83SLuigi Rizzo /* user-controlled variables */
1085819da83SLuigi Rizzo int netmap_verbose;
1095819da83SLuigi Rizzo 
1105819da83SLuigi Rizzo static int netmap_no_timestamp; /* don't timestamp on rxsync */
1115819da83SLuigi Rizzo 
1125819da83SLuigi Rizzo SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args");
1135819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose,
1145819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode");
1155819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp,
1165819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp");
1175819da83SLuigi Rizzo int netmap_mitigate = 1;
1185819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mitigate, CTLFLAG_RW, &netmap_mitigate, 0, "");
119c85cb1a0SLuigi Rizzo int netmap_no_pendintr = 1;
1205819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr,
1215819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_pendintr, 0, "Always look for new received packets.");
1225819da83SLuigi Rizzo 
123f196ce38SLuigi Rizzo int netmap_drop = 0;	/* debugging */
124f196ce38SLuigi Rizzo int netmap_flags = 0;	/* debug flags */
125091fd0abSLuigi Rizzo int netmap_fwd = 0;	/* force transparent mode */
126f196ce38SLuigi Rizzo 
127f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, drop, CTLFLAG_RW, &netmap_drop, 0 , "");
128f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, flags, CTLFLAG_RW, &netmap_flags, 0 , "");
129091fd0abSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, fwd, CTLFLAG_RW, &netmap_fwd, 0 , "");
130f196ce38SLuigi Rizzo 
131f196ce38SLuigi Rizzo #ifdef NM_BRIDGE /* support for netmap bridge */
132f196ce38SLuigi Rizzo 
133f196ce38SLuigi Rizzo /*
134f196ce38SLuigi Rizzo  * system parameters.
135f196ce38SLuigi Rizzo  *
136f196ce38SLuigi Rizzo  * All switched ports have prefix NM_NAME.
137f196ce38SLuigi Rizzo  * The switch has a max of NM_BDG_MAXPORTS ports (often stored in a bitmap,
138f196ce38SLuigi Rizzo  * so a practical upper bound is 64).
139f196ce38SLuigi Rizzo  * Each tx ring is read-write, whereas rx rings are readonly (XXX not done yet).
140f196ce38SLuigi Rizzo  * The virtual interfaces use per-queue lock instead of core lock.
141f196ce38SLuigi Rizzo  * In the tx loop, we aggregate traffic in batches to make all operations
142f196ce38SLuigi Rizzo  * faster. The batch size is NM_BDG_BATCH
143f196ce38SLuigi Rizzo  */
144f196ce38SLuigi Rizzo #define	NM_NAME			"vale"	/* prefix for the interface */
145f196ce38SLuigi Rizzo #define NM_BDG_MAXPORTS		16	/* up to 64 ? */
146f196ce38SLuigi Rizzo #define NM_BRIDGE_RINGSIZE	1024	/* in the device */
147f196ce38SLuigi Rizzo #define NM_BDG_HASH		1024	/* forwarding table entries */
148f196ce38SLuigi Rizzo #define NM_BDG_BATCH		1024	/* entries in the forwarding buffer */
149f196ce38SLuigi Rizzo #define	NM_BRIDGES		4	/* number of bridges */
150d4b42e08SLuigi Rizzo 
151d4b42e08SLuigi Rizzo 
152f196ce38SLuigi Rizzo int netmap_bridge = NM_BDG_BATCH; /* bridge batch size */
153f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, bridge, CTLFLAG_RW, &netmap_bridge, 0 , "");
15401c7d25fSLuigi Rizzo 
155f196ce38SLuigi Rizzo #ifdef linux
156*849bec0eSLuigi Rizzo 
157*849bec0eSLuigi Rizzo #define	refcount_acquire(_a)	atomic_add(1, (atomic_t *)_a)
158*849bec0eSLuigi Rizzo #define	refcount_release(_a)	atomic_dec_and_test((atomic_t *)_a)
159*849bec0eSLuigi Rizzo 
160f196ce38SLuigi Rizzo #else /* !linux */
161*849bec0eSLuigi Rizzo 
162f196ce38SLuigi Rizzo #ifdef __FreeBSD__
163f196ce38SLuigi Rizzo #include <sys/endian.h>
164f196ce38SLuigi Rizzo #include <sys/refcount.h>
165f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */
166*849bec0eSLuigi Rizzo 
16701c7d25fSLuigi Rizzo #define prefetch(x)	__builtin_prefetch(x)
168*849bec0eSLuigi Rizzo 
169f196ce38SLuigi Rizzo #endif /* !linux */
170f196ce38SLuigi Rizzo 
171*849bec0eSLuigi Rizzo /*
172*849bec0eSLuigi Rizzo  * These are used to handle reference counters for bridge ports.
173*849bec0eSLuigi Rizzo  */
174*849bec0eSLuigi Rizzo #define	ADD_BDG_REF(ifp)	refcount_acquire(&NA(ifp)->na_bdg_refcount)
175*849bec0eSLuigi Rizzo #define	DROP_BDG_REF(ifp)	refcount_release(&NA(ifp)->na_bdg_refcount)
176*849bec0eSLuigi Rizzo 
177f196ce38SLuigi Rizzo static void bdg_netmap_attach(struct ifnet *ifp);
178f196ce38SLuigi Rizzo static int bdg_netmap_reg(struct ifnet *ifp, int onoff);
179f196ce38SLuigi Rizzo /* per-tx-queue entry */
180f196ce38SLuigi Rizzo struct nm_bdg_fwd {	/* forwarding entry for a bridge */
181f196ce38SLuigi Rizzo 	void *buf;
182f196ce38SLuigi Rizzo 	uint64_t dst;	/* dst mask */
183f196ce38SLuigi Rizzo 	uint32_t src;	/* src index ? */
184f196ce38SLuigi Rizzo 	uint16_t len;	/* src len */
185f196ce38SLuigi Rizzo };
186f196ce38SLuigi Rizzo 
187f196ce38SLuigi Rizzo struct nm_hash_ent {
188f196ce38SLuigi Rizzo 	uint64_t	mac;	/* the top 2 bytes are the epoch */
189f196ce38SLuigi Rizzo 	uint64_t	ports;
190f196ce38SLuigi Rizzo };
191f196ce38SLuigi Rizzo 
192f196ce38SLuigi Rizzo /*
193*849bec0eSLuigi Rizzo  * Interfaces for a bridge are all in bdg_ports[].
194f196ce38SLuigi Rizzo  * The array has fixed size, an empty entry does not terminate
195*849bec0eSLuigi Rizzo  * the search. But lookups only occur on attach/detach so we
196*849bec0eSLuigi Rizzo  * don't mind if they are slow.
197*849bec0eSLuigi Rizzo  *
198*849bec0eSLuigi Rizzo  * The bridge is non blocking on the transmit ports.
199*849bec0eSLuigi Rizzo  *
200*849bec0eSLuigi Rizzo  * bdg_lock protects accesses to the bdg_ports array.
201f196ce38SLuigi Rizzo  */
202f196ce38SLuigi Rizzo struct nm_bridge {
203f196ce38SLuigi Rizzo 	struct ifnet *bdg_ports[NM_BDG_MAXPORTS];
204f196ce38SLuigi Rizzo 	int n_ports;
205f196ce38SLuigi Rizzo 	uint64_t act_ports;
206f196ce38SLuigi Rizzo 	int freelist;	/* first buffer index */
207f196ce38SLuigi Rizzo 	NM_SELINFO_T si;	/* poll/select wait queue */
208f196ce38SLuigi Rizzo 	NM_LOCK_T bdg_lock;	/* protect the selinfo ? */
209f196ce38SLuigi Rizzo 
210f196ce38SLuigi Rizzo 	/* the forwarding table, MAC+ports */
211f196ce38SLuigi Rizzo 	struct nm_hash_ent ht[NM_BDG_HASH];
212f196ce38SLuigi Rizzo 
213f196ce38SLuigi Rizzo 	int namelen;	/* 0 means free */
214f196ce38SLuigi Rizzo 	char basename[IFNAMSIZ];
215f196ce38SLuigi Rizzo };
216f196ce38SLuigi Rizzo 
217f196ce38SLuigi Rizzo struct nm_bridge nm_bridges[NM_BRIDGES];
218f196ce38SLuigi Rizzo 
219f196ce38SLuigi Rizzo #define BDG_LOCK(b)	mtx_lock(&(b)->bdg_lock)
220f196ce38SLuigi Rizzo #define BDG_UNLOCK(b)	mtx_unlock(&(b)->bdg_lock)
221f196ce38SLuigi Rizzo 
222f196ce38SLuigi Rizzo /*
223f196ce38SLuigi Rizzo  * NA(ifp)->bdg_port	port index
224f196ce38SLuigi Rizzo  */
225f196ce38SLuigi Rizzo 
226f196ce38SLuigi Rizzo // XXX only for multiples of 64 bytes, non overlapped.
227f196ce38SLuigi Rizzo static inline void
228f196ce38SLuigi Rizzo pkt_copy(void *_src, void *_dst, int l)
229f196ce38SLuigi Rizzo {
230f196ce38SLuigi Rizzo         uint64_t *src = _src;
231f196ce38SLuigi Rizzo         uint64_t *dst = _dst;
232f196ce38SLuigi Rizzo         if (unlikely(l >= 1024)) {
233f196ce38SLuigi Rizzo                 bcopy(src, dst, l);
234f196ce38SLuigi Rizzo                 return;
235f196ce38SLuigi Rizzo         }
236f196ce38SLuigi Rizzo         for (; likely(l > 0); l-=64) {
237f196ce38SLuigi Rizzo                 *dst++ = *src++;
238f196ce38SLuigi Rizzo                 *dst++ = *src++;
239f196ce38SLuigi Rizzo                 *dst++ = *src++;
240f196ce38SLuigi Rizzo                 *dst++ = *src++;
241f196ce38SLuigi Rizzo                 *dst++ = *src++;
242f196ce38SLuigi Rizzo                 *dst++ = *src++;
243f196ce38SLuigi Rizzo                 *dst++ = *src++;
244f196ce38SLuigi Rizzo                 *dst++ = *src++;
245f196ce38SLuigi Rizzo         }
246f196ce38SLuigi Rizzo }
247f196ce38SLuigi Rizzo 
248f196ce38SLuigi Rizzo /*
249f196ce38SLuigi Rizzo  * locate a bridge among the existing ones.
250f196ce38SLuigi Rizzo  * a ':' in the name terminates the bridge name. Otherwise, just NM_NAME.
251f196ce38SLuigi Rizzo  * We assume that this is called with a name of at least NM_NAME chars.
252f196ce38SLuigi Rizzo  */
253f196ce38SLuigi Rizzo static struct nm_bridge *
254f196ce38SLuigi Rizzo nm_find_bridge(const char *name)
255f196ce38SLuigi Rizzo {
256f196ce38SLuigi Rizzo 	int i, l, namelen, e;
257f196ce38SLuigi Rizzo 	struct nm_bridge *b = NULL;
258f196ce38SLuigi Rizzo 
259f196ce38SLuigi Rizzo 	namelen = strlen(NM_NAME);	/* base length */
260f196ce38SLuigi Rizzo 	l = strlen(name);		/* actual length */
261f196ce38SLuigi Rizzo 	for (i = namelen + 1; i < l; i++) {
262f196ce38SLuigi Rizzo 		if (name[i] == ':') {
263f196ce38SLuigi Rizzo 			namelen = i;
264f196ce38SLuigi Rizzo 			break;
265f196ce38SLuigi Rizzo 		}
266f196ce38SLuigi Rizzo 	}
267f196ce38SLuigi Rizzo 	if (namelen >= IFNAMSIZ)
268f196ce38SLuigi Rizzo 		namelen = IFNAMSIZ;
269f196ce38SLuigi Rizzo 	ND("--- prefix is '%.*s' ---", namelen, name);
270f196ce38SLuigi Rizzo 
271f196ce38SLuigi Rizzo 	/* use the first entry for locking */
272f196ce38SLuigi Rizzo 	BDG_LOCK(nm_bridges); // XXX do better
273f196ce38SLuigi Rizzo 	for (e = -1, i = 1; i < NM_BRIDGES; i++) {
274f196ce38SLuigi Rizzo 		b = nm_bridges + i;
275f196ce38SLuigi Rizzo 		if (b->namelen == 0)
276f196ce38SLuigi Rizzo 			e = i;	/* record empty slot */
277f196ce38SLuigi Rizzo 		else if (strncmp(name, b->basename, namelen) == 0) {
278f196ce38SLuigi Rizzo 			ND("found '%.*s' at %d", namelen, name, i);
279f196ce38SLuigi Rizzo 			break;
280f196ce38SLuigi Rizzo 		}
281f196ce38SLuigi Rizzo 	}
282f196ce38SLuigi Rizzo 	if (i == NM_BRIDGES) { /* all full */
283f196ce38SLuigi Rizzo 		if (e == -1) { /* no empty slot */
284f196ce38SLuigi Rizzo 			b = NULL;
285f196ce38SLuigi Rizzo 		} else {
286f196ce38SLuigi Rizzo 			b = nm_bridges + e;
287f196ce38SLuigi Rizzo 			strncpy(b->basename, name, namelen);
288f196ce38SLuigi Rizzo 			b->namelen = namelen;
289f196ce38SLuigi Rizzo 		}
290f196ce38SLuigi Rizzo 	}
291f196ce38SLuigi Rizzo 	BDG_UNLOCK(nm_bridges);
292f196ce38SLuigi Rizzo 	return b;
293f196ce38SLuigi Rizzo }
294f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */
2955819da83SLuigi Rizzo 
296ae10d1afSLuigi Rizzo 
297ae10d1afSLuigi Rizzo /*
298ae10d1afSLuigi Rizzo  * Fetch configuration from the device, to cope with dynamic
299ae10d1afSLuigi Rizzo  * reconfigurations after loading the module.
300ae10d1afSLuigi Rizzo  */
301ae10d1afSLuigi Rizzo static int
302ae10d1afSLuigi Rizzo netmap_update_config(struct netmap_adapter *na)
303ae10d1afSLuigi Rizzo {
304ae10d1afSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
305ae10d1afSLuigi Rizzo 	u_int txr, txd, rxr, rxd;
306ae10d1afSLuigi Rizzo 
307ae10d1afSLuigi Rizzo 	txr = txd = rxr = rxd = 0;
308ae10d1afSLuigi Rizzo 	if (na->nm_config) {
309ae10d1afSLuigi Rizzo 		na->nm_config(ifp, &txr, &txd, &rxr, &rxd);
310ae10d1afSLuigi Rizzo 	} else {
311ae10d1afSLuigi Rizzo 		/* take whatever we had at init time */
312ae10d1afSLuigi Rizzo 		txr = na->num_tx_rings;
313ae10d1afSLuigi Rizzo 		txd = na->num_tx_desc;
314ae10d1afSLuigi Rizzo 		rxr = na->num_rx_rings;
315ae10d1afSLuigi Rizzo 		rxd = na->num_rx_desc;
316ae10d1afSLuigi Rizzo 	}
317ae10d1afSLuigi Rizzo 
318ae10d1afSLuigi Rizzo 	if (na->num_tx_rings == txr && na->num_tx_desc == txd &&
319ae10d1afSLuigi Rizzo 	    na->num_rx_rings == rxr && na->num_rx_desc == rxd)
320ae10d1afSLuigi Rizzo 		return 0; /* nothing changed */
321ae10d1afSLuigi Rizzo 	if (netmap_verbose || na->refcount > 0) {
322ae10d1afSLuigi Rizzo 		D("stored config %s: txring %d x %d, rxring %d x %d",
323ae10d1afSLuigi Rizzo 			ifp->if_xname,
324ae10d1afSLuigi Rizzo 			na->num_tx_rings, na->num_tx_desc,
325ae10d1afSLuigi Rizzo 			na->num_rx_rings, na->num_rx_desc);
326ae10d1afSLuigi Rizzo 		D("new config %s: txring %d x %d, rxring %d x %d",
327ae10d1afSLuigi Rizzo 			ifp->if_xname, txr, txd, rxr, rxd);
328ae10d1afSLuigi Rizzo 	}
329ae10d1afSLuigi Rizzo 	if (na->refcount == 0) {
330ae10d1afSLuigi Rizzo 		D("configuration changed (but fine)");
331ae10d1afSLuigi Rizzo 		na->num_tx_rings = txr;
332ae10d1afSLuigi Rizzo 		na->num_tx_desc = txd;
333ae10d1afSLuigi Rizzo 		na->num_rx_rings = rxr;
334ae10d1afSLuigi Rizzo 		na->num_rx_desc = rxd;
335ae10d1afSLuigi Rizzo 		return 0;
336ae10d1afSLuigi Rizzo 	}
337ae10d1afSLuigi Rizzo 	D("configuration changed while active, this is bad...");
338ae10d1afSLuigi Rizzo 	return 1;
339ae10d1afSLuigi Rizzo }
340ae10d1afSLuigi Rizzo 
3413c0caf6cSLuigi Rizzo /*------------- memory allocator -----------------*/
3423c0caf6cSLuigi Rizzo #include "netmap_mem2.c"
3433c0caf6cSLuigi Rizzo /*------------ end of memory allocator ----------*/
34468b8534bSLuigi Rizzo 
3458241616dSLuigi Rizzo 
3468241616dSLuigi Rizzo /* Structure associated to each thread which registered an interface.
3478241616dSLuigi Rizzo  *
3488241616dSLuigi Rizzo  * The first 4 fields of this structure are written by NIOCREGIF and
3498241616dSLuigi Rizzo  * read by poll() and NIOC?XSYNC.
3508241616dSLuigi Rizzo  * There is low contention among writers (actually, a correct user program
3518241616dSLuigi Rizzo  * should have no contention among writers) and among writers and readers,
3528241616dSLuigi Rizzo  * so we use a single global lock to protect the structure initialization.
3538241616dSLuigi Rizzo  * Since initialization involves the allocation of memory, we reuse the memory
3548241616dSLuigi Rizzo  * allocator lock.
3558241616dSLuigi Rizzo  * Read access to the structure is lock free. Readers must check that
3568241616dSLuigi Rizzo  * np_nifp is not NULL before using the other fields.
3578241616dSLuigi Rizzo  * If np_nifp is NULL initialization has not been performed, so they should
3588241616dSLuigi Rizzo  * return an error to userlevel.
3598241616dSLuigi Rizzo  *
3608241616dSLuigi Rizzo  * The ref_done field is used to regulate access to the refcount in the
3618241616dSLuigi Rizzo  * memory allocator. The refcount must be incremented at most once for
3628241616dSLuigi Rizzo  * each open("/dev/netmap"). The increment is performed by the first
3638241616dSLuigi Rizzo  * function that calls netmap_get_memory() (currently called by
3648241616dSLuigi Rizzo  * mmap(), NIOCGINFO and NIOCREGIF).
3658241616dSLuigi Rizzo  * If the refcount is incremented, it is then decremented when the
3668241616dSLuigi Rizzo  * private structure is destroyed.
3678241616dSLuigi Rizzo  */
36868b8534bSLuigi Rizzo struct netmap_priv_d {
3698241616dSLuigi Rizzo 	struct netmap_if * volatile np_nifp;	/* netmap interface descriptor. */
37068b8534bSLuigi Rizzo 
37168b8534bSLuigi Rizzo 	struct ifnet	*np_ifp;	/* device for which we hold a reference */
37268b8534bSLuigi Rizzo 	int		np_ringid;	/* from the ioctl */
37368b8534bSLuigi Rizzo 	u_int		np_qfirst, np_qlast;	/* range of rings to scan */
37468b8534bSLuigi Rizzo 	uint16_t	np_txpoll;
3758241616dSLuigi Rizzo 
3768241616dSLuigi Rizzo 	unsigned long	ref_done;	/* use with NMA_LOCK held */
37768b8534bSLuigi Rizzo };
37868b8534bSLuigi Rizzo 
37968b8534bSLuigi Rizzo 
3808241616dSLuigi Rizzo static int
3818241616dSLuigi Rizzo netmap_get_memory(struct netmap_priv_d* p)
3828241616dSLuigi Rizzo {
3838241616dSLuigi Rizzo 	int error = 0;
3848241616dSLuigi Rizzo 	NMA_LOCK();
3858241616dSLuigi Rizzo 	if (!p->ref_done) {
3868241616dSLuigi Rizzo 		error = netmap_memory_finalize();
3878241616dSLuigi Rizzo 		if (!error)
3888241616dSLuigi Rizzo 			p->ref_done = 1;
3898241616dSLuigi Rizzo 	}
3908241616dSLuigi Rizzo 	NMA_UNLOCK();
3918241616dSLuigi Rizzo 	return error;
3928241616dSLuigi Rizzo }
3938241616dSLuigi Rizzo 
39468b8534bSLuigi Rizzo /*
39568b8534bSLuigi Rizzo  * File descriptor's private data destructor.
39668b8534bSLuigi Rizzo  *
39768b8534bSLuigi Rizzo  * Call nm_register(ifp,0) to stop netmap mode on the interface and
39868b8534bSLuigi Rizzo  * revert to normal operation. We expect that np_ifp has not gone.
39968b8534bSLuigi Rizzo  */
4008241616dSLuigi Rizzo /* call with NMA_LOCK held */
40168b8534bSLuigi Rizzo static void
4025819da83SLuigi Rizzo netmap_dtor_locked(void *data)
40368b8534bSLuigi Rizzo {
40468b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = data;
40568b8534bSLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
40668b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
40768b8534bSLuigi Rizzo 	struct netmap_if *nifp = priv->np_nifp;
40868b8534bSLuigi Rizzo 
40968b8534bSLuigi Rizzo 	na->refcount--;
41068b8534bSLuigi Rizzo 	if (na->refcount <= 0) {	/* last instance */
41164ae02c3SLuigi Rizzo 		u_int i, j, lim;
41268b8534bSLuigi Rizzo 
413ae10d1afSLuigi Rizzo 		if (netmap_verbose)
414ae10d1afSLuigi Rizzo 			D("deleting last instance for %s", ifp->if_xname);
41568b8534bSLuigi Rizzo 		/*
41668b8534bSLuigi Rizzo 		 * there is a race here with *_netmap_task() and
4171a26580eSLuigi Rizzo 		 * netmap_poll(), which don't run under NETMAP_REG_LOCK.
41868b8534bSLuigi Rizzo 		 * na->refcount == 0 && na->ifp->if_capenable & IFCAP_NETMAP
41968b8534bSLuigi Rizzo 		 * (aka NETMAP_DELETING(na)) are a unique marker that the
42068b8534bSLuigi Rizzo 		 * device is dying.
42168b8534bSLuigi Rizzo 		 * Before destroying stuff we sleep a bit, and then complete
42268b8534bSLuigi Rizzo 		 * the job. NIOCREG should realize the condition and
42368b8534bSLuigi Rizzo 		 * loop until they can continue; the other routines
42468b8534bSLuigi Rizzo 		 * should check the condition at entry and quit if
42568b8534bSLuigi Rizzo 		 * they cannot run.
42668b8534bSLuigi Rizzo 		 */
4271a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
42868b8534bSLuigi Rizzo 		tsleep(na, 0, "NIOCUNREG", 4);
4291a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
43068b8534bSLuigi Rizzo 		na->nm_register(ifp, 0); /* off, clear IFCAP_NETMAP */
43168b8534bSLuigi Rizzo 		/* Wake up any sleeping threads. netmap_poll will
43268b8534bSLuigi Rizzo 		 * then return POLLERR
43368b8534bSLuigi Rizzo 		 */
434d76bf4ffSLuigi Rizzo 		for (i = 0; i < na->num_tx_rings + 1; i++)
43568b8534bSLuigi Rizzo 			selwakeuppri(&na->tx_rings[i].si, PI_NET);
436d76bf4ffSLuigi Rizzo 		for (i = 0; i < na->num_rx_rings + 1; i++)
43768b8534bSLuigi Rizzo 			selwakeuppri(&na->rx_rings[i].si, PI_NET);
43864ae02c3SLuigi Rizzo 		selwakeuppri(&na->tx_si, PI_NET);
43964ae02c3SLuigi Rizzo 		selwakeuppri(&na->rx_si, PI_NET);
44068b8534bSLuigi Rizzo 		/* release all buffers */
441d76bf4ffSLuigi Rizzo 		for (i = 0; i < na->num_tx_rings + 1; i++) {
44264ae02c3SLuigi Rizzo 			struct netmap_ring *ring = na->tx_rings[i].ring;
44368b8534bSLuigi Rizzo 			lim = na->tx_rings[i].nkr_num_slots;
44468b8534bSLuigi Rizzo 			for (j = 0; j < lim; j++)
445446ee301SLuigi Rizzo 				netmap_free_buf(nifp, ring->slot[j].buf_idx);
4462f70fca5SEd Maste 			/* knlist_destroy(&na->tx_rings[i].si.si_note); */
4472f70fca5SEd Maste 			mtx_destroy(&na->tx_rings[i].q_lock);
44864ae02c3SLuigi Rizzo 		}
449d76bf4ffSLuigi Rizzo 		for (i = 0; i < na->num_rx_rings + 1; i++) {
45064ae02c3SLuigi Rizzo 			struct netmap_ring *ring = na->rx_rings[i].ring;
45168b8534bSLuigi Rizzo 			lim = na->rx_rings[i].nkr_num_slots;
45268b8534bSLuigi Rizzo 			for (j = 0; j < lim; j++)
453446ee301SLuigi Rizzo 				netmap_free_buf(nifp, ring->slot[j].buf_idx);
4542f70fca5SEd Maste 			/* knlist_destroy(&na->rx_rings[i].si.si_note); */
4552f70fca5SEd Maste 			mtx_destroy(&na->rx_rings[i].q_lock);
45668b8534bSLuigi Rizzo 		}
4572f70fca5SEd Maste 		/* XXX kqueue(9) needed; these will mirror knlist_init. */
4582f70fca5SEd Maste 		/* knlist_destroy(&na->tx_si.si_note); */
4592f70fca5SEd Maste 		/* knlist_destroy(&na->rx_si.si_note); */
4606e10c8b8SLuigi Rizzo 		netmap_free_rings(na);
46168b8534bSLuigi Rizzo 		wakeup(na);
46268b8534bSLuigi Rizzo 	}
4636e10c8b8SLuigi Rizzo 	netmap_if_free(nifp);
4645819da83SLuigi Rizzo }
46568b8534bSLuigi Rizzo 
466f196ce38SLuigi Rizzo static void
467f196ce38SLuigi Rizzo nm_if_rele(struct ifnet *ifp)
468f196ce38SLuigi Rizzo {
469f196ce38SLuigi Rizzo #ifndef NM_BRIDGE
470f196ce38SLuigi Rizzo 	if_rele(ifp);
471f196ce38SLuigi Rizzo #else /* NM_BRIDGE */
472f196ce38SLuigi Rizzo 	int i, full;
473f196ce38SLuigi Rizzo 	struct nm_bridge *b;
474f196ce38SLuigi Rizzo 
475f196ce38SLuigi Rizzo 	if (strncmp(ifp->if_xname, NM_NAME, sizeof(NM_NAME) - 1)) {
476f196ce38SLuigi Rizzo 		if_rele(ifp);
477f196ce38SLuigi Rizzo 		return;
478f196ce38SLuigi Rizzo 	}
479f196ce38SLuigi Rizzo 	if (!DROP_BDG_REF(ifp))
480f196ce38SLuigi Rizzo 		return;
481f196ce38SLuigi Rizzo 	b = ifp->if_bridge;
482f196ce38SLuigi Rizzo 	BDG_LOCK(nm_bridges);
483f196ce38SLuigi Rizzo 	BDG_LOCK(b);
484f196ce38SLuigi Rizzo 	ND("want to disconnect %s from the bridge", ifp->if_xname);
485f196ce38SLuigi Rizzo 	full = 0;
486f196ce38SLuigi Rizzo 	for (i = 0; i < NM_BDG_MAXPORTS; i++) {
487f196ce38SLuigi Rizzo 		if (b->bdg_ports[i] == ifp) {
488f196ce38SLuigi Rizzo 			b->bdg_ports[i] = NULL;
489f196ce38SLuigi Rizzo 			bzero(ifp, sizeof(*ifp));
490f196ce38SLuigi Rizzo 			free(ifp, M_DEVBUF);
491f196ce38SLuigi Rizzo 			break;
492f196ce38SLuigi Rizzo 		}
493f196ce38SLuigi Rizzo 		else if (b->bdg_ports[i] != NULL)
494f196ce38SLuigi Rizzo 			full = 1;
495f196ce38SLuigi Rizzo 	}
496f196ce38SLuigi Rizzo 	BDG_UNLOCK(b);
497f196ce38SLuigi Rizzo 	if (full == 0) {
498f196ce38SLuigi Rizzo 		ND("freeing bridge %d", b - nm_bridges);
499f196ce38SLuigi Rizzo 		b->namelen = 0;
500f196ce38SLuigi Rizzo 	}
501f196ce38SLuigi Rizzo 	BDG_UNLOCK(nm_bridges);
502f196ce38SLuigi Rizzo 	if (i == NM_BDG_MAXPORTS)
503f196ce38SLuigi Rizzo 		D("ouch, cannot find ifp to remove");
504f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */
505f196ce38SLuigi Rizzo }
5065819da83SLuigi Rizzo 
5075819da83SLuigi Rizzo static void
5085819da83SLuigi Rizzo netmap_dtor(void *data)
5095819da83SLuigi Rizzo {
5105819da83SLuigi Rizzo 	struct netmap_priv_d *priv = data;
5115819da83SLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
5125819da83SLuigi Rizzo 
5138241616dSLuigi Rizzo 	NMA_LOCK();
5148241616dSLuigi Rizzo 	if (ifp) {
5152579e2d7SLuigi Rizzo 		struct netmap_adapter *na = NA(ifp);
5162579e2d7SLuigi Rizzo 
5171a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
5185819da83SLuigi Rizzo 		netmap_dtor_locked(data);
5191a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
52068b8534bSLuigi Rizzo 
5212579e2d7SLuigi Rizzo 		nm_if_rele(ifp); /* might also destroy *na */
5228241616dSLuigi Rizzo 	}
5238241616dSLuigi Rizzo 	if (priv->ref_done) {
5248241616dSLuigi Rizzo 		netmap_memory_deref();
5258241616dSLuigi Rizzo 	}
5268241616dSLuigi Rizzo 	NMA_UNLOCK();
52768b8534bSLuigi Rizzo 	bzero(priv, sizeof(*priv));	/* XXX for safety */
52868b8534bSLuigi Rizzo 	free(priv, M_DEVBUF);
52968b8534bSLuigi Rizzo }
53068b8534bSLuigi Rizzo 
5318241616dSLuigi Rizzo #ifdef __FreeBSD__
5328241616dSLuigi Rizzo #include <vm/vm.h>
5338241616dSLuigi Rizzo #include <vm/vm_param.h>
5348241616dSLuigi Rizzo #include <vm/vm_object.h>
5358241616dSLuigi Rizzo #include <vm/vm_page.h>
5368241616dSLuigi Rizzo #include <vm/vm_pager.h>
5378241616dSLuigi Rizzo #include <vm/uma.h>
5388241616dSLuigi Rizzo 
5398241616dSLuigi Rizzo static struct cdev_pager_ops saved_cdev_pager_ops;
5408241616dSLuigi Rizzo 
5418241616dSLuigi Rizzo static int
5428241616dSLuigi Rizzo netmap_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
5438241616dSLuigi Rizzo     vm_ooffset_t foff, struct ucred *cred, u_short *color)
5448241616dSLuigi Rizzo {
545ae10d1afSLuigi Rizzo 	if (netmap_verbose)
5468241616dSLuigi Rizzo 		D("first mmap for %p", handle);
5478241616dSLuigi Rizzo 	return saved_cdev_pager_ops.cdev_pg_ctor(handle,
5488241616dSLuigi Rizzo 			size, prot, foff, cred, color);
5498241616dSLuigi Rizzo }
5508241616dSLuigi Rizzo 
5518241616dSLuigi Rizzo static void
5528241616dSLuigi Rizzo netmap_dev_pager_dtor(void *handle)
5538241616dSLuigi Rizzo {
5548241616dSLuigi Rizzo 	saved_cdev_pager_ops.cdev_pg_dtor(handle);
555ae10d1afSLuigi Rizzo 	ND("ready to release memory for %p", handle);
5568241616dSLuigi Rizzo }
5578241616dSLuigi Rizzo 
5588241616dSLuigi Rizzo 
5598241616dSLuigi Rizzo static struct cdev_pager_ops netmap_cdev_pager_ops = {
5608241616dSLuigi Rizzo         .cdev_pg_ctor = netmap_dev_pager_ctor,
5618241616dSLuigi Rizzo         .cdev_pg_dtor = netmap_dev_pager_dtor,
5628241616dSLuigi Rizzo         .cdev_pg_fault = NULL,
5638241616dSLuigi Rizzo };
5648241616dSLuigi Rizzo 
5658241616dSLuigi Rizzo static int
5668241616dSLuigi Rizzo netmap_mmap_single(struct cdev *cdev, vm_ooffset_t *foff,
5678241616dSLuigi Rizzo 	vm_size_t objsize,  vm_object_t *objp, int prot)
5688241616dSLuigi Rizzo {
5698241616dSLuigi Rizzo 	vm_object_t obj;
5708241616dSLuigi Rizzo 
571ae10d1afSLuigi Rizzo 	ND("cdev %p foff %jd size %jd objp %p prot %d", cdev,
57288f79057SGleb Smirnoff 	    (intmax_t )*foff, (intmax_t )objsize, objp, prot);
5738241616dSLuigi Rizzo 	obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
5748241616dSLuigi Rizzo             curthread->td_ucred);
5758241616dSLuigi Rizzo 	ND("returns obj %p", obj);
5768241616dSLuigi Rizzo 	if (obj == NULL)
5778241616dSLuigi Rizzo 		return EINVAL;
5788241616dSLuigi Rizzo 	if (saved_cdev_pager_ops.cdev_pg_fault == NULL) {
579ae10d1afSLuigi Rizzo 		ND("initialize cdev_pager_ops");
5808241616dSLuigi Rizzo 		saved_cdev_pager_ops = *(obj->un_pager.devp.ops);
5818241616dSLuigi Rizzo 		netmap_cdev_pager_ops.cdev_pg_fault =
5828241616dSLuigi Rizzo 			saved_cdev_pager_ops.cdev_pg_fault;
5838241616dSLuigi Rizzo 	};
5848241616dSLuigi Rizzo 	obj->un_pager.devp.ops = &netmap_cdev_pager_ops;
5858241616dSLuigi Rizzo 	*objp = obj;
5868241616dSLuigi Rizzo 	return 0;
5878241616dSLuigi Rizzo }
5888241616dSLuigi Rizzo #endif /* __FreeBSD__ */
5898241616dSLuigi Rizzo 
59068b8534bSLuigi Rizzo 
59168b8534bSLuigi Rizzo /*
59268b8534bSLuigi Rizzo  * mmap(2) support for the "netmap" device.
59368b8534bSLuigi Rizzo  *
59468b8534bSLuigi Rizzo  * Expose all the memory previously allocated by our custom memory
59568b8534bSLuigi Rizzo  * allocator: this way the user has only to issue a single mmap(2), and
59668b8534bSLuigi Rizzo  * can work on all the data structures flawlessly.
59768b8534bSLuigi Rizzo  *
59868b8534bSLuigi Rizzo  * Return 0 on success, -1 otherwise.
59968b8534bSLuigi Rizzo  */
600babc7c12SLuigi Rizzo 
601f196ce38SLuigi Rizzo #ifdef __FreeBSD__
60268b8534bSLuigi Rizzo static int
603babc7c12SLuigi Rizzo netmap_mmap(__unused struct cdev *dev,
60468b8534bSLuigi Rizzo #if __FreeBSD_version < 900000
605babc7c12SLuigi Rizzo 		vm_offset_t offset, vm_paddr_t *paddr, int nprot
60668b8534bSLuigi Rizzo #else
607babc7c12SLuigi Rizzo 		vm_ooffset_t offset, vm_paddr_t *paddr, int nprot,
608babc7c12SLuigi Rizzo 		__unused vm_memattr_t *memattr
60968b8534bSLuigi Rizzo #endif
610babc7c12SLuigi Rizzo 	)
61168b8534bSLuigi Rizzo {
6128241616dSLuigi Rizzo 	int error = 0;
6138241616dSLuigi Rizzo 	struct netmap_priv_d *priv;
6148241616dSLuigi Rizzo 
61568b8534bSLuigi Rizzo 	if (nprot & PROT_EXEC)
61668b8534bSLuigi Rizzo 		return (-1);	// XXX -1 or EINVAL ?
617446ee301SLuigi Rizzo 
6188241616dSLuigi Rizzo 	error = devfs_get_cdevpriv((void **)&priv);
6198241616dSLuigi Rizzo 	if (error == EBADF) {	/* called on fault, memory is initialized */
6208241616dSLuigi Rizzo 		ND(5, "handling fault at ofs 0x%x", offset);
6218241616dSLuigi Rizzo 		error = 0;
6228241616dSLuigi Rizzo 	} else if (error == 0)	/* make sure memory is set */
6238241616dSLuigi Rizzo 		error = netmap_get_memory(priv);
6248241616dSLuigi Rizzo 	if (error)
6258241616dSLuigi Rizzo 		return (error);
6268241616dSLuigi Rizzo 
62768b8534bSLuigi Rizzo 	ND("request for offset 0x%x", (uint32_t)offset);
628446ee301SLuigi Rizzo 	*paddr = netmap_ofstophys(offset);
62968b8534bSLuigi Rizzo 
6308241616dSLuigi Rizzo 	return (*paddr ? 0 : ENOMEM);
6318241616dSLuigi Rizzo }
6328241616dSLuigi Rizzo 
6338241616dSLuigi Rizzo static int
6348241616dSLuigi Rizzo netmap_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
6358241616dSLuigi Rizzo {
636ae10d1afSLuigi Rizzo 	if (netmap_verbose)
637ae10d1afSLuigi Rizzo 		D("dev %p fflag 0x%x devtype %d td %p",
638ae10d1afSLuigi Rizzo 			dev, fflag, devtype, td);
6398241616dSLuigi Rizzo 	return 0;
6408241616dSLuigi Rizzo }
6418241616dSLuigi Rizzo 
6428241616dSLuigi Rizzo static int
6438241616dSLuigi Rizzo netmap_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
6448241616dSLuigi Rizzo {
6458241616dSLuigi Rizzo 	struct netmap_priv_d *priv;
6468241616dSLuigi Rizzo 	int error;
6478241616dSLuigi Rizzo 
6488241616dSLuigi Rizzo 	priv = malloc(sizeof(struct netmap_priv_d), M_DEVBUF,
6498241616dSLuigi Rizzo 			      M_NOWAIT | M_ZERO);
6508241616dSLuigi Rizzo 	if (priv == NULL)
6518241616dSLuigi Rizzo 		return ENOMEM;
6528241616dSLuigi Rizzo 
6538241616dSLuigi Rizzo 	error = devfs_set_cdevpriv(priv, netmap_dtor);
6548241616dSLuigi Rizzo 	if (error)
6558241616dSLuigi Rizzo 	        return error;
6568241616dSLuigi Rizzo 
6578241616dSLuigi Rizzo 	return 0;
65868b8534bSLuigi Rizzo }
659f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */
66068b8534bSLuigi Rizzo 
66168b8534bSLuigi Rizzo 
66268b8534bSLuigi Rizzo /*
66302ad4083SLuigi Rizzo  * Handlers for synchronization of the queues from/to the host.
664091fd0abSLuigi Rizzo  * Netmap has two operating modes:
665091fd0abSLuigi Rizzo  * - in the default mode, the rings connected to the host stack are
666091fd0abSLuigi Rizzo  *   just another ring pair managed by userspace;
667091fd0abSLuigi Rizzo  * - in transparent mode (XXX to be defined) incoming packets
668091fd0abSLuigi Rizzo  *   (from the host or the NIC) are marked as NS_FORWARD upon
669091fd0abSLuigi Rizzo  *   arrival, and the user application has a chance to reset the
670091fd0abSLuigi Rizzo  *   flag for packets that should be dropped.
671091fd0abSLuigi Rizzo  *   On the RXSYNC or poll(), packets in RX rings between
672091fd0abSLuigi Rizzo  *   kring->nr_kcur and ring->cur with NS_FORWARD still set are moved
673091fd0abSLuigi Rizzo  *   to the other side.
674091fd0abSLuigi Rizzo  * The transfer NIC --> host is relatively easy, just encapsulate
675091fd0abSLuigi Rizzo  * into mbufs and we are done. The host --> NIC side is slightly
676091fd0abSLuigi Rizzo  * harder because there might not be room in the tx ring so it
677091fd0abSLuigi Rizzo  * might take a while before releasing the buffer.
678091fd0abSLuigi Rizzo  */
679091fd0abSLuigi Rizzo 
680091fd0abSLuigi Rizzo /*
681091fd0abSLuigi Rizzo  * pass a chain of buffers to the host stack as coming from 'dst'
682091fd0abSLuigi Rizzo  */
683091fd0abSLuigi Rizzo static void
684091fd0abSLuigi Rizzo netmap_send_up(struct ifnet *dst, struct mbuf *head)
685091fd0abSLuigi Rizzo {
686091fd0abSLuigi Rizzo 	struct mbuf *m;
687091fd0abSLuigi Rizzo 
688091fd0abSLuigi Rizzo 	/* send packets up, outside the lock */
689091fd0abSLuigi Rizzo 	while ((m = head) != NULL) {
690091fd0abSLuigi Rizzo 		head = head->m_nextpkt;
691091fd0abSLuigi Rizzo 		m->m_nextpkt = NULL;
692091fd0abSLuigi Rizzo 		if (netmap_verbose & NM_VERB_HOST)
693091fd0abSLuigi Rizzo 			D("sending up pkt %p size %d", m, MBUF_LEN(m));
694091fd0abSLuigi Rizzo 		NM_SEND_UP(dst, m);
695091fd0abSLuigi Rizzo 	}
696091fd0abSLuigi Rizzo }
697091fd0abSLuigi Rizzo 
698091fd0abSLuigi Rizzo struct mbq {
699091fd0abSLuigi Rizzo 	struct mbuf *head;
700091fd0abSLuigi Rizzo 	struct mbuf *tail;
701091fd0abSLuigi Rizzo 	int count;
702091fd0abSLuigi Rizzo };
703091fd0abSLuigi Rizzo 
704091fd0abSLuigi Rizzo /*
705091fd0abSLuigi Rizzo  * put a copy of the buffers marked NS_FORWARD into an mbuf chain.
706091fd0abSLuigi Rizzo  * Run from hwcur to cur - reserved
707091fd0abSLuigi Rizzo  */
708091fd0abSLuigi Rizzo static void
709091fd0abSLuigi Rizzo netmap_grab_packets(struct netmap_kring *kring, struct mbq *q, int force)
710091fd0abSLuigi Rizzo {
711091fd0abSLuigi Rizzo 	/* Take packets from hwcur to cur-reserved and pass them up.
712091fd0abSLuigi Rizzo 	 * In case of no buffers we give up. At the end of the loop,
713091fd0abSLuigi Rizzo 	 * the queue is drained in all cases.
714091fd0abSLuigi Rizzo 	 * XXX handle reserved
715091fd0abSLuigi Rizzo 	 */
716091fd0abSLuigi Rizzo 	int k = kring->ring->cur - kring->ring->reserved;
717091fd0abSLuigi Rizzo 	u_int n, lim = kring->nkr_num_slots - 1;
718091fd0abSLuigi Rizzo 	struct mbuf *m, *tail = q->tail;
719091fd0abSLuigi Rizzo 
720091fd0abSLuigi Rizzo 	if (k < 0)
721091fd0abSLuigi Rizzo 		k = k + kring->nkr_num_slots;
722091fd0abSLuigi Rizzo 	for (n = kring->nr_hwcur; n != k;) {
723091fd0abSLuigi Rizzo 		struct netmap_slot *slot = &kring->ring->slot[n];
724091fd0abSLuigi Rizzo 
725091fd0abSLuigi Rizzo 		n = (n == lim) ? 0 : n + 1;
726091fd0abSLuigi Rizzo 		if ((slot->flags & NS_FORWARD) == 0 && !force)
727091fd0abSLuigi Rizzo 			continue;
728091fd0abSLuigi Rizzo 		if (slot->len < 14 || slot->len > NETMAP_BUF_SIZE) {
729091fd0abSLuigi Rizzo 			D("bad pkt at %d len %d", n, slot->len);
730091fd0abSLuigi Rizzo 			continue;
731091fd0abSLuigi Rizzo 		}
732091fd0abSLuigi Rizzo 		slot->flags &= ~NS_FORWARD; // XXX needed ?
733091fd0abSLuigi Rizzo 		m = m_devget(NMB(slot), slot->len, 0, kring->na->ifp, NULL);
734091fd0abSLuigi Rizzo 
735091fd0abSLuigi Rizzo 		if (m == NULL)
736091fd0abSLuigi Rizzo 			break;
737091fd0abSLuigi Rizzo 		if (tail)
738091fd0abSLuigi Rizzo 			tail->m_nextpkt = m;
739091fd0abSLuigi Rizzo 		else
740091fd0abSLuigi Rizzo 			q->head = m;
741091fd0abSLuigi Rizzo 		tail = m;
742091fd0abSLuigi Rizzo 		q->count++;
743091fd0abSLuigi Rizzo 		m->m_nextpkt = NULL;
744091fd0abSLuigi Rizzo 	}
745091fd0abSLuigi Rizzo 	q->tail = tail;
746091fd0abSLuigi Rizzo }
747091fd0abSLuigi Rizzo 
748091fd0abSLuigi Rizzo /*
749091fd0abSLuigi Rizzo  * called under main lock to send packets from the host to the NIC
750091fd0abSLuigi Rizzo  * The host ring has packets from nr_hwcur to (cur - reserved)
751091fd0abSLuigi Rizzo  * to be sent down. We scan the tx rings, which have just been
752091fd0abSLuigi Rizzo  * flushed so nr_hwcur == cur. Pushing packets down means
753091fd0abSLuigi Rizzo  * increment cur and decrement avail.
754091fd0abSLuigi Rizzo  * XXX to be verified
755091fd0abSLuigi Rizzo  */
756091fd0abSLuigi Rizzo static void
757091fd0abSLuigi Rizzo netmap_sw_to_nic(struct netmap_adapter *na)
758091fd0abSLuigi Rizzo {
759091fd0abSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
760091fd0abSLuigi Rizzo 	struct netmap_kring *k1 = &na->tx_rings[0];
761091fd0abSLuigi Rizzo 	int i, howmany, src_lim, dst_lim;
762091fd0abSLuigi Rizzo 
763091fd0abSLuigi Rizzo 	howmany = kring->nr_hwavail;	/* XXX otherwise cur - reserved - nr_hwcur */
764091fd0abSLuigi Rizzo 
765091fd0abSLuigi Rizzo 	src_lim = kring->nkr_num_slots;
766091fd0abSLuigi Rizzo 	for (i = 0; howmany > 0 && i < na->num_tx_rings; i++, k1++) {
767091fd0abSLuigi Rizzo 		ND("%d packets left to ring %d (space %d)", howmany, i, k1->nr_hwavail);
768091fd0abSLuigi Rizzo 		dst_lim = k1->nkr_num_slots;
769091fd0abSLuigi Rizzo 		while (howmany > 0 && k1->ring->avail > 0) {
770091fd0abSLuigi Rizzo 			struct netmap_slot *src, *dst, tmp;
771091fd0abSLuigi Rizzo 			src = &kring->ring->slot[kring->nr_hwcur];
772091fd0abSLuigi Rizzo 			dst = &k1->ring->slot[k1->ring->cur];
773091fd0abSLuigi Rizzo 			tmp = *src;
774091fd0abSLuigi Rizzo 			src->buf_idx = dst->buf_idx;
775091fd0abSLuigi Rizzo 			src->flags = NS_BUF_CHANGED;
776091fd0abSLuigi Rizzo 
777091fd0abSLuigi Rizzo 			dst->buf_idx = tmp.buf_idx;
778091fd0abSLuigi Rizzo 			dst->len = tmp.len;
779091fd0abSLuigi Rizzo 			dst->flags = NS_BUF_CHANGED;
780091fd0abSLuigi Rizzo 			ND("out len %d buf %d from %d to %d",
781091fd0abSLuigi Rizzo 				dst->len, dst->buf_idx,
782091fd0abSLuigi Rizzo 				kring->nr_hwcur, k1->ring->cur);
783091fd0abSLuigi Rizzo 
784091fd0abSLuigi Rizzo 			if (++kring->nr_hwcur >= src_lim)
785091fd0abSLuigi Rizzo 				kring->nr_hwcur = 0;
786091fd0abSLuigi Rizzo 			howmany--;
787091fd0abSLuigi Rizzo 			kring->nr_hwavail--;
788091fd0abSLuigi Rizzo 			if (++k1->ring->cur >= dst_lim)
789091fd0abSLuigi Rizzo 				k1->ring->cur = 0;
790091fd0abSLuigi Rizzo 			k1->ring->avail--;
791091fd0abSLuigi Rizzo 		}
792091fd0abSLuigi Rizzo 		kring->ring->cur = kring->nr_hwcur; // XXX
793091fd0abSLuigi Rizzo 		k1++;
794091fd0abSLuigi Rizzo 	}
795091fd0abSLuigi Rizzo }
796091fd0abSLuigi Rizzo 
797091fd0abSLuigi Rizzo /*
79802ad4083SLuigi Rizzo  * netmap_sync_to_host() passes packets up. We are called from a
79902ad4083SLuigi Rizzo  * system call in user process context, and the only contention
80002ad4083SLuigi Rizzo  * can be among multiple user threads erroneously calling
801091fd0abSLuigi Rizzo  * this routine concurrently.
80268b8534bSLuigi Rizzo  */
80368b8534bSLuigi Rizzo static void
80468b8534bSLuigi Rizzo netmap_sync_to_host(struct netmap_adapter *na)
80568b8534bSLuigi Rizzo {
806d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->tx_rings[na->num_tx_rings];
80768b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
808091fd0abSLuigi Rizzo 	u_int k, lim = kring->nkr_num_slots - 1;
809091fd0abSLuigi Rizzo 	struct mbq q = { NULL, NULL };
81068b8534bSLuigi Rizzo 
81102ad4083SLuigi Rizzo 	k = ring->cur;
81202ad4083SLuigi Rizzo 	if (k > lim) {
81302ad4083SLuigi Rizzo 		netmap_ring_reinit(kring);
81402ad4083SLuigi Rizzo 		return;
81502ad4083SLuigi Rizzo 	}
8161a26580eSLuigi Rizzo 	// na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0);
81768b8534bSLuigi Rizzo 
81868b8534bSLuigi Rizzo 	/* Take packets from hwcur to cur and pass them up.
81968b8534bSLuigi Rizzo 	 * In case of no buffers we give up. At the end of the loop,
82068b8534bSLuigi Rizzo 	 * the queue is drained in all cases.
82168b8534bSLuigi Rizzo 	 */
822091fd0abSLuigi Rizzo 	netmap_grab_packets(kring, &q, 1);
82302ad4083SLuigi Rizzo 	kring->nr_hwcur = k;
82468b8534bSLuigi Rizzo 	kring->nr_hwavail = ring->avail = lim;
8251a26580eSLuigi Rizzo 	// na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0);
82668b8534bSLuigi Rizzo 
827091fd0abSLuigi Rizzo 	netmap_send_up(na->ifp, q.head);
82868b8534bSLuigi Rizzo }
82968b8534bSLuigi Rizzo 
83068b8534bSLuigi Rizzo /*
83102ad4083SLuigi Rizzo  * rxsync backend for packets coming from the host stack.
83202ad4083SLuigi Rizzo  * They have been put in the queue by netmap_start() so we
83302ad4083SLuigi Rizzo  * need to protect access to the kring using a lock.
83402ad4083SLuigi Rizzo  *
83568b8534bSLuigi Rizzo  * This routine also does the selrecord if called from the poll handler
83668b8534bSLuigi Rizzo  * (we know because td != NULL).
83701c7d25fSLuigi Rizzo  *
83801c7d25fSLuigi Rizzo  * NOTE: on linux, selrecord() is defined as a macro and uses pwait
83901c7d25fSLuigi Rizzo  *     as an additional hidden argument.
84068b8534bSLuigi Rizzo  */
84168b8534bSLuigi Rizzo static void
84201c7d25fSLuigi Rizzo netmap_sync_from_host(struct netmap_adapter *na, struct thread *td, void *pwait)
84368b8534bSLuigi Rizzo {
844d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
84568b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
84664ae02c3SLuigi Rizzo 	u_int j, n, lim = kring->nkr_num_slots;
84764ae02c3SLuigi Rizzo 	u_int k = ring->cur, resvd = ring->reserved;
84868b8534bSLuigi Rizzo 
84901c7d25fSLuigi Rizzo 	(void)pwait;	/* disable unused warnings */
8501a26580eSLuigi Rizzo 	na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0);
85164ae02c3SLuigi Rizzo 	if (k >= lim) {
85264ae02c3SLuigi Rizzo 		netmap_ring_reinit(kring);
85364ae02c3SLuigi Rizzo 		return;
85464ae02c3SLuigi Rizzo 	}
85564ae02c3SLuigi Rizzo 	/* new packets are already set in nr_hwavail */
85664ae02c3SLuigi Rizzo 	/* skip past packets that userspace has released */
85764ae02c3SLuigi Rizzo 	j = kring->nr_hwcur;
85864ae02c3SLuigi Rizzo 	if (resvd > 0) {
85964ae02c3SLuigi Rizzo 		if (resvd + ring->avail >= lim + 1) {
86064ae02c3SLuigi Rizzo 			D("XXX invalid reserve/avail %d %d", resvd, ring->avail);
86164ae02c3SLuigi Rizzo 			ring->reserved = resvd = 0; // XXX panic...
86264ae02c3SLuigi Rizzo 		}
86364ae02c3SLuigi Rizzo 		k = (k >= resvd) ? k - resvd : k + lim - resvd;
86464ae02c3SLuigi Rizzo         }
86564ae02c3SLuigi Rizzo 	if (j != k) {
86664ae02c3SLuigi Rizzo 		n = k >= j ? k - j : k + lim - j;
86764ae02c3SLuigi Rizzo 		kring->nr_hwavail -= n;
86802ad4083SLuigi Rizzo 		kring->nr_hwcur = k;
86964ae02c3SLuigi Rizzo 	}
87064ae02c3SLuigi Rizzo 	k = ring->avail = kring->nr_hwavail - resvd;
87102ad4083SLuigi Rizzo 	if (k == 0 && td)
87268b8534bSLuigi Rizzo 		selrecord(td, &kring->si);
87302ad4083SLuigi Rizzo 	if (k && (netmap_verbose & NM_VERB_HOST))
87402ad4083SLuigi Rizzo 		D("%d pkts from stack", k);
8751a26580eSLuigi Rizzo 	na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0);
87668b8534bSLuigi Rizzo }
87768b8534bSLuigi Rizzo 
87868b8534bSLuigi Rizzo 
87968b8534bSLuigi Rizzo /*
88068b8534bSLuigi Rizzo  * get a refcounted reference to an interface.
88168b8534bSLuigi Rizzo  * Return ENXIO if the interface does not exist, EINVAL if netmap
88268b8534bSLuigi Rizzo  * is not supported by the interface.
88368b8534bSLuigi Rizzo  * If successful, hold a reference.
88468b8534bSLuigi Rizzo  */
88568b8534bSLuigi Rizzo static int
88668b8534bSLuigi Rizzo get_ifp(const char *name, struct ifnet **ifp)
88768b8534bSLuigi Rizzo {
888f196ce38SLuigi Rizzo #ifdef NM_BRIDGE
889f196ce38SLuigi Rizzo 	struct ifnet *iter = NULL;
890f196ce38SLuigi Rizzo 
891f196ce38SLuigi Rizzo 	do {
892f196ce38SLuigi Rizzo 		struct nm_bridge *b;
893f196ce38SLuigi Rizzo 		int i, l, cand = -1;
894f196ce38SLuigi Rizzo 
895f196ce38SLuigi Rizzo 		if (strncmp(name, NM_NAME, sizeof(NM_NAME) - 1))
896f196ce38SLuigi Rizzo 			break;
897f196ce38SLuigi Rizzo 		b = nm_find_bridge(name);
898f196ce38SLuigi Rizzo 		if (b == NULL) {
899f196ce38SLuigi Rizzo 			D("no bridges available for '%s'", name);
900f196ce38SLuigi Rizzo 			return (ENXIO);
901f196ce38SLuigi Rizzo 		}
902f196ce38SLuigi Rizzo 		/* XXX locking */
903f196ce38SLuigi Rizzo 		BDG_LOCK(b);
904f196ce38SLuigi Rizzo 		/* lookup in the local list of ports */
905f196ce38SLuigi Rizzo 		for (i = 0; i < NM_BDG_MAXPORTS; i++) {
906f196ce38SLuigi Rizzo 			iter = b->bdg_ports[i];
907f196ce38SLuigi Rizzo 			if (iter == NULL) {
908f196ce38SLuigi Rizzo 				if (cand == -1)
909f196ce38SLuigi Rizzo 					cand = i; /* potential insert point */
910f196ce38SLuigi Rizzo 				continue;
911f196ce38SLuigi Rizzo 			}
912f196ce38SLuigi Rizzo 			if (!strcmp(iter->if_xname, name)) {
913f196ce38SLuigi Rizzo 				ADD_BDG_REF(iter);
914f196ce38SLuigi Rizzo 				ND("found existing interface");
915f196ce38SLuigi Rizzo 				BDG_UNLOCK(b);
916f196ce38SLuigi Rizzo 				break;
917f196ce38SLuigi Rizzo 			}
918f196ce38SLuigi Rizzo 		}
919f196ce38SLuigi Rizzo 		if (i < NM_BDG_MAXPORTS) /* already unlocked */
920f196ce38SLuigi Rizzo 			break;
921f196ce38SLuigi Rizzo 		if (cand == -1) {
922f196ce38SLuigi Rizzo 			D("bridge full, cannot create new port");
923f196ce38SLuigi Rizzo no_port:
924f196ce38SLuigi Rizzo 			BDG_UNLOCK(b);
925f196ce38SLuigi Rizzo 			*ifp = NULL;
926f196ce38SLuigi Rizzo 			return EINVAL;
927f196ce38SLuigi Rizzo 		}
928f196ce38SLuigi Rizzo 		ND("create new bridge port %s", name);
929f196ce38SLuigi Rizzo 		/* space for forwarding list after the ifnet */
930f196ce38SLuigi Rizzo 		l = sizeof(*iter) +
931f196ce38SLuigi Rizzo 			 sizeof(struct nm_bdg_fwd)*NM_BDG_BATCH ;
932f196ce38SLuigi Rizzo 		iter = malloc(l, M_DEVBUF, M_NOWAIT | M_ZERO);
933f196ce38SLuigi Rizzo 		if (!iter)
934f196ce38SLuigi Rizzo 			goto no_port;
935f196ce38SLuigi Rizzo 		strcpy(iter->if_xname, name);
936f196ce38SLuigi Rizzo 		bdg_netmap_attach(iter);
937f196ce38SLuigi Rizzo 		b->bdg_ports[cand] = iter;
938f196ce38SLuigi Rizzo 		iter->if_bridge = b;
939f196ce38SLuigi Rizzo 		ADD_BDG_REF(iter);
940f196ce38SLuigi Rizzo 		BDG_UNLOCK(b);
941f196ce38SLuigi Rizzo 		ND("attaching virtual bridge %p", b);
942f196ce38SLuigi Rizzo 	} while (0);
943f196ce38SLuigi Rizzo 	*ifp = iter;
944f196ce38SLuigi Rizzo 	if (! *ifp)
945f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */
94668b8534bSLuigi Rizzo 	*ifp = ifunit_ref(name);
94768b8534bSLuigi Rizzo 	if (*ifp == NULL)
94868b8534bSLuigi Rizzo 		return (ENXIO);
94968b8534bSLuigi Rizzo 	/* can do this if the capability exists and if_pspare[0]
95068b8534bSLuigi Rizzo 	 * points to the netmap descriptor.
95168b8534bSLuigi Rizzo 	 */
9528241616dSLuigi Rizzo 	if (NETMAP_CAPABLE(*ifp))
95368b8534bSLuigi Rizzo 		return 0;	/* valid pointer, we hold the refcount */
954f196ce38SLuigi Rizzo 	nm_if_rele(*ifp);
95568b8534bSLuigi Rizzo 	return EINVAL;	// not NETMAP capable
95668b8534bSLuigi Rizzo }
95768b8534bSLuigi Rizzo 
95868b8534bSLuigi Rizzo 
95968b8534bSLuigi Rizzo /*
96068b8534bSLuigi Rizzo  * Error routine called when txsync/rxsync detects an error.
96168b8534bSLuigi Rizzo  * Can't do much more than resetting cur = hwcur, avail = hwavail.
96268b8534bSLuigi Rizzo  * Return 1 on reinit.
963506cc70cSLuigi Rizzo  *
964506cc70cSLuigi Rizzo  * This routine is only called by the upper half of the kernel.
965506cc70cSLuigi Rizzo  * It only reads hwcur (which is changed only by the upper half, too)
966506cc70cSLuigi Rizzo  * and hwavail (which may be changed by the lower half, but only on
967506cc70cSLuigi Rizzo  * a tx ring and only to increase it, so any error will be recovered
968506cc70cSLuigi Rizzo  * on the next call). For the above, we don't strictly need to call
969506cc70cSLuigi Rizzo  * it under lock.
97068b8534bSLuigi Rizzo  */
97168b8534bSLuigi Rizzo int
97268b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring)
97368b8534bSLuigi Rizzo {
97468b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
97568b8534bSLuigi Rizzo 	u_int i, lim = kring->nkr_num_slots - 1;
97668b8534bSLuigi Rizzo 	int errors = 0;
97768b8534bSLuigi Rizzo 
9788241616dSLuigi Rizzo 	RD(10, "called for %s", kring->na->ifp->if_xname);
97968b8534bSLuigi Rizzo 	if (ring->cur > lim)
98068b8534bSLuigi Rizzo 		errors++;
98168b8534bSLuigi Rizzo 	for (i = 0; i <= lim; i++) {
98268b8534bSLuigi Rizzo 		u_int idx = ring->slot[i].buf_idx;
98368b8534bSLuigi Rizzo 		u_int len = ring->slot[i].len;
98468b8534bSLuigi Rizzo 		if (idx < 2 || idx >= netmap_total_buffers) {
98568b8534bSLuigi Rizzo 			if (!errors++)
98668b8534bSLuigi Rizzo 				D("bad buffer at slot %d idx %d len %d ", i, idx, len);
98768b8534bSLuigi Rizzo 			ring->slot[i].buf_idx = 0;
98868b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
98968b8534bSLuigi Rizzo 		} else if (len > NETMAP_BUF_SIZE) {
99068b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
99168b8534bSLuigi Rizzo 			if (!errors++)
99268b8534bSLuigi Rizzo 				D("bad len %d at slot %d idx %d",
99368b8534bSLuigi Rizzo 					len, i, idx);
99468b8534bSLuigi Rizzo 		}
99568b8534bSLuigi Rizzo 	}
99668b8534bSLuigi Rizzo 	if (errors) {
99768b8534bSLuigi Rizzo 		int pos = kring - kring->na->tx_rings;
998d76bf4ffSLuigi Rizzo 		int n = kring->na->num_tx_rings + 1;
99968b8534bSLuigi Rizzo 
10008241616dSLuigi Rizzo 		RD(10, "total %d errors", errors);
100168b8534bSLuigi Rizzo 		errors++;
10028241616dSLuigi Rizzo 		RD(10, "%s %s[%d] reinit, cur %d -> %d avail %d -> %d",
100368b8534bSLuigi Rizzo 			kring->na->ifp->if_xname,
100468b8534bSLuigi Rizzo 			pos < n ?  "TX" : "RX", pos < n ? pos : pos - n,
100568b8534bSLuigi Rizzo 			ring->cur, kring->nr_hwcur,
100668b8534bSLuigi Rizzo 			ring->avail, kring->nr_hwavail);
100768b8534bSLuigi Rizzo 		ring->cur = kring->nr_hwcur;
100868b8534bSLuigi Rizzo 		ring->avail = kring->nr_hwavail;
100968b8534bSLuigi Rizzo 	}
101068b8534bSLuigi Rizzo 	return (errors ? 1 : 0);
101168b8534bSLuigi Rizzo }
101268b8534bSLuigi Rizzo 
101368b8534bSLuigi Rizzo 
101468b8534bSLuigi Rizzo /*
101568b8534bSLuigi Rizzo  * Set the ring ID. For devices with a single queue, a request
101668b8534bSLuigi Rizzo  * for all rings is the same as a single ring.
101768b8534bSLuigi Rizzo  */
101868b8534bSLuigi Rizzo static int
101968b8534bSLuigi Rizzo netmap_set_ringid(struct netmap_priv_d *priv, u_int ringid)
102068b8534bSLuigi Rizzo {
102168b8534bSLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
102268b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
102368b8534bSLuigi Rizzo 	u_int i = ringid & NETMAP_RING_MASK;
102464ae02c3SLuigi Rizzo 	/* initially (np_qfirst == np_qlast) we don't want to lock */
102568b8534bSLuigi Rizzo 	int need_lock = (priv->np_qfirst != priv->np_qlast);
1026d76bf4ffSLuigi Rizzo 	int lim = na->num_rx_rings;
102768b8534bSLuigi Rizzo 
1028d76bf4ffSLuigi Rizzo 	if (na->num_tx_rings > lim)
1029d76bf4ffSLuigi Rizzo 		lim = na->num_tx_rings;
103064ae02c3SLuigi Rizzo 	if ( (ringid & NETMAP_HW_RING) && i >= lim) {
103168b8534bSLuigi Rizzo 		D("invalid ring id %d", i);
103268b8534bSLuigi Rizzo 		return (EINVAL);
103368b8534bSLuigi Rizzo 	}
103468b8534bSLuigi Rizzo 	if (need_lock)
10351a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
103668b8534bSLuigi Rizzo 	priv->np_ringid = ringid;
103768b8534bSLuigi Rizzo 	if (ringid & NETMAP_SW_RING) {
103864ae02c3SLuigi Rizzo 		priv->np_qfirst = NETMAP_SW_RING;
103964ae02c3SLuigi Rizzo 		priv->np_qlast = 0;
104068b8534bSLuigi Rizzo 	} else if (ringid & NETMAP_HW_RING) {
104168b8534bSLuigi Rizzo 		priv->np_qfirst = i;
104268b8534bSLuigi Rizzo 		priv->np_qlast = i + 1;
104368b8534bSLuigi Rizzo 	} else {
104468b8534bSLuigi Rizzo 		priv->np_qfirst = 0;
104564ae02c3SLuigi Rizzo 		priv->np_qlast = NETMAP_HW_RING ;
104668b8534bSLuigi Rizzo 	}
104768b8534bSLuigi Rizzo 	priv->np_txpoll = (ringid & NETMAP_NO_TX_POLL) ? 0 : 1;
104868b8534bSLuigi Rizzo 	if (need_lock)
10491a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
1050ae10d1afSLuigi Rizzo     if (netmap_verbose) {
105168b8534bSLuigi Rizzo 	if (ringid & NETMAP_SW_RING)
105268b8534bSLuigi Rizzo 		D("ringid %s set to SW RING", ifp->if_xname);
105368b8534bSLuigi Rizzo 	else if (ringid & NETMAP_HW_RING)
105468b8534bSLuigi Rizzo 		D("ringid %s set to HW RING %d", ifp->if_xname,
105568b8534bSLuigi Rizzo 			priv->np_qfirst);
105668b8534bSLuigi Rizzo 	else
105764ae02c3SLuigi Rizzo 		D("ringid %s set to all %d HW RINGS", ifp->if_xname, lim);
1058ae10d1afSLuigi Rizzo     }
105968b8534bSLuigi Rizzo 	return 0;
106068b8534bSLuigi Rizzo }
106168b8534bSLuigi Rizzo 
106268b8534bSLuigi Rizzo /*
106368b8534bSLuigi Rizzo  * ioctl(2) support for the "netmap" device.
106468b8534bSLuigi Rizzo  *
106568b8534bSLuigi Rizzo  * Following a list of accepted commands:
106668b8534bSLuigi Rizzo  * - NIOCGINFO
106768b8534bSLuigi Rizzo  * - SIOCGIFADDR	just for convenience
106868b8534bSLuigi Rizzo  * - NIOCREGIF
106968b8534bSLuigi Rizzo  * - NIOCUNREGIF
107068b8534bSLuigi Rizzo  * - NIOCTXSYNC
107168b8534bSLuigi Rizzo  * - NIOCRXSYNC
107268b8534bSLuigi Rizzo  *
107368b8534bSLuigi Rizzo  * Return 0 on success, errno otherwise.
107468b8534bSLuigi Rizzo  */
107568b8534bSLuigi Rizzo static int
10760b8ed8e0SLuigi Rizzo netmap_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
10770b8ed8e0SLuigi Rizzo 	int fflag, struct thread *td)
107868b8534bSLuigi Rizzo {
107968b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
108068b8534bSLuigi Rizzo 	struct ifnet *ifp;
108168b8534bSLuigi Rizzo 	struct nmreq *nmr = (struct nmreq *) data;
108268b8534bSLuigi Rizzo 	struct netmap_adapter *na;
108368b8534bSLuigi Rizzo 	int error;
108464ae02c3SLuigi Rizzo 	u_int i, lim;
108568b8534bSLuigi Rizzo 	struct netmap_if *nifp;
108668b8534bSLuigi Rizzo 
10870b8ed8e0SLuigi Rizzo 	(void)dev;	/* UNUSED */
10880b8ed8e0SLuigi Rizzo 	(void)fflag;	/* UNUSED */
1089f196ce38SLuigi Rizzo #ifdef linux
1090f196ce38SLuigi Rizzo #define devfs_get_cdevpriv(pp)				\
1091f196ce38SLuigi Rizzo 	({ *(struct netmap_priv_d **)pp = ((struct file *)td)->private_data; 	\
1092f196ce38SLuigi Rizzo 		(*pp ? 0 : ENOENT); })
1093f196ce38SLuigi Rizzo 
1094f196ce38SLuigi Rizzo /* devfs_set_cdevpriv cannot fail on linux */
1095f196ce38SLuigi Rizzo #define devfs_set_cdevpriv(p, fn)				\
1096f196ce38SLuigi Rizzo 	({ ((struct file *)td)->private_data = p; (p ? 0 : EINVAL); })
1097f196ce38SLuigi Rizzo 
1098f196ce38SLuigi Rizzo 
1099f196ce38SLuigi Rizzo #define devfs_clear_cdevpriv()	do {				\
1100f196ce38SLuigi Rizzo 		netmap_dtor(priv); ((struct file *)td)->private_data = 0;	\
1101f196ce38SLuigi Rizzo 	} while (0)
1102f196ce38SLuigi Rizzo #endif /* linux */
1103f196ce38SLuigi Rizzo 
1104506cc70cSLuigi Rizzo 	CURVNET_SET(TD_TO_VNET(td));
1105506cc70cSLuigi Rizzo 
110668b8534bSLuigi Rizzo 	error = devfs_get_cdevpriv((void **)&priv);
11078241616dSLuigi Rizzo 	if (error) {
1108506cc70cSLuigi Rizzo 		CURVNET_RESTORE();
11098241616dSLuigi Rizzo 		/* XXX ENOENT should be impossible, since the priv
11108241616dSLuigi Rizzo 		 * is now created in the open */
11118241616dSLuigi Rizzo 		return (error == ENOENT ? ENXIO : error);
1112506cc70cSLuigi Rizzo 	}
111368b8534bSLuigi Rizzo 
1114f196ce38SLuigi Rizzo 	nmr->nr_name[sizeof(nmr->nr_name) - 1] = '\0';	/* truncate name */
111568b8534bSLuigi Rizzo 	switch (cmd) {
111668b8534bSLuigi Rizzo 	case NIOCGINFO:		/* return capabilities etc */
111764ae02c3SLuigi Rizzo 		if (nmr->nr_version != NETMAP_API) {
111864ae02c3SLuigi Rizzo 			D("API mismatch got %d have %d",
111964ae02c3SLuigi Rizzo 				nmr->nr_version, NETMAP_API);
112064ae02c3SLuigi Rizzo 			nmr->nr_version = NETMAP_API;
112164ae02c3SLuigi Rizzo 			error = EINVAL;
112264ae02c3SLuigi Rizzo 			break;
112364ae02c3SLuigi Rizzo 		}
11248241616dSLuigi Rizzo 		/* update configuration */
11258241616dSLuigi Rizzo 		error = netmap_get_memory(priv);
11268241616dSLuigi Rizzo 		ND("get_memory returned %d", error);
11278241616dSLuigi Rizzo 		if (error)
11288241616dSLuigi Rizzo 			break;
11298241616dSLuigi Rizzo 		/* memsize is always valid */
11308241616dSLuigi Rizzo 		nmr->nr_memsize = nm_mem.nm_totalsize;
11318241616dSLuigi Rizzo 		nmr->nr_offset = 0;
11328241616dSLuigi Rizzo 		nmr->nr_rx_rings = nmr->nr_tx_rings = 0;
11338241616dSLuigi Rizzo 		nmr->nr_rx_slots = nmr->nr_tx_slots = 0;
113468b8534bSLuigi Rizzo 		if (nmr->nr_name[0] == '\0')	/* just get memory info */
113568b8534bSLuigi Rizzo 			break;
113668b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* get a refcount */
113768b8534bSLuigi Rizzo 		if (error)
113868b8534bSLuigi Rizzo 			break;
113968b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap_adapter */
1140ae10d1afSLuigi Rizzo 		netmap_update_config(na);
1141d76bf4ffSLuigi Rizzo 		nmr->nr_rx_rings = na->num_rx_rings;
1142d76bf4ffSLuigi Rizzo 		nmr->nr_tx_rings = na->num_tx_rings;
114364ae02c3SLuigi Rizzo 		nmr->nr_rx_slots = na->num_rx_desc;
114464ae02c3SLuigi Rizzo 		nmr->nr_tx_slots = na->num_tx_desc;
1145f196ce38SLuigi Rizzo 		nm_if_rele(ifp);	/* return the refcount */
114668b8534bSLuigi Rizzo 		break;
114768b8534bSLuigi Rizzo 
114868b8534bSLuigi Rizzo 	case NIOCREGIF:
114964ae02c3SLuigi Rizzo 		if (nmr->nr_version != NETMAP_API) {
115064ae02c3SLuigi Rizzo 			nmr->nr_version = NETMAP_API;
115164ae02c3SLuigi Rizzo 			error = EINVAL;
115264ae02c3SLuigi Rizzo 			break;
115364ae02c3SLuigi Rizzo 		}
11548241616dSLuigi Rizzo 		/* ensure allocators are ready */
11558241616dSLuigi Rizzo 		error = netmap_get_memory(priv);
11568241616dSLuigi Rizzo 		ND("get_memory returned %d", error);
11578241616dSLuigi Rizzo 		if (error)
11588241616dSLuigi Rizzo 			break;
11598241616dSLuigi Rizzo 
11608241616dSLuigi Rizzo 		/* protect access to priv from concurrent NIOCREGIF */
11618241616dSLuigi Rizzo 		NMA_LOCK();
11628241616dSLuigi Rizzo 		if (priv->np_ifp != NULL) {	/* thread already registered */
1163506cc70cSLuigi Rizzo 			error = netmap_set_ringid(priv, nmr->nr_ringid);
11648241616dSLuigi Rizzo 			NMA_UNLOCK();
1165506cc70cSLuigi Rizzo 			break;
1166506cc70cSLuigi Rizzo 		}
116768b8534bSLuigi Rizzo 		/* find the interface and a reference */
116868b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* keep reference */
11698241616dSLuigi Rizzo 		if (error) {
11708241616dSLuigi Rizzo 			NMA_UNLOCK();
117168b8534bSLuigi Rizzo 			break;
117268b8534bSLuigi Rizzo 		}
11738241616dSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap adapter */
117468b8534bSLuigi Rizzo 
117568b8534bSLuigi Rizzo 		for (i = 10; i > 0; i--) {
11761a26580eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
117768b8534bSLuigi Rizzo 			if (!NETMAP_DELETING(na))
117868b8534bSLuigi Rizzo 				break;
11791a26580eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
118068b8534bSLuigi Rizzo 			tsleep(na, 0, "NIOCREGIF", hz/10);
118168b8534bSLuigi Rizzo 		}
118268b8534bSLuigi Rizzo 		if (i == 0) {
118368b8534bSLuigi Rizzo 			D("too many NIOCREGIF attempts, give up");
118468b8534bSLuigi Rizzo 			error = EINVAL;
1185f196ce38SLuigi Rizzo 			nm_if_rele(ifp);	/* return the refcount */
11868241616dSLuigi Rizzo 			NMA_UNLOCK();
118768b8534bSLuigi Rizzo 			break;
118868b8534bSLuigi Rizzo 		}
118968b8534bSLuigi Rizzo 
1190ae10d1afSLuigi Rizzo 		/* ring configuration may have changed, fetch from the card */
1191ae10d1afSLuigi Rizzo 		netmap_update_config(na);
119268b8534bSLuigi Rizzo 		priv->np_ifp = ifp;	/* store the reference */
119368b8534bSLuigi Rizzo 		error = netmap_set_ringid(priv, nmr->nr_ringid);
119468b8534bSLuigi Rizzo 		if (error)
119568b8534bSLuigi Rizzo 			goto error;
11968241616dSLuigi Rizzo 		nifp = netmap_if_new(nmr->nr_name, na);
119768b8534bSLuigi Rizzo 		if (nifp == NULL) { /* allocation failed */
119868b8534bSLuigi Rizzo 			error = ENOMEM;
119968b8534bSLuigi Rizzo 		} else if (ifp->if_capenable & IFCAP_NETMAP) {
120068b8534bSLuigi Rizzo 			/* was already set */
120168b8534bSLuigi Rizzo 		} else {
120268b8534bSLuigi Rizzo 			/* Otherwise set the card in netmap mode
120368b8534bSLuigi Rizzo 			 * and make it use the shared buffers.
120468b8534bSLuigi Rizzo 			 */
1205f196ce38SLuigi Rizzo 			for (i = 0 ; i < na->num_tx_rings + 1; i++)
1206f196ce38SLuigi Rizzo 				mtx_init(&na->tx_rings[i].q_lock, "nm_txq_lock", MTX_NETWORK_LOCK, MTX_DEF);
1207f196ce38SLuigi Rizzo 			for (i = 0 ; i < na->num_rx_rings + 1; i++) {
1208f196ce38SLuigi Rizzo 				mtx_init(&na->rx_rings[i].q_lock, "nm_rxq_lock", MTX_NETWORK_LOCK, MTX_DEF);
1209f196ce38SLuigi Rizzo 			}
121068b8534bSLuigi Rizzo 			error = na->nm_register(ifp, 1); /* mode on */
12118241616dSLuigi Rizzo 			if (error) {
12125819da83SLuigi Rizzo 				netmap_dtor_locked(priv);
12138241616dSLuigi Rizzo 				netmap_if_free(nifp);
12148241616dSLuigi Rizzo 			}
121568b8534bSLuigi Rizzo 		}
121668b8534bSLuigi Rizzo 
121768b8534bSLuigi Rizzo 		if (error) {	/* reg. failed, release priv and ref */
121868b8534bSLuigi Rizzo error:
12191a26580eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
1220f196ce38SLuigi Rizzo 			nm_if_rele(ifp);	/* return the refcount */
12218241616dSLuigi Rizzo 			priv->np_ifp = NULL;
12228241616dSLuigi Rizzo 			priv->np_nifp = NULL;
12238241616dSLuigi Rizzo 			NMA_UNLOCK();
122468b8534bSLuigi Rizzo 			break;
122568b8534bSLuigi Rizzo 		}
122668b8534bSLuigi Rizzo 
12271a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
122868b8534bSLuigi Rizzo 
12298241616dSLuigi Rizzo 		/* the following assignment is a commitment.
12308241616dSLuigi Rizzo 		 * Readers (i.e., poll and *SYNC) check for
12318241616dSLuigi Rizzo 		 * np_nifp != NULL without locking
123268b8534bSLuigi Rizzo 		 */
12338241616dSLuigi Rizzo 		wmb(); /* make sure previous writes are visible to all CPUs */
12348241616dSLuigi Rizzo 		priv->np_nifp = nifp;
12358241616dSLuigi Rizzo 		NMA_UNLOCK();
123668b8534bSLuigi Rizzo 
123768b8534bSLuigi Rizzo 		/* return the offset of the netmap_if object */
1238d76bf4ffSLuigi Rizzo 		nmr->nr_rx_rings = na->num_rx_rings;
1239d76bf4ffSLuigi Rizzo 		nmr->nr_tx_rings = na->num_tx_rings;
124064ae02c3SLuigi Rizzo 		nmr->nr_rx_slots = na->num_rx_desc;
124164ae02c3SLuigi Rizzo 		nmr->nr_tx_slots = na->num_tx_desc;
12428241616dSLuigi Rizzo 		nmr->nr_memsize = nm_mem.nm_totalsize;
1243446ee301SLuigi Rizzo 		nmr->nr_offset = netmap_if_offset(nifp);
124468b8534bSLuigi Rizzo 		break;
124568b8534bSLuigi Rizzo 
124668b8534bSLuigi Rizzo 	case NIOCUNREGIF:
12478241616dSLuigi Rizzo 		// XXX we have no data here ?
12488241616dSLuigi Rizzo 		D("deprecated, data is %p", nmr);
12498241616dSLuigi Rizzo 		error = EINVAL;
125068b8534bSLuigi Rizzo 		break;
125168b8534bSLuigi Rizzo 
125268b8534bSLuigi Rizzo 	case NIOCTXSYNC:
125368b8534bSLuigi Rizzo 	case NIOCRXSYNC:
12548241616dSLuigi Rizzo 		nifp = priv->np_nifp;
12558241616dSLuigi Rizzo 
12568241616dSLuigi Rizzo 		if (nifp == NULL) {
1257506cc70cSLuigi Rizzo 			error = ENXIO;
1258506cc70cSLuigi Rizzo 			break;
1259506cc70cSLuigi Rizzo 		}
12608241616dSLuigi Rizzo 		rmb(); /* make sure following reads are not from cache */
12618241616dSLuigi Rizzo 
12628241616dSLuigi Rizzo 
126368b8534bSLuigi Rizzo 		ifp = priv->np_ifp;	/* we have a reference */
12648241616dSLuigi Rizzo 
12658241616dSLuigi Rizzo 		if (ifp == NULL) {
12668241616dSLuigi Rizzo 			D("Internal error: nifp != NULL && ifp == NULL");
12678241616dSLuigi Rizzo 			error = ENXIO;
12688241616dSLuigi Rizzo 			break;
12698241616dSLuigi Rizzo 		}
12708241616dSLuigi Rizzo 
127168b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap adapter */
127264ae02c3SLuigi Rizzo 		if (priv->np_qfirst == NETMAP_SW_RING) { /* host rings */
127368b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC)
127468b8534bSLuigi Rizzo 				netmap_sync_to_host(na);
127568b8534bSLuigi Rizzo 			else
127601c7d25fSLuigi Rizzo 				netmap_sync_from_host(na, NULL, NULL);
1277506cc70cSLuigi Rizzo 			break;
127868b8534bSLuigi Rizzo 		}
127964ae02c3SLuigi Rizzo 		/* find the last ring to scan */
128064ae02c3SLuigi Rizzo 		lim = priv->np_qlast;
128164ae02c3SLuigi Rizzo 		if (lim == NETMAP_HW_RING)
12823c0caf6cSLuigi Rizzo 			lim = (cmd == NIOCTXSYNC) ?
1283d76bf4ffSLuigi Rizzo 			    na->num_tx_rings : na->num_rx_rings;
128468b8534bSLuigi Rizzo 
128564ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim; i++) {
128668b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC) {
128768b8534bSLuigi Rizzo 				struct netmap_kring *kring = &na->tx_rings[i];
128868b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
12893c0caf6cSLuigi Rizzo 					D("pre txsync ring %d cur %d hwcur %d",
129068b8534bSLuigi Rizzo 					    i, kring->ring->cur,
129168b8534bSLuigi Rizzo 					    kring->nr_hwcur);
12921a26580eSLuigi Rizzo 				na->nm_txsync(ifp, i, 1 /* do lock */);
129368b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
12943c0caf6cSLuigi Rizzo 					D("post txsync ring %d cur %d hwcur %d",
129568b8534bSLuigi Rizzo 					    i, kring->ring->cur,
129668b8534bSLuigi Rizzo 					    kring->nr_hwcur);
129768b8534bSLuigi Rizzo 			} else {
12981a26580eSLuigi Rizzo 				na->nm_rxsync(ifp, i, 1 /* do lock */);
129968b8534bSLuigi Rizzo 				microtime(&na->rx_rings[i].ring->ts);
130068b8534bSLuigi Rizzo 			}
130168b8534bSLuigi Rizzo 		}
130268b8534bSLuigi Rizzo 
130368b8534bSLuigi Rizzo 		break;
130468b8534bSLuigi Rizzo 
1305f196ce38SLuigi Rizzo #ifdef __FreeBSD__
130668b8534bSLuigi Rizzo 	case BIOCIMMEDIATE:
130768b8534bSLuigi Rizzo 	case BIOCGHDRCMPLT:
130868b8534bSLuigi Rizzo 	case BIOCSHDRCMPLT:
130968b8534bSLuigi Rizzo 	case BIOCSSEESENT:
131068b8534bSLuigi Rizzo 		D("ignore BIOCIMMEDIATE/BIOCSHDRCMPLT/BIOCSHDRCMPLT/BIOCSSEESENT");
131168b8534bSLuigi Rizzo 		break;
131268b8534bSLuigi Rizzo 
1313babc7c12SLuigi Rizzo 	default:	/* allow device-specific ioctls */
131468b8534bSLuigi Rizzo 	    {
131568b8534bSLuigi Rizzo 		struct socket so;
131668b8534bSLuigi Rizzo 		bzero(&so, sizeof(so));
131768b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* keep reference */
131868b8534bSLuigi Rizzo 		if (error)
131968b8534bSLuigi Rizzo 			break;
132068b8534bSLuigi Rizzo 		so.so_vnet = ifp->if_vnet;
132168b8534bSLuigi Rizzo 		// so->so_proto not null.
132268b8534bSLuigi Rizzo 		error = ifioctl(&so, cmd, data, td);
1323f196ce38SLuigi Rizzo 		nm_if_rele(ifp);
1324babc7c12SLuigi Rizzo 		break;
132568b8534bSLuigi Rizzo 	    }
1326f196ce38SLuigi Rizzo 
1327f196ce38SLuigi Rizzo #else /* linux */
1328f196ce38SLuigi Rizzo 	default:
1329f196ce38SLuigi Rizzo 		error = EOPNOTSUPP;
1330f196ce38SLuigi Rizzo #endif /* linux */
133168b8534bSLuigi Rizzo 	}
133268b8534bSLuigi Rizzo 
1333506cc70cSLuigi Rizzo 	CURVNET_RESTORE();
133468b8534bSLuigi Rizzo 	return (error);
133568b8534bSLuigi Rizzo }
133668b8534bSLuigi Rizzo 
133768b8534bSLuigi Rizzo 
133868b8534bSLuigi Rizzo /*
133968b8534bSLuigi Rizzo  * select(2) and poll(2) handlers for the "netmap" device.
134068b8534bSLuigi Rizzo  *
134168b8534bSLuigi Rizzo  * Can be called for one or more queues.
134268b8534bSLuigi Rizzo  * Return true the event mask corresponding to ready events.
134368b8534bSLuigi Rizzo  * If there are no ready events, do a selrecord on either individual
134468b8534bSLuigi Rizzo  * selfd or on the global one.
134568b8534bSLuigi Rizzo  * Device-dependent parts (locking and sync of tx/rx rings)
134668b8534bSLuigi Rizzo  * are done through callbacks.
1347f196ce38SLuigi Rizzo  *
134801c7d25fSLuigi Rizzo  * On linux, arguments are really pwait, the poll table, and 'td' is struct file *
134901c7d25fSLuigi Rizzo  * The first one is remapped to pwait as selrecord() uses the name as an
135001c7d25fSLuigi Rizzo  * hidden argument.
135168b8534bSLuigi Rizzo  */
135268b8534bSLuigi Rizzo static int
135301c7d25fSLuigi Rizzo netmap_poll(struct cdev *dev, int events, struct thread *td)
135468b8534bSLuigi Rizzo {
135568b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
135668b8534bSLuigi Rizzo 	struct netmap_adapter *na;
135768b8534bSLuigi Rizzo 	struct ifnet *ifp;
135868b8534bSLuigi Rizzo 	struct netmap_kring *kring;
1359d0c7b075SLuigi Rizzo 	u_int core_lock, i, check_all, want_tx, want_rx, revents = 0;
1360091fd0abSLuigi Rizzo 	u_int lim_tx, lim_rx, host_forwarded = 0;
1361091fd0abSLuigi Rizzo 	struct mbq q = { NULL, NULL, 0 };
1362bcda432eSLuigi Rizzo 	enum {NO_CL, NEED_CL, LOCKED_CL }; /* see below */
136301c7d25fSLuigi Rizzo 	void *pwait = dev;	/* linux compatibility */
136401c7d25fSLuigi Rizzo 
136501c7d25fSLuigi Rizzo 	(void)pwait;
136668b8534bSLuigi Rizzo 
136768b8534bSLuigi Rizzo 	if (devfs_get_cdevpriv((void **)&priv) != 0 || priv == NULL)
136868b8534bSLuigi Rizzo 		return POLLERR;
136968b8534bSLuigi Rizzo 
13708241616dSLuigi Rizzo 	if (priv->np_nifp == NULL) {
13718241616dSLuigi Rizzo 		D("No if registered");
13728241616dSLuigi Rizzo 		return POLLERR;
13738241616dSLuigi Rizzo 	}
13748241616dSLuigi Rizzo 	rmb(); /* make sure following reads are not from cache */
13758241616dSLuigi Rizzo 
137668b8534bSLuigi Rizzo 	ifp = priv->np_ifp;
137768b8534bSLuigi Rizzo 	// XXX check for deleting() ?
137868b8534bSLuigi Rizzo 	if ( (ifp->if_capenable & IFCAP_NETMAP) == 0)
137968b8534bSLuigi Rizzo 		return POLLERR;
138068b8534bSLuigi Rizzo 
138168b8534bSLuigi Rizzo 	if (netmap_verbose & 0x8000)
138268b8534bSLuigi Rizzo 		D("device %s events 0x%x", ifp->if_xname, events);
138368b8534bSLuigi Rizzo 	want_tx = events & (POLLOUT | POLLWRNORM);
138468b8534bSLuigi Rizzo 	want_rx = events & (POLLIN | POLLRDNORM);
138568b8534bSLuigi Rizzo 
138668b8534bSLuigi Rizzo 	na = NA(ifp); /* retrieve netmap adapter */
138768b8534bSLuigi Rizzo 
1388d76bf4ffSLuigi Rizzo 	lim_tx = na->num_tx_rings;
1389d76bf4ffSLuigi Rizzo 	lim_rx = na->num_rx_rings;
139068b8534bSLuigi Rizzo 	/* how many queues we are scanning */
139164ae02c3SLuigi Rizzo 	if (priv->np_qfirst == NETMAP_SW_RING) {
139268b8534bSLuigi Rizzo 		if (priv->np_txpoll || want_tx) {
139368b8534bSLuigi Rizzo 			/* push any packets up, then we are always ready */
139464ae02c3SLuigi Rizzo 			kring = &na->tx_rings[lim_tx];
139568b8534bSLuigi Rizzo 			netmap_sync_to_host(na);
139668b8534bSLuigi Rizzo 			revents |= want_tx;
139768b8534bSLuigi Rizzo 		}
139868b8534bSLuigi Rizzo 		if (want_rx) {
139964ae02c3SLuigi Rizzo 			kring = &na->rx_rings[lim_rx];
140068b8534bSLuigi Rizzo 			if (kring->ring->avail == 0)
140101c7d25fSLuigi Rizzo 				netmap_sync_from_host(na, td, dev);
140268b8534bSLuigi Rizzo 			if (kring->ring->avail > 0) {
140368b8534bSLuigi Rizzo 				revents |= want_rx;
140468b8534bSLuigi Rizzo 			}
140568b8534bSLuigi Rizzo 		}
140668b8534bSLuigi Rizzo 		return (revents);
140768b8534bSLuigi Rizzo 	}
140868b8534bSLuigi Rizzo 
1409091fd0abSLuigi Rizzo 	/* if we are in transparent mode, check also the host rx ring */
1410091fd0abSLuigi Rizzo 	kring = &na->rx_rings[lim_rx];
1411091fd0abSLuigi Rizzo 	if ( (priv->np_qlast == NETMAP_HW_RING) // XXX check_all
1412091fd0abSLuigi Rizzo 			&& want_rx
1413091fd0abSLuigi Rizzo 			&& (netmap_fwd || kring->ring->flags & NR_FORWARD) ) {
1414091fd0abSLuigi Rizzo 		if (kring->ring->avail == 0)
1415091fd0abSLuigi Rizzo 			netmap_sync_from_host(na, td, dev);
1416091fd0abSLuigi Rizzo 		if (kring->ring->avail > 0)
1417091fd0abSLuigi Rizzo 			revents |= want_rx;
1418091fd0abSLuigi Rizzo 	}
1419091fd0abSLuigi Rizzo 
142068b8534bSLuigi Rizzo 	/*
142168b8534bSLuigi Rizzo 	 * check_all is set if the card has more than one queue and
142268b8534bSLuigi Rizzo 	 * the client is polling all of them. If true, we sleep on
142368b8534bSLuigi Rizzo 	 * the "global" selfd, otherwise we sleep on individual selfd
142468b8534bSLuigi Rizzo 	 * (we can only sleep on one of them per direction).
142568b8534bSLuigi Rizzo 	 * The interrupt routine in the driver should always wake on
142668b8534bSLuigi Rizzo 	 * the individual selfd, and also on the global one if the card
142768b8534bSLuigi Rizzo 	 * has more than one ring.
142868b8534bSLuigi Rizzo 	 *
142968b8534bSLuigi Rizzo 	 * If the card has only one lock, we just use that.
143068b8534bSLuigi Rizzo 	 * If the card has separate ring locks, we just use those
143168b8534bSLuigi Rizzo 	 * unless we are doing check_all, in which case the whole
143268b8534bSLuigi Rizzo 	 * loop is wrapped by the global lock.
143368b8534bSLuigi Rizzo 	 * We acquire locks only when necessary: if poll is called
143468b8534bSLuigi Rizzo 	 * when buffers are available, we can just return without locks.
143568b8534bSLuigi Rizzo 	 *
143668b8534bSLuigi Rizzo 	 * rxsync() is only called if we run out of buffers on a POLLIN.
143768b8534bSLuigi Rizzo 	 * txsync() is called if we run out of buffers on POLLOUT, or
143868b8534bSLuigi Rizzo 	 * there are pending packets to send. The latter can be disabled
143968b8534bSLuigi Rizzo 	 * passing NETMAP_NO_TX_POLL in the NIOCREG call.
144068b8534bSLuigi Rizzo 	 */
144164ae02c3SLuigi Rizzo 	check_all = (priv->np_qlast == NETMAP_HW_RING) && (lim_tx > 1 || lim_rx > 1);
144268b8534bSLuigi Rizzo 
144368b8534bSLuigi Rizzo 	/*
144468b8534bSLuigi Rizzo 	 * core_lock indicates what to do with the core lock.
144568b8534bSLuigi Rizzo 	 * The core lock is used when either the card has no individual
144668b8534bSLuigi Rizzo 	 * locks, or it has individual locks but we are cheking all
144768b8534bSLuigi Rizzo 	 * rings so we need the core lock to avoid missing wakeup events.
144868b8534bSLuigi Rizzo 	 *
144968b8534bSLuigi Rizzo 	 * It has three possible states:
145068b8534bSLuigi Rizzo 	 * NO_CL	we don't need to use the core lock, e.g.
145168b8534bSLuigi Rizzo 	 *		because we are protected by individual locks.
145268b8534bSLuigi Rizzo 	 * NEED_CL	we need the core lock. In this case, when we
145368b8534bSLuigi Rizzo 	 *		call the lock routine, move to LOCKED_CL
145468b8534bSLuigi Rizzo 	 *		to remember to release the lock once done.
145568b8534bSLuigi Rizzo 	 * LOCKED_CL	core lock is set, so we need to release it.
145668b8534bSLuigi Rizzo 	 */
1457d0c7b075SLuigi Rizzo 	core_lock = (check_all || !na->separate_locks) ? NEED_CL : NO_CL;
1458f196ce38SLuigi Rizzo #ifdef NM_BRIDGE
1459f196ce38SLuigi Rizzo 	/* the bridge uses separate locks */
1460f196ce38SLuigi Rizzo 	if (na->nm_register == bdg_netmap_reg) {
1461f196ce38SLuigi Rizzo 		ND("not using core lock for %s", ifp->if_xname);
1462f196ce38SLuigi Rizzo 		core_lock = NO_CL;
1463f196ce38SLuigi Rizzo 	}
1464f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */
146564ae02c3SLuigi Rizzo 	if (priv->np_qlast != NETMAP_HW_RING) {
146664ae02c3SLuigi Rizzo 		lim_tx = lim_rx = priv->np_qlast;
146764ae02c3SLuigi Rizzo 	}
146864ae02c3SLuigi Rizzo 
146968b8534bSLuigi Rizzo 	/*
147068b8534bSLuigi Rizzo 	 * We start with a lock free round which is good if we have
147168b8534bSLuigi Rizzo 	 * data available. If this fails, then lock and call the sync
147268b8534bSLuigi Rizzo 	 * routines.
147368b8534bSLuigi Rizzo 	 */
147464ae02c3SLuigi Rizzo 	for (i = priv->np_qfirst; want_rx && i < lim_rx; i++) {
147568b8534bSLuigi Rizzo 		kring = &na->rx_rings[i];
147668b8534bSLuigi Rizzo 		if (kring->ring->avail > 0) {
147768b8534bSLuigi Rizzo 			revents |= want_rx;
147868b8534bSLuigi Rizzo 			want_rx = 0;	/* also breaks the loop */
147968b8534bSLuigi Rizzo 		}
148068b8534bSLuigi Rizzo 	}
148164ae02c3SLuigi Rizzo 	for (i = priv->np_qfirst; want_tx && i < lim_tx; i++) {
148268b8534bSLuigi Rizzo 		kring = &na->tx_rings[i];
148368b8534bSLuigi Rizzo 		if (kring->ring->avail > 0) {
148468b8534bSLuigi Rizzo 			revents |= want_tx;
148568b8534bSLuigi Rizzo 			want_tx = 0;	/* also breaks the loop */
148668b8534bSLuigi Rizzo 		}
148768b8534bSLuigi Rizzo 	}
148868b8534bSLuigi Rizzo 
148968b8534bSLuigi Rizzo 	/*
149068b8534bSLuigi Rizzo 	 * If we to push packets out (priv->np_txpoll) or want_tx is
149168b8534bSLuigi Rizzo 	 * still set, we do need to run the txsync calls (on all rings,
149268b8534bSLuigi Rizzo 	 * to avoid that the tx rings stall).
149368b8534bSLuigi Rizzo 	 */
149468b8534bSLuigi Rizzo 	if (priv->np_txpoll || want_tx) {
1495091fd0abSLuigi Rizzo flush_tx:
149664ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim_tx; i++) {
149768b8534bSLuigi Rizzo 			kring = &na->tx_rings[i];
14985819da83SLuigi Rizzo 			/*
14995819da83SLuigi Rizzo 			 * Skip the current ring if want_tx == 0
15005819da83SLuigi Rizzo 			 * (we have already done a successful sync on
15015819da83SLuigi Rizzo 			 * a previous ring) AND kring->cur == kring->hwcur
15025819da83SLuigi Rizzo 			 * (there are no pending transmissions for this ring).
15035819da83SLuigi Rizzo 			 */
150468b8534bSLuigi Rizzo 			if (!want_tx && kring->ring->cur == kring->nr_hwcur)
150568b8534bSLuigi Rizzo 				continue;
150668b8534bSLuigi Rizzo 			if (core_lock == NEED_CL) {
15071a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
150868b8534bSLuigi Rizzo 				core_lock = LOCKED_CL;
150968b8534bSLuigi Rizzo 			}
151068b8534bSLuigi Rizzo 			if (na->separate_locks)
15111a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_TX_LOCK, i);
151268b8534bSLuigi Rizzo 			if (netmap_verbose & NM_VERB_TXSYNC)
151368b8534bSLuigi Rizzo 				D("send %d on %s %d",
151468b8534bSLuigi Rizzo 					kring->ring->cur,
151568b8534bSLuigi Rizzo 					ifp->if_xname, i);
15161a26580eSLuigi Rizzo 			if (na->nm_txsync(ifp, i, 0 /* no lock */))
151768b8534bSLuigi Rizzo 				revents |= POLLERR;
151868b8534bSLuigi Rizzo 
15195819da83SLuigi Rizzo 			/* Check avail/call selrecord only if called with POLLOUT */
152068b8534bSLuigi Rizzo 			if (want_tx) {
152168b8534bSLuigi Rizzo 				if (kring->ring->avail > 0) {
152268b8534bSLuigi Rizzo 					/* stop at the first ring. We don't risk
152368b8534bSLuigi Rizzo 					 * starvation.
152468b8534bSLuigi Rizzo 					 */
152568b8534bSLuigi Rizzo 					revents |= want_tx;
152668b8534bSLuigi Rizzo 					want_tx = 0;
152768b8534bSLuigi Rizzo 				} else if (!check_all)
152868b8534bSLuigi Rizzo 					selrecord(td, &kring->si);
152968b8534bSLuigi Rizzo 			}
153068b8534bSLuigi Rizzo 			if (na->separate_locks)
15311a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_TX_UNLOCK, i);
153268b8534bSLuigi Rizzo 		}
153368b8534bSLuigi Rizzo 	}
153468b8534bSLuigi Rizzo 
153568b8534bSLuigi Rizzo 	/*
153668b8534bSLuigi Rizzo 	 * now if want_rx is still set we need to lock and rxsync.
153768b8534bSLuigi Rizzo 	 * Do it on all rings because otherwise we starve.
153868b8534bSLuigi Rizzo 	 */
153968b8534bSLuigi Rizzo 	if (want_rx) {
154064ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim_rx; i++) {
154168b8534bSLuigi Rizzo 			kring = &na->rx_rings[i];
154268b8534bSLuigi Rizzo 			if (core_lock == NEED_CL) {
15431a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
154468b8534bSLuigi Rizzo 				core_lock = LOCKED_CL;
154568b8534bSLuigi Rizzo 			}
154668b8534bSLuigi Rizzo 			if (na->separate_locks)
15471a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_RX_LOCK, i);
1548091fd0abSLuigi Rizzo 			if (netmap_fwd ||kring->ring->flags & NR_FORWARD) {
1549091fd0abSLuigi Rizzo 				ND(10, "forwarding some buffers up %d to %d",
1550091fd0abSLuigi Rizzo 				    kring->nr_hwcur, kring->ring->cur);
1551091fd0abSLuigi Rizzo 				netmap_grab_packets(kring, &q, netmap_fwd);
1552091fd0abSLuigi Rizzo 			}
155368b8534bSLuigi Rizzo 
15541a26580eSLuigi Rizzo 			if (na->nm_rxsync(ifp, i, 0 /* no lock */))
155568b8534bSLuigi Rizzo 				revents |= POLLERR;
15565819da83SLuigi Rizzo 			if (netmap_no_timestamp == 0 ||
15575819da83SLuigi Rizzo 					kring->ring->flags & NR_TIMESTAMP) {
155868b8534bSLuigi Rizzo 				microtime(&kring->ring->ts);
15595819da83SLuigi Rizzo 			}
156068b8534bSLuigi Rizzo 
156168b8534bSLuigi Rizzo 			if (kring->ring->avail > 0)
156268b8534bSLuigi Rizzo 				revents |= want_rx;
156368b8534bSLuigi Rizzo 			else if (!check_all)
156468b8534bSLuigi Rizzo 				selrecord(td, &kring->si);
156568b8534bSLuigi Rizzo 			if (na->separate_locks)
15661a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_RX_UNLOCK, i);
156768b8534bSLuigi Rizzo 		}
156868b8534bSLuigi Rizzo 	}
156964ae02c3SLuigi Rizzo 	if (check_all && revents == 0) { /* signal on the global queue */
157068b8534bSLuigi Rizzo 		if (want_tx)
157164ae02c3SLuigi Rizzo 			selrecord(td, &na->tx_si);
157268b8534bSLuigi Rizzo 		if (want_rx)
157364ae02c3SLuigi Rizzo 			selrecord(td, &na->rx_si);
157468b8534bSLuigi Rizzo 	}
1575091fd0abSLuigi Rizzo 
1576091fd0abSLuigi Rizzo 	/* forward host to the netmap ring */
1577091fd0abSLuigi Rizzo 	kring = &na->rx_rings[lim_rx];
1578091fd0abSLuigi Rizzo 	if (kring->nr_hwavail > 0)
1579091fd0abSLuigi Rizzo 		ND("host rx %d has %d packets", lim_rx, kring->nr_hwavail);
1580091fd0abSLuigi Rizzo 	if ( (priv->np_qlast == NETMAP_HW_RING) // XXX check_all
1581091fd0abSLuigi Rizzo 			&& (netmap_fwd || kring->ring->flags & NR_FORWARD)
1582091fd0abSLuigi Rizzo 			 && kring->nr_hwavail > 0 && !host_forwarded) {
1583091fd0abSLuigi Rizzo 		if (core_lock == NEED_CL) {
1584091fd0abSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
1585091fd0abSLuigi Rizzo 			core_lock = LOCKED_CL;
1586091fd0abSLuigi Rizzo 		}
1587091fd0abSLuigi Rizzo 		netmap_sw_to_nic(na);
1588091fd0abSLuigi Rizzo 		host_forwarded = 1; /* prevent another pass */
1589091fd0abSLuigi Rizzo 		want_rx = 0;
1590091fd0abSLuigi Rizzo 		goto flush_tx;
1591091fd0abSLuigi Rizzo 	}
1592091fd0abSLuigi Rizzo 
159368b8534bSLuigi Rizzo 	if (core_lock == LOCKED_CL)
15941a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
1595091fd0abSLuigi Rizzo 	if (q.head)
1596091fd0abSLuigi Rizzo 		netmap_send_up(na->ifp, q.head);
159768b8534bSLuigi Rizzo 
159868b8534bSLuigi Rizzo 	return (revents);
159968b8534bSLuigi Rizzo }
160068b8534bSLuigi Rizzo 
160168b8534bSLuigi Rizzo /*------- driver support routines ------*/
160268b8534bSLuigi Rizzo 
160368b8534bSLuigi Rizzo /*
1604babc7c12SLuigi Rizzo  * default lock wrapper.
16051a26580eSLuigi Rizzo  */
16061a26580eSLuigi Rizzo static void
1607babc7c12SLuigi Rizzo netmap_lock_wrapper(struct ifnet *dev, int what, u_int queueid)
16081a26580eSLuigi Rizzo {
1609babc7c12SLuigi Rizzo 	struct netmap_adapter *na = NA(dev);
16101a26580eSLuigi Rizzo 
16111a26580eSLuigi Rizzo 	switch (what) {
1612babc7c12SLuigi Rizzo #ifdef linux	/* some system do not need lock on register */
16131a26580eSLuigi Rizzo 	case NETMAP_REG_LOCK:
16141a26580eSLuigi Rizzo 	case NETMAP_REG_UNLOCK:
16151a26580eSLuigi Rizzo 		break;
1616babc7c12SLuigi Rizzo #endif /* linux */
16171a26580eSLuigi Rizzo 
16181a26580eSLuigi Rizzo 	case NETMAP_CORE_LOCK:
16191a26580eSLuigi Rizzo 		mtx_lock(&na->core_lock);
16201a26580eSLuigi Rizzo 		break;
16211a26580eSLuigi Rizzo 
16221a26580eSLuigi Rizzo 	case NETMAP_CORE_UNLOCK:
16231a26580eSLuigi Rizzo 		mtx_unlock(&na->core_lock);
16241a26580eSLuigi Rizzo 		break;
16251a26580eSLuigi Rizzo 
16261a26580eSLuigi Rizzo 	case NETMAP_TX_LOCK:
16271a26580eSLuigi Rizzo 		mtx_lock(&na->tx_rings[queueid].q_lock);
16281a26580eSLuigi Rizzo 		break;
16291a26580eSLuigi Rizzo 
16301a26580eSLuigi Rizzo 	case NETMAP_TX_UNLOCK:
16311a26580eSLuigi Rizzo 		mtx_unlock(&na->tx_rings[queueid].q_lock);
16321a26580eSLuigi Rizzo 		break;
16331a26580eSLuigi Rizzo 
16341a26580eSLuigi Rizzo 	case NETMAP_RX_LOCK:
16351a26580eSLuigi Rizzo 		mtx_lock(&na->rx_rings[queueid].q_lock);
16361a26580eSLuigi Rizzo 		break;
16371a26580eSLuigi Rizzo 
16381a26580eSLuigi Rizzo 	case NETMAP_RX_UNLOCK:
16391a26580eSLuigi Rizzo 		mtx_unlock(&na->rx_rings[queueid].q_lock);
16401a26580eSLuigi Rizzo 		break;
16411a26580eSLuigi Rizzo 	}
16421a26580eSLuigi Rizzo }
16431a26580eSLuigi Rizzo 
16441a26580eSLuigi Rizzo 
16451a26580eSLuigi Rizzo /*
164668b8534bSLuigi Rizzo  * Initialize a ``netmap_adapter`` object created by driver on attach.
164768b8534bSLuigi Rizzo  * We allocate a block of memory with room for a struct netmap_adapter
164868b8534bSLuigi Rizzo  * plus two sets of N+2 struct netmap_kring (where N is the number
164968b8534bSLuigi Rizzo  * of hardware rings):
165068b8534bSLuigi Rizzo  * krings	0..N-1	are for the hardware queues.
165168b8534bSLuigi Rizzo  * kring	N	is for the host stack queue
165268b8534bSLuigi Rizzo  * kring	N+1	is only used for the selinfo for all queues.
165368b8534bSLuigi Rizzo  * Return 0 on success, ENOMEM otherwise.
165464ae02c3SLuigi Rizzo  *
16550bf88954SEd Maste  * By default the receive and transmit adapter ring counts are both initialized
16560bf88954SEd Maste  * to num_queues.  na->num_tx_rings can be set for cards with different tx/rx
165724e57ec9SEd Maste  * setups.
165868b8534bSLuigi Rizzo  */
165968b8534bSLuigi Rizzo int
1660ae10d1afSLuigi Rizzo netmap_attach(struct netmap_adapter *arg, int num_queues)
166168b8534bSLuigi Rizzo {
1662ae10d1afSLuigi Rizzo 	struct netmap_adapter *na = NULL;
1663ae10d1afSLuigi Rizzo 	struct ifnet *ifp = arg ? arg->ifp : NULL;
166468b8534bSLuigi Rizzo 
1665ae10d1afSLuigi Rizzo 	if (arg == NULL || ifp == NULL)
1666ae10d1afSLuigi Rizzo 		goto fail;
1667ae10d1afSLuigi Rizzo 	na = malloc(sizeof(*na), M_DEVBUF, M_NOWAIT | M_ZERO);
1668ae10d1afSLuigi Rizzo 	if (na == NULL)
1669ae10d1afSLuigi Rizzo 		goto fail;
1670ae10d1afSLuigi Rizzo 	WNA(ifp) = na;
1671ae10d1afSLuigi Rizzo 	*na = *arg; /* copy everything, trust the driver to not pass junk */
1672ae10d1afSLuigi Rizzo 	NETMAP_SET_CAPABLE(ifp);
1673d76bf4ffSLuigi Rizzo 	if (na->num_tx_rings == 0)
1674d76bf4ffSLuigi Rizzo 		na->num_tx_rings = num_queues;
1675d76bf4ffSLuigi Rizzo 	na->num_rx_rings = num_queues;
1676ae10d1afSLuigi Rizzo 	na->refcount = na->na_single = na->na_multi = 0;
1677ae10d1afSLuigi Rizzo 	/* Core lock initialized here, others after netmap_if_new. */
1678ae10d1afSLuigi Rizzo 	mtx_init(&na->core_lock, "netmap core lock", MTX_NETWORK_LOCK, MTX_DEF);
1679f196ce38SLuigi Rizzo 	if (na->nm_lock == NULL) {
1680f196ce38SLuigi Rizzo 		ND("using default locks for %s", ifp->if_xname);
16811a26580eSLuigi Rizzo 		na->nm_lock = netmap_lock_wrapper;
1682f196ce38SLuigi Rizzo 	}
1683*849bec0eSLuigi Rizzo 
168464ae02c3SLuigi Rizzo #ifdef linux
1685*849bec0eSLuigi Rizzo 	if (!ifp->netdev_ops) {
1686*849bec0eSLuigi Rizzo 		D("ouch, we cannot override netdev_ops");
1687*849bec0eSLuigi Rizzo 		goto fail;
1688f196ce38SLuigi Rizzo 	}
1689*849bec0eSLuigi Rizzo #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
1690*849bec0eSLuigi Rizzo 	/* if needed, prepare a clone of the entire netdev ops */
1691*849bec0eSLuigi Rizzo 	na->nm_ndo = *ifp->netdev_ops;
1692*849bec0eSLuigi Rizzo #endif /* 2.6.28 and above */
169342a3a5bdSLuigi Rizzo 	na->nm_ndo.ndo_start_xmit = linux_netmap_start;
1694*849bec0eSLuigi Rizzo #endif /* linux */
1695*849bec0eSLuigi Rizzo 
1696ae10d1afSLuigi Rizzo 	D("success for %s", ifp->if_xname);
1697ae10d1afSLuigi Rizzo 	return 0;
169868b8534bSLuigi Rizzo 
1699ae10d1afSLuigi Rizzo fail:
1700ae10d1afSLuigi Rizzo 	D("fail, arg %p ifp %p na %p", arg, ifp, na);
1701*849bec0eSLuigi Rizzo 	netmap_detach(ifp);
1702ae10d1afSLuigi Rizzo 	return (na ? EINVAL : ENOMEM);
170368b8534bSLuigi Rizzo }
170468b8534bSLuigi Rizzo 
170568b8534bSLuigi Rizzo 
170668b8534bSLuigi Rizzo /*
170768b8534bSLuigi Rizzo  * Free the allocated memory linked to the given ``netmap_adapter``
170868b8534bSLuigi Rizzo  * object.
170968b8534bSLuigi Rizzo  */
171068b8534bSLuigi Rizzo void
171168b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp)
171268b8534bSLuigi Rizzo {
171368b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
171468b8534bSLuigi Rizzo 
171568b8534bSLuigi Rizzo 	if (!na)
171668b8534bSLuigi Rizzo 		return;
171768b8534bSLuigi Rizzo 
17182f70fca5SEd Maste 	mtx_destroy(&na->core_lock);
17192f70fca5SEd Maste 
1720ae10d1afSLuigi Rizzo 	if (na->tx_rings) { /* XXX should not happen */
1721ae10d1afSLuigi Rizzo 		D("freeing leftover tx_rings");
1722ae10d1afSLuigi Rizzo 		free(na->tx_rings, M_DEVBUF);
1723ae10d1afSLuigi Rizzo 	}
172468b8534bSLuigi Rizzo 	bzero(na, sizeof(*na));
1725d0c7b075SLuigi Rizzo 	WNA(ifp) = NULL;
172668b8534bSLuigi Rizzo 	free(na, M_DEVBUF);
172768b8534bSLuigi Rizzo }
172868b8534bSLuigi Rizzo 
172968b8534bSLuigi Rizzo 
173068b8534bSLuigi Rizzo /*
173102ad4083SLuigi Rizzo  * Intercept packets from the network stack and pass them
173202ad4083SLuigi Rizzo  * to netmap as incoming packets on the 'software' ring.
173368b8534bSLuigi Rizzo  * We are not locked when called.
173468b8534bSLuigi Rizzo  */
173568b8534bSLuigi Rizzo int
173668b8534bSLuigi Rizzo netmap_start(struct ifnet *ifp, struct mbuf *m)
173768b8534bSLuigi Rizzo {
173868b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
1739d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
17401a26580eSLuigi Rizzo 	u_int i, len = MBUF_LEN(m);
1741b3d53016SLuigi Rizzo 	u_int error = EBUSY, lim = kring->nkr_num_slots - 1;
174268b8534bSLuigi Rizzo 	struct netmap_slot *slot;
174368b8534bSLuigi Rizzo 
174468b8534bSLuigi Rizzo 	if (netmap_verbose & NM_VERB_HOST)
174568b8534bSLuigi Rizzo 		D("%s packet %d len %d from the stack", ifp->if_xname,
174668b8534bSLuigi Rizzo 			kring->nr_hwcur + kring->nr_hwavail, len);
1747*849bec0eSLuigi Rizzo 	if (len > NETMAP_BUF_SIZE) { /* too long for us */
1748*849bec0eSLuigi Rizzo 		D("%s from_host, drop packet size %d > %d", ifp->if_xname,
1749*849bec0eSLuigi Rizzo 			len, NETMAP_BUF_SIZE);
1750*849bec0eSLuigi Rizzo 		m_freem(m);
1751*849bec0eSLuigi Rizzo 		return EINVAL;
1752*849bec0eSLuigi Rizzo 	}
17531a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
175402ad4083SLuigi Rizzo 	if (kring->nr_hwavail >= lim) {
17555b248374SLuigi Rizzo 		if (netmap_verbose)
175668b8534bSLuigi Rizzo 			D("stack ring %s full\n", ifp->if_xname);
175768b8534bSLuigi Rizzo 		goto done;	/* no space */
175868b8534bSLuigi Rizzo 	}
175968b8534bSLuigi Rizzo 
176068b8534bSLuigi Rizzo 	/* compute the insert position */
176168b8534bSLuigi Rizzo 	i = kring->nr_hwcur + kring->nr_hwavail;
176202ad4083SLuigi Rizzo 	if (i > lim)
176302ad4083SLuigi Rizzo 		i -= lim + 1;
176468b8534bSLuigi Rizzo 	slot = &kring->ring->slot[i];
176568b8534bSLuigi Rizzo 	m_copydata(m, 0, len, NMB(slot));
176668b8534bSLuigi Rizzo 	slot->len = len;
1767091fd0abSLuigi Rizzo 	slot->flags = kring->nkr_slot_flags;
176868b8534bSLuigi Rizzo 	kring->nr_hwavail++;
176968b8534bSLuigi Rizzo 	if (netmap_verbose  & NM_VERB_HOST)
1770d76bf4ffSLuigi Rizzo 		D("wake up host ring %s %d", na->ifp->if_xname, na->num_rx_rings);
177168b8534bSLuigi Rizzo 	selwakeuppri(&kring->si, PI_NET);
177268b8534bSLuigi Rizzo 	error = 0;
177368b8534bSLuigi Rizzo done:
17741a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
177568b8534bSLuigi Rizzo 
177668b8534bSLuigi Rizzo 	/* release the mbuf in either cases of success or failure. As an
177768b8534bSLuigi Rizzo 	 * alternative, put the mbuf in a free list and free the list
177868b8534bSLuigi Rizzo 	 * only when really necessary.
177968b8534bSLuigi Rizzo 	 */
178068b8534bSLuigi Rizzo 	m_freem(m);
178168b8534bSLuigi Rizzo 
178268b8534bSLuigi Rizzo 	return (error);
178368b8534bSLuigi Rizzo }
178468b8534bSLuigi Rizzo 
178568b8534bSLuigi Rizzo 
178668b8534bSLuigi Rizzo /*
178768b8534bSLuigi Rizzo  * netmap_reset() is called by the driver routines when reinitializing
178868b8534bSLuigi Rizzo  * a ring. The driver is in charge of locking to protect the kring.
178968b8534bSLuigi Rizzo  * If netmap mode is not set just return NULL.
179068b8534bSLuigi Rizzo  */
179168b8534bSLuigi Rizzo struct netmap_slot *
179268b8534bSLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, int n,
179368b8534bSLuigi Rizzo 	u_int new_cur)
179468b8534bSLuigi Rizzo {
179568b8534bSLuigi Rizzo 	struct netmap_kring *kring;
1796506cc70cSLuigi Rizzo 	int new_hwofs, lim;
179768b8534bSLuigi Rizzo 
179868b8534bSLuigi Rizzo 	if (na == NULL)
179968b8534bSLuigi Rizzo 		return NULL;	/* no netmap support here */
180068b8534bSLuigi Rizzo 	if (!(na->ifp->if_capenable & IFCAP_NETMAP))
180168b8534bSLuigi Rizzo 		return NULL;	/* nothing to reinitialize */
180268b8534bSLuigi Rizzo 
180364ae02c3SLuigi Rizzo 	if (tx == NR_TX) {
18048241616dSLuigi Rizzo 		if (n >= na->num_tx_rings)
18058241616dSLuigi Rizzo 			return NULL;
180664ae02c3SLuigi Rizzo 		kring = na->tx_rings + n;
1807506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur - new_cur;
180864ae02c3SLuigi Rizzo 	} else {
18098241616dSLuigi Rizzo 		if (n >= na->num_rx_rings)
18108241616dSLuigi Rizzo 			return NULL;
181164ae02c3SLuigi Rizzo 		kring = na->rx_rings + n;
1812506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur + kring->nr_hwavail - new_cur;
181364ae02c3SLuigi Rizzo 	}
181464ae02c3SLuigi Rizzo 	lim = kring->nkr_num_slots - 1;
1815506cc70cSLuigi Rizzo 	if (new_hwofs > lim)
1816506cc70cSLuigi Rizzo 		new_hwofs -= lim + 1;
1817506cc70cSLuigi Rizzo 
1818506cc70cSLuigi Rizzo 	/* Alwayws set the new offset value and realign the ring. */
1819506cc70cSLuigi Rizzo 	kring->nkr_hwofs = new_hwofs;
1820506cc70cSLuigi Rizzo 	if (tx == NR_TX)
1821506cc70cSLuigi Rizzo 		kring->nr_hwavail = kring->nkr_num_slots - 1;
18228241616dSLuigi Rizzo 	ND(10, "new hwofs %d on %s %s[%d]",
1823506cc70cSLuigi Rizzo 			kring->nkr_hwofs, na->ifp->if_xname,
1824506cc70cSLuigi Rizzo 			tx == NR_TX ? "TX" : "RX", n);
1825506cc70cSLuigi Rizzo 
1826f196ce38SLuigi Rizzo #if 0 // def linux
1827f196ce38SLuigi Rizzo 	/* XXX check that the mappings are correct */
1828f196ce38SLuigi Rizzo 	/* need ring_nr, adapter->pdev, direction */
1829f196ce38SLuigi Rizzo 	buffer_info->dma = dma_map_single(&pdev->dev, addr, adapter->rx_buffer_len, DMA_FROM_DEVICE);
1830f196ce38SLuigi Rizzo 	if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
1831f196ce38SLuigi Rizzo 		D("error mapping rx netmap buffer %d", i);
1832f196ce38SLuigi Rizzo 		// XXX fix error handling
1833f196ce38SLuigi Rizzo 	}
1834f196ce38SLuigi Rizzo 
1835f196ce38SLuigi Rizzo #endif /* linux */
183668b8534bSLuigi Rizzo 	/*
183764ae02c3SLuigi Rizzo 	 * Wakeup on the individual and global lock
1838506cc70cSLuigi Rizzo 	 * We do the wakeup here, but the ring is not yet reconfigured.
1839506cc70cSLuigi Rizzo 	 * However, we are under lock so there are no races.
184068b8534bSLuigi Rizzo 	 */
184168b8534bSLuigi Rizzo 	selwakeuppri(&kring->si, PI_NET);
184264ae02c3SLuigi Rizzo 	selwakeuppri(tx == NR_TX ? &na->tx_si : &na->rx_si, PI_NET);
184368b8534bSLuigi Rizzo 	return kring->ring->slot;
184468b8534bSLuigi Rizzo }
184568b8534bSLuigi Rizzo 
184668b8534bSLuigi Rizzo 
184768b8534bSLuigi Rizzo /*
18481a26580eSLuigi Rizzo  * Default functions to handle rx/tx interrupts
18491a26580eSLuigi Rizzo  * we have 4 cases:
18501a26580eSLuigi Rizzo  * 1 ring, single lock:
18511a26580eSLuigi Rizzo  *	lock(core); wake(i=0); unlock(core)
18521a26580eSLuigi Rizzo  * N rings, single lock:
18531a26580eSLuigi Rizzo  *	lock(core); wake(i); wake(N+1) unlock(core)
18541a26580eSLuigi Rizzo  * 1 ring, separate locks: (i=0)
18551a26580eSLuigi Rizzo  *	lock(i); wake(i); unlock(i)
18561a26580eSLuigi Rizzo  * N rings, separate locks:
18571a26580eSLuigi Rizzo  *	lock(i); wake(i); unlock(i); lock(core) wake(N+1) unlock(core)
185864ae02c3SLuigi Rizzo  * work_done is non-null on the RX path.
1859*849bec0eSLuigi Rizzo  *
1860*849bec0eSLuigi Rizzo  * The 'q' argument also includes flag to tell whether the queue is
1861*849bec0eSLuigi Rizzo  * already locked on enter, and whether it should remain locked on exit.
1862*849bec0eSLuigi Rizzo  * This helps adapting to different defaults in drivers and OSes.
18631a26580eSLuigi Rizzo  */
1864babc7c12SLuigi Rizzo int
1865babc7c12SLuigi Rizzo netmap_rx_irq(struct ifnet *ifp, int q, int *work_done)
18661a26580eSLuigi Rizzo {
18671a26580eSLuigi Rizzo 	struct netmap_adapter *na;
18681a26580eSLuigi Rizzo 	struct netmap_kring *r;
186964ae02c3SLuigi Rizzo 	NM_SELINFO_T *main_wq;
1870*849bec0eSLuigi Rizzo 	int locktype, unlocktype, lock;
18711a26580eSLuigi Rizzo 
18721a26580eSLuigi Rizzo 	if (!(ifp->if_capenable & IFCAP_NETMAP))
18731a26580eSLuigi Rizzo 		return 0;
1874*849bec0eSLuigi Rizzo 
1875*849bec0eSLuigi Rizzo 	lock = q & (NETMAP_LOCKED_ENTER | NETMAP_LOCKED_EXIT);
1876*849bec0eSLuigi Rizzo 	q = q & NETMAP_RING_MASK;
1877*849bec0eSLuigi Rizzo 
18788241616dSLuigi Rizzo 	ND(5, "received %s queue %d", work_done ? "RX" : "TX" , q);
18791a26580eSLuigi Rizzo 	na = NA(ifp);
18808241616dSLuigi Rizzo 	if (na->na_flags & NAF_SKIP_INTR) {
18818241616dSLuigi Rizzo 		ND("use regular interrupt");
18828241616dSLuigi Rizzo 		return 0;
18838241616dSLuigi Rizzo 	}
18848241616dSLuigi Rizzo 
188564ae02c3SLuigi Rizzo 	if (work_done) { /* RX path */
18868241616dSLuigi Rizzo 		if (q >= na->num_rx_rings)
1887*849bec0eSLuigi Rizzo 			return 0;	// not a physical queue
188864ae02c3SLuigi Rizzo 		r = na->rx_rings + q;
188964ae02c3SLuigi Rizzo 		r->nr_kflags |= NKR_PENDINTR;
1890d76bf4ffSLuigi Rizzo 		main_wq = (na->num_rx_rings > 1) ? &na->rx_si : NULL;
1891*849bec0eSLuigi Rizzo 		locktype = NETMAP_RX_LOCK;
1892*849bec0eSLuigi Rizzo 		unlocktype = NETMAP_RX_UNLOCK;
1893*849bec0eSLuigi Rizzo 	} else { /* TX path */
18948241616dSLuigi Rizzo 		if (q >= na->num_tx_rings)
1895*849bec0eSLuigi Rizzo 			return 0;	// not a physical queue
189664ae02c3SLuigi Rizzo 		r = na->tx_rings + q;
1897d76bf4ffSLuigi Rizzo 		main_wq = (na->num_tx_rings > 1) ? &na->tx_si : NULL;
189864ae02c3SLuigi Rizzo 		work_done = &q; /* dummy */
1899*849bec0eSLuigi Rizzo 		locktype = NETMAP_TX_LOCK;
1900*849bec0eSLuigi Rizzo 		unlocktype = NETMAP_TX_UNLOCK;
190164ae02c3SLuigi Rizzo 	}
19021a26580eSLuigi Rizzo 	if (na->separate_locks) {
1903*849bec0eSLuigi Rizzo 		if (!(lock & NETMAP_LOCKED_ENTER))
1904*849bec0eSLuigi Rizzo 			na->nm_lock(ifp, locktype, q);
190564ae02c3SLuigi Rizzo 		selwakeuppri(&r->si, PI_NET);
1906*849bec0eSLuigi Rizzo 		na->nm_lock(ifp, unlocktype, q);
190764ae02c3SLuigi Rizzo 		if (main_wq) {
1908*849bec0eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
190964ae02c3SLuigi Rizzo 			selwakeuppri(main_wq, PI_NET);
1910*849bec0eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
19111a26580eSLuigi Rizzo 		}
1912*849bec0eSLuigi Rizzo 		/* lock the queue again if requested */
1913*849bec0eSLuigi Rizzo 		if (lock & NETMAP_LOCKED_EXIT)
1914*849bec0eSLuigi Rizzo 			na->nm_lock(ifp, locktype, q);
19151a26580eSLuigi Rizzo 	} else {
1916*849bec0eSLuigi Rizzo 		if (!(lock & NETMAP_LOCKED_ENTER))
1917*849bec0eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
191864ae02c3SLuigi Rizzo 		selwakeuppri(&r->si, PI_NET);
191964ae02c3SLuigi Rizzo 		if (main_wq)
192064ae02c3SLuigi Rizzo 			selwakeuppri(main_wq, PI_NET);
1921*849bec0eSLuigi Rizzo 		if (!(lock & NETMAP_LOCKED_EXIT))
1922*849bec0eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
19231a26580eSLuigi Rizzo 	}
19241a26580eSLuigi Rizzo 	*work_done = 1; /* do not fire napi again */
19251a26580eSLuigi Rizzo 	return 1;
19261a26580eSLuigi Rizzo }
19271a26580eSLuigi Rizzo 
192864ae02c3SLuigi Rizzo 
192901c7d25fSLuigi Rizzo #ifdef linux	/* linux-specific routines */
193001c7d25fSLuigi Rizzo 
193101c7d25fSLuigi Rizzo /*
193201c7d25fSLuigi Rizzo  * Remap linux arguments into the FreeBSD call.
193301c7d25fSLuigi Rizzo  * - pwait is the poll table, passed as 'dev';
193401c7d25fSLuigi Rizzo  *   If pwait == NULL someone else already woke up before. We can report
193501c7d25fSLuigi Rizzo  *   events but they are filtered upstream.
193601c7d25fSLuigi Rizzo  *   If pwait != NULL, then pwait->key contains the list of events.
193701c7d25fSLuigi Rizzo  * - events is computed from pwait as above.
193801c7d25fSLuigi Rizzo  * - file is passed as 'td';
193901c7d25fSLuigi Rizzo  */
194001c7d25fSLuigi Rizzo static u_int
194101c7d25fSLuigi Rizzo linux_netmap_poll(struct file * file, struct poll_table_struct *pwait)
194201c7d25fSLuigi Rizzo {
1943*849bec0eSLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
1944*849bec0eSLuigi Rizzo 	int events = POLLIN | POLLOUT; /* XXX maybe... */
1945*849bec0eSLuigi Rizzo #elif LINUX_VERSION_CODE < KERNEL_VERSION(3,4,0)
194601c7d25fSLuigi Rizzo 	int events = pwait ? pwait->key : POLLIN | POLLOUT;
194701c7d25fSLuigi Rizzo #else /* in 3.4.0 field 'key' was renamed to '_key' */
194801c7d25fSLuigi Rizzo 	int events = pwait ? pwait->_key : POLLIN | POLLOUT;
194901c7d25fSLuigi Rizzo #endif
195001c7d25fSLuigi Rizzo 	return netmap_poll((void *)pwait, events, (void *)file);
195101c7d25fSLuigi Rizzo }
195201c7d25fSLuigi Rizzo 
195301c7d25fSLuigi Rizzo static int
195442a3a5bdSLuigi Rizzo linux_netmap_mmap(struct file *f, struct vm_area_struct *vma)
195501c7d25fSLuigi Rizzo {
195601c7d25fSLuigi Rizzo 	int lut_skip, i, j;
195701c7d25fSLuigi Rizzo 	int user_skip = 0;
195801c7d25fSLuigi Rizzo 	struct lut_entry *l_entry;
19598241616dSLuigi Rizzo 	int error = 0;
19608241616dSLuigi Rizzo 	unsigned long off, tomap;
196101c7d25fSLuigi Rizzo 	/*
196201c7d25fSLuigi Rizzo 	 * vma->vm_start: start of mapping user address space
196301c7d25fSLuigi Rizzo 	 * vma->vm_end: end of the mapping user address space
19648241616dSLuigi Rizzo 	 * vma->vm_pfoff: offset of first page in the device
196501c7d25fSLuigi Rizzo 	 */
196601c7d25fSLuigi Rizzo 
196701c7d25fSLuigi Rizzo 	// XXX security checks
196801c7d25fSLuigi Rizzo 
19698241616dSLuigi Rizzo 	error = netmap_get_memory(f->private_data);
19708241616dSLuigi Rizzo 	ND("get_memory returned %d", error);
19718241616dSLuigi Rizzo 	if (error)
19728241616dSLuigi Rizzo 	    return -error;
19738241616dSLuigi Rizzo 
19748241616dSLuigi Rizzo 	off = vma->vm_pgoff << PAGE_SHIFT; /* offset in bytes */
19758241616dSLuigi Rizzo 	tomap = vma->vm_end - vma->vm_start;
19768241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {  /* loop through obj_pools */
19778241616dSLuigi Rizzo 		const struct netmap_obj_pool *p = &nm_mem.pools[i];
197801c7d25fSLuigi Rizzo 		/*
197901c7d25fSLuigi Rizzo 		 * In each pool memory is allocated in clusters
198001c7d25fSLuigi Rizzo 		 * of size _clustsize, each containing clustentries
198101c7d25fSLuigi Rizzo 		 * entries. For each object k we already store the
19828241616dSLuigi Rizzo 		 * vtophys mapping in lut[k] so we use that, scanning
198301c7d25fSLuigi Rizzo 		 * the lut[] array in steps of clustentries,
198401c7d25fSLuigi Rizzo 		 * and we map each cluster (not individual pages,
1985*849bec0eSLuigi Rizzo 		 * it would be overkill -- XXX slow ? 20130415).
198601c7d25fSLuigi Rizzo 		 */
19878241616dSLuigi Rizzo 
19888241616dSLuigi Rizzo 		/*
19898241616dSLuigi Rizzo 		 * We interpret vm_pgoff as an offset into the whole
19908241616dSLuigi Rizzo 		 * netmap memory, as if all clusters where contiguous.
19918241616dSLuigi Rizzo 		 */
19928241616dSLuigi Rizzo 		for (lut_skip = 0, j = 0; j < p->_numclusters; j++, lut_skip += p->clustentries) {
19938241616dSLuigi Rizzo 			unsigned long paddr, mapsize;
19948241616dSLuigi Rizzo 			if (p->_clustsize <= off) {
19958241616dSLuigi Rizzo 				off -= p->_clustsize;
19968241616dSLuigi Rizzo 				continue;
19978241616dSLuigi Rizzo 			}
19988241616dSLuigi Rizzo 			l_entry = &p->lut[lut_skip]; /* first obj in the cluster */
19998241616dSLuigi Rizzo 			paddr = l_entry->paddr + off;
20008241616dSLuigi Rizzo 			mapsize = p->_clustsize - off;
20018241616dSLuigi Rizzo 			off = 0;
20028241616dSLuigi Rizzo 			if (mapsize > tomap)
20038241616dSLuigi Rizzo 				mapsize = tomap;
20048241616dSLuigi Rizzo 			ND("remap_pfn_range(%lx, %lx, %lx)",
20058241616dSLuigi Rizzo 				vma->vm_start + user_skip,
20068241616dSLuigi Rizzo 				paddr >> PAGE_SHIFT, mapsize);
200701c7d25fSLuigi Rizzo 			if (remap_pfn_range(vma, vma->vm_start + user_skip,
20088241616dSLuigi Rizzo 					paddr >> PAGE_SHIFT, mapsize,
200901c7d25fSLuigi Rizzo 					vma->vm_page_prot))
201001c7d25fSLuigi Rizzo 				return -EAGAIN; // XXX check return value
20118241616dSLuigi Rizzo 			user_skip += mapsize;
20128241616dSLuigi Rizzo 			tomap -= mapsize;
20138241616dSLuigi Rizzo 			if (tomap == 0)
20148241616dSLuigi Rizzo 				goto done;
201501c7d25fSLuigi Rizzo 		}
201601c7d25fSLuigi Rizzo 	}
20178241616dSLuigi Rizzo done:
201801c7d25fSLuigi Rizzo 
201901c7d25fSLuigi Rizzo 	return 0;
202001c7d25fSLuigi Rizzo }
202101c7d25fSLuigi Rizzo 
202201c7d25fSLuigi Rizzo static netdev_tx_t
202342a3a5bdSLuigi Rizzo linux_netmap_start(struct sk_buff *skb, struct net_device *dev)
202401c7d25fSLuigi Rizzo {
202501c7d25fSLuigi Rizzo 	netmap_start(dev, skb);
202601c7d25fSLuigi Rizzo 	return (NETDEV_TX_OK);
202701c7d25fSLuigi Rizzo }
202801c7d25fSLuigi Rizzo 
202901c7d25fSLuigi Rizzo 
20300b8ed8e0SLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)	// XXX was 38
203101c7d25fSLuigi Rizzo #define LIN_IOCTL_NAME	.ioctl
203201c7d25fSLuigi Rizzo int
203301c7d25fSLuigi Rizzo linux_netmap_ioctl(struct inode *inode, struct file *file, u_int cmd, u_long data /* arg */)
203401c7d25fSLuigi Rizzo #else
203501c7d25fSLuigi Rizzo #define LIN_IOCTL_NAME	.unlocked_ioctl
203601c7d25fSLuigi Rizzo long
203701c7d25fSLuigi Rizzo linux_netmap_ioctl(struct file *file, u_int cmd, u_long data /* arg */)
203801c7d25fSLuigi Rizzo #endif
203901c7d25fSLuigi Rizzo {
204001c7d25fSLuigi Rizzo 	int ret;
204101c7d25fSLuigi Rizzo 	struct nmreq nmr;
204201c7d25fSLuigi Rizzo 	bzero(&nmr, sizeof(nmr));
204301c7d25fSLuigi Rizzo 
204401c7d25fSLuigi Rizzo 	if (data && copy_from_user(&nmr, (void *)data, sizeof(nmr) ) != 0)
204501c7d25fSLuigi Rizzo 		return -EFAULT;
204601c7d25fSLuigi Rizzo 	ret = netmap_ioctl(NULL, cmd, (caddr_t)&nmr, 0, (void *)file);
204701c7d25fSLuigi Rizzo 	if (data && copy_to_user((void*)data, &nmr, sizeof(nmr) ) != 0)
204801c7d25fSLuigi Rizzo 		return -EFAULT;
204901c7d25fSLuigi Rizzo 	return -ret;
205001c7d25fSLuigi Rizzo }
205101c7d25fSLuigi Rizzo 
205201c7d25fSLuigi Rizzo 
205301c7d25fSLuigi Rizzo static int
20540b8ed8e0SLuigi Rizzo netmap_release(struct inode *inode, struct file *file)
205501c7d25fSLuigi Rizzo {
20560b8ed8e0SLuigi Rizzo 	(void)inode;	/* UNUSED */
205701c7d25fSLuigi Rizzo 	if (file->private_data)
205801c7d25fSLuigi Rizzo 		netmap_dtor(file->private_data);
205901c7d25fSLuigi Rizzo 	return (0);
206001c7d25fSLuigi Rizzo }
206101c7d25fSLuigi Rizzo 
20628241616dSLuigi Rizzo static int
20638241616dSLuigi Rizzo linux_netmap_open(struct inode *inode, struct file *file)
20648241616dSLuigi Rizzo {
20658241616dSLuigi Rizzo 	struct netmap_priv_d *priv;
20668241616dSLuigi Rizzo 	(void)inode;	/* UNUSED */
20678241616dSLuigi Rizzo 
20688241616dSLuigi Rizzo 	priv = malloc(sizeof(struct netmap_priv_d), M_DEVBUF,
20698241616dSLuigi Rizzo 			      M_NOWAIT | M_ZERO);
20708241616dSLuigi Rizzo 	if (priv == NULL)
20718241616dSLuigi Rizzo 		return -ENOMEM;
20728241616dSLuigi Rizzo 
20738241616dSLuigi Rizzo 	file->private_data = priv;
20748241616dSLuigi Rizzo 
20758241616dSLuigi Rizzo 	return (0);
20768241616dSLuigi Rizzo }
207701c7d25fSLuigi Rizzo 
207801c7d25fSLuigi Rizzo static struct file_operations netmap_fops = {
20798241616dSLuigi Rizzo     .open = linux_netmap_open,
208042a3a5bdSLuigi Rizzo     .mmap = linux_netmap_mmap,
208101c7d25fSLuigi Rizzo     LIN_IOCTL_NAME = linux_netmap_ioctl,
208201c7d25fSLuigi Rizzo     .poll = linux_netmap_poll,
208301c7d25fSLuigi Rizzo     .release = netmap_release,
208401c7d25fSLuigi Rizzo };
208501c7d25fSLuigi Rizzo 
208601c7d25fSLuigi Rizzo static struct miscdevice netmap_cdevsw = {	/* same name as FreeBSD */
208701c7d25fSLuigi Rizzo 	MISC_DYNAMIC_MINOR,
208801c7d25fSLuigi Rizzo 	"netmap",
208901c7d25fSLuigi Rizzo 	&netmap_fops,
209001c7d25fSLuigi Rizzo };
209101c7d25fSLuigi Rizzo 
209201c7d25fSLuigi Rizzo static int netmap_init(void);
209301c7d25fSLuigi Rizzo static void netmap_fini(void);
209401c7d25fSLuigi Rizzo 
209542a3a5bdSLuigi Rizzo /* Errors have negative values on linux */
209642a3a5bdSLuigi Rizzo static int linux_netmap_init(void)
209742a3a5bdSLuigi Rizzo {
209842a3a5bdSLuigi Rizzo 	return -netmap_init();
209942a3a5bdSLuigi Rizzo }
210042a3a5bdSLuigi Rizzo 
210142a3a5bdSLuigi Rizzo module_init(linux_netmap_init);
210201c7d25fSLuigi Rizzo module_exit(netmap_fini);
210301c7d25fSLuigi Rizzo /* export certain symbols to other modules */
210401c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_attach);		// driver attach routines
210501c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_detach);		// driver detach routines
210601c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_ring_reinit);	// ring init on error
210701c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_buffer_lut);
210801c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_total_buffers);	// index check
210901c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_buffer_base);
211001c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_reset);		// ring init routines
211101c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_buf_size);
211201c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_rx_irq);		// default irq handler
211301c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_no_pendintr);	// XXX mitigation - should go away
211401c7d25fSLuigi Rizzo 
211501c7d25fSLuigi Rizzo 
211601c7d25fSLuigi Rizzo MODULE_AUTHOR("http://info.iet.unipi.it/~luigi/netmap/");
211701c7d25fSLuigi Rizzo MODULE_DESCRIPTION("The netmap packet I/O framework");
211801c7d25fSLuigi Rizzo MODULE_LICENSE("Dual BSD/GPL"); /* the code here is all BSD. */
211901c7d25fSLuigi Rizzo 
212001c7d25fSLuigi Rizzo #else /* __FreeBSD__ */
212101c7d25fSLuigi Rizzo 
2122babc7c12SLuigi Rizzo static struct cdevsw netmap_cdevsw = {
2123babc7c12SLuigi Rizzo 	.d_version = D_VERSION,
2124babc7c12SLuigi Rizzo 	.d_name = "netmap",
21258241616dSLuigi Rizzo 	.d_open = netmap_open,
2126babc7c12SLuigi Rizzo 	.d_mmap = netmap_mmap,
21278241616dSLuigi Rizzo 	.d_mmap_single = netmap_mmap_single,
2128babc7c12SLuigi Rizzo 	.d_ioctl = netmap_ioctl,
2129babc7c12SLuigi Rizzo 	.d_poll = netmap_poll,
21308241616dSLuigi Rizzo 	.d_close = netmap_close,
2131babc7c12SLuigi Rizzo };
213201c7d25fSLuigi Rizzo #endif /* __FreeBSD__ */
2133babc7c12SLuigi Rizzo 
2134f196ce38SLuigi Rizzo #ifdef NM_BRIDGE
2135f196ce38SLuigi Rizzo /*
2136f196ce38SLuigi Rizzo  *---- support for virtual bridge -----
2137f196ce38SLuigi Rizzo  */
2138f196ce38SLuigi Rizzo 
2139f196ce38SLuigi Rizzo /* ----- FreeBSD if_bridge hash function ------- */
2140f196ce38SLuigi Rizzo 
2141f196ce38SLuigi Rizzo /*
2142f196ce38SLuigi Rizzo  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2143f196ce38SLuigi Rizzo  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2144f196ce38SLuigi Rizzo  *
2145f196ce38SLuigi Rizzo  * http://www.burtleburtle.net/bob/hash/spooky.html
2146f196ce38SLuigi Rizzo  */
2147f196ce38SLuigi Rizzo #define mix(a, b, c)                                                    \
2148f196ce38SLuigi Rizzo do {                                                                    \
2149f196ce38SLuigi Rizzo         a -= b; a -= c; a ^= (c >> 13);                                 \
2150f196ce38SLuigi Rizzo         b -= c; b -= a; b ^= (a << 8);                                  \
2151f196ce38SLuigi Rizzo         c -= a; c -= b; c ^= (b >> 13);                                 \
2152f196ce38SLuigi Rizzo         a -= b; a -= c; a ^= (c >> 12);                                 \
2153f196ce38SLuigi Rizzo         b -= c; b -= a; b ^= (a << 16);                                 \
2154f196ce38SLuigi Rizzo         c -= a; c -= b; c ^= (b >> 5);                                  \
2155f196ce38SLuigi Rizzo         a -= b; a -= c; a ^= (c >> 3);                                  \
2156f196ce38SLuigi Rizzo         b -= c; b -= a; b ^= (a << 10);                                 \
2157f196ce38SLuigi Rizzo         c -= a; c -= b; c ^= (b >> 15);                                 \
2158f196ce38SLuigi Rizzo } while (/*CONSTCOND*/0)
2159f196ce38SLuigi Rizzo 
2160f196ce38SLuigi Rizzo static __inline uint32_t
2161f196ce38SLuigi Rizzo nm_bridge_rthash(const uint8_t *addr)
2162f196ce38SLuigi Rizzo {
2163f196ce38SLuigi Rizzo         uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = 0; // hask key
2164f196ce38SLuigi Rizzo 
2165f196ce38SLuigi Rizzo         b += addr[5] << 8;
2166f196ce38SLuigi Rizzo         b += addr[4];
2167f196ce38SLuigi Rizzo         a += addr[3] << 24;
2168f196ce38SLuigi Rizzo         a += addr[2] << 16;
2169f196ce38SLuigi Rizzo         a += addr[1] << 8;
2170f196ce38SLuigi Rizzo         a += addr[0];
2171f196ce38SLuigi Rizzo 
2172f196ce38SLuigi Rizzo         mix(a, b, c);
2173f196ce38SLuigi Rizzo #define BRIDGE_RTHASH_MASK	(NM_BDG_HASH-1)
2174f196ce38SLuigi Rizzo         return (c & BRIDGE_RTHASH_MASK);
2175f196ce38SLuigi Rizzo }
2176f196ce38SLuigi Rizzo 
2177f196ce38SLuigi Rizzo #undef mix
2178f196ce38SLuigi Rizzo 
2179f196ce38SLuigi Rizzo 
2180f196ce38SLuigi Rizzo static int
2181f196ce38SLuigi Rizzo bdg_netmap_reg(struct ifnet *ifp, int onoff)
2182f196ce38SLuigi Rizzo {
2183f196ce38SLuigi Rizzo 	int i, err = 0;
2184f196ce38SLuigi Rizzo 	struct nm_bridge *b = ifp->if_bridge;
2185f196ce38SLuigi Rizzo 
2186f196ce38SLuigi Rizzo 	BDG_LOCK(b);
2187f196ce38SLuigi Rizzo 	if (onoff) {
2188f196ce38SLuigi Rizzo 		/* the interface must be already in the list.
2189f196ce38SLuigi Rizzo 		 * only need to mark the port as active
2190f196ce38SLuigi Rizzo 		 */
2191f196ce38SLuigi Rizzo 		ND("should attach %s to the bridge", ifp->if_xname);
2192f196ce38SLuigi Rizzo 		for (i=0; i < NM_BDG_MAXPORTS; i++)
2193f196ce38SLuigi Rizzo 			if (b->bdg_ports[i] == ifp)
2194f196ce38SLuigi Rizzo 				break;
2195f196ce38SLuigi Rizzo 		if (i == NM_BDG_MAXPORTS) {
2196f196ce38SLuigi Rizzo 			D("no more ports available");
2197f196ce38SLuigi Rizzo 			err = EINVAL;
2198f196ce38SLuigi Rizzo 			goto done;
2199f196ce38SLuigi Rizzo 		}
2200f196ce38SLuigi Rizzo 		ND("setting %s in netmap mode", ifp->if_xname);
2201f196ce38SLuigi Rizzo 		ifp->if_capenable |= IFCAP_NETMAP;
2202f196ce38SLuigi Rizzo 		NA(ifp)->bdg_port = i;
2203f196ce38SLuigi Rizzo 		b->act_ports |= (1<<i);
2204f196ce38SLuigi Rizzo 		b->bdg_ports[i] = ifp;
2205f196ce38SLuigi Rizzo 	} else {
2206f196ce38SLuigi Rizzo 		/* should be in the list, too -- remove from the mask */
2207f196ce38SLuigi Rizzo 		ND("removing %s from netmap mode", ifp->if_xname);
2208f196ce38SLuigi Rizzo 		ifp->if_capenable &= ~IFCAP_NETMAP;
2209f196ce38SLuigi Rizzo 		i = NA(ifp)->bdg_port;
2210f196ce38SLuigi Rizzo 		b->act_ports &= ~(1<<i);
2211f196ce38SLuigi Rizzo 	}
2212f196ce38SLuigi Rizzo done:
2213f196ce38SLuigi Rizzo 	BDG_UNLOCK(b);
2214f196ce38SLuigi Rizzo 	return err;
2215f196ce38SLuigi Rizzo }
2216f196ce38SLuigi Rizzo 
2217f196ce38SLuigi Rizzo 
2218f196ce38SLuigi Rizzo static int
2219f196ce38SLuigi Rizzo nm_bdg_flush(struct nm_bdg_fwd *ft, int n, struct ifnet *ifp)
2220f196ce38SLuigi Rizzo {
2221f196ce38SLuigi Rizzo 	int i, ifn;
2222f196ce38SLuigi Rizzo 	uint64_t all_dst, dst;
2223f196ce38SLuigi Rizzo 	uint32_t sh, dh;
2224f196ce38SLuigi Rizzo 	uint64_t mysrc = 1 << NA(ifp)->bdg_port;
2225f196ce38SLuigi Rizzo 	uint64_t smac, dmac;
2226f196ce38SLuigi Rizzo 	struct netmap_slot *slot;
2227f196ce38SLuigi Rizzo 	struct nm_bridge *b = ifp->if_bridge;
2228f196ce38SLuigi Rizzo 
2229f196ce38SLuigi Rizzo 	ND("prepare to send %d packets, act_ports 0x%x", n, b->act_ports);
2230f196ce38SLuigi Rizzo 	/* only consider valid destinations */
2231f196ce38SLuigi Rizzo 	all_dst = (b->act_ports & ~mysrc);
2232f196ce38SLuigi Rizzo 	/* first pass: hash and find destinations */
2233f196ce38SLuigi Rizzo 	for (i = 0; likely(i < n); i++) {
2234f196ce38SLuigi Rizzo 		uint8_t *buf = ft[i].buf;
2235f196ce38SLuigi Rizzo 		dmac = le64toh(*(uint64_t *)(buf)) & 0xffffffffffff;
2236f196ce38SLuigi Rizzo 		smac = le64toh(*(uint64_t *)(buf + 4));
2237f196ce38SLuigi Rizzo 		smac >>= 16;
2238f196ce38SLuigi Rizzo 		if (unlikely(netmap_verbose)) {
2239f196ce38SLuigi Rizzo 		    uint8_t *s = buf+6, *d = buf;
2240f196ce38SLuigi Rizzo 		    D("%d len %4d %02x:%02x:%02x:%02x:%02x:%02x -> %02x:%02x:%02x:%02x:%02x:%02x",
2241f196ce38SLuigi Rizzo 			i,
2242f196ce38SLuigi Rizzo 			ft[i].len,
2243f196ce38SLuigi Rizzo 			s[0], s[1], s[2], s[3], s[4], s[5],
2244f196ce38SLuigi Rizzo 			d[0], d[1], d[2], d[3], d[4], d[5]);
2245f196ce38SLuigi Rizzo 		}
2246f196ce38SLuigi Rizzo 		/*
2247f196ce38SLuigi Rizzo 		 * The hash is somewhat expensive, there might be some
2248f196ce38SLuigi Rizzo 		 * worthwhile optimizations here.
2249f196ce38SLuigi Rizzo 		 */
2250f196ce38SLuigi Rizzo 		if ((buf[6] & 1) == 0) { /* valid src */
2251f196ce38SLuigi Rizzo 		    	uint8_t *s = buf+6;
2252f196ce38SLuigi Rizzo 			sh = nm_bridge_rthash(buf+6); // XXX hash of source
2253f196ce38SLuigi Rizzo 			/* update source port forwarding entry */
2254f196ce38SLuigi Rizzo 			b->ht[sh].mac = smac;	/* XXX expire ? */
2255f196ce38SLuigi Rizzo 			b->ht[sh].ports = mysrc;
2256f196ce38SLuigi Rizzo 			if (netmap_verbose)
2257f196ce38SLuigi Rizzo 			    D("src %02x:%02x:%02x:%02x:%02x:%02x on port %d",
2258f196ce38SLuigi Rizzo 				s[0], s[1], s[2], s[3], s[4], s[5], NA(ifp)->bdg_port);
2259f196ce38SLuigi Rizzo 		}
2260f196ce38SLuigi Rizzo 		dst = 0;
2261f196ce38SLuigi Rizzo 		if ( (buf[0] & 1) == 0) { /* unicast */
2262f196ce38SLuigi Rizzo 		    	uint8_t *d = buf;
2263f196ce38SLuigi Rizzo 			dh = nm_bridge_rthash(buf); // XXX hash of dst
2264f196ce38SLuigi Rizzo 			if (b->ht[dh].mac == dmac) {	/* found dst */
2265f196ce38SLuigi Rizzo 				dst = b->ht[dh].ports;
2266f196ce38SLuigi Rizzo 				if (netmap_verbose)
2267f196ce38SLuigi Rizzo 				    D("dst %02x:%02x:%02x:%02x:%02x:%02x to port %x",
2268f196ce38SLuigi Rizzo 					d[0], d[1], d[2], d[3], d[4], d[5], (uint32_t)(dst >> 16));
2269f196ce38SLuigi Rizzo 			}
2270f196ce38SLuigi Rizzo 		}
2271f196ce38SLuigi Rizzo 		if (dst == 0)
2272f196ce38SLuigi Rizzo 			dst = all_dst;
2273f196ce38SLuigi Rizzo 		dst &= all_dst; /* only consider valid ports */
2274f196ce38SLuigi Rizzo 		if (unlikely(netmap_verbose))
2275f196ce38SLuigi Rizzo 			D("pkt goes to ports 0x%x", (uint32_t)dst);
2276f196ce38SLuigi Rizzo 		ft[i].dst = dst;
2277f196ce38SLuigi Rizzo 	}
2278f196ce38SLuigi Rizzo 
2279f196ce38SLuigi Rizzo 	/* second pass, scan interfaces and forward */
2280f196ce38SLuigi Rizzo 	all_dst = (b->act_ports & ~mysrc);
2281f196ce38SLuigi Rizzo 	for (ifn = 0; all_dst; ifn++) {
2282f196ce38SLuigi Rizzo 		struct ifnet *dst_ifp = b->bdg_ports[ifn];
2283f196ce38SLuigi Rizzo 		struct netmap_adapter *na;
2284f196ce38SLuigi Rizzo 		struct netmap_kring *kring;
2285f196ce38SLuigi Rizzo 		struct netmap_ring *ring;
2286f196ce38SLuigi Rizzo 		int j, lim, sent, locked;
2287f196ce38SLuigi Rizzo 
2288f196ce38SLuigi Rizzo 		if (!dst_ifp)
2289f196ce38SLuigi Rizzo 			continue;
2290f196ce38SLuigi Rizzo 		ND("scan port %d %s", ifn, dst_ifp->if_xname);
2291f196ce38SLuigi Rizzo 		dst = 1 << ifn;
2292f196ce38SLuigi Rizzo 		if ((dst & all_dst) == 0)	/* skip if not set */
2293f196ce38SLuigi Rizzo 			continue;
2294f196ce38SLuigi Rizzo 		all_dst &= ~dst;	/* clear current node */
2295f196ce38SLuigi Rizzo 		na = NA(dst_ifp);
2296f196ce38SLuigi Rizzo 
2297f196ce38SLuigi Rizzo 		ring = NULL;
2298f196ce38SLuigi Rizzo 		kring = NULL;
2299f196ce38SLuigi Rizzo 		lim = sent = locked = 0;
2300f196ce38SLuigi Rizzo 		/* inside, scan slots */
2301f196ce38SLuigi Rizzo 		for (i = 0; likely(i < n); i++) {
2302f196ce38SLuigi Rizzo 			if ((ft[i].dst & dst) == 0)
2303f196ce38SLuigi Rizzo 				continue;	/* not here */
2304f196ce38SLuigi Rizzo 			if (!locked) {
2305f196ce38SLuigi Rizzo 				kring = &na->rx_rings[0];
2306f196ce38SLuigi Rizzo 				ring = kring->ring;
2307f196ce38SLuigi Rizzo 				lim = kring->nkr_num_slots - 1;
2308f196ce38SLuigi Rizzo 				na->nm_lock(dst_ifp, NETMAP_RX_LOCK, 0);
2309f196ce38SLuigi Rizzo 				locked = 1;
2310f196ce38SLuigi Rizzo 			}
2311f196ce38SLuigi Rizzo 			if (unlikely(kring->nr_hwavail >= lim)) {
2312f196ce38SLuigi Rizzo 				if (netmap_verbose)
2313f196ce38SLuigi Rizzo 					D("rx ring full on %s", ifp->if_xname);
2314f196ce38SLuigi Rizzo 				break;
2315f196ce38SLuigi Rizzo 			}
2316f196ce38SLuigi Rizzo 			j = kring->nr_hwcur + kring->nr_hwavail;
2317f196ce38SLuigi Rizzo 			if (j > lim)
2318f196ce38SLuigi Rizzo 				j -= kring->nkr_num_slots;
2319f196ce38SLuigi Rizzo 			slot = &ring->slot[j];
2320f196ce38SLuigi Rizzo 			ND("send %d %d bytes at %s:%d", i, ft[i].len, dst_ifp->if_xname, j);
2321f196ce38SLuigi Rizzo 			pkt_copy(ft[i].buf, NMB(slot), ft[i].len);
2322f196ce38SLuigi Rizzo 			slot->len = ft[i].len;
2323f196ce38SLuigi Rizzo 			kring->nr_hwavail++;
2324f196ce38SLuigi Rizzo 			sent++;
2325f196ce38SLuigi Rizzo 		}
2326f196ce38SLuigi Rizzo 		if (locked) {
2327f196ce38SLuigi Rizzo 			ND("sent %d on %s", sent, dst_ifp->if_xname);
2328f196ce38SLuigi Rizzo 			if (sent)
2329f196ce38SLuigi Rizzo 				selwakeuppri(&kring->si, PI_NET);
2330f196ce38SLuigi Rizzo 			na->nm_lock(dst_ifp, NETMAP_RX_UNLOCK, 0);
2331f196ce38SLuigi Rizzo 		}
2332f196ce38SLuigi Rizzo 	}
2333f196ce38SLuigi Rizzo 	return 0;
2334f196ce38SLuigi Rizzo }
2335f196ce38SLuigi Rizzo 
2336f196ce38SLuigi Rizzo /*
2337f196ce38SLuigi Rizzo  * main dispatch routine
2338f196ce38SLuigi Rizzo  */
2339f196ce38SLuigi Rizzo static int
2340f196ce38SLuigi Rizzo bdg_netmap_txsync(struct ifnet *ifp, u_int ring_nr, int do_lock)
2341f196ce38SLuigi Rizzo {
2342f196ce38SLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
2343f196ce38SLuigi Rizzo 	struct netmap_kring *kring = &na->tx_rings[ring_nr];
2344f196ce38SLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
2345f196ce38SLuigi Rizzo 	int i, j, k, lim = kring->nkr_num_slots - 1;
2346f196ce38SLuigi Rizzo 	struct nm_bdg_fwd *ft = (struct nm_bdg_fwd *)(ifp + 1);
2347f196ce38SLuigi Rizzo 	int ft_i;	/* position in the forwarding table */
2348f196ce38SLuigi Rizzo 
2349f196ce38SLuigi Rizzo 	k = ring->cur;
2350f196ce38SLuigi Rizzo 	if (k > lim)
2351f196ce38SLuigi Rizzo 		return netmap_ring_reinit(kring);
2352f196ce38SLuigi Rizzo 	if (do_lock)
2353f196ce38SLuigi Rizzo 		na->nm_lock(ifp, NETMAP_TX_LOCK, ring_nr);
2354f196ce38SLuigi Rizzo 
2355f196ce38SLuigi Rizzo 	if (netmap_bridge <= 0) { /* testing only */
2356f196ce38SLuigi Rizzo 		j = k; // used all
2357f196ce38SLuigi Rizzo 		goto done;
2358f196ce38SLuigi Rizzo 	}
2359f196ce38SLuigi Rizzo 	if (netmap_bridge > NM_BDG_BATCH)
2360f196ce38SLuigi Rizzo 		netmap_bridge = NM_BDG_BATCH;
2361f196ce38SLuigi Rizzo 
2362f196ce38SLuigi Rizzo 	ft_i = 0;	/* start from 0 */
2363f196ce38SLuigi Rizzo 	for (j = kring->nr_hwcur; likely(j != k); j = unlikely(j == lim) ? 0 : j+1) {
2364f196ce38SLuigi Rizzo 		struct netmap_slot *slot = &ring->slot[j];
2365f196ce38SLuigi Rizzo 		int len = ft[ft_i].len = slot->len;
2366f196ce38SLuigi Rizzo 		char *buf = ft[ft_i].buf = NMB(slot);
2367f196ce38SLuigi Rizzo 
2368f196ce38SLuigi Rizzo 		prefetch(buf);
2369f196ce38SLuigi Rizzo 		if (unlikely(len < 14))
2370f196ce38SLuigi Rizzo 			continue;
2371f196ce38SLuigi Rizzo 		if (unlikely(++ft_i == netmap_bridge))
2372f196ce38SLuigi Rizzo 			ft_i = nm_bdg_flush(ft, ft_i, ifp);
2373f196ce38SLuigi Rizzo 	}
2374f196ce38SLuigi Rizzo 	if (ft_i)
2375f196ce38SLuigi Rizzo 		ft_i = nm_bdg_flush(ft, ft_i, ifp);
2376f196ce38SLuigi Rizzo 	/* count how many packets we sent */
2377f196ce38SLuigi Rizzo 	i = k - j;
2378f196ce38SLuigi Rizzo 	if (i < 0)
2379f196ce38SLuigi Rizzo 		i += kring->nkr_num_slots;
2380f196ce38SLuigi Rizzo 	kring->nr_hwavail = kring->nkr_num_slots - 1 - i;
2381f196ce38SLuigi Rizzo 	if (j != k)
2382f196ce38SLuigi Rizzo 		D("early break at %d/ %d, avail %d", j, k, kring->nr_hwavail);
2383f196ce38SLuigi Rizzo 
2384f196ce38SLuigi Rizzo done:
2385f196ce38SLuigi Rizzo 	kring->nr_hwcur = j;
2386f196ce38SLuigi Rizzo 	ring->avail = kring->nr_hwavail;
2387f196ce38SLuigi Rizzo 	if (do_lock)
2388f196ce38SLuigi Rizzo 		na->nm_lock(ifp, NETMAP_TX_UNLOCK, ring_nr);
2389f196ce38SLuigi Rizzo 
2390f196ce38SLuigi Rizzo 	if (netmap_verbose)
2391f196ce38SLuigi Rizzo 		D("%s ring %d lock %d", ifp->if_xname, ring_nr, do_lock);
2392f196ce38SLuigi Rizzo 	return 0;
2393f196ce38SLuigi Rizzo }
2394f196ce38SLuigi Rizzo 
2395f196ce38SLuigi Rizzo static int
2396f196ce38SLuigi Rizzo bdg_netmap_rxsync(struct ifnet *ifp, u_int ring_nr, int do_lock)
2397f196ce38SLuigi Rizzo {
2398f196ce38SLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
2399f196ce38SLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[ring_nr];
2400f196ce38SLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
2401b3d53016SLuigi Rizzo 	u_int j, n, lim = kring->nkr_num_slots - 1;
2402f196ce38SLuigi Rizzo 	u_int k = ring->cur, resvd = ring->reserved;
2403f196ce38SLuigi Rizzo 
2404f196ce38SLuigi Rizzo 	ND("%s ring %d lock %d avail %d",
2405f196ce38SLuigi Rizzo 		ifp->if_xname, ring_nr, do_lock, kring->nr_hwavail);
2406f196ce38SLuigi Rizzo 
2407f196ce38SLuigi Rizzo 	if (k > lim)
2408f196ce38SLuigi Rizzo 		return netmap_ring_reinit(kring);
2409f196ce38SLuigi Rizzo 	if (do_lock)
2410f196ce38SLuigi Rizzo 		na->nm_lock(ifp, NETMAP_RX_LOCK, ring_nr);
2411f196ce38SLuigi Rizzo 
2412f196ce38SLuigi Rizzo 	/* skip past packets that userspace has released */
2413f196ce38SLuigi Rizzo 	j = kring->nr_hwcur;    /* netmap ring index */
2414f196ce38SLuigi Rizzo 	if (resvd > 0) {
2415f196ce38SLuigi Rizzo 		if (resvd + ring->avail >= lim + 1) {
2416f196ce38SLuigi Rizzo 			D("XXX invalid reserve/avail %d %d", resvd, ring->avail);
2417f196ce38SLuigi Rizzo 			ring->reserved = resvd = 0; // XXX panic...
2418f196ce38SLuigi Rizzo 		}
2419f196ce38SLuigi Rizzo 		k = (k >= resvd) ? k - resvd : k + lim + 1 - resvd;
2420f196ce38SLuigi Rizzo 	}
2421f196ce38SLuigi Rizzo 
2422f196ce38SLuigi Rizzo 	if (j != k) { /* userspace has released some packets. */
2423f196ce38SLuigi Rizzo 		n = k - j;
2424f196ce38SLuigi Rizzo 		if (n < 0)
2425f196ce38SLuigi Rizzo 			n += kring->nkr_num_slots;
2426f196ce38SLuigi Rizzo 		ND("userspace releases %d packets", n);
2427f196ce38SLuigi Rizzo                 for (n = 0; likely(j != k); n++) {
2428f196ce38SLuigi Rizzo                         struct netmap_slot *slot = &ring->slot[j];
2429f196ce38SLuigi Rizzo                         void *addr = NMB(slot);
2430f196ce38SLuigi Rizzo 
2431f196ce38SLuigi Rizzo                         if (addr == netmap_buffer_base) { /* bad buf */
2432f196ce38SLuigi Rizzo                                 if (do_lock)
2433f196ce38SLuigi Rizzo                                         na->nm_lock(ifp, NETMAP_RX_UNLOCK, ring_nr);
2434f196ce38SLuigi Rizzo                                 return netmap_ring_reinit(kring);
2435f196ce38SLuigi Rizzo                         }
2436f196ce38SLuigi Rizzo 			/* decrease refcount for buffer */
2437f196ce38SLuigi Rizzo 
2438f196ce38SLuigi Rizzo 			slot->flags &= ~NS_BUF_CHANGED;
2439f196ce38SLuigi Rizzo                         j = unlikely(j == lim) ? 0 : j + 1;
2440f196ce38SLuigi Rizzo                 }
2441f196ce38SLuigi Rizzo                 kring->nr_hwavail -= n;
2442f196ce38SLuigi Rizzo                 kring->nr_hwcur = k;
2443f196ce38SLuigi Rizzo         }
2444f196ce38SLuigi Rizzo         /* tell userspace that there are new packets */
2445f196ce38SLuigi Rizzo         ring->avail = kring->nr_hwavail - resvd;
2446f196ce38SLuigi Rizzo 
2447f196ce38SLuigi Rizzo 	if (do_lock)
2448f196ce38SLuigi Rizzo 		na->nm_lock(ifp, NETMAP_RX_UNLOCK, ring_nr);
2449f196ce38SLuigi Rizzo 	return 0;
2450f196ce38SLuigi Rizzo }
2451f196ce38SLuigi Rizzo 
2452f196ce38SLuigi Rizzo static void
2453f196ce38SLuigi Rizzo bdg_netmap_attach(struct ifnet *ifp)
2454f196ce38SLuigi Rizzo {
2455f196ce38SLuigi Rizzo 	struct netmap_adapter na;
2456f196ce38SLuigi Rizzo 
2457f196ce38SLuigi Rizzo 	ND("attaching virtual bridge");
2458f196ce38SLuigi Rizzo 	bzero(&na, sizeof(na));
2459f196ce38SLuigi Rizzo 
2460f196ce38SLuigi Rizzo 	na.ifp = ifp;
2461f196ce38SLuigi Rizzo 	na.separate_locks = 1;
2462f196ce38SLuigi Rizzo 	na.num_tx_desc = NM_BRIDGE_RINGSIZE;
2463f196ce38SLuigi Rizzo 	na.num_rx_desc = NM_BRIDGE_RINGSIZE;
2464f196ce38SLuigi Rizzo 	na.nm_txsync = bdg_netmap_txsync;
2465f196ce38SLuigi Rizzo 	na.nm_rxsync = bdg_netmap_rxsync;
2466f196ce38SLuigi Rizzo 	na.nm_register = bdg_netmap_reg;
2467f196ce38SLuigi Rizzo 	netmap_attach(&na, 1);
2468f196ce38SLuigi Rizzo }
2469f196ce38SLuigi Rizzo 
2470f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */
2471babc7c12SLuigi Rizzo 
2472babc7c12SLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */
2473babc7c12SLuigi Rizzo 
2474babc7c12SLuigi Rizzo 
24751a26580eSLuigi Rizzo /*
247668b8534bSLuigi Rizzo  * Module loader.
247768b8534bSLuigi Rizzo  *
247868b8534bSLuigi Rizzo  * Create the /dev/netmap device and initialize all global
247968b8534bSLuigi Rizzo  * variables.
248068b8534bSLuigi Rizzo  *
248168b8534bSLuigi Rizzo  * Return 0 on success, errno on failure.
248268b8534bSLuigi Rizzo  */
248368b8534bSLuigi Rizzo static int
248468b8534bSLuigi Rizzo netmap_init(void)
248568b8534bSLuigi Rizzo {
248668b8534bSLuigi Rizzo 	int error;
248768b8534bSLuigi Rizzo 
248868b8534bSLuigi Rizzo 	error = netmap_memory_init();
248968b8534bSLuigi Rizzo 	if (error != 0) {
249042a3a5bdSLuigi Rizzo 		printf("netmap: unable to initialize the memory allocator.\n");
249168b8534bSLuigi Rizzo 		return (error);
249268b8534bSLuigi Rizzo 	}
24938241616dSLuigi Rizzo 	printf("netmap: loaded module\n");
249468b8534bSLuigi Rizzo 	netmap_dev = make_dev(&netmap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660,
249568b8534bSLuigi Rizzo 			      "netmap");
2496f196ce38SLuigi Rizzo 
2497f196ce38SLuigi Rizzo #ifdef NM_BRIDGE
2498f196ce38SLuigi Rizzo 	{
2499f196ce38SLuigi Rizzo 	int i;
2500f196ce38SLuigi Rizzo 	for (i = 0; i < NM_BRIDGES; i++)
2501f196ce38SLuigi Rizzo 		mtx_init(&nm_bridges[i].bdg_lock, "bdg lock", "bdg_lock", MTX_DEF);
2502f196ce38SLuigi Rizzo 	}
2503f196ce38SLuigi Rizzo #endif
2504babc7c12SLuigi Rizzo 	return (error);
250568b8534bSLuigi Rizzo }
250668b8534bSLuigi Rizzo 
250768b8534bSLuigi Rizzo 
250868b8534bSLuigi Rizzo /*
250968b8534bSLuigi Rizzo  * Module unloader.
251068b8534bSLuigi Rizzo  *
251168b8534bSLuigi Rizzo  * Free all the memory, and destroy the ``/dev/netmap`` device.
251268b8534bSLuigi Rizzo  */
251368b8534bSLuigi Rizzo static void
251468b8534bSLuigi Rizzo netmap_fini(void)
251568b8534bSLuigi Rizzo {
251668b8534bSLuigi Rizzo 	destroy_dev(netmap_dev);
251768b8534bSLuigi Rizzo 	netmap_memory_fini();
251868b8534bSLuigi Rizzo 	printf("netmap: unloaded module.\n");
251968b8534bSLuigi Rizzo }
252068b8534bSLuigi Rizzo 
252168b8534bSLuigi Rizzo 
2522f196ce38SLuigi Rizzo #ifdef __FreeBSD__
252368b8534bSLuigi Rizzo /*
252468b8534bSLuigi Rizzo  * Kernel entry point.
252568b8534bSLuigi Rizzo  *
252668b8534bSLuigi Rizzo  * Initialize/finalize the module and return.
252768b8534bSLuigi Rizzo  *
252868b8534bSLuigi Rizzo  * Return 0 on success, errno on failure.
252968b8534bSLuigi Rizzo  */
253068b8534bSLuigi Rizzo static int
253168b8534bSLuigi Rizzo netmap_loader(__unused struct module *module, int event, __unused void *arg)
253268b8534bSLuigi Rizzo {
253368b8534bSLuigi Rizzo 	int error = 0;
253468b8534bSLuigi Rizzo 
253568b8534bSLuigi Rizzo 	switch (event) {
253668b8534bSLuigi Rizzo 	case MOD_LOAD:
253768b8534bSLuigi Rizzo 		error = netmap_init();
253868b8534bSLuigi Rizzo 		break;
253968b8534bSLuigi Rizzo 
254068b8534bSLuigi Rizzo 	case MOD_UNLOAD:
254168b8534bSLuigi Rizzo 		netmap_fini();
254268b8534bSLuigi Rizzo 		break;
254368b8534bSLuigi Rizzo 
254468b8534bSLuigi Rizzo 	default:
254568b8534bSLuigi Rizzo 		error = EOPNOTSUPP;
254668b8534bSLuigi Rizzo 		break;
254768b8534bSLuigi Rizzo 	}
254868b8534bSLuigi Rizzo 
254968b8534bSLuigi Rizzo 	return (error);
255068b8534bSLuigi Rizzo }
255168b8534bSLuigi Rizzo 
255268b8534bSLuigi Rizzo 
255368b8534bSLuigi Rizzo DEV_MODULE(netmap, netmap_loader, NULL);
2554f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */
2555