1*522d5c66SAsim Jamshed /*
2*522d5c66SAsim Jamshed  * Copyright (C) 2011-2014 Matteo Landi, Luigi Rizzo. All rights reserved.
3*522d5c66SAsim Jamshed  *
4*522d5c66SAsim Jamshed  * Redistribution and use in source and binary forms, with or without
5*522d5c66SAsim Jamshed  * modification, are permitted provided that the following conditions
6*522d5c66SAsim Jamshed  * are met:
7*522d5c66SAsim Jamshed  *
8*522d5c66SAsim Jamshed  *   1. Redistributions of source code must retain the above copyright
9*522d5c66SAsim Jamshed  *      notice, this list of conditions and the following disclaimer.
10*522d5c66SAsim Jamshed  *   2. Redistributions in binary form must reproduce the above copyright
11*522d5c66SAsim Jamshed  *      notice, this list of conditions and the following disclaimer in the
12*522d5c66SAsim Jamshed  *      documentation and/or other materials provided with the distribution.
13*522d5c66SAsim Jamshed  *
14*522d5c66SAsim Jamshed  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``S IS''AND
15*522d5c66SAsim Jamshed  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*522d5c66SAsim Jamshed  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*522d5c66SAsim Jamshed  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*522d5c66SAsim Jamshed  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*522d5c66SAsim Jamshed  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*522d5c66SAsim Jamshed  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*522d5c66SAsim Jamshed  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*522d5c66SAsim Jamshed  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*522d5c66SAsim Jamshed  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*522d5c66SAsim Jamshed  * SUCH DAMAGE.
25*522d5c66SAsim Jamshed  */
26*522d5c66SAsim Jamshed 
27*522d5c66SAsim Jamshed /*
28*522d5c66SAsim Jamshed  * $FreeBSD: head/sys/net/netmap.h 251139 2013-05-30 14:07:14Z luigi $
29*522d5c66SAsim Jamshed  *
30*522d5c66SAsim Jamshed  * Definitions of constants and the structures used by the netmap
31*522d5c66SAsim Jamshed  * framework, for the part visible to both kernel and userspace.
32*522d5c66SAsim Jamshed  * Detailed info on netmap is available with "man netmap" or at
33*522d5c66SAsim Jamshed  *
34*522d5c66SAsim Jamshed  *	http://info.iet.unipi.it/~luigi/netmap/
35*522d5c66SAsim Jamshed  *
36*522d5c66SAsim Jamshed  * This API is also used to communicate with the VALE software switch
37*522d5c66SAsim Jamshed  */
38*522d5c66SAsim Jamshed 
39*522d5c66SAsim Jamshed #ifndef _NET_NETMAP_H_
40*522d5c66SAsim Jamshed #define _NET_NETMAP_H_
41*522d5c66SAsim Jamshed 
42*522d5c66SAsim Jamshed #define	NETMAP_API	11		/* current API version */
43*522d5c66SAsim Jamshed 
44*522d5c66SAsim Jamshed #define	NETMAP_MIN_API	11		/* min and max versions accepted */
45*522d5c66SAsim Jamshed #define	NETMAP_MAX_API	15
46*522d5c66SAsim Jamshed /*
47*522d5c66SAsim Jamshed  * Some fields should be cache-aligned to reduce contention.
48*522d5c66SAsim Jamshed  * The alignment is architecture and OS dependent, but rather than
49*522d5c66SAsim Jamshed  * digging into OS headers to find the exact value we use an estimate
50*522d5c66SAsim Jamshed  * that should cover most architectures.
51*522d5c66SAsim Jamshed  */
52*522d5c66SAsim Jamshed #define NM_CACHE_ALIGN	128
53*522d5c66SAsim Jamshed 
54*522d5c66SAsim Jamshed /*
55*522d5c66SAsim Jamshed  * --- Netmap data structures ---
56*522d5c66SAsim Jamshed  *
57*522d5c66SAsim Jamshed  * The userspace data structures used by netmap are shown below.
58*522d5c66SAsim Jamshed  * They are allocated by the kernel and mmap()ed by userspace threads.
59*522d5c66SAsim Jamshed  * Pointers are implemented as memory offsets or indexes,
60*522d5c66SAsim Jamshed  * so that they can be easily dereferenced in kernel and userspace.
61*522d5c66SAsim Jamshed 
62*522d5c66SAsim Jamshed    KERNEL (opaque, obviously)
63*522d5c66SAsim Jamshed 
64*522d5c66SAsim Jamshed   ====================================================================
65*522d5c66SAsim Jamshed                                          |
66*522d5c66SAsim Jamshed    USERSPACE                             |      struct netmap_ring
67*522d5c66SAsim Jamshed                                          +---->+---------------+
68*522d5c66SAsim Jamshed                                              / | head,cur,tail |
69*522d5c66SAsim Jamshed    struct netmap_if (nifp, 1 per fd)        /  | buf_ofs       |
70*522d5c66SAsim Jamshed     +---------------+                      /   | other fields  |
71*522d5c66SAsim Jamshed     | ni_tx_rings   |                     /    +===============+
72*522d5c66SAsim Jamshed     | ni_rx_rings   |                    /     | buf_idx, len  | slot[0]
73*522d5c66SAsim Jamshed     |               |                   /      | flags, ptr    |
74*522d5c66SAsim Jamshed     |               |                  /       +---------------+
75*522d5c66SAsim Jamshed     +===============+                 /        | buf_idx, len  | slot[1]
76*522d5c66SAsim Jamshed     | txring_ofs[0] | (rel.to nifp)--'         | flags, ptr    |
77*522d5c66SAsim Jamshed     | txring_ofs[1] |                          +---------------+
78*522d5c66SAsim Jamshed      (tx+1 entries)                           (num_slots entries)
79*522d5c66SAsim Jamshed     | txring_ofs[t] |                          | buf_idx, len  | slot[n-1]
80*522d5c66SAsim Jamshed     +---------------+                          | flags, ptr    |
81*522d5c66SAsim Jamshed     | rxring_ofs[0] |                          +---------------+
82*522d5c66SAsim Jamshed     | rxring_ofs[1] |
83*522d5c66SAsim Jamshed      (rx+1 entries)
84*522d5c66SAsim Jamshed     | rxring_ofs[r] |
85*522d5c66SAsim Jamshed     +---------------+
86*522d5c66SAsim Jamshed 
87*522d5c66SAsim Jamshed  * For each "interface" (NIC, host stack, PIPE, VALE switch port) bound to
88*522d5c66SAsim Jamshed  * a file descriptor, the mmap()ed region contains a (logically readonly)
89*522d5c66SAsim Jamshed  * struct netmap_if pointing to struct netmap_ring's.
90*522d5c66SAsim Jamshed  *
91*522d5c66SAsim Jamshed  * There is one netmap_ring per physical NIC ring, plus one tx/rx ring
92*522d5c66SAsim Jamshed  * pair attached to the host stack (this pair is unused for non-NIC ports).
93*522d5c66SAsim Jamshed  *
94*522d5c66SAsim Jamshed  * All physical/host stack ports share the same memory region,
95*522d5c66SAsim Jamshed  * so that zero-copy can be implemented between them.
96*522d5c66SAsim Jamshed  * VALE switch ports instead have separate memory regions.
97*522d5c66SAsim Jamshed  *
98*522d5c66SAsim Jamshed  * The netmap_ring is the userspace-visible replica of the NIC ring.
99*522d5c66SAsim Jamshed  * Each slot has the index of a buffer (MTU-sized and residing in the
100*522d5c66SAsim Jamshed  * mmapped region), its length and some flags. An extra 64-bit pointer
101*522d5c66SAsim Jamshed  * is provided for user-supplied buffers in the tx path.
102*522d5c66SAsim Jamshed  *
103*522d5c66SAsim Jamshed  * In user space, the buffer address is computed as
104*522d5c66SAsim Jamshed  *	(char *)ring + buf_ofs + index * NETMAP_BUF_SIZE
105*522d5c66SAsim Jamshed  *
106*522d5c66SAsim Jamshed  * Added in NETMAP_API 11:
107*522d5c66SAsim Jamshed  *
108*522d5c66SAsim Jamshed  * + NIOCREGIF can request the allocation of extra spare buffers from
109*522d5c66SAsim Jamshed  *   the same memory pool. The desired number of buffers must be in
110*522d5c66SAsim Jamshed  *   nr_arg3. The ioctl may return fewer buffers, depending on memory
111*522d5c66SAsim Jamshed  *   availability. nr_arg3 will return the actual value, and, once
112*522d5c66SAsim Jamshed  *   mapped, nifp->ni_bufs_head will be the index of the first buffer.
113*522d5c66SAsim Jamshed  *
114*522d5c66SAsim Jamshed  *   The buffers are linked to each other using the first uint32_t
115*522d5c66SAsim Jamshed  *   as the index. On close, ni_bufs_head must point to the list of
116*522d5c66SAsim Jamshed  *   buffers to be released.
117*522d5c66SAsim Jamshed  *
118*522d5c66SAsim Jamshed  * + NIOCREGIF can request space for extra rings (and buffers)
119*522d5c66SAsim Jamshed  *   allocated in the same memory space. The number of extra rings
120*522d5c66SAsim Jamshed  *   is in nr_arg1, and is advisory. This is a no-op on NICs where
121*522d5c66SAsim Jamshed  *   the size of the memory space is fixed.
122*522d5c66SAsim Jamshed  *
123*522d5c66SAsim Jamshed  * + NIOCREGIF can attach to PIPE rings sharing the same memory
124*522d5c66SAsim Jamshed  *   space with a parent device. The ifname indicates the parent device,
125*522d5c66SAsim Jamshed  *   which must already exist. Flags in nr_flags indicate if we want to
126*522d5c66SAsim Jamshed  *   bind the master or slave side, the index (from nr_ringid)
127*522d5c66SAsim Jamshed  *   is just a cookie and does not need to be sequential.
128*522d5c66SAsim Jamshed  *
129*522d5c66SAsim Jamshed  * + NIOCREGIF can also attach to 'monitor' rings that replicate
130*522d5c66SAsim Jamshed  *   the content of specific rings, also from the same memory space.
131*522d5c66SAsim Jamshed  *
132*522d5c66SAsim Jamshed  *   Extra flags in nr_flags support the above functions.
133*522d5c66SAsim Jamshed  *   Application libraries may use the following naming scheme:
134*522d5c66SAsim Jamshed  *	netmap:foo			all NIC ring pairs
135*522d5c66SAsim Jamshed  *	netmap:foo^			only host ring pair
136*522d5c66SAsim Jamshed  *	netmap:foo+			all NIC ring + host ring pairs
137*522d5c66SAsim Jamshed  *	netmap:foo-k			the k-th NIC ring pair
138*522d5c66SAsim Jamshed  *	netmap:foo{k			PIPE ring pair k, master side
139*522d5c66SAsim Jamshed  *	netmap:foo}k			PIPE ring pair k, slave side
140*522d5c66SAsim Jamshed  */
141*522d5c66SAsim Jamshed 
142*522d5c66SAsim Jamshed /*
143*522d5c66SAsim Jamshed  * struct netmap_slot is a buffer descriptor
144*522d5c66SAsim Jamshed  */
145*522d5c66SAsim Jamshed struct netmap_slot {
146*522d5c66SAsim Jamshed 	uint32_t buf_idx;	/* buffer index */
147*522d5c66SAsim Jamshed 	uint16_t len;		/* length for this slot */
148*522d5c66SAsim Jamshed 	uint16_t flags;		/* buf changed, etc. */
149*522d5c66SAsim Jamshed 	uint64_t ptr;		/* pointer for indirect buffers */
150*522d5c66SAsim Jamshed };
151*522d5c66SAsim Jamshed 
152*522d5c66SAsim Jamshed /*
153*522d5c66SAsim Jamshed  * The following flags control how the slot is used
154*522d5c66SAsim Jamshed  */
155*522d5c66SAsim Jamshed 
156*522d5c66SAsim Jamshed #define	NS_BUF_CHANGED	0x0001	/* buf_idx changed */
157*522d5c66SAsim Jamshed 	/*
158*522d5c66SAsim Jamshed 	 * must be set whenever buf_idx is changed (as it might be
159*522d5c66SAsim Jamshed 	 * necessary to recompute the physical address and mapping)
160*522d5c66SAsim Jamshed 	 *
161*522d5c66SAsim Jamshed 	 * It is also set by the kernel whenever the buf_idx is
162*522d5c66SAsim Jamshed 	 * changed internally (e.g., by pipes). Applications may
163*522d5c66SAsim Jamshed 	 * use this information to know when they can reuse the
164*522d5c66SAsim Jamshed 	 * contents of previously prepared buffers.
165*522d5c66SAsim Jamshed 	 */
166*522d5c66SAsim Jamshed 
167*522d5c66SAsim Jamshed #define	NS_REPORT	0x0002	/* ask the hardware to report results */
168*522d5c66SAsim Jamshed 	/*
169*522d5c66SAsim Jamshed 	 * Request notification when slot is used by the hardware.
170*522d5c66SAsim Jamshed 	 * Normally transmit completions are handled lazily and
171*522d5c66SAsim Jamshed 	 * may be unreported. This flag lets us know when a slot
172*522d5c66SAsim Jamshed 	 * has been sent (e.g. to terminate the sender).
173*522d5c66SAsim Jamshed 	 */
174*522d5c66SAsim Jamshed 
175*522d5c66SAsim Jamshed #define	NS_FORWARD	0x0004	/* pass packet 'forward' */
176*522d5c66SAsim Jamshed 	/*
177*522d5c66SAsim Jamshed 	 * (Only for physical ports, rx rings with NR_FORWARD set).
178*522d5c66SAsim Jamshed 	 * Slot released to the kernel (i.e. before ring->head) with
179*522d5c66SAsim Jamshed 	 * this flag set are passed to the peer ring (host/NIC),
180*522d5c66SAsim Jamshed 	 * thus restoring the host-NIC connection for these slots.
181*522d5c66SAsim Jamshed 	 * This supports efficient traffic monitoring or firewalling.
182*522d5c66SAsim Jamshed 	 */
183*522d5c66SAsim Jamshed 
184*522d5c66SAsim Jamshed #define	NS_NO_LEARN	0x0008	/* disable bridge learning */
185*522d5c66SAsim Jamshed  	/*
186*522d5c66SAsim Jamshed 	 * On a VALE switch, do not 'learn' the source port for
187*522d5c66SAsim Jamshed  	 * this buffer.
188*522d5c66SAsim Jamshed 	 */
189*522d5c66SAsim Jamshed 
190*522d5c66SAsim Jamshed #define	NS_INDIRECT	0x0010	/* userspace buffer */
191*522d5c66SAsim Jamshed  	/*
192*522d5c66SAsim Jamshed 	 * (VALE tx rings only) data is in a userspace buffer,
193*522d5c66SAsim Jamshed 	 * whose address is in the 'ptr' field in the slot.
194*522d5c66SAsim Jamshed 	 */
195*522d5c66SAsim Jamshed 
196*522d5c66SAsim Jamshed #define	NS_MOREFRAG	0x0020	/* packet has more fragments */
197*522d5c66SAsim Jamshed  	/*
198*522d5c66SAsim Jamshed 	 * (VALE ports only)
199*522d5c66SAsim Jamshed 	 * Set on all but the last slot of a multi-segment packet.
200*522d5c66SAsim Jamshed 	 * The 'len' field refers to the individual fragment.
201*522d5c66SAsim Jamshed 	 */
202*522d5c66SAsim Jamshed 
203*522d5c66SAsim Jamshed #define	NS_PORT_SHIFT	8
204*522d5c66SAsim Jamshed #define	NS_PORT_MASK	(0xff << NS_PORT_SHIFT)
205*522d5c66SAsim Jamshed 	/*
206*522d5c66SAsim Jamshed  	 * The high 8 bits of the flag, if not zero, indicate the
207*522d5c66SAsim Jamshed 	 * destination port for the VALE switch, overriding
208*522d5c66SAsim Jamshed  	 * the lookup table.
209*522d5c66SAsim Jamshed  	 */
210*522d5c66SAsim Jamshed 
211*522d5c66SAsim Jamshed #define	NS_RFRAGS(_slot)	( ((_slot)->flags >> 8) & 0xff)
212*522d5c66SAsim Jamshed 	/*
213*522d5c66SAsim Jamshed 	 * (VALE rx rings only) the high 8 bits
214*522d5c66SAsim Jamshed 	 *  are the number of fragments.
215*522d5c66SAsim Jamshed 	 */
216*522d5c66SAsim Jamshed 
217*522d5c66SAsim Jamshed 
218*522d5c66SAsim Jamshed /*
219*522d5c66SAsim Jamshed  * struct netmap_ring
220*522d5c66SAsim Jamshed  *
221*522d5c66SAsim Jamshed  * Netmap representation of a TX or RX ring (also known as "queue").
222*522d5c66SAsim Jamshed  * This is a queue implemented as a fixed-size circular array.
223*522d5c66SAsim Jamshed  * At the software level the important fields are: head, cur, tail.
224*522d5c66SAsim Jamshed  *
225*522d5c66SAsim Jamshed  * In TX rings:
226*522d5c66SAsim Jamshed  *
227*522d5c66SAsim Jamshed  *	head	first slot available for transmission.
228*522d5c66SAsim Jamshed  *	cur	wakeup point. select() and poll() will unblock
229*522d5c66SAsim Jamshed  *		when 'tail' moves past 'cur'
230*522d5c66SAsim Jamshed  *	tail	(readonly) first slot reserved to the kernel
231*522d5c66SAsim Jamshed  *
232*522d5c66SAsim Jamshed  *	[head .. tail-1] can be used for new packets to send;
233*522d5c66SAsim Jamshed  *	'head' and 'cur' must be incremented as slots are filled
234*522d5c66SAsim Jamshed  *	    with new packets to be sent;
235*522d5c66SAsim Jamshed  *	'cur' can be moved further ahead if we need more space
236*522d5c66SAsim Jamshed  *	for new transmissions. XXX todo (2014-03-12)
237*522d5c66SAsim Jamshed  *
238*522d5c66SAsim Jamshed  * In RX rings:
239*522d5c66SAsim Jamshed  *
240*522d5c66SAsim Jamshed  *	head	first valid received packet
241*522d5c66SAsim Jamshed  *	cur	wakeup point. select() and poll() will unblock
242*522d5c66SAsim Jamshed  *		when 'tail' moves past 'cur'
243*522d5c66SAsim Jamshed  *	tail	(readonly) first slot reserved to the kernel
244*522d5c66SAsim Jamshed  *
245*522d5c66SAsim Jamshed  *	[head .. tail-1] contain received packets;
246*522d5c66SAsim Jamshed  *	'head' and 'cur' must be incremented as slots are consumed
247*522d5c66SAsim Jamshed  *		and can be returned to the kernel;
248*522d5c66SAsim Jamshed  *	'cur' can be moved further ahead if we want to wait for
249*522d5c66SAsim Jamshed  *		new packets without returning the previous ones.
250*522d5c66SAsim Jamshed  *
251*522d5c66SAsim Jamshed  * DATA OWNERSHIP/LOCKING:
252*522d5c66SAsim Jamshed  *	The netmap_ring, and all slots and buffers in the range
253*522d5c66SAsim Jamshed  *	[head .. tail-1] are owned by the user program;
254*522d5c66SAsim Jamshed  *	the kernel only accesses them during a netmap system call
255*522d5c66SAsim Jamshed  *	and in the user thread context.
256*522d5c66SAsim Jamshed  *
257*522d5c66SAsim Jamshed  *	Other slots and buffers are reserved for use by the kernel
258*522d5c66SAsim Jamshed  */
259*522d5c66SAsim Jamshed struct netmap_ring {
260*522d5c66SAsim Jamshed 	/*
261*522d5c66SAsim Jamshed 	 * buf_ofs is meant to be used through macros.
262*522d5c66SAsim Jamshed 	 * It contains the offset of the buffer region from this
263*522d5c66SAsim Jamshed 	 * descriptor.
264*522d5c66SAsim Jamshed 	 */
265*522d5c66SAsim Jamshed 	const int64_t	buf_ofs;
266*522d5c66SAsim Jamshed 	const uint32_t	num_slots;	/* number of slots in the ring. */
267*522d5c66SAsim Jamshed 	const uint32_t	nr_buf_size;
268*522d5c66SAsim Jamshed 	const uint16_t	ringid;
269*522d5c66SAsim Jamshed 	const uint16_t	dir;		/* 0: tx, 1: rx */
270*522d5c66SAsim Jamshed 
271*522d5c66SAsim Jamshed 	uint32_t        head;		/* (u) first user slot */
272*522d5c66SAsim Jamshed 	uint32_t        cur;		/* (u) wakeup point */
273*522d5c66SAsim Jamshed 	uint32_t	tail;		/* (k) first kernel slot */
274*522d5c66SAsim Jamshed 
275*522d5c66SAsim Jamshed 	uint32_t	flags;
276*522d5c66SAsim Jamshed 
277*522d5c66SAsim Jamshed 	struct timeval	ts;		/* (k) time of last *sync() */
278*522d5c66SAsim Jamshed 
279*522d5c66SAsim Jamshed 	/* opaque room for a mutex or similar object */
280*522d5c66SAsim Jamshed #if !defined(_WIN32) || defined(__CYGWIN__)
281*522d5c66SAsim Jamshed 	uint8_t	__attribute__((__aligned__(NM_CACHE_ALIGN))) sem[128];
282*522d5c66SAsim Jamshed #else
283*522d5c66SAsim Jamshed 	uint8_t	__declspec(align(NM_CACHE_ALIGN)) sem[128];
284*522d5c66SAsim Jamshed #endif
285*522d5c66SAsim Jamshed 
286*522d5c66SAsim Jamshed 	/* the slots follow. This struct has variable size */
287*522d5c66SAsim Jamshed 	struct netmap_slot slot[0];	/* array of slots. */
288*522d5c66SAsim Jamshed };
289*522d5c66SAsim Jamshed 
290*522d5c66SAsim Jamshed 
291*522d5c66SAsim Jamshed /*
292*522d5c66SAsim Jamshed  * RING FLAGS
293*522d5c66SAsim Jamshed  */
294*522d5c66SAsim Jamshed #define	NR_TIMESTAMP	0x0002		/* set timestamp on *sync() */
295*522d5c66SAsim Jamshed 	/*
296*522d5c66SAsim Jamshed 	 * updates the 'ts' field on each netmap syscall. This saves
297*522d5c66SAsim Jamshed 	 * saves a separate gettimeofday(), and is not much worse than
298*522d5c66SAsim Jamshed 	 * software timestamps generated in the interrupt handler.
299*522d5c66SAsim Jamshed 	 */
300*522d5c66SAsim Jamshed 
301*522d5c66SAsim Jamshed #define	NR_FORWARD	0x0004		/* enable NS_FORWARD for ring */
302*522d5c66SAsim Jamshed  	/*
303*522d5c66SAsim Jamshed 	 * Enables the NS_FORWARD slot flag for the ring.
304*522d5c66SAsim Jamshed 	 */
305*522d5c66SAsim Jamshed 
306*522d5c66SAsim Jamshed 
307*522d5c66SAsim Jamshed /*
308*522d5c66SAsim Jamshed  * Netmap representation of an interface and its queue(s).
309*522d5c66SAsim Jamshed  * This is initialized by the kernel when binding a file
310*522d5c66SAsim Jamshed  * descriptor to a port, and should be considered as readonly
311*522d5c66SAsim Jamshed  * by user programs. The kernel never uses it.
312*522d5c66SAsim Jamshed  *
313*522d5c66SAsim Jamshed  * There is one netmap_if for each file descriptor on which we want
314*522d5c66SAsim Jamshed  * to select/poll.
315*522d5c66SAsim Jamshed  * select/poll operates on one or all pairs depending on the value of
316*522d5c66SAsim Jamshed  * nmr_queueid passed on the ioctl.
317*522d5c66SAsim Jamshed  */
318*522d5c66SAsim Jamshed struct netmap_if {
319*522d5c66SAsim Jamshed 	char		ni_name[IFNAMSIZ]; /* name of the interface. */
320*522d5c66SAsim Jamshed 	const uint32_t	ni_version;	/* API version, currently unused */
321*522d5c66SAsim Jamshed 	const uint32_t	ni_flags;	/* properties */
322*522d5c66SAsim Jamshed #define	NI_PRIV_MEM	0x1		/* private memory region */
323*522d5c66SAsim Jamshed 
324*522d5c66SAsim Jamshed 	/*
325*522d5c66SAsim Jamshed 	 * The number of packet rings available in netmap mode.
326*522d5c66SAsim Jamshed 	 * Physical NICs can have different numbers of tx and rx rings.
327*522d5c66SAsim Jamshed 	 * Physical NICs also have a 'host' ring pair.
328*522d5c66SAsim Jamshed 	 * Additionally, clients can request additional ring pairs to
329*522d5c66SAsim Jamshed 	 * be used for internal communication.
330*522d5c66SAsim Jamshed 	 */
331*522d5c66SAsim Jamshed 	const uint32_t	ni_tx_rings;	/* number of HW tx rings */
332*522d5c66SAsim Jamshed 	const uint32_t	ni_rx_rings;	/* number of HW rx rings */
333*522d5c66SAsim Jamshed 
334*522d5c66SAsim Jamshed 	uint32_t	ni_bufs_head;	/* head index for extra bufs */
335*522d5c66SAsim Jamshed 	uint32_t	ni_spare1[5];
336*522d5c66SAsim Jamshed 	/*
337*522d5c66SAsim Jamshed 	 * The following array contains the offset of each netmap ring
338*522d5c66SAsim Jamshed 	 * from this structure, in the following order:
339*522d5c66SAsim Jamshed 	 * NIC tx rings (ni_tx_rings); host tx ring (1); extra tx rings;
340*522d5c66SAsim Jamshed 	 * NIC rx rings (ni_rx_rings); host tx ring (1); extra rx rings.
341*522d5c66SAsim Jamshed 	 *
342*522d5c66SAsim Jamshed 	 * The area is filled up by the kernel on NIOCREGIF,
343*522d5c66SAsim Jamshed 	 * and then only read by userspace code.
344*522d5c66SAsim Jamshed 	 */
345*522d5c66SAsim Jamshed 	const ssize_t	ring_ofs[0];
346*522d5c66SAsim Jamshed };
347*522d5c66SAsim Jamshed 
348*522d5c66SAsim Jamshed 
349*522d5c66SAsim Jamshed #ifndef NIOCREGIF
350*522d5c66SAsim Jamshed /*
351*522d5c66SAsim Jamshed  * ioctl names and related fields
352*522d5c66SAsim Jamshed  *
353*522d5c66SAsim Jamshed  * NIOCTXSYNC, NIOCRXSYNC synchronize tx or rx queues,
354*522d5c66SAsim Jamshed  *	whose identity is set in NIOCREGIF through nr_ringid.
355*522d5c66SAsim Jamshed  *	These are non blocking and take no argument.
356*522d5c66SAsim Jamshed  *
357*522d5c66SAsim Jamshed  * NIOCGINFO takes a struct ifreq, the interface name is the input,
358*522d5c66SAsim Jamshed  *	the outputs are number of queues and number of descriptor
359*522d5c66SAsim Jamshed  *	for each queue (useful to set number of threads etc.).
360*522d5c66SAsim Jamshed  *	The info returned is only advisory and may change before
361*522d5c66SAsim Jamshed  *	the interface is bound to a file descriptor.
362*522d5c66SAsim Jamshed  *
363*522d5c66SAsim Jamshed  * NIOCREGIF takes an interface name within a struct nmre,
364*522d5c66SAsim Jamshed  *	and activates netmap mode on the interface (if possible).
365*522d5c66SAsim Jamshed  *
366*522d5c66SAsim Jamshed  * The argument to NIOCGINFO/NIOCREGIF overlays struct ifreq so we
367*522d5c66SAsim Jamshed  * can pass it down to other NIC-related ioctls.
368*522d5c66SAsim Jamshed  *
369*522d5c66SAsim Jamshed  * The actual argument (struct nmreq) has a number of options to request
370*522d5c66SAsim Jamshed  * different functions.
371*522d5c66SAsim Jamshed  * The following are used in NIOCREGIF when nr_cmd == 0:
372*522d5c66SAsim Jamshed  *
373*522d5c66SAsim Jamshed  * nr_name	(in)
374*522d5c66SAsim Jamshed  *	The name of the port (em0, valeXXX:YYY, etc.)
375*522d5c66SAsim Jamshed  *	limited to IFNAMSIZ for backward compatibility.
376*522d5c66SAsim Jamshed  *
377*522d5c66SAsim Jamshed  * nr_version	(in/out)
378*522d5c66SAsim Jamshed  *	Must match NETMAP_API as used in the kernel, error otherwise.
379*522d5c66SAsim Jamshed  *	Always returns the desired value on output.
380*522d5c66SAsim Jamshed  *
381*522d5c66SAsim Jamshed  * nr_tx_slots, nr_tx_slots, nr_tx_rings, nr_rx_rings (in/out)
382*522d5c66SAsim Jamshed  *	On input, non-zero values may be used to reconfigure the port
383*522d5c66SAsim Jamshed  *	according to the requested values, but this is not guaranteed.
384*522d5c66SAsim Jamshed  *	On output the actual values in use are reported.
385*522d5c66SAsim Jamshed  *
386*522d5c66SAsim Jamshed  * nr_ringid (in)
387*522d5c66SAsim Jamshed  *	Indicates how rings should be bound to the file descriptors.
388*522d5c66SAsim Jamshed  *	If nr_flags != 0, then the low bits (in NETMAP_RING_MASK)
389*522d5c66SAsim Jamshed  *	are used to indicate the ring number, and nr_flags specifies
390*522d5c66SAsim Jamshed  *	the actual rings to bind. NETMAP_NO_TX_POLL is unaffected.
391*522d5c66SAsim Jamshed  *
392*522d5c66SAsim Jamshed  *	NOTE: THE FOLLOWING (nr_flags == 0) IS DEPRECATED:
393*522d5c66SAsim Jamshed  *	If nr_flags == 0, NETMAP_HW_RING and NETMAP_SW_RING control
394*522d5c66SAsim Jamshed  *	the binding as follows:
395*522d5c66SAsim Jamshed  *	0 (default)			binds all physical rings
396*522d5c66SAsim Jamshed  *	NETMAP_HW_RING | ring number	binds a single ring pair
397*522d5c66SAsim Jamshed  *	NETMAP_SW_RING			binds only the host tx/rx rings
398*522d5c66SAsim Jamshed  *
399*522d5c66SAsim Jamshed  *	NETMAP_NO_TX_POLL can be OR-ed to make select()/poll() push
400*522d5c66SAsim Jamshed  *		packets on tx rings only if POLLOUT is set.
401*522d5c66SAsim Jamshed  *		The default is to push any pending packet.
402*522d5c66SAsim Jamshed  *
403*522d5c66SAsim Jamshed  *	NETMAP_DO_RX_POLL can be OR-ed to make select()/poll() release
404*522d5c66SAsim Jamshed  *		packets on rx rings also when POLLIN is NOT set.
405*522d5c66SAsim Jamshed  *		The default is to touch the rx ring only with POLLIN.
406*522d5c66SAsim Jamshed  *		Note that this is the opposite of TX because it
407*522d5c66SAsim Jamshed  *		reflects the common usage.
408*522d5c66SAsim Jamshed  *
409*522d5c66SAsim Jamshed  *	NOTE: NETMAP_PRIV_MEM IS DEPRECATED, use nr_arg2 instead.
410*522d5c66SAsim Jamshed  *	NETMAP_PRIV_MEM is set on return for ports that do not use
411*522d5c66SAsim Jamshed  *		the global memory allocator.
412*522d5c66SAsim Jamshed  *		This information is not significant and applications
413*522d5c66SAsim Jamshed  *		should look at the region id in nr_arg2
414*522d5c66SAsim Jamshed  *
415*522d5c66SAsim Jamshed  * nr_flags	is the recommended mode to indicate which rings should
416*522d5c66SAsim Jamshed  *		be bound to a file descriptor. Values are NR_REG_*
417*522d5c66SAsim Jamshed  *
418*522d5c66SAsim Jamshed  * nr_arg1 (in)	The number of extra rings to be reserved.
419*522d5c66SAsim Jamshed  *		Especially when allocating a VALE port the system only
420*522d5c66SAsim Jamshed  *		allocates the amount of memory needed for the port.
421*522d5c66SAsim Jamshed  *		If more shared memory rings are desired (e.g. for pipes),
422*522d5c66SAsim Jamshed  *		the first invocation for the same basename/allocator
423*522d5c66SAsim Jamshed  *		should specify a suitable number. Memory cannot be
424*522d5c66SAsim Jamshed  *		extended after the first allocation without closing
425*522d5c66SAsim Jamshed  *		all ports on the same region.
426*522d5c66SAsim Jamshed  *
427*522d5c66SAsim Jamshed  * nr_arg2 (in/out) The identity of the memory region used.
428*522d5c66SAsim Jamshed  *		On input, 0 means the system decides autonomously,
429*522d5c66SAsim Jamshed  *		other values may try to select a specific region.
430*522d5c66SAsim Jamshed  *		On return the actual value is reported.
431*522d5c66SAsim Jamshed  *		Region '1' is the global allocator, normally shared
432*522d5c66SAsim Jamshed  *		by all interfaces. Other values are private regions.
433*522d5c66SAsim Jamshed  *		If two ports the same region zero-copy is possible.
434*522d5c66SAsim Jamshed  *
435*522d5c66SAsim Jamshed  * nr_arg3 (in/out)	number of extra buffers to be allocated.
436*522d5c66SAsim Jamshed  *
437*522d5c66SAsim Jamshed  *
438*522d5c66SAsim Jamshed  *
439*522d5c66SAsim Jamshed  * nr_cmd (in)	if non-zero indicates a special command:
440*522d5c66SAsim Jamshed  *	NETMAP_BDG_ATTACH	 and nr_name = vale*:ifname
441*522d5c66SAsim Jamshed  *		attaches the NIC to the switch; nr_ringid specifies
442*522d5c66SAsim Jamshed  *		which rings to use. Used by vale-ctl -a ...
443*522d5c66SAsim Jamshed  *	    nr_arg1 = NETMAP_BDG_HOST also attaches the host port
444*522d5c66SAsim Jamshed  *		as in vale-ctl -h ...
445*522d5c66SAsim Jamshed  *
446*522d5c66SAsim Jamshed  *	NETMAP_BDG_DETACH	and nr_name = vale*:ifname
447*522d5c66SAsim Jamshed  *		disconnects a previously attached NIC.
448*522d5c66SAsim Jamshed  *		Used by vale-ctl -d ...
449*522d5c66SAsim Jamshed  *
450*522d5c66SAsim Jamshed  *	NETMAP_BDG_LIST
451*522d5c66SAsim Jamshed  *		list the configuration of VALE switches.
452*522d5c66SAsim Jamshed  *
453*522d5c66SAsim Jamshed  *	NETMAP_BDG_VNET_HDR
454*522d5c66SAsim Jamshed  *		Set the virtio-net header length used by the client
455*522d5c66SAsim Jamshed  *		of a VALE switch port.
456*522d5c66SAsim Jamshed  *
457*522d5c66SAsim Jamshed  *	NETMAP_BDG_NEWIF
458*522d5c66SAsim Jamshed  *		create a persistent VALE port with name nr_name.
459*522d5c66SAsim Jamshed  *		Used by vale-ctl -n ...
460*522d5c66SAsim Jamshed  *
461*522d5c66SAsim Jamshed  *	NETMAP_BDG_DELIF
462*522d5c66SAsim Jamshed  *		delete a persistent VALE port. Used by vale-ctl -d ...
463*522d5c66SAsim Jamshed  *
464*522d5c66SAsim Jamshed  * nr_arg1, nr_arg2, nr_arg3  (in/out)		command specific
465*522d5c66SAsim Jamshed  *
466*522d5c66SAsim Jamshed  *
467*522d5c66SAsim Jamshed  *
468*522d5c66SAsim Jamshed  */
469*522d5c66SAsim Jamshed 
470*522d5c66SAsim Jamshed 
471*522d5c66SAsim Jamshed /*
472*522d5c66SAsim Jamshed  * struct nmreq overlays a struct ifreq (just the name)
473*522d5c66SAsim Jamshed  */
474*522d5c66SAsim Jamshed struct nmreq {
475*522d5c66SAsim Jamshed 	char		nr_name[IFNAMSIZ];
476*522d5c66SAsim Jamshed 	uint32_t	nr_version;	/* API version */
477*522d5c66SAsim Jamshed 	uint32_t	nr_offset;	/* nifp offset in the shared region */
478*522d5c66SAsim Jamshed 	uint32_t	nr_memsize;	/* size of the shared region */
479*522d5c66SAsim Jamshed 	uint32_t	nr_tx_slots;	/* slots in tx rings */
480*522d5c66SAsim Jamshed 	uint32_t	nr_rx_slots;	/* slots in rx rings */
481*522d5c66SAsim Jamshed 	uint16_t	nr_tx_rings;	/* number of tx rings */
482*522d5c66SAsim Jamshed 	uint16_t	nr_rx_rings;	/* number of rx rings */
483*522d5c66SAsim Jamshed 
484*522d5c66SAsim Jamshed 	uint16_t	nr_ringid;	/* ring(s) we care about */
485*522d5c66SAsim Jamshed #define NETMAP_HW_RING		0x4000	/* single NIC ring pair */
486*522d5c66SAsim Jamshed #define NETMAP_SW_RING		0x2000	/* only host ring pair */
487*522d5c66SAsim Jamshed 
488*522d5c66SAsim Jamshed #define NETMAP_RING_MASK	0x0fff	/* the ring number */
489*522d5c66SAsim Jamshed 
490*522d5c66SAsim Jamshed #define NETMAP_NO_TX_POLL	0x1000	/* no automatic txsync on poll */
491*522d5c66SAsim Jamshed 
492*522d5c66SAsim Jamshed #define NETMAP_DO_RX_POLL	0x8000	/* DO automatic rxsync on poll */
493*522d5c66SAsim Jamshed 
494*522d5c66SAsim Jamshed 	uint16_t	nr_cmd;
495*522d5c66SAsim Jamshed #define NETMAP_BDG_ATTACH	1	/* attach the NIC */
496*522d5c66SAsim Jamshed #define NETMAP_BDG_DETACH	2	/* detach the NIC */
497*522d5c66SAsim Jamshed #define NETMAP_BDG_REGOPS	3	/* register bridge callbacks */
498*522d5c66SAsim Jamshed #define NETMAP_BDG_LIST		4	/* get bridge's info */
499*522d5c66SAsim Jamshed #define NETMAP_BDG_VNET_HDR     5       /* set the port virtio-net-hdr length */
500*522d5c66SAsim Jamshed #define NETMAP_BDG_OFFSET	NETMAP_BDG_VNET_HDR	/* deprecated alias */
501*522d5c66SAsim Jamshed #define NETMAP_BDG_NEWIF	6	/* create a virtual port */
502*522d5c66SAsim Jamshed #define NETMAP_BDG_DELIF	7	/* destroy a virtual port */
503*522d5c66SAsim Jamshed #define NETMAP_PT_HOST_CREATE	8	/* create ptnetmap kthreads */
504*522d5c66SAsim Jamshed #define NETMAP_PT_HOST_DELETE	9	/* delete ptnetmap kthreads */
505*522d5c66SAsim Jamshed #define NETMAP_BDG_POLLING_ON	10	/* delete polling kthread */
506*522d5c66SAsim Jamshed #define NETMAP_BDG_POLLING_OFF	11	/* delete polling kthread */
507*522d5c66SAsim Jamshed #define NETMAP_VNET_HDR_GET	12      /* get the port virtio-net-hdr length */
508*522d5c66SAsim Jamshed 	uint16_t	nr_arg1;	/* reserve extra rings in NIOCREGIF */
509*522d5c66SAsim Jamshed #define NETMAP_BDG_HOST		1	/* attach the host stack on ATTACH */
510*522d5c66SAsim Jamshed 
511*522d5c66SAsim Jamshed 	uint16_t	nr_arg2;
512*522d5c66SAsim Jamshed 	uint32_t	nr_arg3;	/* req. extra buffers in NIOCREGIF */
513*522d5c66SAsim Jamshed 	uint32_t	nr_flags;
514*522d5c66SAsim Jamshed 	/* various modes, extends nr_ringid */
515*522d5c66SAsim Jamshed 	uint32_t	spare2[1];
516*522d5c66SAsim Jamshed };
517*522d5c66SAsim Jamshed 
518*522d5c66SAsim Jamshed #define NR_REG_MASK		0xf /* values for nr_flags */
519*522d5c66SAsim Jamshed enum {	NR_REG_DEFAULT	= 0,	/* backward compat, should not be used. */
520*522d5c66SAsim Jamshed 	NR_REG_ALL_NIC	= 1,
521*522d5c66SAsim Jamshed 	NR_REG_SW	= 2,
522*522d5c66SAsim Jamshed 	NR_REG_NIC_SW	= 3,
523*522d5c66SAsim Jamshed 	NR_REG_ONE_NIC	= 4,
524*522d5c66SAsim Jamshed 	NR_REG_PIPE_MASTER = 5,
525*522d5c66SAsim Jamshed 	NR_REG_PIPE_SLAVE = 6,
526*522d5c66SAsim Jamshed };
527*522d5c66SAsim Jamshed /* monitor uses the NR_REG to select the rings to monitor */
528*522d5c66SAsim Jamshed #define NR_MONITOR_TX	0x100
529*522d5c66SAsim Jamshed #define NR_MONITOR_RX	0x200
530*522d5c66SAsim Jamshed #define NR_ZCOPY_MON	0x400
531*522d5c66SAsim Jamshed /* request exclusive access to the selected rings */
532*522d5c66SAsim Jamshed #define NR_EXCLUSIVE	0x800
533*522d5c66SAsim Jamshed /* request ptnetmap host support */
534*522d5c66SAsim Jamshed #define NR_PASSTHROUGH_HOST	NR_PTNETMAP_HOST /* deprecated */
535*522d5c66SAsim Jamshed #define NR_PTNETMAP_HOST	0x1000
536*522d5c66SAsim Jamshed #define NR_RX_RINGS_ONLY	0x2000
537*522d5c66SAsim Jamshed #define NR_TX_RINGS_ONLY	0x4000
538*522d5c66SAsim Jamshed /* Applications set this flag if they are able to deal with virtio-net headers,
539*522d5c66SAsim Jamshed  * that is send/receive frames that start with a virtio-net header.
540*522d5c66SAsim Jamshed  * If not set, NIOCREGIF will fail with netmap ports that require applications
541*522d5c66SAsim Jamshed  * to use those headers. If the flag is set, the application can use the
542*522d5c66SAsim Jamshed  * NETMAP_VNET_HDR_GET command to figure out the header length. */
543*522d5c66SAsim Jamshed #define NR_ACCEPT_VNET_HDR	0x8000
544*522d5c66SAsim Jamshed 
545*522d5c66SAsim Jamshed 
546*522d5c66SAsim Jamshed /*
547*522d5c66SAsim Jamshed  * Windows does not have _IOWR(). _IO(), _IOW() and _IOR() are defined
548*522d5c66SAsim Jamshed  * in ws2def.h but not sure if they are in the form we need.
549*522d5c66SAsim Jamshed  * XXX so we redefine them
550*522d5c66SAsim Jamshed  * in a convenient way to use for DeviceIoControl signatures
551*522d5c66SAsim Jamshed  */
552*522d5c66SAsim Jamshed #ifdef _WIN32
553*522d5c66SAsim Jamshed #undef _IO	// ws2def.h
554*522d5c66SAsim Jamshed #define _WIN_NM_IOCTL_TYPE 40000
555*522d5c66SAsim Jamshed #define _IO(_c, _n)	CTL_CODE(_WIN_NM_IOCTL_TYPE, ((_n) + 0x800) , \
556*522d5c66SAsim Jamshed 		METHOD_BUFFERED, FILE_ANY_ACCESS  )
557*522d5c66SAsim Jamshed #define _IO_direct(_c, _n)	CTL_CODE(_WIN_NM_IOCTL_TYPE, ((_n) + 0x800) , \
558*522d5c66SAsim Jamshed 		METHOD_OUT_DIRECT, FILE_ANY_ACCESS  )
559*522d5c66SAsim Jamshed 
560*522d5c66SAsim Jamshed #define _IOWR(_c, _n, _s)	_IO(_c, _n)
561*522d5c66SAsim Jamshed 
562*522d5c66SAsim Jamshed /* We havesome internal sysctl in addition to the externally visible ones */
563*522d5c66SAsim Jamshed #define NETMAP_MMAP _IO_direct('i', 160)	// note METHOD_OUT_DIRECT
564*522d5c66SAsim Jamshed #define NETMAP_POLL _IO('i', 162)
565*522d5c66SAsim Jamshed 
566*522d5c66SAsim Jamshed /* and also two setsockopt for sysctl emulation */
567*522d5c66SAsim Jamshed #define NETMAP_SETSOCKOPT _IO('i', 140)
568*522d5c66SAsim Jamshed #define NETMAP_GETSOCKOPT _IO('i', 141)
569*522d5c66SAsim Jamshed 
570*522d5c66SAsim Jamshed 
571*522d5c66SAsim Jamshed //These linknames are for the Netmap Core Driver
572*522d5c66SAsim Jamshed #define NETMAP_NT_DEVICE_NAME			L"\\Device\\NETMAP"
573*522d5c66SAsim Jamshed #define NETMAP_DOS_DEVICE_NAME			L"\\DosDevices\\netmap"
574*522d5c66SAsim Jamshed 
575*522d5c66SAsim Jamshed //Definition of a structure used to pass a virtual address within an IOCTL
576*522d5c66SAsim Jamshed typedef struct _MEMORY_ENTRY {
577*522d5c66SAsim Jamshed 	PVOID       pUsermodeVirtualAddress;
578*522d5c66SAsim Jamshed } MEMORY_ENTRY, *PMEMORY_ENTRY;
579*522d5c66SAsim Jamshed 
580*522d5c66SAsim Jamshed typedef struct _POLL_REQUEST_DATA {
581*522d5c66SAsim Jamshed 	int events;
582*522d5c66SAsim Jamshed 	int timeout;
583*522d5c66SAsim Jamshed 	int revents;
584*522d5c66SAsim Jamshed } POLL_REQUEST_DATA;
585*522d5c66SAsim Jamshed 
586*522d5c66SAsim Jamshed #endif /* _WIN32 */
587*522d5c66SAsim Jamshed 
588*522d5c66SAsim Jamshed /*
589*522d5c66SAsim Jamshed  * FreeBSD uses the size value embedded in the _IOWR to determine
590*522d5c66SAsim Jamshed  * how much to copy in/out. So we need it to match the actual
591*522d5c66SAsim Jamshed  * data structure we pass. We put some spares in the structure
592*522d5c66SAsim Jamshed  * to ease compatibility with other versions
593*522d5c66SAsim Jamshed  */
594*522d5c66SAsim Jamshed #define NIOCGINFO	_IOWR('i', 145, struct nmreq) /* return IF info */
595*522d5c66SAsim Jamshed #define NIOCREGIF	_IOWR('i', 146, struct nmreq) /* interface register */
596*522d5c66SAsim Jamshed #define NIOCTXSYNC	_IO('i', 148) /* sync tx queues */
597*522d5c66SAsim Jamshed #define NIOCRXSYNC	_IO('i', 149) /* sync rx queues */
598*522d5c66SAsim Jamshed #define NIOCCONFIG	_IOWR('i',150, struct nm_ifreq) /* for ext. modules */
599*522d5c66SAsim Jamshed #endif /* !NIOCREGIF */
600*522d5c66SAsim Jamshed 
601*522d5c66SAsim Jamshed 
602*522d5c66SAsim Jamshed /*
603*522d5c66SAsim Jamshed  * Helper functions for kernel and userspace
604*522d5c66SAsim Jamshed  */
605*522d5c66SAsim Jamshed 
606*522d5c66SAsim Jamshed /*
607*522d5c66SAsim Jamshed  * check if space is available in the ring.
608*522d5c66SAsim Jamshed  */
609*522d5c66SAsim Jamshed static inline int
nm_ring_empty(struct netmap_ring * ring)610*522d5c66SAsim Jamshed nm_ring_empty(struct netmap_ring *ring)
611*522d5c66SAsim Jamshed {
612*522d5c66SAsim Jamshed 	return (ring->cur == ring->tail);
613*522d5c66SAsim Jamshed }
614*522d5c66SAsim Jamshed 
615*522d5c66SAsim Jamshed /*
616*522d5c66SAsim Jamshed  * Opaque structure that is passed to an external kernel
617*522d5c66SAsim Jamshed  * module via ioctl(fd, NIOCCONFIG, req) for a user-owned
618*522d5c66SAsim Jamshed  * bridge port (at this point ephemeral VALE interface).
619*522d5c66SAsim Jamshed  */
620*522d5c66SAsim Jamshed #define NM_IFRDATA_LEN 256
621*522d5c66SAsim Jamshed struct nm_ifreq {
622*522d5c66SAsim Jamshed 	char nifr_name[IFNAMSIZ];
623*522d5c66SAsim Jamshed 	char data[NM_IFRDATA_LEN];
624*522d5c66SAsim Jamshed };
625*522d5c66SAsim Jamshed 
626*522d5c66SAsim Jamshed /*
627*522d5c66SAsim Jamshed  * netmap kernel thread configuration
628*522d5c66SAsim Jamshed  */
629*522d5c66SAsim Jamshed /* bhyve/vmm.ko MSIX parameters for IOCTL */
630*522d5c66SAsim Jamshed struct ptn_vmm_ioctl_msix {
631*522d5c66SAsim Jamshed 	uint64_t        msg;
632*522d5c66SAsim Jamshed 	uint64_t        addr;
633*522d5c66SAsim Jamshed };
634*522d5c66SAsim Jamshed 
635*522d5c66SAsim Jamshed /* IOCTL parameters */
636*522d5c66SAsim Jamshed struct nm_kth_ioctl {
637*522d5c66SAsim Jamshed 	u_long				com;
638*522d5c66SAsim Jamshed 	/* TODO: use union */
639*522d5c66SAsim Jamshed 	union {
640*522d5c66SAsim Jamshed 		struct ptn_vmm_ioctl_msix msix;
641*522d5c66SAsim Jamshed 	} data;
642*522d5c66SAsim Jamshed };
643*522d5c66SAsim Jamshed 
644*522d5c66SAsim Jamshed /* Configuration of a ptnetmap ring */
645*522d5c66SAsim Jamshed struct ptnet_ring_cfg {
646*522d5c66SAsim Jamshed 	uint64_t ioeventfd;		/* eventfd in linux, tsleep() parameter in FreeBSD */
647*522d5c66SAsim Jamshed 	uint64_t irqfd;			/* eventfd in linux, ioctl fd in FreeBSD */
648*522d5c66SAsim Jamshed 	struct nm_kth_ioctl ioctl;	/* ioctl parameter to send irq (only used in bhyve/FreeBSD) */
649*522d5c66SAsim Jamshed };
650*522d5c66SAsim Jamshed #endif /* _NET_NETMAP_H_ */
651