xref: /freebsd/sys/dev/netmap/netmap_kern.h (revision 039dd540f58fca5779965233bfa8e788382a2f54)
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 
37f9790aebSLuigi Rizzo #define WITH_VALE	// comment out to disable VALE support
38f0ea3689SLuigi Rizzo #define WITH_PIPES
394bf50f18SLuigi Rizzo #define WITH_MONITOR
40*039dd540SLuigi Rizzo #define WITH_GENERIC
41f9790aebSLuigi Rizzo 
421a26580eSLuigi Rizzo #if defined(__FreeBSD__)
43d4b42e08SLuigi Rizzo 
44ce3ee1e7SLuigi Rizzo #define likely(x)	__builtin_expect((long)!!(x), 1L)
45ce3ee1e7SLuigi Rizzo #define unlikely(x)	__builtin_expect((long)!!(x), 0L)
46f196ce38SLuigi Rizzo 
471a26580eSLuigi Rizzo #define	NM_LOCK_T	struct mtx
48*039dd540SLuigi Rizzo 
49*039dd540SLuigi Rizzo /* netmap global lock */
509721a22dSNavdeep Parhar #define	NMG_LOCK_T	struct sx
519721a22dSNavdeep Parhar #define NMG_LOCK_INIT()	sx_init(&netmap_global_lock, \
529721a22dSNavdeep Parhar 				"netmap global lock")
539721a22dSNavdeep Parhar #define NMG_LOCK_DESTROY()	sx_destroy(&netmap_global_lock)
549721a22dSNavdeep Parhar #define NMG_LOCK()	sx_xlock(&netmap_global_lock)
559721a22dSNavdeep Parhar #define NMG_UNLOCK()	sx_xunlock(&netmap_global_lock)
569721a22dSNavdeep Parhar #define NMG_LOCK_ASSERT()	sx_assert(&netmap_global_lock, SA_XLOCKED)
57f9790aebSLuigi Rizzo 
581a26580eSLuigi Rizzo #define	NM_SELINFO_T	struct selinfo
591a26580eSLuigi Rizzo #define	MBUF_LEN(m)	((m)->m_pkthdr.len)
60f9790aebSLuigi Rizzo #define	MBUF_IFP(m)	((m)->m_pkthdr.rcvif)
6117885a7bSLuigi Rizzo #define	NM_SEND_UP(ifp, m)	((NA(ifp))->if_input)(ifp, m)
62d4b42e08SLuigi Rizzo 
63f9790aebSLuigi Rizzo #define NM_ATOMIC_T	volatile int	// XXX ?
64f9790aebSLuigi Rizzo /* atomic operations */
65f9790aebSLuigi Rizzo #include <machine/atomic.h>
66f9790aebSLuigi Rizzo #define NM_ATOMIC_TEST_AND_SET(p)       (!atomic_cmpset_acq_int((p), 0, 1))
67f9790aebSLuigi Rizzo #define NM_ATOMIC_CLEAR(p)              atomic_store_rel_int((p), 0)
68f9790aebSLuigi Rizzo 
697f154b71SLuigi Rizzo #if __FreeBSD_version >= 1100030
707f154b71SLuigi Rizzo #define	WNA(_ifp)	(_ifp)->if_netmap
717f154b71SLuigi Rizzo #else /* older FreeBSD */
727f154b71SLuigi Rizzo #define	WNA(_ifp)	(_ifp)->if_pspare[0]
737f154b71SLuigi Rizzo #endif /* older FreeBSD */
747f154b71SLuigi Rizzo 
7546aa1303SLuigi Rizzo #if __FreeBSD_version >= 1100005
7646aa1303SLuigi Rizzo struct netmap_adapter *netmap_getna(if_t ifp);
7746aa1303SLuigi Rizzo #endif
78f9790aebSLuigi Rizzo 
794bf50f18SLuigi Rizzo #if __FreeBSD_version >= 1100027
804bf50f18SLuigi Rizzo #define GET_MBUF_REFCNT(m)      ((m)->m_ext.ext_cnt ? *((m)->m_ext.ext_cnt) : -1)
814bf50f18SLuigi Rizzo #define SET_MBUF_REFCNT(m, x)   *((m)->m_ext.ext_cnt) = x
824bf50f18SLuigi Rizzo #define PNT_MBUF_REFCNT(m)      ((m)->m_ext.ext_cnt)
834bf50f18SLuigi Rizzo #else
844bf50f18SLuigi Rizzo #define GET_MBUF_REFCNT(m)      ((m)->m_ext.ref_cnt ? *((m)->m_ext.ref_cnt) : -1)
854bf50f18SLuigi Rizzo #define SET_MBUF_REFCNT(m, x)   *((m)->m_ext.ref_cnt) = x
864bf50f18SLuigi Rizzo #define PNT_MBUF_REFCNT(m)      ((m)->m_ext.ref_cnt)
874bf50f18SLuigi Rizzo #endif
884bf50f18SLuigi Rizzo 
89f9790aebSLuigi Rizzo MALLOC_DECLARE(M_NETMAP);
90f9790aebSLuigi Rizzo 
91f9790aebSLuigi Rizzo // XXX linux struct, not used in FreeBSD
92f9790aebSLuigi Rizzo struct net_device_ops {
93f9790aebSLuigi Rizzo };
944bf50f18SLuigi Rizzo struct ethtool_ops {
954bf50f18SLuigi Rizzo };
96f9790aebSLuigi Rizzo struct hrtimer {
97f9790aebSLuigi Rizzo };
98ce3ee1e7SLuigi Rizzo 
9964ae02c3SLuigi Rizzo #elif defined (linux)
100d4b42e08SLuigi Rizzo 
1012579e2d7SLuigi Rizzo #define	NM_LOCK_T	safe_spinlock_t	// see bsd_glue.h
1021a26580eSLuigi Rizzo #define	NM_SELINFO_T	wait_queue_head_t
1031a26580eSLuigi Rizzo #define	MBUF_LEN(m)	((m)->len)
104f9790aebSLuigi Rizzo #define	MBUF_IFP(m)	((m)->dev)
10517885a7bSLuigi Rizzo #define	NM_SEND_UP(ifp, m)  \
10617885a7bSLuigi Rizzo                         do { \
1074bf50f18SLuigi Rizzo                             m->priority = NM_MAGIC_PRIORITY_RX; \
10817885a7bSLuigi Rizzo                             netif_rx(m); \
10917885a7bSLuigi Rizzo                         } while (0)
110f196ce38SLuigi Rizzo 
111ce3ee1e7SLuigi Rizzo #define NM_ATOMIC_T	volatile long unsigned int
112ce3ee1e7SLuigi Rizzo 
113*039dd540SLuigi Rizzo #define NM_MTX_T		struct mutex
114*039dd540SLuigi Rizzo #define NM_MTX_INIT(m, s)	do { (void)s; mutex_init(&(m)); } while (0)
115*039dd540SLuigi Rizzo #define NM_MTX_DESTROY(m)	do { (void)m; } while (0)
116*039dd540SLuigi Rizzo #define NM_MTX_LOCK(m)		mutex_lock(&(m))
117*039dd540SLuigi Rizzo #define NM_MTX_UNLOCK(m)	mutex_unlock(&(m))
118*039dd540SLuigi Rizzo #define NM_MTX_LOCK_ASSERT(m)	mutex_is_locked(&(m))
119*039dd540SLuigi Rizzo 
120*039dd540SLuigi Rizzo #define	NMG_LOCK_T		NM_MTX_T
121*039dd540SLuigi Rizzo #define	NMG_LOCK_INIT()		NM_MTX_INIT(netmap_global_lock, \
122*039dd540SLuigi Rizzo 					"netmap_global_lock")
123*039dd540SLuigi Rizzo #define	NMG_LOCK_DESTROY()	NM_MTX_DESTROY(netmap_global_lock)
124*039dd540SLuigi Rizzo #define	NMG_LOCK()		NM_MTX_LOCK(netmap_global_lock)
125*039dd540SLuigi Rizzo #define	NMG_UNLOCK()		NM_MTX_UNLOCK(netmap_global_lock)
126*039dd540SLuigi Rizzo #define	NMG_LOCK_ASSERT()	NM_MTX_LOCK_ASSERT(netmap_global_lock)
127f9790aebSLuigi Rizzo 
128f196ce38SLuigi Rizzo #ifndef DEV_NETMAP
129f196ce38SLuigi Rizzo #define DEV_NETMAP
130ce3ee1e7SLuigi Rizzo #endif /* DEV_NETMAP */
131f196ce38SLuigi Rizzo 
132f196ce38SLuigi Rizzo #elif defined (__APPLE__)
133d4b42e08SLuigi Rizzo 
1348241616dSLuigi Rizzo #warning apple support is incomplete.
135f196ce38SLuigi Rizzo #define likely(x)	__builtin_expect(!!(x), 1)
136f196ce38SLuigi Rizzo #define unlikely(x)	__builtin_expect(!!(x), 0)
137f196ce38SLuigi Rizzo #define	NM_LOCK_T	IOLock *
138f196ce38SLuigi Rizzo #define	NM_SELINFO_T	struct selinfo
139f196ce38SLuigi Rizzo #define	MBUF_LEN(m)	((m)->m_pkthdr.len)
140f196ce38SLuigi Rizzo #define	NM_SEND_UP(ifp, m)	((ifp)->if_input)(ifp, m)
141f196ce38SLuigi Rizzo 
1421a26580eSLuigi Rizzo #else
143d4b42e08SLuigi Rizzo 
1441a26580eSLuigi Rizzo #error unsupported platform
145d4b42e08SLuigi Rizzo 
146d4b42e08SLuigi Rizzo #endif /* end - platform-specific code */
1471a26580eSLuigi Rizzo 
14868b8534bSLuigi Rizzo #define ND(format, ...)
14968b8534bSLuigi Rizzo #define D(format, ...)						\
15068b8534bSLuigi Rizzo 	do {							\
15168b8534bSLuigi Rizzo 		struct timeval __xxts;				\
15268b8534bSLuigi Rizzo 		microtime(&__xxts);				\
15317885a7bSLuigi Rizzo 		printf("%03d.%06d [%4d] %-25s " format "\n",	\
15468b8534bSLuigi Rizzo 		(int)__xxts.tv_sec % 1000, (int)__xxts.tv_usec,	\
15517885a7bSLuigi Rizzo 		__LINE__, __FUNCTION__, ##__VA_ARGS__);		\
15668b8534bSLuigi Rizzo 	} while (0)
15768b8534bSLuigi Rizzo 
1588241616dSLuigi Rizzo /* rate limited, lps indicates how many per second */
1598241616dSLuigi Rizzo #define RD(lps, format, ...)					\
1608241616dSLuigi Rizzo 	do {							\
1618241616dSLuigi Rizzo 		static int t0, __cnt;				\
1628241616dSLuigi Rizzo 		if (t0 != time_second) {			\
1638241616dSLuigi Rizzo 			t0 = time_second;			\
1648241616dSLuigi Rizzo 			__cnt = 0;				\
1658241616dSLuigi Rizzo 		}						\
1668241616dSLuigi Rizzo 		if (__cnt++ < lps)				\
1678241616dSLuigi Rizzo 			D(format, ##__VA_ARGS__);		\
1688241616dSLuigi Rizzo 	} while (0)
1698241616dSLuigi Rizzo 
17068b8534bSLuigi Rizzo struct netmap_adapter;
171f18be576SLuigi Rizzo struct nm_bdg_fwd;
172f18be576SLuigi Rizzo struct nm_bridge;
173f18be576SLuigi Rizzo struct netmap_priv_d;
17468b8534bSLuigi Rizzo 
175ce3ee1e7SLuigi Rizzo const char *nm_dump_buf(char *p, int len, int lim, char *dst);
176ce3ee1e7SLuigi Rizzo 
177f9790aebSLuigi Rizzo #include "netmap_mbq.h"
178f9790aebSLuigi Rizzo 
179f9790aebSLuigi Rizzo extern NMG_LOCK_T	netmap_global_lock;
180f9790aebSLuigi Rizzo 
18168b8534bSLuigi Rizzo /*
18264ae02c3SLuigi Rizzo  * private, kernel view of a ring. Keeps track of the status of
18364ae02c3SLuigi Rizzo  * a ring across system calls.
18464ae02c3SLuigi Rizzo  *
18564ae02c3SLuigi Rizzo  *	nr_hwcur	index of the next buffer to refill.
18617885a7bSLuigi Rizzo  *			It corresponds to ring->head
18717885a7bSLuigi Rizzo  *			at the time the system call returns.
18864ae02c3SLuigi Rizzo  *
18917885a7bSLuigi Rizzo  *	nr_hwtail	index of the first buffer owned by the kernel.
19017885a7bSLuigi Rizzo  *			On RX, hwcur->hwtail are receive buffers
19117885a7bSLuigi Rizzo  *			not yet released. hwcur is advanced following
19217885a7bSLuigi Rizzo  *			ring->head, hwtail is advanced on incoming packets,
19317885a7bSLuigi Rizzo  *			and a wakeup is generated when hwtail passes ring->cur
19417885a7bSLuigi Rizzo  *			    On TX, hwcur->rcur have been filled by the sender
19517885a7bSLuigi Rizzo  *			but not sent yet to the NIC; rcur->hwtail are available
19617885a7bSLuigi Rizzo  *			for new transmissions, and hwtail->hwcur-1 are pending
19717885a7bSLuigi Rizzo  *			transmissions not yet acknowledged.
19868b8534bSLuigi Rizzo  *
1991a26580eSLuigi Rizzo  * The indexes in the NIC and netmap rings are offset by nkr_hwofs slots.
20068b8534bSLuigi Rizzo  * This is so that, on a reset, buffers owned by userspace are not
20168b8534bSLuigi Rizzo  * modified by the kernel. In particular:
20217885a7bSLuigi Rizzo  * RX rings: the next empty buffer (hwtail + hwofs) coincides with
20368b8534bSLuigi Rizzo  * 	the next empty buffer as known by the hardware (next_to_check or so).
20468b8534bSLuigi Rizzo  * TX rings: hwcur + hwofs coincides with next_to_send
2051dce924dSLuigi Rizzo  *
2061dce924dSLuigi Rizzo  * For received packets, slot->flags is set to nkr_slot_flags
2071dce924dSLuigi Rizzo  * so we can provide a proper initial value (e.g. set NS_FORWARD
2081dce924dSLuigi Rizzo  * when operating in 'transparent' mode).
209ce3ee1e7SLuigi Rizzo  *
210ce3ee1e7SLuigi Rizzo  * The following fields are used to implement lock-free copy of packets
211ce3ee1e7SLuigi Rizzo  * from input to output ports in VALE switch:
212ce3ee1e7SLuigi Rizzo  *	nkr_hwlease	buffer after the last one being copied.
213ce3ee1e7SLuigi Rizzo  *			A writer in nm_bdg_flush reserves N buffers
214ce3ee1e7SLuigi Rizzo  *			from nr_hwlease, advances it, then does the
215ce3ee1e7SLuigi Rizzo  *			copy outside the lock.
216ce3ee1e7SLuigi Rizzo  *			In RX rings (used for VALE ports),
21717885a7bSLuigi Rizzo  *			nkr_hwtail <= nkr_hwlease < nkr_hwcur+N-1
218ce3ee1e7SLuigi Rizzo  *			In TX rings (used for NIC or host stack ports)
21917885a7bSLuigi Rizzo  *			nkr_hwcur <= nkr_hwlease < nkr_hwtail
220ce3ee1e7SLuigi Rizzo  *	nkr_leases	array of nkr_num_slots where writers can report
221ce3ee1e7SLuigi Rizzo  *			completion of their block. NR_NOSLOT (~0) indicates
222ce3ee1e7SLuigi Rizzo  *			that the writer has not finished yet
223ce3ee1e7SLuigi Rizzo  *	nkr_lease_idx	index of next free slot in nr_leases, to be assigned
224ce3ee1e7SLuigi Rizzo  *
225ce3ee1e7SLuigi Rizzo  * The kring is manipulated by txsync/rxsync and generic netmap function.
22617885a7bSLuigi Rizzo  *
22717885a7bSLuigi Rizzo  * Concurrent rxsync or txsync on the same ring are prevented through
22889cc2556SLuigi Rizzo  * by nm_kr_(try)lock() which in turn uses nr_busy. This is all we need
22917885a7bSLuigi Rizzo  * for NIC rings, and for TX rings attached to the host stack.
23017885a7bSLuigi Rizzo  *
23117885a7bSLuigi Rizzo  * RX rings attached to the host stack use an mbq (rx_queue) on both
23217885a7bSLuigi Rizzo  * rxsync_from_host() and netmap_transmit(). The mbq is protected
23317885a7bSLuigi Rizzo  * by its internal lock.
23417885a7bSLuigi Rizzo  *
2354bf50f18SLuigi Rizzo  * RX rings attached to the VALE switch are accessed by both senders
23617885a7bSLuigi Rizzo  * and receiver. They are protected through the q_lock on the RX ring.
23768b8534bSLuigi Rizzo  */
23868b8534bSLuigi Rizzo struct netmap_kring {
23968b8534bSLuigi Rizzo 	struct netmap_ring	*ring;
24017885a7bSLuigi Rizzo 
241ce3ee1e7SLuigi Rizzo 	uint32_t	nr_hwcur;
24217885a7bSLuigi Rizzo 	uint32_t	nr_hwtail;
24317885a7bSLuigi Rizzo 
24417885a7bSLuigi Rizzo 	/*
24517885a7bSLuigi Rizzo 	 * Copies of values in user rings, so we do not need to look
24617885a7bSLuigi Rizzo 	 * at the ring (which could be modified). These are set in the
24717885a7bSLuigi Rizzo 	 * *sync_prologue()/finalize() routines.
24817885a7bSLuigi Rizzo 	 */
24917885a7bSLuigi Rizzo 	uint32_t	rhead;
25017885a7bSLuigi Rizzo 	uint32_t	rcur;
25117885a7bSLuigi Rizzo 	uint32_t	rtail;
25217885a7bSLuigi Rizzo 
253ce3ee1e7SLuigi Rizzo 	uint32_t	nr_kflags;	/* private driver flags */
2542157a17cSLuigi Rizzo #define NKR_PENDINTR	0x1		// Pending interrupt.
255ce3ee1e7SLuigi Rizzo 	uint32_t	nkr_num_slots;
25617885a7bSLuigi Rizzo 
25717885a7bSLuigi Rizzo 	/*
25817885a7bSLuigi Rizzo 	 * On a NIC reset, the NIC ring indexes may be reset but the
25917885a7bSLuigi Rizzo 	 * indexes in the netmap rings remain the same. nkr_hwofs
26017885a7bSLuigi Rizzo 	 * keeps track of the offset between the two.
26117885a7bSLuigi Rizzo 	 */
26217885a7bSLuigi Rizzo 	int32_t		nkr_hwofs;
26368b8534bSLuigi Rizzo 
2641dce924dSLuigi Rizzo 	uint16_t	nkr_slot_flags;	/* initial value for flags */
26517885a7bSLuigi Rizzo 
26617885a7bSLuigi Rizzo 	/* last_reclaim is opaque marker to help reduce the frequency
26717885a7bSLuigi Rizzo 	 * of operations such as reclaiming tx buffers. A possible use
26817885a7bSLuigi Rizzo 	 * is set it to ticks and do the reclaim only once per tick.
26917885a7bSLuigi Rizzo 	 */
27017885a7bSLuigi Rizzo 	uint64_t	last_reclaim;
27117885a7bSLuigi Rizzo 
272ce3ee1e7SLuigi Rizzo 
2731a26580eSLuigi Rizzo 	NM_SELINFO_T	si;		/* poll/select wait queue */
274ce3ee1e7SLuigi Rizzo 	NM_LOCK_T	q_lock;		/* protects kring and ring. */
275ce3ee1e7SLuigi Rizzo 	NM_ATOMIC_T	nr_busy;	/* prevent concurrent syscalls */
276ce3ee1e7SLuigi Rizzo 
27717885a7bSLuigi Rizzo 	struct netmap_adapter *na;
27817885a7bSLuigi Rizzo 
2796435a0dcSLuigi Rizzo 	/* The following fields are for VALE switch support */
28017885a7bSLuigi Rizzo 	struct nm_bdg_fwd *nkr_ft;
28117885a7bSLuigi Rizzo 	uint32_t	*nkr_leases;
28217885a7bSLuigi Rizzo #define NR_NOSLOT	((uint32_t)~0)	/* used in nkr_*lease* */
28317885a7bSLuigi Rizzo 	uint32_t	nkr_hwlease;
28417885a7bSLuigi Rizzo 	uint32_t	nkr_lease_idx;
28517885a7bSLuigi Rizzo 
2864bf50f18SLuigi Rizzo 	/* while nkr_stopped is set, no new [tr]xsync operations can
2874bf50f18SLuigi Rizzo 	 * be started on this kring.
2884bf50f18SLuigi Rizzo 	 * This is used by netmap_disable_all_rings()
2894bf50f18SLuigi Rizzo 	 * to find a synchronization point where critical data
2904bf50f18SLuigi Rizzo 	 * structures pointed to by the kring can be added or removed
2914bf50f18SLuigi Rizzo 	 */
2924bf50f18SLuigi Rizzo 	volatile int nkr_stopped;
293f9790aebSLuigi Rizzo 
294f0ea3689SLuigi Rizzo 	/* Support for adapters without native netmap support.
295f9790aebSLuigi Rizzo 	 * On tx rings we preallocate an array of tx buffers
296f9790aebSLuigi Rizzo 	 * (same size as the netmap ring), on rx rings we
297f0ea3689SLuigi Rizzo 	 * store incoming mbufs in a queue that is drained by
298f0ea3689SLuigi Rizzo 	 * a rxsync.
299f9790aebSLuigi Rizzo 	 */
300f9790aebSLuigi Rizzo 	struct mbuf **tx_pool;
30117885a7bSLuigi Rizzo 	// u_int nr_ntc;		/* Emulation of a next-to-clean RX ring pointer. */
30217885a7bSLuigi Rizzo 	struct mbq rx_queue;            /* intercepted rx mbufs. */
30317885a7bSLuigi Rizzo 
30417885a7bSLuigi Rizzo 	uint32_t	ring_id;	/* debugging */
30517885a7bSLuigi Rizzo 	char name[64];			/* diagnostic */
306f9790aebSLuigi Rizzo 
3074bf50f18SLuigi Rizzo 	/* [tx]sync callback for this kring.
3084bf50f18SLuigi Rizzo 	 * The default nm_kring_create callback (netmap_krings_create)
3094bf50f18SLuigi Rizzo 	 * sets the nm_sync callback of each hardware tx(rx) kring to
3104bf50f18SLuigi Rizzo 	 * the corresponding nm_txsync(nm_rxsync) taken from the
3114bf50f18SLuigi Rizzo 	 * netmap_adapter; moreover, it sets the sync callback
3124bf50f18SLuigi Rizzo 	 * of the host tx(rx) ring to netmap_txsync_to_host
3134bf50f18SLuigi Rizzo 	 * (netmap_rxsync_from_host).
3144bf50f18SLuigi Rizzo 	 *
3154bf50f18SLuigi Rizzo 	 * Overrides: the above configuration is not changed by
3164bf50f18SLuigi Rizzo 	 * any of the nm_krings_create callbacks.
3174bf50f18SLuigi Rizzo 	 */
318f0ea3689SLuigi Rizzo 	int (*nm_sync)(struct netmap_kring *kring, int flags);
319f0ea3689SLuigi Rizzo 
320f0ea3689SLuigi Rizzo #ifdef WITH_PIPES
3214bf50f18SLuigi Rizzo 	struct netmap_kring *pipe;	/* if this is a pipe ring,
3224bf50f18SLuigi Rizzo 					 * pointer to the other end
3234bf50f18SLuigi Rizzo 					 */
3244bf50f18SLuigi Rizzo 	struct netmap_ring *save_ring;	/* pointer to hidden rings
3254bf50f18SLuigi Rizzo        					 * (see netmap_pipe.c for details)
3264bf50f18SLuigi Rizzo 					 */
327f0ea3689SLuigi Rizzo #endif /* WITH_PIPES */
328f0ea3689SLuigi Rizzo 
3294bf50f18SLuigi Rizzo #ifdef WITH_MONITOR
3304bf50f18SLuigi Rizzo 	/* pointer to the adapter that is monitoring this kring (if any)
3314bf50f18SLuigi Rizzo 	 */
3324bf50f18SLuigi Rizzo 	struct netmap_monitor_adapter *monitor;
3334bf50f18SLuigi Rizzo 	/*
3344bf50f18SLuigi Rizzo 	 * Monitors work by intercepting the txsync and/or rxsync of the
3354bf50f18SLuigi Rizzo 	 * monitored krings. This is implemented by replacing
3364bf50f18SLuigi Rizzo 	 * the nm_sync pointer above and saving the previous
3374bf50f18SLuigi Rizzo 	 * one in save_sync below.
3384bf50f18SLuigi Rizzo 	 */
3394bf50f18SLuigi Rizzo 	int (*save_sync)(struct netmap_kring *kring, int flags);
3404bf50f18SLuigi Rizzo #endif
3412157a17cSLuigi Rizzo } __attribute__((__aligned__(64)));
34268b8534bSLuigi Rizzo 
343ce3ee1e7SLuigi Rizzo 
344ce3ee1e7SLuigi Rizzo /* return the next index, with wraparound */
345ce3ee1e7SLuigi Rizzo static inline uint32_t
346ce3ee1e7SLuigi Rizzo nm_next(uint32_t i, uint32_t lim)
347ce3ee1e7SLuigi Rizzo {
348ce3ee1e7SLuigi Rizzo 	return unlikely (i == lim) ? 0 : i + 1;
349ce3ee1e7SLuigi Rizzo }
350ce3ee1e7SLuigi Rizzo 
35117885a7bSLuigi Rizzo 
35217885a7bSLuigi Rizzo /* return the previous index, with wraparound */
35317885a7bSLuigi Rizzo static inline uint32_t
35417885a7bSLuigi Rizzo nm_prev(uint32_t i, uint32_t lim)
35517885a7bSLuigi Rizzo {
35617885a7bSLuigi Rizzo 	return unlikely (i == 0) ? lim : i - 1;
35717885a7bSLuigi Rizzo }
35817885a7bSLuigi Rizzo 
35917885a7bSLuigi Rizzo 
360ce3ee1e7SLuigi Rizzo /*
361ce3ee1e7SLuigi Rizzo  *
362ce3ee1e7SLuigi Rizzo  * Here is the layout for the Rx and Tx rings.
363ce3ee1e7SLuigi Rizzo 
364ce3ee1e7SLuigi Rizzo        RxRING                            TxRING
365ce3ee1e7SLuigi Rizzo 
366ce3ee1e7SLuigi Rizzo       +-----------------+            +-----------------+
367ce3ee1e7SLuigi Rizzo       |                 |            |                 |
368ce3ee1e7SLuigi Rizzo       |XXX free slot XXX|            |XXX free slot XXX|
369ce3ee1e7SLuigi Rizzo       +-----------------+            +-----------------+
37017885a7bSLuigi Rizzo head->| owned by user   |<-hwcur     | not sent to nic |<-hwcur
37117885a7bSLuigi Rizzo       |                 |            | yet             |
37217885a7bSLuigi Rizzo       +-----------------+            |                 |
37317885a7bSLuigi Rizzo  cur->| available to    |            |                 |
37417885a7bSLuigi Rizzo       | user, not read  |            +-----------------+
37517885a7bSLuigi Rizzo       | yet             |       cur->| (being          |
37617885a7bSLuigi Rizzo       |                 |            |  prepared)      |
377ce3ee1e7SLuigi Rizzo       |                 |            |                 |
37817885a7bSLuigi Rizzo       +-----------------+            +     ------      +
37917885a7bSLuigi Rizzo tail->|                 |<-hwtail    |                 |<-hwlease
38017885a7bSLuigi Rizzo       | (being          | ...        |                 | ...
38117885a7bSLuigi Rizzo       |  prepared)      | ...        |                 | ...
38217885a7bSLuigi Rizzo       +-----------------+ ...        |                 | ...
38317885a7bSLuigi Rizzo       |                 |<-hwlease   +-----------------+
38417885a7bSLuigi Rizzo       |                 |      tail->|                 |<-hwtail
385ce3ee1e7SLuigi Rizzo       |                 |            |                 |
386ce3ee1e7SLuigi Rizzo       |                 |            |                 |
387ce3ee1e7SLuigi Rizzo       |                 |            |                 |
388ce3ee1e7SLuigi Rizzo       +-----------------+            +-----------------+
389ce3ee1e7SLuigi Rizzo 
39017885a7bSLuigi Rizzo  * The cur/tail (user view) and hwcur/hwtail (kernel view)
391ce3ee1e7SLuigi Rizzo  * are used in the normal operation of the card.
392ce3ee1e7SLuigi Rizzo  *
393ce3ee1e7SLuigi Rizzo  * When a ring is the output of a switch port (Rx ring for
394ce3ee1e7SLuigi Rizzo  * a VALE port, Tx ring for the host stack or NIC), slots
395ce3ee1e7SLuigi Rizzo  * are reserved in blocks through 'hwlease' which points
396ce3ee1e7SLuigi Rizzo  * to the next unused slot.
39717885a7bSLuigi Rizzo  * On an Rx ring, hwlease is always after hwtail,
39817885a7bSLuigi Rizzo  * and completions cause hwtail to advance.
39917885a7bSLuigi Rizzo  * On a Tx ring, hwlease is always between cur and hwtail,
400ce3ee1e7SLuigi Rizzo  * and completions cause cur to advance.
401ce3ee1e7SLuigi Rizzo  *
402ce3ee1e7SLuigi Rizzo  * nm_kr_space() returns the maximum number of slots that
403ce3ee1e7SLuigi Rizzo  * can be assigned.
404ce3ee1e7SLuigi Rizzo  * nm_kr_lease() reserves the required number of buffers,
405ce3ee1e7SLuigi Rizzo  *    advances nkr_hwlease and also returns an entry in
406ce3ee1e7SLuigi Rizzo  *    a circular array where completions should be reported.
407ce3ee1e7SLuigi Rizzo  */
408ce3ee1e7SLuigi Rizzo 
409ce3ee1e7SLuigi Rizzo 
410ce3ee1e7SLuigi Rizzo 
411f9790aebSLuigi Rizzo enum txrx { NR_RX = 0, NR_TX = 1 };
412ce3ee1e7SLuigi Rizzo 
4134bf50f18SLuigi Rizzo struct netmap_vp_adapter; // forward
4144bf50f18SLuigi Rizzo 
41568b8534bSLuigi Rizzo /*
416f9790aebSLuigi Rizzo  * The "struct netmap_adapter" extends the "struct adapter"
417f9790aebSLuigi Rizzo  * (or equivalent) device descriptor.
418f9790aebSLuigi Rizzo  * It contains all base fields needed to support netmap operation.
419f9790aebSLuigi Rizzo  * There are in fact different types of netmap adapters
420f9790aebSLuigi Rizzo  * (native, generic, VALE switch...) so a netmap_adapter is
421f9790aebSLuigi Rizzo  * just the first field in the derived type.
42268b8534bSLuigi Rizzo  */
42368b8534bSLuigi Rizzo struct netmap_adapter {
4248241616dSLuigi Rizzo 	/*
4258241616dSLuigi Rizzo 	 * On linux we do not have a good way to tell if an interface
426f9790aebSLuigi Rizzo 	 * is netmap-capable. So we always use the following trick:
4278241616dSLuigi Rizzo 	 * NA(ifp) points here, and the first entry (which hopefully
4288241616dSLuigi Rizzo 	 * always exists and is at least 32 bits) contains a magic
4298241616dSLuigi Rizzo 	 * value which we can use to detect that the interface is good.
4308241616dSLuigi Rizzo 	 */
4318241616dSLuigi Rizzo 	uint32_t magic;
432f9790aebSLuigi Rizzo 	uint32_t na_flags;	/* enabled, and other flags */
4338241616dSLuigi Rizzo #define NAF_SKIP_INTR	1	/* use the regular interrupt handler.
4348241616dSLuigi Rizzo 				 * useful during initialization
4358241616dSLuigi Rizzo 				 */
436f18be576SLuigi Rizzo #define NAF_SW_ONLY	2	/* forward packets only to sw adapter */
437ce3ee1e7SLuigi Rizzo #define NAF_BDG_MAYSLEEP 4	/* the bridge is allowed to sleep when
438ce3ee1e7SLuigi Rizzo 				 * forwarding packets coming from this
439ce3ee1e7SLuigi Rizzo 				 * interface
440ce3ee1e7SLuigi Rizzo 				 */
441ce3ee1e7SLuigi Rizzo #define NAF_MEM_OWNER	8	/* the adapter is responsible for the
442ce3ee1e7SLuigi Rizzo 				 * deallocation of the memory allocator
443ce3ee1e7SLuigi Rizzo 				 */
444f9790aebSLuigi Rizzo #define NAF_NATIVE_ON   16      /* the adapter is native and the attached
4454bf50f18SLuigi Rizzo 				 * interface is in netmap mode.
4464bf50f18SLuigi Rizzo 				 * Virtual ports (vale, pipe, monitor...)
4474bf50f18SLuigi Rizzo 				 * should never use this flag.
448f9790aebSLuigi Rizzo 				 */
449f9790aebSLuigi Rizzo #define	NAF_NETMAP_ON	32	/* netmap is active (either native or
4504bf50f18SLuigi Rizzo 				 * emulated). Where possible (e.g. FreeBSD)
451f9790aebSLuigi Rizzo 				 * IFCAP_NETMAP also mirrors this flag.
452f9790aebSLuigi Rizzo 				 */
453f0ea3689SLuigi Rizzo #define NAF_HOST_RINGS  64	/* the adapter supports the host rings */
4544bf50f18SLuigi Rizzo #define NAF_FORCE_NATIVE 128	/* the adapter is always NATIVE */
4554bf50f18SLuigi Rizzo #define	NAF_BUSY	(1U<<31) /* the adapter is used internally and
4564bf50f18SLuigi Rizzo 				  * cannot be registered from userspace
4574bf50f18SLuigi Rizzo 				  */
458f9790aebSLuigi Rizzo 	int active_fds; /* number of user-space descriptors using this
45968b8534bSLuigi Rizzo 			 interface, which is equal to the number of
46068b8534bSLuigi Rizzo 			 struct netmap_if objs in the mapped region. */
46168b8534bSLuigi Rizzo 
46224e57ec9SEd Maste 	u_int num_rx_rings; /* number of adapter receive rings */
46324e57ec9SEd Maste 	u_int num_tx_rings; /* number of adapter transmit rings */
46468b8534bSLuigi Rizzo 
46568b8534bSLuigi Rizzo 	u_int num_tx_desc; /* number of descriptor in each queue */
46668b8534bSLuigi Rizzo 	u_int num_rx_desc;
46768b8534bSLuigi Rizzo 
46868b8534bSLuigi Rizzo 	/* tx_rings and rx_rings are private but allocated
46968b8534bSLuigi Rizzo 	 * as a contiguous chunk of memory. Each array has
47068b8534bSLuigi Rizzo 	 * N+1 entries, for the adapter queues and for the host queue.
47168b8534bSLuigi Rizzo 	 */
47268b8534bSLuigi Rizzo 	struct netmap_kring *tx_rings; /* array of TX rings. */
47368b8534bSLuigi Rizzo 	struct netmap_kring *rx_rings; /* array of RX rings. */
47417885a7bSLuigi Rizzo 
475f9790aebSLuigi Rizzo 	void *tailroom;		       /* space below the rings array */
476f9790aebSLuigi Rizzo 				       /* (used for leases) */
477f9790aebSLuigi Rizzo 
47868b8534bSLuigi Rizzo 
47964ae02c3SLuigi Rizzo 	NM_SELINFO_T tx_si, rx_si;	/* global wait queues */
48064ae02c3SLuigi Rizzo 
481f0ea3689SLuigi Rizzo 	/* count users of the global wait queues */
482f0ea3689SLuigi Rizzo 	int tx_si_users, rx_si_users;
483f0ea3689SLuigi Rizzo 
4844bf50f18SLuigi Rizzo 	void *pdev; /* used to store pci device */
4854bf50f18SLuigi Rizzo 
48668b8534bSLuigi Rizzo 	/* copy of if_qflush and if_transmit pointers, to intercept
48768b8534bSLuigi Rizzo 	 * packets from the network stack when netmap is active.
48868b8534bSLuigi Rizzo 	 */
48968b8534bSLuigi Rizzo 	int     (*if_transmit)(struct ifnet *, struct mbuf *);
49068b8534bSLuigi Rizzo 
49117885a7bSLuigi Rizzo 	/* copy of if_input for netmap_send_up() */
49217885a7bSLuigi Rizzo 	void     (*if_input)(struct ifnet *, struct mbuf *);
49317885a7bSLuigi Rizzo 
49468b8534bSLuigi Rizzo 	/* references to the ifnet and device routines, used by
49568b8534bSLuigi Rizzo 	 * the generic netmap functions.
49668b8534bSLuigi Rizzo 	 */
49768b8534bSLuigi Rizzo 	struct ifnet *ifp; /* adapter is ifp->if_softc */
49868b8534bSLuigi Rizzo 
49917885a7bSLuigi Rizzo 	/*---- callbacks for this netmap adapter -----*/
50017885a7bSLuigi Rizzo 	/*
50117885a7bSLuigi Rizzo 	 * nm_dtor() is the cleanup routine called when destroying
50217885a7bSLuigi Rizzo 	 *	the adapter.
50389cc2556SLuigi Rizzo 	 *	Called with NMG_LOCK held.
50417885a7bSLuigi Rizzo 	 *
50517885a7bSLuigi Rizzo 	 * nm_register() is called on NIOCREGIF and close() to enter
50617885a7bSLuigi Rizzo 	 *	or exit netmap mode on the NIC
5074bf50f18SLuigi Rizzo 	 *	Called with NNG_LOCK held.
50817885a7bSLuigi Rizzo 	 *
50917885a7bSLuigi Rizzo 	 * nm_txsync() pushes packets to the underlying hw/switch
51017885a7bSLuigi Rizzo 	 *
51117885a7bSLuigi Rizzo 	 * nm_rxsync() collects packets from the underlying hw/switch
51217885a7bSLuigi Rizzo 	 *
51317885a7bSLuigi Rizzo 	 * nm_config() returns configuration information from the OS
51489cc2556SLuigi Rizzo 	 *	Called with NMG_LOCK held.
51517885a7bSLuigi Rizzo 	 *
5164bf50f18SLuigi Rizzo 	 * nm_krings_create() create and init the tx_rings and
5174bf50f18SLuigi Rizzo 	 * 	rx_rings arrays of kring structures. In particular,
5184bf50f18SLuigi Rizzo 	 * 	set the nm_sync callbacks for each ring.
5194bf50f18SLuigi Rizzo 	 * 	There is no need to also allocate the corresponding
5204bf50f18SLuigi Rizzo 	 * 	netmap_rings, since netmap_mem_rings_create() will always
5214bf50f18SLuigi Rizzo 	 * 	be called to provide the missing ones.
5224bf50f18SLuigi Rizzo 	 *	Called with NNG_LOCK held.
52317885a7bSLuigi Rizzo 	 *
5244bf50f18SLuigi Rizzo 	 * nm_krings_delete() cleanup and delete the tx_rings and rx_rings
5254bf50f18SLuigi Rizzo 	 * 	arrays
5264bf50f18SLuigi Rizzo 	 *	Called with NMG_LOCK held.
52717885a7bSLuigi Rizzo 	 *
52889cc2556SLuigi Rizzo 	 * nm_notify() is used to act after data have become available
52989cc2556SLuigi Rizzo 	 * 	(or the stopped state of the ring has changed)
53017885a7bSLuigi Rizzo 	 *	For hw devices this is typically a selwakeup(),
53117885a7bSLuigi Rizzo 	 *	but for NIC/host ports attached to a switch (or vice-versa)
53217885a7bSLuigi Rizzo 	 *	we also need to invoke the 'txsync' code downstream.
53317885a7bSLuigi Rizzo 	 */
534f9790aebSLuigi Rizzo 	void (*nm_dtor)(struct netmap_adapter *);
5351a26580eSLuigi Rizzo 
536f9790aebSLuigi Rizzo 	int (*nm_register)(struct netmap_adapter *, int onoff);
537ce3ee1e7SLuigi Rizzo 
5384bf50f18SLuigi Rizzo 	int (*nm_txsync)(struct netmap_kring *kring, int flags);
5394bf50f18SLuigi Rizzo 	int (*nm_rxsync)(struct netmap_kring *kring, int flags);
540ce3ee1e7SLuigi Rizzo #define NAF_FORCE_READ    1
541ce3ee1e7SLuigi Rizzo #define NAF_FORCE_RECLAIM 2
542ae10d1afSLuigi Rizzo 	/* return configuration information */
543f9790aebSLuigi Rizzo 	int (*nm_config)(struct netmap_adapter *,
544f9790aebSLuigi Rizzo 		u_int *txr, u_int *txd, u_int *rxr, u_int *rxd);
545f9790aebSLuigi Rizzo 	int (*nm_krings_create)(struct netmap_adapter *);
546f9790aebSLuigi Rizzo 	void (*nm_krings_delete)(struct netmap_adapter *);
547f9790aebSLuigi Rizzo 	int (*nm_notify)(struct netmap_adapter *,
548f9790aebSLuigi Rizzo 		u_int ring, enum txrx, int flags);
5494bf50f18SLuigi Rizzo #define NAF_DISABLE_NOTIFY 8	/* notify that the stopped state of the
5504bf50f18SLuigi Rizzo 				 * ring has changed (kring->nkr_stopped)
5514bf50f18SLuigi Rizzo 				 */
5524bf50f18SLuigi Rizzo 
5534bf50f18SLuigi Rizzo #ifdef WITH_VALE
5544bf50f18SLuigi Rizzo 	/*
5554bf50f18SLuigi Rizzo 	 * nm_bdg_attach() initializes the na_vp field to point
5564bf50f18SLuigi Rizzo 	 *      to an adapter that can be attached to a VALE switch. If the
5574bf50f18SLuigi Rizzo 	 *      current adapter is already a VALE port, na_vp is simply a cast;
5584bf50f18SLuigi Rizzo 	 *      otherwise, na_vp points to a netmap_bwrap_adapter.
5594bf50f18SLuigi Rizzo 	 *      If applicable, this callback also initializes na_hostvp,
5604bf50f18SLuigi Rizzo 	 *      that can be used to connect the adapter host rings to the
5614bf50f18SLuigi Rizzo 	 *      switch.
5624bf50f18SLuigi Rizzo 	 *      Called with NMG_LOCK held.
5634bf50f18SLuigi Rizzo 	 *
5644bf50f18SLuigi Rizzo 	 * nm_bdg_ctl() is called on the actual attach/detach to/from
5654bf50f18SLuigi Rizzo 	 *      to/from the switch, to perform adapter-specific
5664bf50f18SLuigi Rizzo 	 *      initializations
5674bf50f18SLuigi Rizzo 	 *      Called with NMG_LOCK held.
5684bf50f18SLuigi Rizzo 	 */
5694bf50f18SLuigi Rizzo 	int (*nm_bdg_attach)(const char *bdg_name, struct netmap_adapter *);
5704bf50f18SLuigi Rizzo 	int (*nm_bdg_ctl)(struct netmap_adapter *, struct nmreq *, int);
5714bf50f18SLuigi Rizzo 
5724bf50f18SLuigi Rizzo 	/* adapter used to attach this adapter to a VALE switch (if any) */
5734bf50f18SLuigi Rizzo 	struct netmap_vp_adapter *na_vp;
5744bf50f18SLuigi Rizzo 	/* adapter used to attach the host rings of this adapter
5754bf50f18SLuigi Rizzo 	 * to a VALE switch (if any) */
5764bf50f18SLuigi Rizzo 	struct netmap_vp_adapter *na_hostvp;
5774bf50f18SLuigi Rizzo #endif
578f9790aebSLuigi Rizzo 
579f9790aebSLuigi Rizzo 	/* standard refcount to control the lifetime of the adapter
580f9790aebSLuigi Rizzo 	 * (it should be equal to the lifetime of the corresponding ifp)
581f9790aebSLuigi Rizzo 	 */
582f9790aebSLuigi Rizzo 	int na_refcount;
583f9790aebSLuigi Rizzo 
584f9790aebSLuigi Rizzo 	/* memory allocator (opaque)
585f9790aebSLuigi Rizzo 	 * We also cache a pointer to the lut_entry for translating
586f9790aebSLuigi Rizzo 	 * buffer addresses, and the total number of buffers.
587f9790aebSLuigi Rizzo 	 */
588f9790aebSLuigi Rizzo  	struct netmap_mem_d *nm_mem;
589f9790aebSLuigi Rizzo 	struct lut_entry *na_lut;
590f9790aebSLuigi Rizzo 	uint32_t na_lut_objtotal;	/* max buffer index */
5914bf50f18SLuigi Rizzo 	uint32_t na_lut_objsize;	/* buffer size */
592f9790aebSLuigi Rizzo 
5934bf50f18SLuigi Rizzo 	/* additional information attached to this adapter
5944bf50f18SLuigi Rizzo 	 * by other netmap subsystems. Currently used by
5954bf50f18SLuigi Rizzo 	 * bwrap and LINUX/v1000.
596f9790aebSLuigi Rizzo 	 */
597f9790aebSLuigi Rizzo 	void *na_private;
598f0ea3689SLuigi Rizzo 
599f0ea3689SLuigi Rizzo #ifdef WITH_PIPES
6004bf50f18SLuigi Rizzo 	/* array of pipes that have this adapter as a parent */
601f0ea3689SLuigi Rizzo 	struct netmap_pipe_adapter **na_pipes;
6024bf50f18SLuigi Rizzo 	int na_next_pipe;	/* next free slot in the array */
6034bf50f18SLuigi Rizzo 	int na_max_pipes;	/* size of the array */
604f0ea3689SLuigi Rizzo #endif /* WITH_PIPES */
6054bf50f18SLuigi Rizzo 
6064bf50f18SLuigi Rizzo 	char name[64];
607f9790aebSLuigi Rizzo };
608f9790aebSLuigi Rizzo 
60917885a7bSLuigi Rizzo 
610f9790aebSLuigi Rizzo /*
611f9790aebSLuigi Rizzo  * If the NIC is owned by the kernel
612f9790aebSLuigi Rizzo  * (i.e., bridge), neither another bridge nor user can use it;
613f9790aebSLuigi Rizzo  * if the NIC is owned by a user, only users can share it.
614f9790aebSLuigi Rizzo  * Evaluation must be done under NMG_LOCK().
615f9790aebSLuigi Rizzo  */
6164bf50f18SLuigi Rizzo #define NETMAP_OWNED_BY_KERN(na)	((na)->na_flags & NAF_BUSY)
617f9790aebSLuigi Rizzo #define NETMAP_OWNED_BY_ANY(na) \
6184bf50f18SLuigi Rizzo 	(NETMAP_OWNED_BY_KERN(na) || ((na)->active_fds > 0))
619f9790aebSLuigi Rizzo 
620f9790aebSLuigi Rizzo 
621f9790aebSLuigi Rizzo /*
622f9790aebSLuigi Rizzo  * derived netmap adapters for various types of ports
623f9790aebSLuigi Rizzo  */
624f9790aebSLuigi Rizzo struct netmap_vp_adapter {	/* VALE software port */
625f9790aebSLuigi Rizzo 	struct netmap_adapter up;
626f196ce38SLuigi Rizzo 
627849bec0eSLuigi Rizzo 	/*
628849bec0eSLuigi Rizzo 	 * Bridge support:
629849bec0eSLuigi Rizzo 	 *
630849bec0eSLuigi Rizzo 	 * bdg_port is the port number used in the bridge;
631f18be576SLuigi Rizzo 	 * na_bdg points to the bridge this NA is attached to.
632849bec0eSLuigi Rizzo 	 */
633f196ce38SLuigi Rizzo 	int bdg_port;
634f18be576SLuigi Rizzo 	struct nm_bridge *na_bdg;
635f9790aebSLuigi Rizzo 	int retry;
636f9790aebSLuigi Rizzo 
637f0ea3689SLuigi Rizzo 	/* Offset of ethernet header for each packet. */
638f0ea3689SLuigi Rizzo 	u_int virt_hdr_len;
639f0ea3689SLuigi Rizzo 	/* Maximum Frame Size, used in bdg_mismatch_datapath() */
640f0ea3689SLuigi Rizzo 	u_int mfs;
641f9790aebSLuigi Rizzo };
642f9790aebSLuigi Rizzo 
64317885a7bSLuigi Rizzo 
644f9790aebSLuigi Rizzo struct netmap_hw_adapter {	/* physical device */
645f9790aebSLuigi Rizzo 	struct netmap_adapter up;
646f9790aebSLuigi Rizzo 
647f9790aebSLuigi Rizzo 	struct net_device_ops nm_ndo;	// XXX linux only
6484bf50f18SLuigi Rizzo 	struct ethtool_ops    nm_eto;	// XXX linux only
6494bf50f18SLuigi Rizzo 	const struct ethtool_ops*   save_ethtool;
6504bf50f18SLuigi Rizzo 
6514bf50f18SLuigi Rizzo 	int (*nm_hw_register)(struct netmap_adapter *, int onoff);
652f9790aebSLuigi Rizzo };
653f9790aebSLuigi Rizzo 
654*039dd540SLuigi Rizzo #ifdef WITH_GENERIC
655f0ea3689SLuigi Rizzo /* Mitigation support. */
656f0ea3689SLuigi Rizzo struct nm_generic_mit {
657f0ea3689SLuigi Rizzo 	struct hrtimer mit_timer;
658f0ea3689SLuigi Rizzo 	int mit_pending;
6594bf50f18SLuigi Rizzo 	int mit_ring_idx;  /* index of the ring being mitigated */
660f0ea3689SLuigi Rizzo 	struct netmap_adapter *mit_na;  /* backpointer */
661f0ea3689SLuigi Rizzo };
66217885a7bSLuigi Rizzo 
66317885a7bSLuigi Rizzo struct netmap_generic_adapter {	/* emulated device */
664f9790aebSLuigi Rizzo 	struct netmap_hw_adapter up;
665f9790aebSLuigi Rizzo 
666f9790aebSLuigi Rizzo 	/* Pointer to a previously used netmap adapter. */
667f9790aebSLuigi Rizzo 	struct netmap_adapter *prev;
668f9790aebSLuigi Rizzo 
669f9790aebSLuigi Rizzo 	/* generic netmap adapters support:
670f9790aebSLuigi Rizzo 	 * a net_device_ops struct overrides ndo_select_queue(),
671f9790aebSLuigi Rizzo 	 * save_if_input saves the if_input hook (FreeBSD),
672f0ea3689SLuigi Rizzo 	 * mit implements rx interrupt mitigation,
673f9790aebSLuigi Rizzo 	 */
674f9790aebSLuigi Rizzo 	struct net_device_ops generic_ndo;
675f9790aebSLuigi Rizzo 	void (*save_if_input)(struct ifnet *, struct mbuf *);
676f9790aebSLuigi Rizzo 
677f0ea3689SLuigi Rizzo 	struct nm_generic_mit *mit;
67817885a7bSLuigi Rizzo #ifdef linux
67917885a7bSLuigi Rizzo         netdev_tx_t (*save_start_xmit)(struct mbuf *, struct ifnet *);
68017885a7bSLuigi Rizzo #endif
681f9790aebSLuigi Rizzo };
682*039dd540SLuigi Rizzo #endif  /* WITH_GENERIC */
683f9790aebSLuigi Rizzo 
684f0ea3689SLuigi Rizzo static __inline int
685f0ea3689SLuigi Rizzo netmap_real_tx_rings(struct netmap_adapter *na)
686f0ea3689SLuigi Rizzo {
687f0ea3689SLuigi Rizzo 	return na->num_tx_rings + !!(na->na_flags & NAF_HOST_RINGS);
688f0ea3689SLuigi Rizzo }
689f0ea3689SLuigi Rizzo 
690f0ea3689SLuigi Rizzo static __inline int
691f0ea3689SLuigi Rizzo netmap_real_rx_rings(struct netmap_adapter *na)
692f0ea3689SLuigi Rizzo {
693f0ea3689SLuigi Rizzo 	return na->num_rx_rings + !!(na->na_flags & NAF_HOST_RINGS);
694f0ea3689SLuigi Rizzo }
695f0ea3689SLuigi Rizzo 
696f9790aebSLuigi Rizzo #ifdef WITH_VALE
697f9790aebSLuigi Rizzo 
69817885a7bSLuigi Rizzo /*
69917885a7bSLuigi Rizzo  * Bridge wrapper for non VALE ports attached to a VALE switch.
700f9790aebSLuigi Rizzo  *
70117885a7bSLuigi Rizzo  * The real device must already have its own netmap adapter (hwna).
70217885a7bSLuigi Rizzo  * The bridge wrapper and the hwna adapter share the same set of
70317885a7bSLuigi Rizzo  * netmap rings and buffers, but they have two separate sets of
70417885a7bSLuigi Rizzo  * krings descriptors, with tx/rx meanings swapped:
705f9790aebSLuigi Rizzo  *
706f9790aebSLuigi Rizzo  *                                  netmap
707f9790aebSLuigi Rizzo  *           bwrap     krings       rings      krings      hwna
708f9790aebSLuigi Rizzo  *         +------+   +------+     +-----+    +------+   +------+
709f9790aebSLuigi Rizzo  *         |tx_rings->|      |\   /|     |----|      |<-tx_rings|
710f9790aebSLuigi Rizzo  *         |      |   +------+ \ / +-----+    +------+   |      |
711f9790aebSLuigi Rizzo  *         |      |             X                        |      |
712f9790aebSLuigi Rizzo  *         |      |            / \                       |      |
713f9790aebSLuigi Rizzo  *         |      |   +------+/   \+-----+    +------+   |      |
714f9790aebSLuigi Rizzo  *         |rx_rings->|      |     |     |----|      |<-rx_rings|
715f9790aebSLuigi Rizzo  *         |      |   +------+     +-----+    +------+   |      |
716f9790aebSLuigi Rizzo  *         +------+                                      +------+
717f9790aebSLuigi Rizzo  *
71817885a7bSLuigi Rizzo  * - packets coming from the bridge go to the brwap rx rings,
71917885a7bSLuigi Rizzo  *   which are also the hwna tx rings.  The bwrap notify callback
72017885a7bSLuigi Rizzo  *   will then complete the hwna tx (see netmap_bwrap_notify).
721f9790aebSLuigi Rizzo  *
72217885a7bSLuigi Rizzo  * - packets coming from the outside go to the hwna rx rings,
72317885a7bSLuigi Rizzo  *   which are also the bwrap tx rings.  The (overwritten) hwna
72417885a7bSLuigi Rizzo  *   notify method will then complete the bridge tx
72517885a7bSLuigi Rizzo  *   (see netmap_bwrap_intr_notify).
726f9790aebSLuigi Rizzo  *
72717885a7bSLuigi Rizzo  *   The bridge wrapper may optionally connect the hwna 'host' rings
72817885a7bSLuigi Rizzo  *   to the bridge. This is done by using a second port in the
72917885a7bSLuigi Rizzo  *   bridge and connecting it to the 'host' netmap_vp_adapter
73017885a7bSLuigi Rizzo  *   contained in the netmap_bwrap_adapter. The brwap host adapter
73117885a7bSLuigi Rizzo  *   cross-links the hwna host rings in the same way as shown above.
73217885a7bSLuigi Rizzo  *
73317885a7bSLuigi Rizzo  * - packets coming from the bridge and directed to the host stack
73417885a7bSLuigi Rizzo  *   are handled by the bwrap host notify callback
73517885a7bSLuigi Rizzo  *   (see netmap_bwrap_host_notify)
73617885a7bSLuigi Rizzo  *
73717885a7bSLuigi Rizzo  * - packets coming from the host stack are still handled by the
73817885a7bSLuigi Rizzo  *   overwritten hwna notify callback (netmap_bwrap_intr_notify),
73917885a7bSLuigi Rizzo  *   but are diverted to the host adapter depending on the ring number.
740f9790aebSLuigi Rizzo  *
741f9790aebSLuigi Rizzo  */
742f9790aebSLuigi Rizzo struct netmap_bwrap_adapter {
743f9790aebSLuigi Rizzo 	struct netmap_vp_adapter up;
744f9790aebSLuigi Rizzo 	struct netmap_vp_adapter host;  /* for host rings */
745f9790aebSLuigi Rizzo 	struct netmap_adapter *hwna;	/* the underlying device */
746f9790aebSLuigi Rizzo 
747f9790aebSLuigi Rizzo 	/* backup of the hwna notify callback */
748f9790aebSLuigi Rizzo 	int (*save_notify)(struct netmap_adapter *,
749f9790aebSLuigi Rizzo 			u_int ring, enum txrx, int flags);
7504bf50f18SLuigi Rizzo 	/* backup of the hwna memory allocator */
7514bf50f18SLuigi Rizzo 	struct netmap_mem_d *save_nmd;
75217885a7bSLuigi Rizzo 
75317885a7bSLuigi Rizzo 	/*
75417885a7bSLuigi Rizzo 	 * When we attach a physical interface to the bridge, we
755f18be576SLuigi Rizzo 	 * allow the controlling process to terminate, so we need
7564bf50f18SLuigi Rizzo 	 * a place to store the n_detmap_priv_d data structure.
75717885a7bSLuigi Rizzo 	 * This is only done when physical interfaces
75817885a7bSLuigi Rizzo 	 * are attached to a bridge.
759f18be576SLuigi Rizzo 	 */
760f18be576SLuigi Rizzo 	struct netmap_priv_d *na_kpriv;
76168b8534bSLuigi Rizzo };
7624bf50f18SLuigi Rizzo int netmap_bwrap_attach(const char *name, struct netmap_adapter *);
76368b8534bSLuigi Rizzo 
764f9790aebSLuigi Rizzo 
76517885a7bSLuigi Rizzo #endif /* WITH_VALE */
766ce3ee1e7SLuigi Rizzo 
767f0ea3689SLuigi Rizzo #ifdef WITH_PIPES
768f0ea3689SLuigi Rizzo 
769f0ea3689SLuigi Rizzo #define NM_MAXPIPES 	64	/* max number of pipes per adapter */
770f0ea3689SLuigi Rizzo 
771f0ea3689SLuigi Rizzo struct netmap_pipe_adapter {
772f0ea3689SLuigi Rizzo 	struct netmap_adapter up;
773f0ea3689SLuigi Rizzo 
774f0ea3689SLuigi Rizzo 	u_int id; 	/* pipe identifier */
775f0ea3689SLuigi Rizzo 	int role;	/* either NR_REG_PIPE_MASTER or NR_REG_PIPE_SLAVE */
776f0ea3689SLuigi Rizzo 
777f0ea3689SLuigi Rizzo 	struct netmap_adapter *parent; /* adapter that owns the memory */
778f0ea3689SLuigi Rizzo 	struct netmap_pipe_adapter *peer; /* the other end of the pipe */
779f0ea3689SLuigi Rizzo 	int peer_ref;		/* 1 iff we are holding a ref to the peer */
780f0ea3689SLuigi Rizzo 
781f0ea3689SLuigi Rizzo 	u_int parent_slot; /* index in the parent pipe array */
782f0ea3689SLuigi Rizzo };
783f0ea3689SLuigi Rizzo 
784f0ea3689SLuigi Rizzo #endif /* WITH_PIPES */
785f0ea3689SLuigi Rizzo 
78617885a7bSLuigi Rizzo 
78717885a7bSLuigi Rizzo /* return slots reserved to rx clients; used in drivers */
78817885a7bSLuigi Rizzo static inline uint32_t
78917885a7bSLuigi Rizzo nm_kr_rxspace(struct netmap_kring *k)
79017885a7bSLuigi Rizzo {
79117885a7bSLuigi Rizzo 	int space = k->nr_hwtail - k->nr_hwcur;
792ce3ee1e7SLuigi Rizzo 	if (space < 0)
793ce3ee1e7SLuigi Rizzo 		space += k->nkr_num_slots;
79417885a7bSLuigi Rizzo 	ND("preserving %d rx slots %d -> %d", space, k->nr_hwcur, k->nr_hwtail);
79517885a7bSLuigi Rizzo 
796ce3ee1e7SLuigi Rizzo 	return space;
797ce3ee1e7SLuigi Rizzo }
798ce3ee1e7SLuigi Rizzo 
799ce3ee1e7SLuigi Rizzo 
80017885a7bSLuigi Rizzo /* True if no space in the tx ring. only valid after txsync_prologue */
80117885a7bSLuigi Rizzo static inline int
80217885a7bSLuigi Rizzo nm_kr_txempty(struct netmap_kring *kring)
803ce3ee1e7SLuigi Rizzo {
80417885a7bSLuigi Rizzo 	return kring->rcur == kring->nr_hwtail;
805f9790aebSLuigi Rizzo }
806f9790aebSLuigi Rizzo 
807ce3ee1e7SLuigi Rizzo 
808ce3ee1e7SLuigi Rizzo /*
809f9790aebSLuigi Rizzo  * protect against multiple threads using the same ring.
810f9790aebSLuigi Rizzo  * also check that the ring has not been stopped.
811f9790aebSLuigi Rizzo  * We only care for 0 or !=0 as a return code.
81268b8534bSLuigi Rizzo  */
813f9790aebSLuigi Rizzo #define NM_KR_BUSY	1
814f9790aebSLuigi Rizzo #define NM_KR_STOPPED	2
81568b8534bSLuigi Rizzo 
81617885a7bSLuigi Rizzo 
817f9790aebSLuigi Rizzo static __inline void nm_kr_put(struct netmap_kring *kr)
818f9790aebSLuigi Rizzo {
819f9790aebSLuigi Rizzo 	NM_ATOMIC_CLEAR(&kr->nr_busy);
820f9790aebSLuigi Rizzo }
821f9790aebSLuigi Rizzo 
82217885a7bSLuigi Rizzo 
823f9790aebSLuigi Rizzo static __inline int nm_kr_tryget(struct netmap_kring *kr)
824f9790aebSLuigi Rizzo {
825f9790aebSLuigi Rizzo 	/* check a first time without taking the lock
826f9790aebSLuigi Rizzo 	 * to avoid starvation for nm_kr_get()
827f9790aebSLuigi Rizzo 	 */
828f9790aebSLuigi Rizzo 	if (unlikely(kr->nkr_stopped)) {
829f9790aebSLuigi Rizzo 		ND("ring %p stopped (%d)", kr, kr->nkr_stopped);
830f9790aebSLuigi Rizzo 		return NM_KR_STOPPED;
831f9790aebSLuigi Rizzo 	}
832f9790aebSLuigi Rizzo 	if (unlikely(NM_ATOMIC_TEST_AND_SET(&kr->nr_busy)))
833f9790aebSLuigi Rizzo 		return NM_KR_BUSY;
834f9790aebSLuigi Rizzo 	/* check a second time with lock held */
835f9790aebSLuigi Rizzo 	if (unlikely(kr->nkr_stopped)) {
836f9790aebSLuigi Rizzo 		ND("ring %p stopped (%d)", kr, kr->nkr_stopped);
837f9790aebSLuigi Rizzo 		nm_kr_put(kr);
838f9790aebSLuigi Rizzo 		return NM_KR_STOPPED;
839f9790aebSLuigi Rizzo 	}
840f9790aebSLuigi Rizzo 	return 0;
841f9790aebSLuigi Rizzo }
84268b8534bSLuigi Rizzo 
843849bec0eSLuigi Rizzo 
84468b8534bSLuigi Rizzo /*
84517885a7bSLuigi Rizzo  * The following functions are used by individual drivers to
84668b8534bSLuigi Rizzo  * support netmap operation.
84768b8534bSLuigi Rizzo  *
84868b8534bSLuigi Rizzo  * netmap_attach() initializes a struct netmap_adapter, allocating the
84968b8534bSLuigi Rizzo  * 	struct netmap_ring's and the struct selinfo.
85068b8534bSLuigi Rizzo  *
85168b8534bSLuigi Rizzo  * netmap_detach() frees the memory allocated by netmap_attach().
85268b8534bSLuigi Rizzo  *
853ce3ee1e7SLuigi Rizzo  * netmap_transmit() replaces the if_transmit routine of the interface,
85468b8534bSLuigi Rizzo  *	and is used to intercept packets coming from the stack.
85568b8534bSLuigi Rizzo  *
85668b8534bSLuigi Rizzo  * netmap_load_map/netmap_reload_map are helper routines to set/reset
85768b8534bSLuigi Rizzo  *	the dmamap for a packet buffer
85868b8534bSLuigi Rizzo  *
8594bf50f18SLuigi Rizzo  * netmap_reset() is a helper routine to be called in the hw driver
8604bf50f18SLuigi Rizzo  *	when reinitializing a ring. It should not be called by
8614bf50f18SLuigi Rizzo  *	virtual ports (vale, pipes, monitor)
86268b8534bSLuigi Rizzo  */
863f9790aebSLuigi Rizzo int netmap_attach(struct netmap_adapter *);
86468b8534bSLuigi Rizzo void netmap_detach(struct ifnet *);
865ce3ee1e7SLuigi Rizzo int netmap_transmit(struct ifnet *, struct mbuf *);
86668b8534bSLuigi Rizzo struct netmap_slot *netmap_reset(struct netmap_adapter *na,
867ce3ee1e7SLuigi Rizzo 	enum txrx tx, u_int n, u_int new_cur);
86868b8534bSLuigi Rizzo int netmap_ring_reinit(struct netmap_kring *);
86968b8534bSLuigi Rizzo 
87017885a7bSLuigi Rizzo /* default functions to handle rx/tx interrupts */
87117885a7bSLuigi Rizzo int netmap_rx_irq(struct ifnet *, u_int, u_int *);
87217885a7bSLuigi Rizzo #define netmap_tx_irq(_n, _q) netmap_rx_irq(_n, _q, NULL)
87317885a7bSLuigi Rizzo void netmap_common_irq(struct ifnet *, u_int, u_int *work_done);
87417885a7bSLuigi Rizzo 
87517885a7bSLuigi Rizzo 
8764bf50f18SLuigi Rizzo #ifdef WITH_VALE
8774bf50f18SLuigi Rizzo /* functions used by external modules to interface with VALE */
8784bf50f18SLuigi Rizzo #define netmap_vp_to_ifp(_vp)	((_vp)->up.ifp)
8794bf50f18SLuigi Rizzo #define netmap_ifp_to_vp(_ifp)	(NA(_ifp)->na_vp)
8804bf50f18SLuigi Rizzo #define netmap_ifp_to_host_vp(_ifp) (NA(_ifp)->na_hostvp)
8814bf50f18SLuigi Rizzo #define netmap_bdg_idx(_vp)	((_vp)->bdg_port)
8824bf50f18SLuigi Rizzo const char *netmap_bdg_name(struct netmap_vp_adapter *);
8834bf50f18SLuigi Rizzo #else /* !WITH_VALE */
8844bf50f18SLuigi Rizzo #define netmap_vp_to_ifp(_vp)	NULL
8854bf50f18SLuigi Rizzo #define netmap_ifp_to_vp(_ifp)	NULL
8864bf50f18SLuigi Rizzo #define netmap_ifp_to_host_vp(_ifp) NULL
8874bf50f18SLuigi Rizzo #define netmap_bdg_idx(_vp)	-1
8884bf50f18SLuigi Rizzo #define netmap_bdg_name(_vp)	NULL
8894bf50f18SLuigi Rizzo #endif /* WITH_VALE */
8904bf50f18SLuigi Rizzo 
8914bf50f18SLuigi Rizzo static inline int
8924bf50f18SLuigi Rizzo nm_native_on(struct netmap_adapter *na)
8934bf50f18SLuigi Rizzo {
8944bf50f18SLuigi Rizzo 	return na && na->na_flags & NAF_NATIVE_ON;
8954bf50f18SLuigi Rizzo }
8964bf50f18SLuigi Rizzo 
8974bf50f18SLuigi Rizzo static inline int
8984bf50f18SLuigi Rizzo nm_netmap_on(struct netmap_adapter *na)
8994bf50f18SLuigi Rizzo {
9004bf50f18SLuigi Rizzo 	return na && na->na_flags & NAF_NETMAP_ON;
9014bf50f18SLuigi Rizzo }
90217885a7bSLuigi Rizzo 
90317885a7bSLuigi Rizzo /* set/clear native flags and if_transmit/netdev_ops */
904f9790aebSLuigi Rizzo static inline void
905f9790aebSLuigi Rizzo nm_set_native_flags(struct netmap_adapter *na)
906f9790aebSLuigi Rizzo {
907f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
908ce3ee1e7SLuigi Rizzo 
909f9790aebSLuigi Rizzo 	na->na_flags |= (NAF_NATIVE_ON | NAF_NETMAP_ON);
910f9790aebSLuigi Rizzo #ifdef IFCAP_NETMAP /* or FreeBSD ? */
911f9790aebSLuigi Rizzo 	ifp->if_capenable |= IFCAP_NETMAP;
912f9790aebSLuigi Rizzo #endif
913f9790aebSLuigi Rizzo #ifdef __FreeBSD__
914f9790aebSLuigi Rizzo 	na->if_transmit = ifp->if_transmit;
915f9790aebSLuigi Rizzo 	ifp->if_transmit = netmap_transmit;
916f9790aebSLuigi Rizzo #else
917f9790aebSLuigi Rizzo 	na->if_transmit = (void *)ifp->netdev_ops;
918f9790aebSLuigi Rizzo 	ifp->netdev_ops = &((struct netmap_hw_adapter *)na)->nm_ndo;
9194bf50f18SLuigi Rizzo 	((struct netmap_hw_adapter *)na)->save_ethtool = ifp->ethtool_ops;
9204bf50f18SLuigi Rizzo 	ifp->ethtool_ops = &((struct netmap_hw_adapter*)na)->nm_eto;
921f9790aebSLuigi Rizzo #endif
922f9790aebSLuigi Rizzo }
923f9790aebSLuigi Rizzo 
92417885a7bSLuigi Rizzo 
925f9790aebSLuigi Rizzo static inline void
926f9790aebSLuigi Rizzo nm_clear_native_flags(struct netmap_adapter *na)
927f9790aebSLuigi Rizzo {
928f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
929f9790aebSLuigi Rizzo 
930f9790aebSLuigi Rizzo #ifdef __FreeBSD__
931f9790aebSLuigi Rizzo 	ifp->if_transmit = na->if_transmit;
932f9790aebSLuigi Rizzo #else
933f9790aebSLuigi Rizzo 	ifp->netdev_ops = (void *)na->if_transmit;
9344bf50f18SLuigi Rizzo 	ifp->ethtool_ops = ((struct netmap_hw_adapter*)na)->save_ethtool;
935f9790aebSLuigi Rizzo #endif
936f9790aebSLuigi Rizzo 	na->na_flags &= ~(NAF_NATIVE_ON | NAF_NETMAP_ON);
937f9790aebSLuigi Rizzo #ifdef IFCAP_NETMAP /* or FreeBSD ? */
938f9790aebSLuigi Rizzo 	ifp->if_capenable &= ~IFCAP_NETMAP;
939f9790aebSLuigi Rizzo #endif
940f9790aebSLuigi Rizzo }
941f9790aebSLuigi Rizzo 
942f9790aebSLuigi Rizzo 
943f9790aebSLuigi Rizzo /*
94417885a7bSLuigi Rizzo  * validates parameters in the ring/kring, returns a value for head
94517885a7bSLuigi Rizzo  * If any error, returns ring_size to force a reinit.
94617885a7bSLuigi Rizzo  */
94717885a7bSLuigi Rizzo uint32_t nm_txsync_prologue(struct netmap_kring *);
94817885a7bSLuigi Rizzo 
94917885a7bSLuigi Rizzo 
95017885a7bSLuigi Rizzo /*
95117885a7bSLuigi Rizzo  * validates parameters in the ring/kring, returns a value for head,
952f9790aebSLuigi Rizzo  * and the 'reserved' value in the argument.
95317885a7bSLuigi Rizzo  * If any error, returns ring_size lim to force a reinit.
954f9790aebSLuigi Rizzo  */
95517885a7bSLuigi Rizzo uint32_t nm_rxsync_prologue(struct netmap_kring *);
95617885a7bSLuigi Rizzo 
957f9790aebSLuigi Rizzo 
958f9790aebSLuigi Rizzo /*
95917885a7bSLuigi Rizzo  * update kring and ring at the end of txsync.
960f9790aebSLuigi Rizzo  */
961f9790aebSLuigi Rizzo static inline void
96217885a7bSLuigi Rizzo nm_txsync_finalize(struct netmap_kring *kring)
963f9790aebSLuigi Rizzo {
964f0ea3689SLuigi Rizzo 	/* update ring tail to what the kernel knows */
96517885a7bSLuigi Rizzo 	kring->ring->tail = kring->rtail = kring->nr_hwtail;
966f9790aebSLuigi Rizzo 
96717885a7bSLuigi Rizzo 	/* note, head/rhead/hwcur might be behind cur/rcur
96817885a7bSLuigi Rizzo 	 * if no carrier
96917885a7bSLuigi Rizzo 	 */
97017885a7bSLuigi Rizzo 	ND(5, "%s now hwcur %d hwtail %d head %d cur %d tail %d",
97117885a7bSLuigi Rizzo 		kring->name, kring->nr_hwcur, kring->nr_hwtail,
97217885a7bSLuigi Rizzo 		kring->rhead, kring->rcur, kring->rtail);
973f9790aebSLuigi Rizzo }
974f9790aebSLuigi Rizzo 
97517885a7bSLuigi Rizzo 
97617885a7bSLuigi Rizzo /*
97717885a7bSLuigi Rizzo  * update kring and ring at the end of rxsync
97817885a7bSLuigi Rizzo  */
97917885a7bSLuigi Rizzo static inline void
98017885a7bSLuigi Rizzo nm_rxsync_finalize(struct netmap_kring *kring)
98117885a7bSLuigi Rizzo {
98217885a7bSLuigi Rizzo 	/* tell userspace that there might be new packets */
98317885a7bSLuigi Rizzo 	//struct netmap_ring *ring = kring->ring;
98417885a7bSLuigi Rizzo 	ND("head %d cur %d tail %d -> %d", ring->head, ring->cur, ring->tail,
98517885a7bSLuigi Rizzo 		kring->nr_hwtail);
98617885a7bSLuigi Rizzo 	kring->ring->tail = kring->rtail = kring->nr_hwtail;
98717885a7bSLuigi Rizzo 	/* make a copy of the state for next round */
98817885a7bSLuigi Rizzo 	kring->rhead = kring->ring->head;
98917885a7bSLuigi Rizzo 	kring->rcur = kring->ring->cur;
99017885a7bSLuigi Rizzo }
99117885a7bSLuigi Rizzo 
99217885a7bSLuigi Rizzo 
993f9790aebSLuigi Rizzo /* check/fix address and len in tx rings */
994f9790aebSLuigi Rizzo #if 1 /* debug version */
9954bf50f18SLuigi Rizzo #define	NM_CHECK_ADDR_LEN(_na, _a, _l)	do {				\
9964bf50f18SLuigi Rizzo 	if (_a == NETMAP_BUF_BASE(_na) || _l > NETMAP_BUF_SIZE(_na)) {	\
997f9790aebSLuigi Rizzo 		RD(5, "bad addr/len ring %d slot %d idx %d len %d",	\
9984bf50f18SLuigi Rizzo 			kring->ring_id, nm_i, slot->buf_idx, len);	\
9994bf50f18SLuigi Rizzo 		if (_l > NETMAP_BUF_SIZE(_na))				\
10004bf50f18SLuigi Rizzo 			_l = NETMAP_BUF_SIZE(_na);			\
1001f9790aebSLuigi Rizzo 	} } while (0)
1002f9790aebSLuigi Rizzo #else /* no debug version */
10034bf50f18SLuigi Rizzo #define	NM_CHECK_ADDR_LEN(_na, _a, _l)	do {				\
10044bf50f18SLuigi Rizzo 		if (_l > NETMAP_BUF_SIZE(_na))				\
10054bf50f18SLuigi Rizzo 			_l = NETMAP_BUF_SIZE(_na);			\
1006f9790aebSLuigi Rizzo 	} while (0)
1007f9790aebSLuigi Rizzo #endif
1008f9790aebSLuigi Rizzo 
1009f9790aebSLuigi Rizzo 
1010f9790aebSLuigi Rizzo /*---------------------------------------------------------------*/
1011f9790aebSLuigi Rizzo /*
10124bf50f18SLuigi Rizzo  * Support routines used by netmap subsystems
10134bf50f18SLuigi Rizzo  * (native drivers, VALE, generic, pipes, monitors, ...)
10144bf50f18SLuigi Rizzo  */
10154bf50f18SLuigi Rizzo 
10164bf50f18SLuigi Rizzo 
10174bf50f18SLuigi Rizzo /* common routine for all functions that create a netmap adapter. It performs
10184bf50f18SLuigi Rizzo  * two main tasks:
10194bf50f18SLuigi Rizzo  * - if the na points to an ifp, mark the ifp as netmap capable
10204bf50f18SLuigi Rizzo  *   using na as its native adapter;
10214bf50f18SLuigi Rizzo  * - provide defaults for the setup callbacks and the memory allocator
10224bf50f18SLuigi Rizzo  */
10234bf50f18SLuigi Rizzo int netmap_attach_common(struct netmap_adapter *);
10244bf50f18SLuigi Rizzo /* common actions to be performed on netmap adapter destruction */
10254bf50f18SLuigi Rizzo void netmap_detach_common(struct netmap_adapter *);
10264bf50f18SLuigi Rizzo /* fill priv->np_[tr]xq{first,last} using the ringid and flags information
10274bf50f18SLuigi Rizzo  * coming from a struct nmreq
10284bf50f18SLuigi Rizzo  */
10294bf50f18SLuigi Rizzo int netmap_interp_ringid(struct netmap_priv_d *priv, uint16_t ringid, uint32_t flags);
10304bf50f18SLuigi Rizzo /* update the ring parameters (number and size of tx and rx rings).
10314bf50f18SLuigi Rizzo  * It calls the nm_config callback, if available.
1032f9790aebSLuigi Rizzo  */
1033f9790aebSLuigi Rizzo int netmap_update_config(struct netmap_adapter *na);
10344bf50f18SLuigi Rizzo /* create and initialize the common fields of the krings array.
10354bf50f18SLuigi Rizzo  * using the information that must be already available in the na.
10364bf50f18SLuigi Rizzo  * tailroom can be used to request the allocation of additional
10374bf50f18SLuigi Rizzo  * tailroom bytes after the krings array. This is used by
10384bf50f18SLuigi Rizzo  * netmap_vp_adapter's (i.e., VALE ports) to make room for
10394bf50f18SLuigi Rizzo  * leasing-related data structures
10404bf50f18SLuigi Rizzo  */
1041f0ea3689SLuigi Rizzo int netmap_krings_create(struct netmap_adapter *na, u_int tailroom);
10424bf50f18SLuigi Rizzo /* deletes the kring array of the adapter. The array must have
10434bf50f18SLuigi Rizzo  * been created using netmap_krings_create
10444bf50f18SLuigi Rizzo  */
1045f9790aebSLuigi Rizzo void netmap_krings_delete(struct netmap_adapter *na);
104617885a7bSLuigi Rizzo 
10474bf50f18SLuigi Rizzo /* set the stopped/enabled status of ring
10484bf50f18SLuigi Rizzo  * When stopping, they also wait for all current activity on the ring to
10494bf50f18SLuigi Rizzo  * terminate. The status change is then notified using the na nm_notify
10504bf50f18SLuigi Rizzo  * callback.
10514bf50f18SLuigi Rizzo  */
10524bf50f18SLuigi Rizzo void netmap_set_txring(struct netmap_adapter *, u_int ring_id, int stopped);
10534bf50f18SLuigi Rizzo void netmap_set_rxring(struct netmap_adapter *, u_int ring_id, int stopped);
10544bf50f18SLuigi Rizzo /* set the stopped/enabled status of all rings of the adapter. */
10554bf50f18SLuigi Rizzo void netmap_set_all_rings(struct netmap_adapter *, int stopped);
10564bf50f18SLuigi Rizzo /* convenience wrappers for netmap_set_all_rings, used in drivers */
10574bf50f18SLuigi Rizzo void netmap_disable_all_rings(struct ifnet *);
10584bf50f18SLuigi Rizzo void netmap_enable_all_rings(struct ifnet *);
10594bf50f18SLuigi Rizzo 
10604bf50f18SLuigi Rizzo int netmap_rxsync_from_host(struct netmap_adapter *na, struct thread *td, void *pwait);
1061f9790aebSLuigi Rizzo 
1062f9790aebSLuigi Rizzo struct netmap_if *
1063f9790aebSLuigi Rizzo netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na,
1064f0ea3689SLuigi Rizzo 	uint16_t ringid, uint32_t flags, int *err);
1065f9790aebSLuigi Rizzo 
1066f9790aebSLuigi Rizzo 
1067f9790aebSLuigi Rizzo 
1068f9790aebSLuigi Rizzo u_int nm_bound_var(u_int *v, u_int dflt, u_int lo, u_int hi, const char *msg);
1069f9790aebSLuigi Rizzo int netmap_get_na(struct nmreq *nmr, struct netmap_adapter **na, int create);
1070f9790aebSLuigi Rizzo int netmap_get_hw_na(struct ifnet *ifp, struct netmap_adapter **na);
1071f9790aebSLuigi Rizzo 
107217885a7bSLuigi Rizzo 
1073f9790aebSLuigi Rizzo #ifdef WITH_VALE
1074f18be576SLuigi Rizzo /*
107517885a7bSLuigi Rizzo  * The following bridge-related functions are used by other
107617885a7bSLuigi Rizzo  * kernel modules.
107717885a7bSLuigi Rizzo  *
107817885a7bSLuigi Rizzo  * VALE only supports unicast or broadcast. The lookup
1079f18be576SLuigi Rizzo  * function can return 0 .. NM_BDG_MAXPORTS-1 for regular ports,
1080f18be576SLuigi Rizzo  * NM_BDG_MAXPORTS for broadcast, NM_BDG_MAXPORTS+1 for unknown.
1081f18be576SLuigi Rizzo  * XXX in practice "unknown" might be handled same as broadcast.
1082f18be576SLuigi Rizzo  */
10834bf50f18SLuigi Rizzo typedef u_int (*bdg_lookup_fn_t)(struct nm_bdg_fwd *ft, uint8_t *ring_nr,
10844bf50f18SLuigi Rizzo 		const struct netmap_vp_adapter *);
10854bf50f18SLuigi Rizzo typedef int (*bdg_config_fn_t)(struct nm_ifreq *);
10864bf50f18SLuigi Rizzo typedef void (*bdg_dtor_fn_t)(const struct netmap_vp_adapter *);
10874bf50f18SLuigi Rizzo struct netmap_bdg_ops {
10884bf50f18SLuigi Rizzo 	bdg_lookup_fn_t lookup;
10894bf50f18SLuigi Rizzo 	bdg_config_fn_t config;
10904bf50f18SLuigi Rizzo 	bdg_dtor_fn_t	dtor;
10914bf50f18SLuigi Rizzo };
10924bf50f18SLuigi Rizzo 
10934bf50f18SLuigi Rizzo u_int netmap_bdg_learning(struct nm_bdg_fwd *ft, uint8_t *dst_ring,
10944bf50f18SLuigi Rizzo 		const struct netmap_vp_adapter *);
1095f9790aebSLuigi Rizzo 
1096f9790aebSLuigi Rizzo #define	NM_BDG_MAXPORTS		254	/* up to 254 */
1097f18be576SLuigi Rizzo #define	NM_BDG_BROADCAST	NM_BDG_MAXPORTS
1098f18be576SLuigi Rizzo #define	NM_BDG_NOPORT		(NM_BDG_MAXPORTS+1)
1099f18be576SLuigi Rizzo 
1100f9790aebSLuigi Rizzo #define	NM_NAME			"vale"	/* prefix for bridge port name */
1101f9790aebSLuigi Rizzo 
1102f9790aebSLuigi Rizzo /* these are redefined in case of no VALE support */
1103f9790aebSLuigi Rizzo int netmap_get_bdg_na(struct nmreq *nmr, struct netmap_adapter **na, int create);
1104f9790aebSLuigi Rizzo void netmap_init_bridges(void);
11054bf50f18SLuigi Rizzo int netmap_bdg_ctl(struct nmreq *nmr, struct netmap_bdg_ops *bdg_ops);
11064bf50f18SLuigi Rizzo int netmap_bdg_config(struct nmreq *nmr);
1107f9790aebSLuigi Rizzo 
1108f9790aebSLuigi Rizzo #else /* !WITH_VALE */
1109f9790aebSLuigi Rizzo #define	netmap_get_bdg_na(_1, _2, _3)	0
1110f9790aebSLuigi Rizzo #define netmap_init_bridges(_1)
1111f9790aebSLuigi Rizzo #define	netmap_bdg_ctl(_1, _2)	EINVAL
1112f9790aebSLuigi Rizzo #endif /* !WITH_VALE */
1113f9790aebSLuigi Rizzo 
1114f0ea3689SLuigi Rizzo #ifdef WITH_PIPES
1115f0ea3689SLuigi Rizzo /* max number of pipes per device */
1116f0ea3689SLuigi Rizzo #define NM_MAXPIPES	64	/* XXX how many? */
1117f0ea3689SLuigi Rizzo /* in case of no error, returns the actual number of pipes in nmr->nr_arg1 */
1118f0ea3689SLuigi Rizzo int netmap_pipe_alloc(struct netmap_adapter *, struct nmreq *nmr);
1119f0ea3689SLuigi Rizzo void netmap_pipe_dealloc(struct netmap_adapter *);
1120f0ea3689SLuigi Rizzo int netmap_get_pipe_na(struct nmreq *nmr, struct netmap_adapter **na, int create);
1121f0ea3689SLuigi Rizzo #else /* !WITH_PIPES */
1122f0ea3689SLuigi Rizzo #define NM_MAXPIPES	0
1123f0ea3689SLuigi Rizzo #define netmap_pipe_alloc(_1, _2) 	EOPNOTSUPP
1124f0ea3689SLuigi Rizzo #define netmap_pipe_dealloc(_1)
1125f0ea3689SLuigi Rizzo #define netmap_get_pipe_na(_1, _2, _3)	0
1126f0ea3689SLuigi Rizzo #endif
1127f0ea3689SLuigi Rizzo 
11284bf50f18SLuigi Rizzo #ifdef WITH_MONITOR
11294bf50f18SLuigi Rizzo int netmap_get_monitor_na(struct nmreq *nmr, struct netmap_adapter **na, int create);
11304bf50f18SLuigi Rizzo #else
11314bf50f18SLuigi Rizzo #define netmap_get_monitor_na(_1, _2, _3) 0
11324bf50f18SLuigi Rizzo #endif
11334bf50f18SLuigi Rizzo 
1134f9790aebSLuigi Rizzo /* Various prototypes */
1135f9790aebSLuigi Rizzo int netmap_poll(struct cdev *dev, int events, struct thread *td);
1136f9790aebSLuigi Rizzo int netmap_init(void);
1137f9790aebSLuigi Rizzo void netmap_fini(void);
1138f9790aebSLuigi Rizzo int netmap_get_memory(struct netmap_priv_d* p);
1139f9790aebSLuigi Rizzo void netmap_dtor(void *data);
1140f9790aebSLuigi Rizzo int netmap_dtor_locked(struct netmap_priv_d *priv);
1141f9790aebSLuigi Rizzo 
1142f9790aebSLuigi Rizzo int netmap_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td);
1143f9790aebSLuigi Rizzo 
1144f9790aebSLuigi Rizzo /* netmap_adapter creation/destruction */
114517885a7bSLuigi Rizzo 
114617885a7bSLuigi Rizzo // #define NM_DEBUG_PUTGET 1
1147f9790aebSLuigi Rizzo 
1148f9790aebSLuigi Rizzo #ifdef NM_DEBUG_PUTGET
1149f9790aebSLuigi Rizzo 
1150f9790aebSLuigi Rizzo #define NM_DBG(f) __##f
1151f9790aebSLuigi Rizzo 
1152f9790aebSLuigi Rizzo void __netmap_adapter_get(struct netmap_adapter *na);
1153f9790aebSLuigi Rizzo 
1154f9790aebSLuigi Rizzo #define netmap_adapter_get(na) 				\
1155f9790aebSLuigi Rizzo 	do {						\
1156f9790aebSLuigi Rizzo 		struct netmap_adapter *__na = na;	\
11574bf50f18SLuigi Rizzo 		D("getting %p:%s (%d)", __na, (__na)->name, (__na)->na_refcount);	\
1158f9790aebSLuigi Rizzo 		__netmap_adapter_get(__na);		\
1159f9790aebSLuigi Rizzo 	} while (0)
1160f9790aebSLuigi Rizzo 
1161f9790aebSLuigi Rizzo int __netmap_adapter_put(struct netmap_adapter *na);
1162f9790aebSLuigi Rizzo 
1163f9790aebSLuigi Rizzo #define netmap_adapter_put(na)				\
1164fb25194fSLuigi Rizzo 	({						\
1165f9790aebSLuigi Rizzo 		struct netmap_adapter *__na = na;	\
11664bf50f18SLuigi Rizzo 		D("putting %p:%s (%d)", __na, (__na)->name, (__na)->na_refcount);	\
1167f9790aebSLuigi Rizzo 		__netmap_adapter_put(__na);		\
1168fb25194fSLuigi Rizzo 	})
1169f9790aebSLuigi Rizzo 
1170f9790aebSLuigi Rizzo #else /* !NM_DEBUG_PUTGET */
1171f9790aebSLuigi Rizzo 
1172f9790aebSLuigi Rizzo #define NM_DBG(f) f
1173f9790aebSLuigi Rizzo void netmap_adapter_get(struct netmap_adapter *na);
1174f9790aebSLuigi Rizzo int netmap_adapter_put(struct netmap_adapter *na);
1175f9790aebSLuigi Rizzo 
1176f9790aebSLuigi Rizzo #endif /* !NM_DEBUG_PUTGET */
1177f9790aebSLuigi Rizzo 
1178f9790aebSLuigi Rizzo 
117917885a7bSLuigi Rizzo /*
118017885a7bSLuigi Rizzo  * module variables
118117885a7bSLuigi Rizzo  */
11824bf50f18SLuigi Rizzo #define NETMAP_BUF_BASE(na)	((na)->na_lut[0].vaddr)
11834bf50f18SLuigi Rizzo #define NETMAP_BUF_SIZE(na)	((na)->na_lut_objsize)
118417885a7bSLuigi Rizzo extern int netmap_mitigate;	// XXX not really used
11855819da83SLuigi Rizzo extern int netmap_no_pendintr;
118668b8534bSLuigi Rizzo extern int netmap_verbose;	// XXX debugging
118768b8534bSLuigi Rizzo enum {                                  /* verbose flags */
118868b8534bSLuigi Rizzo 	NM_VERB_ON = 1,                 /* generic verbose */
118968b8534bSLuigi Rizzo 	NM_VERB_HOST = 0x2,             /* verbose host stack */
119068b8534bSLuigi Rizzo 	NM_VERB_RXSYNC = 0x10,          /* verbose on rxsync/txsync */
119168b8534bSLuigi Rizzo 	NM_VERB_TXSYNC = 0x20,
119268b8534bSLuigi Rizzo 	NM_VERB_RXINTR = 0x100,         /* verbose on rx/tx intr (driver) */
119368b8534bSLuigi Rizzo 	NM_VERB_TXINTR = 0x200,
119468b8534bSLuigi Rizzo 	NM_VERB_NIC_RXSYNC = 0x1000,    /* verbose on rx/tx intr (driver) */
119568b8534bSLuigi Rizzo 	NM_VERB_NIC_TXSYNC = 0x2000,
119668b8534bSLuigi Rizzo };
119768b8534bSLuigi Rizzo 
1198f9790aebSLuigi Rizzo extern int netmap_txsync_retry;
1199f9790aebSLuigi Rizzo extern int netmap_generic_mit;
1200f9790aebSLuigi Rizzo extern int netmap_generic_ringsize;
1201f0ea3689SLuigi Rizzo extern int netmap_generic_rings;
1202f9790aebSLuigi Rizzo 
120368b8534bSLuigi Rizzo /*
1204d0c7b075SLuigi Rizzo  * NA returns a pointer to the struct netmap adapter from the ifp,
1205d0c7b075SLuigi Rizzo  * WNA is used to write it.
120668b8534bSLuigi Rizzo  */
1207d0c7b075SLuigi Rizzo #define	NA(_ifp)	((struct netmap_adapter *)WNA(_ifp))
120868b8534bSLuigi Rizzo 
12098241616dSLuigi Rizzo /*
12108241616dSLuigi Rizzo  * Macros to determine if an interface is netmap capable or netmap enabled.
12118241616dSLuigi Rizzo  * See the magic field in struct netmap_adapter.
12128241616dSLuigi Rizzo  */
12138241616dSLuigi Rizzo #ifdef __FreeBSD__
12148241616dSLuigi Rizzo /*
12158241616dSLuigi Rizzo  * on FreeBSD just use if_capabilities and if_capenable.
12168241616dSLuigi Rizzo  */
12178241616dSLuigi Rizzo #define NETMAP_CAPABLE(ifp)	(NA(ifp) &&		\
12188241616dSLuigi Rizzo 	(ifp)->if_capabilities & IFCAP_NETMAP )
12198241616dSLuigi Rizzo 
12208241616dSLuigi Rizzo #define	NETMAP_SET_CAPABLE(ifp)				\
12218241616dSLuigi Rizzo 	(ifp)->if_capabilities |= IFCAP_NETMAP
12228241616dSLuigi Rizzo 
12238241616dSLuigi Rizzo #else	/* linux */
12248241616dSLuigi Rizzo 
12258241616dSLuigi Rizzo /*
12268241616dSLuigi Rizzo  * on linux:
12278241616dSLuigi Rizzo  * we check if NA(ifp) is set and its first element has a related
12288241616dSLuigi Rizzo  * magic value. The capenable is within the struct netmap_adapter.
12298241616dSLuigi Rizzo  */
12308241616dSLuigi Rizzo #define	NETMAP_MAGIC	0x52697a7a
12318241616dSLuigi Rizzo 
12328241616dSLuigi Rizzo #define NETMAP_CAPABLE(ifp)	(NA(ifp) &&		\
12338241616dSLuigi Rizzo 	((uint32_t)(uintptr_t)NA(ifp) ^ NA(ifp)->magic) == NETMAP_MAGIC )
12348241616dSLuigi Rizzo 
12358241616dSLuigi Rizzo #define	NETMAP_SET_CAPABLE(ifp)				\
12368241616dSLuigi Rizzo 	NA(ifp)->magic = ((uint32_t)(uintptr_t)NA(ifp)) ^ NETMAP_MAGIC
12378241616dSLuigi Rizzo 
12388241616dSLuigi Rizzo #endif	/* linux */
123968b8534bSLuigi Rizzo 
1240f196ce38SLuigi Rizzo #ifdef __FreeBSD__
1241f9790aebSLuigi Rizzo 
12424bf50f18SLuigi Rizzo /* Assigns the device IOMMU domain to an allocator.
12434bf50f18SLuigi Rizzo  * Returns -ENOMEM in case the domain is different */
12444bf50f18SLuigi Rizzo #define nm_iommu_group_id(dev) (0)
12454bf50f18SLuigi Rizzo 
124617885a7bSLuigi Rizzo /* Callback invoked by the dma machinery after a successful dmamap_load */
12476dba29a2SLuigi Rizzo static void netmap_dmamap_cb(__unused void *arg,
12486dba29a2SLuigi Rizzo     __unused bus_dma_segment_t * segs, __unused int nseg, __unused int error)
12496dba29a2SLuigi Rizzo {
12506dba29a2SLuigi Rizzo }
12516dba29a2SLuigi Rizzo 
12526dba29a2SLuigi Rizzo /* bus_dmamap_load wrapper: call aforementioned function if map != NULL.
12536dba29a2SLuigi Rizzo  * XXX can we do it without a callback ?
12546dba29a2SLuigi Rizzo  */
12556dba29a2SLuigi Rizzo static inline void
12564bf50f18SLuigi Rizzo netmap_load_map(struct netmap_adapter *na,
12574bf50f18SLuigi Rizzo 	bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
12586dba29a2SLuigi Rizzo {
12596dba29a2SLuigi Rizzo 	if (map)
12604bf50f18SLuigi Rizzo 		bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE(na),
12616dba29a2SLuigi Rizzo 		    netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
12626dba29a2SLuigi Rizzo }
12636dba29a2SLuigi Rizzo 
12644bf50f18SLuigi Rizzo static inline void
12654bf50f18SLuigi Rizzo netmap_unload_map(struct netmap_adapter *na,
12664bf50f18SLuigi Rizzo         bus_dma_tag_t tag, bus_dmamap_t map)
12674bf50f18SLuigi Rizzo {
12684bf50f18SLuigi Rizzo 	if (map)
12694bf50f18SLuigi Rizzo 		bus_dmamap_unload(tag, map);
12704bf50f18SLuigi Rizzo }
12714bf50f18SLuigi Rizzo 
12726dba29a2SLuigi Rizzo /* update the map when a buffer changes. */
12736dba29a2SLuigi Rizzo static inline void
12744bf50f18SLuigi Rizzo netmap_reload_map(struct netmap_adapter *na,
12754bf50f18SLuigi Rizzo 	bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
12766dba29a2SLuigi Rizzo {
12776dba29a2SLuigi Rizzo 	if (map) {
12786dba29a2SLuigi Rizzo 		bus_dmamap_unload(tag, map);
12794bf50f18SLuigi Rizzo 		bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE(na),
12806dba29a2SLuigi Rizzo 		    netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
12816dba29a2SLuigi Rizzo 	}
12826dba29a2SLuigi Rizzo }
1283f9790aebSLuigi Rizzo 
1284f196ce38SLuigi Rizzo #else /* linux */
1285f196ce38SLuigi Rizzo 
12864bf50f18SLuigi Rizzo int nm_iommu_group_id(bus_dma_tag_t dev);
12874bf50f18SLuigi Rizzo extern size_t     netmap_mem_get_bufsize(struct netmap_mem_d *);
12884bf50f18SLuigi Rizzo #include <linux/dma-mapping.h>
12894bf50f18SLuigi Rizzo 
12904bf50f18SLuigi Rizzo static inline void
12914bf50f18SLuigi Rizzo netmap_load_map(struct netmap_adapter *na,
12924bf50f18SLuigi Rizzo 	bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
12934bf50f18SLuigi Rizzo {
12944bf50f18SLuigi Rizzo 	if (map) {
12954bf50f18SLuigi Rizzo 		*map = dma_map_single(na->pdev, buf, netmap_mem_get_bufsize(na->nm_mem),
12964bf50f18SLuigi Rizzo 				DMA_BIDIRECTIONAL);
12974bf50f18SLuigi Rizzo 	}
12984bf50f18SLuigi Rizzo }
12994bf50f18SLuigi Rizzo 
13004bf50f18SLuigi Rizzo static inline void
13014bf50f18SLuigi Rizzo netmap_unload_map(struct netmap_adapter *na,
13024bf50f18SLuigi Rizzo 	bus_dma_tag_t tag, bus_dmamap_t map)
13034bf50f18SLuigi Rizzo {
13044bf50f18SLuigi Rizzo 	u_int sz = netmap_mem_get_bufsize(na->nm_mem);
13054bf50f18SLuigi Rizzo 
13064bf50f18SLuigi Rizzo 	if (*map) {
13074bf50f18SLuigi Rizzo 		dma_unmap_single(na->pdev, *map, sz,
13084bf50f18SLuigi Rizzo 				DMA_BIDIRECTIONAL);
13094bf50f18SLuigi Rizzo 	}
13104bf50f18SLuigi Rizzo }
13114bf50f18SLuigi Rizzo 
13124bf50f18SLuigi Rizzo static inline void
13134bf50f18SLuigi Rizzo netmap_reload_map(struct netmap_adapter *na,
13144bf50f18SLuigi Rizzo 	bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
13154bf50f18SLuigi Rizzo {
13164bf50f18SLuigi Rizzo 	u_int sz = netmap_mem_get_bufsize(na->nm_mem);
13174bf50f18SLuigi Rizzo 
13184bf50f18SLuigi Rizzo 	if (*map) {
13194bf50f18SLuigi Rizzo 		dma_unmap_single(na->pdev, *map, sz,
13204bf50f18SLuigi Rizzo 				DMA_BIDIRECTIONAL);
13214bf50f18SLuigi Rizzo 	}
13224bf50f18SLuigi Rizzo 
13234bf50f18SLuigi Rizzo 	*map = dma_map_single(na->pdev, buf, sz,
13244bf50f18SLuigi Rizzo 				DMA_BIDIRECTIONAL);
13254bf50f18SLuigi Rizzo }
13264bf50f18SLuigi Rizzo 
1327f196ce38SLuigi Rizzo /*
1328f196ce38SLuigi Rizzo  * XXX How do we redefine these functions:
1329f196ce38SLuigi Rizzo  *
1330f196ce38SLuigi Rizzo  * on linux we need
1331f196ce38SLuigi Rizzo  *	dma_map_single(&pdev->dev, virt_addr, len, direction)
1332f196ce38SLuigi Rizzo  *	dma_unmap_single(&adapter->pdev->dev, phys_addr, len, direction
1333f196ce38SLuigi Rizzo  * The len can be implicit (on netmap it is NETMAP_BUF_SIZE)
1334f196ce38SLuigi Rizzo  * unfortunately the direction is not, so we need to change
1335f196ce38SLuigi Rizzo  * something to have a cross API
1336f196ce38SLuigi Rizzo  */
13374bf50f18SLuigi Rizzo 
1338f196ce38SLuigi Rizzo #if 0
1339f196ce38SLuigi Rizzo 	struct e1000_buffer *buffer_info =  &tx_ring->buffer_info[l];
1340f196ce38SLuigi Rizzo 	/* set time_stamp *before* dma to help avoid a possible race */
1341f196ce38SLuigi Rizzo 	buffer_info->time_stamp = jiffies;
1342f196ce38SLuigi Rizzo 	buffer_info->mapped_as_page = false;
1343f196ce38SLuigi Rizzo 	buffer_info->length = len;
1344f196ce38SLuigi Rizzo 	//buffer_info->next_to_watch = l;
1345f196ce38SLuigi Rizzo 	/* reload dma map */
1346f196ce38SLuigi Rizzo 	dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
1347f196ce38SLuigi Rizzo 			NETMAP_BUF_SIZE, DMA_TO_DEVICE);
1348f196ce38SLuigi Rizzo 	buffer_info->dma = dma_map_single(&adapter->pdev->dev,
1349f196ce38SLuigi Rizzo 			addr, NETMAP_BUF_SIZE, DMA_TO_DEVICE);
1350f196ce38SLuigi Rizzo 
1351f196ce38SLuigi Rizzo 	if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
1352f196ce38SLuigi Rizzo 		D("dma mapping error");
1353f196ce38SLuigi Rizzo 		/* goto dma_error; See e1000_put_txbuf() */
1354f196ce38SLuigi Rizzo 		/* XXX reset */
1355f196ce38SLuigi Rizzo 	}
1356f196ce38SLuigi Rizzo 	tx_desc->buffer_addr = htole64(buffer_info->dma); //XXX
1357f196ce38SLuigi Rizzo 
1358f196ce38SLuigi Rizzo #endif
1359f196ce38SLuigi Rizzo 
1360f196ce38SLuigi Rizzo /*
1361f196ce38SLuigi Rizzo  * The bus_dmamap_sync() can be one of wmb() or rmb() depending on direction.
1362f196ce38SLuigi Rizzo  */
1363f196ce38SLuigi Rizzo #define bus_dmamap_sync(_a, _b, _c)
1364f196ce38SLuigi Rizzo 
1365f196ce38SLuigi Rizzo #endif /* linux */
13666dba29a2SLuigi Rizzo 
1367ce3ee1e7SLuigi Rizzo 
13685644ccecSLuigi Rizzo /*
13695644ccecSLuigi Rizzo  * functions to map NIC to KRING indexes (n2k) and vice versa (k2n)
13705644ccecSLuigi Rizzo  */
13715644ccecSLuigi Rizzo static inline int
137264ae02c3SLuigi Rizzo netmap_idx_n2k(struct netmap_kring *kr, int idx)
13735644ccecSLuigi Rizzo {
137464ae02c3SLuigi Rizzo 	int n = kr->nkr_num_slots;
137564ae02c3SLuigi Rizzo 	idx += kr->nkr_hwofs;
137664ae02c3SLuigi Rizzo 	if (idx < 0)
137764ae02c3SLuigi Rizzo 		return idx + n;
137864ae02c3SLuigi Rizzo 	else if (idx < n)
137964ae02c3SLuigi Rizzo 		return idx;
13805644ccecSLuigi Rizzo 	else
138164ae02c3SLuigi Rizzo 		return idx - n;
13825644ccecSLuigi Rizzo }
13835644ccecSLuigi Rizzo 
13845644ccecSLuigi Rizzo 
13855644ccecSLuigi Rizzo static inline int
138664ae02c3SLuigi Rizzo netmap_idx_k2n(struct netmap_kring *kr, int idx)
13875644ccecSLuigi Rizzo {
138864ae02c3SLuigi Rizzo 	int n = kr->nkr_num_slots;
138964ae02c3SLuigi Rizzo 	idx -= kr->nkr_hwofs;
139064ae02c3SLuigi Rizzo 	if (idx < 0)
139164ae02c3SLuigi Rizzo 		return idx + n;
139264ae02c3SLuigi Rizzo 	else if (idx < n)
139364ae02c3SLuigi Rizzo 		return idx;
13945644ccecSLuigi Rizzo 	else
139564ae02c3SLuigi Rizzo 		return idx - n;
13965644ccecSLuigi Rizzo }
13975644ccecSLuigi Rizzo 
13985644ccecSLuigi Rizzo 
1399d76bf4ffSLuigi Rizzo /* Entries of the look-up table. */
1400d76bf4ffSLuigi Rizzo struct lut_entry {
1401d76bf4ffSLuigi Rizzo 	void *vaddr;		/* virtual address. */
1402849bec0eSLuigi Rizzo 	vm_paddr_t paddr;	/* physical address. */
1403d76bf4ffSLuigi Rizzo };
1404d76bf4ffSLuigi Rizzo 
1405d76bf4ffSLuigi Rizzo struct netmap_obj_pool;
1406d76bf4ffSLuigi Rizzo 
140768b8534bSLuigi Rizzo /*
14086e10c8b8SLuigi Rizzo  * NMB return the virtual address of a buffer (buffer 0 on bad index)
14096e10c8b8SLuigi Rizzo  * PNMB also fills the physical address
141068b8534bSLuigi Rizzo  */
14116e10c8b8SLuigi Rizzo static inline void *
14124bf50f18SLuigi Rizzo NMB(struct netmap_adapter *na, struct netmap_slot *slot)
1413f9790aebSLuigi Rizzo {
1414f9790aebSLuigi Rizzo 	struct lut_entry *lut = na->na_lut;
1415f9790aebSLuigi Rizzo 	uint32_t i = slot->buf_idx;
1416f9790aebSLuigi Rizzo 	return (unlikely(i >= na->na_lut_objtotal)) ?
1417f9790aebSLuigi Rizzo 		lut[0].vaddr : lut[i].vaddr;
1418f9790aebSLuigi Rizzo }
1419f9790aebSLuigi Rizzo 
14204bf50f18SLuigi Rizzo static inline void *
14214bf50f18SLuigi Rizzo PNMB(struct netmap_adapter *na, struct netmap_slot *slot, uint64_t *pp)
14224bf50f18SLuigi Rizzo {
14234bf50f18SLuigi Rizzo 	uint32_t i = slot->buf_idx;
14244bf50f18SLuigi Rizzo 	struct lut_entry *lut = na->na_lut;
14254bf50f18SLuigi Rizzo 	void *ret = (i >= na->na_lut_objtotal) ? lut[0].vaddr : lut[i].vaddr;
14264bf50f18SLuigi Rizzo 
14274bf50f18SLuigi Rizzo 	*pp = (i >= na->na_lut_objtotal) ? lut[0].paddr : lut[i].paddr;
14284bf50f18SLuigi Rizzo 	return ret;
14294bf50f18SLuigi Rizzo }
14304bf50f18SLuigi Rizzo 
14314bf50f18SLuigi Rizzo /* Generic version of NMB, which uses device-specific memory. */
14324bf50f18SLuigi Rizzo 
1433ce3ee1e7SLuigi Rizzo 
1434ce3ee1e7SLuigi Rizzo 
1435f9790aebSLuigi Rizzo void netmap_txsync_to_host(struct netmap_adapter *na);
1436f9790aebSLuigi Rizzo 
1437f9790aebSLuigi Rizzo 
143817885a7bSLuigi Rizzo /*
143917885a7bSLuigi Rizzo  * Structure associated to each thread which registered an interface.
1440f9790aebSLuigi Rizzo  *
1441f9790aebSLuigi Rizzo  * The first 4 fields of this structure are written by NIOCREGIF and
1442f9790aebSLuigi Rizzo  * read by poll() and NIOC?XSYNC.
144317885a7bSLuigi Rizzo  *
144417885a7bSLuigi Rizzo  * There is low contention among writers (a correct user program
144517885a7bSLuigi Rizzo  * should have none) and among writers and readers, so we use a
144617885a7bSLuigi Rizzo  * single global lock to protect the structure initialization;
144717885a7bSLuigi Rizzo  * since initialization involves the allocation of memory,
144817885a7bSLuigi Rizzo  * we reuse the memory allocator lock.
144917885a7bSLuigi Rizzo  *
1450f9790aebSLuigi Rizzo  * Read access to the structure is lock free. Readers must check that
1451f9790aebSLuigi Rizzo  * np_nifp is not NULL before using the other fields.
145217885a7bSLuigi Rizzo  * If np_nifp is NULL initialization has not been performed,
145317885a7bSLuigi Rizzo  * so they should return an error to userspace.
1454f9790aebSLuigi Rizzo  *
1455f9790aebSLuigi Rizzo  * The ref_done field is used to regulate access to the refcount in the
1456f9790aebSLuigi Rizzo  * memory allocator. The refcount must be incremented at most once for
1457f9790aebSLuigi Rizzo  * each open("/dev/netmap"). The increment is performed by the first
1458f9790aebSLuigi Rizzo  * function that calls netmap_get_memory() (currently called by
1459f9790aebSLuigi Rizzo  * mmap(), NIOCGINFO and NIOCREGIF).
1460f9790aebSLuigi Rizzo  * If the refcount is incremented, it is then decremented when the
1461f9790aebSLuigi Rizzo  * private structure is destroyed.
1462f9790aebSLuigi Rizzo  */
1463f9790aebSLuigi Rizzo struct netmap_priv_d {
1464f9790aebSLuigi Rizzo 	struct netmap_if * volatile np_nifp;	/* netmap if descriptor. */
1465f9790aebSLuigi Rizzo 
1466f9790aebSLuigi Rizzo 	struct netmap_adapter	*np_na;
1467f0ea3689SLuigi Rizzo 	uint32_t	np_flags;	/* from the ioctl */
1468f0ea3689SLuigi Rizzo 	u_int		np_txqfirst, np_txqlast; /* range of tx rings to scan */
1469f0ea3689SLuigi Rizzo 	u_int		np_rxqfirst, np_rxqlast; /* range of rx rings to scan */
1470f0ea3689SLuigi Rizzo 	uint16_t	np_txpoll;	/* XXX and also np_rxpoll ? */
1471f9790aebSLuigi Rizzo 
1472f9790aebSLuigi Rizzo 	struct netmap_mem_d     *np_mref;	/* use with NMG_LOCK held */
1473f9790aebSLuigi Rizzo 	/* np_refcount is only used on FreeBSD */
1474f9790aebSLuigi Rizzo 	int		np_refcount;	/* use with NMG_LOCK held */
1475f0ea3689SLuigi Rizzo 
1476f0ea3689SLuigi Rizzo 	/* pointers to the selinfo to be used for selrecord.
1477f0ea3689SLuigi Rizzo 	 * Either the local or the global one depending on the
1478f0ea3689SLuigi Rizzo 	 * number of rings.
1479f0ea3689SLuigi Rizzo 	 */
1480f0ea3689SLuigi Rizzo 	NM_SELINFO_T *np_rxsi, *np_txsi;
1481f0ea3689SLuigi Rizzo 	struct thread	*np_td;		/* kqueue, just debugging */
1482f9790aebSLuigi Rizzo };
1483f9790aebSLuigi Rizzo 
14844bf50f18SLuigi Rizzo #ifdef WITH_MONITOR
14854bf50f18SLuigi Rizzo 
14864bf50f18SLuigi Rizzo struct netmap_monitor_adapter {
14874bf50f18SLuigi Rizzo 	struct netmap_adapter up;
14884bf50f18SLuigi Rizzo 
14894bf50f18SLuigi Rizzo 	struct netmap_priv_d priv;
14904bf50f18SLuigi Rizzo 	uint32_t flags;
14914bf50f18SLuigi Rizzo };
14924bf50f18SLuigi Rizzo 
14934bf50f18SLuigi Rizzo #endif /* WITH_MONITOR */
14944bf50f18SLuigi Rizzo 
1495f9790aebSLuigi Rizzo 
1496*039dd540SLuigi Rizzo #ifdef WITH_GENERIC
1497f9790aebSLuigi Rizzo /*
1498f9790aebSLuigi Rizzo  * generic netmap emulation for devices that do not have
1499f9790aebSLuigi Rizzo  * native netmap support.
1500f9790aebSLuigi Rizzo  */
1501f9790aebSLuigi Rizzo int generic_netmap_attach(struct ifnet *ifp);
1502f9790aebSLuigi Rizzo 
1503f9790aebSLuigi Rizzo int netmap_catch_rx(struct netmap_adapter *na, int intercept);
1504f9790aebSLuigi Rizzo void generic_rx_handler(struct ifnet *ifp, struct mbuf *m);;
150517885a7bSLuigi Rizzo void netmap_catch_tx(struct netmap_generic_adapter *na, int enable);
1506f9790aebSLuigi Rizzo int generic_xmit_frame(struct ifnet *ifp, struct mbuf *m, void *addr, u_int len, u_int ring_nr);
1507f9790aebSLuigi Rizzo int generic_find_num_desc(struct ifnet *ifp, u_int *tx, u_int *rx);
1508f9790aebSLuigi Rizzo void generic_find_num_queues(struct ifnet *ifp, u_int *txq, u_int *rxq);
1509f9790aebSLuigi Rizzo 
15104bf50f18SLuigi Rizzo //#define RATE_GENERIC  /* Enables communication statistics for generic. */
15114bf50f18SLuigi Rizzo #ifdef RATE_GENERIC
15124bf50f18SLuigi Rizzo void generic_rate(int txp, int txs, int txi, int rxp, int rxs, int rxi);
15134bf50f18SLuigi Rizzo #else
15144bf50f18SLuigi Rizzo #define generic_rate(txp, txs, txi, rxp, rxs, rxi)
15154bf50f18SLuigi Rizzo #endif
15164bf50f18SLuigi Rizzo 
1517f9790aebSLuigi Rizzo /*
1518f9790aebSLuigi Rizzo  * netmap_mitigation API. This is used by the generic adapter
1519f9790aebSLuigi Rizzo  * to reduce the number of interrupt requests/selwakeup
1520f9790aebSLuigi Rizzo  * to clients on incoming packets.
1521f9790aebSLuigi Rizzo  */
15224bf50f18SLuigi Rizzo void netmap_mitigation_init(struct nm_generic_mit *mit, int idx,
15234bf50f18SLuigi Rizzo                                 struct netmap_adapter *na);
1524f0ea3689SLuigi Rizzo void netmap_mitigation_start(struct nm_generic_mit *mit);
1525f0ea3689SLuigi Rizzo void netmap_mitigation_restart(struct nm_generic_mit *mit);
1526f0ea3689SLuigi Rizzo int netmap_mitigation_active(struct nm_generic_mit *mit);
1527f0ea3689SLuigi Rizzo void netmap_mitigation_cleanup(struct nm_generic_mit *mit);
1528*039dd540SLuigi Rizzo #endif /* WITH_GENERIC */
1529f0ea3689SLuigi Rizzo 
1530f0ea3689SLuigi Rizzo 
1531f0ea3689SLuigi Rizzo 
1532f0ea3689SLuigi Rizzo /* Shared declarations for the VALE switch. */
1533f0ea3689SLuigi Rizzo 
1534f0ea3689SLuigi Rizzo /*
1535f0ea3689SLuigi Rizzo  * Each transmit queue accumulates a batch of packets into
1536f0ea3689SLuigi Rizzo  * a structure before forwarding. Packets to the same
1537f0ea3689SLuigi Rizzo  * destination are put in a list using ft_next as a link field.
1538f0ea3689SLuigi Rizzo  * ft_frags and ft_next are valid only on the first fragment.
1539f0ea3689SLuigi Rizzo  */
1540f0ea3689SLuigi Rizzo struct nm_bdg_fwd {	/* forwarding entry for a bridge */
1541f0ea3689SLuigi Rizzo 	void *ft_buf;		/* netmap or indirect buffer */
1542f0ea3689SLuigi Rizzo 	uint8_t ft_frags;	/* how many fragments (only on 1st frag) */
1543f0ea3689SLuigi Rizzo 	uint8_t _ft_port;	/* dst port (unused) */
1544f0ea3689SLuigi Rizzo 	uint16_t ft_flags;	/* flags, e.g. indirect */
1545f0ea3689SLuigi Rizzo 	uint16_t ft_len;	/* src fragment len */
1546f0ea3689SLuigi Rizzo 	uint16_t ft_next;	/* next packet to same destination */
1547f0ea3689SLuigi Rizzo };
1548f0ea3689SLuigi Rizzo 
1549f0ea3689SLuigi Rizzo /* struct 'virtio_net_hdr' from linux. */
1550f0ea3689SLuigi Rizzo struct nm_vnet_hdr {
1551f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_F_NEEDS_CSUM     1	/* Use csum_start, csum_offset */
1552f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_F_DATA_VALID    2	/* Csum is valid */
1553f0ea3689SLuigi Rizzo     uint8_t flags;
1554f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_NONE         0       /* Not a GSO frame */
1555f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_TCPV4        1       /* GSO frame, IPv4 TCP (TSO) */
1556f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_UDP          3       /* GSO frame, IPv4 UDP (UFO) */
1557f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_TCPV6        4       /* GSO frame, IPv6 TCP */
1558f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_ECN          0x80    /* TCP has ECN set */
1559f0ea3689SLuigi Rizzo     uint8_t gso_type;
1560f0ea3689SLuigi Rizzo     uint16_t hdr_len;
1561f0ea3689SLuigi Rizzo     uint16_t gso_size;
1562f0ea3689SLuigi Rizzo     uint16_t csum_start;
1563f0ea3689SLuigi Rizzo     uint16_t csum_offset;
1564f0ea3689SLuigi Rizzo };
1565f0ea3689SLuigi Rizzo 
1566f0ea3689SLuigi Rizzo #define WORST_CASE_GSO_HEADER	(14+40+60)  /* IPv6 + TCP */
1567f0ea3689SLuigi Rizzo 
1568f0ea3689SLuigi Rizzo /* Private definitions for IPv4, IPv6, UDP and TCP headers. */
1569f0ea3689SLuigi Rizzo 
1570f0ea3689SLuigi Rizzo struct nm_iphdr {
1571f0ea3689SLuigi Rizzo 	uint8_t		version_ihl;
1572f0ea3689SLuigi Rizzo 	uint8_t		tos;
1573f0ea3689SLuigi Rizzo 	uint16_t	tot_len;
1574f0ea3689SLuigi Rizzo 	uint16_t	id;
1575f0ea3689SLuigi Rizzo 	uint16_t	frag_off;
1576f0ea3689SLuigi Rizzo 	uint8_t		ttl;
1577f0ea3689SLuigi Rizzo 	uint8_t		protocol;
1578f0ea3689SLuigi Rizzo 	uint16_t	check;
1579f0ea3689SLuigi Rizzo 	uint32_t	saddr;
1580f0ea3689SLuigi Rizzo 	uint32_t	daddr;
1581f0ea3689SLuigi Rizzo 	/*The options start here. */
1582f0ea3689SLuigi Rizzo };
1583f0ea3689SLuigi Rizzo 
1584f0ea3689SLuigi Rizzo struct nm_tcphdr {
1585f0ea3689SLuigi Rizzo 	uint16_t	source;
1586f0ea3689SLuigi Rizzo 	uint16_t	dest;
1587f0ea3689SLuigi Rizzo 	uint32_t	seq;
1588f0ea3689SLuigi Rizzo 	uint32_t	ack_seq;
1589f0ea3689SLuigi Rizzo 	uint8_t		doff;  /* Data offset + Reserved */
1590f0ea3689SLuigi Rizzo 	uint8_t		flags;
1591f0ea3689SLuigi Rizzo 	uint16_t	window;
1592f0ea3689SLuigi Rizzo 	uint16_t	check;
1593f0ea3689SLuigi Rizzo 	uint16_t	urg_ptr;
1594f0ea3689SLuigi Rizzo };
1595f0ea3689SLuigi Rizzo 
1596f0ea3689SLuigi Rizzo struct nm_udphdr {
1597f0ea3689SLuigi Rizzo 	uint16_t	source;
1598f0ea3689SLuigi Rizzo 	uint16_t	dest;
1599f0ea3689SLuigi Rizzo 	uint16_t	len;
1600f0ea3689SLuigi Rizzo 	uint16_t	check;
1601f0ea3689SLuigi Rizzo };
1602f0ea3689SLuigi Rizzo 
1603f0ea3689SLuigi Rizzo struct nm_ipv6hdr {
1604f0ea3689SLuigi Rizzo 	uint8_t		priority_version;
1605f0ea3689SLuigi Rizzo 	uint8_t		flow_lbl[3];
1606f0ea3689SLuigi Rizzo 
1607f0ea3689SLuigi Rizzo 	uint16_t	payload_len;
1608f0ea3689SLuigi Rizzo 	uint8_t		nexthdr;
1609f0ea3689SLuigi Rizzo 	uint8_t		hop_limit;
1610f0ea3689SLuigi Rizzo 
1611f0ea3689SLuigi Rizzo 	uint8_t		saddr[16];
1612f0ea3689SLuigi Rizzo 	uint8_t		daddr[16];
1613f0ea3689SLuigi Rizzo };
1614f0ea3689SLuigi Rizzo 
1615f0ea3689SLuigi Rizzo /* Type used to store a checksum (in host byte order) that hasn't been
1616f0ea3689SLuigi Rizzo  * folded yet.
1617f0ea3689SLuigi Rizzo  */
1618f0ea3689SLuigi Rizzo #define rawsum_t uint32_t
1619f0ea3689SLuigi Rizzo 
1620f0ea3689SLuigi Rizzo rawsum_t nm_csum_raw(uint8_t *data, size_t len, rawsum_t cur_sum);
1621f0ea3689SLuigi Rizzo uint16_t nm_csum_ipv4(struct nm_iphdr *iph);
1622f0ea3689SLuigi Rizzo void nm_csum_tcpudp_ipv4(struct nm_iphdr *iph, void *data,
1623f0ea3689SLuigi Rizzo 		      size_t datalen, uint16_t *check);
1624f0ea3689SLuigi Rizzo void nm_csum_tcpudp_ipv6(struct nm_ipv6hdr *ip6h, void *data,
1625f0ea3689SLuigi Rizzo 		      size_t datalen, uint16_t *check);
1626f0ea3689SLuigi Rizzo uint16_t nm_csum_fold(rawsum_t cur_sum);
1627f0ea3689SLuigi Rizzo 
1628f0ea3689SLuigi Rizzo void bdg_mismatch_datapath(struct netmap_vp_adapter *na,
1629f0ea3689SLuigi Rizzo 			   struct netmap_vp_adapter *dst_na,
1630f0ea3689SLuigi Rizzo 			   struct nm_bdg_fwd *ft_p, struct netmap_ring *ring,
1631f0ea3689SLuigi Rizzo 			   u_int *j, u_int lim, u_int *howmany);
16324bf50f18SLuigi Rizzo 
16334bf50f18SLuigi Rizzo /* persistent virtual port routines */
16344bf50f18SLuigi Rizzo int nm_vi_persist(const char *, struct ifnet **);
16354bf50f18SLuigi Rizzo void nm_vi_detach(struct ifnet *);
16364bf50f18SLuigi Rizzo void nm_vi_init_index(void);
16374bf50f18SLuigi Rizzo 
163868b8534bSLuigi Rizzo #endif /* _NET_NETMAP_KERN_H_ */
1639