xref: /freebsd/sys/net/netmap.h (revision b6e66be22bdce2aadcf52ee6230faa1e6cd3f805)
1fe267a55SPedro F. Giffuni /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3fe267a55SPedro F. Giffuni  *
417885a7bSLuigi Rizzo  * Copyright (C) 2011-2014 Matteo Landi, Luigi Rizzo. All rights reserved.
568b8534bSLuigi Rizzo  *
668b8534bSLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
7f9790aebSLuigi Rizzo  * modification, are permitted provided that the following conditions
8f9790aebSLuigi Rizzo  * are met:
968b8534bSLuigi Rizzo  *
1068b8534bSLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
1168b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
1268b8534bSLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
1368b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
14f9790aebSLuigi Rizzo  *      documentation and/or other materials provided with the distribution.
1568b8534bSLuigi Rizzo  *
16f9790aebSLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``S IS''AND
1768b8534bSLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18f9790aebSLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19f9790aebSLuigi Rizzo  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20f9790aebSLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21f9790aebSLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22f9790aebSLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23f9790aebSLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24f9790aebSLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25f9790aebSLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26f9790aebSLuigi Rizzo  * SUCH DAMAGE.
2768b8534bSLuigi Rizzo  */
2868b8534bSLuigi Rizzo 
2968b8534bSLuigi Rizzo /*
3068b8534bSLuigi Rizzo  * $FreeBSD$
3168b8534bSLuigi Rizzo  *
3264ae02c3SLuigi Rizzo  * Definitions of constants and the structures used by the netmap
3364ae02c3SLuigi Rizzo  * framework, for the part visible to both kernel and userspace.
3464ae02c3SLuigi Rizzo  * Detailed info on netmap is available with "man netmap" or at
3564ae02c3SLuigi Rizzo  *
3664ae02c3SLuigi Rizzo  *	http://info.iet.unipi.it/~luigi/netmap/
37ce3ee1e7SLuigi Rizzo  *
38ce3ee1e7SLuigi Rizzo  * This API is also used to communicate with the VALE software switch
3968b8534bSLuigi Rizzo  */
4068b8534bSLuigi Rizzo 
4168b8534bSLuigi Rizzo #ifndef _NET_NETMAP_H_
4268b8534bSLuigi Rizzo #define _NET_NETMAP_H_
4368b8534bSLuigi Rizzo 
44*b6e66be2SVincenzo Maffione #define	NETMAP_API	13		/* current API version */
4517885a7bSLuigi Rizzo 
46*b6e66be2SVincenzo Maffione #define	NETMAP_MIN_API	13		/* min and max versions accepted */
47f0ea3689SLuigi Rizzo #define	NETMAP_MAX_API	15
4817885a7bSLuigi Rizzo /*
4917885a7bSLuigi Rizzo  * Some fields should be cache-aligned to reduce contention.
5017885a7bSLuigi Rizzo  * The alignment is architecture and OS dependent, but rather than
5117885a7bSLuigi Rizzo  * digging into OS headers to find the exact value we use an estimate
5217885a7bSLuigi Rizzo  * that should cover most architectures.
5317885a7bSLuigi Rizzo  */
5417885a7bSLuigi Rizzo #define NM_CACHE_ALIGN	128
5517885a7bSLuigi Rizzo 
5668b8534bSLuigi Rizzo /*
5768b8534bSLuigi Rizzo  * --- Netmap data structures ---
5868b8534bSLuigi Rizzo  *
59ce3ee1e7SLuigi Rizzo  * The userspace data structures used by netmap are shown below.
60ce3ee1e7SLuigi Rizzo  * They are allocated by the kernel and mmap()ed by userspace threads.
61ce3ee1e7SLuigi Rizzo  * Pointers are implemented as memory offsets or indexes,
62ce3ee1e7SLuigi Rizzo  * so that they can be easily dereferenced in kernel and userspace.
6368b8534bSLuigi Rizzo 
64ce3ee1e7SLuigi Rizzo    KERNEL (opaque, obviously)
6568b8534bSLuigi Rizzo 
6664ae02c3SLuigi Rizzo   ====================================================================
6764ae02c3SLuigi Rizzo                                          |
68ce3ee1e7SLuigi Rizzo    USERSPACE                             |      struct netmap_ring
6917885a7bSLuigi Rizzo                                          +---->+---------------+
7017885a7bSLuigi Rizzo                                              / | head,cur,tail |
7117885a7bSLuigi Rizzo    struct netmap_if (nifp, 1 per fd)        /  | buf_ofs       |
7217885a7bSLuigi Rizzo     +---------------+                      /   | other fields  |
7317885a7bSLuigi Rizzo     | ni_tx_rings   |                     /    +===============+
74ce3ee1e7SLuigi Rizzo     | ni_rx_rings   |                    /     | buf_idx, len  | slot[0]
75ce3ee1e7SLuigi Rizzo     |               |                   /      | flags, ptr    |
7617885a7bSLuigi Rizzo     |               |                  /       +---------------+
77ce3ee1e7SLuigi Rizzo     +===============+                 /        | buf_idx, len  | slot[1]
78ce3ee1e7SLuigi Rizzo     | txring_ofs[0] | (rel.to nifp)--'         | flags, ptr    |
7917885a7bSLuigi Rizzo     | txring_ofs[1] |                          +---------------+
80f0ea3689SLuigi Rizzo      (tx+1 entries)                           (num_slots entries)
81ce3ee1e7SLuigi Rizzo     | txring_ofs[t] |                          | buf_idx, len  | slot[n-1]
82ce3ee1e7SLuigi Rizzo     +---------------+                          | flags, ptr    |
8317885a7bSLuigi Rizzo     | rxring_ofs[0] |                          +---------------+
8468b8534bSLuigi Rizzo     | rxring_ofs[1] |
85f0ea3689SLuigi Rizzo      (rx+1 entries)
86ce3ee1e7SLuigi Rizzo     | rxring_ofs[r] |
8768b8534bSLuigi Rizzo     +---------------+
8868b8534bSLuigi Rizzo 
89f0ea3689SLuigi Rizzo  * For each "interface" (NIC, host stack, PIPE, VALE switch port) bound to
90f0ea3689SLuigi Rizzo  * a file descriptor, the mmap()ed region contains a (logically readonly)
91ce3ee1e7SLuigi Rizzo  * struct netmap_if pointing to struct netmap_ring's.
92f0ea3689SLuigi Rizzo  *
93ce3ee1e7SLuigi Rizzo  * There is one netmap_ring per physical NIC ring, plus one tx/rx ring
94f0ea3689SLuigi Rizzo  * pair attached to the host stack (this pair is unused for non-NIC ports).
95ce3ee1e7SLuigi Rizzo  *
96ce3ee1e7SLuigi Rizzo  * All physical/host stack ports share the same memory region,
97ce3ee1e7SLuigi Rizzo  * so that zero-copy can be implemented between them.
98ce3ee1e7SLuigi Rizzo  * VALE switch ports instead have separate memory regions.
99ce3ee1e7SLuigi Rizzo  *
100ce3ee1e7SLuigi Rizzo  * The netmap_ring is the userspace-visible replica of the NIC ring.
101ce3ee1e7SLuigi Rizzo  * Each slot has the index of a buffer (MTU-sized and residing in the
102ce3ee1e7SLuigi Rizzo  * mmapped region), its length and some flags. An extra 64-bit pointer
103ce3ee1e7SLuigi Rizzo  * is provided for user-supplied buffers in the tx path.
104ce3ee1e7SLuigi Rizzo  *
10568b8534bSLuigi Rizzo  * In user space, the buffer address is computed as
10664ae02c3SLuigi Rizzo  *	(char *)ring + buf_ofs + index * NETMAP_BUF_SIZE
107f0ea3689SLuigi Rizzo  *
108f0ea3689SLuigi Rizzo  * Added in NETMAP_API 11:
109f0ea3689SLuigi Rizzo  *
110f0ea3689SLuigi Rizzo  * + NIOCREGIF can request the allocation of extra spare buffers from
111f0ea3689SLuigi Rizzo  *   the same memory pool. The desired number of buffers must be in
112f0ea3689SLuigi Rizzo  *   nr_arg3. The ioctl may return fewer buffers, depending on memory
113f0ea3689SLuigi Rizzo  *   availability. nr_arg3 will return the actual value, and, once
114f0ea3689SLuigi Rizzo  *   mapped, nifp->ni_bufs_head will be the index of the first buffer.
115f0ea3689SLuigi Rizzo  *
116f0ea3689SLuigi Rizzo  *   The buffers are linked to each other using the first uint32_t
117f0ea3689SLuigi Rizzo  *   as the index. On close, ni_bufs_head must point to the list of
118f0ea3689SLuigi Rizzo  *   buffers to be released.
119f0ea3689SLuigi Rizzo  *
120f0ea3689SLuigi Rizzo  * + NIOCREGIF can request space for extra rings (and buffers)
121f0ea3689SLuigi Rizzo  *   allocated in the same memory space. The number of extra rings
122f0ea3689SLuigi Rizzo  *   is in nr_arg1, and is advisory. This is a no-op on NICs where
123f0ea3689SLuigi Rizzo  *   the size of the memory space is fixed.
124f0ea3689SLuigi Rizzo  *
125f0ea3689SLuigi Rizzo  * + NIOCREGIF can attach to PIPE rings sharing the same memory
126f0ea3689SLuigi Rizzo  *   space with a parent device. The ifname indicates the parent device,
127f0ea3689SLuigi Rizzo  *   which must already exist. Flags in nr_flags indicate if we want to
128f0ea3689SLuigi Rizzo  *   bind the master or slave side, the index (from nr_ringid)
1295c8c1004SLuigi Rizzo  *   is just a cookie and does not need to be sequential.
130f0ea3689SLuigi Rizzo  *
131f0ea3689SLuigi Rizzo  * + NIOCREGIF can also attach to 'monitor' rings that replicate
132f0ea3689SLuigi Rizzo  *   the content of specific rings, also from the same memory space.
133f0ea3689SLuigi Rizzo  *
134f0ea3689SLuigi Rizzo  *   Extra flags in nr_flags support the above functions.
135f0ea3689SLuigi Rizzo  *   Application libraries may use the following naming scheme:
136f0ea3689SLuigi Rizzo  *	netmap:foo			all NIC ring pairs
137f0ea3689SLuigi Rizzo  *	netmap:foo^			only host ring pair
138f0ea3689SLuigi Rizzo  *	netmap:foo+			all NIC ring + host ring pairs
139f0ea3689SLuigi Rizzo  *	netmap:foo-k			the k-th NIC ring pair
140f0ea3689SLuigi Rizzo  *	netmap:foo{k			PIPE ring pair k, master side
141f0ea3689SLuigi Rizzo  *	netmap:foo}k			PIPE ring pair k, slave side
14237e3a6d3SLuigi Rizzo  *
14337e3a6d3SLuigi Rizzo  * Some notes about host rings:
14437e3a6d3SLuigi Rizzo  *
14537e3a6d3SLuigi Rizzo  * + The RX host ring is used to store those packets that the host network
14637e3a6d3SLuigi Rizzo  *   stack is trying to transmit through a NIC queue, but only if that queue
14737e3a6d3SLuigi Rizzo  *   is currently in netmap mode. Netmap will not intercept host stack mbufs
14837e3a6d3SLuigi Rizzo  *   designated to NIC queues that are not in netmap mode. As a consequence,
14937e3a6d3SLuigi Rizzo  *   registering a netmap port with netmap:foo^ is not enough to intercept
15037e3a6d3SLuigi Rizzo  *   mbufs in the RX host ring; the netmap port should be registered with
15137e3a6d3SLuigi Rizzo  *   netmap:foo*, or another registration should be done to open at least a
15237e3a6d3SLuigi Rizzo  *   NIC TX queue in netmap mode.
15337e3a6d3SLuigi Rizzo  *
15437e3a6d3SLuigi Rizzo  * + Netmap is not currently able to deal with intercepted trasmit mbufs which
15537e3a6d3SLuigi Rizzo  *   require offloadings like TSO, UFO, checksumming offloadings, etc. It is
15637e3a6d3SLuigi Rizzo  *   responsibility of the user to disable those offloadings (e.g. using
15737e3a6d3SLuigi Rizzo  *   ifconfig on FreeBSD or ethtool -K on Linux) for an interface that is being
15837e3a6d3SLuigi Rizzo  *   used in netmap mode. If the offloadings are not disabled, GSO and/or
15937e3a6d3SLuigi Rizzo  *   unchecksummed packets may be dropped immediately or end up in the host RX
16037e3a6d3SLuigi Rizzo  *   ring, and will be dropped as soon as the packet reaches another netmap
16137e3a6d3SLuigi Rizzo  *   adapter.
162ce3ee1e7SLuigi Rizzo  */
163ce3ee1e7SLuigi Rizzo 
164ce3ee1e7SLuigi Rizzo /*
165ce3ee1e7SLuigi Rizzo  * struct netmap_slot is a buffer descriptor
16617885a7bSLuigi Rizzo  */
16717885a7bSLuigi Rizzo struct netmap_slot {
16817885a7bSLuigi Rizzo 	uint32_t buf_idx;	/* buffer index */
16917885a7bSLuigi Rizzo 	uint16_t len;		/* length for this slot */
17017885a7bSLuigi Rizzo 	uint16_t flags;		/* buf changed, etc. */
17117885a7bSLuigi Rizzo 	uint64_t ptr;		/* pointer for indirect buffers */
17217885a7bSLuigi Rizzo };
17317885a7bSLuigi Rizzo 
17417885a7bSLuigi Rizzo /*
17517885a7bSLuigi Rizzo  * The following flags control how the slot is used
17617885a7bSLuigi Rizzo  */
17717885a7bSLuigi Rizzo 
17817885a7bSLuigi Rizzo #define	NS_BUF_CHANGED	0x0001	/* buf_idx changed */
17917885a7bSLuigi Rizzo 	/*
18017885a7bSLuigi Rizzo 	 * must be set whenever buf_idx is changed (as it might be
18117885a7bSLuigi Rizzo 	 * necessary to recompute the physical address and mapping)
182847bf383SLuigi Rizzo 	 *
183847bf383SLuigi Rizzo 	 * It is also set by the kernel whenever the buf_idx is
184847bf383SLuigi Rizzo 	 * changed internally (e.g., by pipes). Applications may
185847bf383SLuigi Rizzo 	 * use this information to know when they can reuse the
186847bf383SLuigi Rizzo 	 * contents of previously prepared buffers.
18717885a7bSLuigi Rizzo 	 */
18817885a7bSLuigi Rizzo 
18917885a7bSLuigi Rizzo #define	NS_REPORT	0x0002	/* ask the hardware to report results */
19017885a7bSLuigi Rizzo 	/*
19117885a7bSLuigi Rizzo 	 * Request notification when slot is used by the hardware.
19217885a7bSLuigi Rizzo 	 * Normally transmit completions are handled lazily and
19317885a7bSLuigi Rizzo 	 * may be unreported. This flag lets us know when a slot
19417885a7bSLuigi Rizzo 	 * has been sent (e.g. to terminate the sender).
19517885a7bSLuigi Rizzo 	 */
19617885a7bSLuigi Rizzo 
19717885a7bSLuigi Rizzo #define	NS_FORWARD	0x0004	/* pass packet 'forward' */
19817885a7bSLuigi Rizzo 	/*
19917885a7bSLuigi Rizzo 	 * (Only for physical ports, rx rings with NR_FORWARD set).
20017885a7bSLuigi Rizzo 	 * Slot released to the kernel (i.e. before ring->head) with
20117885a7bSLuigi Rizzo 	 * this flag set are passed to the peer ring (host/NIC),
20217885a7bSLuigi Rizzo 	 * thus restoring the host-NIC connection for these slots.
20317885a7bSLuigi Rizzo 	 * This supports efficient traffic monitoring or firewalling.
20417885a7bSLuigi Rizzo 	 */
20517885a7bSLuigi Rizzo 
20617885a7bSLuigi Rizzo #define	NS_NO_LEARN	0x0008	/* disable bridge learning */
20717885a7bSLuigi Rizzo  	/*
20817885a7bSLuigi Rizzo 	 * On a VALE switch, do not 'learn' the source port for
20917885a7bSLuigi Rizzo  	 * this buffer.
21017885a7bSLuigi Rizzo 	 */
21117885a7bSLuigi Rizzo 
21217885a7bSLuigi Rizzo #define	NS_INDIRECT	0x0010	/* userspace buffer */
21317885a7bSLuigi Rizzo  	/*
21417885a7bSLuigi Rizzo 	 * (VALE tx rings only) data is in a userspace buffer,
21517885a7bSLuigi Rizzo 	 * whose address is in the 'ptr' field in the slot.
21617885a7bSLuigi Rizzo 	 */
21717885a7bSLuigi Rizzo 
21817885a7bSLuigi Rizzo #define	NS_MOREFRAG	0x0020	/* packet has more fragments */
21917885a7bSLuigi Rizzo  	/*
2204f80b14cSVincenzo Maffione 	 * (VALE ports, ptnetmap ports and some NIC ports, e.g.
2214f80b14cSVincenzo Maffione          * ixgbe and i40e on Linux)
22217885a7bSLuigi Rizzo 	 * Set on all but the last slot of a multi-segment packet.
22317885a7bSLuigi Rizzo 	 * The 'len' field refers to the individual fragment.
22417885a7bSLuigi Rizzo 	 */
22517885a7bSLuigi Rizzo 
22617885a7bSLuigi Rizzo #define	NS_PORT_SHIFT	8
22717885a7bSLuigi Rizzo #define	NS_PORT_MASK	(0xff << NS_PORT_SHIFT)
22817885a7bSLuigi Rizzo 	/*
22917885a7bSLuigi Rizzo  	 * The high 8 bits of the flag, if not zero, indicate the
23001c039a1SLuigi Rizzo 	 * destination port for the VALE switch, overriding
23101c039a1SLuigi Rizzo  	 * the lookup table.
23268b8534bSLuigi Rizzo  	 */
23301c039a1SLuigi Rizzo 
23417885a7bSLuigi Rizzo #define	NS_RFRAGS(_slot)	( ((_slot)->flags >> 8) & 0xff)
235ce3ee1e7SLuigi Rizzo 	/*
23617885a7bSLuigi Rizzo 	 * (VALE rx rings only) the high 8 bits
237ce3ee1e7SLuigi Rizzo 	 *  are the number of fragments.
238ce3ee1e7SLuigi Rizzo 	 */
23917885a7bSLuigi Rizzo 
2402a7db7a6SVincenzo Maffione #define NETMAP_MAX_FRAGS	64	/* max number of fragments */
2412a7db7a6SVincenzo Maffione 
24268b8534bSLuigi Rizzo 
24368b8534bSLuigi Rizzo /*
244ce3ee1e7SLuigi Rizzo  * struct netmap_ring
245ce3ee1e7SLuigi Rizzo  *
24668b8534bSLuigi Rizzo  * Netmap representation of a TX or RX ring (also known as "queue").
24768b8534bSLuigi Rizzo  * This is a queue implemented as a fixed-size circular array.
24817885a7bSLuigi Rizzo  * At the software level the important fields are: head, cur, tail.
24968b8534bSLuigi Rizzo  *
25068b8534bSLuigi Rizzo  * In TX rings:
251ce3ee1e7SLuigi Rizzo  *
25217885a7bSLuigi Rizzo  *	head	first slot available for transmission.
25317885a7bSLuigi Rizzo  *	cur	wakeup point. select() and poll() will unblock
25417885a7bSLuigi Rizzo  *		when 'tail' moves past 'cur'
25517885a7bSLuigi Rizzo  *	tail	(readonly) first slot reserved to the kernel
256ce3ee1e7SLuigi Rizzo  *
25717885a7bSLuigi Rizzo  *	[head .. tail-1] can be used for new packets to send;
25817885a7bSLuigi Rizzo  *	'head' and 'cur' must be incremented as slots are filled
25917885a7bSLuigi Rizzo  *	    with new packets to be sent;
26017885a7bSLuigi Rizzo  *	'cur' can be moved further ahead if we need more space
2615c8c1004SLuigi Rizzo  *	for new transmissions. XXX todo (2014-03-12)
26268b8534bSLuigi Rizzo  *
26368b8534bSLuigi Rizzo  * In RX rings:
264ce3ee1e7SLuigi Rizzo  *
26517885a7bSLuigi Rizzo  *	head	first valid received packet
26617885a7bSLuigi Rizzo  *	cur	wakeup point. select() and poll() will unblock
26717885a7bSLuigi Rizzo  *		when 'tail' moves past 'cur'
26817885a7bSLuigi Rizzo  *	tail	(readonly) first slot reserved to the kernel
269ce3ee1e7SLuigi Rizzo  *
27017885a7bSLuigi Rizzo  *	[head .. tail-1] contain received packets;
27117885a7bSLuigi Rizzo  *	'head' and 'cur' must be incremented as slots are consumed
27217885a7bSLuigi Rizzo  *		and can be returned to the kernel;
27317885a7bSLuigi Rizzo  *	'cur' can be moved further ahead if we want to wait for
27417885a7bSLuigi Rizzo  *		new packets without returning the previous ones.
27568b8534bSLuigi Rizzo  *
27668b8534bSLuigi Rizzo  * DATA OWNERSHIP/LOCKING:
27717885a7bSLuigi Rizzo  *	The netmap_ring, and all slots and buffers in the range
27817885a7bSLuigi Rizzo  *	[head .. tail-1] are owned by the user program;
27917885a7bSLuigi Rizzo  *	the kernel only accesses them during a netmap system call
28017885a7bSLuigi Rizzo  *	and in the user thread context.
28101c039a1SLuigi Rizzo  *
28217885a7bSLuigi Rizzo  *	Other slots and buffers are reserved for use by the kernel
28368b8534bSLuigi Rizzo  */
28468b8534bSLuigi Rizzo struct netmap_ring {
28568b8534bSLuigi Rizzo 	/*
286ce3ee1e7SLuigi Rizzo 	 * buf_ofs is meant to be used through macros.
28768b8534bSLuigi Rizzo 	 * It contains the offset of the buffer region from this
28868b8534bSLuigi Rizzo 	 * descriptor.
28968b8534bSLuigi Rizzo 	 */
29017885a7bSLuigi Rizzo 	const int64_t	buf_ofs;
29168b8534bSLuigi Rizzo 	const uint32_t	num_slots;	/* number of slots in the ring. */
29217885a7bSLuigi Rizzo 	const uint32_t	nr_buf_size;
29317885a7bSLuigi Rizzo 	const uint16_t	ringid;
29417885a7bSLuigi Rizzo 	const uint16_t	dir;		/* 0: tx, 1: rx */
29568b8534bSLuigi Rizzo 
29617885a7bSLuigi Rizzo 	uint32_t        head;		/* (u) first user slot */
29717885a7bSLuigi Rizzo 	uint32_t        cur;		/* (u) wakeup point */
29817885a7bSLuigi Rizzo 	uint32_t	tail;		/* (k) first kernel slot */
29968b8534bSLuigi Rizzo 
30017885a7bSLuigi Rizzo 	uint32_t	flags;
30117885a7bSLuigi Rizzo 
30217885a7bSLuigi Rizzo 	struct timeval	ts;		/* (k) time of last *sync() */
30317885a7bSLuigi Rizzo 
30417885a7bSLuigi Rizzo 	/* opaque room for a mutex or similar object */
30537e3a6d3SLuigi Rizzo #if !defined(_WIN32) || defined(__CYGWIN__)
30637e3a6d3SLuigi Rizzo 	uint8_t	__attribute__((__aligned__(NM_CACHE_ALIGN))) sem[128];
30737e3a6d3SLuigi Rizzo #else
30837e3a6d3SLuigi Rizzo 	uint8_t	__declspec(align(NM_CACHE_ALIGN)) sem[128];
30937e3a6d3SLuigi Rizzo #endif
31068b8534bSLuigi Rizzo 
31168b8534bSLuigi Rizzo 	/* the slots follow. This struct has variable size */
31268b8534bSLuigi Rizzo 	struct netmap_slot slot[0];	/* array of slots. */
31368b8534bSLuigi Rizzo };
31468b8534bSLuigi Rizzo 
31568b8534bSLuigi Rizzo 
31668b8534bSLuigi Rizzo /*
31717885a7bSLuigi Rizzo  * RING FLAGS
31817885a7bSLuigi Rizzo  */
31917885a7bSLuigi Rizzo #define	NR_TIMESTAMP	0x0002		/* set timestamp on *sync() */
32017885a7bSLuigi Rizzo 	/*
32117885a7bSLuigi Rizzo 	 * updates the 'ts' field on each netmap syscall. This saves
32217885a7bSLuigi Rizzo 	 * saves a separate gettimeofday(), and is not much worse than
32317885a7bSLuigi Rizzo 	 * software timestamps generated in the interrupt handler.
32417885a7bSLuigi Rizzo 	 */
32517885a7bSLuigi Rizzo 
32617885a7bSLuigi Rizzo #define	NR_FORWARD	0x0004		/* enable NS_FORWARD for ring */
32717885a7bSLuigi Rizzo  	/*
32817885a7bSLuigi Rizzo 	 * Enables the NS_FORWARD slot flag for the ring.
32917885a7bSLuigi Rizzo 	 */
33017885a7bSLuigi Rizzo 
3312ff91c17SVincenzo Maffione /*
3322ff91c17SVincenzo Maffione  * Helper functions for kernel and userspace
3332ff91c17SVincenzo Maffione  */
3342ff91c17SVincenzo Maffione 
3352ff91c17SVincenzo Maffione /*
336*b6e66be2SVincenzo Maffione  * Check if space is available in the ring. We use ring->head, which
337*b6e66be2SVincenzo Maffione  * points to the next netmap slot to be published to netmap. It is
338*b6e66be2SVincenzo Maffione  * possible that the applications moves ring->cur ahead of ring->tail
339*b6e66be2SVincenzo Maffione  * (e.g., by setting ring->cur <== ring->tail), if it wants more slots
340*b6e66be2SVincenzo Maffione  * than the ones currently available, and it wants to be notified when
341*b6e66be2SVincenzo Maffione  * more arrive. See netmap(4) for more details and examples.
3422ff91c17SVincenzo Maffione  */
3432ff91c17SVincenzo Maffione static inline int
3442ff91c17SVincenzo Maffione nm_ring_empty(struct netmap_ring *ring)
3452ff91c17SVincenzo Maffione {
346*b6e66be2SVincenzo Maffione 	return (ring->head == ring->tail);
3472ff91c17SVincenzo Maffione }
34817885a7bSLuigi Rizzo 
34917885a7bSLuigi Rizzo /*
35068b8534bSLuigi Rizzo  * Netmap representation of an interface and its queue(s).
351ce3ee1e7SLuigi Rizzo  * This is initialized by the kernel when binding a file
352ce3ee1e7SLuigi Rizzo  * descriptor to a port, and should be considered as readonly
353ce3ee1e7SLuigi Rizzo  * by user programs. The kernel never uses it.
354ce3ee1e7SLuigi Rizzo  *
35568b8534bSLuigi Rizzo  * There is one netmap_if for each file descriptor on which we want
356ce3ee1e7SLuigi Rizzo  * to select/poll.
35768b8534bSLuigi Rizzo  * select/poll operates on one or all pairs depending on the value of
35868b8534bSLuigi Rizzo  * nmr_queueid passed on the ioctl.
35968b8534bSLuigi Rizzo  */
36068b8534bSLuigi Rizzo struct netmap_if {
36168b8534bSLuigi Rizzo 	char		ni_name[IFNAMSIZ]; /* name of the interface. */
362ce3ee1e7SLuigi Rizzo 	const uint32_t	ni_version;	/* API version, currently unused */
363ce3ee1e7SLuigi Rizzo 	const uint32_t	ni_flags;	/* properties */
364ce3ee1e7SLuigi Rizzo #define	NI_PRIV_MEM	0x1		/* private memory region */
365ce3ee1e7SLuigi Rizzo 
36617885a7bSLuigi Rizzo 	/*
36717885a7bSLuigi Rizzo 	 * The number of packet rings available in netmap mode.
36817885a7bSLuigi Rizzo 	 * Physical NICs can have different numbers of tx and rx rings.
36917885a7bSLuigi Rizzo 	 * Physical NICs also have a 'host' ring pair.
37017885a7bSLuigi Rizzo 	 * Additionally, clients can request additional ring pairs to
37117885a7bSLuigi Rizzo 	 * be used for internal communication.
37217885a7bSLuigi Rizzo 	 */
37317885a7bSLuigi Rizzo 	const uint32_t	ni_tx_rings;	/* number of HW tx rings */
37417885a7bSLuigi Rizzo 	const uint32_t	ni_rx_rings;	/* number of HW rx rings */
37517885a7bSLuigi Rizzo 
376f0ea3689SLuigi Rizzo 	uint32_t	ni_bufs_head;	/* head index for extra bufs */
377f0ea3689SLuigi Rizzo 	uint32_t	ni_spare1[5];
37868b8534bSLuigi Rizzo 	/*
37964ae02c3SLuigi Rizzo 	 * The following array contains the offset of each netmap ring
38017885a7bSLuigi Rizzo 	 * from this structure, in the following order:
38117885a7bSLuigi Rizzo 	 * NIC tx rings (ni_tx_rings); host tx ring (1); extra tx rings;
38217885a7bSLuigi Rizzo 	 * NIC rx rings (ni_rx_rings); host tx ring (1); extra rx rings.
38317885a7bSLuigi Rizzo 	 *
384ce3ee1e7SLuigi Rizzo 	 * The area is filled up by the kernel on NIOCREGIF,
38568b8534bSLuigi Rizzo 	 * and then only read by userspace code.
38668b8534bSLuigi Rizzo 	 */
38768b8534bSLuigi Rizzo 	const ssize_t	ring_ofs[0];
38868b8534bSLuigi Rizzo };
38968b8534bSLuigi Rizzo 
3902ff91c17SVincenzo Maffione /* Legacy interface to interact with a netmap control device.
3912ff91c17SVincenzo Maffione  * Included for backward compatibility. The user should not include this
3922ff91c17SVincenzo Maffione  * file directly. */
3932ff91c17SVincenzo Maffione #include "netmap_legacy.h"
39417885a7bSLuigi Rizzo 
39568b8534bSLuigi Rizzo /*
3962ff91c17SVincenzo Maffione  * New API to control netmap control devices. New applications should only use
3972ff91c17SVincenzo Maffione  * nmreq_xyz structs with the NIOCCTRL ioctl() command.
39868b8534bSLuigi Rizzo  *
3992ff91c17SVincenzo Maffione  * NIOCCTRL takes a nmreq_header struct, which contains the required
4002ff91c17SVincenzo Maffione  * API version, the name of a netmap port, a command type, and pointers
4012ff91c17SVincenzo Maffione  * to request body and options.
402f18be576SLuigi Rizzo  *
40317885a7bSLuigi Rizzo  *	nr_name	(in)
4042ff91c17SVincenzo Maffione  *		The name of the port (em0, valeXXX:YYY, eth0{pn1 etc.)
405ce3ee1e7SLuigi Rizzo  *
40617885a7bSLuigi Rizzo  *	nr_version (in/out)
40717885a7bSLuigi Rizzo  *		Must match NETMAP_API as used in the kernel, error otherwise.
40817885a7bSLuigi Rizzo  *		Always returns the desired value on output.
40917885a7bSLuigi Rizzo  *
4102ff91c17SVincenzo Maffione  *	nr_reqtype (in)
4112ff91c17SVincenzo Maffione  *		One of the NETMAP_REQ_* command types below
4122ff91c17SVincenzo Maffione  *
4132ff91c17SVincenzo Maffione  *	nr_body (in)
4142ff91c17SVincenzo Maffione  *		Pointer to a command-specific struct, described by one
4152ff91c17SVincenzo Maffione  *		of the struct nmreq_xyz below.
4162ff91c17SVincenzo Maffione  *
4172ff91c17SVincenzo Maffione  *	nr_options (in)
4182ff91c17SVincenzo Maffione  *		Command specific options, if any.
4192ff91c17SVincenzo Maffione  *
4202ff91c17SVincenzo Maffione  * A NETMAP_REQ_REGISTER command activates netmap mode on the netmap
4212ff91c17SVincenzo Maffione  * port (e.g. physical interface) specified by nmreq_header.nr_name.
4222ff91c17SVincenzo Maffione  * The request body (struct nmreq_register) has several arguments to
4232ff91c17SVincenzo Maffione  * specify how the port is to be registered.
4242ff91c17SVincenzo Maffione  *
42517885a7bSLuigi Rizzo  *	nr_tx_slots, nr_tx_slots, nr_tx_rings, nr_rx_rings (in/out)
42617885a7bSLuigi Rizzo  *		On input, non-zero values may be used to reconfigure the port
42717885a7bSLuigi Rizzo  *		according to the requested values, but this is not guaranteed.
42817885a7bSLuigi Rizzo  *		On output the actual values in use are reported.
42917885a7bSLuigi Rizzo  *
4302ff91c17SVincenzo Maffione  *	nr_mode (in)
4312ff91c17SVincenzo Maffione  *		Indicate what set of rings must be bound to the netmap
4322ff91c17SVincenzo Maffione  *		device (e.g. all NIC rings, host rings only, NIC and
4332ff91c17SVincenzo Maffione  *		host rings, ...). Values are in NR_REG_*.
4342ff91c17SVincenzo Maffione  *
43517885a7bSLuigi Rizzo  *	nr_ringid (in)
4362ff91c17SVincenzo Maffione  *		If nr_mode == NR_REG_ONE_NIC (only a single couple of TX/RX
4372ff91c17SVincenzo Maffione  *		rings), indicate which NIC TX and/or RX ring is to be bound
4382ff91c17SVincenzo Maffione  *		(0..nr_*x_rings-1).
439f0ea3689SLuigi Rizzo  *
4402ff91c17SVincenzo Maffione  *	nr_flags (in)
4412ff91c17SVincenzo Maffione  *		Indicate special options for how to open the port.
442ce3ee1e7SLuigi Rizzo  *
4432ff91c17SVincenzo Maffione  *		NR_NO_TX_POLL can be OR-ed to make select()/poll() push
44417885a7bSLuigi Rizzo  *			packets on tx rings only if POLLOUT is set.
44517885a7bSLuigi Rizzo  *			The default is to push any pending packet.
446ce3ee1e7SLuigi Rizzo  *
4472ff91c17SVincenzo Maffione  *		NR_DO_RX_POLL can be OR-ed to make select()/poll() release
448f0ea3689SLuigi Rizzo  *			packets on rx rings also when POLLIN is NOT set.
449f0ea3689SLuigi Rizzo  *			The default is to touch the rx ring only with POLLIN.
450f0ea3689SLuigi Rizzo  *			Note that this is the opposite of TX because it
451f0ea3689SLuigi Rizzo  *			reflects the common usage.
452f0ea3689SLuigi Rizzo  *
4532ff91c17SVincenzo Maffione  *		Other options are NR_MONITOR_TX, NR_MONITOR_RX, NR_ZCOPY_MON,
4542ff91c17SVincenzo Maffione  *		NR_EXCLUSIVE, NR_RX_RINGS_ONLY, NR_TX_RINGS_ONLY and
4552ff91c17SVincenzo Maffione  *		NR_ACCEPT_VNET_HDR.
456f0ea3689SLuigi Rizzo  *
4572ff91c17SVincenzo Maffione  *	nr_mem_id (in/out)
4582ff91c17SVincenzo Maffione  *		The identity of the memory region used.
459f0ea3689SLuigi Rizzo  *		On input, 0 means the system decides autonomously,
460f0ea3689SLuigi Rizzo  *		other values may try to select a specific region.
461f0ea3689SLuigi Rizzo  *		On return the actual value is reported.
462f0ea3689SLuigi Rizzo  *		Region '1' is the global allocator, normally shared
463f0ea3689SLuigi Rizzo  *		by all interfaces. Other values are private regions.
464f0ea3689SLuigi Rizzo  *		If two ports the same region zero-copy is possible.
465f0ea3689SLuigi Rizzo  *
4662ff91c17SVincenzo Maffione  *	nr_extra_bufs (in/out)
4672ff91c17SVincenzo Maffione  *		Number of extra buffers to be allocated.
468f0ea3689SLuigi Rizzo  *
4692ff91c17SVincenzo Maffione  * The other NETMAP_REQ_* commands are described below.
47017885a7bSLuigi Rizzo  *
47168b8534bSLuigi Rizzo  */
47268b8534bSLuigi Rizzo 
4732ff91c17SVincenzo Maffione /* maximum size of a request, including all options */
4742ff91c17SVincenzo Maffione #define NETMAP_REQ_MAXSIZE	4096
4752ff91c17SVincenzo Maffione 
4762ff91c17SVincenzo Maffione /* Header common to all request options. */
4772ff91c17SVincenzo Maffione struct nmreq_option {
4782ff91c17SVincenzo Maffione 	/* Pointer ot the next option. */
4792ff91c17SVincenzo Maffione 	uint64_t		nro_next;
4802ff91c17SVincenzo Maffione 	/* Option type. */
4812ff91c17SVincenzo Maffione 	uint32_t		nro_reqtype;
4822ff91c17SVincenzo Maffione 	/* (out) status of the option:
4832ff91c17SVincenzo Maffione 	 * 0: recognized and processed
4842ff91c17SVincenzo Maffione 	 * !=0: errno value
4852ff91c17SVincenzo Maffione 	 */
4862ff91c17SVincenzo Maffione 	uint32_t		nro_status;
487*b6e66be2SVincenzo Maffione 	/* Option size, used only for options that can have variable size
488*b6e66be2SVincenzo Maffione 	 * (e.g. because they contain arrays). For fixed-size options this
489*b6e66be2SVincenzo Maffione 	 * field should be set to zero. */
490*b6e66be2SVincenzo Maffione 	uint64_t		nro_size;
4912ff91c17SVincenzo Maffione };
4922ff91c17SVincenzo Maffione 
4932ff91c17SVincenzo Maffione /* Header common to all requests. Do not reorder these fields, as we need
4942ff91c17SVincenzo Maffione  * the second one (nr_reqtype) to know how much to copy from/to userspace. */
4952ff91c17SVincenzo Maffione struct nmreq_header {
4962ff91c17SVincenzo Maffione 	uint16_t		nr_version;	/* API version */
4972ff91c17SVincenzo Maffione 	uint16_t		nr_reqtype;	/* nmreq type (NETMAP_REQ_*) */
4982ff91c17SVincenzo Maffione 	uint32_t		nr_reserved;	/* must be zero */
4992ff91c17SVincenzo Maffione #define NETMAP_REQ_IFNAMSIZ	64
5002ff91c17SVincenzo Maffione 	char			nr_name[NETMAP_REQ_IFNAMSIZ]; /* port name */
5012ff91c17SVincenzo Maffione 	uint64_t		nr_options;	/* command-specific options */
5022ff91c17SVincenzo Maffione 	uint64_t		nr_body;	/* ptr to nmreq_xyz struct */
5032ff91c17SVincenzo Maffione };
5042ff91c17SVincenzo Maffione 
5052ff91c17SVincenzo Maffione enum {
5062ff91c17SVincenzo Maffione 	/* Register a netmap port with the device. */
5072ff91c17SVincenzo Maffione 	NETMAP_REQ_REGISTER = 1,
5082ff91c17SVincenzo Maffione 	/* Get information from a netmap port. */
5092ff91c17SVincenzo Maffione 	NETMAP_REQ_PORT_INFO_GET,
5102ff91c17SVincenzo Maffione 	/* Attach a netmap port to a VALE switch. */
5112ff91c17SVincenzo Maffione 	NETMAP_REQ_VALE_ATTACH,
5122ff91c17SVincenzo Maffione 	/* Detach a netmap port from a VALE switch. */
5132ff91c17SVincenzo Maffione 	NETMAP_REQ_VALE_DETACH,
5142ff91c17SVincenzo Maffione 	/* List the ports attached to a VALE switch. */
5152ff91c17SVincenzo Maffione 	NETMAP_REQ_VALE_LIST,
5162ff91c17SVincenzo Maffione 	/* Set the port header length (was virtio-net header length). */
5172ff91c17SVincenzo Maffione 	NETMAP_REQ_PORT_HDR_SET,
5182ff91c17SVincenzo Maffione 	/* Get the port header length (was virtio-net header length). */
5192ff91c17SVincenzo Maffione 	NETMAP_REQ_PORT_HDR_GET,
5202ff91c17SVincenzo Maffione 	/* Create a new persistent VALE port. */
5212ff91c17SVincenzo Maffione 	NETMAP_REQ_VALE_NEWIF,
5222ff91c17SVincenzo Maffione 	/* Delete a persistent VALE port. */
5232ff91c17SVincenzo Maffione 	NETMAP_REQ_VALE_DELIF,
5242ff91c17SVincenzo Maffione 	/* Enable polling kernel thread(s) on an attached VALE port. */
5252ff91c17SVincenzo Maffione 	NETMAP_REQ_VALE_POLLING_ENABLE,
5262ff91c17SVincenzo Maffione 	/* Disable polling kernel thread(s) on an attached VALE port. */
5272ff91c17SVincenzo Maffione 	NETMAP_REQ_VALE_POLLING_DISABLE,
5282ff91c17SVincenzo Maffione 	/* Get info about the pools of a memory allocator. */
5292ff91c17SVincenzo Maffione 	NETMAP_REQ_POOLS_INFO_GET,
530*b6e66be2SVincenzo Maffione 	/* Start an in-kernel loop that syncs the rings periodically or
531*b6e66be2SVincenzo Maffione 	 * on notifications. The loop runs in the context of the ioctl
532*b6e66be2SVincenzo Maffione 	 * syscall, and only stops on NETMAP_REQ_SYNC_KLOOP_STOP. */
533*b6e66be2SVincenzo Maffione 	NETMAP_REQ_SYNC_KLOOP_START,
534*b6e66be2SVincenzo Maffione 	/* Stops the thread executing the in-kernel loop. The thread
535*b6e66be2SVincenzo Maffione 	 * returns from the ioctl syscall. */
536*b6e66be2SVincenzo Maffione 	NETMAP_REQ_SYNC_KLOOP_STOP,
537*b6e66be2SVincenzo Maffione 	/* Enable CSB mode on a registered netmap control device. */
538*b6e66be2SVincenzo Maffione 	NETMAP_REQ_CSB_ENABLE,
5392ff91c17SVincenzo Maffione };
5402ff91c17SVincenzo Maffione 
5412ff91c17SVincenzo Maffione enum {
5422ff91c17SVincenzo Maffione 	/* On NETMAP_REQ_REGISTER, ask netmap to use memory allocated
5432ff91c17SVincenzo Maffione 	 * from user-space allocated memory pools (e.g. hugepages). */
5442ff91c17SVincenzo Maffione 	NETMAP_REQ_OPT_EXTMEM = 1,
545*b6e66be2SVincenzo Maffione 
546*b6e66be2SVincenzo Maffione 	/* ON NETMAP_REQ_SYNC_KLOOP_START, ask netmap to use eventfd-based
547*b6e66be2SVincenzo Maffione 	 * notifications to synchronize the kernel loop with the application.
548*b6e66be2SVincenzo Maffione 	 */
549*b6e66be2SVincenzo Maffione 	NETMAP_REQ_OPT_SYNC_KLOOP_EVENTFDS,
550*b6e66be2SVincenzo Maffione 
551*b6e66be2SVincenzo Maffione 	/* On NETMAP_REQ_REGISTER, ask netmap to work in CSB mode, where
552*b6e66be2SVincenzo Maffione 	 * head, cur and tail pointers are not exchanged through the
553*b6e66be2SVincenzo Maffione 	 * struct netmap_ring header, but rather using an user-provided
554*b6e66be2SVincenzo Maffione 	 * memory area (see struct nm_csb_atok and struct nm_csb_ktoa). */
555*b6e66be2SVincenzo Maffione 	NETMAP_REQ_OPT_CSB,
5562ff91c17SVincenzo Maffione };
55717885a7bSLuigi Rizzo 
55868b8534bSLuigi Rizzo /*
5592ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_REGISTER
5602ff91c17SVincenzo Maffione  * Bind (register) a netmap port to this control device.
56168b8534bSLuigi Rizzo  */
5622ff91c17SVincenzo Maffione struct nmreq_register {
5632ff91c17SVincenzo Maffione 	uint64_t	nr_offset;	/* nifp offset in the shared region */
5642ff91c17SVincenzo Maffione 	uint64_t	nr_memsize;	/* size of the shared region */
56564ae02c3SLuigi Rizzo 	uint32_t	nr_tx_slots;	/* slots in tx rings */
56664ae02c3SLuigi Rizzo 	uint32_t	nr_rx_slots;	/* slots in rx rings */
56764ae02c3SLuigi Rizzo 	uint16_t	nr_tx_rings;	/* number of tx rings */
56864ae02c3SLuigi Rizzo 	uint16_t	nr_rx_rings;	/* number of rx rings */
569f0ea3689SLuigi Rizzo 
5702ff91c17SVincenzo Maffione 	uint16_t	nr_mem_id;	/* id of the memory allocator */
57168b8534bSLuigi Rizzo 	uint16_t	nr_ringid;	/* ring(s) we care about */
5722ff91c17SVincenzo Maffione 	uint32_t	nr_mode;	/* specify NR_REG_* modes */
573*b6e66be2SVincenzo Maffione 	uint32_t	nr_extra_bufs;	/* number of requested extra buffers */
574f0ea3689SLuigi Rizzo 
5752ff91c17SVincenzo Maffione 	uint64_t	nr_flags;	/* additional flags (see below) */
5762ff91c17SVincenzo Maffione /* monitors use nr_ringid and nr_mode to select the rings to monitor */
577f0ea3689SLuigi Rizzo #define NR_MONITOR_TX	0x100
578f0ea3689SLuigi Rizzo #define NR_MONITOR_RX	0x200
579847bf383SLuigi Rizzo #define NR_ZCOPY_MON	0x400
580847bf383SLuigi Rizzo /* request exclusive access to the selected rings */
581847bf383SLuigi Rizzo #define NR_EXCLUSIVE	0x800
582*b6e66be2SVincenzo Maffione /* 0x1000 unused */
58337e3a6d3SLuigi Rizzo #define NR_RX_RINGS_ONLY	0x2000
58437e3a6d3SLuigi Rizzo #define NR_TX_RINGS_ONLY	0x4000
58537e3a6d3SLuigi Rizzo /* Applications set this flag if they are able to deal with virtio-net headers,
58637e3a6d3SLuigi Rizzo  * that is send/receive frames that start with a virtio-net header.
58737e3a6d3SLuigi Rizzo  * If not set, NIOCREGIF will fail with netmap ports that require applications
58837e3a6d3SLuigi Rizzo  * to use those headers. If the flag is set, the application can use the
58937e3a6d3SLuigi Rizzo  * NETMAP_VNET_HDR_GET command to figure out the header length. */
59037e3a6d3SLuigi Rizzo #define NR_ACCEPT_VNET_HDR	0x8000
5912ff91c17SVincenzo Maffione /* The following two have the same meaning of NETMAP_NO_TX_POLL and
5922ff91c17SVincenzo Maffione  * NETMAP_DO_RX_POLL. */
5932ff91c17SVincenzo Maffione #define NR_DO_RX_POLL		0x10000
5942ff91c17SVincenzo Maffione #define NR_NO_TX_POLL		0x20000
5952ff91c17SVincenzo Maffione };
5962ff91c17SVincenzo Maffione 
5972ff91c17SVincenzo Maffione /* Valid values for nmreq_register.nr_mode (see above). */
5982ff91c17SVincenzo Maffione enum {	NR_REG_DEFAULT	= 0,	/* backward compat, should not be used. */
5992ff91c17SVincenzo Maffione 	NR_REG_ALL_NIC	= 1,
6002ff91c17SVincenzo Maffione 	NR_REG_SW	= 2,
6012ff91c17SVincenzo Maffione 	NR_REG_NIC_SW	= 3,
6022ff91c17SVincenzo Maffione 	NR_REG_ONE_NIC	= 4,
6032ff91c17SVincenzo Maffione 	NR_REG_PIPE_MASTER = 5, /* deprecated, use "x{y" port name syntax */
6042ff91c17SVincenzo Maffione 	NR_REG_PIPE_SLAVE = 6,  /* deprecated, use "x}y" port name syntax */
605*b6e66be2SVincenzo Maffione 	NR_REG_NULL     = 7,
6062ff91c17SVincenzo Maffione };
6072ff91c17SVincenzo Maffione 
6082ff91c17SVincenzo Maffione /* A single ioctl number is shared by all the new API command.
609*b6e66be2SVincenzo Maffione  * Demultiplexing is done using the hdr.nr_reqtype field.
6102ff91c17SVincenzo Maffione  * FreeBSD uses the size value embedded in the _IOWR to determine
6112ff91c17SVincenzo Maffione  * how much to copy in/out, so we define the ioctl() command
6122ff91c17SVincenzo Maffione  * specifying only nmreq_header, and copyin/copyout the rest. */
6132ff91c17SVincenzo Maffione #define NIOCCTRL	_IOWR('i', 151, struct nmreq_header)
6142ff91c17SVincenzo Maffione 
6152ff91c17SVincenzo Maffione /* The ioctl commands to sync TX/RX netmap rings.
6162ff91c17SVincenzo Maffione  * NIOCTXSYNC, NIOCRXSYNC synchronize tx or rx queues,
6172ff91c17SVincenzo Maffione  *	whose identity is set in NIOCREGIF through nr_ringid.
6182ff91c17SVincenzo Maffione  *	These are non blocking and take no argument. */
6192ff91c17SVincenzo Maffione #define NIOCTXSYNC	_IO('i', 148) /* sync tx queues */
6202ff91c17SVincenzo Maffione #define NIOCRXSYNC	_IO('i', 149) /* sync rx queues */
6212ff91c17SVincenzo Maffione 
6222ff91c17SVincenzo Maffione /*
6232ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_PORT_INFO_GET
6242ff91c17SVincenzo Maffione  * Get information about a netmap port, including number of rings.
625*b6e66be2SVincenzo Maffione  * slots per ring, id of the memory allocator, etc. The netmap
626*b6e66be2SVincenzo Maffione  * control device used for this operation does not need to be bound
627*b6e66be2SVincenzo Maffione  * to a netmap port.
6282ff91c17SVincenzo Maffione  */
6292ff91c17SVincenzo Maffione struct nmreq_port_info_get {
6302ff91c17SVincenzo Maffione 	uint64_t	nr_memsize;	/* size of the shared region */
6312ff91c17SVincenzo Maffione 	uint32_t	nr_tx_slots;	/* slots in tx rings */
6322ff91c17SVincenzo Maffione 	uint32_t	nr_rx_slots;	/* slots in rx rings */
6332ff91c17SVincenzo Maffione 	uint16_t	nr_tx_rings;	/* number of tx rings */
6342ff91c17SVincenzo Maffione 	uint16_t	nr_rx_rings;	/* number of rx rings */
635*b6e66be2SVincenzo Maffione 	uint16_t	nr_mem_id;	/* memory allocator id (in/out) */
636*b6e66be2SVincenzo Maffione 	uint16_t	pad1;
6372ff91c17SVincenzo Maffione };
638f0ea3689SLuigi Rizzo 
63937e3a6d3SLuigi Rizzo #define	NM_BDG_NAME		"vale"	/* prefix for bridge port name */
64037e3a6d3SLuigi Rizzo 
64137e3a6d3SLuigi Rizzo /*
6422ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_VALE_ATTACH
6432ff91c17SVincenzo Maffione  * Attach a netmap port to a VALE switch. Both the name of the netmap
6442ff91c17SVincenzo Maffione  * port and the VALE switch are specified through the nr_name argument.
6452ff91c17SVincenzo Maffione  * The attach operation could need to register a port, so at least
6462ff91c17SVincenzo Maffione  * the same arguments are available.
6472ff91c17SVincenzo Maffione  * port_index will contain the index where the port has been attached.
64837e3a6d3SLuigi Rizzo  */
6492ff91c17SVincenzo Maffione struct nmreq_vale_attach {
6502ff91c17SVincenzo Maffione 	struct nmreq_register reg;
6512ff91c17SVincenzo Maffione 	uint32_t port_index;
652*b6e66be2SVincenzo Maffione 	uint32_t pad1;
6532ff91c17SVincenzo Maffione };
65417885a7bSLuigi Rizzo 
65564ae02c3SLuigi Rizzo /*
6562ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_VALE_DETACH
6572ff91c17SVincenzo Maffione  * Detach a netmap port from a VALE switch. Both the name of the netmap
6582ff91c17SVincenzo Maffione  * port and the VALE switch are specified through the nr_name argument.
6592ff91c17SVincenzo Maffione  * port_index will contain the index where the port was attached.
66064ae02c3SLuigi Rizzo  */
6612ff91c17SVincenzo Maffione struct nmreq_vale_detach {
6622ff91c17SVincenzo Maffione 	uint32_t port_index;
663*b6e66be2SVincenzo Maffione 	uint32_t pad1;
6642ff91c17SVincenzo Maffione };
66517885a7bSLuigi Rizzo 
66617885a7bSLuigi Rizzo /*
6672ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_VALE_LIST
6682ff91c17SVincenzo Maffione  * List the ports of a VALE switch.
66917885a7bSLuigi Rizzo  */
6702ff91c17SVincenzo Maffione struct nmreq_vale_list {
6712ff91c17SVincenzo Maffione 	/* Name of the VALE port (valeXXX:YYY) or empty. */
6722ff91c17SVincenzo Maffione 	uint16_t	nr_bridge_idx;
673*b6e66be2SVincenzo Maffione 	uint16_t	pad1;
6742ff91c17SVincenzo Maffione 	uint32_t	nr_port_idx;
6752ff91c17SVincenzo Maffione };
67617885a7bSLuigi Rizzo 
67717885a7bSLuigi Rizzo /*
6782ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_PORT_HDR_SET or NETMAP_REQ_PORT_HDR_GET
679*b6e66be2SVincenzo Maffione  * Set or get the port header length of the port identified by hdr.nr_name.
680*b6e66be2SVincenzo Maffione  * The control device does not need to be bound to a netmap port.
68117885a7bSLuigi Rizzo  */
6822ff91c17SVincenzo Maffione struct nmreq_port_hdr {
6832ff91c17SVincenzo Maffione 	uint32_t	nr_hdr_len;
684*b6e66be2SVincenzo Maffione 	uint32_t	pad1;
6852ff91c17SVincenzo Maffione };
68617885a7bSLuigi Rizzo 
6874bf50f18SLuigi Rizzo /*
6882ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_VALE_NEWIF
6892ff91c17SVincenzo Maffione  * Create a new persistent VALE port.
6904bf50f18SLuigi Rizzo  */
6912ff91c17SVincenzo Maffione struct nmreq_vale_newif {
6922ff91c17SVincenzo Maffione 	uint32_t	nr_tx_slots;	/* slots in tx rings */
6932ff91c17SVincenzo Maffione 	uint32_t	nr_rx_slots;	/* slots in rx rings */
6942ff91c17SVincenzo Maffione 	uint16_t	nr_tx_rings;	/* number of tx rings */
6952ff91c17SVincenzo Maffione 	uint16_t	nr_rx_rings;	/* number of rx rings */
6962ff91c17SVincenzo Maffione 	uint16_t	nr_mem_id;	/* id of the memory allocator */
697*b6e66be2SVincenzo Maffione 	uint16_t	pad1;
6982ff91c17SVincenzo Maffione };
6992ff91c17SVincenzo Maffione 
7002ff91c17SVincenzo Maffione /*
7012ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_VALE_POLLING_ENABLE or NETMAP_REQ_VALE_POLLING_DISABLE
7022ff91c17SVincenzo Maffione  * Enable or disable polling kthreads on a VALE port.
7032ff91c17SVincenzo Maffione  */
7042ff91c17SVincenzo Maffione struct nmreq_vale_polling {
7052ff91c17SVincenzo Maffione 	uint32_t	nr_mode;
7062ff91c17SVincenzo Maffione #define NETMAP_POLLING_MODE_SINGLE_CPU 1
7072ff91c17SVincenzo Maffione #define NETMAP_POLLING_MODE_MULTI_CPU 2
7082ff91c17SVincenzo Maffione 	uint32_t	nr_first_cpu_id;
7092ff91c17SVincenzo Maffione 	uint32_t	nr_num_polling_cpus;
710*b6e66be2SVincenzo Maffione 	uint32_t	pad1;
7112ff91c17SVincenzo Maffione };
7122ff91c17SVincenzo Maffione 
7132ff91c17SVincenzo Maffione /*
7142ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_POOLS_INFO_GET
715*b6e66be2SVincenzo Maffione  * Get info about the pools of the memory allocator of the netmap
716*b6e66be2SVincenzo Maffione  * port specified by hdr.nr_name and nr_mem_id. The netmap control
717*b6e66be2SVincenzo Maffione  * device used for this operation does not need to be bound to a netmap
718*b6e66be2SVincenzo Maffione  * port.
7192ff91c17SVincenzo Maffione  */
7202ff91c17SVincenzo Maffione struct nmreq_pools_info {
7212ff91c17SVincenzo Maffione 	uint64_t	nr_memsize;
722*b6e66be2SVincenzo Maffione 	uint16_t	nr_mem_id; /* in/out argument */
723*b6e66be2SVincenzo Maffione 	uint16_t	pad1[3];
7242ff91c17SVincenzo Maffione 	uint64_t	nr_if_pool_offset;
7252ff91c17SVincenzo Maffione 	uint32_t	nr_if_pool_objtotal;
7262ff91c17SVincenzo Maffione 	uint32_t	nr_if_pool_objsize;
7272ff91c17SVincenzo Maffione 	uint64_t	nr_ring_pool_offset;
7282ff91c17SVincenzo Maffione 	uint32_t	nr_ring_pool_objtotal;
7292ff91c17SVincenzo Maffione 	uint32_t	nr_ring_pool_objsize;
7302ff91c17SVincenzo Maffione 	uint64_t	nr_buf_pool_offset;
7312ff91c17SVincenzo Maffione 	uint32_t	nr_buf_pool_objtotal;
7322ff91c17SVincenzo Maffione 	uint32_t	nr_buf_pool_objsize;
7332ff91c17SVincenzo Maffione };
7342ff91c17SVincenzo Maffione 
7352ff91c17SVincenzo Maffione /*
736*b6e66be2SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_SYNC_KLOOP_START
737*b6e66be2SVincenzo Maffione  * Start an in-kernel loop that syncs the rings periodically or on
738*b6e66be2SVincenzo Maffione  * notifications. The loop runs in the context of the ioctl syscall,
739*b6e66be2SVincenzo Maffione  * and only stops on NETMAP_REQ_SYNC_KLOOP_STOP.
740*b6e66be2SVincenzo Maffione  * The registered netmap port must be open in CSB mode.
741*b6e66be2SVincenzo Maffione  */
742*b6e66be2SVincenzo Maffione struct nmreq_sync_kloop_start {
743*b6e66be2SVincenzo Maffione 	/* Sleeping is the default synchronization method for the kloop.
744*b6e66be2SVincenzo Maffione 	 * The 'sleep_us' field specifies how many microsconds to sleep for
745*b6e66be2SVincenzo Maffione 	 * when there is no work to do, before doing another kloop iteration.
746*b6e66be2SVincenzo Maffione 	 */
747*b6e66be2SVincenzo Maffione 	uint32_t	sleep_us;
748*b6e66be2SVincenzo Maffione 	uint32_t	pad1;
749*b6e66be2SVincenzo Maffione };
750*b6e66be2SVincenzo Maffione 
751*b6e66be2SVincenzo Maffione /* A CSB entry for the application --> kernel direction. */
752*b6e66be2SVincenzo Maffione struct nm_csb_atok {
753*b6e66be2SVincenzo Maffione 	uint32_t head;		  /* AW+ KR+ the head of the appl netmap_ring */
754*b6e66be2SVincenzo Maffione 	uint32_t cur;		  /* AW+ KR+ the cur of the appl netmap_ring */
755*b6e66be2SVincenzo Maffione 	uint32_t appl_need_kick;  /* AW+ KR+ kern --> appl notification enable */
756*b6e66be2SVincenzo Maffione 	uint32_t sync_flags;	  /* AW+ KR+ the flags of the appl [tx|rx]sync() */
757*b6e66be2SVincenzo Maffione 	uint32_t pad[12];	  /* pad to a 64 bytes cacheline */
758*b6e66be2SVincenzo Maffione };
759*b6e66be2SVincenzo Maffione 
760*b6e66be2SVincenzo Maffione /* A CSB entry for the application <-- kernel direction. */
761*b6e66be2SVincenzo Maffione struct nm_csb_ktoa {
762*b6e66be2SVincenzo Maffione 	uint32_t hwcur;		  /* AR+ KW+ the hwcur of the kern netmap_kring */
763*b6e66be2SVincenzo Maffione 	uint32_t hwtail;	  /* AR+ KW+ the hwtail of the kern netmap_kring */
764*b6e66be2SVincenzo Maffione 	uint32_t kern_need_kick;  /* AR+ KW+ appl-->kern notification enable */
765*b6e66be2SVincenzo Maffione 	uint32_t pad[13];
766*b6e66be2SVincenzo Maffione };
767*b6e66be2SVincenzo Maffione 
768*b6e66be2SVincenzo Maffione #ifdef __linux__
769*b6e66be2SVincenzo Maffione 
770*b6e66be2SVincenzo Maffione #ifdef __KERNEL__
771*b6e66be2SVincenzo Maffione #define nm_stst_barrier smp_wmb
772*b6e66be2SVincenzo Maffione #else  /* !__KERNEL__ */
773*b6e66be2SVincenzo Maffione static inline void nm_stst_barrier(void)
774*b6e66be2SVincenzo Maffione {
775*b6e66be2SVincenzo Maffione 	/* A memory barrier with release semantic has the combined
776*b6e66be2SVincenzo Maffione 	 * effect of a store-store barrier and a load-store barrier,
777*b6e66be2SVincenzo Maffione 	 * which is fine for us. */
778*b6e66be2SVincenzo Maffione 	__atomic_thread_fence(__ATOMIC_RELEASE);
779*b6e66be2SVincenzo Maffione }
780*b6e66be2SVincenzo Maffione #endif /* !__KERNEL__ */
781*b6e66be2SVincenzo Maffione 
782*b6e66be2SVincenzo Maffione #elif defined(__FreeBSD__)
783*b6e66be2SVincenzo Maffione 
784*b6e66be2SVincenzo Maffione #ifdef _KERNEL
785*b6e66be2SVincenzo Maffione #define nm_stst_barrier	atomic_thread_fence_rel
786*b6e66be2SVincenzo Maffione #else  /* !_KERNEL */
787*b6e66be2SVincenzo Maffione static inline void nm_stst_barrier(void)
788*b6e66be2SVincenzo Maffione {
789*b6e66be2SVincenzo Maffione 	__atomic_thread_fence(__ATOMIC_RELEASE);
790*b6e66be2SVincenzo Maffione }
791*b6e66be2SVincenzo Maffione #endif /* !_KERNEL */
792*b6e66be2SVincenzo Maffione 
793*b6e66be2SVincenzo Maffione #else  /* !__linux__ && !__FreeBSD__ */
794*b6e66be2SVincenzo Maffione #error "OS not supported"
795*b6e66be2SVincenzo Maffione #endif /* !__linux__ && !__FreeBSD__ */
796*b6e66be2SVincenzo Maffione 
797*b6e66be2SVincenzo Maffione /* Application side of sync-kloop: Write ring pointers (cur, head) to the CSB.
798*b6e66be2SVincenzo Maffione  * This routine is coupled with sync_kloop_kernel_read(). */
799*b6e66be2SVincenzo Maffione static inline void
800*b6e66be2SVincenzo Maffione nm_sync_kloop_appl_write(struct nm_csb_atok *atok, uint32_t cur,
801*b6e66be2SVincenzo Maffione 			 uint32_t head)
802*b6e66be2SVincenzo Maffione {
803*b6e66be2SVincenzo Maffione 	/*
804*b6e66be2SVincenzo Maffione 	 * We need to write cur and head to the CSB but we cannot do it atomically.
805*b6e66be2SVincenzo Maffione 	 * There is no way we can prevent the host from reading the updated value
806*b6e66be2SVincenzo Maffione 	 * of one of the two and the old value of the other. However, if we make
807*b6e66be2SVincenzo Maffione 	 * sure that the host never reads a value of head more recent than the
808*b6e66be2SVincenzo Maffione 	 * value of cur we are safe. We can allow the host to read a value of cur
809*b6e66be2SVincenzo Maffione 	 * more recent than the value of head, since in the netmap ring cur can be
810*b6e66be2SVincenzo Maffione 	 * ahead of head and cur cannot wrap around head because it must be behind
811*b6e66be2SVincenzo Maffione 	 * tail. Inverting the order of writes below could instead result into the
812*b6e66be2SVincenzo Maffione 	 * host to think head went ahead of cur, which would cause the sync
813*b6e66be2SVincenzo Maffione 	 * prologue to fail.
814*b6e66be2SVincenzo Maffione 	 *
815*b6e66be2SVincenzo Maffione 	 * The following memory barrier scheme is used to make this happen:
816*b6e66be2SVincenzo Maffione 	 *
817*b6e66be2SVincenzo Maffione 	 *          Guest              Host
818*b6e66be2SVincenzo Maffione 	 *
819*b6e66be2SVincenzo Maffione 	 *          STORE(cur)         LOAD(head)
820*b6e66be2SVincenzo Maffione 	 *          mb() <-----------> mb()
821*b6e66be2SVincenzo Maffione 	 *          STORE(head)        LOAD(cur)
822*b6e66be2SVincenzo Maffione 	 *
823*b6e66be2SVincenzo Maffione 	 */
824*b6e66be2SVincenzo Maffione 	atok->cur = cur;
825*b6e66be2SVincenzo Maffione 	nm_stst_barrier();
826*b6e66be2SVincenzo Maffione 	atok->head = head;
827*b6e66be2SVincenzo Maffione }
828*b6e66be2SVincenzo Maffione 
829*b6e66be2SVincenzo Maffione /* Application side of sync-kloop: Read kring pointers (hwcur, hwtail) from
830*b6e66be2SVincenzo Maffione  * the CSB. This routine is coupled with sync_kloop_kernel_write(). */
831*b6e66be2SVincenzo Maffione static inline void
832*b6e66be2SVincenzo Maffione nm_sync_kloop_appl_read(struct nm_csb_ktoa *ktoa, uint32_t *hwtail,
833*b6e66be2SVincenzo Maffione 			uint32_t *hwcur)
834*b6e66be2SVincenzo Maffione {
835*b6e66be2SVincenzo Maffione 	/*
836*b6e66be2SVincenzo Maffione 	 * We place a memory barrier to make sure that the update of hwtail never
837*b6e66be2SVincenzo Maffione 	 * overtakes the update of hwcur.
838*b6e66be2SVincenzo Maffione 	 * (see explanation in sync_kloop_kernel_write).
839*b6e66be2SVincenzo Maffione 	 */
840*b6e66be2SVincenzo Maffione 	*hwtail = ktoa->hwtail;
841*b6e66be2SVincenzo Maffione 	nm_stst_barrier();
842*b6e66be2SVincenzo Maffione 	*hwcur = ktoa->hwcur;
843*b6e66be2SVincenzo Maffione }
844*b6e66be2SVincenzo Maffione 
845*b6e66be2SVincenzo Maffione /*
8462ff91c17SVincenzo Maffione  * data for NETMAP_REQ_OPT_* options
8472ff91c17SVincenzo Maffione  */
8482ff91c17SVincenzo Maffione 
849*b6e66be2SVincenzo Maffione struct nmreq_opt_sync_kloop_eventfds {
850*b6e66be2SVincenzo Maffione 	struct nmreq_option	nro_opt;	/* common header */
851*b6e66be2SVincenzo Maffione 	/* An array of N entries for bidirectional notifications between
852*b6e66be2SVincenzo Maffione 	 * the kernel loop and the application. The number of entries and
853*b6e66be2SVincenzo Maffione 	 * their order must agree with the CSB arrays passed in the
854*b6e66be2SVincenzo Maffione 	 * NETMAP_REQ_OPT_CSB option. Each entry contains a file descriptor
855*b6e66be2SVincenzo Maffione 	 * backed by an eventfd.
856*b6e66be2SVincenzo Maffione 	 */
857*b6e66be2SVincenzo Maffione 	struct {
858*b6e66be2SVincenzo Maffione 		/* Notifier for the application --> kernel loop direction. */
859*b6e66be2SVincenzo Maffione 		int32_t ioeventfd;
860*b6e66be2SVincenzo Maffione 		/* Notifier for the kernel loop --> application direction. */
861*b6e66be2SVincenzo Maffione 		int32_t irqfd;
862*b6e66be2SVincenzo Maffione 	} eventfds[0];
863*b6e66be2SVincenzo Maffione };
864*b6e66be2SVincenzo Maffione 
8652ff91c17SVincenzo Maffione struct nmreq_opt_extmem {
8662ff91c17SVincenzo Maffione 	struct nmreq_option	nro_opt;	/* common header */
8672ff91c17SVincenzo Maffione 	uint64_t		nro_usrptr;	/* (in) ptr to usr memory */
8682ff91c17SVincenzo Maffione 	struct nmreq_pools_info	nro_info;	/* (in/out) */
8694bf50f18SLuigi Rizzo };
8704bf50f18SLuigi Rizzo 
871*b6e66be2SVincenzo Maffione struct nmreq_opt_csb {
872*b6e66be2SVincenzo Maffione 	struct nmreq_option	nro_opt;
873*b6e66be2SVincenzo Maffione 
874*b6e66be2SVincenzo Maffione 	/* Array of CSB entries for application --> kernel communication
875*b6e66be2SVincenzo Maffione 	 * (N entries). */
876*b6e66be2SVincenzo Maffione 	uint64_t		csb_atok;
877*b6e66be2SVincenzo Maffione 
878*b6e66be2SVincenzo Maffione 	/* Array of CSB entries for kernel --> application communication
879*b6e66be2SVincenzo Maffione 	 * (N entries). */
880*b6e66be2SVincenzo Maffione 	uint64_t		csb_ktoa;
881*b6e66be2SVincenzo Maffione };
882*b6e66be2SVincenzo Maffione 
88368b8534bSLuigi Rizzo #endif /* _NET_NETMAP_H_ */
884