xref: /freebsd-14.2/sys/dev/netmap/netmap_kern.h (revision f0ea3689)
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
38*f0ea3689SLuigi Rizzo #define WITH_PIPES
39f9790aebSLuigi Rizzo 
401a26580eSLuigi Rizzo #if defined(__FreeBSD__)
41d4b42e08SLuigi Rizzo 
42ce3ee1e7SLuigi Rizzo #define likely(x)	__builtin_expect((long)!!(x), 1L)
43ce3ee1e7SLuigi Rizzo #define unlikely(x)	__builtin_expect((long)!!(x), 0L)
44f196ce38SLuigi Rizzo 
451a26580eSLuigi Rizzo #define	NM_LOCK_T	struct mtx
46f9790aebSLuigi Rizzo #define	NMG_LOCK_T	struct mtx
47f9790aebSLuigi Rizzo #define NMG_LOCK_INIT()	mtx_init(&netmap_global_lock, \
48f9790aebSLuigi Rizzo 				"netmap global lock", NULL, MTX_DEF)
49f9790aebSLuigi Rizzo #define NMG_LOCK_DESTROY()	mtx_destroy(&netmap_global_lock)
50f9790aebSLuigi Rizzo #define NMG_LOCK()	mtx_lock(&netmap_global_lock)
51f9790aebSLuigi Rizzo #define NMG_UNLOCK()	mtx_unlock(&netmap_global_lock)
52f9790aebSLuigi Rizzo #define NMG_LOCK_ASSERT()	mtx_assert(&netmap_global_lock, MA_OWNED)
53f9790aebSLuigi Rizzo 
541a26580eSLuigi Rizzo #define	NM_SELINFO_T	struct selinfo
551a26580eSLuigi Rizzo #define	MBUF_LEN(m)	((m)->m_pkthdr.len)
56f9790aebSLuigi Rizzo #define	MBUF_IFP(m)	((m)->m_pkthdr.rcvif)
5717885a7bSLuigi Rizzo #define	NM_SEND_UP(ifp, m)	((NA(ifp))->if_input)(ifp, m)
58d4b42e08SLuigi Rizzo 
59f9790aebSLuigi Rizzo #define NM_ATOMIC_T	volatile int	// XXX ?
60f9790aebSLuigi Rizzo /* atomic operations */
61f9790aebSLuigi Rizzo #include <machine/atomic.h>
62f9790aebSLuigi Rizzo #define NM_ATOMIC_TEST_AND_SET(p)       (!atomic_cmpset_acq_int((p), 0, 1))
63f9790aebSLuigi Rizzo #define NM_ATOMIC_CLEAR(p)              atomic_store_rel_int((p), 0)
64f9790aebSLuigi Rizzo 
65f9790aebSLuigi Rizzo 
66f9790aebSLuigi Rizzo MALLOC_DECLARE(M_NETMAP);
67f9790aebSLuigi Rizzo 
68f9790aebSLuigi Rizzo // XXX linux struct, not used in FreeBSD
69f9790aebSLuigi Rizzo struct net_device_ops {
70f9790aebSLuigi Rizzo };
71f9790aebSLuigi Rizzo struct hrtimer {
72f9790aebSLuigi Rizzo };
73ce3ee1e7SLuigi Rizzo 
7464ae02c3SLuigi Rizzo #elif defined (linux)
75d4b42e08SLuigi Rizzo 
762579e2d7SLuigi Rizzo #define	NM_LOCK_T	safe_spinlock_t	// see bsd_glue.h
771a26580eSLuigi Rizzo #define	NM_SELINFO_T	wait_queue_head_t
781a26580eSLuigi Rizzo #define	MBUF_LEN(m)	((m)->len)
79f9790aebSLuigi Rizzo #define	MBUF_IFP(m)	((m)->dev)
8017885a7bSLuigi Rizzo #define	NM_SEND_UP(ifp, m)  \
8117885a7bSLuigi Rizzo                         do { \
8217885a7bSLuigi Rizzo                             m->priority = NM_MAGIC_PRIORITY; \
8317885a7bSLuigi Rizzo                             netif_rx(m); \
8417885a7bSLuigi Rizzo                         } while (0)
85f196ce38SLuigi Rizzo 
86ce3ee1e7SLuigi Rizzo #define NM_ATOMIC_T	volatile long unsigned int
87ce3ee1e7SLuigi Rizzo 
88f9790aebSLuigi Rizzo // XXX a mtx would suffice here too 20130404 gl
89f9790aebSLuigi Rizzo #define NMG_LOCK_T		struct semaphore
90f9790aebSLuigi Rizzo #define NMG_LOCK_INIT()		sema_init(&netmap_global_lock, 1)
91f9790aebSLuigi Rizzo #define NMG_LOCK_DESTROY()
92f9790aebSLuigi Rizzo #define NMG_LOCK()		down(&netmap_global_lock)
93f9790aebSLuigi Rizzo #define NMG_UNLOCK()		up(&netmap_global_lock)
94f9790aebSLuigi Rizzo #define NMG_LOCK_ASSERT()	//	XXX to be completed
95f9790aebSLuigi Rizzo 
96f196ce38SLuigi Rizzo #ifndef DEV_NETMAP
97f196ce38SLuigi Rizzo #define DEV_NETMAP
98ce3ee1e7SLuigi Rizzo #endif /* DEV_NETMAP */
99f196ce38SLuigi Rizzo 
100f196ce38SLuigi Rizzo /*
1018241616dSLuigi Rizzo  * IFCAP_NETMAP goes into net_device's priv_flags (if_capenable).
1028241616dSLuigi Rizzo  * This was 16 bits up to linux 2.6.36, so we need a 16 bit value on older
103f196ce38SLuigi Rizzo  * platforms and tolerate the clash with IFF_DYNAMIC and IFF_BRIDGE_PORT.
1048241616dSLuigi Rizzo  * For the 32-bit value, 0x100000 has no clashes until at least 3.5.1
105f196ce38SLuigi Rizzo  */
106f196ce38SLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
107f196ce38SLuigi Rizzo #define IFCAP_NETMAP	0x8000
108f196ce38SLuigi Rizzo #else
109f18be576SLuigi Rizzo #define IFCAP_NETMAP	0x200000
110f196ce38SLuigi Rizzo #endif
111f196ce38SLuigi Rizzo 
112f196ce38SLuigi Rizzo #elif defined (__APPLE__)
113d4b42e08SLuigi Rizzo 
1148241616dSLuigi Rizzo #warning apple support is incomplete.
115f196ce38SLuigi Rizzo #define likely(x)	__builtin_expect(!!(x), 1)
116f196ce38SLuigi Rizzo #define unlikely(x)	__builtin_expect(!!(x), 0)
117f196ce38SLuigi Rizzo #define	NM_LOCK_T	IOLock *
118f196ce38SLuigi Rizzo #define	NM_SELINFO_T	struct selinfo
119f196ce38SLuigi Rizzo #define	MBUF_LEN(m)	((m)->m_pkthdr.len)
120f196ce38SLuigi Rizzo #define	NM_SEND_UP(ifp, m)	((ifp)->if_input)(ifp, m)
121f196ce38SLuigi Rizzo 
1221a26580eSLuigi Rizzo #else
123d4b42e08SLuigi Rizzo 
1241a26580eSLuigi Rizzo #error unsupported platform
125d4b42e08SLuigi Rizzo 
126d4b42e08SLuigi Rizzo #endif /* end - platform-specific code */
1271a26580eSLuigi Rizzo 
12868b8534bSLuigi Rizzo #define ND(format, ...)
12968b8534bSLuigi Rizzo #define D(format, ...)						\
13068b8534bSLuigi Rizzo 	do {							\
13168b8534bSLuigi Rizzo 		struct timeval __xxts;				\
13268b8534bSLuigi Rizzo 		microtime(&__xxts);				\
13317885a7bSLuigi Rizzo 		printf("%03d.%06d [%4d] %-25s " format "\n",	\
13468b8534bSLuigi Rizzo 		(int)__xxts.tv_sec % 1000, (int)__xxts.tv_usec,	\
13517885a7bSLuigi Rizzo 		__LINE__, __FUNCTION__, ##__VA_ARGS__);		\
13668b8534bSLuigi Rizzo 	} while (0)
13768b8534bSLuigi Rizzo 
1388241616dSLuigi Rizzo /* rate limited, lps indicates how many per second */
1398241616dSLuigi Rizzo #define RD(lps, format, ...)					\
1408241616dSLuigi Rizzo 	do {							\
1418241616dSLuigi Rizzo 		static int t0, __cnt;				\
1428241616dSLuigi Rizzo 		if (t0 != time_second) {			\
1438241616dSLuigi Rizzo 			t0 = time_second;			\
1448241616dSLuigi Rizzo 			__cnt = 0;				\
1458241616dSLuigi Rizzo 		}						\
1468241616dSLuigi Rizzo 		if (__cnt++ < lps)				\
1478241616dSLuigi Rizzo 			D(format, ##__VA_ARGS__);		\
1488241616dSLuigi Rizzo 	} while (0)
1498241616dSLuigi Rizzo 
15068b8534bSLuigi Rizzo struct netmap_adapter;
151f18be576SLuigi Rizzo struct nm_bdg_fwd;
152f18be576SLuigi Rizzo struct nm_bridge;
153f18be576SLuigi Rizzo struct netmap_priv_d;
15468b8534bSLuigi Rizzo 
155ce3ee1e7SLuigi Rizzo const char *nm_dump_buf(char *p, int len, int lim, char *dst);
156ce3ee1e7SLuigi Rizzo 
157f9790aebSLuigi Rizzo #include "netmap_mbq.h"
158f9790aebSLuigi Rizzo 
159f9790aebSLuigi Rizzo extern NMG_LOCK_T	netmap_global_lock;
160f9790aebSLuigi Rizzo 
16168b8534bSLuigi Rizzo /*
16264ae02c3SLuigi Rizzo  * private, kernel view of a ring. Keeps track of the status of
16364ae02c3SLuigi Rizzo  * a ring across system calls.
16464ae02c3SLuigi Rizzo  *
16564ae02c3SLuigi Rizzo  *	nr_hwcur	index of the next buffer to refill.
16617885a7bSLuigi Rizzo  *			It corresponds to ring->head
16717885a7bSLuigi Rizzo  *			at the time the system call returns.
16864ae02c3SLuigi Rizzo  *
16917885a7bSLuigi Rizzo  *	nr_hwtail	index of the first buffer owned by the kernel.
17017885a7bSLuigi Rizzo  *			On RX, hwcur->hwtail are receive buffers
17117885a7bSLuigi Rizzo  *			not yet released. hwcur is advanced following
17217885a7bSLuigi Rizzo  *			ring->head, hwtail is advanced on incoming packets,
17317885a7bSLuigi Rizzo  *			and a wakeup is generated when hwtail passes ring->cur
17417885a7bSLuigi Rizzo  *			    On TX, hwcur->rcur have been filled by the sender
17517885a7bSLuigi Rizzo  *			but not sent yet to the NIC; rcur->hwtail are available
17617885a7bSLuigi Rizzo  *			for new transmissions, and hwtail->hwcur-1 are pending
17717885a7bSLuigi Rizzo  *			transmissions not yet acknowledged.
17868b8534bSLuigi Rizzo  *
1791a26580eSLuigi Rizzo  * The indexes in the NIC and netmap rings are offset by nkr_hwofs slots.
18068b8534bSLuigi Rizzo  * This is so that, on a reset, buffers owned by userspace are not
18168b8534bSLuigi Rizzo  * modified by the kernel. In particular:
18217885a7bSLuigi Rizzo  * RX rings: the next empty buffer (hwtail + hwofs) coincides with
18368b8534bSLuigi Rizzo  * 	the next empty buffer as known by the hardware (next_to_check or so).
18468b8534bSLuigi Rizzo  * TX rings: hwcur + hwofs coincides with next_to_send
1851dce924dSLuigi Rizzo  *
186ce3ee1e7SLuigi Rizzo  * Clients cannot issue concurrent syscall on a ring. The system
187ce3ee1e7SLuigi Rizzo  * detects this and reports an error using two flags,
188ce3ee1e7SLuigi Rizzo  * NKR_WBUSY and NKR_RBUSY
1891dce924dSLuigi Rizzo  * For received packets, slot->flags is set to nkr_slot_flags
1901dce924dSLuigi Rizzo  * so we can provide a proper initial value (e.g. set NS_FORWARD
1911dce924dSLuigi Rizzo  * when operating in 'transparent' mode).
192ce3ee1e7SLuigi Rizzo  *
193ce3ee1e7SLuigi Rizzo  * The following fields are used to implement lock-free copy of packets
194ce3ee1e7SLuigi Rizzo  * from input to output ports in VALE switch:
195ce3ee1e7SLuigi Rizzo  *	nkr_hwlease	buffer after the last one being copied.
196ce3ee1e7SLuigi Rizzo  *			A writer in nm_bdg_flush reserves N buffers
197ce3ee1e7SLuigi Rizzo  *			from nr_hwlease, advances it, then does the
198ce3ee1e7SLuigi Rizzo  *			copy outside the lock.
199ce3ee1e7SLuigi Rizzo  *			In RX rings (used for VALE ports),
20017885a7bSLuigi Rizzo  *			nkr_hwtail <= nkr_hwlease < nkr_hwcur+N-1
201ce3ee1e7SLuigi Rizzo  *			In TX rings (used for NIC or host stack ports)
20217885a7bSLuigi Rizzo  *			nkr_hwcur <= nkr_hwlease < nkr_hwtail
203ce3ee1e7SLuigi Rizzo  *	nkr_leases	array of nkr_num_slots where writers can report
204ce3ee1e7SLuigi Rizzo  *			completion of their block. NR_NOSLOT (~0) indicates
205ce3ee1e7SLuigi Rizzo  *			that the writer has not finished yet
206ce3ee1e7SLuigi Rizzo  *	nkr_lease_idx	index of next free slot in nr_leases, to be assigned
207ce3ee1e7SLuigi Rizzo  *
208ce3ee1e7SLuigi Rizzo  * The kring is manipulated by txsync/rxsync and generic netmap function.
20917885a7bSLuigi Rizzo  *
21017885a7bSLuigi Rizzo  * Concurrent rxsync or txsync on the same ring are prevented through
21117885a7bSLuigi Rizzo  * by nm_kr_lock() which in turn uses nr_busy. This is all we need
21217885a7bSLuigi Rizzo  * for NIC rings, and for TX rings attached to the host stack.
21317885a7bSLuigi Rizzo  *
21417885a7bSLuigi Rizzo  * RX rings attached to the host stack use an mbq (rx_queue) on both
21517885a7bSLuigi Rizzo  * rxsync_from_host() and netmap_transmit(). The mbq is protected
21617885a7bSLuigi Rizzo  * by its internal lock.
21717885a7bSLuigi Rizzo  *
21817885a7bSLuigi Rizzo  * RX rings attached to the VALE switch are accessed by both sender
21917885a7bSLuigi Rizzo  * and receiver. They are protected through the q_lock on the RX ring.
22068b8534bSLuigi Rizzo  */
22168b8534bSLuigi Rizzo struct netmap_kring {
22268b8534bSLuigi Rizzo 	struct netmap_ring	*ring;
22317885a7bSLuigi Rizzo 
224ce3ee1e7SLuigi Rizzo 	uint32_t	nr_hwcur;
22517885a7bSLuigi Rizzo 	uint32_t	nr_hwtail;
22617885a7bSLuigi Rizzo 
22717885a7bSLuigi Rizzo 	/*
22817885a7bSLuigi Rizzo 	 * Copies of values in user rings, so we do not need to look
22917885a7bSLuigi Rizzo 	 * at the ring (which could be modified). These are set in the
23017885a7bSLuigi Rizzo 	 * *sync_prologue()/finalize() routines.
23117885a7bSLuigi Rizzo 	 */
23217885a7bSLuigi Rizzo 	uint32_t	rhead;
23317885a7bSLuigi Rizzo 	uint32_t	rcur;
23417885a7bSLuigi Rizzo 	uint32_t	rtail;
23517885a7bSLuigi Rizzo 
236ce3ee1e7SLuigi Rizzo 	uint32_t	nr_kflags;	/* private driver flags */
2372157a17cSLuigi Rizzo #define NKR_PENDINTR	0x1		// Pending interrupt.
238ce3ee1e7SLuigi Rizzo 	uint32_t	nkr_num_slots;
23917885a7bSLuigi Rizzo 
24017885a7bSLuigi Rizzo 	/*
24117885a7bSLuigi Rizzo 	 * On a NIC reset, the NIC ring indexes may be reset but the
24217885a7bSLuigi Rizzo 	 * indexes in the netmap rings remain the same. nkr_hwofs
24317885a7bSLuigi Rizzo 	 * keeps track of the offset between the two.
24417885a7bSLuigi Rizzo 	 */
24517885a7bSLuigi Rizzo 	int32_t		nkr_hwofs;
24668b8534bSLuigi Rizzo 
2471dce924dSLuigi Rizzo 	uint16_t	nkr_slot_flags;	/* initial value for flags */
24817885a7bSLuigi Rizzo 
24917885a7bSLuigi Rizzo 	/* last_reclaim is opaque marker to help reduce the frequency
25017885a7bSLuigi Rizzo 	 * of operations such as reclaiming tx buffers. A possible use
25117885a7bSLuigi Rizzo 	 * is set it to ticks and do the reclaim only once per tick.
25217885a7bSLuigi Rizzo 	 */
25317885a7bSLuigi Rizzo 	uint64_t	last_reclaim;
25417885a7bSLuigi Rizzo 
255ce3ee1e7SLuigi Rizzo 
2561a26580eSLuigi Rizzo 	NM_SELINFO_T	si;		/* poll/select wait queue */
257ce3ee1e7SLuigi Rizzo 	NM_LOCK_T	q_lock;		/* protects kring and ring. */
258ce3ee1e7SLuigi Rizzo 	NM_ATOMIC_T	nr_busy;	/* prevent concurrent syscalls */
259ce3ee1e7SLuigi Rizzo 
26017885a7bSLuigi Rizzo 	struct netmap_adapter *na;
26117885a7bSLuigi Rizzo 
26217885a7bSLuigi Rizzo 	/* The folloiwing fields are for VALE switch support */
26317885a7bSLuigi Rizzo 	struct nm_bdg_fwd *nkr_ft;
26417885a7bSLuigi Rizzo 	uint32_t	*nkr_leases;
26517885a7bSLuigi Rizzo #define NR_NOSLOT	((uint32_t)~0)	/* used in nkr_*lease* */
26617885a7bSLuigi Rizzo 	uint32_t	nkr_hwlease;
26717885a7bSLuigi Rizzo 	uint32_t	nkr_lease_idx;
26817885a7bSLuigi Rizzo 
26917885a7bSLuigi Rizzo 	volatile int nkr_stopped;	// XXX what for ?
270f9790aebSLuigi Rizzo 
271*f0ea3689SLuigi Rizzo 	/* Support for adapters without native netmap support.
272f9790aebSLuigi Rizzo 	 * On tx rings we preallocate an array of tx buffers
273f9790aebSLuigi Rizzo 	 * (same size as the netmap ring), on rx rings we
274*f0ea3689SLuigi Rizzo 	 * store incoming mbufs in a queue that is drained by
275*f0ea3689SLuigi Rizzo 	 * a rxsync.
276f9790aebSLuigi Rizzo 	 */
277f9790aebSLuigi Rizzo 	struct mbuf **tx_pool;
27817885a7bSLuigi Rizzo 	// u_int nr_ntc;		/* Emulation of a next-to-clean RX ring pointer. */
27917885a7bSLuigi Rizzo 	struct mbq rx_queue;            /* intercepted rx mbufs. */
28017885a7bSLuigi Rizzo 
28117885a7bSLuigi Rizzo 	uint32_t	ring_id;	/* debugging */
28217885a7bSLuigi Rizzo 	char name[64];			/* diagnostic */
283f9790aebSLuigi Rizzo 
284*f0ea3689SLuigi Rizzo 	int (*nm_sync)(struct netmap_kring *kring, int flags);
285*f0ea3689SLuigi Rizzo 
286*f0ea3689SLuigi Rizzo #ifdef WITH_PIPES
287*f0ea3689SLuigi Rizzo 	struct netmap_kring *pipe;
288*f0ea3689SLuigi Rizzo 	struct netmap_ring *save_ring;
289*f0ea3689SLuigi Rizzo #endif /* WITH_PIPES */
290*f0ea3689SLuigi Rizzo 
2912157a17cSLuigi Rizzo } __attribute__((__aligned__(64)));
29268b8534bSLuigi Rizzo 
293ce3ee1e7SLuigi Rizzo 
294ce3ee1e7SLuigi Rizzo /* return the next index, with wraparound */
295ce3ee1e7SLuigi Rizzo static inline uint32_t
296ce3ee1e7SLuigi Rizzo nm_next(uint32_t i, uint32_t lim)
297ce3ee1e7SLuigi Rizzo {
298ce3ee1e7SLuigi Rizzo 	return unlikely (i == lim) ? 0 : i + 1;
299ce3ee1e7SLuigi Rizzo }
300ce3ee1e7SLuigi Rizzo 
30117885a7bSLuigi Rizzo 
30217885a7bSLuigi Rizzo /* return the previous index, with wraparound */
30317885a7bSLuigi Rizzo static inline uint32_t
30417885a7bSLuigi Rizzo nm_prev(uint32_t i, uint32_t lim)
30517885a7bSLuigi Rizzo {
30617885a7bSLuigi Rizzo 	return unlikely (i == 0) ? lim : i - 1;
30717885a7bSLuigi Rizzo }
30817885a7bSLuigi Rizzo 
30917885a7bSLuigi Rizzo 
310ce3ee1e7SLuigi Rizzo /*
311ce3ee1e7SLuigi Rizzo  *
312ce3ee1e7SLuigi Rizzo  * Here is the layout for the Rx and Tx rings.
313ce3ee1e7SLuigi Rizzo 
314ce3ee1e7SLuigi Rizzo        RxRING                            TxRING
315ce3ee1e7SLuigi Rizzo 
316ce3ee1e7SLuigi Rizzo       +-----------------+            +-----------------+
317ce3ee1e7SLuigi Rizzo       |                 |            |                 |
318ce3ee1e7SLuigi Rizzo       |XXX free slot XXX|            |XXX free slot XXX|
319ce3ee1e7SLuigi Rizzo       +-----------------+            +-----------------+
32017885a7bSLuigi Rizzo head->| owned by user   |<-hwcur     | not sent to nic |<-hwcur
32117885a7bSLuigi Rizzo       |                 |            | yet             |
32217885a7bSLuigi Rizzo       +-----------------+            |                 |
32317885a7bSLuigi Rizzo  cur->| available to    |            |                 |
32417885a7bSLuigi Rizzo       | user, not read  |            +-----------------+
32517885a7bSLuigi Rizzo       | yet             |       cur->| (being          |
32617885a7bSLuigi Rizzo       |                 |            |  prepared)      |
327ce3ee1e7SLuigi Rizzo       |                 |            |                 |
32817885a7bSLuigi Rizzo       +-----------------+            +     ------      +
32917885a7bSLuigi Rizzo tail->|                 |<-hwtail    |                 |<-hwlease
33017885a7bSLuigi Rizzo       | (being          | ...        |                 | ...
33117885a7bSLuigi Rizzo       |  prepared)      | ...        |                 | ...
33217885a7bSLuigi Rizzo       +-----------------+ ...        |                 | ...
33317885a7bSLuigi Rizzo       |                 |<-hwlease   +-----------------+
33417885a7bSLuigi Rizzo       |                 |      tail->|                 |<-hwtail
335ce3ee1e7SLuigi Rizzo       |                 |            |                 |
336ce3ee1e7SLuigi Rizzo       |                 |            |                 |
337ce3ee1e7SLuigi Rizzo       |                 |            |                 |
338ce3ee1e7SLuigi Rizzo       +-----------------+            +-----------------+
339ce3ee1e7SLuigi Rizzo 
34017885a7bSLuigi Rizzo  * The cur/tail (user view) and hwcur/hwtail (kernel view)
341ce3ee1e7SLuigi Rizzo  * are used in the normal operation of the card.
342ce3ee1e7SLuigi Rizzo  *
343ce3ee1e7SLuigi Rizzo  * When a ring is the output of a switch port (Rx ring for
344ce3ee1e7SLuigi Rizzo  * a VALE port, Tx ring for the host stack or NIC), slots
345ce3ee1e7SLuigi Rizzo  * are reserved in blocks through 'hwlease' which points
346ce3ee1e7SLuigi Rizzo  * to the next unused slot.
34717885a7bSLuigi Rizzo  * On an Rx ring, hwlease is always after hwtail,
34817885a7bSLuigi Rizzo  * and completions cause hwtail to advance.
34917885a7bSLuigi Rizzo  * On a Tx ring, hwlease is always between cur and hwtail,
350ce3ee1e7SLuigi Rizzo  * and completions cause cur to advance.
351ce3ee1e7SLuigi Rizzo  *
352ce3ee1e7SLuigi Rizzo  * nm_kr_space() returns the maximum number of slots that
353ce3ee1e7SLuigi Rizzo  * can be assigned.
354ce3ee1e7SLuigi Rizzo  * nm_kr_lease() reserves the required number of buffers,
355ce3ee1e7SLuigi Rizzo  *    advances nkr_hwlease and also returns an entry in
356ce3ee1e7SLuigi Rizzo  *    a circular array where completions should be reported.
357ce3ee1e7SLuigi Rizzo  */
358ce3ee1e7SLuigi Rizzo 
359ce3ee1e7SLuigi Rizzo 
360ce3ee1e7SLuigi Rizzo 
361f9790aebSLuigi Rizzo enum txrx { NR_RX = 0, NR_TX = 1 };
362ce3ee1e7SLuigi Rizzo 
36368b8534bSLuigi Rizzo /*
364f9790aebSLuigi Rizzo  * The "struct netmap_adapter" extends the "struct adapter"
365f9790aebSLuigi Rizzo  * (or equivalent) device descriptor.
366f9790aebSLuigi Rizzo  * It contains all base fields needed to support netmap operation.
367f9790aebSLuigi Rizzo  * There are in fact different types of netmap adapters
368f9790aebSLuigi Rizzo  * (native, generic, VALE switch...) so a netmap_adapter is
369f9790aebSLuigi Rizzo  * just the first field in the derived type.
37068b8534bSLuigi Rizzo  */
37168b8534bSLuigi Rizzo struct netmap_adapter {
3728241616dSLuigi Rizzo 	/*
3738241616dSLuigi Rizzo 	 * On linux we do not have a good way to tell if an interface
374f9790aebSLuigi Rizzo 	 * is netmap-capable. So we always use the following trick:
3758241616dSLuigi Rizzo 	 * NA(ifp) points here, and the first entry (which hopefully
3768241616dSLuigi Rizzo 	 * always exists and is at least 32 bits) contains a magic
3778241616dSLuigi Rizzo 	 * value which we can use to detect that the interface is good.
3788241616dSLuigi Rizzo 	 */
3798241616dSLuigi Rizzo 	uint32_t magic;
380f9790aebSLuigi Rizzo 	uint32_t na_flags;	/* enabled, and other flags */
3818241616dSLuigi Rizzo #define NAF_SKIP_INTR	1	/* use the regular interrupt handler.
3828241616dSLuigi Rizzo 				 * useful during initialization
3838241616dSLuigi Rizzo 				 */
384f18be576SLuigi Rizzo #define NAF_SW_ONLY	2	/* forward packets only to sw adapter */
385ce3ee1e7SLuigi Rizzo #define NAF_BDG_MAYSLEEP 4	/* the bridge is allowed to sleep when
386ce3ee1e7SLuigi Rizzo 				 * forwarding packets coming from this
387ce3ee1e7SLuigi Rizzo 				 * interface
388ce3ee1e7SLuigi Rizzo 				 */
389ce3ee1e7SLuigi Rizzo #define NAF_MEM_OWNER	8	/* the adapter is responsible for the
390ce3ee1e7SLuigi Rizzo 				 * deallocation of the memory allocator
391ce3ee1e7SLuigi Rizzo 				 */
392f9790aebSLuigi Rizzo #define NAF_NATIVE_ON   16      /* the adapter is native and the attached
393f9790aebSLuigi Rizzo 				 * interface is in netmap mode
394f9790aebSLuigi Rizzo 				 */
395f9790aebSLuigi Rizzo #define	NAF_NETMAP_ON	32	/* netmap is active (either native or
396f9790aebSLuigi Rizzo 				 * emulated. Where possible (e.g. FreeBSD)
397f9790aebSLuigi Rizzo 				 * IFCAP_NETMAP also mirrors this flag.
398f9790aebSLuigi Rizzo 				 */
399*f0ea3689SLuigi Rizzo #define NAF_HOST_RINGS  64	/* the adapter supports the host rings */
400f9790aebSLuigi Rizzo 	int active_fds; /* number of user-space descriptors using this
40168b8534bSLuigi Rizzo 			 interface, which is equal to the number of
40268b8534bSLuigi Rizzo 			 struct netmap_if objs in the mapped region. */
40368b8534bSLuigi Rizzo 
40424e57ec9SEd Maste 	u_int num_rx_rings; /* number of adapter receive rings */
40524e57ec9SEd Maste 	u_int num_tx_rings; /* number of adapter transmit rings */
40668b8534bSLuigi Rizzo 
40768b8534bSLuigi Rizzo 	u_int num_tx_desc; /* number of descriptor in each queue */
40868b8534bSLuigi Rizzo 	u_int num_rx_desc;
40968b8534bSLuigi Rizzo 
41068b8534bSLuigi Rizzo 	/* tx_rings and rx_rings are private but allocated
41168b8534bSLuigi Rizzo 	 * as a contiguous chunk of memory. Each array has
41268b8534bSLuigi Rizzo 	 * N+1 entries, for the adapter queues and for the host queue.
41368b8534bSLuigi Rizzo 	 */
41468b8534bSLuigi Rizzo 	struct netmap_kring *tx_rings; /* array of TX rings. */
41568b8534bSLuigi Rizzo 	struct netmap_kring *rx_rings; /* array of RX rings. */
41617885a7bSLuigi Rizzo 
417f9790aebSLuigi Rizzo 	void *tailroom;		       /* space below the rings array */
418f9790aebSLuigi Rizzo 				       /* (used for leases) */
419f9790aebSLuigi Rizzo 
42068b8534bSLuigi Rizzo 
42164ae02c3SLuigi Rizzo 	NM_SELINFO_T tx_si, rx_si;	/* global wait queues */
42264ae02c3SLuigi Rizzo 
423*f0ea3689SLuigi Rizzo 	/* count users of the global wait queues */
424*f0ea3689SLuigi Rizzo 	int tx_si_users, rx_si_users;
425*f0ea3689SLuigi Rizzo 
42668b8534bSLuigi Rizzo 	/* copy of if_qflush and if_transmit pointers, to intercept
42768b8534bSLuigi Rizzo 	 * packets from the network stack when netmap is active.
42868b8534bSLuigi Rizzo 	 */
42968b8534bSLuigi Rizzo 	int     (*if_transmit)(struct ifnet *, struct mbuf *);
43068b8534bSLuigi Rizzo 
43117885a7bSLuigi Rizzo 	/* copy of if_input for netmap_send_up() */
43217885a7bSLuigi Rizzo 	void     (*if_input)(struct ifnet *, struct mbuf *);
43317885a7bSLuigi Rizzo 
43468b8534bSLuigi Rizzo 	/* references to the ifnet and device routines, used by
43568b8534bSLuigi Rizzo 	 * the generic netmap functions.
43668b8534bSLuigi Rizzo 	 */
43768b8534bSLuigi Rizzo 	struct ifnet *ifp; /* adapter is ifp->if_softc */
43868b8534bSLuigi Rizzo 
43917885a7bSLuigi Rizzo 	/*---- callbacks for this netmap adapter -----*/
44017885a7bSLuigi Rizzo 	/*
44117885a7bSLuigi Rizzo 	 * nm_dtor() is the cleanup routine called when destroying
44217885a7bSLuigi Rizzo 	 *	the adapter.
44317885a7bSLuigi Rizzo 	 *
44417885a7bSLuigi Rizzo 	 * nm_register() is called on NIOCREGIF and close() to enter
44517885a7bSLuigi Rizzo 	 *	or exit netmap mode on the NIC
44617885a7bSLuigi Rizzo 	 *
44717885a7bSLuigi Rizzo 	 * nm_txsync() pushes packets to the underlying hw/switch
44817885a7bSLuigi Rizzo 	 *
44917885a7bSLuigi Rizzo 	 * nm_rxsync() collects packets from the underlying hw/switch
45017885a7bSLuigi Rizzo 	 *
45117885a7bSLuigi Rizzo 	 * nm_config() returns configuration information from the OS
45217885a7bSLuigi Rizzo 	 *
453*f0ea3689SLuigi Rizzo 	 * nm_krings_create() create and init the krings array
454*f0ea3689SLuigi Rizzo 	 * 	(the array layout must conform to the description
455*f0ea3689SLuigi Rizzo 	 * 	found above the definition of netmap_krings_create)
45617885a7bSLuigi Rizzo 	 *
457*f0ea3689SLuigi Rizzo 	 * nm_krings_delete() cleanup and delete the kring array
45817885a7bSLuigi Rizzo 	 *
45917885a7bSLuigi Rizzo 	 * nm_notify() is used to act after data have become available.
46017885a7bSLuigi Rizzo 	 *	For hw devices this is typically a selwakeup(),
46117885a7bSLuigi Rizzo 	 *	but for NIC/host ports attached to a switch (or vice-versa)
46217885a7bSLuigi Rizzo 	 *	we also need to invoke the 'txsync' code downstream.
46317885a7bSLuigi Rizzo 	 */
46417885a7bSLuigi Rizzo 
465f9790aebSLuigi Rizzo 	/* private cleanup */
466f9790aebSLuigi Rizzo 	void (*nm_dtor)(struct netmap_adapter *);
4671a26580eSLuigi Rizzo 
468f9790aebSLuigi Rizzo 	int (*nm_register)(struct netmap_adapter *, int onoff);
469ce3ee1e7SLuigi Rizzo 
470f9790aebSLuigi Rizzo 	int (*nm_txsync)(struct netmap_adapter *, u_int ring, int flags);
471f9790aebSLuigi Rizzo 	int (*nm_rxsync)(struct netmap_adapter *, u_int ring, int flags);
472ce3ee1e7SLuigi Rizzo #define NAF_FORCE_READ    1
473ce3ee1e7SLuigi Rizzo #define NAF_FORCE_RECLAIM 2
474ae10d1afSLuigi Rizzo 	/* return configuration information */
475f9790aebSLuigi Rizzo 	int (*nm_config)(struct netmap_adapter *,
476f9790aebSLuigi Rizzo 		u_int *txr, u_int *txd, u_int *rxr, u_int *rxd);
477f9790aebSLuigi Rizzo 	int (*nm_krings_create)(struct netmap_adapter *);
478f9790aebSLuigi Rizzo 	void (*nm_krings_delete)(struct netmap_adapter *);
479f9790aebSLuigi Rizzo 	int (*nm_notify)(struct netmap_adapter *,
480f9790aebSLuigi Rizzo 		u_int ring, enum txrx, int flags);
481f9790aebSLuigi Rizzo #define NAF_DISABLE_NOTIFY 8
482f9790aebSLuigi Rizzo 
483f9790aebSLuigi Rizzo 	/* standard refcount to control the lifetime of the adapter
484f9790aebSLuigi Rizzo 	 * (it should be equal to the lifetime of the corresponding ifp)
485f9790aebSLuigi Rizzo 	 */
486f9790aebSLuigi Rizzo 	int na_refcount;
487f9790aebSLuigi Rizzo 
488f9790aebSLuigi Rizzo 	/* memory allocator (opaque)
489f9790aebSLuigi Rizzo 	 * We also cache a pointer to the lut_entry for translating
490f9790aebSLuigi Rizzo 	 * buffer addresses, and the total number of buffers.
491f9790aebSLuigi Rizzo 	 */
492f9790aebSLuigi Rizzo  	struct netmap_mem_d *nm_mem;
493f9790aebSLuigi Rizzo 	struct lut_entry *na_lut;
494f9790aebSLuigi Rizzo 	uint32_t na_lut_objtotal;	/* max buffer index */
495f9790aebSLuigi Rizzo 
496f9790aebSLuigi Rizzo 	/* used internally. If non-null, the interface cannot be bound
497f9790aebSLuigi Rizzo 	 * from userspace
498f9790aebSLuigi Rizzo 	 */
499f9790aebSLuigi Rizzo 	void *na_private;
500*f0ea3689SLuigi Rizzo 
501*f0ea3689SLuigi Rizzo #ifdef WITH_PIPES
502*f0ea3689SLuigi Rizzo 	struct netmap_pipe_adapter **na_pipes;
503*f0ea3689SLuigi Rizzo 	int na_next_pipe;
504*f0ea3689SLuigi Rizzo 	int na_max_pipes;
505*f0ea3689SLuigi Rizzo #endif /* WITH_PIPES */
506f9790aebSLuigi Rizzo };
507f9790aebSLuigi Rizzo 
50817885a7bSLuigi Rizzo 
509f9790aebSLuigi Rizzo /*
510f9790aebSLuigi Rizzo  * If the NIC is owned by the kernel
511f9790aebSLuigi Rizzo  * (i.e., bridge), neither another bridge nor user can use it;
512f9790aebSLuigi Rizzo  * if the NIC is owned by a user, only users can share it.
513f9790aebSLuigi Rizzo  * Evaluation must be done under NMG_LOCK().
514f9790aebSLuigi Rizzo  */
515f9790aebSLuigi Rizzo #define NETMAP_OWNED_BY_KERN(na)	(na->na_private)
516f9790aebSLuigi Rizzo #define NETMAP_OWNED_BY_ANY(na) \
517f9790aebSLuigi Rizzo 	(NETMAP_OWNED_BY_KERN(na) || (na->active_fds > 0))
518f9790aebSLuigi Rizzo 
519f9790aebSLuigi Rizzo 
520f9790aebSLuigi Rizzo /*
521f9790aebSLuigi Rizzo  * derived netmap adapters for various types of ports
522f9790aebSLuigi Rizzo  */
523f9790aebSLuigi Rizzo struct netmap_vp_adapter {	/* VALE software port */
524f9790aebSLuigi Rizzo 	struct netmap_adapter up;
525f196ce38SLuigi Rizzo 
526849bec0eSLuigi Rizzo 	/*
527849bec0eSLuigi Rizzo 	 * Bridge support:
528849bec0eSLuigi Rizzo 	 *
529849bec0eSLuigi Rizzo 	 * bdg_port is the port number used in the bridge;
530f18be576SLuigi Rizzo 	 * na_bdg points to the bridge this NA is attached to.
531849bec0eSLuigi Rizzo 	 */
532f196ce38SLuigi Rizzo 	int bdg_port;
533f18be576SLuigi Rizzo 	struct nm_bridge *na_bdg;
534f9790aebSLuigi Rizzo 	int retry;
535f9790aebSLuigi Rizzo 
536*f0ea3689SLuigi Rizzo 	/* Offset of ethernet header for each packet. */
537*f0ea3689SLuigi Rizzo 	u_int virt_hdr_len;
538*f0ea3689SLuigi Rizzo 	/* Maximum Frame Size, used in bdg_mismatch_datapath() */
539*f0ea3689SLuigi Rizzo 	u_int mfs;
540f9790aebSLuigi Rizzo };
541f9790aebSLuigi Rizzo 
54217885a7bSLuigi Rizzo 
543f9790aebSLuigi Rizzo struct netmap_hw_adapter {	/* physical device */
544f9790aebSLuigi Rizzo 	struct netmap_adapter up;
545f9790aebSLuigi Rizzo 
546f9790aebSLuigi Rizzo 	struct net_device_ops nm_ndo;	// XXX linux only
547f9790aebSLuigi Rizzo };
548f9790aebSLuigi Rizzo 
549*f0ea3689SLuigi Rizzo /* Mitigation support. */
550*f0ea3689SLuigi Rizzo struct nm_generic_mit {
551*f0ea3689SLuigi Rizzo 	struct hrtimer mit_timer;
552*f0ea3689SLuigi Rizzo 	int mit_pending;
553*f0ea3689SLuigi Rizzo 	struct netmap_adapter *mit_na;  /* backpointer */
554*f0ea3689SLuigi Rizzo };
55517885a7bSLuigi Rizzo 
55617885a7bSLuigi Rizzo struct netmap_generic_adapter {	/* emulated device */
557f9790aebSLuigi Rizzo 	struct netmap_hw_adapter up;
558f9790aebSLuigi Rizzo 
559f9790aebSLuigi Rizzo 	/* Pointer to a previously used netmap adapter. */
560f9790aebSLuigi Rizzo 	struct netmap_adapter *prev;
561f9790aebSLuigi Rizzo 
562f9790aebSLuigi Rizzo 	/* generic netmap adapters support:
563f9790aebSLuigi Rizzo 	 * a net_device_ops struct overrides ndo_select_queue(),
564f9790aebSLuigi Rizzo 	 * save_if_input saves the if_input hook (FreeBSD),
565*f0ea3689SLuigi Rizzo 	 * mit implements rx interrupt mitigation,
566f9790aebSLuigi Rizzo 	 */
567f9790aebSLuigi Rizzo 	struct net_device_ops generic_ndo;
568f9790aebSLuigi Rizzo 	void (*save_if_input)(struct ifnet *, struct mbuf *);
569f9790aebSLuigi Rizzo 
570*f0ea3689SLuigi Rizzo 	struct nm_generic_mit *mit;
57117885a7bSLuigi Rizzo #ifdef linux
57217885a7bSLuigi Rizzo         netdev_tx_t (*save_start_xmit)(struct mbuf *, struct ifnet *);
57317885a7bSLuigi Rizzo #endif
574f9790aebSLuigi Rizzo };
575f9790aebSLuigi Rizzo 
576*f0ea3689SLuigi Rizzo static __inline int
577*f0ea3689SLuigi Rizzo netmap_real_tx_rings(struct netmap_adapter *na)
578*f0ea3689SLuigi Rizzo {
579*f0ea3689SLuigi Rizzo 	return na->num_tx_rings + !!(na->na_flags & NAF_HOST_RINGS);
580*f0ea3689SLuigi Rizzo }
581*f0ea3689SLuigi Rizzo 
582*f0ea3689SLuigi Rizzo static __inline int
583*f0ea3689SLuigi Rizzo netmap_real_rx_rings(struct netmap_adapter *na)
584*f0ea3689SLuigi Rizzo {
585*f0ea3689SLuigi Rizzo 	return na->num_rx_rings + !!(na->na_flags & NAF_HOST_RINGS);
586*f0ea3689SLuigi Rizzo }
587*f0ea3689SLuigi Rizzo 
588f9790aebSLuigi Rizzo #ifdef WITH_VALE
589f9790aebSLuigi Rizzo 
59017885a7bSLuigi Rizzo /*
59117885a7bSLuigi Rizzo  * Bridge wrapper for non VALE ports attached to a VALE switch.
592f9790aebSLuigi Rizzo  *
59317885a7bSLuigi Rizzo  * The real device must already have its own netmap adapter (hwna).
59417885a7bSLuigi Rizzo  * The bridge wrapper and the hwna adapter share the same set of
59517885a7bSLuigi Rizzo  * netmap rings and buffers, but they have two separate sets of
59617885a7bSLuigi Rizzo  * krings descriptors, with tx/rx meanings swapped:
597f9790aebSLuigi Rizzo  *
598f9790aebSLuigi Rizzo  *                                  netmap
599f9790aebSLuigi Rizzo  *           bwrap     krings       rings      krings      hwna
600f9790aebSLuigi Rizzo  *         +------+   +------+     +-----+    +------+   +------+
601f9790aebSLuigi Rizzo  *         |tx_rings->|      |\   /|     |----|      |<-tx_rings|
602f9790aebSLuigi Rizzo  *         |      |   +------+ \ / +-----+    +------+   |      |
603f9790aebSLuigi Rizzo  *         |      |             X                        |      |
604f9790aebSLuigi Rizzo  *         |      |            / \                       |      |
605f9790aebSLuigi Rizzo  *         |      |   +------+/   \+-----+    +------+   |      |
606f9790aebSLuigi Rizzo  *         |rx_rings->|      |     |     |----|      |<-rx_rings|
607f9790aebSLuigi Rizzo  *         |      |   +------+     +-----+    +------+   |      |
608f9790aebSLuigi Rizzo  *         +------+                                      +------+
609f9790aebSLuigi Rizzo  *
61017885a7bSLuigi Rizzo  * - packets coming from the bridge go to the brwap rx rings,
61117885a7bSLuigi Rizzo  *   which are also the hwna tx rings.  The bwrap notify callback
61217885a7bSLuigi Rizzo  *   will then complete the hwna tx (see netmap_bwrap_notify).
613f9790aebSLuigi Rizzo  *
61417885a7bSLuigi Rizzo  * - packets coming from the outside go to the hwna rx rings,
61517885a7bSLuigi Rizzo  *   which are also the bwrap tx rings.  The (overwritten) hwna
61617885a7bSLuigi Rizzo  *   notify method will then complete the bridge tx
61717885a7bSLuigi Rizzo  *   (see netmap_bwrap_intr_notify).
618f9790aebSLuigi Rizzo  *
61917885a7bSLuigi Rizzo  *   The bridge wrapper may optionally connect the hwna 'host' rings
62017885a7bSLuigi Rizzo  *   to the bridge. This is done by using a second port in the
62117885a7bSLuigi Rizzo  *   bridge and connecting it to the 'host' netmap_vp_adapter
62217885a7bSLuigi Rizzo  *   contained in the netmap_bwrap_adapter. The brwap host adapter
62317885a7bSLuigi Rizzo  *   cross-links the hwna host rings in the same way as shown above.
62417885a7bSLuigi Rizzo  *
62517885a7bSLuigi Rizzo  * - packets coming from the bridge and directed to the host stack
62617885a7bSLuigi Rizzo  *   are handled by the bwrap host notify callback
62717885a7bSLuigi Rizzo  *   (see netmap_bwrap_host_notify)
62817885a7bSLuigi Rizzo  *
62917885a7bSLuigi Rizzo  * - packets coming from the host stack are still handled by the
63017885a7bSLuigi Rizzo  *   overwritten hwna notify callback (netmap_bwrap_intr_notify),
63117885a7bSLuigi Rizzo  *   but are diverted to the host adapter depending on the ring number.
632f9790aebSLuigi Rizzo  *
633f9790aebSLuigi Rizzo  */
634f9790aebSLuigi Rizzo struct netmap_bwrap_adapter {
635f9790aebSLuigi Rizzo 	struct netmap_vp_adapter up;
636f9790aebSLuigi Rizzo 	struct netmap_vp_adapter host;  /* for host rings */
637f9790aebSLuigi Rizzo 	struct netmap_adapter *hwna;	/* the underlying device */
638f9790aebSLuigi Rizzo 
639f9790aebSLuigi Rizzo 	/* backup of the hwna notify callback */
640f9790aebSLuigi Rizzo 	int (*save_notify)(struct netmap_adapter *,
641f9790aebSLuigi Rizzo 			u_int ring, enum txrx, int flags);
64217885a7bSLuigi Rizzo 
64317885a7bSLuigi Rizzo 	/*
64417885a7bSLuigi Rizzo 	 * When we attach a physical interface to the bridge, we
645f18be576SLuigi Rizzo 	 * allow the controlling process to terminate, so we need
646f18be576SLuigi Rizzo 	 * a place to store the netmap_priv_d data structure.
64717885a7bSLuigi Rizzo 	 * This is only done when physical interfaces
64817885a7bSLuigi Rizzo 	 * are attached to a bridge.
649f18be576SLuigi Rizzo 	 */
650f18be576SLuigi Rizzo 	struct netmap_priv_d *na_kpriv;
65168b8534bSLuigi Rizzo };
65268b8534bSLuigi Rizzo 
653f9790aebSLuigi Rizzo 
65417885a7bSLuigi Rizzo #endif /* WITH_VALE */
655ce3ee1e7SLuigi Rizzo 
656*f0ea3689SLuigi Rizzo #ifdef WITH_PIPES
657*f0ea3689SLuigi Rizzo 
658*f0ea3689SLuigi Rizzo #define NM_MAXPIPES 	64	/* max number of pipes per adapter */
659*f0ea3689SLuigi Rizzo 
660*f0ea3689SLuigi Rizzo struct netmap_pipe_adapter {
661*f0ea3689SLuigi Rizzo 	struct netmap_adapter up;
662*f0ea3689SLuigi Rizzo 
663*f0ea3689SLuigi Rizzo 	u_int id; 	/* pipe identifier */
664*f0ea3689SLuigi Rizzo 	int role;	/* either NR_REG_PIPE_MASTER or NR_REG_PIPE_SLAVE */
665*f0ea3689SLuigi Rizzo 
666*f0ea3689SLuigi Rizzo 	struct netmap_adapter *parent; /* adapter that owns the memory */
667*f0ea3689SLuigi Rizzo 	struct netmap_pipe_adapter *peer; /* the other end of the pipe */
668*f0ea3689SLuigi Rizzo 	int peer_ref;		/* 1 iff we are holding a ref to the peer */
669*f0ea3689SLuigi Rizzo 
670*f0ea3689SLuigi Rizzo 	u_int parent_slot; /* index in the parent pipe array */
671*f0ea3689SLuigi Rizzo };
672*f0ea3689SLuigi Rizzo 
673*f0ea3689SLuigi Rizzo #endif /* WITH_PIPES */
674*f0ea3689SLuigi Rizzo 
67517885a7bSLuigi Rizzo 
67617885a7bSLuigi Rizzo /* return slots reserved to rx clients; used in drivers */
67717885a7bSLuigi Rizzo static inline uint32_t
67817885a7bSLuigi Rizzo nm_kr_rxspace(struct netmap_kring *k)
67917885a7bSLuigi Rizzo {
68017885a7bSLuigi Rizzo 	int space = k->nr_hwtail - k->nr_hwcur;
681ce3ee1e7SLuigi Rizzo 	if (space < 0)
682ce3ee1e7SLuigi Rizzo 		space += k->nkr_num_slots;
68317885a7bSLuigi Rizzo 	ND("preserving %d rx slots %d -> %d", space, k->nr_hwcur, k->nr_hwtail);
68417885a7bSLuigi Rizzo 
685ce3ee1e7SLuigi Rizzo 	return space;
686ce3ee1e7SLuigi Rizzo }
687ce3ee1e7SLuigi Rizzo 
688ce3ee1e7SLuigi Rizzo 
68917885a7bSLuigi Rizzo /* True if no space in the tx ring. only valid after txsync_prologue */
69017885a7bSLuigi Rizzo static inline int
69117885a7bSLuigi Rizzo nm_kr_txempty(struct netmap_kring *kring)
692ce3ee1e7SLuigi Rizzo {
69317885a7bSLuigi Rizzo 	return kring->rcur == kring->nr_hwtail;
694f9790aebSLuigi Rizzo }
695f9790aebSLuigi Rizzo 
696ce3ee1e7SLuigi Rizzo 
697ce3ee1e7SLuigi Rizzo /*
698f9790aebSLuigi Rizzo  * protect against multiple threads using the same ring.
699f9790aebSLuigi Rizzo  * also check that the ring has not been stopped.
700f9790aebSLuigi Rizzo  * We only care for 0 or !=0 as a return code.
70168b8534bSLuigi Rizzo  */
702f9790aebSLuigi Rizzo #define NM_KR_BUSY	1
703f9790aebSLuigi Rizzo #define NM_KR_STOPPED	2
70468b8534bSLuigi Rizzo 
70517885a7bSLuigi Rizzo 
706f9790aebSLuigi Rizzo static __inline void nm_kr_put(struct netmap_kring *kr)
707f9790aebSLuigi Rizzo {
708f9790aebSLuigi Rizzo 	NM_ATOMIC_CLEAR(&kr->nr_busy);
709f9790aebSLuigi Rizzo }
710f9790aebSLuigi Rizzo 
71117885a7bSLuigi Rizzo 
712f9790aebSLuigi Rizzo static __inline int nm_kr_tryget(struct netmap_kring *kr)
713f9790aebSLuigi Rizzo {
714f9790aebSLuigi Rizzo 	/* check a first time without taking the lock
715f9790aebSLuigi Rizzo 	 * to avoid starvation for nm_kr_get()
716f9790aebSLuigi Rizzo 	 */
717f9790aebSLuigi Rizzo 	if (unlikely(kr->nkr_stopped)) {
718f9790aebSLuigi Rizzo 		ND("ring %p stopped (%d)", kr, kr->nkr_stopped);
719f9790aebSLuigi Rizzo 		return NM_KR_STOPPED;
720f9790aebSLuigi Rizzo 	}
721f9790aebSLuigi Rizzo 	if (unlikely(NM_ATOMIC_TEST_AND_SET(&kr->nr_busy)))
722f9790aebSLuigi Rizzo 		return NM_KR_BUSY;
723f9790aebSLuigi Rizzo 	/* check a second time with lock held */
724f9790aebSLuigi Rizzo 	if (unlikely(kr->nkr_stopped)) {
725f9790aebSLuigi Rizzo 		ND("ring %p stopped (%d)", kr, kr->nkr_stopped);
726f9790aebSLuigi Rizzo 		nm_kr_put(kr);
727f9790aebSLuigi Rizzo 		return NM_KR_STOPPED;
728f9790aebSLuigi Rizzo 	}
729f9790aebSLuigi Rizzo 	return 0;
730f9790aebSLuigi Rizzo }
73168b8534bSLuigi Rizzo 
732849bec0eSLuigi Rizzo 
73368b8534bSLuigi Rizzo /*
73417885a7bSLuigi Rizzo  * The following functions are used by individual drivers to
73568b8534bSLuigi Rizzo  * support netmap operation.
73668b8534bSLuigi Rizzo  *
73768b8534bSLuigi Rizzo  * netmap_attach() initializes a struct netmap_adapter, allocating the
73868b8534bSLuigi Rizzo  * 	struct netmap_ring's and the struct selinfo.
73968b8534bSLuigi Rizzo  *
74068b8534bSLuigi Rizzo  * netmap_detach() frees the memory allocated by netmap_attach().
74168b8534bSLuigi Rizzo  *
742ce3ee1e7SLuigi Rizzo  * netmap_transmit() replaces the if_transmit routine of the interface,
74368b8534bSLuigi Rizzo  *	and is used to intercept packets coming from the stack.
74468b8534bSLuigi Rizzo  *
74568b8534bSLuigi Rizzo  * netmap_load_map/netmap_reload_map are helper routines to set/reset
74668b8534bSLuigi Rizzo  *	the dmamap for a packet buffer
74768b8534bSLuigi Rizzo  *
74868b8534bSLuigi Rizzo  * netmap_reset() is a helper routine to be called in the driver
74968b8534bSLuigi Rizzo  *	when reinitializing a ring.
75068b8534bSLuigi Rizzo  */
751f9790aebSLuigi Rizzo int netmap_attach(struct netmap_adapter *);
752f9790aebSLuigi Rizzo int netmap_attach_common(struct netmap_adapter *);
753f9790aebSLuigi Rizzo void netmap_detach_common(struct netmap_adapter *na);
75468b8534bSLuigi Rizzo void netmap_detach(struct ifnet *);
755ce3ee1e7SLuigi Rizzo int netmap_transmit(struct ifnet *, struct mbuf *);
75668b8534bSLuigi Rizzo struct netmap_slot *netmap_reset(struct netmap_adapter *na,
757ce3ee1e7SLuigi Rizzo 	enum txrx tx, u_int n, u_int new_cur);
75868b8534bSLuigi Rizzo int netmap_ring_reinit(struct netmap_kring *);
75968b8534bSLuigi Rizzo 
76017885a7bSLuigi Rizzo /* default functions to handle rx/tx interrupts */
76117885a7bSLuigi Rizzo int netmap_rx_irq(struct ifnet *, u_int, u_int *);
76217885a7bSLuigi Rizzo #define netmap_tx_irq(_n, _q) netmap_rx_irq(_n, _q, NULL)
76317885a7bSLuigi Rizzo void netmap_common_irq(struct ifnet *, u_int, u_int *work_done);
76417885a7bSLuigi Rizzo 
76517885a7bSLuigi Rizzo void netmap_disable_all_rings(struct ifnet *);
76617885a7bSLuigi Rizzo void netmap_enable_all_rings(struct ifnet *);
76717885a7bSLuigi Rizzo void netmap_disable_ring(struct netmap_kring *kr);
76817885a7bSLuigi Rizzo 
76917885a7bSLuigi Rizzo 
77017885a7bSLuigi Rizzo /* set/clear native flags and if_transmit/netdev_ops */
771f9790aebSLuigi Rizzo static inline void
772f9790aebSLuigi Rizzo nm_set_native_flags(struct netmap_adapter *na)
773f9790aebSLuigi Rizzo {
774f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
775ce3ee1e7SLuigi Rizzo 
776f9790aebSLuigi Rizzo 	na->na_flags |= (NAF_NATIVE_ON | NAF_NETMAP_ON);
777f9790aebSLuigi Rizzo #ifdef IFCAP_NETMAP /* or FreeBSD ? */
778f9790aebSLuigi Rizzo 	ifp->if_capenable |= IFCAP_NETMAP;
779f9790aebSLuigi Rizzo #endif
780f9790aebSLuigi Rizzo #ifdef __FreeBSD__
781f9790aebSLuigi Rizzo 	na->if_transmit = ifp->if_transmit;
782f9790aebSLuigi Rizzo 	ifp->if_transmit = netmap_transmit;
783f9790aebSLuigi Rizzo #else
784f9790aebSLuigi Rizzo 	na->if_transmit = (void *)ifp->netdev_ops;
785f9790aebSLuigi Rizzo 	ifp->netdev_ops = &((struct netmap_hw_adapter *)na)->nm_ndo;
786f9790aebSLuigi Rizzo #endif
787f9790aebSLuigi Rizzo }
788f9790aebSLuigi Rizzo 
78917885a7bSLuigi Rizzo 
790f9790aebSLuigi Rizzo static inline void
791f9790aebSLuigi Rizzo nm_clear_native_flags(struct netmap_adapter *na)
792f9790aebSLuigi Rizzo {
793f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
794f9790aebSLuigi Rizzo 
795f9790aebSLuigi Rizzo #ifdef __FreeBSD__
796f9790aebSLuigi Rizzo 	ifp->if_transmit = na->if_transmit;
797f9790aebSLuigi Rizzo #else
798f9790aebSLuigi Rizzo 	ifp->netdev_ops = (void *)na->if_transmit;
799f9790aebSLuigi Rizzo #endif
800f9790aebSLuigi Rizzo 	na->na_flags &= ~(NAF_NATIVE_ON | NAF_NETMAP_ON);
801f9790aebSLuigi Rizzo #ifdef IFCAP_NETMAP /* or FreeBSD ? */
802f9790aebSLuigi Rizzo 	ifp->if_capenable &= ~IFCAP_NETMAP;
803f9790aebSLuigi Rizzo #endif
804f9790aebSLuigi Rizzo }
805f9790aebSLuigi Rizzo 
806f9790aebSLuigi Rizzo 
807f9790aebSLuigi Rizzo /*
80817885a7bSLuigi Rizzo  * validates parameters in the ring/kring, returns a value for head
80917885a7bSLuigi Rizzo  * If any error, returns ring_size to force a reinit.
81017885a7bSLuigi Rizzo  */
81117885a7bSLuigi Rizzo uint32_t nm_txsync_prologue(struct netmap_kring *);
81217885a7bSLuigi Rizzo 
81317885a7bSLuigi Rizzo 
81417885a7bSLuigi Rizzo /*
81517885a7bSLuigi Rizzo  * validates parameters in the ring/kring, returns a value for head,
816f9790aebSLuigi Rizzo  * and the 'reserved' value in the argument.
81717885a7bSLuigi Rizzo  * If any error, returns ring_size lim to force a reinit.
818f9790aebSLuigi Rizzo  */
81917885a7bSLuigi Rizzo uint32_t nm_rxsync_prologue(struct netmap_kring *);
82017885a7bSLuigi Rizzo 
821f9790aebSLuigi Rizzo 
822f9790aebSLuigi Rizzo /*
82317885a7bSLuigi Rizzo  * update kring and ring at the end of txsync.
824f9790aebSLuigi Rizzo  */
825f9790aebSLuigi Rizzo static inline void
82617885a7bSLuigi Rizzo nm_txsync_finalize(struct netmap_kring *kring)
827f9790aebSLuigi Rizzo {
828*f0ea3689SLuigi Rizzo 	/* update ring tail to what the kernel knows */
82917885a7bSLuigi Rizzo 	kring->ring->tail = kring->rtail = kring->nr_hwtail;
830f9790aebSLuigi Rizzo 
83117885a7bSLuigi Rizzo 	/* note, head/rhead/hwcur might be behind cur/rcur
83217885a7bSLuigi Rizzo 	 * if no carrier
83317885a7bSLuigi Rizzo 	 */
83417885a7bSLuigi Rizzo 	ND(5, "%s now hwcur %d hwtail %d head %d cur %d tail %d",
83517885a7bSLuigi Rizzo 		kring->name, kring->nr_hwcur, kring->nr_hwtail,
83617885a7bSLuigi Rizzo 		kring->rhead, kring->rcur, kring->rtail);
837f9790aebSLuigi Rizzo }
838f9790aebSLuigi Rizzo 
83917885a7bSLuigi Rizzo 
84017885a7bSLuigi Rizzo /*
84117885a7bSLuigi Rizzo  * update kring and ring at the end of rxsync
84217885a7bSLuigi Rizzo  */
84317885a7bSLuigi Rizzo static inline void
84417885a7bSLuigi Rizzo nm_rxsync_finalize(struct netmap_kring *kring)
84517885a7bSLuigi Rizzo {
84617885a7bSLuigi Rizzo 	/* tell userspace that there might be new packets */
84717885a7bSLuigi Rizzo 	//struct netmap_ring *ring = kring->ring;
84817885a7bSLuigi Rizzo 	ND("head %d cur %d tail %d -> %d", ring->head, ring->cur, ring->tail,
84917885a7bSLuigi Rizzo 		kring->nr_hwtail);
85017885a7bSLuigi Rizzo 	kring->ring->tail = kring->rtail = kring->nr_hwtail;
85117885a7bSLuigi Rizzo 	/* make a copy of the state for next round */
85217885a7bSLuigi Rizzo 	kring->rhead = kring->ring->head;
85317885a7bSLuigi Rizzo 	kring->rcur = kring->ring->cur;
85417885a7bSLuigi Rizzo }
85517885a7bSLuigi Rizzo 
85617885a7bSLuigi Rizzo 
857f9790aebSLuigi Rizzo /* check/fix address and len in tx rings */
858f9790aebSLuigi Rizzo #if 1 /* debug version */
859f9790aebSLuigi Rizzo #define	NM_CHECK_ADDR_LEN(_a, _l)	do {				\
860f9790aebSLuigi Rizzo 	if (_a == netmap_buffer_base || _l > NETMAP_BUF_SIZE) {		\
861f9790aebSLuigi Rizzo 		RD(5, "bad addr/len ring %d slot %d idx %d len %d",	\
862f9790aebSLuigi Rizzo 			ring_nr, nm_i, slot->buf_idx, len);		\
863f9790aebSLuigi Rizzo 		if (_l > NETMAP_BUF_SIZE)				\
864f9790aebSLuigi Rizzo 			_l = NETMAP_BUF_SIZE;				\
865f9790aebSLuigi Rizzo 	} } while (0)
866f9790aebSLuigi Rizzo #else /* no debug version */
867f9790aebSLuigi Rizzo #define	NM_CHECK_ADDR_LEN(_a, _l)	do {				\
868f9790aebSLuigi Rizzo 		if (_l > NETMAP_BUF_SIZE)				\
869f9790aebSLuigi Rizzo 			_l = NETMAP_BUF_SIZE;				\
870f9790aebSLuigi Rizzo 	} while (0)
871f9790aebSLuigi Rizzo #endif
872f9790aebSLuigi Rizzo 
873f9790aebSLuigi Rizzo 
874f9790aebSLuigi Rizzo /*---------------------------------------------------------------*/
875f9790aebSLuigi Rizzo /*
876f9790aebSLuigi Rizzo  * Support routines to be used with the VALE switch
877f9790aebSLuigi Rizzo  */
878f9790aebSLuigi Rizzo int netmap_update_config(struct netmap_adapter *na);
879*f0ea3689SLuigi Rizzo int netmap_krings_create(struct netmap_adapter *na, u_int tailroom);
880f9790aebSLuigi Rizzo void netmap_krings_delete(struct netmap_adapter *na);
88117885a7bSLuigi Rizzo int netmap_rxsync_from_host(struct netmap_adapter *na, struct thread *td, void *pwait);
88217885a7bSLuigi Rizzo 
883f9790aebSLuigi Rizzo 
884f9790aebSLuigi Rizzo struct netmap_if *
885f9790aebSLuigi Rizzo netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na,
886*f0ea3689SLuigi Rizzo 	uint16_t ringid, uint32_t flags, int *err);
887f9790aebSLuigi Rizzo 
888f9790aebSLuigi Rizzo 
889f9790aebSLuigi Rizzo 
890f9790aebSLuigi Rizzo u_int nm_bound_var(u_int *v, u_int dflt, u_int lo, u_int hi, const char *msg);
891f9790aebSLuigi Rizzo int netmap_get_na(struct nmreq *nmr, struct netmap_adapter **na, int create);
892f9790aebSLuigi Rizzo int netmap_get_hw_na(struct ifnet *ifp, struct netmap_adapter **na);
893f9790aebSLuigi Rizzo 
89417885a7bSLuigi Rizzo 
895f9790aebSLuigi Rizzo #ifdef WITH_VALE
896f18be576SLuigi Rizzo /*
89717885a7bSLuigi Rizzo  * The following bridge-related functions are used by other
89817885a7bSLuigi Rizzo  * kernel modules.
89917885a7bSLuigi Rizzo  *
90017885a7bSLuigi Rizzo  * VALE only supports unicast or broadcast. The lookup
901f18be576SLuigi Rizzo  * function can return 0 .. NM_BDG_MAXPORTS-1 for regular ports,
902f18be576SLuigi Rizzo  * NM_BDG_MAXPORTS for broadcast, NM_BDG_MAXPORTS+1 for unknown.
903f18be576SLuigi Rizzo  * XXX in practice "unknown" might be handled same as broadcast.
904f18be576SLuigi Rizzo  */
905f9790aebSLuigi Rizzo typedef u_int (*bdg_lookup_fn_t)(char *buf, u_int len,
906f9790aebSLuigi Rizzo 		uint8_t *ring_nr, struct netmap_vp_adapter *);
907f9790aebSLuigi Rizzo u_int netmap_bdg_learning(char *, u_int, uint8_t *,
908f9790aebSLuigi Rizzo 		struct netmap_vp_adapter *);
909f9790aebSLuigi Rizzo 
910f9790aebSLuigi Rizzo #define	NM_BDG_MAXPORTS		254	/* up to 254 */
911f18be576SLuigi Rizzo #define	NM_BDG_BROADCAST	NM_BDG_MAXPORTS
912f18be576SLuigi Rizzo #define	NM_BDG_NOPORT		(NM_BDG_MAXPORTS+1)
913f18be576SLuigi Rizzo 
914f9790aebSLuigi Rizzo #define	NM_NAME			"vale"	/* prefix for bridge port name */
915f9790aebSLuigi Rizzo 
916f9790aebSLuigi Rizzo 
917f9790aebSLuigi Rizzo /* these are redefined in case of no VALE support */
918f9790aebSLuigi Rizzo int netmap_get_bdg_na(struct nmreq *nmr, struct netmap_adapter **na, int create);
919f9790aebSLuigi Rizzo void netmap_init_bridges(void);
920f9790aebSLuigi Rizzo int netmap_bdg_ctl(struct nmreq *nmr, bdg_lookup_fn_t func);
921f9790aebSLuigi Rizzo 
922f9790aebSLuigi Rizzo #else /* !WITH_VALE */
923f9790aebSLuigi Rizzo #define	netmap_get_bdg_na(_1, _2, _3)	0
924f9790aebSLuigi Rizzo #define netmap_init_bridges(_1)
925f9790aebSLuigi Rizzo #define	netmap_bdg_ctl(_1, _2)	EINVAL
926f9790aebSLuigi Rizzo #endif /* !WITH_VALE */
927f9790aebSLuigi Rizzo 
928*f0ea3689SLuigi Rizzo #ifdef WITH_PIPES
929*f0ea3689SLuigi Rizzo /* max number of pipes per device */
930*f0ea3689SLuigi Rizzo #define NM_MAXPIPES	64	/* XXX how many? */
931*f0ea3689SLuigi Rizzo /* in case of no error, returns the actual number of pipes in nmr->nr_arg1 */
932*f0ea3689SLuigi Rizzo int netmap_pipe_alloc(struct netmap_adapter *, struct nmreq *nmr);
933*f0ea3689SLuigi Rizzo void netmap_pipe_dealloc(struct netmap_adapter *);
934*f0ea3689SLuigi Rizzo int netmap_get_pipe_na(struct nmreq *nmr, struct netmap_adapter **na, int create);
935*f0ea3689SLuigi Rizzo #else /* !WITH_PIPES */
936*f0ea3689SLuigi Rizzo #define NM_MAXPIPES	0
937*f0ea3689SLuigi Rizzo #define netmap_pipe_alloc(_1, _2) 	EOPNOTSUPP
938*f0ea3689SLuigi Rizzo #define netmap_pipe_dealloc(_1)
939*f0ea3689SLuigi Rizzo #define netmap_get_pipe_na(_1, _2, _3)	0
940*f0ea3689SLuigi Rizzo #endif
941*f0ea3689SLuigi Rizzo 
942f9790aebSLuigi Rizzo /* Various prototypes */
943f9790aebSLuigi Rizzo int netmap_poll(struct cdev *dev, int events, struct thread *td);
944f9790aebSLuigi Rizzo int netmap_init(void);
945f9790aebSLuigi Rizzo void netmap_fini(void);
946f9790aebSLuigi Rizzo int netmap_get_memory(struct netmap_priv_d* p);
947f9790aebSLuigi Rizzo void netmap_dtor(void *data);
948f9790aebSLuigi Rizzo int netmap_dtor_locked(struct netmap_priv_d *priv);
949f9790aebSLuigi Rizzo 
950f9790aebSLuigi Rizzo int netmap_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td);
951f9790aebSLuigi Rizzo 
952f9790aebSLuigi Rizzo /* netmap_adapter creation/destruction */
953f9790aebSLuigi Rizzo #define NM_IFPNAME(ifp) ((ifp) ? (ifp)->if_xname : "zombie")
95417885a7bSLuigi Rizzo 
95517885a7bSLuigi Rizzo // #define NM_DEBUG_PUTGET 1
956f9790aebSLuigi Rizzo 
957f9790aebSLuigi Rizzo #ifdef NM_DEBUG_PUTGET
958f9790aebSLuigi Rizzo 
959f9790aebSLuigi Rizzo #define NM_DBG(f) __##f
960f9790aebSLuigi Rizzo 
961f9790aebSLuigi Rizzo void __netmap_adapter_get(struct netmap_adapter *na);
962f9790aebSLuigi Rizzo 
963f9790aebSLuigi Rizzo #define netmap_adapter_get(na) 				\
964f9790aebSLuigi Rizzo 	do {						\
965f9790aebSLuigi Rizzo 		struct netmap_adapter *__na = na;	\
966f9790aebSLuigi Rizzo 		D("getting %p:%s (%d)", __na, NM_IFPNAME(__na->ifp), __na->na_refcount);	\
967f9790aebSLuigi Rizzo 		__netmap_adapter_get(__na);		\
968f9790aebSLuigi Rizzo 	} while (0)
969f9790aebSLuigi Rizzo 
970f9790aebSLuigi Rizzo int __netmap_adapter_put(struct netmap_adapter *na);
971f9790aebSLuigi Rizzo 
972f9790aebSLuigi Rizzo #define netmap_adapter_put(na)				\
973fb25194fSLuigi Rizzo 	({						\
974f9790aebSLuigi Rizzo 		struct netmap_adapter *__na = na;	\
975f9790aebSLuigi Rizzo 		D("putting %p:%s (%d)", __na, NM_IFPNAME(__na->ifp), __na->na_refcount);	\
976f9790aebSLuigi Rizzo 		__netmap_adapter_put(__na);		\
977fb25194fSLuigi Rizzo 	})
978f9790aebSLuigi Rizzo 
979f9790aebSLuigi Rizzo #else /* !NM_DEBUG_PUTGET */
980f9790aebSLuigi Rizzo 
981f9790aebSLuigi Rizzo #define NM_DBG(f) f
982f9790aebSLuigi Rizzo void netmap_adapter_get(struct netmap_adapter *na);
983f9790aebSLuigi Rizzo int netmap_adapter_put(struct netmap_adapter *na);
984f9790aebSLuigi Rizzo 
985f9790aebSLuigi Rizzo #endif /* !NM_DEBUG_PUTGET */
986f9790aebSLuigi Rizzo 
987f9790aebSLuigi Rizzo 
98817885a7bSLuigi Rizzo /*
98917885a7bSLuigi Rizzo  * module variables
99017885a7bSLuigi Rizzo  */
991b3d53016SLuigi Rizzo extern u_int netmap_buf_size;
992849bec0eSLuigi Rizzo #define NETMAP_BUF_SIZE	netmap_buf_size	// XXX remove
99317885a7bSLuigi Rizzo extern int netmap_mitigate;	// XXX not really used
9945819da83SLuigi Rizzo extern int netmap_no_pendintr;
99517885a7bSLuigi Rizzo extern u_int netmap_total_buffers;	// global allocator
99617885a7bSLuigi Rizzo extern char *netmap_buffer_base;	// global allocator
99768b8534bSLuigi Rizzo extern int netmap_verbose;	// XXX debugging
99868b8534bSLuigi Rizzo enum {                                  /* verbose flags */
99968b8534bSLuigi Rizzo 	NM_VERB_ON = 1,                 /* generic verbose */
100068b8534bSLuigi Rizzo 	NM_VERB_HOST = 0x2,             /* verbose host stack */
100168b8534bSLuigi Rizzo 	NM_VERB_RXSYNC = 0x10,          /* verbose on rxsync/txsync */
100268b8534bSLuigi Rizzo 	NM_VERB_TXSYNC = 0x20,
100368b8534bSLuigi Rizzo 	NM_VERB_RXINTR = 0x100,         /* verbose on rx/tx intr (driver) */
100468b8534bSLuigi Rizzo 	NM_VERB_TXINTR = 0x200,
100568b8534bSLuigi Rizzo 	NM_VERB_NIC_RXSYNC = 0x1000,    /* verbose on rx/tx intr (driver) */
100668b8534bSLuigi Rizzo 	NM_VERB_NIC_TXSYNC = 0x2000,
100768b8534bSLuigi Rizzo };
100868b8534bSLuigi Rizzo 
1009f9790aebSLuigi Rizzo extern int netmap_txsync_retry;
1010f9790aebSLuigi Rizzo extern int netmap_generic_mit;
1011f9790aebSLuigi Rizzo extern int netmap_generic_ringsize;
1012*f0ea3689SLuigi Rizzo extern int netmap_generic_rings;
1013f9790aebSLuigi Rizzo 
101468b8534bSLuigi Rizzo /*
1015d0c7b075SLuigi Rizzo  * NA returns a pointer to the struct netmap adapter from the ifp,
1016d0c7b075SLuigi Rizzo  * WNA is used to write it.
101768b8534bSLuigi Rizzo  */
1018d0c7b075SLuigi Rizzo #ifndef WNA
1019d0c7b075SLuigi Rizzo #define	WNA(_ifp)	(_ifp)->if_pspare[0]
1020d0c7b075SLuigi Rizzo #endif
1021d0c7b075SLuigi Rizzo #define	NA(_ifp)	((struct netmap_adapter *)WNA(_ifp))
102268b8534bSLuigi Rizzo 
10238241616dSLuigi Rizzo /*
10248241616dSLuigi Rizzo  * Macros to determine if an interface is netmap capable or netmap enabled.
10258241616dSLuigi Rizzo  * See the magic field in struct netmap_adapter.
10268241616dSLuigi Rizzo  */
10278241616dSLuigi Rizzo #ifdef __FreeBSD__
10288241616dSLuigi Rizzo /*
10298241616dSLuigi Rizzo  * on FreeBSD just use if_capabilities and if_capenable.
10308241616dSLuigi Rizzo  */
10318241616dSLuigi Rizzo #define NETMAP_CAPABLE(ifp)	(NA(ifp) &&		\
10328241616dSLuigi Rizzo 	(ifp)->if_capabilities & IFCAP_NETMAP )
10338241616dSLuigi Rizzo 
10348241616dSLuigi Rizzo #define	NETMAP_SET_CAPABLE(ifp)				\
10358241616dSLuigi Rizzo 	(ifp)->if_capabilities |= IFCAP_NETMAP
10368241616dSLuigi Rizzo 
10378241616dSLuigi Rizzo #else	/* linux */
10388241616dSLuigi Rizzo 
10398241616dSLuigi Rizzo /*
10408241616dSLuigi Rizzo  * on linux:
10418241616dSLuigi Rizzo  * we check if NA(ifp) is set and its first element has a related
10428241616dSLuigi Rizzo  * magic value. The capenable is within the struct netmap_adapter.
10438241616dSLuigi Rizzo  */
10448241616dSLuigi Rizzo #define	NETMAP_MAGIC	0x52697a7a
10458241616dSLuigi Rizzo 
10468241616dSLuigi Rizzo #define NETMAP_CAPABLE(ifp)	(NA(ifp) &&		\
10478241616dSLuigi Rizzo 	((uint32_t)(uintptr_t)NA(ifp) ^ NA(ifp)->magic) == NETMAP_MAGIC )
10488241616dSLuigi Rizzo 
10498241616dSLuigi Rizzo #define	NETMAP_SET_CAPABLE(ifp)				\
10508241616dSLuigi Rizzo 	NA(ifp)->magic = ((uint32_t)(uintptr_t)NA(ifp)) ^ NETMAP_MAGIC
10518241616dSLuigi Rizzo 
10528241616dSLuigi Rizzo #endif	/* linux */
105368b8534bSLuigi Rizzo 
1054f196ce38SLuigi Rizzo #ifdef __FreeBSD__
1055f9790aebSLuigi Rizzo 
105617885a7bSLuigi Rizzo /* Callback invoked by the dma machinery after a successful dmamap_load */
10576dba29a2SLuigi Rizzo static void netmap_dmamap_cb(__unused void *arg,
10586dba29a2SLuigi Rizzo     __unused bus_dma_segment_t * segs, __unused int nseg, __unused int error)
10596dba29a2SLuigi Rizzo {
10606dba29a2SLuigi Rizzo }
10616dba29a2SLuigi Rizzo 
10626dba29a2SLuigi Rizzo /* bus_dmamap_load wrapper: call aforementioned function if map != NULL.
10636dba29a2SLuigi Rizzo  * XXX can we do it without a callback ?
10646dba29a2SLuigi Rizzo  */
10656dba29a2SLuigi Rizzo static inline void
10666dba29a2SLuigi Rizzo netmap_load_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
10676dba29a2SLuigi Rizzo {
10686dba29a2SLuigi Rizzo 	if (map)
10696dba29a2SLuigi Rizzo 		bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE,
10706dba29a2SLuigi Rizzo 		    netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
10716dba29a2SLuigi Rizzo }
10726dba29a2SLuigi Rizzo 
10736dba29a2SLuigi Rizzo /* update the map when a buffer changes. */
10746dba29a2SLuigi Rizzo static inline void
10756dba29a2SLuigi Rizzo netmap_reload_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
10766dba29a2SLuigi Rizzo {
10776dba29a2SLuigi Rizzo 	if (map) {
10786dba29a2SLuigi Rizzo 		bus_dmamap_unload(tag, map);
10796dba29a2SLuigi Rizzo 		bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE,
10806dba29a2SLuigi Rizzo 		    netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
10816dba29a2SLuigi Rizzo 	}
10826dba29a2SLuigi Rizzo }
1083f9790aebSLuigi Rizzo 
1084f196ce38SLuigi Rizzo #else /* linux */
1085f196ce38SLuigi Rizzo 
1086f196ce38SLuigi Rizzo /*
1087f196ce38SLuigi Rizzo  * XXX How do we redefine these functions:
1088f196ce38SLuigi Rizzo  *
1089f196ce38SLuigi Rizzo  * on linux we need
1090f196ce38SLuigi Rizzo  *	dma_map_single(&pdev->dev, virt_addr, len, direction)
1091f196ce38SLuigi Rizzo  *	dma_unmap_single(&adapter->pdev->dev, phys_addr, len, direction
1092f196ce38SLuigi Rizzo  * The len can be implicit (on netmap it is NETMAP_BUF_SIZE)
1093f196ce38SLuigi Rizzo  * unfortunately the direction is not, so we need to change
1094f196ce38SLuigi Rizzo  * something to have a cross API
1095f196ce38SLuigi Rizzo  */
1096f196ce38SLuigi Rizzo #define netmap_load_map(_t, _m, _b)
1097f196ce38SLuigi Rizzo #define netmap_reload_map(_t, _m, _b)
1098f196ce38SLuigi Rizzo #if 0
1099f196ce38SLuigi Rizzo 	struct e1000_buffer *buffer_info =  &tx_ring->buffer_info[l];
1100f196ce38SLuigi Rizzo 	/* set time_stamp *before* dma to help avoid a possible race */
1101f196ce38SLuigi Rizzo 	buffer_info->time_stamp = jiffies;
1102f196ce38SLuigi Rizzo 	buffer_info->mapped_as_page = false;
1103f196ce38SLuigi Rizzo 	buffer_info->length = len;
1104f196ce38SLuigi Rizzo 	//buffer_info->next_to_watch = l;
1105f196ce38SLuigi Rizzo 	/* reload dma map */
1106f196ce38SLuigi Rizzo 	dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
1107f196ce38SLuigi Rizzo 			NETMAP_BUF_SIZE, DMA_TO_DEVICE);
1108f196ce38SLuigi Rizzo 	buffer_info->dma = dma_map_single(&adapter->pdev->dev,
1109f196ce38SLuigi Rizzo 			addr, NETMAP_BUF_SIZE, DMA_TO_DEVICE);
1110f196ce38SLuigi Rizzo 
1111f196ce38SLuigi Rizzo 	if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
1112f196ce38SLuigi Rizzo 		D("dma mapping error");
1113f196ce38SLuigi Rizzo 		/* goto dma_error; See e1000_put_txbuf() */
1114f196ce38SLuigi Rizzo 		/* XXX reset */
1115f196ce38SLuigi Rizzo 	}
1116f196ce38SLuigi Rizzo 	tx_desc->buffer_addr = htole64(buffer_info->dma); //XXX
1117f196ce38SLuigi Rizzo 
1118f196ce38SLuigi Rizzo #endif
1119f196ce38SLuigi Rizzo 
1120f196ce38SLuigi Rizzo /*
1121f196ce38SLuigi Rizzo  * The bus_dmamap_sync() can be one of wmb() or rmb() depending on direction.
1122f196ce38SLuigi Rizzo  */
1123f196ce38SLuigi Rizzo #define bus_dmamap_sync(_a, _b, _c)
1124f196ce38SLuigi Rizzo 
1125f196ce38SLuigi Rizzo #endif /* linux */
11266dba29a2SLuigi Rizzo 
1127ce3ee1e7SLuigi Rizzo 
11285644ccecSLuigi Rizzo /*
11295644ccecSLuigi Rizzo  * functions to map NIC to KRING indexes (n2k) and vice versa (k2n)
11305644ccecSLuigi Rizzo  */
11315644ccecSLuigi Rizzo static inline int
113264ae02c3SLuigi Rizzo netmap_idx_n2k(struct netmap_kring *kr, int idx)
11335644ccecSLuigi Rizzo {
113464ae02c3SLuigi Rizzo 	int n = kr->nkr_num_slots;
113564ae02c3SLuigi Rizzo 	idx += kr->nkr_hwofs;
113664ae02c3SLuigi Rizzo 	if (idx < 0)
113764ae02c3SLuigi Rizzo 		return idx + n;
113864ae02c3SLuigi Rizzo 	else if (idx < n)
113964ae02c3SLuigi Rizzo 		return idx;
11405644ccecSLuigi Rizzo 	else
114164ae02c3SLuigi Rizzo 		return idx - n;
11425644ccecSLuigi Rizzo }
11435644ccecSLuigi Rizzo 
11445644ccecSLuigi Rizzo 
11455644ccecSLuigi Rizzo static inline int
114664ae02c3SLuigi Rizzo netmap_idx_k2n(struct netmap_kring *kr, int idx)
11475644ccecSLuigi Rizzo {
114864ae02c3SLuigi Rizzo 	int n = kr->nkr_num_slots;
114964ae02c3SLuigi Rizzo 	idx -= kr->nkr_hwofs;
115064ae02c3SLuigi Rizzo 	if (idx < 0)
115164ae02c3SLuigi Rizzo 		return idx + n;
115264ae02c3SLuigi Rizzo 	else if (idx < n)
115364ae02c3SLuigi Rizzo 		return idx;
11545644ccecSLuigi Rizzo 	else
115564ae02c3SLuigi Rizzo 		return idx - n;
11565644ccecSLuigi Rizzo }
11575644ccecSLuigi Rizzo 
11585644ccecSLuigi Rizzo 
1159d76bf4ffSLuigi Rizzo /* Entries of the look-up table. */
1160d76bf4ffSLuigi Rizzo struct lut_entry {
1161d76bf4ffSLuigi Rizzo 	void *vaddr;		/* virtual address. */
1162849bec0eSLuigi Rizzo 	vm_paddr_t paddr;	/* physical address. */
1163d76bf4ffSLuigi Rizzo };
1164d76bf4ffSLuigi Rizzo 
1165d76bf4ffSLuigi Rizzo struct netmap_obj_pool;
1166d76bf4ffSLuigi Rizzo extern struct lut_entry *netmap_buffer_lut;
1167d76bf4ffSLuigi Rizzo #define NMB_VA(i)	(netmap_buffer_lut[i].vaddr)
1168d76bf4ffSLuigi Rizzo #define NMB_PA(i)	(netmap_buffer_lut[i].paddr)
1169d76bf4ffSLuigi Rizzo 
117068b8534bSLuigi Rizzo /*
11716e10c8b8SLuigi Rizzo  * NMB return the virtual address of a buffer (buffer 0 on bad index)
11726e10c8b8SLuigi Rizzo  * PNMB also fills the physical address
117368b8534bSLuigi Rizzo  */
11746e10c8b8SLuigi Rizzo static inline void *
117568b8534bSLuigi Rizzo NMB(struct netmap_slot *slot)
117668b8534bSLuigi Rizzo {
117768b8534bSLuigi Rizzo 	uint32_t i = slot->buf_idx;
1178f196ce38SLuigi Rizzo 	return (unlikely(i >= netmap_total_buffers)) ?  NMB_VA(0) : NMB_VA(i);
117968b8534bSLuigi Rizzo }
118068b8534bSLuigi Rizzo 
11816e10c8b8SLuigi Rizzo static inline void *
11826e10c8b8SLuigi Rizzo PNMB(struct netmap_slot *slot, uint64_t *pp)
11836e10c8b8SLuigi Rizzo {
11846e10c8b8SLuigi Rizzo 	uint32_t i = slot->buf_idx;
1185d76bf4ffSLuigi Rizzo 	void *ret = (i >= netmap_total_buffers) ? NMB_VA(0) : NMB_VA(i);
11862579e2d7SLuigi Rizzo 
1187d76bf4ffSLuigi Rizzo 	*pp = (i >= netmap_total_buffers) ? NMB_PA(0) : NMB_PA(i);
11886e10c8b8SLuigi Rizzo 	return ret;
11896e10c8b8SLuigi Rizzo }
11906e10c8b8SLuigi Rizzo 
1191f9790aebSLuigi Rizzo /* Generic version of NMB, which uses device-specific memory. */
1192f9790aebSLuigi Rizzo static inline void *
1193f9790aebSLuigi Rizzo BDG_NMB(struct netmap_adapter *na, struct netmap_slot *slot)
1194f9790aebSLuigi Rizzo {
1195f9790aebSLuigi Rizzo 	struct lut_entry *lut = na->na_lut;
1196f9790aebSLuigi Rizzo 	uint32_t i = slot->buf_idx;
1197f9790aebSLuigi Rizzo 	return (unlikely(i >= na->na_lut_objtotal)) ?
1198f9790aebSLuigi Rizzo 		lut[0].vaddr : lut[i].vaddr;
1199f9790aebSLuigi Rizzo }
1200f9790aebSLuigi Rizzo 
1201ce3ee1e7SLuigi Rizzo 
1202ce3ee1e7SLuigi Rizzo 
1203f9790aebSLuigi Rizzo void netmap_txsync_to_host(struct netmap_adapter *na);
1204f9790aebSLuigi Rizzo 
1205f9790aebSLuigi Rizzo 
120617885a7bSLuigi Rizzo /*
120717885a7bSLuigi Rizzo  * Structure associated to each thread which registered an interface.
1208f9790aebSLuigi Rizzo  *
1209f9790aebSLuigi Rizzo  * The first 4 fields of this structure are written by NIOCREGIF and
1210f9790aebSLuigi Rizzo  * read by poll() and NIOC?XSYNC.
121117885a7bSLuigi Rizzo  *
121217885a7bSLuigi Rizzo  * There is low contention among writers (a correct user program
121317885a7bSLuigi Rizzo  * should have none) and among writers and readers, so we use a
121417885a7bSLuigi Rizzo  * single global lock to protect the structure initialization;
121517885a7bSLuigi Rizzo  * since initialization involves the allocation of memory,
121617885a7bSLuigi Rizzo  * we reuse the memory allocator lock.
121717885a7bSLuigi Rizzo  *
1218f9790aebSLuigi Rizzo  * Read access to the structure is lock free. Readers must check that
1219f9790aebSLuigi Rizzo  * np_nifp is not NULL before using the other fields.
122017885a7bSLuigi Rizzo  * If np_nifp is NULL initialization has not been performed,
122117885a7bSLuigi Rizzo  * so they should return an error to userspace.
1222f9790aebSLuigi Rizzo  *
1223f9790aebSLuigi Rizzo  * The ref_done field is used to regulate access to the refcount in the
1224f9790aebSLuigi Rizzo  * memory allocator. The refcount must be incremented at most once for
1225f9790aebSLuigi Rizzo  * each open("/dev/netmap"). The increment is performed by the first
1226f9790aebSLuigi Rizzo  * function that calls netmap_get_memory() (currently called by
1227f9790aebSLuigi Rizzo  * mmap(), NIOCGINFO and NIOCREGIF).
1228f9790aebSLuigi Rizzo  * If the refcount is incremented, it is then decremented when the
1229f9790aebSLuigi Rizzo  * private structure is destroyed.
1230f9790aebSLuigi Rizzo  */
1231f9790aebSLuigi Rizzo struct netmap_priv_d {
1232f9790aebSLuigi Rizzo 	struct netmap_if * volatile np_nifp;	/* netmap if descriptor. */
1233f9790aebSLuigi Rizzo 
1234f9790aebSLuigi Rizzo 	struct netmap_adapter	*np_na;
1235*f0ea3689SLuigi Rizzo 	uint32_t	np_flags;	/* from the ioctl */
1236*f0ea3689SLuigi Rizzo 	u_int		np_txqfirst, np_txqlast; /* range of tx rings to scan */
1237*f0ea3689SLuigi Rizzo 	u_int		np_rxqfirst, np_rxqlast; /* range of rx rings to scan */
1238*f0ea3689SLuigi Rizzo 	uint16_t	np_txpoll;	/* XXX and also np_rxpoll ? */
1239f9790aebSLuigi Rizzo 
1240f9790aebSLuigi Rizzo 	struct netmap_mem_d     *np_mref;	/* use with NMG_LOCK held */
1241f9790aebSLuigi Rizzo 	/* np_refcount is only used on FreeBSD */
1242f9790aebSLuigi Rizzo 	int		np_refcount;	/* use with NMG_LOCK held */
1243*f0ea3689SLuigi Rizzo 
1244*f0ea3689SLuigi Rizzo 	/* pointers to the selinfo to be used for selrecord.
1245*f0ea3689SLuigi Rizzo 	 * Either the local or the global one depending on the
1246*f0ea3689SLuigi Rizzo 	 * number of rings.
1247*f0ea3689SLuigi Rizzo 	 */
1248*f0ea3689SLuigi Rizzo 	NM_SELINFO_T *np_rxsi, *np_txsi;
1249*f0ea3689SLuigi Rizzo 	struct thread	*np_td;		/* kqueue, just debugging */
1250f9790aebSLuigi Rizzo };
1251f9790aebSLuigi Rizzo 
1252f9790aebSLuigi Rizzo 
1253f9790aebSLuigi Rizzo /*
1254f9790aebSLuigi Rizzo  * generic netmap emulation for devices that do not have
1255f9790aebSLuigi Rizzo  * native netmap support.
1256f9790aebSLuigi Rizzo  */
1257f9790aebSLuigi Rizzo int generic_netmap_attach(struct ifnet *ifp);
1258f9790aebSLuigi Rizzo 
1259f9790aebSLuigi Rizzo int netmap_catch_rx(struct netmap_adapter *na, int intercept);
1260f9790aebSLuigi Rizzo void generic_rx_handler(struct ifnet *ifp, struct mbuf *m);;
126117885a7bSLuigi Rizzo void netmap_catch_tx(struct netmap_generic_adapter *na, int enable);
1262f9790aebSLuigi Rizzo int generic_xmit_frame(struct ifnet *ifp, struct mbuf *m, void *addr, u_int len, u_int ring_nr);
1263f9790aebSLuigi Rizzo int generic_find_num_desc(struct ifnet *ifp, u_int *tx, u_int *rx);
1264f9790aebSLuigi Rizzo void generic_find_num_queues(struct ifnet *ifp, u_int *txq, u_int *rxq);
1265f9790aebSLuigi Rizzo 
1266f9790aebSLuigi Rizzo /*
1267f9790aebSLuigi Rizzo  * netmap_mitigation API. This is used by the generic adapter
1268f9790aebSLuigi Rizzo  * to reduce the number of interrupt requests/selwakeup
1269f9790aebSLuigi Rizzo  * to clients on incoming packets.
1270f9790aebSLuigi Rizzo  */
1271*f0ea3689SLuigi Rizzo void netmap_mitigation_init(struct nm_generic_mit *mit, struct netmap_adapter *na);
1272*f0ea3689SLuigi Rizzo void netmap_mitigation_start(struct nm_generic_mit *mit);
1273*f0ea3689SLuigi Rizzo void netmap_mitigation_restart(struct nm_generic_mit *mit);
1274*f0ea3689SLuigi Rizzo int netmap_mitigation_active(struct nm_generic_mit *mit);
1275*f0ea3689SLuigi Rizzo void netmap_mitigation_cleanup(struct nm_generic_mit *mit);
1276*f0ea3689SLuigi Rizzo 
1277*f0ea3689SLuigi Rizzo 
1278*f0ea3689SLuigi Rizzo 
1279*f0ea3689SLuigi Rizzo /* Shared declarations for the VALE switch. */
1280*f0ea3689SLuigi Rizzo 
1281*f0ea3689SLuigi Rizzo /*
1282*f0ea3689SLuigi Rizzo  * Each transmit queue accumulates a batch of packets into
1283*f0ea3689SLuigi Rizzo  * a structure before forwarding. Packets to the same
1284*f0ea3689SLuigi Rizzo  * destination are put in a list using ft_next as a link field.
1285*f0ea3689SLuigi Rizzo  * ft_frags and ft_next are valid only on the first fragment.
1286*f0ea3689SLuigi Rizzo  */
1287*f0ea3689SLuigi Rizzo struct nm_bdg_fwd {	/* forwarding entry for a bridge */
1288*f0ea3689SLuigi Rizzo 	void *ft_buf;		/* netmap or indirect buffer */
1289*f0ea3689SLuigi Rizzo 	uint8_t ft_frags;	/* how many fragments (only on 1st frag) */
1290*f0ea3689SLuigi Rizzo 	uint8_t _ft_port;	/* dst port (unused) */
1291*f0ea3689SLuigi Rizzo 	uint16_t ft_flags;	/* flags, e.g. indirect */
1292*f0ea3689SLuigi Rizzo 	uint16_t ft_len;	/* src fragment len */
1293*f0ea3689SLuigi Rizzo 	uint16_t ft_next;	/* next packet to same destination */
1294*f0ea3689SLuigi Rizzo };
1295*f0ea3689SLuigi Rizzo 
1296*f0ea3689SLuigi Rizzo /* struct 'virtio_net_hdr' from linux. */
1297*f0ea3689SLuigi Rizzo struct nm_vnet_hdr {
1298*f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_F_NEEDS_CSUM     1	/* Use csum_start, csum_offset */
1299*f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_F_DATA_VALID    2	/* Csum is valid */
1300*f0ea3689SLuigi Rizzo     uint8_t flags;
1301*f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_NONE         0       /* Not a GSO frame */
1302*f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_TCPV4        1       /* GSO frame, IPv4 TCP (TSO) */
1303*f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_UDP          3       /* GSO frame, IPv4 UDP (UFO) */
1304*f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_TCPV6        4       /* GSO frame, IPv6 TCP */
1305*f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_ECN          0x80    /* TCP has ECN set */
1306*f0ea3689SLuigi Rizzo     uint8_t gso_type;
1307*f0ea3689SLuigi Rizzo     uint16_t hdr_len;
1308*f0ea3689SLuigi Rizzo     uint16_t gso_size;
1309*f0ea3689SLuigi Rizzo     uint16_t csum_start;
1310*f0ea3689SLuigi Rizzo     uint16_t csum_offset;
1311*f0ea3689SLuigi Rizzo };
1312*f0ea3689SLuigi Rizzo 
1313*f0ea3689SLuigi Rizzo #define WORST_CASE_GSO_HEADER	(14+40+60)  /* IPv6 + TCP */
1314*f0ea3689SLuigi Rizzo 
1315*f0ea3689SLuigi Rizzo /* Private definitions for IPv4, IPv6, UDP and TCP headers. */
1316*f0ea3689SLuigi Rizzo 
1317*f0ea3689SLuigi Rizzo struct nm_iphdr {
1318*f0ea3689SLuigi Rizzo 	uint8_t		version_ihl;
1319*f0ea3689SLuigi Rizzo 	uint8_t		tos;
1320*f0ea3689SLuigi Rizzo 	uint16_t	tot_len;
1321*f0ea3689SLuigi Rizzo 	uint16_t	id;
1322*f0ea3689SLuigi Rizzo 	uint16_t	frag_off;
1323*f0ea3689SLuigi Rizzo 	uint8_t		ttl;
1324*f0ea3689SLuigi Rizzo 	uint8_t		protocol;
1325*f0ea3689SLuigi Rizzo 	uint16_t	check;
1326*f0ea3689SLuigi Rizzo 	uint32_t	saddr;
1327*f0ea3689SLuigi Rizzo 	uint32_t	daddr;
1328*f0ea3689SLuigi Rizzo 	/*The options start here. */
1329*f0ea3689SLuigi Rizzo };
1330*f0ea3689SLuigi Rizzo 
1331*f0ea3689SLuigi Rizzo struct nm_tcphdr {
1332*f0ea3689SLuigi Rizzo 	uint16_t	source;
1333*f0ea3689SLuigi Rizzo 	uint16_t	dest;
1334*f0ea3689SLuigi Rizzo 	uint32_t	seq;
1335*f0ea3689SLuigi Rizzo 	uint32_t	ack_seq;
1336*f0ea3689SLuigi Rizzo 	uint8_t		doff;  /* Data offset + Reserved */
1337*f0ea3689SLuigi Rizzo 	uint8_t		flags;
1338*f0ea3689SLuigi Rizzo 	uint16_t	window;
1339*f0ea3689SLuigi Rizzo 	uint16_t	check;
1340*f0ea3689SLuigi Rizzo 	uint16_t	urg_ptr;
1341*f0ea3689SLuigi Rizzo };
1342*f0ea3689SLuigi Rizzo 
1343*f0ea3689SLuigi Rizzo struct nm_udphdr {
1344*f0ea3689SLuigi Rizzo 	uint16_t	source;
1345*f0ea3689SLuigi Rizzo 	uint16_t	dest;
1346*f0ea3689SLuigi Rizzo 	uint16_t	len;
1347*f0ea3689SLuigi Rizzo 	uint16_t	check;
1348*f0ea3689SLuigi Rizzo };
1349*f0ea3689SLuigi Rizzo 
1350*f0ea3689SLuigi Rizzo struct nm_ipv6hdr {
1351*f0ea3689SLuigi Rizzo 	uint8_t		priority_version;
1352*f0ea3689SLuigi Rizzo 	uint8_t		flow_lbl[3];
1353*f0ea3689SLuigi Rizzo 
1354*f0ea3689SLuigi Rizzo 	uint16_t	payload_len;
1355*f0ea3689SLuigi Rizzo 	uint8_t		nexthdr;
1356*f0ea3689SLuigi Rizzo 	uint8_t		hop_limit;
1357*f0ea3689SLuigi Rizzo 
1358*f0ea3689SLuigi Rizzo 	uint8_t		saddr[16];
1359*f0ea3689SLuigi Rizzo 	uint8_t		daddr[16];
1360*f0ea3689SLuigi Rizzo };
1361*f0ea3689SLuigi Rizzo 
1362*f0ea3689SLuigi Rizzo /* Type used to store a checksum (in host byte order) that hasn't been
1363*f0ea3689SLuigi Rizzo  * folded yet.
1364*f0ea3689SLuigi Rizzo  */
1365*f0ea3689SLuigi Rizzo #define rawsum_t uint32_t
1366*f0ea3689SLuigi Rizzo 
1367*f0ea3689SLuigi Rizzo rawsum_t nm_csum_raw(uint8_t *data, size_t len, rawsum_t cur_sum);
1368*f0ea3689SLuigi Rizzo uint16_t nm_csum_ipv4(struct nm_iphdr *iph);
1369*f0ea3689SLuigi Rizzo void nm_csum_tcpudp_ipv4(struct nm_iphdr *iph, void *data,
1370*f0ea3689SLuigi Rizzo 		      size_t datalen, uint16_t *check);
1371*f0ea3689SLuigi Rizzo void nm_csum_tcpudp_ipv6(struct nm_ipv6hdr *ip6h, void *data,
1372*f0ea3689SLuigi Rizzo 		      size_t datalen, uint16_t *check);
1373*f0ea3689SLuigi Rizzo uint16_t nm_csum_fold(rawsum_t cur_sum);
1374*f0ea3689SLuigi Rizzo 
1375*f0ea3689SLuigi Rizzo void bdg_mismatch_datapath(struct netmap_vp_adapter *na,
1376*f0ea3689SLuigi Rizzo 			   struct netmap_vp_adapter *dst_na,
1377*f0ea3689SLuigi Rizzo 			   struct nm_bdg_fwd *ft_p, struct netmap_ring *ring,
1378*f0ea3689SLuigi Rizzo 			   u_int *j, u_int lim, u_int *howmany);
1379f9790aebSLuigi Rizzo 
138068b8534bSLuigi Rizzo #endif /* _NET_NETMAP_KERN_H_ */
1381