xref: /freebsd/sys/dev/netmap/netmap_kern.h (revision 5faab7782202ab1e6815c5da956d8997f59aa105)
1718cf2ccSPedro F. Giffuni /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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  * $FreeBSD$
3268b8534bSLuigi Rizzo  *
3368b8534bSLuigi Rizzo  * The header contains the definitions of constants and function
3468b8534bSLuigi Rizzo  * prototypes used only in kernelspace.
3568b8534bSLuigi Rizzo  */
3668b8534bSLuigi Rizzo 
3768b8534bSLuigi Rizzo #ifndef _NET_NETMAP_KERN_H_
3868b8534bSLuigi Rizzo #define _NET_NETMAP_KERN_H_
3968b8534bSLuigi Rizzo 
40847bf383SLuigi Rizzo #if defined(linux)
41847bf383SLuigi Rizzo 
424f80b14cSVincenzo Maffione #if defined(CONFIG_NETMAP_EXTMEM)
434f80b14cSVincenzo Maffione #define WITH_EXTMEM
444f80b14cSVincenzo Maffione #endif
45847bf383SLuigi Rizzo #if  defined(CONFIG_NETMAP_VALE)
46847bf383SLuigi Rizzo #define WITH_VALE
47847bf383SLuigi Rizzo #endif
48847bf383SLuigi Rizzo #if defined(CONFIG_NETMAP_PIPE)
49847bf383SLuigi Rizzo #define WITH_PIPES
50847bf383SLuigi Rizzo #endif
51847bf383SLuigi Rizzo #if defined(CONFIG_NETMAP_MONITOR)
52847bf383SLuigi Rizzo #define WITH_MONITOR
53847bf383SLuigi Rizzo #endif
54847bf383SLuigi Rizzo #if defined(CONFIG_NETMAP_GENERIC)
55847bf383SLuigi Rizzo #define WITH_GENERIC
56847bf383SLuigi Rizzo #endif
57b6e66be2SVincenzo Maffione #if defined(CONFIG_NETMAP_PTNETMAP)
58b6e66be2SVincenzo Maffione #define WITH_PTNETMAP
59847bf383SLuigi Rizzo #endif
60c3e9b4dbSLuiz Otavio O Souza #if defined(CONFIG_NETMAP_SINK)
61c3e9b4dbSLuiz Otavio O Souza #define WITH_SINK
62c3e9b4dbSLuiz Otavio O Souza #endif
63b6e66be2SVincenzo Maffione #if defined(CONFIG_NETMAP_NULL)
64b6e66be2SVincenzo Maffione #define WITH_NMNULL
65b6e66be2SVincenzo Maffione #endif
66847bf383SLuigi Rizzo 
6737e3a6d3SLuigi Rizzo #elif defined (_WIN32)
68f9790aebSLuigi Rizzo #define WITH_VALE	// comment out to disable VALE support
69f0ea3689SLuigi Rizzo #define WITH_PIPES
704bf50f18SLuigi Rizzo #define WITH_MONITOR
71039dd540SLuigi Rizzo #define WITH_GENERIC
72b6e66be2SVincenzo Maffione #define WITH_NMNULL
73f9790aebSLuigi Rizzo 
7437e3a6d3SLuigi Rizzo #else	/* neither linux nor windows */
7537e3a6d3SLuigi Rizzo #define WITH_VALE	// comment out to disable VALE support
7637e3a6d3SLuigi Rizzo #define WITH_PIPES
7737e3a6d3SLuigi Rizzo #define WITH_MONITOR
7837e3a6d3SLuigi Rizzo #define WITH_GENERIC
79b6e66be2SVincenzo Maffione #define WITH_PTNETMAP	/* ptnetmap guest support */
802ff91c17SVincenzo Maffione #define WITH_EXTMEM
81b6e66be2SVincenzo Maffione #define WITH_NMNULL
82847bf383SLuigi Rizzo #endif
83847bf383SLuigi Rizzo 
841a26580eSLuigi Rizzo #if defined(__FreeBSD__)
85225d33ffSSean Bruno #include <sys/selinfo.h>
86d4b42e08SLuigi Rizzo 
87ce3ee1e7SLuigi Rizzo #define likely(x)	__builtin_expect((long)!!(x), 1L)
88ce3ee1e7SLuigi Rizzo #define unlikely(x)	__builtin_expect((long)!!(x), 0L)
8937e3a6d3SLuigi Rizzo #define __user
90f196ce38SLuigi Rizzo 
91847bf383SLuigi Rizzo #define	NM_LOCK_T	struct mtx	/* low level spinlock, used to protect queues */
92039dd540SLuigi Rizzo 
93847bf383SLuigi Rizzo #define NM_MTX_T	struct sx	/* OS-specific mutex (sleepable) */
94847bf383SLuigi Rizzo #define NM_MTX_INIT(m)		sx_init(&(m), #m)
95847bf383SLuigi Rizzo #define NM_MTX_DESTROY(m)	sx_destroy(&(m))
96847bf383SLuigi Rizzo #define NM_MTX_LOCK(m)		sx_xlock(&(m))
974f80b14cSVincenzo Maffione #define NM_MTX_SPINLOCK(m)	while (!sx_try_xlock(&(m))) ;
98847bf383SLuigi Rizzo #define NM_MTX_UNLOCK(m)	sx_xunlock(&(m))
99847bf383SLuigi Rizzo #define NM_MTX_ASSERT(m)	sx_assert(&(m), SA_XLOCKED)
100f9790aebSLuigi Rizzo 
1010e73f29aSLuigi Rizzo #define	NM_SELINFO_T	struct nm_selinfo
10237e3a6d3SLuigi Rizzo #define NM_SELRECORD_T	struct thread
1031a26580eSLuigi Rizzo #define	MBUF_LEN(m)	((m)->m_pkthdr.len)
10437e3a6d3SLuigi Rizzo #define MBUF_TXQ(m)	((m)->m_pkthdr.flowid)
10537e3a6d3SLuigi Rizzo #define MBUF_TRANSMIT(na, ifp, m)	((na)->if_transmit(ifp, m))
10637e3a6d3SLuigi Rizzo #define	GEN_TX_MBUF_IFP(m)	((m)->m_pkthdr.rcvif)
107d4b42e08SLuigi Rizzo 
1084f80b14cSVincenzo Maffione #define NM_ATOMIC_T	volatile int /* required by atomic/bitops.h */
109f9790aebSLuigi Rizzo /* atomic operations */
110f9790aebSLuigi Rizzo #include <machine/atomic.h>
111f9790aebSLuigi Rizzo #define NM_ATOMIC_TEST_AND_SET(p)       (!atomic_cmpset_acq_int((p), 0, 1))
112f9790aebSLuigi Rizzo #define NM_ATOMIC_CLEAR(p)              atomic_store_rel_int((p), 0)
113f9790aebSLuigi Rizzo 
1147f154b71SLuigi Rizzo #if __FreeBSD_version >= 1100030
1157f154b71SLuigi Rizzo #define	WNA(_ifp)	(_ifp)->if_netmap
1167f154b71SLuigi Rizzo #else /* older FreeBSD */
1177f154b71SLuigi Rizzo #define	WNA(_ifp)	(_ifp)->if_pspare[0]
1187f154b71SLuigi Rizzo #endif /* older FreeBSD */
1197f154b71SLuigi Rizzo 
12046aa1303SLuigi Rizzo #if __FreeBSD_version >= 1100005
12146aa1303SLuigi Rizzo struct netmap_adapter *netmap_getna(if_t ifp);
12246aa1303SLuigi Rizzo #endif
123f9790aebSLuigi Rizzo 
1244bf50f18SLuigi Rizzo #if __FreeBSD_version >= 1100027
12537e3a6d3SLuigi Rizzo #define MBUF_REFCNT(m)		((m)->m_ext.ext_count)
12637e3a6d3SLuigi Rizzo #define SET_MBUF_REFCNT(m, x)   (m)->m_ext.ext_count = x
1274bf50f18SLuigi Rizzo #else
12837e3a6d3SLuigi Rizzo #define MBUF_REFCNT(m)		((m)->m_ext.ref_cnt ? *((m)->m_ext.ref_cnt) : -1)
1294bf50f18SLuigi Rizzo #define SET_MBUF_REFCNT(m, x)   *((m)->m_ext.ref_cnt) = x
1304bf50f18SLuigi Rizzo #endif
1314bf50f18SLuigi Rizzo 
13237e3a6d3SLuigi Rizzo #define MBUF_QUEUED(m)		1
133f9790aebSLuigi Rizzo 
1340e73f29aSLuigi Rizzo struct nm_selinfo {
1350e73f29aSLuigi Rizzo 	struct selinfo si;
13619c4ec08SVincenzo Maffione 	struct taskqueue *ntfytq;
13719c4ec08SVincenzo Maffione 	struct task ntfytask;
1380e73f29aSLuigi Rizzo 	struct mtx m;
13919c4ec08SVincenzo Maffione 	char mtxname[32];
1400e73f29aSLuigi Rizzo };
1410e73f29aSLuigi Rizzo 
1420e73f29aSLuigi Rizzo 
143f9790aebSLuigi Rizzo struct hrtimer {
1444f80b14cSVincenzo Maffione     /* Not used in FreeBSD. */
145f9790aebSLuigi Rizzo };
1464f80b14cSVincenzo Maffione 
147847bf383SLuigi Rizzo #define NM_BNS_GET(b)
148847bf383SLuigi Rizzo #define NM_BNS_PUT(b)
149ce3ee1e7SLuigi Rizzo 
15064ae02c3SLuigi Rizzo #elif defined (linux)
151d4b42e08SLuigi Rizzo 
1522579e2d7SLuigi Rizzo #define	NM_LOCK_T	safe_spinlock_t	// see bsd_glue.h
1531a26580eSLuigi Rizzo #define	NM_SELINFO_T	wait_queue_head_t
1541a26580eSLuigi Rizzo #define	MBUF_LEN(m)	((m)->len)
15537e3a6d3SLuigi Rizzo #define MBUF_TRANSMIT(na, ifp, m)							\
15637e3a6d3SLuigi Rizzo 	({										\
15737e3a6d3SLuigi Rizzo 		/* Avoid infinite recursion with generic. */				\
15837e3a6d3SLuigi Rizzo 		m->priority = NM_MAGIC_PRIORITY_TX;					\
15937e3a6d3SLuigi Rizzo 		(((struct net_device_ops *)(na)->if_transmit)->ndo_start_xmit(m, ifp));	\
16037e3a6d3SLuigi Rizzo 		0;									\
16137e3a6d3SLuigi Rizzo 	})
16237e3a6d3SLuigi Rizzo 
16337e3a6d3SLuigi Rizzo /* See explanation in nm_os_generic_xmit_frame. */
16437e3a6d3SLuigi Rizzo #define	GEN_TX_MBUF_IFP(m)	((struct ifnet *)skb_shinfo(m)->destructor_arg)
165f196ce38SLuigi Rizzo 
166ce3ee1e7SLuigi Rizzo #define NM_ATOMIC_T	volatile long unsigned int
167ce3ee1e7SLuigi Rizzo 
168847bf383SLuigi Rizzo #define NM_MTX_T	struct mutex	/* OS-specific sleepable lock */
169847bf383SLuigi Rizzo #define NM_MTX_INIT(m)	mutex_init(&(m))
170847bf383SLuigi Rizzo #define NM_MTX_DESTROY(m)	do { (void)(m); } while (0)
171039dd540SLuigi Rizzo #define NM_MTX_LOCK(m)		mutex_lock(&(m))
172039dd540SLuigi Rizzo #define NM_MTX_UNLOCK(m)	mutex_unlock(&(m))
173847bf383SLuigi Rizzo #define NM_MTX_ASSERT(m)	mutex_is_locked(&(m))
174f9790aebSLuigi Rizzo 
175f196ce38SLuigi Rizzo #ifndef DEV_NETMAP
176f196ce38SLuigi Rizzo #define DEV_NETMAP
177ce3ee1e7SLuigi Rizzo #endif /* DEV_NETMAP */
178f196ce38SLuigi Rizzo 
179f196ce38SLuigi Rizzo #elif defined (__APPLE__)
180d4b42e08SLuigi Rizzo 
1818241616dSLuigi Rizzo #warning apple support is incomplete.
182f196ce38SLuigi Rizzo #define likely(x)	__builtin_expect(!!(x), 1)
183f196ce38SLuigi Rizzo #define unlikely(x)	__builtin_expect(!!(x), 0)
184f196ce38SLuigi Rizzo #define	NM_LOCK_T	IOLock *
185f196ce38SLuigi Rizzo #define	NM_SELINFO_T	struct selinfo
186f196ce38SLuigi Rizzo #define	MBUF_LEN(m)	((m)->m_pkthdr.len)
18737e3a6d3SLuigi Rizzo 
18837e3a6d3SLuigi Rizzo #elif defined (_WIN32)
18937e3a6d3SLuigi Rizzo #include "../../../WINDOWS/win_glue.h"
19037e3a6d3SLuigi Rizzo 
19137e3a6d3SLuigi Rizzo #define NM_SELRECORD_T		IO_STACK_LOCATION
19237e3a6d3SLuigi Rizzo #define NM_SELINFO_T		win_SELINFO		// see win_glue.h
19337e3a6d3SLuigi Rizzo #define NM_LOCK_T		win_spinlock_t	// see win_glue.h
19437e3a6d3SLuigi Rizzo #define NM_MTX_T		KGUARDED_MUTEX	/* OS-specific mutex (sleepable) */
19537e3a6d3SLuigi Rizzo 
19637e3a6d3SLuigi Rizzo #define NM_MTX_INIT(m)		KeInitializeGuardedMutex(&m);
19737e3a6d3SLuigi Rizzo #define NM_MTX_DESTROY(m)	do { (void)(m); } while (0)
19837e3a6d3SLuigi Rizzo #define NM_MTX_LOCK(m)		KeAcquireGuardedMutex(&(m))
19937e3a6d3SLuigi Rizzo #define NM_MTX_UNLOCK(m)	KeReleaseGuardedMutex(&(m))
20037e3a6d3SLuigi Rizzo #define NM_MTX_ASSERT(m)	assert(&m.Count>0)
20137e3a6d3SLuigi Rizzo 
20237e3a6d3SLuigi Rizzo //These linknames are for the NDIS driver
20337e3a6d3SLuigi Rizzo #define NETMAP_NDIS_LINKNAME_STRING             L"\\DosDevices\\NMAPNDIS"
20437e3a6d3SLuigi Rizzo #define NETMAP_NDIS_NTDEVICE_STRING             L"\\Device\\NMAPNDIS"
20537e3a6d3SLuigi Rizzo 
20637e3a6d3SLuigi Rizzo //Definition of internal driver-to-driver ioctl codes
20737e3a6d3SLuigi Rizzo #define NETMAP_KERNEL_XCHANGE_POINTERS		_IO('i', 180)
20837e3a6d3SLuigi Rizzo #define NETMAP_KERNEL_SEND_SHUTDOWN_SIGNAL	_IO_direct('i', 195)
20937e3a6d3SLuigi Rizzo 
21037e3a6d3SLuigi Rizzo typedef struct hrtimer{
21137e3a6d3SLuigi Rizzo 	KTIMER timer;
21237e3a6d3SLuigi Rizzo 	BOOLEAN active;
21337e3a6d3SLuigi Rizzo 	KDPC deferred_proc;
21437e3a6d3SLuigi Rizzo };
21537e3a6d3SLuigi Rizzo 
21637e3a6d3SLuigi Rizzo /* MSVC does not have likely/unlikely support */
21737e3a6d3SLuigi Rizzo #ifdef _MSC_VER
21837e3a6d3SLuigi Rizzo #define likely(x)	(x)
21937e3a6d3SLuigi Rizzo #define unlikely(x)	(x)
22037e3a6d3SLuigi Rizzo #else
22137e3a6d3SLuigi Rizzo #define likely(x)	__builtin_expect((long)!!(x), 1L)
22237e3a6d3SLuigi Rizzo #define unlikely(x)	__builtin_expect((long)!!(x), 0L)
22337e3a6d3SLuigi Rizzo #endif //_MSC_VER
224f196ce38SLuigi Rizzo 
2251a26580eSLuigi Rizzo #else
226d4b42e08SLuigi Rizzo 
2271a26580eSLuigi Rizzo #error unsupported platform
228d4b42e08SLuigi Rizzo 
229d4b42e08SLuigi Rizzo #endif /* end - platform-specific code */
2301a26580eSLuigi Rizzo 
23137e3a6d3SLuigi Rizzo #ifndef _WIN32 /* support for emulated sysctl */
23237e3a6d3SLuigi Rizzo #define SYSBEGIN(x)
23337e3a6d3SLuigi Rizzo #define SYSEND
23437e3a6d3SLuigi Rizzo #endif /* _WIN32 */
23537e3a6d3SLuigi Rizzo 
23637e3a6d3SLuigi Rizzo #define NM_ACCESS_ONCE(x)	(*(volatile __typeof__(x) *)&(x))
23737e3a6d3SLuigi Rizzo 
238847bf383SLuigi Rizzo #define	NMG_LOCK_T		NM_MTX_T
239847bf383SLuigi Rizzo #define	NMG_LOCK_INIT()		NM_MTX_INIT(netmap_global_lock)
240847bf383SLuigi Rizzo #define	NMG_LOCK_DESTROY()	NM_MTX_DESTROY(netmap_global_lock)
241847bf383SLuigi Rizzo #define	NMG_LOCK()		NM_MTX_LOCK(netmap_global_lock)
242847bf383SLuigi Rizzo #define	NMG_UNLOCK()		NM_MTX_UNLOCK(netmap_global_lock)
243847bf383SLuigi Rizzo #define	NMG_LOCK_ASSERT()	NM_MTX_ASSERT(netmap_global_lock)
244847bf383SLuigi Rizzo 
245c3e9b4dbSLuiz Otavio O Souza #if defined(__FreeBSD__)
246b6e66be2SVincenzo Maffione #define nm_prerr_int	printf
247b6e66be2SVincenzo Maffione #define nm_prinf_int	printf
248c3e9b4dbSLuiz Otavio O Souza #elif defined (_WIN32)
249b6e66be2SVincenzo Maffione #define nm_prerr_int	DbgPrint
250b6e66be2SVincenzo Maffione #define nm_prinf_int	DbgPrint
251c3e9b4dbSLuiz Otavio O Souza #elif defined(linux)
252b6e66be2SVincenzo Maffione #define nm_prerr_int(fmt, arg...)    printk(KERN_ERR fmt, ##arg)
253b6e66be2SVincenzo Maffione #define nm_prinf_int(fmt, arg...)    printk(KERN_INFO fmt, ##arg)
254c3e9b4dbSLuiz Otavio O Souza #endif
255c3e9b4dbSLuiz Otavio O Souza 
256b6e66be2SVincenzo Maffione #define nm_prinf(format, ...)					\
25768b8534bSLuigi Rizzo 	do {							\
25868b8534bSLuigi Rizzo 		struct timeval __xxts;				\
25968b8534bSLuigi Rizzo 		microtime(&__xxts);				\
260b6e66be2SVincenzo Maffione 		nm_prinf_int("%03d.%06d [%4d] %-25s " format "\n",\
26168b8534bSLuigi Rizzo 		(int)__xxts.tv_sec % 1000, (int)__xxts.tv_usec,	\
26217885a7bSLuigi Rizzo 		__LINE__, __FUNCTION__, ##__VA_ARGS__);		\
26368b8534bSLuigi Rizzo 	} while (0)
26468b8534bSLuigi Rizzo 
265b6e66be2SVincenzo Maffione #define nm_prerr(format, ...)					\
266b6e66be2SVincenzo Maffione 	do {							\
267b6e66be2SVincenzo Maffione 		struct timeval __xxts;				\
268b6e66be2SVincenzo Maffione 		microtime(&__xxts);				\
269b6e66be2SVincenzo Maffione 		nm_prerr_int("%03d.%06d [%4d] %-25s " format "\n",\
270b6e66be2SVincenzo Maffione 		(int)__xxts.tv_sec % 1000, (int)__xxts.tv_usec,	\
271b6e66be2SVincenzo Maffione 		__LINE__, __FUNCTION__, ##__VA_ARGS__);		\
272b6e66be2SVincenzo Maffione 	} while (0)
273b6e66be2SVincenzo Maffione 
274b6e66be2SVincenzo Maffione /* Disabled printf (used to be ND). */
275b6e66be2SVincenzo Maffione #define nm_prdis(format, ...)
276b6e66be2SVincenzo Maffione 
277b6e66be2SVincenzo Maffione /* Rate limited, lps indicates how many per second. */
278b6e66be2SVincenzo Maffione #define nm_prlim(lps, format, ...)				\
2798241616dSLuigi Rizzo 	do {							\
2808241616dSLuigi Rizzo 		static int t0, __cnt;				\
2818241616dSLuigi Rizzo 		if (t0 != time_second) {			\
2828241616dSLuigi Rizzo 			t0 = time_second;			\
2838241616dSLuigi Rizzo 			__cnt = 0;				\
2848241616dSLuigi Rizzo 		}						\
2858241616dSLuigi Rizzo 		if (__cnt++ < lps)				\
286b6e66be2SVincenzo Maffione 			nm_prinf(format, ##__VA_ARGS__);	\
2878241616dSLuigi Rizzo 	} while (0)
2888241616dSLuigi Rizzo 
289b6e66be2SVincenzo Maffione /* Old macros. */
290b6e66be2SVincenzo Maffione #define ND	nm_prdis
291b6e66be2SVincenzo Maffione #define D	nm_prerr
292b6e66be2SVincenzo Maffione #define RD	nm_prlim
293b6e66be2SVincenzo Maffione 
29468b8534bSLuigi Rizzo struct netmap_adapter;
295f18be576SLuigi Rizzo struct nm_bdg_fwd;
296f18be576SLuigi Rizzo struct nm_bridge;
297f18be576SLuigi Rizzo struct netmap_priv_d;
2982a7db7a6SVincenzo Maffione struct nm_bdg_args;
29968b8534bSLuigi Rizzo 
30037e3a6d3SLuigi Rizzo /* os-specific NM_SELINFO_T initialzation/destruction functions */
30119c4ec08SVincenzo Maffione int nm_os_selinfo_init(NM_SELINFO_T *, const char *name);
30237e3a6d3SLuigi Rizzo void nm_os_selinfo_uninit(NM_SELINFO_T *);
30337e3a6d3SLuigi Rizzo 
304ce3ee1e7SLuigi Rizzo const char *nm_dump_buf(char *p, int len, int lim, char *dst);
305ce3ee1e7SLuigi Rizzo 
30637e3a6d3SLuigi Rizzo void nm_os_selwakeup(NM_SELINFO_T *si);
30737e3a6d3SLuigi Rizzo void nm_os_selrecord(NM_SELRECORD_T *sr, NM_SELINFO_T *si);
30837e3a6d3SLuigi Rizzo 
30937e3a6d3SLuigi Rizzo int nm_os_ifnet_init(void);
31037e3a6d3SLuigi Rizzo void nm_os_ifnet_fini(void);
31137e3a6d3SLuigi Rizzo void nm_os_ifnet_lock(void);
31237e3a6d3SLuigi Rizzo void nm_os_ifnet_unlock(void);
31337e3a6d3SLuigi Rizzo 
3144f80b14cSVincenzo Maffione unsigned nm_os_ifnet_mtu(struct ifnet *ifp);
3154f80b14cSVincenzo Maffione 
31637e3a6d3SLuigi Rizzo void nm_os_get_module(void);
31737e3a6d3SLuigi Rizzo void nm_os_put_module(void);
31837e3a6d3SLuigi Rizzo 
31937e3a6d3SLuigi Rizzo void netmap_make_zombie(struct ifnet *);
32037e3a6d3SLuigi Rizzo void netmap_undo_zombie(struct ifnet *);
32137e3a6d3SLuigi Rizzo 
322c3e9b4dbSLuiz Otavio O Souza /* os independent alloc/realloc/free */
323c3e9b4dbSLuiz Otavio O Souza void *nm_os_malloc(size_t);
3244f80b14cSVincenzo Maffione void *nm_os_vmalloc(size_t);
325c3e9b4dbSLuiz Otavio O Souza void *nm_os_realloc(void *, size_t new_size, size_t old_size);
326c3e9b4dbSLuiz Otavio O Souza void nm_os_free(void *);
3274f80b14cSVincenzo Maffione void nm_os_vfree(void *);
328c3e9b4dbSLuiz Otavio O Souza 
3292a7db7a6SVincenzo Maffione /* os specific attach/detach enter/exit-netmap-mode routines */
3302a7db7a6SVincenzo Maffione void nm_os_onattach(struct ifnet *);
3312a7db7a6SVincenzo Maffione void nm_os_ondetach(struct ifnet *);
3322a7db7a6SVincenzo Maffione void nm_os_onenter(struct ifnet *);
3332a7db7a6SVincenzo Maffione void nm_os_onexit(struct ifnet *);
3342a7db7a6SVincenzo Maffione 
33537e3a6d3SLuigi Rizzo /* passes a packet up to the host stack.
33637e3a6d3SLuigi Rizzo  * If the packet is sent (or dropped) immediately it returns NULL,
33737e3a6d3SLuigi Rizzo  * otherwise it links the packet to prev and returns m.
33837e3a6d3SLuigi Rizzo  * In this case, a final call with m=NULL and prev != NULL will send up
33937e3a6d3SLuigi Rizzo  * the entire chain to the host stack.
34037e3a6d3SLuigi Rizzo  */
34137e3a6d3SLuigi Rizzo void *nm_os_send_up(struct ifnet *, struct mbuf *m, struct mbuf *prev);
34237e3a6d3SLuigi Rizzo 
3432a7db7a6SVincenzo Maffione int nm_os_mbuf_has_seg_offld(struct mbuf *m);
3442a7db7a6SVincenzo Maffione int nm_os_mbuf_has_csum_offld(struct mbuf *m);
34537e3a6d3SLuigi Rizzo 
346f9790aebSLuigi Rizzo #include "netmap_mbq.h"
347f9790aebSLuigi Rizzo 
348f9790aebSLuigi Rizzo extern NMG_LOCK_T	netmap_global_lock;
349f9790aebSLuigi Rizzo 
350847bf383SLuigi Rizzo enum txrx { NR_RX = 0, NR_TX = 1, NR_TXRX };
351847bf383SLuigi Rizzo 
352847bf383SLuigi Rizzo static __inline const char*
353847bf383SLuigi Rizzo nm_txrx2str(enum txrx t)
354847bf383SLuigi Rizzo {
355847bf383SLuigi Rizzo 	return (t== NR_RX ? "RX" : "TX");
356847bf383SLuigi Rizzo }
357847bf383SLuigi Rizzo 
358847bf383SLuigi Rizzo static __inline enum txrx
359847bf383SLuigi Rizzo nm_txrx_swap(enum txrx t)
360847bf383SLuigi Rizzo {
361847bf383SLuigi Rizzo 	return (t== NR_RX ? NR_TX : NR_RX);
362847bf383SLuigi Rizzo }
363847bf383SLuigi Rizzo 
364847bf383SLuigi Rizzo #define for_rx_tx(t)	for ((t) = 0; (t) < NR_TXRX; (t)++)
365847bf383SLuigi Rizzo 
366c3e9b4dbSLuiz Otavio O Souza #ifdef WITH_MONITOR
367c3e9b4dbSLuiz Otavio O Souza struct netmap_zmon_list {
368c3e9b4dbSLuiz Otavio O Souza 	struct netmap_kring *next;
369c3e9b4dbSLuiz Otavio O Souza 	struct netmap_kring *prev;
370c3e9b4dbSLuiz Otavio O Souza };
371c3e9b4dbSLuiz Otavio O Souza #endif /* WITH_MONITOR */
372847bf383SLuigi Rizzo 
37368b8534bSLuigi Rizzo /*
37464ae02c3SLuigi Rizzo  * private, kernel view of a ring. Keeps track of the status of
37564ae02c3SLuigi Rizzo  * a ring across system calls.
37664ae02c3SLuigi Rizzo  *
37764ae02c3SLuigi Rizzo  *	nr_hwcur	index of the next buffer to refill.
37817885a7bSLuigi Rizzo  *			It corresponds to ring->head
37917885a7bSLuigi Rizzo  *			at the time the system call returns.
38064ae02c3SLuigi Rizzo  *
38117885a7bSLuigi Rizzo  *	nr_hwtail	index of the first buffer owned by the kernel.
38217885a7bSLuigi Rizzo  *			On RX, hwcur->hwtail are receive buffers
38317885a7bSLuigi Rizzo  *			not yet released. hwcur is advanced following
38417885a7bSLuigi Rizzo  *			ring->head, hwtail is advanced on incoming packets,
38517885a7bSLuigi Rizzo  *			and a wakeup is generated when hwtail passes ring->cur
38617885a7bSLuigi Rizzo  *			    On TX, hwcur->rcur have been filled by the sender
38717885a7bSLuigi Rizzo  *			but not sent yet to the NIC; rcur->hwtail are available
38817885a7bSLuigi Rizzo  *			for new transmissions, and hwtail->hwcur-1 are pending
38917885a7bSLuigi Rizzo  *			transmissions not yet acknowledged.
39068b8534bSLuigi Rizzo  *
3911a26580eSLuigi Rizzo  * The indexes in the NIC and netmap rings are offset by nkr_hwofs slots.
39268b8534bSLuigi Rizzo  * This is so that, on a reset, buffers owned by userspace are not
39368b8534bSLuigi Rizzo  * modified by the kernel. In particular:
39417885a7bSLuigi Rizzo  * RX rings: the next empty buffer (hwtail + hwofs) coincides with
39568b8534bSLuigi Rizzo  * 	the next empty buffer as known by the hardware (next_to_check or so).
39668b8534bSLuigi Rizzo  * TX rings: hwcur + hwofs coincides with next_to_send
3971dce924dSLuigi Rizzo  *
398ce3ee1e7SLuigi Rizzo  * The following fields are used to implement lock-free copy of packets
399ce3ee1e7SLuigi Rizzo  * from input to output ports in VALE switch:
400ce3ee1e7SLuigi Rizzo  *	nkr_hwlease	buffer after the last one being copied.
401ce3ee1e7SLuigi Rizzo  *			A writer in nm_bdg_flush reserves N buffers
402ce3ee1e7SLuigi Rizzo  *			from nr_hwlease, advances it, then does the
403ce3ee1e7SLuigi Rizzo  *			copy outside the lock.
404ce3ee1e7SLuigi Rizzo  *			In RX rings (used for VALE ports),
40517885a7bSLuigi Rizzo  *			nkr_hwtail <= nkr_hwlease < nkr_hwcur+N-1
406ce3ee1e7SLuigi Rizzo  *			In TX rings (used for NIC or host stack ports)
40717885a7bSLuigi Rizzo  *			nkr_hwcur <= nkr_hwlease < nkr_hwtail
408ce3ee1e7SLuigi Rizzo  *	nkr_leases	array of nkr_num_slots where writers can report
409ce3ee1e7SLuigi Rizzo  *			completion of their block. NR_NOSLOT (~0) indicates
410ce3ee1e7SLuigi Rizzo  *			that the writer has not finished yet
411ce3ee1e7SLuigi Rizzo  *	nkr_lease_idx	index of next free slot in nr_leases, to be assigned
412ce3ee1e7SLuigi Rizzo  *
413ce3ee1e7SLuigi Rizzo  * The kring is manipulated by txsync/rxsync and generic netmap function.
41417885a7bSLuigi Rizzo  *
41517885a7bSLuigi Rizzo  * Concurrent rxsync or txsync on the same ring are prevented through
41689cc2556SLuigi Rizzo  * by nm_kr_(try)lock() which in turn uses nr_busy. This is all we need
41717885a7bSLuigi Rizzo  * for NIC rings, and for TX rings attached to the host stack.
41817885a7bSLuigi Rizzo  *
41917885a7bSLuigi Rizzo  * RX rings attached to the host stack use an mbq (rx_queue) on both
42017885a7bSLuigi Rizzo  * rxsync_from_host() and netmap_transmit(). The mbq is protected
42117885a7bSLuigi Rizzo  * by its internal lock.
42217885a7bSLuigi Rizzo  *
4234bf50f18SLuigi Rizzo  * RX rings attached to the VALE switch are accessed by both senders
42417885a7bSLuigi Rizzo  * and receiver. They are protected through the q_lock on the RX ring.
42568b8534bSLuigi Rizzo  */
42668b8534bSLuigi Rizzo struct netmap_kring {
42768b8534bSLuigi Rizzo 	struct netmap_ring	*ring;
42817885a7bSLuigi Rizzo 
4292ff91c17SVincenzo Maffione 	uint32_t	nr_hwcur;  /* should be nr_hwhead */
43017885a7bSLuigi Rizzo 	uint32_t	nr_hwtail;
43117885a7bSLuigi Rizzo 
43217885a7bSLuigi Rizzo 	/*
43317885a7bSLuigi Rizzo 	 * Copies of values in user rings, so we do not need to look
43417885a7bSLuigi Rizzo 	 * at the ring (which could be modified). These are set in the
43517885a7bSLuigi Rizzo 	 * *sync_prologue()/finalize() routines.
43617885a7bSLuigi Rizzo 	 */
43717885a7bSLuigi Rizzo 	uint32_t	rhead;
43817885a7bSLuigi Rizzo 	uint32_t	rcur;
43917885a7bSLuigi Rizzo 	uint32_t	rtail;
44017885a7bSLuigi Rizzo 
441ce3ee1e7SLuigi Rizzo 	uint32_t	nr_kflags;	/* private driver flags */
4422157a17cSLuigi Rizzo #define NKR_PENDINTR	0x1		// Pending interrupt.
443847bf383SLuigi Rizzo #define NKR_EXCLUSIVE	0x2		/* exclusive binding */
44437e3a6d3SLuigi Rizzo #define NKR_FORWARD	0x4		/* (host ring only) there are
44537e3a6d3SLuigi Rizzo 					   packets to forward
44637e3a6d3SLuigi Rizzo 					 */
44737e3a6d3SLuigi Rizzo #define NKR_NEEDRING	0x8		/* ring needed even if users==0
44837e3a6d3SLuigi Rizzo 					 * (used internally by pipes and
44937e3a6d3SLuigi Rizzo 					 *  by ptnetmap host ports)
45037e3a6d3SLuigi Rizzo 					 */
4514f80b14cSVincenzo Maffione #define NKR_NOINTR      0x10            /* don't use interrupts on this ring */
4522ff91c17SVincenzo Maffione #define NKR_FAKERING	0x20		/* don't allocate/free buffers */
45337e3a6d3SLuigi Rizzo 
45437e3a6d3SLuigi Rizzo 	uint32_t	nr_mode;
45537e3a6d3SLuigi Rizzo 	uint32_t	nr_pending_mode;
45637e3a6d3SLuigi Rizzo #define NKR_NETMAP_OFF	0x0
45737e3a6d3SLuigi Rizzo #define NKR_NETMAP_ON	0x1
45837e3a6d3SLuigi Rizzo 
459ce3ee1e7SLuigi Rizzo 	uint32_t	nkr_num_slots;
46017885a7bSLuigi Rizzo 
46117885a7bSLuigi Rizzo 	/*
46217885a7bSLuigi Rizzo 	 * On a NIC reset, the NIC ring indexes may be reset but the
46317885a7bSLuigi Rizzo 	 * indexes in the netmap rings remain the same. nkr_hwofs
46417885a7bSLuigi Rizzo 	 * keeps track of the offset between the two.
46517885a7bSLuigi Rizzo 	 */
46617885a7bSLuigi Rizzo 	int32_t		nkr_hwofs;
46768b8534bSLuigi Rizzo 
46817885a7bSLuigi Rizzo 	/* last_reclaim is opaque marker to help reduce the frequency
46917885a7bSLuigi Rizzo 	 * of operations such as reclaiming tx buffers. A possible use
47017885a7bSLuigi Rizzo 	 * is set it to ticks and do the reclaim only once per tick.
47117885a7bSLuigi Rizzo 	 */
47217885a7bSLuigi Rizzo 	uint64_t	last_reclaim;
47317885a7bSLuigi Rizzo 
474ce3ee1e7SLuigi Rizzo 
4751a26580eSLuigi Rizzo 	NM_SELINFO_T	si;		/* poll/select wait queue */
476ce3ee1e7SLuigi Rizzo 	NM_LOCK_T	q_lock;		/* protects kring and ring. */
477ce3ee1e7SLuigi Rizzo 	NM_ATOMIC_T	nr_busy;	/* prevent concurrent syscalls */
478ce3ee1e7SLuigi Rizzo 
4792ff91c17SVincenzo Maffione 	/* the adapter the owns this kring */
48017885a7bSLuigi Rizzo 	struct netmap_adapter *na;
48117885a7bSLuigi Rizzo 
4822ff91c17SVincenzo Maffione 	/* the adapter that wants to be notified when this kring has
4832ff91c17SVincenzo Maffione 	 * new slots avaialable. This is usually the same as the above,
4842ff91c17SVincenzo Maffione 	 * but wrappers may let it point to themselves
4852ff91c17SVincenzo Maffione 	 */
4862ff91c17SVincenzo Maffione 	struct netmap_adapter *notify_na;
4872ff91c17SVincenzo Maffione 
4886435a0dcSLuigi Rizzo 	/* The following fields are for VALE switch support */
48917885a7bSLuigi Rizzo 	struct nm_bdg_fwd *nkr_ft;
49017885a7bSLuigi Rizzo 	uint32_t	*nkr_leases;
49117885a7bSLuigi Rizzo #define NR_NOSLOT	((uint32_t)~0)	/* used in nkr_*lease* */
49217885a7bSLuigi Rizzo 	uint32_t	nkr_hwlease;
49317885a7bSLuigi Rizzo 	uint32_t	nkr_lease_idx;
49417885a7bSLuigi Rizzo 
4954bf50f18SLuigi Rizzo 	/* while nkr_stopped is set, no new [tr]xsync operations can
4964bf50f18SLuigi Rizzo 	 * be started on this kring.
4974bf50f18SLuigi Rizzo 	 * This is used by netmap_disable_all_rings()
4984bf50f18SLuigi Rizzo 	 * to find a synchronization point where critical data
4994bf50f18SLuigi Rizzo 	 * structures pointed to by the kring can be added or removed
5004bf50f18SLuigi Rizzo 	 */
5014bf50f18SLuigi Rizzo 	volatile int nkr_stopped;
502f9790aebSLuigi Rizzo 
503f0ea3689SLuigi Rizzo 	/* Support for adapters without native netmap support.
504f9790aebSLuigi Rizzo 	 * On tx rings we preallocate an array of tx buffers
505f9790aebSLuigi Rizzo 	 * (same size as the netmap ring), on rx rings we
506f0ea3689SLuigi Rizzo 	 * store incoming mbufs in a queue that is drained by
507f0ea3689SLuigi Rizzo 	 * a rxsync.
508f9790aebSLuigi Rizzo 	 */
509f9790aebSLuigi Rizzo 	struct mbuf	**tx_pool;
51037e3a6d3SLuigi Rizzo 	struct mbuf	*tx_event;	/* TX event used as a notification */
51137e3a6d3SLuigi Rizzo 	NM_LOCK_T	tx_event_lock;	/* protects the tx_event mbuf */
51217885a7bSLuigi Rizzo 	struct mbq	rx_queue;       /* intercepted rx mbufs. */
51317885a7bSLuigi Rizzo 
514847bf383SLuigi Rizzo 	uint32_t	users;		/* existing bindings for this ring */
515847bf383SLuigi Rizzo 
51637e3a6d3SLuigi Rizzo 	uint32_t	ring_id;	/* kring identifier */
517847bf383SLuigi Rizzo 	enum txrx	tx;		/* kind of ring (tx or rx) */
51817885a7bSLuigi Rizzo 	char name[64];			/* diagnostic */
519f9790aebSLuigi Rizzo 
5204bf50f18SLuigi Rizzo 	/* [tx]sync callback for this kring.
5214bf50f18SLuigi Rizzo 	 * The default nm_kring_create callback (netmap_krings_create)
5224bf50f18SLuigi Rizzo 	 * sets the nm_sync callback of each hardware tx(rx) kring to
5234bf50f18SLuigi Rizzo 	 * the corresponding nm_txsync(nm_rxsync) taken from the
5244bf50f18SLuigi Rizzo 	 * netmap_adapter; moreover, it sets the sync callback
5254bf50f18SLuigi Rizzo 	 * of the host tx(rx) ring to netmap_txsync_to_host
5264bf50f18SLuigi Rizzo 	 * (netmap_rxsync_from_host).
5274bf50f18SLuigi Rizzo 	 *
5284bf50f18SLuigi Rizzo 	 * Overrides: the above configuration is not changed by
5294bf50f18SLuigi Rizzo 	 * any of the nm_krings_create callbacks.
5304bf50f18SLuigi Rizzo 	 */
531f0ea3689SLuigi Rizzo 	int (*nm_sync)(struct netmap_kring *kring, int flags);
532847bf383SLuigi Rizzo 	int (*nm_notify)(struct netmap_kring *kring, int flags);
533f0ea3689SLuigi Rizzo 
534f0ea3689SLuigi Rizzo #ifdef WITH_PIPES
5354bf50f18SLuigi Rizzo 	struct netmap_kring *pipe;	/* if this is a pipe ring,
5364bf50f18SLuigi Rizzo 					 * pointer to the other end
5374bf50f18SLuigi Rizzo 					 */
5382a7db7a6SVincenzo Maffione 	uint32_t pipe_tail;		/* hwtail updated by the other end */
539f0ea3689SLuigi Rizzo #endif /* WITH_PIPES */
540f0ea3689SLuigi Rizzo 
541847bf383SLuigi Rizzo 	int (*save_notify)(struct netmap_kring *kring, int flags);
542847bf383SLuigi Rizzo 
5434bf50f18SLuigi Rizzo #ifdef WITH_MONITOR
544847bf383SLuigi Rizzo 	/* array of krings that are monitoring this kring */
545847bf383SLuigi Rizzo 	struct netmap_kring **monitors;
546847bf383SLuigi Rizzo 	uint32_t max_monitors; /* current size of the monitors array */
547847bf383SLuigi Rizzo 	uint32_t n_monitors;	/* next unused entry in the monitor array */
548c3e9b4dbSLuiz Otavio O Souza 	uint32_t mon_pos[NR_TXRX]; /* index of this ring in the monitored ring array */
549c3e9b4dbSLuiz Otavio O Souza 	uint32_t mon_tail;  /* last seen slot on rx */
550c3e9b4dbSLuiz Otavio O Souza 
551c3e9b4dbSLuiz Otavio O Souza 	/* circular list of zero-copy monitors */
552c3e9b4dbSLuiz Otavio O Souza 	struct netmap_zmon_list zmon_list[NR_TXRX];
553c3e9b4dbSLuiz Otavio O Souza 
5544bf50f18SLuigi Rizzo 	/*
555847bf383SLuigi Rizzo 	 * Monitors work by intercepting the sync and notify callbacks of the
556847bf383SLuigi Rizzo 	 * monitored krings. This is implemented by replacing the pointers
557847bf383SLuigi Rizzo 	 * above and saving the previous ones in mon_* pointers below
5584bf50f18SLuigi Rizzo 	 */
559847bf383SLuigi Rizzo 	int (*mon_sync)(struct netmap_kring *kring, int flags);
560847bf383SLuigi Rizzo 	int (*mon_notify)(struct netmap_kring *kring, int flags);
561847bf383SLuigi Rizzo 
5624bf50f18SLuigi Rizzo #endif
56337e3a6d3SLuigi Rizzo }
56437e3a6d3SLuigi Rizzo #ifdef _WIN32
56537e3a6d3SLuigi Rizzo __declspec(align(64));
56637e3a6d3SLuigi Rizzo #else
56737e3a6d3SLuigi Rizzo __attribute__((__aligned__(64)));
56837e3a6d3SLuigi Rizzo #endif
56968b8534bSLuigi Rizzo 
57037e3a6d3SLuigi Rizzo /* return 1 iff the kring needs to be turned on */
57137e3a6d3SLuigi Rizzo static inline int
57237e3a6d3SLuigi Rizzo nm_kring_pending_on(struct netmap_kring *kring)
57337e3a6d3SLuigi Rizzo {
57437e3a6d3SLuigi Rizzo 	return kring->nr_pending_mode == NKR_NETMAP_ON &&
57537e3a6d3SLuigi Rizzo 	       kring->nr_mode == NKR_NETMAP_OFF;
57637e3a6d3SLuigi Rizzo }
57737e3a6d3SLuigi Rizzo 
57837e3a6d3SLuigi Rizzo /* return 1 iff the kring needs to be turned off */
57937e3a6d3SLuigi Rizzo static inline int
58037e3a6d3SLuigi Rizzo nm_kring_pending_off(struct netmap_kring *kring)
58137e3a6d3SLuigi Rizzo {
58237e3a6d3SLuigi Rizzo 	return kring->nr_pending_mode == NKR_NETMAP_OFF &&
58337e3a6d3SLuigi Rizzo 	       kring->nr_mode == NKR_NETMAP_ON;
58437e3a6d3SLuigi Rizzo }
585ce3ee1e7SLuigi Rizzo 
586ce3ee1e7SLuigi Rizzo /* return the next index, with wraparound */
587ce3ee1e7SLuigi Rizzo static inline uint32_t
588ce3ee1e7SLuigi Rizzo nm_next(uint32_t i, uint32_t lim)
589ce3ee1e7SLuigi Rizzo {
590ce3ee1e7SLuigi Rizzo 	return unlikely (i == lim) ? 0 : i + 1;
591ce3ee1e7SLuigi Rizzo }
592ce3ee1e7SLuigi Rizzo 
59317885a7bSLuigi Rizzo 
59417885a7bSLuigi Rizzo /* return the previous index, with wraparound */
59517885a7bSLuigi Rizzo static inline uint32_t
59617885a7bSLuigi Rizzo nm_prev(uint32_t i, uint32_t lim)
59717885a7bSLuigi Rizzo {
59817885a7bSLuigi Rizzo 	return unlikely (i == 0) ? lim : i - 1;
59917885a7bSLuigi Rizzo }
60017885a7bSLuigi Rizzo 
60117885a7bSLuigi Rizzo 
602ce3ee1e7SLuigi Rizzo /*
603ce3ee1e7SLuigi Rizzo  *
604ce3ee1e7SLuigi Rizzo  * Here is the layout for the Rx and Tx rings.
605ce3ee1e7SLuigi Rizzo 
606ce3ee1e7SLuigi Rizzo        RxRING                            TxRING
607ce3ee1e7SLuigi Rizzo 
608ce3ee1e7SLuigi Rizzo       +-----------------+            +-----------------+
609ce3ee1e7SLuigi Rizzo       |                 |            |                 |
6104f80b14cSVincenzo Maffione       |      free       |            |      free       |
611ce3ee1e7SLuigi Rizzo       +-----------------+            +-----------------+
61217885a7bSLuigi Rizzo head->| owned by user   |<-hwcur     | not sent to nic |<-hwcur
61317885a7bSLuigi Rizzo       |                 |            | yet             |
61417885a7bSLuigi Rizzo       +-----------------+            |                 |
61517885a7bSLuigi Rizzo  cur->| available to    |            |                 |
61617885a7bSLuigi Rizzo       | user, not read  |            +-----------------+
61717885a7bSLuigi Rizzo       | yet             |       cur->| (being          |
61817885a7bSLuigi Rizzo       |                 |            |  prepared)      |
619ce3ee1e7SLuigi Rizzo       |                 |            |                 |
62017885a7bSLuigi Rizzo       +-----------------+            +     ------      +
62117885a7bSLuigi Rizzo tail->|                 |<-hwtail    |                 |<-hwlease
62217885a7bSLuigi Rizzo       | (being          | ...        |                 | ...
62317885a7bSLuigi Rizzo       |  prepared)      | ...        |                 | ...
62417885a7bSLuigi Rizzo       +-----------------+ ...        |                 | ...
62517885a7bSLuigi Rizzo       |                 |<-hwlease   +-----------------+
62617885a7bSLuigi Rizzo       |                 |      tail->|                 |<-hwtail
627ce3ee1e7SLuigi Rizzo       |                 |            |                 |
628ce3ee1e7SLuigi Rizzo       |                 |            |                 |
629ce3ee1e7SLuigi Rizzo       |                 |            |                 |
630ce3ee1e7SLuigi Rizzo       +-----------------+            +-----------------+
631ce3ee1e7SLuigi Rizzo 
63217885a7bSLuigi Rizzo  * The cur/tail (user view) and hwcur/hwtail (kernel view)
633ce3ee1e7SLuigi Rizzo  * are used in the normal operation of the card.
634ce3ee1e7SLuigi Rizzo  *
635ce3ee1e7SLuigi Rizzo  * When a ring is the output of a switch port (Rx ring for
636ce3ee1e7SLuigi Rizzo  * a VALE port, Tx ring for the host stack or NIC), slots
637ce3ee1e7SLuigi Rizzo  * are reserved in blocks through 'hwlease' which points
638ce3ee1e7SLuigi Rizzo  * to the next unused slot.
63917885a7bSLuigi Rizzo  * On an Rx ring, hwlease is always after hwtail,
64017885a7bSLuigi Rizzo  * and completions cause hwtail to advance.
64117885a7bSLuigi Rizzo  * On a Tx ring, hwlease is always between cur and hwtail,
642ce3ee1e7SLuigi Rizzo  * and completions cause cur to advance.
643ce3ee1e7SLuigi Rizzo  *
644ce3ee1e7SLuigi Rizzo  * nm_kr_space() returns the maximum number of slots that
645ce3ee1e7SLuigi Rizzo  * can be assigned.
646ce3ee1e7SLuigi Rizzo  * nm_kr_lease() reserves the required number of buffers,
647ce3ee1e7SLuigi Rizzo  *    advances nkr_hwlease and also returns an entry in
648ce3ee1e7SLuigi Rizzo  *    a circular array where completions should be reported.
649ce3ee1e7SLuigi Rizzo  */
650ce3ee1e7SLuigi Rizzo 
6514f80b14cSVincenzo Maffione struct lut_entry;
6524f80b14cSVincenzo Maffione #ifdef __FreeBSD__
6534f80b14cSVincenzo Maffione #define plut_entry lut_entry
6544f80b14cSVincenzo Maffione #endif
655ce3ee1e7SLuigi Rizzo 
656847bf383SLuigi Rizzo struct netmap_lut {
657847bf383SLuigi Rizzo 	struct lut_entry *lut;
6584f80b14cSVincenzo Maffione 	struct plut_entry *plut;
659847bf383SLuigi Rizzo 	uint32_t objtotal;	/* max buffer index */
660847bf383SLuigi Rizzo 	uint32_t objsize;	/* buffer size */
661847bf383SLuigi Rizzo };
662ce3ee1e7SLuigi Rizzo 
6634bf50f18SLuigi Rizzo struct netmap_vp_adapter; // forward
6642a7db7a6SVincenzo Maffione struct nm_bridge;
6654bf50f18SLuigi Rizzo 
6662ff91c17SVincenzo Maffione /* Struct to be filled by nm_config callbacks. */
6672ff91c17SVincenzo Maffione struct nm_config_info {
6682ff91c17SVincenzo Maffione 	unsigned num_tx_rings;
6692ff91c17SVincenzo Maffione 	unsigned num_rx_rings;
6702ff91c17SVincenzo Maffione 	unsigned num_tx_descs;
6712ff91c17SVincenzo Maffione 	unsigned num_rx_descs;
6722ff91c17SVincenzo Maffione 	unsigned rx_buf_maxsize;
6732ff91c17SVincenzo Maffione };
6742ff91c17SVincenzo Maffione 
67568b8534bSLuigi Rizzo /*
6762a7db7a6SVincenzo Maffione  * default type for the magic field.
6772a7db7a6SVincenzo Maffione  * May be overriden in glue code.
6782a7db7a6SVincenzo Maffione  */
6792a7db7a6SVincenzo Maffione #ifndef NM_OS_MAGIC
6802a7db7a6SVincenzo Maffione #define NM_OS_MAGIC uint32_t
6812a7db7a6SVincenzo Maffione #endif /* !NM_OS_MAGIC */
6822a7db7a6SVincenzo Maffione 
6832a7db7a6SVincenzo Maffione /*
684f9790aebSLuigi Rizzo  * The "struct netmap_adapter" extends the "struct adapter"
685f9790aebSLuigi Rizzo  * (or equivalent) device descriptor.
686f9790aebSLuigi Rizzo  * It contains all base fields needed to support netmap operation.
687f9790aebSLuigi Rizzo  * There are in fact different types of netmap adapters
688f9790aebSLuigi Rizzo  * (native, generic, VALE switch...) so a netmap_adapter is
689f9790aebSLuigi Rizzo  * just the first field in the derived type.
69068b8534bSLuigi Rizzo  */
69168b8534bSLuigi Rizzo struct netmap_adapter {
6928241616dSLuigi Rizzo 	/*
6938241616dSLuigi Rizzo 	 * On linux we do not have a good way to tell if an interface
694f9790aebSLuigi Rizzo 	 * is netmap-capable. So we always use the following trick:
6958241616dSLuigi Rizzo 	 * NA(ifp) points here, and the first entry (which hopefully
6968241616dSLuigi Rizzo 	 * always exists and is at least 32 bits) contains a magic
6978241616dSLuigi Rizzo 	 * value which we can use to detect that the interface is good.
6988241616dSLuigi Rizzo 	 */
6992a7db7a6SVincenzo Maffione 	NM_OS_MAGIC magic;
700f9790aebSLuigi Rizzo 	uint32_t na_flags;	/* enabled, and other flags */
7018241616dSLuigi Rizzo #define NAF_SKIP_INTR	1	/* use the regular interrupt handler.
7028241616dSLuigi Rizzo 				 * useful during initialization
7038241616dSLuigi Rizzo 				 */
704f18be576SLuigi Rizzo #define NAF_SW_ONLY	2	/* forward packets only to sw adapter */
705ce3ee1e7SLuigi Rizzo #define NAF_BDG_MAYSLEEP 4	/* the bridge is allowed to sleep when
706ce3ee1e7SLuigi Rizzo 				 * forwarding packets coming from this
707ce3ee1e7SLuigi Rizzo 				 * interface
708ce3ee1e7SLuigi Rizzo 				 */
709847bf383SLuigi Rizzo #define NAF_MEM_OWNER	8	/* the adapter uses its own memory area
710847bf383SLuigi Rizzo 				 * that cannot be changed
711ce3ee1e7SLuigi Rizzo 				 */
712847bf383SLuigi Rizzo #define NAF_NATIVE      16      /* the adapter is native.
71385fe4e7cSLuigi Rizzo 				 * Virtual ports (non persistent vale ports,
71485fe4e7cSLuigi Rizzo 				 * pipes, monitors...) should never use
71585fe4e7cSLuigi Rizzo 				 * this flag.
716f9790aebSLuigi Rizzo 				 */
717f9790aebSLuigi Rizzo #define	NAF_NETMAP_ON	32	/* netmap is active (either native or
7184bf50f18SLuigi Rizzo 				 * emulated). Where possible (e.g. FreeBSD)
719f9790aebSLuigi Rizzo 				 * IFCAP_NETMAP also mirrors this flag.
720f9790aebSLuigi Rizzo 				 */
721f0ea3689SLuigi Rizzo #define NAF_HOST_RINGS  64	/* the adapter supports the host rings */
7224bf50f18SLuigi Rizzo #define NAF_FORCE_NATIVE 128	/* the adapter is always NATIVE */
723b6e66be2SVincenzo Maffione /* free */
7244f80b14cSVincenzo Maffione #define NAF_MOREFRAG	512	/* the adapter supports NS_MOREFRAG */
72537e3a6d3SLuigi Rizzo #define NAF_ZOMBIE	(1U<<30) /* the nic driver has been unloaded */
7264bf50f18SLuigi Rizzo #define	NAF_BUSY	(1U<<31) /* the adapter is used internally and
7274bf50f18SLuigi Rizzo 				  * cannot be registered from userspace
7284bf50f18SLuigi Rizzo 				  */
729f9790aebSLuigi Rizzo 	int active_fds; /* number of user-space descriptors using this
73068b8534bSLuigi Rizzo 			 interface, which is equal to the number of
73168b8534bSLuigi Rizzo 			 struct netmap_if objs in the mapped region. */
73268b8534bSLuigi Rizzo 
73324e57ec9SEd Maste 	u_int num_rx_rings; /* number of adapter receive rings */
73424e57ec9SEd Maste 	u_int num_tx_rings; /* number of adapter transmit rings */
7352a7db7a6SVincenzo Maffione 	u_int num_host_rx_rings; /* number of host receive rings */
7362a7db7a6SVincenzo Maffione 	u_int num_host_tx_rings; /* number of host transmit rings */
73768b8534bSLuigi Rizzo 
73868b8534bSLuigi Rizzo 	u_int num_tx_desc;  /* number of descriptor in each queue */
73968b8534bSLuigi Rizzo 	u_int num_rx_desc;
74068b8534bSLuigi Rizzo 
741b6e66be2SVincenzo Maffione 	/* tx_rings and rx_rings are private but allocated as a
742b6e66be2SVincenzo Maffione 	 * contiguous chunk of memory. Each array has N+K entries,
743b6e66be2SVincenzo Maffione 	 * N for the hardware rings and K for the host rings.
74468b8534bSLuigi Rizzo 	 */
7452ff91c17SVincenzo Maffione 	struct netmap_kring **tx_rings; /* array of TX rings. */
7462ff91c17SVincenzo Maffione 	struct netmap_kring **rx_rings; /* array of RX rings. */
74717885a7bSLuigi Rizzo 
748f9790aebSLuigi Rizzo 	void *tailroom;		       /* space below the rings array */
749f9790aebSLuigi Rizzo 				       /* (used for leases) */
750f9790aebSLuigi Rizzo 
75168b8534bSLuigi Rizzo 
752847bf383SLuigi Rizzo 	NM_SELINFO_T si[NR_TXRX];	/* global wait queues */
75364ae02c3SLuigi Rizzo 
754f0ea3689SLuigi Rizzo 	/* count users of the global wait queues */
755847bf383SLuigi Rizzo 	int si_users[NR_TXRX];
756f0ea3689SLuigi Rizzo 
7574bf50f18SLuigi Rizzo 	void *pdev; /* used to store pci device */
7584bf50f18SLuigi Rizzo 
75968b8534bSLuigi Rizzo 	/* copy of if_qflush and if_transmit pointers, to intercept
76068b8534bSLuigi Rizzo 	 * packets from the network stack when netmap is active.
76168b8534bSLuigi Rizzo 	 */
76268b8534bSLuigi Rizzo 	int     (*if_transmit)(struct ifnet *, struct mbuf *);
76368b8534bSLuigi Rizzo 
76417885a7bSLuigi Rizzo 	/* copy of if_input for netmap_send_up() */
76517885a7bSLuigi Rizzo 	void     (*if_input)(struct ifnet *, struct mbuf *);
76617885a7bSLuigi Rizzo 
7674f80b14cSVincenzo Maffione 	/* Back reference to the parent ifnet struct. Used for
7684f80b14cSVincenzo Maffione 	 * hardware ports (emulated netmap included). */
76968b8534bSLuigi Rizzo 	struct ifnet *ifp; /* adapter is ifp->if_softc */
77068b8534bSLuigi Rizzo 
77117885a7bSLuigi Rizzo 	/*---- callbacks for this netmap adapter -----*/
77217885a7bSLuigi Rizzo 	/*
77317885a7bSLuigi Rizzo 	 * nm_dtor() is the cleanup routine called when destroying
77417885a7bSLuigi Rizzo 	 *	the adapter.
77589cc2556SLuigi Rizzo 	 *	Called with NMG_LOCK held.
77617885a7bSLuigi Rizzo 	 *
77717885a7bSLuigi Rizzo 	 * nm_register() is called on NIOCREGIF and close() to enter
77817885a7bSLuigi Rizzo 	 *	or exit netmap mode on the NIC
7794bf50f18SLuigi Rizzo 	 *	Called with NNG_LOCK held.
78017885a7bSLuigi Rizzo 	 *
78117885a7bSLuigi Rizzo 	 * nm_txsync() pushes packets to the underlying hw/switch
78217885a7bSLuigi Rizzo 	 *
78317885a7bSLuigi Rizzo 	 * nm_rxsync() collects packets from the underlying hw/switch
78417885a7bSLuigi Rizzo 	 *
78517885a7bSLuigi Rizzo 	 * nm_config() returns configuration information from the OS
78689cc2556SLuigi Rizzo 	 *	Called with NMG_LOCK held.
78717885a7bSLuigi Rizzo 	 *
7884bf50f18SLuigi Rizzo 	 * nm_krings_create() create and init the tx_rings and
7894bf50f18SLuigi Rizzo 	 * 	rx_rings arrays of kring structures. In particular,
7904bf50f18SLuigi Rizzo 	 * 	set the nm_sync callbacks for each ring.
7914bf50f18SLuigi Rizzo 	 * 	There is no need to also allocate the corresponding
7924bf50f18SLuigi Rizzo 	 * 	netmap_rings, since netmap_mem_rings_create() will always
7934bf50f18SLuigi Rizzo 	 * 	be called to provide the missing ones.
7944bf50f18SLuigi Rizzo 	 *	Called with NNG_LOCK held.
79517885a7bSLuigi Rizzo 	 *
7964bf50f18SLuigi Rizzo 	 * nm_krings_delete() cleanup and delete the tx_rings and rx_rings
7974bf50f18SLuigi Rizzo 	 * 	arrays
7984bf50f18SLuigi Rizzo 	 *	Called with NMG_LOCK held.
79917885a7bSLuigi Rizzo 	 *
80089cc2556SLuigi Rizzo 	 * nm_notify() is used to act after data have become available
80189cc2556SLuigi Rizzo 	 * 	(or the stopped state of the ring has changed)
80217885a7bSLuigi Rizzo 	 *	For hw devices this is typically a selwakeup(),
80317885a7bSLuigi Rizzo 	 *	but for NIC/host ports attached to a switch (or vice-versa)
80417885a7bSLuigi Rizzo 	 *	we also need to invoke the 'txsync' code downstream.
80537e3a6d3SLuigi Rizzo 	 *      This callback pointer is actually used only to initialize
80637e3a6d3SLuigi Rizzo 	 *      kring->nm_notify.
80737e3a6d3SLuigi Rizzo 	 *      Return values are the same as for netmap_rx_irq().
80817885a7bSLuigi Rizzo 	 */
809f9790aebSLuigi Rizzo 	void (*nm_dtor)(struct netmap_adapter *);
8101a26580eSLuigi Rizzo 
811f9790aebSLuigi Rizzo 	int (*nm_register)(struct netmap_adapter *, int onoff);
81237e3a6d3SLuigi Rizzo 	void (*nm_intr)(struct netmap_adapter *, int onoff);
813ce3ee1e7SLuigi Rizzo 
8144bf50f18SLuigi Rizzo 	int (*nm_txsync)(struct netmap_kring *kring, int flags);
8154bf50f18SLuigi Rizzo 	int (*nm_rxsync)(struct netmap_kring *kring, int flags);
816847bf383SLuigi Rizzo 	int (*nm_notify)(struct netmap_kring *kring, int flags);
817ce3ee1e7SLuigi Rizzo #define NAF_FORCE_READ      1
818ce3ee1e7SLuigi Rizzo #define NAF_FORCE_RECLAIM   2
819c3e9b4dbSLuiz Otavio O Souza #define NAF_CAN_FORWARD_DOWN 4
820ae10d1afSLuigi Rizzo 	/* return configuration information */
8212ff91c17SVincenzo Maffione 	int (*nm_config)(struct netmap_adapter *, struct nm_config_info *info);
822f9790aebSLuigi Rizzo 	int (*nm_krings_create)(struct netmap_adapter *);
823f9790aebSLuigi Rizzo 	void (*nm_krings_delete)(struct netmap_adapter *);
8244bf50f18SLuigi Rizzo 	/*
8254bf50f18SLuigi Rizzo 	 * nm_bdg_attach() initializes the na_vp field to point
8264bf50f18SLuigi Rizzo 	 *      to an adapter that can be attached to a VALE switch. If the
8274bf50f18SLuigi Rizzo 	 *      current adapter is already a VALE port, na_vp is simply a cast;
8284bf50f18SLuigi Rizzo 	 *      otherwise, na_vp points to a netmap_bwrap_adapter.
8294bf50f18SLuigi Rizzo 	 *      If applicable, this callback also initializes na_hostvp,
8304bf50f18SLuigi Rizzo 	 *      that can be used to connect the adapter host rings to the
8314bf50f18SLuigi Rizzo 	 *      switch.
8324bf50f18SLuigi Rizzo 	 *      Called with NMG_LOCK held.
8334bf50f18SLuigi Rizzo 	 *
8344bf50f18SLuigi Rizzo 	 * nm_bdg_ctl() is called on the actual attach/detach to/from
8354bf50f18SLuigi Rizzo 	 *      to/from the switch, to perform adapter-specific
8364bf50f18SLuigi Rizzo 	 *      initializations
8374bf50f18SLuigi Rizzo 	 *      Called with NMG_LOCK held.
8384bf50f18SLuigi Rizzo 	 */
8392a7db7a6SVincenzo Maffione 	int (*nm_bdg_attach)(const char *bdg_name, struct netmap_adapter *,
8402a7db7a6SVincenzo Maffione 			struct nm_bridge *);
8412ff91c17SVincenzo Maffione 	int (*nm_bdg_ctl)(struct nmreq_header *, struct netmap_adapter *);
8424bf50f18SLuigi Rizzo 
8434bf50f18SLuigi Rizzo 	/* adapter used to attach this adapter to a VALE switch (if any) */
8444bf50f18SLuigi Rizzo 	struct netmap_vp_adapter *na_vp;
8454bf50f18SLuigi Rizzo 	/* adapter used to attach the host rings of this adapter
8464bf50f18SLuigi Rizzo 	 * to a VALE switch (if any) */
8474bf50f18SLuigi Rizzo 	struct netmap_vp_adapter *na_hostvp;
848f9790aebSLuigi Rizzo 
849f9790aebSLuigi Rizzo 	/* standard refcount to control the lifetime of the adapter
850f9790aebSLuigi Rizzo 	 * (it should be equal to the lifetime of the corresponding ifp)
851f9790aebSLuigi Rizzo 	 */
852f9790aebSLuigi Rizzo 	int na_refcount;
853f9790aebSLuigi Rizzo 
854f9790aebSLuigi Rizzo 	/* memory allocator (opaque)
855f9790aebSLuigi Rizzo 	 * We also cache a pointer to the lut_entry for translating
85637e3a6d3SLuigi Rizzo 	 * buffer addresses, the total number of buffers and the buffer size.
857f9790aebSLuigi Rizzo 	 */
858f9790aebSLuigi Rizzo  	struct netmap_mem_d *nm_mem;
8594f80b14cSVincenzo Maffione 	struct netmap_mem_d *nm_mem_prev;
860847bf383SLuigi Rizzo 	struct netmap_lut na_lut;
861f9790aebSLuigi Rizzo 
8624bf50f18SLuigi Rizzo 	/* additional information attached to this adapter
8634bf50f18SLuigi Rizzo 	 * by other netmap subsystems. Currently used by
86437e3a6d3SLuigi Rizzo 	 * bwrap, LINUX/v1000 and ptnetmap
865f9790aebSLuigi Rizzo 	 */
866f9790aebSLuigi Rizzo 	void *na_private;
867f0ea3689SLuigi Rizzo 
8684bf50f18SLuigi Rizzo 	/* array of pipes that have this adapter as a parent */
869f0ea3689SLuigi Rizzo 	struct netmap_pipe_adapter **na_pipes;
8704bf50f18SLuigi Rizzo 	int na_next_pipe;	/* next free slot in the array */
8714bf50f18SLuigi Rizzo 	int na_max_pipes;	/* size of the array */
8724bf50f18SLuigi Rizzo 
87337e3a6d3SLuigi Rizzo 	/* Offset of ethernet header for each packet. */
87437e3a6d3SLuigi Rizzo 	u_int virt_hdr_len;
87537e3a6d3SLuigi Rizzo 
8762ff91c17SVincenzo Maffione 	/* Max number of bytes that the NIC can store in the buffer
8772ff91c17SVincenzo Maffione 	 * referenced by each RX descriptor. This translates to the maximum
8782ff91c17SVincenzo Maffione 	 * bytes that a single netmap slot can reference. Larger packets
8792ff91c17SVincenzo Maffione 	 * require NS_MOREFRAG support. */
8802ff91c17SVincenzo Maffione 	unsigned rx_buf_maxsize;
8812ff91c17SVincenzo Maffione 
8822ff91c17SVincenzo Maffione 	char name[NETMAP_REQ_IFNAMSIZ]; /* used at least by pipes */
8832a7db7a6SVincenzo Maffione 
8842a7db7a6SVincenzo Maffione #ifdef WITH_MONITOR
8852a7db7a6SVincenzo Maffione 	unsigned long	monitor_id;	/* debugging */
8862a7db7a6SVincenzo Maffione #endif
887f9790aebSLuigi Rizzo };
888f9790aebSLuigi Rizzo 
889847bf383SLuigi Rizzo static __inline u_int
890847bf383SLuigi Rizzo nma_get_ndesc(struct netmap_adapter *na, enum txrx t)
891847bf383SLuigi Rizzo {
892847bf383SLuigi Rizzo 	return (t == NR_TX ? na->num_tx_desc : na->num_rx_desc);
893847bf383SLuigi Rizzo }
894847bf383SLuigi Rizzo 
895847bf383SLuigi Rizzo static __inline void
896847bf383SLuigi Rizzo nma_set_ndesc(struct netmap_adapter *na, enum txrx t, u_int v)
897847bf383SLuigi Rizzo {
898847bf383SLuigi Rizzo 	if (t == NR_TX)
899847bf383SLuigi Rizzo 		na->num_tx_desc = v;
900847bf383SLuigi Rizzo 	else
901847bf383SLuigi Rizzo 		na->num_rx_desc = v;
902847bf383SLuigi Rizzo }
903847bf383SLuigi Rizzo 
904847bf383SLuigi Rizzo static __inline u_int
905847bf383SLuigi Rizzo nma_get_nrings(struct netmap_adapter *na, enum txrx t)
906847bf383SLuigi Rizzo {
907847bf383SLuigi Rizzo 	return (t == NR_TX ? na->num_tx_rings : na->num_rx_rings);
908847bf383SLuigi Rizzo }
909847bf383SLuigi Rizzo 
9102a7db7a6SVincenzo Maffione static __inline u_int
9112a7db7a6SVincenzo Maffione nma_get_host_nrings(struct netmap_adapter *na, enum txrx t)
9122a7db7a6SVincenzo Maffione {
9132a7db7a6SVincenzo Maffione 	return (t == NR_TX ? na->num_host_tx_rings : na->num_host_rx_rings);
9142a7db7a6SVincenzo Maffione }
9152a7db7a6SVincenzo Maffione 
916847bf383SLuigi Rizzo static __inline void
917847bf383SLuigi Rizzo nma_set_nrings(struct netmap_adapter *na, enum txrx t, u_int v)
918847bf383SLuigi Rizzo {
919847bf383SLuigi Rizzo 	if (t == NR_TX)
920847bf383SLuigi Rizzo 		na->num_tx_rings = v;
921847bf383SLuigi Rizzo 	else
922847bf383SLuigi Rizzo 		na->num_rx_rings = v;
923847bf383SLuigi Rizzo }
924847bf383SLuigi Rizzo 
9252a7db7a6SVincenzo Maffione static __inline void
9262a7db7a6SVincenzo Maffione nma_set_host_nrings(struct netmap_adapter *na, enum txrx t, u_int v)
9272a7db7a6SVincenzo Maffione {
9282a7db7a6SVincenzo Maffione 	if (t == NR_TX)
9292a7db7a6SVincenzo Maffione 		na->num_host_tx_rings = v;
9302a7db7a6SVincenzo Maffione 	else
9312a7db7a6SVincenzo Maffione 		na->num_host_rx_rings = v;
9322a7db7a6SVincenzo Maffione }
9332a7db7a6SVincenzo Maffione 
9342ff91c17SVincenzo Maffione static __inline struct netmap_kring**
935847bf383SLuigi Rizzo NMR(struct netmap_adapter *na, enum txrx t)
936847bf383SLuigi Rizzo {
937847bf383SLuigi Rizzo 	return (t == NR_TX ? na->tx_rings : na->rx_rings);
938847bf383SLuigi Rizzo }
93917885a7bSLuigi Rizzo 
9404f80b14cSVincenzo Maffione int nma_intr_enable(struct netmap_adapter *na, int onoff);
9414f80b14cSVincenzo Maffione 
942f9790aebSLuigi Rizzo /*
943f9790aebSLuigi Rizzo  * If the NIC is owned by the kernel
944f9790aebSLuigi Rizzo  * (i.e., bridge), neither another bridge nor user can use it;
945f9790aebSLuigi Rizzo  * if the NIC is owned by a user, only users can share it.
946f9790aebSLuigi Rizzo  * Evaluation must be done under NMG_LOCK().
947f9790aebSLuigi Rizzo  */
9484bf50f18SLuigi Rizzo #define NETMAP_OWNED_BY_KERN(na)	((na)->na_flags & NAF_BUSY)
949f9790aebSLuigi Rizzo #define NETMAP_OWNED_BY_ANY(na) \
9504bf50f18SLuigi Rizzo 	(NETMAP_OWNED_BY_KERN(na) || ((na)->active_fds > 0))
951f9790aebSLuigi Rizzo 
952f9790aebSLuigi Rizzo /*
953f9790aebSLuigi Rizzo  * derived netmap adapters for various types of ports
954f9790aebSLuigi Rizzo  */
955f9790aebSLuigi Rizzo struct netmap_vp_adapter {	/* VALE software port */
956f9790aebSLuigi Rizzo 	struct netmap_adapter up;
957f196ce38SLuigi Rizzo 
958849bec0eSLuigi Rizzo 	/*
959849bec0eSLuigi Rizzo 	 * Bridge support:
960849bec0eSLuigi Rizzo 	 *
961849bec0eSLuigi Rizzo 	 * bdg_port is the port number used in the bridge;
962f18be576SLuigi Rizzo 	 * na_bdg points to the bridge this NA is attached to.
963849bec0eSLuigi Rizzo 	 */
964f196ce38SLuigi Rizzo 	int bdg_port;
965f18be576SLuigi Rizzo 	struct nm_bridge *na_bdg;
966f9790aebSLuigi Rizzo 	int retry;
967c3e9b4dbSLuiz Otavio O Souza 	int autodelete; /* remove the ifp on last reference */
968f9790aebSLuigi Rizzo 
969f0ea3689SLuigi Rizzo 	/* Maximum Frame Size, used in bdg_mismatch_datapath() */
970f0ea3689SLuigi Rizzo 	u_int mfs;
971847bf383SLuigi Rizzo 	/* Last source MAC on this port */
972847bf383SLuigi Rizzo 	uint64_t last_smac;
973f9790aebSLuigi Rizzo };
974f9790aebSLuigi Rizzo 
97517885a7bSLuigi Rizzo 
976f9790aebSLuigi Rizzo struct netmap_hw_adapter {	/* physical device */
977f9790aebSLuigi Rizzo 	struct netmap_adapter up;
978f9790aebSLuigi Rizzo 
9794f80b14cSVincenzo Maffione #ifdef linux
9804f80b14cSVincenzo Maffione 	struct net_device_ops nm_ndo;
9814f80b14cSVincenzo Maffione 	struct ethtool_ops    nm_eto;
9824f80b14cSVincenzo Maffione #endif
9834bf50f18SLuigi Rizzo 	const struct ethtool_ops*   save_ethtool;
9844bf50f18SLuigi Rizzo 
9854bf50f18SLuigi Rizzo 	int (*nm_hw_register)(struct netmap_adapter *, int onoff);
986f9790aebSLuigi Rizzo };
987f9790aebSLuigi Rizzo 
988039dd540SLuigi Rizzo #ifdef WITH_GENERIC
989f0ea3689SLuigi Rizzo /* Mitigation support. */
990f0ea3689SLuigi Rizzo struct nm_generic_mit {
991f0ea3689SLuigi Rizzo 	struct hrtimer mit_timer;
992f0ea3689SLuigi Rizzo 	int mit_pending;
9934bf50f18SLuigi Rizzo 	int mit_ring_idx;  /* index of the ring being mitigated */
994f0ea3689SLuigi Rizzo 	struct netmap_adapter *mit_na;  /* backpointer */
995f0ea3689SLuigi Rizzo };
99617885a7bSLuigi Rizzo 
99717885a7bSLuigi Rizzo struct netmap_generic_adapter {	/* emulated device */
998f9790aebSLuigi Rizzo 	struct netmap_hw_adapter up;
999f9790aebSLuigi Rizzo 
1000f9790aebSLuigi Rizzo 	/* Pointer to a previously used netmap adapter. */
1001f9790aebSLuigi Rizzo 	struct netmap_adapter *prev;
1002f9790aebSLuigi Rizzo 
10034f80b14cSVincenzo Maffione 	/* Emulated netmap adapters support:
10044f80b14cSVincenzo Maffione 	 *  - save_if_input saves the if_input hook (FreeBSD);
10054f80b14cSVincenzo Maffione 	 *  - mit implements rx interrupt mitigation;
1006f9790aebSLuigi Rizzo 	 */
1007f9790aebSLuigi Rizzo 	void (*save_if_input)(struct ifnet *, struct mbuf *);
1008f9790aebSLuigi Rizzo 
1009f0ea3689SLuigi Rizzo 	struct nm_generic_mit *mit;
101017885a7bSLuigi Rizzo #ifdef linux
101117885a7bSLuigi Rizzo         netdev_tx_t (*save_start_xmit)(struct mbuf *, struct ifnet *);
101217885a7bSLuigi Rizzo #endif
101337e3a6d3SLuigi Rizzo 	/* Is the adapter able to use multiple RX slots to scatter
101437e3a6d3SLuigi Rizzo 	 * each packet pushed up by the driver? */
101537e3a6d3SLuigi Rizzo 	int rxsg;
101637e3a6d3SLuigi Rizzo 
101737e3a6d3SLuigi Rizzo 	/* Is the transmission path controlled by a netmap-aware
101837e3a6d3SLuigi Rizzo 	 * device queue (i.e. qdisc on linux)? */
101937e3a6d3SLuigi Rizzo 	int txqdisc;
1020f9790aebSLuigi Rizzo };
1021039dd540SLuigi Rizzo #endif  /* WITH_GENERIC */
1022f9790aebSLuigi Rizzo 
10232a7db7a6SVincenzo Maffione static __inline u_int
1024847bf383SLuigi Rizzo netmap_real_rings(struct netmap_adapter *na, enum txrx t)
1025f0ea3689SLuigi Rizzo {
10262a7db7a6SVincenzo Maffione 	return nma_get_nrings(na, t) +
10272a7db7a6SVincenzo Maffione 		!!(na->na_flags & NAF_HOST_RINGS) * nma_get_host_nrings(na, t);
1028f0ea3689SLuigi Rizzo }
1029f0ea3689SLuigi Rizzo 
10302a7db7a6SVincenzo Maffione /* account for fake rings */
10312a7db7a6SVincenzo Maffione static __inline u_int
10322a7db7a6SVincenzo Maffione netmap_all_rings(struct netmap_adapter *na, enum txrx t)
10332a7db7a6SVincenzo Maffione {
10342a7db7a6SVincenzo Maffione 	return max(nma_get_nrings(na, t) + 1, netmap_real_rings(na, t));
10352a7db7a6SVincenzo Maffione }
10362a7db7a6SVincenzo Maffione 
10372a7db7a6SVincenzo Maffione int netmap_default_bdg_attach(const char *name, struct netmap_adapter *na,
10382a7db7a6SVincenzo Maffione 		struct nm_bridge *);
103937e3a6d3SLuigi Rizzo struct nm_bdg_polling_state;
104017885a7bSLuigi Rizzo /*
104117885a7bSLuigi Rizzo  * Bridge wrapper for non VALE ports attached to a VALE switch.
1042f9790aebSLuigi Rizzo  *
104317885a7bSLuigi Rizzo  * The real device must already have its own netmap adapter (hwna).
104417885a7bSLuigi Rizzo  * The bridge wrapper and the hwna adapter share the same set of
104517885a7bSLuigi Rizzo  * netmap rings and buffers, but they have two separate sets of
104617885a7bSLuigi Rizzo  * krings descriptors, with tx/rx meanings swapped:
1047f9790aebSLuigi Rizzo  *
1048f9790aebSLuigi Rizzo  *                                  netmap
1049f9790aebSLuigi Rizzo  *           bwrap     krings       rings      krings      hwna
1050f9790aebSLuigi Rizzo  *         +------+   +------+     +-----+    +------+   +------+
1051f9790aebSLuigi Rizzo  *         |tx_rings->|      |\   /|     |----|      |<-tx_rings|
1052f9790aebSLuigi Rizzo  *         |      |   +------+ \ / +-----+    +------+   |      |
1053f9790aebSLuigi Rizzo  *         |      |             X                        |      |
1054f9790aebSLuigi Rizzo  *         |      |            / \                       |      |
1055f9790aebSLuigi Rizzo  *         |      |   +------+/   \+-----+    +------+   |      |
1056f9790aebSLuigi Rizzo  *         |rx_rings->|      |     |     |----|      |<-rx_rings|
1057f9790aebSLuigi Rizzo  *         |      |   +------+     +-----+    +------+   |      |
1058f9790aebSLuigi Rizzo  *         +------+                                      +------+
1059f9790aebSLuigi Rizzo  *
106017885a7bSLuigi Rizzo  * - packets coming from the bridge go to the brwap rx rings,
106117885a7bSLuigi Rizzo  *   which are also the hwna tx rings.  The bwrap notify callback
106217885a7bSLuigi Rizzo  *   will then complete the hwna tx (see netmap_bwrap_notify).
1063f9790aebSLuigi Rizzo  *
106417885a7bSLuigi Rizzo  * - packets coming from the outside go to the hwna rx rings,
106517885a7bSLuigi Rizzo  *   which are also the bwrap tx rings.  The (overwritten) hwna
106617885a7bSLuigi Rizzo  *   notify method will then complete the bridge tx
106717885a7bSLuigi Rizzo  *   (see netmap_bwrap_intr_notify).
1068f9790aebSLuigi Rizzo  *
106917885a7bSLuigi Rizzo  *   The bridge wrapper may optionally connect the hwna 'host' rings
107017885a7bSLuigi Rizzo  *   to the bridge. This is done by using a second port in the
107117885a7bSLuigi Rizzo  *   bridge and connecting it to the 'host' netmap_vp_adapter
107217885a7bSLuigi Rizzo  *   contained in the netmap_bwrap_adapter. The brwap host adapter
107317885a7bSLuigi Rizzo  *   cross-links the hwna host rings in the same way as shown above.
107417885a7bSLuigi Rizzo  *
107517885a7bSLuigi Rizzo  * - packets coming from the bridge and directed to the host stack
107617885a7bSLuigi Rizzo  *   are handled by the bwrap host notify callback
107717885a7bSLuigi Rizzo  *   (see netmap_bwrap_host_notify)
107817885a7bSLuigi Rizzo  *
107917885a7bSLuigi Rizzo  * - packets coming from the host stack are still handled by the
108017885a7bSLuigi Rizzo  *   overwritten hwna notify callback (netmap_bwrap_intr_notify),
108117885a7bSLuigi Rizzo  *   but are diverted to the host adapter depending on the ring number.
1082f9790aebSLuigi Rizzo  *
1083f9790aebSLuigi Rizzo  */
1084f9790aebSLuigi Rizzo struct netmap_bwrap_adapter {
1085f9790aebSLuigi Rizzo 	struct netmap_vp_adapter up;
1086f9790aebSLuigi Rizzo 	struct netmap_vp_adapter host;  /* for host rings */
1087f9790aebSLuigi Rizzo 	struct netmap_adapter *hwna;	/* the underlying device */
1088f9790aebSLuigi Rizzo 
108917885a7bSLuigi Rizzo 	/*
109017885a7bSLuigi Rizzo 	 * When we attach a physical interface to the bridge, we
1091f18be576SLuigi Rizzo 	 * allow the controlling process to terminate, so we need
10924bf50f18SLuigi Rizzo 	 * a place to store the n_detmap_priv_d data structure.
109317885a7bSLuigi Rizzo 	 * This is only done when physical interfaces
109417885a7bSLuigi Rizzo 	 * are attached to a bridge.
1095f18be576SLuigi Rizzo 	 */
1096f18be576SLuigi Rizzo 	struct netmap_priv_d *na_kpriv;
109737e3a6d3SLuigi Rizzo 	struct nm_bdg_polling_state *na_polling_state;
10982ff91c17SVincenzo Maffione 	/* we overwrite the hwna->na_vp pointer, so we save
10992ff91c17SVincenzo Maffione 	 * here its original value, to be restored at detach
11002ff91c17SVincenzo Maffione 	 */
11012ff91c17SVincenzo Maffione 	struct netmap_vp_adapter *saved_na_vp;
110268b8534bSLuigi Rizzo };
11032ff91c17SVincenzo Maffione int nm_bdg_polling(struct nmreq_header *hdr);
11042a7db7a6SVincenzo Maffione 
11052a7db7a6SVincenzo Maffione #ifdef WITH_VALE
1106b6e66be2SVincenzo Maffione int netmap_vale_attach(struct nmreq_header *hdr, void *auth_token);
1107b6e66be2SVincenzo Maffione int netmap_vale_detach(struct nmreq_header *hdr, void *auth_token);
1108b6e66be2SVincenzo Maffione int netmap_vale_list(struct nmreq_header *hdr);
11092ff91c17SVincenzo Maffione int netmap_vi_create(struct nmreq_header *hdr, int);
11102ff91c17SVincenzo Maffione int nm_vi_create(struct nmreq_header *);
11112ff91c17SVincenzo Maffione int nm_vi_destroy(const char *name);
1112c3e9b4dbSLuiz Otavio O Souza #else /* !WITH_VALE */
11132ff91c17SVincenzo Maffione #define netmap_vi_create(hdr, a) (EOPNOTSUPP)
111417885a7bSLuigi Rizzo #endif /* WITH_VALE */
1115ce3ee1e7SLuigi Rizzo 
1116f0ea3689SLuigi Rizzo #ifdef WITH_PIPES
1117f0ea3689SLuigi Rizzo 
1118f0ea3689SLuigi Rizzo #define NM_MAXPIPES 	64	/* max number of pipes per adapter */
1119f0ea3689SLuigi Rizzo 
1120f0ea3689SLuigi Rizzo struct netmap_pipe_adapter {
11212ff91c17SVincenzo Maffione 	/* pipe identifier is up.name */
1122f0ea3689SLuigi Rizzo 	struct netmap_adapter up;
1123f0ea3689SLuigi Rizzo 
11242ff91c17SVincenzo Maffione #define NM_PIPE_ROLE_MASTER	0x1
11252ff91c17SVincenzo Maffione #define NM_PIPE_ROLE_SLAVE	0x2
11262ff91c17SVincenzo Maffione 	int role;	/* either NM_PIPE_ROLE_MASTER or NM_PIPE_ROLE_SLAVE */
1127f0ea3689SLuigi Rizzo 
1128f0ea3689SLuigi Rizzo 	struct netmap_adapter *parent; /* adapter that owns the memory */
1129f0ea3689SLuigi Rizzo 	struct netmap_pipe_adapter *peer; /* the other end of the pipe */
1130f0ea3689SLuigi Rizzo 	int peer_ref;		/* 1 iff we are holding a ref to the peer */
1131c3e9b4dbSLuiz Otavio O Souza 	struct ifnet *parent_ifp;	/* maybe null */
1132f0ea3689SLuigi Rizzo 
1133f0ea3689SLuigi Rizzo 	u_int parent_slot; /* index in the parent pipe array */
1134f0ea3689SLuigi Rizzo };
1135f0ea3689SLuigi Rizzo 
1136f0ea3689SLuigi Rizzo #endif /* WITH_PIPES */
1137f0ea3689SLuigi Rizzo 
1138b6e66be2SVincenzo Maffione #ifdef WITH_NMNULL
1139b6e66be2SVincenzo Maffione struct netmap_null_adapter {
1140b6e66be2SVincenzo Maffione 	struct netmap_adapter up;
1141b6e66be2SVincenzo Maffione };
1142b6e66be2SVincenzo Maffione #endif /* WITH_NMNULL */
1143b6e66be2SVincenzo Maffione 
114417885a7bSLuigi Rizzo 
114517885a7bSLuigi Rizzo /* return slots reserved to rx clients; used in drivers */
114617885a7bSLuigi Rizzo static inline uint32_t
114717885a7bSLuigi Rizzo nm_kr_rxspace(struct netmap_kring *k)
114817885a7bSLuigi Rizzo {
114917885a7bSLuigi Rizzo 	int space = k->nr_hwtail - k->nr_hwcur;
1150ce3ee1e7SLuigi Rizzo 	if (space < 0)
1151ce3ee1e7SLuigi Rizzo 		space += k->nkr_num_slots;
115217885a7bSLuigi Rizzo 	ND("preserving %d rx slots %d -> %d", space, k->nr_hwcur, k->nr_hwtail);
115317885a7bSLuigi Rizzo 
1154ce3ee1e7SLuigi Rizzo 	return space;
1155ce3ee1e7SLuigi Rizzo }
1156ce3ee1e7SLuigi Rizzo 
115737e3a6d3SLuigi Rizzo /* return slots reserved to tx clients */
115837e3a6d3SLuigi Rizzo #define nm_kr_txspace(_k) nm_kr_rxspace(_k)
1159ce3ee1e7SLuigi Rizzo 
116037e3a6d3SLuigi Rizzo 
116137e3a6d3SLuigi Rizzo /* True if no space in the tx ring, only valid after txsync_prologue */
116217885a7bSLuigi Rizzo static inline int
116317885a7bSLuigi Rizzo nm_kr_txempty(struct netmap_kring *kring)
1164ce3ee1e7SLuigi Rizzo {
1165f79ba6d7SVincenzo Maffione 	return kring->rhead == kring->nr_hwtail;
1166f9790aebSLuigi Rizzo }
1167f9790aebSLuigi Rizzo 
116837e3a6d3SLuigi Rizzo /* True if no more completed slots in the rx ring, only valid after
116937e3a6d3SLuigi Rizzo  * rxsync_prologue */
117037e3a6d3SLuigi Rizzo #define nm_kr_rxempty(_k)	nm_kr_txempty(_k)
1171ce3ee1e7SLuigi Rizzo 
1172*5faab778SVincenzo Maffione /* True if the application needs to wait for more space on the ring
1173*5faab778SVincenzo Maffione  * (more received packets or more free tx slots).
1174*5faab778SVincenzo Maffione  * Only valid after *xsync_prologue. */
1175*5faab778SVincenzo Maffione static inline int
1176*5faab778SVincenzo Maffione nm_kr_wouldblock(struct netmap_kring *kring)
1177*5faab778SVincenzo Maffione {
1178*5faab778SVincenzo Maffione 	return kring->rcur == kring->nr_hwtail;
1179*5faab778SVincenzo Maffione }
1180*5faab778SVincenzo Maffione 
1181ce3ee1e7SLuigi Rizzo /*
1182f9790aebSLuigi Rizzo  * protect against multiple threads using the same ring.
118337e3a6d3SLuigi Rizzo  * also check that the ring has not been stopped or locked
118468b8534bSLuigi Rizzo  */
118537e3a6d3SLuigi Rizzo #define NM_KR_BUSY	1	/* some other thread is syncing the ring */
118637e3a6d3SLuigi Rizzo #define NM_KR_STOPPED	2	/* unbounded stop (ifconfig down or driver unload) */
118737e3a6d3SLuigi Rizzo #define NM_KR_LOCKED	3	/* bounded, brief stop for mutual exclusion */
118868b8534bSLuigi Rizzo 
118917885a7bSLuigi Rizzo 
119037e3a6d3SLuigi Rizzo /* release the previously acquired right to use the *sync() methods of the ring */
1191f9790aebSLuigi Rizzo static __inline void nm_kr_put(struct netmap_kring *kr)
1192f9790aebSLuigi Rizzo {
1193f9790aebSLuigi Rizzo 	NM_ATOMIC_CLEAR(&kr->nr_busy);
1194f9790aebSLuigi Rizzo }
1195f9790aebSLuigi Rizzo 
119617885a7bSLuigi Rizzo 
119737e3a6d3SLuigi Rizzo /* true if the ifp that backed the adapter has disappeared (e.g., the
119837e3a6d3SLuigi Rizzo  * driver has been unloaded)
119937e3a6d3SLuigi Rizzo  */
120037e3a6d3SLuigi Rizzo static inline int nm_iszombie(struct netmap_adapter *na);
120137e3a6d3SLuigi Rizzo 
120237e3a6d3SLuigi Rizzo /* try to obtain exclusive right to issue the *sync() operations on the ring.
120337e3a6d3SLuigi Rizzo  * The right is obtained and must be later relinquished via nm_kr_put() if and
120437e3a6d3SLuigi Rizzo  * only if nm_kr_tryget() returns 0.
120537e3a6d3SLuigi Rizzo  * If can_sleep is 1 there are only two other possible outcomes:
120637e3a6d3SLuigi Rizzo  * - the function returns NM_KR_BUSY
120737e3a6d3SLuigi Rizzo  * - the function returns NM_KR_STOPPED and sets the POLLERR bit in *perr
120837e3a6d3SLuigi Rizzo  *   (if non-null)
120937e3a6d3SLuigi Rizzo  * In both cases the caller will typically skip the ring, possibly collecting
121037e3a6d3SLuigi Rizzo  * errors along the way.
121137e3a6d3SLuigi Rizzo  * If the calling context does not allow sleeping, the caller must pass 0 in can_sleep.
121237e3a6d3SLuigi Rizzo  * In the latter case, the function may also return NM_KR_LOCKED and leave *perr
121337e3a6d3SLuigi Rizzo  * untouched: ideally, the caller should try again at a later time.
121437e3a6d3SLuigi Rizzo  */
121537e3a6d3SLuigi Rizzo static __inline int nm_kr_tryget(struct netmap_kring *kr, int can_sleep, int *perr)
1216f9790aebSLuigi Rizzo {
121737e3a6d3SLuigi Rizzo 	int busy = 1, stopped;
1218f9790aebSLuigi Rizzo 	/* check a first time without taking the lock
1219f9790aebSLuigi Rizzo 	 * to avoid starvation for nm_kr_get()
1220f9790aebSLuigi Rizzo 	 */
122137e3a6d3SLuigi Rizzo retry:
122237e3a6d3SLuigi Rizzo 	stopped = kr->nkr_stopped;
122337e3a6d3SLuigi Rizzo 	if (unlikely(stopped)) {
122437e3a6d3SLuigi Rizzo 		goto stop;
1225f9790aebSLuigi Rizzo 	}
122637e3a6d3SLuigi Rizzo 	busy = NM_ATOMIC_TEST_AND_SET(&kr->nr_busy);
122737e3a6d3SLuigi Rizzo 	/* we should not return NM_KR_BUSY if the ring was
122837e3a6d3SLuigi Rizzo 	 * actually stopped, so check another time after
122937e3a6d3SLuigi Rizzo 	 * the barrier provided by the atomic operation
123037e3a6d3SLuigi Rizzo 	 */
123137e3a6d3SLuigi Rizzo 	stopped = kr->nkr_stopped;
123237e3a6d3SLuigi Rizzo 	if (unlikely(stopped)) {
123337e3a6d3SLuigi Rizzo 		goto stop;
1234f9790aebSLuigi Rizzo 	}
123568b8534bSLuigi Rizzo 
123637e3a6d3SLuigi Rizzo 	if (unlikely(nm_iszombie(kr->na))) {
123737e3a6d3SLuigi Rizzo 		stopped = NM_KR_STOPPED;
123837e3a6d3SLuigi Rizzo 		goto stop;
123937e3a6d3SLuigi Rizzo 	}
124037e3a6d3SLuigi Rizzo 
124137e3a6d3SLuigi Rizzo 	return unlikely(busy) ? NM_KR_BUSY : 0;
124237e3a6d3SLuigi Rizzo 
124337e3a6d3SLuigi Rizzo stop:
124437e3a6d3SLuigi Rizzo 	if (!busy)
124537e3a6d3SLuigi Rizzo 		nm_kr_put(kr);
124637e3a6d3SLuigi Rizzo 	if (stopped == NM_KR_STOPPED) {
124737e3a6d3SLuigi Rizzo /* if POLLERR is defined we want to use it to simplify netmap_poll().
124837e3a6d3SLuigi Rizzo  * Otherwise, any non-zero value will do.
124937e3a6d3SLuigi Rizzo  */
125037e3a6d3SLuigi Rizzo #ifdef POLLERR
125137e3a6d3SLuigi Rizzo #define NM_POLLERR POLLERR
125237e3a6d3SLuigi Rizzo #else
125337e3a6d3SLuigi Rizzo #define NM_POLLERR 1
125437e3a6d3SLuigi Rizzo #endif /* POLLERR */
125537e3a6d3SLuigi Rizzo 		if (perr)
125637e3a6d3SLuigi Rizzo 			*perr |= NM_POLLERR;
125737e3a6d3SLuigi Rizzo #undef NM_POLLERR
125837e3a6d3SLuigi Rizzo 	} else if (can_sleep) {
125937e3a6d3SLuigi Rizzo 		tsleep(kr, 0, "NM_KR_TRYGET", 4);
126037e3a6d3SLuigi Rizzo 		goto retry;
126137e3a6d3SLuigi Rizzo 	}
126237e3a6d3SLuigi Rizzo 	return stopped;
126337e3a6d3SLuigi Rizzo }
126437e3a6d3SLuigi Rizzo 
126537e3a6d3SLuigi Rizzo /* put the ring in the 'stopped' state and wait for the current user (if any) to
126637e3a6d3SLuigi Rizzo  * notice. stopped must be either NM_KR_STOPPED or NM_KR_LOCKED
126737e3a6d3SLuigi Rizzo  */
126837e3a6d3SLuigi Rizzo static __inline void nm_kr_stop(struct netmap_kring *kr, int stopped)
1269847bf383SLuigi Rizzo {
127037e3a6d3SLuigi Rizzo 	kr->nkr_stopped = stopped;
1271847bf383SLuigi Rizzo 	while (NM_ATOMIC_TEST_AND_SET(&kr->nr_busy))
1272847bf383SLuigi Rizzo 		tsleep(kr, 0, "NM_KR_GET", 4);
1273847bf383SLuigi Rizzo }
1274847bf383SLuigi Rizzo 
127537e3a6d3SLuigi Rizzo /* restart a ring after a stop */
127637e3a6d3SLuigi Rizzo static __inline void nm_kr_start(struct netmap_kring *kr)
127737e3a6d3SLuigi Rizzo {
127837e3a6d3SLuigi Rizzo 	kr->nkr_stopped = 0;
127937e3a6d3SLuigi Rizzo 	nm_kr_put(kr);
128037e3a6d3SLuigi Rizzo }
128137e3a6d3SLuigi Rizzo 
1282847bf383SLuigi Rizzo 
128368b8534bSLuigi Rizzo /*
128417885a7bSLuigi Rizzo  * The following functions are used by individual drivers to
128568b8534bSLuigi Rizzo  * support netmap operation.
128668b8534bSLuigi Rizzo  *
128768b8534bSLuigi Rizzo  * netmap_attach() initializes a struct netmap_adapter, allocating the
128868b8534bSLuigi Rizzo  * 	struct netmap_ring's and the struct selinfo.
128968b8534bSLuigi Rizzo  *
129068b8534bSLuigi Rizzo  * netmap_detach() frees the memory allocated by netmap_attach().
129168b8534bSLuigi Rizzo  *
1292ce3ee1e7SLuigi Rizzo  * netmap_transmit() replaces the if_transmit routine of the interface,
129368b8534bSLuigi Rizzo  *	and is used to intercept packets coming from the stack.
129468b8534bSLuigi Rizzo  *
129568b8534bSLuigi Rizzo  * netmap_load_map/netmap_reload_map are helper routines to set/reset
129668b8534bSLuigi Rizzo  *	the dmamap for a packet buffer
129768b8534bSLuigi Rizzo  *
12984bf50f18SLuigi Rizzo  * netmap_reset() is a helper routine to be called in the hw driver
12994bf50f18SLuigi Rizzo  *	when reinitializing a ring. It should not be called by
13004bf50f18SLuigi Rizzo  *	virtual ports (vale, pipes, monitor)
130168b8534bSLuigi Rizzo  */
1302f9790aebSLuigi Rizzo int netmap_attach(struct netmap_adapter *);
13034f80b14cSVincenzo Maffione int netmap_attach_ext(struct netmap_adapter *, size_t size, int override_reg);
130468b8534bSLuigi Rizzo void netmap_detach(struct ifnet *);
1305ce3ee1e7SLuigi Rizzo int netmap_transmit(struct ifnet *, struct mbuf *);
130668b8534bSLuigi Rizzo struct netmap_slot *netmap_reset(struct netmap_adapter *na,
1307ce3ee1e7SLuigi Rizzo 	enum txrx tx, u_int n, u_int new_cur);
130868b8534bSLuigi Rizzo int netmap_ring_reinit(struct netmap_kring *);
13092ff91c17SVincenzo Maffione int netmap_rings_config_get(struct netmap_adapter *, struct nm_config_info *);
131068b8534bSLuigi Rizzo 
131137e3a6d3SLuigi Rizzo /* Return codes for netmap_*x_irq. */
131237e3a6d3SLuigi Rizzo enum {
131337e3a6d3SLuigi Rizzo 	/* Driver should do normal interrupt processing, e.g. because
131437e3a6d3SLuigi Rizzo 	 * the interface is not in netmap mode. */
131537e3a6d3SLuigi Rizzo 	NM_IRQ_PASS = 0,
131637e3a6d3SLuigi Rizzo 	/* Port is in netmap mode, and the interrupt work has been
131737e3a6d3SLuigi Rizzo 	 * completed. The driver does not have to notify netmap
131837e3a6d3SLuigi Rizzo 	 * again before the next interrupt. */
131937e3a6d3SLuigi Rizzo 	NM_IRQ_COMPLETED = -1,
132037e3a6d3SLuigi Rizzo 	/* Port is in netmap mode, but the interrupt work has not been
132137e3a6d3SLuigi Rizzo 	 * completed. The driver has to make sure netmap will be
132237e3a6d3SLuigi Rizzo 	 * notified again soon, even if no more interrupts come (e.g.
132337e3a6d3SLuigi Rizzo 	 * on Linux the driver should not call napi_complete()). */
132437e3a6d3SLuigi Rizzo 	NM_IRQ_RESCHED = -2,
132537e3a6d3SLuigi Rizzo };
132637e3a6d3SLuigi Rizzo 
132717885a7bSLuigi Rizzo /* default functions to handle rx/tx interrupts */
132817885a7bSLuigi Rizzo int netmap_rx_irq(struct ifnet *, u_int, u_int *);
132917885a7bSLuigi Rizzo #define netmap_tx_irq(_n, _q) netmap_rx_irq(_n, _q, NULL)
133037e3a6d3SLuigi Rizzo int netmap_common_irq(struct netmap_adapter *, u_int, u_int *work_done);
133117885a7bSLuigi Rizzo 
133217885a7bSLuigi Rizzo 
13334bf50f18SLuigi Rizzo #ifdef WITH_VALE
13344bf50f18SLuigi Rizzo /* functions used by external modules to interface with VALE */
13354bf50f18SLuigi Rizzo #define netmap_vp_to_ifp(_vp)	((_vp)->up.ifp)
13364bf50f18SLuigi Rizzo #define netmap_ifp_to_vp(_ifp)	(NA(_ifp)->na_vp)
13374bf50f18SLuigi Rizzo #define netmap_ifp_to_host_vp(_ifp) (NA(_ifp)->na_hostvp)
13384bf50f18SLuigi Rizzo #define netmap_bdg_idx(_vp)	((_vp)->bdg_port)
13394bf50f18SLuigi Rizzo const char *netmap_bdg_name(struct netmap_vp_adapter *);
13404bf50f18SLuigi Rizzo #else /* !WITH_VALE */
13414bf50f18SLuigi Rizzo #define netmap_vp_to_ifp(_vp)	NULL
13424bf50f18SLuigi Rizzo #define netmap_ifp_to_vp(_ifp)	NULL
13434bf50f18SLuigi Rizzo #define netmap_ifp_to_host_vp(_ifp) NULL
13444bf50f18SLuigi Rizzo #define netmap_bdg_idx(_vp)	-1
13454bf50f18SLuigi Rizzo #endif /* WITH_VALE */
13464bf50f18SLuigi Rizzo 
13474bf50f18SLuigi Rizzo static inline int
13484bf50f18SLuigi Rizzo nm_netmap_on(struct netmap_adapter *na)
13494bf50f18SLuigi Rizzo {
13504bf50f18SLuigi Rizzo 	return na && na->na_flags & NAF_NETMAP_ON;
13514bf50f18SLuigi Rizzo }
135217885a7bSLuigi Rizzo 
1353847bf383SLuigi Rizzo static inline int
1354847bf383SLuigi Rizzo nm_native_on(struct netmap_adapter *na)
1355847bf383SLuigi Rizzo {
1356847bf383SLuigi Rizzo 	return nm_netmap_on(na) && (na->na_flags & NAF_NATIVE);
1357847bf383SLuigi Rizzo }
1358847bf383SLuigi Rizzo 
135937e3a6d3SLuigi Rizzo static inline int
136037e3a6d3SLuigi Rizzo nm_iszombie(struct netmap_adapter *na)
136137e3a6d3SLuigi Rizzo {
136237e3a6d3SLuigi Rizzo 	return na == NULL || (na->na_flags & NAF_ZOMBIE);
136337e3a6d3SLuigi Rizzo }
136437e3a6d3SLuigi Rizzo 
136537e3a6d3SLuigi Rizzo static inline void
136637e3a6d3SLuigi Rizzo nm_update_hostrings_mode(struct netmap_adapter *na)
136737e3a6d3SLuigi Rizzo {
136837e3a6d3SLuigi Rizzo 	/* Process nr_mode and nr_pending_mode for host rings. */
13692ff91c17SVincenzo Maffione 	na->tx_rings[na->num_tx_rings]->nr_mode =
13702ff91c17SVincenzo Maffione 		na->tx_rings[na->num_tx_rings]->nr_pending_mode;
13712ff91c17SVincenzo Maffione 	na->rx_rings[na->num_rx_rings]->nr_mode =
13722ff91c17SVincenzo Maffione 		na->rx_rings[na->num_rx_rings]->nr_pending_mode;
137337e3a6d3SLuigi Rizzo }
137437e3a6d3SLuigi Rizzo 
13752a7db7a6SVincenzo Maffione void nm_set_native_flags(struct netmap_adapter *);
13762a7db7a6SVincenzo Maffione void nm_clear_native_flags(struct netmap_adapter *);
13772ff91c17SVincenzo Maffione 
137837e3a6d3SLuigi Rizzo /*
137937e3a6d3SLuigi Rizzo  * nm_*sync_prologue() functions are used in ioctl/poll and ptnetmap
138037e3a6d3SLuigi Rizzo  * kthreads.
138137e3a6d3SLuigi Rizzo  * We need netmap_ring* parameter, because in ptnetmap it is decoupled
138237e3a6d3SLuigi Rizzo  * from host kring.
138337e3a6d3SLuigi Rizzo  * The user-space ring pointers (head/cur/tail) are shared through
138437e3a6d3SLuigi Rizzo  * CSB between host and guest.
138537e3a6d3SLuigi Rizzo  */
138637e3a6d3SLuigi Rizzo 
138737e3a6d3SLuigi Rizzo /*
138837e3a6d3SLuigi Rizzo  * validates parameters in the ring/kring, returns a value for head
138937e3a6d3SLuigi Rizzo  * If any error, returns ring_size to force a reinit.
139037e3a6d3SLuigi Rizzo  */
139137e3a6d3SLuigi Rizzo uint32_t nm_txsync_prologue(struct netmap_kring *, struct netmap_ring *);
139237e3a6d3SLuigi Rizzo 
139337e3a6d3SLuigi Rizzo 
139437e3a6d3SLuigi Rizzo /*
139537e3a6d3SLuigi Rizzo  * validates parameters in the ring/kring, returns a value for head
139637e3a6d3SLuigi Rizzo  * If any error, returns ring_size lim to force a reinit.
139737e3a6d3SLuigi Rizzo  */
139837e3a6d3SLuigi Rizzo uint32_t nm_rxsync_prologue(struct netmap_kring *, struct netmap_ring *);
139937e3a6d3SLuigi Rizzo 
1400f9790aebSLuigi Rizzo 
1401f9790aebSLuigi Rizzo /* check/fix address and len in tx rings */
1402f9790aebSLuigi Rizzo #if 1 /* debug version */
14034bf50f18SLuigi Rizzo #define	NM_CHECK_ADDR_LEN(_na, _a, _l)	do {				\
14044bf50f18SLuigi Rizzo 	if (_a == NETMAP_BUF_BASE(_na) || _l > NETMAP_BUF_SIZE(_na)) {	\
1405f9790aebSLuigi Rizzo 		RD(5, "bad addr/len ring %d slot %d idx %d len %d",	\
14064bf50f18SLuigi Rizzo 			kring->ring_id, nm_i, slot->buf_idx, len);	\
14074bf50f18SLuigi Rizzo 		if (_l > NETMAP_BUF_SIZE(_na))				\
14084bf50f18SLuigi Rizzo 			_l = NETMAP_BUF_SIZE(_na);			\
1409f9790aebSLuigi Rizzo 	} } while (0)
1410f9790aebSLuigi Rizzo #else /* no debug version */
14114bf50f18SLuigi Rizzo #define	NM_CHECK_ADDR_LEN(_na, _a, _l)	do {				\
14124bf50f18SLuigi Rizzo 		if (_l > NETMAP_BUF_SIZE(_na))				\
14134bf50f18SLuigi Rizzo 			_l = NETMAP_BUF_SIZE(_na);			\
1414f9790aebSLuigi Rizzo 	} while (0)
1415f9790aebSLuigi Rizzo #endif
1416f9790aebSLuigi Rizzo 
1417f9790aebSLuigi Rizzo 
1418f9790aebSLuigi Rizzo /*---------------------------------------------------------------*/
1419f9790aebSLuigi Rizzo /*
14204bf50f18SLuigi Rizzo  * Support routines used by netmap subsystems
14214bf50f18SLuigi Rizzo  * (native drivers, VALE, generic, pipes, monitors, ...)
14224bf50f18SLuigi Rizzo  */
14234bf50f18SLuigi Rizzo 
14244bf50f18SLuigi Rizzo 
14254bf50f18SLuigi Rizzo /* common routine for all functions that create a netmap adapter. It performs
14264bf50f18SLuigi Rizzo  * two main tasks:
14274bf50f18SLuigi Rizzo  * - if the na points to an ifp, mark the ifp as netmap capable
14284bf50f18SLuigi Rizzo  *   using na as its native adapter;
14294bf50f18SLuigi Rizzo  * - provide defaults for the setup callbacks and the memory allocator
14304bf50f18SLuigi Rizzo  */
14314bf50f18SLuigi Rizzo int netmap_attach_common(struct netmap_adapter *);
14324bf50f18SLuigi Rizzo /* fill priv->np_[tr]xq{first,last} using the ringid and flags information
14332ff91c17SVincenzo Maffione  * coming from a struct nmreq_register
14344bf50f18SLuigi Rizzo  */
14352ff91c17SVincenzo Maffione int netmap_interp_ringid(struct netmap_priv_d *priv, uint32_t nr_mode,
14362ff91c17SVincenzo Maffione 			uint16_t nr_ringid, uint64_t nr_flags);
14374bf50f18SLuigi Rizzo /* update the ring parameters (number and size of tx and rx rings).
14384bf50f18SLuigi Rizzo  * It calls the nm_config callback, if available.
1439f9790aebSLuigi Rizzo  */
1440f9790aebSLuigi Rizzo int netmap_update_config(struct netmap_adapter *na);
14414bf50f18SLuigi Rizzo /* create and initialize the common fields of the krings array.
14424bf50f18SLuigi Rizzo  * using the information that must be already available in the na.
14434bf50f18SLuigi Rizzo  * tailroom can be used to request the allocation of additional
14444bf50f18SLuigi Rizzo  * tailroom bytes after the krings array. This is used by
14454bf50f18SLuigi Rizzo  * netmap_vp_adapter's (i.e., VALE ports) to make room for
14464bf50f18SLuigi Rizzo  * leasing-related data structures
14474bf50f18SLuigi Rizzo  */
1448f0ea3689SLuigi Rizzo int netmap_krings_create(struct netmap_adapter *na, u_int tailroom);
14494bf50f18SLuigi Rizzo /* deletes the kring array of the adapter. The array must have
14504bf50f18SLuigi Rizzo  * been created using netmap_krings_create
14514bf50f18SLuigi Rizzo  */
1452f9790aebSLuigi Rizzo void netmap_krings_delete(struct netmap_adapter *na);
145317885a7bSLuigi Rizzo 
145437e3a6d3SLuigi Rizzo int netmap_hw_krings_create(struct netmap_adapter *na);
145537e3a6d3SLuigi Rizzo void netmap_hw_krings_delete(struct netmap_adapter *na);
145637e3a6d3SLuigi Rizzo 
14574bf50f18SLuigi Rizzo /* set the stopped/enabled status of ring
14584bf50f18SLuigi Rizzo  * When stopping, they also wait for all current activity on the ring to
14594bf50f18SLuigi Rizzo  * terminate. The status change is then notified using the na nm_notify
14604bf50f18SLuigi Rizzo  * callback.
14614bf50f18SLuigi Rizzo  */
1462847bf383SLuigi Rizzo void netmap_set_ring(struct netmap_adapter *, u_int ring_id, enum txrx, int stopped);
14634bf50f18SLuigi Rizzo /* set the stopped/enabled status of all rings of the adapter. */
14644bf50f18SLuigi Rizzo void netmap_set_all_rings(struct netmap_adapter *, int stopped);
146537e3a6d3SLuigi Rizzo /* convenience wrappers for netmap_set_all_rings */
14664bf50f18SLuigi Rizzo void netmap_disable_all_rings(struct ifnet *);
14674bf50f18SLuigi Rizzo void netmap_enable_all_rings(struct ifnet *);
14684bf50f18SLuigi Rizzo 
146977a2baf5SVincenzo Maffione int netmap_buf_size_validate(const struct netmap_adapter *na, unsigned mtu);
14708fd44c93SLuigi Rizzo int netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na,
14712ff91c17SVincenzo Maffione 		uint32_t nr_mode, uint16_t nr_ringid, uint64_t nr_flags);
147237e3a6d3SLuigi Rizzo void netmap_do_unregif(struct netmap_priv_d *priv);
1473f9790aebSLuigi Rizzo 
1474f9790aebSLuigi Rizzo u_int nm_bound_var(u_int *v, u_int dflt, u_int lo, u_int hi, const char *msg);
14752ff91c17SVincenzo Maffione int netmap_get_na(struct nmreq_header *hdr, struct netmap_adapter **na,
1476c3e9b4dbSLuiz Otavio O Souza 		struct ifnet **ifp, struct netmap_mem_d *nmd, int create);
147737e3a6d3SLuigi Rizzo void netmap_unget_na(struct netmap_adapter *na, struct ifnet *ifp);
1478c3e9b4dbSLuiz Otavio O Souza int netmap_get_hw_na(struct ifnet *ifp,
1479c3e9b4dbSLuiz Otavio O Souza 		struct netmap_mem_d *nmd, struct netmap_adapter **na);
1480f9790aebSLuigi Rizzo 
14812a7db7a6SVincenzo Maffione #ifdef WITH_VALE
1482b6e66be2SVincenzo Maffione uint32_t netmap_vale_learning(struct nm_bdg_fwd *ft, uint8_t *dst_ring,
14832a7db7a6SVincenzo Maffione 		struct netmap_vp_adapter *, void *private_data);
14842a7db7a6SVincenzo Maffione 
14852a7db7a6SVincenzo Maffione /* these are redefined in case of no VALE support */
14862a7db7a6SVincenzo Maffione int netmap_get_vale_na(struct nmreq_header *hdr, struct netmap_adapter **na,
14872a7db7a6SVincenzo Maffione 		struct netmap_mem_d *nmd, int create);
14882a7db7a6SVincenzo Maffione void *netmap_vale_create(const char *bdg_name, int *return_status);
14892a7db7a6SVincenzo Maffione int netmap_vale_destroy(const char *bdg_name, void *auth_token);
1490f9790aebSLuigi Rizzo 
1491f9790aebSLuigi Rizzo #else /* !WITH_VALE */
14922a7db7a6SVincenzo Maffione #define netmap_bdg_learning(_1, _2, _3, _4)	0
14932a7db7a6SVincenzo Maffione #define	netmap_get_vale_na(_1, _2, _3, _4)	0
14942a7db7a6SVincenzo Maffione #define netmap_bdg_create(_1, _2)	NULL
14952a7db7a6SVincenzo Maffione #define netmap_bdg_destroy(_1, _2)	0
1496f9790aebSLuigi Rizzo #endif /* !WITH_VALE */
1497f9790aebSLuigi Rizzo 
1498f0ea3689SLuigi Rizzo #ifdef WITH_PIPES
1499f0ea3689SLuigi Rizzo /* max number of pipes per device */
15004f80b14cSVincenzo Maffione #define NM_MAXPIPES	64	/* XXX this should probably be a sysctl */
1501f0ea3689SLuigi Rizzo void netmap_pipe_dealloc(struct netmap_adapter *);
15022ff91c17SVincenzo Maffione int netmap_get_pipe_na(struct nmreq_header *hdr, struct netmap_adapter **na,
1503c3e9b4dbSLuiz Otavio O Souza 			struct netmap_mem_d *nmd, int create);
1504f0ea3689SLuigi Rizzo #else /* !WITH_PIPES */
1505f0ea3689SLuigi Rizzo #define NM_MAXPIPES	0
1506847bf383SLuigi Rizzo #define netmap_pipe_alloc(_1, _2) 	0
1507f0ea3689SLuigi Rizzo #define netmap_pipe_dealloc(_1)
15082ff91c17SVincenzo Maffione #define netmap_get_pipe_na(hdr, _2, _3, _4)	\
15092ff91c17SVincenzo Maffione 	((strchr(hdr->nr_name, '{') != NULL || strchr(hdr->nr_name, '}') != NULL) ? EOPNOTSUPP : 0)
1510f0ea3689SLuigi Rizzo #endif
1511f0ea3689SLuigi Rizzo 
15124bf50f18SLuigi Rizzo #ifdef WITH_MONITOR
15132ff91c17SVincenzo Maffione int netmap_get_monitor_na(struct nmreq_header *hdr, struct netmap_adapter **na,
1514c3e9b4dbSLuiz Otavio O Souza 		struct netmap_mem_d *nmd, int create);
1515847bf383SLuigi Rizzo void netmap_monitor_stop(struct netmap_adapter *na);
15164bf50f18SLuigi Rizzo #else
15172ff91c17SVincenzo Maffione #define netmap_get_monitor_na(hdr, _2, _3, _4) \
1518cfa866f6SMatt Macy 	(((struct nmreq_register *)(uintptr_t)hdr->nr_body)->nr_flags & (NR_MONITOR_TX | NR_MONITOR_RX) ? EOPNOTSUPP : 0)
1519847bf383SLuigi Rizzo #endif
1520847bf383SLuigi Rizzo 
1521b6e66be2SVincenzo Maffione #ifdef WITH_NMNULL
1522b6e66be2SVincenzo Maffione int netmap_get_null_na(struct nmreq_header *hdr, struct netmap_adapter **na,
1523b6e66be2SVincenzo Maffione 		struct netmap_mem_d *nmd, int create);
1524b6e66be2SVincenzo Maffione #else /* !WITH_NMNULL */
1525b6e66be2SVincenzo Maffione #define netmap_get_null_na(hdr, _2, _3, _4) \
1526b6e66be2SVincenzo Maffione 	(((struct nmreq_register *)(uintptr_t)hdr->nr_body)->nr_flags & (NR_MONITOR_TX | NR_MONITOR_RX) ? EOPNOTSUPP : 0)
1527b6e66be2SVincenzo Maffione #endif /* WITH_NMNULL */
1528b6e66be2SVincenzo Maffione 
1529847bf383SLuigi Rizzo #ifdef CONFIG_NET_NS
1530847bf383SLuigi Rizzo struct net *netmap_bns_get(void);
1531847bf383SLuigi Rizzo void netmap_bns_put(struct net *);
1532847bf383SLuigi Rizzo void netmap_bns_getbridges(struct nm_bridge **, u_int *);
1533847bf383SLuigi Rizzo #else
1534b6e66be2SVincenzo Maffione extern struct nm_bridge *nm_bridges;
1535847bf383SLuigi Rizzo #define netmap_bns_get()
1536847bf383SLuigi Rizzo #define netmap_bns_put(_1)
1537847bf383SLuigi Rizzo #define netmap_bns_getbridges(b, n) \
1538847bf383SLuigi Rizzo 	do { *b = nm_bridges; *n = NM_BRIDGES; } while (0)
15394bf50f18SLuigi Rizzo #endif
15404bf50f18SLuigi Rizzo 
1541f9790aebSLuigi Rizzo /* Various prototypes */
154237e3a6d3SLuigi Rizzo int netmap_poll(struct netmap_priv_d *, int events, NM_SELRECORD_T *td);
1543f9790aebSLuigi Rizzo int netmap_init(void);
1544f9790aebSLuigi Rizzo void netmap_fini(void);
1545f9790aebSLuigi Rizzo int netmap_get_memory(struct netmap_priv_d* p);
1546f9790aebSLuigi Rizzo void netmap_dtor(void *data);
1547f9790aebSLuigi Rizzo 
15482ff91c17SVincenzo Maffione int netmap_ioctl(struct netmap_priv_d *priv, u_long cmd, caddr_t data,
15492ff91c17SVincenzo Maffione 		struct thread *, int nr_body_is_user);
15502ff91c17SVincenzo Maffione int netmap_ioctl_legacy(struct netmap_priv_d *priv, u_long cmd, caddr_t data,
15512ff91c17SVincenzo Maffione 			struct thread *td);
15522ff91c17SVincenzo Maffione size_t nmreq_size_by_type(uint16_t nr_reqtype);
1553f9790aebSLuigi Rizzo 
1554f9790aebSLuigi Rizzo /* netmap_adapter creation/destruction */
155517885a7bSLuigi Rizzo 
155617885a7bSLuigi Rizzo // #define NM_DEBUG_PUTGET 1
1557f9790aebSLuigi Rizzo 
1558f9790aebSLuigi Rizzo #ifdef NM_DEBUG_PUTGET
1559f9790aebSLuigi Rizzo 
1560f9790aebSLuigi Rizzo #define NM_DBG(f) __##f
1561f9790aebSLuigi Rizzo 
1562f9790aebSLuigi Rizzo void __netmap_adapter_get(struct netmap_adapter *na);
1563f9790aebSLuigi Rizzo 
1564f9790aebSLuigi Rizzo #define netmap_adapter_get(na) 				\
1565f9790aebSLuigi Rizzo 	do {						\
1566f9790aebSLuigi Rizzo 		struct netmap_adapter *__na = na;	\
15674bf50f18SLuigi Rizzo 		D("getting %p:%s (%d)", __na, (__na)->name, (__na)->na_refcount);	\
1568f9790aebSLuigi Rizzo 		__netmap_adapter_get(__na);		\
1569f9790aebSLuigi Rizzo 	} while (0)
1570f9790aebSLuigi Rizzo 
1571f9790aebSLuigi Rizzo int __netmap_adapter_put(struct netmap_adapter *na);
1572f9790aebSLuigi Rizzo 
1573f9790aebSLuigi Rizzo #define netmap_adapter_put(na)				\
1574fb25194fSLuigi Rizzo 	({						\
1575f9790aebSLuigi Rizzo 		struct netmap_adapter *__na = na;	\
15764bf50f18SLuigi Rizzo 		D("putting %p:%s (%d)", __na, (__na)->name, (__na)->na_refcount);	\
1577f9790aebSLuigi Rizzo 		__netmap_adapter_put(__na);		\
1578fb25194fSLuigi Rizzo 	})
1579f9790aebSLuigi Rizzo 
1580f9790aebSLuigi Rizzo #else /* !NM_DEBUG_PUTGET */
1581f9790aebSLuigi Rizzo 
1582f9790aebSLuigi Rizzo #define NM_DBG(f) f
1583f9790aebSLuigi Rizzo void netmap_adapter_get(struct netmap_adapter *na);
1584f9790aebSLuigi Rizzo int netmap_adapter_put(struct netmap_adapter *na);
1585f9790aebSLuigi Rizzo 
1586f9790aebSLuigi Rizzo #endif /* !NM_DEBUG_PUTGET */
1587f9790aebSLuigi Rizzo 
1588f9790aebSLuigi Rizzo 
158917885a7bSLuigi Rizzo /*
159017885a7bSLuigi Rizzo  * module variables
159117885a7bSLuigi Rizzo  */
159237e3a6d3SLuigi Rizzo #define NETMAP_BUF_BASE(_na)	((_na)->na_lut.lut[0].vaddr)
159337e3a6d3SLuigi Rizzo #define NETMAP_BUF_SIZE(_na)	((_na)->na_lut.objsize)
15945819da83SLuigi Rizzo extern int netmap_no_pendintr;
1595a2a74091SLuigi Rizzo extern int netmap_mitigate;
1596b6e66be2SVincenzo Maffione extern int netmap_verbose;
1597b6e66be2SVincenzo Maffione #ifdef CONFIG_NETMAP_DEBUG
1598b6e66be2SVincenzo Maffione extern int netmap_debug;		/* for debugging */
1599b6e66be2SVincenzo Maffione #else /* !CONFIG_NETMAP_DEBUG */
1600b6e66be2SVincenzo Maffione #define netmap_debug (0)
1601b6e66be2SVincenzo Maffione #endif /* !CONFIG_NETMAP_DEBUG */
1602b6e66be2SVincenzo Maffione enum {                                  /* debug flags */
1603b6e66be2SVincenzo Maffione 	NM_DEBUG_ON = 1,		/* generic debug messsages */
1604b6e66be2SVincenzo Maffione 	NM_DEBUG_HOST = 0x2,            /* debug host stack */
1605b6e66be2SVincenzo Maffione 	NM_DEBUG_RXSYNC = 0x10,         /* debug on rxsync/txsync */
1606b6e66be2SVincenzo Maffione 	NM_DEBUG_TXSYNC = 0x20,
1607b6e66be2SVincenzo Maffione 	NM_DEBUG_RXINTR = 0x100,        /* debug on rx/tx intr (driver) */
1608b6e66be2SVincenzo Maffione 	NM_DEBUG_TXINTR = 0x200,
1609b6e66be2SVincenzo Maffione 	NM_DEBUG_NIC_RXSYNC = 0x1000,   /* debug on rx/tx intr (driver) */
1610b6e66be2SVincenzo Maffione 	NM_DEBUG_NIC_TXSYNC = 0x2000,
1611b6e66be2SVincenzo Maffione 	NM_DEBUG_MEM = 0x4000,		/* verbose memory allocations/deallocations */
1612b6e66be2SVincenzo Maffione 	NM_DEBUG_VALE = 0x8000,		/* debug messages from memory allocators */
1613b6e66be2SVincenzo Maffione 	NM_DEBUG_BDG = NM_DEBUG_VALE,
161468b8534bSLuigi Rizzo };
161568b8534bSLuigi Rizzo 
1616f9790aebSLuigi Rizzo extern int netmap_txsync_retry;
161737e3a6d3SLuigi Rizzo extern int netmap_flags;
16182a7db7a6SVincenzo Maffione extern int netmap_generic_hwcsum;
1619f9790aebSLuigi Rizzo extern int netmap_generic_mit;
1620f9790aebSLuigi Rizzo extern int netmap_generic_ringsize;
1621f0ea3689SLuigi Rizzo extern int netmap_generic_rings;
16224f80b14cSVincenzo Maffione #ifdef linux
162337e3a6d3SLuigi Rizzo extern int netmap_generic_txqdisc;
16244f80b14cSVincenzo Maffione #endif
1625f9790aebSLuigi Rizzo 
162668b8534bSLuigi Rizzo /*
16272a7db7a6SVincenzo Maffione  * NA returns a pointer to the struct netmap adapter from the ifp.
16282a7db7a6SVincenzo Maffione  * WNA is os-specific and must be defined in glue code.
162968b8534bSLuigi Rizzo  */
1630d0c7b075SLuigi Rizzo #define	NA(_ifp)	((struct netmap_adapter *)WNA(_ifp))
163168b8534bSLuigi Rizzo 
16328241616dSLuigi Rizzo /*
16332a7db7a6SVincenzo Maffione  * we provide a default implementation of NM_ATTACH_NA/NM_DETACH_NA
16342a7db7a6SVincenzo Maffione  * based on the WNA field.
16352a7db7a6SVincenzo Maffione  * Glue code may override this by defining its own NM_ATTACH_NA
16362a7db7a6SVincenzo Maffione  */
16372a7db7a6SVincenzo Maffione #ifndef NM_ATTACH_NA
16382a7db7a6SVincenzo Maffione /*
163937e3a6d3SLuigi Rizzo  * On old versions of FreeBSD, NA(ifp) is a pspare. On linux we
164037e3a6d3SLuigi Rizzo  * overload another pointer in the netdev.
164137e3a6d3SLuigi Rizzo  *
164237e3a6d3SLuigi Rizzo  * We check if NA(ifp) is set and its first element has a related
16438241616dSLuigi Rizzo  * magic value. The capenable is within the struct netmap_adapter.
16448241616dSLuigi Rizzo  */
16458241616dSLuigi Rizzo #define	NETMAP_MAGIC	0x52697a7a
16468241616dSLuigi Rizzo 
164737e3a6d3SLuigi Rizzo #define NM_NA_VALID(ifp)	(NA(ifp) &&		\
16488241616dSLuigi Rizzo 	((uint32_t)(uintptr_t)NA(ifp) ^ NA(ifp)->magic) == NETMAP_MAGIC )
16498241616dSLuigi Rizzo 
165037e3a6d3SLuigi Rizzo #define	NM_ATTACH_NA(ifp, na) do {					\
165137e3a6d3SLuigi Rizzo 	WNA(ifp) = na;							\
165237e3a6d3SLuigi Rizzo 	if (NA(ifp))							\
165337e3a6d3SLuigi Rizzo 		NA(ifp)->magic = 					\
165437e3a6d3SLuigi Rizzo 			((uint32_t)(uintptr_t)NA(ifp)) ^ NETMAP_MAGIC;	\
165537e3a6d3SLuigi Rizzo } while(0)
16562a7db7a6SVincenzo Maffione #define NM_RESTORE_NA(ifp, na) 	WNA(ifp) = na;
16572a7db7a6SVincenzo Maffione 
16582a7db7a6SVincenzo Maffione #define NM_DETACH_NA(ifp)	do { WNA(ifp) = NULL; } while (0)
16592a7db7a6SVincenzo Maffione #define NM_NA_CLASH(ifp)	(NA(ifp) && !NM_NA_VALID(ifp))
16602a7db7a6SVincenzo Maffione #endif /* !NM_ATTACH_NA */
16612a7db7a6SVincenzo Maffione 
16628241616dSLuigi Rizzo 
166337e3a6d3SLuigi Rizzo #define NM_IS_NATIVE(ifp)	(NM_NA_VALID(ifp) && NA(ifp)->nm_dtor == netmap_hw_dtor)
166468b8534bSLuigi Rizzo 
166537e3a6d3SLuigi Rizzo #if defined(__FreeBSD__)
1666f9790aebSLuigi Rizzo 
16674bf50f18SLuigi Rizzo /* Assigns the device IOMMU domain to an allocator.
16684bf50f18SLuigi Rizzo  * Returns -ENOMEM in case the domain is different */
16694bf50f18SLuigi Rizzo #define nm_iommu_group_id(dev) (0)
16704bf50f18SLuigi Rizzo 
167117885a7bSLuigi Rizzo /* Callback invoked by the dma machinery after a successful dmamap_load */
16726dba29a2SLuigi Rizzo static void netmap_dmamap_cb(__unused void *arg,
16736dba29a2SLuigi Rizzo     __unused bus_dma_segment_t * segs, __unused int nseg, __unused int error)
16746dba29a2SLuigi Rizzo {
16756dba29a2SLuigi Rizzo }
16766dba29a2SLuigi Rizzo 
16776dba29a2SLuigi Rizzo /* bus_dmamap_load wrapper: call aforementioned function if map != NULL.
16786dba29a2SLuigi Rizzo  * XXX can we do it without a callback ?
16796dba29a2SLuigi Rizzo  */
16804f80b14cSVincenzo Maffione static inline int
16814bf50f18SLuigi Rizzo netmap_load_map(struct netmap_adapter *na,
16824bf50f18SLuigi Rizzo 	bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
16836dba29a2SLuigi Rizzo {
16846dba29a2SLuigi Rizzo 	if (map)
16854bf50f18SLuigi Rizzo 		bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE(na),
16866dba29a2SLuigi Rizzo 		    netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
16874f80b14cSVincenzo Maffione 	return 0;
16886dba29a2SLuigi Rizzo }
16896dba29a2SLuigi Rizzo 
16904bf50f18SLuigi Rizzo static inline void
16914bf50f18SLuigi Rizzo netmap_unload_map(struct netmap_adapter *na,
16924bf50f18SLuigi Rizzo         bus_dma_tag_t tag, bus_dmamap_t map)
16934bf50f18SLuigi Rizzo {
16944bf50f18SLuigi Rizzo 	if (map)
16954bf50f18SLuigi Rizzo 		bus_dmamap_unload(tag, map);
16964bf50f18SLuigi Rizzo }
16974bf50f18SLuigi Rizzo 
16984f80b14cSVincenzo Maffione #define netmap_sync_map(na, tag, map, sz, t)
16994f80b14cSVincenzo Maffione 
17006dba29a2SLuigi Rizzo /* update the map when a buffer changes. */
17016dba29a2SLuigi Rizzo static inline void
17024bf50f18SLuigi Rizzo netmap_reload_map(struct netmap_adapter *na,
17034bf50f18SLuigi Rizzo 	bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
17046dba29a2SLuigi Rizzo {
17056dba29a2SLuigi Rizzo 	if (map) {
17066dba29a2SLuigi Rizzo 		bus_dmamap_unload(tag, map);
17074bf50f18SLuigi Rizzo 		bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE(na),
17086dba29a2SLuigi Rizzo 		    netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
17096dba29a2SLuigi Rizzo 	}
17106dba29a2SLuigi Rizzo }
1711f9790aebSLuigi Rizzo 
171237e3a6d3SLuigi Rizzo #elif defined(_WIN32)
171337e3a6d3SLuigi Rizzo 
1714f196ce38SLuigi Rizzo #else /* linux */
1715f196ce38SLuigi Rizzo 
17164bf50f18SLuigi Rizzo int nm_iommu_group_id(bus_dma_tag_t dev);
17174bf50f18SLuigi Rizzo #include <linux/dma-mapping.h>
17184bf50f18SLuigi Rizzo 
1719f196ce38SLuigi Rizzo /*
1720f196ce38SLuigi Rizzo  * on linux we need
1721f196ce38SLuigi Rizzo  *	dma_map_single(&pdev->dev, virt_addr, len, direction)
17224f80b14cSVincenzo Maffione  *	dma_unmap_single(&adapter->pdev->dev, phys_addr, len, direction)
1723f196ce38SLuigi Rizzo  */
1724f196ce38SLuigi Rizzo #if 0
1725f196ce38SLuigi Rizzo 	struct e1000_buffer *buffer_info =  &tx_ring->buffer_info[l];
1726f196ce38SLuigi Rizzo 	/* set time_stamp *before* dma to help avoid a possible race */
1727f196ce38SLuigi Rizzo 	buffer_info->time_stamp = jiffies;
1728f196ce38SLuigi Rizzo 	buffer_info->mapped_as_page = false;
1729f196ce38SLuigi Rizzo 	buffer_info->length = len;
1730f196ce38SLuigi Rizzo 	//buffer_info->next_to_watch = l;
1731f196ce38SLuigi Rizzo 	/* reload dma map */
1732f196ce38SLuigi Rizzo 	dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
1733f196ce38SLuigi Rizzo 			NETMAP_BUF_SIZE, DMA_TO_DEVICE);
1734f196ce38SLuigi Rizzo 	buffer_info->dma = dma_map_single(&adapter->pdev->dev,
1735f196ce38SLuigi Rizzo 			addr, NETMAP_BUF_SIZE, DMA_TO_DEVICE);
1736f196ce38SLuigi Rizzo 
1737f196ce38SLuigi Rizzo 	if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
1738f196ce38SLuigi Rizzo 		D("dma mapping error");
1739f196ce38SLuigi Rizzo 		/* goto dma_error; See e1000_put_txbuf() */
1740f196ce38SLuigi Rizzo 		/* XXX reset */
1741f196ce38SLuigi Rizzo 	}
1742f196ce38SLuigi Rizzo 	tx_desc->buffer_addr = htole64(buffer_info->dma); //XXX
1743f196ce38SLuigi Rizzo 
1744f196ce38SLuigi Rizzo #endif
1745f196ce38SLuigi Rizzo 
17464f80b14cSVincenzo Maffione static inline int
17474f80b14cSVincenzo Maffione netmap_load_map(struct netmap_adapter *na,
17484f80b14cSVincenzo Maffione 	bus_dma_tag_t tag, bus_dmamap_t map, void *buf, u_int size)
17494f80b14cSVincenzo Maffione {
17504f80b14cSVincenzo Maffione 	if (map) {
17514f80b14cSVincenzo Maffione 		*map = dma_map_single(na->pdev, buf, size,
17524f80b14cSVincenzo Maffione 				      DMA_BIDIRECTIONAL);
17534f80b14cSVincenzo Maffione 		if (dma_mapping_error(na->pdev, *map)) {
17544f80b14cSVincenzo Maffione 			*map = 0;
17554f80b14cSVincenzo Maffione 			return ENOMEM;
17564f80b14cSVincenzo Maffione 		}
17574f80b14cSVincenzo Maffione 	}
17584f80b14cSVincenzo Maffione 	return 0;
17594f80b14cSVincenzo Maffione }
17604f80b14cSVincenzo Maffione 
17614f80b14cSVincenzo Maffione static inline void
17624f80b14cSVincenzo Maffione netmap_unload_map(struct netmap_adapter *na,
17634f80b14cSVincenzo Maffione 	bus_dma_tag_t tag, bus_dmamap_t map, u_int sz)
17644f80b14cSVincenzo Maffione {
17654f80b14cSVincenzo Maffione 	if (*map) {
17664f80b14cSVincenzo Maffione 		dma_unmap_single(na->pdev, *map, sz,
17674f80b14cSVincenzo Maffione 				 DMA_BIDIRECTIONAL);
17684f80b14cSVincenzo Maffione 	}
17694f80b14cSVincenzo Maffione }
17704f80b14cSVincenzo Maffione 
17712a7db7a6SVincenzo Maffione #ifdef NETMAP_LINUX_HAVE_DMASYNC
17724f80b14cSVincenzo Maffione static inline void
17732a7db7a6SVincenzo Maffione netmap_sync_map_cpu(struct netmap_adapter *na,
17744f80b14cSVincenzo Maffione 	bus_dma_tag_t tag, bus_dmamap_t map, u_int sz, enum txrx t)
17754f80b14cSVincenzo Maffione {
17764f80b14cSVincenzo Maffione 	if (*map) {
17774f80b14cSVincenzo Maffione 		dma_sync_single_for_cpu(na->pdev, *map, sz,
17782a7db7a6SVincenzo Maffione 			(t == NR_TX ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
17792a7db7a6SVincenzo Maffione 	}
17802a7db7a6SVincenzo Maffione }
17812a7db7a6SVincenzo Maffione 
17822a7db7a6SVincenzo Maffione static inline void
17832a7db7a6SVincenzo Maffione netmap_sync_map_dev(struct netmap_adapter *na,
17842a7db7a6SVincenzo Maffione 	bus_dma_tag_t tag, bus_dmamap_t map, u_int sz, enum txrx t)
17852a7db7a6SVincenzo Maffione {
17862a7db7a6SVincenzo Maffione 	if (*map) {
17874f80b14cSVincenzo Maffione 		dma_sync_single_for_device(na->pdev, *map, sz,
17882a7db7a6SVincenzo Maffione 			(t == NR_TX ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
17894f80b14cSVincenzo Maffione 	}
17904f80b14cSVincenzo Maffione }
17914f80b14cSVincenzo Maffione 
17924f80b14cSVincenzo Maffione static inline void
17934f80b14cSVincenzo Maffione netmap_reload_map(struct netmap_adapter *na,
17944f80b14cSVincenzo Maffione 	bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
17954f80b14cSVincenzo Maffione {
17964f80b14cSVincenzo Maffione 	u_int sz = NETMAP_BUF_SIZE(na);
17974f80b14cSVincenzo Maffione 
17984f80b14cSVincenzo Maffione 	if (*map) {
17994f80b14cSVincenzo Maffione 		dma_unmap_single(na->pdev, *map, sz,
18004f80b14cSVincenzo Maffione 				DMA_BIDIRECTIONAL);
18014f80b14cSVincenzo Maffione 	}
18024f80b14cSVincenzo Maffione 
18034f80b14cSVincenzo Maffione 	*map = dma_map_single(na->pdev, buf, sz,
18044f80b14cSVincenzo Maffione 				DMA_BIDIRECTIONAL);
18054f80b14cSVincenzo Maffione }
18062a7db7a6SVincenzo Maffione #else /* !NETMAP_LINUX_HAVE_DMASYNC */
18072a7db7a6SVincenzo Maffione #define netmap_sync_map_cpu(na, tag, map, sz, t)
18082a7db7a6SVincenzo Maffione #define netmap_sync_map_dev(na, tag, map, sz, t)
18092a7db7a6SVincenzo Maffione #endif /* NETMAP_LINUX_HAVE_DMASYNC */
1810f196ce38SLuigi Rizzo 
1811f196ce38SLuigi Rizzo #endif /* linux */
18126dba29a2SLuigi Rizzo 
1813ce3ee1e7SLuigi Rizzo 
18145644ccecSLuigi Rizzo /*
18155644ccecSLuigi Rizzo  * functions to map NIC to KRING indexes (n2k) and vice versa (k2n)
18165644ccecSLuigi Rizzo  */
18175644ccecSLuigi Rizzo static inline int
181864ae02c3SLuigi Rizzo netmap_idx_n2k(struct netmap_kring *kr, int idx)
18195644ccecSLuigi Rizzo {
182064ae02c3SLuigi Rizzo 	int n = kr->nkr_num_slots;
1821b6e66be2SVincenzo Maffione 
1822b6e66be2SVincenzo Maffione 	if (likely(kr->nkr_hwofs == 0)) {
1823b6e66be2SVincenzo Maffione 		return idx;
1824b6e66be2SVincenzo Maffione 	}
1825b6e66be2SVincenzo Maffione 
182664ae02c3SLuigi Rizzo 	idx += kr->nkr_hwofs;
182764ae02c3SLuigi Rizzo 	if (idx < 0)
182864ae02c3SLuigi Rizzo 		return idx + n;
182964ae02c3SLuigi Rizzo 	else if (idx < n)
183064ae02c3SLuigi Rizzo 		return idx;
18315644ccecSLuigi Rizzo 	else
183264ae02c3SLuigi Rizzo 		return idx - n;
18335644ccecSLuigi Rizzo }
18345644ccecSLuigi Rizzo 
18355644ccecSLuigi Rizzo 
18365644ccecSLuigi Rizzo static inline int
183764ae02c3SLuigi Rizzo netmap_idx_k2n(struct netmap_kring *kr, int idx)
18385644ccecSLuigi Rizzo {
183964ae02c3SLuigi Rizzo 	int n = kr->nkr_num_slots;
1840b6e66be2SVincenzo Maffione 
1841b6e66be2SVincenzo Maffione 	if (likely(kr->nkr_hwofs == 0)) {
1842b6e66be2SVincenzo Maffione 		return idx;
1843b6e66be2SVincenzo Maffione 	}
1844b6e66be2SVincenzo Maffione 
184564ae02c3SLuigi Rizzo 	idx -= kr->nkr_hwofs;
184664ae02c3SLuigi Rizzo 	if (idx < 0)
184764ae02c3SLuigi Rizzo 		return idx + n;
184864ae02c3SLuigi Rizzo 	else if (idx < n)
184964ae02c3SLuigi Rizzo 		return idx;
18505644ccecSLuigi Rizzo 	else
185164ae02c3SLuigi Rizzo 		return idx - n;
18525644ccecSLuigi Rizzo }
18535644ccecSLuigi Rizzo 
18545644ccecSLuigi Rizzo 
1855d76bf4ffSLuigi Rizzo /* Entries of the look-up table. */
18564f80b14cSVincenzo Maffione #ifdef __FreeBSD__
1857d76bf4ffSLuigi Rizzo struct lut_entry {
1858d76bf4ffSLuigi Rizzo 	void *vaddr;		/* virtual address. */
1859849bec0eSLuigi Rizzo 	vm_paddr_t paddr;	/* physical address. */
1860d76bf4ffSLuigi Rizzo };
18614f80b14cSVincenzo Maffione #else /* linux & _WIN32 */
18624f80b14cSVincenzo Maffione /* dma-mapping in linux can assign a buffer a different address
18634f80b14cSVincenzo Maffione  * depending on the device, so we need to have a separate
18644f80b14cSVincenzo Maffione  * physical-address look-up table for each na.
18654f80b14cSVincenzo Maffione  * We can still share the vaddrs, though, therefore we split
18664f80b14cSVincenzo Maffione  * the lut_entry structure.
18674f80b14cSVincenzo Maffione  */
18684f80b14cSVincenzo Maffione struct lut_entry {
18694f80b14cSVincenzo Maffione 	void *vaddr;		/* virtual address. */
18704f80b14cSVincenzo Maffione };
18714f80b14cSVincenzo Maffione 
18724f80b14cSVincenzo Maffione struct plut_entry {
18734f80b14cSVincenzo Maffione 	vm_paddr_t paddr;	/* physical address. */
18744f80b14cSVincenzo Maffione };
18754f80b14cSVincenzo Maffione #endif /* linux & _WIN32 */
1876d76bf4ffSLuigi Rizzo 
1877d76bf4ffSLuigi Rizzo struct netmap_obj_pool;
1878d76bf4ffSLuigi Rizzo 
187968b8534bSLuigi Rizzo /*
18806e10c8b8SLuigi Rizzo  * NMB return the virtual address of a buffer (buffer 0 on bad index)
18816e10c8b8SLuigi Rizzo  * PNMB also fills the physical address
188268b8534bSLuigi Rizzo  */
18836e10c8b8SLuigi Rizzo static inline void *
18844bf50f18SLuigi Rizzo NMB(struct netmap_adapter *na, struct netmap_slot *slot)
1885f9790aebSLuigi Rizzo {
1886847bf383SLuigi Rizzo 	struct lut_entry *lut = na->na_lut.lut;
1887f9790aebSLuigi Rizzo 	uint32_t i = slot->buf_idx;
1888847bf383SLuigi Rizzo 	return (unlikely(i >= na->na_lut.objtotal)) ?
1889f9790aebSLuigi Rizzo 		lut[0].vaddr : lut[i].vaddr;
1890f9790aebSLuigi Rizzo }
1891f9790aebSLuigi Rizzo 
18924bf50f18SLuigi Rizzo static inline void *
18934bf50f18SLuigi Rizzo PNMB(struct netmap_adapter *na, struct netmap_slot *slot, uint64_t *pp)
18944bf50f18SLuigi Rizzo {
18954bf50f18SLuigi Rizzo 	uint32_t i = slot->buf_idx;
1896847bf383SLuigi Rizzo 	struct lut_entry *lut = na->na_lut.lut;
18974f80b14cSVincenzo Maffione 	struct plut_entry *plut = na->na_lut.plut;
1898847bf383SLuigi Rizzo 	void *ret = (i >= na->na_lut.objtotal) ? lut[0].vaddr : lut[i].vaddr;
18994bf50f18SLuigi Rizzo 
19004f80b14cSVincenzo Maffione #ifdef _WIN32
19014f80b14cSVincenzo Maffione 	*pp = (i >= na->na_lut.objtotal) ? (uint64_t)plut[0].paddr.QuadPart : (uint64_t)plut[i].paddr.QuadPart;
190237e3a6d3SLuigi Rizzo #else
19034f80b14cSVincenzo Maffione 	*pp = (i >= na->na_lut.objtotal) ? plut[0].paddr : plut[i].paddr;
190437e3a6d3SLuigi Rizzo #endif
19054bf50f18SLuigi Rizzo 	return ret;
19064bf50f18SLuigi Rizzo }
19074bf50f18SLuigi Rizzo 
1908f9790aebSLuigi Rizzo 
190917885a7bSLuigi Rizzo /*
19108fd44c93SLuigi Rizzo  * Structure associated to each netmap file descriptor.
19118fd44c93SLuigi Rizzo  * It is created on open and left unbound (np_nifp == NULL).
19128fd44c93SLuigi Rizzo  * A successful NIOCREGIF will set np_nifp and the first few fields;
19138fd44c93SLuigi Rizzo  * this is protected by a global lock (NMG_LOCK) due to low contention.
1914f9790aebSLuigi Rizzo  *
19158fd44c93SLuigi Rizzo  * np_refs counts the number of references to the structure: one for the fd,
19168fd44c93SLuigi Rizzo  * plus (on FreeBSD) one for each active mmap which we track ourselves
191785fe4e7cSLuigi Rizzo  * (linux automatically tracks them, but FreeBSD does not).
19188fd44c93SLuigi Rizzo  * np_refs is protected by NMG_LOCK.
191917885a7bSLuigi Rizzo  *
19208fd44c93SLuigi Rizzo  * Read access to the structure is lock free, because ni_nifp once set
19218fd44c93SLuigi Rizzo  * can only go to 0 when nobody is using the entry anymore. Readers
19228fd44c93SLuigi Rizzo  * must check that np_nifp != NULL before using the other fields.
1923f9790aebSLuigi Rizzo  */
1924f9790aebSLuigi Rizzo struct netmap_priv_d {
1925f9790aebSLuigi Rizzo 	struct netmap_if * volatile np_nifp;	/* netmap if descriptor. */
1926f9790aebSLuigi Rizzo 
1927f9790aebSLuigi Rizzo 	struct netmap_adapter	*np_na;
192837e3a6d3SLuigi Rizzo 	struct ifnet	*np_ifp;
1929f0ea3689SLuigi Rizzo 	uint32_t	np_flags;	/* from the ioctl */
1930847bf383SLuigi Rizzo 	u_int		np_qfirst[NR_TXRX],
1931847bf383SLuigi Rizzo 			np_qlast[NR_TXRX]; /* range of tx/rx rings to scan */
19324f80b14cSVincenzo Maffione 	uint16_t	np_txpoll;
1933b6e66be2SVincenzo Maffione 	uint16_t        np_kloop_state;	/* use with NMG_LOCK held */
1934b6e66be2SVincenzo Maffione #define NM_SYNC_KLOOP_RUNNING	(1 << 0)
1935b6e66be2SVincenzo Maffione #define NM_SYNC_KLOOP_STOPPING	(1 << 1)
1936c3e9b4dbSLuiz Otavio O Souza 	int             np_sync_flags; /* to be passed to nm_sync */
1937f9790aebSLuigi Rizzo 
19388fd44c93SLuigi Rizzo 	int		np_refs;	/* use with NMG_LOCK held */
1939f0ea3689SLuigi Rizzo 
1940f0ea3689SLuigi Rizzo 	/* pointers to the selinfo to be used for selrecord.
1941f0ea3689SLuigi Rizzo 	 * Either the local or the global one depending on the
1942f0ea3689SLuigi Rizzo 	 * number of rings.
1943f0ea3689SLuigi Rizzo 	 */
1944847bf383SLuigi Rizzo 	NM_SELINFO_T *np_si[NR_TXRX];
1945b6e66be2SVincenzo Maffione 
1946b6e66be2SVincenzo Maffione 	/* In the optional CSB mode, the user must specify the start address
1947b6e66be2SVincenzo Maffione 	 * of two arrays of Communication Status Block (CSB) entries, for the
1948b6e66be2SVincenzo Maffione 	 * two directions (kernel read application write, and kernel write
1949b6e66be2SVincenzo Maffione 	 * application read).
1950b6e66be2SVincenzo Maffione 	 * The number of entries must agree with the number of rings bound to
1951b6e66be2SVincenzo Maffione 	 * the netmap file descriptor. The entries corresponding to the TX
1952b6e66be2SVincenzo Maffione 	 * rings are laid out before the ones corresponding to the RX rings.
1953b6e66be2SVincenzo Maffione 	 *
1954b6e66be2SVincenzo Maffione 	 * Array of CSB entries for application --> kernel communication
1955b6e66be2SVincenzo Maffione 	 * (N entries). */
1956b6e66be2SVincenzo Maffione 	struct nm_csb_atok	*np_csb_atok_base;
1957b6e66be2SVincenzo Maffione 	/* Array of CSB entries for kernel --> application communication
1958b6e66be2SVincenzo Maffione 	 * (N entries). */
1959b6e66be2SVincenzo Maffione 	struct nm_csb_ktoa	*np_csb_ktoa_base;
1960b6e66be2SVincenzo Maffione 
1961b6e66be2SVincenzo Maffione #ifdef linux
1962b6e66be2SVincenzo Maffione 	struct file	*np_filp;  /* used by sync kloop */
1963b6e66be2SVincenzo Maffione #endif /* linux */
1964f9790aebSLuigi Rizzo };
1965f9790aebSLuigi Rizzo 
196637e3a6d3SLuigi Rizzo struct netmap_priv_d *netmap_priv_new(void);
196737e3a6d3SLuigi Rizzo void netmap_priv_delete(struct netmap_priv_d *);
196837e3a6d3SLuigi Rizzo 
196937e3a6d3SLuigi Rizzo static inline int nm_kring_pending(struct netmap_priv_d *np)
197037e3a6d3SLuigi Rizzo {
197137e3a6d3SLuigi Rizzo 	struct netmap_adapter *na = np->np_na;
197237e3a6d3SLuigi Rizzo 	enum txrx t;
197337e3a6d3SLuigi Rizzo 	int i;
197437e3a6d3SLuigi Rizzo 
197537e3a6d3SLuigi Rizzo 	for_rx_tx(t) {
197637e3a6d3SLuigi Rizzo 		for (i = np->np_qfirst[t]; i < np->np_qlast[t]; i++) {
19772ff91c17SVincenzo Maffione 			struct netmap_kring *kring = NMR(na, t)[i];
197837e3a6d3SLuigi Rizzo 			if (kring->nr_mode != kring->nr_pending_mode) {
197937e3a6d3SLuigi Rizzo 				return 1;
198037e3a6d3SLuigi Rizzo 			}
198137e3a6d3SLuigi Rizzo 		}
198237e3a6d3SLuigi Rizzo 	}
198337e3a6d3SLuigi Rizzo 	return 0;
198437e3a6d3SLuigi Rizzo }
198537e3a6d3SLuigi Rizzo 
1986b6e66be2SVincenzo Maffione /* call with NMG_LOCK held */
1987b6e66be2SVincenzo Maffione static __inline int
1988b6e66be2SVincenzo Maffione nm_si_user(struct netmap_priv_d *priv, enum txrx t)
1989b6e66be2SVincenzo Maffione {
1990b6e66be2SVincenzo Maffione 	return (priv->np_na != NULL &&
1991b6e66be2SVincenzo Maffione 		(priv->np_qlast[t] - priv->np_qfirst[t] > 1));
1992b6e66be2SVincenzo Maffione }
1993b6e66be2SVincenzo Maffione 
1994c3e9b4dbSLuiz Otavio O Souza #ifdef WITH_PIPES
1995c3e9b4dbSLuiz Otavio O Souza int netmap_pipe_txsync(struct netmap_kring *txkring, int flags);
1996c3e9b4dbSLuiz Otavio O Souza int netmap_pipe_rxsync(struct netmap_kring *rxkring, int flags);
1997c3e9b4dbSLuiz Otavio O Souza #endif /* WITH_PIPES */
1998c3e9b4dbSLuiz Otavio O Souza 
19994bf50f18SLuigi Rizzo #ifdef WITH_MONITOR
20004bf50f18SLuigi Rizzo 
20014bf50f18SLuigi Rizzo struct netmap_monitor_adapter {
20024bf50f18SLuigi Rizzo 	struct netmap_adapter up;
20034bf50f18SLuigi Rizzo 
20044bf50f18SLuigi Rizzo 	struct netmap_priv_d priv;
20054bf50f18SLuigi Rizzo 	uint32_t flags;
20064bf50f18SLuigi Rizzo };
20074bf50f18SLuigi Rizzo 
20084bf50f18SLuigi Rizzo #endif /* WITH_MONITOR */
20094bf50f18SLuigi Rizzo 
2010f9790aebSLuigi Rizzo 
2011039dd540SLuigi Rizzo #ifdef WITH_GENERIC
2012f9790aebSLuigi Rizzo /*
2013f9790aebSLuigi Rizzo  * generic netmap emulation for devices that do not have
2014f9790aebSLuigi Rizzo  * native netmap support.
2015f9790aebSLuigi Rizzo  */
2016f9790aebSLuigi Rizzo int generic_netmap_attach(struct ifnet *ifp);
201737e3a6d3SLuigi Rizzo int generic_rx_handler(struct ifnet *ifp, struct mbuf *m);;
2018f9790aebSLuigi Rizzo 
201937e3a6d3SLuigi Rizzo int nm_os_catch_rx(struct netmap_generic_adapter *gna, int intercept);
202037e3a6d3SLuigi Rizzo int nm_os_catch_tx(struct netmap_generic_adapter *gna, int intercept);
202137e3a6d3SLuigi Rizzo 
2022c3e9b4dbSLuiz Otavio O Souza int na_is_generic(struct netmap_adapter *na);
2023c3e9b4dbSLuiz Otavio O Souza 
202437e3a6d3SLuigi Rizzo /*
202537e3a6d3SLuigi Rizzo  * the generic transmit routine is passed a structure to optionally
202637e3a6d3SLuigi Rizzo  * build a queue of descriptors, in an OS-specific way.
202737e3a6d3SLuigi Rizzo  * The payload is at addr, if non-null, and the routine should send or queue
202837e3a6d3SLuigi Rizzo  * the packet, returning 0 if successful, 1 on failure.
202937e3a6d3SLuigi Rizzo  *
203037e3a6d3SLuigi Rizzo  * At the end, if head is non-null, there will be an additional call
203137e3a6d3SLuigi Rizzo  * to the function with addr = NULL; this should tell the OS-specific
203237e3a6d3SLuigi Rizzo  * routine to send the queue and free any resources. Failure is ignored.
203337e3a6d3SLuigi Rizzo  */
203437e3a6d3SLuigi Rizzo struct nm_os_gen_arg {
203537e3a6d3SLuigi Rizzo 	struct ifnet *ifp;
203637e3a6d3SLuigi Rizzo 	void *m;	/* os-specific mbuf-like object */
203737e3a6d3SLuigi Rizzo 	void *head, *tail; /* tailq, if the OS-specific routine needs to build one */
203837e3a6d3SLuigi Rizzo 	void *addr;	/* payload of current packet */
203937e3a6d3SLuigi Rizzo 	u_int len;	/* packet length */
204037e3a6d3SLuigi Rizzo 	u_int ring_nr;	/* packet length */
204137e3a6d3SLuigi Rizzo 	u_int qevent;   /* in txqdisc mode, place an event on this mbuf */
204237e3a6d3SLuigi Rizzo };
204337e3a6d3SLuigi Rizzo 
204437e3a6d3SLuigi Rizzo int nm_os_generic_xmit_frame(struct nm_os_gen_arg *);
204537e3a6d3SLuigi Rizzo int nm_os_generic_find_num_desc(struct ifnet *ifp, u_int *tx, u_int *rx);
204637e3a6d3SLuigi Rizzo void nm_os_generic_find_num_queues(struct ifnet *ifp, u_int *txq, u_int *rxq);
204737e3a6d3SLuigi Rizzo void nm_os_generic_set_features(struct netmap_generic_adapter *gna);
204837e3a6d3SLuigi Rizzo 
2049847bf383SLuigi Rizzo static inline struct ifnet*
2050847bf383SLuigi Rizzo netmap_generic_getifp(struct netmap_generic_adapter *gna)
2051847bf383SLuigi Rizzo {
2052847bf383SLuigi Rizzo         if (gna->prev)
2053847bf383SLuigi Rizzo             return gna->prev->ifp;
2054847bf383SLuigi Rizzo 
2055847bf383SLuigi Rizzo         return gna->up.up.ifp;
2056847bf383SLuigi Rizzo }
2057f9790aebSLuigi Rizzo 
205837e3a6d3SLuigi Rizzo void netmap_generic_irq(struct netmap_adapter *na, u_int q, u_int *work_done);
205937e3a6d3SLuigi Rizzo 
20604bf50f18SLuigi Rizzo //#define RATE_GENERIC  /* Enables communication statistics for generic. */
20614bf50f18SLuigi Rizzo #ifdef RATE_GENERIC
20624bf50f18SLuigi Rizzo void generic_rate(int txp, int txs, int txi, int rxp, int rxs, int rxi);
20634bf50f18SLuigi Rizzo #else
20644bf50f18SLuigi Rizzo #define generic_rate(txp, txs, txi, rxp, rxs, rxi)
20654bf50f18SLuigi Rizzo #endif
20664bf50f18SLuigi Rizzo 
2067f9790aebSLuigi Rizzo /*
2068f9790aebSLuigi Rizzo  * netmap_mitigation API. This is used by the generic adapter
2069f9790aebSLuigi Rizzo  * to reduce the number of interrupt requests/selwakeup
2070f9790aebSLuigi Rizzo  * to clients on incoming packets.
2071f9790aebSLuigi Rizzo  */
207237e3a6d3SLuigi Rizzo void nm_os_mitigation_init(struct nm_generic_mit *mit, int idx,
20734bf50f18SLuigi Rizzo                                 struct netmap_adapter *na);
207437e3a6d3SLuigi Rizzo void nm_os_mitigation_start(struct nm_generic_mit *mit);
207537e3a6d3SLuigi Rizzo void nm_os_mitigation_restart(struct nm_generic_mit *mit);
207637e3a6d3SLuigi Rizzo int nm_os_mitigation_active(struct nm_generic_mit *mit);
207737e3a6d3SLuigi Rizzo void nm_os_mitigation_cleanup(struct nm_generic_mit *mit);
207837e3a6d3SLuigi Rizzo #else /* !WITH_GENERIC */
207937e3a6d3SLuigi Rizzo #define generic_netmap_attach(ifp)	(EOPNOTSUPP)
2080c3e9b4dbSLuiz Otavio O Souza #define na_is_generic(na)		(0)
2081039dd540SLuigi Rizzo #endif /* WITH_GENERIC */
2082f0ea3689SLuigi Rizzo 
2083f0ea3689SLuigi Rizzo /* Shared declarations for the VALE switch. */
2084f0ea3689SLuigi Rizzo 
2085f0ea3689SLuigi Rizzo /*
2086f0ea3689SLuigi Rizzo  * Each transmit queue accumulates a batch of packets into
2087f0ea3689SLuigi Rizzo  * a structure before forwarding. Packets to the same
2088f0ea3689SLuigi Rizzo  * destination are put in a list using ft_next as a link field.
2089f0ea3689SLuigi Rizzo  * ft_frags and ft_next are valid only on the first fragment.
2090f0ea3689SLuigi Rizzo  */
2091f0ea3689SLuigi Rizzo struct nm_bdg_fwd {	/* forwarding entry for a bridge */
2092f0ea3689SLuigi Rizzo 	void *ft_buf;		/* netmap or indirect buffer */
2093f0ea3689SLuigi Rizzo 	uint8_t ft_frags;	/* how many fragments (only on 1st frag) */
20942ff91c17SVincenzo Maffione 	uint16_t ft_offset;	/* dst port (unused) */
2095f0ea3689SLuigi Rizzo 	uint16_t ft_flags;	/* flags, e.g. indirect */
2096f0ea3689SLuigi Rizzo 	uint16_t ft_len;	/* src fragment len */
2097f0ea3689SLuigi Rizzo 	uint16_t ft_next;	/* next packet to same destination */
2098f0ea3689SLuigi Rizzo };
2099f0ea3689SLuigi Rizzo 
2100f0ea3689SLuigi Rizzo /* struct 'virtio_net_hdr' from linux. */
2101f0ea3689SLuigi Rizzo struct nm_vnet_hdr {
2102f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_F_NEEDS_CSUM     1	/* Use csum_start, csum_offset */
2103f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_F_DATA_VALID    2	/* Csum is valid */
2104f0ea3689SLuigi Rizzo     uint8_t flags;
2105f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_NONE         0       /* Not a GSO frame */
2106f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_TCPV4        1       /* GSO frame, IPv4 TCP (TSO) */
2107f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_UDP          3       /* GSO frame, IPv4 UDP (UFO) */
2108f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_TCPV6        4       /* GSO frame, IPv6 TCP */
2109f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_ECN          0x80    /* TCP has ECN set */
2110f0ea3689SLuigi Rizzo     uint8_t gso_type;
2111f0ea3689SLuigi Rizzo     uint16_t hdr_len;
2112f0ea3689SLuigi Rizzo     uint16_t gso_size;
2113f0ea3689SLuigi Rizzo     uint16_t csum_start;
2114f0ea3689SLuigi Rizzo     uint16_t csum_offset;
2115f0ea3689SLuigi Rizzo };
2116f0ea3689SLuigi Rizzo 
2117f0ea3689SLuigi Rizzo #define WORST_CASE_GSO_HEADER	(14+40+60)  /* IPv6 + TCP */
2118f0ea3689SLuigi Rizzo 
2119f0ea3689SLuigi Rizzo /* Private definitions for IPv4, IPv6, UDP and TCP headers. */
2120f0ea3689SLuigi Rizzo 
2121f0ea3689SLuigi Rizzo struct nm_iphdr {
2122f0ea3689SLuigi Rizzo 	uint8_t		version_ihl;
2123f0ea3689SLuigi Rizzo 	uint8_t		tos;
2124f0ea3689SLuigi Rizzo 	uint16_t	tot_len;
2125f0ea3689SLuigi Rizzo 	uint16_t	id;
2126f0ea3689SLuigi Rizzo 	uint16_t	frag_off;
2127f0ea3689SLuigi Rizzo 	uint8_t		ttl;
2128f0ea3689SLuigi Rizzo 	uint8_t		protocol;
2129f0ea3689SLuigi Rizzo 	uint16_t	check;
2130f0ea3689SLuigi Rizzo 	uint32_t	saddr;
2131f0ea3689SLuigi Rizzo 	uint32_t	daddr;
2132f0ea3689SLuigi Rizzo 	/*The options start here. */
2133f0ea3689SLuigi Rizzo };
2134f0ea3689SLuigi Rizzo 
2135f0ea3689SLuigi Rizzo struct nm_tcphdr {
2136f0ea3689SLuigi Rizzo 	uint16_t	source;
2137f0ea3689SLuigi Rizzo 	uint16_t	dest;
2138f0ea3689SLuigi Rizzo 	uint32_t	seq;
2139f0ea3689SLuigi Rizzo 	uint32_t	ack_seq;
2140f0ea3689SLuigi Rizzo 	uint8_t		doff;  /* Data offset + Reserved */
2141f0ea3689SLuigi Rizzo 	uint8_t		flags;
2142f0ea3689SLuigi Rizzo 	uint16_t	window;
2143f0ea3689SLuigi Rizzo 	uint16_t	check;
2144f0ea3689SLuigi Rizzo 	uint16_t	urg_ptr;
2145f0ea3689SLuigi Rizzo };
2146f0ea3689SLuigi Rizzo 
2147f0ea3689SLuigi Rizzo struct nm_udphdr {
2148f0ea3689SLuigi Rizzo 	uint16_t	source;
2149f0ea3689SLuigi Rizzo 	uint16_t	dest;
2150f0ea3689SLuigi Rizzo 	uint16_t	len;
2151f0ea3689SLuigi Rizzo 	uint16_t	check;
2152f0ea3689SLuigi Rizzo };
2153f0ea3689SLuigi Rizzo 
2154f0ea3689SLuigi Rizzo struct nm_ipv6hdr {
2155f0ea3689SLuigi Rizzo 	uint8_t		priority_version;
2156f0ea3689SLuigi Rizzo 	uint8_t		flow_lbl[3];
2157f0ea3689SLuigi Rizzo 
2158f0ea3689SLuigi Rizzo 	uint16_t	payload_len;
2159f0ea3689SLuigi Rizzo 	uint8_t		nexthdr;
2160f0ea3689SLuigi Rizzo 	uint8_t		hop_limit;
2161f0ea3689SLuigi Rizzo 
2162f0ea3689SLuigi Rizzo 	uint8_t		saddr[16];
2163f0ea3689SLuigi Rizzo 	uint8_t		daddr[16];
2164f0ea3689SLuigi Rizzo };
2165f0ea3689SLuigi Rizzo 
2166f0ea3689SLuigi Rizzo /* Type used to store a checksum (in host byte order) that hasn't been
2167f0ea3689SLuigi Rizzo  * folded yet.
2168f0ea3689SLuigi Rizzo  */
2169f0ea3689SLuigi Rizzo #define rawsum_t uint32_t
2170f0ea3689SLuigi Rizzo 
217137e3a6d3SLuigi Rizzo rawsum_t nm_os_csum_raw(uint8_t *data, size_t len, rawsum_t cur_sum);
217237e3a6d3SLuigi Rizzo uint16_t nm_os_csum_ipv4(struct nm_iphdr *iph);
217337e3a6d3SLuigi Rizzo void nm_os_csum_tcpudp_ipv4(struct nm_iphdr *iph, void *data,
2174f0ea3689SLuigi Rizzo 		      size_t datalen, uint16_t *check);
217537e3a6d3SLuigi Rizzo void nm_os_csum_tcpudp_ipv6(struct nm_ipv6hdr *ip6h, void *data,
2176f0ea3689SLuigi Rizzo 		      size_t datalen, uint16_t *check);
217737e3a6d3SLuigi Rizzo uint16_t nm_os_csum_fold(rawsum_t cur_sum);
2178f0ea3689SLuigi Rizzo 
2179f0ea3689SLuigi Rizzo void bdg_mismatch_datapath(struct netmap_vp_adapter *na,
2180f0ea3689SLuigi Rizzo 			   struct netmap_vp_adapter *dst_na,
218137e3a6d3SLuigi Rizzo 			   const struct nm_bdg_fwd *ft_p,
218237e3a6d3SLuigi Rizzo 			   struct netmap_ring *dst_ring,
2183f0ea3689SLuigi Rizzo 			   u_int *j, u_int lim, u_int *howmany);
21844bf50f18SLuigi Rizzo 
21854bf50f18SLuigi Rizzo /* persistent virtual port routines */
218637e3a6d3SLuigi Rizzo int nm_os_vi_persist(const char *, struct ifnet **);
218737e3a6d3SLuigi Rizzo void nm_os_vi_detach(struct ifnet *);
218837e3a6d3SLuigi Rizzo void nm_os_vi_init_index(void);
218937e3a6d3SLuigi Rizzo 
219037e3a6d3SLuigi Rizzo /*
219137e3a6d3SLuigi Rizzo  * kernel thread routines
219237e3a6d3SLuigi Rizzo  */
2193c3e9b4dbSLuiz Otavio O Souza struct nm_kctx; /* OS-specific kernel context - opaque */
2194b6e66be2SVincenzo Maffione typedef void (*nm_kctx_worker_fn_t)(void *data);
219537e3a6d3SLuigi Rizzo 
219637e3a6d3SLuigi Rizzo /* kthread configuration */
2197c3e9b4dbSLuiz Otavio O Souza struct nm_kctx_cfg {
219837e3a6d3SLuigi Rizzo 	long			type;		/* kthread type/identifier */
2199c3e9b4dbSLuiz Otavio O Souza 	nm_kctx_worker_fn_t	worker_fn;	/* worker function */
220037e3a6d3SLuigi Rizzo 	void			*worker_private;/* worker parameter */
220137e3a6d3SLuigi Rizzo 	int			attach_user;	/* attach kthread to user process */
220237e3a6d3SLuigi Rizzo };
220337e3a6d3SLuigi Rizzo /* kthread configuration */
2204c3e9b4dbSLuiz Otavio O Souza struct nm_kctx *nm_os_kctx_create(struct nm_kctx_cfg *cfg,
2205844a6f0cSLuigi Rizzo 					void *opaque);
2206c3e9b4dbSLuiz Otavio O Souza int nm_os_kctx_worker_start(struct nm_kctx *);
2207c3e9b4dbSLuiz Otavio O Souza void nm_os_kctx_worker_stop(struct nm_kctx *);
2208c3e9b4dbSLuiz Otavio O Souza void nm_os_kctx_destroy(struct nm_kctx *);
2209c3e9b4dbSLuiz Otavio O Souza void nm_os_kctx_worker_setaff(struct nm_kctx *, int);
221037e3a6d3SLuigi Rizzo u_int nm_os_ncpus(void);
221137e3a6d3SLuigi Rizzo 
2212b6e66be2SVincenzo Maffione int netmap_sync_kloop(struct netmap_priv_d *priv,
2213b6e66be2SVincenzo Maffione 		      struct nmreq_header *hdr);
2214b6e66be2SVincenzo Maffione int netmap_sync_kloop_stop(struct netmap_priv_d *priv);
2215b6e66be2SVincenzo Maffione 
2216b6e66be2SVincenzo Maffione #ifdef WITH_PTNETMAP
2217b6e66be2SVincenzo Maffione /* ptnetmap guest routines */
2218b6e66be2SVincenzo Maffione 
221937e3a6d3SLuigi Rizzo /*
2220b6e66be2SVincenzo Maffione  * ptnetmap_memdev routines used to talk with ptnetmap_memdev device driver
222137e3a6d3SLuigi Rizzo  */
2222b6e66be2SVincenzo Maffione struct ptnetmap_memdev;
2223b6e66be2SVincenzo Maffione int nm_os_pt_memdev_iomap(struct ptnetmap_memdev *, vm_paddr_t *, void **,
2224b6e66be2SVincenzo Maffione                           uint64_t *);
2225b6e66be2SVincenzo Maffione void nm_os_pt_memdev_iounmap(struct ptnetmap_memdev *);
2226b6e66be2SVincenzo Maffione uint32_t nm_os_pt_memdev_ioread(struct ptnetmap_memdev *, unsigned int);
222737e3a6d3SLuigi Rizzo 
222837e3a6d3SLuigi Rizzo /*
222937e3a6d3SLuigi Rizzo  * netmap adapter for guest ptnetmap ports
223037e3a6d3SLuigi Rizzo  */
223137e3a6d3SLuigi Rizzo struct netmap_pt_guest_adapter {
223237e3a6d3SLuigi Rizzo         /* The netmap adapter to be used by netmap applications.
223337e3a6d3SLuigi Rizzo 	 * This field must be the first, to allow upcast. */
223437e3a6d3SLuigi Rizzo 	struct netmap_hw_adapter hwup;
223537e3a6d3SLuigi Rizzo 
223637e3a6d3SLuigi Rizzo         /* The netmap adapter to be used by the driver. */
223737e3a6d3SLuigi Rizzo         struct netmap_hw_adapter dr;
223837e3a6d3SLuigi Rizzo 
223937e3a6d3SLuigi Rizzo 	/* Reference counter to track users of backend netmap port: the
224037e3a6d3SLuigi Rizzo 	 * network stack and netmap clients.
224137e3a6d3SLuigi Rizzo 	 * Used to decide when we need (de)allocate krings/rings and
224237e3a6d3SLuigi Rizzo 	 * start (stop) ptnetmap kthreads. */
2243b6e66be2SVincenzo Maffione 	int backend_users;
224437e3a6d3SLuigi Rizzo 
224537e3a6d3SLuigi Rizzo };
224637e3a6d3SLuigi Rizzo 
224746023447SVincenzo Maffione int netmap_pt_guest_attach(struct netmap_adapter *na,
224846023447SVincenzo Maffione 			unsigned int nifp_offset,
224946023447SVincenzo Maffione 			unsigned int memid);
2250b6e66be2SVincenzo Maffione bool netmap_pt_guest_txsync(struct nm_csb_atok *atok,
2251b6e66be2SVincenzo Maffione 			struct nm_csb_ktoa *ktoa,
2252b6e66be2SVincenzo Maffione 			struct netmap_kring *kring, int flags);
2253b6e66be2SVincenzo Maffione bool netmap_pt_guest_rxsync(struct nm_csb_atok *atok,
2254b6e66be2SVincenzo Maffione 			struct nm_csb_ktoa *ktoa,
225546023447SVincenzo Maffione 			struct netmap_kring *kring, int flags);
225637e3a6d3SLuigi Rizzo int ptnet_nm_krings_create(struct netmap_adapter *na);
225737e3a6d3SLuigi Rizzo void ptnet_nm_krings_delete(struct netmap_adapter *na);
225837e3a6d3SLuigi Rizzo void ptnet_nm_dtor(struct netmap_adapter *na);
2259b6e66be2SVincenzo Maffione 
2260f79ba6d7SVincenzo Maffione /* Helper function wrapping nm_sync_kloop_appl_read(). */
2261b6e66be2SVincenzo Maffione static inline void
2262b6e66be2SVincenzo Maffione ptnet_sync_tail(struct nm_csb_ktoa *ktoa, struct netmap_kring *kring)
2263b6e66be2SVincenzo Maffione {
2264b6e66be2SVincenzo Maffione 	struct netmap_ring *ring = kring->ring;
2265b6e66be2SVincenzo Maffione 
2266b6e66be2SVincenzo Maffione 	/* Update hwcur and hwtail as known by the host. */
2267f79ba6d7SVincenzo Maffione         nm_sync_kloop_appl_read(ktoa, &kring->nr_hwtail, &kring->nr_hwcur);
2268b6e66be2SVincenzo Maffione 
2269b6e66be2SVincenzo Maffione 	/* nm_sync_finalize */
2270b6e66be2SVincenzo Maffione 	ring->tail = kring->rtail = kring->nr_hwtail;
2271b6e66be2SVincenzo Maffione }
2272b6e66be2SVincenzo Maffione #endif /* WITH_PTNETMAP */
22734bf50f18SLuigi Rizzo 
22742a7db7a6SVincenzo Maffione #ifdef __FreeBSD__
22752a7db7a6SVincenzo Maffione /*
22762a7db7a6SVincenzo Maffione  * FreeBSD mbuf allocator/deallocator in emulation mode:
22772a7db7a6SVincenzo Maffione  */
22782a7db7a6SVincenzo Maffione #if __FreeBSD_version < 1100000
22792a7db7a6SVincenzo Maffione 
22802a7db7a6SVincenzo Maffione /*
22812a7db7a6SVincenzo Maffione  * For older versions of FreeBSD:
22822a7db7a6SVincenzo Maffione  *
22832a7db7a6SVincenzo Maffione  * We allocate EXT_PACKET mbuf+clusters, but need to set M_NOFREE
22842a7db7a6SVincenzo Maffione  * so that the destructor, if invoked, will not free the packet.
22852a7db7a6SVincenzo Maffione  * In principle we should set the destructor only on demand,
22862a7db7a6SVincenzo Maffione  * but since there might be a race we better do it on allocation.
22872a7db7a6SVincenzo Maffione  * As a consequence, we also need to set the destructor or we
22882a7db7a6SVincenzo Maffione  * would leak buffers.
22892a7db7a6SVincenzo Maffione  */
22902a7db7a6SVincenzo Maffione 
22912a7db7a6SVincenzo Maffione /* mbuf destructor, also need to change the type to EXT_EXTREF,
22922a7db7a6SVincenzo Maffione  * add an M_NOFREE flag, and then clear the flag and
22932a7db7a6SVincenzo Maffione  * chain into uma_zfree(zone_pack, mf)
22942a7db7a6SVincenzo Maffione  * (or reinstall the buffer ?)
22952a7db7a6SVincenzo Maffione  */
22962a7db7a6SVincenzo Maffione #define SET_MBUF_DESTRUCTOR(m, fn)	do {		\
22972a7db7a6SVincenzo Maffione 	(m)->m_ext.ext_free = (void *)fn;	\
22982a7db7a6SVincenzo Maffione 	(m)->m_ext.ext_type = EXT_EXTREF;	\
22992a7db7a6SVincenzo Maffione } while (0)
23002a7db7a6SVincenzo Maffione 
23012a7db7a6SVincenzo Maffione static int
23022a7db7a6SVincenzo Maffione void_mbuf_dtor(struct mbuf *m, void *arg1, void *arg2)
23032a7db7a6SVincenzo Maffione {
23042a7db7a6SVincenzo Maffione 	/* restore original mbuf */
23052a7db7a6SVincenzo Maffione 	m->m_ext.ext_buf = m->m_data = m->m_ext.ext_arg1;
23062a7db7a6SVincenzo Maffione 	m->m_ext.ext_arg1 = NULL;
23072a7db7a6SVincenzo Maffione 	m->m_ext.ext_type = EXT_PACKET;
23082a7db7a6SVincenzo Maffione 	m->m_ext.ext_free = NULL;
23092a7db7a6SVincenzo Maffione 	if (MBUF_REFCNT(m) == 0)
23102a7db7a6SVincenzo Maffione 		SET_MBUF_REFCNT(m, 1);
23112a7db7a6SVincenzo Maffione 	uma_zfree(zone_pack, m);
23122a7db7a6SVincenzo Maffione 
23132a7db7a6SVincenzo Maffione 	return 0;
23142a7db7a6SVincenzo Maffione }
23152a7db7a6SVincenzo Maffione 
23162a7db7a6SVincenzo Maffione static inline struct mbuf *
23172a7db7a6SVincenzo Maffione nm_os_get_mbuf(struct ifnet *ifp, int len)
23182a7db7a6SVincenzo Maffione {
23192a7db7a6SVincenzo Maffione 	struct mbuf *m;
23202a7db7a6SVincenzo Maffione 
23212a7db7a6SVincenzo Maffione 	(void)ifp;
23222a7db7a6SVincenzo Maffione 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
23232a7db7a6SVincenzo Maffione 	if (m) {
23242a7db7a6SVincenzo Maffione 		/* m_getcl() (mb_ctor_mbuf) has an assert that checks that
23252a7db7a6SVincenzo Maffione 		 * M_NOFREE flag is not specified as third argument,
23262a7db7a6SVincenzo Maffione 		 * so we have to set M_NOFREE after m_getcl(). */
23272a7db7a6SVincenzo Maffione 		m->m_flags |= M_NOFREE;
23282a7db7a6SVincenzo Maffione 		m->m_ext.ext_arg1 = m->m_ext.ext_buf; // XXX save
23292a7db7a6SVincenzo Maffione 		m->m_ext.ext_free = (void *)void_mbuf_dtor;
23302a7db7a6SVincenzo Maffione 		m->m_ext.ext_type = EXT_EXTREF;
23312a7db7a6SVincenzo Maffione 		ND(5, "create m %p refcnt %d", m, MBUF_REFCNT(m));
23322a7db7a6SVincenzo Maffione 	}
23332a7db7a6SVincenzo Maffione 	return m;
23342a7db7a6SVincenzo Maffione }
23352a7db7a6SVincenzo Maffione 
23362a7db7a6SVincenzo Maffione #else /* __FreeBSD_version >= 1100000 */
23372a7db7a6SVincenzo Maffione 
23382a7db7a6SVincenzo Maffione /*
23392a7db7a6SVincenzo Maffione  * Newer versions of FreeBSD, using a straightforward scheme.
23402a7db7a6SVincenzo Maffione  *
23412a7db7a6SVincenzo Maffione  * We allocate mbufs with m_gethdr(), since the mbuf header is needed
23422a7db7a6SVincenzo Maffione  * by the driver. We also attach a customly-provided external storage,
23432a7db7a6SVincenzo Maffione  * which in this case is a netmap buffer. When calling m_extadd(), however
23442a7db7a6SVincenzo Maffione  * we pass a NULL address, since the real address (and length) will be
23452a7db7a6SVincenzo Maffione  * filled in by nm_os_generic_xmit_frame() right before calling
23462a7db7a6SVincenzo Maffione  * if_transmit().
23472a7db7a6SVincenzo Maffione  *
23482a7db7a6SVincenzo Maffione  * The dtor function does nothing, however we need it since mb_free_ext()
23492a7db7a6SVincenzo Maffione  * has a KASSERT(), checking that the mbuf dtor function is not NULL.
23502a7db7a6SVincenzo Maffione  */
23512a7db7a6SVincenzo Maffione 
23522a7db7a6SVincenzo Maffione #if __FreeBSD_version <= 1200050
23532a7db7a6SVincenzo Maffione static void void_mbuf_dtor(struct mbuf *m, void *arg1, void *arg2) { }
23542a7db7a6SVincenzo Maffione #else  /* __FreeBSD_version >= 1200051 */
23552a7db7a6SVincenzo Maffione /* The arg1 and arg2 pointers argument were removed by r324446, which
23562a7db7a6SVincenzo Maffione  * in included since version 1200051. */
23572a7db7a6SVincenzo Maffione static void void_mbuf_dtor(struct mbuf *m) { }
23582a7db7a6SVincenzo Maffione #endif /* __FreeBSD_version >= 1200051 */
23592a7db7a6SVincenzo Maffione 
23602a7db7a6SVincenzo Maffione #define SET_MBUF_DESTRUCTOR(m, fn)	do {		\
23612a7db7a6SVincenzo Maffione 	(m)->m_ext.ext_free = (fn != NULL) ?		\
23622a7db7a6SVincenzo Maffione 	    (void *)fn : (void *)void_mbuf_dtor;	\
23632a7db7a6SVincenzo Maffione } while (0)
23642a7db7a6SVincenzo Maffione 
23652a7db7a6SVincenzo Maffione static inline struct mbuf *
23662a7db7a6SVincenzo Maffione nm_os_get_mbuf(struct ifnet *ifp, int len)
23672a7db7a6SVincenzo Maffione {
23682a7db7a6SVincenzo Maffione 	struct mbuf *m;
23692a7db7a6SVincenzo Maffione 
23702a7db7a6SVincenzo Maffione 	(void)ifp;
23712a7db7a6SVincenzo Maffione 	(void)len;
23722a7db7a6SVincenzo Maffione 
23732a7db7a6SVincenzo Maffione 	m = m_gethdr(M_NOWAIT, MT_DATA);
23742a7db7a6SVincenzo Maffione 	if (m == NULL) {
23752a7db7a6SVincenzo Maffione 		return m;
23762a7db7a6SVincenzo Maffione 	}
23772a7db7a6SVincenzo Maffione 
23782a7db7a6SVincenzo Maffione 	m_extadd(m, NULL /* buf */, 0 /* size */, void_mbuf_dtor,
23792a7db7a6SVincenzo Maffione 		 NULL, NULL, 0, EXT_NET_DRV);
23802a7db7a6SVincenzo Maffione 
23812a7db7a6SVincenzo Maffione 	return m;
23822a7db7a6SVincenzo Maffione }
23832a7db7a6SVincenzo Maffione 
23842a7db7a6SVincenzo Maffione #endif /* __FreeBSD_version >= 1100000 */
23852a7db7a6SVincenzo Maffione #endif /* __FreeBSD__ */
23862a7db7a6SVincenzo Maffione 
23872ff91c17SVincenzo Maffione struct nmreq_option * nmreq_findoption(struct nmreq_option *, uint16_t);
23882ff91c17SVincenzo Maffione int nmreq_checkduplicate(struct nmreq_option *);
23892ff91c17SVincenzo Maffione 
2390b6e66be2SVincenzo Maffione int netmap_init_bridges(void);
2391b6e66be2SVincenzo Maffione void netmap_uninit_bridges(void);
2392b6e66be2SVincenzo Maffione 
2393b6e66be2SVincenzo Maffione /* Functions to read and write CSB fields from the kernel. */
2394b6e66be2SVincenzo Maffione #if defined (linux)
2395b6e66be2SVincenzo Maffione #define CSB_READ(csb, field, r) (get_user(r, &csb->field))
2396b6e66be2SVincenzo Maffione #define CSB_WRITE(csb, field, v) (put_user(v, &csb->field))
2397b6e66be2SVincenzo Maffione #else  /* ! linux */
2398b6e66be2SVincenzo Maffione #define CSB_READ(csb, field, r) (r = fuword32(&csb->field))
2399b6e66be2SVincenzo Maffione #define CSB_WRITE(csb, field, v) (suword32(&csb->field, v))
2400b6e66be2SVincenzo Maffione #endif /* ! linux */
2401b6e66be2SVincenzo Maffione 
240268b8534bSLuigi Rizzo #endif /* _NET_NETMAP_KERN_H_ */
2403