xref: /freebsd-14.2/sys/net/netmap_user.h (revision 37e3a6d3)
168b8534bSLuigi Rizzo /*
2*37e3a6d3SLuigi Rizzo  * Copyright (C) 2011-2016 Universita` di Pisa
3*37e3a6d3SLuigi Rizzo  * All rights reserved.
468b8534bSLuigi Rizzo  *
568b8534bSLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
6f9790aebSLuigi Rizzo  * modification, are permitted provided that the following conditions
7f9790aebSLuigi Rizzo  * are met:
868b8534bSLuigi Rizzo  *
968b8534bSLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
1068b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
1168b8534bSLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
1268b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
13f9790aebSLuigi Rizzo  *      documentation and/or other materials provided with the distribution.
1468b8534bSLuigi Rizzo  *
15f9790aebSLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1668b8534bSLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17f9790aebSLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18f9790aebSLuigi Rizzo  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19f9790aebSLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20f9790aebSLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21f9790aebSLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22f9790aebSLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23f9790aebSLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24f9790aebSLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25f9790aebSLuigi Rizzo  * SUCH DAMAGE.
2668b8534bSLuigi Rizzo  */
2768b8534bSLuigi Rizzo 
2868b8534bSLuigi Rizzo /*
2968b8534bSLuigi Rizzo  * $FreeBSD$
3068b8534bSLuigi Rizzo  *
3117885a7bSLuigi Rizzo  * Functions and macros to manipulate netmap structures and packets
3217885a7bSLuigi Rizzo  * in userspace. See netmap(4) for more information.
3368b8534bSLuigi Rizzo  *
3464ae02c3SLuigi Rizzo  * The address of the struct netmap_if, say nifp, is computed from the
3564ae02c3SLuigi Rizzo  * value returned from ioctl(.., NIOCREG, ...) and the mmap region:
3668b8534bSLuigi Rizzo  *	ioctl(fd, NIOCREG, &req);
3768b8534bSLuigi Rizzo  *	mem = mmap(0, ... );
3868b8534bSLuigi Rizzo  *	nifp = NETMAP_IF(mem, req.nr_nifp);
3968b8534bSLuigi Rizzo  *		(so simple, we could just do it manually)
4068b8534bSLuigi Rizzo  *
4168b8534bSLuigi Rizzo  * From there:
4268b8534bSLuigi Rizzo  *	struct netmap_ring *NETMAP_TXRING(nifp, index)
4368b8534bSLuigi Rizzo  *	struct netmap_ring *NETMAP_RXRING(nifp, index)
440506889cSLuigi Rizzo  *		we can access ring->cur, ring->head, ring->tail, etc.
4568b8534bSLuigi Rizzo  *
4668b8534bSLuigi Rizzo  *	ring->slot[i] gives us the i-th slot (we can access
4717885a7bSLuigi Rizzo  *		directly len, flags, buf_idx)
4868b8534bSLuigi Rizzo  *
4927892e02SLuigi Rizzo  *	char *buf = NETMAP_BUF(ring, x) returns a pointer to
5027892e02SLuigi Rizzo  *		the buffer numbered x
5168b8534bSLuigi Rizzo  *
5217885a7bSLuigi Rizzo  * All ring indexes (head, cur, tail) should always move forward.
5317885a7bSLuigi Rizzo  * To compute the next index in a circular ring you can use
5417885a7bSLuigi Rizzo  *	i = nm_ring_next(ring, i);
55f9790aebSLuigi Rizzo  *
56f9790aebSLuigi Rizzo  * To ease porting apps from pcap to netmap we supply a few fuctions
5717885a7bSLuigi Rizzo  * that can be called to open, close, read and write on netmap in a way
5817885a7bSLuigi Rizzo  * similar to libpcap. Note that the read/write function depend on
5917885a7bSLuigi Rizzo  * an ioctl()/select()/poll() being issued to refill rings or push
6017885a7bSLuigi Rizzo  * packets out.
61f9790aebSLuigi Rizzo  *
62f9790aebSLuigi Rizzo  * In order to use these, include #define NETMAP_WITH_LIBS
63f9790aebSLuigi Rizzo  * in the source file that invokes these functions.
6468b8534bSLuigi Rizzo  */
6568b8534bSLuigi Rizzo 
6668b8534bSLuigi Rizzo #ifndef _NET_NETMAP_USER_H_
6768b8534bSLuigi Rizzo #define _NET_NETMAP_USER_H_
6868b8534bSLuigi Rizzo 
69*37e3a6d3SLuigi Rizzo #define NETMAP_DEVICE_NAME "/dev/netmap"
70*37e3a6d3SLuigi Rizzo 
71*37e3a6d3SLuigi Rizzo #ifdef __CYGWIN__
72*37e3a6d3SLuigi Rizzo /*
73*37e3a6d3SLuigi Rizzo  * we can compile userspace apps with either cygwin or msvc,
74*37e3a6d3SLuigi Rizzo  * and we use _WIN32 to identify windows specific code
75*37e3a6d3SLuigi Rizzo  */
76*37e3a6d3SLuigi Rizzo #ifndef _WIN32
77*37e3a6d3SLuigi Rizzo #define _WIN32
78*37e3a6d3SLuigi Rizzo #endif	/* _WIN32 */
79*37e3a6d3SLuigi Rizzo 
80*37e3a6d3SLuigi Rizzo #endif	/* __CYGWIN__ */
81*37e3a6d3SLuigi Rizzo 
82*37e3a6d3SLuigi Rizzo #ifdef _WIN32
83*37e3a6d3SLuigi Rizzo #undef NETMAP_DEVICE_NAME
84*37e3a6d3SLuigi Rizzo #define NETMAP_DEVICE_NAME "/proc/sys/DosDevices/Global/netmap"
85*37e3a6d3SLuigi Rizzo #include <windows.h>
86*37e3a6d3SLuigi Rizzo #include <WinDef.h>
87*37e3a6d3SLuigi Rizzo #include <sys/cygwin.h>
88*37e3a6d3SLuigi Rizzo #endif /* _WIN32 */
89*37e3a6d3SLuigi Rizzo 
90f9790aebSLuigi Rizzo #include <stdint.h>
91f0ea3689SLuigi Rizzo #include <sys/socket.h>		/* apple needs sockaddr */
92f9790aebSLuigi Rizzo #include <net/if.h>		/* IFNAMSIZ */
93*37e3a6d3SLuigi Rizzo #include <ctype.h>
9417885a7bSLuigi Rizzo 
9517885a7bSLuigi Rizzo #ifndef likely
9617885a7bSLuigi Rizzo #define likely(x)	__builtin_expect(!!(x), 1)
9717885a7bSLuigi Rizzo #define unlikely(x)	__builtin_expect(!!(x), 0)
9817885a7bSLuigi Rizzo #endif /* likely and unlikely */
9917885a7bSLuigi Rizzo 
100f9790aebSLuigi Rizzo #include <net/netmap.h>
101f9790aebSLuigi Rizzo 
10217885a7bSLuigi Rizzo /* helper macro */
103104d9fc7SEd Maste #define _NETMAP_OFFSET(type, ptr, offset) \
104104d9fc7SEd Maste 	((type)(void *)((char *)(ptr) + (offset)))
10568b8534bSLuigi Rizzo 
10617885a7bSLuigi Rizzo #define NETMAP_IF(_base, _ofs)	_NETMAP_OFFSET(struct netmap_if *, _base, _ofs)
10768b8534bSLuigi Rizzo 
108104d9fc7SEd Maste #define NETMAP_TXRING(nifp, index) _NETMAP_OFFSET(struct netmap_ring *, \
109104d9fc7SEd Maste 	nifp, (nifp)->ring_ofs[index] )
110104d9fc7SEd Maste 
111104d9fc7SEd Maste #define NETMAP_RXRING(nifp, index) _NETMAP_OFFSET(struct netmap_ring *,	\
112104d9fc7SEd Maste 	nifp, (nifp)->ring_ofs[index + (nifp)->ni_tx_rings + 1] )
11368b8534bSLuigi Rizzo 
11468b8534bSLuigi Rizzo #define NETMAP_BUF(ring, index)				\
1155819da83SLuigi Rizzo 	((char *)(ring) + (ring)->buf_ofs + ((index)*(ring)->nr_buf_size))
11668b8534bSLuigi Rizzo 
11764ae02c3SLuigi Rizzo #define NETMAP_BUF_IDX(ring, buf)			\
11864ae02c3SLuigi Rizzo 	( ((char *)(buf) - ((char *)(ring) + (ring)->buf_ofs) ) / \
119a7250582SLuigi Rizzo 		(ring)->nr_buf_size )
12064ae02c3SLuigi Rizzo 
12168b8534bSLuigi Rizzo 
12217885a7bSLuigi Rizzo static inline uint32_t
12317885a7bSLuigi Rizzo nm_ring_next(struct netmap_ring *r, uint32_t i)
12417885a7bSLuigi Rizzo {
12517885a7bSLuigi Rizzo 	return ( unlikely(i + 1 == r->num_slots) ? 0 : i + 1);
12617885a7bSLuigi Rizzo }
12717885a7bSLuigi Rizzo 
128a7250582SLuigi Rizzo 
12968b8534bSLuigi Rizzo /*
13017885a7bSLuigi Rizzo  * Return 1 if we have pending transmissions in the tx ring.
131f0ea3689SLuigi Rizzo  * When everything is complete ring->head = ring->tail + 1 (modulo ring size)
13268b8534bSLuigi Rizzo  */
13317885a7bSLuigi Rizzo static inline int
13417885a7bSLuigi Rizzo nm_tx_pending(struct netmap_ring *r)
13517885a7bSLuigi Rizzo {
136f0ea3689SLuigi Rizzo 	return nm_ring_next(r, r->tail) != r->head;
13717885a7bSLuigi Rizzo }
13817885a7bSLuigi Rizzo 
13917885a7bSLuigi Rizzo 
14017885a7bSLuigi Rizzo static inline uint32_t
14117885a7bSLuigi Rizzo nm_ring_space(struct netmap_ring *ring)
14217885a7bSLuigi Rizzo {
14317885a7bSLuigi Rizzo         int ret = ring->tail - ring->cur;
14417885a7bSLuigi Rizzo         if (ret < 0)
14517885a7bSLuigi Rizzo                 ret += ring->num_slots;
14617885a7bSLuigi Rizzo         return ret;
14717885a7bSLuigi Rizzo }
14817885a7bSLuigi Rizzo 
14968b8534bSLuigi Rizzo 
150f9790aebSLuigi Rizzo #ifdef NETMAP_WITH_LIBS
151f9790aebSLuigi Rizzo /*
152f9790aebSLuigi Rizzo  * Support for simple I/O libraries.
153f9790aebSLuigi Rizzo  * Include other system headers required for compiling this.
154f9790aebSLuigi Rizzo  */
155f9790aebSLuigi Rizzo 
156f9790aebSLuigi Rizzo #ifndef HAVE_NETMAP_WITH_LIBS
157f9790aebSLuigi Rizzo #define HAVE_NETMAP_WITH_LIBS
158f9790aebSLuigi Rizzo 
1599225c808SLuigi Rizzo #include <stdio.h>
160f9790aebSLuigi Rizzo #include <sys/time.h>
161f9790aebSLuigi Rizzo #include <sys/mman.h>
162f9790aebSLuigi Rizzo #include <string.h>	/* memset */
163f9790aebSLuigi Rizzo #include <sys/ioctl.h>
164f9790aebSLuigi Rizzo #include <sys/errno.h>	/* EINVAL */
165f9790aebSLuigi Rizzo #include <fcntl.h>	/* O_RDWR */
16617885a7bSLuigi Rizzo #include <unistd.h>	/* close() */
16766015019SLuigi Rizzo #include <signal.h>
16817885a7bSLuigi Rizzo #include <stdlib.h>
169f9790aebSLuigi Rizzo 
170f0ea3689SLuigi Rizzo #ifndef ND /* debug macros */
171f0ea3689SLuigi Rizzo /* debug support */
172f0ea3689SLuigi Rizzo #define ND(_fmt, ...) do {} while(0)
173f0ea3689SLuigi Rizzo #define D(_fmt, ...)						\
174f0ea3689SLuigi Rizzo 	do {							\
1754bf50f18SLuigi Rizzo 		struct timeval _t0;				\
1764bf50f18SLuigi Rizzo 		gettimeofday(&_t0, NULL);			\
177f0ea3689SLuigi Rizzo 		fprintf(stderr, "%03d.%06d %s [%d] " _fmt "\n",	\
1784bf50f18SLuigi Rizzo 		    (int)(_t0.tv_sec % 1000), (int)_t0.tv_usec,	\
179f0ea3689SLuigi Rizzo 		    __FUNCTION__, __LINE__, ##__VA_ARGS__);	\
180f0ea3689SLuigi Rizzo         } while (0)
181f0ea3689SLuigi Rizzo 
182f0ea3689SLuigi Rizzo /* Rate limited version of "D", lps indicates how many per second */
183f0ea3689SLuigi Rizzo #define RD(lps, format, ...)                                    \
184f0ea3689SLuigi Rizzo     do {                                                        \
1854bf50f18SLuigi Rizzo         static int __t0, __cnt;                                 \
186f0ea3689SLuigi Rizzo         struct timeval __xxts;                                  \
187f0ea3689SLuigi Rizzo         gettimeofday(&__xxts, NULL);                            \
1884bf50f18SLuigi Rizzo         if (__t0 != __xxts.tv_sec) {                            \
1894bf50f18SLuigi Rizzo             __t0 = __xxts.tv_sec;                               \
190f0ea3689SLuigi Rizzo             __cnt = 0;                                          \
191f0ea3689SLuigi Rizzo         }                                                       \
192f0ea3689SLuigi Rizzo         if (__cnt++ < lps) {                                    \
193f0ea3689SLuigi Rizzo             D(format, ##__VA_ARGS__);                           \
194f0ea3689SLuigi Rizzo         }                                                       \
195f0ea3689SLuigi Rizzo     } while (0)
196f0ea3689SLuigi Rizzo #endif
197f0ea3689SLuigi Rizzo 
198*37e3a6d3SLuigi Rizzo struct nm_pkthdr {	/* first part is the same as pcap_pkthdr */
199f9790aebSLuigi Rizzo 	struct timeval	ts;
200f9790aebSLuigi Rizzo 	uint32_t	caplen;
201f9790aebSLuigi Rizzo 	uint32_t	len;
202*37e3a6d3SLuigi Rizzo 
203*37e3a6d3SLuigi Rizzo 	uint64_t flags;	/* NM_MORE_PKTS etc */
204*37e3a6d3SLuigi Rizzo #define NM_MORE_PKTS	1
205*37e3a6d3SLuigi Rizzo 	struct nm_desc *d;
206*37e3a6d3SLuigi Rizzo 	struct netmap_slot *slot;
207*37e3a6d3SLuigi Rizzo 	uint8_t *buf;
208f9790aebSLuigi Rizzo };
209f9790aebSLuigi Rizzo 
210f0ea3689SLuigi Rizzo struct nm_stat {	/* same as pcap_stat	*/
21166015019SLuigi Rizzo 	u_int	ps_recv;
21266015019SLuigi Rizzo 	u_int	ps_drop;
21366015019SLuigi Rizzo 	u_int	ps_ifdrop;
214*37e3a6d3SLuigi Rizzo #ifdef WIN32 /* XXX or _WIN32 ? */
21566015019SLuigi Rizzo 	u_int	bs_capt;
21666015019SLuigi Rizzo #endif /* WIN32 */
21766015019SLuigi Rizzo };
21866015019SLuigi Rizzo 
21966015019SLuigi Rizzo #define NM_ERRBUF_SIZE	512
22066015019SLuigi Rizzo 
221f0ea3689SLuigi Rizzo struct nm_desc {
222f0ea3689SLuigi Rizzo 	struct nm_desc *self; /* point to self if netmap. */
223f9790aebSLuigi Rizzo 	int fd;
224f9790aebSLuigi Rizzo 	void *mem;
2259225c808SLuigi Rizzo 	uint32_t memsize;
226f0ea3689SLuigi Rizzo 	int done_mmap;	/* set if mem is the result of mmap */
227f0ea3689SLuigi Rizzo 	struct netmap_if * const nifp;
22866015019SLuigi Rizzo 	uint16_t first_tx_ring, last_tx_ring, cur_tx_ring;
22966015019SLuigi Rizzo 	uint16_t first_rx_ring, last_rx_ring, cur_rx_ring;
23066015019SLuigi Rizzo 	struct nmreq req;	/* also contains the nr_name = ifname */
231f0ea3689SLuigi Rizzo 	struct nm_pkthdr hdr;
23266015019SLuigi Rizzo 
233f0ea3689SLuigi Rizzo 	/*
234f0ea3689SLuigi Rizzo 	 * The memory contains netmap_if, rings and then buffers.
235f0ea3689SLuigi Rizzo 	 * Given a pointer (e.g. to nm_inject) we can compare with
236f0ea3689SLuigi Rizzo 	 * mem/buf_start/buf_end to tell if it is a buffer or
237f0ea3689SLuigi Rizzo 	 * some other descriptor in our region.
238f0ea3689SLuigi Rizzo 	 * We also store a pointer to some ring as it helps in the
239f0ea3689SLuigi Rizzo 	 * translation from buffer indexes to addresses.
240f0ea3689SLuigi Rizzo 	 */
241f0ea3689SLuigi Rizzo 	struct netmap_ring * const some_ring;
242f0ea3689SLuigi Rizzo 	void * const buf_start;
243f0ea3689SLuigi Rizzo 	void * const buf_end;
24466015019SLuigi Rizzo 	/* parameters from pcap_open_live */
24566015019SLuigi Rizzo 	int snaplen;
24666015019SLuigi Rizzo 	int promisc;
24766015019SLuigi Rizzo 	int to_ms;
24866015019SLuigi Rizzo 	char *errbuf;
24966015019SLuigi Rizzo 
25066015019SLuigi Rizzo 	/* save flags so we can restore them on close */
25166015019SLuigi Rizzo 	uint32_t if_flags;
25266015019SLuigi Rizzo         uint32_t if_reqcap;
25366015019SLuigi Rizzo         uint32_t if_curcap;
25466015019SLuigi Rizzo 
255f0ea3689SLuigi Rizzo 	struct nm_stat st;
25666015019SLuigi Rizzo 	char msg[NM_ERRBUF_SIZE];
257f9790aebSLuigi Rizzo };
258f9790aebSLuigi Rizzo 
259f9790aebSLuigi Rizzo /*
260f9790aebSLuigi Rizzo  * when the descriptor is open correctly, d->self == d
261b82b2211SLuigi Rizzo  * Eventually we should also use some magic number.
262f9790aebSLuigi Rizzo  */
263f0ea3689SLuigi Rizzo #define P2NMD(p)		((struct nm_desc *)(p))
264f0ea3689SLuigi Rizzo #define IS_NETMAP_DESC(d)	((d) && P2NMD(d)->self == P2NMD(d))
265f9790aebSLuigi Rizzo #define NETMAP_FD(d)		(P2NMD(d)->fd)
266f9790aebSLuigi Rizzo 
26717885a7bSLuigi Rizzo 
26817885a7bSLuigi Rizzo /*
26917885a7bSLuigi Rizzo  * this is a slightly optimized copy routine which rounds
27017885a7bSLuigi Rizzo  * to multiple of 64 bytes and is often faster than dealing
27117885a7bSLuigi Rizzo  * with other odd sizes. We assume there is enough room
27217885a7bSLuigi Rizzo  * in the source and destination buffers.
27317885a7bSLuigi Rizzo  *
27417885a7bSLuigi Rizzo  * XXX only for multiples of 64 bytes, non overlapped.
27517885a7bSLuigi Rizzo  */
27617885a7bSLuigi Rizzo static inline void
277f0ea3689SLuigi Rizzo nm_pkt_copy(const void *_src, void *_dst, int l)
27817885a7bSLuigi Rizzo {
279b82b2211SLuigi Rizzo 	const uint64_t *src = (const uint64_t *)_src;
280b82b2211SLuigi Rizzo 	uint64_t *dst = (uint64_t *)_dst;
281b82b2211SLuigi Rizzo 
28217885a7bSLuigi Rizzo 	if (unlikely(l >= 1024)) {
28317885a7bSLuigi Rizzo 		memcpy(dst, src, l);
28417885a7bSLuigi Rizzo 		return;
28517885a7bSLuigi Rizzo 	}
28617885a7bSLuigi Rizzo 	for (; likely(l > 0); l-=64) {
28717885a7bSLuigi Rizzo 		*dst++ = *src++;
28817885a7bSLuigi Rizzo 		*dst++ = *src++;
28917885a7bSLuigi Rizzo 		*dst++ = *src++;
29017885a7bSLuigi Rizzo 		*dst++ = *src++;
29117885a7bSLuigi Rizzo 		*dst++ = *src++;
29217885a7bSLuigi Rizzo 		*dst++ = *src++;
29317885a7bSLuigi Rizzo 		*dst++ = *src++;
29417885a7bSLuigi Rizzo 		*dst++ = *src++;
29517885a7bSLuigi Rizzo 	}
29617885a7bSLuigi Rizzo }
29717885a7bSLuigi Rizzo 
29817885a7bSLuigi Rizzo 
299f9790aebSLuigi Rizzo /*
300f9790aebSLuigi Rizzo  * The callback, invoked on each received packet. Same as libpcap
301f9790aebSLuigi Rizzo  */
302f0ea3689SLuigi Rizzo typedef void (*nm_cb_t)(u_char *, const struct nm_pkthdr *, const u_char *d);
303f9790aebSLuigi Rizzo 
304f9790aebSLuigi Rizzo /*
30517885a7bSLuigi Rizzo  *--- the pcap-like API ---
30617885a7bSLuigi Rizzo  *
30717885a7bSLuigi Rizzo  * nm_open() opens a file descriptor, binds to a port and maps memory.
30817885a7bSLuigi Rizzo  *
30917885a7bSLuigi Rizzo  * ifname	(netmap:foo or vale:foo) is the port name
310f0ea3689SLuigi Rizzo  *		a suffix can indicate the follwing:
311f0ea3689SLuigi Rizzo  *		^		bind the host (sw) ring pair
312f0ea3689SLuigi Rizzo  *		*		bind host and NIC ring pairs (transparent)
313f0ea3689SLuigi Rizzo  *		-NN		bind individual NIC ring pair
314f0ea3689SLuigi Rizzo  *		{NN		bind master side of pipe NN
315f0ea3689SLuigi Rizzo  *		}NN		bind slave side of pipe NN
316*37e3a6d3SLuigi Rizzo  *		a suffix starting with / and the following flags,
317847bf383SLuigi Rizzo  *		in any order:
318847bf383SLuigi Rizzo  *		x		exclusive access
319847bf383SLuigi Rizzo  *		z		zero copy monitor
320847bf383SLuigi Rizzo  *		t		monitor tx side
321847bf383SLuigi Rizzo  *		r		monitor rx side
322*37e3a6d3SLuigi Rizzo  *		R		bind only RX ring(s)
323*37e3a6d3SLuigi Rizzo  *		T		bind only TX ring(s)
324f0ea3689SLuigi Rizzo  *
325f0ea3689SLuigi Rizzo  * req		provides the initial values of nmreq before parsing ifname.
326f0ea3689SLuigi Rizzo  *		Remember that the ifname parsing will override the ring
327f0ea3689SLuigi Rizzo  *		number in nm_ringid, and part of nm_flags;
328f0ea3689SLuigi Rizzo  * flags	special functions, normally 0
329f0ea3689SLuigi Rizzo  *		indicates which fields of *arg are significant
330f0ea3689SLuigi Rizzo  * arg		special functions, normally NULL
331f0ea3689SLuigi Rizzo  *		if passed a netmap_desc with mem != NULL,
332f0ea3689SLuigi Rizzo  *		use that memory instead of mmap.
333f9790aebSLuigi Rizzo  */
33417885a7bSLuigi Rizzo 
335f0ea3689SLuigi Rizzo static struct nm_desc *nm_open(const char *ifname, const struct nmreq *req,
336f0ea3689SLuigi Rizzo 	uint64_t flags, const struct nm_desc *arg);
337f0ea3689SLuigi Rizzo 
338f0ea3689SLuigi Rizzo /*
339f0ea3689SLuigi Rizzo  * nm_open can import some fields from the parent descriptor.
340f0ea3689SLuigi Rizzo  * These flags control which ones.
341f0ea3689SLuigi Rizzo  * Also in flags you can specify NETMAP_NO_TX_POLL and NETMAP_DO_RX_POLL,
342f0ea3689SLuigi Rizzo  * which set the initial value for these flags.
343f0ea3689SLuigi Rizzo  * Note that the 16 low bits of the flags are reserved for data
344f0ea3689SLuigi Rizzo  * that may go into the nmreq.
345f0ea3689SLuigi Rizzo  */
346f0ea3689SLuigi Rizzo enum {
347f0ea3689SLuigi Rizzo 	NM_OPEN_NO_MMAP =	0x040000, /* reuse mmap from parent */
348f0ea3689SLuigi Rizzo 	NM_OPEN_IFNAME =	0x080000, /* nr_name, nr_ringid, nr_flags */
349f0ea3689SLuigi Rizzo 	NM_OPEN_ARG1 =		0x100000,
350f0ea3689SLuigi Rizzo 	NM_OPEN_ARG2 =		0x200000,
351f0ea3689SLuigi Rizzo 	NM_OPEN_ARG3 =		0x400000,
352f0ea3689SLuigi Rizzo 	NM_OPEN_RING_CFG =	0x800000, /* tx|rx rings|slots */
353f0ea3689SLuigi Rizzo };
354f0ea3689SLuigi Rizzo 
355f9790aebSLuigi Rizzo 
356f9790aebSLuigi Rizzo /*
35717885a7bSLuigi Rizzo  * nm_close()	closes and restores the port to its previous state
358f9790aebSLuigi Rizzo  */
35917885a7bSLuigi Rizzo 
360f0ea3689SLuigi Rizzo static int nm_close(struct nm_desc *);
361f9790aebSLuigi Rizzo 
362f9790aebSLuigi Rizzo /*
363*37e3a6d3SLuigi Rizzo  * nm_mmap()    do mmap or inherit from parent if the nr_arg2
364*37e3a6d3SLuigi Rizzo  *              (memory block) matches.
365*37e3a6d3SLuigi Rizzo  */
366*37e3a6d3SLuigi Rizzo 
367*37e3a6d3SLuigi Rizzo static int nm_mmap(struct nm_desc *, const struct nm_desc *);
368*37e3a6d3SLuigi Rizzo 
369*37e3a6d3SLuigi Rizzo /*
37017885a7bSLuigi Rizzo  * nm_inject() is the same as pcap_inject()
37117885a7bSLuigi Rizzo  * nm_dispatch() is the same as pcap_dispatch()
37217885a7bSLuigi Rizzo  * nm_nextpkt() is the same as pcap_next()
373f9790aebSLuigi Rizzo  */
37417885a7bSLuigi Rizzo 
375f0ea3689SLuigi Rizzo static int nm_inject(struct nm_desc *, const void *, size_t);
376f0ea3689SLuigi Rizzo static int nm_dispatch(struct nm_desc *, int, nm_cb_t, u_char *);
377f0ea3689SLuigi Rizzo static u_char *nm_nextpkt(struct nm_desc *, struct nm_pkthdr *);
378f9790aebSLuigi Rizzo 
379*37e3a6d3SLuigi Rizzo #ifdef _WIN32
380*37e3a6d3SLuigi Rizzo 
381*37e3a6d3SLuigi Rizzo intptr_t _get_osfhandle(int); /* defined in io.h in windows */
382*37e3a6d3SLuigi Rizzo 
383*37e3a6d3SLuigi Rizzo /*
384*37e3a6d3SLuigi Rizzo  * In windows we do not have yet native poll support, so we keep track
385*37e3a6d3SLuigi Rizzo  * of file descriptors associated to netmap ports to emulate poll on
386*37e3a6d3SLuigi Rizzo  * them and fall back on regular poll on other file descriptors.
387*37e3a6d3SLuigi Rizzo  */
388*37e3a6d3SLuigi Rizzo struct win_netmap_fd_list {
389*37e3a6d3SLuigi Rizzo 	struct win_netmap_fd_list *next;
390*37e3a6d3SLuigi Rizzo 	int win_netmap_fd;
391*37e3a6d3SLuigi Rizzo 	HANDLE win_netmap_handle;
392*37e3a6d3SLuigi Rizzo };
393*37e3a6d3SLuigi Rizzo 
394*37e3a6d3SLuigi Rizzo /*
395*37e3a6d3SLuigi Rizzo  * list head containing all the netmap opened fd and their
396*37e3a6d3SLuigi Rizzo  * windows HANDLE counterparts
397*37e3a6d3SLuigi Rizzo  */
398*37e3a6d3SLuigi Rizzo static struct win_netmap_fd_list *win_netmap_fd_list_head;
399*37e3a6d3SLuigi Rizzo 
400*37e3a6d3SLuigi Rizzo static void
401*37e3a6d3SLuigi Rizzo win_insert_fd_record(int fd)
402*37e3a6d3SLuigi Rizzo {
403*37e3a6d3SLuigi Rizzo 	struct win_netmap_fd_list *curr;
404*37e3a6d3SLuigi Rizzo 
405*37e3a6d3SLuigi Rizzo 	for (curr = win_netmap_fd_list_head; curr; curr = curr->next) {
406*37e3a6d3SLuigi Rizzo 		if (fd == curr->win_netmap_fd) {
407*37e3a6d3SLuigi Rizzo 			return;
408*37e3a6d3SLuigi Rizzo 		}
409*37e3a6d3SLuigi Rizzo 	}
410*37e3a6d3SLuigi Rizzo 	curr = calloc(1, sizeof(*curr));
411*37e3a6d3SLuigi Rizzo 	curr->next = win_netmap_fd_list_head;
412*37e3a6d3SLuigi Rizzo 	curr->win_netmap_fd = fd;
413*37e3a6d3SLuigi Rizzo 	curr->win_netmap_handle = IntToPtr(_get_osfhandle(fd));
414*37e3a6d3SLuigi Rizzo 	win_netmap_fd_list_head = curr;
415*37e3a6d3SLuigi Rizzo }
416*37e3a6d3SLuigi Rizzo 
417*37e3a6d3SLuigi Rizzo void
418*37e3a6d3SLuigi Rizzo win_remove_fd_record(int fd)
419*37e3a6d3SLuigi Rizzo {
420*37e3a6d3SLuigi Rizzo 	struct win_netmap_fd_list *curr = win_netmap_fd_list_head;
421*37e3a6d3SLuigi Rizzo 	struct win_netmap_fd_list *prev = NULL;
422*37e3a6d3SLuigi Rizzo 	for (; curr ; prev = curr, curr = curr->next) {
423*37e3a6d3SLuigi Rizzo 		if (fd != curr->win_netmap_fd)
424*37e3a6d3SLuigi Rizzo 			continue;
425*37e3a6d3SLuigi Rizzo 		/* found the entry */
426*37e3a6d3SLuigi Rizzo 		if (prev == NULL) { /* we are freeing the first entry */
427*37e3a6d3SLuigi Rizzo 			win_netmap_fd_list_head = curr->next;
428*37e3a6d3SLuigi Rizzo 		} else {
429*37e3a6d3SLuigi Rizzo 			prev->next = curr->next;
430*37e3a6d3SLuigi Rizzo 		}
431*37e3a6d3SLuigi Rizzo 		free(curr);
432*37e3a6d3SLuigi Rizzo 		break;
433*37e3a6d3SLuigi Rizzo 	}
434*37e3a6d3SLuigi Rizzo }
435*37e3a6d3SLuigi Rizzo 
436*37e3a6d3SLuigi Rizzo 
437*37e3a6d3SLuigi Rizzo HANDLE
438*37e3a6d3SLuigi Rizzo win_get_netmap_handle(int fd)
439*37e3a6d3SLuigi Rizzo {
440*37e3a6d3SLuigi Rizzo 	struct win_netmap_fd_list *curr;
441*37e3a6d3SLuigi Rizzo 
442*37e3a6d3SLuigi Rizzo 	for (curr = win_netmap_fd_list_head; curr; curr = curr->next) {
443*37e3a6d3SLuigi Rizzo 		if (fd == curr->win_netmap_fd) {
444*37e3a6d3SLuigi Rizzo 			return curr->win_netmap_handle;
445*37e3a6d3SLuigi Rizzo 		}
446*37e3a6d3SLuigi Rizzo 	}
447*37e3a6d3SLuigi Rizzo 	return NULL;
448*37e3a6d3SLuigi Rizzo }
449*37e3a6d3SLuigi Rizzo 
450*37e3a6d3SLuigi Rizzo /*
451*37e3a6d3SLuigi Rizzo  * we need to wrap ioctl and mmap, at least for the netmap file descriptors
452*37e3a6d3SLuigi Rizzo  */
453*37e3a6d3SLuigi Rizzo 
454*37e3a6d3SLuigi Rizzo /*
455*37e3a6d3SLuigi Rizzo  * use this function only from netmap_user.h internal functions
456*37e3a6d3SLuigi Rizzo  * same as ioctl, returns 0 on success and -1 on error
457*37e3a6d3SLuigi Rizzo  */
458*37e3a6d3SLuigi Rizzo static int
459*37e3a6d3SLuigi Rizzo win_nm_ioctl_internal(HANDLE h, int32_t ctlCode, void *arg)
460*37e3a6d3SLuigi Rizzo {
461*37e3a6d3SLuigi Rizzo 	DWORD bReturn = 0, szIn, szOut;
462*37e3a6d3SLuigi Rizzo 	BOOL ioctlReturnStatus;
463*37e3a6d3SLuigi Rizzo 	void *inParam = arg, *outParam = arg;
464*37e3a6d3SLuigi Rizzo 
465*37e3a6d3SLuigi Rizzo 	switch (ctlCode) {
466*37e3a6d3SLuigi Rizzo 	case NETMAP_POLL:
467*37e3a6d3SLuigi Rizzo 		szIn = sizeof(POLL_REQUEST_DATA);
468*37e3a6d3SLuigi Rizzo 		szOut = sizeof(POLL_REQUEST_DATA);
469*37e3a6d3SLuigi Rizzo 		break;
470*37e3a6d3SLuigi Rizzo 	case NETMAP_MMAP:
471*37e3a6d3SLuigi Rizzo 		szIn = 0;
472*37e3a6d3SLuigi Rizzo 		szOut = sizeof(void*);
473*37e3a6d3SLuigi Rizzo 		inParam = NULL; /* nothing on input */
474*37e3a6d3SLuigi Rizzo 		break;
475*37e3a6d3SLuigi Rizzo 	case NIOCTXSYNC:
476*37e3a6d3SLuigi Rizzo 	case NIOCRXSYNC:
477*37e3a6d3SLuigi Rizzo 		szIn = 0;
478*37e3a6d3SLuigi Rizzo 		szOut = 0;
479*37e3a6d3SLuigi Rizzo 		break;
480*37e3a6d3SLuigi Rizzo 	case NIOCREGIF:
481*37e3a6d3SLuigi Rizzo 		szIn = sizeof(struct nmreq);
482*37e3a6d3SLuigi Rizzo 		szOut = sizeof(struct nmreq);
483*37e3a6d3SLuigi Rizzo 		break;
484*37e3a6d3SLuigi Rizzo 	case NIOCCONFIG:
485*37e3a6d3SLuigi Rizzo 		D("unsupported NIOCCONFIG!");
486*37e3a6d3SLuigi Rizzo 		return -1;
487*37e3a6d3SLuigi Rizzo 
488*37e3a6d3SLuigi Rizzo 	default: /* a regular ioctl */
489*37e3a6d3SLuigi Rizzo 		D("invalid ioctl %x on netmap fd", ctlCode);
490*37e3a6d3SLuigi Rizzo 		return -1;
491*37e3a6d3SLuigi Rizzo 	}
492*37e3a6d3SLuigi Rizzo 
493*37e3a6d3SLuigi Rizzo 	ioctlReturnStatus = DeviceIoControl(h,
494*37e3a6d3SLuigi Rizzo 				ctlCode, inParam, szIn,
495*37e3a6d3SLuigi Rizzo 				outParam, szOut,
496*37e3a6d3SLuigi Rizzo 				&bReturn, NULL);
497*37e3a6d3SLuigi Rizzo 	// XXX note windows returns 0 on error or async call, 1 on success
498*37e3a6d3SLuigi Rizzo 	// we could call GetLastError() to figure out what happened
499*37e3a6d3SLuigi Rizzo 	return ioctlReturnStatus ? 0 : -1;
500*37e3a6d3SLuigi Rizzo }
501*37e3a6d3SLuigi Rizzo 
502*37e3a6d3SLuigi Rizzo /*
503*37e3a6d3SLuigi Rizzo  * this function is what must be called from user-space programs
504*37e3a6d3SLuigi Rizzo  * same as ioctl, returns 0 on success and -1 on error
505*37e3a6d3SLuigi Rizzo  */
506*37e3a6d3SLuigi Rizzo static int
507*37e3a6d3SLuigi Rizzo win_nm_ioctl(int fd, int32_t ctlCode, void *arg)
508*37e3a6d3SLuigi Rizzo {
509*37e3a6d3SLuigi Rizzo 	HANDLE h = win_get_netmap_handle(fd);
510*37e3a6d3SLuigi Rizzo 
511*37e3a6d3SLuigi Rizzo 	if (h == NULL) {
512*37e3a6d3SLuigi Rizzo 		return ioctl(fd, ctlCode, arg);
513*37e3a6d3SLuigi Rizzo 	} else {
514*37e3a6d3SLuigi Rizzo 		return win_nm_ioctl_internal(h, ctlCode, arg);
515*37e3a6d3SLuigi Rizzo 	}
516*37e3a6d3SLuigi Rizzo }
517*37e3a6d3SLuigi Rizzo 
518*37e3a6d3SLuigi Rizzo #define ioctl win_nm_ioctl /* from now on, within this file ... */
519*37e3a6d3SLuigi Rizzo 
520*37e3a6d3SLuigi Rizzo /*
521*37e3a6d3SLuigi Rizzo  * We cannot use the native mmap on windows
522*37e3a6d3SLuigi Rizzo  * The only parameter used is "fd", the other ones are just declared to
523*37e3a6d3SLuigi Rizzo  * make this signature comparable to the FreeBSD/Linux one
524*37e3a6d3SLuigi Rizzo  */
525*37e3a6d3SLuigi Rizzo static void *
526*37e3a6d3SLuigi Rizzo win32_mmap_emulated(void *addr, size_t length, int prot, int flags, int fd, int32_t offset)
527*37e3a6d3SLuigi Rizzo {
528*37e3a6d3SLuigi Rizzo 	HANDLE h = win_get_netmap_handle(fd);
529*37e3a6d3SLuigi Rizzo 
530*37e3a6d3SLuigi Rizzo 	if (h == NULL) {
531*37e3a6d3SLuigi Rizzo 		return mmap(addr, length, prot, flags, fd, offset);
532*37e3a6d3SLuigi Rizzo 	} else {
533*37e3a6d3SLuigi Rizzo 		MEMORY_ENTRY ret;
534*37e3a6d3SLuigi Rizzo 
535*37e3a6d3SLuigi Rizzo 		return win_nm_ioctl_internal(h, NETMAP_MMAP, &ret) ?
536*37e3a6d3SLuigi Rizzo 			NULL : ret.pUsermodeVirtualAddress;
537*37e3a6d3SLuigi Rizzo 	}
538*37e3a6d3SLuigi Rizzo }
539*37e3a6d3SLuigi Rizzo 
540*37e3a6d3SLuigi Rizzo #define mmap win32_mmap_emulated
541*37e3a6d3SLuigi Rizzo 
542*37e3a6d3SLuigi Rizzo #include <sys/poll.h> /* XXX needed to use the structure pollfd */
543*37e3a6d3SLuigi Rizzo 
544*37e3a6d3SLuigi Rizzo static int
545*37e3a6d3SLuigi Rizzo win_nm_poll(struct pollfd *fds, int nfds, int timeout)
546*37e3a6d3SLuigi Rizzo {
547*37e3a6d3SLuigi Rizzo 	HANDLE h;
548*37e3a6d3SLuigi Rizzo 
549*37e3a6d3SLuigi Rizzo 	if (nfds != 1 || fds == NULL || (h = win_get_netmap_handle(fds->fd)) == NULL) {;
550*37e3a6d3SLuigi Rizzo 		return poll(fds, nfds, timeout);
551*37e3a6d3SLuigi Rizzo 	} else {
552*37e3a6d3SLuigi Rizzo 		POLL_REQUEST_DATA prd;
553*37e3a6d3SLuigi Rizzo 
554*37e3a6d3SLuigi Rizzo 		prd.timeout = timeout;
555*37e3a6d3SLuigi Rizzo 		prd.events = fds->events;
556*37e3a6d3SLuigi Rizzo 
557*37e3a6d3SLuigi Rizzo 		win_nm_ioctl_internal(h, NETMAP_POLL, &prd);
558*37e3a6d3SLuigi Rizzo 		if ((prd.revents == POLLERR) || (prd.revents == STATUS_TIMEOUT)) {
559*37e3a6d3SLuigi Rizzo 			return -1;
560*37e3a6d3SLuigi Rizzo 		}
561*37e3a6d3SLuigi Rizzo 		return 1;
562*37e3a6d3SLuigi Rizzo 	}
563*37e3a6d3SLuigi Rizzo }
564*37e3a6d3SLuigi Rizzo 
565*37e3a6d3SLuigi Rizzo #define poll win_nm_poll
566*37e3a6d3SLuigi Rizzo 
567*37e3a6d3SLuigi Rizzo static int
568*37e3a6d3SLuigi Rizzo win_nm_open(char* pathname, int flags)
569*37e3a6d3SLuigi Rizzo {
570*37e3a6d3SLuigi Rizzo 
571*37e3a6d3SLuigi Rizzo 	if (strcmp(pathname, NETMAP_DEVICE_NAME) == 0) {
572*37e3a6d3SLuigi Rizzo 		int fd = open(NETMAP_DEVICE_NAME, O_RDWR);
573*37e3a6d3SLuigi Rizzo 		if (fd < 0) {
574*37e3a6d3SLuigi Rizzo 			return -1;
575*37e3a6d3SLuigi Rizzo 		}
576*37e3a6d3SLuigi Rizzo 
577*37e3a6d3SLuigi Rizzo 		win_insert_fd_record(fd);
578*37e3a6d3SLuigi Rizzo 		return fd;
579*37e3a6d3SLuigi Rizzo 	} else {
580*37e3a6d3SLuigi Rizzo 		return open(pathname, flags);
581*37e3a6d3SLuigi Rizzo 	}
582*37e3a6d3SLuigi Rizzo }
583*37e3a6d3SLuigi Rizzo 
584*37e3a6d3SLuigi Rizzo #define open win_nm_open
585*37e3a6d3SLuigi Rizzo 
586*37e3a6d3SLuigi Rizzo static int
587*37e3a6d3SLuigi Rizzo win_nm_close(int fd)
588*37e3a6d3SLuigi Rizzo {
589*37e3a6d3SLuigi Rizzo 	if (fd != -1) {
590*37e3a6d3SLuigi Rizzo 		close(fd);
591*37e3a6d3SLuigi Rizzo 		if (win_get_netmap_handle(fd) != NULL) {
592*37e3a6d3SLuigi Rizzo 			win_remove_fd_record(fd);
593*37e3a6d3SLuigi Rizzo 		}
594*37e3a6d3SLuigi Rizzo 	}
595*37e3a6d3SLuigi Rizzo 	return 0;
596*37e3a6d3SLuigi Rizzo }
597*37e3a6d3SLuigi Rizzo 
598*37e3a6d3SLuigi Rizzo #define close win_nm_close
599*37e3a6d3SLuigi Rizzo 
600*37e3a6d3SLuigi Rizzo #endif /* _WIN32 */
601*37e3a6d3SLuigi Rizzo 
602*37e3a6d3SLuigi Rizzo static int
603*37e3a6d3SLuigi Rizzo nm_is_identifier(const char *s, const char *e)
604*37e3a6d3SLuigi Rizzo {
605*37e3a6d3SLuigi Rizzo 	for (; s != e; s++) {
606*37e3a6d3SLuigi Rizzo 		if (!isalnum(*s) && *s != '_') {
607*37e3a6d3SLuigi Rizzo 			return 0;
608*37e3a6d3SLuigi Rizzo 		}
609*37e3a6d3SLuigi Rizzo 	}
610*37e3a6d3SLuigi Rizzo 
611*37e3a6d3SLuigi Rizzo 	return 1;
612*37e3a6d3SLuigi Rizzo }
613f9790aebSLuigi Rizzo 
614f9790aebSLuigi Rizzo /*
615f9790aebSLuigi Rizzo  * Try to open, return descriptor if successful, NULL otherwise.
616f9790aebSLuigi Rizzo  * An invalid netmap name will return errno = 0;
617f0ea3689SLuigi Rizzo  * You can pass a pointer to a pre-filled nm_desc to add special
618f0ea3689SLuigi Rizzo  * parameters. Flags is used as follows
619*37e3a6d3SLuigi Rizzo  * NM_OPEN_NO_MMAP	use the memory from arg, only XXX avoid mmap
620f0ea3689SLuigi Rizzo  *			if the nr_arg2 (memory block) matches.
621f0ea3689SLuigi Rizzo  * NM_OPEN_ARG1		use req.nr_arg1 from arg
622f0ea3689SLuigi Rizzo  * NM_OPEN_ARG2		use req.nr_arg2 from arg
623f0ea3689SLuigi Rizzo  * NM_OPEN_RING_CFG	user ring config from arg
624f9790aebSLuigi Rizzo  */
625f0ea3689SLuigi Rizzo static struct nm_desc *
626f0ea3689SLuigi Rizzo nm_open(const char *ifname, const struct nmreq *req,
627f0ea3689SLuigi Rizzo 	uint64_t new_flags, const struct nm_desc *arg)
628f9790aebSLuigi Rizzo {
629f0ea3689SLuigi Rizzo 	struct nm_desc *d = NULL;
630f0ea3689SLuigi Rizzo 	const struct nm_desc *parent = arg;
631f0ea3689SLuigi Rizzo 	u_int namelen;
632847bf383SLuigi Rizzo 	uint32_t nr_ringid = 0, nr_flags, nr_reg;
633f0ea3689SLuigi Rizzo 	const char *port = NULL;
634*37e3a6d3SLuigi Rizzo 	const char *vpname = NULL;
635847bf383SLuigi Rizzo #define MAXERRMSG 80
636847bf383SLuigi Rizzo 	char errmsg[MAXERRMSG] = "";
637847bf383SLuigi Rizzo 	enum { P_START, P_RNGSFXOK, P_GETNUM, P_FLAGS, P_FLAGSOK } p_state;
638*37e3a6d3SLuigi Rizzo 	int is_vale;
639847bf383SLuigi Rizzo 	long num;
640f9790aebSLuigi Rizzo 
641*37e3a6d3SLuigi Rizzo 	if (strncmp(ifname, "netmap:", 7) &&
642*37e3a6d3SLuigi Rizzo 			strncmp(ifname, NM_BDG_NAME, strlen(NM_BDG_NAME))) {
643f0ea3689SLuigi Rizzo 		errno = 0; /* name not recognised, not an error */
644f9790aebSLuigi Rizzo 		return NULL;
645f9790aebSLuigi Rizzo 	}
646*37e3a6d3SLuigi Rizzo 
647*37e3a6d3SLuigi Rizzo 	is_vale = (ifname[0] == 'v');
648*37e3a6d3SLuigi Rizzo 	if (is_vale) {
649*37e3a6d3SLuigi Rizzo 		port = index(ifname, ':');
650*37e3a6d3SLuigi Rizzo 		if (port == NULL) {
651*37e3a6d3SLuigi Rizzo 			snprintf(errmsg, MAXERRMSG,
652*37e3a6d3SLuigi Rizzo 				 "missing ':' in vale name");
653*37e3a6d3SLuigi Rizzo 			goto fail;
654*37e3a6d3SLuigi Rizzo 		}
655*37e3a6d3SLuigi Rizzo 
656*37e3a6d3SLuigi Rizzo 		if (!nm_is_identifier(ifname + 4, port)) {
657*37e3a6d3SLuigi Rizzo 			snprintf(errmsg, MAXERRMSG, "invalid bridge name");
658*37e3a6d3SLuigi Rizzo 			goto fail;
659*37e3a6d3SLuigi Rizzo 		}
660*37e3a6d3SLuigi Rizzo 
661*37e3a6d3SLuigi Rizzo 		vpname = ++port;
662*37e3a6d3SLuigi Rizzo 	} else {
663f9790aebSLuigi Rizzo 		ifname += 7;
664*37e3a6d3SLuigi Rizzo 		port = ifname;
665*37e3a6d3SLuigi Rizzo 	}
666*37e3a6d3SLuigi Rizzo 
667f0ea3689SLuigi Rizzo 	/* scan for a separator */
668*37e3a6d3SLuigi Rizzo 	for (; *port && !index("-*^{}/", *port); port++)
669f0ea3689SLuigi Rizzo 		;
670*37e3a6d3SLuigi Rizzo 
671*37e3a6d3SLuigi Rizzo 	if (is_vale && !nm_is_identifier(vpname, port)) {
672*37e3a6d3SLuigi Rizzo 		snprintf(errmsg, MAXERRMSG, "invalid bridge port name");
673*37e3a6d3SLuigi Rizzo 		goto fail;
674*37e3a6d3SLuigi Rizzo 	}
675*37e3a6d3SLuigi Rizzo 
67666015019SLuigi Rizzo 	namelen = port - ifname;
677f0ea3689SLuigi Rizzo 	if (namelen >= sizeof(d->req.nr_name)) {
678847bf383SLuigi Rizzo 		snprintf(errmsg, MAXERRMSG, "name too long");
679f0ea3689SLuigi Rizzo 		goto fail;
68066015019SLuigi Rizzo 	}
681847bf383SLuigi Rizzo 	p_state = P_START;
682847bf383SLuigi Rizzo 	nr_flags = NR_REG_ALL_NIC; /* default for no suffix */
683847bf383SLuigi Rizzo 	while (*port) {
684847bf383SLuigi Rizzo 		switch (p_state) {
685847bf383SLuigi Rizzo 		case P_START:
686f0ea3689SLuigi Rizzo 			switch (*port) {
687847bf383SLuigi Rizzo 			case '^': /* only SW ring */
688f0ea3689SLuigi Rizzo 				nr_flags = NR_REG_SW;
689847bf383SLuigi Rizzo 				p_state = P_RNGSFXOK;
690f0ea3689SLuigi Rizzo 				break;
691847bf383SLuigi Rizzo 			case '*': /* NIC and SW */
692847bf383SLuigi Rizzo 				nr_flags = NR_REG_NIC_SW;
693847bf383SLuigi Rizzo 				p_state = P_RNGSFXOK;
694847bf383SLuigi Rizzo 				break;
695847bf383SLuigi Rizzo 			case '-': /* one NIC ring pair */
696847bf383SLuigi Rizzo 				nr_flags = NR_REG_ONE_NIC;
697847bf383SLuigi Rizzo 				p_state = P_GETNUM;
698847bf383SLuigi Rizzo 				break;
699847bf383SLuigi Rizzo 			case '{': /* pipe (master endpoint) */
700f0ea3689SLuigi Rizzo 				nr_flags = NR_REG_PIPE_MASTER;
701847bf383SLuigi Rizzo 				p_state = P_GETNUM;
702f0ea3689SLuigi Rizzo 				break;
703847bf383SLuigi Rizzo 			case '}': /* pipe (slave endoint) */
704f0ea3689SLuigi Rizzo 				nr_flags = NR_REG_PIPE_SLAVE;
705847bf383SLuigi Rizzo 				p_state = P_GETNUM;
706f0ea3689SLuigi Rizzo 				break;
707847bf383SLuigi Rizzo 			case '/': /* start of flags */
708847bf383SLuigi Rizzo 				p_state = P_FLAGS;
709847bf383SLuigi Rizzo 				break;
710847bf383SLuigi Rizzo 			default:
711847bf383SLuigi Rizzo 				snprintf(errmsg, MAXERRMSG, "unknown modifier: '%c'", *port);
712f0ea3689SLuigi Rizzo 				goto fail;
713f0ea3689SLuigi Rizzo 			}
714847bf383SLuigi Rizzo 			port++;
715847bf383SLuigi Rizzo 			break;
716847bf383SLuigi Rizzo 		case P_RNGSFXOK:
717847bf383SLuigi Rizzo 			switch (*port) {
718847bf383SLuigi Rizzo 			case '/':
719847bf383SLuigi Rizzo 				p_state = P_FLAGS;
720847bf383SLuigi Rizzo 				break;
721847bf383SLuigi Rizzo 			default:
722847bf383SLuigi Rizzo 				snprintf(errmsg, MAXERRMSG, "unexpected character: '%c'", *port);
723847bf383SLuigi Rizzo 				goto fail;
724847bf383SLuigi Rizzo 			}
725847bf383SLuigi Rizzo 			port++;
726847bf383SLuigi Rizzo 			break;
727847bf383SLuigi Rizzo 		case P_GETNUM:
728847bf383SLuigi Rizzo 			num = strtol(port, (char **)&port, 10);
729847bf383SLuigi Rizzo 			if (num < 0 || num >= NETMAP_RING_MASK) {
730847bf383SLuigi Rizzo 				snprintf(errmsg, MAXERRMSG, "'%ld' out of range [0, %d)",
731847bf383SLuigi Rizzo 						num, NETMAP_RING_MASK);
732847bf383SLuigi Rizzo 				goto fail;
733847bf383SLuigi Rizzo 			}
734847bf383SLuigi Rizzo 			nr_ringid = num & NETMAP_RING_MASK;
735847bf383SLuigi Rizzo 			p_state = P_RNGSFXOK;
736847bf383SLuigi Rizzo 			break;
737847bf383SLuigi Rizzo 		case P_FLAGS:
738847bf383SLuigi Rizzo 		case P_FLAGSOK:
739847bf383SLuigi Rizzo 			switch (*port) {
740847bf383SLuigi Rizzo 			case 'x':
741847bf383SLuigi Rizzo 				nr_flags |= NR_EXCLUSIVE;
742847bf383SLuigi Rizzo 				break;
743847bf383SLuigi Rizzo 			case 'z':
744847bf383SLuigi Rizzo 				nr_flags |= NR_ZCOPY_MON;
745847bf383SLuigi Rizzo 				break;
746847bf383SLuigi Rizzo 			case 't':
747847bf383SLuigi Rizzo 				nr_flags |= NR_MONITOR_TX;
748847bf383SLuigi Rizzo 				break;
749847bf383SLuigi Rizzo 			case 'r':
750847bf383SLuigi Rizzo 				nr_flags |= NR_MONITOR_RX;
751847bf383SLuigi Rizzo 				break;
752*37e3a6d3SLuigi Rizzo 			case 'R':
753*37e3a6d3SLuigi Rizzo 				nr_flags |= NR_RX_RINGS_ONLY;
754*37e3a6d3SLuigi Rizzo 				break;
755*37e3a6d3SLuigi Rizzo 			case 'T':
756*37e3a6d3SLuigi Rizzo 				nr_flags |= NR_TX_RINGS_ONLY;
757*37e3a6d3SLuigi Rizzo 				break;
758847bf383SLuigi Rizzo 			default:
759847bf383SLuigi Rizzo 				snprintf(errmsg, MAXERRMSG, "unrecognized flag: '%c'", *port);
760847bf383SLuigi Rizzo 				goto fail;
761847bf383SLuigi Rizzo 			}
762847bf383SLuigi Rizzo 			port++;
763847bf383SLuigi Rizzo 			p_state = P_FLAGSOK;
764847bf383SLuigi Rizzo 			break;
765847bf383SLuigi Rizzo 		}
766847bf383SLuigi Rizzo 	}
767847bf383SLuigi Rizzo 	if (p_state != P_START && p_state != P_RNGSFXOK && p_state != P_FLAGSOK) {
768847bf383SLuigi Rizzo 		snprintf(errmsg, MAXERRMSG, "unexpected end of port name");
769847bf383SLuigi Rizzo 		goto fail;
770847bf383SLuigi Rizzo 	}
771*37e3a6d3SLuigi Rizzo 	if ((nr_flags & NR_ZCOPY_MON) &&
772*37e3a6d3SLuigi Rizzo 	   !(nr_flags & (NR_MONITOR_TX|NR_MONITOR_RX))) {
773*37e3a6d3SLuigi Rizzo 		snprintf(errmsg, MAXERRMSG, "'z' used but neither 'r', nor 't' found");
774*37e3a6d3SLuigi Rizzo 		goto fail;
775*37e3a6d3SLuigi Rizzo 	}
776847bf383SLuigi Rizzo 	ND("flags: %s %s %s %s",
777847bf383SLuigi Rizzo 			(nr_flags & NR_EXCLUSIVE) ? "EXCLUSIVE" : "",
778847bf383SLuigi Rizzo 			(nr_flags & NR_ZCOPY_MON) ? "ZCOPY_MON" : "",
779847bf383SLuigi Rizzo 			(nr_flags & NR_MONITOR_TX) ? "MONITOR_TX" : "",
780847bf383SLuigi Rizzo 			(nr_flags & NR_MONITOR_RX) ? "MONITOR_RX" : "");
781f0ea3689SLuigi Rizzo 	d = (struct nm_desc *)calloc(1, sizeof(*d));
782f9790aebSLuigi Rizzo 	if (d == NULL) {
783847bf383SLuigi Rizzo 		snprintf(errmsg, MAXERRMSG, "nm_desc alloc failure");
784f9790aebSLuigi Rizzo 		errno = ENOMEM;
785f9790aebSLuigi Rizzo 		return NULL;
786f9790aebSLuigi Rizzo 	}
787f9790aebSLuigi Rizzo 	d->self = d;	/* set this early so nm_close() works */
788*37e3a6d3SLuigi Rizzo 	d->fd = open(NETMAP_DEVICE_NAME, O_RDWR);
789f0ea3689SLuigi Rizzo 	if (d->fd < 0) {
790847bf383SLuigi Rizzo 		snprintf(errmsg, MAXERRMSG, "cannot open /dev/netmap: %s", strerror(errno));
791f9790aebSLuigi Rizzo 		goto fail;
792f9790aebSLuigi Rizzo 	}
793f0ea3689SLuigi Rizzo 
794f0ea3689SLuigi Rizzo 	if (req)
795f0ea3689SLuigi Rizzo 		d->req = *req;
796f9790aebSLuigi Rizzo 	d->req.nr_version = NETMAP_API;
797f0ea3689SLuigi Rizzo 	d->req.nr_ringid &= ~NETMAP_RING_MASK;
798f0ea3689SLuigi Rizzo 
799f0ea3689SLuigi Rizzo 	/* these fields are overridden by ifname and flags processing */
800f0ea3689SLuigi Rizzo 	d->req.nr_ringid |= nr_ringid;
801*37e3a6d3SLuigi Rizzo 	d->req.nr_flags |= nr_flags;
80266015019SLuigi Rizzo 	memcpy(d->req.nr_name, ifname, namelen);
80366015019SLuigi Rizzo 	d->req.nr_name[namelen] = '\0';
804f0ea3689SLuigi Rizzo 	/* optionally import info from parent */
805f0ea3689SLuigi Rizzo 	if (IS_NETMAP_DESC(parent) && new_flags) {
806f0ea3689SLuigi Rizzo 		if (new_flags & NM_OPEN_ARG1)
807f0ea3689SLuigi Rizzo 			D("overriding ARG1 %d", parent->req.nr_arg1);
808f0ea3689SLuigi Rizzo 		d->req.nr_arg1 = new_flags & NM_OPEN_ARG1 ?
809f0ea3689SLuigi Rizzo 			parent->req.nr_arg1 : 4;
810f0ea3689SLuigi Rizzo 		if (new_flags & NM_OPEN_ARG2)
811f0ea3689SLuigi Rizzo 			D("overriding ARG2 %d", parent->req.nr_arg2);
812f0ea3689SLuigi Rizzo 		d->req.nr_arg2 = new_flags & NM_OPEN_ARG2 ?
813f0ea3689SLuigi Rizzo 			parent->req.nr_arg2 : 0;
814f0ea3689SLuigi Rizzo 		if (new_flags & NM_OPEN_ARG3)
815f0ea3689SLuigi Rizzo 			D("overriding ARG3 %d", parent->req.nr_arg3);
816f0ea3689SLuigi Rizzo 		d->req.nr_arg3 = new_flags & NM_OPEN_ARG3 ?
817f0ea3689SLuigi Rizzo 			parent->req.nr_arg3 : 0;
818f0ea3689SLuigi Rizzo 		if (new_flags & NM_OPEN_RING_CFG) {
819f0ea3689SLuigi Rizzo 			D("overriding RING_CFG");
820f0ea3689SLuigi Rizzo 			d->req.nr_tx_slots = parent->req.nr_tx_slots;
821f0ea3689SLuigi Rizzo 			d->req.nr_rx_slots = parent->req.nr_rx_slots;
822f0ea3689SLuigi Rizzo 			d->req.nr_tx_rings = parent->req.nr_tx_rings;
823f0ea3689SLuigi Rizzo 			d->req.nr_rx_rings = parent->req.nr_rx_rings;
824f0ea3689SLuigi Rizzo 		}
825f0ea3689SLuigi Rizzo 		if (new_flags & NM_OPEN_IFNAME) {
826f0ea3689SLuigi Rizzo 			D("overriding ifname %s ringid 0x%x flags 0x%x",
827f0ea3689SLuigi Rizzo 				parent->req.nr_name, parent->req.nr_ringid,
828f0ea3689SLuigi Rizzo 				parent->req.nr_flags);
829f0ea3689SLuigi Rizzo 			memcpy(d->req.nr_name, parent->req.nr_name,
830f0ea3689SLuigi Rizzo 				sizeof(d->req.nr_name));
831f0ea3689SLuigi Rizzo 			d->req.nr_ringid = parent->req.nr_ringid;
832f0ea3689SLuigi Rizzo 			d->req.nr_flags = parent->req.nr_flags;
833f0ea3689SLuigi Rizzo 		}
834f0ea3689SLuigi Rizzo 	}
8359225c808SLuigi Rizzo 	/* add the *XPOLL flags */
8369225c808SLuigi Rizzo 	d->req.nr_ringid |= new_flags & (NETMAP_NO_TX_POLL | NETMAP_DO_RX_POLL);
8379225c808SLuigi Rizzo 
83866015019SLuigi Rizzo 	if (ioctl(d->fd, NIOCREGIF, &d->req)) {
839847bf383SLuigi Rizzo 		snprintf(errmsg, MAXERRMSG, "NIOCREGIF failed: %s", strerror(errno));
840f9790aebSLuigi Rizzo 		goto fail;
84166015019SLuigi Rizzo 	}
842f9790aebSLuigi Rizzo 
843*37e3a6d3SLuigi Rizzo         /* if parent is defined, do nm_mmap() even if NM_OPEN_NO_MMAP is set */
844*37e3a6d3SLuigi Rizzo 	if ((!(new_flags & NM_OPEN_NO_MMAP) || parent) && nm_mmap(d, parent)) {
845847bf383SLuigi Rizzo 	        snprintf(errmsg, MAXERRMSG, "mmap failed: %s", strerror(errno));
846f9790aebSLuigi Rizzo 		goto fail;
847f0ea3689SLuigi Rizzo 	}
848f0ea3689SLuigi Rizzo 
849847bf383SLuigi Rizzo 	nr_reg = d->req.nr_flags & NR_REG_MASK;
850847bf383SLuigi Rizzo 
851847bf383SLuigi Rizzo 	if (nr_reg ==  NR_REG_SW) { /* host stack */
85266015019SLuigi Rizzo 		d->first_tx_ring = d->last_tx_ring = d->req.nr_tx_rings;
85366015019SLuigi Rizzo 		d->first_rx_ring = d->last_rx_ring = d->req.nr_rx_rings;
854847bf383SLuigi Rizzo 	} else if (nr_reg ==  NR_REG_ALL_NIC) { /* only nic */
855f0ea3689SLuigi Rizzo 		d->first_tx_ring = 0;
856f0ea3689SLuigi Rizzo 		d->first_rx_ring = 0;
85766015019SLuigi Rizzo 		d->last_tx_ring = d->req.nr_tx_rings - 1;
85866015019SLuigi Rizzo 		d->last_rx_ring = d->req.nr_rx_rings - 1;
859847bf383SLuigi Rizzo 	} else if (nr_reg ==  NR_REG_NIC_SW) {
860f0ea3689SLuigi Rizzo 		d->first_tx_ring = 0;
861f0ea3689SLuigi Rizzo 		d->first_rx_ring = 0;
862f0ea3689SLuigi Rizzo 		d->last_tx_ring = d->req.nr_tx_rings;
863f0ea3689SLuigi Rizzo 		d->last_rx_ring = d->req.nr_rx_rings;
864847bf383SLuigi Rizzo 	} else if (nr_reg == NR_REG_ONE_NIC) {
865f0ea3689SLuigi Rizzo 		/* XXX check validity */
866f0ea3689SLuigi Rizzo 		d->first_tx_ring = d->last_tx_ring =
8674bf50f18SLuigi Rizzo 		d->first_rx_ring = d->last_rx_ring = d->req.nr_ringid & NETMAP_RING_MASK;
868f0ea3689SLuigi Rizzo 	} else { /* pipes */
869f0ea3689SLuigi Rizzo 		d->first_tx_ring = d->last_tx_ring = 0;
870f0ea3689SLuigi Rizzo 		d->first_rx_ring = d->last_rx_ring = 0;
871f9790aebSLuigi Rizzo 	}
872f0ea3689SLuigi Rizzo 
873f0ea3689SLuigi Rizzo #ifdef DEBUG_NETMAP_USER
874f0ea3689SLuigi Rizzo     { /* debugging code */
875f0ea3689SLuigi Rizzo 	int i;
876f0ea3689SLuigi Rizzo 
877f0ea3689SLuigi Rizzo 	D("%s tx %d .. %d %d rx %d .. %d %d", ifname,
878f0ea3689SLuigi Rizzo 		d->first_tx_ring, d->last_tx_ring, d->req.nr_tx_rings,
879f0ea3689SLuigi Rizzo                 d->first_rx_ring, d->last_rx_ring, d->req.nr_rx_rings);
880f0ea3689SLuigi Rizzo 	for (i = 0; i <= d->req.nr_tx_rings; i++) {
881f0ea3689SLuigi Rizzo 		struct netmap_ring *r = NETMAP_TXRING(d->nifp, i);
882f0ea3689SLuigi Rizzo 		D("TX%d %p h %d c %d t %d", i, r, r->head, r->cur, r->tail);
883f0ea3689SLuigi Rizzo 	}
884f0ea3689SLuigi Rizzo 	for (i = 0; i <= d->req.nr_rx_rings; i++) {
885f0ea3689SLuigi Rizzo 		struct netmap_ring *r = NETMAP_RXRING(d->nifp, i);
886f0ea3689SLuigi Rizzo 		D("RX%d %p h %d c %d t %d", i, r, r->head, r->cur, r->tail);
887f0ea3689SLuigi Rizzo 	}
888f0ea3689SLuigi Rizzo     }
889f0ea3689SLuigi Rizzo #endif /* debugging */
890f0ea3689SLuigi Rizzo 
89166015019SLuigi Rizzo 	d->cur_tx_ring = d->first_tx_ring;
89266015019SLuigi Rizzo 	d->cur_rx_ring = d->first_rx_ring;
893f9790aebSLuigi Rizzo 	return d;
894f9790aebSLuigi Rizzo 
895f9790aebSLuigi Rizzo fail:
896f9790aebSLuigi Rizzo 	nm_close(d);
897847bf383SLuigi Rizzo 	if (errmsg[0])
898f0ea3689SLuigi Rizzo 		D("%s %s", errmsg, ifname);
8990506889cSLuigi Rizzo 	if (errno == 0)
900f9790aebSLuigi Rizzo 		errno = EINVAL;
901f9790aebSLuigi Rizzo 	return NULL;
902f9790aebSLuigi Rizzo }
903f9790aebSLuigi Rizzo 
904f9790aebSLuigi Rizzo 
905f9790aebSLuigi Rizzo static int
906f0ea3689SLuigi Rizzo nm_close(struct nm_desc *d)
907f9790aebSLuigi Rizzo {
90817885a7bSLuigi Rizzo 	/*
90917885a7bSLuigi Rizzo 	 * ugly trick to avoid unused warnings
91017885a7bSLuigi Rizzo 	 */
91117885a7bSLuigi Rizzo 	static void *__xxzt[] __attribute__ ((unused))  =
912b82b2211SLuigi Rizzo 		{ (void *)nm_open, (void *)nm_inject,
913b82b2211SLuigi Rizzo 		  (void *)nm_dispatch, (void *)nm_nextpkt } ;
91417885a7bSLuigi Rizzo 
915f9790aebSLuigi Rizzo 	if (d == NULL || d->self != d)
916f9790aebSLuigi Rizzo 		return EINVAL;
917f0ea3689SLuigi Rizzo 	if (d->done_mmap && d->mem)
918f9790aebSLuigi Rizzo 		munmap(d->mem, d->memsize);
919*37e3a6d3SLuigi Rizzo 	if (d->fd != -1) {
920f9790aebSLuigi Rizzo 		close(d->fd);
921*37e3a6d3SLuigi Rizzo 	}
922*37e3a6d3SLuigi Rizzo 
923f9790aebSLuigi Rizzo 	bzero(d, sizeof(*d));
924f9790aebSLuigi Rizzo 	free(d);
925f9790aebSLuigi Rizzo 	return 0;
926f9790aebSLuigi Rizzo }
927f9790aebSLuigi Rizzo 
928f9790aebSLuigi Rizzo 
929*37e3a6d3SLuigi Rizzo static int
930*37e3a6d3SLuigi Rizzo nm_mmap(struct nm_desc *d, const struct nm_desc *parent)
931*37e3a6d3SLuigi Rizzo {
932*37e3a6d3SLuigi Rizzo 	//XXX TODO: check if mmap is already done
933*37e3a6d3SLuigi Rizzo 
934*37e3a6d3SLuigi Rizzo 	if (IS_NETMAP_DESC(parent) && parent->mem &&
935*37e3a6d3SLuigi Rizzo 	    parent->req.nr_arg2 == d->req.nr_arg2) {
936*37e3a6d3SLuigi Rizzo 		/* do not mmap, inherit from parent */
937*37e3a6d3SLuigi Rizzo 		D("do not mmap, inherit from parent");
938*37e3a6d3SLuigi Rizzo 		d->memsize = parent->memsize;
939*37e3a6d3SLuigi Rizzo 		d->mem = parent->mem;
940*37e3a6d3SLuigi Rizzo 	} else {
941*37e3a6d3SLuigi Rizzo 		/* XXX TODO: check if memsize is too large (or there is overflow) */
942*37e3a6d3SLuigi Rizzo 		d->memsize = d->req.nr_memsize;
943*37e3a6d3SLuigi Rizzo 		d->mem = mmap(0, d->memsize, PROT_WRITE | PROT_READ, MAP_SHARED,
944*37e3a6d3SLuigi Rizzo 				d->fd, 0);
945*37e3a6d3SLuigi Rizzo 		if (d->mem == MAP_FAILED) {
946*37e3a6d3SLuigi Rizzo 			goto fail;
947*37e3a6d3SLuigi Rizzo 		}
948*37e3a6d3SLuigi Rizzo 		d->done_mmap = 1;
949*37e3a6d3SLuigi Rizzo 	}
950*37e3a6d3SLuigi Rizzo 	{
951*37e3a6d3SLuigi Rizzo 		struct netmap_if *nifp = NETMAP_IF(d->mem, d->req.nr_offset);
952*37e3a6d3SLuigi Rizzo 		struct netmap_ring *r = NETMAP_RXRING(nifp, );
953*37e3a6d3SLuigi Rizzo 
954*37e3a6d3SLuigi Rizzo 		*(struct netmap_if **)(uintptr_t)&(d->nifp) = nifp;
955*37e3a6d3SLuigi Rizzo 		*(struct netmap_ring **)(uintptr_t)&d->some_ring = r;
956*37e3a6d3SLuigi Rizzo 		*(void **)(uintptr_t)&d->buf_start = NETMAP_BUF(r, 0);
957*37e3a6d3SLuigi Rizzo 		*(void **)(uintptr_t)&d->buf_end =
958*37e3a6d3SLuigi Rizzo 			(char *)d->mem + d->memsize;
959*37e3a6d3SLuigi Rizzo 	}
960*37e3a6d3SLuigi Rizzo 
961*37e3a6d3SLuigi Rizzo 	return 0;
962*37e3a6d3SLuigi Rizzo 
963*37e3a6d3SLuigi Rizzo fail:
964*37e3a6d3SLuigi Rizzo 	return EINVAL;
965*37e3a6d3SLuigi Rizzo }
966*37e3a6d3SLuigi Rizzo 
967f9790aebSLuigi Rizzo /*
96817885a7bSLuigi Rizzo  * Same prototype as pcap_inject(), only need to cast.
96917885a7bSLuigi Rizzo  */
97017885a7bSLuigi Rizzo static int
971f0ea3689SLuigi Rizzo nm_inject(struct nm_desc *d, const void *buf, size_t size)
97217885a7bSLuigi Rizzo {
97366015019SLuigi Rizzo 	u_int c, n = d->last_tx_ring - d->first_tx_ring + 1;
97417885a7bSLuigi Rizzo 
97517885a7bSLuigi Rizzo 	for (c = 0; c < n ; c++) {
97617885a7bSLuigi Rizzo 		/* compute current ring to use */
97717885a7bSLuigi Rizzo 		struct netmap_ring *ring;
97817885a7bSLuigi Rizzo 		uint32_t i, idx;
97966015019SLuigi Rizzo 		uint32_t ri = d->cur_tx_ring + c;
98017885a7bSLuigi Rizzo 
98166015019SLuigi Rizzo 		if (ri > d->last_tx_ring)
98266015019SLuigi Rizzo 			ri = d->first_tx_ring;
98317885a7bSLuigi Rizzo 		ring = NETMAP_TXRING(d->nifp, ri);
98417885a7bSLuigi Rizzo 		if (nm_ring_empty(ring)) {
98517885a7bSLuigi Rizzo 			continue;
98617885a7bSLuigi Rizzo 		}
98717885a7bSLuigi Rizzo 		i = ring->cur;
98817885a7bSLuigi Rizzo 		idx = ring->slot[i].buf_idx;
98917885a7bSLuigi Rizzo 		ring->slot[i].len = size;
990f0ea3689SLuigi Rizzo 		nm_pkt_copy(buf, NETMAP_BUF(ring, idx), size);
99166015019SLuigi Rizzo 		d->cur_tx_ring = ri;
99217885a7bSLuigi Rizzo 		ring->head = ring->cur = nm_ring_next(ring, i);
99317885a7bSLuigi Rizzo 		return size;
99417885a7bSLuigi Rizzo 	}
99517885a7bSLuigi Rizzo 	return 0; /* fail */
99617885a7bSLuigi Rizzo }
99717885a7bSLuigi Rizzo 
99817885a7bSLuigi Rizzo 
99917885a7bSLuigi Rizzo /*
1000f9790aebSLuigi Rizzo  * Same prototype as pcap_dispatch(), only need to cast.
1001f9790aebSLuigi Rizzo  */
1002f9790aebSLuigi Rizzo static int
1003f0ea3689SLuigi Rizzo nm_dispatch(struct nm_desc *d, int cnt, nm_cb_t cb, u_char *arg)
1004f9790aebSLuigi Rizzo {
100566015019SLuigi Rizzo 	int n = d->last_rx_ring - d->first_rx_ring + 1;
100666015019SLuigi Rizzo 	int c, got = 0, ri = d->cur_rx_ring;
1007*37e3a6d3SLuigi Rizzo 	d->hdr.buf = NULL;
1008*37e3a6d3SLuigi Rizzo 	d->hdr.flags = NM_MORE_PKTS;
1009*37e3a6d3SLuigi Rizzo 	d->hdr.d = d;
1010f9790aebSLuigi Rizzo 
1011f9790aebSLuigi Rizzo 	if (cnt == 0)
1012f9790aebSLuigi Rizzo 		cnt = -1;
1013f9790aebSLuigi Rizzo 	/* cnt == -1 means infinite, but rings have a finite amount
1014f9790aebSLuigi Rizzo 	 * of buffers and the int is large enough that we never wrap,
1015f9790aebSLuigi Rizzo 	 * so we can omit checking for -1
1016f9790aebSLuigi Rizzo 	 */
1017f9790aebSLuigi Rizzo 	for (c=0; c < n && cnt != got; c++) {
1018f9790aebSLuigi Rizzo 		/* compute current ring to use */
1019f9790aebSLuigi Rizzo 		struct netmap_ring *ring;
1020f9790aebSLuigi Rizzo 
102166015019SLuigi Rizzo 		ri = d->cur_rx_ring + c;
102266015019SLuigi Rizzo 		if (ri > d->last_rx_ring)
102366015019SLuigi Rizzo 			ri = d->first_rx_ring;
1024f9790aebSLuigi Rizzo 		ring = NETMAP_RXRING(d->nifp, ri);
102517885a7bSLuigi Rizzo 		for ( ; !nm_ring_empty(ring) && cnt != got; got++) {
1026*37e3a6d3SLuigi Rizzo 			u_int idx, i;
1027*37e3a6d3SLuigi Rizzo 			if (d->hdr.buf) { /* from previous round */
1028*37e3a6d3SLuigi Rizzo 				cb(arg, &d->hdr, d->hdr.buf);
1029*37e3a6d3SLuigi Rizzo 			}
1030*37e3a6d3SLuigi Rizzo 			i = ring->cur;
1031*37e3a6d3SLuigi Rizzo 			idx = ring->slot[i].buf_idx;
1032*37e3a6d3SLuigi Rizzo 			d->hdr.slot = &ring->slot[i];
1033*37e3a6d3SLuigi Rizzo 			d->hdr.buf = (u_char *)NETMAP_BUF(ring, idx);
103466015019SLuigi Rizzo 			// __builtin_prefetch(buf);
1035f9790aebSLuigi Rizzo 			d->hdr.len = d->hdr.caplen = ring->slot[i].len;
1036f9790aebSLuigi Rizzo 			d->hdr.ts = ring->ts;
103717885a7bSLuigi Rizzo 			ring->head = ring->cur = nm_ring_next(ring, i);
1038f9790aebSLuigi Rizzo 		}
1039f9790aebSLuigi Rizzo 	}
1040*37e3a6d3SLuigi Rizzo 	if (d->hdr.buf) { /* from previous round */
1041*37e3a6d3SLuigi Rizzo 		d->hdr.flags = 0;
1042*37e3a6d3SLuigi Rizzo 		cb(arg, &d->hdr, d->hdr.buf);
1043*37e3a6d3SLuigi Rizzo 	}
104466015019SLuigi Rizzo 	d->cur_rx_ring = ri;
1045f9790aebSLuigi Rizzo 	return got;
1046f9790aebSLuigi Rizzo }
1047f9790aebSLuigi Rizzo 
1048f9790aebSLuigi Rizzo static u_char *
1049f0ea3689SLuigi Rizzo nm_nextpkt(struct nm_desc *d, struct nm_pkthdr *hdr)
1050f9790aebSLuigi Rizzo {
105166015019SLuigi Rizzo 	int ri = d->cur_rx_ring;
1052f9790aebSLuigi Rizzo 
1053f9790aebSLuigi Rizzo 	do {
1054f9790aebSLuigi Rizzo 		/* compute current ring to use */
1055f9790aebSLuigi Rizzo 		struct netmap_ring *ring = NETMAP_RXRING(d->nifp, ri);
105617885a7bSLuigi Rizzo 		if (!nm_ring_empty(ring)) {
1057f9790aebSLuigi Rizzo 			u_int i = ring->cur;
1058f9790aebSLuigi Rizzo 			u_int idx = ring->slot[i].buf_idx;
1059f9790aebSLuigi Rizzo 			u_char *buf = (u_char *)NETMAP_BUF(ring, idx);
106066015019SLuigi Rizzo 
106166015019SLuigi Rizzo 			// __builtin_prefetch(buf);
1062f9790aebSLuigi Rizzo 			hdr->ts = ring->ts;
1063f9790aebSLuigi Rizzo 			hdr->len = hdr->caplen = ring->slot[i].len;
106417885a7bSLuigi Rizzo 			ring->cur = nm_ring_next(ring, i);
106517885a7bSLuigi Rizzo 			/* we could postpone advancing head if we want
106617885a7bSLuigi Rizzo 			 * to hold the buffer. This can be supported in
106717885a7bSLuigi Rizzo 			 * the future.
106817885a7bSLuigi Rizzo 			 */
106917885a7bSLuigi Rizzo 			ring->head = ring->cur;
107066015019SLuigi Rizzo 			d->cur_rx_ring = ri;
1071f9790aebSLuigi Rizzo 			return buf;
1072f9790aebSLuigi Rizzo 		}
1073f9790aebSLuigi Rizzo 		ri++;
107466015019SLuigi Rizzo 		if (ri > d->last_rx_ring)
107566015019SLuigi Rizzo 			ri = d->first_rx_ring;
107666015019SLuigi Rizzo 	} while (ri != d->cur_rx_ring);
1077f9790aebSLuigi Rizzo 	return NULL; /* nothing found */
1078f9790aebSLuigi Rizzo }
1079f9790aebSLuigi Rizzo 
1080f9790aebSLuigi Rizzo #endif /* !HAVE_NETMAP_WITH_LIBS */
1081f9790aebSLuigi Rizzo 
1082f9790aebSLuigi Rizzo #endif /* NETMAP_WITH_LIBS */
1083f9790aebSLuigi Rizzo 
108468b8534bSLuigi Rizzo #endif /* _NET_NETMAP_USER_H_ */
1085