xref: /freebsd/sys/dev/netmap/netmap_kern.h (revision 99efa2c88d93c6272a8f54b18d18d0fd9d60f137)
1718cf2ccSPedro F. Giffuni /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
437e3a6d3SLuigi Rizzo  * Copyright (C) 2011-2014 Matteo Landi, Luigi Rizzo
537e3a6d3SLuigi Rizzo  * Copyright (C) 2013-2016 Universita` di Pisa
637e3a6d3SLuigi Rizzo  * All rights reserved.
768b8534bSLuigi Rizzo  *
868b8534bSLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
968b8534bSLuigi Rizzo  * modification, are permitted provided that the following conditions
1068b8534bSLuigi Rizzo  * are met:
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
1568b8534bSLuigi Rizzo  *    documentation and/or other materials provided with the distribution.
1668b8534bSLuigi Rizzo  *
1768b8534bSLuigi 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
1968b8534bSLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2068b8534bSLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2168b8534bSLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2268b8534bSLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2368b8534bSLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2468b8534bSLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2568b8534bSLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2668b8534bSLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2768b8534bSLuigi Rizzo  * SUCH DAMAGE.
2868b8534bSLuigi Rizzo  */
2968b8534bSLuigi Rizzo 
3068b8534bSLuigi Rizzo /*
3168b8534bSLuigi Rizzo  *
3268b8534bSLuigi Rizzo  * The header contains the definitions of constants and function
3368b8534bSLuigi Rizzo  * prototypes used only in kernelspace.
3468b8534bSLuigi Rizzo  */
3568b8534bSLuigi Rizzo 
3668b8534bSLuigi Rizzo #ifndef _NET_NETMAP_KERN_H_
3768b8534bSLuigi Rizzo #define _NET_NETMAP_KERN_H_
3868b8534bSLuigi Rizzo 
39847bf383SLuigi Rizzo #if defined(linux)
40847bf383SLuigi Rizzo 
414f80b14cSVincenzo Maffione #if defined(CONFIG_NETMAP_EXTMEM)
424f80b14cSVincenzo Maffione #define WITH_EXTMEM
434f80b14cSVincenzo Maffione #endif
44847bf383SLuigi Rizzo #if  defined(CONFIG_NETMAP_VALE)
45847bf383SLuigi Rizzo #define WITH_VALE
46847bf383SLuigi Rizzo #endif
47847bf383SLuigi Rizzo #if defined(CONFIG_NETMAP_PIPE)
48847bf383SLuigi Rizzo #define WITH_PIPES
49847bf383SLuigi Rizzo #endif
50847bf383SLuigi Rizzo #if defined(CONFIG_NETMAP_MONITOR)
51847bf383SLuigi Rizzo #define WITH_MONITOR
52847bf383SLuigi Rizzo #endif
53847bf383SLuigi Rizzo #if defined(CONFIG_NETMAP_GENERIC)
54847bf383SLuigi Rizzo #define WITH_GENERIC
55847bf383SLuigi Rizzo #endif
56b6e66be2SVincenzo Maffione #if defined(CONFIG_NETMAP_PTNETMAP)
57b6e66be2SVincenzo Maffione #define WITH_PTNETMAP
58847bf383SLuigi Rizzo #endif
59c3e9b4dbSLuiz Otavio O Souza #if defined(CONFIG_NETMAP_SINK)
60c3e9b4dbSLuiz Otavio O Souza #define WITH_SINK
61c3e9b4dbSLuiz Otavio O Souza #endif
62b6e66be2SVincenzo Maffione #if defined(CONFIG_NETMAP_NULL)
63b6e66be2SVincenzo Maffione #define WITH_NMNULL
64b6e66be2SVincenzo Maffione #endif
65847bf383SLuigi Rizzo 
6637e3a6d3SLuigi Rizzo #elif defined (_WIN32)
67f9790aebSLuigi Rizzo #define WITH_VALE	// comment out to disable VALE support
68f0ea3689SLuigi Rizzo #define WITH_PIPES
694bf50f18SLuigi Rizzo #define WITH_MONITOR
70039dd540SLuigi Rizzo #define WITH_GENERIC
71b6e66be2SVincenzo Maffione #define WITH_NMNULL
72f9790aebSLuigi Rizzo 
7337e3a6d3SLuigi Rizzo #else	/* neither linux nor windows */
7437e3a6d3SLuigi Rizzo #define WITH_VALE	// comment out to disable VALE support
7537e3a6d3SLuigi Rizzo #define WITH_PIPES
7637e3a6d3SLuigi Rizzo #define WITH_MONITOR
7737e3a6d3SLuigi Rizzo #define WITH_GENERIC
782ff91c17SVincenzo Maffione #define WITH_EXTMEM
79b6e66be2SVincenzo Maffione #define WITH_NMNULL
80847bf383SLuigi Rizzo #endif
81847bf383SLuigi Rizzo 
821a26580eSLuigi Rizzo #if defined(__FreeBSD__)
83225d33ffSSean Bruno #include <sys/selinfo.h>
84d4b42e08SLuigi Rizzo 
85ce3ee1e7SLuigi Rizzo #define likely(x)	__builtin_expect((long)!!(x), 1L)
86ce3ee1e7SLuigi Rizzo #define unlikely(x)	__builtin_expect((long)!!(x), 0L)
8737e3a6d3SLuigi Rizzo #define __user
88f196ce38SLuigi Rizzo 
89847bf383SLuigi Rizzo #define	NM_LOCK_T	struct mtx	/* low level spinlock, used to protect queues */
90039dd540SLuigi Rizzo 
91847bf383SLuigi Rizzo #define NM_MTX_T	struct sx	/* OS-specific mutex (sleepable) */
92847bf383SLuigi Rizzo #define NM_MTX_INIT(m)		sx_init(&(m), #m)
93847bf383SLuigi Rizzo #define NM_MTX_DESTROY(m)	sx_destroy(&(m))
94847bf383SLuigi Rizzo #define NM_MTX_LOCK(m)		sx_xlock(&(m))
954f80b14cSVincenzo Maffione #define NM_MTX_SPINLOCK(m)	while (!sx_try_xlock(&(m))) ;
96847bf383SLuigi Rizzo #define NM_MTX_UNLOCK(m)	sx_xunlock(&(m))
97847bf383SLuigi Rizzo #define NM_MTX_ASSERT(m)	sx_assert(&(m), SA_XLOCKED)
98f9790aebSLuigi Rizzo 
990e73f29aSLuigi Rizzo #define	NM_SELINFO_T	struct nm_selinfo
10037e3a6d3SLuigi Rizzo #define NM_SELRECORD_T	struct thread
1011a26580eSLuigi Rizzo #define	MBUF_LEN(m)	((m)->m_pkthdr.len)
10237e3a6d3SLuigi Rizzo #define MBUF_TXQ(m)	((m)->m_pkthdr.flowid)
10337e3a6d3SLuigi Rizzo #define MBUF_TRANSMIT(na, ifp, m)	((na)->if_transmit(ifp, m))
10437e3a6d3SLuigi Rizzo #define	GEN_TX_MBUF_IFP(m)	((m)->m_pkthdr.rcvif)
105d4b42e08SLuigi Rizzo 
1064f80b14cSVincenzo Maffione #define NM_ATOMIC_T	volatile int /* required by atomic/bitops.h */
107f9790aebSLuigi Rizzo /* atomic operations */
108f9790aebSLuigi Rizzo #include <machine/atomic.h>
109f9790aebSLuigi Rizzo #define NM_ATOMIC_TEST_AND_SET(p)       (!atomic_cmpset_acq_int((p), 0, 1))
110f9790aebSLuigi Rizzo #define NM_ATOMIC_CLEAR(p)              atomic_store_rel_int((p), 0)
111f9790aebSLuigi Rizzo 
11246aa1303SLuigi Rizzo struct netmap_adapter *netmap_getna(if_t ifp);
113f9790aebSLuigi Rizzo 
11437e3a6d3SLuigi Rizzo #define MBUF_REFCNT(m)		((m)->m_ext.ext_count)
11537e3a6d3SLuigi Rizzo #define SET_MBUF_REFCNT(m, x)   (m)->m_ext.ext_count = x
1164bf50f18SLuigi Rizzo 
11737e3a6d3SLuigi Rizzo #define MBUF_QUEUED(m)		1
118f9790aebSLuigi Rizzo 
1190e73f29aSLuigi Rizzo struct nm_selinfo {
12045100257SVincenzo Maffione 	/* Support for select(2) and poll(2). */
1210e73f29aSLuigi Rizzo 	struct selinfo si;
12245100257SVincenzo Maffione 	/* Support for kqueue(9). See comments in netmap_freebsd.c */
12319c4ec08SVincenzo Maffione 	struct taskqueue *ntfytq;
12419c4ec08SVincenzo Maffione 	struct task ntfytask;
1250e73f29aSLuigi Rizzo 	struct mtx m;
12619c4ec08SVincenzo Maffione 	char mtxname[32];
12745100257SVincenzo Maffione 	int kqueue_users;
1280e73f29aSLuigi Rizzo };
1290e73f29aSLuigi Rizzo 
1300e73f29aSLuigi Rizzo 
131f9790aebSLuigi Rizzo struct hrtimer {
1324f80b14cSVincenzo Maffione     /* Not used in FreeBSD. */
133f9790aebSLuigi Rizzo };
1344f80b14cSVincenzo Maffione 
135847bf383SLuigi Rizzo #define NM_BNS_GET(b)
136847bf383SLuigi Rizzo #define NM_BNS_PUT(b)
137ce3ee1e7SLuigi Rizzo 
13864ae02c3SLuigi Rizzo #elif defined (linux)
139d4b42e08SLuigi Rizzo 
1402579e2d7SLuigi Rizzo #define	NM_LOCK_T	safe_spinlock_t	// see bsd_glue.h
1411a26580eSLuigi Rizzo #define	NM_SELINFO_T	wait_queue_head_t
1421a26580eSLuigi Rizzo #define	MBUF_LEN(m)	((m)->len)
14337e3a6d3SLuigi Rizzo #define MBUF_TRANSMIT(na, ifp, m)							\
14437e3a6d3SLuigi Rizzo 	({										\
14537e3a6d3SLuigi Rizzo 		/* Avoid infinite recursion with generic. */				\
14637e3a6d3SLuigi Rizzo 		m->priority = NM_MAGIC_PRIORITY_TX;					\
14737e3a6d3SLuigi Rizzo 		(((struct net_device_ops *)(na)->if_transmit)->ndo_start_xmit(m, ifp));	\
14837e3a6d3SLuigi Rizzo 		0;									\
14937e3a6d3SLuigi Rizzo 	})
15037e3a6d3SLuigi Rizzo 
15137e3a6d3SLuigi Rizzo /* See explanation in nm_os_generic_xmit_frame. */
152e330262fSJustin Hibbits #define	GEN_TX_MBUF_IFP(m)	((if_t)skb_shinfo(m)->destructor_arg)
153f196ce38SLuigi Rizzo 
154ce3ee1e7SLuigi Rizzo #define NM_ATOMIC_T	volatile long unsigned int
155ce3ee1e7SLuigi Rizzo 
156847bf383SLuigi Rizzo #define NM_MTX_T	struct mutex	/* OS-specific sleepable lock */
157847bf383SLuigi Rizzo #define NM_MTX_INIT(m)	mutex_init(&(m))
158847bf383SLuigi Rizzo #define NM_MTX_DESTROY(m)	do { (void)(m); } while (0)
159039dd540SLuigi Rizzo #define NM_MTX_LOCK(m)		mutex_lock(&(m))
160039dd540SLuigi Rizzo #define NM_MTX_UNLOCK(m)	mutex_unlock(&(m))
161847bf383SLuigi Rizzo #define NM_MTX_ASSERT(m)	mutex_is_locked(&(m))
162f9790aebSLuigi Rizzo 
163f196ce38SLuigi Rizzo #ifndef DEV_NETMAP
164f196ce38SLuigi Rizzo #define DEV_NETMAP
165ce3ee1e7SLuigi Rizzo #endif /* DEV_NETMAP */
166f196ce38SLuigi Rizzo 
167f196ce38SLuigi Rizzo #elif defined (__APPLE__)
168d4b42e08SLuigi Rizzo 
1698241616dSLuigi Rizzo #warning apple support is incomplete.
170f196ce38SLuigi Rizzo #define likely(x)	__builtin_expect(!!(x), 1)
171f196ce38SLuigi Rizzo #define unlikely(x)	__builtin_expect(!!(x), 0)
172f196ce38SLuigi Rizzo #define	NM_LOCK_T	IOLock *
173f196ce38SLuigi Rizzo #define	NM_SELINFO_T	struct selinfo
174f196ce38SLuigi Rizzo #define	MBUF_LEN(m)	((m)->m_pkthdr.len)
17537e3a6d3SLuigi Rizzo 
17637e3a6d3SLuigi Rizzo #elif defined (_WIN32)
17737e3a6d3SLuigi Rizzo #include "../../../WINDOWS/win_glue.h"
17837e3a6d3SLuigi Rizzo 
17937e3a6d3SLuigi Rizzo #define NM_SELRECORD_T		IO_STACK_LOCATION
18037e3a6d3SLuigi Rizzo #define NM_SELINFO_T		win_SELINFO		// see win_glue.h
18137e3a6d3SLuigi Rizzo #define NM_LOCK_T		win_spinlock_t	// see win_glue.h
18237e3a6d3SLuigi Rizzo #define NM_MTX_T		KGUARDED_MUTEX	/* OS-specific mutex (sleepable) */
18337e3a6d3SLuigi Rizzo 
18437e3a6d3SLuigi Rizzo #define NM_MTX_INIT(m)		KeInitializeGuardedMutex(&m);
18537e3a6d3SLuigi Rizzo #define NM_MTX_DESTROY(m)	do { (void)(m); } while (0)
18637e3a6d3SLuigi Rizzo #define NM_MTX_LOCK(m)		KeAcquireGuardedMutex(&(m))
18737e3a6d3SLuigi Rizzo #define NM_MTX_UNLOCK(m)	KeReleaseGuardedMutex(&(m))
18837e3a6d3SLuigi Rizzo #define NM_MTX_ASSERT(m)	assert(&m.Count>0)
18937e3a6d3SLuigi Rizzo 
19037e3a6d3SLuigi Rizzo //These linknames are for the NDIS driver
19137e3a6d3SLuigi Rizzo #define NETMAP_NDIS_LINKNAME_STRING             L"\\DosDevices\\NMAPNDIS"
19237e3a6d3SLuigi Rizzo #define NETMAP_NDIS_NTDEVICE_STRING             L"\\Device\\NMAPNDIS"
19337e3a6d3SLuigi Rizzo 
19437e3a6d3SLuigi Rizzo //Definition of internal driver-to-driver ioctl codes
19537e3a6d3SLuigi Rizzo #define NETMAP_KERNEL_XCHANGE_POINTERS		_IO('i', 180)
19637e3a6d3SLuigi Rizzo #define NETMAP_KERNEL_SEND_SHUTDOWN_SIGNAL	_IO_direct('i', 195)
19737e3a6d3SLuigi Rizzo 
19837e3a6d3SLuigi Rizzo typedef struct hrtimer{
19937e3a6d3SLuigi Rizzo 	KTIMER timer;
20037e3a6d3SLuigi Rizzo 	BOOLEAN active;
20137e3a6d3SLuigi Rizzo 	KDPC deferred_proc;
20237e3a6d3SLuigi Rizzo };
20337e3a6d3SLuigi Rizzo 
20437e3a6d3SLuigi Rizzo /* MSVC does not have likely/unlikely support */
20537e3a6d3SLuigi Rizzo #ifdef _MSC_VER
20637e3a6d3SLuigi Rizzo #define likely(x)	(x)
20737e3a6d3SLuigi Rizzo #define unlikely(x)	(x)
20837e3a6d3SLuigi Rizzo #else
20937e3a6d3SLuigi Rizzo #define likely(x)	__builtin_expect((long)!!(x), 1L)
21037e3a6d3SLuigi Rizzo #define unlikely(x)	__builtin_expect((long)!!(x), 0L)
21137e3a6d3SLuigi Rizzo #endif //_MSC_VER
212f196ce38SLuigi Rizzo 
2131a26580eSLuigi Rizzo #else
214d4b42e08SLuigi Rizzo 
2151a26580eSLuigi Rizzo #error unsupported platform
216d4b42e08SLuigi Rizzo 
217d4b42e08SLuigi Rizzo #endif /* end - platform-specific code */
2181a26580eSLuigi Rizzo 
21937e3a6d3SLuigi Rizzo #ifndef _WIN32 /* support for emulated sysctl */
22037e3a6d3SLuigi Rizzo #define SYSBEGIN(x)
22137e3a6d3SLuigi Rizzo #define SYSEND
22237e3a6d3SLuigi Rizzo #endif /* _WIN32 */
22337e3a6d3SLuigi Rizzo 
22437e3a6d3SLuigi Rizzo #define NM_ACCESS_ONCE(x)	(*(volatile __typeof__(x) *)&(x))
22537e3a6d3SLuigi Rizzo 
226847bf383SLuigi Rizzo #define	NMG_LOCK_T		NM_MTX_T
227847bf383SLuigi Rizzo #define	NMG_LOCK_INIT()		NM_MTX_INIT(netmap_global_lock)
228847bf383SLuigi Rizzo #define	NMG_LOCK_DESTROY()	NM_MTX_DESTROY(netmap_global_lock)
229847bf383SLuigi Rizzo #define	NMG_LOCK()		NM_MTX_LOCK(netmap_global_lock)
230847bf383SLuigi Rizzo #define	NMG_UNLOCK()		NM_MTX_UNLOCK(netmap_global_lock)
231847bf383SLuigi Rizzo #define	NMG_LOCK_ASSERT()	NM_MTX_ASSERT(netmap_global_lock)
232847bf383SLuigi Rizzo 
233c3e9b4dbSLuiz Otavio O Souza #if defined(__FreeBSD__)
234b6e66be2SVincenzo Maffione #define nm_prerr_int	printf
235b6e66be2SVincenzo Maffione #define nm_prinf_int	printf
236c3e9b4dbSLuiz Otavio O Souza #elif defined (_WIN32)
237b6e66be2SVincenzo Maffione #define nm_prerr_int	DbgPrint
238b6e66be2SVincenzo Maffione #define nm_prinf_int	DbgPrint
239c3e9b4dbSLuiz Otavio O Souza #elif defined(linux)
240b6e66be2SVincenzo Maffione #define nm_prerr_int(fmt, arg...)    printk(KERN_ERR fmt, ##arg)
241b6e66be2SVincenzo Maffione #define nm_prinf_int(fmt, arg...)    printk(KERN_INFO fmt, ##arg)
242c3e9b4dbSLuiz Otavio O Souza #endif
243c3e9b4dbSLuiz Otavio O Souza 
244b6e66be2SVincenzo Maffione #define nm_prinf(format, ...)					\
24568b8534bSLuigi Rizzo 	do {							\
24668b8534bSLuigi Rizzo 		struct timeval __xxts;				\
24768b8534bSLuigi Rizzo 		microtime(&__xxts);				\
248b6e66be2SVincenzo Maffione 		nm_prinf_int("%03d.%06d [%4d] %-25s " format "\n",\
24968b8534bSLuigi Rizzo 		(int)__xxts.tv_sec % 1000, (int)__xxts.tv_usec,	\
25017885a7bSLuigi Rizzo 		__LINE__, __FUNCTION__, ##__VA_ARGS__);		\
25168b8534bSLuigi Rizzo 	} while (0)
25268b8534bSLuigi Rizzo 
253b6e66be2SVincenzo Maffione #define nm_prerr(format, ...)					\
254b6e66be2SVincenzo Maffione 	do {							\
255b6e66be2SVincenzo Maffione 		struct timeval __xxts;				\
256b6e66be2SVincenzo Maffione 		microtime(&__xxts);				\
257b6e66be2SVincenzo Maffione 		nm_prerr_int("%03d.%06d [%4d] %-25s " format "\n",\
258b6e66be2SVincenzo Maffione 		(int)__xxts.tv_sec % 1000, (int)__xxts.tv_usec,	\
259b6e66be2SVincenzo Maffione 		__LINE__, __FUNCTION__, ##__VA_ARGS__);		\
260b6e66be2SVincenzo Maffione 	} while (0)
261b6e66be2SVincenzo Maffione 
26275f4f3edSVincenzo Maffione /* Disabled printf (used to be nm_prdis). */
263b6e66be2SVincenzo Maffione #define nm_prdis(format, ...)
264b6e66be2SVincenzo Maffione 
265b6e66be2SVincenzo Maffione /* Rate limited, lps indicates how many per second. */
266b6e66be2SVincenzo Maffione #define nm_prlim(lps, format, ...)				\
2678241616dSLuigi Rizzo 	do {							\
2688241616dSLuigi Rizzo 		static int t0, __cnt;				\
2698241616dSLuigi Rizzo 		if (t0 != time_second) {			\
2708241616dSLuigi Rizzo 			t0 = time_second;			\
2718241616dSLuigi Rizzo 			__cnt = 0;				\
2728241616dSLuigi Rizzo 		}						\
2738241616dSLuigi Rizzo 		if (__cnt++ < lps)				\
274b6e66be2SVincenzo Maffione 			nm_prinf(format, ##__VA_ARGS__);	\
2758241616dSLuigi Rizzo 	} while (0)
2768241616dSLuigi Rizzo 
27768b8534bSLuigi Rizzo struct netmap_adapter;
278f18be576SLuigi Rizzo struct nm_bdg_fwd;
279f18be576SLuigi Rizzo struct nm_bridge;
280f18be576SLuigi Rizzo struct netmap_priv_d;
2812a7db7a6SVincenzo Maffione struct nm_bdg_args;
28268b8534bSLuigi Rizzo 
28345c67e8fSVincenzo Maffione /* os-specific NM_SELINFO_T initialization/destruction functions */
28419c4ec08SVincenzo Maffione int nm_os_selinfo_init(NM_SELINFO_T *, const char *name);
28537e3a6d3SLuigi Rizzo void nm_os_selinfo_uninit(NM_SELINFO_T *);
28637e3a6d3SLuigi Rizzo 
287ce3ee1e7SLuigi Rizzo const char *nm_dump_buf(char *p, int len, int lim, char *dst);
288ce3ee1e7SLuigi Rizzo 
28937e3a6d3SLuigi Rizzo void nm_os_selwakeup(NM_SELINFO_T *si);
29037e3a6d3SLuigi Rizzo void nm_os_selrecord(NM_SELRECORD_T *sr, NM_SELINFO_T *si);
29137e3a6d3SLuigi Rizzo 
29237e3a6d3SLuigi Rizzo int nm_os_ifnet_init(void);
29337e3a6d3SLuigi Rizzo void nm_os_ifnet_fini(void);
29437e3a6d3SLuigi Rizzo void nm_os_ifnet_lock(void);
29537e3a6d3SLuigi Rizzo void nm_os_ifnet_unlock(void);
29637e3a6d3SLuigi Rizzo 
297e330262fSJustin Hibbits unsigned nm_os_ifnet_mtu(if_t ifp);
2984f80b14cSVincenzo Maffione 
29937e3a6d3SLuigi Rizzo void nm_os_get_module(void);
30037e3a6d3SLuigi Rizzo void nm_os_put_module(void);
30137e3a6d3SLuigi Rizzo 
302e330262fSJustin Hibbits void netmap_make_zombie(if_t);
303e330262fSJustin Hibbits void netmap_undo_zombie(if_t);
30437e3a6d3SLuigi Rizzo 
305c3e9b4dbSLuiz Otavio O Souza /* os independent alloc/realloc/free */
306c3e9b4dbSLuiz Otavio O Souza void *nm_os_malloc(size_t);
3074f80b14cSVincenzo Maffione void *nm_os_vmalloc(size_t);
308c3e9b4dbSLuiz Otavio O Souza void *nm_os_realloc(void *, size_t new_size, size_t old_size);
309c3e9b4dbSLuiz Otavio O Souza void nm_os_free(void *);
3104f80b14cSVincenzo Maffione void nm_os_vfree(void *);
311c3e9b4dbSLuiz Otavio O Souza 
3122a7db7a6SVincenzo Maffione /* os specific attach/detach enter/exit-netmap-mode routines */
313e330262fSJustin Hibbits void nm_os_onattach(if_t);
314e330262fSJustin Hibbits void nm_os_ondetach(if_t);
315e330262fSJustin Hibbits void nm_os_onenter(if_t);
316e330262fSJustin Hibbits void nm_os_onexit(if_t);
3172a7db7a6SVincenzo Maffione 
31837e3a6d3SLuigi Rizzo /* passes a packet up to the host stack.
31937e3a6d3SLuigi Rizzo  * If the packet is sent (or dropped) immediately it returns NULL,
32037e3a6d3SLuigi Rizzo  * otherwise it links the packet to prev and returns m.
32137e3a6d3SLuigi Rizzo  * In this case, a final call with m=NULL and prev != NULL will send up
32237e3a6d3SLuigi Rizzo  * the entire chain to the host stack.
32337e3a6d3SLuigi Rizzo  */
324e330262fSJustin Hibbits void *nm_os_send_up(if_t, struct mbuf *m, struct mbuf *prev);
32537e3a6d3SLuigi Rizzo 
3262a7db7a6SVincenzo Maffione int nm_os_mbuf_has_seg_offld(struct mbuf *m);
3272a7db7a6SVincenzo Maffione int nm_os_mbuf_has_csum_offld(struct mbuf *m);
32837e3a6d3SLuigi Rizzo 
329f9790aebSLuigi Rizzo #include "netmap_mbq.h"
330f9790aebSLuigi Rizzo 
331f9790aebSLuigi Rizzo extern NMG_LOCK_T	netmap_global_lock;
332f9790aebSLuigi Rizzo 
333847bf383SLuigi Rizzo enum txrx { NR_RX = 0, NR_TX = 1, NR_TXRX };
334847bf383SLuigi Rizzo 
335847bf383SLuigi Rizzo static __inline const char*
336847bf383SLuigi Rizzo nm_txrx2str(enum txrx t)
337847bf383SLuigi Rizzo {
338847bf383SLuigi Rizzo 	return (t== NR_RX ? "RX" : "TX");
339847bf383SLuigi Rizzo }
340847bf383SLuigi Rizzo 
341847bf383SLuigi Rizzo static __inline enum txrx
342847bf383SLuigi Rizzo nm_txrx_swap(enum txrx t)
343847bf383SLuigi Rizzo {
344847bf383SLuigi Rizzo 	return (t== NR_RX ? NR_TX : NR_RX);
345847bf383SLuigi Rizzo }
346847bf383SLuigi Rizzo 
347847bf383SLuigi Rizzo #define for_rx_tx(t)	for ((t) = 0; (t) < NR_TXRX; (t)++)
348847bf383SLuigi Rizzo 
349c3e9b4dbSLuiz Otavio O Souza #ifdef WITH_MONITOR
350c3e9b4dbSLuiz Otavio O Souza struct netmap_zmon_list {
351c3e9b4dbSLuiz Otavio O Souza 	struct netmap_kring *next;
352c3e9b4dbSLuiz Otavio O Souza 	struct netmap_kring *prev;
353c3e9b4dbSLuiz Otavio O Souza };
354c3e9b4dbSLuiz Otavio O Souza #endif /* WITH_MONITOR */
355847bf383SLuigi Rizzo 
35668b8534bSLuigi Rizzo /*
35764ae02c3SLuigi Rizzo  * private, kernel view of a ring. Keeps track of the status of
35864ae02c3SLuigi Rizzo  * a ring across system calls.
35964ae02c3SLuigi Rizzo  *
36064ae02c3SLuigi Rizzo  *	nr_hwcur	index of the next buffer to refill.
36117885a7bSLuigi Rizzo  *			It corresponds to ring->head
36217885a7bSLuigi Rizzo  *			at the time the system call returns.
36364ae02c3SLuigi Rizzo  *
36417885a7bSLuigi Rizzo  *	nr_hwtail	index of the first buffer owned by the kernel.
36517885a7bSLuigi Rizzo  *			On RX, hwcur->hwtail are receive buffers
36617885a7bSLuigi Rizzo  *			not yet released. hwcur is advanced following
36717885a7bSLuigi Rizzo  *			ring->head, hwtail is advanced on incoming packets,
36817885a7bSLuigi Rizzo  *			and a wakeup is generated when hwtail passes ring->cur
36917885a7bSLuigi Rizzo  *			    On TX, hwcur->rcur have been filled by the sender
37017885a7bSLuigi Rizzo  *			but not sent yet to the NIC; rcur->hwtail are available
37117885a7bSLuigi Rizzo  *			for new transmissions, and hwtail->hwcur-1 are pending
37217885a7bSLuigi Rizzo  *			transmissions not yet acknowledged.
37368b8534bSLuigi Rizzo  *
3741a26580eSLuigi Rizzo  * The indexes in the NIC and netmap rings are offset by nkr_hwofs slots.
37568b8534bSLuigi Rizzo  * This is so that, on a reset, buffers owned by userspace are not
37668b8534bSLuigi Rizzo  * modified by the kernel. In particular:
37717885a7bSLuigi Rizzo  * RX rings: the next empty buffer (hwtail + hwofs) coincides with
37868b8534bSLuigi Rizzo  * 	the next empty buffer as known by the hardware (next_to_check or so).
37968b8534bSLuigi Rizzo  * TX rings: hwcur + hwofs coincides with next_to_send
3801dce924dSLuigi Rizzo  *
381ce3ee1e7SLuigi Rizzo  * The following fields are used to implement lock-free copy of packets
382ce3ee1e7SLuigi Rizzo  * from input to output ports in VALE switch:
383ce3ee1e7SLuigi Rizzo  *	nkr_hwlease	buffer after the last one being copied.
384ce3ee1e7SLuigi Rizzo  *			A writer in nm_bdg_flush reserves N buffers
385ce3ee1e7SLuigi Rizzo  *			from nr_hwlease, advances it, then does the
386ce3ee1e7SLuigi Rizzo  *			copy outside the lock.
387ce3ee1e7SLuigi Rizzo  *			In RX rings (used for VALE ports),
38817885a7bSLuigi Rizzo  *			nkr_hwtail <= nkr_hwlease < nkr_hwcur+N-1
389ce3ee1e7SLuigi Rizzo  *			In TX rings (used for NIC or host stack ports)
39017885a7bSLuigi Rizzo  *			nkr_hwcur <= nkr_hwlease < nkr_hwtail
391ce3ee1e7SLuigi Rizzo  *	nkr_leases	array of nkr_num_slots where writers can report
392ce3ee1e7SLuigi Rizzo  *			completion of their block. NR_NOSLOT (~0) indicates
393ce3ee1e7SLuigi Rizzo  *			that the writer has not finished yet
394ce3ee1e7SLuigi Rizzo  *	nkr_lease_idx	index of next free slot in nr_leases, to be assigned
395ce3ee1e7SLuigi Rizzo  *
396ce3ee1e7SLuigi Rizzo  * The kring is manipulated by txsync/rxsync and generic netmap function.
39717885a7bSLuigi Rizzo  *
39817885a7bSLuigi Rizzo  * Concurrent rxsync or txsync on the same ring are prevented through
39989cc2556SLuigi Rizzo  * by nm_kr_(try)lock() which in turn uses nr_busy. This is all we need
40017885a7bSLuigi Rizzo  * for NIC rings, and for TX rings attached to the host stack.
40117885a7bSLuigi Rizzo  *
40217885a7bSLuigi Rizzo  * RX rings attached to the host stack use an mbq (rx_queue) on both
40317885a7bSLuigi Rizzo  * rxsync_from_host() and netmap_transmit(). The mbq is protected
40417885a7bSLuigi Rizzo  * by its internal lock.
40517885a7bSLuigi Rizzo  *
4064bf50f18SLuigi Rizzo  * RX rings attached to the VALE switch are accessed by both senders
40717885a7bSLuigi Rizzo  * and receiver. They are protected through the q_lock on the RX ring.
40868b8534bSLuigi Rizzo  */
40968b8534bSLuigi Rizzo struct netmap_kring {
41068b8534bSLuigi Rizzo 	struct netmap_ring	*ring;
41117885a7bSLuigi Rizzo 
4122ff91c17SVincenzo Maffione 	uint32_t	nr_hwcur;  /* should be nr_hwhead */
41317885a7bSLuigi Rizzo 	uint32_t	nr_hwtail;
41417885a7bSLuigi Rizzo 
41517885a7bSLuigi Rizzo 	/*
41617885a7bSLuigi Rizzo 	 * Copies of values in user rings, so we do not need to look
41717885a7bSLuigi Rizzo 	 * at the ring (which could be modified). These are set in the
41817885a7bSLuigi Rizzo 	 * *sync_prologue()/finalize() routines.
41917885a7bSLuigi Rizzo 	 */
42017885a7bSLuigi Rizzo 	uint32_t	rhead;
42117885a7bSLuigi Rizzo 	uint32_t	rcur;
42217885a7bSLuigi Rizzo 	uint32_t	rtail;
42317885a7bSLuigi Rizzo 
424ce3ee1e7SLuigi Rizzo 	uint32_t	nr_kflags;	/* private driver flags */
4252157a17cSLuigi Rizzo #define NKR_PENDINTR	0x1		// Pending interrupt.
426847bf383SLuigi Rizzo #define NKR_EXCLUSIVE	0x2		/* exclusive binding */
42737e3a6d3SLuigi Rizzo #define NKR_FORWARD	0x4		/* (host ring only) there are
42837e3a6d3SLuigi Rizzo 					   packets to forward
42937e3a6d3SLuigi Rizzo 					 */
43037e3a6d3SLuigi Rizzo #define NKR_NEEDRING	0x8		/* ring needed even if users==0
43137e3a6d3SLuigi Rizzo 					 * (used internally by pipes and
43237e3a6d3SLuigi Rizzo 					 *  by ptnetmap host ports)
43337e3a6d3SLuigi Rizzo 					 */
4344f80b14cSVincenzo Maffione #define NKR_NOINTR      0x10            /* don't use interrupts on this ring */
4352ff91c17SVincenzo Maffione #define NKR_FAKERING	0x20		/* don't allocate/free buffers */
43637e3a6d3SLuigi Rizzo 
43737e3a6d3SLuigi Rizzo 	uint32_t	nr_mode;
43837e3a6d3SLuigi Rizzo 	uint32_t	nr_pending_mode;
43937e3a6d3SLuigi Rizzo #define NKR_NETMAP_OFF	0x0
44037e3a6d3SLuigi Rizzo #define NKR_NETMAP_ON	0x1
44137e3a6d3SLuigi Rizzo 
442ce3ee1e7SLuigi Rizzo 	uint32_t	nkr_num_slots;
44317885a7bSLuigi Rizzo 
44417885a7bSLuigi Rizzo 	/*
44517885a7bSLuigi Rizzo 	 * On a NIC reset, the NIC ring indexes may be reset but the
44617885a7bSLuigi Rizzo 	 * indexes in the netmap rings remain the same. nkr_hwofs
44717885a7bSLuigi Rizzo 	 * keeps track of the offset between the two.
448a6d768d8SVincenzo Maffione 	 *
449a6d768d8SVincenzo Maffione 	 * Moreover, during reset, we can restore only the subset of
450a6d768d8SVincenzo Maffione 	 * the NIC ring that corresponds to the kernel-owned part of
451a6d768d8SVincenzo Maffione 	 * the netmap ring. The rest of the slots must be restored
452a6d768d8SVincenzo Maffione 	 * by the *sync routines when the user releases more slots.
453a6d768d8SVincenzo Maffione 	 * The nkr_to_refill field keeps track of the number of slots
454a6d768d8SVincenzo Maffione 	 * that still need to be restored.
45517885a7bSLuigi Rizzo 	 */
45617885a7bSLuigi Rizzo 	int32_t		nkr_hwofs;
457a6d768d8SVincenzo Maffione 	int32_t		nkr_to_refill;
45868b8534bSLuigi Rizzo 
45917885a7bSLuigi Rizzo 	/* last_reclaim is opaque marker to help reduce the frequency
46017885a7bSLuigi Rizzo 	 * of operations such as reclaiming tx buffers. A possible use
46117885a7bSLuigi Rizzo 	 * is set it to ticks and do the reclaim only once per tick.
46217885a7bSLuigi Rizzo 	 */
46317885a7bSLuigi Rizzo 	uint64_t	last_reclaim;
46417885a7bSLuigi Rizzo 
465ce3ee1e7SLuigi Rizzo 
4661a26580eSLuigi Rizzo 	NM_SELINFO_T	si;		/* poll/select wait queue */
467ce3ee1e7SLuigi Rizzo 	NM_LOCK_T	q_lock;		/* protects kring and ring. */
468ce3ee1e7SLuigi Rizzo 	NM_ATOMIC_T	nr_busy;	/* prevent concurrent syscalls */
469ce3ee1e7SLuigi Rizzo 
4702ff91c17SVincenzo Maffione 	/* the adapter the owns this kring */
47117885a7bSLuigi Rizzo 	struct netmap_adapter *na;
47217885a7bSLuigi Rizzo 
4732ff91c17SVincenzo Maffione 	/* the adapter that wants to be notified when this kring has
47445c67e8fSVincenzo Maffione 	 * new slots available. This is usually the same as the above,
4752ff91c17SVincenzo Maffione 	 * but wrappers may let it point to themselves
4762ff91c17SVincenzo Maffione 	 */
4772ff91c17SVincenzo Maffione 	struct netmap_adapter *notify_na;
4782ff91c17SVincenzo Maffione 
4796435a0dcSLuigi Rizzo 	/* The following fields are for VALE switch support */
48017885a7bSLuigi Rizzo 	struct nm_bdg_fwd *nkr_ft;
48117885a7bSLuigi Rizzo 	uint32_t	*nkr_leases;
48217885a7bSLuigi Rizzo #define NR_NOSLOT	((uint32_t)~0)	/* used in nkr_*lease* */
48317885a7bSLuigi Rizzo 	uint32_t	nkr_hwlease;
48417885a7bSLuigi Rizzo 	uint32_t	nkr_lease_idx;
48517885a7bSLuigi Rizzo 
4864bf50f18SLuigi Rizzo 	/* while nkr_stopped is set, no new [tr]xsync operations can
4874bf50f18SLuigi Rizzo 	 * be started on this kring.
4884bf50f18SLuigi Rizzo 	 * This is used by netmap_disable_all_rings()
4894bf50f18SLuigi Rizzo 	 * to find a synchronization point where critical data
4904bf50f18SLuigi Rizzo 	 * structures pointed to by the kring can be added or removed
4914bf50f18SLuigi Rizzo 	 */
4924bf50f18SLuigi Rizzo 	volatile int nkr_stopped;
493f9790aebSLuigi Rizzo 
494f0ea3689SLuigi Rizzo 	/* Support for adapters without native netmap support.
495f9790aebSLuigi Rizzo 	 * On tx rings we preallocate an array of tx buffers
496f9790aebSLuigi Rizzo 	 * (same size as the netmap ring), on rx rings we
497f0ea3689SLuigi Rizzo 	 * store incoming mbufs in a queue that is drained by
498f0ea3689SLuigi Rizzo 	 * a rxsync.
499f9790aebSLuigi Rizzo 	 */
500f9790aebSLuigi Rizzo 	struct mbuf	**tx_pool;
50137e3a6d3SLuigi Rizzo 	struct mbuf	*tx_event;	/* TX event used as a notification */
50237e3a6d3SLuigi Rizzo 	NM_LOCK_T	tx_event_lock;	/* protects the tx_event mbuf */
503ce12afaaSMark Johnston #ifdef __FreeBSD__
504ce12afaaSMark Johnston 	struct callout	tx_event_callout;
505ce12afaaSMark Johnston #endif
50617885a7bSLuigi Rizzo 	struct mbq	rx_queue;       /* intercepted rx mbufs. */
50717885a7bSLuigi Rizzo 
508847bf383SLuigi Rizzo 	uint32_t	users;		/* existing bindings for this ring */
509847bf383SLuigi Rizzo 
51037e3a6d3SLuigi Rizzo 	uint32_t	ring_id;	/* kring identifier */
511847bf383SLuigi Rizzo 	enum txrx	tx;		/* kind of ring (tx or rx) */
51217885a7bSLuigi Rizzo 	char name[64];			/* diagnostic */
513f9790aebSLuigi Rizzo 
5144bf50f18SLuigi Rizzo 	/* [tx]sync callback for this kring.
5154bf50f18SLuigi Rizzo 	 * The default nm_kring_create callback (netmap_krings_create)
5164bf50f18SLuigi Rizzo 	 * sets the nm_sync callback of each hardware tx(rx) kring to
5174bf50f18SLuigi Rizzo 	 * the corresponding nm_txsync(nm_rxsync) taken from the
5184bf50f18SLuigi Rizzo 	 * netmap_adapter; moreover, it sets the sync callback
5194bf50f18SLuigi Rizzo 	 * of the host tx(rx) ring to netmap_txsync_to_host
5204bf50f18SLuigi Rizzo 	 * (netmap_rxsync_from_host).
5214bf50f18SLuigi Rizzo 	 *
5224bf50f18SLuigi Rizzo 	 * Overrides: the above configuration is not changed by
5234bf50f18SLuigi Rizzo 	 * any of the nm_krings_create callbacks.
5244bf50f18SLuigi Rizzo 	 */
525f0ea3689SLuigi Rizzo 	int (*nm_sync)(struct netmap_kring *kring, int flags);
526847bf383SLuigi Rizzo 	int (*nm_notify)(struct netmap_kring *kring, int flags);
527f0ea3689SLuigi Rizzo 
528f0ea3689SLuigi Rizzo #ifdef WITH_PIPES
5294bf50f18SLuigi Rizzo 	struct netmap_kring *pipe;	/* if this is a pipe ring,
5304bf50f18SLuigi Rizzo 					 * pointer to the other end
5314bf50f18SLuigi Rizzo 					 */
5322a7db7a6SVincenzo Maffione 	uint32_t pipe_tail;		/* hwtail updated by the other end */
533f0ea3689SLuigi Rizzo #endif /* WITH_PIPES */
534f0ea3689SLuigi Rizzo 
535a6d768d8SVincenzo Maffione 	/* mask for the offset-related part of the ptr field in the slots */
536a6d768d8SVincenzo Maffione 	uint64_t offset_mask;
537a6d768d8SVincenzo Maffione 	/* maximum user-specified offset, as stipulated at bind time.
538a6d768d8SVincenzo Maffione 	 * Larger offset requests will be silently capped to offset_max.
539a6d768d8SVincenzo Maffione 	 */
540a6d768d8SVincenzo Maffione 	uint64_t offset_max;
541a6d768d8SVincenzo Maffione 	/* minimum gap between two consecutive offsets into the same
542a6d768d8SVincenzo Maffione 	 * buffer, as stipulated at bind time. This is used to choose
543a6d768d8SVincenzo Maffione 	 * the hwbuf_len, but is not otherwise checked for compliance
544a6d768d8SVincenzo Maffione 	 * at runtime.
545a6d768d8SVincenzo Maffione 	 */
546a6d768d8SVincenzo Maffione 	uint64_t offset_gap;
547a6d768d8SVincenzo Maffione 
548a6d768d8SVincenzo Maffione 	/* size of hardware buffer. This may be less than the size of
549a6d768d8SVincenzo Maffione 	 * the netmap buffers because of non-zero offsets, or because
550a6d768d8SVincenzo Maffione 	 * the netmap buffer size exceeds the capability of the hardware.
551a6d768d8SVincenzo Maffione 	 */
552a6d768d8SVincenzo Maffione 	uint64_t hwbuf_len;
553a6d768d8SVincenzo Maffione 
55445c67e8fSVincenzo Maffione 	/* required alignment (in bytes) for the buffers used by this ring.
555a6d768d8SVincenzo Maffione 	 * Netmap buffers are aligned to cachelines, which should suffice
556a6d768d8SVincenzo Maffione 	 * for most NICs. If the user is passing offsets, though, we need
557a6d768d8SVincenzo Maffione 	 * to check that the resulting buf address complies with any
558a6d768d8SVincenzo Maffione 	 * alignment restriction.
559a6d768d8SVincenzo Maffione 	 */
560a6d768d8SVincenzo Maffione 	uint64_t buf_align;
561a6d768d8SVincenzo Maffione 
56245c67e8fSVincenzo Maffione 	/* hardware specific logic for the selection of the hwbuf_len */
563a6d768d8SVincenzo Maffione 	int (*nm_bufcfg)(struct netmap_kring *kring, uint64_t target);
564a6d768d8SVincenzo Maffione 
565847bf383SLuigi Rizzo 	int (*save_notify)(struct netmap_kring *kring, int flags);
566847bf383SLuigi Rizzo 
5674bf50f18SLuigi Rizzo #ifdef WITH_MONITOR
568847bf383SLuigi Rizzo 	/* array of krings that are monitoring this kring */
569847bf383SLuigi Rizzo 	struct netmap_kring **monitors;
570847bf383SLuigi Rizzo 	uint32_t max_monitors; /* current size of the monitors array */
571847bf383SLuigi Rizzo 	uint32_t n_monitors;	/* next unused entry in the monitor array */
572c3e9b4dbSLuiz Otavio O Souza 	uint32_t mon_pos[NR_TXRX]; /* index of this ring in the monitored ring array */
573c3e9b4dbSLuiz Otavio O Souza 	uint32_t mon_tail;  /* last seen slot on rx */
574c3e9b4dbSLuiz Otavio O Souza 
575c3e9b4dbSLuiz Otavio O Souza 	/* circular list of zero-copy monitors */
576c3e9b4dbSLuiz Otavio O Souza 	struct netmap_zmon_list zmon_list[NR_TXRX];
577c3e9b4dbSLuiz Otavio O Souza 
5784bf50f18SLuigi Rizzo 	/*
579847bf383SLuigi Rizzo 	 * Monitors work by intercepting the sync and notify callbacks of the
580847bf383SLuigi Rizzo 	 * monitored krings. This is implemented by replacing the pointers
581847bf383SLuigi Rizzo 	 * above and saving the previous ones in mon_* pointers below
5824bf50f18SLuigi Rizzo 	 */
583847bf383SLuigi Rizzo 	int (*mon_sync)(struct netmap_kring *kring, int flags);
584847bf383SLuigi Rizzo 	int (*mon_notify)(struct netmap_kring *kring, int flags);
585847bf383SLuigi Rizzo 
5864bf50f18SLuigi Rizzo #endif
58737e3a6d3SLuigi Rizzo }
58837e3a6d3SLuigi Rizzo #ifdef _WIN32
58937e3a6d3SLuigi Rizzo __declspec(align(64));
59037e3a6d3SLuigi Rizzo #else
59137e3a6d3SLuigi Rizzo __attribute__((__aligned__(64)));
59237e3a6d3SLuigi Rizzo #endif
59368b8534bSLuigi Rizzo 
59437e3a6d3SLuigi Rizzo /* return 1 iff the kring needs to be turned on */
59537e3a6d3SLuigi Rizzo static inline int
59637e3a6d3SLuigi Rizzo nm_kring_pending_on(struct netmap_kring *kring)
59737e3a6d3SLuigi Rizzo {
59837e3a6d3SLuigi Rizzo 	return kring->nr_pending_mode == NKR_NETMAP_ON &&
59937e3a6d3SLuigi Rizzo 	       kring->nr_mode == NKR_NETMAP_OFF;
60037e3a6d3SLuigi Rizzo }
60137e3a6d3SLuigi Rizzo 
60237e3a6d3SLuigi Rizzo /* return 1 iff the kring needs to be turned off */
60337e3a6d3SLuigi Rizzo static inline int
60437e3a6d3SLuigi Rizzo nm_kring_pending_off(struct netmap_kring *kring)
60537e3a6d3SLuigi Rizzo {
60637e3a6d3SLuigi Rizzo 	return kring->nr_pending_mode == NKR_NETMAP_OFF &&
60737e3a6d3SLuigi Rizzo 	       kring->nr_mode == NKR_NETMAP_ON;
60837e3a6d3SLuigi Rizzo }
609ce3ee1e7SLuigi Rizzo 
610ce3ee1e7SLuigi Rizzo /* return the next index, with wraparound */
611ce3ee1e7SLuigi Rizzo static inline uint32_t
612ce3ee1e7SLuigi Rizzo nm_next(uint32_t i, uint32_t lim)
613ce3ee1e7SLuigi Rizzo {
614ce3ee1e7SLuigi Rizzo 	return unlikely (i == lim) ? 0 : i + 1;
615ce3ee1e7SLuigi Rizzo }
616ce3ee1e7SLuigi Rizzo 
61717885a7bSLuigi Rizzo 
61817885a7bSLuigi Rizzo /* return the previous index, with wraparound */
61917885a7bSLuigi Rizzo static inline uint32_t
62017885a7bSLuigi Rizzo nm_prev(uint32_t i, uint32_t lim)
62117885a7bSLuigi Rizzo {
62217885a7bSLuigi Rizzo 	return unlikely (i == 0) ? lim : i - 1;
62317885a7bSLuigi Rizzo }
62417885a7bSLuigi Rizzo 
62517885a7bSLuigi Rizzo 
626ce3ee1e7SLuigi Rizzo /*
627ce3ee1e7SLuigi Rizzo  *
628ce3ee1e7SLuigi Rizzo  * Here is the layout for the Rx and Tx rings.
629ce3ee1e7SLuigi Rizzo 
630ce3ee1e7SLuigi Rizzo        RxRING                            TxRING
631ce3ee1e7SLuigi Rizzo 
632ce3ee1e7SLuigi Rizzo       +-----------------+            +-----------------+
633ce3ee1e7SLuigi Rizzo       |                 |            |                 |
6344f80b14cSVincenzo Maffione       |      free       |            |      free       |
635ce3ee1e7SLuigi Rizzo       +-----------------+            +-----------------+
63617885a7bSLuigi Rizzo head->| owned by user   |<-hwcur     | not sent to nic |<-hwcur
63717885a7bSLuigi Rizzo       |                 |            | yet             |
63817885a7bSLuigi Rizzo       +-----------------+            |                 |
63917885a7bSLuigi Rizzo  cur->| available to    |            |                 |
64017885a7bSLuigi Rizzo       | user, not read  |            +-----------------+
64117885a7bSLuigi Rizzo       | yet             |       cur->| (being          |
64217885a7bSLuigi Rizzo       |                 |            |  prepared)      |
643ce3ee1e7SLuigi Rizzo       |                 |            |                 |
64417885a7bSLuigi Rizzo       +-----------------+            +     ------      +
64517885a7bSLuigi Rizzo tail->|                 |<-hwtail    |                 |<-hwlease
64617885a7bSLuigi Rizzo       | (being          | ...        |                 | ...
64717885a7bSLuigi Rizzo       |  prepared)      | ...        |                 | ...
64817885a7bSLuigi Rizzo       +-----------------+ ...        |                 | ...
64917885a7bSLuigi Rizzo       |                 |<-hwlease   +-----------------+
65017885a7bSLuigi Rizzo       |                 |      tail->|                 |<-hwtail
651ce3ee1e7SLuigi Rizzo       |                 |            |                 |
652ce3ee1e7SLuigi Rizzo       |                 |            |                 |
653ce3ee1e7SLuigi Rizzo       |                 |            |                 |
654ce3ee1e7SLuigi Rizzo       +-----------------+            +-----------------+
655ce3ee1e7SLuigi Rizzo 
65617885a7bSLuigi Rizzo  * The cur/tail (user view) and hwcur/hwtail (kernel view)
657ce3ee1e7SLuigi Rizzo  * are used in the normal operation of the card.
658ce3ee1e7SLuigi Rizzo  *
659ce3ee1e7SLuigi Rizzo  * When a ring is the output of a switch port (Rx ring for
660ce3ee1e7SLuigi Rizzo  * a VALE port, Tx ring for the host stack or NIC), slots
661ce3ee1e7SLuigi Rizzo  * are reserved in blocks through 'hwlease' which points
662ce3ee1e7SLuigi Rizzo  * to the next unused slot.
66317885a7bSLuigi Rizzo  * On an Rx ring, hwlease is always after hwtail,
66417885a7bSLuigi Rizzo  * and completions cause hwtail to advance.
66517885a7bSLuigi Rizzo  * On a Tx ring, hwlease is always between cur and hwtail,
666ce3ee1e7SLuigi Rizzo  * and completions cause cur to advance.
667ce3ee1e7SLuigi Rizzo  *
668ce3ee1e7SLuigi Rizzo  * nm_kr_space() returns the maximum number of slots that
669ce3ee1e7SLuigi Rizzo  * can be assigned.
670ce3ee1e7SLuigi Rizzo  * nm_kr_lease() reserves the required number of buffers,
671ce3ee1e7SLuigi Rizzo  *    advances nkr_hwlease and also returns an entry in
672ce3ee1e7SLuigi Rizzo  *    a circular array where completions should be reported.
673ce3ee1e7SLuigi Rizzo  */
674ce3ee1e7SLuigi Rizzo 
6754f80b14cSVincenzo Maffione struct lut_entry;
6764f80b14cSVincenzo Maffione #ifdef __FreeBSD__
6774f80b14cSVincenzo Maffione #define plut_entry lut_entry
6784f80b14cSVincenzo Maffione #endif
679ce3ee1e7SLuigi Rizzo 
680847bf383SLuigi Rizzo struct netmap_lut {
681847bf383SLuigi Rizzo 	struct lut_entry *lut;
6824f80b14cSVincenzo Maffione 	struct plut_entry *plut;
683847bf383SLuigi Rizzo 	uint32_t objtotal;	/* max buffer index */
684847bf383SLuigi Rizzo 	uint32_t objsize;	/* buffer size */
685847bf383SLuigi Rizzo };
686ce3ee1e7SLuigi Rizzo 
6874bf50f18SLuigi Rizzo struct netmap_vp_adapter; // forward
6882a7db7a6SVincenzo Maffione struct nm_bridge;
6894bf50f18SLuigi Rizzo 
6902ff91c17SVincenzo Maffione /* Struct to be filled by nm_config callbacks. */
6912ff91c17SVincenzo Maffione struct nm_config_info {
6922ff91c17SVincenzo Maffione 	unsigned num_tx_rings;
6932ff91c17SVincenzo Maffione 	unsigned num_rx_rings;
6942ff91c17SVincenzo Maffione 	unsigned num_tx_descs;
6952ff91c17SVincenzo Maffione 	unsigned num_rx_descs;
6962ff91c17SVincenzo Maffione 	unsigned rx_buf_maxsize;
6972ff91c17SVincenzo Maffione };
6982ff91c17SVincenzo Maffione 
69968b8534bSLuigi Rizzo /*
7002a7db7a6SVincenzo Maffione  * default type for the magic field.
70145c67e8fSVincenzo Maffione  * May be overridden in glue code.
7022a7db7a6SVincenzo Maffione  */
7032a7db7a6SVincenzo Maffione #ifndef NM_OS_MAGIC
7042a7db7a6SVincenzo Maffione #define NM_OS_MAGIC uint32_t
7052a7db7a6SVincenzo Maffione #endif /* !NM_OS_MAGIC */
7062a7db7a6SVincenzo Maffione 
7072a7db7a6SVincenzo Maffione /*
708f9790aebSLuigi Rizzo  * The "struct netmap_adapter" extends the "struct adapter"
709f9790aebSLuigi Rizzo  * (or equivalent) device descriptor.
710f9790aebSLuigi Rizzo  * It contains all base fields needed to support netmap operation.
711f9790aebSLuigi Rizzo  * There are in fact different types of netmap adapters
712f9790aebSLuigi Rizzo  * (native, generic, VALE switch...) so a netmap_adapter is
713f9790aebSLuigi Rizzo  * just the first field in the derived type.
71468b8534bSLuigi Rizzo  */
71568b8534bSLuigi Rizzo struct netmap_adapter {
7168241616dSLuigi Rizzo 	/*
7178241616dSLuigi Rizzo 	 * On linux we do not have a good way to tell if an interface
718f9790aebSLuigi Rizzo 	 * is netmap-capable. So we always use the following trick:
7198241616dSLuigi Rizzo 	 * NA(ifp) points here, and the first entry (which hopefully
7208241616dSLuigi Rizzo 	 * always exists and is at least 32 bits) contains a magic
7218241616dSLuigi Rizzo 	 * value which we can use to detect that the interface is good.
7228241616dSLuigi Rizzo 	 */
7232a7db7a6SVincenzo Maffione 	NM_OS_MAGIC magic;
724f9790aebSLuigi Rizzo 	uint32_t na_flags;	/* enabled, and other flags */
7258241616dSLuigi Rizzo #define NAF_SKIP_INTR	1	/* use the regular interrupt handler.
7268241616dSLuigi Rizzo 				 * useful during initialization
7278241616dSLuigi Rizzo 				 */
728f18be576SLuigi Rizzo #define NAF_SW_ONLY	2	/* forward packets only to sw adapter */
729ce3ee1e7SLuigi Rizzo #define NAF_BDG_MAYSLEEP 4	/* the bridge is allowed to sleep when
730ce3ee1e7SLuigi Rizzo 				 * forwarding packets coming from this
731ce3ee1e7SLuigi Rizzo 				 * interface
732ce3ee1e7SLuigi Rizzo 				 */
733847bf383SLuigi Rizzo #define NAF_MEM_OWNER	8	/* the adapter uses its own memory area
734847bf383SLuigi Rizzo 				 * that cannot be changed
735ce3ee1e7SLuigi Rizzo 				 */
736847bf383SLuigi Rizzo #define NAF_NATIVE      16      /* the adapter is native.
73785fe4e7cSLuigi Rizzo 				 * Virtual ports (non persistent vale ports,
73885fe4e7cSLuigi Rizzo 				 * pipes, monitors...) should never use
73985fe4e7cSLuigi Rizzo 				 * this flag.
740f9790aebSLuigi Rizzo 				 */
741f9790aebSLuigi Rizzo #define	NAF_NETMAP_ON	32	/* netmap is active (either native or
7424bf50f18SLuigi Rizzo 				 * emulated). Where possible (e.g. FreeBSD)
743f9790aebSLuigi Rizzo 				 * IFCAP_NETMAP also mirrors this flag.
744f9790aebSLuigi Rizzo 				 */
745f0ea3689SLuigi Rizzo #define NAF_HOST_RINGS  64	/* the adapter supports the host rings */
7464bf50f18SLuigi Rizzo #define NAF_FORCE_NATIVE 128	/* the adapter is always NATIVE */
747b6e66be2SVincenzo Maffione /* free */
7484f80b14cSVincenzo Maffione #define NAF_MOREFRAG	512	/* the adapter supports NS_MOREFRAG */
749a6d768d8SVincenzo Maffione #define NAF_OFFSETS	1024	/* the adapter supports the slot offsets */
750a6d768d8SVincenzo Maffione #define NAF_HOST_ALL	2048	/* the adapter wants as many host rings as hw */
75137e3a6d3SLuigi Rizzo #define NAF_ZOMBIE	(1U<<30) /* the nic driver has been unloaded */
7524bf50f18SLuigi Rizzo #define	NAF_BUSY	(1U<<31) /* the adapter is used internally and
7534bf50f18SLuigi Rizzo 				  * cannot be registered from userspace
7544bf50f18SLuigi Rizzo 				  */
755f9790aebSLuigi Rizzo 	int active_fds; /* number of user-space descriptors using this
75668b8534bSLuigi Rizzo 			 interface, which is equal to the number of
75768b8534bSLuigi Rizzo 			 struct netmap_if objs in the mapped region. */
75868b8534bSLuigi Rizzo 
75924e57ec9SEd Maste 	u_int num_rx_rings; /* number of adapter receive rings */
76024e57ec9SEd Maste 	u_int num_tx_rings; /* number of adapter transmit rings */
7612a7db7a6SVincenzo Maffione 	u_int num_host_rx_rings; /* number of host receive rings */
7622a7db7a6SVincenzo Maffione 	u_int num_host_tx_rings; /* number of host transmit rings */
76368b8534bSLuigi Rizzo 
76468b8534bSLuigi Rizzo 	u_int num_tx_desc;  /* number of descriptor in each queue */
76568b8534bSLuigi Rizzo 	u_int num_rx_desc;
76668b8534bSLuigi Rizzo 
767b6e66be2SVincenzo Maffione 	/* tx_rings and rx_rings are private but allocated as a
768b6e66be2SVincenzo Maffione 	 * contiguous chunk of memory. Each array has N+K entries,
769b6e66be2SVincenzo Maffione 	 * N for the hardware rings and K for the host rings.
77068b8534bSLuigi Rizzo 	 */
7712ff91c17SVincenzo Maffione 	struct netmap_kring **tx_rings; /* array of TX rings. */
7722ff91c17SVincenzo Maffione 	struct netmap_kring **rx_rings; /* array of RX rings. */
77317885a7bSLuigi Rizzo 
774f9790aebSLuigi Rizzo 	void *tailroom;		       /* space below the rings array */
775f9790aebSLuigi Rizzo 				       /* (used for leases) */
776f9790aebSLuigi Rizzo 
77768b8534bSLuigi Rizzo 
778847bf383SLuigi Rizzo 	NM_SELINFO_T si[NR_TXRX];	/* global wait queues */
77964ae02c3SLuigi Rizzo 
780f0ea3689SLuigi Rizzo 	/* count users of the global wait queues */
781847bf383SLuigi Rizzo 	int si_users[NR_TXRX];
782f0ea3689SLuigi Rizzo 
7834bf50f18SLuigi Rizzo 	void *pdev; /* used to store pci device */
7844bf50f18SLuigi Rizzo 
78568b8534bSLuigi Rizzo 	/* copy of if_qflush and if_transmit pointers, to intercept
78668b8534bSLuigi Rizzo 	 * packets from the network stack when netmap is active.
78768b8534bSLuigi Rizzo 	 */
788e330262fSJustin Hibbits 	int     (*if_transmit)(if_t, struct mbuf *);
78968b8534bSLuigi Rizzo 
79017885a7bSLuigi Rizzo 	/* copy of if_input for netmap_send_up() */
791e330262fSJustin Hibbits 	void     (*if_input)(if_t, struct mbuf *);
79217885a7bSLuigi Rizzo 
7934f80b14cSVincenzo Maffione 	/* Back reference to the parent ifnet struct. Used for
7944f80b14cSVincenzo Maffione 	 * hardware ports (emulated netmap included). */
795e330262fSJustin Hibbits 	if_t ifp; /* adapter is if_getsoftc(ifp) */
79668b8534bSLuigi Rizzo 
79717885a7bSLuigi Rizzo 	/*---- callbacks for this netmap adapter -----*/
79817885a7bSLuigi Rizzo 	/*
79917885a7bSLuigi Rizzo 	 * nm_dtor() is the cleanup routine called when destroying
80017885a7bSLuigi Rizzo 	 *	the adapter.
80189cc2556SLuigi Rizzo 	 *	Called with NMG_LOCK held.
80217885a7bSLuigi Rizzo 	 *
80317885a7bSLuigi Rizzo 	 * nm_register() is called on NIOCREGIF and close() to enter
80417885a7bSLuigi Rizzo 	 *	or exit netmap mode on the NIC
8054bf50f18SLuigi Rizzo 	 *	Called with NNG_LOCK held.
80617885a7bSLuigi Rizzo 	 *
80717885a7bSLuigi Rizzo 	 * nm_txsync() pushes packets to the underlying hw/switch
80817885a7bSLuigi Rizzo 	 *
80917885a7bSLuigi Rizzo 	 * nm_rxsync() collects packets from the underlying hw/switch
81017885a7bSLuigi Rizzo 	 *
81117885a7bSLuigi Rizzo 	 * nm_config() returns configuration information from the OS
81289cc2556SLuigi Rizzo 	 *	Called with NMG_LOCK held.
81317885a7bSLuigi Rizzo 	 *
814a6d768d8SVincenzo Maffione 	 * nm_bufcfg()
815a6d768d8SVincenzo Maffione 	 *      the purpose of this callback is to fill the kring->hwbuf_len
816a6d768d8SVincenzo Maffione 	 *      (l) and kring->buf_align fields. The l value is most important
817a6d768d8SVincenzo Maffione 	 *      for RX rings, where we want to disallow writes outside of the
818a6d768d8SVincenzo Maffione 	 *      netmap buffer. The l value must be computed taking into account
81945c67e8fSVincenzo Maffione 	 *      the stipulated max_offset (o), possibly increased if there are
820a6d768d8SVincenzo Maffione 	 *      alignment constraints, the maxframe (m), if known, and the
821a6d768d8SVincenzo Maffione 	 *      current NETMAP_BUF_SIZE (b) of the memory region used by the
822a6d768d8SVincenzo Maffione 	 *      adapter. We want the largest supported l such that o + l <= b.
823a6d768d8SVincenzo Maffione 	 *      If m is known to be <= b - o, the callback may also choose the
82466671ae5SVincenzo Maffione 	 *      largest l <= m, ignoring the offset.  The buf_align field is
825a6d768d8SVincenzo Maffione 	 *      most important for TX rings when there are offsets.  The user
826a6d768d8SVincenzo Maffione 	 *      will see this value in the ring->buf_align field.  Misaligned
827a6d768d8SVincenzo Maffione 	 *      offsets will cause the corresponding packets to be silently
828a6d768d8SVincenzo Maffione 	 *      dropped.
829a6d768d8SVincenzo Maffione 	 *
8304bf50f18SLuigi Rizzo 	 * nm_krings_create() create and init the tx_rings and
8314bf50f18SLuigi Rizzo 	 * 	rx_rings arrays of kring structures. In particular,
8324bf50f18SLuigi Rizzo 	 * 	set the nm_sync callbacks for each ring.
8334bf50f18SLuigi Rizzo 	 * 	There is no need to also allocate the corresponding
8344bf50f18SLuigi Rizzo 	 * 	netmap_rings, since netmap_mem_rings_create() will always
8354bf50f18SLuigi Rizzo 	 * 	be called to provide the missing ones.
8364bf50f18SLuigi Rizzo 	 *	Called with NNG_LOCK held.
83717885a7bSLuigi Rizzo 	 *
8384bf50f18SLuigi Rizzo 	 * nm_krings_delete() cleanup and delete the tx_rings and rx_rings
8394bf50f18SLuigi Rizzo 	 * 	arrays
8404bf50f18SLuigi Rizzo 	 *	Called with NMG_LOCK held.
84117885a7bSLuigi Rizzo 	 *
84289cc2556SLuigi Rizzo 	 * nm_notify() is used to act after data have become available
84389cc2556SLuigi Rizzo 	 * 	(or the stopped state of the ring has changed)
84417885a7bSLuigi Rizzo 	 *	For hw devices this is typically a selwakeup(),
84517885a7bSLuigi Rizzo 	 *	but for NIC/host ports attached to a switch (or vice-versa)
84617885a7bSLuigi Rizzo 	 *	we also need to invoke the 'txsync' code downstream.
84737e3a6d3SLuigi Rizzo 	 *      This callback pointer is actually used only to initialize
84837e3a6d3SLuigi Rizzo 	 *      kring->nm_notify.
84937e3a6d3SLuigi Rizzo 	 *      Return values are the same as for netmap_rx_irq().
85017885a7bSLuigi Rizzo 	 */
851f9790aebSLuigi Rizzo 	void (*nm_dtor)(struct netmap_adapter *);
8521a26580eSLuigi Rizzo 
853f9790aebSLuigi Rizzo 	int (*nm_register)(struct netmap_adapter *, int onoff);
85437e3a6d3SLuigi Rizzo 	void (*nm_intr)(struct netmap_adapter *, int onoff);
855ce3ee1e7SLuigi Rizzo 
8564bf50f18SLuigi Rizzo 	int (*nm_txsync)(struct netmap_kring *kring, int flags);
8574bf50f18SLuigi Rizzo 	int (*nm_rxsync)(struct netmap_kring *kring, int flags);
858847bf383SLuigi Rizzo 	int (*nm_notify)(struct netmap_kring *kring, int flags);
859a6d768d8SVincenzo Maffione 	int (*nm_bufcfg)(struct netmap_kring *kring, uint64_t target);
860ce3ee1e7SLuigi Rizzo #define NAF_FORCE_READ      1
861ce3ee1e7SLuigi Rizzo #define NAF_FORCE_RECLAIM   2
862c3e9b4dbSLuiz Otavio O Souza #define NAF_CAN_FORWARD_DOWN 4
863ae10d1afSLuigi Rizzo 	/* return configuration information */
8642ff91c17SVincenzo Maffione 	int (*nm_config)(struct netmap_adapter *, struct nm_config_info *info);
865f9790aebSLuigi Rizzo 	int (*nm_krings_create)(struct netmap_adapter *);
866f9790aebSLuigi Rizzo 	void (*nm_krings_delete)(struct netmap_adapter *);
8674bf50f18SLuigi Rizzo 	/*
8684bf50f18SLuigi Rizzo 	 * nm_bdg_attach() initializes the na_vp field to point
8694bf50f18SLuigi Rizzo 	 *      to an adapter that can be attached to a VALE switch. If the
8704bf50f18SLuigi Rizzo 	 *      current adapter is already a VALE port, na_vp is simply a cast;
8714bf50f18SLuigi Rizzo 	 *      otherwise, na_vp points to a netmap_bwrap_adapter.
8724bf50f18SLuigi Rizzo 	 *      If applicable, this callback also initializes na_hostvp,
8734bf50f18SLuigi Rizzo 	 *      that can be used to connect the adapter host rings to the
8744bf50f18SLuigi Rizzo 	 *      switch.
8754bf50f18SLuigi Rizzo 	 *      Called with NMG_LOCK held.
8764bf50f18SLuigi Rizzo 	 *
8774bf50f18SLuigi Rizzo 	 * nm_bdg_ctl() is called on the actual attach/detach to/from
8784bf50f18SLuigi Rizzo 	 *      to/from the switch, to perform adapter-specific
8794bf50f18SLuigi Rizzo 	 *      initializations
8804bf50f18SLuigi Rizzo 	 *      Called with NMG_LOCK held.
8814bf50f18SLuigi Rizzo 	 */
8822a7db7a6SVincenzo Maffione 	int (*nm_bdg_attach)(const char *bdg_name, struct netmap_adapter *,
8832a7db7a6SVincenzo Maffione 			struct nm_bridge *);
8842ff91c17SVincenzo Maffione 	int (*nm_bdg_ctl)(struct nmreq_header *, struct netmap_adapter *);
8854bf50f18SLuigi Rizzo 
8864bf50f18SLuigi Rizzo 	/* adapter used to attach this adapter to a VALE switch (if any) */
8874bf50f18SLuigi Rizzo 	struct netmap_vp_adapter *na_vp;
8884bf50f18SLuigi Rizzo 	/* adapter used to attach the host rings of this adapter
8894bf50f18SLuigi Rizzo 	 * to a VALE switch (if any) */
8904bf50f18SLuigi Rizzo 	struct netmap_vp_adapter *na_hostvp;
891f9790aebSLuigi Rizzo 
892f9790aebSLuigi Rizzo 	/* standard refcount to control the lifetime of the adapter
893f9790aebSLuigi Rizzo 	 * (it should be equal to the lifetime of the corresponding ifp)
894f9790aebSLuigi Rizzo 	 */
895f9790aebSLuigi Rizzo 	int na_refcount;
896f9790aebSLuigi Rizzo 
897f9790aebSLuigi Rizzo 	/* memory allocator (opaque)
898f9790aebSLuigi Rizzo 	 * We also cache a pointer to the lut_entry for translating
89937e3a6d3SLuigi Rizzo 	 * buffer addresses, the total number of buffers and the buffer size.
900f9790aebSLuigi Rizzo 	 */
901f9790aebSLuigi Rizzo  	struct netmap_mem_d *nm_mem;
9024f80b14cSVincenzo Maffione 	struct netmap_mem_d *nm_mem_prev;
903847bf383SLuigi Rizzo 	struct netmap_lut na_lut;
904f9790aebSLuigi Rizzo 
9054bf50f18SLuigi Rizzo 	/* additional information attached to this adapter
9064bf50f18SLuigi Rizzo 	 * by other netmap subsystems. Currently used by
90737e3a6d3SLuigi Rizzo 	 * bwrap, LINUX/v1000 and ptnetmap
908f9790aebSLuigi Rizzo 	 */
909f9790aebSLuigi Rizzo 	void *na_private;
910f0ea3689SLuigi Rizzo 
9114bf50f18SLuigi Rizzo 	/* array of pipes that have this adapter as a parent */
912f0ea3689SLuigi Rizzo 	struct netmap_pipe_adapter **na_pipes;
9134bf50f18SLuigi Rizzo 	int na_next_pipe;	/* next free slot in the array */
9144bf50f18SLuigi Rizzo 	int na_max_pipes;	/* size of the array */
9154bf50f18SLuigi Rizzo 
91637e3a6d3SLuigi Rizzo 	/* Offset of ethernet header for each packet. */
91737e3a6d3SLuigi Rizzo 	u_int virt_hdr_len;
91837e3a6d3SLuigi Rizzo 
9192ff91c17SVincenzo Maffione 	/* Max number of bytes that the NIC can store in the buffer
9202ff91c17SVincenzo Maffione 	 * referenced by each RX descriptor. This translates to the maximum
9212ff91c17SVincenzo Maffione 	 * bytes that a single netmap slot can reference. Larger packets
9222ff91c17SVincenzo Maffione 	 * require NS_MOREFRAG support. */
9232ff91c17SVincenzo Maffione 	unsigned rx_buf_maxsize;
9242ff91c17SVincenzo Maffione 
9252ff91c17SVincenzo Maffione 	char name[NETMAP_REQ_IFNAMSIZ]; /* used at least by pipes */
9262a7db7a6SVincenzo Maffione 
9272a7db7a6SVincenzo Maffione #ifdef WITH_MONITOR
9282a7db7a6SVincenzo Maffione 	unsigned long	monitor_id;	/* debugging */
9292a7db7a6SVincenzo Maffione #endif
930f9790aebSLuigi Rizzo };
931f9790aebSLuigi Rizzo 
932847bf383SLuigi Rizzo static __inline u_int
933847bf383SLuigi Rizzo nma_get_ndesc(struct netmap_adapter *na, enum txrx t)
934847bf383SLuigi Rizzo {
935847bf383SLuigi Rizzo 	return (t == NR_TX ? na->num_tx_desc : na->num_rx_desc);
936847bf383SLuigi Rizzo }
937847bf383SLuigi Rizzo 
938847bf383SLuigi Rizzo static __inline void
939847bf383SLuigi Rizzo nma_set_ndesc(struct netmap_adapter *na, enum txrx t, u_int v)
940847bf383SLuigi Rizzo {
941847bf383SLuigi Rizzo 	if (t == NR_TX)
942847bf383SLuigi Rizzo 		na->num_tx_desc = v;
943847bf383SLuigi Rizzo 	else
944847bf383SLuigi Rizzo 		na->num_rx_desc = v;
945847bf383SLuigi Rizzo }
946847bf383SLuigi Rizzo 
947847bf383SLuigi Rizzo static __inline u_int
948847bf383SLuigi Rizzo nma_get_nrings(struct netmap_adapter *na, enum txrx t)
949847bf383SLuigi Rizzo {
950847bf383SLuigi Rizzo 	return (t == NR_TX ? na->num_tx_rings : na->num_rx_rings);
951847bf383SLuigi Rizzo }
952847bf383SLuigi Rizzo 
9532a7db7a6SVincenzo Maffione static __inline u_int
9542a7db7a6SVincenzo Maffione nma_get_host_nrings(struct netmap_adapter *na, enum txrx t)
9552a7db7a6SVincenzo Maffione {
9562a7db7a6SVincenzo Maffione 	return (t == NR_TX ? na->num_host_tx_rings : na->num_host_rx_rings);
9572a7db7a6SVincenzo Maffione }
9582a7db7a6SVincenzo Maffione 
959847bf383SLuigi Rizzo static __inline void
960847bf383SLuigi Rizzo nma_set_nrings(struct netmap_adapter *na, enum txrx t, u_int v)
961847bf383SLuigi Rizzo {
962847bf383SLuigi Rizzo 	if (t == NR_TX)
963847bf383SLuigi Rizzo 		na->num_tx_rings = v;
964847bf383SLuigi Rizzo 	else
965847bf383SLuigi Rizzo 		na->num_rx_rings = v;
966847bf383SLuigi Rizzo }
967847bf383SLuigi Rizzo 
9682a7db7a6SVincenzo Maffione static __inline void
9692a7db7a6SVincenzo Maffione nma_set_host_nrings(struct netmap_adapter *na, enum txrx t, u_int v)
9702a7db7a6SVincenzo Maffione {
9712a7db7a6SVincenzo Maffione 	if (t == NR_TX)
9722a7db7a6SVincenzo Maffione 		na->num_host_tx_rings = v;
9732a7db7a6SVincenzo Maffione 	else
9742a7db7a6SVincenzo Maffione 		na->num_host_rx_rings = v;
9752a7db7a6SVincenzo Maffione }
9762a7db7a6SVincenzo Maffione 
9772ff91c17SVincenzo Maffione static __inline struct netmap_kring**
978847bf383SLuigi Rizzo NMR(struct netmap_adapter *na, enum txrx t)
979847bf383SLuigi Rizzo {
980847bf383SLuigi Rizzo 	return (t == NR_TX ? na->tx_rings : na->rx_rings);
981847bf383SLuigi Rizzo }
98217885a7bSLuigi Rizzo 
9834f80b14cSVincenzo Maffione int nma_intr_enable(struct netmap_adapter *na, int onoff);
9844f80b14cSVincenzo Maffione 
985f9790aebSLuigi Rizzo /*
986f9790aebSLuigi Rizzo  * If the NIC is owned by the kernel
987f9790aebSLuigi Rizzo  * (i.e., bridge), neither another bridge nor user can use it;
988f9790aebSLuigi Rizzo  * if the NIC is owned by a user, only users can share it.
989f9790aebSLuigi Rizzo  * Evaluation must be done under NMG_LOCK().
990f9790aebSLuigi Rizzo  */
9914bf50f18SLuigi Rizzo #define NETMAP_OWNED_BY_KERN(na)	((na)->na_flags & NAF_BUSY)
992f9790aebSLuigi Rizzo #define NETMAP_OWNED_BY_ANY(na) \
9934bf50f18SLuigi Rizzo 	(NETMAP_OWNED_BY_KERN(na) || ((na)->active_fds > 0))
994f9790aebSLuigi Rizzo 
995f9790aebSLuigi Rizzo /*
996f9790aebSLuigi Rizzo  * derived netmap adapters for various types of ports
997f9790aebSLuigi Rizzo  */
998f9790aebSLuigi Rizzo struct netmap_vp_adapter {	/* VALE software port */
999f9790aebSLuigi Rizzo 	struct netmap_adapter up;
1000f196ce38SLuigi Rizzo 
1001849bec0eSLuigi Rizzo 	/*
1002849bec0eSLuigi Rizzo 	 * Bridge support:
1003849bec0eSLuigi Rizzo 	 *
1004849bec0eSLuigi Rizzo 	 * bdg_port is the port number used in the bridge;
1005f18be576SLuigi Rizzo 	 * na_bdg points to the bridge this NA is attached to.
1006849bec0eSLuigi Rizzo 	 */
1007f196ce38SLuigi Rizzo 	int bdg_port;
1008f18be576SLuigi Rizzo 	struct nm_bridge *na_bdg;
1009f9790aebSLuigi Rizzo 	int retry;
1010c3e9b4dbSLuiz Otavio O Souza 	int autodelete; /* remove the ifp on last reference */
1011f9790aebSLuigi Rizzo 
1012f0ea3689SLuigi Rizzo 	/* Maximum Frame Size, used in bdg_mismatch_datapath() */
1013f0ea3689SLuigi Rizzo 	u_int mfs;
1014847bf383SLuigi Rizzo 	/* Last source MAC on this port */
1015847bf383SLuigi Rizzo 	uint64_t last_smac;
1016f9790aebSLuigi Rizzo };
1017f9790aebSLuigi Rizzo 
101817885a7bSLuigi Rizzo 
1019f9790aebSLuigi Rizzo struct netmap_hw_adapter {	/* physical device */
1020f9790aebSLuigi Rizzo 	struct netmap_adapter up;
1021f9790aebSLuigi Rizzo 
10224f80b14cSVincenzo Maffione #ifdef linux
10234f80b14cSVincenzo Maffione 	struct net_device_ops nm_ndo;
10244f80b14cSVincenzo Maffione 	struct ethtool_ops    nm_eto;
10254f80b14cSVincenzo Maffione #endif
10264bf50f18SLuigi Rizzo 	const struct ethtool_ops*   save_ethtool;
10274bf50f18SLuigi Rizzo 
10284bf50f18SLuigi Rizzo 	int (*nm_hw_register)(struct netmap_adapter *, int onoff);
1029f9790aebSLuigi Rizzo };
1030f9790aebSLuigi Rizzo 
1031039dd540SLuigi Rizzo #ifdef WITH_GENERIC
1032f0ea3689SLuigi Rizzo /* Mitigation support. */
1033f0ea3689SLuigi Rizzo struct nm_generic_mit {
1034f0ea3689SLuigi Rizzo 	struct hrtimer mit_timer;
1035f0ea3689SLuigi Rizzo 	int mit_pending;
10364bf50f18SLuigi Rizzo 	int mit_ring_idx;  /* index of the ring being mitigated */
1037f0ea3689SLuigi Rizzo 	struct netmap_adapter *mit_na;  /* backpointer */
1038f0ea3689SLuigi Rizzo };
103917885a7bSLuigi Rizzo 
104017885a7bSLuigi Rizzo struct netmap_generic_adapter {	/* emulated device */
1041f9790aebSLuigi Rizzo 	struct netmap_hw_adapter up;
1042f9790aebSLuigi Rizzo 
1043f9790aebSLuigi Rizzo 	/* Pointer to a previously used netmap adapter. */
1044f9790aebSLuigi Rizzo 	struct netmap_adapter *prev;
1045f9790aebSLuigi Rizzo 
10464f80b14cSVincenzo Maffione 	/* Emulated netmap adapters support:
10474f80b14cSVincenzo Maffione 	 *  - mit implements rx interrupt mitigation;
1048f9790aebSLuigi Rizzo 	 */
1049f0ea3689SLuigi Rizzo 	struct nm_generic_mit *mit;
105017885a7bSLuigi Rizzo #ifdef linux
1051e330262fSJustin Hibbits         netdev_tx_t (*save_start_xmit)(struct mbuf *, if_t);
105217885a7bSLuigi Rizzo #endif
105337e3a6d3SLuigi Rizzo 	/* Is the adapter able to use multiple RX slots to scatter
105437e3a6d3SLuigi Rizzo 	 * each packet pushed up by the driver? */
105537e3a6d3SLuigi Rizzo 	int rxsg;
105637e3a6d3SLuigi Rizzo 
105737e3a6d3SLuigi Rizzo 	/* Is the transmission path controlled by a netmap-aware
105837e3a6d3SLuigi Rizzo 	 * device queue (i.e. qdisc on linux)? */
105937e3a6d3SLuigi Rizzo 	int txqdisc;
1060f9790aebSLuigi Rizzo };
1061039dd540SLuigi Rizzo #endif  /* WITH_GENERIC */
1062f9790aebSLuigi Rizzo 
10632a7db7a6SVincenzo Maffione static __inline u_int
1064847bf383SLuigi Rizzo netmap_real_rings(struct netmap_adapter *na, enum txrx t)
1065f0ea3689SLuigi Rizzo {
10662a7db7a6SVincenzo Maffione 	return nma_get_nrings(na, t) +
10672a7db7a6SVincenzo Maffione 		!!(na->na_flags & NAF_HOST_RINGS) * nma_get_host_nrings(na, t);
1068f0ea3689SLuigi Rizzo }
1069f0ea3689SLuigi Rizzo 
10702a7db7a6SVincenzo Maffione /* account for fake rings */
10712a7db7a6SVincenzo Maffione static __inline u_int
10722a7db7a6SVincenzo Maffione netmap_all_rings(struct netmap_adapter *na, enum txrx t)
10732a7db7a6SVincenzo Maffione {
10742a7db7a6SVincenzo Maffione 	return max(nma_get_nrings(na, t) + 1, netmap_real_rings(na, t));
10752a7db7a6SVincenzo Maffione }
10762a7db7a6SVincenzo Maffione 
10772a7db7a6SVincenzo Maffione int netmap_default_bdg_attach(const char *name, struct netmap_adapter *na,
10782a7db7a6SVincenzo Maffione 		struct nm_bridge *);
107937e3a6d3SLuigi Rizzo struct nm_bdg_polling_state;
108017885a7bSLuigi Rizzo /*
108117885a7bSLuigi Rizzo  * Bridge wrapper for non VALE ports attached to a VALE switch.
1082f9790aebSLuigi Rizzo  *
108317885a7bSLuigi Rizzo  * The real device must already have its own netmap adapter (hwna).
108417885a7bSLuigi Rizzo  * The bridge wrapper and the hwna adapter share the same set of
108517885a7bSLuigi Rizzo  * netmap rings and buffers, but they have two separate sets of
108617885a7bSLuigi Rizzo  * krings descriptors, with tx/rx meanings swapped:
1087f9790aebSLuigi Rizzo  *
1088f9790aebSLuigi Rizzo  *                                  netmap
1089f9790aebSLuigi Rizzo  *           bwrap     krings       rings      krings      hwna
1090f9790aebSLuigi Rizzo  *         +------+   +------+     +-----+    +------+   +------+
1091f9790aebSLuigi Rizzo  *         |tx_rings->|      |\   /|     |----|      |<-tx_rings|
1092f9790aebSLuigi Rizzo  *         |      |   +------+ \ / +-----+    +------+   |      |
1093f9790aebSLuigi Rizzo  *         |      |             X                        |      |
1094f9790aebSLuigi Rizzo  *         |      |            / \                       |      |
1095f9790aebSLuigi Rizzo  *         |      |   +------+/   \+-----+    +------+   |      |
1096f9790aebSLuigi Rizzo  *         |rx_rings->|      |     |     |----|      |<-rx_rings|
1097f9790aebSLuigi Rizzo  *         |      |   +------+     +-----+    +------+   |      |
1098f9790aebSLuigi Rizzo  *         +------+                                      +------+
1099f9790aebSLuigi Rizzo  *
110017885a7bSLuigi Rizzo  * - packets coming from the bridge go to the brwap rx rings,
110117885a7bSLuigi Rizzo  *   which are also the hwna tx rings.  The bwrap notify callback
110217885a7bSLuigi Rizzo  *   will then complete the hwna tx (see netmap_bwrap_notify).
1103f9790aebSLuigi Rizzo  *
110417885a7bSLuigi Rizzo  * - packets coming from the outside go to the hwna rx rings,
110517885a7bSLuigi Rizzo  *   which are also the bwrap tx rings.  The (overwritten) hwna
110617885a7bSLuigi Rizzo  *   notify method will then complete the bridge tx
110717885a7bSLuigi Rizzo  *   (see netmap_bwrap_intr_notify).
1108f9790aebSLuigi Rizzo  *
110917885a7bSLuigi Rizzo  *   The bridge wrapper may optionally connect the hwna 'host' rings
111017885a7bSLuigi Rizzo  *   to the bridge. This is done by using a second port in the
111117885a7bSLuigi Rizzo  *   bridge and connecting it to the 'host' netmap_vp_adapter
111217885a7bSLuigi Rizzo  *   contained in the netmap_bwrap_adapter. The brwap host adapter
111317885a7bSLuigi Rizzo  *   cross-links the hwna host rings in the same way as shown above.
111417885a7bSLuigi Rizzo  *
111517885a7bSLuigi Rizzo  * - packets coming from the bridge and directed to the host stack
111617885a7bSLuigi Rizzo  *   are handled by the bwrap host notify callback
111717885a7bSLuigi Rizzo  *   (see netmap_bwrap_host_notify)
111817885a7bSLuigi Rizzo  *
111917885a7bSLuigi Rizzo  * - packets coming from the host stack are still handled by the
112017885a7bSLuigi Rizzo  *   overwritten hwna notify callback (netmap_bwrap_intr_notify),
112117885a7bSLuigi Rizzo  *   but are diverted to the host adapter depending on the ring number.
1122f9790aebSLuigi Rizzo  *
1123f9790aebSLuigi Rizzo  */
1124f9790aebSLuigi Rizzo struct netmap_bwrap_adapter {
1125f9790aebSLuigi Rizzo 	struct netmap_vp_adapter up;
1126f9790aebSLuigi Rizzo 	struct netmap_vp_adapter host;  /* for host rings */
1127f9790aebSLuigi Rizzo 	struct netmap_adapter *hwna;	/* the underlying device */
1128f9790aebSLuigi Rizzo 
112917885a7bSLuigi Rizzo 	/*
113017885a7bSLuigi Rizzo 	 * When we attach a physical interface to the bridge, we
1131f18be576SLuigi Rizzo 	 * allow the controlling process to terminate, so we need
11324bf50f18SLuigi Rizzo 	 * a place to store the n_detmap_priv_d data structure.
113317885a7bSLuigi Rizzo 	 * This is only done when physical interfaces
113417885a7bSLuigi Rizzo 	 * are attached to a bridge.
1135f18be576SLuigi Rizzo 	 */
1136f18be576SLuigi Rizzo 	struct netmap_priv_d *na_kpriv;
113737e3a6d3SLuigi Rizzo 	struct nm_bdg_polling_state *na_polling_state;
11382ff91c17SVincenzo Maffione 	/* we overwrite the hwna->na_vp pointer, so we save
11392ff91c17SVincenzo Maffione 	 * here its original value, to be restored at detach
11402ff91c17SVincenzo Maffione 	 */
11412ff91c17SVincenzo Maffione 	struct netmap_vp_adapter *saved_na_vp;
1142a6d768d8SVincenzo Maffione 	int (*nm_intr_notify)(struct netmap_kring *kring, int flags);
114368b8534bSLuigi Rizzo };
11444ad57c7aSVincenzo Maffione int nm_is_bwrap(struct netmap_adapter *na);
11452ff91c17SVincenzo Maffione int nm_bdg_polling(struct nmreq_header *hdr);
11462a7db7a6SVincenzo Maffione 
1147a6d768d8SVincenzo Maffione int netmap_bdg_attach(struct nmreq_header *hdr, void *auth_token);
1148a6d768d8SVincenzo Maffione int netmap_bdg_detach(struct nmreq_header *hdr, void *auth_token);
11492a7db7a6SVincenzo Maffione #ifdef WITH_VALE
1150b6e66be2SVincenzo Maffione int netmap_vale_list(struct nmreq_header *hdr);
11512ff91c17SVincenzo Maffione int netmap_vi_create(struct nmreq_header *hdr, int);
11522ff91c17SVincenzo Maffione int nm_vi_create(struct nmreq_header *);
11532ff91c17SVincenzo Maffione int nm_vi_destroy(const char *name);
1154c3e9b4dbSLuiz Otavio O Souza #else /* !WITH_VALE */
11552ff91c17SVincenzo Maffione #define netmap_vi_create(hdr, a) (EOPNOTSUPP)
115617885a7bSLuigi Rizzo #endif /* WITH_VALE */
1157ce3ee1e7SLuigi Rizzo 
1158f0ea3689SLuigi Rizzo #ifdef WITH_PIPES
1159f0ea3689SLuigi Rizzo 
1160f0ea3689SLuigi Rizzo #define NM_MAXPIPES 	64	/* max number of pipes per adapter */
1161f0ea3689SLuigi Rizzo 
1162f0ea3689SLuigi Rizzo struct netmap_pipe_adapter {
11632ff91c17SVincenzo Maffione 	/* pipe identifier is up.name */
1164f0ea3689SLuigi Rizzo 	struct netmap_adapter up;
1165f0ea3689SLuigi Rizzo 
11662ff91c17SVincenzo Maffione #define NM_PIPE_ROLE_MASTER	0x1
11672ff91c17SVincenzo Maffione #define NM_PIPE_ROLE_SLAVE	0x2
11682ff91c17SVincenzo Maffione 	int role;	/* either NM_PIPE_ROLE_MASTER or NM_PIPE_ROLE_SLAVE */
1169f0ea3689SLuigi Rizzo 
1170f0ea3689SLuigi Rizzo 	struct netmap_adapter *parent; /* adapter that owns the memory */
1171f0ea3689SLuigi Rizzo 	struct netmap_pipe_adapter *peer; /* the other end of the pipe */
1172f0ea3689SLuigi Rizzo 	int peer_ref;		/* 1 iff we are holding a ref to the peer */
1173e330262fSJustin Hibbits 	if_t parent_ifp;	/* maybe null */
1174f0ea3689SLuigi Rizzo 
1175f0ea3689SLuigi Rizzo 	u_int parent_slot; /* index in the parent pipe array */
1176f0ea3689SLuigi Rizzo };
1177f0ea3689SLuigi Rizzo 
1178f0ea3689SLuigi Rizzo #endif /* WITH_PIPES */
1179f0ea3689SLuigi Rizzo 
1180b6e66be2SVincenzo Maffione #ifdef WITH_NMNULL
1181b6e66be2SVincenzo Maffione struct netmap_null_adapter {
1182b6e66be2SVincenzo Maffione 	struct netmap_adapter up;
1183b6e66be2SVincenzo Maffione };
1184b6e66be2SVincenzo Maffione #endif /* WITH_NMNULL */
1185b6e66be2SVincenzo Maffione 
118617885a7bSLuigi Rizzo 
118717885a7bSLuigi Rizzo /* return slots reserved to rx clients; used in drivers */
118817885a7bSLuigi Rizzo static inline uint32_t
118917885a7bSLuigi Rizzo nm_kr_rxspace(struct netmap_kring *k)
119017885a7bSLuigi Rizzo {
119117885a7bSLuigi Rizzo 	int space = k->nr_hwtail - k->nr_hwcur;
1192ce3ee1e7SLuigi Rizzo 	if (space < 0)
1193ce3ee1e7SLuigi Rizzo 		space += k->nkr_num_slots;
119475f4f3edSVincenzo Maffione 	nm_prdis("preserving %d rx slots %d -> %d", space, k->nr_hwcur, k->nr_hwtail);
119517885a7bSLuigi Rizzo 
1196ce3ee1e7SLuigi Rizzo 	return space;
1197ce3ee1e7SLuigi Rizzo }
1198ce3ee1e7SLuigi Rizzo 
119937e3a6d3SLuigi Rizzo /* return slots reserved to tx clients */
120037e3a6d3SLuigi Rizzo #define nm_kr_txspace(_k) nm_kr_rxspace(_k)
1201ce3ee1e7SLuigi Rizzo 
120237e3a6d3SLuigi Rizzo 
120337e3a6d3SLuigi Rizzo /* True if no space in the tx ring, only valid after txsync_prologue */
120417885a7bSLuigi Rizzo static inline int
120517885a7bSLuigi Rizzo nm_kr_txempty(struct netmap_kring *kring)
1206ce3ee1e7SLuigi Rizzo {
1207f79ba6d7SVincenzo Maffione 	return kring->rhead == kring->nr_hwtail;
1208f9790aebSLuigi Rizzo }
1209f9790aebSLuigi Rizzo 
121037e3a6d3SLuigi Rizzo /* True if no more completed slots in the rx ring, only valid after
121137e3a6d3SLuigi Rizzo  * rxsync_prologue */
121237e3a6d3SLuigi Rizzo #define nm_kr_rxempty(_k)	nm_kr_txempty(_k)
1213ce3ee1e7SLuigi Rizzo 
12145faab778SVincenzo Maffione /* True if the application needs to wait for more space on the ring
12155faab778SVincenzo Maffione  * (more received packets or more free tx slots).
12165faab778SVincenzo Maffione  * Only valid after *xsync_prologue. */
12175faab778SVincenzo Maffione static inline int
12185faab778SVincenzo Maffione nm_kr_wouldblock(struct netmap_kring *kring)
12195faab778SVincenzo Maffione {
12205faab778SVincenzo Maffione 	return kring->rcur == kring->nr_hwtail;
12215faab778SVincenzo Maffione }
12225faab778SVincenzo Maffione 
1223ce3ee1e7SLuigi Rizzo /*
1224f9790aebSLuigi Rizzo  * protect against multiple threads using the same ring.
122537e3a6d3SLuigi Rizzo  * also check that the ring has not been stopped or locked
122668b8534bSLuigi Rizzo  */
122737e3a6d3SLuigi Rizzo #define NM_KR_BUSY	1	/* some other thread is syncing the ring */
122837e3a6d3SLuigi Rizzo #define NM_KR_STOPPED	2	/* unbounded stop (ifconfig down or driver unload) */
122937e3a6d3SLuigi Rizzo #define NM_KR_LOCKED	3	/* bounded, brief stop for mutual exclusion */
123068b8534bSLuigi Rizzo 
123117885a7bSLuigi Rizzo 
123237e3a6d3SLuigi Rizzo /* release the previously acquired right to use the *sync() methods of the ring */
1233f9790aebSLuigi Rizzo static __inline void nm_kr_put(struct netmap_kring *kr)
1234f9790aebSLuigi Rizzo {
1235f9790aebSLuigi Rizzo 	NM_ATOMIC_CLEAR(&kr->nr_busy);
1236f9790aebSLuigi Rizzo }
1237f9790aebSLuigi Rizzo 
123817885a7bSLuigi Rizzo 
123937e3a6d3SLuigi Rizzo /* true if the ifp that backed the adapter has disappeared (e.g., the
124037e3a6d3SLuigi Rizzo  * driver has been unloaded)
124137e3a6d3SLuigi Rizzo  */
124237e3a6d3SLuigi Rizzo static inline int nm_iszombie(struct netmap_adapter *na);
124337e3a6d3SLuigi Rizzo 
124437e3a6d3SLuigi Rizzo /* try to obtain exclusive right to issue the *sync() operations on the ring.
124537e3a6d3SLuigi Rizzo  * The right is obtained and must be later relinquished via nm_kr_put() if and
124637e3a6d3SLuigi Rizzo  * only if nm_kr_tryget() returns 0.
124737e3a6d3SLuigi Rizzo  * If can_sleep is 1 there are only two other possible outcomes:
124837e3a6d3SLuigi Rizzo  * - the function returns NM_KR_BUSY
124937e3a6d3SLuigi Rizzo  * - the function returns NM_KR_STOPPED and sets the POLLERR bit in *perr
125037e3a6d3SLuigi Rizzo  *   (if non-null)
125137e3a6d3SLuigi Rizzo  * In both cases the caller will typically skip the ring, possibly collecting
125237e3a6d3SLuigi Rizzo  * errors along the way.
125337e3a6d3SLuigi Rizzo  * If the calling context does not allow sleeping, the caller must pass 0 in can_sleep.
125437e3a6d3SLuigi Rizzo  * In the latter case, the function may also return NM_KR_LOCKED and leave *perr
125537e3a6d3SLuigi Rizzo  * untouched: ideally, the caller should try again at a later time.
125637e3a6d3SLuigi Rizzo  */
125737e3a6d3SLuigi Rizzo static __inline int nm_kr_tryget(struct netmap_kring *kr, int can_sleep, int *perr)
1258f9790aebSLuigi Rizzo {
125937e3a6d3SLuigi Rizzo 	int busy = 1, stopped;
1260f9790aebSLuigi Rizzo 	/* check a first time without taking the lock
1261f9790aebSLuigi Rizzo 	 * to avoid starvation for nm_kr_get()
1262f9790aebSLuigi Rizzo 	 */
126337e3a6d3SLuigi Rizzo retry:
126437e3a6d3SLuigi Rizzo 	stopped = kr->nkr_stopped;
126537e3a6d3SLuigi Rizzo 	if (unlikely(stopped)) {
126637e3a6d3SLuigi Rizzo 		goto stop;
1267f9790aebSLuigi Rizzo 	}
126837e3a6d3SLuigi Rizzo 	busy = NM_ATOMIC_TEST_AND_SET(&kr->nr_busy);
126937e3a6d3SLuigi Rizzo 	/* we should not return NM_KR_BUSY if the ring was
127037e3a6d3SLuigi Rizzo 	 * actually stopped, so check another time after
127137e3a6d3SLuigi Rizzo 	 * the barrier provided by the atomic operation
127237e3a6d3SLuigi Rizzo 	 */
127337e3a6d3SLuigi Rizzo 	stopped = kr->nkr_stopped;
127437e3a6d3SLuigi Rizzo 	if (unlikely(stopped)) {
127537e3a6d3SLuigi Rizzo 		goto stop;
1276f9790aebSLuigi Rizzo 	}
127768b8534bSLuigi Rizzo 
127837e3a6d3SLuigi Rizzo 	if (unlikely(nm_iszombie(kr->na))) {
127937e3a6d3SLuigi Rizzo 		stopped = NM_KR_STOPPED;
128037e3a6d3SLuigi Rizzo 		goto stop;
128137e3a6d3SLuigi Rizzo 	}
128237e3a6d3SLuigi Rizzo 
128337e3a6d3SLuigi Rizzo 	return unlikely(busy) ? NM_KR_BUSY : 0;
128437e3a6d3SLuigi Rizzo 
128537e3a6d3SLuigi Rizzo stop:
128637e3a6d3SLuigi Rizzo 	if (!busy)
128737e3a6d3SLuigi Rizzo 		nm_kr_put(kr);
128837e3a6d3SLuigi Rizzo 	if (stopped == NM_KR_STOPPED) {
128937e3a6d3SLuigi Rizzo /* if POLLERR is defined we want to use it to simplify netmap_poll().
129037e3a6d3SLuigi Rizzo  * Otherwise, any non-zero value will do.
129137e3a6d3SLuigi Rizzo  */
129237e3a6d3SLuigi Rizzo #ifdef POLLERR
129337e3a6d3SLuigi Rizzo #define NM_POLLERR POLLERR
129437e3a6d3SLuigi Rizzo #else
129537e3a6d3SLuigi Rizzo #define NM_POLLERR 1
129637e3a6d3SLuigi Rizzo #endif /* POLLERR */
129737e3a6d3SLuigi Rizzo 		if (perr)
129837e3a6d3SLuigi Rizzo 			*perr |= NM_POLLERR;
129937e3a6d3SLuigi Rizzo #undef NM_POLLERR
130037e3a6d3SLuigi Rizzo 	} else if (can_sleep) {
130137e3a6d3SLuigi Rizzo 		tsleep(kr, 0, "NM_KR_TRYGET", 4);
130237e3a6d3SLuigi Rizzo 		goto retry;
130337e3a6d3SLuigi Rizzo 	}
130437e3a6d3SLuigi Rizzo 	return stopped;
130537e3a6d3SLuigi Rizzo }
130637e3a6d3SLuigi Rizzo 
130737e3a6d3SLuigi Rizzo /* put the ring in the 'stopped' state and wait for the current user (if any) to
130837e3a6d3SLuigi Rizzo  * notice. stopped must be either NM_KR_STOPPED or NM_KR_LOCKED
130937e3a6d3SLuigi Rizzo  */
131037e3a6d3SLuigi Rizzo static __inline void nm_kr_stop(struct netmap_kring *kr, int stopped)
1311847bf383SLuigi Rizzo {
131237e3a6d3SLuigi Rizzo 	kr->nkr_stopped = stopped;
1313847bf383SLuigi Rizzo 	while (NM_ATOMIC_TEST_AND_SET(&kr->nr_busy))
1314847bf383SLuigi Rizzo 		tsleep(kr, 0, "NM_KR_GET", 4);
1315847bf383SLuigi Rizzo }
1316847bf383SLuigi Rizzo 
131737e3a6d3SLuigi Rizzo /* restart a ring after a stop */
131837e3a6d3SLuigi Rizzo static __inline void nm_kr_start(struct netmap_kring *kr)
131937e3a6d3SLuigi Rizzo {
132037e3a6d3SLuigi Rizzo 	kr->nkr_stopped = 0;
132137e3a6d3SLuigi Rizzo 	nm_kr_put(kr);
132237e3a6d3SLuigi Rizzo }
132337e3a6d3SLuigi Rizzo 
1324847bf383SLuigi Rizzo 
132568b8534bSLuigi Rizzo /*
132617885a7bSLuigi Rizzo  * The following functions are used by individual drivers to
132768b8534bSLuigi Rizzo  * support netmap operation.
132868b8534bSLuigi Rizzo  *
132968b8534bSLuigi Rizzo  * netmap_attach() initializes a struct netmap_adapter, allocating the
133068b8534bSLuigi Rizzo  * 	struct netmap_ring's and the struct selinfo.
133168b8534bSLuigi Rizzo  *
133268b8534bSLuigi Rizzo  * netmap_detach() frees the memory allocated by netmap_attach().
133368b8534bSLuigi Rizzo  *
1334ce3ee1e7SLuigi Rizzo  * netmap_transmit() replaces the if_transmit routine of the interface,
133568b8534bSLuigi Rizzo  *	and is used to intercept packets coming from the stack.
133668b8534bSLuigi Rizzo  *
133768b8534bSLuigi Rizzo  * netmap_load_map/netmap_reload_map are helper routines to set/reset
133868b8534bSLuigi Rizzo  *	the dmamap for a packet buffer
133968b8534bSLuigi Rizzo  *
13404bf50f18SLuigi Rizzo  * netmap_reset() is a helper routine to be called in the hw driver
13414bf50f18SLuigi Rizzo  *	when reinitializing a ring. It should not be called by
13424bf50f18SLuigi Rizzo  *	virtual ports (vale, pipes, monitor)
134368b8534bSLuigi Rizzo  */
1344f9790aebSLuigi Rizzo int netmap_attach(struct netmap_adapter *);
13454f80b14cSVincenzo Maffione int netmap_attach_ext(struct netmap_adapter *, size_t size, int override_reg);
1346e330262fSJustin Hibbits void netmap_detach(if_t);
1347e330262fSJustin Hibbits int netmap_transmit(if_t, struct mbuf *);
134868b8534bSLuigi Rizzo struct netmap_slot *netmap_reset(struct netmap_adapter *na,
1349ce3ee1e7SLuigi Rizzo 	enum txrx tx, u_int n, u_int new_cur);
135068b8534bSLuigi Rizzo int netmap_ring_reinit(struct netmap_kring *);
13512ff91c17SVincenzo Maffione int netmap_rings_config_get(struct netmap_adapter *, struct nm_config_info *);
135268b8534bSLuigi Rizzo 
135337e3a6d3SLuigi Rizzo /* Return codes for netmap_*x_irq. */
135437e3a6d3SLuigi Rizzo enum {
135537e3a6d3SLuigi Rizzo 	/* Driver should do normal interrupt processing, e.g. because
135637e3a6d3SLuigi Rizzo 	 * the interface is not in netmap mode. */
135737e3a6d3SLuigi Rizzo 	NM_IRQ_PASS = 0,
135837e3a6d3SLuigi Rizzo 	/* Port is in netmap mode, and the interrupt work has been
135937e3a6d3SLuigi Rizzo 	 * completed. The driver does not have to notify netmap
136037e3a6d3SLuigi Rizzo 	 * again before the next interrupt. */
136137e3a6d3SLuigi Rizzo 	NM_IRQ_COMPLETED = -1,
136237e3a6d3SLuigi Rizzo 	/* Port is in netmap mode, but the interrupt work has not been
136337e3a6d3SLuigi Rizzo 	 * completed. The driver has to make sure netmap will be
136437e3a6d3SLuigi Rizzo 	 * notified again soon, even if no more interrupts come (e.g.
136537e3a6d3SLuigi Rizzo 	 * on Linux the driver should not call napi_complete()). */
136637e3a6d3SLuigi Rizzo 	NM_IRQ_RESCHED = -2,
136737e3a6d3SLuigi Rizzo };
136837e3a6d3SLuigi Rizzo 
136917885a7bSLuigi Rizzo /* default functions to handle rx/tx interrupts */
1370e330262fSJustin Hibbits int netmap_rx_irq(if_t, u_int, u_int *);
137117885a7bSLuigi Rizzo #define netmap_tx_irq(_n, _q) netmap_rx_irq(_n, _q, NULL)
137237e3a6d3SLuigi Rizzo int netmap_common_irq(struct netmap_adapter *, u_int, u_int *work_done);
137317885a7bSLuigi Rizzo 
137417885a7bSLuigi Rizzo 
13754bf50f18SLuigi Rizzo #ifdef WITH_VALE
13764bf50f18SLuigi Rizzo /* functions used by external modules to interface with VALE */
13774bf50f18SLuigi Rizzo #define netmap_vp_to_ifp(_vp)	((_vp)->up.ifp)
13784bf50f18SLuigi Rizzo #define netmap_ifp_to_vp(_ifp)	(NA(_ifp)->na_vp)
13794bf50f18SLuigi Rizzo #define netmap_ifp_to_host_vp(_ifp) (NA(_ifp)->na_hostvp)
13804bf50f18SLuigi Rizzo #define netmap_bdg_idx(_vp)	((_vp)->bdg_port)
13814bf50f18SLuigi Rizzo const char *netmap_bdg_name(struct netmap_vp_adapter *);
13824bf50f18SLuigi Rizzo #else /* !WITH_VALE */
13834bf50f18SLuigi Rizzo #define netmap_vp_to_ifp(_vp)	NULL
13844bf50f18SLuigi Rizzo #define netmap_ifp_to_vp(_ifp)	NULL
13854bf50f18SLuigi Rizzo #define netmap_ifp_to_host_vp(_ifp) NULL
13864bf50f18SLuigi Rizzo #define netmap_bdg_idx(_vp)	-1
13874bf50f18SLuigi Rizzo #endif /* WITH_VALE */
13884bf50f18SLuigi Rizzo 
13894bf50f18SLuigi Rizzo static inline int
13904bf50f18SLuigi Rizzo nm_netmap_on(struct netmap_adapter *na)
13914bf50f18SLuigi Rizzo {
13924bf50f18SLuigi Rizzo 	return na && na->na_flags & NAF_NETMAP_ON;
13934bf50f18SLuigi Rizzo }
139417885a7bSLuigi Rizzo 
1395847bf383SLuigi Rizzo static inline int
1396847bf383SLuigi Rizzo nm_native_on(struct netmap_adapter *na)
1397847bf383SLuigi Rizzo {
1398847bf383SLuigi Rizzo 	return nm_netmap_on(na) && (na->na_flags & NAF_NATIVE);
1399847bf383SLuigi Rizzo }
1400847bf383SLuigi Rizzo 
140166823237SVincenzo Maffione static inline struct netmap_kring *
140266823237SVincenzo Maffione netmap_kring_on(struct netmap_adapter *na, u_int q, enum txrx t)
140366823237SVincenzo Maffione {
140466823237SVincenzo Maffione 	struct netmap_kring *kring = NULL;
140566823237SVincenzo Maffione 
140666823237SVincenzo Maffione 	if (!nm_native_on(na))
140766823237SVincenzo Maffione 		return NULL;
140866823237SVincenzo Maffione 
140966823237SVincenzo Maffione 	if (t == NR_RX && q < na->num_rx_rings)
141066823237SVincenzo Maffione 		kring = na->rx_rings[q];
141166823237SVincenzo Maffione 	else if (t == NR_TX && q < na->num_tx_rings)
141266823237SVincenzo Maffione 		kring = na->tx_rings[q];
141366823237SVincenzo Maffione 	else
141466823237SVincenzo Maffione 		return NULL;
141566823237SVincenzo Maffione 
141666823237SVincenzo Maffione 	return (kring->nr_mode == NKR_NETMAP_ON) ? kring : NULL;
141766823237SVincenzo Maffione }
141866823237SVincenzo Maffione 
141937e3a6d3SLuigi Rizzo static inline int
142037e3a6d3SLuigi Rizzo nm_iszombie(struct netmap_adapter *na)
142137e3a6d3SLuigi Rizzo {
142237e3a6d3SLuigi Rizzo 	return na == NULL || (na->na_flags & NAF_ZOMBIE);
142337e3a6d3SLuigi Rizzo }
142437e3a6d3SLuigi Rizzo 
14252a7db7a6SVincenzo Maffione void nm_set_native_flags(struct netmap_adapter *);
14262a7db7a6SVincenzo Maffione void nm_clear_native_flags(struct netmap_adapter *);
14272ff91c17SVincenzo Maffione 
142875f4f3edSVincenzo Maffione void netmap_krings_mode_commit(struct netmap_adapter *na, int onoff);
142975f4f3edSVincenzo Maffione 
143037e3a6d3SLuigi Rizzo /*
143137e3a6d3SLuigi Rizzo  * nm_*sync_prologue() functions are used in ioctl/poll and ptnetmap
143237e3a6d3SLuigi Rizzo  * kthreads.
143337e3a6d3SLuigi Rizzo  * We need netmap_ring* parameter, because in ptnetmap it is decoupled
143437e3a6d3SLuigi Rizzo  * from host kring.
143537e3a6d3SLuigi Rizzo  * The user-space ring pointers (head/cur/tail) are shared through
143637e3a6d3SLuigi Rizzo  * CSB between host and guest.
143737e3a6d3SLuigi Rizzo  */
143837e3a6d3SLuigi Rizzo 
143937e3a6d3SLuigi Rizzo /*
144037e3a6d3SLuigi Rizzo  * validates parameters in the ring/kring, returns a value for head
144137e3a6d3SLuigi Rizzo  * If any error, returns ring_size to force a reinit.
144237e3a6d3SLuigi Rizzo  */
144337e3a6d3SLuigi Rizzo uint32_t nm_txsync_prologue(struct netmap_kring *, struct netmap_ring *);
144437e3a6d3SLuigi Rizzo 
144537e3a6d3SLuigi Rizzo 
144637e3a6d3SLuigi Rizzo /*
144737e3a6d3SLuigi Rizzo  * validates parameters in the ring/kring, returns a value for head
144837e3a6d3SLuigi Rizzo  * If any error, returns ring_size lim to force a reinit.
144937e3a6d3SLuigi Rizzo  */
145037e3a6d3SLuigi Rizzo uint32_t nm_rxsync_prologue(struct netmap_kring *, struct netmap_ring *);
145137e3a6d3SLuigi Rizzo 
1452f9790aebSLuigi Rizzo 
1453f9790aebSLuigi Rizzo /* check/fix address and len in tx rings */
1454f9790aebSLuigi Rizzo #if 1 /* debug version */
14554bf50f18SLuigi Rizzo #define	NM_CHECK_ADDR_LEN(_na, _a, _l)	do {				\
14564bf50f18SLuigi Rizzo 	if (_a == NETMAP_BUF_BASE(_na) || _l > NETMAP_BUF_SIZE(_na)) {	\
145775f4f3edSVincenzo Maffione 		nm_prlim(5, "bad addr/len ring %d slot %d idx %d len %d",	\
14584bf50f18SLuigi Rizzo 			kring->ring_id, nm_i, slot->buf_idx, len);	\
14594bf50f18SLuigi Rizzo 		if (_l > NETMAP_BUF_SIZE(_na))				\
14604bf50f18SLuigi Rizzo 			_l = NETMAP_BUF_SIZE(_na);			\
1461f9790aebSLuigi Rizzo 	} } while (0)
1462f9790aebSLuigi Rizzo #else /* no debug version */
14634bf50f18SLuigi Rizzo #define	NM_CHECK_ADDR_LEN(_na, _a, _l)	do {				\
14644bf50f18SLuigi Rizzo 		if (_l > NETMAP_BUF_SIZE(_na))				\
14654bf50f18SLuigi Rizzo 			_l = NETMAP_BUF_SIZE(_na);			\
1466f9790aebSLuigi Rizzo 	} while (0)
1467f9790aebSLuigi Rizzo #endif
1468f9790aebSLuigi Rizzo 
1469a6d768d8SVincenzo Maffione #define NM_CHECK_ADDR_LEN_OFF(na_, l_, o_) do {				\
1470a6d768d8SVincenzo Maffione 	if ((l_) + (o_) < (l_) || 					\
1471a6d768d8SVincenzo Maffione 	    (l_) + (o_) > NETMAP_BUF_SIZE(na_)) {			\
1472a6d768d8SVincenzo Maffione 		(l_) = NETMAP_BUF_SIZE(na_) - (o_);			\
1473a6d768d8SVincenzo Maffione 	} } while (0)
1474a6d768d8SVincenzo Maffione 
1475f9790aebSLuigi Rizzo 
1476f9790aebSLuigi Rizzo /*---------------------------------------------------------------*/
1477f9790aebSLuigi Rizzo /*
14784bf50f18SLuigi Rizzo  * Support routines used by netmap subsystems
14794bf50f18SLuigi Rizzo  * (native drivers, VALE, generic, pipes, monitors, ...)
14804bf50f18SLuigi Rizzo  */
14814bf50f18SLuigi Rizzo 
14824bf50f18SLuigi Rizzo 
14834bf50f18SLuigi Rizzo /* common routine for all functions that create a netmap adapter. It performs
14844bf50f18SLuigi Rizzo  * two main tasks:
14854bf50f18SLuigi Rizzo  * - if the na points to an ifp, mark the ifp as netmap capable
14864bf50f18SLuigi Rizzo  *   using na as its native adapter;
14874bf50f18SLuigi Rizzo  * - provide defaults for the setup callbacks and the memory allocator
14884bf50f18SLuigi Rizzo  */
14894bf50f18SLuigi Rizzo int netmap_attach_common(struct netmap_adapter *);
14904bf50f18SLuigi Rizzo /* fill priv->np_[tr]xq{first,last} using the ringid and flags information
14912ff91c17SVincenzo Maffione  * coming from a struct nmreq_register
14924bf50f18SLuigi Rizzo  */
1493ee0005f1SVincenzo Maffione int netmap_interp_ringid(struct netmap_priv_d *priv, struct nmreq_header *hdr);
14944bf50f18SLuigi Rizzo /* update the ring parameters (number and size of tx and rx rings).
14954bf50f18SLuigi Rizzo  * It calls the nm_config callback, if available.
1496f9790aebSLuigi Rizzo  */
1497f9790aebSLuigi Rizzo int netmap_update_config(struct netmap_adapter *na);
14984bf50f18SLuigi Rizzo /* create and initialize the common fields of the krings array.
14994bf50f18SLuigi Rizzo  * using the information that must be already available in the na.
15004bf50f18SLuigi Rizzo  * tailroom can be used to request the allocation of additional
15014bf50f18SLuigi Rizzo  * tailroom bytes after the krings array. This is used by
15024bf50f18SLuigi Rizzo  * netmap_vp_adapter's (i.e., VALE ports) to make room for
15034bf50f18SLuigi Rizzo  * leasing-related data structures
15044bf50f18SLuigi Rizzo  */
1505f0ea3689SLuigi Rizzo int netmap_krings_create(struct netmap_adapter *na, u_int tailroom);
15064bf50f18SLuigi Rizzo /* deletes the kring array of the adapter. The array must have
15074bf50f18SLuigi Rizzo  * been created using netmap_krings_create
15084bf50f18SLuigi Rizzo  */
1509f9790aebSLuigi Rizzo void netmap_krings_delete(struct netmap_adapter *na);
151017885a7bSLuigi Rizzo 
151137e3a6d3SLuigi Rizzo int netmap_hw_krings_create(struct netmap_adapter *na);
151237e3a6d3SLuigi Rizzo void netmap_hw_krings_delete(struct netmap_adapter *na);
151337e3a6d3SLuigi Rizzo 
15144bf50f18SLuigi Rizzo /* set the stopped/enabled status of ring
15154bf50f18SLuigi Rizzo  * When stopping, they also wait for all current activity on the ring to
15164bf50f18SLuigi Rizzo  * terminate. The status change is then notified using the na nm_notify
15174bf50f18SLuigi Rizzo  * callback.
15184bf50f18SLuigi Rizzo  */
1519847bf383SLuigi Rizzo void netmap_set_ring(struct netmap_adapter *, u_int ring_id, enum txrx, int stopped);
15204bf50f18SLuigi Rizzo /* set the stopped/enabled status of all rings of the adapter. */
15214bf50f18SLuigi Rizzo void netmap_set_all_rings(struct netmap_adapter *, int stopped);
152237e3a6d3SLuigi Rizzo /* convenience wrappers for netmap_set_all_rings */
1523e330262fSJustin Hibbits void netmap_disable_all_rings(if_t);
1524e330262fSJustin Hibbits void netmap_enable_all_rings(if_t);
15254bf50f18SLuigi Rizzo 
152677a2baf5SVincenzo Maffione int netmap_buf_size_validate(const struct netmap_adapter *na, unsigned mtu);
15278fd44c93SLuigi Rizzo int netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na,
1528ee0005f1SVincenzo Maffione 		struct nmreq_header *);
152937e3a6d3SLuigi Rizzo void netmap_do_unregif(struct netmap_priv_d *priv);
1530f9790aebSLuigi Rizzo 
1531f9790aebSLuigi Rizzo u_int nm_bound_var(u_int *v, u_int dflt, u_int lo, u_int hi, const char *msg);
15322ff91c17SVincenzo Maffione int netmap_get_na(struct nmreq_header *hdr, struct netmap_adapter **na,
1533e330262fSJustin Hibbits 		if_t *ifp, struct netmap_mem_d *nmd, int create);
1534e330262fSJustin Hibbits void netmap_unget_na(struct netmap_adapter *na, if_t ifp);
1535e330262fSJustin Hibbits int netmap_get_hw_na(if_t ifp,
1536c3e9b4dbSLuiz Otavio O Souza 		struct netmap_mem_d *nmd, struct netmap_adapter **na);
1537a6d768d8SVincenzo Maffione void netmap_mem_restore(struct netmap_adapter *na);
1538f9790aebSLuigi Rizzo 
15392a7db7a6SVincenzo Maffione #ifdef WITH_VALE
1540b6e66be2SVincenzo Maffione uint32_t netmap_vale_learning(struct nm_bdg_fwd *ft, uint8_t *dst_ring,
15412a7db7a6SVincenzo Maffione 		struct netmap_vp_adapter *, void *private_data);
15422a7db7a6SVincenzo Maffione 
15432a7db7a6SVincenzo Maffione /* these are redefined in case of no VALE support */
15442a7db7a6SVincenzo Maffione int netmap_get_vale_na(struct nmreq_header *hdr, struct netmap_adapter **na,
15452a7db7a6SVincenzo Maffione 		struct netmap_mem_d *nmd, int create);
15462a7db7a6SVincenzo Maffione void *netmap_vale_create(const char *bdg_name, int *return_status);
15472a7db7a6SVincenzo Maffione int netmap_vale_destroy(const char *bdg_name, void *auth_token);
1548f9790aebSLuigi Rizzo 
1549dd6ab49aSVincenzo Maffione extern unsigned int vale_max_bridges;
1550dd6ab49aSVincenzo Maffione 
1551f9790aebSLuigi Rizzo #else /* !WITH_VALE */
15522a7db7a6SVincenzo Maffione #define netmap_bdg_learning(_1, _2, _3, _4)	0
15532a7db7a6SVincenzo Maffione #define	netmap_get_vale_na(_1, _2, _3, _4)	0
15542a7db7a6SVincenzo Maffione #define netmap_bdg_create(_1, _2)	NULL
15552a7db7a6SVincenzo Maffione #define netmap_bdg_destroy(_1, _2)	0
15564ad57c7aSVincenzo Maffione #define vale_max_bridges		1
1557f9790aebSLuigi Rizzo #endif /* !WITH_VALE */
1558f9790aebSLuigi Rizzo 
1559f0ea3689SLuigi Rizzo #ifdef WITH_PIPES
1560f0ea3689SLuigi Rizzo /* max number of pipes per device */
15614f80b14cSVincenzo Maffione #define NM_MAXPIPES	64	/* XXX this should probably be a sysctl */
1562f0ea3689SLuigi Rizzo void netmap_pipe_dealloc(struct netmap_adapter *);
15632ff91c17SVincenzo Maffione int netmap_get_pipe_na(struct nmreq_header *hdr, struct netmap_adapter **na,
1564c3e9b4dbSLuiz Otavio O Souza 			struct netmap_mem_d *nmd, int create);
1565f0ea3689SLuigi Rizzo #else /* !WITH_PIPES */
1566f0ea3689SLuigi Rizzo #define NM_MAXPIPES	0
1567847bf383SLuigi Rizzo #define netmap_pipe_alloc(_1, _2) 	0
1568f0ea3689SLuigi Rizzo #define netmap_pipe_dealloc(_1)
15692ff91c17SVincenzo Maffione #define netmap_get_pipe_na(hdr, _2, _3, _4)	\
15702ff91c17SVincenzo Maffione 	((strchr(hdr->nr_name, '{') != NULL || strchr(hdr->nr_name, '}') != NULL) ? EOPNOTSUPP : 0)
1571f0ea3689SLuigi Rizzo #endif
1572f0ea3689SLuigi Rizzo 
15734bf50f18SLuigi Rizzo #ifdef WITH_MONITOR
15742ff91c17SVincenzo Maffione int netmap_get_monitor_na(struct nmreq_header *hdr, struct netmap_adapter **na,
1575c3e9b4dbSLuiz Otavio O Souza 		struct netmap_mem_d *nmd, int create);
1576847bf383SLuigi Rizzo void netmap_monitor_stop(struct netmap_adapter *na);
15774bf50f18SLuigi Rizzo #else
15782ff91c17SVincenzo Maffione #define netmap_get_monitor_na(hdr, _2, _3, _4) \
1579cfa866f6SMatt Macy 	(((struct nmreq_register *)(uintptr_t)hdr->nr_body)->nr_flags & (NR_MONITOR_TX | NR_MONITOR_RX) ? EOPNOTSUPP : 0)
1580847bf383SLuigi Rizzo #endif
1581847bf383SLuigi Rizzo 
1582b6e66be2SVincenzo Maffione #ifdef WITH_NMNULL
1583b6e66be2SVincenzo Maffione int netmap_get_null_na(struct nmreq_header *hdr, struct netmap_adapter **na,
1584b6e66be2SVincenzo Maffione 		struct netmap_mem_d *nmd, int create);
1585b6e66be2SVincenzo Maffione #else /* !WITH_NMNULL */
1586b6e66be2SVincenzo Maffione #define netmap_get_null_na(hdr, _2, _3, _4) \
1587b6e66be2SVincenzo Maffione 	(((struct nmreq_register *)(uintptr_t)hdr->nr_body)->nr_flags & (NR_MONITOR_TX | NR_MONITOR_RX) ? EOPNOTSUPP : 0)
1588b6e66be2SVincenzo Maffione #endif /* WITH_NMNULL */
1589b6e66be2SVincenzo Maffione 
1590847bf383SLuigi Rizzo #ifdef CONFIG_NET_NS
1591847bf383SLuigi Rizzo struct net *netmap_bns_get(void);
1592847bf383SLuigi Rizzo void netmap_bns_put(struct net *);
1593847bf383SLuigi Rizzo void netmap_bns_getbridges(struct nm_bridge **, u_int *);
1594847bf383SLuigi Rizzo #else
1595b6e66be2SVincenzo Maffione extern struct nm_bridge *nm_bridges;
1596847bf383SLuigi Rizzo #define netmap_bns_get()
1597847bf383SLuigi Rizzo #define netmap_bns_put(_1)
1598847bf383SLuigi Rizzo #define netmap_bns_getbridges(b, n) \
1599dd6ab49aSVincenzo Maffione 	do { *b = nm_bridges; *n = vale_max_bridges; } while (0)
16004bf50f18SLuigi Rizzo #endif
16014bf50f18SLuigi Rizzo 
1602f9790aebSLuigi Rizzo /* Various prototypes */
160337e3a6d3SLuigi Rizzo int netmap_poll(struct netmap_priv_d *, int events, NM_SELRECORD_T *td);
1604f9790aebSLuigi Rizzo int netmap_init(void);
1605f9790aebSLuigi Rizzo void netmap_fini(void);
1606f9790aebSLuigi Rizzo int netmap_get_memory(struct netmap_priv_d* p);
1607f9790aebSLuigi Rizzo void netmap_dtor(void *data);
1608f9790aebSLuigi Rizzo 
16092ff91c17SVincenzo Maffione int netmap_ioctl(struct netmap_priv_d *priv, u_long cmd, caddr_t data,
16102ff91c17SVincenzo Maffione 		struct thread *, int nr_body_is_user);
16112ff91c17SVincenzo Maffione int netmap_ioctl_legacy(struct netmap_priv_d *priv, u_long cmd, caddr_t data,
16122ff91c17SVincenzo Maffione 			struct thread *td);
16132ff91c17SVincenzo Maffione size_t nmreq_size_by_type(uint16_t nr_reqtype);
1614f9790aebSLuigi Rizzo 
1615f9790aebSLuigi Rizzo /* netmap_adapter creation/destruction */
161617885a7bSLuigi Rizzo 
161717885a7bSLuigi Rizzo // #define NM_DEBUG_PUTGET 1
1618f9790aebSLuigi Rizzo 
1619f9790aebSLuigi Rizzo #ifdef NM_DEBUG_PUTGET
1620f9790aebSLuigi Rizzo 
1621f9790aebSLuigi Rizzo #define NM_DBG(f) __##f
1622f9790aebSLuigi Rizzo 
1623f9790aebSLuigi Rizzo void __netmap_adapter_get(struct netmap_adapter *na);
1624f9790aebSLuigi Rizzo 
1625f9790aebSLuigi Rizzo #define netmap_adapter_get(na) 				\
1626f9790aebSLuigi Rizzo 	do {						\
1627f9790aebSLuigi Rizzo 		struct netmap_adapter *__na = na;	\
1628f9790aebSLuigi Rizzo 		__netmap_adapter_get(__na);		\
162992e8b4a6SVincenzo Maffione 		nm_prinf("getting %p:%s -> %d", __na, (__na)->name, (__na)->na_refcount);	\
1630f9790aebSLuigi Rizzo 	} while (0)
1631f9790aebSLuigi Rizzo 
1632f9790aebSLuigi Rizzo int __netmap_adapter_put(struct netmap_adapter *na);
1633f9790aebSLuigi Rizzo 
1634f9790aebSLuigi Rizzo #define netmap_adapter_put(na)				\
1635fb25194fSLuigi Rizzo 	({						\
1636f9790aebSLuigi Rizzo 		struct netmap_adapter *__na = na;	\
163792e8b4a6SVincenzo Maffione 		if (__na == NULL)			\
163892e8b4a6SVincenzo Maffione 			nm_prinf("putting NULL");	\
163992e8b4a6SVincenzo Maffione 		else					\
164092e8b4a6SVincenzo Maffione 			nm_prinf("putting %p:%s -> %d", __na, (__na)->name, (__na)->na_refcount - 1);	\
1641f9790aebSLuigi Rizzo 		__netmap_adapter_put(__na);	\
1642fb25194fSLuigi Rizzo 	})
1643f9790aebSLuigi Rizzo 
1644f9790aebSLuigi Rizzo #else /* !NM_DEBUG_PUTGET */
1645f9790aebSLuigi Rizzo 
1646f9790aebSLuigi Rizzo #define NM_DBG(f) f
1647f9790aebSLuigi Rizzo void netmap_adapter_get(struct netmap_adapter *na);
1648f9790aebSLuigi Rizzo int netmap_adapter_put(struct netmap_adapter *na);
1649f9790aebSLuigi Rizzo 
1650f9790aebSLuigi Rizzo #endif /* !NM_DEBUG_PUTGET */
1651f9790aebSLuigi Rizzo 
1652f9790aebSLuigi Rizzo 
165317885a7bSLuigi Rizzo /*
165417885a7bSLuigi Rizzo  * module variables
165517885a7bSLuigi Rizzo  */
165637e3a6d3SLuigi Rizzo #define NETMAP_BUF_BASE(_na)	((_na)->na_lut.lut[0].vaddr)
165737e3a6d3SLuigi Rizzo #define NETMAP_BUF_SIZE(_na)	((_na)->na_lut.objsize)
16585819da83SLuigi Rizzo extern int netmap_no_pendintr;
1659b6e66be2SVincenzo Maffione extern int netmap_verbose;
1660b6e66be2SVincenzo Maffione #ifdef CONFIG_NETMAP_DEBUG
1661b6e66be2SVincenzo Maffione extern int netmap_debug;		/* for debugging */
1662b6e66be2SVincenzo Maffione #else /* !CONFIG_NETMAP_DEBUG */
1663b6e66be2SVincenzo Maffione #define netmap_debug (0)
1664b6e66be2SVincenzo Maffione #endif /* !CONFIG_NETMAP_DEBUG */
1665b6e66be2SVincenzo Maffione enum {                                  /* debug flags */
166645c67e8fSVincenzo Maffione 	NM_DEBUG_ON = 1,		/* generic debug messages */
1667b6e66be2SVincenzo Maffione 	NM_DEBUG_HOST = 0x2,            /* debug host stack */
1668b6e66be2SVincenzo Maffione 	NM_DEBUG_RXSYNC = 0x10,         /* debug on rxsync/txsync */
1669b6e66be2SVincenzo Maffione 	NM_DEBUG_TXSYNC = 0x20,
1670b6e66be2SVincenzo Maffione 	NM_DEBUG_RXINTR = 0x100,        /* debug on rx/tx intr (driver) */
1671b6e66be2SVincenzo Maffione 	NM_DEBUG_TXINTR = 0x200,
1672b6e66be2SVincenzo Maffione 	NM_DEBUG_NIC_RXSYNC = 0x1000,   /* debug on rx/tx intr (driver) */
1673b6e66be2SVincenzo Maffione 	NM_DEBUG_NIC_TXSYNC = 0x2000,
1674b6e66be2SVincenzo Maffione 	NM_DEBUG_MEM = 0x4000,		/* verbose memory allocations/deallocations */
1675b6e66be2SVincenzo Maffione 	NM_DEBUG_VALE = 0x8000,		/* debug messages from memory allocators */
1676b6e66be2SVincenzo Maffione 	NM_DEBUG_BDG = NM_DEBUG_VALE,
167768b8534bSLuigi Rizzo };
167868b8534bSLuigi Rizzo 
1679f9790aebSLuigi Rizzo extern int netmap_txsync_retry;
16802a7db7a6SVincenzo Maffione extern int netmap_generic_hwcsum;
1681f9790aebSLuigi Rizzo extern int netmap_generic_mit;
1682f9790aebSLuigi Rizzo extern int netmap_generic_ringsize;
1683f0ea3689SLuigi Rizzo extern int netmap_generic_rings;
16844f80b14cSVincenzo Maffione #ifdef linux
168537e3a6d3SLuigi Rizzo extern int netmap_generic_txqdisc;
16864f80b14cSVincenzo Maffione #endif
1687f9790aebSLuigi Rizzo 
168868b8534bSLuigi Rizzo /*
16892a7db7a6SVincenzo Maffione  * NA returns a pointer to the struct netmap adapter from the ifp.
169022bf2a47SVincenzo Maffione  * The if_getnetmapadapter() and if_setnetmapadapter() helpers are
169122bf2a47SVincenzo Maffione  * os-specific and must be defined in glue code.
169268b8534bSLuigi Rizzo  */
169322bf2a47SVincenzo Maffione #define	NA(_ifp)	(if_getnetmapadapter(_ifp))
169468b8534bSLuigi Rizzo 
16958241616dSLuigi Rizzo /*
16962a7db7a6SVincenzo Maffione  * we provide a default implementation of NM_ATTACH_NA/NM_DETACH_NA
169722bf2a47SVincenzo Maffione  * based on the if_setnetmapadapter() setter function.
16982a7db7a6SVincenzo Maffione  * Glue code may override this by defining its own NM_ATTACH_NA
16992a7db7a6SVincenzo Maffione  */
17002a7db7a6SVincenzo Maffione #ifndef NM_ATTACH_NA
17012a7db7a6SVincenzo Maffione /*
170237e3a6d3SLuigi Rizzo  * On old versions of FreeBSD, NA(ifp) is a pspare. On linux we
170337e3a6d3SLuigi Rizzo  * overload another pointer in the netdev.
170437e3a6d3SLuigi Rizzo  *
170537e3a6d3SLuigi Rizzo  * We check if NA(ifp) is set and its first element has a related
17068241616dSLuigi Rizzo  * magic value. The capenable is within the struct netmap_adapter.
17078241616dSLuigi Rizzo  */
17088241616dSLuigi Rizzo #define	NETMAP_MAGIC	0x52697a7a
17098241616dSLuigi Rizzo 
171037e3a6d3SLuigi Rizzo #define NM_NA_VALID(ifp)	(NA(ifp) &&		\
17118241616dSLuigi Rizzo 	((uint32_t)(uintptr_t)NA(ifp) ^ NA(ifp)->magic) == NETMAP_MAGIC )
17128241616dSLuigi Rizzo 
171337e3a6d3SLuigi Rizzo #define	NM_ATTACH_NA(ifp, na) do {					\
1714e330262fSJustin Hibbits 	if_setnetmapadapter(ifp, na);					\
171537e3a6d3SLuigi Rizzo 	if (NA(ifp))							\
171637e3a6d3SLuigi Rizzo 		NA(ifp)->magic = 					\
171737e3a6d3SLuigi Rizzo 			((uint32_t)(uintptr_t)NA(ifp)) ^ NETMAP_MAGIC;	\
171837e3a6d3SLuigi Rizzo } while(0)
1719e330262fSJustin Hibbits #define NM_RESTORE_NA(ifp, na) 	if_setnetmapadapter(ifp, na);
17202a7db7a6SVincenzo Maffione 
1721e330262fSJustin Hibbits #define NM_DETACH_NA(ifp)	do { if_setnetmapadapter(ifp, NULL); } while (0)
17222a7db7a6SVincenzo Maffione #define NM_NA_CLASH(ifp)	(NA(ifp) && !NM_NA_VALID(ifp))
17232a7db7a6SVincenzo Maffione #endif /* !NM_ATTACH_NA */
17242a7db7a6SVincenzo Maffione 
17258241616dSLuigi Rizzo 
172637e3a6d3SLuigi Rizzo #define NM_IS_NATIVE(ifp)	(NM_NA_VALID(ifp) && NA(ifp)->nm_dtor == netmap_hw_dtor)
172768b8534bSLuigi Rizzo 
172837e3a6d3SLuigi Rizzo #if defined(__FreeBSD__)
1729f9790aebSLuigi Rizzo 
17304bf50f18SLuigi Rizzo /* Assigns the device IOMMU domain to an allocator.
17314bf50f18SLuigi Rizzo  * Returns -ENOMEM in case the domain is different */
1732a6d768d8SVincenzo Maffione #define nm_iommu_group_id(dev) (-1)
17334bf50f18SLuigi Rizzo 
173417885a7bSLuigi Rizzo /* Callback invoked by the dma machinery after a successful dmamap_load */
17356dba29a2SLuigi Rizzo static void netmap_dmamap_cb(__unused void *arg,
17366dba29a2SLuigi Rizzo     __unused bus_dma_segment_t * segs, __unused int nseg, __unused int error)
17376dba29a2SLuigi Rizzo {
17386dba29a2SLuigi Rizzo }
17396dba29a2SLuigi Rizzo 
17406dba29a2SLuigi Rizzo /* bus_dmamap_load wrapper: call aforementioned function if map != NULL.
17416dba29a2SLuigi Rizzo  * XXX can we do it without a callback ?
17426dba29a2SLuigi Rizzo  */
17434f80b14cSVincenzo Maffione static inline int
17444bf50f18SLuigi Rizzo netmap_load_map(struct netmap_adapter *na,
17454bf50f18SLuigi Rizzo 	bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
17466dba29a2SLuigi Rizzo {
17476dba29a2SLuigi Rizzo 	if (map)
17484bf50f18SLuigi Rizzo 		bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE(na),
17496dba29a2SLuigi Rizzo 		    netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
17504f80b14cSVincenzo Maffione 	return 0;
17516dba29a2SLuigi Rizzo }
17526dba29a2SLuigi Rizzo 
17534bf50f18SLuigi Rizzo static inline void
17544bf50f18SLuigi Rizzo netmap_unload_map(struct netmap_adapter *na,
17554bf50f18SLuigi Rizzo         bus_dma_tag_t tag, bus_dmamap_t map)
17564bf50f18SLuigi Rizzo {
17574bf50f18SLuigi Rizzo 	if (map)
17584bf50f18SLuigi Rizzo 		bus_dmamap_unload(tag, map);
17594bf50f18SLuigi Rizzo }
17604bf50f18SLuigi Rizzo 
17614f80b14cSVincenzo Maffione #define netmap_sync_map(na, tag, map, sz, t)
17624f80b14cSVincenzo Maffione 
17636dba29a2SLuigi Rizzo /* update the map when a buffer changes. */
17646dba29a2SLuigi Rizzo static inline void
17654bf50f18SLuigi Rizzo netmap_reload_map(struct netmap_adapter *na,
17664bf50f18SLuigi Rizzo 	bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
17676dba29a2SLuigi Rizzo {
17686dba29a2SLuigi Rizzo 	if (map) {
17696dba29a2SLuigi Rizzo 		bus_dmamap_unload(tag, map);
17704bf50f18SLuigi Rizzo 		bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE(na),
17716dba29a2SLuigi Rizzo 		    netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
17726dba29a2SLuigi Rizzo 	}
17736dba29a2SLuigi Rizzo }
1774f9790aebSLuigi Rizzo 
177537e3a6d3SLuigi Rizzo #elif defined(_WIN32)
177637e3a6d3SLuigi Rizzo 
1777f196ce38SLuigi Rizzo #else /* linux */
1778f196ce38SLuigi Rizzo 
17794bf50f18SLuigi Rizzo int nm_iommu_group_id(bus_dma_tag_t dev);
17804bf50f18SLuigi Rizzo #include <linux/dma-mapping.h>
17814bf50f18SLuigi Rizzo 
1782f196ce38SLuigi Rizzo /*
1783f196ce38SLuigi Rizzo  * on linux we need
1784f196ce38SLuigi Rizzo  *	dma_map_single(&pdev->dev, virt_addr, len, direction)
17854f80b14cSVincenzo Maffione  *	dma_unmap_single(&adapter->pdev->dev, phys_addr, len, direction)
1786f196ce38SLuigi Rizzo  */
1787f196ce38SLuigi Rizzo #if 0
1788f196ce38SLuigi Rizzo 	struct e1000_buffer *buffer_info =  &tx_ring->buffer_info[l];
1789f196ce38SLuigi Rizzo 	/* set time_stamp *before* dma to help avoid a possible race */
1790f196ce38SLuigi Rizzo 	buffer_info->time_stamp = jiffies;
1791f196ce38SLuigi Rizzo 	buffer_info->mapped_as_page = false;
1792f196ce38SLuigi Rizzo 	buffer_info->length = len;
1793f196ce38SLuigi Rizzo 	//buffer_info->next_to_watch = l;
1794f196ce38SLuigi Rizzo 	/* reload dma map */
1795f196ce38SLuigi Rizzo 	dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
1796f196ce38SLuigi Rizzo 			NETMAP_BUF_SIZE, DMA_TO_DEVICE);
1797f196ce38SLuigi Rizzo 	buffer_info->dma = dma_map_single(&adapter->pdev->dev,
1798f196ce38SLuigi Rizzo 			addr, NETMAP_BUF_SIZE, DMA_TO_DEVICE);
1799f196ce38SLuigi Rizzo 
1800f196ce38SLuigi Rizzo 	if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
180175f4f3edSVincenzo Maffione 		nm_prerr("dma mapping error");
1802f196ce38SLuigi Rizzo 		/* goto dma_error; See e1000_put_txbuf() */
1803f196ce38SLuigi Rizzo 		/* XXX reset */
1804f196ce38SLuigi Rizzo 	}
1805f196ce38SLuigi Rizzo 	tx_desc->buffer_addr = htole64(buffer_info->dma); //XXX
1806f196ce38SLuigi Rizzo 
1807f196ce38SLuigi Rizzo #endif
1808f196ce38SLuigi Rizzo 
18094f80b14cSVincenzo Maffione static inline int
18104f80b14cSVincenzo Maffione netmap_load_map(struct netmap_adapter *na,
18114f80b14cSVincenzo Maffione 	bus_dma_tag_t tag, bus_dmamap_t map, void *buf, u_int size)
18124f80b14cSVincenzo Maffione {
18134f80b14cSVincenzo Maffione 	if (map) {
18144f80b14cSVincenzo Maffione 		*map = dma_map_single(na->pdev, buf, size,
18154f80b14cSVincenzo Maffione 				      DMA_BIDIRECTIONAL);
18164f80b14cSVincenzo Maffione 		if (dma_mapping_error(na->pdev, *map)) {
18174f80b14cSVincenzo Maffione 			*map = 0;
18184f80b14cSVincenzo Maffione 			return ENOMEM;
18194f80b14cSVincenzo Maffione 		}
18204f80b14cSVincenzo Maffione 	}
18214f80b14cSVincenzo Maffione 	return 0;
18224f80b14cSVincenzo Maffione }
18234f80b14cSVincenzo Maffione 
18244f80b14cSVincenzo Maffione static inline void
18254f80b14cSVincenzo Maffione netmap_unload_map(struct netmap_adapter *na,
18264f80b14cSVincenzo Maffione 	bus_dma_tag_t tag, bus_dmamap_t map, u_int sz)
18274f80b14cSVincenzo Maffione {
18284f80b14cSVincenzo Maffione 	if (*map) {
18294f80b14cSVincenzo Maffione 		dma_unmap_single(na->pdev, *map, sz,
18304f80b14cSVincenzo Maffione 				 DMA_BIDIRECTIONAL);
18314f80b14cSVincenzo Maffione 	}
18324f80b14cSVincenzo Maffione }
18334f80b14cSVincenzo Maffione 
18342a7db7a6SVincenzo Maffione #ifdef NETMAP_LINUX_HAVE_DMASYNC
18354f80b14cSVincenzo Maffione static inline void
18362a7db7a6SVincenzo Maffione netmap_sync_map_cpu(struct netmap_adapter *na,
18374f80b14cSVincenzo Maffione 	bus_dma_tag_t tag, bus_dmamap_t map, u_int sz, enum txrx t)
18384f80b14cSVincenzo Maffione {
18394f80b14cSVincenzo Maffione 	if (*map) {
18404f80b14cSVincenzo Maffione 		dma_sync_single_for_cpu(na->pdev, *map, sz,
18412a7db7a6SVincenzo Maffione 			(t == NR_TX ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
18422a7db7a6SVincenzo Maffione 	}
18432a7db7a6SVincenzo Maffione }
18442a7db7a6SVincenzo Maffione 
18452a7db7a6SVincenzo Maffione static inline void
18462a7db7a6SVincenzo Maffione netmap_sync_map_dev(struct netmap_adapter *na,
18472a7db7a6SVincenzo Maffione 	bus_dma_tag_t tag, bus_dmamap_t map, u_int sz, enum txrx t)
18482a7db7a6SVincenzo Maffione {
18492a7db7a6SVincenzo Maffione 	if (*map) {
18504f80b14cSVincenzo Maffione 		dma_sync_single_for_device(na->pdev, *map, sz,
18512a7db7a6SVincenzo Maffione 			(t == NR_TX ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
18524f80b14cSVincenzo Maffione 	}
18534f80b14cSVincenzo Maffione }
18544f80b14cSVincenzo Maffione 
18554f80b14cSVincenzo Maffione static inline void
18564f80b14cSVincenzo Maffione netmap_reload_map(struct netmap_adapter *na,
18574f80b14cSVincenzo Maffione 	bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
18584f80b14cSVincenzo Maffione {
18594f80b14cSVincenzo Maffione 	u_int sz = NETMAP_BUF_SIZE(na);
18604f80b14cSVincenzo Maffione 
18614f80b14cSVincenzo Maffione 	if (*map) {
18624f80b14cSVincenzo Maffione 		dma_unmap_single(na->pdev, *map, sz,
18634f80b14cSVincenzo Maffione 				DMA_BIDIRECTIONAL);
18644f80b14cSVincenzo Maffione 	}
18654f80b14cSVincenzo Maffione 
18664f80b14cSVincenzo Maffione 	*map = dma_map_single(na->pdev, buf, sz,
18674f80b14cSVincenzo Maffione 				DMA_BIDIRECTIONAL);
18684f80b14cSVincenzo Maffione }
18692a7db7a6SVincenzo Maffione #else /* !NETMAP_LINUX_HAVE_DMASYNC */
18702a7db7a6SVincenzo Maffione #define netmap_sync_map_cpu(na, tag, map, sz, t)
18712a7db7a6SVincenzo Maffione #define netmap_sync_map_dev(na, tag, map, sz, t)
18722a7db7a6SVincenzo Maffione #endif /* NETMAP_LINUX_HAVE_DMASYNC */
1873f196ce38SLuigi Rizzo 
1874f196ce38SLuigi Rizzo #endif /* linux */
18756dba29a2SLuigi Rizzo 
1876ce3ee1e7SLuigi Rizzo 
18775644ccecSLuigi Rizzo /*
18785644ccecSLuigi Rizzo  * functions to map NIC to KRING indexes (n2k) and vice versa (k2n)
18795644ccecSLuigi Rizzo  */
18805644ccecSLuigi Rizzo static inline int
188164ae02c3SLuigi Rizzo netmap_idx_n2k(struct netmap_kring *kr, int idx)
18825644ccecSLuigi Rizzo {
188364ae02c3SLuigi Rizzo 	int n = kr->nkr_num_slots;
1884b6e66be2SVincenzo Maffione 
1885b6e66be2SVincenzo Maffione 	if (likely(kr->nkr_hwofs == 0)) {
1886b6e66be2SVincenzo Maffione 		return idx;
1887b6e66be2SVincenzo Maffione 	}
1888b6e66be2SVincenzo Maffione 
188964ae02c3SLuigi Rizzo 	idx += kr->nkr_hwofs;
189064ae02c3SLuigi Rizzo 	if (idx < 0)
189164ae02c3SLuigi Rizzo 		return idx + n;
189264ae02c3SLuigi Rizzo 	else if (idx < n)
189364ae02c3SLuigi Rizzo 		return idx;
18945644ccecSLuigi Rizzo 	else
189564ae02c3SLuigi Rizzo 		return idx - n;
18965644ccecSLuigi Rizzo }
18975644ccecSLuigi Rizzo 
18985644ccecSLuigi Rizzo 
18995644ccecSLuigi Rizzo static inline int
190064ae02c3SLuigi Rizzo netmap_idx_k2n(struct netmap_kring *kr, int idx)
19015644ccecSLuigi Rizzo {
190264ae02c3SLuigi Rizzo 	int n = kr->nkr_num_slots;
1903b6e66be2SVincenzo Maffione 
1904b6e66be2SVincenzo Maffione 	if (likely(kr->nkr_hwofs == 0)) {
1905b6e66be2SVincenzo Maffione 		return idx;
1906b6e66be2SVincenzo Maffione 	}
1907b6e66be2SVincenzo Maffione 
190864ae02c3SLuigi Rizzo 	idx -= kr->nkr_hwofs;
190964ae02c3SLuigi Rizzo 	if (idx < 0)
191064ae02c3SLuigi Rizzo 		return idx + n;
191164ae02c3SLuigi Rizzo 	else if (idx < n)
191264ae02c3SLuigi Rizzo 		return idx;
19135644ccecSLuigi Rizzo 	else
191464ae02c3SLuigi Rizzo 		return idx - n;
19155644ccecSLuigi Rizzo }
19165644ccecSLuigi Rizzo 
19175644ccecSLuigi Rizzo 
1918d76bf4ffSLuigi Rizzo /* Entries of the look-up table. */
19194f80b14cSVincenzo Maffione #ifdef __FreeBSD__
1920d76bf4ffSLuigi Rizzo struct lut_entry {
1921d76bf4ffSLuigi Rizzo 	void *vaddr;		/* virtual address. */
1922849bec0eSLuigi Rizzo 	vm_paddr_t paddr;	/* physical address. */
1923d76bf4ffSLuigi Rizzo };
19244f80b14cSVincenzo Maffione #else /* linux & _WIN32 */
19254f80b14cSVincenzo Maffione /* dma-mapping in linux can assign a buffer a different address
19264f80b14cSVincenzo Maffione  * depending on the device, so we need to have a separate
19274f80b14cSVincenzo Maffione  * physical-address look-up table for each na.
19284f80b14cSVincenzo Maffione  * We can still share the vaddrs, though, therefore we split
19294f80b14cSVincenzo Maffione  * the lut_entry structure.
19304f80b14cSVincenzo Maffione  */
19314f80b14cSVincenzo Maffione struct lut_entry {
19324f80b14cSVincenzo Maffione 	void *vaddr;		/* virtual address. */
19334f80b14cSVincenzo Maffione };
19344f80b14cSVincenzo Maffione 
19354f80b14cSVincenzo Maffione struct plut_entry {
19364f80b14cSVincenzo Maffione 	vm_paddr_t paddr;	/* physical address. */
19374f80b14cSVincenzo Maffione };
19384f80b14cSVincenzo Maffione #endif /* linux & _WIN32 */
1939d76bf4ffSLuigi Rizzo 
1940d76bf4ffSLuigi Rizzo struct netmap_obj_pool;
1941d76bf4ffSLuigi Rizzo 
1942a6d768d8SVincenzo Maffione /* alignment for netmap buffers */
1943a6d768d8SVincenzo Maffione #define NM_BUF_ALIGN	64
1944a6d768d8SVincenzo Maffione 
194568b8534bSLuigi Rizzo /*
19466e10c8b8SLuigi Rizzo  * NMB return the virtual address of a buffer (buffer 0 on bad index)
19476e10c8b8SLuigi Rizzo  * PNMB also fills the physical address
194868b8534bSLuigi Rizzo  */
19496e10c8b8SLuigi Rizzo static inline void *
19504bf50f18SLuigi Rizzo NMB(struct netmap_adapter *na, struct netmap_slot *slot)
1951f9790aebSLuigi Rizzo {
1952847bf383SLuigi Rizzo 	struct lut_entry *lut = na->na_lut.lut;
1953f9790aebSLuigi Rizzo 	uint32_t i = slot->buf_idx;
1954847bf383SLuigi Rizzo 	return (unlikely(i >= na->na_lut.objtotal)) ?
1955f9790aebSLuigi Rizzo 		lut[0].vaddr : lut[i].vaddr;
1956f9790aebSLuigi Rizzo }
1957f9790aebSLuigi Rizzo 
19584bf50f18SLuigi Rizzo static inline void *
19594bf50f18SLuigi Rizzo PNMB(struct netmap_adapter *na, struct netmap_slot *slot, uint64_t *pp)
19604bf50f18SLuigi Rizzo {
19614bf50f18SLuigi Rizzo 	uint32_t i = slot->buf_idx;
1962847bf383SLuigi Rizzo 	struct lut_entry *lut = na->na_lut.lut;
19634f80b14cSVincenzo Maffione 	struct plut_entry *plut = na->na_lut.plut;
1964847bf383SLuigi Rizzo 	void *ret = (i >= na->na_lut.objtotal) ? lut[0].vaddr : lut[i].vaddr;
19654bf50f18SLuigi Rizzo 
19664f80b14cSVincenzo Maffione #ifdef _WIN32
19674f80b14cSVincenzo Maffione 	*pp = (i >= na->na_lut.objtotal) ? (uint64_t)plut[0].paddr.QuadPart : (uint64_t)plut[i].paddr.QuadPart;
196837e3a6d3SLuigi Rizzo #else
19694f80b14cSVincenzo Maffione 	*pp = (i >= na->na_lut.objtotal) ? plut[0].paddr : plut[i].paddr;
197037e3a6d3SLuigi Rizzo #endif
19714bf50f18SLuigi Rizzo 	return ret;
19724bf50f18SLuigi Rizzo }
19734bf50f18SLuigi Rizzo 
1974a6d768d8SVincenzo Maffione static inline void
1975a6d768d8SVincenzo Maffione nm_write_offset(struct netmap_kring *kring,
1976a6d768d8SVincenzo Maffione 		struct netmap_slot *slot, uint64_t offset)
1977a6d768d8SVincenzo Maffione {
1978a6d768d8SVincenzo Maffione 	slot->ptr = (slot->ptr & ~kring->offset_mask) |
1979a6d768d8SVincenzo Maffione 		(offset & kring->offset_mask);
1980a6d768d8SVincenzo Maffione }
1981a6d768d8SVincenzo Maffione 
1982a6d768d8SVincenzo Maffione static inline uint64_t
1983a6d768d8SVincenzo Maffione nm_get_offset(struct netmap_kring *kring, struct netmap_slot *slot)
1984a6d768d8SVincenzo Maffione {
1985a6d768d8SVincenzo Maffione 	uint64_t offset = (slot->ptr & kring->offset_mask);
1986a6d768d8SVincenzo Maffione 	if (unlikely(offset > kring->offset_max))
1987a6d768d8SVincenzo Maffione 		offset = kring->offset_max;
1988a6d768d8SVincenzo Maffione 	return offset;
1989a6d768d8SVincenzo Maffione }
1990a6d768d8SVincenzo Maffione 
1991a6d768d8SVincenzo Maffione static inline void *
1992a6d768d8SVincenzo Maffione NMB_O(struct netmap_kring *kring, struct netmap_slot *slot)
1993a6d768d8SVincenzo Maffione {
1994a6d768d8SVincenzo Maffione 	void *addr = NMB(kring->na, slot);
1995a6d768d8SVincenzo Maffione 	return (char *)addr + nm_get_offset(kring, slot);
1996a6d768d8SVincenzo Maffione }
1997a6d768d8SVincenzo Maffione 
1998a6d768d8SVincenzo Maffione static inline void *
1999a6d768d8SVincenzo Maffione PNMB_O(struct netmap_kring *kring, struct netmap_slot *slot, uint64_t *pp)
2000a6d768d8SVincenzo Maffione {
2001a6d768d8SVincenzo Maffione 	void *addr = PNMB(kring->na, slot, pp);
2002a6d768d8SVincenzo Maffione 	uint64_t offset = nm_get_offset(kring, slot);
2003a6d768d8SVincenzo Maffione 	addr = (char *)addr + offset;
2004a6d768d8SVincenzo Maffione 	*pp += offset;
2005a6d768d8SVincenzo Maffione 	return addr;
2006a6d768d8SVincenzo Maffione }
2007a6d768d8SVincenzo Maffione 
2008f9790aebSLuigi Rizzo 
200917885a7bSLuigi Rizzo /*
20108fd44c93SLuigi Rizzo  * Structure associated to each netmap file descriptor.
20118fd44c93SLuigi Rizzo  * It is created on open and left unbound (np_nifp == NULL).
20128fd44c93SLuigi Rizzo  * A successful NIOCREGIF will set np_nifp and the first few fields;
20138fd44c93SLuigi Rizzo  * this is protected by a global lock (NMG_LOCK) due to low contention.
2014f9790aebSLuigi Rizzo  *
20158fd44c93SLuigi Rizzo  * np_refs counts the number of references to the structure: one for the fd,
20168fd44c93SLuigi Rizzo  * plus (on FreeBSD) one for each active mmap which we track ourselves
201785fe4e7cSLuigi Rizzo  * (linux automatically tracks them, but FreeBSD does not).
20188fd44c93SLuigi Rizzo  * np_refs is protected by NMG_LOCK.
201917885a7bSLuigi Rizzo  *
20208fd44c93SLuigi Rizzo  * Read access to the structure is lock free, because ni_nifp once set
20218fd44c93SLuigi Rizzo  * can only go to 0 when nobody is using the entry anymore. Readers
20228fd44c93SLuigi Rizzo  * must check that np_nifp != NULL before using the other fields.
2023f9790aebSLuigi Rizzo  */
2024f9790aebSLuigi Rizzo struct netmap_priv_d {
2025f9790aebSLuigi Rizzo 	struct netmap_if * volatile np_nifp;	/* netmap if descriptor. */
2026f9790aebSLuigi Rizzo 
2027f9790aebSLuigi Rizzo 	struct netmap_adapter	*np_na;
2028e330262fSJustin Hibbits 	if_t		np_ifp;
2029f0ea3689SLuigi Rizzo 	uint32_t	np_flags;	/* from the ioctl */
2030847bf383SLuigi Rizzo 	u_int		np_qfirst[NR_TXRX],
2031847bf383SLuigi Rizzo 			np_qlast[NR_TXRX]; /* range of tx/rx rings to scan */
20324f80b14cSVincenzo Maffione 	uint16_t	np_txpoll;
2033b6e66be2SVincenzo Maffione 	uint16_t        np_kloop_state;	/* use with NMG_LOCK held */
2034b6e66be2SVincenzo Maffione #define NM_SYNC_KLOOP_RUNNING	(1 << 0)
2035b6e66be2SVincenzo Maffione #define NM_SYNC_KLOOP_STOPPING	(1 << 1)
2036c3e9b4dbSLuiz Otavio O Souza 	int             np_sync_flags; /* to be passed to nm_sync */
2037f9790aebSLuigi Rizzo 
20388fd44c93SLuigi Rizzo 	int		np_refs;	/* use with NMG_LOCK held */
2039f0ea3689SLuigi Rizzo 
2040f0ea3689SLuigi Rizzo 	/* pointers to the selinfo to be used for selrecord.
2041f0ea3689SLuigi Rizzo 	 * Either the local or the global one depending on the
2042f0ea3689SLuigi Rizzo 	 * number of rings.
2043f0ea3689SLuigi Rizzo 	 */
2044847bf383SLuigi Rizzo 	NM_SELINFO_T *np_si[NR_TXRX];
2045b6e66be2SVincenzo Maffione 
2046b6e66be2SVincenzo Maffione 	/* In the optional CSB mode, the user must specify the start address
2047b6e66be2SVincenzo Maffione 	 * of two arrays of Communication Status Block (CSB) entries, for the
2048b6e66be2SVincenzo Maffione 	 * two directions (kernel read application write, and kernel write
2049b6e66be2SVincenzo Maffione 	 * application read).
2050b6e66be2SVincenzo Maffione 	 * The number of entries must agree with the number of rings bound to
2051b6e66be2SVincenzo Maffione 	 * the netmap file descriptor. The entries corresponding to the TX
2052b6e66be2SVincenzo Maffione 	 * rings are laid out before the ones corresponding to the RX rings.
2053b6e66be2SVincenzo Maffione 	 *
2054b6e66be2SVincenzo Maffione 	 * Array of CSB entries for application --> kernel communication
2055b6e66be2SVincenzo Maffione 	 * (N entries). */
2056b6e66be2SVincenzo Maffione 	struct nm_csb_atok	*np_csb_atok_base;
2057b6e66be2SVincenzo Maffione 	/* Array of CSB entries for kernel --> application communication
2058b6e66be2SVincenzo Maffione 	 * (N entries). */
2059b6e66be2SVincenzo Maffione 	struct nm_csb_ktoa	*np_csb_ktoa_base;
2060b6e66be2SVincenzo Maffione 
2061b6e66be2SVincenzo Maffione #ifdef linux
2062b6e66be2SVincenzo Maffione 	struct file	*np_filp;  /* used by sync kloop */
2063b6e66be2SVincenzo Maffione #endif /* linux */
2064f9790aebSLuigi Rizzo };
2065f9790aebSLuigi Rizzo 
206637e3a6d3SLuigi Rizzo struct netmap_priv_d *netmap_priv_new(void);
206737e3a6d3SLuigi Rizzo void netmap_priv_delete(struct netmap_priv_d *);
206837e3a6d3SLuigi Rizzo 
206937e3a6d3SLuigi Rizzo static inline int nm_kring_pending(struct netmap_priv_d *np)
207037e3a6d3SLuigi Rizzo {
207137e3a6d3SLuigi Rizzo 	struct netmap_adapter *na = np->np_na;
207237e3a6d3SLuigi Rizzo 	enum txrx t;
207337e3a6d3SLuigi Rizzo 	int i;
207437e3a6d3SLuigi Rizzo 
207537e3a6d3SLuigi Rizzo 	for_rx_tx(t) {
207637e3a6d3SLuigi Rizzo 		for (i = np->np_qfirst[t]; i < np->np_qlast[t]; i++) {
20772ff91c17SVincenzo Maffione 			struct netmap_kring *kring = NMR(na, t)[i];
207837e3a6d3SLuigi Rizzo 			if (kring->nr_mode != kring->nr_pending_mode) {
207937e3a6d3SLuigi Rizzo 				return 1;
208037e3a6d3SLuigi Rizzo 			}
208137e3a6d3SLuigi Rizzo 		}
208237e3a6d3SLuigi Rizzo 	}
208337e3a6d3SLuigi Rizzo 	return 0;
208437e3a6d3SLuigi Rizzo }
208537e3a6d3SLuigi Rizzo 
2086b6e66be2SVincenzo Maffione /* call with NMG_LOCK held */
2087b6e66be2SVincenzo Maffione static __inline int
2088b6e66be2SVincenzo Maffione nm_si_user(struct netmap_priv_d *priv, enum txrx t)
2089b6e66be2SVincenzo Maffione {
2090b6e66be2SVincenzo Maffione 	return (priv->np_na != NULL &&
2091b6e66be2SVincenzo Maffione 		(priv->np_qlast[t] - priv->np_qfirst[t] > 1));
2092b6e66be2SVincenzo Maffione }
2093b6e66be2SVincenzo Maffione 
2094c3e9b4dbSLuiz Otavio O Souza #ifdef WITH_PIPES
2095c3e9b4dbSLuiz Otavio O Souza int netmap_pipe_txsync(struct netmap_kring *txkring, int flags);
2096c3e9b4dbSLuiz Otavio O Souza int netmap_pipe_rxsync(struct netmap_kring *rxkring, int flags);
209775f4f3edSVincenzo Maffione int netmap_pipe_krings_create_both(struct netmap_adapter *na,
209875f4f3edSVincenzo Maffione 				  struct netmap_adapter *ona);
209975f4f3edSVincenzo Maffione void netmap_pipe_krings_delete_both(struct netmap_adapter *na,
210075f4f3edSVincenzo Maffione 				    struct netmap_adapter *ona);
210175f4f3edSVincenzo Maffione int netmap_pipe_reg_both(struct netmap_adapter *na,
210275f4f3edSVincenzo Maffione 			 struct netmap_adapter *ona);
2103c3e9b4dbSLuiz Otavio O Souza #endif /* WITH_PIPES */
2104c3e9b4dbSLuiz Otavio O Souza 
21054bf50f18SLuigi Rizzo #ifdef WITH_MONITOR
21064bf50f18SLuigi Rizzo 
21074bf50f18SLuigi Rizzo struct netmap_monitor_adapter {
21084bf50f18SLuigi Rizzo 	struct netmap_adapter up;
21094bf50f18SLuigi Rizzo 
21104bf50f18SLuigi Rizzo 	struct netmap_priv_d priv;
21114bf50f18SLuigi Rizzo 	uint32_t flags;
21124bf50f18SLuigi Rizzo };
21134bf50f18SLuigi Rizzo 
21144bf50f18SLuigi Rizzo #endif /* WITH_MONITOR */
21154bf50f18SLuigi Rizzo 
2116f9790aebSLuigi Rizzo 
2117039dd540SLuigi Rizzo #ifdef WITH_GENERIC
2118f9790aebSLuigi Rizzo /*
2119f9790aebSLuigi Rizzo  * generic netmap emulation for devices that do not have
2120f9790aebSLuigi Rizzo  * native netmap support.
2121f9790aebSLuigi Rizzo  */
2122e330262fSJustin Hibbits int generic_netmap_attach(if_t ifp);
2123e330262fSJustin Hibbits int generic_rx_handler(if_t ifp, struct mbuf *m);
2124f9790aebSLuigi Rizzo 
212537e3a6d3SLuigi Rizzo int nm_os_catch_rx(struct netmap_generic_adapter *gna, int intercept);
212637e3a6d3SLuigi Rizzo int nm_os_catch_tx(struct netmap_generic_adapter *gna, int intercept);
212737e3a6d3SLuigi Rizzo 
2128c3e9b4dbSLuiz Otavio O Souza int na_is_generic(struct netmap_adapter *na);
2129c3e9b4dbSLuiz Otavio O Souza 
213037e3a6d3SLuigi Rizzo /*
213137e3a6d3SLuigi Rizzo  * the generic transmit routine is passed a structure to optionally
213237e3a6d3SLuigi Rizzo  * build a queue of descriptors, in an OS-specific way.
213337e3a6d3SLuigi Rizzo  * The payload is at addr, if non-null, and the routine should send or queue
213437e3a6d3SLuigi Rizzo  * the packet, returning 0 if successful, 1 on failure.
213537e3a6d3SLuigi Rizzo  *
213637e3a6d3SLuigi Rizzo  * At the end, if head is non-null, there will be an additional call
213737e3a6d3SLuigi Rizzo  * to the function with addr = NULL; this should tell the OS-specific
213837e3a6d3SLuigi Rizzo  * routine to send the queue and free any resources. Failure is ignored.
213937e3a6d3SLuigi Rizzo  */
214037e3a6d3SLuigi Rizzo struct nm_os_gen_arg {
2141e330262fSJustin Hibbits 	if_t ifp;
214237e3a6d3SLuigi Rizzo 	void *m;	/* os-specific mbuf-like object */
214337e3a6d3SLuigi Rizzo 	void *head, *tail; /* tailq, if the OS-specific routine needs to build one */
214437e3a6d3SLuigi Rizzo 	void *addr;	/* payload of current packet */
214537e3a6d3SLuigi Rizzo 	u_int len;	/* packet length */
2146854b2f30SMark Johnston 	u_int ring_nr;	/* transmit ring index */
214737e3a6d3SLuigi Rizzo 	u_int qevent;   /* in txqdisc mode, place an event on this mbuf */
214837e3a6d3SLuigi Rizzo };
214937e3a6d3SLuigi Rizzo 
215037e3a6d3SLuigi Rizzo int nm_os_generic_xmit_frame(struct nm_os_gen_arg *);
2151e330262fSJustin Hibbits int nm_os_generic_find_num_desc(if_t ifp, u_int *tx, u_int *rx);
2152e330262fSJustin Hibbits void nm_os_generic_find_num_queues(if_t ifp, u_int *txq, u_int *rxq);
215337e3a6d3SLuigi Rizzo void nm_os_generic_set_features(struct netmap_generic_adapter *gna);
215437e3a6d3SLuigi Rizzo 
2155e330262fSJustin Hibbits static inline if_t
2156847bf383SLuigi Rizzo netmap_generic_getifp(struct netmap_generic_adapter *gna)
2157847bf383SLuigi Rizzo {
2158847bf383SLuigi Rizzo         if (gna->prev)
2159847bf383SLuigi Rizzo             return gna->prev->ifp;
2160847bf383SLuigi Rizzo 
2161847bf383SLuigi Rizzo         return gna->up.up.ifp;
2162847bf383SLuigi Rizzo }
2163f9790aebSLuigi Rizzo 
216437e3a6d3SLuigi Rizzo void netmap_generic_irq(struct netmap_adapter *na, u_int q, u_int *work_done);
216537e3a6d3SLuigi Rizzo 
21664bf50f18SLuigi Rizzo //#define RATE_GENERIC  /* Enables communication statistics for generic. */
21674bf50f18SLuigi Rizzo #ifdef RATE_GENERIC
21684bf50f18SLuigi Rizzo void generic_rate(int txp, int txs, int txi, int rxp, int rxs, int rxi);
21694bf50f18SLuigi Rizzo #else
21704bf50f18SLuigi Rizzo #define generic_rate(txp, txs, txi, rxp, rxs, rxi)
21714bf50f18SLuigi Rizzo #endif
21724bf50f18SLuigi Rizzo 
2173f9790aebSLuigi Rizzo /*
2174f9790aebSLuigi Rizzo  * netmap_mitigation API. This is used by the generic adapter
2175f9790aebSLuigi Rizzo  * to reduce the number of interrupt requests/selwakeup
2176f9790aebSLuigi Rizzo  * to clients on incoming packets.
2177f9790aebSLuigi Rizzo  */
217837e3a6d3SLuigi Rizzo void nm_os_mitigation_init(struct nm_generic_mit *mit, int idx,
21794bf50f18SLuigi Rizzo                                 struct netmap_adapter *na);
218037e3a6d3SLuigi Rizzo void nm_os_mitigation_start(struct nm_generic_mit *mit);
218137e3a6d3SLuigi Rizzo void nm_os_mitigation_restart(struct nm_generic_mit *mit);
218237e3a6d3SLuigi Rizzo int nm_os_mitigation_active(struct nm_generic_mit *mit);
218337e3a6d3SLuigi Rizzo void nm_os_mitigation_cleanup(struct nm_generic_mit *mit);
218437e3a6d3SLuigi Rizzo #else /* !WITH_GENERIC */
218537e3a6d3SLuigi Rizzo #define generic_netmap_attach(ifp)	(EOPNOTSUPP)
2186c3e9b4dbSLuiz Otavio O Souza #define na_is_generic(na)		(0)
2187039dd540SLuigi Rizzo #endif /* WITH_GENERIC */
2188f0ea3689SLuigi Rizzo 
2189f0ea3689SLuigi Rizzo /* Shared declarations for the VALE switch. */
2190f0ea3689SLuigi Rizzo 
2191f0ea3689SLuigi Rizzo /*
2192f0ea3689SLuigi Rizzo  * Each transmit queue accumulates a batch of packets into
2193f0ea3689SLuigi Rizzo  * a structure before forwarding. Packets to the same
2194f0ea3689SLuigi Rizzo  * destination are put in a list using ft_next as a link field.
2195f0ea3689SLuigi Rizzo  * ft_frags and ft_next are valid only on the first fragment.
2196f0ea3689SLuigi Rizzo  */
2197f0ea3689SLuigi Rizzo struct nm_bdg_fwd {	/* forwarding entry for a bridge */
2198f0ea3689SLuigi Rizzo 	void *ft_buf;		/* netmap or indirect buffer */
2199f0ea3689SLuigi Rizzo 	uint8_t ft_frags;	/* how many fragments (only on 1st frag) */
22002ff91c17SVincenzo Maffione 	uint16_t ft_offset;	/* dst port (unused) */
2201f0ea3689SLuigi Rizzo 	uint16_t ft_flags;	/* flags, e.g. indirect */
2202f0ea3689SLuigi Rizzo 	uint16_t ft_len;	/* src fragment len */
2203f0ea3689SLuigi Rizzo 	uint16_t ft_next;	/* next packet to same destination */
2204f0ea3689SLuigi Rizzo };
2205f0ea3689SLuigi Rizzo 
2206f0ea3689SLuigi Rizzo /* struct 'virtio_net_hdr' from linux. */
2207f0ea3689SLuigi Rizzo struct nm_vnet_hdr {
2208f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_F_NEEDS_CSUM     1	/* Use csum_start, csum_offset */
2209f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_F_DATA_VALID    2	/* Csum is valid */
2210f0ea3689SLuigi Rizzo     uint8_t flags;
2211f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_NONE         0       /* Not a GSO frame */
2212f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_TCPV4        1       /* GSO frame, IPv4 TCP (TSO) */
2213f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_UDP          3       /* GSO frame, IPv4 UDP (UFO) */
2214f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_TCPV6        4       /* GSO frame, IPv6 TCP */
2215f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_ECN          0x80    /* TCP has ECN set */
2216f0ea3689SLuigi Rizzo     uint8_t gso_type;
2217f0ea3689SLuigi Rizzo     uint16_t hdr_len;
2218f0ea3689SLuigi Rizzo     uint16_t gso_size;
2219f0ea3689SLuigi Rizzo     uint16_t csum_start;
2220f0ea3689SLuigi Rizzo     uint16_t csum_offset;
2221f0ea3689SLuigi Rizzo };
2222f0ea3689SLuigi Rizzo 
2223f0ea3689SLuigi Rizzo #define WORST_CASE_GSO_HEADER	(14+40+60)  /* IPv6 + TCP */
2224f0ea3689SLuigi Rizzo 
2225f0ea3689SLuigi Rizzo /* Private definitions for IPv4, IPv6, UDP and TCP headers. */
2226f0ea3689SLuigi Rizzo 
2227f0ea3689SLuigi Rizzo struct nm_iphdr {
2228f0ea3689SLuigi Rizzo 	uint8_t		version_ihl;
2229f0ea3689SLuigi Rizzo 	uint8_t		tos;
2230f0ea3689SLuigi Rizzo 	uint16_t	tot_len;
2231f0ea3689SLuigi Rizzo 	uint16_t	id;
2232f0ea3689SLuigi Rizzo 	uint16_t	frag_off;
2233f0ea3689SLuigi Rizzo 	uint8_t		ttl;
2234f0ea3689SLuigi Rizzo 	uint8_t		protocol;
2235f0ea3689SLuigi Rizzo 	uint16_t	check;
2236f0ea3689SLuigi Rizzo 	uint32_t	saddr;
2237f0ea3689SLuigi Rizzo 	uint32_t	daddr;
2238f0ea3689SLuigi Rizzo 	/*The options start here. */
2239f0ea3689SLuigi Rizzo };
2240f0ea3689SLuigi Rizzo 
2241f0ea3689SLuigi Rizzo struct nm_tcphdr {
2242f0ea3689SLuigi Rizzo 	uint16_t	source;
2243f0ea3689SLuigi Rizzo 	uint16_t	dest;
2244f0ea3689SLuigi Rizzo 	uint32_t	seq;
2245f0ea3689SLuigi Rizzo 	uint32_t	ack_seq;
2246f0ea3689SLuigi Rizzo 	uint8_t		doff;  /* Data offset + Reserved */
2247f0ea3689SLuigi Rizzo 	uint8_t		flags;
2248f0ea3689SLuigi Rizzo 	uint16_t	window;
2249f0ea3689SLuigi Rizzo 	uint16_t	check;
2250f0ea3689SLuigi Rizzo 	uint16_t	urg_ptr;
2251f0ea3689SLuigi Rizzo };
2252f0ea3689SLuigi Rizzo 
2253f0ea3689SLuigi Rizzo struct nm_udphdr {
2254f0ea3689SLuigi Rizzo 	uint16_t	source;
2255f0ea3689SLuigi Rizzo 	uint16_t	dest;
2256f0ea3689SLuigi Rizzo 	uint16_t	len;
2257f0ea3689SLuigi Rizzo 	uint16_t	check;
2258f0ea3689SLuigi Rizzo };
2259f0ea3689SLuigi Rizzo 
2260f0ea3689SLuigi Rizzo struct nm_ipv6hdr {
2261f0ea3689SLuigi Rizzo 	uint8_t		priority_version;
2262f0ea3689SLuigi Rizzo 	uint8_t		flow_lbl[3];
2263f0ea3689SLuigi Rizzo 
2264f0ea3689SLuigi Rizzo 	uint16_t	payload_len;
2265f0ea3689SLuigi Rizzo 	uint8_t		nexthdr;
2266f0ea3689SLuigi Rizzo 	uint8_t		hop_limit;
2267f0ea3689SLuigi Rizzo 
2268f0ea3689SLuigi Rizzo 	uint8_t		saddr[16];
2269f0ea3689SLuigi Rizzo 	uint8_t		daddr[16];
2270f0ea3689SLuigi Rizzo };
2271f0ea3689SLuigi Rizzo 
2272f0ea3689SLuigi Rizzo /* Type used to store a checksum (in host byte order) that hasn't been
2273f0ea3689SLuigi Rizzo  * folded yet.
2274f0ea3689SLuigi Rizzo  */
2275f0ea3689SLuigi Rizzo #define rawsum_t uint32_t
2276f0ea3689SLuigi Rizzo 
227737e3a6d3SLuigi Rizzo rawsum_t nm_os_csum_raw(uint8_t *data, size_t len, rawsum_t cur_sum);
227837e3a6d3SLuigi Rizzo uint16_t nm_os_csum_ipv4(struct nm_iphdr *iph);
227937e3a6d3SLuigi Rizzo void nm_os_csum_tcpudp_ipv4(struct nm_iphdr *iph, void *data,
2280f0ea3689SLuigi Rizzo 		      size_t datalen, uint16_t *check);
228137e3a6d3SLuigi Rizzo void nm_os_csum_tcpudp_ipv6(struct nm_ipv6hdr *ip6h, void *data,
2282f0ea3689SLuigi Rizzo 		      size_t datalen, uint16_t *check);
228337e3a6d3SLuigi Rizzo uint16_t nm_os_csum_fold(rawsum_t cur_sum);
2284f0ea3689SLuigi Rizzo 
2285f0ea3689SLuigi Rizzo void bdg_mismatch_datapath(struct netmap_vp_adapter *na,
2286f0ea3689SLuigi Rizzo 			   struct netmap_vp_adapter *dst_na,
228737e3a6d3SLuigi Rizzo 			   const struct nm_bdg_fwd *ft_p,
228837e3a6d3SLuigi Rizzo 			   struct netmap_ring *dst_ring,
2289f0ea3689SLuigi Rizzo 			   u_int *j, u_int lim, u_int *howmany);
22904bf50f18SLuigi Rizzo 
22914bf50f18SLuigi Rizzo /* persistent virtual port routines */
2292e330262fSJustin Hibbits int nm_os_vi_persist(const char *, if_t *);
2293e330262fSJustin Hibbits void nm_os_vi_detach(if_t);
229437e3a6d3SLuigi Rizzo void nm_os_vi_init_index(void);
229537e3a6d3SLuigi Rizzo 
229637e3a6d3SLuigi Rizzo /*
229737e3a6d3SLuigi Rizzo  * kernel thread routines
229837e3a6d3SLuigi Rizzo  */
2299c3e9b4dbSLuiz Otavio O Souza struct nm_kctx; /* OS-specific kernel context - opaque */
2300b6e66be2SVincenzo Maffione typedef void (*nm_kctx_worker_fn_t)(void *data);
230137e3a6d3SLuigi Rizzo 
230237e3a6d3SLuigi Rizzo /* kthread configuration */
2303c3e9b4dbSLuiz Otavio O Souza struct nm_kctx_cfg {
230437e3a6d3SLuigi Rizzo 	long			type;		/* kthread type/identifier */
2305c3e9b4dbSLuiz Otavio O Souza 	nm_kctx_worker_fn_t	worker_fn;	/* worker function */
230637e3a6d3SLuigi Rizzo 	void			*worker_private;/* worker parameter */
230737e3a6d3SLuigi Rizzo 	int			attach_user;	/* attach kthread to user process */
230837e3a6d3SLuigi Rizzo };
230937e3a6d3SLuigi Rizzo /* kthread configuration */
2310c3e9b4dbSLuiz Otavio O Souza struct nm_kctx *nm_os_kctx_create(struct nm_kctx_cfg *cfg,
2311844a6f0cSLuigi Rizzo 					void *opaque);
2312c3e9b4dbSLuiz Otavio O Souza int nm_os_kctx_worker_start(struct nm_kctx *);
2313c3e9b4dbSLuiz Otavio O Souza void nm_os_kctx_worker_stop(struct nm_kctx *);
2314c3e9b4dbSLuiz Otavio O Souza void nm_os_kctx_destroy(struct nm_kctx *);
2315c3e9b4dbSLuiz Otavio O Souza void nm_os_kctx_worker_setaff(struct nm_kctx *, int);
231637e3a6d3SLuigi Rizzo u_int nm_os_ncpus(void);
231737e3a6d3SLuigi Rizzo 
2318b6e66be2SVincenzo Maffione int netmap_sync_kloop(struct netmap_priv_d *priv,
2319b6e66be2SVincenzo Maffione 		      struct nmreq_header *hdr);
2320b6e66be2SVincenzo Maffione int netmap_sync_kloop_stop(struct netmap_priv_d *priv);
2321b6e66be2SVincenzo Maffione 
2322b6e66be2SVincenzo Maffione #ifdef WITH_PTNETMAP
2323b6e66be2SVincenzo Maffione /* ptnetmap guest routines */
2324b6e66be2SVincenzo Maffione 
232537e3a6d3SLuigi Rizzo /*
2326b6e66be2SVincenzo Maffione  * ptnetmap_memdev routines used to talk with ptnetmap_memdev device driver
232737e3a6d3SLuigi Rizzo  */
2328b6e66be2SVincenzo Maffione struct ptnetmap_memdev;
2329b6e66be2SVincenzo Maffione int nm_os_pt_memdev_iomap(struct ptnetmap_memdev *, vm_paddr_t *, void **,
2330b6e66be2SVincenzo Maffione                           uint64_t *);
2331b6e66be2SVincenzo Maffione void nm_os_pt_memdev_iounmap(struct ptnetmap_memdev *);
2332b6e66be2SVincenzo Maffione uint32_t nm_os_pt_memdev_ioread(struct ptnetmap_memdev *, unsigned int);
233337e3a6d3SLuigi Rizzo 
233437e3a6d3SLuigi Rizzo /*
233537e3a6d3SLuigi Rizzo  * netmap adapter for guest ptnetmap ports
233637e3a6d3SLuigi Rizzo  */
233737e3a6d3SLuigi Rizzo struct netmap_pt_guest_adapter {
233837e3a6d3SLuigi Rizzo         /* The netmap adapter to be used by netmap applications.
233937e3a6d3SLuigi Rizzo 	 * This field must be the first, to allow upcast. */
234037e3a6d3SLuigi Rizzo 	struct netmap_hw_adapter hwup;
234137e3a6d3SLuigi Rizzo 
234237e3a6d3SLuigi Rizzo         /* The netmap adapter to be used by the driver. */
234337e3a6d3SLuigi Rizzo         struct netmap_hw_adapter dr;
234437e3a6d3SLuigi Rizzo 
234537e3a6d3SLuigi Rizzo 	/* Reference counter to track users of backend netmap port: the
234637e3a6d3SLuigi Rizzo 	 * network stack and netmap clients.
234737e3a6d3SLuigi Rizzo 	 * Used to decide when we need (de)allocate krings/rings and
234837e3a6d3SLuigi Rizzo 	 * start (stop) ptnetmap kthreads. */
2349b6e66be2SVincenzo Maffione 	int backend_users;
235037e3a6d3SLuigi Rizzo 
235137e3a6d3SLuigi Rizzo };
235237e3a6d3SLuigi Rizzo 
235346023447SVincenzo Maffione int netmap_pt_guest_attach(struct netmap_adapter *na,
235446023447SVincenzo Maffione 			unsigned int nifp_offset,
235546023447SVincenzo Maffione 			unsigned int memid);
2356b6e66be2SVincenzo Maffione bool netmap_pt_guest_txsync(struct nm_csb_atok *atok,
2357b6e66be2SVincenzo Maffione 			struct nm_csb_ktoa *ktoa,
2358b6e66be2SVincenzo Maffione 			struct netmap_kring *kring, int flags);
2359b6e66be2SVincenzo Maffione bool netmap_pt_guest_rxsync(struct nm_csb_atok *atok,
2360b6e66be2SVincenzo Maffione 			struct nm_csb_ktoa *ktoa,
236146023447SVincenzo Maffione 			struct netmap_kring *kring, int flags);
236237e3a6d3SLuigi Rizzo int ptnet_nm_krings_create(struct netmap_adapter *na);
236337e3a6d3SLuigi Rizzo void ptnet_nm_krings_delete(struct netmap_adapter *na);
236437e3a6d3SLuigi Rizzo void ptnet_nm_dtor(struct netmap_adapter *na);
2365b6e66be2SVincenzo Maffione 
2366f79ba6d7SVincenzo Maffione /* Helper function wrapping nm_sync_kloop_appl_read(). */
2367b6e66be2SVincenzo Maffione static inline void
2368b6e66be2SVincenzo Maffione ptnet_sync_tail(struct nm_csb_ktoa *ktoa, struct netmap_kring *kring)
2369b6e66be2SVincenzo Maffione {
2370b6e66be2SVincenzo Maffione 	struct netmap_ring *ring = kring->ring;
2371b6e66be2SVincenzo Maffione 
2372b6e66be2SVincenzo Maffione 	/* Update hwcur and hwtail as known by the host. */
2373f79ba6d7SVincenzo Maffione         nm_sync_kloop_appl_read(ktoa, &kring->nr_hwtail, &kring->nr_hwcur);
2374b6e66be2SVincenzo Maffione 
2375b6e66be2SVincenzo Maffione 	/* nm_sync_finalize */
2376b6e66be2SVincenzo Maffione 	ring->tail = kring->rtail = kring->nr_hwtail;
2377b6e66be2SVincenzo Maffione }
2378b6e66be2SVincenzo Maffione #endif /* WITH_PTNETMAP */
23794bf50f18SLuigi Rizzo 
23802a7db7a6SVincenzo Maffione #ifdef __FreeBSD__
23812a7db7a6SVincenzo Maffione /*
23822a7db7a6SVincenzo Maffione  * FreeBSD mbuf allocator/deallocator in emulation mode:
23832a7db7a6SVincenzo Maffione  *
23842a7db7a6SVincenzo Maffione  * We allocate mbufs with m_gethdr(), since the mbuf header is needed
23852a7db7a6SVincenzo Maffione  * by the driver. We also attach a customly-provided external storage,
2386ce12afaaSMark Johnston  * which in this case is a netmap buffer.
23872a7db7a6SVincenzo Maffione  *
23882a7db7a6SVincenzo Maffione  * The dtor function does nothing, however we need it since mb_free_ext()
23892a7db7a6SVincenzo Maffione  * has a KASSERT(), checking that the mbuf dtor function is not NULL.
23902a7db7a6SVincenzo Maffione  */
23912a7db7a6SVincenzo Maffione 
2392ce12afaaSMark Johnston static inline void
2393ce12afaaSMark Johnston nm_generic_mbuf_dtor(struct mbuf *m)
2394ce12afaaSMark Johnston {
2395ce12afaaSMark Johnston 	uma_zfree(zone_clust, m->m_ext.ext_buf);
2396ce12afaaSMark Johnston }
23972a7db7a6SVincenzo Maffione 
23982a7db7a6SVincenzo Maffione #define SET_MBUF_DESTRUCTOR(m, fn)	do {		\
23992a7db7a6SVincenzo Maffione 	(m)->m_ext.ext_free = (fn != NULL) ?		\
2400ce12afaaSMark Johnston 	    (void *)fn : (void *)nm_generic_mbuf_dtor;	\
24012a7db7a6SVincenzo Maffione } while (0)
24022a7db7a6SVincenzo Maffione 
24032a7db7a6SVincenzo Maffione static inline struct mbuf *
2404ce12afaaSMark Johnston nm_os_get_mbuf(if_t ifp __unused, int len)
24052a7db7a6SVincenzo Maffione {
24062a7db7a6SVincenzo Maffione 	struct mbuf *m;
2407ce12afaaSMark Johnston 	void *buf;
24082a7db7a6SVincenzo Maffione 
2409ce12afaaSMark Johnston 	KASSERT(len <= MCLBYTES, ("%s: len %d", __func__, len));
24102a7db7a6SVincenzo Maffione 
24112a7db7a6SVincenzo Maffione 	m = m_gethdr(M_NOWAIT, MT_DATA);
2412ce12afaaSMark Johnston 	if (__predict_false(m == NULL))
2413ce12afaaSMark Johnston 		return (NULL);
2414ce12afaaSMark Johnston 	buf = uma_zalloc(zone_clust, M_NOWAIT);
2415ce12afaaSMark Johnston 	if (__predict_false(buf == NULL)) {
2416ce12afaaSMark Johnston 		m_free(m);
2417ce12afaaSMark Johnston 		return (NULL);
2418ce12afaaSMark Johnston 	}
2419ce12afaaSMark Johnston 	m_extadd(m, buf, MCLBYTES, nm_generic_mbuf_dtor, NULL, NULL, 0,
2420ce12afaaSMark Johnston 	    EXT_NET_DRV);
2421ce12afaaSMark Johnston 	return (m);
24222a7db7a6SVincenzo Maffione }
24232a7db7a6SVincenzo Maffione 
2424ce12afaaSMark Johnston static inline void
2425ce12afaaSMark Johnston nm_os_mbuf_reinit(struct mbuf *m)
2426ce12afaaSMark Johnston {
2427ce12afaaSMark Johnston 	void *buf;
24282a7db7a6SVincenzo Maffione 
2429ce12afaaSMark Johnston 	KASSERT((m->m_flags & M_EXT) != 0,
2430ce12afaaSMark Johnston 	    ("%s: mbuf %p has no external storage", __func__, m));
2431ce12afaaSMark Johnston 	KASSERT(m->m_ext.ext_size == MCLBYTES,
2432ce12afaaSMark Johnston 	    ("%s: mbuf %p has wrong external storage size %u", __func__, m,
2433ce12afaaSMark Johnston 	    m->m_ext.ext_size));
2434ce12afaaSMark Johnston 
2435ce12afaaSMark Johnston 	buf = m->m_ext.ext_buf;
2436ce12afaaSMark Johnston 	m_init(m, M_NOWAIT, MT_DATA, M_PKTHDR);
2437ce12afaaSMark Johnston 	m_extadd(m, buf, MCLBYTES, nm_generic_mbuf_dtor, NULL, NULL, 0,
2438ce12afaaSMark Johnston 	    EXT_NET_DRV);
24392a7db7a6SVincenzo Maffione }
24402a7db7a6SVincenzo Maffione 
24412a7db7a6SVincenzo Maffione #endif /* __FreeBSD__ */
24422a7db7a6SVincenzo Maffione 
2443253b2ec1SVincenzo Maffione struct nmreq_option * nmreq_getoption(struct nmreq_header *, uint16_t);
24442ff91c17SVincenzo Maffione 
2445b6e66be2SVincenzo Maffione int netmap_init_bridges(void);
2446b6e66be2SVincenzo Maffione void netmap_uninit_bridges(void);
2447b6e66be2SVincenzo Maffione 
2448b6e66be2SVincenzo Maffione /* Functions to read and write CSB fields from the kernel. */
2449b6e66be2SVincenzo Maffione #if defined (linux)
2450b6e66be2SVincenzo Maffione #define CSB_READ(csb, field, r) (get_user(r, &csb->field))
2451b6e66be2SVincenzo Maffione #define CSB_WRITE(csb, field, v) (put_user(v, &csb->field))
2452b6e66be2SVincenzo Maffione #else  /* ! linux */
2453*99efa2c8SMark Johnston #define CSB_READ(csb, field, r) do {				\
2454*99efa2c8SMark Johnston 	int32_t v __diagused;					\
2455*99efa2c8SMark Johnston 								\
2456*99efa2c8SMark Johnston 	v = fuword32(&csb->field);				\
2457*99efa2c8SMark Johnston 	KASSERT(v != -1, ("%s: fuword32 failed", __func__));	\
2458*99efa2c8SMark Johnston 	r = v;							\
2459*99efa2c8SMark Johnston } while (0)
2460*99efa2c8SMark Johnston #define CSB_WRITE(csb, field, v) do {				\
2461*99efa2c8SMark Johnston 	int error __diagused;					\
2462*99efa2c8SMark Johnston 								\
2463*99efa2c8SMark Johnston 	error = suword32(&csb->field, v);			\
2464*99efa2c8SMark Johnston 	KASSERT(error == 0, ("%s: suword32 failed", __func__));	\
2465*99efa2c8SMark Johnston } while (0)
2466b6e66be2SVincenzo Maffione #endif /* ! linux */
2467b6e66be2SVincenzo Maffione 
2468a6d768d8SVincenzo Maffione /* some macros that may not be defined */
2469a6d768d8SVincenzo Maffione #ifndef ETH_HLEN
2470a6d768d8SVincenzo Maffione #define ETH_HLEN 6
2471a6d768d8SVincenzo Maffione #endif
2472a6d768d8SVincenzo Maffione #ifndef ETH_FCS_LEN
2473a6d768d8SVincenzo Maffione #define ETH_FCS_LEN 4
2474a6d768d8SVincenzo Maffione #endif
2475a6d768d8SVincenzo Maffione #ifndef VLAN_HLEN
2476a6d768d8SVincenzo Maffione #define VLAN_HLEN 4
2477a6d768d8SVincenzo Maffione #endif
2478a6d768d8SVincenzo Maffione 
247968b8534bSLuigi Rizzo #endif /* _NET_NETMAP_KERN_H_ */
2480