xref: /freebsd/sys/dev/netmap/netmap.c (revision 0b8ed8e069ba0d3c813c2b3f306168718e3f1ac4)
168b8534bSLuigi Rizzo /*
2f196ce38SLuigi Rizzo  * Copyright (C) 2011-2012 Matteo Landi, Luigi Rizzo. All rights reserved.
368b8534bSLuigi Rizzo  *
468b8534bSLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
568b8534bSLuigi Rizzo  * modification, are permitted provided that the following conditions
668b8534bSLuigi Rizzo  * are met:
768b8534bSLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
868b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
968b8534bSLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
1068b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
1168b8534bSLuigi Rizzo  *    documentation and/or other materials provided with the distribution.
1268b8534bSLuigi Rizzo  *
1368b8534bSLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1468b8534bSLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1568b8534bSLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1668b8534bSLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1768b8534bSLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1868b8534bSLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1968b8534bSLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2068b8534bSLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2168b8534bSLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2268b8534bSLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2368b8534bSLuigi Rizzo  * SUCH DAMAGE.
2468b8534bSLuigi Rizzo  */
2568b8534bSLuigi Rizzo 
26f196ce38SLuigi Rizzo #define NM_BRIDGE
27f196ce38SLuigi Rizzo 
2868b8534bSLuigi Rizzo /*
2968b8534bSLuigi Rizzo  * This module supports memory mapped access to network devices,
3068b8534bSLuigi Rizzo  * see netmap(4).
3168b8534bSLuigi Rizzo  *
3268b8534bSLuigi Rizzo  * The module uses a large, memory pool allocated by the kernel
3368b8534bSLuigi Rizzo  * and accessible as mmapped memory by multiple userspace threads/processes.
3468b8534bSLuigi Rizzo  * The memory pool contains packet buffers and "netmap rings",
3568b8534bSLuigi Rizzo  * i.e. user-accessible copies of the interface's queues.
3668b8534bSLuigi Rizzo  *
3768b8534bSLuigi Rizzo  * Access to the network card works like this:
3868b8534bSLuigi Rizzo  * 1. a process/thread issues one or more open() on /dev/netmap, to create
3968b8534bSLuigi Rizzo  *    select()able file descriptor on which events are reported.
4068b8534bSLuigi Rizzo  * 2. on each descriptor, the process issues an ioctl() to identify
4168b8534bSLuigi Rizzo  *    the interface that should report events to the file descriptor.
4268b8534bSLuigi Rizzo  * 3. on each descriptor, the process issues an mmap() request to
4368b8534bSLuigi Rizzo  *    map the shared memory region within the process' address space.
4468b8534bSLuigi Rizzo  *    The list of interesting queues is indicated by a location in
4568b8534bSLuigi Rizzo  *    the shared memory region.
4668b8534bSLuigi Rizzo  * 4. using the functions in the netmap(4) userspace API, a process
4768b8534bSLuigi Rizzo  *    can look up the occupation state of a queue, access memory buffers,
4868b8534bSLuigi Rizzo  *    and retrieve received packets or enqueue packets to transmit.
4968b8534bSLuigi Rizzo  * 5. using some ioctl()s the process can synchronize the userspace view
5068b8534bSLuigi Rizzo  *    of the queue with the actual status in the kernel. This includes both
5168b8534bSLuigi Rizzo  *    receiving the notification of new packets, and transmitting new
5268b8534bSLuigi Rizzo  *    packets on the output interface.
5368b8534bSLuigi Rizzo  * 6. select() or poll() can be used to wait for events on individual
5468b8534bSLuigi Rizzo  *    transmit or receive queues (or all queues for a given interface).
5568b8534bSLuigi Rizzo  */
5668b8534bSLuigi Rizzo 
57f196ce38SLuigi Rizzo #ifdef linux
58f196ce38SLuigi Rizzo #include "bsd_glue.h"
59f196ce38SLuigi Rizzo static netdev_tx_t netmap_start_linux(struct sk_buff *skb, struct net_device *dev);
60f196ce38SLuigi Rizzo #endif /* linux */
6101c7d25fSLuigi Rizzo 
62f196ce38SLuigi Rizzo #ifdef __APPLE__
63f196ce38SLuigi Rizzo #include "osx_glue.h"
6401c7d25fSLuigi Rizzo #endif /* __APPLE__ */
6501c7d25fSLuigi Rizzo 
66f196ce38SLuigi Rizzo #ifdef __FreeBSD__
6768b8534bSLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */
6868b8534bSLuigi Rizzo __FBSDID("$FreeBSD$");
6968b8534bSLuigi Rizzo 
7068b8534bSLuigi Rizzo #include <sys/types.h>
7168b8534bSLuigi Rizzo #include <sys/module.h>
7268b8534bSLuigi Rizzo #include <sys/errno.h>
7368b8534bSLuigi Rizzo #include <sys/param.h>	/* defines used in kernel.h */
74506cc70cSLuigi Rizzo #include <sys/jail.h>
7568b8534bSLuigi Rizzo #include <sys/kernel.h>	/* types used in module initialization */
7668b8534bSLuigi Rizzo #include <sys/conf.h>	/* cdevsw struct */
7768b8534bSLuigi Rizzo #include <sys/uio.h>	/* uio struct */
7868b8534bSLuigi Rizzo #include <sys/sockio.h>
7968b8534bSLuigi Rizzo #include <sys/socketvar.h>	/* struct socket */
8068b8534bSLuigi Rizzo #include <sys/malloc.h>
8168b8534bSLuigi Rizzo #include <sys/mman.h>	/* PROT_EXEC */
8268b8534bSLuigi Rizzo #include <sys/poll.h>
83506cc70cSLuigi Rizzo #include <sys/proc.h>
8468b8534bSLuigi Rizzo #include <vm/vm.h>	/* vtophys */
8568b8534bSLuigi Rizzo #include <vm/pmap.h>	/* vtophys */
8668b8534bSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
8768b8534bSLuigi Rizzo #include <machine/bus.h>
8868b8534bSLuigi Rizzo #include <sys/selinfo.h>
8968b8534bSLuigi Rizzo #include <sys/sysctl.h>
9068b8534bSLuigi Rizzo #include <net/if.h>
9168b8534bSLuigi Rizzo #include <net/bpf.h>		/* BIOCIMMEDIATE */
92506cc70cSLuigi Rizzo #include <net/vnet.h>
9368b8534bSLuigi Rizzo #include <machine/bus.h>	/* bus_dmamap_* */
9468b8534bSLuigi Rizzo 
9568b8534bSLuigi Rizzo MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map");
96f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */
9768b8534bSLuigi Rizzo 
98*0b8ed8e0SLuigi Rizzo #include <net/netmap.h>
99*0b8ed8e0SLuigi Rizzo #include <dev/netmap/netmap_kern.h>
100*0b8ed8e0SLuigi Rizzo 
10168b8534bSLuigi Rizzo /*
10268b8534bSLuigi Rizzo  * lock and unlock for the netmap memory allocator
10368b8534bSLuigi Rizzo  */
10464ae02c3SLuigi Rizzo #define NMA_LOCK()	mtx_lock(&nm_mem->nm_mtx);
10564ae02c3SLuigi Rizzo #define NMA_UNLOCK()	mtx_unlock(&nm_mem->nm_mtx);
1065819da83SLuigi Rizzo struct netmap_mem_d;
10764ae02c3SLuigi Rizzo static struct netmap_mem_d *nm_mem;	/* Our memory allocator. */
1085819da83SLuigi Rizzo 
1095819da83SLuigi Rizzo u_int netmap_total_buffers;
1105819da83SLuigi Rizzo char *netmap_buffer_base;	/* address of an invalid buffer */
1115819da83SLuigi Rizzo 
1125819da83SLuigi Rizzo /* user-controlled variables */
1135819da83SLuigi Rizzo int netmap_verbose;
1145819da83SLuigi Rizzo 
1155819da83SLuigi Rizzo static int netmap_no_timestamp; /* don't timestamp on rxsync */
1165819da83SLuigi Rizzo 
1175819da83SLuigi Rizzo SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args");
1185819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose,
1195819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode");
1205819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp,
1215819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp");
1225819da83SLuigi Rizzo int netmap_buf_size = 2048;
1235819da83SLuigi Rizzo TUNABLE_INT("hw.netmap.buf_size", &netmap_buf_size);
1245819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, buf_size,
1255819da83SLuigi Rizzo     CTLFLAG_RD, &netmap_buf_size, 0, "Size of packet buffers");
1265819da83SLuigi Rizzo int netmap_mitigate = 1;
1275819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mitigate, CTLFLAG_RW, &netmap_mitigate, 0, "");
128c85cb1a0SLuigi Rizzo int netmap_no_pendintr = 1;
1295819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr,
1305819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_pendintr, 0, "Always look for new received packets.");
1315819da83SLuigi Rizzo 
132f196ce38SLuigi Rizzo int netmap_drop = 0;	/* debugging */
133f196ce38SLuigi Rizzo int netmap_flags = 0;	/* debug flags */
134f196ce38SLuigi Rizzo int netmap_copy = 0;	/* debugging, copy content */
135f196ce38SLuigi Rizzo 
136f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, drop, CTLFLAG_RW, &netmap_drop, 0 , "");
137f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, flags, CTLFLAG_RW, &netmap_flags, 0 , "");
138f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, copy, CTLFLAG_RW, &netmap_copy, 0 , "");
139f196ce38SLuigi Rizzo 
140f196ce38SLuigi Rizzo #ifdef NM_BRIDGE /* support for netmap bridge */
141f196ce38SLuigi Rizzo 
142f196ce38SLuigi Rizzo /*
143f196ce38SLuigi Rizzo  * system parameters.
144f196ce38SLuigi Rizzo  *
145f196ce38SLuigi Rizzo  * All switched ports have prefix NM_NAME.
146f196ce38SLuigi Rizzo  * The switch has a max of NM_BDG_MAXPORTS ports (often stored in a bitmap,
147f196ce38SLuigi Rizzo  * so a practical upper bound is 64).
148f196ce38SLuigi Rizzo  * Each tx ring is read-write, whereas rx rings are readonly (XXX not done yet).
149f196ce38SLuigi Rizzo  * The virtual interfaces use per-queue lock instead of core lock.
150f196ce38SLuigi Rizzo  * In the tx loop, we aggregate traffic in batches to make all operations
151f196ce38SLuigi Rizzo  * faster. The batch size is NM_BDG_BATCH
152f196ce38SLuigi Rizzo  */
153f196ce38SLuigi Rizzo #define	NM_NAME			"vale"	/* prefix for the interface */
154f196ce38SLuigi Rizzo #define NM_BDG_MAXPORTS		16	/* up to 64 ? */
155f196ce38SLuigi Rizzo #define NM_BRIDGE_RINGSIZE	1024	/* in the device */
156f196ce38SLuigi Rizzo #define NM_BDG_HASH		1024	/* forwarding table entries */
157f196ce38SLuigi Rizzo #define NM_BDG_BATCH		1024	/* entries in the forwarding buffer */
158f196ce38SLuigi Rizzo #define	NM_BRIDGES		4	/* number of bridges */
159f196ce38SLuigi Rizzo int netmap_bridge = NM_BDG_BATCH; /* bridge batch size */
160f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, bridge, CTLFLAG_RW, &netmap_bridge, 0 , "");
16101c7d25fSLuigi Rizzo 
162f196ce38SLuigi Rizzo #ifdef linux
163f196ce38SLuigi Rizzo #define	ADD_BDG_REF(ifp)	(NA(ifp)->if_refcount++)
164f196ce38SLuigi Rizzo #define	DROP_BDG_REF(ifp)	(NA(ifp)->if_refcount-- <= 1)
165f196ce38SLuigi Rizzo #else /* !linux */
166f196ce38SLuigi Rizzo #define	ADD_BDG_REF(ifp)	(ifp)->if_refcount++
167f196ce38SLuigi Rizzo #define	DROP_BDG_REF(ifp)	refcount_release(&(ifp)->if_refcount)
168f196ce38SLuigi Rizzo #ifdef __FreeBSD__
169f196ce38SLuigi Rizzo #include <sys/endian.h>
170f196ce38SLuigi Rizzo #include <sys/refcount.h>
171f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */
17201c7d25fSLuigi Rizzo #define prefetch(x)	__builtin_prefetch(x)
173f196ce38SLuigi Rizzo #endif /* !linux */
174f196ce38SLuigi Rizzo 
175f196ce38SLuigi Rizzo static void bdg_netmap_attach(struct ifnet *ifp);
176f196ce38SLuigi Rizzo static int bdg_netmap_reg(struct ifnet *ifp, int onoff);
177f196ce38SLuigi Rizzo /* per-tx-queue entry */
178f196ce38SLuigi Rizzo struct nm_bdg_fwd {	/* forwarding entry for a bridge */
179f196ce38SLuigi Rizzo 	void *buf;
180f196ce38SLuigi Rizzo 	uint64_t dst;	/* dst mask */
181f196ce38SLuigi Rizzo 	uint32_t src;	/* src index ? */
182f196ce38SLuigi Rizzo 	uint16_t len;	/* src len */
183f196ce38SLuigi Rizzo };
184f196ce38SLuigi Rizzo 
185f196ce38SLuigi Rizzo struct nm_hash_ent {
186f196ce38SLuigi Rizzo 	uint64_t	mac;	/* the top 2 bytes are the epoch */
187f196ce38SLuigi Rizzo 	uint64_t	ports;
188f196ce38SLuigi Rizzo };
189f196ce38SLuigi Rizzo 
190f196ce38SLuigi Rizzo /*
191f196ce38SLuigi Rizzo  * Interfaces for a bridge are all in ports[].
192f196ce38SLuigi Rizzo  * The array has fixed size, an empty entry does not terminate
193f196ce38SLuigi Rizzo  * the search.
194f196ce38SLuigi Rizzo  */
195f196ce38SLuigi Rizzo struct nm_bridge {
196f196ce38SLuigi Rizzo 	struct ifnet *bdg_ports[NM_BDG_MAXPORTS];
197f196ce38SLuigi Rizzo 	int n_ports;
198f196ce38SLuigi Rizzo 	uint64_t act_ports;
199f196ce38SLuigi Rizzo 	int freelist;	/* first buffer index */
200f196ce38SLuigi Rizzo 	NM_SELINFO_T si;	/* poll/select wait queue */
201f196ce38SLuigi Rizzo 	NM_LOCK_T bdg_lock;	/* protect the selinfo ? */
202f196ce38SLuigi Rizzo 
203f196ce38SLuigi Rizzo 	/* the forwarding table, MAC+ports */
204f196ce38SLuigi Rizzo 	struct nm_hash_ent ht[NM_BDG_HASH];
205f196ce38SLuigi Rizzo 
206f196ce38SLuigi Rizzo 	int namelen;	/* 0 means free */
207f196ce38SLuigi Rizzo 	char basename[IFNAMSIZ];
208f196ce38SLuigi Rizzo };
209f196ce38SLuigi Rizzo 
210f196ce38SLuigi Rizzo struct nm_bridge nm_bridges[NM_BRIDGES];
211f196ce38SLuigi Rizzo 
212f196ce38SLuigi Rizzo #define BDG_LOCK(b)	mtx_lock(&(b)->bdg_lock)
213f196ce38SLuigi Rizzo #define BDG_UNLOCK(b)	mtx_unlock(&(b)->bdg_lock)
214f196ce38SLuigi Rizzo 
215f196ce38SLuigi Rizzo /*
216f196ce38SLuigi Rizzo  * NA(ifp)->bdg_port	port index
217f196ce38SLuigi Rizzo  */
218f196ce38SLuigi Rizzo 
219f196ce38SLuigi Rizzo // XXX only for multiples of 64 bytes, non overlapped.
220f196ce38SLuigi Rizzo static inline void
221f196ce38SLuigi Rizzo pkt_copy(void *_src, void *_dst, int l)
222f196ce38SLuigi Rizzo {
223f196ce38SLuigi Rizzo         uint64_t *src = _src;
224f196ce38SLuigi Rizzo         uint64_t *dst = _dst;
225f196ce38SLuigi Rizzo         if (unlikely(l >= 1024)) {
226f196ce38SLuigi Rizzo                 bcopy(src, dst, l);
227f196ce38SLuigi Rizzo                 return;
228f196ce38SLuigi Rizzo         }
229f196ce38SLuigi Rizzo         for (; likely(l > 0); l-=64) {
230f196ce38SLuigi Rizzo                 *dst++ = *src++;
231f196ce38SLuigi Rizzo                 *dst++ = *src++;
232f196ce38SLuigi Rizzo                 *dst++ = *src++;
233f196ce38SLuigi Rizzo                 *dst++ = *src++;
234f196ce38SLuigi Rizzo                 *dst++ = *src++;
235f196ce38SLuigi Rizzo                 *dst++ = *src++;
236f196ce38SLuigi Rizzo                 *dst++ = *src++;
237f196ce38SLuigi Rizzo                 *dst++ = *src++;
238f196ce38SLuigi Rizzo         }
239f196ce38SLuigi Rizzo }
240f196ce38SLuigi Rizzo 
241f196ce38SLuigi Rizzo /*
242f196ce38SLuigi Rizzo  * locate a bridge among the existing ones.
243f196ce38SLuigi Rizzo  * a ':' in the name terminates the bridge name. Otherwise, just NM_NAME.
244f196ce38SLuigi Rizzo  * We assume that this is called with a name of at least NM_NAME chars.
245f196ce38SLuigi Rizzo  */
246f196ce38SLuigi Rizzo static struct nm_bridge *
247f196ce38SLuigi Rizzo nm_find_bridge(const char *name)
248f196ce38SLuigi Rizzo {
249f196ce38SLuigi Rizzo 	int i, l, namelen, e;
250f196ce38SLuigi Rizzo 	struct nm_bridge *b = NULL;
251f196ce38SLuigi Rizzo 
252f196ce38SLuigi Rizzo 	namelen = strlen(NM_NAME);	/* base length */
253f196ce38SLuigi Rizzo 	l = strlen(name);		/* actual length */
254f196ce38SLuigi Rizzo 	for (i = namelen + 1; i < l; i++) {
255f196ce38SLuigi Rizzo 		if (name[i] == ':') {
256f196ce38SLuigi Rizzo 			namelen = i;
257f196ce38SLuigi Rizzo 			break;
258f196ce38SLuigi Rizzo 		}
259f196ce38SLuigi Rizzo 	}
260f196ce38SLuigi Rizzo 	if (namelen >= IFNAMSIZ)
261f196ce38SLuigi Rizzo 		namelen = IFNAMSIZ;
262f196ce38SLuigi Rizzo 	ND("--- prefix is '%.*s' ---", namelen, name);
263f196ce38SLuigi Rizzo 
264f196ce38SLuigi Rizzo 	/* use the first entry for locking */
265f196ce38SLuigi Rizzo 	BDG_LOCK(nm_bridges); // XXX do better
266f196ce38SLuigi Rizzo 	for (e = -1, i = 1; i < NM_BRIDGES; i++) {
267f196ce38SLuigi Rizzo 		b = nm_bridges + i;
268f196ce38SLuigi Rizzo 		if (b->namelen == 0)
269f196ce38SLuigi Rizzo 			e = i;	/* record empty slot */
270f196ce38SLuigi Rizzo 		else if (strncmp(name, b->basename, namelen) == 0) {
271f196ce38SLuigi Rizzo 			ND("found '%.*s' at %d", namelen, name, i);
272f196ce38SLuigi Rizzo 			break;
273f196ce38SLuigi Rizzo 		}
274f196ce38SLuigi Rizzo 	}
275f196ce38SLuigi Rizzo 	if (i == NM_BRIDGES) { /* all full */
276f196ce38SLuigi Rizzo 		if (e == -1) { /* no empty slot */
277f196ce38SLuigi Rizzo 			b = NULL;
278f196ce38SLuigi Rizzo 		} else {
279f196ce38SLuigi Rizzo 			b = nm_bridges + e;
280f196ce38SLuigi Rizzo 			strncpy(b->basename, name, namelen);
281f196ce38SLuigi Rizzo 			b->namelen = namelen;
282f196ce38SLuigi Rizzo 		}
283f196ce38SLuigi Rizzo 	}
284f196ce38SLuigi Rizzo 	BDG_UNLOCK(nm_bridges);
285f196ce38SLuigi Rizzo 	return b;
286f196ce38SLuigi Rizzo }
287f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */
2885819da83SLuigi Rizzo 
2893c0caf6cSLuigi Rizzo /*------------- memory allocator -----------------*/
2903c0caf6cSLuigi Rizzo #ifdef NETMAP_MEM2
2913c0caf6cSLuigi Rizzo #include "netmap_mem2.c"
2923c0caf6cSLuigi Rizzo #else /* !NETMAP_MEM2 */
2933c0caf6cSLuigi Rizzo #include "netmap_mem1.c"
2943c0caf6cSLuigi Rizzo #endif /* !NETMAP_MEM2 */
2953c0caf6cSLuigi Rizzo /*------------ end of memory allocator ----------*/
29668b8534bSLuigi Rizzo 
29768b8534bSLuigi Rizzo /* Structure associated to each thread which registered an interface. */
29868b8534bSLuigi Rizzo struct netmap_priv_d {
29968b8534bSLuigi Rizzo 	struct netmap_if *np_nifp;	/* netmap interface descriptor. */
30068b8534bSLuigi Rizzo 
30168b8534bSLuigi Rizzo 	struct ifnet	*np_ifp;	/* device for which we hold a reference */
30268b8534bSLuigi Rizzo 	int		np_ringid;	/* from the ioctl */
30368b8534bSLuigi Rizzo 	u_int		np_qfirst, np_qlast;	/* range of rings to scan */
30468b8534bSLuigi Rizzo 	uint16_t	np_txpoll;
30568b8534bSLuigi Rizzo };
30668b8534bSLuigi Rizzo 
30768b8534bSLuigi Rizzo 
30868b8534bSLuigi Rizzo /*
30968b8534bSLuigi Rizzo  * File descriptor's private data destructor.
31068b8534bSLuigi Rizzo  *
31168b8534bSLuigi Rizzo  * Call nm_register(ifp,0) to stop netmap mode on the interface and
31268b8534bSLuigi Rizzo  * revert to normal operation. We expect that np_ifp has not gone.
31368b8534bSLuigi Rizzo  */
31468b8534bSLuigi Rizzo static void
3155819da83SLuigi Rizzo netmap_dtor_locked(void *data)
31668b8534bSLuigi Rizzo {
31768b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = data;
31868b8534bSLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
31968b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
32068b8534bSLuigi Rizzo 	struct netmap_if *nifp = priv->np_nifp;
32168b8534bSLuigi Rizzo 
32268b8534bSLuigi Rizzo 	na->refcount--;
32368b8534bSLuigi Rizzo 	if (na->refcount <= 0) {	/* last instance */
32464ae02c3SLuigi Rizzo 		u_int i, j, lim;
32568b8534bSLuigi Rizzo 
32668b8534bSLuigi Rizzo 		D("deleting last netmap instance for %s", ifp->if_xname);
32768b8534bSLuigi Rizzo 		/*
32868b8534bSLuigi Rizzo 		 * there is a race here with *_netmap_task() and
3291a26580eSLuigi Rizzo 		 * netmap_poll(), which don't run under NETMAP_REG_LOCK.
33068b8534bSLuigi Rizzo 		 * na->refcount == 0 && na->ifp->if_capenable & IFCAP_NETMAP
33168b8534bSLuigi Rizzo 		 * (aka NETMAP_DELETING(na)) are a unique marker that the
33268b8534bSLuigi Rizzo 		 * device is dying.
33368b8534bSLuigi Rizzo 		 * Before destroying stuff we sleep a bit, and then complete
33468b8534bSLuigi Rizzo 		 * the job. NIOCREG should realize the condition and
33568b8534bSLuigi Rizzo 		 * loop until they can continue; the other routines
33668b8534bSLuigi Rizzo 		 * should check the condition at entry and quit if
33768b8534bSLuigi Rizzo 		 * they cannot run.
33868b8534bSLuigi Rizzo 		 */
3391a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
34068b8534bSLuigi Rizzo 		tsleep(na, 0, "NIOCUNREG", 4);
3411a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
34268b8534bSLuigi Rizzo 		na->nm_register(ifp, 0); /* off, clear IFCAP_NETMAP */
34368b8534bSLuigi Rizzo 		/* Wake up any sleeping threads. netmap_poll will
34468b8534bSLuigi Rizzo 		 * then return POLLERR
34568b8534bSLuigi Rizzo 		 */
346d76bf4ffSLuigi Rizzo 		for (i = 0; i < na->num_tx_rings + 1; i++)
34768b8534bSLuigi Rizzo 			selwakeuppri(&na->tx_rings[i].si, PI_NET);
348d76bf4ffSLuigi Rizzo 		for (i = 0; i < na->num_rx_rings + 1; i++)
34968b8534bSLuigi Rizzo 			selwakeuppri(&na->rx_rings[i].si, PI_NET);
35064ae02c3SLuigi Rizzo 		selwakeuppri(&na->tx_si, PI_NET);
35164ae02c3SLuigi Rizzo 		selwakeuppri(&na->rx_si, PI_NET);
35268b8534bSLuigi Rizzo 		/* release all buffers */
35368b8534bSLuigi Rizzo 		NMA_LOCK();
354d76bf4ffSLuigi Rizzo 		for (i = 0; i < na->num_tx_rings + 1; i++) {
35564ae02c3SLuigi Rizzo 			struct netmap_ring *ring = na->tx_rings[i].ring;
35668b8534bSLuigi Rizzo 			lim = na->tx_rings[i].nkr_num_slots;
35768b8534bSLuigi Rizzo 			for (j = 0; j < lim; j++)
358446ee301SLuigi Rizzo 				netmap_free_buf(nifp, ring->slot[j].buf_idx);
35964ae02c3SLuigi Rizzo 		}
360d76bf4ffSLuigi Rizzo 		for (i = 0; i < na->num_rx_rings + 1; i++) {
36164ae02c3SLuigi Rizzo 			struct netmap_ring *ring = na->rx_rings[i].ring;
36268b8534bSLuigi Rizzo 			lim = na->rx_rings[i].nkr_num_slots;
36368b8534bSLuigi Rizzo 			for (j = 0; j < lim; j++)
364446ee301SLuigi Rizzo 				netmap_free_buf(nifp, ring->slot[j].buf_idx);
36568b8534bSLuigi Rizzo 		}
36668b8534bSLuigi Rizzo 		NMA_UNLOCK();
3676e10c8b8SLuigi Rizzo 		netmap_free_rings(na);
36868b8534bSLuigi Rizzo 		wakeup(na);
36968b8534bSLuigi Rizzo 	}
3706e10c8b8SLuigi Rizzo 	netmap_if_free(nifp);
3715819da83SLuigi Rizzo }
37268b8534bSLuigi Rizzo 
373f196ce38SLuigi Rizzo static void
374f196ce38SLuigi Rizzo nm_if_rele(struct ifnet *ifp)
375f196ce38SLuigi Rizzo {
376f196ce38SLuigi Rizzo #ifndef NM_BRIDGE
377f196ce38SLuigi Rizzo 	if_rele(ifp);
378f196ce38SLuigi Rizzo #else /* NM_BRIDGE */
379f196ce38SLuigi Rizzo 	int i, full;
380f196ce38SLuigi Rizzo 	struct nm_bridge *b;
381f196ce38SLuigi Rizzo 
382f196ce38SLuigi Rizzo 	if (strncmp(ifp->if_xname, NM_NAME, sizeof(NM_NAME) - 1)) {
383f196ce38SLuigi Rizzo 		if_rele(ifp);
384f196ce38SLuigi Rizzo 		return;
385f196ce38SLuigi Rizzo 	}
386f196ce38SLuigi Rizzo 	if (!DROP_BDG_REF(ifp))
387f196ce38SLuigi Rizzo 		return;
388f196ce38SLuigi Rizzo 	b = ifp->if_bridge;
389f196ce38SLuigi Rizzo 	BDG_LOCK(nm_bridges);
390f196ce38SLuigi Rizzo 	BDG_LOCK(b);
391f196ce38SLuigi Rizzo 	ND("want to disconnect %s from the bridge", ifp->if_xname);
392f196ce38SLuigi Rizzo 	full = 0;
393f196ce38SLuigi Rizzo 	for (i = 0; i < NM_BDG_MAXPORTS; i++) {
394f196ce38SLuigi Rizzo 		if (b->bdg_ports[i] == ifp) {
395f196ce38SLuigi Rizzo 			b->bdg_ports[i] = NULL;
396f196ce38SLuigi Rizzo 			bzero(ifp, sizeof(*ifp));
397f196ce38SLuigi Rizzo 			free(ifp, M_DEVBUF);
398f196ce38SLuigi Rizzo 			break;
399f196ce38SLuigi Rizzo 		}
400f196ce38SLuigi Rizzo 		else if (b->bdg_ports[i] != NULL)
401f196ce38SLuigi Rizzo 			full = 1;
402f196ce38SLuigi Rizzo 	}
403f196ce38SLuigi Rizzo 	BDG_UNLOCK(b);
404f196ce38SLuigi Rizzo 	if (full == 0) {
405f196ce38SLuigi Rizzo 		ND("freeing bridge %d", b - nm_bridges);
406f196ce38SLuigi Rizzo 		b->namelen = 0;
407f196ce38SLuigi Rizzo 	}
408f196ce38SLuigi Rizzo 	BDG_UNLOCK(nm_bridges);
409f196ce38SLuigi Rizzo 	if (i == NM_BDG_MAXPORTS)
410f196ce38SLuigi Rizzo 		D("ouch, cannot find ifp to remove");
411f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */
412f196ce38SLuigi Rizzo }
4135819da83SLuigi Rizzo 
4145819da83SLuigi Rizzo static void
4155819da83SLuigi Rizzo netmap_dtor(void *data)
4165819da83SLuigi Rizzo {
4175819da83SLuigi Rizzo 	struct netmap_priv_d *priv = data;
4185819da83SLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
4195819da83SLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
4205819da83SLuigi Rizzo 
4211a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
4225819da83SLuigi Rizzo 	netmap_dtor_locked(data);
4231a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
42468b8534bSLuigi Rizzo 
425f196ce38SLuigi Rizzo 	nm_if_rele(ifp);
42668b8534bSLuigi Rizzo 	bzero(priv, sizeof(*priv));	/* XXX for safety */
42768b8534bSLuigi Rizzo 	free(priv, M_DEVBUF);
42868b8534bSLuigi Rizzo }
42968b8534bSLuigi Rizzo 
43068b8534bSLuigi Rizzo 
43168b8534bSLuigi Rizzo /*
43268b8534bSLuigi Rizzo  * mmap(2) support for the "netmap" device.
43368b8534bSLuigi Rizzo  *
43468b8534bSLuigi Rizzo  * Expose all the memory previously allocated by our custom memory
43568b8534bSLuigi Rizzo  * allocator: this way the user has only to issue a single mmap(2), and
43668b8534bSLuigi Rizzo  * can work on all the data structures flawlessly.
43768b8534bSLuigi Rizzo  *
43868b8534bSLuigi Rizzo  * Return 0 on success, -1 otherwise.
43968b8534bSLuigi Rizzo  */
440babc7c12SLuigi Rizzo 
441f196ce38SLuigi Rizzo #ifdef __FreeBSD__
44268b8534bSLuigi Rizzo static int
443babc7c12SLuigi Rizzo netmap_mmap(__unused struct cdev *dev,
44468b8534bSLuigi Rizzo #if __FreeBSD_version < 900000
445babc7c12SLuigi Rizzo 		vm_offset_t offset, vm_paddr_t *paddr, int nprot
44668b8534bSLuigi Rizzo #else
447babc7c12SLuigi Rizzo 		vm_ooffset_t offset, vm_paddr_t *paddr, int nprot,
448babc7c12SLuigi Rizzo 		__unused vm_memattr_t *memattr
44968b8534bSLuigi Rizzo #endif
450babc7c12SLuigi Rizzo 	)
45168b8534bSLuigi Rizzo {
45268b8534bSLuigi Rizzo 	if (nprot & PROT_EXEC)
45368b8534bSLuigi Rizzo 		return (-1);	// XXX -1 or EINVAL ?
454446ee301SLuigi Rizzo 
45568b8534bSLuigi Rizzo 	ND("request for offset 0x%x", (uint32_t)offset);
456446ee301SLuigi Rizzo 	*paddr = netmap_ofstophys(offset);
45768b8534bSLuigi Rizzo 
45868b8534bSLuigi Rizzo 	return (0);
45968b8534bSLuigi Rizzo }
460f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */
46168b8534bSLuigi Rizzo 
46268b8534bSLuigi Rizzo 
46368b8534bSLuigi Rizzo /*
46402ad4083SLuigi Rizzo  * Handlers for synchronization of the queues from/to the host.
46502ad4083SLuigi Rizzo  *
46602ad4083SLuigi Rizzo  * netmap_sync_to_host() passes packets up. We are called from a
46702ad4083SLuigi Rizzo  * system call in user process context, and the only contention
46802ad4083SLuigi Rizzo  * can be among multiple user threads erroneously calling
46902ad4083SLuigi Rizzo  * this routine concurrently. In principle we should not even
47002ad4083SLuigi Rizzo  * need to lock.
47168b8534bSLuigi Rizzo  */
47268b8534bSLuigi Rizzo static void
47368b8534bSLuigi Rizzo netmap_sync_to_host(struct netmap_adapter *na)
47468b8534bSLuigi Rizzo {
475d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->tx_rings[na->num_tx_rings];
47668b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
47768b8534bSLuigi Rizzo 	struct mbuf *head = NULL, *tail = NULL, *m;
47802ad4083SLuigi Rizzo 	u_int k, n, lim = kring->nkr_num_slots - 1;
47968b8534bSLuigi Rizzo 
48002ad4083SLuigi Rizzo 	k = ring->cur;
48102ad4083SLuigi Rizzo 	if (k > lim) {
48202ad4083SLuigi Rizzo 		netmap_ring_reinit(kring);
48302ad4083SLuigi Rizzo 		return;
48402ad4083SLuigi Rizzo 	}
4851a26580eSLuigi Rizzo 	// na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0);
48668b8534bSLuigi Rizzo 
48768b8534bSLuigi Rizzo 	/* Take packets from hwcur to cur and pass them up.
48868b8534bSLuigi Rizzo 	 * In case of no buffers we give up. At the end of the loop,
48968b8534bSLuigi Rizzo 	 * the queue is drained in all cases.
49068b8534bSLuigi Rizzo 	 */
49102ad4083SLuigi Rizzo 	for (n = kring->nr_hwcur; n != k;) {
49268b8534bSLuigi Rizzo 		struct netmap_slot *slot = &ring->slot[n];
49368b8534bSLuigi Rizzo 
49468b8534bSLuigi Rizzo 		n = (n == lim) ? 0 : n + 1;
49568b8534bSLuigi Rizzo 		if (slot->len < 14 || slot->len > NETMAP_BUF_SIZE) {
49668b8534bSLuigi Rizzo 			D("bad pkt at %d len %d", n, slot->len);
49768b8534bSLuigi Rizzo 			continue;
49868b8534bSLuigi Rizzo 		}
49968b8534bSLuigi Rizzo 		m = m_devget(NMB(slot), slot->len, 0, na->ifp, NULL);
50068b8534bSLuigi Rizzo 
50168b8534bSLuigi Rizzo 		if (m == NULL)
50268b8534bSLuigi Rizzo 			break;
50368b8534bSLuigi Rizzo 		if (tail)
50468b8534bSLuigi Rizzo 			tail->m_nextpkt = m;
50568b8534bSLuigi Rizzo 		else
50668b8534bSLuigi Rizzo 			head = m;
50768b8534bSLuigi Rizzo 		tail = m;
50868b8534bSLuigi Rizzo 		m->m_nextpkt = NULL;
50968b8534bSLuigi Rizzo 	}
51002ad4083SLuigi Rizzo 	kring->nr_hwcur = k;
51168b8534bSLuigi Rizzo 	kring->nr_hwavail = ring->avail = lim;
5121a26580eSLuigi Rizzo 	// na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0);
51368b8534bSLuigi Rizzo 
51468b8534bSLuigi Rizzo 	/* send packets up, outside the lock */
51568b8534bSLuigi Rizzo 	while ((m = head) != NULL) {
51668b8534bSLuigi Rizzo 		head = head->m_nextpkt;
51768b8534bSLuigi Rizzo 		m->m_nextpkt = NULL;
51868b8534bSLuigi Rizzo 		if (netmap_verbose & NM_VERB_HOST)
5191a26580eSLuigi Rizzo 			D("sending up pkt %p size %d", m, MBUF_LEN(m));
5201a26580eSLuigi Rizzo 		NM_SEND_UP(na->ifp, m);
52168b8534bSLuigi Rizzo 	}
52268b8534bSLuigi Rizzo }
52368b8534bSLuigi Rizzo 
52468b8534bSLuigi Rizzo /*
52502ad4083SLuigi Rizzo  * rxsync backend for packets coming from the host stack.
52602ad4083SLuigi Rizzo  * They have been put in the queue by netmap_start() so we
52702ad4083SLuigi Rizzo  * need to protect access to the kring using a lock.
52802ad4083SLuigi Rizzo  *
52968b8534bSLuigi Rizzo  * This routine also does the selrecord if called from the poll handler
53068b8534bSLuigi Rizzo  * (we know because td != NULL).
53101c7d25fSLuigi Rizzo  *
53201c7d25fSLuigi Rizzo  * NOTE: on linux, selrecord() is defined as a macro and uses pwait
53301c7d25fSLuigi Rizzo  *     as an additional hidden argument.
53468b8534bSLuigi Rizzo  */
53568b8534bSLuigi Rizzo static void
53601c7d25fSLuigi Rizzo netmap_sync_from_host(struct netmap_adapter *na, struct thread *td, void *pwait)
53768b8534bSLuigi Rizzo {
538d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
53968b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
54064ae02c3SLuigi Rizzo 	u_int j, n, lim = kring->nkr_num_slots;
54164ae02c3SLuigi Rizzo 	u_int k = ring->cur, resvd = ring->reserved;
54268b8534bSLuigi Rizzo 
54301c7d25fSLuigi Rizzo 	(void)pwait;	/* disable unused warnings */
5441a26580eSLuigi Rizzo 	na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0);
54564ae02c3SLuigi Rizzo 	if (k >= lim) {
54664ae02c3SLuigi Rizzo 		netmap_ring_reinit(kring);
54764ae02c3SLuigi Rizzo 		return;
54864ae02c3SLuigi Rizzo 	}
54964ae02c3SLuigi Rizzo 	/* new packets are already set in nr_hwavail */
55064ae02c3SLuigi Rizzo 	/* skip past packets that userspace has released */
55164ae02c3SLuigi Rizzo 	j = kring->nr_hwcur;
55264ae02c3SLuigi Rizzo 	if (resvd > 0) {
55364ae02c3SLuigi Rizzo 		if (resvd + ring->avail >= lim + 1) {
55464ae02c3SLuigi Rizzo 			D("XXX invalid reserve/avail %d %d", resvd, ring->avail);
55564ae02c3SLuigi Rizzo 			ring->reserved = resvd = 0; // XXX panic...
55664ae02c3SLuigi Rizzo 		}
55764ae02c3SLuigi Rizzo 		k = (k >= resvd) ? k - resvd : k + lim - resvd;
55864ae02c3SLuigi Rizzo         }
55964ae02c3SLuigi Rizzo 	if (j != k) {
56064ae02c3SLuigi Rizzo 		n = k >= j ? k - j : k + lim - j;
56164ae02c3SLuigi Rizzo 		kring->nr_hwavail -= n;
56202ad4083SLuigi Rizzo 		kring->nr_hwcur = k;
56364ae02c3SLuigi Rizzo 	}
56464ae02c3SLuigi Rizzo 	k = ring->avail = kring->nr_hwavail - resvd;
56502ad4083SLuigi Rizzo 	if (k == 0 && td)
56668b8534bSLuigi Rizzo 		selrecord(td, &kring->si);
56702ad4083SLuigi Rizzo 	if (k && (netmap_verbose & NM_VERB_HOST))
56802ad4083SLuigi Rizzo 		D("%d pkts from stack", k);
5691a26580eSLuigi Rizzo 	na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0);
57068b8534bSLuigi Rizzo }
57168b8534bSLuigi Rizzo 
57268b8534bSLuigi Rizzo 
57368b8534bSLuigi Rizzo /*
57468b8534bSLuigi Rizzo  * get a refcounted reference to an interface.
57568b8534bSLuigi Rizzo  * Return ENXIO if the interface does not exist, EINVAL if netmap
57668b8534bSLuigi Rizzo  * is not supported by the interface.
57768b8534bSLuigi Rizzo  * If successful, hold a reference.
57868b8534bSLuigi Rizzo  */
57968b8534bSLuigi Rizzo static int
58068b8534bSLuigi Rizzo get_ifp(const char *name, struct ifnet **ifp)
58168b8534bSLuigi Rizzo {
582f196ce38SLuigi Rizzo #ifdef NM_BRIDGE
583f196ce38SLuigi Rizzo 	struct ifnet *iter = NULL;
584f196ce38SLuigi Rizzo 
585f196ce38SLuigi Rizzo 	do {
586f196ce38SLuigi Rizzo 		struct nm_bridge *b;
587f196ce38SLuigi Rizzo 		int i, l, cand = -1;
588f196ce38SLuigi Rizzo 
589f196ce38SLuigi Rizzo 		if (strncmp(name, NM_NAME, sizeof(NM_NAME) - 1))
590f196ce38SLuigi Rizzo 			break;
591f196ce38SLuigi Rizzo 		b = nm_find_bridge(name);
592f196ce38SLuigi Rizzo 		if (b == NULL) {
593f196ce38SLuigi Rizzo 			D("no bridges available for '%s'", name);
594f196ce38SLuigi Rizzo 			return (ENXIO);
595f196ce38SLuigi Rizzo 		}
596f196ce38SLuigi Rizzo 		/* XXX locking */
597f196ce38SLuigi Rizzo 		BDG_LOCK(b);
598f196ce38SLuigi Rizzo 		/* lookup in the local list of ports */
599f196ce38SLuigi Rizzo 		for (i = 0; i < NM_BDG_MAXPORTS; i++) {
600f196ce38SLuigi Rizzo 			iter = b->bdg_ports[i];
601f196ce38SLuigi Rizzo 			if (iter == NULL) {
602f196ce38SLuigi Rizzo 				if (cand == -1)
603f196ce38SLuigi Rizzo 					cand = i; /* potential insert point */
604f196ce38SLuigi Rizzo 				continue;
605f196ce38SLuigi Rizzo 			}
606f196ce38SLuigi Rizzo 			if (!strcmp(iter->if_xname, name)) {
607f196ce38SLuigi Rizzo 				ADD_BDG_REF(iter);
608f196ce38SLuigi Rizzo 				ND("found existing interface");
609f196ce38SLuigi Rizzo 				BDG_UNLOCK(b);
610f196ce38SLuigi Rizzo 				break;
611f196ce38SLuigi Rizzo 			}
612f196ce38SLuigi Rizzo 		}
613f196ce38SLuigi Rizzo 		if (i < NM_BDG_MAXPORTS) /* already unlocked */
614f196ce38SLuigi Rizzo 			break;
615f196ce38SLuigi Rizzo 		if (cand == -1) {
616f196ce38SLuigi Rizzo 			D("bridge full, cannot create new port");
617f196ce38SLuigi Rizzo no_port:
618f196ce38SLuigi Rizzo 			BDG_UNLOCK(b);
619f196ce38SLuigi Rizzo 			*ifp = NULL;
620f196ce38SLuigi Rizzo 			return EINVAL;
621f196ce38SLuigi Rizzo 		}
622f196ce38SLuigi Rizzo 		ND("create new bridge port %s", name);
623f196ce38SLuigi Rizzo 		/* space for forwarding list after the ifnet */
624f196ce38SLuigi Rizzo 		l = sizeof(*iter) +
625f196ce38SLuigi Rizzo 			 sizeof(struct nm_bdg_fwd)*NM_BDG_BATCH ;
626f196ce38SLuigi Rizzo 		iter = malloc(l, M_DEVBUF, M_NOWAIT | M_ZERO);
627f196ce38SLuigi Rizzo 		if (!iter)
628f196ce38SLuigi Rizzo 			goto no_port;
629f196ce38SLuigi Rizzo 		strcpy(iter->if_xname, name);
630f196ce38SLuigi Rizzo 		bdg_netmap_attach(iter);
631f196ce38SLuigi Rizzo 		b->bdg_ports[cand] = iter;
632f196ce38SLuigi Rizzo 		iter->if_bridge = b;
633f196ce38SLuigi Rizzo 		ADD_BDG_REF(iter);
634f196ce38SLuigi Rizzo 		BDG_UNLOCK(b);
635f196ce38SLuigi Rizzo 		ND("attaching virtual bridge %p", b);
636f196ce38SLuigi Rizzo 	} while (0);
637f196ce38SLuigi Rizzo 	*ifp = iter;
638f196ce38SLuigi Rizzo 	if (! *ifp)
639f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */
64068b8534bSLuigi Rizzo 	*ifp = ifunit_ref(name);
64168b8534bSLuigi Rizzo 	if (*ifp == NULL)
64268b8534bSLuigi Rizzo 		return (ENXIO);
64368b8534bSLuigi Rizzo 	/* can do this if the capability exists and if_pspare[0]
64468b8534bSLuigi Rizzo 	 * points to the netmap descriptor.
64568b8534bSLuigi Rizzo 	 */
64668b8534bSLuigi Rizzo 	if ((*ifp)->if_capabilities & IFCAP_NETMAP && NA(*ifp))
64768b8534bSLuigi Rizzo 		return 0;	/* valid pointer, we hold the refcount */
648f196ce38SLuigi Rizzo 	nm_if_rele(*ifp);
64968b8534bSLuigi Rizzo 	return EINVAL;	// not NETMAP capable
65068b8534bSLuigi Rizzo }
65168b8534bSLuigi Rizzo 
65268b8534bSLuigi Rizzo 
65368b8534bSLuigi Rizzo /*
65468b8534bSLuigi Rizzo  * Error routine called when txsync/rxsync detects an error.
65568b8534bSLuigi Rizzo  * Can't do much more than resetting cur = hwcur, avail = hwavail.
65668b8534bSLuigi Rizzo  * Return 1 on reinit.
657506cc70cSLuigi Rizzo  *
658506cc70cSLuigi Rizzo  * This routine is only called by the upper half of the kernel.
659506cc70cSLuigi Rizzo  * It only reads hwcur (which is changed only by the upper half, too)
660506cc70cSLuigi Rizzo  * and hwavail (which may be changed by the lower half, but only on
661506cc70cSLuigi Rizzo  * a tx ring and only to increase it, so any error will be recovered
662506cc70cSLuigi Rizzo  * on the next call). For the above, we don't strictly need to call
663506cc70cSLuigi Rizzo  * it under lock.
66468b8534bSLuigi Rizzo  */
66568b8534bSLuigi Rizzo int
66668b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring)
66768b8534bSLuigi Rizzo {
66868b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
66968b8534bSLuigi Rizzo 	u_int i, lim = kring->nkr_num_slots - 1;
67068b8534bSLuigi Rizzo 	int errors = 0;
67168b8534bSLuigi Rizzo 
67268b8534bSLuigi Rizzo 	D("called for %s", kring->na->ifp->if_xname);
67368b8534bSLuigi Rizzo 	if (ring->cur > lim)
67468b8534bSLuigi Rizzo 		errors++;
67568b8534bSLuigi Rizzo 	for (i = 0; i <= lim; i++) {
67668b8534bSLuigi Rizzo 		u_int idx = ring->slot[i].buf_idx;
67768b8534bSLuigi Rizzo 		u_int len = ring->slot[i].len;
67868b8534bSLuigi Rizzo 		if (idx < 2 || idx >= netmap_total_buffers) {
67968b8534bSLuigi Rizzo 			if (!errors++)
68068b8534bSLuigi Rizzo 				D("bad buffer at slot %d idx %d len %d ", i, idx, len);
68168b8534bSLuigi Rizzo 			ring->slot[i].buf_idx = 0;
68268b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
68368b8534bSLuigi Rizzo 		} else if (len > NETMAP_BUF_SIZE) {
68468b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
68568b8534bSLuigi Rizzo 			if (!errors++)
68668b8534bSLuigi Rizzo 				D("bad len %d at slot %d idx %d",
68768b8534bSLuigi Rizzo 					len, i, idx);
68868b8534bSLuigi Rizzo 		}
68968b8534bSLuigi Rizzo 	}
69068b8534bSLuigi Rizzo 	if (errors) {
69168b8534bSLuigi Rizzo 		int pos = kring - kring->na->tx_rings;
692d76bf4ffSLuigi Rizzo 		int n = kring->na->num_tx_rings + 1;
69368b8534bSLuigi Rizzo 
69468b8534bSLuigi Rizzo 		D("total %d errors", errors);
69568b8534bSLuigi Rizzo 		errors++;
69668b8534bSLuigi Rizzo 		D("%s %s[%d] reinit, cur %d -> %d avail %d -> %d",
69768b8534bSLuigi Rizzo 			kring->na->ifp->if_xname,
69868b8534bSLuigi Rizzo 			pos < n ?  "TX" : "RX", pos < n ? pos : pos - n,
69968b8534bSLuigi Rizzo 			ring->cur, kring->nr_hwcur,
70068b8534bSLuigi Rizzo 			ring->avail, kring->nr_hwavail);
70168b8534bSLuigi Rizzo 		ring->cur = kring->nr_hwcur;
70268b8534bSLuigi Rizzo 		ring->avail = kring->nr_hwavail;
70368b8534bSLuigi Rizzo 	}
70468b8534bSLuigi Rizzo 	return (errors ? 1 : 0);
70568b8534bSLuigi Rizzo }
70668b8534bSLuigi Rizzo 
70768b8534bSLuigi Rizzo 
70868b8534bSLuigi Rizzo /*
70968b8534bSLuigi Rizzo  * Set the ring ID. For devices with a single queue, a request
71068b8534bSLuigi Rizzo  * for all rings is the same as a single ring.
71168b8534bSLuigi Rizzo  */
71268b8534bSLuigi Rizzo static int
71368b8534bSLuigi Rizzo netmap_set_ringid(struct netmap_priv_d *priv, u_int ringid)
71468b8534bSLuigi Rizzo {
71568b8534bSLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
71668b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
71768b8534bSLuigi Rizzo 	u_int i = ringid & NETMAP_RING_MASK;
71864ae02c3SLuigi Rizzo 	/* initially (np_qfirst == np_qlast) we don't want to lock */
71968b8534bSLuigi Rizzo 	int need_lock = (priv->np_qfirst != priv->np_qlast);
720d76bf4ffSLuigi Rizzo 	int lim = na->num_rx_rings;
72168b8534bSLuigi Rizzo 
722d76bf4ffSLuigi Rizzo 	if (na->num_tx_rings > lim)
723d76bf4ffSLuigi Rizzo 		lim = na->num_tx_rings;
72464ae02c3SLuigi Rizzo 	if ( (ringid & NETMAP_HW_RING) && i >= lim) {
72568b8534bSLuigi Rizzo 		D("invalid ring id %d", i);
72668b8534bSLuigi Rizzo 		return (EINVAL);
72768b8534bSLuigi Rizzo 	}
72868b8534bSLuigi Rizzo 	if (need_lock)
7291a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
73068b8534bSLuigi Rizzo 	priv->np_ringid = ringid;
73168b8534bSLuigi Rizzo 	if (ringid & NETMAP_SW_RING) {
73264ae02c3SLuigi Rizzo 		priv->np_qfirst = NETMAP_SW_RING;
73364ae02c3SLuigi Rizzo 		priv->np_qlast = 0;
73468b8534bSLuigi Rizzo 	} else if (ringid & NETMAP_HW_RING) {
73568b8534bSLuigi Rizzo 		priv->np_qfirst = i;
73668b8534bSLuigi Rizzo 		priv->np_qlast = i + 1;
73768b8534bSLuigi Rizzo 	} else {
73868b8534bSLuigi Rizzo 		priv->np_qfirst = 0;
73964ae02c3SLuigi Rizzo 		priv->np_qlast = NETMAP_HW_RING ;
74068b8534bSLuigi Rizzo 	}
74168b8534bSLuigi Rizzo 	priv->np_txpoll = (ringid & NETMAP_NO_TX_POLL) ? 0 : 1;
74268b8534bSLuigi Rizzo 	if (need_lock)
7431a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
74468b8534bSLuigi Rizzo 	if (ringid & NETMAP_SW_RING)
74568b8534bSLuigi Rizzo 		D("ringid %s set to SW RING", ifp->if_xname);
74668b8534bSLuigi Rizzo 	else if (ringid & NETMAP_HW_RING)
74768b8534bSLuigi Rizzo 		D("ringid %s set to HW RING %d", ifp->if_xname,
74868b8534bSLuigi Rizzo 			priv->np_qfirst);
74968b8534bSLuigi Rizzo 	else
75064ae02c3SLuigi Rizzo 		D("ringid %s set to all %d HW RINGS", ifp->if_xname, lim);
75168b8534bSLuigi Rizzo 	return 0;
75268b8534bSLuigi Rizzo }
75368b8534bSLuigi Rizzo 
75468b8534bSLuigi Rizzo /*
75568b8534bSLuigi Rizzo  * ioctl(2) support for the "netmap" device.
75668b8534bSLuigi Rizzo  *
75768b8534bSLuigi Rizzo  * Following a list of accepted commands:
75868b8534bSLuigi Rizzo  * - NIOCGINFO
75968b8534bSLuigi Rizzo  * - SIOCGIFADDR	just for convenience
76068b8534bSLuigi Rizzo  * - NIOCREGIF
76168b8534bSLuigi Rizzo  * - NIOCUNREGIF
76268b8534bSLuigi Rizzo  * - NIOCTXSYNC
76368b8534bSLuigi Rizzo  * - NIOCRXSYNC
76468b8534bSLuigi Rizzo  *
76568b8534bSLuigi Rizzo  * Return 0 on success, errno otherwise.
76668b8534bSLuigi Rizzo  */
76768b8534bSLuigi Rizzo static int
768*0b8ed8e0SLuigi Rizzo netmap_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
769*0b8ed8e0SLuigi Rizzo 	int fflag, struct thread *td)
77068b8534bSLuigi Rizzo {
77168b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
77268b8534bSLuigi Rizzo 	struct ifnet *ifp;
77368b8534bSLuigi Rizzo 	struct nmreq *nmr = (struct nmreq *) data;
77468b8534bSLuigi Rizzo 	struct netmap_adapter *na;
77568b8534bSLuigi Rizzo 	int error;
77664ae02c3SLuigi Rizzo 	u_int i, lim;
77768b8534bSLuigi Rizzo 	struct netmap_if *nifp;
77868b8534bSLuigi Rizzo 
779*0b8ed8e0SLuigi Rizzo 	(void)dev;	/* UNUSED */
780*0b8ed8e0SLuigi Rizzo 	(void)fflag;	/* UNUSED */
781f196ce38SLuigi Rizzo #ifdef linux
782f196ce38SLuigi Rizzo #define devfs_get_cdevpriv(pp)				\
783f196ce38SLuigi Rizzo 	({ *(struct netmap_priv_d **)pp = ((struct file *)td)->private_data; 	\
784f196ce38SLuigi Rizzo 		(*pp ? 0 : ENOENT); })
785f196ce38SLuigi Rizzo 
786f196ce38SLuigi Rizzo /* devfs_set_cdevpriv cannot fail on linux */
787f196ce38SLuigi Rizzo #define devfs_set_cdevpriv(p, fn)				\
788f196ce38SLuigi Rizzo 	({ ((struct file *)td)->private_data = p; (p ? 0 : EINVAL); })
789f196ce38SLuigi Rizzo 
790f196ce38SLuigi Rizzo 
791f196ce38SLuigi Rizzo #define devfs_clear_cdevpriv()	do {				\
792f196ce38SLuigi Rizzo 		netmap_dtor(priv); ((struct file *)td)->private_data = 0;	\
793f196ce38SLuigi Rizzo 	} while (0)
794f196ce38SLuigi Rizzo #endif /* linux */
795f196ce38SLuigi Rizzo 
796506cc70cSLuigi Rizzo 	CURVNET_SET(TD_TO_VNET(td));
797506cc70cSLuigi Rizzo 
79868b8534bSLuigi Rizzo 	error = devfs_get_cdevpriv((void **)&priv);
799506cc70cSLuigi Rizzo 	if (error != ENOENT && error != 0) {
800506cc70cSLuigi Rizzo 		CURVNET_RESTORE();
80168b8534bSLuigi Rizzo 		return (error);
802506cc70cSLuigi Rizzo 	}
80368b8534bSLuigi Rizzo 
80468b8534bSLuigi Rizzo 	error = 0;	/* Could be ENOENT */
805f196ce38SLuigi Rizzo 	nmr->nr_name[sizeof(nmr->nr_name) - 1] = '\0';	/* truncate name */
80668b8534bSLuigi Rizzo 	switch (cmd) {
80768b8534bSLuigi Rizzo 	case NIOCGINFO:		/* return capabilities etc */
80868b8534bSLuigi Rizzo 		/* memsize is always valid */
80964ae02c3SLuigi Rizzo 		nmr->nr_memsize = nm_mem->nm_totalsize;
81068b8534bSLuigi Rizzo 		nmr->nr_offset = 0;
81164ae02c3SLuigi Rizzo 		nmr->nr_rx_rings = nmr->nr_tx_rings = 0;
81264ae02c3SLuigi Rizzo 		nmr->nr_rx_slots = nmr->nr_tx_slots = 0;
81364ae02c3SLuigi Rizzo 		if (nmr->nr_version != NETMAP_API) {
81464ae02c3SLuigi Rizzo 			D("API mismatch got %d have %d",
81564ae02c3SLuigi Rizzo 				nmr->nr_version, NETMAP_API);
81664ae02c3SLuigi Rizzo 			nmr->nr_version = NETMAP_API;
81764ae02c3SLuigi Rizzo 			error = EINVAL;
81864ae02c3SLuigi Rizzo 			break;
81964ae02c3SLuigi Rizzo 		}
82068b8534bSLuigi Rizzo 		if (nmr->nr_name[0] == '\0')	/* just get memory info */
82168b8534bSLuigi Rizzo 			break;
82268b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* get a refcount */
82368b8534bSLuigi Rizzo 		if (error)
82468b8534bSLuigi Rizzo 			break;
82568b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap_adapter */
826d76bf4ffSLuigi Rizzo 		nmr->nr_rx_rings = na->num_rx_rings;
827d76bf4ffSLuigi Rizzo 		nmr->nr_tx_rings = na->num_tx_rings;
82864ae02c3SLuigi Rizzo 		nmr->nr_rx_slots = na->num_rx_desc;
82964ae02c3SLuigi Rizzo 		nmr->nr_tx_slots = na->num_tx_desc;
830f196ce38SLuigi Rizzo 		nm_if_rele(ifp);	/* return the refcount */
83168b8534bSLuigi Rizzo 		break;
83268b8534bSLuigi Rizzo 
83368b8534bSLuigi Rizzo 	case NIOCREGIF:
83464ae02c3SLuigi Rizzo 		if (nmr->nr_version != NETMAP_API) {
83564ae02c3SLuigi Rizzo 			nmr->nr_version = NETMAP_API;
83664ae02c3SLuigi Rizzo 			error = EINVAL;
83764ae02c3SLuigi Rizzo 			break;
83864ae02c3SLuigi Rizzo 		}
839506cc70cSLuigi Rizzo 		if (priv != NULL) {	/* thread already registered */
840506cc70cSLuigi Rizzo 			error = netmap_set_ringid(priv, nmr->nr_ringid);
841506cc70cSLuigi Rizzo 			break;
842506cc70cSLuigi Rizzo 		}
84368b8534bSLuigi Rizzo 		/* find the interface and a reference */
84468b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* keep reference */
84568b8534bSLuigi Rizzo 		if (error)
84668b8534bSLuigi Rizzo 			break;
84768b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap adapter */
84868b8534bSLuigi Rizzo 		/*
84968b8534bSLuigi Rizzo 		 * Allocate the private per-thread structure.
85068b8534bSLuigi Rizzo 		 * XXX perhaps we can use a blocking malloc ?
85168b8534bSLuigi Rizzo 		 */
85268b8534bSLuigi Rizzo 		priv = malloc(sizeof(struct netmap_priv_d), M_DEVBUF,
85368b8534bSLuigi Rizzo 			      M_NOWAIT | M_ZERO);
85468b8534bSLuigi Rizzo 		if (priv == NULL) {
85568b8534bSLuigi Rizzo 			error = ENOMEM;
856f196ce38SLuigi Rizzo 			nm_if_rele(ifp);   /* return the refcount */
85768b8534bSLuigi Rizzo 			break;
85868b8534bSLuigi Rizzo 		}
85968b8534bSLuigi Rizzo 
86068b8534bSLuigi Rizzo 		for (i = 10; i > 0; i--) {
8611a26580eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
86268b8534bSLuigi Rizzo 			if (!NETMAP_DELETING(na))
86368b8534bSLuigi Rizzo 				break;
8641a26580eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
86568b8534bSLuigi Rizzo 			tsleep(na, 0, "NIOCREGIF", hz/10);
86668b8534bSLuigi Rizzo 		}
86768b8534bSLuigi Rizzo 		if (i == 0) {
86868b8534bSLuigi Rizzo 			D("too many NIOCREGIF attempts, give up");
86968b8534bSLuigi Rizzo 			error = EINVAL;
87068b8534bSLuigi Rizzo 			free(priv, M_DEVBUF);
871f196ce38SLuigi Rizzo 			nm_if_rele(ifp);	/* return the refcount */
87268b8534bSLuigi Rizzo 			break;
87368b8534bSLuigi Rizzo 		}
87468b8534bSLuigi Rizzo 
87568b8534bSLuigi Rizzo 		priv->np_ifp = ifp;	/* store the reference */
87668b8534bSLuigi Rizzo 		error = netmap_set_ringid(priv, nmr->nr_ringid);
87768b8534bSLuigi Rizzo 		if (error)
87868b8534bSLuigi Rizzo 			goto error;
87968b8534bSLuigi Rizzo 		priv->np_nifp = nifp = netmap_if_new(nmr->nr_name, na);
88068b8534bSLuigi Rizzo 		if (nifp == NULL) { /* allocation failed */
88168b8534bSLuigi Rizzo 			error = ENOMEM;
88268b8534bSLuigi Rizzo 		} else if (ifp->if_capenable & IFCAP_NETMAP) {
88368b8534bSLuigi Rizzo 			/* was already set */
88468b8534bSLuigi Rizzo 		} else {
88568b8534bSLuigi Rizzo 			/* Otherwise set the card in netmap mode
88668b8534bSLuigi Rizzo 			 * and make it use the shared buffers.
88768b8534bSLuigi Rizzo 			 */
888f196ce38SLuigi Rizzo 			for (i = 0 ; i < na->num_tx_rings + 1; i++)
889f196ce38SLuigi Rizzo 				mtx_init(&na->tx_rings[i].q_lock, "nm_txq_lock", MTX_NETWORK_LOCK, MTX_DEF);
890f196ce38SLuigi Rizzo 			for (i = 0 ; i < na->num_rx_rings + 1; i++) {
891f196ce38SLuigi Rizzo 				mtx_init(&na->rx_rings[i].q_lock, "nm_rxq_lock", MTX_NETWORK_LOCK, MTX_DEF);
892f196ce38SLuigi Rizzo 			}
89368b8534bSLuigi Rizzo 			error = na->nm_register(ifp, 1); /* mode on */
8945819da83SLuigi Rizzo 			if (error)
8955819da83SLuigi Rizzo 				netmap_dtor_locked(priv);
89668b8534bSLuigi Rizzo 		}
89768b8534bSLuigi Rizzo 
89868b8534bSLuigi Rizzo 		if (error) {	/* reg. failed, release priv and ref */
89968b8534bSLuigi Rizzo error:
9001a26580eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
901f196ce38SLuigi Rizzo 			nm_if_rele(ifp);	/* return the refcount */
9025819da83SLuigi Rizzo 			bzero(priv, sizeof(*priv));
9035819da83SLuigi Rizzo 			free(priv, M_DEVBUF);
90468b8534bSLuigi Rizzo 			break;
90568b8534bSLuigi Rizzo 		}
90668b8534bSLuigi Rizzo 
9071a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
90868b8534bSLuigi Rizzo 		error = devfs_set_cdevpriv(priv, netmap_dtor);
90968b8534bSLuigi Rizzo 
91068b8534bSLuigi Rizzo 		if (error != 0) {
91168b8534bSLuigi Rizzo 			/* could not assign the private storage for the
91268b8534bSLuigi Rizzo 			 * thread, call the destructor explicitly.
91368b8534bSLuigi Rizzo 			 */
91468b8534bSLuigi Rizzo 			netmap_dtor(priv);
91568b8534bSLuigi Rizzo 			break;
91668b8534bSLuigi Rizzo 		}
91768b8534bSLuigi Rizzo 
91868b8534bSLuigi Rizzo 		/* return the offset of the netmap_if object */
919d76bf4ffSLuigi Rizzo 		nmr->nr_rx_rings = na->num_rx_rings;
920d76bf4ffSLuigi Rizzo 		nmr->nr_tx_rings = na->num_tx_rings;
92164ae02c3SLuigi Rizzo 		nmr->nr_rx_slots = na->num_rx_desc;
92264ae02c3SLuigi Rizzo 		nmr->nr_tx_slots = na->num_tx_desc;
92364ae02c3SLuigi Rizzo 		nmr->nr_memsize = nm_mem->nm_totalsize;
924446ee301SLuigi Rizzo 		nmr->nr_offset = netmap_if_offset(nifp);
92568b8534bSLuigi Rizzo 		break;
92668b8534bSLuigi Rizzo 
92768b8534bSLuigi Rizzo 	case NIOCUNREGIF:
928506cc70cSLuigi Rizzo 		if (priv == NULL) {
929506cc70cSLuigi Rizzo 			error = ENXIO;
930506cc70cSLuigi Rizzo 			break;
931506cc70cSLuigi Rizzo 		}
93268b8534bSLuigi Rizzo 
93368b8534bSLuigi Rizzo 		/* the interface is unregistered inside the
93468b8534bSLuigi Rizzo 		   destructor of the private data. */
93568b8534bSLuigi Rizzo 		devfs_clear_cdevpriv();
93668b8534bSLuigi Rizzo 		break;
93768b8534bSLuigi Rizzo 
93868b8534bSLuigi Rizzo 	case NIOCTXSYNC:
93968b8534bSLuigi Rizzo         case NIOCRXSYNC:
940506cc70cSLuigi Rizzo 		if (priv == NULL) {
941506cc70cSLuigi Rizzo 			error = ENXIO;
942506cc70cSLuigi Rizzo 			break;
943506cc70cSLuigi Rizzo 		}
94468b8534bSLuigi Rizzo 		ifp = priv->np_ifp;	/* we have a reference */
94568b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap adapter */
94664ae02c3SLuigi Rizzo 		if (priv->np_qfirst == NETMAP_SW_RING) { /* host rings */
94768b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC)
94868b8534bSLuigi Rizzo 				netmap_sync_to_host(na);
94968b8534bSLuigi Rizzo 			else
95001c7d25fSLuigi Rizzo 				netmap_sync_from_host(na, NULL, NULL);
951506cc70cSLuigi Rizzo 			break;
95268b8534bSLuigi Rizzo 		}
95364ae02c3SLuigi Rizzo 		/* find the last ring to scan */
95464ae02c3SLuigi Rizzo 		lim = priv->np_qlast;
95564ae02c3SLuigi Rizzo 		if (lim == NETMAP_HW_RING)
9563c0caf6cSLuigi Rizzo 			lim = (cmd == NIOCTXSYNC) ?
957d76bf4ffSLuigi Rizzo 			    na->num_tx_rings : na->num_rx_rings;
95868b8534bSLuigi Rizzo 
95964ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim; i++) {
96068b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC) {
96168b8534bSLuigi Rizzo 				struct netmap_kring *kring = &na->tx_rings[i];
96268b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
9633c0caf6cSLuigi Rizzo 					D("pre txsync ring %d cur %d hwcur %d",
96468b8534bSLuigi Rizzo 					    i, kring->ring->cur,
96568b8534bSLuigi Rizzo 					    kring->nr_hwcur);
9661a26580eSLuigi Rizzo 				na->nm_txsync(ifp, i, 1 /* do lock */);
96768b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
9683c0caf6cSLuigi Rizzo 					D("post txsync ring %d cur %d hwcur %d",
96968b8534bSLuigi Rizzo 					    i, kring->ring->cur,
97068b8534bSLuigi Rizzo 					    kring->nr_hwcur);
97168b8534bSLuigi Rizzo 			} else {
9721a26580eSLuigi Rizzo 				na->nm_rxsync(ifp, i, 1 /* do lock */);
97368b8534bSLuigi Rizzo 				microtime(&na->rx_rings[i].ring->ts);
97468b8534bSLuigi Rizzo 			}
97568b8534bSLuigi Rizzo 		}
97668b8534bSLuigi Rizzo 
97768b8534bSLuigi Rizzo 		break;
97868b8534bSLuigi Rizzo 
979f196ce38SLuigi Rizzo #ifdef __FreeBSD__
98068b8534bSLuigi Rizzo 	case BIOCIMMEDIATE:
98168b8534bSLuigi Rizzo 	case BIOCGHDRCMPLT:
98268b8534bSLuigi Rizzo 	case BIOCSHDRCMPLT:
98368b8534bSLuigi Rizzo 	case BIOCSSEESENT:
98468b8534bSLuigi Rizzo 		D("ignore BIOCIMMEDIATE/BIOCSHDRCMPLT/BIOCSHDRCMPLT/BIOCSSEESENT");
98568b8534bSLuigi Rizzo 		break;
98668b8534bSLuigi Rizzo 
987babc7c12SLuigi Rizzo 	default:	/* allow device-specific ioctls */
98868b8534bSLuigi Rizzo 	    {
98968b8534bSLuigi Rizzo 		struct socket so;
99068b8534bSLuigi Rizzo 		bzero(&so, sizeof(so));
99168b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* keep reference */
99268b8534bSLuigi Rizzo 		if (error)
99368b8534bSLuigi Rizzo 			break;
99468b8534bSLuigi Rizzo 		so.so_vnet = ifp->if_vnet;
99568b8534bSLuigi Rizzo 		// so->so_proto not null.
99668b8534bSLuigi Rizzo 		error = ifioctl(&so, cmd, data, td);
997f196ce38SLuigi Rizzo 		nm_if_rele(ifp);
998babc7c12SLuigi Rizzo 		break;
99968b8534bSLuigi Rizzo 	    }
1000f196ce38SLuigi Rizzo 
1001f196ce38SLuigi Rizzo #else /* linux */
1002f196ce38SLuigi Rizzo 	default:
1003f196ce38SLuigi Rizzo 		error = EOPNOTSUPP;
1004f196ce38SLuigi Rizzo #endif /* linux */
100568b8534bSLuigi Rizzo 	}
100668b8534bSLuigi Rizzo 
1007506cc70cSLuigi Rizzo 	CURVNET_RESTORE();
100868b8534bSLuigi Rizzo 	return (error);
100968b8534bSLuigi Rizzo }
101068b8534bSLuigi Rizzo 
101168b8534bSLuigi Rizzo 
101268b8534bSLuigi Rizzo /*
101368b8534bSLuigi Rizzo  * select(2) and poll(2) handlers for the "netmap" device.
101468b8534bSLuigi Rizzo  *
101568b8534bSLuigi Rizzo  * Can be called for one or more queues.
101668b8534bSLuigi Rizzo  * Return true the event mask corresponding to ready events.
101768b8534bSLuigi Rizzo  * If there are no ready events, do a selrecord on either individual
101868b8534bSLuigi Rizzo  * selfd or on the global one.
101968b8534bSLuigi Rizzo  * Device-dependent parts (locking and sync of tx/rx rings)
102068b8534bSLuigi Rizzo  * are done through callbacks.
1021f196ce38SLuigi Rizzo  *
102201c7d25fSLuigi Rizzo  * On linux, arguments are really pwait, the poll table, and 'td' is struct file *
102301c7d25fSLuigi Rizzo  * The first one is remapped to pwait as selrecord() uses the name as an
102401c7d25fSLuigi Rizzo  * hidden argument.
102568b8534bSLuigi Rizzo  */
102668b8534bSLuigi Rizzo static int
102701c7d25fSLuigi Rizzo netmap_poll(struct cdev *dev, int events, struct thread *td)
102868b8534bSLuigi Rizzo {
102968b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
103068b8534bSLuigi Rizzo 	struct netmap_adapter *na;
103168b8534bSLuigi Rizzo 	struct ifnet *ifp;
103268b8534bSLuigi Rizzo 	struct netmap_kring *kring;
1033d0c7b075SLuigi Rizzo 	u_int core_lock, i, check_all, want_tx, want_rx, revents = 0;
103464ae02c3SLuigi Rizzo 	u_int lim_tx, lim_rx;
1035bcda432eSLuigi Rizzo 	enum {NO_CL, NEED_CL, LOCKED_CL }; /* see below */
103601c7d25fSLuigi Rizzo 	void *pwait = dev;	/* linux compatibility */
103701c7d25fSLuigi Rizzo 
103801c7d25fSLuigi Rizzo 	(void)pwait;
103968b8534bSLuigi Rizzo 
104068b8534bSLuigi Rizzo 	if (devfs_get_cdevpriv((void **)&priv) != 0 || priv == NULL)
104168b8534bSLuigi Rizzo 		return POLLERR;
104268b8534bSLuigi Rizzo 
104368b8534bSLuigi Rizzo 	ifp = priv->np_ifp;
104468b8534bSLuigi Rizzo 	// XXX check for deleting() ?
104568b8534bSLuigi Rizzo 	if ( (ifp->if_capenable & IFCAP_NETMAP) == 0)
104668b8534bSLuigi Rizzo 		return POLLERR;
104768b8534bSLuigi Rizzo 
104868b8534bSLuigi Rizzo 	if (netmap_verbose & 0x8000)
104968b8534bSLuigi Rizzo 		D("device %s events 0x%x", ifp->if_xname, events);
105068b8534bSLuigi Rizzo 	want_tx = events & (POLLOUT | POLLWRNORM);
105168b8534bSLuigi Rizzo 	want_rx = events & (POLLIN | POLLRDNORM);
105268b8534bSLuigi Rizzo 
105368b8534bSLuigi Rizzo 	na = NA(ifp); /* retrieve netmap adapter */
105468b8534bSLuigi Rizzo 
1055d76bf4ffSLuigi Rizzo 	lim_tx = na->num_tx_rings;
1056d76bf4ffSLuigi Rizzo 	lim_rx = na->num_rx_rings;
105768b8534bSLuigi Rizzo 	/* how many queues we are scanning */
105864ae02c3SLuigi Rizzo 	if (priv->np_qfirst == NETMAP_SW_RING) {
105968b8534bSLuigi Rizzo 		if (priv->np_txpoll || want_tx) {
106068b8534bSLuigi Rizzo 			/* push any packets up, then we are always ready */
106164ae02c3SLuigi Rizzo 			kring = &na->tx_rings[lim_tx];
106268b8534bSLuigi Rizzo 			netmap_sync_to_host(na);
106368b8534bSLuigi Rizzo 			revents |= want_tx;
106468b8534bSLuigi Rizzo 		}
106568b8534bSLuigi Rizzo 		if (want_rx) {
106664ae02c3SLuigi Rizzo 			kring = &na->rx_rings[lim_rx];
106768b8534bSLuigi Rizzo 			if (kring->ring->avail == 0)
106801c7d25fSLuigi Rizzo 				netmap_sync_from_host(na, td, dev);
106968b8534bSLuigi Rizzo 			if (kring->ring->avail > 0) {
107068b8534bSLuigi Rizzo 				revents |= want_rx;
107168b8534bSLuigi Rizzo 			}
107268b8534bSLuigi Rizzo 		}
107368b8534bSLuigi Rizzo 		return (revents);
107468b8534bSLuigi Rizzo 	}
107568b8534bSLuigi Rizzo 
107668b8534bSLuigi Rizzo 	/*
107768b8534bSLuigi Rizzo 	 * check_all is set if the card has more than one queue and
107868b8534bSLuigi Rizzo 	 * the client is polling all of them. If true, we sleep on
107968b8534bSLuigi Rizzo 	 * the "global" selfd, otherwise we sleep on individual selfd
108068b8534bSLuigi Rizzo 	 * (we can only sleep on one of them per direction).
108168b8534bSLuigi Rizzo 	 * The interrupt routine in the driver should always wake on
108268b8534bSLuigi Rizzo 	 * the individual selfd, and also on the global one if the card
108368b8534bSLuigi Rizzo 	 * has more than one ring.
108468b8534bSLuigi Rizzo 	 *
108568b8534bSLuigi Rizzo 	 * If the card has only one lock, we just use that.
108668b8534bSLuigi Rizzo 	 * If the card has separate ring locks, we just use those
108768b8534bSLuigi Rizzo 	 * unless we are doing check_all, in which case the whole
108868b8534bSLuigi Rizzo 	 * loop is wrapped by the global lock.
108968b8534bSLuigi Rizzo 	 * We acquire locks only when necessary: if poll is called
109068b8534bSLuigi Rizzo 	 * when buffers are available, we can just return without locks.
109168b8534bSLuigi Rizzo 	 *
109268b8534bSLuigi Rizzo 	 * rxsync() is only called if we run out of buffers on a POLLIN.
109368b8534bSLuigi Rizzo 	 * txsync() is called if we run out of buffers on POLLOUT, or
109468b8534bSLuigi Rizzo 	 * there are pending packets to send. The latter can be disabled
109568b8534bSLuigi Rizzo 	 * passing NETMAP_NO_TX_POLL in the NIOCREG call.
109668b8534bSLuigi Rizzo 	 */
109764ae02c3SLuigi Rizzo 	check_all = (priv->np_qlast == NETMAP_HW_RING) && (lim_tx > 1 || lim_rx > 1);
109868b8534bSLuigi Rizzo 
109968b8534bSLuigi Rizzo 	/*
110068b8534bSLuigi Rizzo 	 * core_lock indicates what to do with the core lock.
110168b8534bSLuigi Rizzo 	 * The core lock is used when either the card has no individual
110268b8534bSLuigi Rizzo 	 * locks, or it has individual locks but we are cheking all
110368b8534bSLuigi Rizzo 	 * rings so we need the core lock to avoid missing wakeup events.
110468b8534bSLuigi Rizzo 	 *
110568b8534bSLuigi Rizzo 	 * It has three possible states:
110668b8534bSLuigi Rizzo 	 * NO_CL	we don't need to use the core lock, e.g.
110768b8534bSLuigi Rizzo 	 *		because we are protected by individual locks.
110868b8534bSLuigi Rizzo 	 * NEED_CL	we need the core lock. In this case, when we
110968b8534bSLuigi Rizzo 	 *		call the lock routine, move to LOCKED_CL
111068b8534bSLuigi Rizzo 	 *		to remember to release the lock once done.
111168b8534bSLuigi Rizzo 	 * LOCKED_CL	core lock is set, so we need to release it.
111268b8534bSLuigi Rizzo 	 */
1113d0c7b075SLuigi Rizzo 	core_lock = (check_all || !na->separate_locks) ? NEED_CL : NO_CL;
1114f196ce38SLuigi Rizzo #ifdef NM_BRIDGE
1115f196ce38SLuigi Rizzo 	/* the bridge uses separate locks */
1116f196ce38SLuigi Rizzo 	if (na->nm_register == bdg_netmap_reg) {
1117f196ce38SLuigi Rizzo 		ND("not using core lock for %s", ifp->if_xname);
1118f196ce38SLuigi Rizzo 		core_lock = NO_CL;
1119f196ce38SLuigi Rizzo 	}
1120f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */
112164ae02c3SLuigi Rizzo 	if (priv->np_qlast != NETMAP_HW_RING) {
112264ae02c3SLuigi Rizzo 		lim_tx = lim_rx = priv->np_qlast;
112364ae02c3SLuigi Rizzo 	}
112464ae02c3SLuigi Rizzo 
112568b8534bSLuigi Rizzo 	/*
112668b8534bSLuigi Rizzo 	 * We start with a lock free round which is good if we have
112768b8534bSLuigi Rizzo 	 * data available. If this fails, then lock and call the sync
112868b8534bSLuigi Rizzo 	 * routines.
112968b8534bSLuigi Rizzo 	 */
113064ae02c3SLuigi Rizzo 	for (i = priv->np_qfirst; want_rx && i < lim_rx; i++) {
113168b8534bSLuigi Rizzo 		kring = &na->rx_rings[i];
113268b8534bSLuigi Rizzo 		if (kring->ring->avail > 0) {
113368b8534bSLuigi Rizzo 			revents |= want_rx;
113468b8534bSLuigi Rizzo 			want_rx = 0;	/* also breaks the loop */
113568b8534bSLuigi Rizzo 		}
113668b8534bSLuigi Rizzo 	}
113764ae02c3SLuigi Rizzo 	for (i = priv->np_qfirst; want_tx && i < lim_tx; i++) {
113868b8534bSLuigi Rizzo 		kring = &na->tx_rings[i];
113968b8534bSLuigi Rizzo 		if (kring->ring->avail > 0) {
114068b8534bSLuigi Rizzo 			revents |= want_tx;
114168b8534bSLuigi Rizzo 			want_tx = 0;	/* also breaks the loop */
114268b8534bSLuigi Rizzo 		}
114368b8534bSLuigi Rizzo 	}
114468b8534bSLuigi Rizzo 
114568b8534bSLuigi Rizzo 	/*
114668b8534bSLuigi Rizzo 	 * If we to push packets out (priv->np_txpoll) or want_tx is
114768b8534bSLuigi Rizzo 	 * still set, we do need to run the txsync calls (on all rings,
114868b8534bSLuigi Rizzo 	 * to avoid that the tx rings stall).
114968b8534bSLuigi Rizzo 	 */
115068b8534bSLuigi Rizzo 	if (priv->np_txpoll || want_tx) {
115164ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim_tx; i++) {
115268b8534bSLuigi Rizzo 			kring = &na->tx_rings[i];
11535819da83SLuigi Rizzo 			/*
11545819da83SLuigi Rizzo 			 * Skip the current ring if want_tx == 0
11555819da83SLuigi Rizzo 			 * (we have already done a successful sync on
11565819da83SLuigi Rizzo 			 * a previous ring) AND kring->cur == kring->hwcur
11575819da83SLuigi Rizzo 			 * (there are no pending transmissions for this ring).
11585819da83SLuigi Rizzo 			 */
115968b8534bSLuigi Rizzo 			if (!want_tx && kring->ring->cur == kring->nr_hwcur)
116068b8534bSLuigi Rizzo 				continue;
116168b8534bSLuigi Rizzo 			if (core_lock == NEED_CL) {
11621a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
116368b8534bSLuigi Rizzo 				core_lock = LOCKED_CL;
116468b8534bSLuigi Rizzo 			}
116568b8534bSLuigi Rizzo 			if (na->separate_locks)
11661a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_TX_LOCK, i);
116768b8534bSLuigi Rizzo 			if (netmap_verbose & NM_VERB_TXSYNC)
116868b8534bSLuigi Rizzo 				D("send %d on %s %d",
116968b8534bSLuigi Rizzo 					kring->ring->cur,
117068b8534bSLuigi Rizzo 					ifp->if_xname, i);
11711a26580eSLuigi Rizzo 			if (na->nm_txsync(ifp, i, 0 /* no lock */))
117268b8534bSLuigi Rizzo 				revents |= POLLERR;
117368b8534bSLuigi Rizzo 
11745819da83SLuigi Rizzo 			/* Check avail/call selrecord only if called with POLLOUT */
117568b8534bSLuigi Rizzo 			if (want_tx) {
117668b8534bSLuigi Rizzo 				if (kring->ring->avail > 0) {
117768b8534bSLuigi Rizzo 					/* stop at the first ring. We don't risk
117868b8534bSLuigi Rizzo 					 * starvation.
117968b8534bSLuigi Rizzo 					 */
118068b8534bSLuigi Rizzo 					revents |= want_tx;
118168b8534bSLuigi Rizzo 					want_tx = 0;
118268b8534bSLuigi Rizzo 				} else if (!check_all)
118368b8534bSLuigi Rizzo 					selrecord(td, &kring->si);
118468b8534bSLuigi Rizzo 			}
118568b8534bSLuigi Rizzo 			if (na->separate_locks)
11861a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_TX_UNLOCK, i);
118768b8534bSLuigi Rizzo 		}
118868b8534bSLuigi Rizzo 	}
118968b8534bSLuigi Rizzo 
119068b8534bSLuigi Rizzo 	/*
119168b8534bSLuigi Rizzo 	 * now if want_rx is still set we need to lock and rxsync.
119268b8534bSLuigi Rizzo 	 * Do it on all rings because otherwise we starve.
119368b8534bSLuigi Rizzo 	 */
119468b8534bSLuigi Rizzo 	if (want_rx) {
119564ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim_rx; i++) {
119668b8534bSLuigi Rizzo 			kring = &na->rx_rings[i];
119768b8534bSLuigi Rizzo 			if (core_lock == NEED_CL) {
11981a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
119968b8534bSLuigi Rizzo 				core_lock = LOCKED_CL;
120068b8534bSLuigi Rizzo 			}
120168b8534bSLuigi Rizzo 			if (na->separate_locks)
12021a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_RX_LOCK, i);
120368b8534bSLuigi Rizzo 
12041a26580eSLuigi Rizzo 			if (na->nm_rxsync(ifp, i, 0 /* no lock */))
120568b8534bSLuigi Rizzo 				revents |= POLLERR;
12065819da83SLuigi Rizzo 			if (netmap_no_timestamp == 0 ||
12075819da83SLuigi Rizzo 					kring->ring->flags & NR_TIMESTAMP) {
120868b8534bSLuigi Rizzo 				microtime(&kring->ring->ts);
12095819da83SLuigi Rizzo 			}
121068b8534bSLuigi Rizzo 
121168b8534bSLuigi Rizzo 			if (kring->ring->avail > 0)
121268b8534bSLuigi Rizzo 				revents |= want_rx;
121368b8534bSLuigi Rizzo 			else if (!check_all)
121468b8534bSLuigi Rizzo 				selrecord(td, &kring->si);
121568b8534bSLuigi Rizzo 			if (na->separate_locks)
12161a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_RX_UNLOCK, i);
121768b8534bSLuigi Rizzo 		}
121868b8534bSLuigi Rizzo 	}
121964ae02c3SLuigi Rizzo 	if (check_all && revents == 0) { /* signal on the global queue */
122068b8534bSLuigi Rizzo 		if (want_tx)
122164ae02c3SLuigi Rizzo 			selrecord(td, &na->tx_si);
122268b8534bSLuigi Rizzo 		if (want_rx)
122364ae02c3SLuigi Rizzo 			selrecord(td, &na->rx_si);
122468b8534bSLuigi Rizzo 	}
122568b8534bSLuigi Rizzo 	if (core_lock == LOCKED_CL)
12261a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
122768b8534bSLuigi Rizzo 
122868b8534bSLuigi Rizzo 	return (revents);
122968b8534bSLuigi Rizzo }
123068b8534bSLuigi Rizzo 
123168b8534bSLuigi Rizzo /*------- driver support routines ------*/
123268b8534bSLuigi Rizzo 
123368b8534bSLuigi Rizzo /*
1234babc7c12SLuigi Rizzo  * default lock wrapper.
12351a26580eSLuigi Rizzo  */
12361a26580eSLuigi Rizzo static void
1237babc7c12SLuigi Rizzo netmap_lock_wrapper(struct ifnet *dev, int what, u_int queueid)
12381a26580eSLuigi Rizzo {
1239babc7c12SLuigi Rizzo 	struct netmap_adapter *na = NA(dev);
12401a26580eSLuigi Rizzo 
12411a26580eSLuigi Rizzo 	switch (what) {
1242babc7c12SLuigi Rizzo #ifdef linux	/* some system do not need lock on register */
12431a26580eSLuigi Rizzo 	case NETMAP_REG_LOCK:
12441a26580eSLuigi Rizzo 	case NETMAP_REG_UNLOCK:
12451a26580eSLuigi Rizzo 		break;
1246babc7c12SLuigi Rizzo #endif /* linux */
12471a26580eSLuigi Rizzo 
12481a26580eSLuigi Rizzo 	case NETMAP_CORE_LOCK:
12491a26580eSLuigi Rizzo 		mtx_lock(&na->core_lock);
12501a26580eSLuigi Rizzo 		break;
12511a26580eSLuigi Rizzo 
12521a26580eSLuigi Rizzo 	case NETMAP_CORE_UNLOCK:
12531a26580eSLuigi Rizzo 		mtx_unlock(&na->core_lock);
12541a26580eSLuigi Rizzo 		break;
12551a26580eSLuigi Rizzo 
12561a26580eSLuigi Rizzo 	case NETMAP_TX_LOCK:
12571a26580eSLuigi Rizzo 		mtx_lock(&na->tx_rings[queueid].q_lock);
12581a26580eSLuigi Rizzo 		break;
12591a26580eSLuigi Rizzo 
12601a26580eSLuigi Rizzo 	case NETMAP_TX_UNLOCK:
12611a26580eSLuigi Rizzo 		mtx_unlock(&na->tx_rings[queueid].q_lock);
12621a26580eSLuigi Rizzo 		break;
12631a26580eSLuigi Rizzo 
12641a26580eSLuigi Rizzo 	case NETMAP_RX_LOCK:
12651a26580eSLuigi Rizzo 		mtx_lock(&na->rx_rings[queueid].q_lock);
12661a26580eSLuigi Rizzo 		break;
12671a26580eSLuigi Rizzo 
12681a26580eSLuigi Rizzo 	case NETMAP_RX_UNLOCK:
12691a26580eSLuigi Rizzo 		mtx_unlock(&na->rx_rings[queueid].q_lock);
12701a26580eSLuigi Rizzo 		break;
12711a26580eSLuigi Rizzo 	}
12721a26580eSLuigi Rizzo }
12731a26580eSLuigi Rizzo 
12741a26580eSLuigi Rizzo 
12751a26580eSLuigi Rizzo /*
127668b8534bSLuigi Rizzo  * Initialize a ``netmap_adapter`` object created by driver on attach.
127768b8534bSLuigi Rizzo  * We allocate a block of memory with room for a struct netmap_adapter
127868b8534bSLuigi Rizzo  * plus two sets of N+2 struct netmap_kring (where N is the number
127968b8534bSLuigi Rizzo  * of hardware rings):
128068b8534bSLuigi Rizzo  * krings	0..N-1	are for the hardware queues.
128168b8534bSLuigi Rizzo  * kring	N	is for the host stack queue
128268b8534bSLuigi Rizzo  * kring	N+1	is only used for the selinfo for all queues.
128368b8534bSLuigi Rizzo  * Return 0 on success, ENOMEM otherwise.
128464ae02c3SLuigi Rizzo  *
1285d76bf4ffSLuigi Rizzo  * na->num_tx_rings can be set for cards with different tx/rx setups
128668b8534bSLuigi Rizzo  */
128768b8534bSLuigi Rizzo int
128868b8534bSLuigi Rizzo netmap_attach(struct netmap_adapter *na, int num_queues)
128968b8534bSLuigi Rizzo {
1290f196ce38SLuigi Rizzo 	int n, size;
129168b8534bSLuigi Rizzo 	void *buf;
129268b8534bSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
129368b8534bSLuigi Rizzo 
129468b8534bSLuigi Rizzo 	if (ifp == NULL) {
129568b8534bSLuigi Rizzo 		D("ifp not set, giving up");
129668b8534bSLuigi Rizzo 		return EINVAL;
129768b8534bSLuigi Rizzo 	}
129864ae02c3SLuigi Rizzo 	/* clear other fields ? */
129968b8534bSLuigi Rizzo 	na->refcount = 0;
1300d76bf4ffSLuigi Rizzo 	if (na->num_tx_rings == 0)
1301d76bf4ffSLuigi Rizzo 		na->num_tx_rings = num_queues;
1302d76bf4ffSLuigi Rizzo 	na->num_rx_rings = num_queues;
130364ae02c3SLuigi Rizzo 	/* on each direction we have N+1 resources
130464ae02c3SLuigi Rizzo 	 * 0..n-1	are the hardware rings
130564ae02c3SLuigi Rizzo 	 * n		is the ring attached to the stack.
130664ae02c3SLuigi Rizzo 	 */
1307d76bf4ffSLuigi Rizzo 	n = na->num_rx_rings + na->num_tx_rings + 2;
130864ae02c3SLuigi Rizzo 	size = sizeof(*na) + n * sizeof(struct netmap_kring);
130968b8534bSLuigi Rizzo 
131068b8534bSLuigi Rizzo 	buf = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
131168b8534bSLuigi Rizzo 	if (buf) {
1312d0c7b075SLuigi Rizzo 		WNA(ifp) = buf;
131368b8534bSLuigi Rizzo 		na->tx_rings = (void *)((char *)buf + sizeof(*na));
1314d76bf4ffSLuigi Rizzo 		na->rx_rings = na->tx_rings + na->num_tx_rings + 1;
131568b8534bSLuigi Rizzo 		bcopy(na, buf, sizeof(*na));
131668b8534bSLuigi Rizzo 		ifp->if_capabilities |= IFCAP_NETMAP;
13171a26580eSLuigi Rizzo 
13181a26580eSLuigi Rizzo 		na = buf;
1319f196ce38SLuigi Rizzo 		if (na->nm_lock == NULL) {
1320f196ce38SLuigi Rizzo 			ND("using default locks for %s", ifp->if_xname);
13211a26580eSLuigi Rizzo 			na->nm_lock = netmap_lock_wrapper;
1322f196ce38SLuigi Rizzo 			/* core lock initialized here.
1323f196ce38SLuigi Rizzo 			 * others initialized after netmap_if_new
1324f196ce38SLuigi Rizzo 			 */
1325f196ce38SLuigi Rizzo 			mtx_init(&na->core_lock, "netmap core lock", MTX_NETWORK_LOCK, MTX_DEF);
1326f196ce38SLuigi Rizzo 		}
132768b8534bSLuigi Rizzo 	}
132864ae02c3SLuigi Rizzo #ifdef linux
1329f196ce38SLuigi Rizzo 	if (ifp->netdev_ops) {
133064ae02c3SLuigi Rizzo 		D("netdev_ops %p", ifp->netdev_ops);
133164ae02c3SLuigi Rizzo 		/* prepare a clone of the netdev ops */
133264ae02c3SLuigi Rizzo 		na->nm_ndo = *ifp->netdev_ops;
1333f196ce38SLuigi Rizzo 	}
133464ae02c3SLuigi Rizzo 	na->nm_ndo.ndo_start_xmit = netmap_start_linux;
133564ae02c3SLuigi Rizzo #endif
133668b8534bSLuigi Rizzo 	D("%s for %s", buf ? "ok" : "failed", ifp->if_xname);
133768b8534bSLuigi Rizzo 
133868b8534bSLuigi Rizzo 	return (buf ? 0 : ENOMEM);
133968b8534bSLuigi Rizzo }
134068b8534bSLuigi Rizzo 
134168b8534bSLuigi Rizzo 
134268b8534bSLuigi Rizzo /*
134368b8534bSLuigi Rizzo  * Free the allocated memory linked to the given ``netmap_adapter``
134468b8534bSLuigi Rizzo  * object.
134568b8534bSLuigi Rizzo  */
134668b8534bSLuigi Rizzo void
134768b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp)
134868b8534bSLuigi Rizzo {
134968b8534bSLuigi Rizzo 	u_int i;
135068b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
135168b8534bSLuigi Rizzo 
135268b8534bSLuigi Rizzo 	if (!na)
135368b8534bSLuigi Rizzo 		return;
135468b8534bSLuigi Rizzo 
1355d76bf4ffSLuigi Rizzo 	for (i = 0; i < na->num_tx_rings + 1; i++) {
135668b8534bSLuigi Rizzo 		knlist_destroy(&na->tx_rings[i].si.si_note);
135764ae02c3SLuigi Rizzo 		mtx_destroy(&na->tx_rings[i].q_lock);
135868b8534bSLuigi Rizzo 	}
1359d76bf4ffSLuigi Rizzo 	for (i = 0; i < na->num_rx_rings + 1; i++) {
136064ae02c3SLuigi Rizzo 		knlist_destroy(&na->rx_rings[i].si.si_note);
136164ae02c3SLuigi Rizzo 		mtx_destroy(&na->rx_rings[i].q_lock);
136264ae02c3SLuigi Rizzo 	}
136364ae02c3SLuigi Rizzo 	knlist_destroy(&na->tx_si.si_note);
136464ae02c3SLuigi Rizzo 	knlist_destroy(&na->rx_si.si_note);
136568b8534bSLuigi Rizzo 	bzero(na, sizeof(*na));
1366d0c7b075SLuigi Rizzo 	WNA(ifp) = NULL;
136768b8534bSLuigi Rizzo 	free(na, M_DEVBUF);
136868b8534bSLuigi Rizzo }
136968b8534bSLuigi Rizzo 
137068b8534bSLuigi Rizzo 
137168b8534bSLuigi Rizzo /*
137202ad4083SLuigi Rizzo  * Intercept packets from the network stack and pass them
137302ad4083SLuigi Rizzo  * to netmap as incoming packets on the 'software' ring.
137468b8534bSLuigi Rizzo  * We are not locked when called.
137568b8534bSLuigi Rizzo  */
137668b8534bSLuigi Rizzo int
137768b8534bSLuigi Rizzo netmap_start(struct ifnet *ifp, struct mbuf *m)
137868b8534bSLuigi Rizzo {
137968b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
1380d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
13811a26580eSLuigi Rizzo 	u_int i, len = MBUF_LEN(m);
138202ad4083SLuigi Rizzo 	int error = EBUSY, lim = kring->nkr_num_slots - 1;
138368b8534bSLuigi Rizzo 	struct netmap_slot *slot;
138468b8534bSLuigi Rizzo 
138568b8534bSLuigi Rizzo 	if (netmap_verbose & NM_VERB_HOST)
138668b8534bSLuigi Rizzo 		D("%s packet %d len %d from the stack", ifp->if_xname,
138768b8534bSLuigi Rizzo 			kring->nr_hwcur + kring->nr_hwavail, len);
13881a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
138902ad4083SLuigi Rizzo 	if (kring->nr_hwavail >= lim) {
13905b248374SLuigi Rizzo 		if (netmap_verbose)
139168b8534bSLuigi Rizzo 			D("stack ring %s full\n", ifp->if_xname);
139268b8534bSLuigi Rizzo 		goto done;	/* no space */
139368b8534bSLuigi Rizzo 	}
139464ae02c3SLuigi Rizzo 	if (len > NETMAP_BUF_SIZE) {
139564ae02c3SLuigi Rizzo 		D("drop packet size %d > %d", len, NETMAP_BUF_SIZE);
139668b8534bSLuigi Rizzo 		goto done;	/* too long for us */
139768b8534bSLuigi Rizzo 	}
139868b8534bSLuigi Rizzo 
139968b8534bSLuigi Rizzo 	/* compute the insert position */
140068b8534bSLuigi Rizzo 	i = kring->nr_hwcur + kring->nr_hwavail;
140102ad4083SLuigi Rizzo 	if (i > lim)
140202ad4083SLuigi Rizzo 		i -= lim + 1;
140368b8534bSLuigi Rizzo 	slot = &kring->ring->slot[i];
140468b8534bSLuigi Rizzo 	m_copydata(m, 0, len, NMB(slot));
140568b8534bSLuigi Rizzo 	slot->len = len;
140668b8534bSLuigi Rizzo 	kring->nr_hwavail++;
140768b8534bSLuigi Rizzo 	if (netmap_verbose  & NM_VERB_HOST)
1408d76bf4ffSLuigi Rizzo 		D("wake up host ring %s %d", na->ifp->if_xname, na->num_rx_rings);
140968b8534bSLuigi Rizzo 	selwakeuppri(&kring->si, PI_NET);
141068b8534bSLuigi Rizzo 	error = 0;
141168b8534bSLuigi Rizzo done:
14121a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
141368b8534bSLuigi Rizzo 
141468b8534bSLuigi Rizzo 	/* release the mbuf in either cases of success or failure. As an
141568b8534bSLuigi Rizzo 	 * alternative, put the mbuf in a free list and free the list
141668b8534bSLuigi Rizzo 	 * only when really necessary.
141768b8534bSLuigi Rizzo 	 */
141868b8534bSLuigi Rizzo 	m_freem(m);
141968b8534bSLuigi Rizzo 
142068b8534bSLuigi Rizzo 	return (error);
142168b8534bSLuigi Rizzo }
142268b8534bSLuigi Rizzo 
142368b8534bSLuigi Rizzo 
142468b8534bSLuigi Rizzo /*
142568b8534bSLuigi Rizzo  * netmap_reset() is called by the driver routines when reinitializing
142668b8534bSLuigi Rizzo  * a ring. The driver is in charge of locking to protect the kring.
142768b8534bSLuigi Rizzo  * If netmap mode is not set just return NULL.
142868b8534bSLuigi Rizzo  */
142968b8534bSLuigi Rizzo struct netmap_slot *
143068b8534bSLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, int n,
143168b8534bSLuigi Rizzo 	u_int new_cur)
143268b8534bSLuigi Rizzo {
143368b8534bSLuigi Rizzo 	struct netmap_kring *kring;
1434506cc70cSLuigi Rizzo 	int new_hwofs, lim;
143568b8534bSLuigi Rizzo 
143668b8534bSLuigi Rizzo 	if (na == NULL)
143768b8534bSLuigi Rizzo 		return NULL;	/* no netmap support here */
143868b8534bSLuigi Rizzo 	if (!(na->ifp->if_capenable & IFCAP_NETMAP))
143968b8534bSLuigi Rizzo 		return NULL;	/* nothing to reinitialize */
144068b8534bSLuigi Rizzo 
144164ae02c3SLuigi Rizzo 	if (tx == NR_TX) {
144264ae02c3SLuigi Rizzo 		kring = na->tx_rings + n;
1443506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur - new_cur;
144464ae02c3SLuigi Rizzo 	} else {
144564ae02c3SLuigi Rizzo 		kring = na->rx_rings + n;
1446506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur + kring->nr_hwavail - new_cur;
144764ae02c3SLuigi Rizzo 	}
144864ae02c3SLuigi Rizzo 	lim = kring->nkr_num_slots - 1;
1449506cc70cSLuigi Rizzo 	if (new_hwofs > lim)
1450506cc70cSLuigi Rizzo 		new_hwofs -= lim + 1;
1451506cc70cSLuigi Rizzo 
1452506cc70cSLuigi Rizzo 	/* Alwayws set the new offset value and realign the ring. */
1453506cc70cSLuigi Rizzo 	kring->nkr_hwofs = new_hwofs;
1454506cc70cSLuigi Rizzo 	if (tx == NR_TX)
1455506cc70cSLuigi Rizzo 		kring->nr_hwavail = kring->nkr_num_slots - 1;
1456506cc70cSLuigi Rizzo 	D("new hwofs %d on %s %s[%d]",
1457506cc70cSLuigi Rizzo 			kring->nkr_hwofs, na->ifp->if_xname,
1458506cc70cSLuigi Rizzo 			tx == NR_TX ? "TX" : "RX", n);
1459506cc70cSLuigi Rizzo 
1460f196ce38SLuigi Rizzo #if 0 // def linux
1461f196ce38SLuigi Rizzo 	/* XXX check that the mappings are correct */
1462f196ce38SLuigi Rizzo 	/* need ring_nr, adapter->pdev, direction */
1463f196ce38SLuigi Rizzo 	buffer_info->dma = dma_map_single(&pdev->dev, addr, adapter->rx_buffer_len, DMA_FROM_DEVICE);
1464f196ce38SLuigi Rizzo 	if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
1465f196ce38SLuigi Rizzo 		D("error mapping rx netmap buffer %d", i);
1466f196ce38SLuigi Rizzo 		// XXX fix error handling
1467f196ce38SLuigi Rizzo 	}
1468f196ce38SLuigi Rizzo 
1469f196ce38SLuigi Rizzo #endif /* linux */
147068b8534bSLuigi Rizzo 	/*
147164ae02c3SLuigi Rizzo 	 * Wakeup on the individual and global lock
1472506cc70cSLuigi Rizzo 	 * We do the wakeup here, but the ring is not yet reconfigured.
1473506cc70cSLuigi Rizzo 	 * However, we are under lock so there are no races.
147468b8534bSLuigi Rizzo 	 */
147568b8534bSLuigi Rizzo 	selwakeuppri(&kring->si, PI_NET);
147664ae02c3SLuigi Rizzo 	selwakeuppri(tx == NR_TX ? &na->tx_si : &na->rx_si, PI_NET);
147768b8534bSLuigi Rizzo 	return kring->ring->slot;
147868b8534bSLuigi Rizzo }
147968b8534bSLuigi Rizzo 
148068b8534bSLuigi Rizzo 
148168b8534bSLuigi Rizzo /*
14821a26580eSLuigi Rizzo  * Default functions to handle rx/tx interrupts
14831a26580eSLuigi Rizzo  * we have 4 cases:
14841a26580eSLuigi Rizzo  * 1 ring, single lock:
14851a26580eSLuigi Rizzo  *	lock(core); wake(i=0); unlock(core)
14861a26580eSLuigi Rizzo  * N rings, single lock:
14871a26580eSLuigi Rizzo  *	lock(core); wake(i); wake(N+1) unlock(core)
14881a26580eSLuigi Rizzo  * 1 ring, separate locks: (i=0)
14891a26580eSLuigi Rizzo  *	lock(i); wake(i); unlock(i)
14901a26580eSLuigi Rizzo  * N rings, separate locks:
14911a26580eSLuigi Rizzo  *	lock(i); wake(i); unlock(i); lock(core) wake(N+1) unlock(core)
149264ae02c3SLuigi Rizzo  * work_done is non-null on the RX path.
14931a26580eSLuigi Rizzo  */
1494babc7c12SLuigi Rizzo int
1495babc7c12SLuigi Rizzo netmap_rx_irq(struct ifnet *ifp, int q, int *work_done)
14961a26580eSLuigi Rizzo {
14971a26580eSLuigi Rizzo 	struct netmap_adapter *na;
14981a26580eSLuigi Rizzo 	struct netmap_kring *r;
149964ae02c3SLuigi Rizzo 	NM_SELINFO_T *main_wq;
15001a26580eSLuigi Rizzo 
15011a26580eSLuigi Rizzo 	if (!(ifp->if_capenable & IFCAP_NETMAP))
15021a26580eSLuigi Rizzo 		return 0;
15031a26580eSLuigi Rizzo 	na = NA(ifp);
150464ae02c3SLuigi Rizzo 	if (work_done) { /* RX path */
150564ae02c3SLuigi Rizzo 		r = na->rx_rings + q;
150664ae02c3SLuigi Rizzo 		r->nr_kflags |= NKR_PENDINTR;
1507d76bf4ffSLuigi Rizzo 		main_wq = (na->num_rx_rings > 1) ? &na->rx_si : NULL;
150864ae02c3SLuigi Rizzo 	} else { /* tx path */
150964ae02c3SLuigi Rizzo 		r = na->tx_rings + q;
1510d76bf4ffSLuigi Rizzo 		main_wq = (na->num_tx_rings > 1) ? &na->tx_si : NULL;
151164ae02c3SLuigi Rizzo 		work_done = &q; /* dummy */
151264ae02c3SLuigi Rizzo 	}
15131a26580eSLuigi Rizzo 	if (na->separate_locks) {
151464ae02c3SLuigi Rizzo 		mtx_lock(&r->q_lock);
151564ae02c3SLuigi Rizzo 		selwakeuppri(&r->si, PI_NET);
151664ae02c3SLuigi Rizzo 		mtx_unlock(&r->q_lock);
151764ae02c3SLuigi Rizzo 		if (main_wq) {
15181a26580eSLuigi Rizzo 			mtx_lock(&na->core_lock);
151964ae02c3SLuigi Rizzo 			selwakeuppri(main_wq, PI_NET);
15201a26580eSLuigi Rizzo 			mtx_unlock(&na->core_lock);
15211a26580eSLuigi Rizzo 		}
15221a26580eSLuigi Rizzo 	} else {
15231a26580eSLuigi Rizzo 		mtx_lock(&na->core_lock);
152464ae02c3SLuigi Rizzo 		selwakeuppri(&r->si, PI_NET);
152564ae02c3SLuigi Rizzo 		if (main_wq)
152664ae02c3SLuigi Rizzo 			selwakeuppri(main_wq, PI_NET);
15271a26580eSLuigi Rizzo 		mtx_unlock(&na->core_lock);
15281a26580eSLuigi Rizzo 	}
15291a26580eSLuigi Rizzo 	*work_done = 1; /* do not fire napi again */
15301a26580eSLuigi Rizzo 	return 1;
15311a26580eSLuigi Rizzo }
15321a26580eSLuigi Rizzo 
153364ae02c3SLuigi Rizzo 
153401c7d25fSLuigi Rizzo #ifdef linux	/* linux-specific routines */
153501c7d25fSLuigi Rizzo 
153601c7d25fSLuigi Rizzo /*
153701c7d25fSLuigi Rizzo  * Remap linux arguments into the FreeBSD call.
153801c7d25fSLuigi Rizzo  * - pwait is the poll table, passed as 'dev';
153901c7d25fSLuigi Rizzo  *   If pwait == NULL someone else already woke up before. We can report
154001c7d25fSLuigi Rizzo  *   events but they are filtered upstream.
154101c7d25fSLuigi Rizzo  *   If pwait != NULL, then pwait->key contains the list of events.
154201c7d25fSLuigi Rizzo  * - events is computed from pwait as above.
154301c7d25fSLuigi Rizzo  * - file is passed as 'td';
154401c7d25fSLuigi Rizzo  */
154501c7d25fSLuigi Rizzo static u_int
154601c7d25fSLuigi Rizzo linux_netmap_poll(struct file * file, struct poll_table_struct *pwait)
154701c7d25fSLuigi Rizzo {
154801c7d25fSLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(3,4,0)
154901c7d25fSLuigi Rizzo 	int events = pwait ? pwait->key : POLLIN | POLLOUT;
155001c7d25fSLuigi Rizzo #else /* in 3.4.0 field 'key' was renamed to '_key' */
155101c7d25fSLuigi Rizzo 	int events = pwait ? pwait->_key : POLLIN | POLLOUT;
155201c7d25fSLuigi Rizzo #endif
155301c7d25fSLuigi Rizzo 	return netmap_poll((void *)pwait, events, (void *)file);
155401c7d25fSLuigi Rizzo }
155501c7d25fSLuigi Rizzo 
155601c7d25fSLuigi Rizzo static int
1557*0b8ed8e0SLuigi Rizzo netmap_mmap(struct file *f, struct vm_area_struct *vma)
155801c7d25fSLuigi Rizzo {
155901c7d25fSLuigi Rizzo 	int lut_skip, i, j;
156001c7d25fSLuigi Rizzo 	int user_skip = 0;
156101c7d25fSLuigi Rizzo 	struct lut_entry *l_entry;
156201c7d25fSLuigi Rizzo 	const struct netmap_obj_pool *p[] = {
156301c7d25fSLuigi Rizzo 		nm_mem->nm_if_pool,
156401c7d25fSLuigi Rizzo 		nm_mem->nm_ring_pool,
156501c7d25fSLuigi Rizzo 		nm_mem->nm_buf_pool };
156601c7d25fSLuigi Rizzo 	/*
156701c7d25fSLuigi Rizzo 	 * vma->vm_start: start of mapping user address space
156801c7d25fSLuigi Rizzo 	 * vma->vm_end: end of the mapping user address space
156901c7d25fSLuigi Rizzo 	 */
157001c7d25fSLuigi Rizzo 
1571*0b8ed8e0SLuigi Rizzo 	(void)f;	/* UNUSED */
157201c7d25fSLuigi Rizzo 	// XXX security checks
157301c7d25fSLuigi Rizzo 
157401c7d25fSLuigi Rizzo 	for (i = 0; i < 3; i++) {  /* loop through obj_pools */
157501c7d25fSLuigi Rizzo 		/*
157601c7d25fSLuigi Rizzo 		 * In each pool memory is allocated in clusters
157701c7d25fSLuigi Rizzo 		 * of size _clustsize , each containing clustentries
157801c7d25fSLuigi Rizzo 		 * entries. For each object k we already store the
157901c7d25fSLuigi Rizzo 		 * vtophys malling in lut[k] so we use that, scanning
158001c7d25fSLuigi Rizzo 		 * the lut[] array in steps of clustentries,
158101c7d25fSLuigi Rizzo 		 * and we map each cluster (not individual pages,
158201c7d25fSLuigi Rizzo 		 * it would be overkill).
158301c7d25fSLuigi Rizzo 		 */
158401c7d25fSLuigi Rizzo 		for (lut_skip = 0, j = 0; j < p[i]->_numclusters; j++) {
158501c7d25fSLuigi Rizzo 			l_entry = &p[i]->lut[lut_skip];
158601c7d25fSLuigi Rizzo 			if (remap_pfn_range(vma, vma->vm_start + user_skip,
158701c7d25fSLuigi Rizzo 					l_entry->paddr >> PAGE_SHIFT, p[i]->_clustsize,
158801c7d25fSLuigi Rizzo 					vma->vm_page_prot))
158901c7d25fSLuigi Rizzo 				return -EAGAIN; // XXX check return value
159001c7d25fSLuigi Rizzo 			lut_skip += p[i]->clustentries;
159101c7d25fSLuigi Rizzo 			user_skip += p[i]->_clustsize;
159201c7d25fSLuigi Rizzo 		}
159301c7d25fSLuigi Rizzo 	}
159401c7d25fSLuigi Rizzo 
159501c7d25fSLuigi Rizzo 	return 0;
159601c7d25fSLuigi Rizzo }
159701c7d25fSLuigi Rizzo 
159801c7d25fSLuigi Rizzo static netdev_tx_t
159901c7d25fSLuigi Rizzo netmap_start_linux(struct sk_buff *skb, struct net_device *dev)
160001c7d25fSLuigi Rizzo {
160101c7d25fSLuigi Rizzo 	netmap_start(dev, skb);
160201c7d25fSLuigi Rizzo 	return (NETDEV_TX_OK);
160301c7d25fSLuigi Rizzo }
160401c7d25fSLuigi Rizzo 
160501c7d25fSLuigi Rizzo 
1606*0b8ed8e0SLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)	// XXX was 38
160701c7d25fSLuigi Rizzo #define LIN_IOCTL_NAME	.ioctl
160801c7d25fSLuigi Rizzo int
160901c7d25fSLuigi Rizzo linux_netmap_ioctl(struct inode *inode, struct file *file, u_int cmd, u_long data /* arg */)
161001c7d25fSLuigi Rizzo #else
161101c7d25fSLuigi Rizzo #define LIN_IOCTL_NAME	.unlocked_ioctl
161201c7d25fSLuigi Rizzo long
161301c7d25fSLuigi Rizzo linux_netmap_ioctl(struct file *file, u_int cmd, u_long data /* arg */)
161401c7d25fSLuigi Rizzo #endif
161501c7d25fSLuigi Rizzo {
161601c7d25fSLuigi Rizzo 	int ret;
161701c7d25fSLuigi Rizzo 	struct nmreq nmr;
161801c7d25fSLuigi Rizzo 	bzero(&nmr, sizeof(nmr));
161901c7d25fSLuigi Rizzo 
162001c7d25fSLuigi Rizzo 	if (data && copy_from_user(&nmr, (void *)data, sizeof(nmr) ) != 0)
162101c7d25fSLuigi Rizzo 		return -EFAULT;
162201c7d25fSLuigi Rizzo 	ret = netmap_ioctl(NULL, cmd, (caddr_t)&nmr, 0, (void *)file);
162301c7d25fSLuigi Rizzo 	if (data && copy_to_user((void*)data, &nmr, sizeof(nmr) ) != 0)
162401c7d25fSLuigi Rizzo 		return -EFAULT;
162501c7d25fSLuigi Rizzo 	return -ret;
162601c7d25fSLuigi Rizzo }
162701c7d25fSLuigi Rizzo 
162801c7d25fSLuigi Rizzo 
162901c7d25fSLuigi Rizzo static int
1630*0b8ed8e0SLuigi Rizzo netmap_release(struct inode *inode, struct file *file)
163101c7d25fSLuigi Rizzo {
1632*0b8ed8e0SLuigi Rizzo 	(void)inode;	/* UNUSED */
163301c7d25fSLuigi Rizzo 	if (file->private_data)
163401c7d25fSLuigi Rizzo 		netmap_dtor(file->private_data);
163501c7d25fSLuigi Rizzo 	return (0);
163601c7d25fSLuigi Rizzo }
163701c7d25fSLuigi Rizzo 
163801c7d25fSLuigi Rizzo 
163901c7d25fSLuigi Rizzo static struct file_operations netmap_fops = {
164001c7d25fSLuigi Rizzo     .mmap = netmap_mmap,
164101c7d25fSLuigi Rizzo     LIN_IOCTL_NAME = linux_netmap_ioctl,
164201c7d25fSLuigi Rizzo     .poll = linux_netmap_poll,
164301c7d25fSLuigi Rizzo     .release = netmap_release,
164401c7d25fSLuigi Rizzo };
164501c7d25fSLuigi Rizzo 
164601c7d25fSLuigi Rizzo static struct miscdevice netmap_cdevsw = {	/* same name as FreeBSD */
164701c7d25fSLuigi Rizzo 	MISC_DYNAMIC_MINOR,
164801c7d25fSLuigi Rizzo 	"netmap",
164901c7d25fSLuigi Rizzo 	&netmap_fops,
165001c7d25fSLuigi Rizzo };
165101c7d25fSLuigi Rizzo 
165201c7d25fSLuigi Rizzo static int netmap_init(void);
165301c7d25fSLuigi Rizzo static void netmap_fini(void);
165401c7d25fSLuigi Rizzo 
165501c7d25fSLuigi Rizzo module_init(netmap_init);
165601c7d25fSLuigi Rizzo module_exit(netmap_fini);
165701c7d25fSLuigi Rizzo /* export certain symbols to other modules */
165801c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_attach);		// driver attach routines
165901c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_detach);		// driver detach routines
166001c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_ring_reinit);	// ring init on error
166101c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_buffer_lut);
166201c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_total_buffers);	// index check
166301c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_buffer_base);
166401c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_reset);		// ring init routines
166501c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_buf_size);
166601c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_rx_irq);		// default irq handler
166701c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_no_pendintr);	// XXX mitigation - should go away
166801c7d25fSLuigi Rizzo 
166901c7d25fSLuigi Rizzo 
167001c7d25fSLuigi Rizzo MODULE_AUTHOR("http://info.iet.unipi.it/~luigi/netmap/");
167101c7d25fSLuigi Rizzo MODULE_DESCRIPTION("The netmap packet I/O framework");
167201c7d25fSLuigi Rizzo MODULE_LICENSE("Dual BSD/GPL"); /* the code here is all BSD. */
167301c7d25fSLuigi Rizzo 
167401c7d25fSLuigi Rizzo #else /* __FreeBSD__ */
167501c7d25fSLuigi Rizzo 
1676babc7c12SLuigi Rizzo static struct cdevsw netmap_cdevsw = {
1677babc7c12SLuigi Rizzo 	.d_version = D_VERSION,
1678babc7c12SLuigi Rizzo 	.d_name = "netmap",
1679babc7c12SLuigi Rizzo 	.d_mmap = netmap_mmap,
1680babc7c12SLuigi Rizzo 	.d_ioctl = netmap_ioctl,
1681babc7c12SLuigi Rizzo 	.d_poll = netmap_poll,
1682babc7c12SLuigi Rizzo };
168301c7d25fSLuigi Rizzo #endif /* __FreeBSD__ */
1684babc7c12SLuigi Rizzo 
1685f196ce38SLuigi Rizzo #ifdef NM_BRIDGE
1686f196ce38SLuigi Rizzo /*
1687f196ce38SLuigi Rizzo  *---- support for virtual bridge -----
1688f196ce38SLuigi Rizzo  */
1689f196ce38SLuigi Rizzo 
1690f196ce38SLuigi Rizzo /* ----- FreeBSD if_bridge hash function ------- */
1691f196ce38SLuigi Rizzo 
1692f196ce38SLuigi Rizzo /*
1693f196ce38SLuigi Rizzo  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
1694f196ce38SLuigi Rizzo  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
1695f196ce38SLuigi Rizzo  *
1696f196ce38SLuigi Rizzo  * http://www.burtleburtle.net/bob/hash/spooky.html
1697f196ce38SLuigi Rizzo  */
1698f196ce38SLuigi Rizzo #define mix(a, b, c)                                                    \
1699f196ce38SLuigi Rizzo do {                                                                    \
1700f196ce38SLuigi Rizzo         a -= b; a -= c; a ^= (c >> 13);                                 \
1701f196ce38SLuigi Rizzo         b -= c; b -= a; b ^= (a << 8);                                  \
1702f196ce38SLuigi Rizzo         c -= a; c -= b; c ^= (b >> 13);                                 \
1703f196ce38SLuigi Rizzo         a -= b; a -= c; a ^= (c >> 12);                                 \
1704f196ce38SLuigi Rizzo         b -= c; b -= a; b ^= (a << 16);                                 \
1705f196ce38SLuigi Rizzo         c -= a; c -= b; c ^= (b >> 5);                                  \
1706f196ce38SLuigi Rizzo         a -= b; a -= c; a ^= (c >> 3);                                  \
1707f196ce38SLuigi Rizzo         b -= c; b -= a; b ^= (a << 10);                                 \
1708f196ce38SLuigi Rizzo         c -= a; c -= b; c ^= (b >> 15);                                 \
1709f196ce38SLuigi Rizzo } while (/*CONSTCOND*/0)
1710f196ce38SLuigi Rizzo 
1711f196ce38SLuigi Rizzo static __inline uint32_t
1712f196ce38SLuigi Rizzo nm_bridge_rthash(const uint8_t *addr)
1713f196ce38SLuigi Rizzo {
1714f196ce38SLuigi Rizzo         uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = 0; // hask key
1715f196ce38SLuigi Rizzo 
1716f196ce38SLuigi Rizzo         b += addr[5] << 8;
1717f196ce38SLuigi Rizzo         b += addr[4];
1718f196ce38SLuigi Rizzo         a += addr[3] << 24;
1719f196ce38SLuigi Rizzo         a += addr[2] << 16;
1720f196ce38SLuigi Rizzo         a += addr[1] << 8;
1721f196ce38SLuigi Rizzo         a += addr[0];
1722f196ce38SLuigi Rizzo 
1723f196ce38SLuigi Rizzo         mix(a, b, c);
1724f196ce38SLuigi Rizzo #define BRIDGE_RTHASH_MASK	(NM_BDG_HASH-1)
1725f196ce38SLuigi Rizzo         return (c & BRIDGE_RTHASH_MASK);
1726f196ce38SLuigi Rizzo }
1727f196ce38SLuigi Rizzo 
1728f196ce38SLuigi Rizzo #undef mix
1729f196ce38SLuigi Rizzo 
1730f196ce38SLuigi Rizzo 
1731f196ce38SLuigi Rizzo static int
1732f196ce38SLuigi Rizzo bdg_netmap_reg(struct ifnet *ifp, int onoff)
1733f196ce38SLuigi Rizzo {
1734f196ce38SLuigi Rizzo 	int i, err = 0;
1735f196ce38SLuigi Rizzo 	struct nm_bridge *b = ifp->if_bridge;
1736f196ce38SLuigi Rizzo 
1737f196ce38SLuigi Rizzo 	BDG_LOCK(b);
1738f196ce38SLuigi Rizzo 	if (onoff) {
1739f196ce38SLuigi Rizzo 		/* the interface must be already in the list.
1740f196ce38SLuigi Rizzo 		 * only need to mark the port as active
1741f196ce38SLuigi Rizzo 		 */
1742f196ce38SLuigi Rizzo 		ND("should attach %s to the bridge", ifp->if_xname);
1743f196ce38SLuigi Rizzo 		for (i=0; i < NM_BDG_MAXPORTS; i++)
1744f196ce38SLuigi Rizzo 			if (b->bdg_ports[i] == ifp)
1745f196ce38SLuigi Rizzo 				break;
1746f196ce38SLuigi Rizzo 		if (i == NM_BDG_MAXPORTS) {
1747f196ce38SLuigi Rizzo 			D("no more ports available");
1748f196ce38SLuigi Rizzo 			err = EINVAL;
1749f196ce38SLuigi Rizzo 			goto done;
1750f196ce38SLuigi Rizzo 		}
1751f196ce38SLuigi Rizzo 		ND("setting %s in netmap mode", ifp->if_xname);
1752f196ce38SLuigi Rizzo 		ifp->if_capenable |= IFCAP_NETMAP;
1753f196ce38SLuigi Rizzo 		NA(ifp)->bdg_port = i;
1754f196ce38SLuigi Rizzo 		b->act_ports |= (1<<i);
1755f196ce38SLuigi Rizzo 		b->bdg_ports[i] = ifp;
1756f196ce38SLuigi Rizzo 	} else {
1757f196ce38SLuigi Rizzo 		/* should be in the list, too -- remove from the mask */
1758f196ce38SLuigi Rizzo 		ND("removing %s from netmap mode", ifp->if_xname);
1759f196ce38SLuigi Rizzo 		ifp->if_capenable &= ~IFCAP_NETMAP;
1760f196ce38SLuigi Rizzo 		i = NA(ifp)->bdg_port;
1761f196ce38SLuigi Rizzo 		b->act_ports &= ~(1<<i);
1762f196ce38SLuigi Rizzo 	}
1763f196ce38SLuigi Rizzo done:
1764f196ce38SLuigi Rizzo 	BDG_UNLOCK(b);
1765f196ce38SLuigi Rizzo 	return err;
1766f196ce38SLuigi Rizzo }
1767f196ce38SLuigi Rizzo 
1768f196ce38SLuigi Rizzo 
1769f196ce38SLuigi Rizzo static int
1770f196ce38SLuigi Rizzo nm_bdg_flush(struct nm_bdg_fwd *ft, int n, struct ifnet *ifp)
1771f196ce38SLuigi Rizzo {
1772f196ce38SLuigi Rizzo 	int i, ifn;
1773f196ce38SLuigi Rizzo 	uint64_t all_dst, dst;
1774f196ce38SLuigi Rizzo 	uint32_t sh, dh;
1775f196ce38SLuigi Rizzo 	uint64_t mysrc = 1 << NA(ifp)->bdg_port;
1776f196ce38SLuigi Rizzo 	uint64_t smac, dmac;
1777f196ce38SLuigi Rizzo 	struct netmap_slot *slot;
1778f196ce38SLuigi Rizzo 	struct nm_bridge *b = ifp->if_bridge;
1779f196ce38SLuigi Rizzo 
1780f196ce38SLuigi Rizzo 	ND("prepare to send %d packets, act_ports 0x%x", n, b->act_ports);
1781f196ce38SLuigi Rizzo 	/* only consider valid destinations */
1782f196ce38SLuigi Rizzo 	all_dst = (b->act_ports & ~mysrc);
1783f196ce38SLuigi Rizzo 	/* first pass: hash and find destinations */
1784f196ce38SLuigi Rizzo 	for (i = 0; likely(i < n); i++) {
1785f196ce38SLuigi Rizzo 		uint8_t *buf = ft[i].buf;
1786f196ce38SLuigi Rizzo 		dmac = le64toh(*(uint64_t *)(buf)) & 0xffffffffffff;
1787f196ce38SLuigi Rizzo 		smac = le64toh(*(uint64_t *)(buf + 4));
1788f196ce38SLuigi Rizzo 		smac >>= 16;
1789f196ce38SLuigi Rizzo 		if (unlikely(netmap_verbose)) {
1790f196ce38SLuigi Rizzo 		    uint8_t *s = buf+6, *d = buf;
1791f196ce38SLuigi Rizzo 		    D("%d len %4d %02x:%02x:%02x:%02x:%02x:%02x -> %02x:%02x:%02x:%02x:%02x:%02x",
1792f196ce38SLuigi Rizzo 			i,
1793f196ce38SLuigi Rizzo 			ft[i].len,
1794f196ce38SLuigi Rizzo 			s[0], s[1], s[2], s[3], s[4], s[5],
1795f196ce38SLuigi Rizzo 			d[0], d[1], d[2], d[3], d[4], d[5]);
1796f196ce38SLuigi Rizzo 		}
1797f196ce38SLuigi Rizzo 		/*
1798f196ce38SLuigi Rizzo 		 * The hash is somewhat expensive, there might be some
1799f196ce38SLuigi Rizzo 		 * worthwhile optimizations here.
1800f196ce38SLuigi Rizzo 		 */
1801f196ce38SLuigi Rizzo 		if ((buf[6] & 1) == 0) { /* valid src */
1802f196ce38SLuigi Rizzo 		    	uint8_t *s = buf+6;
1803f196ce38SLuigi Rizzo 			sh = nm_bridge_rthash(buf+6); // XXX hash of source
1804f196ce38SLuigi Rizzo 			/* update source port forwarding entry */
1805f196ce38SLuigi Rizzo 			b->ht[sh].mac = smac;	/* XXX expire ? */
1806f196ce38SLuigi Rizzo 			b->ht[sh].ports = mysrc;
1807f196ce38SLuigi Rizzo 			if (netmap_verbose)
1808f196ce38SLuigi Rizzo 			    D("src %02x:%02x:%02x:%02x:%02x:%02x on port %d",
1809f196ce38SLuigi Rizzo 				s[0], s[1], s[2], s[3], s[4], s[5], NA(ifp)->bdg_port);
1810f196ce38SLuigi Rizzo 		}
1811f196ce38SLuigi Rizzo 		dst = 0;
1812f196ce38SLuigi Rizzo 		if ( (buf[0] & 1) == 0) { /* unicast */
1813f196ce38SLuigi Rizzo 		    	uint8_t *d = buf;
1814f196ce38SLuigi Rizzo 			dh = nm_bridge_rthash(buf); // XXX hash of dst
1815f196ce38SLuigi Rizzo 			if (b->ht[dh].mac == dmac) {	/* found dst */
1816f196ce38SLuigi Rizzo 				dst = b->ht[dh].ports;
1817f196ce38SLuigi Rizzo 				if (netmap_verbose)
1818f196ce38SLuigi Rizzo 				    D("dst %02x:%02x:%02x:%02x:%02x:%02x to port %x",
1819f196ce38SLuigi Rizzo 					d[0], d[1], d[2], d[3], d[4], d[5], (uint32_t)(dst >> 16));
1820f196ce38SLuigi Rizzo 			}
1821f196ce38SLuigi Rizzo 		}
1822f196ce38SLuigi Rizzo 		if (dst == 0)
1823f196ce38SLuigi Rizzo 			dst = all_dst;
1824f196ce38SLuigi Rizzo 		dst &= all_dst; /* only consider valid ports */
1825f196ce38SLuigi Rizzo 		if (unlikely(netmap_verbose))
1826f196ce38SLuigi Rizzo 			D("pkt goes to ports 0x%x", (uint32_t)dst);
1827f196ce38SLuigi Rizzo 		ft[i].dst = dst;
1828f196ce38SLuigi Rizzo 	}
1829f196ce38SLuigi Rizzo 
1830f196ce38SLuigi Rizzo 	/* second pass, scan interfaces and forward */
1831f196ce38SLuigi Rizzo 	all_dst = (b->act_ports & ~mysrc);
1832f196ce38SLuigi Rizzo 	for (ifn = 0; all_dst; ifn++) {
1833f196ce38SLuigi Rizzo 		struct ifnet *dst_ifp = b->bdg_ports[ifn];
1834f196ce38SLuigi Rizzo 		struct netmap_adapter *na;
1835f196ce38SLuigi Rizzo 		struct netmap_kring *kring;
1836f196ce38SLuigi Rizzo 		struct netmap_ring *ring;
1837f196ce38SLuigi Rizzo 		int j, lim, sent, locked;
1838f196ce38SLuigi Rizzo 
1839f196ce38SLuigi Rizzo 		if (!dst_ifp)
1840f196ce38SLuigi Rizzo 			continue;
1841f196ce38SLuigi Rizzo 		ND("scan port %d %s", ifn, dst_ifp->if_xname);
1842f196ce38SLuigi Rizzo 		dst = 1 << ifn;
1843f196ce38SLuigi Rizzo 		if ((dst & all_dst) == 0)	/* skip if not set */
1844f196ce38SLuigi Rizzo 			continue;
1845f196ce38SLuigi Rizzo 		all_dst &= ~dst;	/* clear current node */
1846f196ce38SLuigi Rizzo 		na = NA(dst_ifp);
1847f196ce38SLuigi Rizzo 
1848f196ce38SLuigi Rizzo 		ring = NULL;
1849f196ce38SLuigi Rizzo 		kring = NULL;
1850f196ce38SLuigi Rizzo 		lim = sent = locked = 0;
1851f196ce38SLuigi Rizzo 		/* inside, scan slots */
1852f196ce38SLuigi Rizzo 		for (i = 0; likely(i < n); i++) {
1853f196ce38SLuigi Rizzo 			if ((ft[i].dst & dst) == 0)
1854f196ce38SLuigi Rizzo 				continue;	/* not here */
1855f196ce38SLuigi Rizzo 			if (!locked) {
1856f196ce38SLuigi Rizzo 				kring = &na->rx_rings[0];
1857f196ce38SLuigi Rizzo 				ring = kring->ring;
1858f196ce38SLuigi Rizzo 				lim = kring->nkr_num_slots - 1;
1859f196ce38SLuigi Rizzo 				na->nm_lock(dst_ifp, NETMAP_RX_LOCK, 0);
1860f196ce38SLuigi Rizzo 				locked = 1;
1861f196ce38SLuigi Rizzo 			}
1862f196ce38SLuigi Rizzo 			if (unlikely(kring->nr_hwavail >= lim)) {
1863f196ce38SLuigi Rizzo 				if (netmap_verbose)
1864f196ce38SLuigi Rizzo 					D("rx ring full on %s", ifp->if_xname);
1865f196ce38SLuigi Rizzo 				break;
1866f196ce38SLuigi Rizzo 			}
1867f196ce38SLuigi Rizzo 			j = kring->nr_hwcur + kring->nr_hwavail;
1868f196ce38SLuigi Rizzo 			if (j > lim)
1869f196ce38SLuigi Rizzo 				j -= kring->nkr_num_slots;
1870f196ce38SLuigi Rizzo 			slot = &ring->slot[j];
1871f196ce38SLuigi Rizzo 			ND("send %d %d bytes at %s:%d", i, ft[i].len, dst_ifp->if_xname, j);
1872f196ce38SLuigi Rizzo 			pkt_copy(ft[i].buf, NMB(slot), ft[i].len);
1873f196ce38SLuigi Rizzo 			slot->len = ft[i].len;
1874f196ce38SLuigi Rizzo 			kring->nr_hwavail++;
1875f196ce38SLuigi Rizzo 			sent++;
1876f196ce38SLuigi Rizzo 		}
1877f196ce38SLuigi Rizzo 		if (locked) {
1878f196ce38SLuigi Rizzo 			ND("sent %d on %s", sent, dst_ifp->if_xname);
1879f196ce38SLuigi Rizzo 			if (sent)
1880f196ce38SLuigi Rizzo 				selwakeuppri(&kring->si, PI_NET);
1881f196ce38SLuigi Rizzo 			na->nm_lock(dst_ifp, NETMAP_RX_UNLOCK, 0);
1882f196ce38SLuigi Rizzo 		}
1883f196ce38SLuigi Rizzo 	}
1884f196ce38SLuigi Rizzo 	return 0;
1885f196ce38SLuigi Rizzo }
1886f196ce38SLuigi Rizzo 
1887f196ce38SLuigi Rizzo /*
1888f196ce38SLuigi Rizzo  * main dispatch routine
1889f196ce38SLuigi Rizzo  */
1890f196ce38SLuigi Rizzo static int
1891f196ce38SLuigi Rizzo bdg_netmap_txsync(struct ifnet *ifp, u_int ring_nr, int do_lock)
1892f196ce38SLuigi Rizzo {
1893f196ce38SLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
1894f196ce38SLuigi Rizzo 	struct netmap_kring *kring = &na->tx_rings[ring_nr];
1895f196ce38SLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
1896f196ce38SLuigi Rizzo 	int i, j, k, lim = kring->nkr_num_slots - 1;
1897f196ce38SLuigi Rizzo 	struct nm_bdg_fwd *ft = (struct nm_bdg_fwd *)(ifp + 1);
1898f196ce38SLuigi Rizzo 	int ft_i;	/* position in the forwarding table */
1899f196ce38SLuigi Rizzo 
1900f196ce38SLuigi Rizzo 	k = ring->cur;
1901f196ce38SLuigi Rizzo 	if (k > lim)
1902f196ce38SLuigi Rizzo 		return netmap_ring_reinit(kring);
1903f196ce38SLuigi Rizzo 	if (do_lock)
1904f196ce38SLuigi Rizzo 		na->nm_lock(ifp, NETMAP_TX_LOCK, ring_nr);
1905f196ce38SLuigi Rizzo 
1906f196ce38SLuigi Rizzo 	if (netmap_bridge <= 0) { /* testing only */
1907f196ce38SLuigi Rizzo 		j = k; // used all
1908f196ce38SLuigi Rizzo 		goto done;
1909f196ce38SLuigi Rizzo 	}
1910f196ce38SLuigi Rizzo 	if (netmap_bridge > NM_BDG_BATCH)
1911f196ce38SLuigi Rizzo 		netmap_bridge = NM_BDG_BATCH;
1912f196ce38SLuigi Rizzo 
1913f196ce38SLuigi Rizzo 	ft_i = 0;	/* start from 0 */
1914f196ce38SLuigi Rizzo 	for (j = kring->nr_hwcur; likely(j != k); j = unlikely(j == lim) ? 0 : j+1) {
1915f196ce38SLuigi Rizzo 		struct netmap_slot *slot = &ring->slot[j];
1916f196ce38SLuigi Rizzo 		int len = ft[ft_i].len = slot->len;
1917f196ce38SLuigi Rizzo 		char *buf = ft[ft_i].buf = NMB(slot);
1918f196ce38SLuigi Rizzo 
1919f196ce38SLuigi Rizzo 		prefetch(buf);
1920f196ce38SLuigi Rizzo 		if (unlikely(len < 14))
1921f196ce38SLuigi Rizzo 			continue;
1922f196ce38SLuigi Rizzo 		if (unlikely(++ft_i == netmap_bridge))
1923f196ce38SLuigi Rizzo 			ft_i = nm_bdg_flush(ft, ft_i, ifp);
1924f196ce38SLuigi Rizzo 	}
1925f196ce38SLuigi Rizzo 	if (ft_i)
1926f196ce38SLuigi Rizzo 		ft_i = nm_bdg_flush(ft, ft_i, ifp);
1927f196ce38SLuigi Rizzo 	/* count how many packets we sent */
1928f196ce38SLuigi Rizzo 	i = k - j;
1929f196ce38SLuigi Rizzo 	if (i < 0)
1930f196ce38SLuigi Rizzo 		i += kring->nkr_num_slots;
1931f196ce38SLuigi Rizzo 	kring->nr_hwavail = kring->nkr_num_slots - 1 - i;
1932f196ce38SLuigi Rizzo 	if (j != k)
1933f196ce38SLuigi Rizzo 		D("early break at %d/ %d, avail %d", j, k, kring->nr_hwavail);
1934f196ce38SLuigi Rizzo 
1935f196ce38SLuigi Rizzo done:
1936f196ce38SLuigi Rizzo 	kring->nr_hwcur = j;
1937f196ce38SLuigi Rizzo 	ring->avail = kring->nr_hwavail;
1938f196ce38SLuigi Rizzo 	if (do_lock)
1939f196ce38SLuigi Rizzo 		na->nm_lock(ifp, NETMAP_TX_UNLOCK, ring_nr);
1940f196ce38SLuigi Rizzo 
1941f196ce38SLuigi Rizzo 	if (netmap_verbose)
1942f196ce38SLuigi Rizzo 		D("%s ring %d lock %d", ifp->if_xname, ring_nr, do_lock);
1943f196ce38SLuigi Rizzo 	return 0;
1944f196ce38SLuigi Rizzo }
1945f196ce38SLuigi Rizzo 
1946f196ce38SLuigi Rizzo static int
1947f196ce38SLuigi Rizzo bdg_netmap_rxsync(struct ifnet *ifp, u_int ring_nr, int do_lock)
1948f196ce38SLuigi Rizzo {
1949f196ce38SLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
1950f196ce38SLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[ring_nr];
1951f196ce38SLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
1952f196ce38SLuigi Rizzo 	int j, n, lim = kring->nkr_num_slots - 1;
1953f196ce38SLuigi Rizzo 	u_int k = ring->cur, resvd = ring->reserved;
1954f196ce38SLuigi Rizzo 
1955f196ce38SLuigi Rizzo 	ND("%s ring %d lock %d avail %d",
1956f196ce38SLuigi Rizzo 		ifp->if_xname, ring_nr, do_lock, kring->nr_hwavail);
1957f196ce38SLuigi Rizzo 
1958f196ce38SLuigi Rizzo 	if (k > lim)
1959f196ce38SLuigi Rizzo 		return netmap_ring_reinit(kring);
1960f196ce38SLuigi Rizzo 	if (do_lock)
1961f196ce38SLuigi Rizzo 		na->nm_lock(ifp, NETMAP_RX_LOCK, ring_nr);
1962f196ce38SLuigi Rizzo 
1963f196ce38SLuigi Rizzo 	/* skip past packets that userspace has released */
1964f196ce38SLuigi Rizzo 	j = kring->nr_hwcur;    /* netmap ring index */
1965f196ce38SLuigi Rizzo 	if (resvd > 0) {
1966f196ce38SLuigi Rizzo 		if (resvd + ring->avail >= lim + 1) {
1967f196ce38SLuigi Rizzo 			D("XXX invalid reserve/avail %d %d", resvd, ring->avail);
1968f196ce38SLuigi Rizzo 			ring->reserved = resvd = 0; // XXX panic...
1969f196ce38SLuigi Rizzo 		}
1970f196ce38SLuigi Rizzo 		k = (k >= resvd) ? k - resvd : k + lim + 1 - resvd;
1971f196ce38SLuigi Rizzo 	}
1972f196ce38SLuigi Rizzo 
1973f196ce38SLuigi Rizzo 	if (j != k) { /* userspace has released some packets. */
1974f196ce38SLuigi Rizzo 		n = k - j;
1975f196ce38SLuigi Rizzo 		if (n < 0)
1976f196ce38SLuigi Rizzo 			n += kring->nkr_num_slots;
1977f196ce38SLuigi Rizzo 		ND("userspace releases %d packets", n);
1978f196ce38SLuigi Rizzo                 for (n = 0; likely(j != k); n++) {
1979f196ce38SLuigi Rizzo                         struct netmap_slot *slot = &ring->slot[j];
1980f196ce38SLuigi Rizzo                         void *addr = NMB(slot);
1981f196ce38SLuigi Rizzo 
1982f196ce38SLuigi Rizzo                         if (addr == netmap_buffer_base) { /* bad buf */
1983f196ce38SLuigi Rizzo                                 if (do_lock)
1984f196ce38SLuigi Rizzo                                         na->nm_lock(ifp, NETMAP_RX_UNLOCK, ring_nr);
1985f196ce38SLuigi Rizzo                                 return netmap_ring_reinit(kring);
1986f196ce38SLuigi Rizzo                         }
1987f196ce38SLuigi Rizzo 			/* decrease refcount for buffer */
1988f196ce38SLuigi Rizzo 
1989f196ce38SLuigi Rizzo 			slot->flags &= ~NS_BUF_CHANGED;
1990f196ce38SLuigi Rizzo                         j = unlikely(j == lim) ? 0 : j + 1;
1991f196ce38SLuigi Rizzo                 }
1992f196ce38SLuigi Rizzo                 kring->nr_hwavail -= n;
1993f196ce38SLuigi Rizzo                 kring->nr_hwcur = k;
1994f196ce38SLuigi Rizzo         }
1995f196ce38SLuigi Rizzo         /* tell userspace that there are new packets */
1996f196ce38SLuigi Rizzo         ring->avail = kring->nr_hwavail - resvd;
1997f196ce38SLuigi Rizzo 
1998f196ce38SLuigi Rizzo 	if (do_lock)
1999f196ce38SLuigi Rizzo 		na->nm_lock(ifp, NETMAP_RX_UNLOCK, ring_nr);
2000f196ce38SLuigi Rizzo 	return 0;
2001f196ce38SLuigi Rizzo }
2002f196ce38SLuigi Rizzo 
2003f196ce38SLuigi Rizzo static void
2004f196ce38SLuigi Rizzo bdg_netmap_attach(struct ifnet *ifp)
2005f196ce38SLuigi Rizzo {
2006f196ce38SLuigi Rizzo 	struct netmap_adapter na;
2007f196ce38SLuigi Rizzo 
2008f196ce38SLuigi Rizzo 	ND("attaching virtual bridge");
2009f196ce38SLuigi Rizzo 	bzero(&na, sizeof(na));
2010f196ce38SLuigi Rizzo 
2011f196ce38SLuigi Rizzo 	na.ifp = ifp;
2012f196ce38SLuigi Rizzo 	na.separate_locks = 1;
2013f196ce38SLuigi Rizzo 	na.num_tx_desc = NM_BRIDGE_RINGSIZE;
2014f196ce38SLuigi Rizzo 	na.num_rx_desc = NM_BRIDGE_RINGSIZE;
2015f196ce38SLuigi Rizzo 	na.nm_txsync = bdg_netmap_txsync;
2016f196ce38SLuigi Rizzo 	na.nm_rxsync = bdg_netmap_rxsync;
2017f196ce38SLuigi Rizzo 	na.nm_register = bdg_netmap_reg;
2018f196ce38SLuigi Rizzo 	netmap_attach(&na, 1);
2019f196ce38SLuigi Rizzo }
2020f196ce38SLuigi Rizzo 
2021f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */
2022babc7c12SLuigi Rizzo 
2023babc7c12SLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */
2024babc7c12SLuigi Rizzo 
2025babc7c12SLuigi Rizzo 
20261a26580eSLuigi Rizzo /*
202768b8534bSLuigi Rizzo  * Module loader.
202868b8534bSLuigi Rizzo  *
202968b8534bSLuigi Rizzo  * Create the /dev/netmap device and initialize all global
203068b8534bSLuigi Rizzo  * variables.
203168b8534bSLuigi Rizzo  *
203268b8534bSLuigi Rizzo  * Return 0 on success, errno on failure.
203368b8534bSLuigi Rizzo  */
203468b8534bSLuigi Rizzo static int
203568b8534bSLuigi Rizzo netmap_init(void)
203668b8534bSLuigi Rizzo {
203768b8534bSLuigi Rizzo 	int error;
203868b8534bSLuigi Rizzo 
203968b8534bSLuigi Rizzo 	error = netmap_memory_init();
204068b8534bSLuigi Rizzo 	if (error != 0) {
204168b8534bSLuigi Rizzo 		printf("netmap: unable to initialize the memory allocator.");
204268b8534bSLuigi Rizzo 		return (error);
204368b8534bSLuigi Rizzo 	}
204468b8534bSLuigi Rizzo 	printf("netmap: loaded module with %d Mbytes\n",
204564ae02c3SLuigi Rizzo 		(int)(nm_mem->nm_totalsize >> 20));
204668b8534bSLuigi Rizzo 	netmap_dev = make_dev(&netmap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660,
204768b8534bSLuigi Rizzo 			      "netmap");
2048f196ce38SLuigi Rizzo 
2049f196ce38SLuigi Rizzo #ifdef NM_BRIDGE
2050f196ce38SLuigi Rizzo 	{
2051f196ce38SLuigi Rizzo 	int i;
2052f196ce38SLuigi Rizzo 	for (i = 0; i < NM_BRIDGES; i++)
2053f196ce38SLuigi Rizzo 		mtx_init(&nm_bridges[i].bdg_lock, "bdg lock", "bdg_lock", MTX_DEF);
2054f196ce38SLuigi Rizzo 	}
2055f196ce38SLuigi Rizzo #endif
2056babc7c12SLuigi Rizzo 	return (error);
205768b8534bSLuigi Rizzo }
205868b8534bSLuigi Rizzo 
205968b8534bSLuigi Rizzo 
206068b8534bSLuigi Rizzo /*
206168b8534bSLuigi Rizzo  * Module unloader.
206268b8534bSLuigi Rizzo  *
206368b8534bSLuigi Rizzo  * Free all the memory, and destroy the ``/dev/netmap`` device.
206468b8534bSLuigi Rizzo  */
206568b8534bSLuigi Rizzo static void
206668b8534bSLuigi Rizzo netmap_fini(void)
206768b8534bSLuigi Rizzo {
206868b8534bSLuigi Rizzo 	destroy_dev(netmap_dev);
206968b8534bSLuigi Rizzo 	netmap_memory_fini();
207068b8534bSLuigi Rizzo 	printf("netmap: unloaded module.\n");
207168b8534bSLuigi Rizzo }
207268b8534bSLuigi Rizzo 
207368b8534bSLuigi Rizzo 
2074f196ce38SLuigi Rizzo #ifdef __FreeBSD__
207568b8534bSLuigi Rizzo /*
207668b8534bSLuigi Rizzo  * Kernel entry point.
207768b8534bSLuigi Rizzo  *
207868b8534bSLuigi Rizzo  * Initialize/finalize the module and return.
207968b8534bSLuigi Rizzo  *
208068b8534bSLuigi Rizzo  * Return 0 on success, errno on failure.
208168b8534bSLuigi Rizzo  */
208268b8534bSLuigi Rizzo static int
208368b8534bSLuigi Rizzo netmap_loader(__unused struct module *module, int event, __unused void *arg)
208468b8534bSLuigi Rizzo {
208568b8534bSLuigi Rizzo 	int error = 0;
208668b8534bSLuigi Rizzo 
208768b8534bSLuigi Rizzo 	switch (event) {
208868b8534bSLuigi Rizzo 	case MOD_LOAD:
208968b8534bSLuigi Rizzo 		error = netmap_init();
209068b8534bSLuigi Rizzo 		break;
209168b8534bSLuigi Rizzo 
209268b8534bSLuigi Rizzo 	case MOD_UNLOAD:
209368b8534bSLuigi Rizzo 		netmap_fini();
209468b8534bSLuigi Rizzo 		break;
209568b8534bSLuigi Rizzo 
209668b8534bSLuigi Rizzo 	default:
209768b8534bSLuigi Rizzo 		error = EOPNOTSUPP;
209868b8534bSLuigi Rizzo 		break;
209968b8534bSLuigi Rizzo 	}
210068b8534bSLuigi Rizzo 
210168b8534bSLuigi Rizzo 	return (error);
210268b8534bSLuigi Rizzo }
210368b8534bSLuigi Rizzo 
210468b8534bSLuigi Rizzo 
210568b8534bSLuigi Rizzo DEV_MODULE(netmap, netmap_loader, NULL);
2106f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */
2107