xref: /freebsd-14.2/sys/net/netmap_user.h (revision 4f80b14c)
1fe267a55SPedro F. Giffuni /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3fe267a55SPedro F. Giffuni  *
437e3a6d3SLuigi Rizzo  * Copyright (C) 2011-2016 Universita` di Pisa
537e3a6d3SLuigi Rizzo  * All rights reserved.
668b8534bSLuigi Rizzo  *
768b8534bSLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
8f9790aebSLuigi Rizzo  * modification, are permitted provided that the following conditions
9f9790aebSLuigi Rizzo  * are met:
1068b8534bSLuigi Rizzo  *
1168b8534bSLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
1268b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
1368b8534bSLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
1468b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
15f9790aebSLuigi Rizzo  *      documentation and/or other materials provided with the distribution.
1668b8534bSLuigi Rizzo  *
17f9790aebSLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1868b8534bSLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19f9790aebSLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20f9790aebSLuigi Rizzo  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21f9790aebSLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22f9790aebSLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23f9790aebSLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24f9790aebSLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25f9790aebSLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26f9790aebSLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27f9790aebSLuigi Rizzo  * SUCH DAMAGE.
2868b8534bSLuigi Rizzo  */
2968b8534bSLuigi Rizzo 
3068b8534bSLuigi Rizzo /*
3168b8534bSLuigi Rizzo  * $FreeBSD$
3268b8534bSLuigi Rizzo  *
3317885a7bSLuigi Rizzo  * Functions and macros to manipulate netmap structures and packets
3417885a7bSLuigi Rizzo  * in userspace. See netmap(4) for more information.
3568b8534bSLuigi Rizzo  *
3664ae02c3SLuigi Rizzo  * The address of the struct netmap_if, say nifp, is computed from the
3764ae02c3SLuigi Rizzo  * value returned from ioctl(.., NIOCREG, ...) and the mmap region:
3868b8534bSLuigi Rizzo  *	ioctl(fd, NIOCREG, &req);
3968b8534bSLuigi Rizzo  *	mem = mmap(0, ... );
4068b8534bSLuigi Rizzo  *	nifp = NETMAP_IF(mem, req.nr_nifp);
4168b8534bSLuigi Rizzo  *		(so simple, we could just do it manually)
4268b8534bSLuigi Rizzo  *
4368b8534bSLuigi Rizzo  * From there:
4468b8534bSLuigi Rizzo  *	struct netmap_ring *NETMAP_TXRING(nifp, index)
4568b8534bSLuigi Rizzo  *	struct netmap_ring *NETMAP_RXRING(nifp, index)
460506889cSLuigi Rizzo  *		we can access ring->cur, ring->head, ring->tail, etc.
4768b8534bSLuigi Rizzo  *
4868b8534bSLuigi Rizzo  *	ring->slot[i] gives us the i-th slot (we can access
4917885a7bSLuigi Rizzo  *		directly len, flags, buf_idx)
5068b8534bSLuigi Rizzo  *
5127892e02SLuigi Rizzo  *	char *buf = NETMAP_BUF(ring, x) returns a pointer to
5227892e02SLuigi Rizzo  *		the buffer numbered x
5368b8534bSLuigi Rizzo  *
5417885a7bSLuigi Rizzo  * All ring indexes (head, cur, tail) should always move forward.
5517885a7bSLuigi Rizzo  * To compute the next index in a circular ring you can use
5617885a7bSLuigi Rizzo  *	i = nm_ring_next(ring, i);
57f9790aebSLuigi Rizzo  *
58f9790aebSLuigi Rizzo  * To ease porting apps from pcap to netmap we supply a few fuctions
5917885a7bSLuigi Rizzo  * that can be called to open, close, read and write on netmap in a way
6017885a7bSLuigi Rizzo  * similar to libpcap. Note that the read/write function depend on
6117885a7bSLuigi Rizzo  * an ioctl()/select()/poll() being issued to refill rings or push
6217885a7bSLuigi Rizzo  * packets out.
63f9790aebSLuigi Rizzo  *
64f9790aebSLuigi Rizzo  * In order to use these, include #define NETMAP_WITH_LIBS
65f9790aebSLuigi Rizzo  * in the source file that invokes these functions.
6668b8534bSLuigi Rizzo  */
6768b8534bSLuigi Rizzo 
6868b8534bSLuigi Rizzo #ifndef _NET_NETMAP_USER_H_
6968b8534bSLuigi Rizzo #define _NET_NETMAP_USER_H_
7068b8534bSLuigi Rizzo 
7137e3a6d3SLuigi Rizzo #define NETMAP_DEVICE_NAME "/dev/netmap"
7237e3a6d3SLuigi Rizzo 
7337e3a6d3SLuigi Rizzo #ifdef __CYGWIN__
7437e3a6d3SLuigi Rizzo /*
7537e3a6d3SLuigi Rizzo  * we can compile userspace apps with either cygwin or msvc,
7637e3a6d3SLuigi Rizzo  * and we use _WIN32 to identify windows specific code
7737e3a6d3SLuigi Rizzo  */
7837e3a6d3SLuigi Rizzo #ifndef _WIN32
7937e3a6d3SLuigi Rizzo #define _WIN32
8037e3a6d3SLuigi Rizzo #endif	/* _WIN32 */
8137e3a6d3SLuigi Rizzo 
8237e3a6d3SLuigi Rizzo #endif	/* __CYGWIN__ */
8337e3a6d3SLuigi Rizzo 
8437e3a6d3SLuigi Rizzo #ifdef _WIN32
8537e3a6d3SLuigi Rizzo #undef NETMAP_DEVICE_NAME
8637e3a6d3SLuigi Rizzo #define NETMAP_DEVICE_NAME "/proc/sys/DosDevices/Global/netmap"
8737e3a6d3SLuigi Rizzo #include <windows.h>
8837e3a6d3SLuigi Rizzo #include <WinDef.h>
8937e3a6d3SLuigi Rizzo #include <sys/cygwin.h>
9037e3a6d3SLuigi Rizzo #endif /* _WIN32 */
9137e3a6d3SLuigi Rizzo 
92f9790aebSLuigi Rizzo #include <stdint.h>
93f0ea3689SLuigi Rizzo #include <sys/socket.h>		/* apple needs sockaddr */
94f9790aebSLuigi Rizzo #include <net/if.h>		/* IFNAMSIZ */
9537e3a6d3SLuigi Rizzo #include <ctype.h>
9617885a7bSLuigi Rizzo 
9717885a7bSLuigi Rizzo #ifndef likely
9817885a7bSLuigi Rizzo #define likely(x)	__builtin_expect(!!(x), 1)
9917885a7bSLuigi Rizzo #define unlikely(x)	__builtin_expect(!!(x), 0)
10017885a7bSLuigi Rizzo #endif /* likely and unlikely */
10117885a7bSLuigi Rizzo 
102f9790aebSLuigi Rizzo #include <net/netmap.h>
103*4f80b14cSVincenzo Maffione #include <net/netmap_virt.h> /* nmreq_pointer_get() */
104f9790aebSLuigi Rizzo 
10517885a7bSLuigi Rizzo /* helper macro */
106104d9fc7SEd Maste #define _NETMAP_OFFSET(type, ptr, offset) \
107104d9fc7SEd Maste 	((type)(void *)((char *)(ptr) + (offset)))
10868b8534bSLuigi Rizzo 
10917885a7bSLuigi Rizzo #define NETMAP_IF(_base, _ofs)	_NETMAP_OFFSET(struct netmap_if *, _base, _ofs)
11068b8534bSLuigi Rizzo 
111104d9fc7SEd Maste #define NETMAP_TXRING(nifp, index) _NETMAP_OFFSET(struct netmap_ring *, \
112104d9fc7SEd Maste 	nifp, (nifp)->ring_ofs[index] )
113104d9fc7SEd Maste 
114104d9fc7SEd Maste #define NETMAP_RXRING(nifp, index) _NETMAP_OFFSET(struct netmap_ring *,	\
115104d9fc7SEd Maste 	nifp, (nifp)->ring_ofs[index + (nifp)->ni_tx_rings + 1] )
11668b8534bSLuigi Rizzo 
11768b8534bSLuigi Rizzo #define NETMAP_BUF(ring, index)				\
118*4f80b14cSVincenzo Maffione 	((char *)(ring) + (ring)->buf_ofs + ((long)(index)*(ring)->nr_buf_size))
11968b8534bSLuigi Rizzo 
12064ae02c3SLuigi Rizzo #define NETMAP_BUF_IDX(ring, buf)			\
12164ae02c3SLuigi Rizzo 	( ((char *)(buf) - ((char *)(ring) + (ring)->buf_ofs) ) / \
122a7250582SLuigi Rizzo 		(ring)->nr_buf_size )
12364ae02c3SLuigi Rizzo 
12468b8534bSLuigi Rizzo 
12517885a7bSLuigi Rizzo static inline uint32_t
12617885a7bSLuigi Rizzo nm_ring_next(struct netmap_ring *r, uint32_t i)
12717885a7bSLuigi Rizzo {
12817885a7bSLuigi Rizzo 	return ( unlikely(i + 1 == r->num_slots) ? 0 : i + 1);
12917885a7bSLuigi Rizzo }
13017885a7bSLuigi Rizzo 
131a7250582SLuigi Rizzo 
13268b8534bSLuigi Rizzo /*
13317885a7bSLuigi Rizzo  * Return 1 if we have pending transmissions in the tx ring.
134f0ea3689SLuigi Rizzo  * When everything is complete ring->head = ring->tail + 1 (modulo ring size)
13568b8534bSLuigi Rizzo  */
13617885a7bSLuigi Rizzo static inline int
13717885a7bSLuigi Rizzo nm_tx_pending(struct netmap_ring *r)
13817885a7bSLuigi Rizzo {
139f0ea3689SLuigi Rizzo 	return nm_ring_next(r, r->tail) != r->head;
14017885a7bSLuigi Rizzo }
14117885a7bSLuigi Rizzo 
14217885a7bSLuigi Rizzo 
14317885a7bSLuigi Rizzo static inline uint32_t
14417885a7bSLuigi Rizzo nm_ring_space(struct netmap_ring *ring)
14517885a7bSLuigi Rizzo {
14617885a7bSLuigi Rizzo         int ret = ring->tail - ring->cur;
14717885a7bSLuigi Rizzo         if (ret < 0)
14817885a7bSLuigi Rizzo                 ret += ring->num_slots;
14917885a7bSLuigi Rizzo         return ret;
15017885a7bSLuigi Rizzo }
15117885a7bSLuigi Rizzo 
15268b8534bSLuigi Rizzo 
153f9790aebSLuigi Rizzo #ifdef NETMAP_WITH_LIBS
154f9790aebSLuigi Rizzo /*
155f9790aebSLuigi Rizzo  * Support for simple I/O libraries.
156f9790aebSLuigi Rizzo  * Include other system headers required for compiling this.
157f9790aebSLuigi Rizzo  */
158f9790aebSLuigi Rizzo 
159f9790aebSLuigi Rizzo #ifndef HAVE_NETMAP_WITH_LIBS
160f9790aebSLuigi Rizzo #define HAVE_NETMAP_WITH_LIBS
161f9790aebSLuigi Rizzo 
1629225c808SLuigi Rizzo #include <stdio.h>
163f9790aebSLuigi Rizzo #include <sys/time.h>
164f9790aebSLuigi Rizzo #include <sys/mman.h>
165f9790aebSLuigi Rizzo #include <string.h>	/* memset */
166f9790aebSLuigi Rizzo #include <sys/ioctl.h>
167f9790aebSLuigi Rizzo #include <sys/errno.h>	/* EINVAL */
168f9790aebSLuigi Rizzo #include <fcntl.h>	/* O_RDWR */
16917885a7bSLuigi Rizzo #include <unistd.h>	/* close() */
17066015019SLuigi Rizzo #include <signal.h>
17117885a7bSLuigi Rizzo #include <stdlib.h>
172f9790aebSLuigi Rizzo 
173f0ea3689SLuigi Rizzo #ifndef ND /* debug macros */
174f0ea3689SLuigi Rizzo /* debug support */
175f0ea3689SLuigi Rizzo #define ND(_fmt, ...) do {} while(0)
176f0ea3689SLuigi Rizzo #define D(_fmt, ...)						\
177f0ea3689SLuigi Rizzo 	do {							\
1784bf50f18SLuigi Rizzo 		struct timeval _t0;				\
1794bf50f18SLuigi Rizzo 		gettimeofday(&_t0, NULL);			\
180f0ea3689SLuigi Rizzo 		fprintf(stderr, "%03d.%06d %s [%d] " _fmt "\n",	\
1814bf50f18SLuigi Rizzo 		    (int)(_t0.tv_sec % 1000), (int)_t0.tv_usec,	\
182f0ea3689SLuigi Rizzo 		    __FUNCTION__, __LINE__, ##__VA_ARGS__);	\
183f0ea3689SLuigi Rizzo         } while (0)
184f0ea3689SLuigi Rizzo 
185f0ea3689SLuigi Rizzo /* Rate limited version of "D", lps indicates how many per second */
186f0ea3689SLuigi Rizzo #define RD(lps, format, ...)                                    \
187f0ea3689SLuigi Rizzo     do {                                                        \
1884bf50f18SLuigi Rizzo         static int __t0, __cnt;                                 \
189f0ea3689SLuigi Rizzo         struct timeval __xxts;                                  \
190f0ea3689SLuigi Rizzo         gettimeofday(&__xxts, NULL);                            \
1914bf50f18SLuigi Rizzo         if (__t0 != __xxts.tv_sec) {                            \
1924bf50f18SLuigi Rizzo             __t0 = __xxts.tv_sec;                               \
193f0ea3689SLuigi Rizzo             __cnt = 0;                                          \
194f0ea3689SLuigi Rizzo         }                                                       \
195f0ea3689SLuigi Rizzo         if (__cnt++ < lps) {                                    \
196f0ea3689SLuigi Rizzo             D(format, ##__VA_ARGS__);                           \
197f0ea3689SLuigi Rizzo         }                                                       \
198f0ea3689SLuigi Rizzo     } while (0)
199f0ea3689SLuigi Rizzo #endif
200f0ea3689SLuigi Rizzo 
20137e3a6d3SLuigi Rizzo struct nm_pkthdr {	/* first part is the same as pcap_pkthdr */
202f9790aebSLuigi Rizzo 	struct timeval	ts;
203f9790aebSLuigi Rizzo 	uint32_t	caplen;
204f9790aebSLuigi Rizzo 	uint32_t	len;
20537e3a6d3SLuigi Rizzo 
20637e3a6d3SLuigi Rizzo 	uint64_t flags;	/* NM_MORE_PKTS etc */
20737e3a6d3SLuigi Rizzo #define NM_MORE_PKTS	1
20837e3a6d3SLuigi Rizzo 	struct nm_desc *d;
20937e3a6d3SLuigi Rizzo 	struct netmap_slot *slot;
21037e3a6d3SLuigi Rizzo 	uint8_t *buf;
211f9790aebSLuigi Rizzo };
212f9790aebSLuigi Rizzo 
213f0ea3689SLuigi Rizzo struct nm_stat {	/* same as pcap_stat	*/
21466015019SLuigi Rizzo 	u_int	ps_recv;
21566015019SLuigi Rizzo 	u_int	ps_drop;
21666015019SLuigi Rizzo 	u_int	ps_ifdrop;
21737e3a6d3SLuigi Rizzo #ifdef WIN32 /* XXX or _WIN32 ? */
21866015019SLuigi Rizzo 	u_int	bs_capt;
21966015019SLuigi Rizzo #endif /* WIN32 */
22066015019SLuigi Rizzo };
22166015019SLuigi Rizzo 
22266015019SLuigi Rizzo #define NM_ERRBUF_SIZE	512
22366015019SLuigi Rizzo 
224f0ea3689SLuigi Rizzo struct nm_desc {
225f0ea3689SLuigi Rizzo 	struct nm_desc *self; /* point to self if netmap. */
226f9790aebSLuigi Rizzo 	int fd;
227f9790aebSLuigi Rizzo 	void *mem;
228*4f80b14cSVincenzo Maffione 	uint64_t memsize;
229f0ea3689SLuigi Rizzo 	int done_mmap;	/* set if mem is the result of mmap */
230f0ea3689SLuigi Rizzo 	struct netmap_if * const nifp;
23166015019SLuigi Rizzo 	uint16_t first_tx_ring, last_tx_ring, cur_tx_ring;
23266015019SLuigi Rizzo 	uint16_t first_rx_ring, last_rx_ring, cur_rx_ring;
23366015019SLuigi Rizzo 	struct nmreq req;	/* also contains the nr_name = ifname */
234f0ea3689SLuigi Rizzo 	struct nm_pkthdr hdr;
23566015019SLuigi Rizzo 
236f0ea3689SLuigi Rizzo 	/*
237f0ea3689SLuigi Rizzo 	 * The memory contains netmap_if, rings and then buffers.
238f0ea3689SLuigi Rizzo 	 * Given a pointer (e.g. to nm_inject) we can compare with
239f0ea3689SLuigi Rizzo 	 * mem/buf_start/buf_end to tell if it is a buffer or
240f0ea3689SLuigi Rizzo 	 * some other descriptor in our region.
241f0ea3689SLuigi Rizzo 	 * We also store a pointer to some ring as it helps in the
242f0ea3689SLuigi Rizzo 	 * translation from buffer indexes to addresses.
243f0ea3689SLuigi Rizzo 	 */
244f0ea3689SLuigi Rizzo 	struct netmap_ring * const some_ring;
245f0ea3689SLuigi Rizzo 	void * const buf_start;
246f0ea3689SLuigi Rizzo 	void * const buf_end;
24766015019SLuigi Rizzo 	/* parameters from pcap_open_live */
24866015019SLuigi Rizzo 	int snaplen;
24966015019SLuigi Rizzo 	int promisc;
25066015019SLuigi Rizzo 	int to_ms;
25166015019SLuigi Rizzo 	char *errbuf;
25266015019SLuigi Rizzo 
25366015019SLuigi Rizzo 	/* save flags so we can restore them on close */
25466015019SLuigi Rizzo 	uint32_t if_flags;
25566015019SLuigi Rizzo         uint32_t if_reqcap;
25666015019SLuigi Rizzo         uint32_t if_curcap;
25766015019SLuigi Rizzo 
258f0ea3689SLuigi Rizzo 	struct nm_stat st;
25966015019SLuigi Rizzo 	char msg[NM_ERRBUF_SIZE];
260f9790aebSLuigi Rizzo };
261f9790aebSLuigi Rizzo 
262f9790aebSLuigi Rizzo /*
263f9790aebSLuigi Rizzo  * when the descriptor is open correctly, d->self == d
264b82b2211SLuigi Rizzo  * Eventually we should also use some magic number.
265f9790aebSLuigi Rizzo  */
266f0ea3689SLuigi Rizzo #define P2NMD(p)		((struct nm_desc *)(p))
267f0ea3689SLuigi Rizzo #define IS_NETMAP_DESC(d)	((d) && P2NMD(d)->self == P2NMD(d))
268f9790aebSLuigi Rizzo #define NETMAP_FD(d)		(P2NMD(d)->fd)
269f9790aebSLuigi Rizzo 
27017885a7bSLuigi Rizzo 
27117885a7bSLuigi Rizzo /*
27217885a7bSLuigi Rizzo  * this is a slightly optimized copy routine which rounds
27317885a7bSLuigi Rizzo  * to multiple of 64 bytes and is often faster than dealing
27417885a7bSLuigi Rizzo  * with other odd sizes. We assume there is enough room
27517885a7bSLuigi Rizzo  * in the source and destination buffers.
27617885a7bSLuigi Rizzo  */
27717885a7bSLuigi Rizzo static inline void
278f0ea3689SLuigi Rizzo nm_pkt_copy(const void *_src, void *_dst, int l)
27917885a7bSLuigi Rizzo {
280b82b2211SLuigi Rizzo 	const uint64_t *src = (const uint64_t *)_src;
281b82b2211SLuigi Rizzo 	uint64_t *dst = (uint64_t *)_dst;
282b82b2211SLuigi Rizzo 
283*4f80b14cSVincenzo Maffione 	if (unlikely(l >= 1024 || l % 64)) {
28417885a7bSLuigi Rizzo 		memcpy(dst, src, l);
28517885a7bSLuigi Rizzo 		return;
28617885a7bSLuigi Rizzo 	}
28717885a7bSLuigi Rizzo 	for (; likely(l > 0); l-=64) {
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 		*dst++ = *src++;
29617885a7bSLuigi Rizzo 	}
29717885a7bSLuigi Rizzo }
29817885a7bSLuigi Rizzo 
29917885a7bSLuigi Rizzo 
300f9790aebSLuigi Rizzo /*
301f9790aebSLuigi Rizzo  * The callback, invoked on each received packet. Same as libpcap
302f9790aebSLuigi Rizzo  */
303f0ea3689SLuigi Rizzo typedef void (*nm_cb_t)(u_char *, const struct nm_pkthdr *, const u_char *d);
304f9790aebSLuigi Rizzo 
305f9790aebSLuigi Rizzo /*
30617885a7bSLuigi Rizzo  *--- the pcap-like API ---
30717885a7bSLuigi Rizzo  *
30817885a7bSLuigi Rizzo  * nm_open() opens a file descriptor, binds to a port and maps memory.
30917885a7bSLuigi Rizzo  *
31017885a7bSLuigi Rizzo  * ifname	(netmap:foo or vale:foo) is the port name
311f0ea3689SLuigi Rizzo  *		a suffix can indicate the follwing:
312f0ea3689SLuigi Rizzo  *		^		bind the host (sw) ring pair
313f9eece46SLuiz Otavio O Souza  *		*		bind host and NIC ring pairs
314f0ea3689SLuigi Rizzo  *		-NN		bind individual NIC ring pair
315f0ea3689SLuigi Rizzo  *		{NN		bind master side of pipe NN
316f0ea3689SLuigi Rizzo  *		}NN		bind slave side of pipe NN
31737e3a6d3SLuigi Rizzo  *		a suffix starting with / and the following flags,
318847bf383SLuigi Rizzo  *		in any order:
319847bf383SLuigi Rizzo  *		x		exclusive access
320f9eece46SLuiz Otavio O Souza  *		z		zero copy monitor (both tx and rx)
321f9eece46SLuiz Otavio O Souza  *		t		monitor tx side (copy monitor)
322f9eece46SLuiz Otavio O Souza  *		r		monitor rx side (copy monitor)
32337e3a6d3SLuigi Rizzo  *		R		bind only RX ring(s)
32437e3a6d3SLuigi Rizzo  *		T		bind only TX ring(s)
325f0ea3689SLuigi Rizzo  *
326f0ea3689SLuigi Rizzo  * req		provides the initial values of nmreq before parsing ifname.
327f0ea3689SLuigi Rizzo  *		Remember that the ifname parsing will override the ring
328f0ea3689SLuigi Rizzo  *		number in nm_ringid, and part of nm_flags;
329f0ea3689SLuigi Rizzo  * flags	special functions, normally 0
330f0ea3689SLuigi Rizzo  *		indicates which fields of *arg are significant
331f0ea3689SLuigi Rizzo  * arg		special functions, normally NULL
332f0ea3689SLuigi Rizzo  *		if passed a netmap_desc with mem != NULL,
333f0ea3689SLuigi Rizzo  *		use that memory instead of mmap.
334f9790aebSLuigi Rizzo  */
33517885a7bSLuigi Rizzo 
336f0ea3689SLuigi Rizzo static struct nm_desc *nm_open(const char *ifname, const struct nmreq *req,
337f0ea3689SLuigi Rizzo 	uint64_t flags, const struct nm_desc *arg);
338f0ea3689SLuigi Rizzo 
339f0ea3689SLuigi Rizzo /*
340f0ea3689SLuigi Rizzo  * nm_open can import some fields from the parent descriptor.
341f0ea3689SLuigi Rizzo  * These flags control which ones.
342f0ea3689SLuigi Rizzo  * Also in flags you can specify NETMAP_NO_TX_POLL and NETMAP_DO_RX_POLL,
343f0ea3689SLuigi Rizzo  * which set the initial value for these flags.
344f0ea3689SLuigi Rizzo  * Note that the 16 low bits of the flags are reserved for data
345f0ea3689SLuigi Rizzo  * that may go into the nmreq.
346f0ea3689SLuigi Rizzo  */
347f0ea3689SLuigi Rizzo enum {
348f0ea3689SLuigi Rizzo 	NM_OPEN_NO_MMAP =	0x040000, /* reuse mmap from parent */
349f0ea3689SLuigi Rizzo 	NM_OPEN_IFNAME =	0x080000, /* nr_name, nr_ringid, nr_flags */
350f0ea3689SLuigi Rizzo 	NM_OPEN_ARG1 =		0x100000,
351f0ea3689SLuigi Rizzo 	NM_OPEN_ARG2 =		0x200000,
352f0ea3689SLuigi Rizzo 	NM_OPEN_ARG3 =		0x400000,
353f0ea3689SLuigi Rizzo 	NM_OPEN_RING_CFG =	0x800000, /* tx|rx rings|slots */
354*4f80b14cSVincenzo Maffione 	NM_OPEN_EXTMEM =       0x1000000,
355f0ea3689SLuigi Rizzo };
356f0ea3689SLuigi Rizzo 
357f9790aebSLuigi Rizzo 
358f9790aebSLuigi Rizzo /*
35917885a7bSLuigi Rizzo  * nm_close()	closes and restores the port to its previous state
360f9790aebSLuigi Rizzo  */
36117885a7bSLuigi Rizzo 
362f0ea3689SLuigi Rizzo static int nm_close(struct nm_desc *);
363f9790aebSLuigi Rizzo 
364f9790aebSLuigi Rizzo /*
36537e3a6d3SLuigi Rizzo  * nm_mmap()    do mmap or inherit from parent if the nr_arg2
36637e3a6d3SLuigi Rizzo  *              (memory block) matches.
36737e3a6d3SLuigi Rizzo  */
36837e3a6d3SLuigi Rizzo 
36937e3a6d3SLuigi Rizzo static int nm_mmap(struct nm_desc *, const struct nm_desc *);
37037e3a6d3SLuigi Rizzo 
37137e3a6d3SLuigi Rizzo /*
37217885a7bSLuigi Rizzo  * nm_inject() is the same as pcap_inject()
37317885a7bSLuigi Rizzo  * nm_dispatch() is the same as pcap_dispatch()
37417885a7bSLuigi Rizzo  * nm_nextpkt() is the same as pcap_next()
375f9790aebSLuigi Rizzo  */
37617885a7bSLuigi Rizzo 
377f0ea3689SLuigi Rizzo static int nm_inject(struct nm_desc *, const void *, size_t);
378f0ea3689SLuigi Rizzo static int nm_dispatch(struct nm_desc *, int, nm_cb_t, u_char *);
379f0ea3689SLuigi Rizzo static u_char *nm_nextpkt(struct nm_desc *, struct nm_pkthdr *);
380f9790aebSLuigi Rizzo 
38137e3a6d3SLuigi Rizzo #ifdef _WIN32
38237e3a6d3SLuigi Rizzo 
38337e3a6d3SLuigi Rizzo intptr_t _get_osfhandle(int); /* defined in io.h in windows */
38437e3a6d3SLuigi Rizzo 
38537e3a6d3SLuigi Rizzo /*
38637e3a6d3SLuigi Rizzo  * In windows we do not have yet native poll support, so we keep track
38737e3a6d3SLuigi Rizzo  * of file descriptors associated to netmap ports to emulate poll on
38837e3a6d3SLuigi Rizzo  * them and fall back on regular poll on other file descriptors.
38937e3a6d3SLuigi Rizzo  */
39037e3a6d3SLuigi Rizzo struct win_netmap_fd_list {
39137e3a6d3SLuigi Rizzo 	struct win_netmap_fd_list *next;
39237e3a6d3SLuigi Rizzo 	int win_netmap_fd;
39337e3a6d3SLuigi Rizzo 	HANDLE win_netmap_handle;
39437e3a6d3SLuigi Rizzo };
39537e3a6d3SLuigi Rizzo 
39637e3a6d3SLuigi Rizzo /*
39737e3a6d3SLuigi Rizzo  * list head containing all the netmap opened fd and their
39837e3a6d3SLuigi Rizzo  * windows HANDLE counterparts
39937e3a6d3SLuigi Rizzo  */
40037e3a6d3SLuigi Rizzo static struct win_netmap_fd_list *win_netmap_fd_list_head;
40137e3a6d3SLuigi Rizzo 
40237e3a6d3SLuigi Rizzo static void
40337e3a6d3SLuigi Rizzo win_insert_fd_record(int fd)
40437e3a6d3SLuigi Rizzo {
40537e3a6d3SLuigi Rizzo 	struct win_netmap_fd_list *curr;
40637e3a6d3SLuigi Rizzo 
40737e3a6d3SLuigi Rizzo 	for (curr = win_netmap_fd_list_head; curr; curr = curr->next) {
40837e3a6d3SLuigi Rizzo 		if (fd == curr->win_netmap_fd) {
40937e3a6d3SLuigi Rizzo 			return;
41037e3a6d3SLuigi Rizzo 		}
41137e3a6d3SLuigi Rizzo 	}
41237e3a6d3SLuigi Rizzo 	curr = calloc(1, sizeof(*curr));
41337e3a6d3SLuigi Rizzo 	curr->next = win_netmap_fd_list_head;
41437e3a6d3SLuigi Rizzo 	curr->win_netmap_fd = fd;
41537e3a6d3SLuigi Rizzo 	curr->win_netmap_handle = IntToPtr(_get_osfhandle(fd));
41637e3a6d3SLuigi Rizzo 	win_netmap_fd_list_head = curr;
41737e3a6d3SLuigi Rizzo }
41837e3a6d3SLuigi Rizzo 
41937e3a6d3SLuigi Rizzo void
42037e3a6d3SLuigi Rizzo win_remove_fd_record(int fd)
42137e3a6d3SLuigi Rizzo {
42237e3a6d3SLuigi Rizzo 	struct win_netmap_fd_list *curr = win_netmap_fd_list_head;
42337e3a6d3SLuigi Rizzo 	struct win_netmap_fd_list *prev = NULL;
42437e3a6d3SLuigi Rizzo 	for (; curr ; prev = curr, curr = curr->next) {
42537e3a6d3SLuigi Rizzo 		if (fd != curr->win_netmap_fd)
42637e3a6d3SLuigi Rizzo 			continue;
42737e3a6d3SLuigi Rizzo 		/* found the entry */
42837e3a6d3SLuigi Rizzo 		if (prev == NULL) { /* we are freeing the first entry */
42937e3a6d3SLuigi Rizzo 			win_netmap_fd_list_head = curr->next;
43037e3a6d3SLuigi Rizzo 		} else {
43137e3a6d3SLuigi Rizzo 			prev->next = curr->next;
43237e3a6d3SLuigi Rizzo 		}
43337e3a6d3SLuigi Rizzo 		free(curr);
43437e3a6d3SLuigi Rizzo 		break;
43537e3a6d3SLuigi Rizzo 	}
43637e3a6d3SLuigi Rizzo }
43737e3a6d3SLuigi Rizzo 
43837e3a6d3SLuigi Rizzo 
43937e3a6d3SLuigi Rizzo HANDLE
44037e3a6d3SLuigi Rizzo win_get_netmap_handle(int fd)
44137e3a6d3SLuigi Rizzo {
44237e3a6d3SLuigi Rizzo 	struct win_netmap_fd_list *curr;
44337e3a6d3SLuigi Rizzo 
44437e3a6d3SLuigi Rizzo 	for (curr = win_netmap_fd_list_head; curr; curr = curr->next) {
44537e3a6d3SLuigi Rizzo 		if (fd == curr->win_netmap_fd) {
44637e3a6d3SLuigi Rizzo 			return curr->win_netmap_handle;
44737e3a6d3SLuigi Rizzo 		}
44837e3a6d3SLuigi Rizzo 	}
44937e3a6d3SLuigi Rizzo 	return NULL;
45037e3a6d3SLuigi Rizzo }
45137e3a6d3SLuigi Rizzo 
45237e3a6d3SLuigi Rizzo /*
45337e3a6d3SLuigi Rizzo  * we need to wrap ioctl and mmap, at least for the netmap file descriptors
45437e3a6d3SLuigi Rizzo  */
45537e3a6d3SLuigi Rizzo 
45637e3a6d3SLuigi Rizzo /*
45737e3a6d3SLuigi Rizzo  * use this function only from netmap_user.h internal functions
45837e3a6d3SLuigi Rizzo  * same as ioctl, returns 0 on success and -1 on error
45937e3a6d3SLuigi Rizzo  */
46037e3a6d3SLuigi Rizzo static int
46137e3a6d3SLuigi Rizzo win_nm_ioctl_internal(HANDLE h, int32_t ctlCode, void *arg)
46237e3a6d3SLuigi Rizzo {
46337e3a6d3SLuigi Rizzo 	DWORD bReturn = 0, szIn, szOut;
46437e3a6d3SLuigi Rizzo 	BOOL ioctlReturnStatus;
46537e3a6d3SLuigi Rizzo 	void *inParam = arg, *outParam = arg;
46637e3a6d3SLuigi Rizzo 
46737e3a6d3SLuigi Rizzo 	switch (ctlCode) {
46837e3a6d3SLuigi Rizzo 	case NETMAP_POLL:
46937e3a6d3SLuigi Rizzo 		szIn = sizeof(POLL_REQUEST_DATA);
47037e3a6d3SLuigi Rizzo 		szOut = sizeof(POLL_REQUEST_DATA);
47137e3a6d3SLuigi Rizzo 		break;
47237e3a6d3SLuigi Rizzo 	case NETMAP_MMAP:
47337e3a6d3SLuigi Rizzo 		szIn = 0;
47437e3a6d3SLuigi Rizzo 		szOut = sizeof(void*);
47537e3a6d3SLuigi Rizzo 		inParam = NULL; /* nothing on input */
47637e3a6d3SLuigi Rizzo 		break;
47737e3a6d3SLuigi Rizzo 	case NIOCTXSYNC:
47837e3a6d3SLuigi Rizzo 	case NIOCRXSYNC:
47937e3a6d3SLuigi Rizzo 		szIn = 0;
48037e3a6d3SLuigi Rizzo 		szOut = 0;
48137e3a6d3SLuigi Rizzo 		break;
48237e3a6d3SLuigi Rizzo 	case NIOCREGIF:
48337e3a6d3SLuigi Rizzo 		szIn = sizeof(struct nmreq);
48437e3a6d3SLuigi Rizzo 		szOut = sizeof(struct nmreq);
48537e3a6d3SLuigi Rizzo 		break;
48637e3a6d3SLuigi Rizzo 	case NIOCCONFIG:
48737e3a6d3SLuigi Rizzo 		D("unsupported NIOCCONFIG!");
48837e3a6d3SLuigi Rizzo 		return -1;
48937e3a6d3SLuigi Rizzo 
49037e3a6d3SLuigi Rizzo 	default: /* a regular ioctl */
49137e3a6d3SLuigi Rizzo 		D("invalid ioctl %x on netmap fd", ctlCode);
49237e3a6d3SLuigi Rizzo 		return -1;
49337e3a6d3SLuigi Rizzo 	}
49437e3a6d3SLuigi Rizzo 
49537e3a6d3SLuigi Rizzo 	ioctlReturnStatus = DeviceIoControl(h,
49637e3a6d3SLuigi Rizzo 				ctlCode, inParam, szIn,
49737e3a6d3SLuigi Rizzo 				outParam, szOut,
49837e3a6d3SLuigi Rizzo 				&bReturn, NULL);
49937e3a6d3SLuigi Rizzo 	// XXX note windows returns 0 on error or async call, 1 on success
50037e3a6d3SLuigi Rizzo 	// we could call GetLastError() to figure out what happened
50137e3a6d3SLuigi Rizzo 	return ioctlReturnStatus ? 0 : -1;
50237e3a6d3SLuigi Rizzo }
50337e3a6d3SLuigi Rizzo 
50437e3a6d3SLuigi Rizzo /*
50537e3a6d3SLuigi Rizzo  * this function is what must be called from user-space programs
50637e3a6d3SLuigi Rizzo  * same as ioctl, returns 0 on success and -1 on error
50737e3a6d3SLuigi Rizzo  */
50837e3a6d3SLuigi Rizzo static int
50937e3a6d3SLuigi Rizzo win_nm_ioctl(int fd, int32_t ctlCode, void *arg)
51037e3a6d3SLuigi Rizzo {
51137e3a6d3SLuigi Rizzo 	HANDLE h = win_get_netmap_handle(fd);
51237e3a6d3SLuigi Rizzo 
51337e3a6d3SLuigi Rizzo 	if (h == NULL) {
51437e3a6d3SLuigi Rizzo 		return ioctl(fd, ctlCode, arg);
51537e3a6d3SLuigi Rizzo 	} else {
51637e3a6d3SLuigi Rizzo 		return win_nm_ioctl_internal(h, ctlCode, arg);
51737e3a6d3SLuigi Rizzo 	}
51837e3a6d3SLuigi Rizzo }
51937e3a6d3SLuigi Rizzo 
52037e3a6d3SLuigi Rizzo #define ioctl win_nm_ioctl /* from now on, within this file ... */
52137e3a6d3SLuigi Rizzo 
52237e3a6d3SLuigi Rizzo /*
52337e3a6d3SLuigi Rizzo  * We cannot use the native mmap on windows
52437e3a6d3SLuigi Rizzo  * The only parameter used is "fd", the other ones are just declared to
52537e3a6d3SLuigi Rizzo  * make this signature comparable to the FreeBSD/Linux one
52637e3a6d3SLuigi Rizzo  */
52737e3a6d3SLuigi Rizzo static void *
52837e3a6d3SLuigi Rizzo win32_mmap_emulated(void *addr, size_t length, int prot, int flags, int fd, int32_t offset)
52937e3a6d3SLuigi Rizzo {
53037e3a6d3SLuigi Rizzo 	HANDLE h = win_get_netmap_handle(fd);
53137e3a6d3SLuigi Rizzo 
53237e3a6d3SLuigi Rizzo 	if (h == NULL) {
53337e3a6d3SLuigi Rizzo 		return mmap(addr, length, prot, flags, fd, offset);
53437e3a6d3SLuigi Rizzo 	} else {
53537e3a6d3SLuigi Rizzo 		MEMORY_ENTRY ret;
53637e3a6d3SLuigi Rizzo 
53737e3a6d3SLuigi Rizzo 		return win_nm_ioctl_internal(h, NETMAP_MMAP, &ret) ?
53837e3a6d3SLuigi Rizzo 			NULL : ret.pUsermodeVirtualAddress;
53937e3a6d3SLuigi Rizzo 	}
54037e3a6d3SLuigi Rizzo }
54137e3a6d3SLuigi Rizzo 
54237e3a6d3SLuigi Rizzo #define mmap win32_mmap_emulated
54337e3a6d3SLuigi Rizzo 
54437e3a6d3SLuigi Rizzo #include <sys/poll.h> /* XXX needed to use the structure pollfd */
54537e3a6d3SLuigi Rizzo 
54637e3a6d3SLuigi Rizzo static int
54737e3a6d3SLuigi Rizzo win_nm_poll(struct pollfd *fds, int nfds, int timeout)
54837e3a6d3SLuigi Rizzo {
54937e3a6d3SLuigi Rizzo 	HANDLE h;
55037e3a6d3SLuigi Rizzo 
55137e3a6d3SLuigi Rizzo 	if (nfds != 1 || fds == NULL || (h = win_get_netmap_handle(fds->fd)) == NULL) {;
55237e3a6d3SLuigi Rizzo 		return poll(fds, nfds, timeout);
55337e3a6d3SLuigi Rizzo 	} else {
55437e3a6d3SLuigi Rizzo 		POLL_REQUEST_DATA prd;
55537e3a6d3SLuigi Rizzo 
55637e3a6d3SLuigi Rizzo 		prd.timeout = timeout;
55737e3a6d3SLuigi Rizzo 		prd.events = fds->events;
55837e3a6d3SLuigi Rizzo 
55937e3a6d3SLuigi Rizzo 		win_nm_ioctl_internal(h, NETMAP_POLL, &prd);
56037e3a6d3SLuigi Rizzo 		if ((prd.revents == POLLERR) || (prd.revents == STATUS_TIMEOUT)) {
56137e3a6d3SLuigi Rizzo 			return -1;
56237e3a6d3SLuigi Rizzo 		}
56337e3a6d3SLuigi Rizzo 		return 1;
56437e3a6d3SLuigi Rizzo 	}
56537e3a6d3SLuigi Rizzo }
56637e3a6d3SLuigi Rizzo 
56737e3a6d3SLuigi Rizzo #define poll win_nm_poll
56837e3a6d3SLuigi Rizzo 
56937e3a6d3SLuigi Rizzo static int
57037e3a6d3SLuigi Rizzo win_nm_open(char* pathname, int flags)
57137e3a6d3SLuigi Rizzo {
57237e3a6d3SLuigi Rizzo 
57337e3a6d3SLuigi Rizzo 	if (strcmp(pathname, NETMAP_DEVICE_NAME) == 0) {
57437e3a6d3SLuigi Rizzo 		int fd = open(NETMAP_DEVICE_NAME, O_RDWR);
57537e3a6d3SLuigi Rizzo 		if (fd < 0) {
57637e3a6d3SLuigi Rizzo 			return -1;
57737e3a6d3SLuigi Rizzo 		}
57837e3a6d3SLuigi Rizzo 
57937e3a6d3SLuigi Rizzo 		win_insert_fd_record(fd);
58037e3a6d3SLuigi Rizzo 		return fd;
58137e3a6d3SLuigi Rizzo 	} else {
58237e3a6d3SLuigi Rizzo 		return open(pathname, flags);
58337e3a6d3SLuigi Rizzo 	}
58437e3a6d3SLuigi Rizzo }
58537e3a6d3SLuigi Rizzo 
58637e3a6d3SLuigi Rizzo #define open win_nm_open
58737e3a6d3SLuigi Rizzo 
58837e3a6d3SLuigi Rizzo static int
58937e3a6d3SLuigi Rizzo win_nm_close(int fd)
59037e3a6d3SLuigi Rizzo {
59137e3a6d3SLuigi Rizzo 	if (fd != -1) {
59237e3a6d3SLuigi Rizzo 		close(fd);
59337e3a6d3SLuigi Rizzo 		if (win_get_netmap_handle(fd) != NULL) {
59437e3a6d3SLuigi Rizzo 			win_remove_fd_record(fd);
59537e3a6d3SLuigi Rizzo 		}
59637e3a6d3SLuigi Rizzo 	}
59737e3a6d3SLuigi Rizzo 	return 0;
59837e3a6d3SLuigi Rizzo }
59937e3a6d3SLuigi Rizzo 
60037e3a6d3SLuigi Rizzo #define close win_nm_close
60137e3a6d3SLuigi Rizzo 
60237e3a6d3SLuigi Rizzo #endif /* _WIN32 */
60337e3a6d3SLuigi Rizzo 
60437e3a6d3SLuigi Rizzo static int
60537e3a6d3SLuigi Rizzo nm_is_identifier(const char *s, const char *e)
60637e3a6d3SLuigi Rizzo {
60737e3a6d3SLuigi Rizzo 	for (; s != e; s++) {
60837e3a6d3SLuigi Rizzo 		if (!isalnum(*s) && *s != '_') {
60937e3a6d3SLuigi Rizzo 			return 0;
61037e3a6d3SLuigi Rizzo 		}
61137e3a6d3SLuigi Rizzo 	}
61237e3a6d3SLuigi Rizzo 
61337e3a6d3SLuigi Rizzo 	return 1;
61437e3a6d3SLuigi Rizzo }
615f9790aebSLuigi Rizzo 
616*4f80b14cSVincenzo Maffione static void
617*4f80b14cSVincenzo Maffione nm_init_offsets(struct nm_desc *d)
618f9790aebSLuigi Rizzo {
619*4f80b14cSVincenzo Maffione 	struct netmap_if *nifp = NETMAP_IF(d->mem, d->req.nr_offset);
620*4f80b14cSVincenzo Maffione 	struct netmap_ring *r = NETMAP_RXRING(nifp, d->first_rx_ring);
621*4f80b14cSVincenzo Maffione 	if ((void *)r == (void *)nifp) {
622*4f80b14cSVincenzo Maffione 		/* the descriptor is open for TX only */
623*4f80b14cSVincenzo Maffione 		r = NETMAP_TXRING(nifp, d->first_tx_ring);
624*4f80b14cSVincenzo Maffione 	}
625*4f80b14cSVincenzo Maffione 
626*4f80b14cSVincenzo Maffione 	*(struct netmap_if **)(uintptr_t)&(d->nifp) = nifp;
627*4f80b14cSVincenzo Maffione 	*(struct netmap_ring **)(uintptr_t)&d->some_ring = r;
628*4f80b14cSVincenzo Maffione 	*(void **)(uintptr_t)&d->buf_start = NETMAP_BUF(r, 0);
629*4f80b14cSVincenzo Maffione 	*(void **)(uintptr_t)&d->buf_end =
630*4f80b14cSVincenzo Maffione 		(char *)d->mem + d->memsize;
631*4f80b14cSVincenzo Maffione }
632*4f80b14cSVincenzo Maffione 
633*4f80b14cSVincenzo Maffione #define MAXERRMSG 80
634*4f80b14cSVincenzo Maffione #define NM_PARSE_OK	  	0
635*4f80b14cSVincenzo Maffione #define NM_PARSE_MEMID 		1
636*4f80b14cSVincenzo Maffione static int
637*4f80b14cSVincenzo Maffione nm_parse_one(const char *ifname, struct nmreq *d, char **out, int memid_allowed)
638*4f80b14cSVincenzo Maffione {
639*4f80b14cSVincenzo Maffione 	int is_vale;
640f0ea3689SLuigi Rizzo 	const char *port = NULL;
64137e3a6d3SLuigi Rizzo 	const char *vpname = NULL;
642*4f80b14cSVincenzo Maffione 	u_int namelen;
643*4f80b14cSVincenzo Maffione 	uint32_t nr_ringid = 0, nr_flags;
644847bf383SLuigi Rizzo 	char errmsg[MAXERRMSG] = "";
645847bf383SLuigi Rizzo 	long num;
646f9eece46SLuiz Otavio O Souza 	uint16_t nr_arg2 = 0;
647*4f80b14cSVincenzo Maffione 	enum { P_START, P_RNGSFXOK, P_GETNUM, P_FLAGS, P_FLAGSOK, P_MEMID } p_state;
648*4f80b14cSVincenzo Maffione 
649*4f80b14cSVincenzo Maffione 	errno = 0;
650f9790aebSLuigi Rizzo 
65137e3a6d3SLuigi Rizzo 	if (strncmp(ifname, "netmap:", 7) &&
65237e3a6d3SLuigi Rizzo 			strncmp(ifname, NM_BDG_NAME, strlen(NM_BDG_NAME))) {
653*4f80b14cSVincenzo Maffione 		snprintf(errmsg, MAXERRMSG, "invalid port name: %s", ifname);
654*4f80b14cSVincenzo Maffione 		errno = EINVAL;
655*4f80b14cSVincenzo Maffione 		goto fail;
656f9790aebSLuigi Rizzo 	}
65737e3a6d3SLuigi Rizzo 
65837e3a6d3SLuigi Rizzo 	is_vale = (ifname[0] == 'v');
65937e3a6d3SLuigi Rizzo 	if (is_vale) {
66037e3a6d3SLuigi Rizzo 		port = index(ifname, ':');
66137e3a6d3SLuigi Rizzo 		if (port == NULL) {
66237e3a6d3SLuigi Rizzo 			snprintf(errmsg, MAXERRMSG,
66337e3a6d3SLuigi Rizzo 				 "missing ':' in vale name");
66437e3a6d3SLuigi Rizzo 			goto fail;
66537e3a6d3SLuigi Rizzo 		}
66637e3a6d3SLuigi Rizzo 
66737e3a6d3SLuigi Rizzo 		if (!nm_is_identifier(ifname + 4, port)) {
66837e3a6d3SLuigi Rizzo 			snprintf(errmsg, MAXERRMSG, "invalid bridge name");
66937e3a6d3SLuigi Rizzo 			goto fail;
67037e3a6d3SLuigi Rizzo 		}
67137e3a6d3SLuigi Rizzo 
67237e3a6d3SLuigi Rizzo 		vpname = ++port;
67337e3a6d3SLuigi Rizzo 	} else {
674f9790aebSLuigi Rizzo 		ifname += 7;
67537e3a6d3SLuigi Rizzo 		port = ifname;
67637e3a6d3SLuigi Rizzo 	}
67737e3a6d3SLuigi Rizzo 
678f0ea3689SLuigi Rizzo 	/* scan for a separator */
679f9eece46SLuiz Otavio O Souza 	for (; *port && !index("-*^{}/@", *port); port++)
680f0ea3689SLuigi Rizzo 		;
68137e3a6d3SLuigi Rizzo 
68237e3a6d3SLuigi Rizzo 	if (is_vale && !nm_is_identifier(vpname, port)) {
68337e3a6d3SLuigi Rizzo 		snprintf(errmsg, MAXERRMSG, "invalid bridge port name");
68437e3a6d3SLuigi Rizzo 		goto fail;
68537e3a6d3SLuigi Rizzo 	}
68637e3a6d3SLuigi Rizzo 
68766015019SLuigi Rizzo 	namelen = port - ifname;
688*4f80b14cSVincenzo Maffione 	if (namelen >= sizeof(d->nr_name)) {
689847bf383SLuigi Rizzo 		snprintf(errmsg, MAXERRMSG, "name too long");
690f0ea3689SLuigi Rizzo 		goto fail;
69166015019SLuigi Rizzo 	}
692*4f80b14cSVincenzo Maffione 	memcpy(d->nr_name, ifname, namelen);
693*4f80b14cSVincenzo Maffione 	d->nr_name[namelen] = '\0';
694*4f80b14cSVincenzo Maffione 	D("name %s", d->nr_name);
695*4f80b14cSVincenzo Maffione 
696847bf383SLuigi Rizzo 	p_state = P_START;
697847bf383SLuigi Rizzo 	nr_flags = NR_REG_ALL_NIC; /* default for no suffix */
698847bf383SLuigi Rizzo 	while (*port) {
699847bf383SLuigi Rizzo 		switch (p_state) {
700847bf383SLuigi Rizzo 		case P_START:
701f0ea3689SLuigi Rizzo 			switch (*port) {
702847bf383SLuigi Rizzo 			case '^': /* only SW ring */
703f0ea3689SLuigi Rizzo 				nr_flags = NR_REG_SW;
704847bf383SLuigi Rizzo 				p_state = P_RNGSFXOK;
705f0ea3689SLuigi Rizzo 				break;
706847bf383SLuigi Rizzo 			case '*': /* NIC and SW */
707847bf383SLuigi Rizzo 				nr_flags = NR_REG_NIC_SW;
708847bf383SLuigi Rizzo 				p_state = P_RNGSFXOK;
709847bf383SLuigi Rizzo 				break;
710847bf383SLuigi Rizzo 			case '-': /* one NIC ring pair */
711847bf383SLuigi Rizzo 				nr_flags = NR_REG_ONE_NIC;
712847bf383SLuigi Rizzo 				p_state = P_GETNUM;
713847bf383SLuigi Rizzo 				break;
714847bf383SLuigi Rizzo 			case '{': /* pipe (master endpoint) */
715f0ea3689SLuigi Rizzo 				nr_flags = NR_REG_PIPE_MASTER;
716847bf383SLuigi Rizzo 				p_state = P_GETNUM;
717f0ea3689SLuigi Rizzo 				break;
718847bf383SLuigi Rizzo 			case '}': /* pipe (slave endoint) */
719f0ea3689SLuigi Rizzo 				nr_flags = NR_REG_PIPE_SLAVE;
720847bf383SLuigi Rizzo 				p_state = P_GETNUM;
721f0ea3689SLuigi Rizzo 				break;
722847bf383SLuigi Rizzo 			case '/': /* start of flags */
723847bf383SLuigi Rizzo 				p_state = P_FLAGS;
724847bf383SLuigi Rizzo 				break;
725f9eece46SLuiz Otavio O Souza 			case '@': /* start of memid */
726f9eece46SLuiz Otavio O Souza 				p_state = P_MEMID;
727f9eece46SLuiz Otavio O Souza 				break;
728847bf383SLuigi Rizzo 			default:
729847bf383SLuigi Rizzo 				snprintf(errmsg, MAXERRMSG, "unknown modifier: '%c'", *port);
730f0ea3689SLuigi Rizzo 				goto fail;
731f0ea3689SLuigi Rizzo 			}
732847bf383SLuigi Rizzo 			port++;
733847bf383SLuigi Rizzo 			break;
734847bf383SLuigi Rizzo 		case P_RNGSFXOK:
735847bf383SLuigi Rizzo 			switch (*port) {
736847bf383SLuigi Rizzo 			case '/':
737847bf383SLuigi Rizzo 				p_state = P_FLAGS;
738847bf383SLuigi Rizzo 				break;
739f9eece46SLuiz Otavio O Souza 			case '@':
740f9eece46SLuiz Otavio O Souza 				p_state = P_MEMID;
741f9eece46SLuiz Otavio O Souza 				break;
742847bf383SLuigi Rizzo 			default:
743847bf383SLuigi Rizzo 				snprintf(errmsg, MAXERRMSG, "unexpected character: '%c'", *port);
744847bf383SLuigi Rizzo 				goto fail;
745847bf383SLuigi Rizzo 			}
746847bf383SLuigi Rizzo 			port++;
747847bf383SLuigi Rizzo 			break;
748847bf383SLuigi Rizzo 		case P_GETNUM:
749847bf383SLuigi Rizzo 			num = strtol(port, (char **)&port, 10);
750847bf383SLuigi Rizzo 			if (num < 0 || num >= NETMAP_RING_MASK) {
751847bf383SLuigi Rizzo 				snprintf(errmsg, MAXERRMSG, "'%ld' out of range [0, %d)",
752847bf383SLuigi Rizzo 						num, NETMAP_RING_MASK);
753847bf383SLuigi Rizzo 				goto fail;
754847bf383SLuigi Rizzo 			}
755847bf383SLuigi Rizzo 			nr_ringid = num & NETMAP_RING_MASK;
756847bf383SLuigi Rizzo 			p_state = P_RNGSFXOK;
757847bf383SLuigi Rizzo 			break;
758847bf383SLuigi Rizzo 		case P_FLAGS:
759847bf383SLuigi Rizzo 		case P_FLAGSOK:
760f9eece46SLuiz Otavio O Souza 			if (*port == '@') {
761f9eece46SLuiz Otavio O Souza 				port++;
762f9eece46SLuiz Otavio O Souza 				p_state = P_MEMID;
763f9eece46SLuiz Otavio O Souza 				break;
764f9eece46SLuiz Otavio O Souza 			}
765847bf383SLuigi Rizzo 			switch (*port) {
766847bf383SLuigi Rizzo 			case 'x':
767847bf383SLuigi Rizzo 				nr_flags |= NR_EXCLUSIVE;
768847bf383SLuigi Rizzo 				break;
769847bf383SLuigi Rizzo 			case 'z':
770847bf383SLuigi Rizzo 				nr_flags |= NR_ZCOPY_MON;
771847bf383SLuigi Rizzo 				break;
772847bf383SLuigi Rizzo 			case 't':
773847bf383SLuigi Rizzo 				nr_flags |= NR_MONITOR_TX;
774847bf383SLuigi Rizzo 				break;
775847bf383SLuigi Rizzo 			case 'r':
776847bf383SLuigi Rizzo 				nr_flags |= NR_MONITOR_RX;
777847bf383SLuigi Rizzo 				break;
77837e3a6d3SLuigi Rizzo 			case 'R':
77937e3a6d3SLuigi Rizzo 				nr_flags |= NR_RX_RINGS_ONLY;
78037e3a6d3SLuigi Rizzo 				break;
78137e3a6d3SLuigi Rizzo 			case 'T':
78237e3a6d3SLuigi Rizzo 				nr_flags |= NR_TX_RINGS_ONLY;
78337e3a6d3SLuigi Rizzo 				break;
784847bf383SLuigi Rizzo 			default:
785847bf383SLuigi Rizzo 				snprintf(errmsg, MAXERRMSG, "unrecognized flag: '%c'", *port);
786847bf383SLuigi Rizzo 				goto fail;
787847bf383SLuigi Rizzo 			}
788847bf383SLuigi Rizzo 			port++;
789847bf383SLuigi Rizzo 			p_state = P_FLAGSOK;
790847bf383SLuigi Rizzo 			break;
791f9eece46SLuiz Otavio O Souza 		case P_MEMID:
792*4f80b14cSVincenzo Maffione 			if (!memid_allowed) {
793f9eece46SLuiz Otavio O Souza 				snprintf(errmsg, MAXERRMSG, "double setting of memid");
794f9eece46SLuiz Otavio O Souza 				goto fail;
795f9eece46SLuiz Otavio O Souza 			}
796f9eece46SLuiz Otavio O Souza 			num = strtol(port, (char **)&port, 10);
797f9eece46SLuiz Otavio O Souza 			if (num <= 0) {
798*4f80b14cSVincenzo Maffione 				ND("non-numeric memid %s (out = %p)", port, out);
799*4f80b14cSVincenzo Maffione 				if (out == NULL)
800f9eece46SLuiz Otavio O Souza 					goto fail;
801*4f80b14cSVincenzo Maffione 				*out = (char *)port;
802*4f80b14cSVincenzo Maffione 				while (*port)
803*4f80b14cSVincenzo Maffione 					port++;
804*4f80b14cSVincenzo Maffione 			} else {
805f9eece46SLuiz Otavio O Souza 				nr_arg2 = num;
806*4f80b14cSVincenzo Maffione 				memid_allowed = 0;
807f9eece46SLuiz Otavio O Souza 				p_state = P_RNGSFXOK;
808*4f80b14cSVincenzo Maffione 			}
809f9eece46SLuiz Otavio O Souza 			break;
810847bf383SLuigi Rizzo 		}
811847bf383SLuigi Rizzo 	}
812*4f80b14cSVincenzo Maffione 	if (p_state != P_START && p_state != P_RNGSFXOK &&
813*4f80b14cSVincenzo Maffione 	    p_state != P_FLAGSOK && p_state != P_MEMID) {
814847bf383SLuigi Rizzo 		snprintf(errmsg, MAXERRMSG, "unexpected end of port name");
815847bf383SLuigi Rizzo 		goto fail;
816847bf383SLuigi Rizzo 	}
817847bf383SLuigi Rizzo 	ND("flags: %s %s %s %s",
818847bf383SLuigi Rizzo 			(nr_flags & NR_EXCLUSIVE) ? "EXCLUSIVE" : "",
819847bf383SLuigi Rizzo 			(nr_flags & NR_ZCOPY_MON) ? "ZCOPY_MON" : "",
820847bf383SLuigi Rizzo 			(nr_flags & NR_MONITOR_TX) ? "MONITOR_TX" : "",
821847bf383SLuigi Rizzo 			(nr_flags & NR_MONITOR_RX) ? "MONITOR_RX" : "");
822*4f80b14cSVincenzo Maffione 
823*4f80b14cSVincenzo Maffione 	d->nr_flags |= nr_flags;
824*4f80b14cSVincenzo Maffione 	d->nr_ringid |= nr_ringid;
825*4f80b14cSVincenzo Maffione 	d->nr_arg2 = nr_arg2;
826*4f80b14cSVincenzo Maffione 
827*4f80b14cSVincenzo Maffione 	return (p_state == P_MEMID) ? NM_PARSE_MEMID : NM_PARSE_OK;
828*4f80b14cSVincenzo Maffione fail:
829*4f80b14cSVincenzo Maffione 	if (!errno)
830*4f80b14cSVincenzo Maffione 		errno = EINVAL;
831*4f80b14cSVincenzo Maffione 	if (out)
832*4f80b14cSVincenzo Maffione 		*out = strdup(errmsg);
833*4f80b14cSVincenzo Maffione 	return -1;
834*4f80b14cSVincenzo Maffione }
835*4f80b14cSVincenzo Maffione 
836*4f80b14cSVincenzo Maffione static int
837*4f80b14cSVincenzo Maffione nm_interp_memid(const char *memid, struct nmreq *req, char **err)
838*4f80b14cSVincenzo Maffione {
839*4f80b14cSVincenzo Maffione 	int fd = -1;
840*4f80b14cSVincenzo Maffione 	char errmsg[MAXERRMSG] = "";
841*4f80b14cSVincenzo Maffione 	struct nmreq greq;
842*4f80b14cSVincenzo Maffione 	off_t mapsize;
843*4f80b14cSVincenzo Maffione 	struct netmap_pools_info *pi;
844*4f80b14cSVincenzo Maffione 
845*4f80b14cSVincenzo Maffione 	/* first, try to look for a netmap port with this name */
846*4f80b14cSVincenzo Maffione 	fd = open("/dev/netmap", O_RDONLY);
847*4f80b14cSVincenzo Maffione 	if (fd < 0) {
848*4f80b14cSVincenzo Maffione 		snprintf(errmsg, MAXERRMSG, "cannot open /dev/netmap: %s", strerror(errno));
849*4f80b14cSVincenzo Maffione 		goto fail;
850*4f80b14cSVincenzo Maffione 	}
851*4f80b14cSVincenzo Maffione 	memset(&greq, 0, sizeof(greq));
852*4f80b14cSVincenzo Maffione 	if (nm_parse_one(memid, &greq, err, 0) == NM_PARSE_OK) {
853*4f80b14cSVincenzo Maffione 		greq.nr_version = NETMAP_API;
854*4f80b14cSVincenzo Maffione 		if (ioctl(fd, NIOCGINFO, &greq) < 0) {
855*4f80b14cSVincenzo Maffione 			if (errno == ENOENT || errno == ENXIO)
856*4f80b14cSVincenzo Maffione 				goto try_external;
857*4f80b14cSVincenzo Maffione 			snprintf(errmsg, MAXERRMSG, "cannot getinfo for %s: %s", memid, strerror(errno));
858*4f80b14cSVincenzo Maffione 			goto fail;
859*4f80b14cSVincenzo Maffione 		}
860*4f80b14cSVincenzo Maffione 		req->nr_arg2 = greq.nr_arg2;
861*4f80b14cSVincenzo Maffione 		close(fd);
862*4f80b14cSVincenzo Maffione 		return 0;
863*4f80b14cSVincenzo Maffione 	}
864*4f80b14cSVincenzo Maffione try_external:
865*4f80b14cSVincenzo Maffione 	D("trying with external memory");
866*4f80b14cSVincenzo Maffione 	close(fd);
867*4f80b14cSVincenzo Maffione 	fd = open(memid, O_RDWR);
868*4f80b14cSVincenzo Maffione 	if (fd < 0) {
869*4f80b14cSVincenzo Maffione 		snprintf(errmsg, MAXERRMSG, "cannot open %s: %s", memid, strerror(errno));
870*4f80b14cSVincenzo Maffione 		goto fail;
871*4f80b14cSVincenzo Maffione 	}
872*4f80b14cSVincenzo Maffione 	mapsize = lseek(fd, 0, SEEK_END);
873*4f80b14cSVincenzo Maffione 	if (mapsize < 0) {
874*4f80b14cSVincenzo Maffione 		snprintf(errmsg, MAXERRMSG, "failed to obtain filesize of %s: %s", memid, strerror(errno));
875*4f80b14cSVincenzo Maffione 		goto fail;
876*4f80b14cSVincenzo Maffione 	}
877*4f80b14cSVincenzo Maffione 	pi = mmap(0, mapsize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
878*4f80b14cSVincenzo Maffione 	if (pi == MAP_FAILED) {
879*4f80b14cSVincenzo Maffione 		snprintf(errmsg, MAXERRMSG, "cannot map %s: %s", memid, strerror(errno));
880*4f80b14cSVincenzo Maffione 		goto fail;
881*4f80b14cSVincenzo Maffione 	}
882*4f80b14cSVincenzo Maffione 	req->nr_cmd = NETMAP_POOLS_CREATE;
883*4f80b14cSVincenzo Maffione 	pi->memsize = mapsize;
884*4f80b14cSVincenzo Maffione 	nmreq_pointer_put(req, pi);
885*4f80b14cSVincenzo Maffione 	D("mapped %zu bytes at %p from file %s", mapsize, pi, memid);
886*4f80b14cSVincenzo Maffione 	return 0;
887*4f80b14cSVincenzo Maffione 
888*4f80b14cSVincenzo Maffione fail:
889*4f80b14cSVincenzo Maffione 	D("%s", errmsg);
890*4f80b14cSVincenzo Maffione 	close(fd);
891*4f80b14cSVincenzo Maffione 	if (err && !*err)
892*4f80b14cSVincenzo Maffione 		*err = strdup(errmsg);
893*4f80b14cSVincenzo Maffione 	return errno;
894*4f80b14cSVincenzo Maffione }
895*4f80b14cSVincenzo Maffione 
896*4f80b14cSVincenzo Maffione static int
897*4f80b14cSVincenzo Maffione nm_parse(const char *ifname, struct nm_desc *d, char *errmsg)
898*4f80b14cSVincenzo Maffione {
899*4f80b14cSVincenzo Maffione 	char *err;
900*4f80b14cSVincenzo Maffione 	switch (nm_parse_one(ifname, &d->req, &err, 1)) {
901*4f80b14cSVincenzo Maffione 	case NM_PARSE_OK:
902*4f80b14cSVincenzo Maffione 		D("parse OK");
903*4f80b14cSVincenzo Maffione 		break;
904*4f80b14cSVincenzo Maffione 	case NM_PARSE_MEMID:
905*4f80b14cSVincenzo Maffione 		D("memid: %s", err);
906*4f80b14cSVincenzo Maffione 		errno = nm_interp_memid(err, &d->req, &err);
907*4f80b14cSVincenzo Maffione 		D("errno = %d", errno);
908*4f80b14cSVincenzo Maffione 		if (!errno)
909*4f80b14cSVincenzo Maffione 			break;
910*4f80b14cSVincenzo Maffione 		/* fallthrough */
911*4f80b14cSVincenzo Maffione 	default:
912*4f80b14cSVincenzo Maffione 		D("error");
913*4f80b14cSVincenzo Maffione 		strncpy(errmsg, err, MAXERRMSG);
914*4f80b14cSVincenzo Maffione 		errmsg[MAXERRMSG-1] = '\0';
915*4f80b14cSVincenzo Maffione 		free(err);
916*4f80b14cSVincenzo Maffione 		return -1;
917*4f80b14cSVincenzo Maffione 	}
918*4f80b14cSVincenzo Maffione 	D("parsed name: %s", d->req.nr_name);
919*4f80b14cSVincenzo Maffione 	d->self = d;
920*4f80b14cSVincenzo Maffione 	return 0;
921*4f80b14cSVincenzo Maffione }
922*4f80b14cSVincenzo Maffione 
923*4f80b14cSVincenzo Maffione /*
924*4f80b14cSVincenzo Maffione  * Try to open, return descriptor if successful, NULL otherwise.
925*4f80b14cSVincenzo Maffione  * An invalid netmap name will return errno = 0;
926*4f80b14cSVincenzo Maffione  * You can pass a pointer to a pre-filled nm_desc to add special
927*4f80b14cSVincenzo Maffione  * parameters. Flags is used as follows
928*4f80b14cSVincenzo Maffione  * NM_OPEN_NO_MMAP	use the memory from arg, only
929*4f80b14cSVincenzo Maffione  *			if the nr_arg2 (memory block) matches.
930*4f80b14cSVincenzo Maffione  *			Special case: if arg is NULL, skip the
931*4f80b14cSVincenzo Maffione  *			mmap entirely (maybe because you are going
932*4f80b14cSVincenzo Maffione  *			to do it by yourself, or you plan to call
933*4f80b14cSVincenzo Maffione  *			nm_mmap() only later)
934*4f80b14cSVincenzo Maffione  * NM_OPEN_ARG1		use req.nr_arg1 from arg
935*4f80b14cSVincenzo Maffione  * NM_OPEN_ARG2		use req.nr_arg2 from arg
936*4f80b14cSVincenzo Maffione  * NM_OPEN_RING_CFG	user ring config from arg
937*4f80b14cSVincenzo Maffione  */
938*4f80b14cSVincenzo Maffione static struct nm_desc *
939*4f80b14cSVincenzo Maffione nm_open(const char *ifname, const struct nmreq *req,
940*4f80b14cSVincenzo Maffione 	uint64_t new_flags, const struct nm_desc *arg)
941*4f80b14cSVincenzo Maffione {
942*4f80b14cSVincenzo Maffione 	struct nm_desc *d = NULL;
943*4f80b14cSVincenzo Maffione 	const struct nm_desc *parent = arg;
944*4f80b14cSVincenzo Maffione 	char errmsg[MAXERRMSG] = "";
945*4f80b14cSVincenzo Maffione 	uint32_t nr_reg;
946*4f80b14cSVincenzo Maffione 	struct netmap_pools_info *pi = NULL;
947*4f80b14cSVincenzo Maffione 
948*4f80b14cSVincenzo Maffione 	if (strncmp(ifname, "netmap:", 7) &&
949*4f80b14cSVincenzo Maffione 			strncmp(ifname, NM_BDG_NAME, strlen(NM_BDG_NAME))) {
950*4f80b14cSVincenzo Maffione 		errno = 0; /* name not recognised, not an error */
951*4f80b14cSVincenzo Maffione 		return NULL;
952*4f80b14cSVincenzo Maffione 	}
953*4f80b14cSVincenzo Maffione 
954f0ea3689SLuigi Rizzo 	d = (struct nm_desc *)calloc(1, sizeof(*d));
955f9790aebSLuigi Rizzo 	if (d == NULL) {
956847bf383SLuigi Rizzo 		snprintf(errmsg, MAXERRMSG, "nm_desc alloc failure");
957f9790aebSLuigi Rizzo 		errno = ENOMEM;
958f9790aebSLuigi Rizzo 		return NULL;
959f9790aebSLuigi Rizzo 	}
960f9790aebSLuigi Rizzo 	d->self = d;	/* set this early so nm_close() works */
96137e3a6d3SLuigi Rizzo 	d->fd = open(NETMAP_DEVICE_NAME, O_RDWR);
962f0ea3689SLuigi Rizzo 	if (d->fd < 0) {
963847bf383SLuigi Rizzo 		snprintf(errmsg, MAXERRMSG, "cannot open /dev/netmap: %s", strerror(errno));
964f9790aebSLuigi Rizzo 		goto fail;
965f9790aebSLuigi Rizzo 	}
966f0ea3689SLuigi Rizzo 
967*4f80b14cSVincenzo Maffione 	if (req) {
968f0ea3689SLuigi Rizzo 		d->req = *req;
969*4f80b14cSVincenzo Maffione 	} else {
970*4f80b14cSVincenzo Maffione 		d->req.nr_arg1 = 4;
971*4f80b14cSVincenzo Maffione 		d->req.nr_arg2 = 0;
972*4f80b14cSVincenzo Maffione 		d->req.nr_arg3 = 0;
973*4f80b14cSVincenzo Maffione 	}
974f0ea3689SLuigi Rizzo 
975*4f80b14cSVincenzo Maffione 	if (!(new_flags & NM_OPEN_IFNAME)) {
976*4f80b14cSVincenzo Maffione 		char *err;
977*4f80b14cSVincenzo Maffione 		switch (nm_parse_one(ifname, &d->req, &err, 1)) {
978*4f80b14cSVincenzo Maffione 		case NM_PARSE_OK:
979*4f80b14cSVincenzo Maffione 			break;
980*4f80b14cSVincenzo Maffione 		case NM_PARSE_MEMID:
981*4f80b14cSVincenzo Maffione 			if ((new_flags & NM_OPEN_NO_MMAP) &&
982*4f80b14cSVincenzo Maffione 					IS_NETMAP_DESC(parent)) {
983*4f80b14cSVincenzo Maffione 				/* ignore the memid setting, since we are
984*4f80b14cSVincenzo Maffione 				 * going to use the parent's one
985*4f80b14cSVincenzo Maffione 				 */
986*4f80b14cSVincenzo Maffione 				break;
987*4f80b14cSVincenzo Maffione 			}
988*4f80b14cSVincenzo Maffione 			errno = nm_interp_memid(err, &d->req, &err);
989*4f80b14cSVincenzo Maffione 			if (!errno)
990*4f80b14cSVincenzo Maffione 				break;
991*4f80b14cSVincenzo Maffione 			/* fallthrough */
992*4f80b14cSVincenzo Maffione 		default:
993*4f80b14cSVincenzo Maffione 			strncpy(errmsg, err, MAXERRMSG);
994*4f80b14cSVincenzo Maffione 			errmsg[MAXERRMSG-1] = '\0';
995*4f80b14cSVincenzo Maffione 			free(err);
996*4f80b14cSVincenzo Maffione 			goto fail;
997*4f80b14cSVincenzo Maffione 		}
998*4f80b14cSVincenzo Maffione 		d->self = d;
999*4f80b14cSVincenzo Maffione 	}
1000*4f80b14cSVincenzo Maffione 
1001*4f80b14cSVincenzo Maffione 	/* compatibility checks for POOL_SCREATE and NM_OPEN flags
1002*4f80b14cSVincenzo Maffione 	 * the first check may be dropped once we have a larger nreq
1003*4f80b14cSVincenzo Maffione 	 */
1004*4f80b14cSVincenzo Maffione 	if (d->req.nr_cmd == NETMAP_POOLS_CREATE) {
1005*4f80b14cSVincenzo Maffione 		if (IS_NETMAP_DESC(parent)) {
1006*4f80b14cSVincenzo Maffione 		       	if (new_flags & (NM_OPEN_ARG1 | NM_OPEN_ARG2 | NM_OPEN_ARG3)) {
1007*4f80b14cSVincenzo Maffione 				snprintf(errmsg, MAXERRMSG,
1008*4f80b14cSVincenzo Maffione 						"POOLS_CREATE is incompatibile "
1009*4f80b14cSVincenzo Maffione 						"with NM_OPEN_ARG? flags");
1010*4f80b14cSVincenzo Maffione 				errno = EINVAL;
1011*4f80b14cSVincenzo Maffione 				goto fail;
1012*4f80b14cSVincenzo Maffione 			}
1013*4f80b14cSVincenzo Maffione 			if (new_flags & NM_OPEN_NO_MMAP) {
1014*4f80b14cSVincenzo Maffione 				snprintf(errmsg, MAXERRMSG,
1015*4f80b14cSVincenzo Maffione 						"POOLS_CREATE is incompatible "
1016*4f80b14cSVincenzo Maffione 						"with NM_OPEN_NO_MMAP flag");
1017*4f80b14cSVincenzo Maffione 				errno = EINVAL;
1018*4f80b14cSVincenzo Maffione 				goto fail;
1019*4f80b14cSVincenzo Maffione 			}
1020*4f80b14cSVincenzo Maffione 		}
1021*4f80b14cSVincenzo Maffione 	}
1022*4f80b14cSVincenzo Maffione 
1023*4f80b14cSVincenzo Maffione 	d->req.nr_version = NETMAP_API;
1024*4f80b14cSVincenzo Maffione 	d->req.nr_ringid &= NETMAP_RING_MASK;
1025*4f80b14cSVincenzo Maffione 
1026f0ea3689SLuigi Rizzo 	/* optionally import info from parent */
1027f0ea3689SLuigi Rizzo 	if (IS_NETMAP_DESC(parent) && new_flags) {
1028*4f80b14cSVincenzo Maffione 		if (new_flags & NM_OPEN_EXTMEM) {
1029*4f80b14cSVincenzo Maffione 			if (parent->req.nr_cmd == NETMAP_POOLS_CREATE) {
1030*4f80b14cSVincenzo Maffione 				d->req.nr_cmd = NETMAP_POOLS_CREATE;
1031*4f80b14cSVincenzo Maffione 				nmreq_pointer_put(&d->req, nmreq_pointer_get(&parent->req));
1032*4f80b14cSVincenzo Maffione 				D("Warning: not overriding arg[1-3] since external memory is being used");
1033*4f80b14cSVincenzo Maffione 				new_flags &= ~(NM_OPEN_ARG1 | NM_OPEN_ARG2 | NM_OPEN_ARG3);
1034*4f80b14cSVincenzo Maffione 			}
1035*4f80b14cSVincenzo Maffione 		}
1036*4f80b14cSVincenzo Maffione 		if (new_flags & NM_OPEN_ARG1) {
1037f0ea3689SLuigi Rizzo 			D("overriding ARG1 %d", parent->req.nr_arg1);
1038*4f80b14cSVincenzo Maffione 			d->req.nr_arg1 = parent->req.nr_arg1;
1039*4f80b14cSVincenzo Maffione 		}
1040*4f80b14cSVincenzo Maffione 		if (new_flags & (NM_OPEN_ARG2 | NM_OPEN_NO_MMAP)) {
1041f0ea3689SLuigi Rizzo 			D("overriding ARG2 %d", parent->req.nr_arg2);
1042*4f80b14cSVincenzo Maffione 			d->req.nr_arg2 = parent->req.nr_arg2;
1043*4f80b14cSVincenzo Maffione 		}
1044*4f80b14cSVincenzo Maffione 		if (new_flags & NM_OPEN_ARG3) {
1045f0ea3689SLuigi Rizzo 			D("overriding ARG3 %d", parent->req.nr_arg3);
1046*4f80b14cSVincenzo Maffione 			d->req.nr_arg3 = parent->req.nr_arg3;
1047*4f80b14cSVincenzo Maffione 		}
1048f0ea3689SLuigi Rizzo 		if (new_flags & NM_OPEN_RING_CFG) {
1049f0ea3689SLuigi Rizzo 			D("overriding RING_CFG");
1050f0ea3689SLuigi Rizzo 			d->req.nr_tx_slots = parent->req.nr_tx_slots;
1051f0ea3689SLuigi Rizzo 			d->req.nr_rx_slots = parent->req.nr_rx_slots;
1052f0ea3689SLuigi Rizzo 			d->req.nr_tx_rings = parent->req.nr_tx_rings;
1053f0ea3689SLuigi Rizzo 			d->req.nr_rx_rings = parent->req.nr_rx_rings;
1054f0ea3689SLuigi Rizzo 		}
1055f0ea3689SLuigi Rizzo 		if (new_flags & NM_OPEN_IFNAME) {
1056f0ea3689SLuigi Rizzo 			D("overriding ifname %s ringid 0x%x flags 0x%x",
1057f0ea3689SLuigi Rizzo 				parent->req.nr_name, parent->req.nr_ringid,
1058f0ea3689SLuigi Rizzo 				parent->req.nr_flags);
1059f0ea3689SLuigi Rizzo 			memcpy(d->req.nr_name, parent->req.nr_name,
1060f0ea3689SLuigi Rizzo 				sizeof(d->req.nr_name));
1061f0ea3689SLuigi Rizzo 			d->req.nr_ringid = parent->req.nr_ringid;
1062f0ea3689SLuigi Rizzo 			d->req.nr_flags = parent->req.nr_flags;
1063f0ea3689SLuigi Rizzo 		}
1064f0ea3689SLuigi Rizzo 	}
10659225c808SLuigi Rizzo 	/* add the *XPOLL flags */
10669225c808SLuigi Rizzo 	d->req.nr_ringid |= new_flags & (NETMAP_NO_TX_POLL | NETMAP_DO_RX_POLL);
10679225c808SLuigi Rizzo 
1068*4f80b14cSVincenzo Maffione 	if (d->req.nr_cmd == NETMAP_POOLS_CREATE) {
1069*4f80b14cSVincenzo Maffione 		pi = nmreq_pointer_get(&d->req);
1070*4f80b14cSVincenzo Maffione 	}
1071*4f80b14cSVincenzo Maffione 
107266015019SLuigi Rizzo 	if (ioctl(d->fd, NIOCREGIF, &d->req)) {
1073847bf383SLuigi Rizzo 		snprintf(errmsg, MAXERRMSG, "NIOCREGIF failed: %s", strerror(errno));
1074f9790aebSLuigi Rizzo 		goto fail;
107566015019SLuigi Rizzo 	}
1076f9790aebSLuigi Rizzo 
1077*4f80b14cSVincenzo Maffione 	if (pi != NULL) {
1078*4f80b14cSVincenzo Maffione 		d->mem = pi;
1079*4f80b14cSVincenzo Maffione 		d->memsize = pi->memsize;
1080*4f80b14cSVincenzo Maffione 		nm_init_offsets(d);
1081*4f80b14cSVincenzo Maffione 	} else if ((!(new_flags & NM_OPEN_NO_MMAP) || parent)) {
108237e3a6d3SLuigi Rizzo 		/* if parent is defined, do nm_mmap() even if NM_OPEN_NO_MMAP is set */
1083*4f80b14cSVincenzo Maffione 	        errno = nm_mmap(d, parent);
1084*4f80b14cSVincenzo Maffione 		if (errno) {
1085847bf383SLuigi Rizzo 			snprintf(errmsg, MAXERRMSG, "mmap failed: %s", strerror(errno));
1086f9790aebSLuigi Rizzo 			goto fail;
1087f0ea3689SLuigi Rizzo 		}
1088*4f80b14cSVincenzo Maffione 	}
1089f0ea3689SLuigi Rizzo 
1090847bf383SLuigi Rizzo 	nr_reg = d->req.nr_flags & NR_REG_MASK;
1091847bf383SLuigi Rizzo 
1092847bf383SLuigi Rizzo 	if (nr_reg == NR_REG_SW) { /* host stack */
109366015019SLuigi Rizzo 		d->first_tx_ring = d->last_tx_ring = d->req.nr_tx_rings;
109466015019SLuigi Rizzo 		d->first_rx_ring = d->last_rx_ring = d->req.nr_rx_rings;
1095847bf383SLuigi Rizzo 	} else if (nr_reg ==  NR_REG_ALL_NIC) { /* only nic */
1096f0ea3689SLuigi Rizzo 		d->first_tx_ring = 0;
1097f0ea3689SLuigi Rizzo 		d->first_rx_ring = 0;
109866015019SLuigi Rizzo 		d->last_tx_ring = d->req.nr_tx_rings - 1;
109966015019SLuigi Rizzo 		d->last_rx_ring = d->req.nr_rx_rings - 1;
1100847bf383SLuigi Rizzo 	} else if (nr_reg ==  NR_REG_NIC_SW) {
1101f0ea3689SLuigi Rizzo 		d->first_tx_ring = 0;
1102f0ea3689SLuigi Rizzo 		d->first_rx_ring = 0;
1103f0ea3689SLuigi Rizzo 		d->last_tx_ring = d->req.nr_tx_rings;
1104f0ea3689SLuigi Rizzo 		d->last_rx_ring = d->req.nr_rx_rings;
1105847bf383SLuigi Rizzo 	} else if (nr_reg == NR_REG_ONE_NIC) {
1106f0ea3689SLuigi Rizzo 		/* XXX check validity */
1107f0ea3689SLuigi Rizzo 		d->first_tx_ring = d->last_tx_ring =
11084bf50f18SLuigi Rizzo 		d->first_rx_ring = d->last_rx_ring = d->req.nr_ringid & NETMAP_RING_MASK;
1109f0ea3689SLuigi Rizzo 	} else { /* pipes */
1110f0ea3689SLuigi Rizzo 		d->first_tx_ring = d->last_tx_ring = 0;
1111f0ea3689SLuigi Rizzo 		d->first_rx_ring = d->last_rx_ring = 0;
1112f9790aebSLuigi Rizzo 	}
1113f0ea3689SLuigi Rizzo 
1114f0ea3689SLuigi Rizzo #ifdef DEBUG_NETMAP_USER
1115f0ea3689SLuigi Rizzo     { /* debugging code */
1116f0ea3689SLuigi Rizzo 	int i;
1117f0ea3689SLuigi Rizzo 
1118f0ea3689SLuigi Rizzo 	D("%s tx %d .. %d %d rx %d .. %d %d", ifname,
1119f0ea3689SLuigi Rizzo 		d->first_tx_ring, d->last_tx_ring, d->req.nr_tx_rings,
1120f0ea3689SLuigi Rizzo                 d->first_rx_ring, d->last_rx_ring, d->req.nr_rx_rings);
1121f0ea3689SLuigi Rizzo 	for (i = 0; i <= d->req.nr_tx_rings; i++) {
1122f0ea3689SLuigi Rizzo 		struct netmap_ring *r = NETMAP_TXRING(d->nifp, i);
1123f0ea3689SLuigi Rizzo 		D("TX%d %p h %d c %d t %d", i, r, r->head, r->cur, r->tail);
1124f0ea3689SLuigi Rizzo 	}
1125f0ea3689SLuigi Rizzo 	for (i = 0; i <= d->req.nr_rx_rings; i++) {
1126f0ea3689SLuigi Rizzo 		struct netmap_ring *r = NETMAP_RXRING(d->nifp, i);
1127f0ea3689SLuigi Rizzo 		D("RX%d %p h %d c %d t %d", i, r, r->head, r->cur, r->tail);
1128f0ea3689SLuigi Rizzo 	}
1129f0ea3689SLuigi Rizzo     }
1130f0ea3689SLuigi Rizzo #endif /* debugging */
1131f0ea3689SLuigi Rizzo 
113266015019SLuigi Rizzo 	d->cur_tx_ring = d->first_tx_ring;
113366015019SLuigi Rizzo 	d->cur_rx_ring = d->first_rx_ring;
1134f9790aebSLuigi Rizzo 	return d;
1135f9790aebSLuigi Rizzo 
1136f9790aebSLuigi Rizzo fail:
1137f9790aebSLuigi Rizzo 	nm_close(d);
1138847bf383SLuigi Rizzo 	if (errmsg[0])
1139f0ea3689SLuigi Rizzo 		D("%s %s", errmsg, ifname);
11400506889cSLuigi Rizzo 	if (errno == 0)
1141f9790aebSLuigi Rizzo 		errno = EINVAL;
1142f9790aebSLuigi Rizzo 	return NULL;
1143f9790aebSLuigi Rizzo }
1144f9790aebSLuigi Rizzo 
1145f9790aebSLuigi Rizzo 
1146f9790aebSLuigi Rizzo static int
1147f0ea3689SLuigi Rizzo nm_close(struct nm_desc *d)
1148f9790aebSLuigi Rizzo {
114917885a7bSLuigi Rizzo 	/*
115017885a7bSLuigi Rizzo 	 * ugly trick to avoid unused warnings
115117885a7bSLuigi Rizzo 	 */
115217885a7bSLuigi Rizzo 	static void *__xxzt[] __attribute__ ((unused))  =
1153b82b2211SLuigi Rizzo 		{ (void *)nm_open, (void *)nm_inject,
1154*4f80b14cSVincenzo Maffione 		  (void *)nm_dispatch, (void *)nm_nextpkt,
1155*4f80b14cSVincenzo Maffione 	          (void *)nm_parse } ;
115617885a7bSLuigi Rizzo 
1157f9790aebSLuigi Rizzo 	if (d == NULL || d->self != d)
1158f9790aebSLuigi Rizzo 		return EINVAL;
1159f0ea3689SLuigi Rizzo 	if (d->done_mmap && d->mem)
1160f9790aebSLuigi Rizzo 		munmap(d->mem, d->memsize);
116137e3a6d3SLuigi Rizzo 	if (d->fd != -1) {
1162f9790aebSLuigi Rizzo 		close(d->fd);
116337e3a6d3SLuigi Rizzo 	}
116437e3a6d3SLuigi Rizzo 
1165f9790aebSLuigi Rizzo 	bzero(d, sizeof(*d));
1166f9790aebSLuigi Rizzo 	free(d);
1167f9790aebSLuigi Rizzo 	return 0;
1168f9790aebSLuigi Rizzo }
1169f9790aebSLuigi Rizzo 
1170f9790aebSLuigi Rizzo 
117137e3a6d3SLuigi Rizzo static int
117237e3a6d3SLuigi Rizzo nm_mmap(struct nm_desc *d, const struct nm_desc *parent)
117337e3a6d3SLuigi Rizzo {
117437e3a6d3SLuigi Rizzo 	//XXX TODO: check if mmap is already done
117537e3a6d3SLuigi Rizzo 
117637e3a6d3SLuigi Rizzo 	if (IS_NETMAP_DESC(parent) && parent->mem &&
117737e3a6d3SLuigi Rizzo 	    parent->req.nr_arg2 == d->req.nr_arg2) {
117837e3a6d3SLuigi Rizzo 		/* do not mmap, inherit from parent */
117937e3a6d3SLuigi Rizzo 		D("do not mmap, inherit from parent");
118037e3a6d3SLuigi Rizzo 		d->memsize = parent->memsize;
118137e3a6d3SLuigi Rizzo 		d->mem = parent->mem;
118237e3a6d3SLuigi Rizzo 	} else {
118337e3a6d3SLuigi Rizzo 		/* XXX TODO: check if memsize is too large (or there is overflow) */
118437e3a6d3SLuigi Rizzo 		d->memsize = d->req.nr_memsize;
118537e3a6d3SLuigi Rizzo 		d->mem = mmap(0, d->memsize, PROT_WRITE | PROT_READ, MAP_SHARED,
118637e3a6d3SLuigi Rizzo 				d->fd, 0);
118737e3a6d3SLuigi Rizzo 		if (d->mem == MAP_FAILED) {
118837e3a6d3SLuigi Rizzo 			goto fail;
118937e3a6d3SLuigi Rizzo 		}
119037e3a6d3SLuigi Rizzo 		d->done_mmap = 1;
119137e3a6d3SLuigi Rizzo 	}
119237e3a6d3SLuigi Rizzo 
1193*4f80b14cSVincenzo Maffione 	nm_init_offsets(d);
119437e3a6d3SLuigi Rizzo 	return 0;
119537e3a6d3SLuigi Rizzo 
119637e3a6d3SLuigi Rizzo fail:
119737e3a6d3SLuigi Rizzo 	return EINVAL;
119837e3a6d3SLuigi Rizzo }
119937e3a6d3SLuigi Rizzo 
1200f9790aebSLuigi Rizzo /*
120117885a7bSLuigi Rizzo  * Same prototype as pcap_inject(), only need to cast.
120217885a7bSLuigi Rizzo  */
120317885a7bSLuigi Rizzo static int
1204f0ea3689SLuigi Rizzo nm_inject(struct nm_desc *d, const void *buf, size_t size)
120517885a7bSLuigi Rizzo {
1206*4f80b14cSVincenzo Maffione 	u_int c, n = d->last_tx_ring - d->first_tx_ring + 1,
1207*4f80b14cSVincenzo Maffione 		ri = d->cur_tx_ring;
120817885a7bSLuigi Rizzo 
1209*4f80b14cSVincenzo Maffione 	for (c = 0; c < n ; c++, ri++) {
121017885a7bSLuigi Rizzo 		/* compute current ring to use */
121117885a7bSLuigi Rizzo 		struct netmap_ring *ring;
121217885a7bSLuigi Rizzo 		uint32_t i, idx;
121317885a7bSLuigi Rizzo 
121466015019SLuigi Rizzo 		if (ri > d->last_tx_ring)
121566015019SLuigi Rizzo 			ri = d->first_tx_ring;
121617885a7bSLuigi Rizzo 		ring = NETMAP_TXRING(d->nifp, ri);
121717885a7bSLuigi Rizzo 		if (nm_ring_empty(ring)) {
121817885a7bSLuigi Rizzo 			continue;
121917885a7bSLuigi Rizzo 		}
122017885a7bSLuigi Rizzo 		i = ring->cur;
122117885a7bSLuigi Rizzo 		idx = ring->slot[i].buf_idx;
122217885a7bSLuigi Rizzo 		ring->slot[i].len = size;
1223f0ea3689SLuigi Rizzo 		nm_pkt_copy(buf, NETMAP_BUF(ring, idx), size);
122466015019SLuigi Rizzo 		d->cur_tx_ring = ri;
122517885a7bSLuigi Rizzo 		ring->head = ring->cur = nm_ring_next(ring, i);
122617885a7bSLuigi Rizzo 		return size;
122717885a7bSLuigi Rizzo 	}
122817885a7bSLuigi Rizzo 	return 0; /* fail */
122917885a7bSLuigi Rizzo }
123017885a7bSLuigi Rizzo 
123117885a7bSLuigi Rizzo 
123217885a7bSLuigi Rizzo /*
1233f9790aebSLuigi Rizzo  * Same prototype as pcap_dispatch(), only need to cast.
1234f9790aebSLuigi Rizzo  */
1235f9790aebSLuigi Rizzo static int
1236f0ea3689SLuigi Rizzo nm_dispatch(struct nm_desc *d, int cnt, nm_cb_t cb, u_char *arg)
1237f9790aebSLuigi Rizzo {
123866015019SLuigi Rizzo 	int n = d->last_rx_ring - d->first_rx_ring + 1;
123966015019SLuigi Rizzo 	int c, got = 0, ri = d->cur_rx_ring;
124037e3a6d3SLuigi Rizzo 	d->hdr.buf = NULL;
124137e3a6d3SLuigi Rizzo 	d->hdr.flags = NM_MORE_PKTS;
124237e3a6d3SLuigi Rizzo 	d->hdr.d = d;
1243f9790aebSLuigi Rizzo 
1244f9790aebSLuigi Rizzo 	if (cnt == 0)
1245f9790aebSLuigi Rizzo 		cnt = -1;
1246f9790aebSLuigi Rizzo 	/* cnt == -1 means infinite, but rings have a finite amount
1247f9790aebSLuigi Rizzo 	 * of buffers and the int is large enough that we never wrap,
1248f9790aebSLuigi Rizzo 	 * so we can omit checking for -1
1249f9790aebSLuigi Rizzo 	 */
1250*4f80b14cSVincenzo Maffione 	for (c=0; c < n && cnt != got; c++, ri++) {
1251f9790aebSLuigi Rizzo 		/* compute current ring to use */
1252f9790aebSLuigi Rizzo 		struct netmap_ring *ring;
1253f9790aebSLuigi Rizzo 
125466015019SLuigi Rizzo 		if (ri > d->last_rx_ring)
125566015019SLuigi Rizzo 			ri = d->first_rx_ring;
1256f9790aebSLuigi Rizzo 		ring = NETMAP_RXRING(d->nifp, ri);
125717885a7bSLuigi Rizzo 		for ( ; !nm_ring_empty(ring) && cnt != got; got++) {
125837e3a6d3SLuigi Rizzo 			u_int idx, i;
125937e3a6d3SLuigi Rizzo 			if (d->hdr.buf) { /* from previous round */
126037e3a6d3SLuigi Rizzo 				cb(arg, &d->hdr, d->hdr.buf);
126137e3a6d3SLuigi Rizzo 			}
126237e3a6d3SLuigi Rizzo 			i = ring->cur;
126337e3a6d3SLuigi Rizzo 			idx = ring->slot[i].buf_idx;
1264*4f80b14cSVincenzo Maffione 			/* d->cur_rx_ring doesn't change inside this loop, but
1265*4f80b14cSVincenzo Maffione 			 * set it here, so it reflects d->hdr.buf's ring */
1266*4f80b14cSVincenzo Maffione 			d->cur_rx_ring = ri;
126737e3a6d3SLuigi Rizzo 			d->hdr.slot = &ring->slot[i];
126837e3a6d3SLuigi Rizzo 			d->hdr.buf = (u_char *)NETMAP_BUF(ring, idx);
126966015019SLuigi Rizzo 			// __builtin_prefetch(buf);
1270f9790aebSLuigi Rizzo 			d->hdr.len = d->hdr.caplen = ring->slot[i].len;
1271f9790aebSLuigi Rizzo 			d->hdr.ts = ring->ts;
127217885a7bSLuigi Rizzo 			ring->head = ring->cur = nm_ring_next(ring, i);
1273f9790aebSLuigi Rizzo 		}
1274f9790aebSLuigi Rizzo 	}
127537e3a6d3SLuigi Rizzo 	if (d->hdr.buf) { /* from previous round */
127637e3a6d3SLuigi Rizzo 		d->hdr.flags = 0;
127737e3a6d3SLuigi Rizzo 		cb(arg, &d->hdr, d->hdr.buf);
127837e3a6d3SLuigi Rizzo 	}
1279f9790aebSLuigi Rizzo 	return got;
1280f9790aebSLuigi Rizzo }
1281f9790aebSLuigi Rizzo 
1282f9790aebSLuigi Rizzo static u_char *
1283f0ea3689SLuigi Rizzo nm_nextpkt(struct nm_desc *d, struct nm_pkthdr *hdr)
1284f9790aebSLuigi Rizzo {
128566015019SLuigi Rizzo 	int ri = d->cur_rx_ring;
1286f9790aebSLuigi Rizzo 
1287f9790aebSLuigi Rizzo 	do {
1288f9790aebSLuigi Rizzo 		/* compute current ring to use */
1289f9790aebSLuigi Rizzo 		struct netmap_ring *ring = NETMAP_RXRING(d->nifp, ri);
129017885a7bSLuigi Rizzo 		if (!nm_ring_empty(ring)) {
1291f9790aebSLuigi Rizzo 			u_int i = ring->cur;
1292f9790aebSLuigi Rizzo 			u_int idx = ring->slot[i].buf_idx;
1293f9790aebSLuigi Rizzo 			u_char *buf = (u_char *)NETMAP_BUF(ring, idx);
129466015019SLuigi Rizzo 
129566015019SLuigi Rizzo 			// __builtin_prefetch(buf);
1296f9790aebSLuigi Rizzo 			hdr->ts = ring->ts;
1297f9790aebSLuigi Rizzo 			hdr->len = hdr->caplen = ring->slot[i].len;
129817885a7bSLuigi Rizzo 			ring->cur = nm_ring_next(ring, i);
129917885a7bSLuigi Rizzo 			/* we could postpone advancing head if we want
130017885a7bSLuigi Rizzo 			 * to hold the buffer. This can be supported in
130117885a7bSLuigi Rizzo 			 * the future.
130217885a7bSLuigi Rizzo 			 */
130317885a7bSLuigi Rizzo 			ring->head = ring->cur;
130466015019SLuigi Rizzo 			d->cur_rx_ring = ri;
1305f9790aebSLuigi Rizzo 			return buf;
1306f9790aebSLuigi Rizzo 		}
1307f9790aebSLuigi Rizzo 		ri++;
130866015019SLuigi Rizzo 		if (ri > d->last_rx_ring)
130966015019SLuigi Rizzo 			ri = d->first_rx_ring;
131066015019SLuigi Rizzo 	} while (ri != d->cur_rx_ring);
1311f9790aebSLuigi Rizzo 	return NULL; /* nothing found */
1312f9790aebSLuigi Rizzo }
1313f9790aebSLuigi Rizzo 
1314f9790aebSLuigi Rizzo #endif /* !HAVE_NETMAP_WITH_LIBS */
1315f9790aebSLuigi Rizzo 
1316f9790aebSLuigi Rizzo #endif /* NETMAP_WITH_LIBS */
1317f9790aebSLuigi Rizzo 
131868b8534bSLuigi Rizzo #endif /* _NET_NETMAP_USER_H_ */
1319