1227614Sluigi/*
2260368Sluigi * Copyright (C) 2011-2014 Matteo Landi, Luigi Rizzo. All rights reserved.
3259412Sluigi *
4227614Sluigi * Redistribution and use in source and binary forms, with or without
5259412Sluigi * modification, are permitted provided that the following conditions
6259412Sluigi * are met:
7259412Sluigi *
8227614Sluigi *   1. Redistributions of source code must retain the above copyright
9227614Sluigi *      notice, this list of conditions and the following disclaimer.
10227614Sluigi *   2. Redistributions in binary form must reproduce the above copyright
11227614Sluigi *      notice, this list of conditions and the following disclaimer in the
12259412Sluigi *      documentation and/or other materials provided with the distribution.
13259412Sluigi *
14259412Sluigi * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``S IS''AND
15227614Sluigi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16259412Sluigi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17259412Sluigi * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18259412Sluigi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19259412Sluigi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20259412Sluigi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21259412Sluigi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22259412Sluigi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23259412Sluigi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24259412Sluigi * SUCH DAMAGE.
25227614Sluigi */
26227614Sluigi
27227614Sluigi/*
28227614Sluigi * $FreeBSD: releng/11.0/sys/net/netmap.h 285349 2015-07-10 05:51:36Z luigi $
29227614Sluigi *
30232238Sluigi * Definitions of constants and the structures used by the netmap
31232238Sluigi * framework, for the part visible to both kernel and userspace.
32232238Sluigi * Detailed info on netmap is available with "man netmap" or at
33259412Sluigi *
34232238Sluigi *	http://info.iet.unipi.it/~luigi/netmap/
35257529Sluigi *
36257529Sluigi * This API is also used to communicate with the VALE software switch
37227614Sluigi */
38227614Sluigi
39227614Sluigi#ifndef _NET_NETMAP_H_
40227614Sluigi#define _NET_NETMAP_H_
41227614Sluigi
42261909Sluigi#define	NETMAP_API	11		/* current API version */
43260368Sluigi
44261909Sluigi#define	NETMAP_MIN_API	11		/* min and max versions accepted */
45261909Sluigi#define	NETMAP_MAX_API	15
46227614Sluigi/*
47260368Sluigi * Some fields should be cache-aligned to reduce contention.
48260368Sluigi * The alignment is architecture and OS dependent, but rather than
49260368Sluigi * digging into OS headers to find the exact value we use an estimate
50260368Sluigi * that should cover most architectures.
51260368Sluigi */
52260368Sluigi#define NM_CACHE_ALIGN	128
53260368Sluigi
54260368Sluigi/*
55227614Sluigi * --- Netmap data structures ---
56227614Sluigi *
57257529Sluigi * The userspace data structures used by netmap are shown below.
58257529Sluigi * They are allocated by the kernel and mmap()ed by userspace threads.
59257529Sluigi * Pointers are implemented as memory offsets or indexes,
60257529Sluigi * so that they can be easily dereferenced in kernel and userspace.
61227614Sluigi
62257529Sluigi   KERNEL (opaque, obviously)
63227614Sluigi
64232238Sluigi  ====================================================================
65232238Sluigi                                         |
66257529Sluigi   USERSPACE                             |      struct netmap_ring
67260368Sluigi                                         +---->+---------------+
68260368Sluigi                                             / | head,cur,tail |
69260368Sluigi   struct netmap_if (nifp, 1 per fd)        /  | buf_ofs       |
70260368Sluigi    +---------------+                      /   | other fields  |
71260368Sluigi    | ni_tx_rings   |                     /    +===============+
72260368Sluigi    | ni_rx_rings   |                    /     | buf_idx, len  | slot[0]
73260368Sluigi    |               |                   /      | flags, ptr    |
74260368Sluigi    |               |                  /       +---------------+
75260368Sluigi    +===============+                 /        | buf_idx, len  | slot[1]
76260368Sluigi    | txring_ofs[0] | (rel.to nifp)--'         | flags, ptr    |
77260368Sluigi    | txring_ofs[1] |                          +---------------+
78261909Sluigi     (tx+1 entries)                           (num_slots entries)
79260368Sluigi    | txring_ofs[t] |                          | buf_idx, len  | slot[n-1]
80260368Sluigi    +---------------+                          | flags, ptr    |
81260368Sluigi    | rxring_ofs[0] |                          +---------------+
82227614Sluigi    | rxring_ofs[1] |
83261909Sluigi     (rx+1 entries)
84257529Sluigi    | rxring_ofs[r] |
85227614Sluigi    +---------------+
86227614Sluigi
87261909Sluigi * For each "interface" (NIC, host stack, PIPE, VALE switch port) bound to
88261909Sluigi * a file descriptor, the mmap()ed region contains a (logically readonly)
89257529Sluigi * struct netmap_if pointing to struct netmap_ring's.
90261909Sluigi *
91257529Sluigi * There is one netmap_ring per physical NIC ring, plus one tx/rx ring
92261909Sluigi * pair attached to the host stack (this pair is unused for non-NIC ports).
93257529Sluigi *
94257529Sluigi * All physical/host stack ports share the same memory region,
95257529Sluigi * so that zero-copy can be implemented between them.
96257529Sluigi * VALE switch ports instead have separate memory regions.
97257529Sluigi *
98257529Sluigi * The netmap_ring is the userspace-visible replica of the NIC ring.
99257529Sluigi * Each slot has the index of a buffer (MTU-sized and residing in the
100257529Sluigi * mmapped region), its length and some flags. An extra 64-bit pointer
101257529Sluigi * is provided for user-supplied buffers in the tx path.
102257529Sluigi *
103227614Sluigi * In user space, the buffer address is computed as
104261909Sluigi *	(char *)ring + buf_ofs + index * NETMAP_BUF_SIZE
105261909Sluigi *
106261909Sluigi * Added in NETMAP_API 11:
107261909Sluigi *
108261909Sluigi * + NIOCREGIF can request the allocation of extra spare buffers from
109261909Sluigi *   the same memory pool. The desired number of buffers must be in
110261909Sluigi *   nr_arg3. The ioctl may return fewer buffers, depending on memory
111261909Sluigi *   availability. nr_arg3 will return the actual value, and, once
112261909Sluigi *   mapped, nifp->ni_bufs_head will be the index of the first buffer.
113261909Sluigi *
114261909Sluigi *   The buffers are linked to each other using the first uint32_t
115261909Sluigi *   as the index. On close, ni_bufs_head must point to the list of
116261909Sluigi *   buffers to be released.
117261909Sluigi *
118261909Sluigi * + NIOCREGIF can request space for extra rings (and buffers)
119261909Sluigi *   allocated in the same memory space. The number of extra rings
120261909Sluigi *   is in nr_arg1, and is advisory. This is a no-op on NICs where
121261909Sluigi *   the size of the memory space is fixed.
122261909Sluigi *
123261909Sluigi * + NIOCREGIF can attach to PIPE rings sharing the same memory
124261909Sluigi *   space with a parent device. The ifname indicates the parent device,
125261909Sluigi *   which must already exist. Flags in nr_flags indicate if we want to
126261909Sluigi *   bind the master or slave side, the index (from nr_ringid)
127267167Sluigi *   is just a cookie and does not need to be sequential.
128261909Sluigi *
129261909Sluigi * + NIOCREGIF can also attach to 'monitor' rings that replicate
130261909Sluigi *   the content of specific rings, also from the same memory space.
131261909Sluigi *
132261909Sluigi *   Extra flags in nr_flags support the above functions.
133261909Sluigi *   Application libraries may use the following naming scheme:
134261909Sluigi *	netmap:foo			all NIC ring pairs
135261909Sluigi *	netmap:foo^			only host ring pair
136261909Sluigi *	netmap:foo+			all NIC ring + host ring pairs
137261909Sluigi *	netmap:foo-k			the k-th NIC ring pair
138261909Sluigi *	netmap:foo{k			PIPE ring pair k, master side
139261909Sluigi *	netmap:foo}k			PIPE ring pair k, slave side
140257529Sluigi */
141257529Sluigi
142257529Sluigi/*
143257529Sluigi * struct netmap_slot is a buffer descriptor
144227614Sluigi */
145227614Sluigistruct netmap_slot {
146257529Sluigi	uint32_t buf_idx;	/* buffer index */
147260368Sluigi	uint16_t len;		/* length for this slot */
148257529Sluigi	uint16_t flags;		/* buf changed, etc. */
149260368Sluigi	uint64_t ptr;		/* pointer for indirect buffers */
150260368Sluigi};
151260368Sluigi
152260368Sluigi/*
153260368Sluigi * The following flags control how the slot is used
154260368Sluigi */
155260368Sluigi
156257529Sluigi#define	NS_BUF_CHANGED	0x0001	/* buf_idx changed */
157260368Sluigi	/*
158260368Sluigi	 * must be set whenever buf_idx is changed (as it might be
159260368Sluigi	 * necessary to recompute the physical address and mapping)
160285349Sluigi	 *
161285349Sluigi	 * It is also set by the kernel whenever the buf_idx is
162285349Sluigi	 * changed internally (e.g., by pipes). Applications may
163285349Sluigi	 * use this information to know when they can reuse the
164285349Sluigi	 * contents of previously prepared buffers.
165260368Sluigi	 */
166260368Sluigi
167260368Sluigi#define	NS_REPORT	0x0002	/* ask the hardware to report results */
168260368Sluigi	/*
169260368Sluigi	 * Request notification when slot is used by the hardware.
170260368Sluigi	 * Normally transmit completions are handled lazily and
171260368Sluigi	 * may be unreported. This flag lets us know when a slot
172260368Sluigi	 * has been sent (e.g. to terminate the sender).
173260368Sluigi	 */
174260368Sluigi
175260368Sluigi#define	NS_FORWARD	0x0004	/* pass packet 'forward' */
176260368Sluigi	/*
177260368Sluigi	 * (Only for physical ports, rx rings with NR_FORWARD set).
178260368Sluigi	 * Slot released to the kernel (i.e. before ring->head) with
179260368Sluigi	 * this flag set are passed to the peer ring (host/NIC),
180260368Sluigi	 * thus restoring the host-NIC connection for these slots.
181260368Sluigi	 * This supports efficient traffic monitoring or firewalling.
182260368Sluigi	 */
183260368Sluigi
184260368Sluigi#define	NS_NO_LEARN	0x0008	/* disable bridge learning */
185260368Sluigi 	/*
186260368Sluigi	 * On a VALE switch, do not 'learn' the source port for
187260368Sluigi 	 * this buffer.
188260368Sluigi	 */
189260368Sluigi
190260368Sluigi#define	NS_INDIRECT	0x0010	/* userspace buffer */
191260368Sluigi 	/*
192260368Sluigi	 * (VALE tx rings only) data is in a userspace buffer,
193260368Sluigi	 * whose address is in the 'ptr' field in the slot.
194260368Sluigi	 */
195260368Sluigi
196260368Sluigi#define	NS_MOREFRAG	0x0020	/* packet has more fragments */
197260368Sluigi 	/*
198260368Sluigi	 * (VALE ports only)
199260368Sluigi	 * Set on all but the last slot of a multi-segment packet.
200260368Sluigi	 * The 'len' field refers to the individual fragment.
201260368Sluigi	 */
202260368Sluigi
203245834Sluigi#define	NS_PORT_SHIFT	8
204245834Sluigi#define	NS_PORT_MASK	(0xff << NS_PORT_SHIFT)
205260368Sluigi	/*
206260368Sluigi 	 * The high 8 bits of the flag, if not zero, indicate the
207260368Sluigi	 * destination port for the VALE switch, overriding
208260368Sluigi 	 * the lookup table.
209260368Sluigi 	 */
210260368Sluigi
211257529Sluigi#define	NS_RFRAGS(_slot)	( ((_slot)->flags >> 8) & 0xff)
212260368Sluigi	/*
213260368Sluigi	 * (VALE rx rings only) the high 8 bits
214260368Sluigi	 *  are the number of fragments.
215260368Sluigi	 */
216227614Sluigi
217260368Sluigi
218227614Sluigi/*
219257529Sluigi * struct netmap_ring
220257529Sluigi *
221227614Sluigi * Netmap representation of a TX or RX ring (also known as "queue").
222227614Sluigi * This is a queue implemented as a fixed-size circular array.
223260368Sluigi * At the software level the important fields are: head, cur, tail.
224227614Sluigi *
225227614Sluigi * In TX rings:
226257529Sluigi *
227260368Sluigi *	head	first slot available for transmission.
228260368Sluigi *	cur	wakeup point. select() and poll() will unblock
229260368Sluigi *		when 'tail' moves past 'cur'
230260368Sluigi *	tail	(readonly) first slot reserved to the kernel
231257529Sluigi *
232260368Sluigi *	[head .. tail-1] can be used for new packets to send;
233260368Sluigi *	'head' and 'cur' must be incremented as slots are filled
234260368Sluigi *	    with new packets to be sent;
235260368Sluigi *	'cur' can be moved further ahead if we need more space
236267167Sluigi *	for new transmissions. XXX todo (2014-03-12)
237227614Sluigi *
238257529Sluigi * In RX rings:
239227614Sluigi *
240260368Sluigi *	head	first valid received packet
241260368Sluigi *	cur	wakeup point. select() and poll() will unblock
242260368Sluigi *		when 'tail' moves past 'cur'
243260368Sluigi *	tail	(readonly) first slot reserved to the kernel
244257529Sluigi *
245260368Sluigi *	[head .. tail-1] contain received packets;
246260368Sluigi *	'head' and 'cur' must be incremented as slots are consumed
247260368Sluigi *		and can be returned to the kernel;
248260368Sluigi *	'cur' can be moved further ahead if we want to wait for
249260368Sluigi *		new packets without returning the previous ones.
250257529Sluigi *
251227614Sluigi * DATA OWNERSHIP/LOCKING:
252260368Sluigi *	The netmap_ring, and all slots and buffers in the range
253260368Sluigi *	[head .. tail-1] are owned by the user program;
254260368Sluigi *	the kernel only accesses them during a netmap system call
255260368Sluigi *	and in the user thread context.
256227614Sluigi *
257260368Sluigi *	Other slots and buffers are reserved for use by the kernel
258227614Sluigi */
259227614Sluigistruct netmap_ring {
260227614Sluigi	/*
261257529Sluigi	 * buf_ofs is meant to be used through macros.
262227614Sluigi	 * It contains the offset of the buffer region from this
263227614Sluigi	 * descriptor.
264227614Sluigi	 */
265260368Sluigi	const int64_t	buf_ofs;
266227614Sluigi	const uint32_t	num_slots;	/* number of slots in the ring. */
267260368Sluigi	const uint32_t	nr_buf_size;
268260368Sluigi	const uint16_t	ringid;
269260368Sluigi	const uint16_t	dir;		/* 0: tx, 1: rx */
270227614Sluigi
271260368Sluigi	uint32_t        head;		/* (u) first user slot */
272260368Sluigi	uint32_t        cur;		/* (u) wakeup point */
273260368Sluigi	uint32_t	tail;		/* (k) first kernel slot */
274227614Sluigi
275260368Sluigi	uint32_t	flags;
276227614Sluigi
277260368Sluigi	struct timeval	ts;		/* (k) time of last *sync() */
278260368Sluigi
279260368Sluigi	/* opaque room for a mutex or similar object */
280260368Sluigi	uint8_t		sem[128] __attribute__((__aligned__(NM_CACHE_ALIGN)));
281260368Sluigi
282227614Sluigi	/* the slots follow. This struct has variable size */
283232238Sluigi	struct netmap_slot slot[0];	/* array of slots. */
284227614Sluigi};
285227614Sluigi
286227614Sluigi
287227614Sluigi/*
288260368Sluigi * RING FLAGS
289260368Sluigi */
290260368Sluigi#define	NR_TIMESTAMP	0x0002		/* set timestamp on *sync() */
291260368Sluigi	/*
292260368Sluigi	 * updates the 'ts' field on each netmap syscall. This saves
293260368Sluigi	 * saves a separate gettimeofday(), and is not much worse than
294260368Sluigi	 * software timestamps generated in the interrupt handler.
295260368Sluigi	 */
296260368Sluigi
297260368Sluigi#define	NR_FORWARD	0x0004		/* enable NS_FORWARD for ring */
298260368Sluigi 	/*
299260368Sluigi	 * Enables the NS_FORWARD slot flag for the ring.
300260368Sluigi	 */
301260368Sluigi
302260368Sluigi
303260368Sluigi/*
304227614Sluigi * Netmap representation of an interface and its queue(s).
305257529Sluigi * This is initialized by the kernel when binding a file
306257529Sluigi * descriptor to a port, and should be considered as readonly
307257529Sluigi * by user programs. The kernel never uses it.
308257529Sluigi *
309227614Sluigi * There is one netmap_if for each file descriptor on which we want
310257529Sluigi * to select/poll.
311227614Sluigi * select/poll operates on one or all pairs depending on the value of
312227614Sluigi * nmr_queueid passed on the ioctl.
313227614Sluigi */
314227614Sluigistruct netmap_if {
315232238Sluigi	char		ni_name[IFNAMSIZ]; /* name of the interface. */
316257529Sluigi	const uint32_t	ni_version;	/* API version, currently unused */
317257529Sluigi	const uint32_t	ni_flags;	/* properties */
318257529Sluigi#define	NI_PRIV_MEM	0x1		/* private memory region */
319257529Sluigi
320227614Sluigi	/*
321260368Sluigi	 * The number of packet rings available in netmap mode.
322260368Sluigi	 * Physical NICs can have different numbers of tx and rx rings.
323260368Sluigi	 * Physical NICs also have a 'host' ring pair.
324260368Sluigi	 * Additionally, clients can request additional ring pairs to
325260368Sluigi	 * be used for internal communication.
326260368Sluigi	 */
327260368Sluigi	const uint32_t	ni_tx_rings;	/* number of HW tx rings */
328260368Sluigi	const uint32_t	ni_rx_rings;	/* number of HW rx rings */
329260368Sluigi
330261909Sluigi	uint32_t	ni_bufs_head;	/* head index for extra bufs */
331261909Sluigi	uint32_t	ni_spare1[5];
332260368Sluigi	/*
333232238Sluigi	 * The following array contains the offset of each netmap ring
334260368Sluigi	 * from this structure, in the following order:
335260368Sluigi	 * NIC tx rings (ni_tx_rings); host tx ring (1); extra tx rings;
336260368Sluigi	 * NIC rx rings (ni_rx_rings); host tx ring (1); extra rx rings.
337260368Sluigi	 *
338257529Sluigi	 * The area is filled up by the kernel on NIOCREGIF,
339227614Sluigi	 * and then only read by userspace code.
340227614Sluigi	 */
341227614Sluigi	const ssize_t	ring_ofs[0];
342227614Sluigi};
343227614Sluigi
344260368Sluigi
345259412Sluigi#ifndef NIOCREGIF
346227614Sluigi/*
347227614Sluigi * ioctl names and related fields
348227614Sluigi *
349260368Sluigi * NIOCTXSYNC, NIOCRXSYNC synchronize tx or rx queues,
350260368Sluigi *	whose identity is set in NIOCREGIF through nr_ringid.
351260368Sluigi *	These are non blocking and take no argument.
352260368Sluigi *
353227614Sluigi * NIOCGINFO takes a struct ifreq, the interface name is the input,
354227614Sluigi *	the outputs are number of queues and number of descriptor
355227614Sluigi *	for each queue (useful to set number of threads etc.).
356257529Sluigi *	The info returned is only advisory and may change before
357257529Sluigi *	the interface is bound to a file descriptor.
358227614Sluigi *
359260368Sluigi * NIOCREGIF takes an interface name within a struct nmre,
360227614Sluigi *	and activates netmap mode on the interface (if possible).
361227614Sluigi *
362260368Sluigi * The argument to NIOCGINFO/NIOCREGIF overlays struct ifreq so we
363260368Sluigi * can pass it down to other NIC-related ioctls.
364251139Sluigi *
365260368Sluigi * The actual argument (struct nmreq) has a number of options to request
366260368Sluigi * different functions.
367261909Sluigi * The following are used in NIOCREGIF when nr_cmd == 0:
368251139Sluigi *
369260368Sluigi * nr_name	(in)
370260368Sluigi *	The name of the port (em0, valeXXX:YYY, etc.)
371260368Sluigi *	limited to IFNAMSIZ for backward compatibility.
372227614Sluigi *
373260368Sluigi * nr_version	(in/out)
374260368Sluigi *	Must match NETMAP_API as used in the kernel, error otherwise.
375260368Sluigi *	Always returns the desired value on output.
376257529Sluigi *
377260368Sluigi * nr_tx_slots, nr_tx_slots, nr_tx_rings, nr_rx_rings (in/out)
378260368Sluigi *	On input, non-zero values may be used to reconfigure the port
379260368Sluigi *	according to the requested values, but this is not guaranteed.
380260368Sluigi *	On output the actual values in use are reported.
381259412Sluigi *
382260368Sluigi * nr_ringid (in)
383260368Sluigi *	Indicates how rings should be bound to the file descriptors.
384261909Sluigi *	If nr_flags != 0, then the low bits (in NETMAP_RING_MASK)
385261909Sluigi *	are used to indicate the ring number, and nr_flags specifies
386261909Sluigi *	the actual rings to bind. NETMAP_NO_TX_POLL is unaffected.
387261909Sluigi *
388261909Sluigi *	NOTE: THE FOLLOWING (nr_flags == 0) IS DEPRECATED:
389261909Sluigi *	If nr_flags == 0, NETMAP_HW_RING and NETMAP_SW_RING control
390261909Sluigi *	the binding as follows:
391260368Sluigi *	0 (default)			binds all physical rings
392260368Sluigi *	NETMAP_HW_RING | ring number	binds a single ring pair
393260368Sluigi *	NETMAP_SW_RING			binds only the host tx/rx rings
394259412Sluigi *
395260368Sluigi *	NETMAP_NO_TX_POLL can be OR-ed to make select()/poll() push
396260368Sluigi *		packets on tx rings only if POLLOUT is set.
397260368Sluigi *		The default is to push any pending packet.
398257529Sluigi *
399261909Sluigi *	NETMAP_DO_RX_POLL can be OR-ed to make select()/poll() release
400261909Sluigi *		packets on rx rings also when POLLIN is NOT set.
401261909Sluigi *		The default is to touch the rx ring only with POLLIN.
402261909Sluigi *		Note that this is the opposite of TX because it
403261909Sluigi *		reflects the common usage.
404257529Sluigi *
405261909Sluigi *	NOTE: NETMAP_PRIV_MEM IS DEPRECATED, use nr_arg2 instead.
406261909Sluigi *	NETMAP_PRIV_MEM is set on return for ports that do not use
407261909Sluigi *		the global memory allocator.
408261909Sluigi *		This information is not significant and applications
409261909Sluigi *		should look at the region id in nr_arg2
410261909Sluigi *
411261909Sluigi * nr_flags	is the recommended mode to indicate which rings should
412261909Sluigi *		be bound to a file descriptor. Values are NR_REG_*
413261909Sluigi *
414261909Sluigi * nr_arg1 (in)	The number of extra rings to be reserved.
415261909Sluigi *		Especially when allocating a VALE port the system only
416261909Sluigi *		allocates the amount of memory needed for the port.
417261909Sluigi *		If more shared memory rings are desired (e.g. for pipes),
418261909Sluigi *		the first invocation for the same basename/allocator
419261909Sluigi *		should specify a suitable number. Memory cannot be
420261909Sluigi *		extended after the first allocation without closing
421261909Sluigi *		all ports on the same region.
422261909Sluigi *
423261909Sluigi * nr_arg2 (in/out) The identity of the memory region used.
424261909Sluigi *		On input, 0 means the system decides autonomously,
425261909Sluigi *		other values may try to select a specific region.
426261909Sluigi *		On return the actual value is reported.
427261909Sluigi *		Region '1' is the global allocator, normally shared
428261909Sluigi *		by all interfaces. Other values are private regions.
429261909Sluigi *		If two ports the same region zero-copy is possible.
430261909Sluigi *
431261909Sluigi * nr_arg3 (in/out)	number of extra buffers to be allocated.
432261909Sluigi *
433261909Sluigi *
434261909Sluigi *
435260368Sluigi * nr_cmd (in)	if non-zero indicates a special command:
436260368Sluigi *	NETMAP_BDG_ATTACH	 and nr_name = vale*:ifname
437260368Sluigi *		attaches the NIC to the switch; nr_ringid specifies
438260368Sluigi *		which rings to use. Used by vale-ctl -a ...
439260368Sluigi *	    nr_arg1 = NETMAP_BDG_HOST also attaches the host port
440260368Sluigi *		as in vale-ctl -h ...
441257529Sluigi *
442260368Sluigi *	NETMAP_BDG_DETACH	and nr_name = vale*:ifname
443260368Sluigi *		disconnects a previously attached NIC.
444260368Sluigi *		Used by vale-ctl -d ...
445251139Sluigi *
446260368Sluigi *	NETMAP_BDG_LIST
447260368Sluigi *		list the configuration of VALE switches.
448260368Sluigi *
449261909Sluigi *	NETMAP_BDG_VNET_HDR
450261909Sluigi *		Set the virtio-net header length used by the client
451261909Sluigi *		of a VALE switch port.
452260368Sluigi *
453270063Sluigi *	NETMAP_BDG_NEWIF
454270063Sluigi *		create a persistent VALE port with name nr_name.
455270063Sluigi *		Used by vale-ctl -n ...
456270063Sluigi *
457270063Sluigi *	NETMAP_BDG_DELIF
458270063Sluigi *		delete a persistent VALE port. Used by vale-ctl -d ...
459270063Sluigi *
460261909Sluigi * nr_arg1, nr_arg2, nr_arg3  (in/out)		command specific
461260368Sluigi *
462261909Sluigi *
463267128Sluigi *
464227614Sluigi */
465227614Sluigi
466260368Sluigi
467227614Sluigi/*
468261909Sluigi * struct nmreq overlays a struct ifreq (just the name)
469227614Sluigi */
470227614Sluigistruct nmreq {
471227614Sluigi	char		nr_name[IFNAMSIZ];
472232238Sluigi	uint32_t	nr_version;	/* API version */
473227614Sluigi	uint32_t	nr_offset;	/* nifp offset in the shared region */
474227614Sluigi	uint32_t	nr_memsize;	/* size of the shared region */
475232238Sluigi	uint32_t	nr_tx_slots;	/* slots in tx rings */
476232238Sluigi	uint32_t	nr_rx_slots;	/* slots in rx rings */
477232238Sluigi	uint16_t	nr_tx_rings;	/* number of tx rings */
478232238Sluigi	uint16_t	nr_rx_rings;	/* number of rx rings */
479261909Sluigi
480227614Sluigi	uint16_t	nr_ringid;	/* ring(s) we care about */
481261909Sluigi#define NETMAP_HW_RING		0x4000	/* single NIC ring pair */
482261909Sluigi#define NETMAP_SW_RING		0x2000	/* only host ring pair */
483261909Sluigi
484261909Sluigi#define NETMAP_RING_MASK	0x0fff	/* the ring number */
485261909Sluigi
486232238Sluigi#define NETMAP_NO_TX_POLL	0x1000	/* no automatic txsync on poll */
487260368Sluigi
488261909Sluigi#define NETMAP_DO_RX_POLL	0x8000	/* DO automatic rxsync on poll */
489261909Sluigi
490251139Sluigi	uint16_t	nr_cmd;
491251139Sluigi#define NETMAP_BDG_ATTACH	1	/* attach the NIC */
492251139Sluigi#define NETMAP_BDG_DETACH	2	/* detach the NIC */
493270063Sluigi#define NETMAP_BDG_REGOPS	3	/* register bridge callbacks */
494251139Sluigi#define NETMAP_BDG_LIST		4	/* get bridge's info */
495261909Sluigi#define NETMAP_BDG_VNET_HDR     5       /* set the port virtio-net-hdr length */
496261909Sluigi#define NETMAP_BDG_OFFSET	NETMAP_BDG_VNET_HDR	/* deprecated alias */
497270063Sluigi#define NETMAP_BDG_NEWIF	6	/* create a virtual port */
498270063Sluigi#define NETMAP_BDG_DELIF	7	/* destroy a virtual port */
499261909Sluigi	uint16_t	nr_arg1;	/* reserve extra rings in NIOCREGIF */
500251139Sluigi#define NETMAP_BDG_HOST		1	/* attach the host stack on ATTACH */
501260368Sluigi
502251139Sluigi	uint16_t	nr_arg2;
503261909Sluigi	uint32_t	nr_arg3;	/* req. extra buffers in NIOCREGIF */
504261909Sluigi	uint32_t	nr_flags;
505261909Sluigi	/* various modes, extends nr_ringid */
506261909Sluigi	uint32_t	spare2[1];
507227614Sluigi};
508227614Sluigi
509261909Sluigi#define NR_REG_MASK		0xf /* values for nr_flags */
510261909Sluigienum {	NR_REG_DEFAULT	= 0,	/* backward compat, should not be used. */
511261909Sluigi	NR_REG_ALL_NIC	= 1,
512261909Sluigi	NR_REG_SW	= 2,
513261909Sluigi	NR_REG_NIC_SW	= 3,
514261909Sluigi	NR_REG_ONE_NIC	= 4,
515261909Sluigi	NR_REG_PIPE_MASTER = 5,
516261909Sluigi	NR_REG_PIPE_SLAVE = 6,
517261909Sluigi};
518261909Sluigi/* monitor uses the NR_REG to select the rings to monitor */
519261909Sluigi#define NR_MONITOR_TX	0x100
520261909Sluigi#define NR_MONITOR_RX	0x200
521285349Sluigi#define NR_ZCOPY_MON	0x400
522285349Sluigi/* request exclusive access to the selected rings */
523285349Sluigi#define NR_EXCLUSIVE	0x800
524260368Sluigi
525261909Sluigi
526232238Sluigi/*
527232238Sluigi * FreeBSD uses the size value embedded in the _IOWR to determine
528232238Sluigi * how much to copy in/out. So we need it to match the actual
529232238Sluigi * data structure we pass. We put some spares in the structure
530232238Sluigi * to ease compatibility with other versions
531232238Sluigi */
532227614Sluigi#define NIOCGINFO	_IOWR('i', 145, struct nmreq) /* return IF info */
533227614Sluigi#define NIOCREGIF	_IOWR('i', 146, struct nmreq) /* interface register */
534227614Sluigi#define NIOCTXSYNC	_IO('i', 148) /* sync tx queues */
535227614Sluigi#define NIOCRXSYNC	_IO('i', 149) /* sync rx queues */
536270063Sluigi#define NIOCCONFIG	_IOWR('i',150, struct nm_ifreq) /* for ext. modules */
537227614Sluigi#endif /* !NIOCREGIF */
538227614Sluigi
539260368Sluigi
540260368Sluigi/*
541260368Sluigi * Helper functions for kernel and userspace
542260368Sluigi */
543260368Sluigi
544260368Sluigi/*
545260368Sluigi * check if space is available in the ring.
546260368Sluigi */
547260368Sluigistatic inline int
548260368Sluiginm_ring_empty(struct netmap_ring *ring)
549260368Sluigi{
550260368Sluigi	return (ring->cur == ring->tail);
551260368Sluigi}
552260368Sluigi
553270063Sluigi/*
554270063Sluigi * Opaque structure that is passed to an external kernel
555270063Sluigi * module via ioctl(fd, NIOCCONFIG, req) for a user-owned
556270063Sluigi * bridge port (at this point ephemeral VALE interface).
557270063Sluigi */
558270063Sluigi#define NM_IFRDATA_LEN 256
559270063Sluigistruct nm_ifreq {
560270063Sluigi	char nifr_name[IFNAMSIZ];
561270063Sluigi	char data[NM_IFRDATA_LEN];
562270063Sluigi};
563270063Sluigi
564227614Sluigi#endif /* _NET_NETMAP_H_ */
565