xref: /freebsd-14.2/sys/dev/netmap/netmap_kern.h (revision 85fe4e7c)
168b8534bSLuigi Rizzo /*
217885a7bSLuigi Rizzo  * Copyright (C) 2011-2014 Matteo Landi, Luigi Rizzo. All rights reserved.
317885a7bSLuigi Rizzo  * Copyright (C) 2013-2014 Universita` di Pisa. All rights reserved.
468b8534bSLuigi Rizzo  *
568b8534bSLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
668b8534bSLuigi Rizzo  * modification, are permitted provided that the following conditions
768b8534bSLuigi Rizzo  * are met:
868b8534bSLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
968b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
1068b8534bSLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
1168b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
1268b8534bSLuigi Rizzo  *    documentation and/or other materials provided with the distribution.
1368b8534bSLuigi Rizzo  *
1468b8534bSLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1568b8534bSLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1668b8534bSLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1768b8534bSLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1868b8534bSLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1968b8534bSLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2068b8534bSLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2168b8534bSLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2268b8534bSLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2368b8534bSLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2468b8534bSLuigi Rizzo  * SUCH DAMAGE.
2568b8534bSLuigi Rizzo  */
2668b8534bSLuigi Rizzo 
2768b8534bSLuigi Rizzo /*
2868b8534bSLuigi Rizzo  * $FreeBSD$
2968b8534bSLuigi Rizzo  *
3068b8534bSLuigi Rizzo  * The header contains the definitions of constants and function
3168b8534bSLuigi Rizzo  * prototypes used only in kernelspace.
3268b8534bSLuigi Rizzo  */
3368b8534bSLuigi Rizzo 
3468b8534bSLuigi Rizzo #ifndef _NET_NETMAP_KERN_H_
3568b8534bSLuigi Rizzo #define _NET_NETMAP_KERN_H_
3668b8534bSLuigi Rizzo 
37847bf383SLuigi Rizzo #if defined(linux)
38847bf383SLuigi Rizzo 
39847bf383SLuigi Rizzo #if  defined(CONFIG_NETMAP_VALE)
40847bf383SLuigi Rizzo #define WITH_VALE
41847bf383SLuigi Rizzo #endif
42847bf383SLuigi Rizzo #if defined(CONFIG_NETMAP_PIPE)
43847bf383SLuigi Rizzo #define WITH_PIPES
44847bf383SLuigi Rizzo #endif
45847bf383SLuigi Rizzo #if defined(CONFIG_NETMAP_MONITOR)
46847bf383SLuigi Rizzo #define WITH_MONITOR
47847bf383SLuigi Rizzo #endif
48847bf383SLuigi Rizzo #if defined(CONFIG_NETMAP_GENERIC)
49847bf383SLuigi Rizzo #define WITH_GENERIC
50847bf383SLuigi Rizzo #endif
51847bf383SLuigi Rizzo #if defined(CONFIG_NETMAP_V1000)
52847bf383SLuigi Rizzo #define WITH_V1000
53847bf383SLuigi Rizzo #endif
54847bf383SLuigi Rizzo 
55847bf383SLuigi Rizzo #else /* not linux */
56847bf383SLuigi Rizzo 
57f9790aebSLuigi Rizzo #define WITH_VALE	// comment out to disable VALE support
58f0ea3689SLuigi Rizzo #define WITH_PIPES
594bf50f18SLuigi Rizzo #define WITH_MONITOR
60039dd540SLuigi Rizzo #define WITH_GENERIC
61f9790aebSLuigi Rizzo 
62847bf383SLuigi Rizzo #endif
63847bf383SLuigi Rizzo 
641a26580eSLuigi Rizzo #if defined(__FreeBSD__)
65d4b42e08SLuigi Rizzo 
66ce3ee1e7SLuigi Rizzo #define likely(x)	__builtin_expect((long)!!(x), 1L)
67ce3ee1e7SLuigi Rizzo #define unlikely(x)	__builtin_expect((long)!!(x), 0L)
68f196ce38SLuigi Rizzo 
69847bf383SLuigi Rizzo #define	NM_LOCK_T	struct mtx	/* low level spinlock, used to protect queues */
70039dd540SLuigi Rizzo 
71847bf383SLuigi Rizzo #define NM_MTX_T	struct sx	/* OS-specific mutex (sleepable) */
72847bf383SLuigi Rizzo #define NM_MTX_INIT(m)		sx_init(&(m), #m)
73847bf383SLuigi Rizzo #define NM_MTX_DESTROY(m)	sx_destroy(&(m))
74847bf383SLuigi Rizzo #define NM_MTX_LOCK(m)		sx_xlock(&(m))
75847bf383SLuigi Rizzo #define NM_MTX_UNLOCK(m)	sx_xunlock(&(m))
76847bf383SLuigi Rizzo #define NM_MTX_ASSERT(m)	sx_assert(&(m), SA_XLOCKED)
77f9790aebSLuigi Rizzo 
780e73f29aSLuigi Rizzo #define	NM_SELINFO_T	struct nm_selinfo
791a26580eSLuigi Rizzo #define	MBUF_LEN(m)	((m)->m_pkthdr.len)
80f9790aebSLuigi Rizzo #define	MBUF_IFP(m)	((m)->m_pkthdr.rcvif)
8117885a7bSLuigi Rizzo #define	NM_SEND_UP(ifp, m)	((NA(ifp))->if_input)(ifp, m)
82d4b42e08SLuigi Rizzo 
83f9790aebSLuigi Rizzo #define NM_ATOMIC_T	volatile int	// XXX ?
84f9790aebSLuigi Rizzo /* atomic operations */
85f9790aebSLuigi Rizzo #include <machine/atomic.h>
86f9790aebSLuigi Rizzo #define NM_ATOMIC_TEST_AND_SET(p)       (!atomic_cmpset_acq_int((p), 0, 1))
87f9790aebSLuigi Rizzo #define NM_ATOMIC_CLEAR(p)              atomic_store_rel_int((p), 0)
88f9790aebSLuigi Rizzo 
897f154b71SLuigi Rizzo #if __FreeBSD_version >= 1100030
907f154b71SLuigi Rizzo #define	WNA(_ifp)	(_ifp)->if_netmap
917f154b71SLuigi Rizzo #else /* older FreeBSD */
927f154b71SLuigi Rizzo #define	WNA(_ifp)	(_ifp)->if_pspare[0]
937f154b71SLuigi Rizzo #endif /* older FreeBSD */
947f154b71SLuigi Rizzo 
9546aa1303SLuigi Rizzo #if __FreeBSD_version >= 1100005
9646aa1303SLuigi Rizzo struct netmap_adapter *netmap_getna(if_t ifp);
9746aa1303SLuigi Rizzo #endif
98f9790aebSLuigi Rizzo 
994bf50f18SLuigi Rizzo #if __FreeBSD_version >= 1100027
1004bf50f18SLuigi Rizzo #define GET_MBUF_REFCNT(m)      ((m)->m_ext.ext_cnt ? *((m)->m_ext.ext_cnt) : -1)
1014bf50f18SLuigi Rizzo #define SET_MBUF_REFCNT(m, x)   *((m)->m_ext.ext_cnt) = x
1024bf50f18SLuigi Rizzo #define PNT_MBUF_REFCNT(m)      ((m)->m_ext.ext_cnt)
1034bf50f18SLuigi Rizzo #else
1044bf50f18SLuigi Rizzo #define GET_MBUF_REFCNT(m)      ((m)->m_ext.ref_cnt ? *((m)->m_ext.ref_cnt) : -1)
1054bf50f18SLuigi Rizzo #define SET_MBUF_REFCNT(m, x)   *((m)->m_ext.ref_cnt) = x
1064bf50f18SLuigi Rizzo #define PNT_MBUF_REFCNT(m)      ((m)->m_ext.ref_cnt)
1074bf50f18SLuigi Rizzo #endif
1084bf50f18SLuigi Rizzo 
109f9790aebSLuigi Rizzo MALLOC_DECLARE(M_NETMAP);
110f9790aebSLuigi Rizzo 
1110e73f29aSLuigi Rizzo struct nm_selinfo {
1120e73f29aSLuigi Rizzo 	struct selinfo si;
1130e73f29aSLuigi Rizzo 	struct mtx m;
1140e73f29aSLuigi Rizzo };
1150e73f29aSLuigi Rizzo 
1160e73f29aSLuigi Rizzo void freebsd_selwakeup(struct nm_selinfo *si, int pri);
1170e73f29aSLuigi Rizzo 
118f9790aebSLuigi Rizzo // XXX linux struct, not used in FreeBSD
119f9790aebSLuigi Rizzo struct net_device_ops {
120f9790aebSLuigi Rizzo };
1214bf50f18SLuigi Rizzo struct ethtool_ops {
1224bf50f18SLuigi Rizzo };
123f9790aebSLuigi Rizzo struct hrtimer {
124f9790aebSLuigi Rizzo };
125847bf383SLuigi Rizzo #define NM_BNS_GET(b)
126847bf383SLuigi Rizzo #define NM_BNS_PUT(b)
127ce3ee1e7SLuigi Rizzo 
12864ae02c3SLuigi Rizzo #elif defined (linux)
129d4b42e08SLuigi Rizzo 
1302579e2d7SLuigi Rizzo #define	NM_LOCK_T	safe_spinlock_t	// see bsd_glue.h
1311a26580eSLuigi Rizzo #define	NM_SELINFO_T	wait_queue_head_t
1321a26580eSLuigi Rizzo #define	MBUF_LEN(m)	((m)->len)
133f9790aebSLuigi Rizzo #define	MBUF_IFP(m)	((m)->dev)
13417885a7bSLuigi Rizzo #define	NM_SEND_UP(ifp, m)  \
13517885a7bSLuigi Rizzo                         do { \
1364bf50f18SLuigi Rizzo                             m->priority = NM_MAGIC_PRIORITY_RX; \
13717885a7bSLuigi Rizzo                             netif_rx(m); \
13817885a7bSLuigi Rizzo                         } while (0)
139f196ce38SLuigi Rizzo 
140ce3ee1e7SLuigi Rizzo #define NM_ATOMIC_T	volatile long unsigned int
141ce3ee1e7SLuigi Rizzo 
142847bf383SLuigi Rizzo #define NM_MTX_T	struct mutex	/* OS-specific sleepable lock */
143847bf383SLuigi Rizzo #define NM_MTX_INIT(m)	mutex_init(&(m))
144847bf383SLuigi Rizzo #define NM_MTX_DESTROY(m)	do { (void)(m); } while (0)
145039dd540SLuigi Rizzo #define NM_MTX_LOCK(m)		mutex_lock(&(m))
146039dd540SLuigi Rizzo #define NM_MTX_UNLOCK(m)	mutex_unlock(&(m))
147847bf383SLuigi Rizzo #define NM_MTX_ASSERT(m)	mutex_is_locked(&(m))
148f9790aebSLuigi Rizzo 
149f196ce38SLuigi Rizzo #ifndef DEV_NETMAP
150f196ce38SLuigi Rizzo #define DEV_NETMAP
151ce3ee1e7SLuigi Rizzo #endif /* DEV_NETMAP */
152f196ce38SLuigi Rizzo 
153f196ce38SLuigi Rizzo #elif defined (__APPLE__)
154d4b42e08SLuigi Rizzo 
1558241616dSLuigi Rizzo #warning apple support is incomplete.
156f196ce38SLuigi Rizzo #define likely(x)	__builtin_expect(!!(x), 1)
157f196ce38SLuigi Rizzo #define unlikely(x)	__builtin_expect(!!(x), 0)
158f196ce38SLuigi Rizzo #define	NM_LOCK_T	IOLock *
159f196ce38SLuigi Rizzo #define	NM_SELINFO_T	struct selinfo
160f196ce38SLuigi Rizzo #define	MBUF_LEN(m)	((m)->m_pkthdr.len)
161f196ce38SLuigi Rizzo #define	NM_SEND_UP(ifp, m)	((ifp)->if_input)(ifp, m)
162f196ce38SLuigi Rizzo 
1631a26580eSLuigi Rizzo #else
164d4b42e08SLuigi Rizzo 
1651a26580eSLuigi Rizzo #error unsupported platform
166d4b42e08SLuigi Rizzo 
167d4b42e08SLuigi Rizzo #endif /* end - platform-specific code */
1681a26580eSLuigi Rizzo 
169847bf383SLuigi Rizzo #define	NMG_LOCK_T		NM_MTX_T
170847bf383SLuigi Rizzo #define	NMG_LOCK_INIT()		NM_MTX_INIT(netmap_global_lock)
171847bf383SLuigi Rizzo #define	NMG_LOCK_DESTROY()	NM_MTX_DESTROY(netmap_global_lock)
172847bf383SLuigi Rizzo #define	NMG_LOCK()		NM_MTX_LOCK(netmap_global_lock)
173847bf383SLuigi Rizzo #define	NMG_UNLOCK()		NM_MTX_UNLOCK(netmap_global_lock)
174847bf383SLuigi Rizzo #define	NMG_LOCK_ASSERT()	NM_MTX_ASSERT(netmap_global_lock)
175847bf383SLuigi Rizzo 
17668b8534bSLuigi Rizzo #define ND(format, ...)
17768b8534bSLuigi Rizzo #define D(format, ...)						\
17868b8534bSLuigi Rizzo 	do {							\
17968b8534bSLuigi Rizzo 		struct timeval __xxts;				\
18068b8534bSLuigi Rizzo 		microtime(&__xxts);				\
18117885a7bSLuigi Rizzo 		printf("%03d.%06d [%4d] %-25s " format "\n",	\
18268b8534bSLuigi Rizzo 		(int)__xxts.tv_sec % 1000, (int)__xxts.tv_usec,	\
18317885a7bSLuigi Rizzo 		__LINE__, __FUNCTION__, ##__VA_ARGS__);		\
18468b8534bSLuigi Rizzo 	} while (0)
18568b8534bSLuigi Rizzo 
1868241616dSLuigi Rizzo /* rate limited, lps indicates how many per second */
1878241616dSLuigi Rizzo #define RD(lps, format, ...)					\
1888241616dSLuigi Rizzo 	do {							\
1898241616dSLuigi Rizzo 		static int t0, __cnt;				\
1908241616dSLuigi Rizzo 		if (t0 != time_second) {			\
1918241616dSLuigi Rizzo 			t0 = time_second;			\
1928241616dSLuigi Rizzo 			__cnt = 0;				\
1938241616dSLuigi Rizzo 		}						\
1948241616dSLuigi Rizzo 		if (__cnt++ < lps)				\
1958241616dSLuigi Rizzo 			D(format, ##__VA_ARGS__);		\
1968241616dSLuigi Rizzo 	} while (0)
1978241616dSLuigi Rizzo 
19868b8534bSLuigi Rizzo struct netmap_adapter;
199f18be576SLuigi Rizzo struct nm_bdg_fwd;
200f18be576SLuigi Rizzo struct nm_bridge;
201f18be576SLuigi Rizzo struct netmap_priv_d;
20268b8534bSLuigi Rizzo 
203ce3ee1e7SLuigi Rizzo const char *nm_dump_buf(char *p, int len, int lim, char *dst);
204ce3ee1e7SLuigi Rizzo 
205f9790aebSLuigi Rizzo #include "netmap_mbq.h"
206f9790aebSLuigi Rizzo 
207f9790aebSLuigi Rizzo extern NMG_LOCK_T	netmap_global_lock;
208f9790aebSLuigi Rizzo 
209847bf383SLuigi Rizzo enum txrx { NR_RX = 0, NR_TX = 1, NR_TXRX };
210847bf383SLuigi Rizzo 
211847bf383SLuigi Rizzo static __inline const char*
212847bf383SLuigi Rizzo nm_txrx2str(enum txrx t)
213847bf383SLuigi Rizzo {
214847bf383SLuigi Rizzo 	return (t== NR_RX ? "RX" : "TX");
215847bf383SLuigi Rizzo }
216847bf383SLuigi Rizzo 
217847bf383SLuigi Rizzo static __inline enum txrx
218847bf383SLuigi Rizzo nm_txrx_swap(enum txrx t)
219847bf383SLuigi Rizzo {
220847bf383SLuigi Rizzo 	return (t== NR_RX ? NR_TX : NR_RX);
221847bf383SLuigi Rizzo }
222847bf383SLuigi Rizzo 
223847bf383SLuigi Rizzo #define for_rx_tx(t)	for ((t) = 0; (t) < NR_TXRX; (t)++)
224847bf383SLuigi Rizzo 
225847bf383SLuigi Rizzo 
22668b8534bSLuigi Rizzo /*
22764ae02c3SLuigi Rizzo  * private, kernel view of a ring. Keeps track of the status of
22864ae02c3SLuigi Rizzo  * a ring across system calls.
22964ae02c3SLuigi Rizzo  *
23064ae02c3SLuigi Rizzo  *	nr_hwcur	index of the next buffer to refill.
23117885a7bSLuigi Rizzo  *			It corresponds to ring->head
23217885a7bSLuigi Rizzo  *			at the time the system call returns.
23364ae02c3SLuigi Rizzo  *
23417885a7bSLuigi Rizzo  *	nr_hwtail	index of the first buffer owned by the kernel.
23517885a7bSLuigi Rizzo  *			On RX, hwcur->hwtail are receive buffers
23617885a7bSLuigi Rizzo  *			not yet released. hwcur is advanced following
23717885a7bSLuigi Rizzo  *			ring->head, hwtail is advanced on incoming packets,
23817885a7bSLuigi Rizzo  *			and a wakeup is generated when hwtail passes ring->cur
23917885a7bSLuigi Rizzo  *			    On TX, hwcur->rcur have been filled by the sender
24017885a7bSLuigi Rizzo  *			but not sent yet to the NIC; rcur->hwtail are available
24117885a7bSLuigi Rizzo  *			for new transmissions, and hwtail->hwcur-1 are pending
24217885a7bSLuigi Rizzo  *			transmissions not yet acknowledged.
24368b8534bSLuigi Rizzo  *
2441a26580eSLuigi Rizzo  * The indexes in the NIC and netmap rings are offset by nkr_hwofs slots.
24568b8534bSLuigi Rizzo  * This is so that, on a reset, buffers owned by userspace are not
24668b8534bSLuigi Rizzo  * modified by the kernel. In particular:
24717885a7bSLuigi Rizzo  * RX rings: the next empty buffer (hwtail + hwofs) coincides with
24868b8534bSLuigi Rizzo  * 	the next empty buffer as known by the hardware (next_to_check or so).
24968b8534bSLuigi Rizzo  * TX rings: hwcur + hwofs coincides with next_to_send
2501dce924dSLuigi Rizzo  *
2511dce924dSLuigi Rizzo  * For received packets, slot->flags is set to nkr_slot_flags
2521dce924dSLuigi Rizzo  * so we can provide a proper initial value (e.g. set NS_FORWARD
2531dce924dSLuigi Rizzo  * when operating in 'transparent' mode).
254ce3ee1e7SLuigi Rizzo  *
255ce3ee1e7SLuigi Rizzo  * The following fields are used to implement lock-free copy of packets
256ce3ee1e7SLuigi Rizzo  * from input to output ports in VALE switch:
257ce3ee1e7SLuigi Rizzo  *	nkr_hwlease	buffer after the last one being copied.
258ce3ee1e7SLuigi Rizzo  *			A writer in nm_bdg_flush reserves N buffers
259ce3ee1e7SLuigi Rizzo  *			from nr_hwlease, advances it, then does the
260ce3ee1e7SLuigi Rizzo  *			copy outside the lock.
261ce3ee1e7SLuigi Rizzo  *			In RX rings (used for VALE ports),
26217885a7bSLuigi Rizzo  *			nkr_hwtail <= nkr_hwlease < nkr_hwcur+N-1
263ce3ee1e7SLuigi Rizzo  *			In TX rings (used for NIC or host stack ports)
26417885a7bSLuigi Rizzo  *			nkr_hwcur <= nkr_hwlease < nkr_hwtail
265ce3ee1e7SLuigi Rizzo  *	nkr_leases	array of nkr_num_slots where writers can report
266ce3ee1e7SLuigi Rizzo  *			completion of their block. NR_NOSLOT (~0) indicates
267ce3ee1e7SLuigi Rizzo  *			that the writer has not finished yet
268ce3ee1e7SLuigi Rizzo  *	nkr_lease_idx	index of next free slot in nr_leases, to be assigned
269ce3ee1e7SLuigi Rizzo  *
270ce3ee1e7SLuigi Rizzo  * The kring is manipulated by txsync/rxsync and generic netmap function.
27117885a7bSLuigi Rizzo  *
27217885a7bSLuigi Rizzo  * Concurrent rxsync or txsync on the same ring are prevented through
27389cc2556SLuigi Rizzo  * by nm_kr_(try)lock() which in turn uses nr_busy. This is all we need
27417885a7bSLuigi Rizzo  * for NIC rings, and for TX rings attached to the host stack.
27517885a7bSLuigi Rizzo  *
27617885a7bSLuigi Rizzo  * RX rings attached to the host stack use an mbq (rx_queue) on both
27717885a7bSLuigi Rizzo  * rxsync_from_host() and netmap_transmit(). The mbq is protected
27817885a7bSLuigi Rizzo  * by its internal lock.
27917885a7bSLuigi Rizzo  *
2804bf50f18SLuigi Rizzo  * RX rings attached to the VALE switch are accessed by both senders
28117885a7bSLuigi Rizzo  * and receiver. They are protected through the q_lock on the RX ring.
28268b8534bSLuigi Rizzo  */
28368b8534bSLuigi Rizzo struct netmap_kring {
28468b8534bSLuigi Rizzo 	struct netmap_ring	*ring;
28517885a7bSLuigi Rizzo 
286ce3ee1e7SLuigi Rizzo 	uint32_t	nr_hwcur;
28717885a7bSLuigi Rizzo 	uint32_t	nr_hwtail;
28817885a7bSLuigi Rizzo 
28917885a7bSLuigi Rizzo 	/*
29017885a7bSLuigi Rizzo 	 * Copies of values in user rings, so we do not need to look
29117885a7bSLuigi Rizzo 	 * at the ring (which could be modified). These are set in the
29217885a7bSLuigi Rizzo 	 * *sync_prologue()/finalize() routines.
29317885a7bSLuigi Rizzo 	 */
29417885a7bSLuigi Rizzo 	uint32_t	rhead;
29517885a7bSLuigi Rizzo 	uint32_t	rcur;
29617885a7bSLuigi Rizzo 	uint32_t	rtail;
29717885a7bSLuigi Rizzo 
298ce3ee1e7SLuigi Rizzo 	uint32_t	nr_kflags;	/* private driver flags */
2992157a17cSLuigi Rizzo #define NKR_PENDINTR	0x1		// Pending interrupt.
300847bf383SLuigi Rizzo #define NKR_EXCLUSIVE	0x2		/* exclusive binding */
301ce3ee1e7SLuigi Rizzo 	uint32_t	nkr_num_slots;
30217885a7bSLuigi Rizzo 
30317885a7bSLuigi Rizzo 	/*
30417885a7bSLuigi Rizzo 	 * On a NIC reset, the NIC ring indexes may be reset but the
30517885a7bSLuigi Rizzo 	 * indexes in the netmap rings remain the same. nkr_hwofs
30617885a7bSLuigi Rizzo 	 * keeps track of the offset between the two.
30717885a7bSLuigi Rizzo 	 */
30817885a7bSLuigi Rizzo 	int32_t		nkr_hwofs;
30968b8534bSLuigi Rizzo 
3101dce924dSLuigi Rizzo 	uint16_t	nkr_slot_flags;	/* initial value for flags */
31117885a7bSLuigi Rizzo 
31217885a7bSLuigi Rizzo 	/* last_reclaim is opaque marker to help reduce the frequency
31317885a7bSLuigi Rizzo 	 * of operations such as reclaiming tx buffers. A possible use
31417885a7bSLuigi Rizzo 	 * is set it to ticks and do the reclaim only once per tick.
31517885a7bSLuigi Rizzo 	 */
31617885a7bSLuigi Rizzo 	uint64_t	last_reclaim;
31717885a7bSLuigi Rizzo 
318ce3ee1e7SLuigi Rizzo 
3191a26580eSLuigi Rizzo 	NM_SELINFO_T	si;		/* poll/select wait queue */
320ce3ee1e7SLuigi Rizzo 	NM_LOCK_T	q_lock;		/* protects kring and ring. */
321ce3ee1e7SLuigi Rizzo 	NM_ATOMIC_T	nr_busy;	/* prevent concurrent syscalls */
322ce3ee1e7SLuigi Rizzo 
32317885a7bSLuigi Rizzo 	struct netmap_adapter *na;
32417885a7bSLuigi Rizzo 
3256435a0dcSLuigi Rizzo 	/* The following fields are for VALE switch support */
32617885a7bSLuigi Rizzo 	struct nm_bdg_fwd *nkr_ft;
32717885a7bSLuigi Rizzo 	uint32_t	*nkr_leases;
32817885a7bSLuigi Rizzo #define NR_NOSLOT	((uint32_t)~0)	/* used in nkr_*lease* */
32917885a7bSLuigi Rizzo 	uint32_t	nkr_hwlease;
33017885a7bSLuigi Rizzo 	uint32_t	nkr_lease_idx;
33117885a7bSLuigi Rizzo 
3324bf50f18SLuigi Rizzo 	/* while nkr_stopped is set, no new [tr]xsync operations can
3334bf50f18SLuigi Rizzo 	 * be started on this kring.
3344bf50f18SLuigi Rizzo 	 * This is used by netmap_disable_all_rings()
3354bf50f18SLuigi Rizzo 	 * to find a synchronization point where critical data
3364bf50f18SLuigi Rizzo 	 * structures pointed to by the kring can be added or removed
3374bf50f18SLuigi Rizzo 	 */
3384bf50f18SLuigi Rizzo 	volatile int nkr_stopped;
339f9790aebSLuigi Rizzo 
340f0ea3689SLuigi Rizzo 	/* Support for adapters without native netmap support.
341f9790aebSLuigi Rizzo 	 * On tx rings we preallocate an array of tx buffers
342f9790aebSLuigi Rizzo 	 * (same size as the netmap ring), on rx rings we
343f0ea3689SLuigi Rizzo 	 * store incoming mbufs in a queue that is drained by
344f0ea3689SLuigi Rizzo 	 * a rxsync.
345f9790aebSLuigi Rizzo 	 */
346f9790aebSLuigi Rizzo 	struct mbuf **tx_pool;
34717885a7bSLuigi Rizzo 	// u_int nr_ntc;		/* Emulation of a next-to-clean RX ring pointer. */
34817885a7bSLuigi Rizzo 	struct mbq rx_queue;            /* intercepted rx mbufs. */
34917885a7bSLuigi Rizzo 
350847bf383SLuigi Rizzo 	uint32_t	users;		/* existing bindings for this ring */
351847bf383SLuigi Rizzo 
35217885a7bSLuigi Rizzo 	uint32_t	ring_id;	/* debugging */
353847bf383SLuigi Rizzo 	enum txrx	tx;		/* kind of ring (tx or rx) */
35417885a7bSLuigi Rizzo 	char name[64];			/* diagnostic */
355f9790aebSLuigi Rizzo 
3564bf50f18SLuigi Rizzo 	/* [tx]sync callback for this kring.
3574bf50f18SLuigi Rizzo 	 * The default nm_kring_create callback (netmap_krings_create)
3584bf50f18SLuigi Rizzo 	 * sets the nm_sync callback of each hardware tx(rx) kring to
3594bf50f18SLuigi Rizzo 	 * the corresponding nm_txsync(nm_rxsync) taken from the
3604bf50f18SLuigi Rizzo 	 * netmap_adapter; moreover, it sets the sync callback
3614bf50f18SLuigi Rizzo 	 * of the host tx(rx) ring to netmap_txsync_to_host
3624bf50f18SLuigi Rizzo 	 * (netmap_rxsync_from_host).
3634bf50f18SLuigi Rizzo 	 *
3644bf50f18SLuigi Rizzo 	 * Overrides: the above configuration is not changed by
3654bf50f18SLuigi Rizzo 	 * any of the nm_krings_create callbacks.
3664bf50f18SLuigi Rizzo 	 */
367f0ea3689SLuigi Rizzo 	int (*nm_sync)(struct netmap_kring *kring, int flags);
368847bf383SLuigi Rizzo 	int (*nm_notify)(struct netmap_kring *kring, int flags);
369f0ea3689SLuigi Rizzo 
370f0ea3689SLuigi Rizzo #ifdef WITH_PIPES
3714bf50f18SLuigi Rizzo 	struct netmap_kring *pipe;	/* if this is a pipe ring,
3724bf50f18SLuigi Rizzo 					 * pointer to the other end
3734bf50f18SLuigi Rizzo 					 */
3744bf50f18SLuigi Rizzo 	struct netmap_ring *save_ring;	/* pointer to hidden rings
3754bf50f18SLuigi Rizzo        					 * (see netmap_pipe.c for details)
3764bf50f18SLuigi Rizzo 					 */
377f0ea3689SLuigi Rizzo #endif /* WITH_PIPES */
378f0ea3689SLuigi Rizzo 
379847bf383SLuigi Rizzo #ifdef WITH_VALE
380847bf383SLuigi Rizzo 	int (*save_notify)(struct netmap_kring *kring, int flags);
381847bf383SLuigi Rizzo #endif
382847bf383SLuigi Rizzo 
3834bf50f18SLuigi Rizzo #ifdef WITH_MONITOR
384847bf383SLuigi Rizzo 	/* array of krings that are monitoring this kring */
385847bf383SLuigi Rizzo 	struct netmap_kring **monitors;
386847bf383SLuigi Rizzo 	uint32_t max_monitors; /* current size of the monitors array */
387847bf383SLuigi Rizzo 	uint32_t n_monitors;	/* next unused entry in the monitor array */
3884bf50f18SLuigi Rizzo 	/*
389847bf383SLuigi Rizzo 	 * Monitors work by intercepting the sync and notify callbacks of the
390847bf383SLuigi Rizzo 	 * monitored krings. This is implemented by replacing the pointers
391847bf383SLuigi Rizzo 	 * above and saving the previous ones in mon_* pointers below
3924bf50f18SLuigi Rizzo 	 */
393847bf383SLuigi Rizzo 	int (*mon_sync)(struct netmap_kring *kring, int flags);
394847bf383SLuigi Rizzo 	int (*mon_notify)(struct netmap_kring *kring, int flags);
395847bf383SLuigi Rizzo 
396847bf383SLuigi Rizzo 	uint32_t mon_tail;  /* last seen slot on rx */
397847bf383SLuigi Rizzo 	uint32_t mon_pos;   /* index of this ring in the monitored ring array */
3984bf50f18SLuigi Rizzo #endif
3992157a17cSLuigi Rizzo } __attribute__((__aligned__(64)));
40068b8534bSLuigi Rizzo 
401ce3ee1e7SLuigi Rizzo 
402ce3ee1e7SLuigi Rizzo /* return the next index, with wraparound */
403ce3ee1e7SLuigi Rizzo static inline uint32_t
404ce3ee1e7SLuigi Rizzo nm_next(uint32_t i, uint32_t lim)
405ce3ee1e7SLuigi Rizzo {
406ce3ee1e7SLuigi Rizzo 	return unlikely (i == lim) ? 0 : i + 1;
407ce3ee1e7SLuigi Rizzo }
408ce3ee1e7SLuigi Rizzo 
40917885a7bSLuigi Rizzo 
41017885a7bSLuigi Rizzo /* return the previous index, with wraparound */
41117885a7bSLuigi Rizzo static inline uint32_t
41217885a7bSLuigi Rizzo nm_prev(uint32_t i, uint32_t lim)
41317885a7bSLuigi Rizzo {
41417885a7bSLuigi Rizzo 	return unlikely (i == 0) ? lim : i - 1;
41517885a7bSLuigi Rizzo }
41617885a7bSLuigi Rizzo 
41717885a7bSLuigi Rizzo 
418ce3ee1e7SLuigi Rizzo /*
419ce3ee1e7SLuigi Rizzo  *
420ce3ee1e7SLuigi Rizzo  * Here is the layout for the Rx and Tx rings.
421ce3ee1e7SLuigi Rizzo 
422ce3ee1e7SLuigi Rizzo        RxRING                            TxRING
423ce3ee1e7SLuigi Rizzo 
424ce3ee1e7SLuigi Rizzo       +-----------------+            +-----------------+
425ce3ee1e7SLuigi Rizzo       |                 |            |                 |
426ce3ee1e7SLuigi Rizzo       |XXX free slot XXX|            |XXX free slot XXX|
427ce3ee1e7SLuigi Rizzo       +-----------------+            +-----------------+
42817885a7bSLuigi Rizzo head->| owned by user   |<-hwcur     | not sent to nic |<-hwcur
42917885a7bSLuigi Rizzo       |                 |            | yet             |
43017885a7bSLuigi Rizzo       +-----------------+            |                 |
43117885a7bSLuigi Rizzo  cur->| available to    |            |                 |
43217885a7bSLuigi Rizzo       | user, not read  |            +-----------------+
43317885a7bSLuigi Rizzo       | yet             |       cur->| (being          |
43417885a7bSLuigi Rizzo       |                 |            |  prepared)      |
435ce3ee1e7SLuigi Rizzo       |                 |            |                 |
43617885a7bSLuigi Rizzo       +-----------------+            +     ------      +
43717885a7bSLuigi Rizzo tail->|                 |<-hwtail    |                 |<-hwlease
43817885a7bSLuigi Rizzo       | (being          | ...        |                 | ...
43917885a7bSLuigi Rizzo       |  prepared)      | ...        |                 | ...
44017885a7bSLuigi Rizzo       +-----------------+ ...        |                 | ...
44117885a7bSLuigi Rizzo       |                 |<-hwlease   +-----------------+
44217885a7bSLuigi Rizzo       |                 |      tail->|                 |<-hwtail
443ce3ee1e7SLuigi Rizzo       |                 |            |                 |
444ce3ee1e7SLuigi Rizzo       |                 |            |                 |
445ce3ee1e7SLuigi Rizzo       |                 |            |                 |
446ce3ee1e7SLuigi Rizzo       +-----------------+            +-----------------+
447ce3ee1e7SLuigi Rizzo 
44817885a7bSLuigi Rizzo  * The cur/tail (user view) and hwcur/hwtail (kernel view)
449ce3ee1e7SLuigi Rizzo  * are used in the normal operation of the card.
450ce3ee1e7SLuigi Rizzo  *
451ce3ee1e7SLuigi Rizzo  * When a ring is the output of a switch port (Rx ring for
452ce3ee1e7SLuigi Rizzo  * a VALE port, Tx ring for the host stack or NIC), slots
453ce3ee1e7SLuigi Rizzo  * are reserved in blocks through 'hwlease' which points
454ce3ee1e7SLuigi Rizzo  * to the next unused slot.
45517885a7bSLuigi Rizzo  * On an Rx ring, hwlease is always after hwtail,
45617885a7bSLuigi Rizzo  * and completions cause hwtail to advance.
45717885a7bSLuigi Rizzo  * On a Tx ring, hwlease is always between cur and hwtail,
458ce3ee1e7SLuigi Rizzo  * and completions cause cur to advance.
459ce3ee1e7SLuigi Rizzo  *
460ce3ee1e7SLuigi Rizzo  * nm_kr_space() returns the maximum number of slots that
461ce3ee1e7SLuigi Rizzo  * can be assigned.
462ce3ee1e7SLuigi Rizzo  * nm_kr_lease() reserves the required number of buffers,
463ce3ee1e7SLuigi Rizzo  *    advances nkr_hwlease and also returns an entry in
464ce3ee1e7SLuigi Rizzo  *    a circular array where completions should be reported.
465ce3ee1e7SLuigi Rizzo  */
466ce3ee1e7SLuigi Rizzo 
467ce3ee1e7SLuigi Rizzo 
468847bf383SLuigi Rizzo struct netmap_lut {
469847bf383SLuigi Rizzo 	struct lut_entry *lut;
470847bf383SLuigi Rizzo 	uint32_t objtotal;	/* max buffer index */
471847bf383SLuigi Rizzo 	uint32_t objsize;	/* buffer size */
472847bf383SLuigi Rizzo };
473ce3ee1e7SLuigi Rizzo 
4744bf50f18SLuigi Rizzo struct netmap_vp_adapter; // forward
4754bf50f18SLuigi Rizzo 
47668b8534bSLuigi Rizzo /*
477f9790aebSLuigi Rizzo  * The "struct netmap_adapter" extends the "struct adapter"
478f9790aebSLuigi Rizzo  * (or equivalent) device descriptor.
479f9790aebSLuigi Rizzo  * It contains all base fields needed to support netmap operation.
480f9790aebSLuigi Rizzo  * There are in fact different types of netmap adapters
481f9790aebSLuigi Rizzo  * (native, generic, VALE switch...) so a netmap_adapter is
482f9790aebSLuigi Rizzo  * just the first field in the derived type.
48368b8534bSLuigi Rizzo  */
48468b8534bSLuigi Rizzo struct netmap_adapter {
4858241616dSLuigi Rizzo 	/*
4868241616dSLuigi Rizzo 	 * On linux we do not have a good way to tell if an interface
487f9790aebSLuigi Rizzo 	 * is netmap-capable. So we always use the following trick:
4888241616dSLuigi Rizzo 	 * NA(ifp) points here, and the first entry (which hopefully
4898241616dSLuigi Rizzo 	 * always exists and is at least 32 bits) contains a magic
4908241616dSLuigi Rizzo 	 * value which we can use to detect that the interface is good.
4918241616dSLuigi Rizzo 	 */
4928241616dSLuigi Rizzo 	uint32_t magic;
493f9790aebSLuigi Rizzo 	uint32_t na_flags;	/* enabled, and other flags */
4948241616dSLuigi Rizzo #define NAF_SKIP_INTR	1	/* use the regular interrupt handler.
4958241616dSLuigi Rizzo 				 * useful during initialization
4968241616dSLuigi Rizzo 				 */
497f18be576SLuigi Rizzo #define NAF_SW_ONLY	2	/* forward packets only to sw adapter */
498ce3ee1e7SLuigi Rizzo #define NAF_BDG_MAYSLEEP 4	/* the bridge is allowed to sleep when
499ce3ee1e7SLuigi Rizzo 				 * forwarding packets coming from this
500ce3ee1e7SLuigi Rizzo 				 * interface
501ce3ee1e7SLuigi Rizzo 				 */
502847bf383SLuigi Rizzo #define NAF_MEM_OWNER	8	/* the adapter uses its own memory area
503847bf383SLuigi Rizzo 				 * that cannot be changed
504ce3ee1e7SLuigi Rizzo 				 */
505847bf383SLuigi Rizzo #define NAF_NATIVE      16      /* the adapter is native.
506*85fe4e7cSLuigi Rizzo 				 * Virtual ports (non persistent vale ports,
507*85fe4e7cSLuigi Rizzo 				 * pipes, monitors...) should never use
508*85fe4e7cSLuigi Rizzo 				 * this flag.
509f9790aebSLuigi Rizzo 				 */
510f9790aebSLuigi Rizzo #define	NAF_NETMAP_ON	32	/* netmap is active (either native or
5114bf50f18SLuigi Rizzo 				 * emulated). Where possible (e.g. FreeBSD)
512f9790aebSLuigi Rizzo 				 * IFCAP_NETMAP also mirrors this flag.
513f9790aebSLuigi Rizzo 				 */
514f0ea3689SLuigi Rizzo #define NAF_HOST_RINGS  64	/* the adapter supports the host rings */
5154bf50f18SLuigi Rizzo #define NAF_FORCE_NATIVE 128	/* the adapter is always NATIVE */
5164bf50f18SLuigi Rizzo #define	NAF_BUSY	(1U<<31) /* the adapter is used internally and
5174bf50f18SLuigi Rizzo 				  * cannot be registered from userspace
5184bf50f18SLuigi Rizzo 				  */
519f9790aebSLuigi Rizzo 	int active_fds; /* number of user-space descriptors using this
52068b8534bSLuigi Rizzo 			 interface, which is equal to the number of
52168b8534bSLuigi Rizzo 			 struct netmap_if objs in the mapped region. */
52268b8534bSLuigi Rizzo 
52324e57ec9SEd Maste 	u_int num_rx_rings; /* number of adapter receive rings */
52424e57ec9SEd Maste 	u_int num_tx_rings; /* number of adapter transmit rings */
52568b8534bSLuigi Rizzo 
52668b8534bSLuigi Rizzo 	u_int num_tx_desc;  /* number of descriptor in each queue */
52768b8534bSLuigi Rizzo 	u_int num_rx_desc;
52868b8534bSLuigi Rizzo 
52968b8534bSLuigi Rizzo 	/* tx_rings and rx_rings are private but allocated
53068b8534bSLuigi Rizzo 	 * as a contiguous chunk of memory. Each array has
53168b8534bSLuigi Rizzo 	 * N+1 entries, for the adapter queues and for the host queue.
53268b8534bSLuigi Rizzo 	 */
53368b8534bSLuigi Rizzo 	struct netmap_kring *tx_rings; /* array of TX rings. */
53468b8534bSLuigi Rizzo 	struct netmap_kring *rx_rings; /* array of RX rings. */
53517885a7bSLuigi Rizzo 
536f9790aebSLuigi Rizzo 	void *tailroom;		       /* space below the rings array */
537f9790aebSLuigi Rizzo 				       /* (used for leases) */
538f9790aebSLuigi Rizzo 
53968b8534bSLuigi Rizzo 
540847bf383SLuigi Rizzo 	NM_SELINFO_T si[NR_TXRX];	/* global wait queues */
54164ae02c3SLuigi Rizzo 
542f0ea3689SLuigi Rizzo 	/* count users of the global wait queues */
543847bf383SLuigi Rizzo 	int si_users[NR_TXRX];
544f0ea3689SLuigi Rizzo 
5454bf50f18SLuigi Rizzo 	void *pdev; /* used to store pci device */
5464bf50f18SLuigi Rizzo 
54768b8534bSLuigi Rizzo 	/* copy of if_qflush and if_transmit pointers, to intercept
54868b8534bSLuigi Rizzo 	 * packets from the network stack when netmap is active.
54968b8534bSLuigi Rizzo 	 */
55068b8534bSLuigi Rizzo 	int     (*if_transmit)(struct ifnet *, struct mbuf *);
55168b8534bSLuigi Rizzo 
55217885a7bSLuigi Rizzo 	/* copy of if_input for netmap_send_up() */
55317885a7bSLuigi Rizzo 	void     (*if_input)(struct ifnet *, struct mbuf *);
55417885a7bSLuigi Rizzo 
55568b8534bSLuigi Rizzo 	/* references to the ifnet and device routines, used by
55668b8534bSLuigi Rizzo 	 * the generic netmap functions.
55768b8534bSLuigi Rizzo 	 */
55868b8534bSLuigi Rizzo 	struct ifnet *ifp; /* adapter is ifp->if_softc */
55968b8534bSLuigi Rizzo 
56017885a7bSLuigi Rizzo 	/*---- callbacks for this netmap adapter -----*/
56117885a7bSLuigi Rizzo 	/*
56217885a7bSLuigi Rizzo 	 * nm_dtor() is the cleanup routine called when destroying
56317885a7bSLuigi Rizzo 	 *	the adapter.
56489cc2556SLuigi Rizzo 	 *	Called with NMG_LOCK held.
56517885a7bSLuigi Rizzo 	 *
56617885a7bSLuigi Rizzo 	 * nm_register() is called on NIOCREGIF and close() to enter
56717885a7bSLuigi Rizzo 	 *	or exit netmap mode on the NIC
5684bf50f18SLuigi Rizzo 	 *	Called with NNG_LOCK held.
56917885a7bSLuigi Rizzo 	 *
57017885a7bSLuigi Rizzo 	 * nm_txsync() pushes packets to the underlying hw/switch
57117885a7bSLuigi Rizzo 	 *
57217885a7bSLuigi Rizzo 	 * nm_rxsync() collects packets from the underlying hw/switch
57317885a7bSLuigi Rizzo 	 *
57417885a7bSLuigi Rizzo 	 * nm_config() returns configuration information from the OS
57589cc2556SLuigi Rizzo 	 *	Called with NMG_LOCK held.
57617885a7bSLuigi Rizzo 	 *
5774bf50f18SLuigi Rizzo 	 * nm_krings_create() create and init the tx_rings and
5784bf50f18SLuigi Rizzo 	 * 	rx_rings arrays of kring structures. In particular,
5794bf50f18SLuigi Rizzo 	 * 	set the nm_sync callbacks for each ring.
5804bf50f18SLuigi Rizzo 	 * 	There is no need to also allocate the corresponding
5814bf50f18SLuigi Rizzo 	 * 	netmap_rings, since netmap_mem_rings_create() will always
5824bf50f18SLuigi Rizzo 	 * 	be called to provide the missing ones.
5834bf50f18SLuigi Rizzo 	 *	Called with NNG_LOCK held.
58417885a7bSLuigi Rizzo 	 *
5854bf50f18SLuigi Rizzo 	 * nm_krings_delete() cleanup and delete the tx_rings and rx_rings
5864bf50f18SLuigi Rizzo 	 * 	arrays
5874bf50f18SLuigi Rizzo 	 *	Called with NMG_LOCK held.
58817885a7bSLuigi Rizzo 	 *
58989cc2556SLuigi Rizzo 	 * nm_notify() is used to act after data have become available
59089cc2556SLuigi Rizzo 	 * 	(or the stopped state of the ring has changed)
59117885a7bSLuigi Rizzo 	 *	For hw devices this is typically a selwakeup(),
59217885a7bSLuigi Rizzo 	 *	but for NIC/host ports attached to a switch (or vice-versa)
59317885a7bSLuigi Rizzo 	 *	we also need to invoke the 'txsync' code downstream.
59417885a7bSLuigi Rizzo 	 */
595f9790aebSLuigi Rizzo 	void (*nm_dtor)(struct netmap_adapter *);
5961a26580eSLuigi Rizzo 
597f9790aebSLuigi Rizzo 	int (*nm_register)(struct netmap_adapter *, int onoff);
598ce3ee1e7SLuigi Rizzo 
5994bf50f18SLuigi Rizzo 	int (*nm_txsync)(struct netmap_kring *kring, int flags);
6004bf50f18SLuigi Rizzo 	int (*nm_rxsync)(struct netmap_kring *kring, int flags);
601847bf383SLuigi Rizzo 	int (*nm_notify)(struct netmap_kring *kring, int flags);
602ce3ee1e7SLuigi Rizzo #define NAF_FORCE_READ    1
603ce3ee1e7SLuigi Rizzo #define NAF_FORCE_RECLAIM 2
604ae10d1afSLuigi Rizzo 	/* return configuration information */
605f9790aebSLuigi Rizzo 	int (*nm_config)(struct netmap_adapter *,
606f9790aebSLuigi Rizzo 		u_int *txr, u_int *txd, u_int *rxr, u_int *rxd);
607f9790aebSLuigi Rizzo 	int (*nm_krings_create)(struct netmap_adapter *);
608f9790aebSLuigi Rizzo 	void (*nm_krings_delete)(struct netmap_adapter *);
6094bf50f18SLuigi Rizzo #ifdef WITH_VALE
6104bf50f18SLuigi Rizzo 	/*
6114bf50f18SLuigi Rizzo 	 * nm_bdg_attach() initializes the na_vp field to point
6124bf50f18SLuigi Rizzo 	 *      to an adapter that can be attached to a VALE switch. If the
6134bf50f18SLuigi Rizzo 	 *      current adapter is already a VALE port, na_vp is simply a cast;
6144bf50f18SLuigi Rizzo 	 *      otherwise, na_vp points to a netmap_bwrap_adapter.
6154bf50f18SLuigi Rizzo 	 *      If applicable, this callback also initializes na_hostvp,
6164bf50f18SLuigi Rizzo 	 *      that can be used to connect the adapter host rings to the
6174bf50f18SLuigi Rizzo 	 *      switch.
6184bf50f18SLuigi Rizzo 	 *      Called with NMG_LOCK held.
6194bf50f18SLuigi Rizzo 	 *
6204bf50f18SLuigi Rizzo 	 * nm_bdg_ctl() is called on the actual attach/detach to/from
6214bf50f18SLuigi Rizzo 	 *      to/from the switch, to perform adapter-specific
6224bf50f18SLuigi Rizzo 	 *      initializations
6234bf50f18SLuigi Rizzo 	 *      Called with NMG_LOCK held.
6244bf50f18SLuigi Rizzo 	 */
6254bf50f18SLuigi Rizzo 	int (*nm_bdg_attach)(const char *bdg_name, struct netmap_adapter *);
6264bf50f18SLuigi Rizzo 	int (*nm_bdg_ctl)(struct netmap_adapter *, struct nmreq *, int);
6274bf50f18SLuigi Rizzo 
6284bf50f18SLuigi Rizzo 	/* adapter used to attach this adapter to a VALE switch (if any) */
6294bf50f18SLuigi Rizzo 	struct netmap_vp_adapter *na_vp;
6304bf50f18SLuigi Rizzo 	/* adapter used to attach the host rings of this adapter
6314bf50f18SLuigi Rizzo 	 * to a VALE switch (if any) */
6324bf50f18SLuigi Rizzo 	struct netmap_vp_adapter *na_hostvp;
6334bf50f18SLuigi Rizzo #endif
634f9790aebSLuigi Rizzo 
635f9790aebSLuigi Rizzo 	/* standard refcount to control the lifetime of the adapter
636f9790aebSLuigi Rizzo 	 * (it should be equal to the lifetime of the corresponding ifp)
637f9790aebSLuigi Rizzo 	 */
638f9790aebSLuigi Rizzo 	int na_refcount;
639f9790aebSLuigi Rizzo 
640f9790aebSLuigi Rizzo 	/* memory allocator (opaque)
641f9790aebSLuigi Rizzo 	 * We also cache a pointer to the lut_entry for translating
642f9790aebSLuigi Rizzo 	 * buffer addresses, and the total number of buffers.
643f9790aebSLuigi Rizzo 	 */
644f9790aebSLuigi Rizzo  	struct netmap_mem_d *nm_mem;
645847bf383SLuigi Rizzo 	struct netmap_lut na_lut;
646f9790aebSLuigi Rizzo 
6474bf50f18SLuigi Rizzo 	/* additional information attached to this adapter
6484bf50f18SLuigi Rizzo 	 * by other netmap subsystems. Currently used by
6494bf50f18SLuigi Rizzo 	 * bwrap and LINUX/v1000.
650f9790aebSLuigi Rizzo 	 */
651f9790aebSLuigi Rizzo 	void *na_private;
652f0ea3689SLuigi Rizzo 
6534bf50f18SLuigi Rizzo 	/* array of pipes that have this adapter as a parent */
654f0ea3689SLuigi Rizzo 	struct netmap_pipe_adapter **na_pipes;
6554bf50f18SLuigi Rizzo 	int na_next_pipe;	/* next free slot in the array */
6564bf50f18SLuigi Rizzo 	int na_max_pipes;	/* size of the array */
6574bf50f18SLuigi Rizzo 
6584bf50f18SLuigi Rizzo 	char name[64];
659f9790aebSLuigi Rizzo };
660f9790aebSLuigi Rizzo 
661847bf383SLuigi Rizzo static __inline u_int
662847bf383SLuigi Rizzo nma_get_ndesc(struct netmap_adapter *na, enum txrx t)
663847bf383SLuigi Rizzo {
664847bf383SLuigi Rizzo 	return (t == NR_TX ? na->num_tx_desc : na->num_rx_desc);
665847bf383SLuigi Rizzo }
666847bf383SLuigi Rizzo 
667847bf383SLuigi Rizzo static __inline void
668847bf383SLuigi Rizzo nma_set_ndesc(struct netmap_adapter *na, enum txrx t, u_int v)
669847bf383SLuigi Rizzo {
670847bf383SLuigi Rizzo 	if (t == NR_TX)
671847bf383SLuigi Rizzo 		na->num_tx_desc = v;
672847bf383SLuigi Rizzo 	else
673847bf383SLuigi Rizzo 		na->num_rx_desc = v;
674847bf383SLuigi Rizzo }
675847bf383SLuigi Rizzo 
676847bf383SLuigi Rizzo static __inline u_int
677847bf383SLuigi Rizzo nma_get_nrings(struct netmap_adapter *na, enum txrx t)
678847bf383SLuigi Rizzo {
679847bf383SLuigi Rizzo 	return (t == NR_TX ? na->num_tx_rings : na->num_rx_rings);
680847bf383SLuigi Rizzo }
681847bf383SLuigi Rizzo 
682847bf383SLuigi Rizzo static __inline void
683847bf383SLuigi Rizzo nma_set_nrings(struct netmap_adapter *na, enum txrx t, u_int v)
684847bf383SLuigi Rizzo {
685847bf383SLuigi Rizzo 	if (t == NR_TX)
686847bf383SLuigi Rizzo 		na->num_tx_rings = v;
687847bf383SLuigi Rizzo 	else
688847bf383SLuigi Rizzo 		na->num_rx_rings = v;
689847bf383SLuigi Rizzo }
690847bf383SLuigi Rizzo 
691847bf383SLuigi Rizzo static __inline struct netmap_kring*
692847bf383SLuigi Rizzo NMR(struct netmap_adapter *na, enum txrx t)
693847bf383SLuigi Rizzo {
694847bf383SLuigi Rizzo 	return (t == NR_TX ? na->tx_rings : na->rx_rings);
695847bf383SLuigi Rizzo }
69617885a7bSLuigi Rizzo 
697f9790aebSLuigi Rizzo /*
698f9790aebSLuigi Rizzo  * If the NIC is owned by the kernel
699f9790aebSLuigi Rizzo  * (i.e., bridge), neither another bridge nor user can use it;
700f9790aebSLuigi Rizzo  * if the NIC is owned by a user, only users can share it.
701f9790aebSLuigi Rizzo  * Evaluation must be done under NMG_LOCK().
702f9790aebSLuigi Rizzo  */
7034bf50f18SLuigi Rizzo #define NETMAP_OWNED_BY_KERN(na)	((na)->na_flags & NAF_BUSY)
704f9790aebSLuigi Rizzo #define NETMAP_OWNED_BY_ANY(na) \
7054bf50f18SLuigi Rizzo 	(NETMAP_OWNED_BY_KERN(na) || ((na)->active_fds > 0))
706f9790aebSLuigi Rizzo 
707f9790aebSLuigi Rizzo /*
708f9790aebSLuigi Rizzo  * derived netmap adapters for various types of ports
709f9790aebSLuigi Rizzo  */
710f9790aebSLuigi Rizzo struct netmap_vp_adapter {	/* VALE software port */
711f9790aebSLuigi Rizzo 	struct netmap_adapter up;
712f196ce38SLuigi Rizzo 
713849bec0eSLuigi Rizzo 	/*
714849bec0eSLuigi Rizzo 	 * Bridge support:
715849bec0eSLuigi Rizzo 	 *
716849bec0eSLuigi Rizzo 	 * bdg_port is the port number used in the bridge;
717f18be576SLuigi Rizzo 	 * na_bdg points to the bridge this NA is attached to.
718849bec0eSLuigi Rizzo 	 */
719f196ce38SLuigi Rizzo 	int bdg_port;
720f18be576SLuigi Rizzo 	struct nm_bridge *na_bdg;
721f9790aebSLuigi Rizzo 	int retry;
722f9790aebSLuigi Rizzo 
723f0ea3689SLuigi Rizzo 	/* Offset of ethernet header for each packet. */
724f0ea3689SLuigi Rizzo 	u_int virt_hdr_len;
725f0ea3689SLuigi Rizzo 	/* Maximum Frame Size, used in bdg_mismatch_datapath() */
726f0ea3689SLuigi Rizzo 	u_int mfs;
727847bf383SLuigi Rizzo 	/* Last source MAC on this port */
728847bf383SLuigi Rizzo 	uint64_t last_smac;
729f9790aebSLuigi Rizzo };
730f9790aebSLuigi Rizzo 
73117885a7bSLuigi Rizzo 
732f9790aebSLuigi Rizzo struct netmap_hw_adapter {	/* physical device */
733f9790aebSLuigi Rizzo 	struct netmap_adapter up;
734f9790aebSLuigi Rizzo 
735f9790aebSLuigi Rizzo 	struct net_device_ops nm_ndo;	// XXX linux only
7364bf50f18SLuigi Rizzo 	struct ethtool_ops    nm_eto;	// XXX linux only
7374bf50f18SLuigi Rizzo 	const struct ethtool_ops*   save_ethtool;
7384bf50f18SLuigi Rizzo 
7394bf50f18SLuigi Rizzo 	int (*nm_hw_register)(struct netmap_adapter *, int onoff);
740f9790aebSLuigi Rizzo };
741f9790aebSLuigi Rizzo 
742039dd540SLuigi Rizzo #ifdef WITH_GENERIC
743f0ea3689SLuigi Rizzo /* Mitigation support. */
744f0ea3689SLuigi Rizzo struct nm_generic_mit {
745f0ea3689SLuigi Rizzo 	struct hrtimer mit_timer;
746f0ea3689SLuigi Rizzo 	int mit_pending;
7474bf50f18SLuigi Rizzo 	int mit_ring_idx;  /* index of the ring being mitigated */
748f0ea3689SLuigi Rizzo 	struct netmap_adapter *mit_na;  /* backpointer */
749f0ea3689SLuigi Rizzo };
75017885a7bSLuigi Rizzo 
75117885a7bSLuigi Rizzo struct netmap_generic_adapter {	/* emulated device */
752f9790aebSLuigi Rizzo 	struct netmap_hw_adapter up;
753f9790aebSLuigi Rizzo 
754f9790aebSLuigi Rizzo 	/* Pointer to a previously used netmap adapter. */
755f9790aebSLuigi Rizzo 	struct netmap_adapter *prev;
756f9790aebSLuigi Rizzo 
757f9790aebSLuigi Rizzo 	/* generic netmap adapters support:
758f9790aebSLuigi Rizzo 	 * a net_device_ops struct overrides ndo_select_queue(),
759f9790aebSLuigi Rizzo 	 * save_if_input saves the if_input hook (FreeBSD),
760f0ea3689SLuigi Rizzo 	 * mit implements rx interrupt mitigation,
761f9790aebSLuigi Rizzo 	 */
762f9790aebSLuigi Rizzo 	struct net_device_ops generic_ndo;
763f9790aebSLuigi Rizzo 	void (*save_if_input)(struct ifnet *, struct mbuf *);
764f9790aebSLuigi Rizzo 
765f0ea3689SLuigi Rizzo 	struct nm_generic_mit *mit;
76617885a7bSLuigi Rizzo #ifdef linux
76717885a7bSLuigi Rizzo         netdev_tx_t (*save_start_xmit)(struct mbuf *, struct ifnet *);
76817885a7bSLuigi Rizzo #endif
769f9790aebSLuigi Rizzo };
770039dd540SLuigi Rizzo #endif  /* WITH_GENERIC */
771f9790aebSLuigi Rizzo 
772f0ea3689SLuigi Rizzo static __inline int
773847bf383SLuigi Rizzo netmap_real_rings(struct netmap_adapter *na, enum txrx t)
774f0ea3689SLuigi Rizzo {
775847bf383SLuigi Rizzo 	return nma_get_nrings(na, t) + !!(na->na_flags & NAF_HOST_RINGS);
776f0ea3689SLuigi Rizzo }
777f0ea3689SLuigi Rizzo 
778f9790aebSLuigi Rizzo #ifdef WITH_VALE
779f9790aebSLuigi Rizzo 
78017885a7bSLuigi Rizzo /*
78117885a7bSLuigi Rizzo  * Bridge wrapper for non VALE ports attached to a VALE switch.
782f9790aebSLuigi Rizzo  *
78317885a7bSLuigi Rizzo  * The real device must already have its own netmap adapter (hwna).
78417885a7bSLuigi Rizzo  * The bridge wrapper and the hwna adapter share the same set of
78517885a7bSLuigi Rizzo  * netmap rings and buffers, but they have two separate sets of
78617885a7bSLuigi Rizzo  * krings descriptors, with tx/rx meanings swapped:
787f9790aebSLuigi Rizzo  *
788f9790aebSLuigi Rizzo  *                                  netmap
789f9790aebSLuigi Rizzo  *           bwrap     krings       rings      krings      hwna
790f9790aebSLuigi Rizzo  *         +------+   +------+     +-----+    +------+   +------+
791f9790aebSLuigi Rizzo  *         |tx_rings->|      |\   /|     |----|      |<-tx_rings|
792f9790aebSLuigi Rizzo  *         |      |   +------+ \ / +-----+    +------+   |      |
793f9790aebSLuigi Rizzo  *         |      |             X                        |      |
794f9790aebSLuigi Rizzo  *         |      |            / \                       |      |
795f9790aebSLuigi Rizzo  *         |      |   +------+/   \+-----+    +------+   |      |
796f9790aebSLuigi Rizzo  *         |rx_rings->|      |     |     |----|      |<-rx_rings|
797f9790aebSLuigi Rizzo  *         |      |   +------+     +-----+    +------+   |      |
798f9790aebSLuigi Rizzo  *         +------+                                      +------+
799f9790aebSLuigi Rizzo  *
80017885a7bSLuigi Rizzo  * - packets coming from the bridge go to the brwap rx rings,
80117885a7bSLuigi Rizzo  *   which are also the hwna tx rings.  The bwrap notify callback
80217885a7bSLuigi Rizzo  *   will then complete the hwna tx (see netmap_bwrap_notify).
803f9790aebSLuigi Rizzo  *
80417885a7bSLuigi Rizzo  * - packets coming from the outside go to the hwna rx rings,
80517885a7bSLuigi Rizzo  *   which are also the bwrap tx rings.  The (overwritten) hwna
80617885a7bSLuigi Rizzo  *   notify method will then complete the bridge tx
80717885a7bSLuigi Rizzo  *   (see netmap_bwrap_intr_notify).
808f9790aebSLuigi Rizzo  *
80917885a7bSLuigi Rizzo  *   The bridge wrapper may optionally connect the hwna 'host' rings
81017885a7bSLuigi Rizzo  *   to the bridge. This is done by using a second port in the
81117885a7bSLuigi Rizzo  *   bridge and connecting it to the 'host' netmap_vp_adapter
81217885a7bSLuigi Rizzo  *   contained in the netmap_bwrap_adapter. The brwap host adapter
81317885a7bSLuigi Rizzo  *   cross-links the hwna host rings in the same way as shown above.
81417885a7bSLuigi Rizzo  *
81517885a7bSLuigi Rizzo  * - packets coming from the bridge and directed to the host stack
81617885a7bSLuigi Rizzo  *   are handled by the bwrap host notify callback
81717885a7bSLuigi Rizzo  *   (see netmap_bwrap_host_notify)
81817885a7bSLuigi Rizzo  *
81917885a7bSLuigi Rizzo  * - packets coming from the host stack are still handled by the
82017885a7bSLuigi Rizzo  *   overwritten hwna notify callback (netmap_bwrap_intr_notify),
82117885a7bSLuigi Rizzo  *   but are diverted to the host adapter depending on the ring number.
822f9790aebSLuigi Rizzo  *
823f9790aebSLuigi Rizzo  */
824f9790aebSLuigi Rizzo struct netmap_bwrap_adapter {
825f9790aebSLuigi Rizzo 	struct netmap_vp_adapter up;
826f9790aebSLuigi Rizzo 	struct netmap_vp_adapter host;  /* for host rings */
827f9790aebSLuigi Rizzo 	struct netmap_adapter *hwna;	/* the underlying device */
828f9790aebSLuigi Rizzo 
8294bf50f18SLuigi Rizzo 	/* backup of the hwna memory allocator */
8304bf50f18SLuigi Rizzo 	struct netmap_mem_d *save_nmd;
83117885a7bSLuigi Rizzo 
83217885a7bSLuigi Rizzo 	/*
83317885a7bSLuigi Rizzo 	 * When we attach a physical interface to the bridge, we
834f18be576SLuigi Rizzo 	 * allow the controlling process to terminate, so we need
8354bf50f18SLuigi Rizzo 	 * a place to store the n_detmap_priv_d data structure.
83617885a7bSLuigi Rizzo 	 * This is only done when physical interfaces
83717885a7bSLuigi Rizzo 	 * are attached to a bridge.
838f18be576SLuigi Rizzo 	 */
839f18be576SLuigi Rizzo 	struct netmap_priv_d *na_kpriv;
84068b8534bSLuigi Rizzo };
8414bf50f18SLuigi Rizzo int netmap_bwrap_attach(const char *name, struct netmap_adapter *);
84268b8534bSLuigi Rizzo 
843f9790aebSLuigi Rizzo 
84417885a7bSLuigi Rizzo #endif /* WITH_VALE */
845ce3ee1e7SLuigi Rizzo 
846f0ea3689SLuigi Rizzo #ifdef WITH_PIPES
847f0ea3689SLuigi Rizzo 
848f0ea3689SLuigi Rizzo #define NM_MAXPIPES 	64	/* max number of pipes per adapter */
849f0ea3689SLuigi Rizzo 
850f0ea3689SLuigi Rizzo struct netmap_pipe_adapter {
851f0ea3689SLuigi Rizzo 	struct netmap_adapter up;
852f0ea3689SLuigi Rizzo 
853f0ea3689SLuigi Rizzo 	u_int id; 	/* pipe identifier */
854f0ea3689SLuigi Rizzo 	int role;	/* either NR_REG_PIPE_MASTER or NR_REG_PIPE_SLAVE */
855f0ea3689SLuigi Rizzo 
856f0ea3689SLuigi Rizzo 	struct netmap_adapter *parent; /* adapter that owns the memory */
857f0ea3689SLuigi Rizzo 	struct netmap_pipe_adapter *peer; /* the other end of the pipe */
858f0ea3689SLuigi Rizzo 	int peer_ref;		/* 1 iff we are holding a ref to the peer */
859f0ea3689SLuigi Rizzo 
860f0ea3689SLuigi Rizzo 	u_int parent_slot; /* index in the parent pipe array */
861f0ea3689SLuigi Rizzo };
862f0ea3689SLuigi Rizzo 
863f0ea3689SLuigi Rizzo #endif /* WITH_PIPES */
864f0ea3689SLuigi Rizzo 
86517885a7bSLuigi Rizzo 
86617885a7bSLuigi Rizzo /* return slots reserved to rx clients; used in drivers */
86717885a7bSLuigi Rizzo static inline uint32_t
86817885a7bSLuigi Rizzo nm_kr_rxspace(struct netmap_kring *k)
86917885a7bSLuigi Rizzo {
87017885a7bSLuigi Rizzo 	int space = k->nr_hwtail - k->nr_hwcur;
871ce3ee1e7SLuigi Rizzo 	if (space < 0)
872ce3ee1e7SLuigi Rizzo 		space += k->nkr_num_slots;
87317885a7bSLuigi Rizzo 	ND("preserving %d rx slots %d -> %d", space, k->nr_hwcur, k->nr_hwtail);
87417885a7bSLuigi Rizzo 
875ce3ee1e7SLuigi Rizzo 	return space;
876ce3ee1e7SLuigi Rizzo }
877ce3ee1e7SLuigi Rizzo 
878ce3ee1e7SLuigi Rizzo 
87917885a7bSLuigi Rizzo /* True if no space in the tx ring. only valid after txsync_prologue */
88017885a7bSLuigi Rizzo static inline int
88117885a7bSLuigi Rizzo nm_kr_txempty(struct netmap_kring *kring)
882ce3ee1e7SLuigi Rizzo {
88317885a7bSLuigi Rizzo 	return kring->rcur == kring->nr_hwtail;
884f9790aebSLuigi Rizzo }
885f9790aebSLuigi Rizzo 
886ce3ee1e7SLuigi Rizzo 
887ce3ee1e7SLuigi Rizzo /*
888f9790aebSLuigi Rizzo  * protect against multiple threads using the same ring.
889f9790aebSLuigi Rizzo  * also check that the ring has not been stopped.
890f9790aebSLuigi Rizzo  * We only care for 0 or !=0 as a return code.
89168b8534bSLuigi Rizzo  */
892f9790aebSLuigi Rizzo #define NM_KR_BUSY	1
893f9790aebSLuigi Rizzo #define NM_KR_STOPPED	2
89468b8534bSLuigi Rizzo 
89517885a7bSLuigi Rizzo 
896f9790aebSLuigi Rizzo static __inline void nm_kr_put(struct netmap_kring *kr)
897f9790aebSLuigi Rizzo {
898f9790aebSLuigi Rizzo 	NM_ATOMIC_CLEAR(&kr->nr_busy);
899f9790aebSLuigi Rizzo }
900f9790aebSLuigi Rizzo 
90117885a7bSLuigi Rizzo 
902f9790aebSLuigi Rizzo static __inline int nm_kr_tryget(struct netmap_kring *kr)
903f9790aebSLuigi Rizzo {
904f9790aebSLuigi Rizzo 	/* check a first time without taking the lock
905f9790aebSLuigi Rizzo 	 * to avoid starvation for nm_kr_get()
906f9790aebSLuigi Rizzo 	 */
907f9790aebSLuigi Rizzo 	if (unlikely(kr->nkr_stopped)) {
908f9790aebSLuigi Rizzo 		ND("ring %p stopped (%d)", kr, kr->nkr_stopped);
909f9790aebSLuigi Rizzo 		return NM_KR_STOPPED;
910f9790aebSLuigi Rizzo 	}
911f9790aebSLuigi Rizzo 	if (unlikely(NM_ATOMIC_TEST_AND_SET(&kr->nr_busy)))
912f9790aebSLuigi Rizzo 		return NM_KR_BUSY;
913f9790aebSLuigi Rizzo 	/* check a second time with lock held */
914f9790aebSLuigi Rizzo 	if (unlikely(kr->nkr_stopped)) {
915f9790aebSLuigi Rizzo 		ND("ring %p stopped (%d)", kr, kr->nkr_stopped);
916f9790aebSLuigi Rizzo 		nm_kr_put(kr);
917f9790aebSLuigi Rizzo 		return NM_KR_STOPPED;
918f9790aebSLuigi Rizzo 	}
919f9790aebSLuigi Rizzo 	return 0;
920f9790aebSLuigi Rizzo }
92168b8534bSLuigi Rizzo 
922847bf383SLuigi Rizzo static __inline void nm_kr_get(struct netmap_kring *kr)
923847bf383SLuigi Rizzo {
924847bf383SLuigi Rizzo 	while (NM_ATOMIC_TEST_AND_SET(&kr->nr_busy))
925847bf383SLuigi Rizzo 		tsleep(kr, 0, "NM_KR_GET", 4);
926847bf383SLuigi Rizzo }
927847bf383SLuigi Rizzo 
928847bf383SLuigi Rizzo 
92968b8534bSLuigi Rizzo /*
93017885a7bSLuigi Rizzo  * The following functions are used by individual drivers to
93168b8534bSLuigi Rizzo  * support netmap operation.
93268b8534bSLuigi Rizzo  *
93368b8534bSLuigi Rizzo  * netmap_attach() initializes a struct netmap_adapter, allocating the
93468b8534bSLuigi Rizzo  * 	struct netmap_ring's and the struct selinfo.
93568b8534bSLuigi Rizzo  *
93668b8534bSLuigi Rizzo  * netmap_detach() frees the memory allocated by netmap_attach().
93768b8534bSLuigi Rizzo  *
938ce3ee1e7SLuigi Rizzo  * netmap_transmit() replaces the if_transmit routine of the interface,
93968b8534bSLuigi Rizzo  *	and is used to intercept packets coming from the stack.
94068b8534bSLuigi Rizzo  *
94168b8534bSLuigi Rizzo  * netmap_load_map/netmap_reload_map are helper routines to set/reset
94268b8534bSLuigi Rizzo  *	the dmamap for a packet buffer
94368b8534bSLuigi Rizzo  *
9444bf50f18SLuigi Rizzo  * netmap_reset() is a helper routine to be called in the hw driver
9454bf50f18SLuigi Rizzo  *	when reinitializing a ring. It should not be called by
9464bf50f18SLuigi Rizzo  *	virtual ports (vale, pipes, monitor)
94768b8534bSLuigi Rizzo  */
948f9790aebSLuigi Rizzo int netmap_attach(struct netmap_adapter *);
94968b8534bSLuigi Rizzo void netmap_detach(struct ifnet *);
950ce3ee1e7SLuigi Rizzo int netmap_transmit(struct ifnet *, struct mbuf *);
95168b8534bSLuigi Rizzo struct netmap_slot *netmap_reset(struct netmap_adapter *na,
952ce3ee1e7SLuigi Rizzo 	enum txrx tx, u_int n, u_int new_cur);
95368b8534bSLuigi Rizzo int netmap_ring_reinit(struct netmap_kring *);
95468b8534bSLuigi Rizzo 
95517885a7bSLuigi Rizzo /* default functions to handle rx/tx interrupts */
95617885a7bSLuigi Rizzo int netmap_rx_irq(struct ifnet *, u_int, u_int *);
95717885a7bSLuigi Rizzo #define netmap_tx_irq(_n, _q) netmap_rx_irq(_n, _q, NULL)
95817885a7bSLuigi Rizzo void netmap_common_irq(struct ifnet *, u_int, u_int *work_done);
95917885a7bSLuigi Rizzo 
96017885a7bSLuigi Rizzo 
9614bf50f18SLuigi Rizzo #ifdef WITH_VALE
9624bf50f18SLuigi Rizzo /* functions used by external modules to interface with VALE */
9634bf50f18SLuigi Rizzo #define netmap_vp_to_ifp(_vp)	((_vp)->up.ifp)
9644bf50f18SLuigi Rizzo #define netmap_ifp_to_vp(_ifp)	(NA(_ifp)->na_vp)
9654bf50f18SLuigi Rizzo #define netmap_ifp_to_host_vp(_ifp) (NA(_ifp)->na_hostvp)
9664bf50f18SLuigi Rizzo #define netmap_bdg_idx(_vp)	((_vp)->bdg_port)
9674bf50f18SLuigi Rizzo const char *netmap_bdg_name(struct netmap_vp_adapter *);
9684bf50f18SLuigi Rizzo #else /* !WITH_VALE */
9694bf50f18SLuigi Rizzo #define netmap_vp_to_ifp(_vp)	NULL
9704bf50f18SLuigi Rizzo #define netmap_ifp_to_vp(_ifp)	NULL
9714bf50f18SLuigi Rizzo #define netmap_ifp_to_host_vp(_ifp) NULL
9724bf50f18SLuigi Rizzo #define netmap_bdg_idx(_vp)	-1
9734bf50f18SLuigi Rizzo #define netmap_bdg_name(_vp)	NULL
9744bf50f18SLuigi Rizzo #endif /* WITH_VALE */
9754bf50f18SLuigi Rizzo 
9764bf50f18SLuigi Rizzo static inline int
9774bf50f18SLuigi Rizzo nm_netmap_on(struct netmap_adapter *na)
9784bf50f18SLuigi Rizzo {
9794bf50f18SLuigi Rizzo 	return na && na->na_flags & NAF_NETMAP_ON;
9804bf50f18SLuigi Rizzo }
98117885a7bSLuigi Rizzo 
982847bf383SLuigi Rizzo static inline int
983847bf383SLuigi Rizzo nm_native_on(struct netmap_adapter *na)
984847bf383SLuigi Rizzo {
985847bf383SLuigi Rizzo 	return nm_netmap_on(na) && (na->na_flags & NAF_NATIVE);
986847bf383SLuigi Rizzo }
987847bf383SLuigi Rizzo 
98817885a7bSLuigi Rizzo /* set/clear native flags and if_transmit/netdev_ops */
989f9790aebSLuigi Rizzo static inline void
990f9790aebSLuigi Rizzo nm_set_native_flags(struct netmap_adapter *na)
991f9790aebSLuigi Rizzo {
992f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
993ce3ee1e7SLuigi Rizzo 
994847bf383SLuigi Rizzo 	na->na_flags |= NAF_NETMAP_ON;
995f9790aebSLuigi Rizzo #ifdef IFCAP_NETMAP /* or FreeBSD ? */
996f9790aebSLuigi Rizzo 	ifp->if_capenable |= IFCAP_NETMAP;
997f9790aebSLuigi Rizzo #endif
998f9790aebSLuigi Rizzo #ifdef __FreeBSD__
999f9790aebSLuigi Rizzo 	na->if_transmit = ifp->if_transmit;
1000f9790aebSLuigi Rizzo 	ifp->if_transmit = netmap_transmit;
1001f9790aebSLuigi Rizzo #else
1002f9790aebSLuigi Rizzo 	na->if_transmit = (void *)ifp->netdev_ops;
1003f9790aebSLuigi Rizzo 	ifp->netdev_ops = &((struct netmap_hw_adapter *)na)->nm_ndo;
10044bf50f18SLuigi Rizzo 	((struct netmap_hw_adapter *)na)->save_ethtool = ifp->ethtool_ops;
10054bf50f18SLuigi Rizzo 	ifp->ethtool_ops = &((struct netmap_hw_adapter*)na)->nm_eto;
1006f9790aebSLuigi Rizzo #endif
1007f9790aebSLuigi Rizzo }
1008f9790aebSLuigi Rizzo 
100917885a7bSLuigi Rizzo 
1010f9790aebSLuigi Rizzo static inline void
1011f9790aebSLuigi Rizzo nm_clear_native_flags(struct netmap_adapter *na)
1012f9790aebSLuigi Rizzo {
1013f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
1014f9790aebSLuigi Rizzo 
1015f9790aebSLuigi Rizzo #ifdef __FreeBSD__
1016f9790aebSLuigi Rizzo 	ifp->if_transmit = na->if_transmit;
1017f9790aebSLuigi Rizzo #else
1018f9790aebSLuigi Rizzo 	ifp->netdev_ops = (void *)na->if_transmit;
10194bf50f18SLuigi Rizzo 	ifp->ethtool_ops = ((struct netmap_hw_adapter*)na)->save_ethtool;
1020f9790aebSLuigi Rizzo #endif
1021847bf383SLuigi Rizzo 	na->na_flags &= ~NAF_NETMAP_ON;
1022f9790aebSLuigi Rizzo #ifdef IFCAP_NETMAP /* or FreeBSD ? */
1023f9790aebSLuigi Rizzo 	ifp->if_capenable &= ~IFCAP_NETMAP;
1024f9790aebSLuigi Rizzo #endif
1025f9790aebSLuigi Rizzo }
1026f9790aebSLuigi Rizzo 
1027f9790aebSLuigi Rizzo 
1028f9790aebSLuigi Rizzo /* check/fix address and len in tx rings */
1029f9790aebSLuigi Rizzo #if 1 /* debug version */
10304bf50f18SLuigi Rizzo #define	NM_CHECK_ADDR_LEN(_na, _a, _l)	do {				\
10314bf50f18SLuigi Rizzo 	if (_a == NETMAP_BUF_BASE(_na) || _l > NETMAP_BUF_SIZE(_na)) {	\
1032f9790aebSLuigi Rizzo 		RD(5, "bad addr/len ring %d slot %d idx %d len %d",	\
10334bf50f18SLuigi Rizzo 			kring->ring_id, nm_i, slot->buf_idx, len);	\
10344bf50f18SLuigi Rizzo 		if (_l > NETMAP_BUF_SIZE(_na))				\
10354bf50f18SLuigi Rizzo 			_l = NETMAP_BUF_SIZE(_na);			\
1036f9790aebSLuigi Rizzo 	} } while (0)
1037f9790aebSLuigi Rizzo #else /* no debug version */
10384bf50f18SLuigi Rizzo #define	NM_CHECK_ADDR_LEN(_na, _a, _l)	do {				\
10394bf50f18SLuigi Rizzo 		if (_l > NETMAP_BUF_SIZE(_na))				\
10404bf50f18SLuigi Rizzo 			_l = NETMAP_BUF_SIZE(_na);			\
1041f9790aebSLuigi Rizzo 	} while (0)
1042f9790aebSLuigi Rizzo #endif
1043f9790aebSLuigi Rizzo 
1044f9790aebSLuigi Rizzo 
1045f9790aebSLuigi Rizzo /*---------------------------------------------------------------*/
1046f9790aebSLuigi Rizzo /*
10474bf50f18SLuigi Rizzo  * Support routines used by netmap subsystems
10484bf50f18SLuigi Rizzo  * (native drivers, VALE, generic, pipes, monitors, ...)
10494bf50f18SLuigi Rizzo  */
10504bf50f18SLuigi Rizzo 
10514bf50f18SLuigi Rizzo 
10524bf50f18SLuigi Rizzo /* common routine for all functions that create a netmap adapter. It performs
10534bf50f18SLuigi Rizzo  * two main tasks:
10544bf50f18SLuigi Rizzo  * - if the na points to an ifp, mark the ifp as netmap capable
10554bf50f18SLuigi Rizzo  *   using na as its native adapter;
10564bf50f18SLuigi Rizzo  * - provide defaults for the setup callbacks and the memory allocator
10574bf50f18SLuigi Rizzo  */
10584bf50f18SLuigi Rizzo int netmap_attach_common(struct netmap_adapter *);
10594bf50f18SLuigi Rizzo /* common actions to be performed on netmap adapter destruction */
10604bf50f18SLuigi Rizzo void netmap_detach_common(struct netmap_adapter *);
10614bf50f18SLuigi Rizzo /* fill priv->np_[tr]xq{first,last} using the ringid and flags information
10624bf50f18SLuigi Rizzo  * coming from a struct nmreq
10634bf50f18SLuigi Rizzo  */
10644bf50f18SLuigi Rizzo int netmap_interp_ringid(struct netmap_priv_d *priv, uint16_t ringid, uint32_t flags);
10654bf50f18SLuigi Rizzo /* update the ring parameters (number and size of tx and rx rings).
10664bf50f18SLuigi Rizzo  * It calls the nm_config callback, if available.
1067f9790aebSLuigi Rizzo  */
1068f9790aebSLuigi Rizzo int netmap_update_config(struct netmap_adapter *na);
10694bf50f18SLuigi Rizzo /* create and initialize the common fields of the krings array.
10704bf50f18SLuigi Rizzo  * using the information that must be already available in the na.
10714bf50f18SLuigi Rizzo  * tailroom can be used to request the allocation of additional
10724bf50f18SLuigi Rizzo  * tailroom bytes after the krings array. This is used by
10734bf50f18SLuigi Rizzo  * netmap_vp_adapter's (i.e., VALE ports) to make room for
10744bf50f18SLuigi Rizzo  * leasing-related data structures
10754bf50f18SLuigi Rizzo  */
1076f0ea3689SLuigi Rizzo int netmap_krings_create(struct netmap_adapter *na, u_int tailroom);
10774bf50f18SLuigi Rizzo /* deletes the kring array of the adapter. The array must have
10784bf50f18SLuigi Rizzo  * been created using netmap_krings_create
10794bf50f18SLuigi Rizzo  */
1080f9790aebSLuigi Rizzo void netmap_krings_delete(struct netmap_adapter *na);
108117885a7bSLuigi Rizzo 
10824bf50f18SLuigi Rizzo /* set the stopped/enabled status of ring
10834bf50f18SLuigi Rizzo  * When stopping, they also wait for all current activity on the ring to
10844bf50f18SLuigi Rizzo  * terminate. The status change is then notified using the na nm_notify
10854bf50f18SLuigi Rizzo  * callback.
10864bf50f18SLuigi Rizzo  */
1087847bf383SLuigi Rizzo void netmap_set_ring(struct netmap_adapter *, u_int ring_id, enum txrx, int stopped);
10884bf50f18SLuigi Rizzo /* set the stopped/enabled status of all rings of the adapter. */
10894bf50f18SLuigi Rizzo void netmap_set_all_rings(struct netmap_adapter *, int stopped);
10904bf50f18SLuigi Rizzo /* convenience wrappers for netmap_set_all_rings, used in drivers */
10914bf50f18SLuigi Rizzo void netmap_disable_all_rings(struct ifnet *);
10924bf50f18SLuigi Rizzo void netmap_enable_all_rings(struct ifnet *);
10934bf50f18SLuigi Rizzo 
10948fd44c93SLuigi Rizzo int netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na,
1095847bf383SLuigi Rizzo 	uint16_t ringid, uint32_t flags);
1096f9790aebSLuigi Rizzo 
1097f9790aebSLuigi Rizzo 
1098f9790aebSLuigi Rizzo u_int nm_bound_var(u_int *v, u_int dflt, u_int lo, u_int hi, const char *msg);
1099f9790aebSLuigi Rizzo int netmap_get_na(struct nmreq *nmr, struct netmap_adapter **na, int create);
1100f9790aebSLuigi Rizzo int netmap_get_hw_na(struct ifnet *ifp, struct netmap_adapter **na);
1101f9790aebSLuigi Rizzo 
110217885a7bSLuigi Rizzo 
1103f9790aebSLuigi Rizzo #ifdef WITH_VALE
1104f18be576SLuigi Rizzo /*
110517885a7bSLuigi Rizzo  * The following bridge-related functions are used by other
110617885a7bSLuigi Rizzo  * kernel modules.
110717885a7bSLuigi Rizzo  *
110817885a7bSLuigi Rizzo  * VALE only supports unicast or broadcast. The lookup
1109f18be576SLuigi Rizzo  * function can return 0 .. NM_BDG_MAXPORTS-1 for regular ports,
1110f18be576SLuigi Rizzo  * NM_BDG_MAXPORTS for broadcast, NM_BDG_MAXPORTS+1 for unknown.
1111f18be576SLuigi Rizzo  * XXX in practice "unknown" might be handled same as broadcast.
1112f18be576SLuigi Rizzo  */
11134bf50f18SLuigi Rizzo typedef u_int (*bdg_lookup_fn_t)(struct nm_bdg_fwd *ft, uint8_t *ring_nr,
1114847bf383SLuigi Rizzo 		struct netmap_vp_adapter *);
11154bf50f18SLuigi Rizzo typedef int (*bdg_config_fn_t)(struct nm_ifreq *);
11164bf50f18SLuigi Rizzo typedef void (*bdg_dtor_fn_t)(const struct netmap_vp_adapter *);
11174bf50f18SLuigi Rizzo struct netmap_bdg_ops {
11184bf50f18SLuigi Rizzo 	bdg_lookup_fn_t lookup;
11194bf50f18SLuigi Rizzo 	bdg_config_fn_t config;
11204bf50f18SLuigi Rizzo 	bdg_dtor_fn_t	dtor;
11214bf50f18SLuigi Rizzo };
11224bf50f18SLuigi Rizzo 
11234bf50f18SLuigi Rizzo u_int netmap_bdg_learning(struct nm_bdg_fwd *ft, uint8_t *dst_ring,
1124847bf383SLuigi Rizzo 		struct netmap_vp_adapter *);
1125f9790aebSLuigi Rizzo 
1126f9790aebSLuigi Rizzo #define	NM_BDG_MAXPORTS		254	/* up to 254 */
1127f18be576SLuigi Rizzo #define	NM_BDG_BROADCAST	NM_BDG_MAXPORTS
1128f18be576SLuigi Rizzo #define	NM_BDG_NOPORT		(NM_BDG_MAXPORTS+1)
1129f18be576SLuigi Rizzo 
1130f9790aebSLuigi Rizzo #define	NM_NAME			"vale"	/* prefix for bridge port name */
1131f9790aebSLuigi Rizzo 
1132f9790aebSLuigi Rizzo /* these are redefined in case of no VALE support */
1133f9790aebSLuigi Rizzo int netmap_get_bdg_na(struct nmreq *nmr, struct netmap_adapter **na, int create);
1134847bf383SLuigi Rizzo struct nm_bridge *netmap_init_bridges2(u_int);
1135847bf383SLuigi Rizzo void netmap_uninit_bridges2(struct nm_bridge *, u_int);
1136847bf383SLuigi Rizzo int netmap_init_bridges(void);
1137847bf383SLuigi Rizzo void netmap_uninit_bridges(void);
11384bf50f18SLuigi Rizzo int netmap_bdg_ctl(struct nmreq *nmr, struct netmap_bdg_ops *bdg_ops);
11394bf50f18SLuigi Rizzo int netmap_bdg_config(struct nmreq *nmr);
1140f9790aebSLuigi Rizzo 
1141f9790aebSLuigi Rizzo #else /* !WITH_VALE */
1142f9790aebSLuigi Rizzo #define	netmap_get_bdg_na(_1, _2, _3)	0
1143847bf383SLuigi Rizzo #define netmap_init_bridges(_1) 0
1144847bf383SLuigi Rizzo #define netmap_uninit_bridges()
1145f9790aebSLuigi Rizzo #define	netmap_bdg_ctl(_1, _2)	EINVAL
1146f9790aebSLuigi Rizzo #endif /* !WITH_VALE */
1147f9790aebSLuigi Rizzo 
1148f0ea3689SLuigi Rizzo #ifdef WITH_PIPES
1149f0ea3689SLuigi Rizzo /* max number of pipes per device */
1150f0ea3689SLuigi Rizzo #define NM_MAXPIPES	64	/* XXX how many? */
1151f0ea3689SLuigi Rizzo void netmap_pipe_dealloc(struct netmap_adapter *);
1152f0ea3689SLuigi Rizzo int netmap_get_pipe_na(struct nmreq *nmr, struct netmap_adapter **na, int create);
1153f0ea3689SLuigi Rizzo #else /* !WITH_PIPES */
1154f0ea3689SLuigi Rizzo #define NM_MAXPIPES	0
1155847bf383SLuigi Rizzo #define netmap_pipe_alloc(_1, _2) 	0
1156f0ea3689SLuigi Rizzo #define netmap_pipe_dealloc(_1)
1157847bf383SLuigi Rizzo #define netmap_get_pipe_na(nmr, _2, _3)	\
1158847bf383SLuigi Rizzo 	({ int role__ = (nmr)->nr_flags & NR_REG_MASK; \
1159847bf383SLuigi Rizzo 	   (role__ == NR_REG_PIPE_MASTER || 	       \
1160847bf383SLuigi Rizzo 	    role__ == NR_REG_PIPE_SLAVE) ? EOPNOTSUPP : 0; })
1161f0ea3689SLuigi Rizzo #endif
1162f0ea3689SLuigi Rizzo 
11634bf50f18SLuigi Rizzo #ifdef WITH_MONITOR
11644bf50f18SLuigi Rizzo int netmap_get_monitor_na(struct nmreq *nmr, struct netmap_adapter **na, int create);
1165847bf383SLuigi Rizzo void netmap_monitor_stop(struct netmap_adapter *na);
11664bf50f18SLuigi Rizzo #else
1167847bf383SLuigi Rizzo #define netmap_get_monitor_na(nmr, _2, _3) \
1168847bf383SLuigi Rizzo 	((nmr)->nr_flags & (NR_MONITOR_TX | NR_MONITOR_RX) ? EOPNOTSUPP : 0)
1169847bf383SLuigi Rizzo #endif
1170847bf383SLuigi Rizzo 
1171847bf383SLuigi Rizzo #ifdef CONFIG_NET_NS
1172847bf383SLuigi Rizzo struct net *netmap_bns_get(void);
1173847bf383SLuigi Rizzo void netmap_bns_put(struct net *);
1174847bf383SLuigi Rizzo void netmap_bns_getbridges(struct nm_bridge **, u_int *);
1175847bf383SLuigi Rizzo #else
1176847bf383SLuigi Rizzo #define netmap_bns_get()
1177847bf383SLuigi Rizzo #define netmap_bns_put(_1)
1178847bf383SLuigi Rizzo #define netmap_bns_getbridges(b, n) \
1179847bf383SLuigi Rizzo 	do { *b = nm_bridges; *n = NM_BRIDGES; } while (0)
11804bf50f18SLuigi Rizzo #endif
11814bf50f18SLuigi Rizzo 
1182f9790aebSLuigi Rizzo /* Various prototypes */
1183f9790aebSLuigi Rizzo int netmap_poll(struct cdev *dev, int events, struct thread *td);
1184f9790aebSLuigi Rizzo int netmap_init(void);
1185f9790aebSLuigi Rizzo void netmap_fini(void);
1186f9790aebSLuigi Rizzo int netmap_get_memory(struct netmap_priv_d* p);
1187f9790aebSLuigi Rizzo void netmap_dtor(void *data);
1188f9790aebSLuigi Rizzo int netmap_dtor_locked(struct netmap_priv_d *priv);
1189f9790aebSLuigi Rizzo 
1190f9790aebSLuigi Rizzo int netmap_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td);
1191f9790aebSLuigi Rizzo 
1192f9790aebSLuigi Rizzo /* netmap_adapter creation/destruction */
119317885a7bSLuigi Rizzo 
119417885a7bSLuigi Rizzo // #define NM_DEBUG_PUTGET 1
1195f9790aebSLuigi Rizzo 
1196f9790aebSLuigi Rizzo #ifdef NM_DEBUG_PUTGET
1197f9790aebSLuigi Rizzo 
1198f9790aebSLuigi Rizzo #define NM_DBG(f) __##f
1199f9790aebSLuigi Rizzo 
1200f9790aebSLuigi Rizzo void __netmap_adapter_get(struct netmap_adapter *na);
1201f9790aebSLuigi Rizzo 
1202f9790aebSLuigi Rizzo #define netmap_adapter_get(na) 				\
1203f9790aebSLuigi Rizzo 	do {						\
1204f9790aebSLuigi Rizzo 		struct netmap_adapter *__na = na;	\
12054bf50f18SLuigi Rizzo 		D("getting %p:%s (%d)", __na, (__na)->name, (__na)->na_refcount);	\
1206f9790aebSLuigi Rizzo 		__netmap_adapter_get(__na);		\
1207f9790aebSLuigi Rizzo 	} while (0)
1208f9790aebSLuigi Rizzo 
1209f9790aebSLuigi Rizzo int __netmap_adapter_put(struct netmap_adapter *na);
1210f9790aebSLuigi Rizzo 
1211f9790aebSLuigi Rizzo #define netmap_adapter_put(na)				\
1212fb25194fSLuigi Rizzo 	({						\
1213f9790aebSLuigi Rizzo 		struct netmap_adapter *__na = na;	\
12144bf50f18SLuigi Rizzo 		D("putting %p:%s (%d)", __na, (__na)->name, (__na)->na_refcount);	\
1215f9790aebSLuigi Rizzo 		__netmap_adapter_put(__na);		\
1216fb25194fSLuigi Rizzo 	})
1217f9790aebSLuigi Rizzo 
1218f9790aebSLuigi Rizzo #else /* !NM_DEBUG_PUTGET */
1219f9790aebSLuigi Rizzo 
1220f9790aebSLuigi Rizzo #define NM_DBG(f) f
1221f9790aebSLuigi Rizzo void netmap_adapter_get(struct netmap_adapter *na);
1222f9790aebSLuigi Rizzo int netmap_adapter_put(struct netmap_adapter *na);
1223f9790aebSLuigi Rizzo 
1224f9790aebSLuigi Rizzo #endif /* !NM_DEBUG_PUTGET */
1225f9790aebSLuigi Rizzo 
1226f9790aebSLuigi Rizzo 
122717885a7bSLuigi Rizzo /*
122817885a7bSLuigi Rizzo  * module variables
122917885a7bSLuigi Rizzo  */
1230847bf383SLuigi Rizzo #define NETMAP_BUF_BASE(na)	((na)->na_lut.lut[0].vaddr)
1231847bf383SLuigi Rizzo #define NETMAP_BUF_SIZE(na)	((na)->na_lut.objsize)
123217885a7bSLuigi Rizzo extern int netmap_mitigate;	// XXX not really used
12335819da83SLuigi Rizzo extern int netmap_no_pendintr;
123468b8534bSLuigi Rizzo extern int netmap_verbose;	// XXX debugging
123568b8534bSLuigi Rizzo enum {                                  /* verbose flags */
123668b8534bSLuigi Rizzo 	NM_VERB_ON = 1,                 /* generic verbose */
123768b8534bSLuigi Rizzo 	NM_VERB_HOST = 0x2,             /* verbose host stack */
123868b8534bSLuigi Rizzo 	NM_VERB_RXSYNC = 0x10,          /* verbose on rxsync/txsync */
123968b8534bSLuigi Rizzo 	NM_VERB_TXSYNC = 0x20,
124068b8534bSLuigi Rizzo 	NM_VERB_RXINTR = 0x100,         /* verbose on rx/tx intr (driver) */
124168b8534bSLuigi Rizzo 	NM_VERB_TXINTR = 0x200,
124268b8534bSLuigi Rizzo 	NM_VERB_NIC_RXSYNC = 0x1000,    /* verbose on rx/tx intr (driver) */
124368b8534bSLuigi Rizzo 	NM_VERB_NIC_TXSYNC = 0x2000,
124468b8534bSLuigi Rizzo };
124568b8534bSLuigi Rizzo 
1246f9790aebSLuigi Rizzo extern int netmap_txsync_retry;
1247f9790aebSLuigi Rizzo extern int netmap_generic_mit;
1248f9790aebSLuigi Rizzo extern int netmap_generic_ringsize;
1249f0ea3689SLuigi Rizzo extern int netmap_generic_rings;
1250f9790aebSLuigi Rizzo 
125168b8534bSLuigi Rizzo /*
1252d0c7b075SLuigi Rizzo  * NA returns a pointer to the struct netmap adapter from the ifp,
1253d0c7b075SLuigi Rizzo  * WNA is used to write it.
125468b8534bSLuigi Rizzo  */
1255d0c7b075SLuigi Rizzo #define	NA(_ifp)	((struct netmap_adapter *)WNA(_ifp))
125668b8534bSLuigi Rizzo 
12578241616dSLuigi Rizzo /*
12588241616dSLuigi Rizzo  * Macros to determine if an interface is netmap capable or netmap enabled.
12598241616dSLuigi Rizzo  * See the magic field in struct netmap_adapter.
12608241616dSLuigi Rizzo  */
12618241616dSLuigi Rizzo #ifdef __FreeBSD__
12628241616dSLuigi Rizzo /*
12638241616dSLuigi Rizzo  * on FreeBSD just use if_capabilities and if_capenable.
12648241616dSLuigi Rizzo  */
12658241616dSLuigi Rizzo #define NETMAP_CAPABLE(ifp)	(NA(ifp) &&		\
12668241616dSLuigi Rizzo 	(ifp)->if_capabilities & IFCAP_NETMAP )
12678241616dSLuigi Rizzo 
12688241616dSLuigi Rizzo #define	NETMAP_SET_CAPABLE(ifp)				\
12698241616dSLuigi Rizzo 	(ifp)->if_capabilities |= IFCAP_NETMAP
12708241616dSLuigi Rizzo 
12718241616dSLuigi Rizzo #else	/* linux */
12728241616dSLuigi Rizzo 
12738241616dSLuigi Rizzo /*
12748241616dSLuigi Rizzo  * on linux:
12758241616dSLuigi Rizzo  * we check if NA(ifp) is set and its first element has a related
12768241616dSLuigi Rizzo  * magic value. The capenable is within the struct netmap_adapter.
12778241616dSLuigi Rizzo  */
12788241616dSLuigi Rizzo #define	NETMAP_MAGIC	0x52697a7a
12798241616dSLuigi Rizzo 
12808241616dSLuigi Rizzo #define NETMAP_CAPABLE(ifp)	(NA(ifp) &&		\
12818241616dSLuigi Rizzo 	((uint32_t)(uintptr_t)NA(ifp) ^ NA(ifp)->magic) == NETMAP_MAGIC )
12828241616dSLuigi Rizzo 
12838241616dSLuigi Rizzo #define	NETMAP_SET_CAPABLE(ifp)				\
12848241616dSLuigi Rizzo 	NA(ifp)->magic = ((uint32_t)(uintptr_t)NA(ifp)) ^ NETMAP_MAGIC
12858241616dSLuigi Rizzo 
12868241616dSLuigi Rizzo #endif	/* linux */
128768b8534bSLuigi Rizzo 
1288f196ce38SLuigi Rizzo #ifdef __FreeBSD__
1289f9790aebSLuigi Rizzo 
12904bf50f18SLuigi Rizzo /* Assigns the device IOMMU domain to an allocator.
12914bf50f18SLuigi Rizzo  * Returns -ENOMEM in case the domain is different */
12924bf50f18SLuigi Rizzo #define nm_iommu_group_id(dev) (0)
12934bf50f18SLuigi Rizzo 
129417885a7bSLuigi Rizzo /* Callback invoked by the dma machinery after a successful dmamap_load */
12956dba29a2SLuigi Rizzo static void netmap_dmamap_cb(__unused void *arg,
12966dba29a2SLuigi Rizzo     __unused bus_dma_segment_t * segs, __unused int nseg, __unused int error)
12976dba29a2SLuigi Rizzo {
12986dba29a2SLuigi Rizzo }
12996dba29a2SLuigi Rizzo 
13006dba29a2SLuigi Rizzo /* bus_dmamap_load wrapper: call aforementioned function if map != NULL.
13016dba29a2SLuigi Rizzo  * XXX can we do it without a callback ?
13026dba29a2SLuigi Rizzo  */
13036dba29a2SLuigi Rizzo static inline void
13044bf50f18SLuigi Rizzo netmap_load_map(struct netmap_adapter *na,
13054bf50f18SLuigi Rizzo 	bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
13066dba29a2SLuigi Rizzo {
13076dba29a2SLuigi Rizzo 	if (map)
13084bf50f18SLuigi Rizzo 		bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE(na),
13096dba29a2SLuigi Rizzo 		    netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
13106dba29a2SLuigi Rizzo }
13116dba29a2SLuigi Rizzo 
13124bf50f18SLuigi Rizzo static inline void
13134bf50f18SLuigi Rizzo netmap_unload_map(struct netmap_adapter *na,
13144bf50f18SLuigi Rizzo         bus_dma_tag_t tag, bus_dmamap_t map)
13154bf50f18SLuigi Rizzo {
13164bf50f18SLuigi Rizzo 	if (map)
13174bf50f18SLuigi Rizzo 		bus_dmamap_unload(tag, map);
13184bf50f18SLuigi Rizzo }
13194bf50f18SLuigi Rizzo 
13206dba29a2SLuigi Rizzo /* update the map when a buffer changes. */
13216dba29a2SLuigi Rizzo static inline void
13224bf50f18SLuigi Rizzo netmap_reload_map(struct netmap_adapter *na,
13234bf50f18SLuigi Rizzo 	bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
13246dba29a2SLuigi Rizzo {
13256dba29a2SLuigi Rizzo 	if (map) {
13266dba29a2SLuigi Rizzo 		bus_dmamap_unload(tag, map);
13274bf50f18SLuigi Rizzo 		bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE(na),
13286dba29a2SLuigi Rizzo 		    netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
13296dba29a2SLuigi Rizzo 	}
13306dba29a2SLuigi Rizzo }
1331f9790aebSLuigi Rizzo 
1332f196ce38SLuigi Rizzo #else /* linux */
1333f196ce38SLuigi Rizzo 
13344bf50f18SLuigi Rizzo int nm_iommu_group_id(bus_dma_tag_t dev);
13354bf50f18SLuigi Rizzo #include <linux/dma-mapping.h>
13364bf50f18SLuigi Rizzo 
13374bf50f18SLuigi Rizzo static inline void
13384bf50f18SLuigi Rizzo netmap_load_map(struct netmap_adapter *na,
13394bf50f18SLuigi Rizzo 	bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
13404bf50f18SLuigi Rizzo {
1341847bf383SLuigi Rizzo 	if (0 && map) {
1342847bf383SLuigi Rizzo 		*map = dma_map_single(na->pdev, buf, na->na_lut.objsize,
13434bf50f18SLuigi Rizzo 				DMA_BIDIRECTIONAL);
13444bf50f18SLuigi Rizzo 	}
13454bf50f18SLuigi Rizzo }
13464bf50f18SLuigi Rizzo 
13474bf50f18SLuigi Rizzo static inline void
13484bf50f18SLuigi Rizzo netmap_unload_map(struct netmap_adapter *na,
13494bf50f18SLuigi Rizzo 	bus_dma_tag_t tag, bus_dmamap_t map)
13504bf50f18SLuigi Rizzo {
1351847bf383SLuigi Rizzo 	u_int sz = na->na_lut.objsize;
13524bf50f18SLuigi Rizzo 
13534bf50f18SLuigi Rizzo 	if (*map) {
13544bf50f18SLuigi Rizzo 		dma_unmap_single(na->pdev, *map, sz,
13554bf50f18SLuigi Rizzo 				DMA_BIDIRECTIONAL);
13564bf50f18SLuigi Rizzo 	}
13574bf50f18SLuigi Rizzo }
13584bf50f18SLuigi Rizzo 
13594bf50f18SLuigi Rizzo static inline void
13604bf50f18SLuigi Rizzo netmap_reload_map(struct netmap_adapter *na,
13614bf50f18SLuigi Rizzo 	bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
13624bf50f18SLuigi Rizzo {
1363847bf383SLuigi Rizzo 	u_int sz = na->na_lut.objsize;
13644bf50f18SLuigi Rizzo 
13654bf50f18SLuigi Rizzo 	if (*map) {
13664bf50f18SLuigi Rizzo 		dma_unmap_single(na->pdev, *map, sz,
13674bf50f18SLuigi Rizzo 				DMA_BIDIRECTIONAL);
13684bf50f18SLuigi Rizzo 	}
13694bf50f18SLuigi Rizzo 
13704bf50f18SLuigi Rizzo 	*map = dma_map_single(na->pdev, buf, sz,
13714bf50f18SLuigi Rizzo 				DMA_BIDIRECTIONAL);
13724bf50f18SLuigi Rizzo }
13734bf50f18SLuigi Rizzo 
1374f196ce38SLuigi Rizzo /*
1375f196ce38SLuigi Rizzo  * XXX How do we redefine these functions:
1376f196ce38SLuigi Rizzo  *
1377f196ce38SLuigi Rizzo  * on linux we need
1378f196ce38SLuigi Rizzo  *	dma_map_single(&pdev->dev, virt_addr, len, direction)
1379f196ce38SLuigi Rizzo  *	dma_unmap_single(&adapter->pdev->dev, phys_addr, len, direction
1380f196ce38SLuigi Rizzo  * The len can be implicit (on netmap it is NETMAP_BUF_SIZE)
1381f196ce38SLuigi Rizzo  * unfortunately the direction is not, so we need to change
1382f196ce38SLuigi Rizzo  * something to have a cross API
1383f196ce38SLuigi Rizzo  */
13844bf50f18SLuigi Rizzo 
1385f196ce38SLuigi Rizzo #if 0
1386f196ce38SLuigi Rizzo 	struct e1000_buffer *buffer_info =  &tx_ring->buffer_info[l];
1387f196ce38SLuigi Rizzo 	/* set time_stamp *before* dma to help avoid a possible race */
1388f196ce38SLuigi Rizzo 	buffer_info->time_stamp = jiffies;
1389f196ce38SLuigi Rizzo 	buffer_info->mapped_as_page = false;
1390f196ce38SLuigi Rizzo 	buffer_info->length = len;
1391f196ce38SLuigi Rizzo 	//buffer_info->next_to_watch = l;
1392f196ce38SLuigi Rizzo 	/* reload dma map */
1393f196ce38SLuigi Rizzo 	dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
1394f196ce38SLuigi Rizzo 			NETMAP_BUF_SIZE, DMA_TO_DEVICE);
1395f196ce38SLuigi Rizzo 	buffer_info->dma = dma_map_single(&adapter->pdev->dev,
1396f196ce38SLuigi Rizzo 			addr, NETMAP_BUF_SIZE, DMA_TO_DEVICE);
1397f196ce38SLuigi Rizzo 
1398f196ce38SLuigi Rizzo 	if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
1399f196ce38SLuigi Rizzo 		D("dma mapping error");
1400f196ce38SLuigi Rizzo 		/* goto dma_error; See e1000_put_txbuf() */
1401f196ce38SLuigi Rizzo 		/* XXX reset */
1402f196ce38SLuigi Rizzo 	}
1403f196ce38SLuigi Rizzo 	tx_desc->buffer_addr = htole64(buffer_info->dma); //XXX
1404f196ce38SLuigi Rizzo 
1405f196ce38SLuigi Rizzo #endif
1406f196ce38SLuigi Rizzo 
1407f196ce38SLuigi Rizzo /*
1408f196ce38SLuigi Rizzo  * The bus_dmamap_sync() can be one of wmb() or rmb() depending on direction.
1409f196ce38SLuigi Rizzo  */
1410f196ce38SLuigi Rizzo #define bus_dmamap_sync(_a, _b, _c)
1411f196ce38SLuigi Rizzo 
1412f196ce38SLuigi Rizzo #endif /* linux */
14136dba29a2SLuigi Rizzo 
1414ce3ee1e7SLuigi Rizzo 
14155644ccecSLuigi Rizzo /*
14165644ccecSLuigi Rizzo  * functions to map NIC to KRING indexes (n2k) and vice versa (k2n)
14175644ccecSLuigi Rizzo  */
14185644ccecSLuigi Rizzo static inline int
141964ae02c3SLuigi Rizzo netmap_idx_n2k(struct netmap_kring *kr, int idx)
14205644ccecSLuigi Rizzo {
142164ae02c3SLuigi Rizzo 	int n = kr->nkr_num_slots;
142264ae02c3SLuigi Rizzo 	idx += kr->nkr_hwofs;
142364ae02c3SLuigi Rizzo 	if (idx < 0)
142464ae02c3SLuigi Rizzo 		return idx + n;
142564ae02c3SLuigi Rizzo 	else if (idx < n)
142664ae02c3SLuigi Rizzo 		return idx;
14275644ccecSLuigi Rizzo 	else
142864ae02c3SLuigi Rizzo 		return idx - n;
14295644ccecSLuigi Rizzo }
14305644ccecSLuigi Rizzo 
14315644ccecSLuigi Rizzo 
14325644ccecSLuigi Rizzo static inline int
143364ae02c3SLuigi Rizzo netmap_idx_k2n(struct netmap_kring *kr, int idx)
14345644ccecSLuigi Rizzo {
143564ae02c3SLuigi Rizzo 	int n = kr->nkr_num_slots;
143664ae02c3SLuigi Rizzo 	idx -= kr->nkr_hwofs;
143764ae02c3SLuigi Rizzo 	if (idx < 0)
143864ae02c3SLuigi Rizzo 		return idx + n;
143964ae02c3SLuigi Rizzo 	else if (idx < n)
144064ae02c3SLuigi Rizzo 		return idx;
14415644ccecSLuigi Rizzo 	else
144264ae02c3SLuigi Rizzo 		return idx - n;
14435644ccecSLuigi Rizzo }
14445644ccecSLuigi Rizzo 
14455644ccecSLuigi Rizzo 
1446d76bf4ffSLuigi Rizzo /* Entries of the look-up table. */
1447d76bf4ffSLuigi Rizzo struct lut_entry {
1448d76bf4ffSLuigi Rizzo 	void *vaddr;		/* virtual address. */
1449849bec0eSLuigi Rizzo 	vm_paddr_t paddr;	/* physical address. */
1450d76bf4ffSLuigi Rizzo };
1451d76bf4ffSLuigi Rizzo 
1452d76bf4ffSLuigi Rizzo struct netmap_obj_pool;
1453d76bf4ffSLuigi Rizzo 
145468b8534bSLuigi Rizzo /*
14556e10c8b8SLuigi Rizzo  * NMB return the virtual address of a buffer (buffer 0 on bad index)
14566e10c8b8SLuigi Rizzo  * PNMB also fills the physical address
145768b8534bSLuigi Rizzo  */
14586e10c8b8SLuigi Rizzo static inline void *
14594bf50f18SLuigi Rizzo NMB(struct netmap_adapter *na, struct netmap_slot *slot)
1460f9790aebSLuigi Rizzo {
1461847bf383SLuigi Rizzo 	struct lut_entry *lut = na->na_lut.lut;
1462f9790aebSLuigi Rizzo 	uint32_t i = slot->buf_idx;
1463847bf383SLuigi Rizzo 	return (unlikely(i >= na->na_lut.objtotal)) ?
1464f9790aebSLuigi Rizzo 		lut[0].vaddr : lut[i].vaddr;
1465f9790aebSLuigi Rizzo }
1466f9790aebSLuigi Rizzo 
14674bf50f18SLuigi Rizzo static inline void *
14684bf50f18SLuigi Rizzo PNMB(struct netmap_adapter *na, struct netmap_slot *slot, uint64_t *pp)
14694bf50f18SLuigi Rizzo {
14704bf50f18SLuigi Rizzo 	uint32_t i = slot->buf_idx;
1471847bf383SLuigi Rizzo 	struct lut_entry *lut = na->na_lut.lut;
1472847bf383SLuigi Rizzo 	void *ret = (i >= na->na_lut.objtotal) ? lut[0].vaddr : lut[i].vaddr;
14734bf50f18SLuigi Rizzo 
1474847bf383SLuigi Rizzo 	*pp = (i >= na->na_lut.objtotal) ? lut[0].paddr : lut[i].paddr;
14754bf50f18SLuigi Rizzo 	return ret;
14764bf50f18SLuigi Rizzo }
14774bf50f18SLuigi Rizzo 
1478f9790aebSLuigi Rizzo 
147917885a7bSLuigi Rizzo /*
14808fd44c93SLuigi Rizzo  * Structure associated to each netmap file descriptor.
14818fd44c93SLuigi Rizzo  * It is created on open and left unbound (np_nifp == NULL).
14828fd44c93SLuigi Rizzo  * A successful NIOCREGIF will set np_nifp and the first few fields;
14838fd44c93SLuigi Rizzo  * this is protected by a global lock (NMG_LOCK) due to low contention.
1484f9790aebSLuigi Rizzo  *
14858fd44c93SLuigi Rizzo  * np_refs counts the number of references to the structure: one for the fd,
14868fd44c93SLuigi Rizzo  * plus (on FreeBSD) one for each active mmap which we track ourselves
1487*85fe4e7cSLuigi Rizzo  * (linux automatically tracks them, but FreeBSD does not).
14888fd44c93SLuigi Rizzo  * np_refs is protected by NMG_LOCK.
148917885a7bSLuigi Rizzo  *
14908fd44c93SLuigi Rizzo  * Read access to the structure is lock free, because ni_nifp once set
14918fd44c93SLuigi Rizzo  * can only go to 0 when nobody is using the entry anymore. Readers
14928fd44c93SLuigi Rizzo  * must check that np_nifp != NULL before using the other fields.
1493f9790aebSLuigi Rizzo  */
1494f9790aebSLuigi Rizzo struct netmap_priv_d {
1495f9790aebSLuigi Rizzo 	struct netmap_if * volatile np_nifp;	/* netmap if descriptor. */
1496f9790aebSLuigi Rizzo 
1497f9790aebSLuigi Rizzo 	struct netmap_adapter	*np_na;
1498f0ea3689SLuigi Rizzo 	uint32_t	np_flags;	/* from the ioctl */
1499847bf383SLuigi Rizzo 	u_int		np_qfirst[NR_TXRX],
1500847bf383SLuigi Rizzo 			np_qlast[NR_TXRX]; /* range of tx/rx rings to scan */
1501f0ea3689SLuigi Rizzo 	uint16_t	np_txpoll;	/* XXX and also np_rxpoll ? */
1502f9790aebSLuigi Rizzo 
15038fd44c93SLuigi Rizzo 	int		np_refs;	/* use with NMG_LOCK held */
1504f0ea3689SLuigi Rizzo 
1505f0ea3689SLuigi Rizzo 	/* pointers to the selinfo to be used for selrecord.
1506f0ea3689SLuigi Rizzo 	 * Either the local or the global one depending on the
1507f0ea3689SLuigi Rizzo 	 * number of rings.
1508f0ea3689SLuigi Rizzo 	 */
1509847bf383SLuigi Rizzo 	NM_SELINFO_T *np_si[NR_TXRX];
1510f0ea3689SLuigi Rizzo 	struct thread	*np_td;		/* kqueue, just debugging */
1511f9790aebSLuigi Rizzo };
1512f9790aebSLuigi Rizzo 
15134bf50f18SLuigi Rizzo #ifdef WITH_MONITOR
15144bf50f18SLuigi Rizzo 
15154bf50f18SLuigi Rizzo struct netmap_monitor_adapter {
15164bf50f18SLuigi Rizzo 	struct netmap_adapter up;
15174bf50f18SLuigi Rizzo 
15184bf50f18SLuigi Rizzo 	struct netmap_priv_d priv;
15194bf50f18SLuigi Rizzo 	uint32_t flags;
15204bf50f18SLuigi Rizzo };
15214bf50f18SLuigi Rizzo 
15224bf50f18SLuigi Rizzo #endif /* WITH_MONITOR */
15234bf50f18SLuigi Rizzo 
1524f9790aebSLuigi Rizzo 
1525039dd540SLuigi Rizzo #ifdef WITH_GENERIC
1526f9790aebSLuigi Rizzo /*
1527f9790aebSLuigi Rizzo  * generic netmap emulation for devices that do not have
1528f9790aebSLuigi Rizzo  * native netmap support.
1529f9790aebSLuigi Rizzo  */
1530f9790aebSLuigi Rizzo int generic_netmap_attach(struct ifnet *ifp);
1531f9790aebSLuigi Rizzo 
1532847bf383SLuigi Rizzo int netmap_catch_rx(struct netmap_generic_adapter *na, int intercept);
1533f9790aebSLuigi Rizzo void generic_rx_handler(struct ifnet *ifp, struct mbuf *m);;
153417885a7bSLuigi Rizzo void netmap_catch_tx(struct netmap_generic_adapter *na, int enable);
1535f9790aebSLuigi Rizzo int generic_xmit_frame(struct ifnet *ifp, struct mbuf *m, void *addr, u_int len, u_int ring_nr);
1536f9790aebSLuigi Rizzo int generic_find_num_desc(struct ifnet *ifp, u_int *tx, u_int *rx);
1537f9790aebSLuigi Rizzo void generic_find_num_queues(struct ifnet *ifp, u_int *txq, u_int *rxq);
1538847bf383SLuigi Rizzo static inline struct ifnet*
1539847bf383SLuigi Rizzo netmap_generic_getifp(struct netmap_generic_adapter *gna)
1540847bf383SLuigi Rizzo {
1541847bf383SLuigi Rizzo         if (gna->prev)
1542847bf383SLuigi Rizzo             return gna->prev->ifp;
1543847bf383SLuigi Rizzo 
1544847bf383SLuigi Rizzo         return gna->up.up.ifp;
1545847bf383SLuigi Rizzo }
1546f9790aebSLuigi Rizzo 
15474bf50f18SLuigi Rizzo //#define RATE_GENERIC  /* Enables communication statistics for generic. */
15484bf50f18SLuigi Rizzo #ifdef RATE_GENERIC
15494bf50f18SLuigi Rizzo void generic_rate(int txp, int txs, int txi, int rxp, int rxs, int rxi);
15504bf50f18SLuigi Rizzo #else
15514bf50f18SLuigi Rizzo #define generic_rate(txp, txs, txi, rxp, rxs, rxi)
15524bf50f18SLuigi Rizzo #endif
15534bf50f18SLuigi Rizzo 
1554f9790aebSLuigi Rizzo /*
1555f9790aebSLuigi Rizzo  * netmap_mitigation API. This is used by the generic adapter
1556f9790aebSLuigi Rizzo  * to reduce the number of interrupt requests/selwakeup
1557f9790aebSLuigi Rizzo  * to clients on incoming packets.
1558f9790aebSLuigi Rizzo  */
15594bf50f18SLuigi Rizzo void netmap_mitigation_init(struct nm_generic_mit *mit, int idx,
15604bf50f18SLuigi Rizzo                                 struct netmap_adapter *na);
1561f0ea3689SLuigi Rizzo void netmap_mitigation_start(struct nm_generic_mit *mit);
1562f0ea3689SLuigi Rizzo void netmap_mitigation_restart(struct nm_generic_mit *mit);
1563f0ea3689SLuigi Rizzo int netmap_mitigation_active(struct nm_generic_mit *mit);
1564f0ea3689SLuigi Rizzo void netmap_mitigation_cleanup(struct nm_generic_mit *mit);
1565039dd540SLuigi Rizzo #endif /* WITH_GENERIC */
1566f0ea3689SLuigi Rizzo 
1567f0ea3689SLuigi Rizzo 
1568f0ea3689SLuigi Rizzo 
1569f0ea3689SLuigi Rizzo /* Shared declarations for the VALE switch. */
1570f0ea3689SLuigi Rizzo 
1571f0ea3689SLuigi Rizzo /*
1572f0ea3689SLuigi Rizzo  * Each transmit queue accumulates a batch of packets into
1573f0ea3689SLuigi Rizzo  * a structure before forwarding. Packets to the same
1574f0ea3689SLuigi Rizzo  * destination are put in a list using ft_next as a link field.
1575f0ea3689SLuigi Rizzo  * ft_frags and ft_next are valid only on the first fragment.
1576f0ea3689SLuigi Rizzo  */
1577f0ea3689SLuigi Rizzo struct nm_bdg_fwd {	/* forwarding entry for a bridge */
1578f0ea3689SLuigi Rizzo 	void *ft_buf;		/* netmap or indirect buffer */
1579f0ea3689SLuigi Rizzo 	uint8_t ft_frags;	/* how many fragments (only on 1st frag) */
1580f0ea3689SLuigi Rizzo 	uint8_t _ft_port;	/* dst port (unused) */
1581f0ea3689SLuigi Rizzo 	uint16_t ft_flags;	/* flags, e.g. indirect */
1582f0ea3689SLuigi Rizzo 	uint16_t ft_len;	/* src fragment len */
1583f0ea3689SLuigi Rizzo 	uint16_t ft_next;	/* next packet to same destination */
1584f0ea3689SLuigi Rizzo };
1585f0ea3689SLuigi Rizzo 
1586f0ea3689SLuigi Rizzo /* struct 'virtio_net_hdr' from linux. */
1587f0ea3689SLuigi Rizzo struct nm_vnet_hdr {
1588f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_F_NEEDS_CSUM     1	/* Use csum_start, csum_offset */
1589f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_F_DATA_VALID    2	/* Csum is valid */
1590f0ea3689SLuigi Rizzo     uint8_t flags;
1591f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_NONE         0       /* Not a GSO frame */
1592f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_TCPV4        1       /* GSO frame, IPv4 TCP (TSO) */
1593f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_UDP          3       /* GSO frame, IPv4 UDP (UFO) */
1594f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_TCPV6        4       /* GSO frame, IPv6 TCP */
1595f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_ECN          0x80    /* TCP has ECN set */
1596f0ea3689SLuigi Rizzo     uint8_t gso_type;
1597f0ea3689SLuigi Rizzo     uint16_t hdr_len;
1598f0ea3689SLuigi Rizzo     uint16_t gso_size;
1599f0ea3689SLuigi Rizzo     uint16_t csum_start;
1600f0ea3689SLuigi Rizzo     uint16_t csum_offset;
1601f0ea3689SLuigi Rizzo };
1602f0ea3689SLuigi Rizzo 
1603f0ea3689SLuigi Rizzo #define WORST_CASE_GSO_HEADER	(14+40+60)  /* IPv6 + TCP */
1604f0ea3689SLuigi Rizzo 
1605f0ea3689SLuigi Rizzo /* Private definitions for IPv4, IPv6, UDP and TCP headers. */
1606f0ea3689SLuigi Rizzo 
1607f0ea3689SLuigi Rizzo struct nm_iphdr {
1608f0ea3689SLuigi Rizzo 	uint8_t		version_ihl;
1609f0ea3689SLuigi Rizzo 	uint8_t		tos;
1610f0ea3689SLuigi Rizzo 	uint16_t	tot_len;
1611f0ea3689SLuigi Rizzo 	uint16_t	id;
1612f0ea3689SLuigi Rizzo 	uint16_t	frag_off;
1613f0ea3689SLuigi Rizzo 	uint8_t		ttl;
1614f0ea3689SLuigi Rizzo 	uint8_t		protocol;
1615f0ea3689SLuigi Rizzo 	uint16_t	check;
1616f0ea3689SLuigi Rizzo 	uint32_t	saddr;
1617f0ea3689SLuigi Rizzo 	uint32_t	daddr;
1618f0ea3689SLuigi Rizzo 	/*The options start here. */
1619f0ea3689SLuigi Rizzo };
1620f0ea3689SLuigi Rizzo 
1621f0ea3689SLuigi Rizzo struct nm_tcphdr {
1622f0ea3689SLuigi Rizzo 	uint16_t	source;
1623f0ea3689SLuigi Rizzo 	uint16_t	dest;
1624f0ea3689SLuigi Rizzo 	uint32_t	seq;
1625f0ea3689SLuigi Rizzo 	uint32_t	ack_seq;
1626f0ea3689SLuigi Rizzo 	uint8_t		doff;  /* Data offset + Reserved */
1627f0ea3689SLuigi Rizzo 	uint8_t		flags;
1628f0ea3689SLuigi Rizzo 	uint16_t	window;
1629f0ea3689SLuigi Rizzo 	uint16_t	check;
1630f0ea3689SLuigi Rizzo 	uint16_t	urg_ptr;
1631f0ea3689SLuigi Rizzo };
1632f0ea3689SLuigi Rizzo 
1633f0ea3689SLuigi Rizzo struct nm_udphdr {
1634f0ea3689SLuigi Rizzo 	uint16_t	source;
1635f0ea3689SLuigi Rizzo 	uint16_t	dest;
1636f0ea3689SLuigi Rizzo 	uint16_t	len;
1637f0ea3689SLuigi Rizzo 	uint16_t	check;
1638f0ea3689SLuigi Rizzo };
1639f0ea3689SLuigi Rizzo 
1640f0ea3689SLuigi Rizzo struct nm_ipv6hdr {
1641f0ea3689SLuigi Rizzo 	uint8_t		priority_version;
1642f0ea3689SLuigi Rizzo 	uint8_t		flow_lbl[3];
1643f0ea3689SLuigi Rizzo 
1644f0ea3689SLuigi Rizzo 	uint16_t	payload_len;
1645f0ea3689SLuigi Rizzo 	uint8_t		nexthdr;
1646f0ea3689SLuigi Rizzo 	uint8_t		hop_limit;
1647f0ea3689SLuigi Rizzo 
1648f0ea3689SLuigi Rizzo 	uint8_t		saddr[16];
1649f0ea3689SLuigi Rizzo 	uint8_t		daddr[16];
1650f0ea3689SLuigi Rizzo };
1651f0ea3689SLuigi Rizzo 
1652f0ea3689SLuigi Rizzo /* Type used to store a checksum (in host byte order) that hasn't been
1653f0ea3689SLuigi Rizzo  * folded yet.
1654f0ea3689SLuigi Rizzo  */
1655f0ea3689SLuigi Rizzo #define rawsum_t uint32_t
1656f0ea3689SLuigi Rizzo 
1657f0ea3689SLuigi Rizzo rawsum_t nm_csum_raw(uint8_t *data, size_t len, rawsum_t cur_sum);
1658f0ea3689SLuigi Rizzo uint16_t nm_csum_ipv4(struct nm_iphdr *iph);
1659f0ea3689SLuigi Rizzo void nm_csum_tcpudp_ipv4(struct nm_iphdr *iph, void *data,
1660f0ea3689SLuigi Rizzo 		      size_t datalen, uint16_t *check);
1661f0ea3689SLuigi Rizzo void nm_csum_tcpudp_ipv6(struct nm_ipv6hdr *ip6h, void *data,
1662f0ea3689SLuigi Rizzo 		      size_t datalen, uint16_t *check);
1663f0ea3689SLuigi Rizzo uint16_t nm_csum_fold(rawsum_t cur_sum);
1664f0ea3689SLuigi Rizzo 
1665f0ea3689SLuigi Rizzo void bdg_mismatch_datapath(struct netmap_vp_adapter *na,
1666f0ea3689SLuigi Rizzo 			   struct netmap_vp_adapter *dst_na,
1667f0ea3689SLuigi Rizzo 			   struct nm_bdg_fwd *ft_p, struct netmap_ring *ring,
1668f0ea3689SLuigi Rizzo 			   u_int *j, u_int lim, u_int *howmany);
16694bf50f18SLuigi Rizzo 
16704bf50f18SLuigi Rizzo /* persistent virtual port routines */
16714bf50f18SLuigi Rizzo int nm_vi_persist(const char *, struct ifnet **);
16724bf50f18SLuigi Rizzo void nm_vi_detach(struct ifnet *);
16734bf50f18SLuigi Rizzo void nm_vi_init_index(void);
16744bf50f18SLuigi Rizzo 
167568b8534bSLuigi Rizzo #endif /* _NET_NETMAP_KERN_H_ */
1676