xref: /freebsd-src/sys/net/netmap_user.h (revision 2ff63af9b88c7413b7d71715b5532625752a248e)
1fe267a55SPedro F. Giffuni /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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  *
3217885a7bSLuigi Rizzo  * Functions and macros to manipulate netmap structures and packets
3317885a7bSLuigi Rizzo  * in userspace. See netmap(4) for more information.
3468b8534bSLuigi Rizzo  *
3564ae02c3SLuigi Rizzo  * The address of the struct netmap_if, say nifp, is computed from the
3664ae02c3SLuigi Rizzo  * value returned from ioctl(.., NIOCREG, ...) and the mmap region:
3768b8534bSLuigi Rizzo  *	ioctl(fd, NIOCREG, &req);
3868b8534bSLuigi Rizzo  *	mem = mmap(0, ... );
3968b8534bSLuigi Rizzo  *	nifp = NETMAP_IF(mem, req.nr_nifp);
4068b8534bSLuigi Rizzo  *		(so simple, we could just do it manually)
4168b8534bSLuigi Rizzo  *
4268b8534bSLuigi Rizzo  * From there:
4368b8534bSLuigi Rizzo  *	struct netmap_ring *NETMAP_TXRING(nifp, index)
4468b8534bSLuigi Rizzo  *	struct netmap_ring *NETMAP_RXRING(nifp, index)
450506889cSLuigi Rizzo  *		we can access ring->cur, ring->head, ring->tail, etc.
4668b8534bSLuigi Rizzo  *
4768b8534bSLuigi Rizzo  *	ring->slot[i] gives us the i-th slot (we can access
4817885a7bSLuigi Rizzo  *		directly len, flags, buf_idx)
4968b8534bSLuigi Rizzo  *
5027892e02SLuigi Rizzo  *	char *buf = NETMAP_BUF(ring, x) returns a pointer to
5127892e02SLuigi Rizzo  *		the buffer numbered x
5268b8534bSLuigi Rizzo  *
5317885a7bSLuigi Rizzo  * All ring indexes (head, cur, tail) should always move forward.
5417885a7bSLuigi Rizzo  * To compute the next index in a circular ring you can use
5517885a7bSLuigi Rizzo  *	i = nm_ring_next(ring, i);
56f9790aebSLuigi Rizzo  *
5745c67e8fSVincenzo Maffione  * To ease porting apps from pcap to netmap we supply a few functions
5817885a7bSLuigi Rizzo  * that can be called to open, close, read and write on netmap in a way
5917885a7bSLuigi Rizzo  * similar to libpcap. Note that the read/write function depend on
6017885a7bSLuigi Rizzo  * an ioctl()/select()/poll() being issued to refill rings or push
6117885a7bSLuigi Rizzo  * packets out.
62f9790aebSLuigi Rizzo  *
63f9790aebSLuigi Rizzo  * In order to use these, include #define NETMAP_WITH_LIBS
64f9790aebSLuigi Rizzo  * in the source file that invokes these functions.
6568b8534bSLuigi Rizzo  */
6668b8534bSLuigi Rizzo 
6768b8534bSLuigi Rizzo #ifndef _NET_NETMAP_USER_H_
6868b8534bSLuigi Rizzo #define _NET_NETMAP_USER_H_
6968b8534bSLuigi Rizzo 
7037e3a6d3SLuigi Rizzo #define NETMAP_DEVICE_NAME "/dev/netmap"
7137e3a6d3SLuigi Rizzo 
7237e3a6d3SLuigi Rizzo #ifdef __CYGWIN__
7337e3a6d3SLuigi Rizzo /*
7437e3a6d3SLuigi Rizzo  * we can compile userspace apps with either cygwin or msvc,
7537e3a6d3SLuigi Rizzo  * and we use _WIN32 to identify windows specific code
7637e3a6d3SLuigi Rizzo  */
7737e3a6d3SLuigi Rizzo #ifndef _WIN32
7837e3a6d3SLuigi Rizzo #define _WIN32
7937e3a6d3SLuigi Rizzo #endif	/* _WIN32 */
8037e3a6d3SLuigi Rizzo 
8137e3a6d3SLuigi Rizzo #endif	/* __CYGWIN__ */
8237e3a6d3SLuigi Rizzo 
8337e3a6d3SLuigi Rizzo #ifdef _WIN32
8437e3a6d3SLuigi Rizzo #undef NETMAP_DEVICE_NAME
8537e3a6d3SLuigi Rizzo #define NETMAP_DEVICE_NAME "/proc/sys/DosDevices/Global/netmap"
8637e3a6d3SLuigi Rizzo #include <windows.h>
8737e3a6d3SLuigi Rizzo #include <WinDef.h>
8837e3a6d3SLuigi Rizzo #include <sys/cygwin.h>
8937e3a6d3SLuigi Rizzo #endif /* _WIN32 */
9037e3a6d3SLuigi Rizzo 
91f9790aebSLuigi Rizzo #include <stdint.h>
92f0ea3689SLuigi Rizzo #include <sys/socket.h>		/* apple needs sockaddr */
93f9790aebSLuigi Rizzo #include <net/if.h>		/* IFNAMSIZ */
9437e3a6d3SLuigi Rizzo #include <ctype.h>
95d12354a5SVincenzo Maffione #include <string.h>	/* memset */
96d12354a5SVincenzo Maffione #include <sys/time.h>   /* gettimeofday */
9717885a7bSLuigi Rizzo 
9817885a7bSLuigi Rizzo #ifndef likely
9917885a7bSLuigi Rizzo #define likely(x)	__builtin_expect(!!(x), 1)
10017885a7bSLuigi Rizzo #define unlikely(x)	__builtin_expect(!!(x), 0)
10117885a7bSLuigi Rizzo #endif /* likely and unlikely */
10217885a7bSLuigi Rizzo 
103f9790aebSLuigi Rizzo #include <net/netmap.h>
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 *,	\
115d12354a5SVincenzo Maffione 	nifp, (nifp)->ring_ofs[index + (nifp)->ni_tx_rings + 		\
116d12354a5SVincenzo Maffione 		(nifp)->ni_host_tx_rings] )
11768b8534bSLuigi Rizzo 
11868b8534bSLuigi Rizzo #define NETMAP_BUF(ring, index)				\
119253b2ec1SVincenzo Maffione 	((char *)(ring) + (ring)->buf_ofs + ((size_t)(index)*(ring)->nr_buf_size))
12068b8534bSLuigi Rizzo 
12164ae02c3SLuigi Rizzo #define NETMAP_BUF_IDX(ring, buf)			\
12264ae02c3SLuigi Rizzo 	( ((char *)(buf) - ((char *)(ring) + (ring)->buf_ofs) ) / \
123a7250582SLuigi Rizzo 		(ring)->nr_buf_size )
12464ae02c3SLuigi Rizzo 
125a6d768d8SVincenzo Maffione /* read the offset field in a ring's slot */
126a6d768d8SVincenzo Maffione #define NETMAP_ROFFSET(ring, slot)			\
127a6d768d8SVincenzo Maffione 	((slot)->ptr & (ring)->offset_mask)
128a6d768d8SVincenzo Maffione 
129a6d768d8SVincenzo Maffione /* update the offset field in a ring's slot */
130a6d768d8SVincenzo Maffione #define NETMAP_WOFFSET(ring, slot, offset)		\
131a6d768d8SVincenzo Maffione 	do { (slot)->ptr = ((slot)->ptr & ~(ring)->offset_mask) | \
132a6d768d8SVincenzo Maffione 		((offset) & (ring)->offset_mask); } while (0)
133a6d768d8SVincenzo Maffione 
134a6d768d8SVincenzo Maffione /* obtain the start of the buffer pointed to by  a ring's slot, taking the
13545c67e8fSVincenzo Maffione  * offset field into account
136a6d768d8SVincenzo Maffione  */
137a6d768d8SVincenzo Maffione #define NETMAP_BUF_OFFSET(ring, slot)			\
138a6d768d8SVincenzo Maffione 	(NETMAP_BUF(ring, (slot)->buf_idx) + NETMAP_ROFFSET(ring, slot))
139a6d768d8SVincenzo Maffione 
140a6d768d8SVincenzo Maffione 
14117885a7bSLuigi Rizzo static inline uint32_t
nm_ring_next(struct netmap_ring * r,uint32_t i)14217885a7bSLuigi Rizzo nm_ring_next(struct netmap_ring *r, uint32_t i)
14317885a7bSLuigi Rizzo {
14417885a7bSLuigi Rizzo 	return ( unlikely(i + 1 == r->num_slots) ? 0 : i + 1);
14517885a7bSLuigi Rizzo }
14617885a7bSLuigi Rizzo 
147a6d768d8SVincenzo Maffione 
14868b8534bSLuigi Rizzo /*
14917885a7bSLuigi Rizzo  * Return 1 if we have pending transmissions in the tx ring.
150f0ea3689SLuigi Rizzo  * When everything is complete ring->head = ring->tail + 1 (modulo ring size)
15168b8534bSLuigi Rizzo  */
15217885a7bSLuigi Rizzo static inline int
nm_tx_pending(struct netmap_ring * r)15317885a7bSLuigi Rizzo nm_tx_pending(struct netmap_ring *r)
15417885a7bSLuigi Rizzo {
155f0ea3689SLuigi Rizzo 	return nm_ring_next(r, r->tail) != r->head;
15617885a7bSLuigi Rizzo }
15717885a7bSLuigi Rizzo 
158b6e66be2SVincenzo Maffione /* Compute the number of slots available in the netmap ring. We use
159b6e66be2SVincenzo Maffione  * ring->head as explained in the comment above nm_ring_empty(). */
16017885a7bSLuigi Rizzo static inline uint32_t
nm_ring_space(struct netmap_ring * ring)16117885a7bSLuigi Rizzo nm_ring_space(struct netmap_ring *ring)
16217885a7bSLuigi Rizzo {
163b6e66be2SVincenzo Maffione         int ret = ring->tail - ring->head;
16417885a7bSLuigi Rizzo         if (ret < 0)
16517885a7bSLuigi Rizzo                 ret += ring->num_slots;
16617885a7bSLuigi Rizzo         return ret;
16717885a7bSLuigi Rizzo }
16817885a7bSLuigi Rizzo 
169f0ea3689SLuigi Rizzo #ifndef ND /* debug macros */
170f0ea3689SLuigi Rizzo /* debug support */
171f0ea3689SLuigi Rizzo #define ND(_fmt, ...) do {} while(0)
172f0ea3689SLuigi Rizzo #define D(_fmt, ...)						\
173f0ea3689SLuigi Rizzo 	do {							\
1744bf50f18SLuigi Rizzo 		struct timeval _t0;				\
1754bf50f18SLuigi Rizzo 		gettimeofday(&_t0, NULL);			\
176f0ea3689SLuigi Rizzo 		fprintf(stderr, "%03d.%06d %s [%d] " _fmt "\n",	\
1774bf50f18SLuigi Rizzo 		    (int)(_t0.tv_sec % 1000), (int)_t0.tv_usec,	\
178f0ea3689SLuigi Rizzo 		    __FUNCTION__, __LINE__, ##__VA_ARGS__);	\
179f0ea3689SLuigi Rizzo         } while (0)
180f0ea3689SLuigi Rizzo 
181f0ea3689SLuigi Rizzo /* Rate limited version of "D", lps indicates how many per second */
182f0ea3689SLuigi Rizzo #define RD(lps, format, ...)                                    \
183f0ea3689SLuigi Rizzo     do {                                                        \
1844bf50f18SLuigi Rizzo         static int __t0, __cnt;                                 \
185f0ea3689SLuigi Rizzo         struct timeval __xxts;                                  \
186f0ea3689SLuigi Rizzo         gettimeofday(&__xxts, NULL);                            \
1874bf50f18SLuigi Rizzo         if (__t0 != __xxts.tv_sec) {                            \
1884bf50f18SLuigi Rizzo             __t0 = __xxts.tv_sec;                               \
189f0ea3689SLuigi Rizzo             __cnt = 0;                                          \
190f0ea3689SLuigi Rizzo         }                                                       \
191f0ea3689SLuigi Rizzo         if (__cnt++ < lps) {                                    \
192f0ea3689SLuigi Rizzo             D(format, ##__VA_ARGS__);                           \
193f0ea3689SLuigi Rizzo         }                                                       \
194f0ea3689SLuigi Rizzo     } while (0)
195f0ea3689SLuigi Rizzo #endif
196f0ea3689SLuigi Rizzo 
197d12354a5SVincenzo Maffione /*
198d12354a5SVincenzo Maffione  * this is a slightly optimized copy routine which rounds
199d12354a5SVincenzo Maffione  * to multiple of 64 bytes and is often faster than dealing
200d12354a5SVincenzo Maffione  * with other odd sizes. We assume there is enough room
201d12354a5SVincenzo Maffione  * in the source and destination buffers.
202d12354a5SVincenzo Maffione  */
203d12354a5SVincenzo Maffione static inline void
nm_pkt_copy(const void * _src,void * _dst,int l)204d12354a5SVincenzo Maffione nm_pkt_copy(const void *_src, void *_dst, int l)
205d12354a5SVincenzo Maffione {
206d12354a5SVincenzo Maffione 	const uint64_t *src = (const uint64_t *)_src;
207d12354a5SVincenzo Maffione 	uint64_t *dst = (uint64_t *)_dst;
208d12354a5SVincenzo Maffione 
209d12354a5SVincenzo Maffione 	if (unlikely(l >= 1024 || l % 64)) {
210d12354a5SVincenzo Maffione 		memcpy(dst, src, l);
211d12354a5SVincenzo Maffione 		return;
212d12354a5SVincenzo Maffione 	}
213d12354a5SVincenzo Maffione 	for (; likely(l > 0); l-=64) {
214d12354a5SVincenzo Maffione 		*dst++ = *src++;
215d12354a5SVincenzo Maffione 		*dst++ = *src++;
216d12354a5SVincenzo Maffione 		*dst++ = *src++;
217d12354a5SVincenzo Maffione 		*dst++ = *src++;
218d12354a5SVincenzo Maffione 		*dst++ = *src++;
219d12354a5SVincenzo Maffione 		*dst++ = *src++;
220d12354a5SVincenzo Maffione 		*dst++ = *src++;
221d12354a5SVincenzo Maffione 		*dst++ = *src++;
222d12354a5SVincenzo Maffione 	}
223d12354a5SVincenzo Maffione }
224d12354a5SVincenzo Maffione 
225d12354a5SVincenzo Maffione #ifdef NETMAP_WITH_LIBS
226d12354a5SVincenzo Maffione /*
227d12354a5SVincenzo Maffione  * Support for simple I/O libraries.
228d12354a5SVincenzo Maffione  * Include other system headers required for compiling this.
229d12354a5SVincenzo Maffione  */
230d12354a5SVincenzo Maffione 
231d12354a5SVincenzo Maffione #ifndef HAVE_NETMAP_WITH_LIBS
232d12354a5SVincenzo Maffione #define HAVE_NETMAP_WITH_LIBS
233d12354a5SVincenzo Maffione 
234d12354a5SVincenzo Maffione #include <stdio.h>
235d12354a5SVincenzo Maffione #include <sys/time.h>
236d12354a5SVincenzo Maffione #include <sys/mman.h>
237d12354a5SVincenzo Maffione #include <sys/ioctl.h>
238d12354a5SVincenzo Maffione #include <sys/errno.h>	/* EINVAL */
239d12354a5SVincenzo Maffione #include <fcntl.h>	/* O_RDWR */
240d12354a5SVincenzo Maffione #include <unistd.h>	/* close() */
241d12354a5SVincenzo Maffione #include <signal.h>
242d12354a5SVincenzo Maffione #include <stdlib.h>
243d12354a5SVincenzo Maffione 
24437e3a6d3SLuigi Rizzo struct nm_pkthdr {	/* first part is the same as pcap_pkthdr */
245f9790aebSLuigi Rizzo 	struct timeval	ts;
246f9790aebSLuigi Rizzo 	uint32_t	caplen;
247f9790aebSLuigi Rizzo 	uint32_t	len;
24837e3a6d3SLuigi Rizzo 
24937e3a6d3SLuigi Rizzo 	uint64_t flags;	/* NM_MORE_PKTS etc */
25037e3a6d3SLuigi Rizzo #define NM_MORE_PKTS	1
25137e3a6d3SLuigi Rizzo 	struct nm_desc *d;
25237e3a6d3SLuigi Rizzo 	struct netmap_slot *slot;
25337e3a6d3SLuigi Rizzo 	uint8_t *buf;
254f9790aebSLuigi Rizzo };
255f9790aebSLuigi Rizzo 
256f0ea3689SLuigi Rizzo struct nm_stat {	/* same as pcap_stat	*/
25766015019SLuigi Rizzo 	u_int	ps_recv;
25866015019SLuigi Rizzo 	u_int	ps_drop;
25966015019SLuigi Rizzo 	u_int	ps_ifdrop;
26037e3a6d3SLuigi Rizzo #ifdef WIN32 /* XXX or _WIN32 ? */
26166015019SLuigi Rizzo 	u_int	bs_capt;
26266015019SLuigi Rizzo #endif /* WIN32 */
26366015019SLuigi Rizzo };
26466015019SLuigi Rizzo 
26566015019SLuigi Rizzo #define NM_ERRBUF_SIZE	512
26666015019SLuigi Rizzo 
267f0ea3689SLuigi Rizzo struct nm_desc {
268f0ea3689SLuigi Rizzo 	struct nm_desc *self; /* point to self if netmap. */
269f9790aebSLuigi Rizzo 	int fd;
270f9790aebSLuigi Rizzo 	void *mem;
271253b2ec1SVincenzo Maffione 	size_t memsize;
272f0ea3689SLuigi Rizzo 	int done_mmap;	/* set if mem is the result of mmap */
273f0ea3689SLuigi Rizzo 	struct netmap_if * const nifp;
27466015019SLuigi Rizzo 	uint16_t first_tx_ring, last_tx_ring, cur_tx_ring;
27566015019SLuigi Rizzo 	uint16_t first_rx_ring, last_rx_ring, cur_rx_ring;
27666015019SLuigi Rizzo 	struct nmreq req;	/* also contains the nr_name = ifname */
277f0ea3689SLuigi Rizzo 	struct nm_pkthdr hdr;
27866015019SLuigi Rizzo 
279f0ea3689SLuigi Rizzo 	/*
280f0ea3689SLuigi Rizzo 	 * The memory contains netmap_if, rings and then buffers.
281f0ea3689SLuigi Rizzo 	 * Given a pointer (e.g. to nm_inject) we can compare with
282f0ea3689SLuigi Rizzo 	 * mem/buf_start/buf_end to tell if it is a buffer or
283f0ea3689SLuigi Rizzo 	 * some other descriptor in our region.
284f0ea3689SLuigi Rizzo 	 * We also store a pointer to some ring as it helps in the
285f0ea3689SLuigi Rizzo 	 * translation from buffer indexes to addresses.
286f0ea3689SLuigi Rizzo 	 */
287f0ea3689SLuigi Rizzo 	struct netmap_ring * const some_ring;
288f0ea3689SLuigi Rizzo 	void * const buf_start;
289f0ea3689SLuigi Rizzo 	void * const buf_end;
29066015019SLuigi Rizzo 	/* parameters from pcap_open_live */
29166015019SLuigi Rizzo 	int snaplen;
29266015019SLuigi Rizzo 	int promisc;
29366015019SLuigi Rizzo 	int to_ms;
29466015019SLuigi Rizzo 	char *errbuf;
29566015019SLuigi Rizzo 
29666015019SLuigi Rizzo 	/* save flags so we can restore them on close */
29766015019SLuigi Rizzo 	uint32_t if_flags;
29866015019SLuigi Rizzo         uint32_t if_reqcap;
29966015019SLuigi Rizzo         uint32_t if_curcap;
30066015019SLuigi Rizzo 
301f0ea3689SLuigi Rizzo 	struct nm_stat st;
30266015019SLuigi Rizzo 	char msg[NM_ERRBUF_SIZE];
303f9790aebSLuigi Rizzo };
304f9790aebSLuigi Rizzo 
305f9790aebSLuigi Rizzo /*
306f9790aebSLuigi Rizzo  * when the descriptor is open correctly, d->self == d
307b82b2211SLuigi Rizzo  * Eventually we should also use some magic number.
308f9790aebSLuigi Rizzo  */
309adf41f07SVincenzo Maffione #define P2NMD(p)		((const struct nm_desc *)(p))
310f0ea3689SLuigi Rizzo #define IS_NETMAP_DESC(d)	((d) && P2NMD(d)->self == P2NMD(d))
311f9790aebSLuigi Rizzo #define NETMAP_FD(d)		(P2NMD(d)->fd)
312f9790aebSLuigi Rizzo 
313f9790aebSLuigi Rizzo /*
314f9790aebSLuigi Rizzo  * The callback, invoked on each received packet. Same as libpcap
315f9790aebSLuigi Rizzo  */
316f0ea3689SLuigi Rizzo typedef void (*nm_cb_t)(u_char *, const struct nm_pkthdr *, const u_char *d);
317f9790aebSLuigi Rizzo 
318f9790aebSLuigi Rizzo /*
31917885a7bSLuigi Rizzo  *--- the pcap-like API ---
32017885a7bSLuigi Rizzo  *
32117885a7bSLuigi Rizzo  * nm_open() opens a file descriptor, binds to a port and maps memory.
32217885a7bSLuigi Rizzo  *
32317885a7bSLuigi Rizzo  * ifname	(netmap:foo or vale:foo) is the port name
32445c67e8fSVincenzo Maffione  *		a suffix can indicate the following:
325f0ea3689SLuigi Rizzo  *		^		bind the host (sw) ring pair
326f9eece46SLuiz Otavio O Souza  *		*		bind host and NIC ring pairs
327f0ea3689SLuigi Rizzo  *		-NN		bind individual NIC ring pair
328f0ea3689SLuigi Rizzo  *		{NN		bind master side of pipe NN
329f0ea3689SLuigi Rizzo  *		}NN		bind slave side of pipe NN
33037e3a6d3SLuigi Rizzo  *		a suffix starting with / and the following flags,
331847bf383SLuigi Rizzo  *		in any order:
332847bf383SLuigi Rizzo  *		x		exclusive access
333f9eece46SLuiz Otavio O Souza  *		z		zero copy monitor (both tx and rx)
334f9eece46SLuiz Otavio O Souza  *		t		monitor tx side (copy monitor)
335f9eece46SLuiz Otavio O Souza  *		r		monitor rx side (copy monitor)
33637e3a6d3SLuigi Rizzo  *		R		bind only RX ring(s)
33737e3a6d3SLuigi Rizzo  *		T		bind only TX ring(s)
338f0ea3689SLuigi Rizzo  *
339f0ea3689SLuigi Rizzo  * req		provides the initial values of nmreq before parsing ifname.
340f0ea3689SLuigi Rizzo  *		Remember that the ifname parsing will override the ring
341f0ea3689SLuigi Rizzo  *		number in nm_ringid, and part of nm_flags;
342f0ea3689SLuigi Rizzo  * flags	special functions, normally 0
343f0ea3689SLuigi Rizzo  *		indicates which fields of *arg are significant
344f0ea3689SLuigi Rizzo  * arg		special functions, normally NULL
345f0ea3689SLuigi Rizzo  *		if passed a netmap_desc with mem != NULL,
346f0ea3689SLuigi Rizzo  *		use that memory instead of mmap.
347f9790aebSLuigi Rizzo  */
34817885a7bSLuigi Rizzo 
349f0ea3689SLuigi Rizzo static struct nm_desc *nm_open(const char *ifname, const struct nmreq *req,
350f0ea3689SLuigi Rizzo 	uint64_t flags, const struct nm_desc *arg);
351f0ea3689SLuigi Rizzo 
352f0ea3689SLuigi Rizzo /*
353f0ea3689SLuigi Rizzo  * nm_open can import some fields from the parent descriptor.
354f0ea3689SLuigi Rizzo  * These flags control which ones.
355f0ea3689SLuigi Rizzo  * Also in flags you can specify NETMAP_NO_TX_POLL and NETMAP_DO_RX_POLL,
356f0ea3689SLuigi Rizzo  * which set the initial value for these flags.
357f0ea3689SLuigi Rizzo  * Note that the 16 low bits of the flags are reserved for data
358f0ea3689SLuigi Rizzo  * that may go into the nmreq.
359f0ea3689SLuigi Rizzo  */
360f0ea3689SLuigi Rizzo enum {
361f0ea3689SLuigi Rizzo 	NM_OPEN_NO_MMAP =	0x040000, /* reuse mmap from parent */
362f0ea3689SLuigi Rizzo 	NM_OPEN_IFNAME =	0x080000, /* nr_name, nr_ringid, nr_flags */
363f0ea3689SLuigi Rizzo 	NM_OPEN_ARG1 =		0x100000,
364f0ea3689SLuigi Rizzo 	NM_OPEN_ARG2 =		0x200000,
365f0ea3689SLuigi Rizzo 	NM_OPEN_ARG3 =		0x400000,
366f0ea3689SLuigi Rizzo 	NM_OPEN_RING_CFG =	0x800000, /* tx|rx rings|slots */
367f0ea3689SLuigi Rizzo };
368f0ea3689SLuigi Rizzo 
369a6d768d8SVincenzo Maffione 
370f9790aebSLuigi Rizzo /*
37117885a7bSLuigi Rizzo  * nm_close()	closes and restores the port to its previous state
372f9790aebSLuigi Rizzo  */
37317885a7bSLuigi Rizzo 
374f0ea3689SLuigi Rizzo static int nm_close(struct nm_desc *);
375f9790aebSLuigi Rizzo 
376f9790aebSLuigi Rizzo /*
37737e3a6d3SLuigi Rizzo  * nm_mmap()    do mmap or inherit from parent if the nr_arg2
37837e3a6d3SLuigi Rizzo  *              (memory block) matches.
37937e3a6d3SLuigi Rizzo  */
38037e3a6d3SLuigi Rizzo 
38137e3a6d3SLuigi Rizzo static int nm_mmap(struct nm_desc *, const struct nm_desc *);
38237e3a6d3SLuigi Rizzo 
38337e3a6d3SLuigi Rizzo /*
38417885a7bSLuigi Rizzo  * nm_inject() is the same as pcap_inject()
38517885a7bSLuigi Rizzo  * nm_dispatch() is the same as pcap_dispatch()
38617885a7bSLuigi Rizzo  * nm_nextpkt() is the same as pcap_next()
387f9790aebSLuigi Rizzo  */
38817885a7bSLuigi Rizzo 
389f0ea3689SLuigi Rizzo static int nm_inject(struct nm_desc *, const void *, size_t);
390f0ea3689SLuigi Rizzo static int nm_dispatch(struct nm_desc *, int, nm_cb_t, u_char *);
391f0ea3689SLuigi Rizzo static u_char *nm_nextpkt(struct nm_desc *, struct nm_pkthdr *);
392f9790aebSLuigi Rizzo 
39337e3a6d3SLuigi Rizzo #ifdef _WIN32
39437e3a6d3SLuigi Rizzo 
39537e3a6d3SLuigi Rizzo intptr_t _get_osfhandle(int); /* defined in io.h in windows */
39637e3a6d3SLuigi Rizzo 
39737e3a6d3SLuigi Rizzo /*
39837e3a6d3SLuigi Rizzo  * In windows we do not have yet native poll support, so we keep track
39937e3a6d3SLuigi Rizzo  * of file descriptors associated to netmap ports to emulate poll on
40037e3a6d3SLuigi Rizzo  * them and fall back on regular poll on other file descriptors.
40137e3a6d3SLuigi Rizzo  */
40237e3a6d3SLuigi Rizzo struct win_netmap_fd_list {
40337e3a6d3SLuigi Rizzo 	struct win_netmap_fd_list *next;
40437e3a6d3SLuigi Rizzo 	int win_netmap_fd;
40537e3a6d3SLuigi Rizzo 	HANDLE win_netmap_handle;
40637e3a6d3SLuigi Rizzo };
40737e3a6d3SLuigi Rizzo 
40837e3a6d3SLuigi Rizzo /*
40937e3a6d3SLuigi Rizzo  * list head containing all the netmap opened fd and their
41037e3a6d3SLuigi Rizzo  * windows HANDLE counterparts
41137e3a6d3SLuigi Rizzo  */
41237e3a6d3SLuigi Rizzo static struct win_netmap_fd_list *win_netmap_fd_list_head;
41337e3a6d3SLuigi Rizzo 
41437e3a6d3SLuigi Rizzo static void
win_insert_fd_record(int fd)41537e3a6d3SLuigi Rizzo win_insert_fd_record(int fd)
41637e3a6d3SLuigi Rizzo {
41737e3a6d3SLuigi Rizzo 	struct win_netmap_fd_list *curr;
41837e3a6d3SLuigi Rizzo 
41937e3a6d3SLuigi Rizzo 	for (curr = win_netmap_fd_list_head; curr; curr = curr->next) {
42037e3a6d3SLuigi Rizzo 		if (fd == curr->win_netmap_fd) {
42137e3a6d3SLuigi Rizzo 			return;
42237e3a6d3SLuigi Rizzo 		}
42337e3a6d3SLuigi Rizzo 	}
42437e3a6d3SLuigi Rizzo 	curr = calloc(1, sizeof(*curr));
42537e3a6d3SLuigi Rizzo 	curr->next = win_netmap_fd_list_head;
42637e3a6d3SLuigi Rizzo 	curr->win_netmap_fd = fd;
42737e3a6d3SLuigi Rizzo 	curr->win_netmap_handle = IntToPtr(_get_osfhandle(fd));
42837e3a6d3SLuigi Rizzo 	win_netmap_fd_list_head = curr;
42937e3a6d3SLuigi Rizzo }
43037e3a6d3SLuigi Rizzo 
43137e3a6d3SLuigi Rizzo void
win_remove_fd_record(int fd)43237e3a6d3SLuigi Rizzo win_remove_fd_record(int fd)
43337e3a6d3SLuigi Rizzo {
43437e3a6d3SLuigi Rizzo 	struct win_netmap_fd_list *curr = win_netmap_fd_list_head;
43537e3a6d3SLuigi Rizzo 	struct win_netmap_fd_list *prev = NULL;
43637e3a6d3SLuigi Rizzo 	for (; curr ; prev = curr, curr = curr->next) {
43737e3a6d3SLuigi Rizzo 		if (fd != curr->win_netmap_fd)
43837e3a6d3SLuigi Rizzo 			continue;
43937e3a6d3SLuigi Rizzo 		/* found the entry */
44037e3a6d3SLuigi Rizzo 		if (prev == NULL) { /* we are freeing the first entry */
44137e3a6d3SLuigi Rizzo 			win_netmap_fd_list_head = curr->next;
44237e3a6d3SLuigi Rizzo 		} else {
44337e3a6d3SLuigi Rizzo 			prev->next = curr->next;
44437e3a6d3SLuigi Rizzo 		}
44537e3a6d3SLuigi Rizzo 		free(curr);
44637e3a6d3SLuigi Rizzo 		break;
44737e3a6d3SLuigi Rizzo 	}
44837e3a6d3SLuigi Rizzo }
44937e3a6d3SLuigi Rizzo 
450a6d768d8SVincenzo Maffione 
45137e3a6d3SLuigi Rizzo HANDLE
win_get_netmap_handle(int fd)45237e3a6d3SLuigi Rizzo win_get_netmap_handle(int fd)
45337e3a6d3SLuigi Rizzo {
45437e3a6d3SLuigi Rizzo 	struct win_netmap_fd_list *curr;
45537e3a6d3SLuigi Rizzo 
45637e3a6d3SLuigi Rizzo 	for (curr = win_netmap_fd_list_head; curr; curr = curr->next) {
45737e3a6d3SLuigi Rizzo 		if (fd == curr->win_netmap_fd) {
45837e3a6d3SLuigi Rizzo 			return curr->win_netmap_handle;
45937e3a6d3SLuigi Rizzo 		}
46037e3a6d3SLuigi Rizzo 	}
46137e3a6d3SLuigi Rizzo 	return NULL;
46237e3a6d3SLuigi Rizzo }
46337e3a6d3SLuigi Rizzo 
46437e3a6d3SLuigi Rizzo /*
46537e3a6d3SLuigi Rizzo  * we need to wrap ioctl and mmap, at least for the netmap file descriptors
46637e3a6d3SLuigi Rizzo  */
46737e3a6d3SLuigi Rizzo 
46837e3a6d3SLuigi Rizzo /*
46937e3a6d3SLuigi Rizzo  * use this function only from netmap_user.h internal functions
47037e3a6d3SLuigi Rizzo  * same as ioctl, returns 0 on success and -1 on error
47137e3a6d3SLuigi Rizzo  */
47237e3a6d3SLuigi Rizzo static int
win_nm_ioctl_internal(HANDLE h,int32_t ctlCode,void * arg)47337e3a6d3SLuigi Rizzo win_nm_ioctl_internal(HANDLE h, int32_t ctlCode, void *arg)
47437e3a6d3SLuigi Rizzo {
47537e3a6d3SLuigi Rizzo 	DWORD bReturn = 0, szIn, szOut;
47637e3a6d3SLuigi Rizzo 	BOOL ioctlReturnStatus;
47737e3a6d3SLuigi Rizzo 	void *inParam = arg, *outParam = arg;
47837e3a6d3SLuigi Rizzo 
47937e3a6d3SLuigi Rizzo 	switch (ctlCode) {
48037e3a6d3SLuigi Rizzo 	case NETMAP_POLL:
48137e3a6d3SLuigi Rizzo 		szIn = sizeof(POLL_REQUEST_DATA);
48237e3a6d3SLuigi Rizzo 		szOut = sizeof(POLL_REQUEST_DATA);
48337e3a6d3SLuigi Rizzo 		break;
48437e3a6d3SLuigi Rizzo 	case NETMAP_MMAP:
48537e3a6d3SLuigi Rizzo 		szIn = 0;
48637e3a6d3SLuigi Rizzo 		szOut = sizeof(void*);
48737e3a6d3SLuigi Rizzo 		inParam = NULL; /* nothing on input */
48837e3a6d3SLuigi Rizzo 		break;
48937e3a6d3SLuigi Rizzo 	case NIOCTXSYNC:
49037e3a6d3SLuigi Rizzo 	case NIOCRXSYNC:
49137e3a6d3SLuigi Rizzo 		szIn = 0;
49237e3a6d3SLuigi Rizzo 		szOut = 0;
49337e3a6d3SLuigi Rizzo 		break;
49437e3a6d3SLuigi Rizzo 	case NIOCREGIF:
49537e3a6d3SLuigi Rizzo 		szIn = sizeof(struct nmreq);
49637e3a6d3SLuigi Rizzo 		szOut = sizeof(struct nmreq);
49737e3a6d3SLuigi Rizzo 		break;
49837e3a6d3SLuigi Rizzo 	case NIOCCONFIG:
49937e3a6d3SLuigi Rizzo 		D("unsupported NIOCCONFIG!");
50037e3a6d3SLuigi Rizzo 		return -1;
50137e3a6d3SLuigi Rizzo 
50237e3a6d3SLuigi Rizzo 	default: /* a regular ioctl */
50337e3a6d3SLuigi Rizzo 		D("invalid ioctl %x on netmap fd", ctlCode);
50437e3a6d3SLuigi Rizzo 		return -1;
50537e3a6d3SLuigi Rizzo 	}
50637e3a6d3SLuigi Rizzo 
50737e3a6d3SLuigi Rizzo 	ioctlReturnStatus = DeviceIoControl(h,
50837e3a6d3SLuigi Rizzo 				ctlCode, inParam, szIn,
50937e3a6d3SLuigi Rizzo 				outParam, szOut,
51037e3a6d3SLuigi Rizzo 				&bReturn, NULL);
51137e3a6d3SLuigi Rizzo 	// XXX note windows returns 0 on error or async call, 1 on success
51237e3a6d3SLuigi Rizzo 	// we could call GetLastError() to figure out what happened
51337e3a6d3SLuigi Rizzo 	return ioctlReturnStatus ? 0 : -1;
51437e3a6d3SLuigi Rizzo }
51537e3a6d3SLuigi Rizzo 
51637e3a6d3SLuigi Rizzo /*
51737e3a6d3SLuigi Rizzo  * this function is what must be called from user-space programs
51837e3a6d3SLuigi Rizzo  * same as ioctl, returns 0 on success and -1 on error
51937e3a6d3SLuigi Rizzo  */
52037e3a6d3SLuigi Rizzo static int
win_nm_ioctl(int fd,int32_t ctlCode,void * arg)52137e3a6d3SLuigi Rizzo win_nm_ioctl(int fd, int32_t ctlCode, void *arg)
52237e3a6d3SLuigi Rizzo {
52337e3a6d3SLuigi Rizzo 	HANDLE h = win_get_netmap_handle(fd);
52437e3a6d3SLuigi Rizzo 
52537e3a6d3SLuigi Rizzo 	if (h == NULL) {
52637e3a6d3SLuigi Rizzo 		return ioctl(fd, ctlCode, arg);
52737e3a6d3SLuigi Rizzo 	} else {
52837e3a6d3SLuigi Rizzo 		return win_nm_ioctl_internal(h, ctlCode, arg);
52937e3a6d3SLuigi Rizzo 	}
53037e3a6d3SLuigi Rizzo }
53137e3a6d3SLuigi Rizzo 
53237e3a6d3SLuigi Rizzo #define ioctl win_nm_ioctl /* from now on, within this file ... */
53337e3a6d3SLuigi Rizzo 
53437e3a6d3SLuigi Rizzo /*
53537e3a6d3SLuigi Rizzo  * We cannot use the native mmap on windows
53637e3a6d3SLuigi Rizzo  * The only parameter used is "fd", the other ones are just declared to
53737e3a6d3SLuigi Rizzo  * make this signature comparable to the FreeBSD/Linux one
53837e3a6d3SLuigi Rizzo  */
53937e3a6d3SLuigi Rizzo static void *
win32_mmap_emulated(void * addr,size_t length,int prot,int flags,int fd,int32_t offset)54037e3a6d3SLuigi Rizzo win32_mmap_emulated(void *addr, size_t length, int prot, int flags, int fd, int32_t offset)
54137e3a6d3SLuigi Rizzo {
54237e3a6d3SLuigi Rizzo 	HANDLE h = win_get_netmap_handle(fd);
54337e3a6d3SLuigi Rizzo 
54437e3a6d3SLuigi Rizzo 	if (h == NULL) {
54537e3a6d3SLuigi Rizzo 		return mmap(addr, length, prot, flags, fd, offset);
54637e3a6d3SLuigi Rizzo 	} else {
54737e3a6d3SLuigi Rizzo 		MEMORY_ENTRY ret;
54837e3a6d3SLuigi Rizzo 
54937e3a6d3SLuigi Rizzo 		return win_nm_ioctl_internal(h, NETMAP_MMAP, &ret) ?
55037e3a6d3SLuigi Rizzo 			NULL : ret.pUsermodeVirtualAddress;
55137e3a6d3SLuigi Rizzo 	}
55237e3a6d3SLuigi Rizzo }
55337e3a6d3SLuigi Rizzo 
55437e3a6d3SLuigi Rizzo #define mmap win32_mmap_emulated
55537e3a6d3SLuigi Rizzo 
55637e3a6d3SLuigi Rizzo #include <sys/poll.h> /* XXX needed to use the structure pollfd */
55737e3a6d3SLuigi Rizzo 
55837e3a6d3SLuigi Rizzo static int
win_nm_poll(struct pollfd * fds,int nfds,int timeout)55937e3a6d3SLuigi Rizzo win_nm_poll(struct pollfd *fds, int nfds, int timeout)
56037e3a6d3SLuigi Rizzo {
56137e3a6d3SLuigi Rizzo 	HANDLE h;
56237e3a6d3SLuigi Rizzo 
56337e3a6d3SLuigi Rizzo 	if (nfds != 1 || fds == NULL || (h = win_get_netmap_handle(fds->fd)) == NULL) {;
56437e3a6d3SLuigi Rizzo 		return poll(fds, nfds, timeout);
56537e3a6d3SLuigi Rizzo 	} else {
56637e3a6d3SLuigi Rizzo 		POLL_REQUEST_DATA prd;
56737e3a6d3SLuigi Rizzo 
56837e3a6d3SLuigi Rizzo 		prd.timeout = timeout;
56937e3a6d3SLuigi Rizzo 		prd.events = fds->events;
57037e3a6d3SLuigi Rizzo 
57137e3a6d3SLuigi Rizzo 		win_nm_ioctl_internal(h, NETMAP_POLL, &prd);
57237e3a6d3SLuigi Rizzo 		if ((prd.revents == POLLERR) || (prd.revents == STATUS_TIMEOUT)) {
57337e3a6d3SLuigi Rizzo 			return -1;
57437e3a6d3SLuigi Rizzo 		}
57537e3a6d3SLuigi Rizzo 		return 1;
57637e3a6d3SLuigi Rizzo 	}
57737e3a6d3SLuigi Rizzo }
57837e3a6d3SLuigi Rizzo 
57937e3a6d3SLuigi Rizzo #define poll win_nm_poll
58037e3a6d3SLuigi Rizzo 
58137e3a6d3SLuigi Rizzo static int
win_nm_open(char * pathname,int flags)58237e3a6d3SLuigi Rizzo win_nm_open(char* pathname, int flags)
58337e3a6d3SLuigi Rizzo {
58437e3a6d3SLuigi Rizzo 
58537e3a6d3SLuigi Rizzo 	if (strcmp(pathname, NETMAP_DEVICE_NAME) == 0) {
58637e3a6d3SLuigi Rizzo 		int fd = open(NETMAP_DEVICE_NAME, O_RDWR);
58737e3a6d3SLuigi Rizzo 		if (fd < 0) {
58837e3a6d3SLuigi Rizzo 			return -1;
58937e3a6d3SLuigi Rizzo 		}
59037e3a6d3SLuigi Rizzo 
59137e3a6d3SLuigi Rizzo 		win_insert_fd_record(fd);
59237e3a6d3SLuigi Rizzo 		return fd;
59337e3a6d3SLuigi Rizzo 	} else {
59437e3a6d3SLuigi Rizzo 		return open(pathname, flags);
59537e3a6d3SLuigi Rizzo 	}
59637e3a6d3SLuigi Rizzo }
59737e3a6d3SLuigi Rizzo 
59837e3a6d3SLuigi Rizzo #define open win_nm_open
59937e3a6d3SLuigi Rizzo 
60037e3a6d3SLuigi Rizzo static int
win_nm_close(int fd)60137e3a6d3SLuigi Rizzo win_nm_close(int fd)
60237e3a6d3SLuigi Rizzo {
60337e3a6d3SLuigi Rizzo 	if (fd != -1) {
60437e3a6d3SLuigi Rizzo 		close(fd);
60537e3a6d3SLuigi Rizzo 		if (win_get_netmap_handle(fd) != NULL) {
60637e3a6d3SLuigi Rizzo 			win_remove_fd_record(fd);
60737e3a6d3SLuigi Rizzo 		}
60837e3a6d3SLuigi Rizzo 	}
60937e3a6d3SLuigi Rizzo 	return 0;
61037e3a6d3SLuigi Rizzo }
61137e3a6d3SLuigi Rizzo 
61237e3a6d3SLuigi Rizzo #define close win_nm_close
61337e3a6d3SLuigi Rizzo 
61437e3a6d3SLuigi Rizzo #endif /* _WIN32 */
61537e3a6d3SLuigi Rizzo 
61637e3a6d3SLuigi Rizzo static int
nm_is_identifier(const char * s,const char * e)61737e3a6d3SLuigi Rizzo nm_is_identifier(const char *s, const char *e)
61837e3a6d3SLuigi Rizzo {
61937e3a6d3SLuigi Rizzo 	for (; s != e; s++) {
62037e3a6d3SLuigi Rizzo 		if (!isalnum(*s) && *s != '_') {
62137e3a6d3SLuigi Rizzo 			return 0;
62237e3a6d3SLuigi Rizzo 		}
62337e3a6d3SLuigi Rizzo 	}
62437e3a6d3SLuigi Rizzo 
62537e3a6d3SLuigi Rizzo 	return 1;
62637e3a6d3SLuigi Rizzo }
627f9790aebSLuigi Rizzo 
6284f80b14cSVincenzo Maffione #define MAXERRMSG 80
6294f80b14cSVincenzo Maffione static int
nm_parse(const char * ifname,struct nm_desc * d,char * err)6302ff91c17SVincenzo Maffione nm_parse(const char *ifname, struct nm_desc *d, char *err)
6314f80b14cSVincenzo Maffione {
6324f80b14cSVincenzo Maffione 	int is_vale;
633f0ea3689SLuigi Rizzo 	const char *port = NULL;
63437e3a6d3SLuigi Rizzo 	const char *vpname = NULL;
6354f80b14cSVincenzo Maffione 	u_int namelen;
6364f80b14cSVincenzo Maffione 	uint32_t nr_ringid = 0, nr_flags;
637adf41f07SVincenzo Maffione 	char errmsg[MAXERRMSG] = "", *tmp;
638847bf383SLuigi Rizzo 	long num;
639f9eece46SLuiz Otavio O Souza 	uint16_t nr_arg2 = 0;
6404f80b14cSVincenzo Maffione 	enum { P_START, P_RNGSFXOK, P_GETNUM, P_FLAGS, P_FLAGSOK, P_MEMID } p_state;
6414f80b14cSVincenzo Maffione 
6424f80b14cSVincenzo Maffione 	errno = 0;
643f9790aebSLuigi Rizzo 
64437e3a6d3SLuigi Rizzo 	is_vale = (ifname[0] == 'v');
64537e3a6d3SLuigi Rizzo 	if (is_vale) {
64637e3a6d3SLuigi Rizzo 		port = index(ifname, ':');
64737e3a6d3SLuigi Rizzo 		if (port == NULL) {
64837e3a6d3SLuigi Rizzo 			snprintf(errmsg, MAXERRMSG,
64937e3a6d3SLuigi Rizzo 				 "missing ':' in vale name");
65037e3a6d3SLuigi Rizzo 			goto fail;
65137e3a6d3SLuigi Rizzo 		}
65237e3a6d3SLuigi Rizzo 
65337e3a6d3SLuigi Rizzo 		if (!nm_is_identifier(ifname + 4, port)) {
65437e3a6d3SLuigi Rizzo 			snprintf(errmsg, MAXERRMSG, "invalid bridge name");
65537e3a6d3SLuigi Rizzo 			goto fail;
65637e3a6d3SLuigi Rizzo 		}
65737e3a6d3SLuigi Rizzo 
65837e3a6d3SLuigi Rizzo 		vpname = ++port;
65937e3a6d3SLuigi Rizzo 	} else {
660f9790aebSLuigi Rizzo 		ifname += 7;
66137e3a6d3SLuigi Rizzo 		port = ifname;
66237e3a6d3SLuigi Rizzo 	}
66337e3a6d3SLuigi Rizzo 
664f0ea3689SLuigi Rizzo 	/* scan for a separator */
665f9eece46SLuiz Otavio O Souza 	for (; *port && !index("-*^{}/@", *port); port++)
666f0ea3689SLuigi Rizzo 		;
66737e3a6d3SLuigi Rizzo 
66837e3a6d3SLuigi Rizzo 	if (is_vale && !nm_is_identifier(vpname, port)) {
66937e3a6d3SLuigi Rizzo 		snprintf(errmsg, MAXERRMSG, "invalid bridge port name");
67037e3a6d3SLuigi Rizzo 		goto fail;
67137e3a6d3SLuigi Rizzo 	}
67237e3a6d3SLuigi Rizzo 
67366015019SLuigi Rizzo 	namelen = port - ifname;
6742ff91c17SVincenzo Maffione 	if (namelen >= sizeof(d->req.nr_name)) {
675847bf383SLuigi Rizzo 		snprintf(errmsg, MAXERRMSG, "name too long");
676f0ea3689SLuigi Rizzo 		goto fail;
67766015019SLuigi Rizzo 	}
6782ff91c17SVincenzo Maffione 	memcpy(d->req.nr_name, ifname, namelen);
6792ff91c17SVincenzo Maffione 	d->req.nr_name[namelen] = '\0';
6804f80b14cSVincenzo Maffione 
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;
70345c67e8fSVincenzo Maffione 			case '}': /* pipe (slave endpoint) */
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;
710f9eece46SLuiz Otavio O Souza 			case '@': /* start of memid */
711f9eece46SLuiz Otavio O Souza 				p_state = P_MEMID;
712f9eece46SLuiz Otavio O Souza 				break;
713847bf383SLuigi Rizzo 			default:
714847bf383SLuigi Rizzo 				snprintf(errmsg, MAXERRMSG, "unknown modifier: '%c'", *port);
715f0ea3689SLuigi Rizzo 				goto fail;
716f0ea3689SLuigi Rizzo 			}
717847bf383SLuigi Rizzo 			port++;
718847bf383SLuigi Rizzo 			break;
719847bf383SLuigi Rizzo 		case P_RNGSFXOK:
720847bf383SLuigi Rizzo 			switch (*port) {
721847bf383SLuigi Rizzo 			case '/':
722847bf383SLuigi Rizzo 				p_state = P_FLAGS;
723847bf383SLuigi Rizzo 				break;
724f9eece46SLuiz Otavio O Souza 			case '@':
725f9eece46SLuiz Otavio O Souza 				p_state = P_MEMID;
726f9eece46SLuiz Otavio O Souza 				break;
727847bf383SLuigi Rizzo 			default:
728847bf383SLuigi Rizzo 				snprintf(errmsg, MAXERRMSG, "unexpected character: '%c'", *port);
729847bf383SLuigi Rizzo 				goto fail;
730847bf383SLuigi Rizzo 			}
731847bf383SLuigi Rizzo 			port++;
732847bf383SLuigi Rizzo 			break;
733847bf383SLuigi Rizzo 		case P_GETNUM:
734adf41f07SVincenzo Maffione 			num = strtol(port, &tmp, 10);
735847bf383SLuigi Rizzo 			if (num < 0 || num >= NETMAP_RING_MASK) {
736847bf383SLuigi Rizzo 				snprintf(errmsg, MAXERRMSG, "'%ld' out of range [0, %d)",
737847bf383SLuigi Rizzo 						num, NETMAP_RING_MASK);
738847bf383SLuigi Rizzo 				goto fail;
739847bf383SLuigi Rizzo 			}
740adf41f07SVincenzo Maffione 			port = tmp;
741847bf383SLuigi Rizzo 			nr_ringid = num & NETMAP_RING_MASK;
742847bf383SLuigi Rizzo 			p_state = P_RNGSFXOK;
743847bf383SLuigi Rizzo 			break;
744847bf383SLuigi Rizzo 		case P_FLAGS:
745847bf383SLuigi Rizzo 		case P_FLAGSOK:
746f9eece46SLuiz Otavio O Souza 			if (*port == '@') {
747f9eece46SLuiz Otavio O Souza 				port++;
748f9eece46SLuiz Otavio O Souza 				p_state = P_MEMID;
749f9eece46SLuiz Otavio O Souza 				break;
750f9eece46SLuiz Otavio O Souza 			}
751847bf383SLuigi Rizzo 			switch (*port) {
752847bf383SLuigi Rizzo 			case 'x':
753847bf383SLuigi Rizzo 				nr_flags |= NR_EXCLUSIVE;
754847bf383SLuigi Rizzo 				break;
755847bf383SLuigi Rizzo 			case 'z':
756847bf383SLuigi Rizzo 				nr_flags |= NR_ZCOPY_MON;
757847bf383SLuigi Rizzo 				break;
758847bf383SLuigi Rizzo 			case 't':
759847bf383SLuigi Rizzo 				nr_flags |= NR_MONITOR_TX;
760847bf383SLuigi Rizzo 				break;
761847bf383SLuigi Rizzo 			case 'r':
762847bf383SLuigi Rizzo 				nr_flags |= NR_MONITOR_RX;
763847bf383SLuigi Rizzo 				break;
76437e3a6d3SLuigi Rizzo 			case 'R':
76537e3a6d3SLuigi Rizzo 				nr_flags |= NR_RX_RINGS_ONLY;
76637e3a6d3SLuigi Rizzo 				break;
76737e3a6d3SLuigi Rizzo 			case 'T':
76837e3a6d3SLuigi Rizzo 				nr_flags |= NR_TX_RINGS_ONLY;
76937e3a6d3SLuigi Rizzo 				break;
770847bf383SLuigi Rizzo 			default:
771847bf383SLuigi Rizzo 				snprintf(errmsg, MAXERRMSG, "unrecognized flag: '%c'", *port);
772847bf383SLuigi Rizzo 				goto fail;
773847bf383SLuigi Rizzo 			}
774847bf383SLuigi Rizzo 			port++;
775847bf383SLuigi Rizzo 			p_state = P_FLAGSOK;
776847bf383SLuigi Rizzo 			break;
777f9eece46SLuiz Otavio O Souza 		case P_MEMID:
7782ff91c17SVincenzo Maffione 			if (nr_arg2 != 0) {
779f9eece46SLuiz Otavio O Souza 				snprintf(errmsg, MAXERRMSG, "double setting of memid");
780f9eece46SLuiz Otavio O Souza 				goto fail;
781f9eece46SLuiz Otavio O Souza 			}
782adf41f07SVincenzo Maffione 			num = strtol(port, &tmp, 10);
783f9eece46SLuiz Otavio O Souza 			if (num <= 0) {
7842ff91c17SVincenzo Maffione 				snprintf(errmsg, MAXERRMSG, "invalid memid %ld, must be >0", num);
785f9eece46SLuiz Otavio O Souza 				goto fail;
7864f80b14cSVincenzo Maffione 			}
787adf41f07SVincenzo Maffione 			port = tmp;
7882ff91c17SVincenzo Maffione 			nr_arg2 = num;
7892ff91c17SVincenzo Maffione 			p_state = P_RNGSFXOK;
790f9eece46SLuiz Otavio O Souza 			break;
791847bf383SLuigi Rizzo 		}
792847bf383SLuigi Rizzo 	}
7932ff91c17SVincenzo Maffione 	if (p_state != P_START && p_state != P_RNGSFXOK && p_state != P_FLAGSOK) {
794847bf383SLuigi Rizzo 		snprintf(errmsg, MAXERRMSG, "unexpected end of port name");
795847bf383SLuigi Rizzo 		goto fail;
796847bf383SLuigi Rizzo 	}
797847bf383SLuigi Rizzo 	ND("flags: %s %s %s %s",
798847bf383SLuigi Rizzo 			(nr_flags & NR_EXCLUSIVE) ? "EXCLUSIVE" : "",
799847bf383SLuigi Rizzo 			(nr_flags & NR_ZCOPY_MON) ? "ZCOPY_MON" : "",
800847bf383SLuigi Rizzo 			(nr_flags & NR_MONITOR_TX) ? "MONITOR_TX" : "",
801847bf383SLuigi Rizzo 			(nr_flags & NR_MONITOR_RX) ? "MONITOR_RX" : "");
8024f80b14cSVincenzo Maffione 
8032ff91c17SVincenzo Maffione 	d->req.nr_flags |= nr_flags;
8042ff91c17SVincenzo Maffione 	d->req.nr_ringid |= nr_ringid;
8052ff91c17SVincenzo Maffione 	d->req.nr_arg2 = nr_arg2;
8064f80b14cSVincenzo Maffione 
8072ff91c17SVincenzo Maffione 	d->self = d;
8082ff91c17SVincenzo Maffione 
8092ff91c17SVincenzo Maffione 	return 0;
8104f80b14cSVincenzo Maffione fail:
8114f80b14cSVincenzo Maffione 	if (!errno)
8124f80b14cSVincenzo Maffione 		errno = EINVAL;
8132ff91c17SVincenzo Maffione 	if (err)
8142ff91c17SVincenzo Maffione 		strncpy(err, errmsg, MAXERRMSG);
8154f80b14cSVincenzo Maffione 	return -1;
8164f80b14cSVincenzo Maffione }
8174f80b14cSVincenzo Maffione 
8184f80b14cSVincenzo Maffione /*
8194f80b14cSVincenzo Maffione  * Try to open, return descriptor if successful, NULL otherwise.
8204f80b14cSVincenzo Maffione  * An invalid netmap name will return errno = 0;
8214f80b14cSVincenzo Maffione  * You can pass a pointer to a pre-filled nm_desc to add special
8224f80b14cSVincenzo Maffione  * parameters. Flags is used as follows
8232ff91c17SVincenzo Maffione  * NM_OPEN_NO_MMAP	use the memory from arg, only XXX avoid mmap
8244f80b14cSVincenzo Maffione  *			if the nr_arg2 (memory block) matches.
8254f80b14cSVincenzo Maffione  * NM_OPEN_ARG1		use req.nr_arg1 from arg
8264f80b14cSVincenzo Maffione  * NM_OPEN_ARG2		use req.nr_arg2 from arg
8274f80b14cSVincenzo Maffione  * NM_OPEN_RING_CFG	user ring config from arg
8284f80b14cSVincenzo Maffione  */
8294f80b14cSVincenzo Maffione static struct nm_desc *
nm_open(const char * ifname,const struct nmreq * req,uint64_t new_flags,const struct nm_desc * arg)8304f80b14cSVincenzo Maffione nm_open(const char *ifname, const struct nmreq *req,
8314f80b14cSVincenzo Maffione 	uint64_t new_flags, const struct nm_desc *arg)
8324f80b14cSVincenzo Maffione {
8334f80b14cSVincenzo Maffione 	struct nm_desc *d = NULL;
8344f80b14cSVincenzo Maffione 	const struct nm_desc *parent = arg;
8354f80b14cSVincenzo Maffione 	char errmsg[MAXERRMSG] = "";
8364f80b14cSVincenzo Maffione 	uint32_t nr_reg;
8374f80b14cSVincenzo Maffione 
8384f80b14cSVincenzo Maffione 	if (strncmp(ifname, "netmap:", 7) &&
8394f80b14cSVincenzo Maffione 			strncmp(ifname, NM_BDG_NAME, strlen(NM_BDG_NAME))) {
8404f80b14cSVincenzo Maffione 		errno = 0; /* name not recognised, not an error */
8414f80b14cSVincenzo Maffione 		return NULL;
8424f80b14cSVincenzo Maffione 	}
8434f80b14cSVincenzo Maffione 
844f0ea3689SLuigi Rizzo 	d = (struct nm_desc *)calloc(1, sizeof(*d));
845f9790aebSLuigi Rizzo 	if (d == NULL) {
846847bf383SLuigi Rizzo 		snprintf(errmsg, MAXERRMSG, "nm_desc alloc failure");
847f9790aebSLuigi Rizzo 		errno = ENOMEM;
848f9790aebSLuigi Rizzo 		return NULL;
849f9790aebSLuigi Rizzo 	}
850f9790aebSLuigi Rizzo 	d->self = d;	/* set this early so nm_close() works */
85137e3a6d3SLuigi Rizzo 	d->fd = open(NETMAP_DEVICE_NAME, O_RDWR);
852f0ea3689SLuigi Rizzo 	if (d->fd < 0) {
853847bf383SLuigi Rizzo 		snprintf(errmsg, MAXERRMSG, "cannot open /dev/netmap: %s", strerror(errno));
854f9790aebSLuigi Rizzo 		goto fail;
855f9790aebSLuigi Rizzo 	}
856f0ea3689SLuigi Rizzo 
8572ff91c17SVincenzo Maffione 	if (req)
858f0ea3689SLuigi Rizzo 		d->req = *req;
859f0ea3689SLuigi Rizzo 
8604f80b14cSVincenzo Maffione 	if (!(new_flags & NM_OPEN_IFNAME)) {
8612ff91c17SVincenzo Maffione 		if (nm_parse(ifname, d, errmsg) < 0)
8624f80b14cSVincenzo Maffione 			goto fail;
8634f80b14cSVincenzo Maffione 	}
8644f80b14cSVincenzo Maffione 
8654f80b14cSVincenzo Maffione 	d->req.nr_version = NETMAP_API;
8664f80b14cSVincenzo Maffione 	d->req.nr_ringid &= NETMAP_RING_MASK;
8674f80b14cSVincenzo Maffione 
868f0ea3689SLuigi Rizzo 	/* optionally import info from parent */
869f0ea3689SLuigi Rizzo 	if (IS_NETMAP_DESC(parent) && new_flags) {
8702ff91c17SVincenzo Maffione 		if (new_flags & NM_OPEN_ARG1)
871f0ea3689SLuigi Rizzo 			D("overriding ARG1 %d", parent->req.nr_arg1);
8722ff91c17SVincenzo Maffione 		d->req.nr_arg1 = new_flags & NM_OPEN_ARG1 ?
8732ff91c17SVincenzo Maffione 			parent->req.nr_arg1 : 4;
8742ff91c17SVincenzo Maffione 		if (new_flags & NM_OPEN_ARG2) {
875f0ea3689SLuigi Rizzo 			D("overriding ARG2 %d", parent->req.nr_arg2);
8764f80b14cSVincenzo Maffione 			d->req.nr_arg2 =  parent->req.nr_arg2;
8774f80b14cSVincenzo Maffione 		}
8782ff91c17SVincenzo Maffione 		if (new_flags & NM_OPEN_ARG3)
879f0ea3689SLuigi Rizzo 			D("overriding ARG3 %d", parent->req.nr_arg3);
8802ff91c17SVincenzo Maffione 		d->req.nr_arg3 = new_flags & NM_OPEN_ARG3 ?
8812ff91c17SVincenzo Maffione 			parent->req.nr_arg3 : 0;
882f0ea3689SLuigi Rizzo 		if (new_flags & NM_OPEN_RING_CFG) {
883f0ea3689SLuigi Rizzo 			D("overriding RING_CFG");
884f0ea3689SLuigi Rizzo 			d->req.nr_tx_slots = parent->req.nr_tx_slots;
885f0ea3689SLuigi Rizzo 			d->req.nr_rx_slots = parent->req.nr_rx_slots;
886f0ea3689SLuigi Rizzo 			d->req.nr_tx_rings = parent->req.nr_tx_rings;
887f0ea3689SLuigi Rizzo 			d->req.nr_rx_rings = parent->req.nr_rx_rings;
888f0ea3689SLuigi Rizzo 		}
889f0ea3689SLuigi Rizzo 		if (new_flags & NM_OPEN_IFNAME) {
890f0ea3689SLuigi Rizzo 			D("overriding ifname %s ringid 0x%x flags 0x%x",
891f0ea3689SLuigi Rizzo 				parent->req.nr_name, parent->req.nr_ringid,
892f0ea3689SLuigi Rizzo 				parent->req.nr_flags);
893f0ea3689SLuigi Rizzo 			memcpy(d->req.nr_name, parent->req.nr_name,
894f0ea3689SLuigi Rizzo 				sizeof(d->req.nr_name));
895f0ea3689SLuigi Rizzo 			d->req.nr_ringid = parent->req.nr_ringid;
896f0ea3689SLuigi Rizzo 			d->req.nr_flags = parent->req.nr_flags;
897f0ea3689SLuigi Rizzo 		}
898f0ea3689SLuigi Rizzo 	}
8999225c808SLuigi Rizzo 	/* add the *XPOLL flags */
9009225c808SLuigi Rizzo 	d->req.nr_ringid |= new_flags & (NETMAP_NO_TX_POLL | NETMAP_DO_RX_POLL);
9019225c808SLuigi Rizzo 
90266015019SLuigi Rizzo 	if (ioctl(d->fd, NIOCREGIF, &d->req)) {
903847bf383SLuigi Rizzo 		snprintf(errmsg, MAXERRMSG, "NIOCREGIF failed: %s", strerror(errno));
904f9790aebSLuigi Rizzo 		goto fail;
90566015019SLuigi Rizzo 	}
906f9790aebSLuigi Rizzo 
907847bf383SLuigi Rizzo 	nr_reg = d->req.nr_flags & NR_REG_MASK;
908847bf383SLuigi Rizzo 
909847bf383SLuigi Rizzo 	if (nr_reg == NR_REG_SW) { /* host stack */
91066015019SLuigi Rizzo 		d->first_tx_ring = d->last_tx_ring = d->req.nr_tx_rings;
91166015019SLuigi Rizzo 		d->first_rx_ring = d->last_rx_ring = d->req.nr_rx_rings;
912847bf383SLuigi Rizzo 	} else if (nr_reg ==  NR_REG_ALL_NIC) { /* only nic */
913f0ea3689SLuigi Rizzo 		d->first_tx_ring = 0;
914f0ea3689SLuigi Rizzo 		d->first_rx_ring = 0;
91566015019SLuigi Rizzo 		d->last_tx_ring = d->req.nr_tx_rings - 1;
91666015019SLuigi Rizzo 		d->last_rx_ring = d->req.nr_rx_rings - 1;
917847bf383SLuigi Rizzo 	} else if (nr_reg ==  NR_REG_NIC_SW) {
918f0ea3689SLuigi Rizzo 		d->first_tx_ring = 0;
919f0ea3689SLuigi Rizzo 		d->first_rx_ring = 0;
920f0ea3689SLuigi Rizzo 		d->last_tx_ring = d->req.nr_tx_rings;
921f0ea3689SLuigi Rizzo 		d->last_rx_ring = d->req.nr_rx_rings;
922847bf383SLuigi Rizzo 	} else if (nr_reg == NR_REG_ONE_NIC) {
923f0ea3689SLuigi Rizzo 		/* XXX check validity */
924f0ea3689SLuigi Rizzo 		d->first_tx_ring = d->last_tx_ring =
9254bf50f18SLuigi Rizzo 		d->first_rx_ring = d->last_rx_ring = d->req.nr_ringid & NETMAP_RING_MASK;
926f0ea3689SLuigi Rizzo 	} else { /* pipes */
927f0ea3689SLuigi Rizzo 		d->first_tx_ring = d->last_tx_ring = 0;
928f0ea3689SLuigi Rizzo 		d->first_rx_ring = d->last_rx_ring = 0;
929f9790aebSLuigi Rizzo 	}
930f0ea3689SLuigi Rizzo 
9312ff91c17SVincenzo Maffione         /* if parent is defined, do nm_mmap() even if NM_OPEN_NO_MMAP is set */
9322ff91c17SVincenzo Maffione 	if ((!(new_flags & NM_OPEN_NO_MMAP) || parent) && nm_mmap(d, parent)) {
9332ff91c17SVincenzo Maffione 	        snprintf(errmsg, MAXERRMSG, "mmap failed: %s", strerror(errno));
9342ff91c17SVincenzo Maffione 		goto fail;
9352ff91c17SVincenzo Maffione 	}
9362ff91c17SVincenzo Maffione 
937a6d768d8SVincenzo Maffione 
938f0ea3689SLuigi Rizzo #ifdef DEBUG_NETMAP_USER
939f0ea3689SLuigi Rizzo     { /* debugging code */
940f0ea3689SLuigi Rizzo 	int i;
941f0ea3689SLuigi Rizzo 
942f0ea3689SLuigi Rizzo 	D("%s tx %d .. %d %d rx %d .. %d %d", ifname,
943f0ea3689SLuigi Rizzo 		d->first_tx_ring, d->last_tx_ring, d->req.nr_tx_rings,
944f0ea3689SLuigi Rizzo                 d->first_rx_ring, d->last_rx_ring, d->req.nr_rx_rings);
945f0ea3689SLuigi Rizzo 	for (i = 0; i <= d->req.nr_tx_rings; i++) {
946f0ea3689SLuigi Rizzo 		struct netmap_ring *r = NETMAP_TXRING(d->nifp, i);
947f0ea3689SLuigi Rizzo 		D("TX%d %p h %d c %d t %d", i, r, r->head, r->cur, r->tail);
948f0ea3689SLuigi Rizzo 	}
949f0ea3689SLuigi Rizzo 	for (i = 0; i <= d->req.nr_rx_rings; i++) {
950f0ea3689SLuigi Rizzo 		struct netmap_ring *r = NETMAP_RXRING(d->nifp, i);
951f0ea3689SLuigi Rizzo 		D("RX%d %p h %d c %d t %d", i, r, r->head, r->cur, r->tail);
952f0ea3689SLuigi Rizzo 	}
953f0ea3689SLuigi Rizzo     }
954f0ea3689SLuigi Rizzo #endif /* debugging */
955f0ea3689SLuigi Rizzo 
95666015019SLuigi Rizzo 	d->cur_tx_ring = d->first_tx_ring;
95766015019SLuigi Rizzo 	d->cur_rx_ring = d->first_rx_ring;
958f9790aebSLuigi Rizzo 	return d;
959f9790aebSLuigi Rizzo 
960f9790aebSLuigi Rizzo fail:
961f9790aebSLuigi Rizzo 	nm_close(d);
962847bf383SLuigi Rizzo 	if (errmsg[0])
963f0ea3689SLuigi Rizzo 		D("%s %s", errmsg, ifname);
9640506889cSLuigi Rizzo 	if (errno == 0)
965f9790aebSLuigi Rizzo 		errno = EINVAL;
966f9790aebSLuigi Rizzo 	return NULL;
967f9790aebSLuigi Rizzo }
968f9790aebSLuigi Rizzo 
969a6d768d8SVincenzo Maffione 
970f9790aebSLuigi Rizzo static int
nm_close(struct nm_desc * d)971f0ea3689SLuigi Rizzo nm_close(struct nm_desc *d)
972f9790aebSLuigi Rizzo {
97317885a7bSLuigi Rizzo 	/*
97417885a7bSLuigi Rizzo 	 * ugly trick to avoid unused warnings
97517885a7bSLuigi Rizzo 	 */
97617885a7bSLuigi Rizzo 	static void *__xxzt[] __attribute__ ((unused))  =
977b82b2211SLuigi Rizzo 		{ (void *)nm_open, (void *)nm_inject,
9782ff91c17SVincenzo Maffione 		  (void *)nm_dispatch, (void *)nm_nextpkt } ;
97917885a7bSLuigi Rizzo 
980f9790aebSLuigi Rizzo 	if (d == NULL || d->self != d)
981f9790aebSLuigi Rizzo 		return EINVAL;
982f0ea3689SLuigi Rizzo 	if (d->done_mmap && d->mem)
983f9790aebSLuigi Rizzo 		munmap(d->mem, d->memsize);
98437e3a6d3SLuigi Rizzo 	if (d->fd != -1) {
985f9790aebSLuigi Rizzo 		close(d->fd);
98637e3a6d3SLuigi Rizzo 	}
98737e3a6d3SLuigi Rizzo 
98809a18933SVincenzo Maffione 	bzero((char *)d, sizeof(*d));
989f9790aebSLuigi Rizzo 	free(d);
990f9790aebSLuigi Rizzo 	return 0;
991f9790aebSLuigi Rizzo }
992f9790aebSLuigi Rizzo 
99345c67e8fSVincenzo Maffione 
99437e3a6d3SLuigi Rizzo static int
nm_mmap(struct nm_desc * d,const struct nm_desc * parent)99537e3a6d3SLuigi Rizzo nm_mmap(struct nm_desc *d, const struct nm_desc *parent)
99637e3a6d3SLuigi Rizzo {
9979bad2638SVincenzo Maffione 	if (d->done_mmap)
9989bad2638SVincenzo Maffione 		return 0;
99937e3a6d3SLuigi Rizzo 
100037e3a6d3SLuigi Rizzo 	if (IS_NETMAP_DESC(parent) && parent->mem &&
100137e3a6d3SLuigi Rizzo 	    parent->req.nr_arg2 == d->req.nr_arg2) {
100237e3a6d3SLuigi Rizzo 		/* do not mmap, inherit from parent */
100337e3a6d3SLuigi Rizzo 		D("do not mmap, inherit from parent");
100437e3a6d3SLuigi Rizzo 		d->memsize = parent->memsize;
100537e3a6d3SLuigi Rizzo 		d->mem = parent->mem;
100637e3a6d3SLuigi Rizzo 	} else {
100737e3a6d3SLuigi Rizzo 		/* XXX TODO: check if memsize is too large (or there is overflow) */
100837e3a6d3SLuigi Rizzo 		d->memsize = d->req.nr_memsize;
100937e3a6d3SLuigi Rizzo 		d->mem = mmap(0, d->memsize, PROT_WRITE | PROT_READ, MAP_SHARED,
101037e3a6d3SLuigi Rizzo 				d->fd, 0);
101137e3a6d3SLuigi Rizzo 		if (d->mem == MAP_FAILED) {
101237e3a6d3SLuigi Rizzo 			goto fail;
101337e3a6d3SLuigi Rizzo 		}
101437e3a6d3SLuigi Rizzo 		d->done_mmap = 1;
101537e3a6d3SLuigi Rizzo 	}
10162ff91c17SVincenzo Maffione 	{
10172ff91c17SVincenzo Maffione 		struct netmap_if *nifp = NETMAP_IF(d->mem, d->req.nr_offset);
10182ff91c17SVincenzo Maffione 		struct netmap_ring *r = NETMAP_RXRING(nifp, d->first_rx_ring);
10192ff91c17SVincenzo Maffione 		if ((void *)r == (void *)nifp) {
10202ff91c17SVincenzo Maffione 			/* the descriptor is open for TX only */
10212ff91c17SVincenzo Maffione 			r = NETMAP_TXRING(nifp, d->first_tx_ring);
10222ff91c17SVincenzo Maffione 		}
102337e3a6d3SLuigi Rizzo 
10242ff91c17SVincenzo Maffione 		*(struct netmap_if **)(uintptr_t)&(d->nifp) = nifp;
10252ff91c17SVincenzo Maffione 		*(struct netmap_ring **)(uintptr_t)&d->some_ring = r;
10262ff91c17SVincenzo Maffione 		*(void **)(uintptr_t)&d->buf_start = NETMAP_BUF(r, 0);
10272ff91c17SVincenzo Maffione 		*(void **)(uintptr_t)&d->buf_end =
10282ff91c17SVincenzo Maffione 			(char *)d->mem + d->memsize;
10292ff91c17SVincenzo Maffione 	}
10302ff91c17SVincenzo Maffione 
103137e3a6d3SLuigi Rizzo 	return 0;
103237e3a6d3SLuigi Rizzo 
103337e3a6d3SLuigi Rizzo fail:
103437e3a6d3SLuigi Rizzo 	return EINVAL;
103537e3a6d3SLuigi Rizzo }
103637e3a6d3SLuigi Rizzo 
1037f9790aebSLuigi Rizzo /*
103817885a7bSLuigi Rizzo  * Same prototype as pcap_inject(), only need to cast.
103917885a7bSLuigi Rizzo  */
104017885a7bSLuigi Rizzo static int
nm_inject(struct nm_desc * d,const void * buf,size_t size)1041f0ea3689SLuigi Rizzo nm_inject(struct nm_desc *d, const void *buf, size_t size)
104217885a7bSLuigi Rizzo {
10434f80b14cSVincenzo Maffione 	u_int c, n = d->last_tx_ring - d->first_tx_ring + 1,
10444f80b14cSVincenzo Maffione 		ri = d->cur_tx_ring;
104517885a7bSLuigi Rizzo 
10464f80b14cSVincenzo Maffione 	for (c = 0; c < n ; c++, ri++) {
104717885a7bSLuigi Rizzo 		/* compute current ring to use */
104817885a7bSLuigi Rizzo 		struct netmap_ring *ring;
10492a7db7a6SVincenzo Maffione 		uint32_t i, j, idx;
10502a7db7a6SVincenzo Maffione 		size_t rem;
105117885a7bSLuigi Rizzo 
105266015019SLuigi Rizzo 		if (ri > d->last_tx_ring)
105366015019SLuigi Rizzo 			ri = d->first_tx_ring;
105417885a7bSLuigi Rizzo 		ring = NETMAP_TXRING(d->nifp, ri);
10552a7db7a6SVincenzo Maffione 		rem = size;
10562a7db7a6SVincenzo Maffione 		j = ring->cur;
10572a7db7a6SVincenzo Maffione 		while (rem > ring->nr_buf_size && j != ring->tail) {
10582a7db7a6SVincenzo Maffione 			rem -= ring->nr_buf_size;
10592a7db7a6SVincenzo Maffione 			j = nm_ring_next(ring, j);
106017885a7bSLuigi Rizzo 		}
10612a7db7a6SVincenzo Maffione 		if (j == ring->tail && rem > 0)
10622a7db7a6SVincenzo Maffione 			continue;
106317885a7bSLuigi Rizzo 		i = ring->cur;
10642a7db7a6SVincenzo Maffione 		while (i != j) {
106517885a7bSLuigi Rizzo 			idx = ring->slot[i].buf_idx;
10662a7db7a6SVincenzo Maffione 			ring->slot[i].len = ring->nr_buf_size;
10672a7db7a6SVincenzo Maffione 			ring->slot[i].flags = NS_MOREFRAG;
10682a7db7a6SVincenzo Maffione 			nm_pkt_copy(buf, NETMAP_BUF(ring, idx), ring->nr_buf_size);
10692a7db7a6SVincenzo Maffione 			i = nm_ring_next(ring, i);
1070adf41f07SVincenzo Maffione 			buf = (const char *)buf + ring->nr_buf_size;
10712a7db7a6SVincenzo Maffione 		}
10722a7db7a6SVincenzo Maffione 		idx = ring->slot[i].buf_idx;
10732a7db7a6SVincenzo Maffione 		ring->slot[i].len = rem;
10742a7db7a6SVincenzo Maffione 		ring->slot[i].flags = 0;
10752a7db7a6SVincenzo Maffione 		nm_pkt_copy(buf, NETMAP_BUF(ring, idx), rem);
107617885a7bSLuigi Rizzo 		ring->head = ring->cur = nm_ring_next(ring, i);
10772a7db7a6SVincenzo Maffione 		d->cur_tx_ring = ri;
107817885a7bSLuigi Rizzo 		return size;
107917885a7bSLuigi Rizzo 	}
108017885a7bSLuigi Rizzo 	return 0; /* fail */
108117885a7bSLuigi Rizzo }
108217885a7bSLuigi Rizzo 
1083a6d768d8SVincenzo Maffione 
108417885a7bSLuigi Rizzo /*
1085f9790aebSLuigi Rizzo  * Same prototype as pcap_dispatch(), only need to cast.
1086f9790aebSLuigi Rizzo  */
1087f9790aebSLuigi Rizzo static int
nm_dispatch(struct nm_desc * d,int cnt,nm_cb_t cb,u_char * arg)1088f0ea3689SLuigi Rizzo nm_dispatch(struct nm_desc *d, int cnt, nm_cb_t cb, u_char *arg)
1089f9790aebSLuigi Rizzo {
109066015019SLuigi Rizzo 	int n = d->last_rx_ring - d->first_rx_ring + 1;
109166015019SLuigi Rizzo 	int c, got = 0, ri = d->cur_rx_ring;
109237e3a6d3SLuigi Rizzo 	d->hdr.buf = NULL;
109337e3a6d3SLuigi Rizzo 	d->hdr.flags = NM_MORE_PKTS;
109437e3a6d3SLuigi Rizzo 	d->hdr.d = d;
1095f9790aebSLuigi Rizzo 
1096f9790aebSLuigi Rizzo 	if (cnt == 0)
1097f9790aebSLuigi Rizzo 		cnt = -1;
1098f9790aebSLuigi Rizzo 	/* cnt == -1 means infinite, but rings have a finite amount
1099f9790aebSLuigi Rizzo 	 * of buffers and the int is large enough that we never wrap,
1100f9790aebSLuigi Rizzo 	 * so we can omit checking for -1
1101f9790aebSLuigi Rizzo 	 */
11024f80b14cSVincenzo Maffione 	for (c=0; c < n && cnt != got; c++, ri++) {
1103f9790aebSLuigi Rizzo 		/* compute current ring to use */
1104f9790aebSLuigi Rizzo 		struct netmap_ring *ring;
1105f9790aebSLuigi Rizzo 
110666015019SLuigi Rizzo 		if (ri > d->last_rx_ring)
110766015019SLuigi Rizzo 			ri = d->first_rx_ring;
1108f9790aebSLuigi Rizzo 		ring = NETMAP_RXRING(d->nifp, ri);
110917885a7bSLuigi Rizzo 		for ( ; !nm_ring_empty(ring) && cnt != got; got++) {
111037e3a6d3SLuigi Rizzo 			u_int idx, i;
1111b6e66be2SVincenzo Maffione 			u_char *oldbuf;
1112b6e66be2SVincenzo Maffione 			struct netmap_slot *slot;
111337e3a6d3SLuigi Rizzo 			if (d->hdr.buf) { /* from previous round */
111437e3a6d3SLuigi Rizzo 				cb(arg, &d->hdr, d->hdr.buf);
111537e3a6d3SLuigi Rizzo 			}
111637e3a6d3SLuigi Rizzo 			i = ring->cur;
1117b6e66be2SVincenzo Maffione 			slot = &ring->slot[i];
1118b6e66be2SVincenzo Maffione 			idx = slot->buf_idx;
11194f80b14cSVincenzo Maffione 			/* d->cur_rx_ring doesn't change inside this loop, but
11204f80b14cSVincenzo Maffione 			 * set it here, so it reflects d->hdr.buf's ring */
11214f80b14cSVincenzo Maffione 			d->cur_rx_ring = ri;
1122b6e66be2SVincenzo Maffione 			d->hdr.slot = slot;
1123b6e66be2SVincenzo Maffione 			oldbuf = d->hdr.buf = (u_char *)NETMAP_BUF(ring, idx);
112466015019SLuigi Rizzo 			// __builtin_prefetch(buf);
1125b6e66be2SVincenzo Maffione 			d->hdr.len = d->hdr.caplen = slot->len;
1126b6e66be2SVincenzo Maffione 			while (slot->flags & NS_MOREFRAG) {
1127b6e66be2SVincenzo Maffione 				u_char *nbuf;
1128b6e66be2SVincenzo Maffione 				u_int oldlen = slot->len;
1129b6e66be2SVincenzo Maffione 				i = nm_ring_next(ring, i);
1130b6e66be2SVincenzo Maffione 				slot = &ring->slot[i];
1131b6e66be2SVincenzo Maffione 				d->hdr.len += slot->len;
1132b6e66be2SVincenzo Maffione 				nbuf = (u_char *)NETMAP_BUF(ring, slot->buf_idx);
113398399ab0SVincenzo Maffione 				if (oldbuf != NULL && (uint32_t)(nbuf - oldbuf) == ring->nr_buf_size &&
1134b6e66be2SVincenzo Maffione 						oldlen == ring->nr_buf_size) {
1135b6e66be2SVincenzo Maffione 					d->hdr.caplen += slot->len;
1136b6e66be2SVincenzo Maffione 					oldbuf = nbuf;
1137b6e66be2SVincenzo Maffione 				} else {
1138b6e66be2SVincenzo Maffione 					oldbuf = NULL;
1139b6e66be2SVincenzo Maffione 				}
1140b6e66be2SVincenzo Maffione 			}
1141f9790aebSLuigi Rizzo 			d->hdr.ts = ring->ts;
114217885a7bSLuigi Rizzo 			ring->head = ring->cur = nm_ring_next(ring, i);
1143f9790aebSLuigi Rizzo 		}
1144f9790aebSLuigi Rizzo 	}
114537e3a6d3SLuigi Rizzo 	if (d->hdr.buf) { /* from previous round */
114637e3a6d3SLuigi Rizzo 		d->hdr.flags = 0;
114737e3a6d3SLuigi Rizzo 		cb(arg, &d->hdr, d->hdr.buf);
114837e3a6d3SLuigi Rizzo 	}
1149f9790aebSLuigi Rizzo 	return got;
1150f9790aebSLuigi Rizzo }
1151f9790aebSLuigi Rizzo 
1152f9790aebSLuigi Rizzo static u_char *
nm_nextpkt(struct nm_desc * d,struct nm_pkthdr * hdr)1153f0ea3689SLuigi Rizzo nm_nextpkt(struct nm_desc *d, struct nm_pkthdr *hdr)
1154f9790aebSLuigi Rizzo {
115566015019SLuigi Rizzo 	int ri = d->cur_rx_ring;
1156f9790aebSLuigi Rizzo 
1157f9790aebSLuigi Rizzo 	do {
1158f9790aebSLuigi Rizzo 		/* compute current ring to use */
1159f9790aebSLuigi Rizzo 		struct netmap_ring *ring = NETMAP_RXRING(d->nifp, ri);
116017885a7bSLuigi Rizzo 		if (!nm_ring_empty(ring)) {
1161f9790aebSLuigi Rizzo 			u_int i = ring->cur;
1162f9790aebSLuigi Rizzo 			u_int idx = ring->slot[i].buf_idx;
1163f9790aebSLuigi Rizzo 			u_char *buf = (u_char *)NETMAP_BUF(ring, idx);
116466015019SLuigi Rizzo 
116566015019SLuigi Rizzo 			// __builtin_prefetch(buf);
1166f9790aebSLuigi Rizzo 			hdr->ts = ring->ts;
1167f9790aebSLuigi Rizzo 			hdr->len = hdr->caplen = ring->slot[i].len;
116817885a7bSLuigi Rizzo 			ring->cur = nm_ring_next(ring, i);
116917885a7bSLuigi Rizzo 			/* we could postpone advancing head if we want
117017885a7bSLuigi Rizzo 			 * to hold the buffer. This can be supported in
117117885a7bSLuigi Rizzo 			 * the future.
117217885a7bSLuigi Rizzo 			 */
117317885a7bSLuigi Rizzo 			ring->head = ring->cur;
117466015019SLuigi Rizzo 			d->cur_rx_ring = ri;
1175f9790aebSLuigi Rizzo 			return buf;
1176f9790aebSLuigi Rizzo 		}
1177f9790aebSLuigi Rizzo 		ri++;
117866015019SLuigi Rizzo 		if (ri > d->last_rx_ring)
117966015019SLuigi Rizzo 			ri = d->first_rx_ring;
118066015019SLuigi Rizzo 	} while (ri != d->cur_rx_ring);
1181f9790aebSLuigi Rizzo 	return NULL; /* nothing found */
1182f9790aebSLuigi Rizzo }
1183f9790aebSLuigi Rizzo 
1184f9790aebSLuigi Rizzo #endif /* !HAVE_NETMAP_WITH_LIBS */
1185f9790aebSLuigi Rizzo 
1186f9790aebSLuigi Rizzo #endif /* NETMAP_WITH_LIBS */
1187f9790aebSLuigi Rizzo 
118868b8534bSLuigi Rizzo #endif /* _NET_NETMAP_USER_H_ */
1189