xref: /freebsd/sys/net/netmap.h (revision 4bf50f18eb272037afaa212738f3d82d4098e782)
168b8534bSLuigi Rizzo /*
217885a7bSLuigi Rizzo  * Copyright (C) 2011-2014 Matteo Landi, Luigi Rizzo. All rights reserved.
368b8534bSLuigi Rizzo  *
468b8534bSLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
5f9790aebSLuigi Rizzo  * modification, are permitted provided that the following conditions
6f9790aebSLuigi Rizzo  * are met:
768b8534bSLuigi Rizzo  *
868b8534bSLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
968b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
1068b8534bSLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
1168b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
12f9790aebSLuigi Rizzo  *      documentation and/or other materials provided with the distribution.
1368b8534bSLuigi Rizzo  *
14f9790aebSLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``S IS''AND
1568b8534bSLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16f9790aebSLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17f9790aebSLuigi Rizzo  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18f9790aebSLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19f9790aebSLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20f9790aebSLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21f9790aebSLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22f9790aebSLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23f9790aebSLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24f9790aebSLuigi Rizzo  * SUCH DAMAGE.
2568b8534bSLuigi Rizzo  */
2668b8534bSLuigi Rizzo 
2768b8534bSLuigi Rizzo /*
2868b8534bSLuigi Rizzo  * $FreeBSD$
2968b8534bSLuigi Rizzo  *
3064ae02c3SLuigi Rizzo  * Definitions of constants and the structures used by the netmap
3164ae02c3SLuigi Rizzo  * framework, for the part visible to both kernel and userspace.
3264ae02c3SLuigi Rizzo  * Detailed info on netmap is available with "man netmap" or at
3364ae02c3SLuigi Rizzo  *
3464ae02c3SLuigi Rizzo  *	http://info.iet.unipi.it/~luigi/netmap/
35ce3ee1e7SLuigi Rizzo  *
36ce3ee1e7SLuigi Rizzo  * This API is also used to communicate with the VALE software switch
3768b8534bSLuigi Rizzo  */
3868b8534bSLuigi Rizzo 
3968b8534bSLuigi Rizzo #ifndef _NET_NETMAP_H_
4068b8534bSLuigi Rizzo #define _NET_NETMAP_H_
4168b8534bSLuigi Rizzo 
42f0ea3689SLuigi Rizzo #define	NETMAP_API	11		/* current API version */
4317885a7bSLuigi Rizzo 
44f0ea3689SLuigi Rizzo #define	NETMAP_MIN_API	11		/* min and max versions accepted */
45f0ea3689SLuigi Rizzo #define	NETMAP_MAX_API	15
4617885a7bSLuigi Rizzo /*
4717885a7bSLuigi Rizzo  * Some fields should be cache-aligned to reduce contention.
4817885a7bSLuigi Rizzo  * The alignment is architecture and OS dependent, but rather than
4917885a7bSLuigi Rizzo  * digging into OS headers to find the exact value we use an estimate
5017885a7bSLuigi Rizzo  * that should cover most architectures.
5117885a7bSLuigi Rizzo  */
5217885a7bSLuigi Rizzo #define NM_CACHE_ALIGN	128
5317885a7bSLuigi Rizzo 
5468b8534bSLuigi Rizzo /*
5568b8534bSLuigi Rizzo  * --- Netmap data structures ---
5668b8534bSLuigi Rizzo  *
57ce3ee1e7SLuigi Rizzo  * The userspace data structures used by netmap are shown below.
58ce3ee1e7SLuigi Rizzo  * They are allocated by the kernel and mmap()ed by userspace threads.
59ce3ee1e7SLuigi Rizzo  * Pointers are implemented as memory offsets or indexes,
60ce3ee1e7SLuigi Rizzo  * so that they can be easily dereferenced in kernel and userspace.
6168b8534bSLuigi Rizzo 
62ce3ee1e7SLuigi Rizzo    KERNEL (opaque, obviously)
6368b8534bSLuigi Rizzo 
6464ae02c3SLuigi Rizzo   ====================================================================
6564ae02c3SLuigi Rizzo                                          |
66ce3ee1e7SLuigi Rizzo    USERSPACE                             |      struct netmap_ring
6717885a7bSLuigi Rizzo                                          +---->+---------------+
6817885a7bSLuigi Rizzo                                              / | head,cur,tail |
6917885a7bSLuigi Rizzo    struct netmap_if (nifp, 1 per fd)        /  | buf_ofs       |
7017885a7bSLuigi Rizzo     +---------------+                      /   | other fields  |
7117885a7bSLuigi Rizzo     | ni_tx_rings   |                     /    +===============+
72ce3ee1e7SLuigi Rizzo     | ni_rx_rings   |                    /     | buf_idx, len  | slot[0]
73ce3ee1e7SLuigi Rizzo     |               |                   /      | flags, ptr    |
7417885a7bSLuigi Rizzo     |               |                  /       +---------------+
75ce3ee1e7SLuigi Rizzo     +===============+                 /        | buf_idx, len  | slot[1]
76ce3ee1e7SLuigi Rizzo     | txring_ofs[0] | (rel.to nifp)--'         | flags, ptr    |
7717885a7bSLuigi Rizzo     | txring_ofs[1] |                          +---------------+
78f0ea3689SLuigi Rizzo      (tx+1 entries)                           (num_slots entries)
79ce3ee1e7SLuigi Rizzo     | txring_ofs[t] |                          | buf_idx, len  | slot[n-1]
80ce3ee1e7SLuigi Rizzo     +---------------+                          | flags, ptr    |
8117885a7bSLuigi Rizzo     | rxring_ofs[0] |                          +---------------+
8268b8534bSLuigi Rizzo     | rxring_ofs[1] |
83f0ea3689SLuigi Rizzo      (rx+1 entries)
84ce3ee1e7SLuigi Rizzo     | rxring_ofs[r] |
8568b8534bSLuigi Rizzo     +---------------+
8668b8534bSLuigi Rizzo 
87f0ea3689SLuigi Rizzo  * For each "interface" (NIC, host stack, PIPE, VALE switch port) bound to
88f0ea3689SLuigi Rizzo  * a file descriptor, the mmap()ed region contains a (logically readonly)
89ce3ee1e7SLuigi Rizzo  * struct netmap_if pointing to struct netmap_ring's.
90f0ea3689SLuigi Rizzo  *
91ce3ee1e7SLuigi Rizzo  * There is one netmap_ring per physical NIC ring, plus one tx/rx ring
92f0ea3689SLuigi Rizzo  * pair attached to the host stack (this pair is unused for non-NIC ports).
93ce3ee1e7SLuigi Rizzo  *
94ce3ee1e7SLuigi Rizzo  * All physical/host stack ports share the same memory region,
95ce3ee1e7SLuigi Rizzo  * so that zero-copy can be implemented between them.
96ce3ee1e7SLuigi Rizzo  * VALE switch ports instead have separate memory regions.
97ce3ee1e7SLuigi Rizzo  *
98ce3ee1e7SLuigi Rizzo  * The netmap_ring is the userspace-visible replica of the NIC ring.
99ce3ee1e7SLuigi Rizzo  * Each slot has the index of a buffer (MTU-sized and residing in the
100ce3ee1e7SLuigi Rizzo  * mmapped region), its length and some flags. An extra 64-bit pointer
101ce3ee1e7SLuigi Rizzo  * is provided for user-supplied buffers in the tx path.
102ce3ee1e7SLuigi Rizzo  *
10368b8534bSLuigi Rizzo  * In user space, the buffer address is computed as
10464ae02c3SLuigi Rizzo  *	(char *)ring + buf_ofs + index * NETMAP_BUF_SIZE
105f0ea3689SLuigi Rizzo  *
106f0ea3689SLuigi Rizzo  * Added in NETMAP_API 11:
107f0ea3689SLuigi Rizzo  *
108f0ea3689SLuigi Rizzo  * + NIOCREGIF can request the allocation of extra spare buffers from
109f0ea3689SLuigi Rizzo  *   the same memory pool. The desired number of buffers must be in
110f0ea3689SLuigi Rizzo  *   nr_arg3. The ioctl may return fewer buffers, depending on memory
111f0ea3689SLuigi Rizzo  *   availability. nr_arg3 will return the actual value, and, once
112f0ea3689SLuigi Rizzo  *   mapped, nifp->ni_bufs_head will be the index of the first buffer.
113f0ea3689SLuigi Rizzo  *
114f0ea3689SLuigi Rizzo  *   The buffers are linked to each other using the first uint32_t
115f0ea3689SLuigi Rizzo  *   as the index. On close, ni_bufs_head must point to the list of
116f0ea3689SLuigi Rizzo  *   buffers to be released.
117f0ea3689SLuigi Rizzo  *
118f0ea3689SLuigi Rizzo  * + NIOCREGIF can request space for extra rings (and buffers)
119f0ea3689SLuigi Rizzo  *   allocated in the same memory space. The number of extra rings
120f0ea3689SLuigi Rizzo  *   is in nr_arg1, and is advisory. This is a no-op on NICs where
121f0ea3689SLuigi Rizzo  *   the size of the memory space is fixed.
122f0ea3689SLuigi Rizzo  *
123f0ea3689SLuigi Rizzo  * + NIOCREGIF can attach to PIPE rings sharing the same memory
124f0ea3689SLuigi Rizzo  *   space with a parent device. The ifname indicates the parent device,
125f0ea3689SLuigi Rizzo  *   which must already exist. Flags in nr_flags indicate if we want to
126f0ea3689SLuigi Rizzo  *   bind the master or slave side, the index (from nr_ringid)
1275c8c1004SLuigi Rizzo  *   is just a cookie and does not need to be sequential.
128f0ea3689SLuigi Rizzo  *
129f0ea3689SLuigi Rizzo  * + NIOCREGIF can also attach to 'monitor' rings that replicate
130f0ea3689SLuigi Rizzo  *   the content of specific rings, also from the same memory space.
131f0ea3689SLuigi Rizzo  *
132f0ea3689SLuigi Rizzo  *   Extra flags in nr_flags support the above functions.
133f0ea3689SLuigi Rizzo  *   Application libraries may use the following naming scheme:
134f0ea3689SLuigi Rizzo  *	netmap:foo			all NIC ring pairs
135f0ea3689SLuigi Rizzo  *	netmap:foo^			only host ring pair
136f0ea3689SLuigi Rizzo  *	netmap:foo+			all NIC ring + host ring pairs
137f0ea3689SLuigi Rizzo  *	netmap:foo-k			the k-th NIC ring pair
138f0ea3689SLuigi Rizzo  *	netmap:foo{k			PIPE ring pair k, master side
139f0ea3689SLuigi Rizzo  *	netmap:foo}k			PIPE ring pair k, slave side
140ce3ee1e7SLuigi Rizzo  */
141ce3ee1e7SLuigi Rizzo 
142ce3ee1e7SLuigi Rizzo /*
143ce3ee1e7SLuigi Rizzo  * struct netmap_slot is a buffer descriptor
14417885a7bSLuigi Rizzo  */
14517885a7bSLuigi Rizzo struct netmap_slot {
14617885a7bSLuigi Rizzo 	uint32_t buf_idx;	/* buffer index */
14717885a7bSLuigi Rizzo 	uint16_t len;		/* length for this slot */
14817885a7bSLuigi Rizzo 	uint16_t flags;		/* buf changed, etc. */
14917885a7bSLuigi Rizzo 	uint64_t ptr;		/* pointer for indirect buffers */
15017885a7bSLuigi Rizzo };
15117885a7bSLuigi Rizzo 
15217885a7bSLuigi Rizzo /*
15317885a7bSLuigi Rizzo  * The following flags control how the slot is used
15417885a7bSLuigi Rizzo  */
15517885a7bSLuigi Rizzo 
15617885a7bSLuigi Rizzo #define	NS_BUF_CHANGED	0x0001	/* buf_idx changed */
15717885a7bSLuigi Rizzo 	/*
15817885a7bSLuigi Rizzo 	 * must be set whenever buf_idx is changed (as it might be
15917885a7bSLuigi Rizzo 	 * necessary to recompute the physical address and mapping)
16017885a7bSLuigi Rizzo 	 */
16117885a7bSLuigi Rizzo 
16217885a7bSLuigi Rizzo #define	NS_REPORT	0x0002	/* ask the hardware to report results */
16317885a7bSLuigi Rizzo 	/*
16417885a7bSLuigi Rizzo 	 * Request notification when slot is used by the hardware.
16517885a7bSLuigi Rizzo 	 * Normally transmit completions are handled lazily and
16617885a7bSLuigi Rizzo 	 * may be unreported. This flag lets us know when a slot
16717885a7bSLuigi Rizzo 	 * has been sent (e.g. to terminate the sender).
16817885a7bSLuigi Rizzo 	 */
16917885a7bSLuigi Rizzo 
17017885a7bSLuigi Rizzo #define	NS_FORWARD	0x0004	/* pass packet 'forward' */
17117885a7bSLuigi Rizzo 	/*
17217885a7bSLuigi Rizzo 	 * (Only for physical ports, rx rings with NR_FORWARD set).
17317885a7bSLuigi Rizzo 	 * Slot released to the kernel (i.e. before ring->head) with
17417885a7bSLuigi Rizzo 	 * this flag set are passed to the peer ring (host/NIC),
17517885a7bSLuigi Rizzo 	 * thus restoring the host-NIC connection for these slots.
17617885a7bSLuigi Rizzo 	 * This supports efficient traffic monitoring or firewalling.
17717885a7bSLuigi Rizzo 	 */
17817885a7bSLuigi Rizzo 
17917885a7bSLuigi Rizzo #define	NS_NO_LEARN	0x0008	/* disable bridge learning */
18017885a7bSLuigi Rizzo  	/*
18117885a7bSLuigi Rizzo 	 * On a VALE switch, do not 'learn' the source port for
18217885a7bSLuigi Rizzo  	 * this buffer.
18317885a7bSLuigi Rizzo 	 */
18417885a7bSLuigi Rizzo 
18517885a7bSLuigi Rizzo #define	NS_INDIRECT	0x0010	/* userspace buffer */
18617885a7bSLuigi Rizzo  	/*
18717885a7bSLuigi Rizzo 	 * (VALE tx rings only) data is in a userspace buffer,
18817885a7bSLuigi Rizzo 	 * whose address is in the 'ptr' field in the slot.
18917885a7bSLuigi Rizzo 	 */
19017885a7bSLuigi Rizzo 
19117885a7bSLuigi Rizzo #define	NS_MOREFRAG	0x0020	/* packet has more fragments */
19217885a7bSLuigi Rizzo  	/*
19317885a7bSLuigi Rizzo 	 * (VALE ports only)
19417885a7bSLuigi Rizzo 	 * Set on all but the last slot of a multi-segment packet.
19517885a7bSLuigi Rizzo 	 * The 'len' field refers to the individual fragment.
19617885a7bSLuigi Rizzo 	 */
19717885a7bSLuigi Rizzo 
19817885a7bSLuigi Rizzo #define	NS_PORT_SHIFT	8
19917885a7bSLuigi Rizzo #define	NS_PORT_MASK	(0xff << NS_PORT_SHIFT)
20017885a7bSLuigi Rizzo 	/*
20117885a7bSLuigi Rizzo  	 * The high 8 bits of the flag, if not zero, indicate the
20201c039a1SLuigi Rizzo 	 * destination port for the VALE switch, overriding
20301c039a1SLuigi Rizzo  	 * the lookup table.
20468b8534bSLuigi Rizzo  	 */
20501c039a1SLuigi Rizzo 
20617885a7bSLuigi Rizzo #define	NS_RFRAGS(_slot)	( ((_slot)->flags >> 8) & 0xff)
207ce3ee1e7SLuigi Rizzo 	/*
20817885a7bSLuigi Rizzo 	 * (VALE rx rings only) the high 8 bits
209ce3ee1e7SLuigi Rizzo 	 *  are the number of fragments.
210ce3ee1e7SLuigi Rizzo 	 */
21117885a7bSLuigi Rizzo 
21268b8534bSLuigi Rizzo 
21368b8534bSLuigi Rizzo /*
214ce3ee1e7SLuigi Rizzo  * struct netmap_ring
215ce3ee1e7SLuigi Rizzo  *
21668b8534bSLuigi Rizzo  * Netmap representation of a TX or RX ring (also known as "queue").
21768b8534bSLuigi Rizzo  * This is a queue implemented as a fixed-size circular array.
21817885a7bSLuigi Rizzo  * At the software level the important fields are: head, cur, tail.
21968b8534bSLuigi Rizzo  *
22068b8534bSLuigi Rizzo  * In TX rings:
221ce3ee1e7SLuigi Rizzo  *
22217885a7bSLuigi Rizzo  *	head	first slot available for transmission.
22317885a7bSLuigi Rizzo  *	cur	wakeup point. select() and poll() will unblock
22417885a7bSLuigi Rizzo  *		when 'tail' moves past 'cur'
22517885a7bSLuigi Rizzo  *	tail	(readonly) first slot reserved to the kernel
226ce3ee1e7SLuigi Rizzo  *
22717885a7bSLuigi Rizzo  *	[head .. tail-1] can be used for new packets to send;
22817885a7bSLuigi Rizzo  *	'head' and 'cur' must be incremented as slots are filled
22917885a7bSLuigi Rizzo  *	    with new packets to be sent;
23017885a7bSLuigi Rizzo  *	'cur' can be moved further ahead if we need more space
2315c8c1004SLuigi Rizzo  *	for new transmissions. XXX todo (2014-03-12)
23268b8534bSLuigi Rizzo  *
23368b8534bSLuigi Rizzo  * In RX rings:
234ce3ee1e7SLuigi Rizzo  *
23517885a7bSLuigi Rizzo  *	head	first valid received packet
23617885a7bSLuigi Rizzo  *	cur	wakeup point. select() and poll() will unblock
23717885a7bSLuigi Rizzo  *		when 'tail' moves past 'cur'
23817885a7bSLuigi Rizzo  *	tail	(readonly) first slot reserved to the kernel
239ce3ee1e7SLuigi Rizzo  *
24017885a7bSLuigi Rizzo  *	[head .. tail-1] contain received packets;
24117885a7bSLuigi Rizzo  *	'head' and 'cur' must be incremented as slots are consumed
24217885a7bSLuigi Rizzo  *		and can be returned to the kernel;
24317885a7bSLuigi Rizzo  *	'cur' can be moved further ahead if we want to wait for
24417885a7bSLuigi Rizzo  *		new packets without returning the previous ones.
24568b8534bSLuigi Rizzo  *
24668b8534bSLuigi Rizzo  * DATA OWNERSHIP/LOCKING:
24717885a7bSLuigi Rizzo  *	The netmap_ring, and all slots and buffers in the range
24817885a7bSLuigi Rizzo  *	[head .. tail-1] are owned by the user program;
24917885a7bSLuigi Rizzo  *	the kernel only accesses them during a netmap system call
25017885a7bSLuigi Rizzo  *	and in the user thread context.
25101c039a1SLuigi Rizzo  *
25217885a7bSLuigi Rizzo  *	Other slots and buffers are reserved for use by the kernel
25368b8534bSLuigi Rizzo  */
25468b8534bSLuigi Rizzo struct netmap_ring {
25568b8534bSLuigi Rizzo 	/*
256ce3ee1e7SLuigi Rizzo 	 * buf_ofs is meant to be used through macros.
25768b8534bSLuigi Rizzo 	 * It contains the offset of the buffer region from this
25868b8534bSLuigi Rizzo 	 * descriptor.
25968b8534bSLuigi Rizzo 	 */
26017885a7bSLuigi Rizzo 	const int64_t	buf_ofs;
26168b8534bSLuigi Rizzo 	const uint32_t	num_slots;	/* number of slots in the ring. */
26217885a7bSLuigi Rizzo 	const uint32_t	nr_buf_size;
26317885a7bSLuigi Rizzo 	const uint16_t	ringid;
26417885a7bSLuigi Rizzo 	const uint16_t	dir;		/* 0: tx, 1: rx */
26568b8534bSLuigi Rizzo 
26617885a7bSLuigi Rizzo 	uint32_t        head;		/* (u) first user slot */
26717885a7bSLuigi Rizzo 	uint32_t        cur;		/* (u) wakeup point */
26817885a7bSLuigi Rizzo 	uint32_t	tail;		/* (k) first kernel slot */
26968b8534bSLuigi Rizzo 
27017885a7bSLuigi Rizzo 	uint32_t	flags;
27117885a7bSLuigi Rizzo 
27217885a7bSLuigi Rizzo 	struct timeval	ts;		/* (k) time of last *sync() */
27317885a7bSLuigi Rizzo 
27417885a7bSLuigi Rizzo 	/* opaque room for a mutex or similar object */
27517885a7bSLuigi Rizzo 	uint8_t		sem[128] __attribute__((__aligned__(NM_CACHE_ALIGN)));
27668b8534bSLuigi Rizzo 
27768b8534bSLuigi Rizzo 	/* the slots follow. This struct has variable size */
27868b8534bSLuigi Rizzo 	struct netmap_slot slot[0];	/* array of slots. */
27968b8534bSLuigi Rizzo };
28068b8534bSLuigi Rizzo 
28168b8534bSLuigi Rizzo 
28268b8534bSLuigi Rizzo /*
28317885a7bSLuigi Rizzo  * RING FLAGS
28417885a7bSLuigi Rizzo  */
28517885a7bSLuigi Rizzo #define	NR_TIMESTAMP	0x0002		/* set timestamp on *sync() */
28617885a7bSLuigi Rizzo 	/*
28717885a7bSLuigi Rizzo 	 * updates the 'ts' field on each netmap syscall. This saves
28817885a7bSLuigi Rizzo 	 * saves a separate gettimeofday(), and is not much worse than
28917885a7bSLuigi Rizzo 	 * software timestamps generated in the interrupt handler.
29017885a7bSLuigi Rizzo 	 */
29117885a7bSLuigi Rizzo 
29217885a7bSLuigi Rizzo #define	NR_FORWARD	0x0004		/* enable NS_FORWARD for ring */
29317885a7bSLuigi Rizzo  	/*
29417885a7bSLuigi Rizzo 	 * Enables the NS_FORWARD slot flag for the ring.
29517885a7bSLuigi Rizzo 	 */
29617885a7bSLuigi Rizzo 
29717885a7bSLuigi Rizzo 
29817885a7bSLuigi Rizzo /*
29968b8534bSLuigi Rizzo  * Netmap representation of an interface and its queue(s).
300ce3ee1e7SLuigi Rizzo  * This is initialized by the kernel when binding a file
301ce3ee1e7SLuigi Rizzo  * descriptor to a port, and should be considered as readonly
302ce3ee1e7SLuigi Rizzo  * by user programs. The kernel never uses it.
303ce3ee1e7SLuigi Rizzo  *
30468b8534bSLuigi Rizzo  * There is one netmap_if for each file descriptor on which we want
305ce3ee1e7SLuigi Rizzo  * to select/poll.
30668b8534bSLuigi Rizzo  * select/poll operates on one or all pairs depending on the value of
30768b8534bSLuigi Rizzo  * nmr_queueid passed on the ioctl.
30868b8534bSLuigi Rizzo  */
30968b8534bSLuigi Rizzo struct netmap_if {
31068b8534bSLuigi Rizzo 	char		ni_name[IFNAMSIZ]; /* name of the interface. */
311ce3ee1e7SLuigi Rizzo 	const uint32_t	ni_version;	/* API version, currently unused */
312ce3ee1e7SLuigi Rizzo 	const uint32_t	ni_flags;	/* properties */
313ce3ee1e7SLuigi Rizzo #define	NI_PRIV_MEM	0x1		/* private memory region */
314ce3ee1e7SLuigi Rizzo 
31517885a7bSLuigi Rizzo 	/*
31617885a7bSLuigi Rizzo 	 * The number of packet rings available in netmap mode.
31717885a7bSLuigi Rizzo 	 * Physical NICs can have different numbers of tx and rx rings.
31817885a7bSLuigi Rizzo 	 * Physical NICs also have a 'host' ring pair.
31917885a7bSLuigi Rizzo 	 * Additionally, clients can request additional ring pairs to
32017885a7bSLuigi Rizzo 	 * be used for internal communication.
32117885a7bSLuigi Rizzo 	 */
32217885a7bSLuigi Rizzo 	const uint32_t	ni_tx_rings;	/* number of HW tx rings */
32317885a7bSLuigi Rizzo 	const uint32_t	ni_rx_rings;	/* number of HW rx rings */
32417885a7bSLuigi Rizzo 
325f0ea3689SLuigi Rizzo 	uint32_t	ni_bufs_head;	/* head index for extra bufs */
326f0ea3689SLuigi Rizzo 	uint32_t	ni_spare1[5];
32768b8534bSLuigi Rizzo 	/*
32864ae02c3SLuigi Rizzo 	 * The following array contains the offset of each netmap ring
32917885a7bSLuigi Rizzo 	 * from this structure, in the following order:
33017885a7bSLuigi Rizzo 	 * NIC tx rings (ni_tx_rings); host tx ring (1); extra tx rings;
33117885a7bSLuigi Rizzo 	 * NIC rx rings (ni_rx_rings); host tx ring (1); extra rx rings.
33217885a7bSLuigi Rizzo 	 *
333ce3ee1e7SLuigi Rizzo 	 * The area is filled up by the kernel on NIOCREGIF,
33468b8534bSLuigi Rizzo 	 * and then only read by userspace code.
33568b8534bSLuigi Rizzo 	 */
33668b8534bSLuigi Rizzo 	const ssize_t	ring_ofs[0];
33768b8534bSLuigi Rizzo };
33868b8534bSLuigi Rizzo 
33917885a7bSLuigi Rizzo 
34068b8534bSLuigi Rizzo #ifndef NIOCREGIF
34168b8534bSLuigi Rizzo /*
34268b8534bSLuigi Rizzo  * ioctl names and related fields
34368b8534bSLuigi Rizzo  *
34417885a7bSLuigi Rizzo  * NIOCTXSYNC, NIOCRXSYNC synchronize tx or rx queues,
34517885a7bSLuigi Rizzo  *	whose identity is set in NIOCREGIF through nr_ringid.
34617885a7bSLuigi Rizzo  *	These are non blocking and take no argument.
34717885a7bSLuigi Rizzo  *
34868b8534bSLuigi Rizzo  * NIOCGINFO takes a struct ifreq, the interface name is the input,
34968b8534bSLuigi Rizzo  *	the outputs are number of queues and number of descriptor
35068b8534bSLuigi Rizzo  *	for each queue (useful to set number of threads etc.).
351ce3ee1e7SLuigi Rizzo  *	The info returned is only advisory and may change before
352ce3ee1e7SLuigi Rizzo  *	the interface is bound to a file descriptor.
35368b8534bSLuigi Rizzo  *
35417885a7bSLuigi Rizzo  * NIOCREGIF takes an interface name within a struct nmre,
35568b8534bSLuigi Rizzo  *	and activates netmap mode on the interface (if possible).
35668b8534bSLuigi Rizzo  *
35717885a7bSLuigi Rizzo  * The argument to NIOCGINFO/NIOCREGIF overlays struct ifreq so we
35817885a7bSLuigi Rizzo  * can pass it down to other NIC-related ioctls.
359f18be576SLuigi Rizzo  *
36017885a7bSLuigi Rizzo  * The actual argument (struct nmreq) has a number of options to request
36117885a7bSLuigi Rizzo  * different functions.
362f0ea3689SLuigi Rizzo  * The following are used in NIOCREGIF when nr_cmd == 0:
363f18be576SLuigi Rizzo  *
36417885a7bSLuigi Rizzo  * nr_name	(in)
36517885a7bSLuigi Rizzo  *	The name of the port (em0, valeXXX:YYY, etc.)
36617885a7bSLuigi Rizzo  *	limited to IFNAMSIZ for backward compatibility.
367ce3ee1e7SLuigi Rizzo  *
36817885a7bSLuigi Rizzo  * nr_version	(in/out)
36917885a7bSLuigi Rizzo  *	Must match NETMAP_API as used in the kernel, error otherwise.
37017885a7bSLuigi Rizzo  *	Always returns the desired value on output.
37117885a7bSLuigi Rizzo  *
37217885a7bSLuigi Rizzo  * nr_tx_slots, nr_tx_slots, nr_tx_rings, nr_rx_rings (in/out)
37317885a7bSLuigi Rizzo  *	On input, non-zero values may be used to reconfigure the port
37417885a7bSLuigi Rizzo  *	according to the requested values, but this is not guaranteed.
37517885a7bSLuigi Rizzo  *	On output the actual values in use are reported.
37617885a7bSLuigi Rizzo  *
37717885a7bSLuigi Rizzo  * nr_ringid (in)
37817885a7bSLuigi Rizzo  *	Indicates how rings should be bound to the file descriptors.
379f0ea3689SLuigi Rizzo  *	If nr_flags != 0, then the low bits (in NETMAP_RING_MASK)
380f0ea3689SLuigi Rizzo  *	are used to indicate the ring number, and nr_flags specifies
381f0ea3689SLuigi Rizzo  *	the actual rings to bind. NETMAP_NO_TX_POLL is unaffected.
382f0ea3689SLuigi Rizzo  *
383f0ea3689SLuigi Rizzo  *	NOTE: THE FOLLOWING (nr_flags == 0) IS DEPRECATED:
384f0ea3689SLuigi Rizzo  *	If nr_flags == 0, NETMAP_HW_RING and NETMAP_SW_RING control
385f0ea3689SLuigi Rizzo  *	the binding as follows:
38617885a7bSLuigi Rizzo  *	0 (default)			binds all physical rings
38717885a7bSLuigi Rizzo  *	NETMAP_HW_RING | ring number	binds a single ring pair
388ce3ee1e7SLuigi Rizzo  *	NETMAP_SW_RING			binds only the host tx/rx rings
389ce3ee1e7SLuigi Rizzo  *
39017885a7bSLuigi Rizzo  *	NETMAP_NO_TX_POLL can be OR-ed to make select()/poll() push
39117885a7bSLuigi Rizzo  *		packets on tx rings only if POLLOUT is set.
39217885a7bSLuigi Rizzo  *		The default is to push any pending packet.
393ce3ee1e7SLuigi Rizzo  *
394f0ea3689SLuigi Rizzo  *	NETMAP_DO_RX_POLL can be OR-ed to make select()/poll() release
395f0ea3689SLuigi Rizzo  *		packets on rx rings also when POLLIN is NOT set.
396f0ea3689SLuigi Rizzo  *		The default is to touch the rx ring only with POLLIN.
397f0ea3689SLuigi Rizzo  *		Note that this is the opposite of TX because it
398f0ea3689SLuigi Rizzo  *		reflects the common usage.
399f0ea3689SLuigi Rizzo  *
400f0ea3689SLuigi Rizzo  *	NOTE: NETMAP_PRIV_MEM IS DEPRECATED, use nr_arg2 instead.
401f0ea3689SLuigi Rizzo  *	NETMAP_PRIV_MEM is set on return for ports that do not use
402f0ea3689SLuigi Rizzo  *		the global memory allocator.
403f0ea3689SLuigi Rizzo  *		This information is not significant and applications
404f0ea3689SLuigi Rizzo  *		should look at the region id in nr_arg2
405f0ea3689SLuigi Rizzo  *
406f0ea3689SLuigi Rizzo  * nr_flags	is the recommended mode to indicate which rings should
407f0ea3689SLuigi Rizzo  *		be bound to a file descriptor. Values are NR_REG_*
408f0ea3689SLuigi Rizzo  *
409f0ea3689SLuigi Rizzo  * nr_arg1 (in)	The number of extra rings to be reserved.
410f0ea3689SLuigi Rizzo  *		Especially when allocating a VALE port the system only
411f0ea3689SLuigi Rizzo  *		allocates the amount of memory needed for the port.
412f0ea3689SLuigi Rizzo  *		If more shared memory rings are desired (e.g. for pipes),
413f0ea3689SLuigi Rizzo  *		the first invocation for the same basename/allocator
414f0ea3689SLuigi Rizzo  *		should specify a suitable number. Memory cannot be
415f0ea3689SLuigi Rizzo  *		extended after the first allocation without closing
416f0ea3689SLuigi Rizzo  *		all ports on the same region.
417f0ea3689SLuigi Rizzo  *
418f0ea3689SLuigi Rizzo  * nr_arg2 (in/out) The identity of the memory region used.
419f0ea3689SLuigi Rizzo  *		On input, 0 means the system decides autonomously,
420f0ea3689SLuigi Rizzo  *		other values may try to select a specific region.
421f0ea3689SLuigi Rizzo  *		On return the actual value is reported.
422f0ea3689SLuigi Rizzo  *		Region '1' is the global allocator, normally shared
423f0ea3689SLuigi Rizzo  *		by all interfaces. Other values are private regions.
424f0ea3689SLuigi Rizzo  *		If two ports the same region zero-copy is possible.
425f0ea3689SLuigi Rizzo  *
426f0ea3689SLuigi Rizzo  * nr_arg3 (in/out)	number of extra buffers to be allocated.
427f0ea3689SLuigi Rizzo  *
428f0ea3689SLuigi Rizzo  *
429ce3ee1e7SLuigi Rizzo  *
43017885a7bSLuigi Rizzo  * nr_cmd (in)	if non-zero indicates a special command:
43117885a7bSLuigi Rizzo  *	NETMAP_BDG_ATTACH	 and nr_name = vale*:ifname
43217885a7bSLuigi Rizzo  *		attaches the NIC to the switch; nr_ringid specifies
43317885a7bSLuigi Rizzo  *		which rings to use. Used by vale-ctl -a ...
43417885a7bSLuigi Rizzo  *	    nr_arg1 = NETMAP_BDG_HOST also attaches the host port
43517885a7bSLuigi Rizzo  *		as in vale-ctl -h ...
436ce3ee1e7SLuigi Rizzo  *
43717885a7bSLuigi Rizzo  *	NETMAP_BDG_DETACH	and nr_name = vale*:ifname
43817885a7bSLuigi Rizzo  *		disconnects a previously attached NIC.
43917885a7bSLuigi Rizzo  *		Used by vale-ctl -d ...
440ce3ee1e7SLuigi Rizzo  *
44117885a7bSLuigi Rizzo  *	NETMAP_BDG_LIST
44217885a7bSLuigi Rizzo  *		list the configuration of VALE switches.
44368b8534bSLuigi Rizzo  *
444f0ea3689SLuigi Rizzo  *	NETMAP_BDG_VNET_HDR
445f0ea3689SLuigi Rizzo  *		Set the virtio-net header length used by the client
446f0ea3689SLuigi Rizzo  *		of a VALE switch port.
447f18be576SLuigi Rizzo  *
448*4bf50f18SLuigi Rizzo  *	NETMAP_BDG_NEWIF
449*4bf50f18SLuigi Rizzo  *		create a persistent VALE port with name nr_name.
450*4bf50f18SLuigi Rizzo  *		Used by vale-ctl -n ...
451*4bf50f18SLuigi Rizzo  *
452*4bf50f18SLuigi Rizzo  *	NETMAP_BDG_DELIF
453*4bf50f18SLuigi Rizzo  *		delete a persistent VALE port. Used by vale-ctl -d ...
454*4bf50f18SLuigi Rizzo  *
455f0ea3689SLuigi Rizzo  * nr_arg1, nr_arg2, nr_arg3  (in/out)		command specific
456f0ea3689SLuigi Rizzo  *
457f0ea3689SLuigi Rizzo  *
45817885a7bSLuigi Rizzo  *
45968b8534bSLuigi Rizzo  */
46068b8534bSLuigi Rizzo 
46117885a7bSLuigi Rizzo 
46268b8534bSLuigi Rizzo /*
463f0ea3689SLuigi Rizzo  * struct nmreq overlays a struct ifreq (just the name)
46468b8534bSLuigi Rizzo  */
46568b8534bSLuigi Rizzo struct nmreq {
46668b8534bSLuigi Rizzo 	char		nr_name[IFNAMSIZ];
46764ae02c3SLuigi Rizzo 	uint32_t	nr_version;	/* API version */
46868b8534bSLuigi Rizzo 	uint32_t	nr_offset;	/* nifp offset in the shared region */
46968b8534bSLuigi Rizzo 	uint32_t	nr_memsize;	/* size of the shared region */
47064ae02c3SLuigi Rizzo 	uint32_t	nr_tx_slots;	/* slots in tx rings */
47164ae02c3SLuigi Rizzo 	uint32_t	nr_rx_slots;	/* slots in rx rings */
47264ae02c3SLuigi Rizzo 	uint16_t	nr_tx_rings;	/* number of tx rings */
47364ae02c3SLuigi Rizzo 	uint16_t	nr_rx_rings;	/* number of rx rings */
474f0ea3689SLuigi Rizzo 
47568b8534bSLuigi Rizzo 	uint16_t	nr_ringid;	/* ring(s) we care about */
476f0ea3689SLuigi Rizzo #define NETMAP_HW_RING		0x4000	/* single NIC ring pair */
477f0ea3689SLuigi Rizzo #define NETMAP_SW_RING		0x2000	/* only host ring pair */
478f0ea3689SLuigi Rizzo 
479f0ea3689SLuigi Rizzo #define NETMAP_RING_MASK	0x0fff	/* the ring number */
480f0ea3689SLuigi Rizzo 
48164ae02c3SLuigi Rizzo #define NETMAP_NO_TX_POLL	0x1000	/* no automatic txsync on poll */
482f0ea3689SLuigi Rizzo 
483f0ea3689SLuigi Rizzo #define NETMAP_DO_RX_POLL	0x8000	/* DO automatic rxsync on poll */
48417885a7bSLuigi Rizzo 
485f18be576SLuigi Rizzo 	uint16_t	nr_cmd;
486f18be576SLuigi Rizzo #define NETMAP_BDG_ATTACH	1	/* attach the NIC */
487f18be576SLuigi Rizzo #define NETMAP_BDG_DETACH	2	/* detach the NIC */
488*4bf50f18SLuigi Rizzo #define NETMAP_BDG_REGOPS	3	/* register bridge callbacks */
489f18be576SLuigi Rizzo #define NETMAP_BDG_LIST		4	/* get bridge's info */
490f0ea3689SLuigi Rizzo #define NETMAP_BDG_VNET_HDR     5       /* set the port virtio-net-hdr length */
491f0ea3689SLuigi Rizzo #define NETMAP_BDG_OFFSET	NETMAP_BDG_VNET_HDR	/* deprecated alias */
492*4bf50f18SLuigi Rizzo #define NETMAP_BDG_NEWIF	6	/* create a virtual port */
493*4bf50f18SLuigi Rizzo #define NETMAP_BDG_DELIF	7	/* destroy a virtual port */
494f0ea3689SLuigi Rizzo 	uint16_t	nr_arg1;	/* reserve extra rings in NIOCREGIF */
495f18be576SLuigi Rizzo #define NETMAP_BDG_HOST		1	/* attach the host stack on ATTACH */
49617885a7bSLuigi Rizzo 
497f18be576SLuigi Rizzo 	uint16_t	nr_arg2;
498f0ea3689SLuigi Rizzo 	uint32_t	nr_arg3;	/* req. extra buffers in NIOCREGIF */
499f0ea3689SLuigi Rizzo 	uint32_t	nr_flags;
500f0ea3689SLuigi Rizzo 	/* various modes, extends nr_ringid */
501f0ea3689SLuigi Rizzo 	uint32_t	spare2[1];
50268b8534bSLuigi Rizzo };
50368b8534bSLuigi Rizzo 
504f0ea3689SLuigi Rizzo #define NR_REG_MASK		0xf /* values for nr_flags */
505f0ea3689SLuigi Rizzo enum {	NR_REG_DEFAULT	= 0,	/* backward compat, should not be used. */
506f0ea3689SLuigi Rizzo 	NR_REG_ALL_NIC	= 1,
507f0ea3689SLuigi Rizzo 	NR_REG_SW	= 2,
508f0ea3689SLuigi Rizzo 	NR_REG_NIC_SW	= 3,
509f0ea3689SLuigi Rizzo 	NR_REG_ONE_NIC	= 4,
510f0ea3689SLuigi Rizzo 	NR_REG_PIPE_MASTER = 5,
511f0ea3689SLuigi Rizzo 	NR_REG_PIPE_SLAVE = 6,
512f0ea3689SLuigi Rizzo };
513f0ea3689SLuigi Rizzo /* monitor uses the NR_REG to select the rings to monitor */
514f0ea3689SLuigi Rizzo #define NR_MONITOR_TX	0x100
515f0ea3689SLuigi Rizzo #define NR_MONITOR_RX	0x200
516f0ea3689SLuigi Rizzo 
51717885a7bSLuigi Rizzo 
51864ae02c3SLuigi Rizzo /*
51964ae02c3SLuigi Rizzo  * FreeBSD uses the size value embedded in the _IOWR to determine
52064ae02c3SLuigi Rizzo  * how much to copy in/out. So we need it to match the actual
52164ae02c3SLuigi Rizzo  * data structure we pass. We put some spares in the structure
52264ae02c3SLuigi Rizzo  * to ease compatibility with other versions
52364ae02c3SLuigi Rizzo  */
52468b8534bSLuigi Rizzo #define NIOCGINFO	_IOWR('i', 145, struct nmreq) /* return IF info */
52568b8534bSLuigi Rizzo #define NIOCREGIF	_IOWR('i', 146, struct nmreq) /* interface register */
52668b8534bSLuigi Rizzo #define NIOCTXSYNC	_IO('i', 148) /* sync tx queues */
52768b8534bSLuigi Rizzo #define NIOCRXSYNC	_IO('i', 149) /* sync rx queues */
528*4bf50f18SLuigi Rizzo #define NIOCCONFIG	_IOWR('i',150, struct nm_ifreq) /* for ext. modules */
52968b8534bSLuigi Rizzo #endif /* !NIOCREGIF */
53068b8534bSLuigi Rizzo 
53117885a7bSLuigi Rizzo 
53217885a7bSLuigi Rizzo /*
53317885a7bSLuigi Rizzo  * Helper functions for kernel and userspace
53417885a7bSLuigi Rizzo  */
53517885a7bSLuigi Rizzo 
53617885a7bSLuigi Rizzo /*
53717885a7bSLuigi Rizzo  * check if space is available in the ring.
53817885a7bSLuigi Rizzo  */
53917885a7bSLuigi Rizzo static inline int
54017885a7bSLuigi Rizzo nm_ring_empty(struct netmap_ring *ring)
54117885a7bSLuigi Rizzo {
54217885a7bSLuigi Rizzo 	return (ring->cur == ring->tail);
54317885a7bSLuigi Rizzo }
54417885a7bSLuigi Rizzo 
545*4bf50f18SLuigi Rizzo /*
546*4bf50f18SLuigi Rizzo  * Opaque structure that is passed to an external kernel
547*4bf50f18SLuigi Rizzo  * module via ioctl(fd, NIOCCONFIG, req) for a user-owned
548*4bf50f18SLuigi Rizzo  * bridge port (at this point ephemeral VALE interface).
549*4bf50f18SLuigi Rizzo  */
550*4bf50f18SLuigi Rizzo #define NM_IFRDATA_LEN 256
551*4bf50f18SLuigi Rizzo struct nm_ifreq {
552*4bf50f18SLuigi Rizzo 	char nifr_name[IFNAMSIZ];
553*4bf50f18SLuigi Rizzo 	char data[NM_IFRDATA_LEN];
554*4bf50f18SLuigi Rizzo };
555*4bf50f18SLuigi Rizzo 
55668b8534bSLuigi Rizzo #endif /* _NET_NETMAP_H_ */
557