1231650Sluigi/*
2262153Sluigi * Copyright (C) 2011-2014 Matteo Landi, Luigi Rizzo. All rights reserved.
3262153Sluigi *
4231650Sluigi * Redistribution and use in source and binary forms, with or without
5262153Sluigi * modification, are permitted provided that the following conditions
6262153Sluigi * are met:
7262153Sluigi *
8231650Sluigi *   1. Redistributions of source code must retain the above copyright
9231650Sluigi *      notice, this list of conditions and the following disclaimer.
10231650Sluigi *   2. Redistributions in binary form must reproduce the above copyright
11231650Sluigi *      notice, this list of conditions and the following disclaimer in the
12262153Sluigi *      documentation and/or other materials provided with the distribution.
13262153Sluigi *
14262153Sluigi * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``S IS''AND
15231650Sluigi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16262153Sluigi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17262153Sluigi * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18262153Sluigi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19262153Sluigi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20262153Sluigi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21262153Sluigi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22262153Sluigi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23262153Sluigi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24262153Sluigi * SUCH DAMAGE.
25231650Sluigi */
26231650Sluigi
27231650Sluigi/*
28231650Sluigi * $FreeBSD$
29231650Sluigi *
30235549Sluigi * Definitions of constants and the structures used by the netmap
31235549Sluigi * framework, for the part visible to both kernel and userspace.
32235549Sluigi * Detailed info on netmap is available with "man netmap" or at
33262153Sluigi *
34235549Sluigi *	http://info.iet.unipi.it/~luigi/netmap/
35257768Sluigi *
36257768Sluigi * This API is also used to communicate with the VALE software switch
37231650Sluigi */
38231650Sluigi
39231650Sluigi#ifndef _NET_NETMAP_H_
40231650Sluigi#define _NET_NETMAP_H_
41231650Sluigi
42262153Sluigi#define	NETMAP_API	11		/* current API version */
43262153Sluigi
44262153Sluigi#define	NETMAP_MIN_API	11		/* min and max versions accepted */
45262153Sluigi#define	NETMAP_MAX_API	15
46231650Sluigi/*
47262153Sluigi * Some fields should be cache-aligned to reduce contention.
48262153Sluigi * The alignment is architecture and OS dependent, but rather than
49262153Sluigi * digging into OS headers to find the exact value we use an estimate
50262153Sluigi * that should cover most architectures.
51262153Sluigi */
52262153Sluigi#define NM_CACHE_ALIGN	128
53262153Sluigi
54262153Sluigi/*
55231650Sluigi * --- Netmap data structures ---
56231650Sluigi *
57257768Sluigi * The userspace data structures used by netmap are shown below.
58257768Sluigi * They are allocated by the kernel and mmap()ed by userspace threads.
59257768Sluigi * Pointers are implemented as memory offsets or indexes,
60257768Sluigi * so that they can be easily dereferenced in kernel and userspace.
61231650Sluigi
62257768Sluigi   KERNEL (opaque, obviously)
63231650Sluigi
64235549Sluigi  ====================================================================
65235549Sluigi                                         |
66257768Sluigi   USERSPACE                             |      struct netmap_ring
67262153Sluigi                                         +---->+---------------+
68262153Sluigi                                             / | head,cur,tail |
69262153Sluigi   struct netmap_if (nifp, 1 per fd)        /  | buf_ofs       |
70262153Sluigi    +---------------+                      /   | other fields  |
71262153Sluigi    | ni_tx_rings   |                     /    +===============+
72262153Sluigi    | ni_rx_rings   |                    /     | buf_idx, len  | slot[0]
73262153Sluigi    |               |                   /      | flags, ptr    |
74262153Sluigi    |               |                  /       +---------------+
75262153Sluigi    +===============+                 /        | buf_idx, len  | slot[1]
76262153Sluigi    | txring_ofs[0] | (rel.to nifp)--'         | flags, ptr    |
77262153Sluigi    | txring_ofs[1] |                          +---------------+
78262153Sluigi     (tx+1 entries)                           (num_slots entries)
79262153Sluigi    | txring_ofs[t] |                          | buf_idx, len  | slot[n-1]
80262153Sluigi    +---------------+                          | flags, ptr    |
81262153Sluigi    | rxring_ofs[0] |                          +---------------+
82231650Sluigi    | rxring_ofs[1] |
83262153Sluigi     (rx+1 entries)
84257768Sluigi    | rxring_ofs[r] |
85231650Sluigi    +---------------+
86231650Sluigi
87262153Sluigi * For each "interface" (NIC, host stack, PIPE, VALE switch port) bound to
88262153Sluigi * a file descriptor, the mmap()ed region contains a (logically readonly)
89257768Sluigi * struct netmap_if pointing to struct netmap_ring's.
90262153Sluigi *
91257768Sluigi * There is one netmap_ring per physical NIC ring, plus one tx/rx ring
92262153Sluigi * pair attached to the host stack (this pair is unused for non-NIC ports).
93257768Sluigi *
94257768Sluigi * All physical/host stack ports share the same memory region,
95257768Sluigi * so that zero-copy can be implemented between them.
96257768Sluigi * VALE switch ports instead have separate memory regions.
97257768Sluigi *
98257768Sluigi * The netmap_ring is the userspace-visible replica of the NIC ring.
99257768Sluigi * Each slot has the index of a buffer (MTU-sized and residing in the
100257768Sluigi * mmapped region), its length and some flags. An extra 64-bit pointer
101257768Sluigi * is provided for user-supplied buffers in the tx path.
102257768Sluigi *
103231650Sluigi * In user space, the buffer address is computed as
104262153Sluigi *	(char *)ring + buf_ofs + index * NETMAP_BUF_SIZE
105231650Sluigi *
106262153Sluigi * Added in NETMAP_API 11:
107246355Sluigi *
108262153Sluigi * + NIOCREGIF can request the allocation of extra spare buffers from
109262153Sluigi *   the same memory pool. The desired number of buffers must be in
110262153Sluigi *   nr_arg3. The ioctl may return fewer buffers, depending on memory
111262153Sluigi *   availability. nr_arg3 will return the actual value, and, once
112262153Sluigi *   mapped, nifp->ni_bufs_head will be the index of the first buffer.
113257768Sluigi *
114262153Sluigi *   The buffers are linked to each other using the first uint32_t
115262153Sluigi *   as the index. On close, ni_bufs_head must point to the list of
116262153Sluigi *   buffers to be released.
117257768Sluigi *
118262153Sluigi * + NIOCREGIF can request space for extra rings (and buffers)
119262153Sluigi *   allocated in the same memory space. The number of extra rings
120262153Sluigi *   is in nr_arg1, and is advisory. This is a no-op on NICs where
121262153Sluigi *   the size of the memory space is fixed.
122257768Sluigi *
123262153Sluigi * + NIOCREGIF can attach to PIPE rings sharing the same memory
124262153Sluigi *   space with a parent device. The ifname indicates the parent device,
125262153Sluigi *   which must already exist. Flags in nr_flags indicate if we want to
126262153Sluigi *   bind the master or slave side, the index (from nr_ringid)
127262153Sluigi *   is just a cookie and does need to be sequential.
128257768Sluigi *
129262153Sluigi * + NIOCREGIF can also attach to 'monitor' rings that replicate
130262153Sluigi *   the content of specific rings, also from the same memory space.
131257768Sluigi *
132262153Sluigi *   Extra flags in nr_flags support the above functions.
133262153Sluigi *   Application libraries may use the following naming scheme:
134262153Sluigi *	netmap:foo			all NIC ring pairs
135262153Sluigi *	netmap:foo^			only host ring pair
136262153Sluigi *	netmap:foo+			all NIC ring + host ring pairs
137262153Sluigi *	netmap:foo-k			the k-th NIC ring pair
138262153Sluigi *	netmap:foo{k			PIPE ring pair k, master side
139262153Sluigi *	netmap:foo}k			PIPE ring pair k, slave side
140231650Sluigi */
141246355Sluigi
142262153Sluigi/*
143262153Sluigi * struct netmap_slot is a buffer descriptor
144262153Sluigi */
145231650Sluigistruct netmap_slot {
146257768Sluigi	uint32_t buf_idx;	/* buffer index */
147262153Sluigi	uint16_t len;		/* length for this slot */
148257768Sluigi	uint16_t flags;		/* buf changed, etc. */
149262153Sluigi	uint64_t ptr;		/* pointer for indirect buffers */
150262153Sluigi};
151262153Sluigi
152262153Sluigi/*
153262153Sluigi * The following flags control how the slot is used
154262153Sluigi */
155262153Sluigi
156257768Sluigi#define	NS_BUF_CHANGED	0x0001	/* buf_idx changed */
157262153Sluigi	/*
158262153Sluigi	 * must be set whenever buf_idx is changed (as it might be
159262153Sluigi	 * necessary to recompute the physical address and mapping)
160262153Sluigi	 */
161262153Sluigi
162262153Sluigi#define	NS_REPORT	0x0002	/* ask the hardware to report results */
163262153Sluigi	/*
164262153Sluigi	 * Request notification when slot is used by the hardware.
165262153Sluigi	 * Normally transmit completions are handled lazily and
166262153Sluigi	 * may be unreported. This flag lets us know when a slot
167262153Sluigi	 * has been sent (e.g. to terminate the sender).
168262153Sluigi	 */
169262153Sluigi
170262153Sluigi#define	NS_FORWARD	0x0004	/* pass packet 'forward' */
171262153Sluigi	/*
172262153Sluigi	 * (Only for physical ports, rx rings with NR_FORWARD set).
173262153Sluigi	 * Slot released to the kernel (i.e. before ring->head) with
174262153Sluigi	 * this flag set are passed to the peer ring (host/NIC),
175262153Sluigi	 * thus restoring the host-NIC connection for these slots.
176262153Sluigi	 * This supports efficient traffic monitoring or firewalling.
177262153Sluigi	 */
178262153Sluigi
179262153Sluigi#define	NS_NO_LEARN	0x0008	/* disable bridge learning */
180262153Sluigi 	/*
181262153Sluigi	 * On a VALE switch, do not 'learn' the source port for
182262153Sluigi 	 * this buffer.
183262153Sluigi	 */
184262153Sluigi
185262153Sluigi#define	NS_INDIRECT	0x0010	/* userspace buffer */
186262153Sluigi 	/*
187262153Sluigi	 * (VALE tx rings only) data is in a userspace buffer,
188262153Sluigi	 * whose address is in the 'ptr' field in the slot.
189262153Sluigi	 */
190262153Sluigi
191262153Sluigi#define	NS_MOREFRAG	0x0020	/* packet has more fragments */
192262153Sluigi 	/*
193262153Sluigi	 * (VALE ports only)
194262153Sluigi	 * Set on all but the last slot of a multi-segment packet.
195262153Sluigi	 * The 'len' field refers to the individual fragment.
196262153Sluigi	 */
197262153Sluigi
198247230Sluigi#define	NS_PORT_SHIFT	8
199247230Sluigi#define	NS_PORT_MASK	(0xff << NS_PORT_SHIFT)
200262153Sluigi	/*
201262153Sluigi 	 * The high 8 bits of the flag, if not zero, indicate the
202262153Sluigi	 * destination port for the VALE switch, overriding
203262153Sluigi 	 * the lookup table.
204262153Sluigi 	 */
205262153Sluigi
206257768Sluigi#define	NS_RFRAGS(_slot)	( ((_slot)->flags >> 8) & 0xff)
207262153Sluigi	/*
208262153Sluigi	 * (VALE rx rings only) the high 8 bits
209262153Sluigi	 *  are the number of fragments.
210262153Sluigi	 */
211231650Sluigi
212262153Sluigi
213231650Sluigi/*
214257768Sluigi * struct netmap_ring
215257768Sluigi *
216231650Sluigi * Netmap representation of a TX or RX ring (also known as "queue").
217231650Sluigi * This is a queue implemented as a fixed-size circular array.
218262153Sluigi * At the software level the important fields are: head, cur, tail.
219231650Sluigi *
220231650Sluigi * In TX rings:
221257768Sluigi *
222262153Sluigi *	head	first slot available for transmission.
223262153Sluigi *	cur	wakeup point. select() and poll() will unblock
224262153Sluigi *		when 'tail' moves past 'cur'
225262153Sluigi *	tail	(readonly) first slot reserved to the kernel
226257768Sluigi *
227262153Sluigi *	[head .. tail-1] can be used for new packets to send;
228262153Sluigi *	'head' and 'cur' must be incremented as slots are filled
229262153Sluigi *	    with new packets to be sent;
230262153Sluigi *	'cur' can be moved further ahead if we need more space
231262153Sluigi *	for new transmissions.
232231650Sluigi *
233257768Sluigi * In RX rings:
234231650Sluigi *
235262153Sluigi *	head	first valid received packet
236262153Sluigi *	cur	wakeup point. select() and poll() will unblock
237262153Sluigi *		when 'tail' moves past 'cur'
238262153Sluigi *	tail	(readonly) first slot reserved to the kernel
239257768Sluigi *
240262153Sluigi *	[head .. tail-1] contain received packets;
241262153Sluigi *	'head' and 'cur' must be incremented as slots are consumed
242262153Sluigi *		and can be returned to the kernel;
243262153Sluigi *	'cur' can be moved further ahead if we want to wait for
244262153Sluigi *		new packets without returning the previous ones.
245257768Sluigi *
246231650Sluigi * DATA OWNERSHIP/LOCKING:
247262153Sluigi *	The netmap_ring, and all slots and buffers in the range
248262153Sluigi *	[head .. tail-1] are owned by the user program;
249262153Sluigi *	the kernel only accesses them during a netmap system call
250262153Sluigi *	and in the user thread context.
251231650Sluigi *
252262153Sluigi *	Other slots and buffers are reserved for use by the kernel
253231650Sluigi */
254231650Sluigistruct netmap_ring {
255231650Sluigi	/*
256257768Sluigi	 * buf_ofs is meant to be used through macros.
257231650Sluigi	 * It contains the offset of the buffer region from this
258231650Sluigi	 * descriptor.
259231650Sluigi	 */
260262153Sluigi	const int64_t	buf_ofs;
261231650Sluigi	const uint32_t	num_slots;	/* number of slots in the ring. */
262262153Sluigi	const uint32_t	nr_buf_size;
263262153Sluigi	const uint16_t	ringid;
264262153Sluigi	const uint16_t	dir;		/* 0: tx, 1: rx */
265231650Sluigi
266262153Sluigi	uint32_t        head;		/* (u) first user slot */
267262153Sluigi	uint32_t        cur;		/* (u) wakeup point */
268262153Sluigi	uint32_t	tail;		/* (k) first kernel slot */
269231650Sluigi
270262153Sluigi	uint32_t	flags;
271231650Sluigi
272262153Sluigi	struct timeval	ts;		/* (k) time of last *sync() */
273262153Sluigi
274262153Sluigi	/* opaque room for a mutex or similar object */
275262153Sluigi	uint8_t		sem[128] __attribute__((__aligned__(NM_CACHE_ALIGN)));
276262153Sluigi
277231650Sluigi	/* the slots follow. This struct has variable size */
278235549Sluigi	struct netmap_slot slot[0];	/* array of slots. */
279231650Sluigi};
280231650Sluigi
281231650Sluigi
282231650Sluigi/*
283262153Sluigi * RING FLAGS
284262153Sluigi */
285262153Sluigi#define	NR_TIMESTAMP	0x0002		/* set timestamp on *sync() */
286262153Sluigi	/*
287262153Sluigi	 * updates the 'ts' field on each netmap syscall. This saves
288262153Sluigi	 * saves a separate gettimeofday(), and is not much worse than
289262153Sluigi	 * software timestamps generated in the interrupt handler.
290262153Sluigi	 */
291262153Sluigi
292262153Sluigi#define	NR_FORWARD	0x0004		/* enable NS_FORWARD for ring */
293262153Sluigi 	/*
294262153Sluigi	 * Enables the NS_FORWARD slot flag for the ring.
295262153Sluigi	 */
296262153Sluigi
297262153Sluigi
298262153Sluigi/*
299231650Sluigi * Netmap representation of an interface and its queue(s).
300257768Sluigi * This is initialized by the kernel when binding a file
301257768Sluigi * descriptor to a port, and should be considered as readonly
302257768Sluigi * by user programs. The kernel never uses it.
303257768Sluigi *
304231650Sluigi * There is one netmap_if for each file descriptor on which we want
305257768Sluigi * to select/poll.
306231650Sluigi * select/poll operates on one or all pairs depending on the value of
307231650Sluigi * nmr_queueid passed on the ioctl.
308231650Sluigi */
309231650Sluigistruct netmap_if {
310235549Sluigi	char		ni_name[IFNAMSIZ]; /* name of the interface. */
311257768Sluigi	const uint32_t	ni_version;	/* API version, currently unused */
312257768Sluigi	const uint32_t	ni_flags;	/* properties */
313257768Sluigi#define	NI_PRIV_MEM	0x1		/* private memory region */
314257768Sluigi
315231650Sluigi	/*
316262153Sluigi	 * The number of packet rings available in netmap mode.
317262153Sluigi	 * Physical NICs can have different numbers of tx and rx rings.
318262153Sluigi	 * Physical NICs also have a 'host' ring pair.
319262153Sluigi	 * Additionally, clients can request additional ring pairs to
320262153Sluigi	 * be used for internal communication.
321262153Sluigi	 */
322262153Sluigi	const uint32_t	ni_tx_rings;	/* number of HW tx rings */
323262153Sluigi	const uint32_t	ni_rx_rings;	/* number of HW rx rings */
324262153Sluigi
325262153Sluigi	uint32_t	ni_bufs_head;	/* head index for extra bufs */
326262153Sluigi	uint32_t	ni_spare1[5];
327262153Sluigi	/*
328235549Sluigi	 * The following array contains the offset of each netmap ring
329262153Sluigi	 * from this structure, in the following order:
330262153Sluigi	 * NIC tx rings (ni_tx_rings); host tx ring (1); extra tx rings;
331262153Sluigi	 * NIC rx rings (ni_rx_rings); host tx ring (1); extra rx rings.
332262153Sluigi	 *
333257768Sluigi	 * The area is filled up by the kernel on NIOCREGIF,
334231650Sluigi	 * and then only read by userspace code.
335231650Sluigi	 */
336231650Sluigi	const ssize_t	ring_ofs[0];
337231650Sluigi};
338231650Sluigi
339262153Sluigi
340262153Sluigi#ifndef NIOCREGIF
341231650Sluigi/*
342231650Sluigi * ioctl names and related fields
343231650Sluigi *
344262153Sluigi * NIOCTXSYNC, NIOCRXSYNC synchronize tx or rx queues,
345262153Sluigi *	whose identity is set in NIOCREGIF through nr_ringid.
346262153Sluigi *	These are non blocking and take no argument.
347262153Sluigi *
348231650Sluigi * NIOCGINFO takes a struct ifreq, the interface name is the input,
349231650Sluigi *	the outputs are number of queues and number of descriptor
350231650Sluigi *	for each queue (useful to set number of threads etc.).
351257768Sluigi *	The info returned is only advisory and may change before
352257768Sluigi *	the interface is bound to a file descriptor.
353231650Sluigi *
354262153Sluigi * NIOCREGIF takes an interface name within a struct nmre,
355231650Sluigi *	and activates netmap mode on the interface (if possible).
356231650Sluigi *
357262153Sluigi * The argument to NIOCGINFO/NIOCREGIF overlays struct ifreq so we
358262153Sluigi * can pass it down to other NIC-related ioctls.
359231650Sluigi *
360262153Sluigi * The actual argument (struct nmreq) has a number of options to request
361262153Sluigi * different functions.
362262153Sluigi * The following are used in NIOCREGIF when nr_cmd == 0:
363257768Sluigi *
364262153Sluigi * nr_name	(in)
365262153Sluigi *	The name of the port (em0, valeXXX:YYY, etc.)
366262153Sluigi *	limited to IFNAMSIZ for backward compatibility.
367257768Sluigi *
368262153Sluigi * nr_version	(in/out)
369262153Sluigi *	Must match NETMAP_API as used in the kernel, error otherwise.
370262153Sluigi *	Always returns the desired value on output.
371257768Sluigi *
372262153Sluigi * nr_tx_slots, nr_tx_slots, nr_tx_rings, nr_rx_rings (in/out)
373262153Sluigi *	On input, non-zero values may be used to reconfigure the port
374262153Sluigi *	according to the requested values, but this is not guaranteed.
375262153Sluigi *	On output the actual values in use are reported.
376257768Sluigi *
377262153Sluigi * nr_ringid (in)
378262153Sluigi *	Indicates how rings should be bound to the file descriptors.
379262153Sluigi *	If nr_flags != 0, then the low bits (in NETMAP_RING_MASK)
380262153Sluigi *	are used to indicate the ring number, and nr_flags specifies
381262153Sluigi *	the actual rings to bind. NETMAP_NO_TX_POLL is unaffected.
382257768Sluigi *
383262153Sluigi *	NOTE: THE FOLLOWING (nr_flags == 0) IS DEPRECATED:
384262153Sluigi *	If nr_flags == 0, NETMAP_HW_RING and NETMAP_SW_RING control
385262153Sluigi *	the binding as follows:
386262153Sluigi *	0 (default)			binds all physical rings
387262153Sluigi *	NETMAP_HW_RING | ring number	binds a single ring pair
388262153Sluigi *	NETMAP_SW_RING			binds only the host tx/rx rings
389257768Sluigi *
390262153Sluigi *	NETMAP_NO_TX_POLL can be OR-ed to make select()/poll() push
391262153Sluigi *		packets on tx rings only if POLLOUT is set.
392262153Sluigi *		The default is to push any pending packet.
393257768Sluigi *
394262153Sluigi *	NETMAP_DO_RX_POLL can be OR-ed to make select()/poll() release
395262153Sluigi *		packets on rx rings also when POLLIN is NOT set.
396262153Sluigi *		The default is to touch the rx ring only with POLLIN.
397262153Sluigi *		Note that this is the opposite of TX because it
398262153Sluigi *		reflects the common usage.
399262153Sluigi *
400262153Sluigi *	NOTE: NETMAP_PRIV_MEM IS DEPRECATED, use nr_arg2 instead.
401262153Sluigi *	NETMAP_PRIV_MEM is set on return for ports that do not use
402262153Sluigi *		the global memory allocator.
403262153Sluigi *		This information is not significant and applications
404262153Sluigi *		should look at the region id in nr_arg2
405262153Sluigi *
406262153Sluigi * nr_flags	is the recommended mode to indicate which rings should
407262153Sluigi *		be bound to a file descriptor. Values are NR_REG_*
408262153Sluigi *
409262153Sluigi * nr_arg1 (in)	The number of extra rings to be reserved.
410262153Sluigi *		Especially when allocating a VALE port the system only
411262153Sluigi *		allocates the amount of memory needed for the port.
412262153Sluigi *		If more shared memory rings are desired (e.g. for pipes),
413262153Sluigi *		the first invocation for the same basename/allocator
414262153Sluigi *		should specify a suitable number. Memory cannot be
415262153Sluigi *		extended after the first allocation without closing
416262153Sluigi *		all ports on the same region.
417262153Sluigi *
418262153Sluigi * nr_arg2 (in/out) The identity of the memory region used.
419262153Sluigi *		On input, 0 means the system decides autonomously,
420262153Sluigi *		other values may try to select a specific region.
421262153Sluigi *		On return the actual value is reported.
422262153Sluigi *		Region '1' is the global allocator, normally shared
423262153Sluigi *		by all interfaces. Other values are private regions.
424262153Sluigi *		If two ports the same region zero-copy is possible.
425262153Sluigi *
426262153Sluigi * nr_arg3 (in/out)	number of extra buffers to be allocated.
427262153Sluigi *
428262153Sluigi *
429262153Sluigi *
430262153Sluigi * nr_cmd (in)	if non-zero indicates a special command:
431262153Sluigi *	NETMAP_BDG_ATTACH	 and nr_name = vale*:ifname
432262153Sluigi *		attaches the NIC to the switch; nr_ringid specifies
433262153Sluigi *		which rings to use. Used by vale-ctl -a ...
434262153Sluigi *	    nr_arg1 = NETMAP_BDG_HOST also attaches the host port
435262153Sluigi *		as in vale-ctl -h ...
436262153Sluigi *
437262153Sluigi *	NETMAP_BDG_DETACH	and nr_name = vale*:ifname
438262153Sluigi *		disconnects a previously attached NIC.
439262153Sluigi *		Used by vale-ctl -d ...
440262153Sluigi *
441262153Sluigi *	NETMAP_BDG_LIST
442262153Sluigi *		list the configuration of VALE switches.
443262153Sluigi *
444262153Sluigi *	NETMAP_BDG_VNET_HDR
445262153Sluigi *		Set the virtio-net header length used by the client
446262153Sluigi *		of a VALE switch port.
447262153Sluigi *
448262153Sluigi * nr_arg1, nr_arg2, nr_arg3  (in/out)		command specific
449262153Sluigi *
450262153Sluigi *
451262153Sluigi *
452231650Sluigi */
453231650Sluigi
454262153Sluigi
455231650Sluigi/*
456262153Sluigi * struct nmreq overlays a struct ifreq (just the name)
457262153Sluigi *
458262153Sluigi * On input, nr_ringid indicates which rings we are requesting,
459262153Sluigi * with the low flags for the specific ring number.
460262153Sluigi * selection			FLAGS	RING INDEX
461262153Sluigi *
462262153Sluigi *	all the NIC rings	0x0000	-
463262153Sluigi *	only HOST ring		0x2000	ring index
464262153Sluigi *	single NIC ring		0x4000	-
465262153Sluigi *	all the NIC+HOST rings	0x6000	-
466262153Sluigi *	one pipe ring, master	0x8000	ring index
467262153Sluigi *	*** INVALID		0xA000
468262153Sluigi *	one pipe ring, slave	0xC000	ring index
469262153Sluigi *	*** INVALID		0xE000
470262153Sluigi *
471231650Sluigi */
472231650Sluigistruct nmreq {
473231650Sluigi	char		nr_name[IFNAMSIZ];
474235549Sluigi	uint32_t	nr_version;	/* API version */
475231650Sluigi	uint32_t	nr_offset;	/* nifp offset in the shared region */
476231650Sluigi	uint32_t	nr_memsize;	/* size of the shared region */
477235549Sluigi	uint32_t	nr_tx_slots;	/* slots in tx rings */
478235549Sluigi	uint32_t	nr_rx_slots;	/* slots in rx rings */
479235549Sluigi	uint16_t	nr_tx_rings;	/* number of tx rings */
480235549Sluigi	uint16_t	nr_rx_rings;	/* number of rx rings */
481262153Sluigi
482231650Sluigi	uint16_t	nr_ringid;	/* ring(s) we care about */
483262153Sluigi#define NETMAP_HW_RING		0x4000	/* single NIC ring pair */
484262153Sluigi#define NETMAP_SW_RING		0x2000	/* only host ring pair */
485262153Sluigi
486262153Sluigi#define NETMAP_RING_MASK	0x0fff	/* the ring number */
487262153Sluigi
488235549Sluigi#define NETMAP_NO_TX_POLL	0x1000	/* no automatic txsync on poll */
489262153Sluigi
490262153Sluigi#define NETMAP_DO_RX_POLL	0x8000	/* DO automatic rxsync on poll */
491262153Sluigi
492257768Sluigi	uint16_t	nr_cmd;
493257768Sluigi#define NETMAP_BDG_ATTACH	1	/* attach the NIC */
494257768Sluigi#define NETMAP_BDG_DETACH	2	/* detach the NIC */
495257768Sluigi#define NETMAP_BDG_LOOKUP_REG	3	/* register lookup function */
496257768Sluigi#define NETMAP_BDG_LIST		4	/* get bridge's info */
497262153Sluigi#define NETMAP_BDG_VNET_HDR     5       /* set the port virtio-net-hdr length */
498262153Sluigi#define NETMAP_BDG_OFFSET	NETMAP_BDG_VNET_HDR	/* deprecated alias */
499262153Sluigi
500262153Sluigi	uint16_t	nr_arg1;	/* reserve extra rings in NIOCREGIF */
501257768Sluigi#define NETMAP_BDG_HOST		1	/* attach the host stack on ATTACH */
502262153Sluigi
503257768Sluigi	uint16_t	nr_arg2;
504262153Sluigi	uint32_t	nr_arg3;	/* req. extra buffers in NIOCREGIF */
505262153Sluigi	uint32_t	nr_flags;
506262153Sluigi	/* various modes, extends nr_ringid */
507262153Sluigi	uint32_t	spare2[1];
508231650Sluigi};
509231650Sluigi
510262153Sluigi#define NR_REG_MASK		0xf /* values for nr_flags */
511262153Sluigienum {	NR_REG_DEFAULT	= 0,	/* backward compat, should not be used. */
512262153Sluigi	NR_REG_ALL_NIC	= 1,
513262153Sluigi	NR_REG_SW	= 2,
514262153Sluigi	NR_REG_NIC_SW	= 3,
515262153Sluigi	NR_REG_ONE_NIC	= 4,
516262153Sluigi	NR_REG_PIPE_MASTER = 5,
517262153Sluigi	NR_REG_PIPE_SLAVE = 6,
518262153Sluigi};
519262153Sluigi/* monitor uses the NR_REG to select the rings to monitor */
520262153Sluigi#define NR_MONITOR_TX	0x100
521262153Sluigi#define NR_MONITOR_RX	0x200
522262153Sluigi
523262153Sluigi
524235549Sluigi/*
525235549Sluigi * FreeBSD uses the size value embedded in the _IOWR to determine
526235549Sluigi * how much to copy in/out. So we need it to match the actual
527235549Sluigi * data structure we pass. We put some spares in the structure
528235549Sluigi * to ease compatibility with other versions
529235549Sluigi */
530231650Sluigi#define NIOCGINFO	_IOWR('i', 145, struct nmreq) /* return IF info */
531231650Sluigi#define NIOCREGIF	_IOWR('i', 146, struct nmreq) /* interface register */
532231650Sluigi#define NIOCTXSYNC	_IO('i', 148) /* sync tx queues */
533231650Sluigi#define NIOCRXSYNC	_IO('i', 149) /* sync rx queues */
534231650Sluigi#endif /* !NIOCREGIF */
535231650Sluigi
536262153Sluigi
537262153Sluigi/*
538262153Sluigi * Helper functions for kernel and userspace
539262153Sluigi */
540262153Sluigi
541262153Sluigi/*
542262153Sluigi * check if space is available in the ring.
543262153Sluigi */
544262153Sluigistatic inline int
545262153Sluiginm_ring_empty(struct netmap_ring *ring)
546262153Sluigi{
547262153Sluigi	return (ring->cur == ring->tail);
548262153Sluigi}
549262153Sluigi
550231650Sluigi#endif /* _NET_NETMAP_H_ */
551