xref: /freebsd/sys/dev/netmap/netmap_kern.h (revision 844a6f0c5308cc9ef487f4c07394c88f3a5f5d4c)
168b8534bSLuigi Rizzo /*
237e3a6d3SLuigi Rizzo  * Copyright (C) 2011-2014 Matteo Landi, Luigi Rizzo
337e3a6d3SLuigi Rizzo  * Copyright (C) 2013-2016 Universita` di Pisa
437e3a6d3SLuigi Rizzo  * All rights reserved.
568b8534bSLuigi Rizzo  *
668b8534bSLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
768b8534bSLuigi Rizzo  * modification, are permitted provided that the following conditions
868b8534bSLuigi Rizzo  * are met:
968b8534bSLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
1068b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
1168b8534bSLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
1268b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
1368b8534bSLuigi Rizzo  *    documentation and/or other materials provided with the distribution.
1468b8534bSLuigi Rizzo  *
1568b8534bSLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1668b8534bSLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1768b8534bSLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1868b8534bSLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1968b8534bSLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2068b8534bSLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2168b8534bSLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2268b8534bSLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2368b8534bSLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2468b8534bSLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2568b8534bSLuigi Rizzo  * SUCH DAMAGE.
2668b8534bSLuigi Rizzo  */
2768b8534bSLuigi Rizzo 
2868b8534bSLuigi Rizzo /*
2968b8534bSLuigi Rizzo  * $FreeBSD$
3068b8534bSLuigi Rizzo  *
3168b8534bSLuigi Rizzo  * The header contains the definitions of constants and function
3268b8534bSLuigi Rizzo  * prototypes used only in kernelspace.
3368b8534bSLuigi Rizzo  */
3468b8534bSLuigi Rizzo 
3568b8534bSLuigi Rizzo #ifndef _NET_NETMAP_KERN_H_
3668b8534bSLuigi Rizzo #define _NET_NETMAP_KERN_H_
3768b8534bSLuigi Rizzo 
38847bf383SLuigi Rizzo #if defined(linux)
39847bf383SLuigi Rizzo 
40847bf383SLuigi Rizzo #if  defined(CONFIG_NETMAP_VALE)
41847bf383SLuigi Rizzo #define WITH_VALE
42847bf383SLuigi Rizzo #endif
43847bf383SLuigi Rizzo #if defined(CONFIG_NETMAP_PIPE)
44847bf383SLuigi Rizzo #define WITH_PIPES
45847bf383SLuigi Rizzo #endif
46847bf383SLuigi Rizzo #if defined(CONFIG_NETMAP_MONITOR)
47847bf383SLuigi Rizzo #define WITH_MONITOR
48847bf383SLuigi Rizzo #endif
49847bf383SLuigi Rizzo #if defined(CONFIG_NETMAP_GENERIC)
50847bf383SLuigi Rizzo #define WITH_GENERIC
51847bf383SLuigi Rizzo #endif
5237e3a6d3SLuigi Rizzo #if defined(CONFIG_NETMAP_PTNETMAP_GUEST)
5337e3a6d3SLuigi Rizzo #define WITH_PTNETMAP_GUEST
5437e3a6d3SLuigi Rizzo #endif
5537e3a6d3SLuigi Rizzo #if defined(CONFIG_NETMAP_PTNETMAP_HOST)
5637e3a6d3SLuigi Rizzo #define WITH_PTNETMAP_HOST
57847bf383SLuigi Rizzo #endif
58847bf383SLuigi Rizzo 
5937e3a6d3SLuigi Rizzo #elif defined (_WIN32)
60f9790aebSLuigi Rizzo #define WITH_VALE	// comment out to disable VALE support
61f0ea3689SLuigi Rizzo #define WITH_PIPES
624bf50f18SLuigi Rizzo #define WITH_MONITOR
63039dd540SLuigi Rizzo #define WITH_GENERIC
64f9790aebSLuigi Rizzo 
6537e3a6d3SLuigi Rizzo #else	/* neither linux nor windows */
6637e3a6d3SLuigi Rizzo #define WITH_VALE	// comment out to disable VALE support
6737e3a6d3SLuigi Rizzo #define WITH_PIPES
6837e3a6d3SLuigi Rizzo #define WITH_MONITOR
6937e3a6d3SLuigi Rizzo #define WITH_GENERIC
7037e3a6d3SLuigi Rizzo #define WITH_PTNETMAP_HOST	/* ptnetmap host support */
7137e3a6d3SLuigi Rizzo #define WITH_PTNETMAP_GUEST	/* ptnetmap guest support */
7237e3a6d3SLuigi Rizzo 
73847bf383SLuigi Rizzo #endif
74847bf383SLuigi Rizzo 
751a26580eSLuigi Rizzo #if defined(__FreeBSD__)
76225d33ffSSean Bruno #include <sys/selinfo.h>
77d4b42e08SLuigi Rizzo 
78ce3ee1e7SLuigi Rizzo #define likely(x)	__builtin_expect((long)!!(x), 1L)
79ce3ee1e7SLuigi Rizzo #define unlikely(x)	__builtin_expect((long)!!(x), 0L)
8037e3a6d3SLuigi Rizzo #define __user
81f196ce38SLuigi Rizzo 
82847bf383SLuigi Rizzo #define	NM_LOCK_T	struct mtx	/* low level spinlock, used to protect queues */
83039dd540SLuigi Rizzo 
84847bf383SLuigi Rizzo #define NM_MTX_T	struct sx	/* OS-specific mutex (sleepable) */
85847bf383SLuigi Rizzo #define NM_MTX_INIT(m)		sx_init(&(m), #m)
86847bf383SLuigi Rizzo #define NM_MTX_DESTROY(m)	sx_destroy(&(m))
87847bf383SLuigi Rizzo #define NM_MTX_LOCK(m)		sx_xlock(&(m))
88847bf383SLuigi Rizzo #define NM_MTX_UNLOCK(m)	sx_xunlock(&(m))
89847bf383SLuigi Rizzo #define NM_MTX_ASSERT(m)	sx_assert(&(m), SA_XLOCKED)
90f9790aebSLuigi Rizzo 
910e73f29aSLuigi Rizzo #define	NM_SELINFO_T	struct nm_selinfo
9237e3a6d3SLuigi Rizzo #define NM_SELRECORD_T	struct thread
931a26580eSLuigi Rizzo #define	MBUF_LEN(m)	((m)->m_pkthdr.len)
9437e3a6d3SLuigi Rizzo #define MBUF_TXQ(m)	((m)->m_pkthdr.flowid)
9537e3a6d3SLuigi Rizzo #define MBUF_TRANSMIT(na, ifp, m)	((na)->if_transmit(ifp, m))
9637e3a6d3SLuigi Rizzo #define	GEN_TX_MBUF_IFP(m)	((m)->m_pkthdr.rcvif)
97d4b42e08SLuigi Rizzo 
98f9790aebSLuigi Rizzo #define NM_ATOMIC_T	volatile int	// XXX ?
99f9790aebSLuigi Rizzo /* atomic operations */
100f9790aebSLuigi Rizzo #include <machine/atomic.h>
101f9790aebSLuigi Rizzo #define NM_ATOMIC_TEST_AND_SET(p)       (!atomic_cmpset_acq_int((p), 0, 1))
102f9790aebSLuigi Rizzo #define NM_ATOMIC_CLEAR(p)              atomic_store_rel_int((p), 0)
103f9790aebSLuigi Rizzo 
1047f154b71SLuigi Rizzo #if __FreeBSD_version >= 1100030
1057f154b71SLuigi Rizzo #define	WNA(_ifp)	(_ifp)->if_netmap
1067f154b71SLuigi Rizzo #else /* older FreeBSD */
1077f154b71SLuigi Rizzo #define	WNA(_ifp)	(_ifp)->if_pspare[0]
1087f154b71SLuigi Rizzo #endif /* older FreeBSD */
1097f154b71SLuigi Rizzo 
11046aa1303SLuigi Rizzo #if __FreeBSD_version >= 1100005
11146aa1303SLuigi Rizzo struct netmap_adapter *netmap_getna(if_t ifp);
11246aa1303SLuigi Rizzo #endif
113f9790aebSLuigi Rizzo 
1144bf50f18SLuigi Rizzo #if __FreeBSD_version >= 1100027
11537e3a6d3SLuigi Rizzo #define MBUF_REFCNT(m)		((m)->m_ext.ext_count)
11637e3a6d3SLuigi Rizzo #define SET_MBUF_REFCNT(m, x)   (m)->m_ext.ext_count = x
1174bf50f18SLuigi Rizzo #else
11837e3a6d3SLuigi Rizzo #define MBUF_REFCNT(m)		((m)->m_ext.ref_cnt ? *((m)->m_ext.ref_cnt) : -1)
1194bf50f18SLuigi Rizzo #define SET_MBUF_REFCNT(m, x)   *((m)->m_ext.ref_cnt) = x
1204bf50f18SLuigi Rizzo #endif
1214bf50f18SLuigi Rizzo 
12237e3a6d3SLuigi Rizzo #define MBUF_QUEUED(m)		1
123f9790aebSLuigi Rizzo 
1240e73f29aSLuigi Rizzo struct nm_selinfo {
1250e73f29aSLuigi Rizzo 	struct selinfo si;
1260e73f29aSLuigi Rizzo 	struct mtx m;
1270e73f29aSLuigi Rizzo };
1280e73f29aSLuigi Rizzo 
1290e73f29aSLuigi Rizzo 
130f9790aebSLuigi Rizzo // XXX linux struct, not used in FreeBSD
131f9790aebSLuigi Rizzo struct net_device_ops {
132f9790aebSLuigi Rizzo };
1334bf50f18SLuigi Rizzo struct ethtool_ops {
1344bf50f18SLuigi Rizzo };
135f9790aebSLuigi Rizzo struct hrtimer {
136f9790aebSLuigi Rizzo };
137847bf383SLuigi Rizzo #define NM_BNS_GET(b)
138847bf383SLuigi Rizzo #define NM_BNS_PUT(b)
139ce3ee1e7SLuigi Rizzo 
14064ae02c3SLuigi Rizzo #elif defined (linux)
141d4b42e08SLuigi Rizzo 
1422579e2d7SLuigi Rizzo #define	NM_LOCK_T	safe_spinlock_t	// see bsd_glue.h
1431a26580eSLuigi Rizzo #define	NM_SELINFO_T	wait_queue_head_t
1441a26580eSLuigi Rizzo #define	MBUF_LEN(m)	((m)->len)
14537e3a6d3SLuigi Rizzo #define MBUF_TRANSMIT(na, ifp, m)							\
14637e3a6d3SLuigi Rizzo 	({										\
14737e3a6d3SLuigi Rizzo 		/* Avoid infinite recursion with generic. */				\
14837e3a6d3SLuigi Rizzo 		m->priority = NM_MAGIC_PRIORITY_TX;					\
14937e3a6d3SLuigi Rizzo 		(((struct net_device_ops *)(na)->if_transmit)->ndo_start_xmit(m, ifp));	\
15037e3a6d3SLuigi Rizzo 		0;									\
15137e3a6d3SLuigi Rizzo 	})
15237e3a6d3SLuigi Rizzo 
15337e3a6d3SLuigi Rizzo /* See explanation in nm_os_generic_xmit_frame. */
15437e3a6d3SLuigi Rizzo #define	GEN_TX_MBUF_IFP(m)	((struct ifnet *)skb_shinfo(m)->destructor_arg)
155f196ce38SLuigi Rizzo 
156ce3ee1e7SLuigi Rizzo #define NM_ATOMIC_T	volatile long unsigned int
157ce3ee1e7SLuigi Rizzo 
158847bf383SLuigi Rizzo #define NM_MTX_T	struct mutex	/* OS-specific sleepable lock */
159847bf383SLuigi Rizzo #define NM_MTX_INIT(m)	mutex_init(&(m))
160847bf383SLuigi Rizzo #define NM_MTX_DESTROY(m)	do { (void)(m); } while (0)
161039dd540SLuigi Rizzo #define NM_MTX_LOCK(m)		mutex_lock(&(m))
162039dd540SLuigi Rizzo #define NM_MTX_UNLOCK(m)	mutex_unlock(&(m))
163847bf383SLuigi Rizzo #define NM_MTX_ASSERT(m)	mutex_is_locked(&(m))
164f9790aebSLuigi Rizzo 
165f196ce38SLuigi Rizzo #ifndef DEV_NETMAP
166f196ce38SLuigi Rizzo #define DEV_NETMAP
167ce3ee1e7SLuigi Rizzo #endif /* DEV_NETMAP */
168f196ce38SLuigi Rizzo 
169f196ce38SLuigi Rizzo #elif defined (__APPLE__)
170d4b42e08SLuigi Rizzo 
1718241616dSLuigi Rizzo #warning apple support is incomplete.
172f196ce38SLuigi Rizzo #define likely(x)	__builtin_expect(!!(x), 1)
173f196ce38SLuigi Rizzo #define unlikely(x)	__builtin_expect(!!(x), 0)
174f196ce38SLuigi Rizzo #define	NM_LOCK_T	IOLock *
175f196ce38SLuigi Rizzo #define	NM_SELINFO_T	struct selinfo
176f196ce38SLuigi Rizzo #define	MBUF_LEN(m)	((m)->m_pkthdr.len)
17737e3a6d3SLuigi Rizzo 
17837e3a6d3SLuigi Rizzo #elif defined (_WIN32)
17937e3a6d3SLuigi Rizzo #include "../../../WINDOWS/win_glue.h"
18037e3a6d3SLuigi Rizzo 
18137e3a6d3SLuigi Rizzo #define NM_SELRECORD_T		IO_STACK_LOCATION
18237e3a6d3SLuigi Rizzo #define NM_SELINFO_T		win_SELINFO		// see win_glue.h
18337e3a6d3SLuigi Rizzo #define NM_LOCK_T		win_spinlock_t	// see win_glue.h
18437e3a6d3SLuigi Rizzo #define NM_MTX_T		KGUARDED_MUTEX	/* OS-specific mutex (sleepable) */
18537e3a6d3SLuigi Rizzo 
18637e3a6d3SLuigi Rizzo #define NM_MTX_INIT(m)		KeInitializeGuardedMutex(&m);
18737e3a6d3SLuigi Rizzo #define NM_MTX_DESTROY(m)	do { (void)(m); } while (0)
18837e3a6d3SLuigi Rizzo #define NM_MTX_LOCK(m)		KeAcquireGuardedMutex(&(m))
18937e3a6d3SLuigi Rizzo #define NM_MTX_UNLOCK(m)	KeReleaseGuardedMutex(&(m))
19037e3a6d3SLuigi Rizzo #define NM_MTX_ASSERT(m)	assert(&m.Count>0)
19137e3a6d3SLuigi Rizzo 
19237e3a6d3SLuigi Rizzo //These linknames are for the NDIS driver
19337e3a6d3SLuigi Rizzo #define NETMAP_NDIS_LINKNAME_STRING             L"\\DosDevices\\NMAPNDIS"
19437e3a6d3SLuigi Rizzo #define NETMAP_NDIS_NTDEVICE_STRING             L"\\Device\\NMAPNDIS"
19537e3a6d3SLuigi Rizzo 
19637e3a6d3SLuigi Rizzo //Definition of internal driver-to-driver ioctl codes
19737e3a6d3SLuigi Rizzo #define NETMAP_KERNEL_XCHANGE_POINTERS		_IO('i', 180)
19837e3a6d3SLuigi Rizzo #define NETMAP_KERNEL_SEND_SHUTDOWN_SIGNAL	_IO_direct('i', 195)
19937e3a6d3SLuigi Rizzo 
20037e3a6d3SLuigi Rizzo //Empty data structures are not permitted by MSVC compiler
20137e3a6d3SLuigi Rizzo //XXX_ale, try to solve this problem
20237e3a6d3SLuigi Rizzo struct net_device_ops{
20337e3a6d3SLuigi Rizzo 	char data[1];
20437e3a6d3SLuigi Rizzo };
20537e3a6d3SLuigi Rizzo typedef struct ethtool_ops{
20637e3a6d3SLuigi Rizzo 	char data[1];
20737e3a6d3SLuigi Rizzo };
20837e3a6d3SLuigi Rizzo typedef struct hrtimer{
20937e3a6d3SLuigi Rizzo 	KTIMER timer;
21037e3a6d3SLuigi Rizzo 	BOOLEAN active;
21137e3a6d3SLuigi Rizzo 	KDPC deferred_proc;
21237e3a6d3SLuigi Rizzo };
21337e3a6d3SLuigi Rizzo 
21437e3a6d3SLuigi Rizzo /* MSVC does not have likely/unlikely support */
21537e3a6d3SLuigi Rizzo #ifdef _MSC_VER
21637e3a6d3SLuigi Rizzo #define likely(x)	(x)
21737e3a6d3SLuigi Rizzo #define unlikely(x)	(x)
21837e3a6d3SLuigi Rizzo #else
21937e3a6d3SLuigi Rizzo #define likely(x)	__builtin_expect((long)!!(x), 1L)
22037e3a6d3SLuigi Rizzo #define unlikely(x)	__builtin_expect((long)!!(x), 0L)
22137e3a6d3SLuigi Rizzo #endif //_MSC_VER
222f196ce38SLuigi Rizzo 
2231a26580eSLuigi Rizzo #else
224d4b42e08SLuigi Rizzo 
2251a26580eSLuigi Rizzo #error unsupported platform
226d4b42e08SLuigi Rizzo 
227d4b42e08SLuigi Rizzo #endif /* end - platform-specific code */
2281a26580eSLuigi Rizzo 
22937e3a6d3SLuigi Rizzo #ifndef _WIN32 /* support for emulated sysctl */
23037e3a6d3SLuigi Rizzo #define SYSBEGIN(x)
23137e3a6d3SLuigi Rizzo #define SYSEND
23237e3a6d3SLuigi Rizzo #endif /* _WIN32 */
23337e3a6d3SLuigi Rizzo 
23437e3a6d3SLuigi Rizzo #define NM_ACCESS_ONCE(x)	(*(volatile __typeof__(x) *)&(x))
23537e3a6d3SLuigi Rizzo 
236847bf383SLuigi Rizzo #define	NMG_LOCK_T		NM_MTX_T
237847bf383SLuigi Rizzo #define	NMG_LOCK_INIT()		NM_MTX_INIT(netmap_global_lock)
238847bf383SLuigi Rizzo #define	NMG_LOCK_DESTROY()	NM_MTX_DESTROY(netmap_global_lock)
239847bf383SLuigi Rizzo #define	NMG_LOCK()		NM_MTX_LOCK(netmap_global_lock)
240847bf383SLuigi Rizzo #define	NMG_UNLOCK()		NM_MTX_UNLOCK(netmap_global_lock)
241847bf383SLuigi Rizzo #define	NMG_LOCK_ASSERT()	NM_MTX_ASSERT(netmap_global_lock)
242847bf383SLuigi Rizzo 
24368b8534bSLuigi Rizzo #define ND(format, ...)
24468b8534bSLuigi Rizzo #define D(format, ...)						\
24568b8534bSLuigi Rizzo 	do {							\
24668b8534bSLuigi Rizzo 		struct timeval __xxts;				\
24768b8534bSLuigi Rizzo 		microtime(&__xxts);				\
24817885a7bSLuigi Rizzo 		printf("%03d.%06d [%4d] %-25s " format "\n",	\
24968b8534bSLuigi Rizzo 		(int)__xxts.tv_sec % 1000, (int)__xxts.tv_usec,	\
25017885a7bSLuigi Rizzo 		__LINE__, __FUNCTION__, ##__VA_ARGS__);		\
25168b8534bSLuigi Rizzo 	} while (0)
25268b8534bSLuigi Rizzo 
2538241616dSLuigi Rizzo /* rate limited, lps indicates how many per second */
2548241616dSLuigi Rizzo #define RD(lps, format, ...)					\
2558241616dSLuigi Rizzo 	do {							\
2568241616dSLuigi Rizzo 		static int t0, __cnt;				\
2578241616dSLuigi Rizzo 		if (t0 != time_second) {			\
2588241616dSLuigi Rizzo 			t0 = time_second;			\
2598241616dSLuigi Rizzo 			__cnt = 0;				\
2608241616dSLuigi Rizzo 		}						\
2618241616dSLuigi Rizzo 		if (__cnt++ < lps)				\
2628241616dSLuigi Rizzo 			D(format, ##__VA_ARGS__);		\
2638241616dSLuigi Rizzo 	} while (0)
2648241616dSLuigi Rizzo 
26568b8534bSLuigi Rizzo struct netmap_adapter;
266f18be576SLuigi Rizzo struct nm_bdg_fwd;
267f18be576SLuigi Rizzo struct nm_bridge;
268f18be576SLuigi Rizzo struct netmap_priv_d;
26968b8534bSLuigi Rizzo 
27037e3a6d3SLuigi Rizzo /* os-specific NM_SELINFO_T initialzation/destruction functions */
27137e3a6d3SLuigi Rizzo void nm_os_selinfo_init(NM_SELINFO_T *);
27237e3a6d3SLuigi Rizzo void nm_os_selinfo_uninit(NM_SELINFO_T *);
27337e3a6d3SLuigi Rizzo 
274ce3ee1e7SLuigi Rizzo const char *nm_dump_buf(char *p, int len, int lim, char *dst);
275ce3ee1e7SLuigi Rizzo 
27637e3a6d3SLuigi Rizzo void nm_os_selwakeup(NM_SELINFO_T *si);
27737e3a6d3SLuigi Rizzo void nm_os_selrecord(NM_SELRECORD_T *sr, NM_SELINFO_T *si);
27837e3a6d3SLuigi Rizzo 
27937e3a6d3SLuigi Rizzo int nm_os_ifnet_init(void);
28037e3a6d3SLuigi Rizzo void nm_os_ifnet_fini(void);
28137e3a6d3SLuigi Rizzo void nm_os_ifnet_lock(void);
28237e3a6d3SLuigi Rizzo void nm_os_ifnet_unlock(void);
28337e3a6d3SLuigi Rizzo 
28437e3a6d3SLuigi Rizzo void nm_os_get_module(void);
28537e3a6d3SLuigi Rizzo void nm_os_put_module(void);
28637e3a6d3SLuigi Rizzo 
28737e3a6d3SLuigi Rizzo void netmap_make_zombie(struct ifnet *);
28837e3a6d3SLuigi Rizzo void netmap_undo_zombie(struct ifnet *);
28937e3a6d3SLuigi Rizzo 
29037e3a6d3SLuigi Rizzo /* passes a packet up to the host stack.
29137e3a6d3SLuigi Rizzo  * If the packet is sent (or dropped) immediately it returns NULL,
29237e3a6d3SLuigi Rizzo  * otherwise it links the packet to prev and returns m.
29337e3a6d3SLuigi Rizzo  * In this case, a final call with m=NULL and prev != NULL will send up
29437e3a6d3SLuigi Rizzo  * the entire chain to the host stack.
29537e3a6d3SLuigi Rizzo  */
29637e3a6d3SLuigi Rizzo void *nm_os_send_up(struct ifnet *, struct mbuf *m, struct mbuf *prev);
29737e3a6d3SLuigi Rizzo 
29837e3a6d3SLuigi Rizzo int nm_os_mbuf_has_offld(struct mbuf *m);
29937e3a6d3SLuigi Rizzo 
300f9790aebSLuigi Rizzo #include "netmap_mbq.h"
301f9790aebSLuigi Rizzo 
302f9790aebSLuigi Rizzo extern NMG_LOCK_T	netmap_global_lock;
303f9790aebSLuigi Rizzo 
304847bf383SLuigi Rizzo enum txrx { NR_RX = 0, NR_TX = 1, NR_TXRX };
305847bf383SLuigi Rizzo 
306847bf383SLuigi Rizzo static __inline const char*
307847bf383SLuigi Rizzo nm_txrx2str(enum txrx t)
308847bf383SLuigi Rizzo {
309847bf383SLuigi Rizzo 	return (t== NR_RX ? "RX" : "TX");
310847bf383SLuigi Rizzo }
311847bf383SLuigi Rizzo 
312847bf383SLuigi Rizzo static __inline enum txrx
313847bf383SLuigi Rizzo nm_txrx_swap(enum txrx t)
314847bf383SLuigi Rizzo {
315847bf383SLuigi Rizzo 	return (t== NR_RX ? NR_TX : NR_RX);
316847bf383SLuigi Rizzo }
317847bf383SLuigi Rizzo 
318847bf383SLuigi Rizzo #define for_rx_tx(t)	for ((t) = 0; (t) < NR_TXRX; (t)++)
319847bf383SLuigi Rizzo 
320847bf383SLuigi Rizzo 
32168b8534bSLuigi Rizzo /*
32264ae02c3SLuigi Rizzo  * private, kernel view of a ring. Keeps track of the status of
32364ae02c3SLuigi Rizzo  * a ring across system calls.
32464ae02c3SLuigi Rizzo  *
32564ae02c3SLuigi Rizzo  *	nr_hwcur	index of the next buffer to refill.
32617885a7bSLuigi Rizzo  *			It corresponds to ring->head
32717885a7bSLuigi Rizzo  *			at the time the system call returns.
32864ae02c3SLuigi Rizzo  *
32917885a7bSLuigi Rizzo  *	nr_hwtail	index of the first buffer owned by the kernel.
33017885a7bSLuigi Rizzo  *			On RX, hwcur->hwtail are receive buffers
33117885a7bSLuigi Rizzo  *			not yet released. hwcur is advanced following
33217885a7bSLuigi Rizzo  *			ring->head, hwtail is advanced on incoming packets,
33317885a7bSLuigi Rizzo  *			and a wakeup is generated when hwtail passes ring->cur
33417885a7bSLuigi Rizzo  *			    On TX, hwcur->rcur have been filled by the sender
33517885a7bSLuigi Rizzo  *			but not sent yet to the NIC; rcur->hwtail are available
33617885a7bSLuigi Rizzo  *			for new transmissions, and hwtail->hwcur-1 are pending
33717885a7bSLuigi Rizzo  *			transmissions not yet acknowledged.
33868b8534bSLuigi Rizzo  *
3391a26580eSLuigi Rizzo  * The indexes in the NIC and netmap rings are offset by nkr_hwofs slots.
34068b8534bSLuigi Rizzo  * This is so that, on a reset, buffers owned by userspace are not
34168b8534bSLuigi Rizzo  * modified by the kernel. In particular:
34217885a7bSLuigi Rizzo  * RX rings: the next empty buffer (hwtail + hwofs) coincides with
34368b8534bSLuigi Rizzo  * 	the next empty buffer as known by the hardware (next_to_check or so).
34468b8534bSLuigi Rizzo  * TX rings: hwcur + hwofs coincides with next_to_send
3451dce924dSLuigi Rizzo  *
3461dce924dSLuigi Rizzo  * For received packets, slot->flags is set to nkr_slot_flags
3471dce924dSLuigi Rizzo  * so we can provide a proper initial value (e.g. set NS_FORWARD
3481dce924dSLuigi Rizzo  * when operating in 'transparent' mode).
349ce3ee1e7SLuigi Rizzo  *
350ce3ee1e7SLuigi Rizzo  * The following fields are used to implement lock-free copy of packets
351ce3ee1e7SLuigi Rizzo  * from input to output ports in VALE switch:
352ce3ee1e7SLuigi Rizzo  *	nkr_hwlease	buffer after the last one being copied.
353ce3ee1e7SLuigi Rizzo  *			A writer in nm_bdg_flush reserves N buffers
354ce3ee1e7SLuigi Rizzo  *			from nr_hwlease, advances it, then does the
355ce3ee1e7SLuigi Rizzo  *			copy outside the lock.
356ce3ee1e7SLuigi Rizzo  *			In RX rings (used for VALE ports),
35717885a7bSLuigi Rizzo  *			nkr_hwtail <= nkr_hwlease < nkr_hwcur+N-1
358ce3ee1e7SLuigi Rizzo  *			In TX rings (used for NIC or host stack ports)
35917885a7bSLuigi Rizzo  *			nkr_hwcur <= nkr_hwlease < nkr_hwtail
360ce3ee1e7SLuigi Rizzo  *	nkr_leases	array of nkr_num_slots where writers can report
361ce3ee1e7SLuigi Rizzo  *			completion of their block. NR_NOSLOT (~0) indicates
362ce3ee1e7SLuigi Rizzo  *			that the writer has not finished yet
363ce3ee1e7SLuigi Rizzo  *	nkr_lease_idx	index of next free slot in nr_leases, to be assigned
364ce3ee1e7SLuigi Rizzo  *
365ce3ee1e7SLuigi Rizzo  * The kring is manipulated by txsync/rxsync and generic netmap function.
36617885a7bSLuigi Rizzo  *
36717885a7bSLuigi Rizzo  * Concurrent rxsync or txsync on the same ring are prevented through
36889cc2556SLuigi Rizzo  * by nm_kr_(try)lock() which in turn uses nr_busy. This is all we need
36917885a7bSLuigi Rizzo  * for NIC rings, and for TX rings attached to the host stack.
37017885a7bSLuigi Rizzo  *
37117885a7bSLuigi Rizzo  * RX rings attached to the host stack use an mbq (rx_queue) on both
37217885a7bSLuigi Rizzo  * rxsync_from_host() and netmap_transmit(). The mbq is protected
37317885a7bSLuigi Rizzo  * by its internal lock.
37417885a7bSLuigi Rizzo  *
3754bf50f18SLuigi Rizzo  * RX rings attached to the VALE switch are accessed by both senders
37617885a7bSLuigi Rizzo  * and receiver. They are protected through the q_lock on the RX ring.
37768b8534bSLuigi Rizzo  */
37868b8534bSLuigi Rizzo struct netmap_kring {
37968b8534bSLuigi Rizzo 	struct netmap_ring	*ring;
38017885a7bSLuigi Rizzo 
381ce3ee1e7SLuigi Rizzo 	uint32_t	nr_hwcur;
38217885a7bSLuigi Rizzo 	uint32_t	nr_hwtail;
38317885a7bSLuigi Rizzo 
38417885a7bSLuigi Rizzo 	/*
38517885a7bSLuigi Rizzo 	 * Copies of values in user rings, so we do not need to look
38617885a7bSLuigi Rizzo 	 * at the ring (which could be modified). These are set in the
38717885a7bSLuigi Rizzo 	 * *sync_prologue()/finalize() routines.
38817885a7bSLuigi Rizzo 	 */
38917885a7bSLuigi Rizzo 	uint32_t	rhead;
39017885a7bSLuigi Rizzo 	uint32_t	rcur;
39117885a7bSLuigi Rizzo 	uint32_t	rtail;
39217885a7bSLuigi Rizzo 
393ce3ee1e7SLuigi Rizzo 	uint32_t	nr_kflags;	/* private driver flags */
3942157a17cSLuigi Rizzo #define NKR_PENDINTR	0x1		// Pending interrupt.
395847bf383SLuigi Rizzo #define NKR_EXCLUSIVE	0x2		/* exclusive binding */
39637e3a6d3SLuigi Rizzo #define NKR_FORWARD	0x4		/* (host ring only) there are
39737e3a6d3SLuigi Rizzo 					   packets to forward
39837e3a6d3SLuigi Rizzo 					 */
39937e3a6d3SLuigi Rizzo #define NKR_NEEDRING	0x8		/* ring needed even if users==0
40037e3a6d3SLuigi Rizzo 					 * (used internally by pipes and
40137e3a6d3SLuigi Rizzo 					 *  by ptnetmap host ports)
40237e3a6d3SLuigi Rizzo 					 */
40337e3a6d3SLuigi Rizzo 
40437e3a6d3SLuigi Rizzo 	uint32_t	nr_mode;
40537e3a6d3SLuigi Rizzo 	uint32_t	nr_pending_mode;
40637e3a6d3SLuigi Rizzo #define NKR_NETMAP_OFF	0x0
40737e3a6d3SLuigi Rizzo #define NKR_NETMAP_ON	0x1
40837e3a6d3SLuigi Rizzo 
409ce3ee1e7SLuigi Rizzo 	uint32_t	nkr_num_slots;
41017885a7bSLuigi Rizzo 
41117885a7bSLuigi Rizzo 	/*
41217885a7bSLuigi Rizzo 	 * On a NIC reset, the NIC ring indexes may be reset but the
41317885a7bSLuigi Rizzo 	 * indexes in the netmap rings remain the same. nkr_hwofs
41417885a7bSLuigi Rizzo 	 * keeps track of the offset between the two.
41517885a7bSLuigi Rizzo 	 */
41617885a7bSLuigi Rizzo 	int32_t		nkr_hwofs;
41768b8534bSLuigi Rizzo 
4181dce924dSLuigi Rizzo 	uint16_t	nkr_slot_flags;	/* initial value for flags */
41917885a7bSLuigi Rizzo 
42017885a7bSLuigi Rizzo 	/* last_reclaim is opaque marker to help reduce the frequency
42117885a7bSLuigi Rizzo 	 * of operations such as reclaiming tx buffers. A possible use
42217885a7bSLuigi Rizzo 	 * is set it to ticks and do the reclaim only once per tick.
42317885a7bSLuigi Rizzo 	 */
42417885a7bSLuigi Rizzo 	uint64_t	last_reclaim;
42517885a7bSLuigi Rizzo 
426ce3ee1e7SLuigi Rizzo 
4271a26580eSLuigi Rizzo 	NM_SELINFO_T	si;		/* poll/select wait queue */
428ce3ee1e7SLuigi Rizzo 	NM_LOCK_T	q_lock;		/* protects kring and ring. */
429ce3ee1e7SLuigi Rizzo 	NM_ATOMIC_T	nr_busy;	/* prevent concurrent syscalls */
430ce3ee1e7SLuigi Rizzo 
43117885a7bSLuigi Rizzo 	struct netmap_adapter *na;
43217885a7bSLuigi Rizzo 
4336435a0dcSLuigi Rizzo 	/* The following fields are for VALE switch support */
43417885a7bSLuigi Rizzo 	struct nm_bdg_fwd *nkr_ft;
43517885a7bSLuigi Rizzo 	uint32_t	*nkr_leases;
43617885a7bSLuigi Rizzo #define NR_NOSLOT	((uint32_t)~0)	/* used in nkr_*lease* */
43717885a7bSLuigi Rizzo 	uint32_t	nkr_hwlease;
43817885a7bSLuigi Rizzo 	uint32_t	nkr_lease_idx;
43917885a7bSLuigi Rizzo 
4404bf50f18SLuigi Rizzo 	/* while nkr_stopped is set, no new [tr]xsync operations can
4414bf50f18SLuigi Rizzo 	 * be started on this kring.
4424bf50f18SLuigi Rizzo 	 * This is used by netmap_disable_all_rings()
4434bf50f18SLuigi Rizzo 	 * to find a synchronization point where critical data
4444bf50f18SLuigi Rizzo 	 * structures pointed to by the kring can be added or removed
4454bf50f18SLuigi Rizzo 	 */
4464bf50f18SLuigi Rizzo 	volatile int nkr_stopped;
447f9790aebSLuigi Rizzo 
448f0ea3689SLuigi Rizzo 	/* Support for adapters without native netmap support.
449f9790aebSLuigi Rizzo 	 * On tx rings we preallocate an array of tx buffers
450f9790aebSLuigi Rizzo 	 * (same size as the netmap ring), on rx rings we
451f0ea3689SLuigi Rizzo 	 * store incoming mbufs in a queue that is drained by
452f0ea3689SLuigi Rizzo 	 * a rxsync.
453f9790aebSLuigi Rizzo 	 */
454f9790aebSLuigi Rizzo 	struct mbuf	**tx_pool;
45537e3a6d3SLuigi Rizzo 	struct mbuf	*tx_event;	/* TX event used as a notification */
45637e3a6d3SLuigi Rizzo 	NM_LOCK_T	tx_event_lock;	/* protects the tx_event mbuf */
45717885a7bSLuigi Rizzo 	struct mbq	rx_queue;       /* intercepted rx mbufs. */
45817885a7bSLuigi Rizzo 
459847bf383SLuigi Rizzo 	uint32_t	users;		/* existing bindings for this ring */
460847bf383SLuigi Rizzo 
46137e3a6d3SLuigi Rizzo 	uint32_t	ring_id;	/* kring identifier */
462847bf383SLuigi Rizzo 	enum txrx	tx;		/* kind of ring (tx or rx) */
46317885a7bSLuigi Rizzo 	char name[64];			/* diagnostic */
464f9790aebSLuigi Rizzo 
4654bf50f18SLuigi Rizzo 	/* [tx]sync callback for this kring.
4664bf50f18SLuigi Rizzo 	 * The default nm_kring_create callback (netmap_krings_create)
4674bf50f18SLuigi Rizzo 	 * sets the nm_sync callback of each hardware tx(rx) kring to
4684bf50f18SLuigi Rizzo 	 * the corresponding nm_txsync(nm_rxsync) taken from the
4694bf50f18SLuigi Rizzo 	 * netmap_adapter; moreover, it sets the sync callback
4704bf50f18SLuigi Rizzo 	 * of the host tx(rx) ring to netmap_txsync_to_host
4714bf50f18SLuigi Rizzo 	 * (netmap_rxsync_from_host).
4724bf50f18SLuigi Rizzo 	 *
4734bf50f18SLuigi Rizzo 	 * Overrides: the above configuration is not changed by
4744bf50f18SLuigi Rizzo 	 * any of the nm_krings_create callbacks.
4754bf50f18SLuigi Rizzo 	 */
476f0ea3689SLuigi Rizzo 	int (*nm_sync)(struct netmap_kring *kring, int flags);
477847bf383SLuigi Rizzo 	int (*nm_notify)(struct netmap_kring *kring, int flags);
478f0ea3689SLuigi Rizzo 
479f0ea3689SLuigi Rizzo #ifdef WITH_PIPES
4804bf50f18SLuigi Rizzo 	struct netmap_kring *pipe;	/* if this is a pipe ring,
4814bf50f18SLuigi Rizzo 					 * pointer to the other end
4824bf50f18SLuigi Rizzo 					 */
483f0ea3689SLuigi Rizzo #endif /* WITH_PIPES */
484f0ea3689SLuigi Rizzo 
485847bf383SLuigi Rizzo #ifdef WITH_VALE
486847bf383SLuigi Rizzo 	int (*save_notify)(struct netmap_kring *kring, int flags);
487847bf383SLuigi Rizzo #endif
488847bf383SLuigi Rizzo 
4894bf50f18SLuigi Rizzo #ifdef WITH_MONITOR
490847bf383SLuigi Rizzo 	/* array of krings that are monitoring this kring */
491847bf383SLuigi Rizzo 	struct netmap_kring **monitors;
492847bf383SLuigi Rizzo 	uint32_t max_monitors; /* current size of the monitors array */
493847bf383SLuigi Rizzo 	uint32_t n_monitors;	/* next unused entry in the monitor array */
4944bf50f18SLuigi Rizzo 	/*
495847bf383SLuigi Rizzo 	 * Monitors work by intercepting the sync and notify callbacks of the
496847bf383SLuigi Rizzo 	 * monitored krings. This is implemented by replacing the pointers
497847bf383SLuigi Rizzo 	 * above and saving the previous ones in mon_* pointers below
4984bf50f18SLuigi Rizzo 	 */
499847bf383SLuigi Rizzo 	int (*mon_sync)(struct netmap_kring *kring, int flags);
500847bf383SLuigi Rizzo 	int (*mon_notify)(struct netmap_kring *kring, int flags);
501847bf383SLuigi Rizzo 
502847bf383SLuigi Rizzo 	uint32_t mon_tail;  /* last seen slot on rx */
503847bf383SLuigi Rizzo 	uint32_t mon_pos;   /* index of this ring in the monitored ring array */
5044bf50f18SLuigi Rizzo #endif
50537e3a6d3SLuigi Rizzo }
50637e3a6d3SLuigi Rizzo #ifdef _WIN32
50737e3a6d3SLuigi Rizzo __declspec(align(64));
50837e3a6d3SLuigi Rizzo #else
50937e3a6d3SLuigi Rizzo __attribute__((__aligned__(64)));
51037e3a6d3SLuigi Rizzo #endif
51168b8534bSLuigi Rizzo 
51237e3a6d3SLuigi Rizzo /* return 1 iff the kring needs to be turned on */
51337e3a6d3SLuigi Rizzo static inline int
51437e3a6d3SLuigi Rizzo nm_kring_pending_on(struct netmap_kring *kring)
51537e3a6d3SLuigi Rizzo {
51637e3a6d3SLuigi Rizzo 	return kring->nr_pending_mode == NKR_NETMAP_ON &&
51737e3a6d3SLuigi Rizzo 	       kring->nr_mode == NKR_NETMAP_OFF;
51837e3a6d3SLuigi Rizzo }
51937e3a6d3SLuigi Rizzo 
52037e3a6d3SLuigi Rizzo /* return 1 iff the kring needs to be turned off */
52137e3a6d3SLuigi Rizzo static inline int
52237e3a6d3SLuigi Rizzo nm_kring_pending_off(struct netmap_kring *kring)
52337e3a6d3SLuigi Rizzo {
52437e3a6d3SLuigi Rizzo 	return kring->nr_pending_mode == NKR_NETMAP_OFF &&
52537e3a6d3SLuigi Rizzo 	       kring->nr_mode == NKR_NETMAP_ON;
52637e3a6d3SLuigi Rizzo }
527ce3ee1e7SLuigi Rizzo 
528ce3ee1e7SLuigi Rizzo /* return the next index, with wraparound */
529ce3ee1e7SLuigi Rizzo static inline uint32_t
530ce3ee1e7SLuigi Rizzo nm_next(uint32_t i, uint32_t lim)
531ce3ee1e7SLuigi Rizzo {
532ce3ee1e7SLuigi Rizzo 	return unlikely (i == lim) ? 0 : i + 1;
533ce3ee1e7SLuigi Rizzo }
534ce3ee1e7SLuigi Rizzo 
53517885a7bSLuigi Rizzo 
53617885a7bSLuigi Rizzo /* return the previous index, with wraparound */
53717885a7bSLuigi Rizzo static inline uint32_t
53817885a7bSLuigi Rizzo nm_prev(uint32_t i, uint32_t lim)
53917885a7bSLuigi Rizzo {
54017885a7bSLuigi Rizzo 	return unlikely (i == 0) ? lim : i - 1;
54117885a7bSLuigi Rizzo }
54217885a7bSLuigi Rizzo 
54317885a7bSLuigi Rizzo 
544ce3ee1e7SLuigi Rizzo /*
545ce3ee1e7SLuigi Rizzo  *
546ce3ee1e7SLuigi Rizzo  * Here is the layout for the Rx and Tx rings.
547ce3ee1e7SLuigi Rizzo 
548ce3ee1e7SLuigi Rizzo        RxRING                            TxRING
549ce3ee1e7SLuigi Rizzo 
550ce3ee1e7SLuigi Rizzo       +-----------------+            +-----------------+
551ce3ee1e7SLuigi Rizzo       |                 |            |                 |
552ce3ee1e7SLuigi Rizzo       |XXX free slot XXX|            |XXX free slot XXX|
553ce3ee1e7SLuigi Rizzo       +-----------------+            +-----------------+
55417885a7bSLuigi Rizzo head->| owned by user   |<-hwcur     | not sent to nic |<-hwcur
55517885a7bSLuigi Rizzo       |                 |            | yet             |
55617885a7bSLuigi Rizzo       +-----------------+            |                 |
55717885a7bSLuigi Rizzo  cur->| available to    |            |                 |
55817885a7bSLuigi Rizzo       | user, not read  |            +-----------------+
55917885a7bSLuigi Rizzo       | yet             |       cur->| (being          |
56017885a7bSLuigi Rizzo       |                 |            |  prepared)      |
561ce3ee1e7SLuigi Rizzo       |                 |            |                 |
56217885a7bSLuigi Rizzo       +-----------------+            +     ------      +
56317885a7bSLuigi Rizzo tail->|                 |<-hwtail    |                 |<-hwlease
56417885a7bSLuigi Rizzo       | (being          | ...        |                 | ...
56517885a7bSLuigi Rizzo       |  prepared)      | ...        |                 | ...
56617885a7bSLuigi Rizzo       +-----------------+ ...        |                 | ...
56717885a7bSLuigi Rizzo       |                 |<-hwlease   +-----------------+
56817885a7bSLuigi Rizzo       |                 |      tail->|                 |<-hwtail
569ce3ee1e7SLuigi Rizzo       |                 |            |                 |
570ce3ee1e7SLuigi Rizzo       |                 |            |                 |
571ce3ee1e7SLuigi Rizzo       |                 |            |                 |
572ce3ee1e7SLuigi Rizzo       +-----------------+            +-----------------+
573ce3ee1e7SLuigi Rizzo 
57417885a7bSLuigi Rizzo  * The cur/tail (user view) and hwcur/hwtail (kernel view)
575ce3ee1e7SLuigi Rizzo  * are used in the normal operation of the card.
576ce3ee1e7SLuigi Rizzo  *
577ce3ee1e7SLuigi Rizzo  * When a ring is the output of a switch port (Rx ring for
578ce3ee1e7SLuigi Rizzo  * a VALE port, Tx ring for the host stack or NIC), slots
579ce3ee1e7SLuigi Rizzo  * are reserved in blocks through 'hwlease' which points
580ce3ee1e7SLuigi Rizzo  * to the next unused slot.
58117885a7bSLuigi Rizzo  * On an Rx ring, hwlease is always after hwtail,
58217885a7bSLuigi Rizzo  * and completions cause hwtail to advance.
58317885a7bSLuigi Rizzo  * On a Tx ring, hwlease is always between cur and hwtail,
584ce3ee1e7SLuigi Rizzo  * and completions cause cur to advance.
585ce3ee1e7SLuigi Rizzo  *
586ce3ee1e7SLuigi Rizzo  * nm_kr_space() returns the maximum number of slots that
587ce3ee1e7SLuigi Rizzo  * can be assigned.
588ce3ee1e7SLuigi Rizzo  * nm_kr_lease() reserves the required number of buffers,
589ce3ee1e7SLuigi Rizzo  *    advances nkr_hwlease and also returns an entry in
590ce3ee1e7SLuigi Rizzo  *    a circular array where completions should be reported.
591ce3ee1e7SLuigi Rizzo  */
592ce3ee1e7SLuigi Rizzo 
593ce3ee1e7SLuigi Rizzo 
594847bf383SLuigi Rizzo struct netmap_lut {
595847bf383SLuigi Rizzo 	struct lut_entry *lut;
596847bf383SLuigi Rizzo 	uint32_t objtotal;	/* max buffer index */
597847bf383SLuigi Rizzo 	uint32_t objsize;	/* buffer size */
598847bf383SLuigi Rizzo };
599ce3ee1e7SLuigi Rizzo 
6004bf50f18SLuigi Rizzo struct netmap_vp_adapter; // forward
6014bf50f18SLuigi Rizzo 
60268b8534bSLuigi Rizzo /*
603f9790aebSLuigi Rizzo  * The "struct netmap_adapter" extends the "struct adapter"
604f9790aebSLuigi Rizzo  * (or equivalent) device descriptor.
605f9790aebSLuigi Rizzo  * It contains all base fields needed to support netmap operation.
606f9790aebSLuigi Rizzo  * There are in fact different types of netmap adapters
607f9790aebSLuigi Rizzo  * (native, generic, VALE switch...) so a netmap_adapter is
608f9790aebSLuigi Rizzo  * just the first field in the derived type.
60968b8534bSLuigi Rizzo  */
61068b8534bSLuigi Rizzo struct netmap_adapter {
6118241616dSLuigi Rizzo 	/*
6128241616dSLuigi Rizzo 	 * On linux we do not have a good way to tell if an interface
613f9790aebSLuigi Rizzo 	 * is netmap-capable. So we always use the following trick:
6148241616dSLuigi Rizzo 	 * NA(ifp) points here, and the first entry (which hopefully
6158241616dSLuigi Rizzo 	 * always exists and is at least 32 bits) contains a magic
6168241616dSLuigi Rizzo 	 * value which we can use to detect that the interface is good.
6178241616dSLuigi Rizzo 	 */
6188241616dSLuigi Rizzo 	uint32_t magic;
619f9790aebSLuigi Rizzo 	uint32_t na_flags;	/* enabled, and other flags */
6208241616dSLuigi Rizzo #define NAF_SKIP_INTR	1	/* use the regular interrupt handler.
6218241616dSLuigi Rizzo 				 * useful during initialization
6228241616dSLuigi Rizzo 				 */
623f18be576SLuigi Rizzo #define NAF_SW_ONLY	2	/* forward packets only to sw adapter */
624ce3ee1e7SLuigi Rizzo #define NAF_BDG_MAYSLEEP 4	/* the bridge is allowed to sleep when
625ce3ee1e7SLuigi Rizzo 				 * forwarding packets coming from this
626ce3ee1e7SLuigi Rizzo 				 * interface
627ce3ee1e7SLuigi Rizzo 				 */
628847bf383SLuigi Rizzo #define NAF_MEM_OWNER	8	/* the adapter uses its own memory area
629847bf383SLuigi Rizzo 				 * that cannot be changed
630ce3ee1e7SLuigi Rizzo 				 */
631847bf383SLuigi Rizzo #define NAF_NATIVE      16      /* the adapter is native.
63285fe4e7cSLuigi Rizzo 				 * Virtual ports (non persistent vale ports,
63385fe4e7cSLuigi Rizzo 				 * pipes, monitors...) should never use
63485fe4e7cSLuigi Rizzo 				 * this flag.
635f9790aebSLuigi Rizzo 				 */
636f9790aebSLuigi Rizzo #define	NAF_NETMAP_ON	32	/* netmap is active (either native or
6374bf50f18SLuigi Rizzo 				 * emulated). Where possible (e.g. FreeBSD)
638f9790aebSLuigi Rizzo 				 * IFCAP_NETMAP also mirrors this flag.
639f9790aebSLuigi Rizzo 				 */
640f0ea3689SLuigi Rizzo #define NAF_HOST_RINGS  64	/* the adapter supports the host rings */
6414bf50f18SLuigi Rizzo #define NAF_FORCE_NATIVE 128	/* the adapter is always NATIVE */
64237e3a6d3SLuigi Rizzo #define NAF_PTNETMAP_HOST 256	/* the adapter supports ptnetmap in the host */
64337e3a6d3SLuigi Rizzo #define NAF_ZOMBIE	(1U<<30) /* the nic driver has been unloaded */
6444bf50f18SLuigi Rizzo #define	NAF_BUSY	(1U<<31) /* the adapter is used internally and
6454bf50f18SLuigi Rizzo 				  * cannot be registered from userspace
6464bf50f18SLuigi Rizzo 				  */
647f9790aebSLuigi Rizzo 	int active_fds; /* number of user-space descriptors using this
64868b8534bSLuigi Rizzo 			 interface, which is equal to the number of
64968b8534bSLuigi Rizzo 			 struct netmap_if objs in the mapped region. */
65068b8534bSLuigi Rizzo 
65124e57ec9SEd Maste 	u_int num_rx_rings; /* number of adapter receive rings */
65224e57ec9SEd Maste 	u_int num_tx_rings; /* number of adapter transmit rings */
65368b8534bSLuigi Rizzo 
65468b8534bSLuigi Rizzo 	u_int num_tx_desc;  /* number of descriptor in each queue */
65568b8534bSLuigi Rizzo 	u_int num_rx_desc;
65668b8534bSLuigi Rizzo 
65768b8534bSLuigi Rizzo 	/* tx_rings and rx_rings are private but allocated
65868b8534bSLuigi Rizzo 	 * as a contiguous chunk of memory. Each array has
65968b8534bSLuigi Rizzo 	 * N+1 entries, for the adapter queues and for the host queue.
66068b8534bSLuigi Rizzo 	 */
66168b8534bSLuigi Rizzo 	struct netmap_kring *tx_rings; /* array of TX rings. */
66268b8534bSLuigi Rizzo 	struct netmap_kring *rx_rings; /* array of RX rings. */
66317885a7bSLuigi Rizzo 
664f9790aebSLuigi Rizzo 	void *tailroom;		       /* space below the rings array */
665f9790aebSLuigi Rizzo 				       /* (used for leases) */
666f9790aebSLuigi Rizzo 
66768b8534bSLuigi Rizzo 
668847bf383SLuigi Rizzo 	NM_SELINFO_T si[NR_TXRX];	/* global wait queues */
66964ae02c3SLuigi Rizzo 
670f0ea3689SLuigi Rizzo 	/* count users of the global wait queues */
671847bf383SLuigi Rizzo 	int si_users[NR_TXRX];
672f0ea3689SLuigi Rizzo 
6734bf50f18SLuigi Rizzo 	void *pdev; /* used to store pci device */
6744bf50f18SLuigi Rizzo 
67568b8534bSLuigi Rizzo 	/* copy of if_qflush and if_transmit pointers, to intercept
67668b8534bSLuigi Rizzo 	 * packets from the network stack when netmap is active.
67768b8534bSLuigi Rizzo 	 */
67868b8534bSLuigi Rizzo 	int     (*if_transmit)(struct ifnet *, struct mbuf *);
67968b8534bSLuigi Rizzo 
68017885a7bSLuigi Rizzo 	/* copy of if_input for netmap_send_up() */
68117885a7bSLuigi Rizzo 	void     (*if_input)(struct ifnet *, struct mbuf *);
68217885a7bSLuigi Rizzo 
68368b8534bSLuigi Rizzo 	/* references to the ifnet and device routines, used by
68468b8534bSLuigi Rizzo 	 * the generic netmap functions.
68568b8534bSLuigi Rizzo 	 */
68668b8534bSLuigi Rizzo 	struct ifnet *ifp; /* adapter is ifp->if_softc */
68768b8534bSLuigi Rizzo 
68817885a7bSLuigi Rizzo 	/*---- callbacks for this netmap adapter -----*/
68917885a7bSLuigi Rizzo 	/*
69017885a7bSLuigi Rizzo 	 * nm_dtor() is the cleanup routine called when destroying
69117885a7bSLuigi Rizzo 	 *	the adapter.
69289cc2556SLuigi Rizzo 	 *	Called with NMG_LOCK held.
69317885a7bSLuigi Rizzo 	 *
69417885a7bSLuigi Rizzo 	 * nm_register() is called on NIOCREGIF and close() to enter
69517885a7bSLuigi Rizzo 	 *	or exit netmap mode on the NIC
6964bf50f18SLuigi Rizzo 	 *	Called with NNG_LOCK held.
69717885a7bSLuigi Rizzo 	 *
69817885a7bSLuigi Rizzo 	 * nm_txsync() pushes packets to the underlying hw/switch
69917885a7bSLuigi Rizzo 	 *
70017885a7bSLuigi Rizzo 	 * nm_rxsync() collects packets from the underlying hw/switch
70117885a7bSLuigi Rizzo 	 *
70217885a7bSLuigi Rizzo 	 * nm_config() returns configuration information from the OS
70389cc2556SLuigi Rizzo 	 *	Called with NMG_LOCK held.
70417885a7bSLuigi Rizzo 	 *
7054bf50f18SLuigi Rizzo 	 * nm_krings_create() create and init the tx_rings and
7064bf50f18SLuigi Rizzo 	 * 	rx_rings arrays of kring structures. In particular,
7074bf50f18SLuigi Rizzo 	 * 	set the nm_sync callbacks for each ring.
7084bf50f18SLuigi Rizzo 	 * 	There is no need to also allocate the corresponding
7094bf50f18SLuigi Rizzo 	 * 	netmap_rings, since netmap_mem_rings_create() will always
7104bf50f18SLuigi Rizzo 	 * 	be called to provide the missing ones.
7114bf50f18SLuigi Rizzo 	 *	Called with NNG_LOCK held.
71217885a7bSLuigi Rizzo 	 *
7134bf50f18SLuigi Rizzo 	 * nm_krings_delete() cleanup and delete the tx_rings and rx_rings
7144bf50f18SLuigi Rizzo 	 * 	arrays
7154bf50f18SLuigi Rizzo 	 *	Called with NMG_LOCK held.
71617885a7bSLuigi Rizzo 	 *
71789cc2556SLuigi Rizzo 	 * nm_notify() is used to act after data have become available
71889cc2556SLuigi Rizzo 	 * 	(or the stopped state of the ring has changed)
71917885a7bSLuigi Rizzo 	 *	For hw devices this is typically a selwakeup(),
72017885a7bSLuigi Rizzo 	 *	but for NIC/host ports attached to a switch (or vice-versa)
72117885a7bSLuigi Rizzo 	 *	we also need to invoke the 'txsync' code downstream.
72237e3a6d3SLuigi Rizzo 	 *      This callback pointer is actually used only to initialize
72337e3a6d3SLuigi Rizzo 	 *      kring->nm_notify.
72437e3a6d3SLuigi Rizzo 	 *      Return values are the same as for netmap_rx_irq().
72517885a7bSLuigi Rizzo 	 */
726f9790aebSLuigi Rizzo 	void (*nm_dtor)(struct netmap_adapter *);
7271a26580eSLuigi Rizzo 
728f9790aebSLuigi Rizzo 	int (*nm_register)(struct netmap_adapter *, int onoff);
72937e3a6d3SLuigi Rizzo 	void (*nm_intr)(struct netmap_adapter *, int onoff);
730ce3ee1e7SLuigi Rizzo 
7314bf50f18SLuigi Rizzo 	int (*nm_txsync)(struct netmap_kring *kring, int flags);
7324bf50f18SLuigi Rizzo 	int (*nm_rxsync)(struct netmap_kring *kring, int flags);
733847bf383SLuigi Rizzo 	int (*nm_notify)(struct netmap_kring *kring, int flags);
734ce3ee1e7SLuigi Rizzo #define NAF_FORCE_READ    1
735ce3ee1e7SLuigi Rizzo #define NAF_FORCE_RECLAIM 2
736ae10d1afSLuigi Rizzo 	/* return configuration information */
737f9790aebSLuigi Rizzo 	int (*nm_config)(struct netmap_adapter *,
738f9790aebSLuigi Rizzo 		u_int *txr, u_int *txd, u_int *rxr, u_int *rxd);
739f9790aebSLuigi Rizzo 	int (*nm_krings_create)(struct netmap_adapter *);
740f9790aebSLuigi Rizzo 	void (*nm_krings_delete)(struct netmap_adapter *);
7414bf50f18SLuigi Rizzo #ifdef WITH_VALE
7424bf50f18SLuigi Rizzo 	/*
7434bf50f18SLuigi Rizzo 	 * nm_bdg_attach() initializes the na_vp field to point
7444bf50f18SLuigi Rizzo 	 *      to an adapter that can be attached to a VALE switch. If the
7454bf50f18SLuigi Rizzo 	 *      current adapter is already a VALE port, na_vp is simply a cast;
7464bf50f18SLuigi Rizzo 	 *      otherwise, na_vp points to a netmap_bwrap_adapter.
7474bf50f18SLuigi Rizzo 	 *      If applicable, this callback also initializes na_hostvp,
7484bf50f18SLuigi Rizzo 	 *      that can be used to connect the adapter host rings to the
7494bf50f18SLuigi Rizzo 	 *      switch.
7504bf50f18SLuigi Rizzo 	 *      Called with NMG_LOCK held.
7514bf50f18SLuigi Rizzo 	 *
7524bf50f18SLuigi Rizzo 	 * nm_bdg_ctl() is called on the actual attach/detach to/from
7534bf50f18SLuigi Rizzo 	 *      to/from the switch, to perform adapter-specific
7544bf50f18SLuigi Rizzo 	 *      initializations
7554bf50f18SLuigi Rizzo 	 *      Called with NMG_LOCK held.
7564bf50f18SLuigi Rizzo 	 */
7574bf50f18SLuigi Rizzo 	int (*nm_bdg_attach)(const char *bdg_name, struct netmap_adapter *);
7584bf50f18SLuigi Rizzo 	int (*nm_bdg_ctl)(struct netmap_adapter *, struct nmreq *, int);
7594bf50f18SLuigi Rizzo 
7604bf50f18SLuigi Rizzo 	/* adapter used to attach this adapter to a VALE switch (if any) */
7614bf50f18SLuigi Rizzo 	struct netmap_vp_adapter *na_vp;
7624bf50f18SLuigi Rizzo 	/* adapter used to attach the host rings of this adapter
7634bf50f18SLuigi Rizzo 	 * to a VALE switch (if any) */
7644bf50f18SLuigi Rizzo 	struct netmap_vp_adapter *na_hostvp;
7654bf50f18SLuigi Rizzo #endif
766f9790aebSLuigi Rizzo 
767f9790aebSLuigi Rizzo 	/* standard refcount to control the lifetime of the adapter
768f9790aebSLuigi Rizzo 	 * (it should be equal to the lifetime of the corresponding ifp)
769f9790aebSLuigi Rizzo 	 */
770f9790aebSLuigi Rizzo 	int na_refcount;
771f9790aebSLuigi Rizzo 
772f9790aebSLuigi Rizzo 	/* memory allocator (opaque)
773f9790aebSLuigi Rizzo 	 * We also cache a pointer to the lut_entry for translating
77437e3a6d3SLuigi Rizzo 	 * buffer addresses, the total number of buffers and the buffer size.
775f9790aebSLuigi Rizzo 	 */
776f9790aebSLuigi Rizzo  	struct netmap_mem_d *nm_mem;
777847bf383SLuigi Rizzo 	struct netmap_lut na_lut;
778f9790aebSLuigi Rizzo 
7794bf50f18SLuigi Rizzo 	/* additional information attached to this adapter
7804bf50f18SLuigi Rizzo 	 * by other netmap subsystems. Currently used by
78137e3a6d3SLuigi Rizzo 	 * bwrap, LINUX/v1000 and ptnetmap
782f9790aebSLuigi Rizzo 	 */
783f9790aebSLuigi Rizzo 	void *na_private;
784f0ea3689SLuigi Rizzo 
7854bf50f18SLuigi Rizzo 	/* array of pipes that have this adapter as a parent */
786f0ea3689SLuigi Rizzo 	struct netmap_pipe_adapter **na_pipes;
7874bf50f18SLuigi Rizzo 	int na_next_pipe;	/* next free slot in the array */
7884bf50f18SLuigi Rizzo 	int na_max_pipes;	/* size of the array */
7894bf50f18SLuigi Rizzo 
79037e3a6d3SLuigi Rizzo 	/* Offset of ethernet header for each packet. */
79137e3a6d3SLuigi Rizzo 	u_int virt_hdr_len;
79237e3a6d3SLuigi Rizzo 
7934bf50f18SLuigi Rizzo 	char name[64];
794f9790aebSLuigi Rizzo };
795f9790aebSLuigi Rizzo 
796847bf383SLuigi Rizzo static __inline u_int
797847bf383SLuigi Rizzo nma_get_ndesc(struct netmap_adapter *na, enum txrx t)
798847bf383SLuigi Rizzo {
799847bf383SLuigi Rizzo 	return (t == NR_TX ? na->num_tx_desc : na->num_rx_desc);
800847bf383SLuigi Rizzo }
801847bf383SLuigi Rizzo 
802847bf383SLuigi Rizzo static __inline void
803847bf383SLuigi Rizzo nma_set_ndesc(struct netmap_adapter *na, enum txrx t, u_int v)
804847bf383SLuigi Rizzo {
805847bf383SLuigi Rizzo 	if (t == NR_TX)
806847bf383SLuigi Rizzo 		na->num_tx_desc = v;
807847bf383SLuigi Rizzo 	else
808847bf383SLuigi Rizzo 		na->num_rx_desc = v;
809847bf383SLuigi Rizzo }
810847bf383SLuigi Rizzo 
811847bf383SLuigi Rizzo static __inline u_int
812847bf383SLuigi Rizzo nma_get_nrings(struct netmap_adapter *na, enum txrx t)
813847bf383SLuigi Rizzo {
814847bf383SLuigi Rizzo 	return (t == NR_TX ? na->num_tx_rings : na->num_rx_rings);
815847bf383SLuigi Rizzo }
816847bf383SLuigi Rizzo 
817847bf383SLuigi Rizzo static __inline void
818847bf383SLuigi Rizzo nma_set_nrings(struct netmap_adapter *na, enum txrx t, u_int v)
819847bf383SLuigi Rizzo {
820847bf383SLuigi Rizzo 	if (t == NR_TX)
821847bf383SLuigi Rizzo 		na->num_tx_rings = v;
822847bf383SLuigi Rizzo 	else
823847bf383SLuigi Rizzo 		na->num_rx_rings = v;
824847bf383SLuigi Rizzo }
825847bf383SLuigi Rizzo 
826847bf383SLuigi Rizzo static __inline struct netmap_kring*
827847bf383SLuigi Rizzo NMR(struct netmap_adapter *na, enum txrx t)
828847bf383SLuigi Rizzo {
829847bf383SLuigi Rizzo 	return (t == NR_TX ? na->tx_rings : na->rx_rings);
830847bf383SLuigi Rizzo }
83117885a7bSLuigi Rizzo 
832f9790aebSLuigi Rizzo /*
833f9790aebSLuigi Rizzo  * If the NIC is owned by the kernel
834f9790aebSLuigi Rizzo  * (i.e., bridge), neither another bridge nor user can use it;
835f9790aebSLuigi Rizzo  * if the NIC is owned by a user, only users can share it.
836f9790aebSLuigi Rizzo  * Evaluation must be done under NMG_LOCK().
837f9790aebSLuigi Rizzo  */
8384bf50f18SLuigi Rizzo #define NETMAP_OWNED_BY_KERN(na)	((na)->na_flags & NAF_BUSY)
839f9790aebSLuigi Rizzo #define NETMAP_OWNED_BY_ANY(na) \
8404bf50f18SLuigi Rizzo 	(NETMAP_OWNED_BY_KERN(na) || ((na)->active_fds > 0))
841f9790aebSLuigi Rizzo 
842f9790aebSLuigi Rizzo /*
843f9790aebSLuigi Rizzo  * derived netmap adapters for various types of ports
844f9790aebSLuigi Rizzo  */
845f9790aebSLuigi Rizzo struct netmap_vp_adapter {	/* VALE software port */
846f9790aebSLuigi Rizzo 	struct netmap_adapter up;
847f196ce38SLuigi Rizzo 
848849bec0eSLuigi Rizzo 	/*
849849bec0eSLuigi Rizzo 	 * Bridge support:
850849bec0eSLuigi Rizzo 	 *
851849bec0eSLuigi Rizzo 	 * bdg_port is the port number used in the bridge;
852f18be576SLuigi Rizzo 	 * na_bdg points to the bridge this NA is attached to.
853849bec0eSLuigi Rizzo 	 */
854f196ce38SLuigi Rizzo 	int bdg_port;
855f18be576SLuigi Rizzo 	struct nm_bridge *na_bdg;
856f9790aebSLuigi Rizzo 	int retry;
857f9790aebSLuigi Rizzo 
858f0ea3689SLuigi Rizzo 	/* Maximum Frame Size, used in bdg_mismatch_datapath() */
859f0ea3689SLuigi Rizzo 	u_int mfs;
860847bf383SLuigi Rizzo 	/* Last source MAC on this port */
861847bf383SLuigi Rizzo 	uint64_t last_smac;
862f9790aebSLuigi Rizzo };
863f9790aebSLuigi Rizzo 
86417885a7bSLuigi Rizzo 
865f9790aebSLuigi Rizzo struct netmap_hw_adapter {	/* physical device */
866f9790aebSLuigi Rizzo 	struct netmap_adapter up;
867f9790aebSLuigi Rizzo 
868f9790aebSLuigi Rizzo 	struct net_device_ops nm_ndo;	// XXX linux only
8694bf50f18SLuigi Rizzo 	struct ethtool_ops    nm_eto;	// XXX linux only
8704bf50f18SLuigi Rizzo 	const struct ethtool_ops*   save_ethtool;
8714bf50f18SLuigi Rizzo 
8724bf50f18SLuigi Rizzo 	int (*nm_hw_register)(struct netmap_adapter *, int onoff);
873f9790aebSLuigi Rizzo };
874f9790aebSLuigi Rizzo 
875039dd540SLuigi Rizzo #ifdef WITH_GENERIC
876f0ea3689SLuigi Rizzo /* Mitigation support. */
877f0ea3689SLuigi Rizzo struct nm_generic_mit {
878f0ea3689SLuigi Rizzo 	struct hrtimer mit_timer;
879f0ea3689SLuigi Rizzo 	int mit_pending;
8804bf50f18SLuigi Rizzo 	int mit_ring_idx;  /* index of the ring being mitigated */
881f0ea3689SLuigi Rizzo 	struct netmap_adapter *mit_na;  /* backpointer */
882f0ea3689SLuigi Rizzo };
88317885a7bSLuigi Rizzo 
88417885a7bSLuigi Rizzo struct netmap_generic_adapter {	/* emulated device */
885f9790aebSLuigi Rizzo 	struct netmap_hw_adapter up;
886f9790aebSLuigi Rizzo 
887f9790aebSLuigi Rizzo 	/* Pointer to a previously used netmap adapter. */
888f9790aebSLuigi Rizzo 	struct netmap_adapter *prev;
889f9790aebSLuigi Rizzo 
890f9790aebSLuigi Rizzo 	/* generic netmap adapters support:
891f9790aebSLuigi Rizzo 	 * a net_device_ops struct overrides ndo_select_queue(),
892f9790aebSLuigi Rizzo 	 * save_if_input saves the if_input hook (FreeBSD),
893f0ea3689SLuigi Rizzo 	 * mit implements rx interrupt mitigation,
894f9790aebSLuigi Rizzo 	 */
895f9790aebSLuigi Rizzo 	struct net_device_ops generic_ndo;
896f9790aebSLuigi Rizzo 	void (*save_if_input)(struct ifnet *, struct mbuf *);
897f9790aebSLuigi Rizzo 
898f0ea3689SLuigi Rizzo 	struct nm_generic_mit *mit;
89917885a7bSLuigi Rizzo #ifdef linux
90017885a7bSLuigi Rizzo         netdev_tx_t (*save_start_xmit)(struct mbuf *, struct ifnet *);
90117885a7bSLuigi Rizzo #endif
90237e3a6d3SLuigi Rizzo 	/* Is the adapter able to use multiple RX slots to scatter
90337e3a6d3SLuigi Rizzo 	 * each packet pushed up by the driver? */
90437e3a6d3SLuigi Rizzo 	int rxsg;
90537e3a6d3SLuigi Rizzo 
90637e3a6d3SLuigi Rizzo 	/* Is the transmission path controlled by a netmap-aware
90737e3a6d3SLuigi Rizzo 	 * device queue (i.e. qdisc on linux)? */
90837e3a6d3SLuigi Rizzo 	int txqdisc;
909f9790aebSLuigi Rizzo };
910039dd540SLuigi Rizzo #endif  /* WITH_GENERIC */
911f9790aebSLuigi Rizzo 
912f0ea3689SLuigi Rizzo static __inline int
913847bf383SLuigi Rizzo netmap_real_rings(struct netmap_adapter *na, enum txrx t)
914f0ea3689SLuigi Rizzo {
915847bf383SLuigi Rizzo 	return nma_get_nrings(na, t) + !!(na->na_flags & NAF_HOST_RINGS);
916f0ea3689SLuigi Rizzo }
917f0ea3689SLuigi Rizzo 
918f9790aebSLuigi Rizzo #ifdef WITH_VALE
91937e3a6d3SLuigi Rizzo struct nm_bdg_polling_state;
92017885a7bSLuigi Rizzo /*
92117885a7bSLuigi Rizzo  * Bridge wrapper for non VALE ports attached to a VALE switch.
922f9790aebSLuigi Rizzo  *
92317885a7bSLuigi Rizzo  * The real device must already have its own netmap adapter (hwna).
92417885a7bSLuigi Rizzo  * The bridge wrapper and the hwna adapter share the same set of
92517885a7bSLuigi Rizzo  * netmap rings and buffers, but they have two separate sets of
92617885a7bSLuigi Rizzo  * krings descriptors, with tx/rx meanings swapped:
927f9790aebSLuigi Rizzo  *
928f9790aebSLuigi Rizzo  *                                  netmap
929f9790aebSLuigi Rizzo  *           bwrap     krings       rings      krings      hwna
930f9790aebSLuigi Rizzo  *         +------+   +------+     +-----+    +------+   +------+
931f9790aebSLuigi Rizzo  *         |tx_rings->|      |\   /|     |----|      |<-tx_rings|
932f9790aebSLuigi Rizzo  *         |      |   +------+ \ / +-----+    +------+   |      |
933f9790aebSLuigi Rizzo  *         |      |             X                        |      |
934f9790aebSLuigi Rizzo  *         |      |            / \                       |      |
935f9790aebSLuigi Rizzo  *         |      |   +------+/   \+-----+    +------+   |      |
936f9790aebSLuigi Rizzo  *         |rx_rings->|      |     |     |----|      |<-rx_rings|
937f9790aebSLuigi Rizzo  *         |      |   +------+     +-----+    +------+   |      |
938f9790aebSLuigi Rizzo  *         +------+                                      +------+
939f9790aebSLuigi Rizzo  *
94017885a7bSLuigi Rizzo  * - packets coming from the bridge go to the brwap rx rings,
94117885a7bSLuigi Rizzo  *   which are also the hwna tx rings.  The bwrap notify callback
94217885a7bSLuigi Rizzo  *   will then complete the hwna tx (see netmap_bwrap_notify).
943f9790aebSLuigi Rizzo  *
94417885a7bSLuigi Rizzo  * - packets coming from the outside go to the hwna rx rings,
94517885a7bSLuigi Rizzo  *   which are also the bwrap tx rings.  The (overwritten) hwna
94617885a7bSLuigi Rizzo  *   notify method will then complete the bridge tx
94717885a7bSLuigi Rizzo  *   (see netmap_bwrap_intr_notify).
948f9790aebSLuigi Rizzo  *
94917885a7bSLuigi Rizzo  *   The bridge wrapper may optionally connect the hwna 'host' rings
95017885a7bSLuigi Rizzo  *   to the bridge. This is done by using a second port in the
95117885a7bSLuigi Rizzo  *   bridge and connecting it to the 'host' netmap_vp_adapter
95217885a7bSLuigi Rizzo  *   contained in the netmap_bwrap_adapter. The brwap host adapter
95317885a7bSLuigi Rizzo  *   cross-links the hwna host rings in the same way as shown above.
95417885a7bSLuigi Rizzo  *
95517885a7bSLuigi Rizzo  * - packets coming from the bridge and directed to the host stack
95617885a7bSLuigi Rizzo  *   are handled by the bwrap host notify callback
95717885a7bSLuigi Rizzo  *   (see netmap_bwrap_host_notify)
95817885a7bSLuigi Rizzo  *
95917885a7bSLuigi Rizzo  * - packets coming from the host stack are still handled by the
96017885a7bSLuigi Rizzo  *   overwritten hwna notify callback (netmap_bwrap_intr_notify),
96117885a7bSLuigi Rizzo  *   but are diverted to the host adapter depending on the ring number.
962f9790aebSLuigi Rizzo  *
963f9790aebSLuigi Rizzo  */
964f9790aebSLuigi Rizzo struct netmap_bwrap_adapter {
965f9790aebSLuigi Rizzo 	struct netmap_vp_adapter up;
966f9790aebSLuigi Rizzo 	struct netmap_vp_adapter host;  /* for host rings */
967f9790aebSLuigi Rizzo 	struct netmap_adapter *hwna;	/* the underlying device */
968f9790aebSLuigi Rizzo 
96917885a7bSLuigi Rizzo 	/*
97017885a7bSLuigi Rizzo 	 * When we attach a physical interface to the bridge, we
971f18be576SLuigi Rizzo 	 * allow the controlling process to terminate, so we need
9724bf50f18SLuigi Rizzo 	 * a place to store the n_detmap_priv_d data structure.
97317885a7bSLuigi Rizzo 	 * This is only done when physical interfaces
97417885a7bSLuigi Rizzo 	 * are attached to a bridge.
975f18be576SLuigi Rizzo 	 */
976f18be576SLuigi Rizzo 	struct netmap_priv_d *na_kpriv;
97737e3a6d3SLuigi Rizzo 	struct nm_bdg_polling_state *na_polling_state;
97868b8534bSLuigi Rizzo };
9794bf50f18SLuigi Rizzo int netmap_bwrap_attach(const char *name, struct netmap_adapter *);
98068b8534bSLuigi Rizzo 
98117885a7bSLuigi Rizzo #endif /* WITH_VALE */
982ce3ee1e7SLuigi Rizzo 
983f0ea3689SLuigi Rizzo #ifdef WITH_PIPES
984f0ea3689SLuigi Rizzo 
985f0ea3689SLuigi Rizzo #define NM_MAXPIPES 	64	/* max number of pipes per adapter */
986f0ea3689SLuigi Rizzo 
987f0ea3689SLuigi Rizzo struct netmap_pipe_adapter {
988f0ea3689SLuigi Rizzo 	struct netmap_adapter up;
989f0ea3689SLuigi Rizzo 
990f0ea3689SLuigi Rizzo 	u_int id; 	/* pipe identifier */
991f0ea3689SLuigi Rizzo 	int role;	/* either NR_REG_PIPE_MASTER or NR_REG_PIPE_SLAVE */
992f0ea3689SLuigi Rizzo 
993f0ea3689SLuigi Rizzo 	struct netmap_adapter *parent; /* adapter that owns the memory */
994f0ea3689SLuigi Rizzo 	struct netmap_pipe_adapter *peer; /* the other end of the pipe */
995f0ea3689SLuigi Rizzo 	int peer_ref;		/* 1 iff we are holding a ref to the peer */
996f0ea3689SLuigi Rizzo 
997f0ea3689SLuigi Rizzo 	u_int parent_slot; /* index in the parent pipe array */
998f0ea3689SLuigi Rizzo };
999f0ea3689SLuigi Rizzo 
1000f0ea3689SLuigi Rizzo #endif /* WITH_PIPES */
1001f0ea3689SLuigi Rizzo 
100217885a7bSLuigi Rizzo 
100317885a7bSLuigi Rizzo /* return slots reserved to rx clients; used in drivers */
100417885a7bSLuigi Rizzo static inline uint32_t
100517885a7bSLuigi Rizzo nm_kr_rxspace(struct netmap_kring *k)
100617885a7bSLuigi Rizzo {
100717885a7bSLuigi Rizzo 	int space = k->nr_hwtail - k->nr_hwcur;
1008ce3ee1e7SLuigi Rizzo 	if (space < 0)
1009ce3ee1e7SLuigi Rizzo 		space += k->nkr_num_slots;
101017885a7bSLuigi Rizzo 	ND("preserving %d rx slots %d -> %d", space, k->nr_hwcur, k->nr_hwtail);
101117885a7bSLuigi Rizzo 
1012ce3ee1e7SLuigi Rizzo 	return space;
1013ce3ee1e7SLuigi Rizzo }
1014ce3ee1e7SLuigi Rizzo 
101537e3a6d3SLuigi Rizzo /* return slots reserved to tx clients */
101637e3a6d3SLuigi Rizzo #define nm_kr_txspace(_k) nm_kr_rxspace(_k)
1017ce3ee1e7SLuigi Rizzo 
101837e3a6d3SLuigi Rizzo 
101937e3a6d3SLuigi Rizzo /* True if no space in the tx ring, only valid after txsync_prologue */
102017885a7bSLuigi Rizzo static inline int
102117885a7bSLuigi Rizzo nm_kr_txempty(struct netmap_kring *kring)
1022ce3ee1e7SLuigi Rizzo {
102317885a7bSLuigi Rizzo 	return kring->rcur == kring->nr_hwtail;
1024f9790aebSLuigi Rizzo }
1025f9790aebSLuigi Rizzo 
102637e3a6d3SLuigi Rizzo /* True if no more completed slots in the rx ring, only valid after
102737e3a6d3SLuigi Rizzo  * rxsync_prologue */
102837e3a6d3SLuigi Rizzo #define nm_kr_rxempty(_k)	nm_kr_txempty(_k)
1029ce3ee1e7SLuigi Rizzo 
1030ce3ee1e7SLuigi Rizzo /*
1031f9790aebSLuigi Rizzo  * protect against multiple threads using the same ring.
103237e3a6d3SLuigi Rizzo  * also check that the ring has not been stopped or locked
103368b8534bSLuigi Rizzo  */
103437e3a6d3SLuigi Rizzo #define NM_KR_BUSY	1	/* some other thread is syncing the ring */
103537e3a6d3SLuigi Rizzo #define NM_KR_STOPPED	2	/* unbounded stop (ifconfig down or driver unload) */
103637e3a6d3SLuigi Rizzo #define NM_KR_LOCKED	3	/* bounded, brief stop for mutual exclusion */
103768b8534bSLuigi Rizzo 
103817885a7bSLuigi Rizzo 
103937e3a6d3SLuigi Rizzo /* release the previously acquired right to use the *sync() methods of the ring */
1040f9790aebSLuigi Rizzo static __inline void nm_kr_put(struct netmap_kring *kr)
1041f9790aebSLuigi Rizzo {
1042f9790aebSLuigi Rizzo 	NM_ATOMIC_CLEAR(&kr->nr_busy);
1043f9790aebSLuigi Rizzo }
1044f9790aebSLuigi Rizzo 
104517885a7bSLuigi Rizzo 
104637e3a6d3SLuigi Rizzo /* true if the ifp that backed the adapter has disappeared (e.g., the
104737e3a6d3SLuigi Rizzo  * driver has been unloaded)
104837e3a6d3SLuigi Rizzo  */
104937e3a6d3SLuigi Rizzo static inline int nm_iszombie(struct netmap_adapter *na);
105037e3a6d3SLuigi Rizzo 
105137e3a6d3SLuigi Rizzo /* try to obtain exclusive right to issue the *sync() operations on the ring.
105237e3a6d3SLuigi Rizzo  * The right is obtained and must be later relinquished via nm_kr_put() if and
105337e3a6d3SLuigi Rizzo  * only if nm_kr_tryget() returns 0.
105437e3a6d3SLuigi Rizzo  * If can_sleep is 1 there are only two other possible outcomes:
105537e3a6d3SLuigi Rizzo  * - the function returns NM_KR_BUSY
105637e3a6d3SLuigi Rizzo  * - the function returns NM_KR_STOPPED and sets the POLLERR bit in *perr
105737e3a6d3SLuigi Rizzo  *   (if non-null)
105837e3a6d3SLuigi Rizzo  * In both cases the caller will typically skip the ring, possibly collecting
105937e3a6d3SLuigi Rizzo  * errors along the way.
106037e3a6d3SLuigi Rizzo  * If the calling context does not allow sleeping, the caller must pass 0 in can_sleep.
106137e3a6d3SLuigi Rizzo  * In the latter case, the function may also return NM_KR_LOCKED and leave *perr
106237e3a6d3SLuigi Rizzo  * untouched: ideally, the caller should try again at a later time.
106337e3a6d3SLuigi Rizzo  */
106437e3a6d3SLuigi Rizzo static __inline int nm_kr_tryget(struct netmap_kring *kr, int can_sleep, int *perr)
1065f9790aebSLuigi Rizzo {
106637e3a6d3SLuigi Rizzo 	int busy = 1, stopped;
1067f9790aebSLuigi Rizzo 	/* check a first time without taking the lock
1068f9790aebSLuigi Rizzo 	 * to avoid starvation for nm_kr_get()
1069f9790aebSLuigi Rizzo 	 */
107037e3a6d3SLuigi Rizzo retry:
107137e3a6d3SLuigi Rizzo 	stopped = kr->nkr_stopped;
107237e3a6d3SLuigi Rizzo 	if (unlikely(stopped)) {
107337e3a6d3SLuigi Rizzo 		goto stop;
1074f9790aebSLuigi Rizzo 	}
107537e3a6d3SLuigi Rizzo 	busy = NM_ATOMIC_TEST_AND_SET(&kr->nr_busy);
107637e3a6d3SLuigi Rizzo 	/* we should not return NM_KR_BUSY if the ring was
107737e3a6d3SLuigi Rizzo 	 * actually stopped, so check another time after
107837e3a6d3SLuigi Rizzo 	 * the barrier provided by the atomic operation
107937e3a6d3SLuigi Rizzo 	 */
108037e3a6d3SLuigi Rizzo 	stopped = kr->nkr_stopped;
108137e3a6d3SLuigi Rizzo 	if (unlikely(stopped)) {
108237e3a6d3SLuigi Rizzo 		goto stop;
1083f9790aebSLuigi Rizzo 	}
108468b8534bSLuigi Rizzo 
108537e3a6d3SLuigi Rizzo 	if (unlikely(nm_iszombie(kr->na))) {
108637e3a6d3SLuigi Rizzo 		stopped = NM_KR_STOPPED;
108737e3a6d3SLuigi Rizzo 		goto stop;
108837e3a6d3SLuigi Rizzo 	}
108937e3a6d3SLuigi Rizzo 
109037e3a6d3SLuigi Rizzo 	return unlikely(busy) ? NM_KR_BUSY : 0;
109137e3a6d3SLuigi Rizzo 
109237e3a6d3SLuigi Rizzo stop:
109337e3a6d3SLuigi Rizzo 	if (!busy)
109437e3a6d3SLuigi Rizzo 		nm_kr_put(kr);
109537e3a6d3SLuigi Rizzo 	if (stopped == NM_KR_STOPPED) {
109637e3a6d3SLuigi Rizzo /* if POLLERR is defined we want to use it to simplify netmap_poll().
109737e3a6d3SLuigi Rizzo  * Otherwise, any non-zero value will do.
109837e3a6d3SLuigi Rizzo  */
109937e3a6d3SLuigi Rizzo #ifdef POLLERR
110037e3a6d3SLuigi Rizzo #define NM_POLLERR POLLERR
110137e3a6d3SLuigi Rizzo #else
110237e3a6d3SLuigi Rizzo #define NM_POLLERR 1
110337e3a6d3SLuigi Rizzo #endif /* POLLERR */
110437e3a6d3SLuigi Rizzo 		if (perr)
110537e3a6d3SLuigi Rizzo 			*perr |= NM_POLLERR;
110637e3a6d3SLuigi Rizzo #undef NM_POLLERR
110737e3a6d3SLuigi Rizzo 	} else if (can_sleep) {
110837e3a6d3SLuigi Rizzo 		tsleep(kr, 0, "NM_KR_TRYGET", 4);
110937e3a6d3SLuigi Rizzo 		goto retry;
111037e3a6d3SLuigi Rizzo 	}
111137e3a6d3SLuigi Rizzo 	return stopped;
111237e3a6d3SLuigi Rizzo }
111337e3a6d3SLuigi Rizzo 
111437e3a6d3SLuigi Rizzo /* put the ring in the 'stopped' state and wait for the current user (if any) to
111537e3a6d3SLuigi Rizzo  * notice. stopped must be either NM_KR_STOPPED or NM_KR_LOCKED
111637e3a6d3SLuigi Rizzo  */
111737e3a6d3SLuigi Rizzo static __inline void nm_kr_stop(struct netmap_kring *kr, int stopped)
1118847bf383SLuigi Rizzo {
111937e3a6d3SLuigi Rizzo 	kr->nkr_stopped = stopped;
1120847bf383SLuigi Rizzo 	while (NM_ATOMIC_TEST_AND_SET(&kr->nr_busy))
1121847bf383SLuigi Rizzo 		tsleep(kr, 0, "NM_KR_GET", 4);
1122847bf383SLuigi Rizzo }
1123847bf383SLuigi Rizzo 
112437e3a6d3SLuigi Rizzo /* restart a ring after a stop */
112537e3a6d3SLuigi Rizzo static __inline void nm_kr_start(struct netmap_kring *kr)
112637e3a6d3SLuigi Rizzo {
112737e3a6d3SLuigi Rizzo 	kr->nkr_stopped = 0;
112837e3a6d3SLuigi Rizzo 	nm_kr_put(kr);
112937e3a6d3SLuigi Rizzo }
113037e3a6d3SLuigi Rizzo 
1131847bf383SLuigi Rizzo 
113268b8534bSLuigi Rizzo /*
113317885a7bSLuigi Rizzo  * The following functions are used by individual drivers to
113468b8534bSLuigi Rizzo  * support netmap operation.
113568b8534bSLuigi Rizzo  *
113668b8534bSLuigi Rizzo  * netmap_attach() initializes a struct netmap_adapter, allocating the
113768b8534bSLuigi Rizzo  * 	struct netmap_ring's and the struct selinfo.
113868b8534bSLuigi Rizzo  *
113968b8534bSLuigi Rizzo  * netmap_detach() frees the memory allocated by netmap_attach().
114068b8534bSLuigi Rizzo  *
1141ce3ee1e7SLuigi Rizzo  * netmap_transmit() replaces the if_transmit routine of the interface,
114268b8534bSLuigi Rizzo  *	and is used to intercept packets coming from the stack.
114368b8534bSLuigi Rizzo  *
114468b8534bSLuigi Rizzo  * netmap_load_map/netmap_reload_map are helper routines to set/reset
114568b8534bSLuigi Rizzo  *	the dmamap for a packet buffer
114668b8534bSLuigi Rizzo  *
11474bf50f18SLuigi Rizzo  * netmap_reset() is a helper routine to be called in the hw driver
11484bf50f18SLuigi Rizzo  *	when reinitializing a ring. It should not be called by
11494bf50f18SLuigi Rizzo  *	virtual ports (vale, pipes, monitor)
115068b8534bSLuigi Rizzo  */
1151f9790aebSLuigi Rizzo int netmap_attach(struct netmap_adapter *);
115268b8534bSLuigi Rizzo void netmap_detach(struct ifnet *);
1153ce3ee1e7SLuigi Rizzo int netmap_transmit(struct ifnet *, struct mbuf *);
115468b8534bSLuigi Rizzo struct netmap_slot *netmap_reset(struct netmap_adapter *na,
1155ce3ee1e7SLuigi Rizzo 	enum txrx tx, u_int n, u_int new_cur);
115668b8534bSLuigi Rizzo int netmap_ring_reinit(struct netmap_kring *);
115768b8534bSLuigi Rizzo 
115837e3a6d3SLuigi Rizzo /* Return codes for netmap_*x_irq. */
115937e3a6d3SLuigi Rizzo enum {
116037e3a6d3SLuigi Rizzo 	/* Driver should do normal interrupt processing, e.g. because
116137e3a6d3SLuigi Rizzo 	 * the interface is not in netmap mode. */
116237e3a6d3SLuigi Rizzo 	NM_IRQ_PASS = 0,
116337e3a6d3SLuigi Rizzo 	/* Port is in netmap mode, and the interrupt work has been
116437e3a6d3SLuigi Rizzo 	 * completed. The driver does not have to notify netmap
116537e3a6d3SLuigi Rizzo 	 * again before the next interrupt. */
116637e3a6d3SLuigi Rizzo 	NM_IRQ_COMPLETED = -1,
116737e3a6d3SLuigi Rizzo 	/* Port is in netmap mode, but the interrupt work has not been
116837e3a6d3SLuigi Rizzo 	 * completed. The driver has to make sure netmap will be
116937e3a6d3SLuigi Rizzo 	 * notified again soon, even if no more interrupts come (e.g.
117037e3a6d3SLuigi Rizzo 	 * on Linux the driver should not call napi_complete()). */
117137e3a6d3SLuigi Rizzo 	NM_IRQ_RESCHED = -2,
117237e3a6d3SLuigi Rizzo };
117337e3a6d3SLuigi Rizzo 
117417885a7bSLuigi Rizzo /* default functions to handle rx/tx interrupts */
117517885a7bSLuigi Rizzo int netmap_rx_irq(struct ifnet *, u_int, u_int *);
117617885a7bSLuigi Rizzo #define netmap_tx_irq(_n, _q) netmap_rx_irq(_n, _q, NULL)
117737e3a6d3SLuigi Rizzo int netmap_common_irq(struct netmap_adapter *, u_int, u_int *work_done);
117817885a7bSLuigi Rizzo 
117917885a7bSLuigi Rizzo 
11804bf50f18SLuigi Rizzo #ifdef WITH_VALE
11814bf50f18SLuigi Rizzo /* functions used by external modules to interface with VALE */
11824bf50f18SLuigi Rizzo #define netmap_vp_to_ifp(_vp)	((_vp)->up.ifp)
11834bf50f18SLuigi Rizzo #define netmap_ifp_to_vp(_ifp)	(NA(_ifp)->na_vp)
11844bf50f18SLuigi Rizzo #define netmap_ifp_to_host_vp(_ifp) (NA(_ifp)->na_hostvp)
11854bf50f18SLuigi Rizzo #define netmap_bdg_idx(_vp)	((_vp)->bdg_port)
11864bf50f18SLuigi Rizzo const char *netmap_bdg_name(struct netmap_vp_adapter *);
11874bf50f18SLuigi Rizzo #else /* !WITH_VALE */
11884bf50f18SLuigi Rizzo #define netmap_vp_to_ifp(_vp)	NULL
11894bf50f18SLuigi Rizzo #define netmap_ifp_to_vp(_ifp)	NULL
11904bf50f18SLuigi Rizzo #define netmap_ifp_to_host_vp(_ifp) NULL
11914bf50f18SLuigi Rizzo #define netmap_bdg_idx(_vp)	-1
11924bf50f18SLuigi Rizzo #define netmap_bdg_name(_vp)	NULL
11934bf50f18SLuigi Rizzo #endif /* WITH_VALE */
11944bf50f18SLuigi Rizzo 
11954bf50f18SLuigi Rizzo static inline int
11964bf50f18SLuigi Rizzo nm_netmap_on(struct netmap_adapter *na)
11974bf50f18SLuigi Rizzo {
11984bf50f18SLuigi Rizzo 	return na && na->na_flags & NAF_NETMAP_ON;
11994bf50f18SLuigi Rizzo }
120017885a7bSLuigi Rizzo 
1201847bf383SLuigi Rizzo static inline int
1202847bf383SLuigi Rizzo nm_native_on(struct netmap_adapter *na)
1203847bf383SLuigi Rizzo {
1204847bf383SLuigi Rizzo 	return nm_netmap_on(na) && (na->na_flags & NAF_NATIVE);
1205847bf383SLuigi Rizzo }
1206847bf383SLuigi Rizzo 
120737e3a6d3SLuigi Rizzo static inline int
120837e3a6d3SLuigi Rizzo nm_iszombie(struct netmap_adapter *na)
120937e3a6d3SLuigi Rizzo {
121037e3a6d3SLuigi Rizzo 	return na == NULL || (na->na_flags & NAF_ZOMBIE);
121137e3a6d3SLuigi Rizzo }
121237e3a6d3SLuigi Rizzo 
121337e3a6d3SLuigi Rizzo static inline void
121437e3a6d3SLuigi Rizzo nm_update_hostrings_mode(struct netmap_adapter *na)
121537e3a6d3SLuigi Rizzo {
121637e3a6d3SLuigi Rizzo 	/* Process nr_mode and nr_pending_mode for host rings. */
121737e3a6d3SLuigi Rizzo 	na->tx_rings[na->num_tx_rings].nr_mode =
121837e3a6d3SLuigi Rizzo 		na->tx_rings[na->num_tx_rings].nr_pending_mode;
121937e3a6d3SLuigi Rizzo 	na->rx_rings[na->num_rx_rings].nr_mode =
122037e3a6d3SLuigi Rizzo 		na->rx_rings[na->num_rx_rings].nr_pending_mode;
122137e3a6d3SLuigi Rizzo }
122237e3a6d3SLuigi Rizzo 
122317885a7bSLuigi Rizzo /* set/clear native flags and if_transmit/netdev_ops */
1224f9790aebSLuigi Rizzo static inline void
1225f9790aebSLuigi Rizzo nm_set_native_flags(struct netmap_adapter *na)
1226f9790aebSLuigi Rizzo {
1227f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
1228ce3ee1e7SLuigi Rizzo 
122937e3a6d3SLuigi Rizzo 	/* We do the setup for intercepting packets only if we are the
123037e3a6d3SLuigi Rizzo 	 * first user of this adapapter. */
123137e3a6d3SLuigi Rizzo 	if (na->active_fds > 0) {
123237e3a6d3SLuigi Rizzo 		return;
123337e3a6d3SLuigi Rizzo 	}
123437e3a6d3SLuigi Rizzo 
1235847bf383SLuigi Rizzo 	na->na_flags |= NAF_NETMAP_ON;
1236f9790aebSLuigi Rizzo #ifdef IFCAP_NETMAP /* or FreeBSD ? */
1237f9790aebSLuigi Rizzo 	ifp->if_capenable |= IFCAP_NETMAP;
1238f9790aebSLuigi Rizzo #endif
123937e3a6d3SLuigi Rizzo #if defined (__FreeBSD__)
1240f9790aebSLuigi Rizzo 	na->if_transmit = ifp->if_transmit;
1241f9790aebSLuigi Rizzo 	ifp->if_transmit = netmap_transmit;
124237e3a6d3SLuigi Rizzo #elif defined (_WIN32)
124337e3a6d3SLuigi Rizzo 	(void)ifp; /* prevent a warning */
124437e3a6d3SLuigi Rizzo 	//XXX_ale can we just comment those?
124537e3a6d3SLuigi Rizzo 	//na->if_transmit = ifp->if_transmit;
124637e3a6d3SLuigi Rizzo 	//ifp->if_transmit = netmap_transmit;
1247f9790aebSLuigi Rizzo #else
1248f9790aebSLuigi Rizzo 	na->if_transmit = (void *)ifp->netdev_ops;
1249f9790aebSLuigi Rizzo 	ifp->netdev_ops = &((struct netmap_hw_adapter *)na)->nm_ndo;
12504bf50f18SLuigi Rizzo 	((struct netmap_hw_adapter *)na)->save_ethtool = ifp->ethtool_ops;
12514bf50f18SLuigi Rizzo 	ifp->ethtool_ops = &((struct netmap_hw_adapter*)na)->nm_eto;
1252f9790aebSLuigi Rizzo #endif
125337e3a6d3SLuigi Rizzo 	nm_update_hostrings_mode(na);
1254f9790aebSLuigi Rizzo }
1255f9790aebSLuigi Rizzo 
1256f9790aebSLuigi Rizzo static inline void
1257f9790aebSLuigi Rizzo nm_clear_native_flags(struct netmap_adapter *na)
1258f9790aebSLuigi Rizzo {
1259f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
1260f9790aebSLuigi Rizzo 
126137e3a6d3SLuigi Rizzo 	/* We undo the setup for intercepting packets only if we are the
126237e3a6d3SLuigi Rizzo 	 * last user of this adapapter. */
126337e3a6d3SLuigi Rizzo 	if (na->active_fds > 0) {
126437e3a6d3SLuigi Rizzo 		return;
126537e3a6d3SLuigi Rizzo 	}
126637e3a6d3SLuigi Rizzo 
126737e3a6d3SLuigi Rizzo 	nm_update_hostrings_mode(na);
126837e3a6d3SLuigi Rizzo 
126937e3a6d3SLuigi Rizzo #if defined(__FreeBSD__)
1270f9790aebSLuigi Rizzo 	ifp->if_transmit = na->if_transmit;
127137e3a6d3SLuigi Rizzo #elif defined(_WIN32)
127237e3a6d3SLuigi Rizzo 	(void)ifp; /* prevent a warning */
127337e3a6d3SLuigi Rizzo 	//XXX_ale can we just comment those?
127437e3a6d3SLuigi Rizzo 	//ifp->if_transmit = na->if_transmit;
1275f9790aebSLuigi Rizzo #else
1276f9790aebSLuigi Rizzo 	ifp->netdev_ops = (void *)na->if_transmit;
12774bf50f18SLuigi Rizzo 	ifp->ethtool_ops = ((struct netmap_hw_adapter*)na)->save_ethtool;
1278f9790aebSLuigi Rizzo #endif
1279847bf383SLuigi Rizzo 	na->na_flags &= ~NAF_NETMAP_ON;
1280f9790aebSLuigi Rizzo #ifdef IFCAP_NETMAP /* or FreeBSD ? */
1281f9790aebSLuigi Rizzo 	ifp->if_capenable &= ~IFCAP_NETMAP;
1282f9790aebSLuigi Rizzo #endif
1283f9790aebSLuigi Rizzo }
1284f9790aebSLuigi Rizzo 
128537e3a6d3SLuigi Rizzo /*
128637e3a6d3SLuigi Rizzo  * nm_*sync_prologue() functions are used in ioctl/poll and ptnetmap
128737e3a6d3SLuigi Rizzo  * kthreads.
128837e3a6d3SLuigi Rizzo  * We need netmap_ring* parameter, because in ptnetmap it is decoupled
128937e3a6d3SLuigi Rizzo  * from host kring.
129037e3a6d3SLuigi Rizzo  * The user-space ring pointers (head/cur/tail) are shared through
129137e3a6d3SLuigi Rizzo  * CSB between host and guest.
129237e3a6d3SLuigi Rizzo  */
129337e3a6d3SLuigi Rizzo 
129437e3a6d3SLuigi Rizzo /*
129537e3a6d3SLuigi Rizzo  * validates parameters in the ring/kring, returns a value for head
129637e3a6d3SLuigi Rizzo  * If any error, returns ring_size to force a reinit.
129737e3a6d3SLuigi Rizzo  */
129837e3a6d3SLuigi Rizzo uint32_t nm_txsync_prologue(struct netmap_kring *, struct netmap_ring *);
129937e3a6d3SLuigi Rizzo 
130037e3a6d3SLuigi Rizzo 
130137e3a6d3SLuigi Rizzo /*
130237e3a6d3SLuigi Rizzo  * validates parameters in the ring/kring, returns a value for head
130337e3a6d3SLuigi Rizzo  * If any error, returns ring_size lim to force a reinit.
130437e3a6d3SLuigi Rizzo  */
130537e3a6d3SLuigi Rizzo uint32_t nm_rxsync_prologue(struct netmap_kring *, struct netmap_ring *);
130637e3a6d3SLuigi Rizzo 
1307f9790aebSLuigi Rizzo 
1308f9790aebSLuigi Rizzo /* check/fix address and len in tx rings */
1309f9790aebSLuigi Rizzo #if 1 /* debug version */
13104bf50f18SLuigi Rizzo #define	NM_CHECK_ADDR_LEN(_na, _a, _l)	do {				\
13114bf50f18SLuigi Rizzo 	if (_a == NETMAP_BUF_BASE(_na) || _l > NETMAP_BUF_SIZE(_na)) {	\
1312f9790aebSLuigi Rizzo 		RD(5, "bad addr/len ring %d slot %d idx %d len %d",	\
13134bf50f18SLuigi Rizzo 			kring->ring_id, nm_i, slot->buf_idx, len);	\
13144bf50f18SLuigi Rizzo 		if (_l > NETMAP_BUF_SIZE(_na))				\
13154bf50f18SLuigi Rizzo 			_l = NETMAP_BUF_SIZE(_na);			\
1316f9790aebSLuigi Rizzo 	} } while (0)
1317f9790aebSLuigi Rizzo #else /* no debug version */
13184bf50f18SLuigi Rizzo #define	NM_CHECK_ADDR_LEN(_na, _a, _l)	do {				\
13194bf50f18SLuigi Rizzo 		if (_l > NETMAP_BUF_SIZE(_na))				\
13204bf50f18SLuigi Rizzo 			_l = NETMAP_BUF_SIZE(_na);			\
1321f9790aebSLuigi Rizzo 	} while (0)
1322f9790aebSLuigi Rizzo #endif
1323f9790aebSLuigi Rizzo 
1324f9790aebSLuigi Rizzo 
1325f9790aebSLuigi Rizzo /*---------------------------------------------------------------*/
1326f9790aebSLuigi Rizzo /*
13274bf50f18SLuigi Rizzo  * Support routines used by netmap subsystems
13284bf50f18SLuigi Rizzo  * (native drivers, VALE, generic, pipes, monitors, ...)
13294bf50f18SLuigi Rizzo  */
13304bf50f18SLuigi Rizzo 
13314bf50f18SLuigi Rizzo 
13324bf50f18SLuigi Rizzo /* common routine for all functions that create a netmap adapter. It performs
13334bf50f18SLuigi Rizzo  * two main tasks:
13344bf50f18SLuigi Rizzo  * - if the na points to an ifp, mark the ifp as netmap capable
13354bf50f18SLuigi Rizzo  *   using na as its native adapter;
13364bf50f18SLuigi Rizzo  * - provide defaults for the setup callbacks and the memory allocator
13374bf50f18SLuigi Rizzo  */
13384bf50f18SLuigi Rizzo int netmap_attach_common(struct netmap_adapter *);
13394bf50f18SLuigi Rizzo /* common actions to be performed on netmap adapter destruction */
13404bf50f18SLuigi Rizzo void netmap_detach_common(struct netmap_adapter *);
13414bf50f18SLuigi Rizzo /* fill priv->np_[tr]xq{first,last} using the ringid and flags information
13424bf50f18SLuigi Rizzo  * coming from a struct nmreq
13434bf50f18SLuigi Rizzo  */
13444bf50f18SLuigi Rizzo int netmap_interp_ringid(struct netmap_priv_d *priv, uint16_t ringid, uint32_t flags);
13454bf50f18SLuigi Rizzo /* update the ring parameters (number and size of tx and rx rings).
13464bf50f18SLuigi Rizzo  * It calls the nm_config callback, if available.
1347f9790aebSLuigi Rizzo  */
1348f9790aebSLuigi Rizzo int netmap_update_config(struct netmap_adapter *na);
13494bf50f18SLuigi Rizzo /* create and initialize the common fields of the krings array.
13504bf50f18SLuigi Rizzo  * using the information that must be already available in the na.
13514bf50f18SLuigi Rizzo  * tailroom can be used to request the allocation of additional
13524bf50f18SLuigi Rizzo  * tailroom bytes after the krings array. This is used by
13534bf50f18SLuigi Rizzo  * netmap_vp_adapter's (i.e., VALE ports) to make room for
13544bf50f18SLuigi Rizzo  * leasing-related data structures
13554bf50f18SLuigi Rizzo  */
1356f0ea3689SLuigi Rizzo int netmap_krings_create(struct netmap_adapter *na, u_int tailroom);
13574bf50f18SLuigi Rizzo /* deletes the kring array of the adapter. The array must have
13584bf50f18SLuigi Rizzo  * been created using netmap_krings_create
13594bf50f18SLuigi Rizzo  */
1360f9790aebSLuigi Rizzo void netmap_krings_delete(struct netmap_adapter *na);
136117885a7bSLuigi Rizzo 
136237e3a6d3SLuigi Rizzo int netmap_hw_krings_create(struct netmap_adapter *na);
136337e3a6d3SLuigi Rizzo void netmap_hw_krings_delete(struct netmap_adapter *na);
136437e3a6d3SLuigi Rizzo 
13654bf50f18SLuigi Rizzo /* set the stopped/enabled status of ring
13664bf50f18SLuigi Rizzo  * When stopping, they also wait for all current activity on the ring to
13674bf50f18SLuigi Rizzo  * terminate. The status change is then notified using the na nm_notify
13684bf50f18SLuigi Rizzo  * callback.
13694bf50f18SLuigi Rizzo  */
1370847bf383SLuigi Rizzo void netmap_set_ring(struct netmap_adapter *, u_int ring_id, enum txrx, int stopped);
13714bf50f18SLuigi Rizzo /* set the stopped/enabled status of all rings of the adapter. */
13724bf50f18SLuigi Rizzo void netmap_set_all_rings(struct netmap_adapter *, int stopped);
137337e3a6d3SLuigi Rizzo /* convenience wrappers for netmap_set_all_rings */
13744bf50f18SLuigi Rizzo void netmap_disable_all_rings(struct ifnet *);
13754bf50f18SLuigi Rizzo void netmap_enable_all_rings(struct ifnet *);
13764bf50f18SLuigi Rizzo 
13778fd44c93SLuigi Rizzo int netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na,
1378847bf383SLuigi Rizzo 	uint16_t ringid, uint32_t flags);
137937e3a6d3SLuigi Rizzo void netmap_do_unregif(struct netmap_priv_d *priv);
1380f9790aebSLuigi Rizzo 
1381f9790aebSLuigi Rizzo u_int nm_bound_var(u_int *v, u_int dflt, u_int lo, u_int hi, const char *msg);
138237e3a6d3SLuigi Rizzo int netmap_get_na(struct nmreq *nmr, struct netmap_adapter **na,
138337e3a6d3SLuigi Rizzo 		  struct ifnet **ifp, int create);
138437e3a6d3SLuigi Rizzo void netmap_unget_na(struct netmap_adapter *na, struct ifnet *ifp);
1385f9790aebSLuigi Rizzo int netmap_get_hw_na(struct ifnet *ifp, struct netmap_adapter **na);
1386f9790aebSLuigi Rizzo 
138717885a7bSLuigi Rizzo 
1388f9790aebSLuigi Rizzo #ifdef WITH_VALE
1389f18be576SLuigi Rizzo /*
139017885a7bSLuigi Rizzo  * The following bridge-related functions are used by other
139117885a7bSLuigi Rizzo  * kernel modules.
139217885a7bSLuigi Rizzo  *
139317885a7bSLuigi Rizzo  * VALE only supports unicast or broadcast. The lookup
1394f18be576SLuigi Rizzo  * function can return 0 .. NM_BDG_MAXPORTS-1 for regular ports,
1395f18be576SLuigi Rizzo  * NM_BDG_MAXPORTS for broadcast, NM_BDG_MAXPORTS+1 for unknown.
1396f18be576SLuigi Rizzo  * XXX in practice "unknown" might be handled same as broadcast.
1397f18be576SLuigi Rizzo  */
13984bf50f18SLuigi Rizzo typedef u_int (*bdg_lookup_fn_t)(struct nm_bdg_fwd *ft, uint8_t *ring_nr,
1399847bf383SLuigi Rizzo 		struct netmap_vp_adapter *);
14004bf50f18SLuigi Rizzo typedef int (*bdg_config_fn_t)(struct nm_ifreq *);
14014bf50f18SLuigi Rizzo typedef void (*bdg_dtor_fn_t)(const struct netmap_vp_adapter *);
14024bf50f18SLuigi Rizzo struct netmap_bdg_ops {
14034bf50f18SLuigi Rizzo 	bdg_lookup_fn_t lookup;
14044bf50f18SLuigi Rizzo 	bdg_config_fn_t config;
14054bf50f18SLuigi Rizzo 	bdg_dtor_fn_t	dtor;
14064bf50f18SLuigi Rizzo };
14074bf50f18SLuigi Rizzo 
14084bf50f18SLuigi Rizzo u_int netmap_bdg_learning(struct nm_bdg_fwd *ft, uint8_t *dst_ring,
1409847bf383SLuigi Rizzo 		struct netmap_vp_adapter *);
1410f9790aebSLuigi Rizzo 
141137e3a6d3SLuigi Rizzo #define	NM_BRIDGES		8	/* number of bridges */
1412f9790aebSLuigi Rizzo #define	NM_BDG_MAXPORTS		254	/* up to 254 */
1413f18be576SLuigi Rizzo #define	NM_BDG_BROADCAST	NM_BDG_MAXPORTS
1414f18be576SLuigi Rizzo #define	NM_BDG_NOPORT		(NM_BDG_MAXPORTS+1)
1415f18be576SLuigi Rizzo 
1416f9790aebSLuigi Rizzo /* these are redefined in case of no VALE support */
1417f9790aebSLuigi Rizzo int netmap_get_bdg_na(struct nmreq *nmr, struct netmap_adapter **na, int create);
1418847bf383SLuigi Rizzo struct nm_bridge *netmap_init_bridges2(u_int);
1419847bf383SLuigi Rizzo void netmap_uninit_bridges2(struct nm_bridge *, u_int);
1420847bf383SLuigi Rizzo int netmap_init_bridges(void);
1421847bf383SLuigi Rizzo void netmap_uninit_bridges(void);
14224bf50f18SLuigi Rizzo int netmap_bdg_ctl(struct nmreq *nmr, struct netmap_bdg_ops *bdg_ops);
14234bf50f18SLuigi Rizzo int netmap_bdg_config(struct nmreq *nmr);
1424f9790aebSLuigi Rizzo 
1425f9790aebSLuigi Rizzo #else /* !WITH_VALE */
1426f9790aebSLuigi Rizzo #define	netmap_get_bdg_na(_1, _2, _3)	0
1427847bf383SLuigi Rizzo #define netmap_init_bridges(_1) 0
1428847bf383SLuigi Rizzo #define netmap_uninit_bridges()
1429f9790aebSLuigi Rizzo #define	netmap_bdg_ctl(_1, _2)	EINVAL
1430f9790aebSLuigi Rizzo #endif /* !WITH_VALE */
1431f9790aebSLuigi Rizzo 
1432f0ea3689SLuigi Rizzo #ifdef WITH_PIPES
1433f0ea3689SLuigi Rizzo /* max number of pipes per device */
1434f0ea3689SLuigi Rizzo #define NM_MAXPIPES	64	/* XXX how many? */
1435f0ea3689SLuigi Rizzo void netmap_pipe_dealloc(struct netmap_adapter *);
1436f0ea3689SLuigi Rizzo int netmap_get_pipe_na(struct nmreq *nmr, struct netmap_adapter **na, int create);
1437f0ea3689SLuigi Rizzo #else /* !WITH_PIPES */
1438f0ea3689SLuigi Rizzo #define NM_MAXPIPES	0
1439847bf383SLuigi Rizzo #define netmap_pipe_alloc(_1, _2) 	0
1440f0ea3689SLuigi Rizzo #define netmap_pipe_dealloc(_1)
1441847bf383SLuigi Rizzo #define netmap_get_pipe_na(nmr, _2, _3)	\
1442847bf383SLuigi Rizzo 	({ int role__ = (nmr)->nr_flags & NR_REG_MASK; \
1443847bf383SLuigi Rizzo 	   (role__ == NR_REG_PIPE_MASTER || 	       \
1444847bf383SLuigi Rizzo 	    role__ == NR_REG_PIPE_SLAVE) ? EOPNOTSUPP : 0; })
1445f0ea3689SLuigi Rizzo #endif
1446f0ea3689SLuigi Rizzo 
14474bf50f18SLuigi Rizzo #ifdef WITH_MONITOR
14484bf50f18SLuigi Rizzo int netmap_get_monitor_na(struct nmreq *nmr, struct netmap_adapter **na, int create);
1449847bf383SLuigi Rizzo void netmap_monitor_stop(struct netmap_adapter *na);
14504bf50f18SLuigi Rizzo #else
1451847bf383SLuigi Rizzo #define netmap_get_monitor_na(nmr, _2, _3) \
1452847bf383SLuigi Rizzo 	((nmr)->nr_flags & (NR_MONITOR_TX | NR_MONITOR_RX) ? EOPNOTSUPP : 0)
1453847bf383SLuigi Rizzo #endif
1454847bf383SLuigi Rizzo 
1455847bf383SLuigi Rizzo #ifdef CONFIG_NET_NS
1456847bf383SLuigi Rizzo struct net *netmap_bns_get(void);
1457847bf383SLuigi Rizzo void netmap_bns_put(struct net *);
1458847bf383SLuigi Rizzo void netmap_bns_getbridges(struct nm_bridge **, u_int *);
1459847bf383SLuigi Rizzo #else
1460847bf383SLuigi Rizzo #define netmap_bns_get()
1461847bf383SLuigi Rizzo #define netmap_bns_put(_1)
1462847bf383SLuigi Rizzo #define netmap_bns_getbridges(b, n) \
1463847bf383SLuigi Rizzo 	do { *b = nm_bridges; *n = NM_BRIDGES; } while (0)
14644bf50f18SLuigi Rizzo #endif
14654bf50f18SLuigi Rizzo 
1466f9790aebSLuigi Rizzo /* Various prototypes */
146737e3a6d3SLuigi Rizzo int netmap_poll(struct netmap_priv_d *, int events, NM_SELRECORD_T *td);
1468f9790aebSLuigi Rizzo int netmap_init(void);
1469f9790aebSLuigi Rizzo void netmap_fini(void);
1470f9790aebSLuigi Rizzo int netmap_get_memory(struct netmap_priv_d* p);
1471f9790aebSLuigi Rizzo void netmap_dtor(void *data);
1472f9790aebSLuigi Rizzo 
147337e3a6d3SLuigi Rizzo int netmap_ioctl(struct netmap_priv_d *priv, u_long cmd, caddr_t data, struct thread *);
1474f9790aebSLuigi Rizzo 
1475f9790aebSLuigi Rizzo /* netmap_adapter creation/destruction */
147617885a7bSLuigi Rizzo 
147717885a7bSLuigi Rizzo // #define NM_DEBUG_PUTGET 1
1478f9790aebSLuigi Rizzo 
1479f9790aebSLuigi Rizzo #ifdef NM_DEBUG_PUTGET
1480f9790aebSLuigi Rizzo 
1481f9790aebSLuigi Rizzo #define NM_DBG(f) __##f
1482f9790aebSLuigi Rizzo 
1483f9790aebSLuigi Rizzo void __netmap_adapter_get(struct netmap_adapter *na);
1484f9790aebSLuigi Rizzo 
1485f9790aebSLuigi Rizzo #define netmap_adapter_get(na) 				\
1486f9790aebSLuigi Rizzo 	do {						\
1487f9790aebSLuigi Rizzo 		struct netmap_adapter *__na = na;	\
14884bf50f18SLuigi Rizzo 		D("getting %p:%s (%d)", __na, (__na)->name, (__na)->na_refcount);	\
1489f9790aebSLuigi Rizzo 		__netmap_adapter_get(__na);		\
1490f9790aebSLuigi Rizzo 	} while (0)
1491f9790aebSLuigi Rizzo 
1492f9790aebSLuigi Rizzo int __netmap_adapter_put(struct netmap_adapter *na);
1493f9790aebSLuigi Rizzo 
1494f9790aebSLuigi Rizzo #define netmap_adapter_put(na)				\
1495fb25194fSLuigi Rizzo 	({						\
1496f9790aebSLuigi Rizzo 		struct netmap_adapter *__na = na;	\
14974bf50f18SLuigi Rizzo 		D("putting %p:%s (%d)", __na, (__na)->name, (__na)->na_refcount);	\
1498f9790aebSLuigi Rizzo 		__netmap_adapter_put(__na);		\
1499fb25194fSLuigi Rizzo 	})
1500f9790aebSLuigi Rizzo 
1501f9790aebSLuigi Rizzo #else /* !NM_DEBUG_PUTGET */
1502f9790aebSLuigi Rizzo 
1503f9790aebSLuigi Rizzo #define NM_DBG(f) f
1504f9790aebSLuigi Rizzo void netmap_adapter_get(struct netmap_adapter *na);
1505f9790aebSLuigi Rizzo int netmap_adapter_put(struct netmap_adapter *na);
1506f9790aebSLuigi Rizzo 
1507f9790aebSLuigi Rizzo #endif /* !NM_DEBUG_PUTGET */
1508f9790aebSLuigi Rizzo 
1509f9790aebSLuigi Rizzo 
151017885a7bSLuigi Rizzo /*
151117885a7bSLuigi Rizzo  * module variables
151217885a7bSLuigi Rizzo  */
151337e3a6d3SLuigi Rizzo #define NETMAP_BUF_BASE(_na)	((_na)->na_lut.lut[0].vaddr)
151437e3a6d3SLuigi Rizzo #define NETMAP_BUF_SIZE(_na)	((_na)->na_lut.objsize)
15155819da83SLuigi Rizzo extern int netmap_no_pendintr;
1516a2a74091SLuigi Rizzo extern int netmap_mitigate;
1517a2a74091SLuigi Rizzo extern int netmap_verbose;		/* for debugging */
151868b8534bSLuigi Rizzo enum {                                  /* verbose flags */
151968b8534bSLuigi Rizzo 	NM_VERB_ON = 1,                 /* generic verbose */
152068b8534bSLuigi Rizzo 	NM_VERB_HOST = 0x2,             /* verbose host stack */
152168b8534bSLuigi Rizzo 	NM_VERB_RXSYNC = 0x10,          /* verbose on rxsync/txsync */
152268b8534bSLuigi Rizzo 	NM_VERB_TXSYNC = 0x20,
152368b8534bSLuigi Rizzo 	NM_VERB_RXINTR = 0x100,         /* verbose on rx/tx intr (driver) */
152468b8534bSLuigi Rizzo 	NM_VERB_TXINTR = 0x200,
152568b8534bSLuigi Rizzo 	NM_VERB_NIC_RXSYNC = 0x1000,    /* verbose on rx/tx intr (driver) */
152668b8534bSLuigi Rizzo 	NM_VERB_NIC_TXSYNC = 0x2000,
152768b8534bSLuigi Rizzo };
152868b8534bSLuigi Rizzo 
1529f9790aebSLuigi Rizzo extern int netmap_txsync_retry;
153037e3a6d3SLuigi Rizzo extern int netmap_flags;
1531f9790aebSLuigi Rizzo extern int netmap_generic_mit;
1532f9790aebSLuigi Rizzo extern int netmap_generic_ringsize;
1533f0ea3689SLuigi Rizzo extern int netmap_generic_rings;
153437e3a6d3SLuigi Rizzo extern int netmap_generic_txqdisc;
1535f9790aebSLuigi Rizzo 
153668b8534bSLuigi Rizzo /*
1537d0c7b075SLuigi Rizzo  * NA returns a pointer to the struct netmap adapter from the ifp,
1538d0c7b075SLuigi Rizzo  * WNA is used to write it.
153968b8534bSLuigi Rizzo  */
1540d0c7b075SLuigi Rizzo #define	NA(_ifp)	((struct netmap_adapter *)WNA(_ifp))
154168b8534bSLuigi Rizzo 
15428241616dSLuigi Rizzo /*
154337e3a6d3SLuigi Rizzo  * On old versions of FreeBSD, NA(ifp) is a pspare. On linux we
154437e3a6d3SLuigi Rizzo  * overload another pointer in the netdev.
154537e3a6d3SLuigi Rizzo  *
154637e3a6d3SLuigi Rizzo  * We check if NA(ifp) is set and its first element has a related
15478241616dSLuigi Rizzo  * magic value. The capenable is within the struct netmap_adapter.
15488241616dSLuigi Rizzo  */
15498241616dSLuigi Rizzo #define	NETMAP_MAGIC	0x52697a7a
15508241616dSLuigi Rizzo 
155137e3a6d3SLuigi Rizzo #define NM_NA_VALID(ifp)	(NA(ifp) &&		\
15528241616dSLuigi Rizzo 	((uint32_t)(uintptr_t)NA(ifp) ^ NA(ifp)->magic) == NETMAP_MAGIC )
15538241616dSLuigi Rizzo 
155437e3a6d3SLuigi Rizzo #define	NM_ATTACH_NA(ifp, na) do {					\
155537e3a6d3SLuigi Rizzo 	WNA(ifp) = na;							\
155637e3a6d3SLuigi Rizzo 	if (NA(ifp))							\
155737e3a6d3SLuigi Rizzo 		NA(ifp)->magic = 					\
155837e3a6d3SLuigi Rizzo 			((uint32_t)(uintptr_t)NA(ifp)) ^ NETMAP_MAGIC;	\
155937e3a6d3SLuigi Rizzo } while(0)
15608241616dSLuigi Rizzo 
156137e3a6d3SLuigi Rizzo #define NM_IS_NATIVE(ifp)	(NM_NA_VALID(ifp) && NA(ifp)->nm_dtor == netmap_hw_dtor)
156268b8534bSLuigi Rizzo 
156337e3a6d3SLuigi Rizzo #if defined(__FreeBSD__)
1564f9790aebSLuigi Rizzo 
15654bf50f18SLuigi Rizzo /* Assigns the device IOMMU domain to an allocator.
15664bf50f18SLuigi Rizzo  * Returns -ENOMEM in case the domain is different */
15674bf50f18SLuigi Rizzo #define nm_iommu_group_id(dev) (0)
15684bf50f18SLuigi Rizzo 
156917885a7bSLuigi Rizzo /* Callback invoked by the dma machinery after a successful dmamap_load */
15706dba29a2SLuigi Rizzo static void netmap_dmamap_cb(__unused void *arg,
15716dba29a2SLuigi Rizzo     __unused bus_dma_segment_t * segs, __unused int nseg, __unused int error)
15726dba29a2SLuigi Rizzo {
15736dba29a2SLuigi Rizzo }
15746dba29a2SLuigi Rizzo 
15756dba29a2SLuigi Rizzo /* bus_dmamap_load wrapper: call aforementioned function if map != NULL.
15766dba29a2SLuigi Rizzo  * XXX can we do it without a callback ?
15776dba29a2SLuigi Rizzo  */
15786dba29a2SLuigi Rizzo static inline void
15794bf50f18SLuigi Rizzo netmap_load_map(struct netmap_adapter *na,
15804bf50f18SLuigi Rizzo 	bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
15816dba29a2SLuigi Rizzo {
15826dba29a2SLuigi Rizzo 	if (map)
15834bf50f18SLuigi Rizzo 		bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE(na),
15846dba29a2SLuigi Rizzo 		    netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
15856dba29a2SLuigi Rizzo }
15866dba29a2SLuigi Rizzo 
15874bf50f18SLuigi Rizzo static inline void
15884bf50f18SLuigi Rizzo netmap_unload_map(struct netmap_adapter *na,
15894bf50f18SLuigi Rizzo         bus_dma_tag_t tag, bus_dmamap_t map)
15904bf50f18SLuigi Rizzo {
15914bf50f18SLuigi Rizzo 	if (map)
15924bf50f18SLuigi Rizzo 		bus_dmamap_unload(tag, map);
15934bf50f18SLuigi Rizzo }
15944bf50f18SLuigi Rizzo 
15956dba29a2SLuigi Rizzo /* update the map when a buffer changes. */
15966dba29a2SLuigi Rizzo static inline void
15974bf50f18SLuigi Rizzo netmap_reload_map(struct netmap_adapter *na,
15984bf50f18SLuigi Rizzo 	bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
15996dba29a2SLuigi Rizzo {
16006dba29a2SLuigi Rizzo 	if (map) {
16016dba29a2SLuigi Rizzo 		bus_dmamap_unload(tag, map);
16024bf50f18SLuigi Rizzo 		bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE(na),
16036dba29a2SLuigi Rizzo 		    netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
16046dba29a2SLuigi Rizzo 	}
16056dba29a2SLuigi Rizzo }
1606f9790aebSLuigi Rizzo 
160737e3a6d3SLuigi Rizzo #elif defined(_WIN32)
160837e3a6d3SLuigi Rizzo 
1609f196ce38SLuigi Rizzo #else /* linux */
1610f196ce38SLuigi Rizzo 
16114bf50f18SLuigi Rizzo int nm_iommu_group_id(bus_dma_tag_t dev);
16124bf50f18SLuigi Rizzo #include <linux/dma-mapping.h>
16134bf50f18SLuigi Rizzo 
16144bf50f18SLuigi Rizzo static inline void
16154bf50f18SLuigi Rizzo netmap_load_map(struct netmap_adapter *na,
16164bf50f18SLuigi Rizzo 	bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
16174bf50f18SLuigi Rizzo {
1618847bf383SLuigi Rizzo 	if (0 && map) {
161937e3a6d3SLuigi Rizzo 		*map = dma_map_single(na->pdev, buf, NETMAP_BUF_SIZE(na),
16204bf50f18SLuigi Rizzo 				      DMA_BIDIRECTIONAL);
16214bf50f18SLuigi Rizzo 	}
16224bf50f18SLuigi Rizzo }
16234bf50f18SLuigi Rizzo 
16244bf50f18SLuigi Rizzo static inline void
16254bf50f18SLuigi Rizzo netmap_unload_map(struct netmap_adapter *na,
16264bf50f18SLuigi Rizzo 	bus_dma_tag_t tag, bus_dmamap_t map)
16274bf50f18SLuigi Rizzo {
162837e3a6d3SLuigi Rizzo 	u_int sz = NETMAP_BUF_SIZE(na);
16294bf50f18SLuigi Rizzo 
16304bf50f18SLuigi Rizzo 	if (*map) {
16314bf50f18SLuigi Rizzo 		dma_unmap_single(na->pdev, *map, sz,
16324bf50f18SLuigi Rizzo 				 DMA_BIDIRECTIONAL);
16334bf50f18SLuigi Rizzo 	}
16344bf50f18SLuigi Rizzo }
16354bf50f18SLuigi Rizzo 
16364bf50f18SLuigi Rizzo static inline void
16374bf50f18SLuigi Rizzo netmap_reload_map(struct netmap_adapter *na,
16384bf50f18SLuigi Rizzo 	bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
16394bf50f18SLuigi Rizzo {
164037e3a6d3SLuigi Rizzo 	u_int sz = NETMAP_BUF_SIZE(na);
16414bf50f18SLuigi Rizzo 
16424bf50f18SLuigi Rizzo 	if (*map) {
16434bf50f18SLuigi Rizzo 		dma_unmap_single(na->pdev, *map, sz,
16444bf50f18SLuigi Rizzo 				DMA_BIDIRECTIONAL);
16454bf50f18SLuigi Rizzo 	}
16464bf50f18SLuigi Rizzo 
16474bf50f18SLuigi Rizzo 	*map = dma_map_single(na->pdev, buf, sz,
16484bf50f18SLuigi Rizzo 				DMA_BIDIRECTIONAL);
16494bf50f18SLuigi Rizzo }
16504bf50f18SLuigi Rizzo 
1651f196ce38SLuigi Rizzo /*
1652f196ce38SLuigi Rizzo  * XXX How do we redefine these functions:
1653f196ce38SLuigi Rizzo  *
1654f196ce38SLuigi Rizzo  * on linux we need
1655f196ce38SLuigi Rizzo  *	dma_map_single(&pdev->dev, virt_addr, len, direction)
1656f196ce38SLuigi Rizzo  *	dma_unmap_single(&adapter->pdev->dev, phys_addr, len, direction
1657f196ce38SLuigi Rizzo  * The len can be implicit (on netmap it is NETMAP_BUF_SIZE)
1658f196ce38SLuigi Rizzo  * unfortunately the direction is not, so we need to change
1659f196ce38SLuigi Rizzo  * something to have a cross API
1660f196ce38SLuigi Rizzo  */
16614bf50f18SLuigi Rizzo 
1662f196ce38SLuigi Rizzo #if 0
1663f196ce38SLuigi Rizzo 	struct e1000_buffer *buffer_info =  &tx_ring->buffer_info[l];
1664f196ce38SLuigi Rizzo 	/* set time_stamp *before* dma to help avoid a possible race */
1665f196ce38SLuigi Rizzo 	buffer_info->time_stamp = jiffies;
1666f196ce38SLuigi Rizzo 	buffer_info->mapped_as_page = false;
1667f196ce38SLuigi Rizzo 	buffer_info->length = len;
1668f196ce38SLuigi Rizzo 	//buffer_info->next_to_watch = l;
1669f196ce38SLuigi Rizzo 	/* reload dma map */
1670f196ce38SLuigi Rizzo 	dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
1671f196ce38SLuigi Rizzo 			NETMAP_BUF_SIZE, DMA_TO_DEVICE);
1672f196ce38SLuigi Rizzo 	buffer_info->dma = dma_map_single(&adapter->pdev->dev,
1673f196ce38SLuigi Rizzo 			addr, NETMAP_BUF_SIZE, DMA_TO_DEVICE);
1674f196ce38SLuigi Rizzo 
1675f196ce38SLuigi Rizzo 	if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
1676f196ce38SLuigi Rizzo 		D("dma mapping error");
1677f196ce38SLuigi Rizzo 		/* goto dma_error; See e1000_put_txbuf() */
1678f196ce38SLuigi Rizzo 		/* XXX reset */
1679f196ce38SLuigi Rizzo 	}
1680f196ce38SLuigi Rizzo 	tx_desc->buffer_addr = htole64(buffer_info->dma); //XXX
1681f196ce38SLuigi Rizzo 
1682f196ce38SLuigi Rizzo #endif
1683f196ce38SLuigi Rizzo 
1684f196ce38SLuigi Rizzo /*
1685f196ce38SLuigi Rizzo  * The bus_dmamap_sync() can be one of wmb() or rmb() depending on direction.
1686f196ce38SLuigi Rizzo  */
1687f196ce38SLuigi Rizzo #define bus_dmamap_sync(_a, _b, _c)
1688f196ce38SLuigi Rizzo 
1689f196ce38SLuigi Rizzo #endif /* linux */
16906dba29a2SLuigi Rizzo 
1691ce3ee1e7SLuigi Rizzo 
16925644ccecSLuigi Rizzo /*
16935644ccecSLuigi Rizzo  * functions to map NIC to KRING indexes (n2k) and vice versa (k2n)
16945644ccecSLuigi Rizzo  */
16955644ccecSLuigi Rizzo static inline int
169664ae02c3SLuigi Rizzo netmap_idx_n2k(struct netmap_kring *kr, int idx)
16975644ccecSLuigi Rizzo {
169864ae02c3SLuigi Rizzo 	int n = kr->nkr_num_slots;
169964ae02c3SLuigi Rizzo 	idx += kr->nkr_hwofs;
170064ae02c3SLuigi Rizzo 	if (idx < 0)
170164ae02c3SLuigi Rizzo 		return idx + n;
170264ae02c3SLuigi Rizzo 	else if (idx < n)
170364ae02c3SLuigi Rizzo 		return idx;
17045644ccecSLuigi Rizzo 	else
170564ae02c3SLuigi Rizzo 		return idx - n;
17065644ccecSLuigi Rizzo }
17075644ccecSLuigi Rizzo 
17085644ccecSLuigi Rizzo 
17095644ccecSLuigi Rizzo static inline int
171064ae02c3SLuigi Rizzo netmap_idx_k2n(struct netmap_kring *kr, int idx)
17115644ccecSLuigi Rizzo {
171264ae02c3SLuigi Rizzo 	int n = kr->nkr_num_slots;
171364ae02c3SLuigi Rizzo 	idx -= kr->nkr_hwofs;
171464ae02c3SLuigi Rizzo 	if (idx < 0)
171564ae02c3SLuigi Rizzo 		return idx + n;
171664ae02c3SLuigi Rizzo 	else if (idx < n)
171764ae02c3SLuigi Rizzo 		return idx;
17185644ccecSLuigi Rizzo 	else
171964ae02c3SLuigi Rizzo 		return idx - n;
17205644ccecSLuigi Rizzo }
17215644ccecSLuigi Rizzo 
17225644ccecSLuigi Rizzo 
1723d76bf4ffSLuigi Rizzo /* Entries of the look-up table. */
1724d76bf4ffSLuigi Rizzo struct lut_entry {
1725d76bf4ffSLuigi Rizzo 	void *vaddr;		/* virtual address. */
1726849bec0eSLuigi Rizzo 	vm_paddr_t paddr;	/* physical address. */
1727d76bf4ffSLuigi Rizzo };
1728d76bf4ffSLuigi Rizzo 
1729d76bf4ffSLuigi Rizzo struct netmap_obj_pool;
1730d76bf4ffSLuigi Rizzo 
173168b8534bSLuigi Rizzo /*
17326e10c8b8SLuigi Rizzo  * NMB return the virtual address of a buffer (buffer 0 on bad index)
17336e10c8b8SLuigi Rizzo  * PNMB also fills the physical address
173468b8534bSLuigi Rizzo  */
17356e10c8b8SLuigi Rizzo static inline void *
17364bf50f18SLuigi Rizzo NMB(struct netmap_adapter *na, struct netmap_slot *slot)
1737f9790aebSLuigi Rizzo {
1738847bf383SLuigi Rizzo 	struct lut_entry *lut = na->na_lut.lut;
1739f9790aebSLuigi Rizzo 	uint32_t i = slot->buf_idx;
1740847bf383SLuigi Rizzo 	return (unlikely(i >= na->na_lut.objtotal)) ?
1741f9790aebSLuigi Rizzo 		lut[0].vaddr : lut[i].vaddr;
1742f9790aebSLuigi Rizzo }
1743f9790aebSLuigi Rizzo 
17444bf50f18SLuigi Rizzo static inline void *
17454bf50f18SLuigi Rizzo PNMB(struct netmap_adapter *na, struct netmap_slot *slot, uint64_t *pp)
17464bf50f18SLuigi Rizzo {
17474bf50f18SLuigi Rizzo 	uint32_t i = slot->buf_idx;
1748847bf383SLuigi Rizzo 	struct lut_entry *lut = na->na_lut.lut;
1749847bf383SLuigi Rizzo 	void *ret = (i >= na->na_lut.objtotal) ? lut[0].vaddr : lut[i].vaddr;
17504bf50f18SLuigi Rizzo 
175137e3a6d3SLuigi Rizzo #ifndef _WIN32
1752847bf383SLuigi Rizzo 	*pp = (i >= na->na_lut.objtotal) ? lut[0].paddr : lut[i].paddr;
175337e3a6d3SLuigi Rizzo #else
175437e3a6d3SLuigi Rizzo 	*pp = (i >= na->na_lut.objtotal) ? (uint64_t)lut[0].paddr.QuadPart : (uint64_t)lut[i].paddr.QuadPart;
175537e3a6d3SLuigi Rizzo #endif
17564bf50f18SLuigi Rizzo 	return ret;
17574bf50f18SLuigi Rizzo }
17584bf50f18SLuigi Rizzo 
1759f9790aebSLuigi Rizzo 
176017885a7bSLuigi Rizzo /*
17618fd44c93SLuigi Rizzo  * Structure associated to each netmap file descriptor.
17628fd44c93SLuigi Rizzo  * It is created on open and left unbound (np_nifp == NULL).
17638fd44c93SLuigi Rizzo  * A successful NIOCREGIF will set np_nifp and the first few fields;
17648fd44c93SLuigi Rizzo  * this is protected by a global lock (NMG_LOCK) due to low contention.
1765f9790aebSLuigi Rizzo  *
17668fd44c93SLuigi Rizzo  * np_refs counts the number of references to the structure: one for the fd,
17678fd44c93SLuigi Rizzo  * plus (on FreeBSD) one for each active mmap which we track ourselves
176885fe4e7cSLuigi Rizzo  * (linux automatically tracks them, but FreeBSD does not).
17698fd44c93SLuigi Rizzo  * np_refs is protected by NMG_LOCK.
177017885a7bSLuigi Rizzo  *
17718fd44c93SLuigi Rizzo  * Read access to the structure is lock free, because ni_nifp once set
17728fd44c93SLuigi Rizzo  * can only go to 0 when nobody is using the entry anymore. Readers
17738fd44c93SLuigi Rizzo  * must check that np_nifp != NULL before using the other fields.
1774f9790aebSLuigi Rizzo  */
1775f9790aebSLuigi Rizzo struct netmap_priv_d {
1776f9790aebSLuigi Rizzo 	struct netmap_if * volatile np_nifp;	/* netmap if descriptor. */
1777f9790aebSLuigi Rizzo 
1778f9790aebSLuigi Rizzo 	struct netmap_adapter	*np_na;
177937e3a6d3SLuigi Rizzo 	struct ifnet	*np_ifp;
1780f0ea3689SLuigi Rizzo 	uint32_t	np_flags;	/* from the ioctl */
1781847bf383SLuigi Rizzo 	u_int		np_qfirst[NR_TXRX],
1782847bf383SLuigi Rizzo 			np_qlast[NR_TXRX]; /* range of tx/rx rings to scan */
1783f0ea3689SLuigi Rizzo 	uint16_t	np_txpoll;	/* XXX and also np_rxpoll ? */
1784f9790aebSLuigi Rizzo 
17858fd44c93SLuigi Rizzo 	int		np_refs;	/* use with NMG_LOCK held */
1786f0ea3689SLuigi Rizzo 
1787f0ea3689SLuigi Rizzo 	/* pointers to the selinfo to be used for selrecord.
1788f0ea3689SLuigi Rizzo 	 * Either the local or the global one depending on the
1789f0ea3689SLuigi Rizzo 	 * number of rings.
1790f0ea3689SLuigi Rizzo 	 */
1791847bf383SLuigi Rizzo 	NM_SELINFO_T *np_si[NR_TXRX];
1792f0ea3689SLuigi Rizzo 	struct thread	*np_td;		/* kqueue, just debugging */
1793f9790aebSLuigi Rizzo };
1794f9790aebSLuigi Rizzo 
179537e3a6d3SLuigi Rizzo struct netmap_priv_d *netmap_priv_new(void);
179637e3a6d3SLuigi Rizzo void netmap_priv_delete(struct netmap_priv_d *);
179737e3a6d3SLuigi Rizzo 
179837e3a6d3SLuigi Rizzo static inline int nm_kring_pending(struct netmap_priv_d *np)
179937e3a6d3SLuigi Rizzo {
180037e3a6d3SLuigi Rizzo 	struct netmap_adapter *na = np->np_na;
180137e3a6d3SLuigi Rizzo 	enum txrx t;
180237e3a6d3SLuigi Rizzo 	int i;
180337e3a6d3SLuigi Rizzo 
180437e3a6d3SLuigi Rizzo 	for_rx_tx(t) {
180537e3a6d3SLuigi Rizzo 		for (i = np->np_qfirst[t]; i < np->np_qlast[t]; i++) {
180637e3a6d3SLuigi Rizzo 			struct netmap_kring *kring = &NMR(na, t)[i];
180737e3a6d3SLuigi Rizzo 			if (kring->nr_mode != kring->nr_pending_mode) {
180837e3a6d3SLuigi Rizzo 				return 1;
180937e3a6d3SLuigi Rizzo 			}
181037e3a6d3SLuigi Rizzo 		}
181137e3a6d3SLuigi Rizzo 	}
181237e3a6d3SLuigi Rizzo 	return 0;
181337e3a6d3SLuigi Rizzo }
181437e3a6d3SLuigi Rizzo 
18154bf50f18SLuigi Rizzo #ifdef WITH_MONITOR
18164bf50f18SLuigi Rizzo 
18174bf50f18SLuigi Rizzo struct netmap_monitor_adapter {
18184bf50f18SLuigi Rizzo 	struct netmap_adapter up;
18194bf50f18SLuigi Rizzo 
18204bf50f18SLuigi Rizzo 	struct netmap_priv_d priv;
18214bf50f18SLuigi Rizzo 	uint32_t flags;
18224bf50f18SLuigi Rizzo };
18234bf50f18SLuigi Rizzo 
18244bf50f18SLuigi Rizzo #endif /* WITH_MONITOR */
18254bf50f18SLuigi Rizzo 
1826f9790aebSLuigi Rizzo 
1827039dd540SLuigi Rizzo #ifdef WITH_GENERIC
1828f9790aebSLuigi Rizzo /*
1829f9790aebSLuigi Rizzo  * generic netmap emulation for devices that do not have
1830f9790aebSLuigi Rizzo  * native netmap support.
1831f9790aebSLuigi Rizzo  */
1832f9790aebSLuigi Rizzo int generic_netmap_attach(struct ifnet *ifp);
183337e3a6d3SLuigi Rizzo int generic_rx_handler(struct ifnet *ifp, struct mbuf *m);;
1834f9790aebSLuigi Rizzo 
183537e3a6d3SLuigi Rizzo int nm_os_catch_rx(struct netmap_generic_adapter *gna, int intercept);
183637e3a6d3SLuigi Rizzo int nm_os_catch_tx(struct netmap_generic_adapter *gna, int intercept);
183737e3a6d3SLuigi Rizzo 
183837e3a6d3SLuigi Rizzo /*
183937e3a6d3SLuigi Rizzo  * the generic transmit routine is passed a structure to optionally
184037e3a6d3SLuigi Rizzo  * build a queue of descriptors, in an OS-specific way.
184137e3a6d3SLuigi Rizzo  * The payload is at addr, if non-null, and the routine should send or queue
184237e3a6d3SLuigi Rizzo  * the packet, returning 0 if successful, 1 on failure.
184337e3a6d3SLuigi Rizzo  *
184437e3a6d3SLuigi Rizzo  * At the end, if head is non-null, there will be an additional call
184537e3a6d3SLuigi Rizzo  * to the function with addr = NULL; this should tell the OS-specific
184637e3a6d3SLuigi Rizzo  * routine to send the queue and free any resources. Failure is ignored.
184737e3a6d3SLuigi Rizzo  */
184837e3a6d3SLuigi Rizzo struct nm_os_gen_arg {
184937e3a6d3SLuigi Rizzo 	struct ifnet *ifp;
185037e3a6d3SLuigi Rizzo 	void *m;	/* os-specific mbuf-like object */
185137e3a6d3SLuigi Rizzo 	void *head, *tail; /* tailq, if the OS-specific routine needs to build one */
185237e3a6d3SLuigi Rizzo 	void *addr;	/* payload of current packet */
185337e3a6d3SLuigi Rizzo 	u_int len;	/* packet length */
185437e3a6d3SLuigi Rizzo 	u_int ring_nr;	/* packet length */
185537e3a6d3SLuigi Rizzo 	u_int qevent;   /* in txqdisc mode, place an event on this mbuf */
185637e3a6d3SLuigi Rizzo };
185737e3a6d3SLuigi Rizzo 
185837e3a6d3SLuigi Rizzo int nm_os_generic_xmit_frame(struct nm_os_gen_arg *);
185937e3a6d3SLuigi Rizzo int nm_os_generic_find_num_desc(struct ifnet *ifp, u_int *tx, u_int *rx);
186037e3a6d3SLuigi Rizzo void nm_os_generic_find_num_queues(struct ifnet *ifp, u_int *txq, u_int *rxq);
186137e3a6d3SLuigi Rizzo void nm_os_generic_set_features(struct netmap_generic_adapter *gna);
186237e3a6d3SLuigi Rizzo 
1863847bf383SLuigi Rizzo static inline struct ifnet*
1864847bf383SLuigi Rizzo netmap_generic_getifp(struct netmap_generic_adapter *gna)
1865847bf383SLuigi Rizzo {
1866847bf383SLuigi Rizzo         if (gna->prev)
1867847bf383SLuigi Rizzo             return gna->prev->ifp;
1868847bf383SLuigi Rizzo 
1869847bf383SLuigi Rizzo         return gna->up.up.ifp;
1870847bf383SLuigi Rizzo }
1871f9790aebSLuigi Rizzo 
187237e3a6d3SLuigi Rizzo void netmap_generic_irq(struct netmap_adapter *na, u_int q, u_int *work_done);
187337e3a6d3SLuigi Rizzo 
18744bf50f18SLuigi Rizzo //#define RATE_GENERIC  /* Enables communication statistics for generic. */
18754bf50f18SLuigi Rizzo #ifdef RATE_GENERIC
18764bf50f18SLuigi Rizzo void generic_rate(int txp, int txs, int txi, int rxp, int rxs, int rxi);
18774bf50f18SLuigi Rizzo #else
18784bf50f18SLuigi Rizzo #define generic_rate(txp, txs, txi, rxp, rxs, rxi)
18794bf50f18SLuigi Rizzo #endif
18804bf50f18SLuigi Rizzo 
1881f9790aebSLuigi Rizzo /*
1882f9790aebSLuigi Rizzo  * netmap_mitigation API. This is used by the generic adapter
1883f9790aebSLuigi Rizzo  * to reduce the number of interrupt requests/selwakeup
1884f9790aebSLuigi Rizzo  * to clients on incoming packets.
1885f9790aebSLuigi Rizzo  */
188637e3a6d3SLuigi Rizzo void nm_os_mitigation_init(struct nm_generic_mit *mit, int idx,
18874bf50f18SLuigi Rizzo                                 struct netmap_adapter *na);
188837e3a6d3SLuigi Rizzo void nm_os_mitigation_start(struct nm_generic_mit *mit);
188937e3a6d3SLuigi Rizzo void nm_os_mitigation_restart(struct nm_generic_mit *mit);
189037e3a6d3SLuigi Rizzo int nm_os_mitigation_active(struct nm_generic_mit *mit);
189137e3a6d3SLuigi Rizzo void nm_os_mitigation_cleanup(struct nm_generic_mit *mit);
189237e3a6d3SLuigi Rizzo #else /* !WITH_GENERIC */
189337e3a6d3SLuigi Rizzo #define generic_netmap_attach(ifp)	(EOPNOTSUPP)
1894039dd540SLuigi Rizzo #endif /* WITH_GENERIC */
1895f0ea3689SLuigi Rizzo 
1896f0ea3689SLuigi Rizzo /* Shared declarations for the VALE switch. */
1897f0ea3689SLuigi Rizzo 
1898f0ea3689SLuigi Rizzo /*
1899f0ea3689SLuigi Rizzo  * Each transmit queue accumulates a batch of packets into
1900f0ea3689SLuigi Rizzo  * a structure before forwarding. Packets to the same
1901f0ea3689SLuigi Rizzo  * destination are put in a list using ft_next as a link field.
1902f0ea3689SLuigi Rizzo  * ft_frags and ft_next are valid only on the first fragment.
1903f0ea3689SLuigi Rizzo  */
1904f0ea3689SLuigi Rizzo struct nm_bdg_fwd {	/* forwarding entry for a bridge */
1905f0ea3689SLuigi Rizzo 	void *ft_buf;		/* netmap or indirect buffer */
1906f0ea3689SLuigi Rizzo 	uint8_t ft_frags;	/* how many fragments (only on 1st frag) */
1907f0ea3689SLuigi Rizzo 	uint8_t _ft_port;	/* dst port (unused) */
1908f0ea3689SLuigi Rizzo 	uint16_t ft_flags;	/* flags, e.g. indirect */
1909f0ea3689SLuigi Rizzo 	uint16_t ft_len;	/* src fragment len */
1910f0ea3689SLuigi Rizzo 	uint16_t ft_next;	/* next packet to same destination */
1911f0ea3689SLuigi Rizzo };
1912f0ea3689SLuigi Rizzo 
1913f0ea3689SLuigi Rizzo /* struct 'virtio_net_hdr' from linux. */
1914f0ea3689SLuigi Rizzo struct nm_vnet_hdr {
1915f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_F_NEEDS_CSUM     1	/* Use csum_start, csum_offset */
1916f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_F_DATA_VALID    2	/* Csum is valid */
1917f0ea3689SLuigi Rizzo     uint8_t flags;
1918f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_NONE         0       /* Not a GSO frame */
1919f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_TCPV4        1       /* GSO frame, IPv4 TCP (TSO) */
1920f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_UDP          3       /* GSO frame, IPv4 UDP (UFO) */
1921f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_TCPV6        4       /* GSO frame, IPv6 TCP */
1922f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_ECN          0x80    /* TCP has ECN set */
1923f0ea3689SLuigi Rizzo     uint8_t gso_type;
1924f0ea3689SLuigi Rizzo     uint16_t hdr_len;
1925f0ea3689SLuigi Rizzo     uint16_t gso_size;
1926f0ea3689SLuigi Rizzo     uint16_t csum_start;
1927f0ea3689SLuigi Rizzo     uint16_t csum_offset;
1928f0ea3689SLuigi Rizzo };
1929f0ea3689SLuigi Rizzo 
1930f0ea3689SLuigi Rizzo #define WORST_CASE_GSO_HEADER	(14+40+60)  /* IPv6 + TCP */
1931f0ea3689SLuigi Rizzo 
1932f0ea3689SLuigi Rizzo /* Private definitions for IPv4, IPv6, UDP and TCP headers. */
1933f0ea3689SLuigi Rizzo 
1934f0ea3689SLuigi Rizzo struct nm_iphdr {
1935f0ea3689SLuigi Rizzo 	uint8_t		version_ihl;
1936f0ea3689SLuigi Rizzo 	uint8_t		tos;
1937f0ea3689SLuigi Rizzo 	uint16_t	tot_len;
1938f0ea3689SLuigi Rizzo 	uint16_t	id;
1939f0ea3689SLuigi Rizzo 	uint16_t	frag_off;
1940f0ea3689SLuigi Rizzo 	uint8_t		ttl;
1941f0ea3689SLuigi Rizzo 	uint8_t		protocol;
1942f0ea3689SLuigi Rizzo 	uint16_t	check;
1943f0ea3689SLuigi Rizzo 	uint32_t	saddr;
1944f0ea3689SLuigi Rizzo 	uint32_t	daddr;
1945f0ea3689SLuigi Rizzo 	/*The options start here. */
1946f0ea3689SLuigi Rizzo };
1947f0ea3689SLuigi Rizzo 
1948f0ea3689SLuigi Rizzo struct nm_tcphdr {
1949f0ea3689SLuigi Rizzo 	uint16_t	source;
1950f0ea3689SLuigi Rizzo 	uint16_t	dest;
1951f0ea3689SLuigi Rizzo 	uint32_t	seq;
1952f0ea3689SLuigi Rizzo 	uint32_t	ack_seq;
1953f0ea3689SLuigi Rizzo 	uint8_t		doff;  /* Data offset + Reserved */
1954f0ea3689SLuigi Rizzo 	uint8_t		flags;
1955f0ea3689SLuigi Rizzo 	uint16_t	window;
1956f0ea3689SLuigi Rizzo 	uint16_t	check;
1957f0ea3689SLuigi Rizzo 	uint16_t	urg_ptr;
1958f0ea3689SLuigi Rizzo };
1959f0ea3689SLuigi Rizzo 
1960f0ea3689SLuigi Rizzo struct nm_udphdr {
1961f0ea3689SLuigi Rizzo 	uint16_t	source;
1962f0ea3689SLuigi Rizzo 	uint16_t	dest;
1963f0ea3689SLuigi Rizzo 	uint16_t	len;
1964f0ea3689SLuigi Rizzo 	uint16_t	check;
1965f0ea3689SLuigi Rizzo };
1966f0ea3689SLuigi Rizzo 
1967f0ea3689SLuigi Rizzo struct nm_ipv6hdr {
1968f0ea3689SLuigi Rizzo 	uint8_t		priority_version;
1969f0ea3689SLuigi Rizzo 	uint8_t		flow_lbl[3];
1970f0ea3689SLuigi Rizzo 
1971f0ea3689SLuigi Rizzo 	uint16_t	payload_len;
1972f0ea3689SLuigi Rizzo 	uint8_t		nexthdr;
1973f0ea3689SLuigi Rizzo 	uint8_t		hop_limit;
1974f0ea3689SLuigi Rizzo 
1975f0ea3689SLuigi Rizzo 	uint8_t		saddr[16];
1976f0ea3689SLuigi Rizzo 	uint8_t		daddr[16];
1977f0ea3689SLuigi Rizzo };
1978f0ea3689SLuigi Rizzo 
1979f0ea3689SLuigi Rizzo /* Type used to store a checksum (in host byte order) that hasn't been
1980f0ea3689SLuigi Rizzo  * folded yet.
1981f0ea3689SLuigi Rizzo  */
1982f0ea3689SLuigi Rizzo #define rawsum_t uint32_t
1983f0ea3689SLuigi Rizzo 
198437e3a6d3SLuigi Rizzo rawsum_t nm_os_csum_raw(uint8_t *data, size_t len, rawsum_t cur_sum);
198537e3a6d3SLuigi Rizzo uint16_t nm_os_csum_ipv4(struct nm_iphdr *iph);
198637e3a6d3SLuigi Rizzo void nm_os_csum_tcpudp_ipv4(struct nm_iphdr *iph, void *data,
1987f0ea3689SLuigi Rizzo 		      size_t datalen, uint16_t *check);
198837e3a6d3SLuigi Rizzo void nm_os_csum_tcpudp_ipv6(struct nm_ipv6hdr *ip6h, void *data,
1989f0ea3689SLuigi Rizzo 		      size_t datalen, uint16_t *check);
199037e3a6d3SLuigi Rizzo uint16_t nm_os_csum_fold(rawsum_t cur_sum);
1991f0ea3689SLuigi Rizzo 
1992f0ea3689SLuigi Rizzo void bdg_mismatch_datapath(struct netmap_vp_adapter *na,
1993f0ea3689SLuigi Rizzo 			   struct netmap_vp_adapter *dst_na,
199437e3a6d3SLuigi Rizzo 			   const struct nm_bdg_fwd *ft_p,
199537e3a6d3SLuigi Rizzo 			   struct netmap_ring *dst_ring,
1996f0ea3689SLuigi Rizzo 			   u_int *j, u_int lim, u_int *howmany);
19974bf50f18SLuigi Rizzo 
19984bf50f18SLuigi Rizzo /* persistent virtual port routines */
199937e3a6d3SLuigi Rizzo int nm_os_vi_persist(const char *, struct ifnet **);
200037e3a6d3SLuigi Rizzo void nm_os_vi_detach(struct ifnet *);
200137e3a6d3SLuigi Rizzo void nm_os_vi_init_index(void);
200237e3a6d3SLuigi Rizzo 
200337e3a6d3SLuigi Rizzo /*
200437e3a6d3SLuigi Rizzo  * kernel thread routines
200537e3a6d3SLuigi Rizzo  */
200637e3a6d3SLuigi Rizzo struct nm_kthread; /* OS-specific kthread - opaque */
200737e3a6d3SLuigi Rizzo typedef void (*nm_kthread_worker_fn_t)(void *data);
200837e3a6d3SLuigi Rizzo 
200937e3a6d3SLuigi Rizzo /* kthread configuration */
201037e3a6d3SLuigi Rizzo struct nm_kthread_cfg {
201137e3a6d3SLuigi Rizzo 	long				type;		/* kthread type/identifier */
201237e3a6d3SLuigi Rizzo 	nm_kthread_worker_fn_t		worker_fn;	/* worker function */
201337e3a6d3SLuigi Rizzo 	void				*worker_private;/* worker parameter */
201437e3a6d3SLuigi Rizzo 	int				attach_user;	/* attach kthread to user process */
201537e3a6d3SLuigi Rizzo };
201637e3a6d3SLuigi Rizzo /* kthread configuration */
2017*844a6f0cSLuigi Rizzo struct nm_kthread *nm_os_kthread_create(struct nm_kthread_cfg *cfg,
2018*844a6f0cSLuigi Rizzo 					unsigned int cfgtype,
2019*844a6f0cSLuigi Rizzo 					void *opaque);
202037e3a6d3SLuigi Rizzo int nm_os_kthread_start(struct nm_kthread *);
202137e3a6d3SLuigi Rizzo void nm_os_kthread_stop(struct nm_kthread *);
202237e3a6d3SLuigi Rizzo void nm_os_kthread_delete(struct nm_kthread *);
202337e3a6d3SLuigi Rizzo void nm_os_kthread_wakeup_worker(struct nm_kthread *nmk);
202437e3a6d3SLuigi Rizzo void nm_os_kthread_send_irq(struct nm_kthread *);
202537e3a6d3SLuigi Rizzo void nm_os_kthread_set_affinity(struct nm_kthread *, int);
202637e3a6d3SLuigi Rizzo u_int nm_os_ncpus(void);
202737e3a6d3SLuigi Rizzo 
202837e3a6d3SLuigi Rizzo #ifdef WITH_PTNETMAP_HOST
202937e3a6d3SLuigi Rizzo /*
203037e3a6d3SLuigi Rizzo  * netmap adapter for host ptnetmap ports
203137e3a6d3SLuigi Rizzo  */
203237e3a6d3SLuigi Rizzo struct netmap_pt_host_adapter {
203337e3a6d3SLuigi Rizzo 	struct netmap_adapter up;
203437e3a6d3SLuigi Rizzo 
203537e3a6d3SLuigi Rizzo 	struct netmap_adapter *parent;
203637e3a6d3SLuigi Rizzo 	int (*parent_nm_notify)(struct netmap_kring *kring, int flags);
203737e3a6d3SLuigi Rizzo 	void *ptns;
203837e3a6d3SLuigi Rizzo };
203937e3a6d3SLuigi Rizzo /* ptnetmap HOST routines */
204037e3a6d3SLuigi Rizzo int netmap_get_pt_host_na(struct nmreq *nmr, struct netmap_adapter **na, int create);
204137e3a6d3SLuigi Rizzo int ptnetmap_ctl(struct nmreq *nmr, struct netmap_adapter *na);
204237e3a6d3SLuigi Rizzo static inline int
204337e3a6d3SLuigi Rizzo nm_ptnetmap_host_on(struct netmap_adapter *na)
204437e3a6d3SLuigi Rizzo {
204537e3a6d3SLuigi Rizzo 	return na && na->na_flags & NAF_PTNETMAP_HOST;
204637e3a6d3SLuigi Rizzo }
204737e3a6d3SLuigi Rizzo #else /* !WITH_PTNETMAP_HOST */
204837e3a6d3SLuigi Rizzo #define netmap_get_pt_host_na(nmr, _2, _3) \
204937e3a6d3SLuigi Rizzo 	((nmr)->nr_flags & (NR_PTNETMAP_HOST) ? EOPNOTSUPP : 0)
205037e3a6d3SLuigi Rizzo #define ptnetmap_ctl(_1, _2)   EINVAL
205137e3a6d3SLuigi Rizzo #define nm_ptnetmap_host_on(_1)   EINVAL
205237e3a6d3SLuigi Rizzo #endif /* !WITH_PTNETMAP_HOST */
205337e3a6d3SLuigi Rizzo 
205437e3a6d3SLuigi Rizzo #ifdef WITH_PTNETMAP_GUEST
205537e3a6d3SLuigi Rizzo /* ptnetmap GUEST routines */
205637e3a6d3SLuigi Rizzo 
205737e3a6d3SLuigi Rizzo /*
205837e3a6d3SLuigi Rizzo  * netmap adapter for guest ptnetmap ports
205937e3a6d3SLuigi Rizzo  */
206037e3a6d3SLuigi Rizzo struct netmap_pt_guest_adapter {
206137e3a6d3SLuigi Rizzo         /* The netmap adapter to be used by netmap applications.
206237e3a6d3SLuigi Rizzo 	 * This field must be the first, to allow upcast. */
206337e3a6d3SLuigi Rizzo 	struct netmap_hw_adapter hwup;
206437e3a6d3SLuigi Rizzo 
206537e3a6d3SLuigi Rizzo         /* The netmap adapter to be used by the driver. */
206637e3a6d3SLuigi Rizzo         struct netmap_hw_adapter dr;
206737e3a6d3SLuigi Rizzo 
206837e3a6d3SLuigi Rizzo 	void *csb;
206937e3a6d3SLuigi Rizzo 
207037e3a6d3SLuigi Rizzo 	/* Reference counter to track users of backend netmap port: the
207137e3a6d3SLuigi Rizzo 	 * network stack and netmap clients.
207237e3a6d3SLuigi Rizzo 	 * Used to decide when we need (de)allocate krings/rings and
207337e3a6d3SLuigi Rizzo 	 * start (stop) ptnetmap kthreads. */
207437e3a6d3SLuigi Rizzo 	int backend_regifs;
207537e3a6d3SLuigi Rizzo 
207637e3a6d3SLuigi Rizzo };
207737e3a6d3SLuigi Rizzo 
2078*844a6f0cSLuigi Rizzo int netmap_pt_guest_attach(struct netmap_adapter *na, void *csb,
2079*844a6f0cSLuigi Rizzo 			   unsigned int nifp_offset, unsigned int memid);
208037e3a6d3SLuigi Rizzo struct ptnet_ring;
208137e3a6d3SLuigi Rizzo bool netmap_pt_guest_txsync(struct ptnet_ring *ptring, struct netmap_kring *kring,
208237e3a6d3SLuigi Rizzo 			    int flags);
208337e3a6d3SLuigi Rizzo bool netmap_pt_guest_rxsync(struct ptnet_ring *ptring, struct netmap_kring *kring,
208437e3a6d3SLuigi Rizzo 			    int flags);
208537e3a6d3SLuigi Rizzo int ptnet_nm_krings_create(struct netmap_adapter *na);
208637e3a6d3SLuigi Rizzo void ptnet_nm_krings_delete(struct netmap_adapter *na);
208737e3a6d3SLuigi Rizzo void ptnet_nm_dtor(struct netmap_adapter *na);
208837e3a6d3SLuigi Rizzo #endif /* WITH_PTNETMAP_GUEST */
20894bf50f18SLuigi Rizzo 
209068b8534bSLuigi Rizzo #endif /* _NET_NETMAP_KERN_H_ */
2091