xref: /freebsd/sys/net/netmap.h (revision 2ff63af9b88c7413b7d71715b5532625752a248e)
1fe267a55SPedro F. Giffuni /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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  *
3164ae02c3SLuigi Rizzo  * Definitions of constants and the structures used by the netmap
3264ae02c3SLuigi Rizzo  * framework, for the part visible to both kernel and userspace.
3364ae02c3SLuigi Rizzo  * Detailed info on netmap is available with "man netmap" or at
3464ae02c3SLuigi Rizzo  *
3564ae02c3SLuigi Rizzo  *	http://info.iet.unipi.it/~luigi/netmap/
36ce3ee1e7SLuigi Rizzo  *
37ce3ee1e7SLuigi Rizzo  * This API is also used to communicate with the VALE software switch
3868b8534bSLuigi Rizzo  */
3968b8534bSLuigi Rizzo 
4068b8534bSLuigi Rizzo #ifndef _NET_NETMAP_H_
4168b8534bSLuigi Rizzo #define _NET_NETMAP_H_
4268b8534bSLuigi Rizzo 
43d12354a5SVincenzo Maffione #define	NETMAP_API	14		/* current API version */
4417885a7bSLuigi Rizzo 
45d12354a5SVincenzo Maffione #define	NETMAP_MIN_API	14		/* min and max versions accepted */
46f0ea3689SLuigi Rizzo #define	NETMAP_MAX_API	15
4717885a7bSLuigi Rizzo /*
4817885a7bSLuigi Rizzo  * Some fields should be cache-aligned to reduce contention.
4917885a7bSLuigi Rizzo  * The alignment is architecture and OS dependent, but rather than
5017885a7bSLuigi Rizzo  * digging into OS headers to find the exact value we use an estimate
5117885a7bSLuigi Rizzo  * that should cover most architectures.
5217885a7bSLuigi Rizzo  */
5317885a7bSLuigi Rizzo #define NM_CACHE_ALIGN	128
5417885a7bSLuigi Rizzo 
5568b8534bSLuigi Rizzo /*
5668b8534bSLuigi Rizzo  * --- Netmap data structures ---
5768b8534bSLuigi Rizzo  *
58ce3ee1e7SLuigi Rizzo  * The userspace data structures used by netmap are shown below.
59ce3ee1e7SLuigi Rizzo  * They are allocated by the kernel and mmap()ed by userspace threads.
60ce3ee1e7SLuigi Rizzo  * Pointers are implemented as memory offsets or indexes,
61ce3ee1e7SLuigi Rizzo  * so that they can be easily dereferenced in kernel and userspace.
6268b8534bSLuigi Rizzo 
63ce3ee1e7SLuigi Rizzo    KERNEL (opaque, obviously)
6468b8534bSLuigi Rizzo 
6564ae02c3SLuigi Rizzo   ====================================================================
6664ae02c3SLuigi Rizzo                                           |
67ce3ee1e7SLuigi Rizzo    USERSPACE                              |      struct netmap_ring
6817885a7bSLuigi Rizzo                                           +---->+---------------+
6917885a7bSLuigi Rizzo                                               / | head,cur,tail |
7017885a7bSLuigi Rizzo    struct netmap_if (nifp, 1 per fd)         /  | buf_ofs       |
71d12354a5SVincenzo Maffione     +----------------+                      /   | other fields  |
7217885a7bSLuigi Rizzo     | ni_tx_rings    |                     /    +===============+
73ce3ee1e7SLuigi Rizzo     | ni_rx_rings    |                    /     | buf_idx, len  | slot[0]
74ce3ee1e7SLuigi Rizzo     |                |                   /      | flags, ptr    |
7517885a7bSLuigi Rizzo     |                |                  /       +---------------+
76d12354a5SVincenzo Maffione     +================+                 /        | buf_idx, len  | slot[1]
77ce3ee1e7SLuigi Rizzo     | txring_ofs[0]  | (rel.to nifp)--'         | flags, ptr    |
7817885a7bSLuigi Rizzo     | txring_ofs[1]  |                          +---------------+
79d12354a5SVincenzo Maffione      (tx+htx entries)                           (num_slots entries)
80ce3ee1e7SLuigi Rizzo     | txring_ofs[t]  |                          | buf_idx, len  | slot[n-1]
81d12354a5SVincenzo Maffione     +----------------+                          | flags, ptr    |
8217885a7bSLuigi Rizzo     | rxring_ofs[0]  |                          +---------------+
8368b8534bSLuigi Rizzo     | rxring_ofs[1]  |
84d12354a5SVincenzo Maffione      (rx+hrx entries)
85ce3ee1e7SLuigi Rizzo     | rxring_ofs[r]  |
86d12354a5SVincenzo Maffione     +----------------+
8768b8534bSLuigi Rizzo 
88f0ea3689SLuigi Rizzo  * For each "interface" (NIC, host stack, PIPE, VALE switch port) bound to
89f0ea3689SLuigi Rizzo  * a file descriptor, the mmap()ed region contains a (logically readonly)
90ce3ee1e7SLuigi Rizzo  * struct netmap_if pointing to struct netmap_ring's.
91f0ea3689SLuigi Rizzo  *
92d12354a5SVincenzo Maffione  * There is one netmap_ring per physical NIC ring, plus at least one tx/rx ring
93d12354a5SVincenzo Maffione  * pair attached to the host stack (these pairs are unused for non-NIC ports).
94ce3ee1e7SLuigi Rizzo  *
95ce3ee1e7SLuigi Rizzo  * All physical/host stack ports share the same memory region,
96ce3ee1e7SLuigi Rizzo  * so that zero-copy can be implemented between them.
97ce3ee1e7SLuigi Rizzo  * VALE switch ports instead have separate memory regions.
98ce3ee1e7SLuigi Rizzo  *
99ce3ee1e7SLuigi Rizzo  * The netmap_ring is the userspace-visible replica of the NIC ring.
100ce3ee1e7SLuigi Rizzo  * Each slot has the index of a buffer (MTU-sized and residing in the
101ce3ee1e7SLuigi Rizzo  * mmapped region), its length and some flags. An extra 64-bit pointer
102ce3ee1e7SLuigi Rizzo  * is provided for user-supplied buffers in the tx path.
103ce3ee1e7SLuigi Rizzo  *
10468b8534bSLuigi Rizzo  * In user space, the buffer address is computed as
10564ae02c3SLuigi Rizzo  *	(char *)ring + buf_ofs + index * NETMAP_BUF_SIZE
106f0ea3689SLuigi Rizzo  *
107f0ea3689SLuigi Rizzo  * Added in NETMAP_API 11:
108f0ea3689SLuigi Rizzo  *
109f0ea3689SLuigi Rizzo  * + NIOCREGIF can request the allocation of extra spare buffers from
110f0ea3689SLuigi Rizzo  *   the same memory pool. The desired number of buffers must be in
111f0ea3689SLuigi Rizzo  *   nr_arg3. The ioctl may return fewer buffers, depending on memory
112f0ea3689SLuigi Rizzo  *   availability. nr_arg3 will return the actual value, and, once
113f0ea3689SLuigi Rizzo  *   mapped, nifp->ni_bufs_head will be the index of the first buffer.
114f0ea3689SLuigi Rizzo  *
115f0ea3689SLuigi Rizzo  *   The buffers are linked to each other using the first uint32_t
116f0ea3689SLuigi Rizzo  *   as the index. On close, ni_bufs_head must point to the list of
117f0ea3689SLuigi Rizzo  *   buffers to be released.
118f0ea3689SLuigi Rizzo  *
119f0ea3689SLuigi Rizzo  * + NIOCREGIF can attach to PIPE rings sharing the same memory
120f0ea3689SLuigi Rizzo  *   space with a parent device. The ifname indicates the parent device,
121f0ea3689SLuigi Rizzo  *   which must already exist. Flags in nr_flags indicate if we want to
122f0ea3689SLuigi Rizzo  *   bind the master or slave side, the index (from nr_ringid)
1235c8c1004SLuigi Rizzo  *   is just a cookie and does not need to be sequential.
124f0ea3689SLuigi Rizzo  *
125f0ea3689SLuigi Rizzo  * + NIOCREGIF can also attach to 'monitor' rings that replicate
126f0ea3689SLuigi Rizzo  *   the content of specific rings, also from the same memory space.
127f0ea3689SLuigi Rizzo  *
128f0ea3689SLuigi Rizzo  *   Extra flags in nr_flags support the above functions.
129f0ea3689SLuigi Rizzo  *   Application libraries may use the following naming scheme:
130d12354a5SVincenzo Maffione  *	netmap:foo			all NIC rings pairs
131d12354a5SVincenzo Maffione  *	netmap:foo^			only host rings pairs
132d12354a5SVincenzo Maffione  *	netmap:foo^k			the k-th host rings pair
133d12354a5SVincenzo Maffione  *	netmap:foo+			all NIC rings + host rings pairs
134d12354a5SVincenzo Maffione  *	netmap:foo-k			the k-th NIC rings pair
135d12354a5SVincenzo Maffione  *	netmap:foo{k			PIPE rings pair k, master side
136d12354a5SVincenzo Maffione  *	netmap:foo}k			PIPE rings pair k, slave side
13737e3a6d3SLuigi Rizzo  *
13837e3a6d3SLuigi Rizzo  * Some notes about host rings:
13937e3a6d3SLuigi Rizzo  *
140d12354a5SVincenzo Maffione  * + The RX host rings are used to store those packets that the host network
14137e3a6d3SLuigi Rizzo  *   stack is trying to transmit through a NIC queue, but only if that queue
14237e3a6d3SLuigi Rizzo  *   is currently in netmap mode. Netmap will not intercept host stack mbufs
14337e3a6d3SLuigi Rizzo  *   designated to NIC queues that are not in netmap mode. As a consequence,
14437e3a6d3SLuigi Rizzo  *   registering a netmap port with netmap:foo^ is not enough to intercept
145d12354a5SVincenzo Maffione  *   mbufs in the RX host rings; the netmap port should be registered with
14637e3a6d3SLuigi Rizzo  *   netmap:foo*, or another registration should be done to open at least a
14737e3a6d3SLuigi Rizzo  *   NIC TX queue in netmap mode.
14837e3a6d3SLuigi Rizzo  *
14945c67e8fSVincenzo Maffione  * + Netmap is not currently able to deal with intercepted transmit mbufs which
15037e3a6d3SLuigi Rizzo  *   require offloadings like TSO, UFO, checksumming offloadings, etc. It is
15137e3a6d3SLuigi Rizzo  *   responsibility of the user to disable those offloadings (e.g. using
15237e3a6d3SLuigi Rizzo  *   ifconfig on FreeBSD or ethtool -K on Linux) for an interface that is being
15337e3a6d3SLuigi Rizzo  *   used in netmap mode. If the offloadings are not disabled, GSO and/or
15437e3a6d3SLuigi Rizzo  *   unchecksummed packets may be dropped immediately or end up in the host RX
155d12354a5SVincenzo Maffione  *   rings, and will be dropped as soon as the packet reaches another netmap
15637e3a6d3SLuigi Rizzo  *   adapter.
157ce3ee1e7SLuigi Rizzo  */
158ce3ee1e7SLuigi Rizzo 
159ce3ee1e7SLuigi Rizzo /*
160ce3ee1e7SLuigi Rizzo  * struct netmap_slot is a buffer descriptor
16117885a7bSLuigi Rizzo  */
16217885a7bSLuigi Rizzo struct netmap_slot {
16317885a7bSLuigi Rizzo 	uint32_t buf_idx;	/* buffer index */
16417885a7bSLuigi Rizzo 	uint16_t len;		/* length for this slot */
16517885a7bSLuigi Rizzo 	uint16_t flags;		/* buf changed, etc. */
16617885a7bSLuigi Rizzo 	uint64_t ptr;		/* pointer for indirect buffers */
16717885a7bSLuigi Rizzo };
16817885a7bSLuigi Rizzo 
16917885a7bSLuigi Rizzo /*
17017885a7bSLuigi Rizzo  * The following flags control how the slot is used
17117885a7bSLuigi Rizzo  */
17217885a7bSLuigi Rizzo 
17317885a7bSLuigi Rizzo #define	NS_BUF_CHANGED	0x0001	/* buf_idx changed */
17417885a7bSLuigi Rizzo 	/*
17517885a7bSLuigi Rizzo 	 * must be set whenever buf_idx is changed (as it might be
17617885a7bSLuigi Rizzo 	 * necessary to recompute the physical address and mapping)
177847bf383SLuigi Rizzo 	 *
178847bf383SLuigi Rizzo 	 * It is also set by the kernel whenever the buf_idx is
179847bf383SLuigi Rizzo 	 * changed internally (e.g., by pipes). Applications may
180847bf383SLuigi Rizzo 	 * use this information to know when they can reuse the
181847bf383SLuigi Rizzo 	 * contents of previously prepared buffers.
18217885a7bSLuigi Rizzo 	 */
18317885a7bSLuigi Rizzo 
18417885a7bSLuigi Rizzo #define	NS_REPORT	0x0002	/* ask the hardware to report results */
18517885a7bSLuigi Rizzo 	/*
18617885a7bSLuigi Rizzo 	 * Request notification when slot is used by the hardware.
18717885a7bSLuigi Rizzo 	 * Normally transmit completions are handled lazily and
18817885a7bSLuigi Rizzo 	 * may be unreported. This flag lets us know when a slot
18917885a7bSLuigi Rizzo 	 * has been sent (e.g. to terminate the sender).
19017885a7bSLuigi Rizzo 	 */
19117885a7bSLuigi Rizzo 
19217885a7bSLuigi Rizzo #define	NS_FORWARD	0x0004	/* pass packet 'forward' */
19317885a7bSLuigi Rizzo 	/*
19417885a7bSLuigi Rizzo 	 * (Only for physical ports, rx rings with NR_FORWARD set).
19517885a7bSLuigi Rizzo 	 * Slot released to the kernel (i.e. before ring->head) with
19617885a7bSLuigi Rizzo 	 * this flag set are passed to the peer ring (host/NIC),
19717885a7bSLuigi Rizzo 	 * thus restoring the host-NIC connection for these slots.
19817885a7bSLuigi Rizzo 	 * This supports efficient traffic monitoring or firewalling.
19917885a7bSLuigi Rizzo 	 */
20017885a7bSLuigi Rizzo 
20117885a7bSLuigi Rizzo #define	NS_NO_LEARN	0x0008	/* disable bridge learning */
20217885a7bSLuigi Rizzo  	/*
20317885a7bSLuigi Rizzo 	 * On a VALE switch, do not 'learn' the source port for
20417885a7bSLuigi Rizzo  	 * this buffer.
20517885a7bSLuigi Rizzo 	 */
20617885a7bSLuigi Rizzo 
20717885a7bSLuigi Rizzo #define	NS_INDIRECT	0x0010	/* userspace buffer */
20817885a7bSLuigi Rizzo  	/*
20917885a7bSLuigi Rizzo 	 * (VALE tx rings only) data is in a userspace buffer,
21017885a7bSLuigi Rizzo 	 * whose address is in the 'ptr' field in the slot.
21117885a7bSLuigi Rizzo 	 */
21217885a7bSLuigi Rizzo 
21317885a7bSLuigi Rizzo #define	NS_MOREFRAG	0x0020	/* packet has more fragments */
21417885a7bSLuigi Rizzo  	/*
2154f80b14cSVincenzo Maffione 	 * (VALE ports, ptnetmap ports and some NIC ports, e.g.
2164f80b14cSVincenzo Maffione          * ixgbe and i40e on Linux)
21717885a7bSLuigi Rizzo 	 * Set on all but the last slot of a multi-segment packet.
21817885a7bSLuigi Rizzo 	 * The 'len' field refers to the individual fragment.
21917885a7bSLuigi Rizzo 	 */
22017885a7bSLuigi Rizzo 
221660a47cbSVincenzo Maffione #define NS_TXMON	0x0040
222660a47cbSVincenzo Maffione 	/* (monitor ports only) the packet comes from the TX
223660a47cbSVincenzo Maffione 	 * ring of the monitored port
224660a47cbSVincenzo Maffione 	 */
225660a47cbSVincenzo Maffione 
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 
242a6d768d8SVincenzo Maffione 
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 
304a6d768d8SVincenzo Maffione 	/* offset_mask is used to isolate the part of the ptr field
305a6d768d8SVincenzo Maffione 	 * in the slots used to contain an offset in the buffer.
306a6d768d8SVincenzo Maffione 	 * It is zero if the ring has not be opened using the
307a6d768d8SVincenzo Maffione 	 * NETMAP_REQ_OPT_OFFSETS option.
308a6d768d8SVincenzo Maffione 	 */
309a6d768d8SVincenzo Maffione 	const uint64_t	offset_mask;
310a6d768d8SVincenzo Maffione 	/* the alignment requirement, in bytes, for the start
311a6d768d8SVincenzo Maffione 	 * of the packets inside the buffers.
312a6d768d8SVincenzo Maffione 	 * User programs should take this alignment into
31345c67e8fSVincenzo Maffione 	 * account when specifying buffer-offsets in TX slots.
314a6d768d8SVincenzo Maffione 	 */
315a6d768d8SVincenzo Maffione 	const uint64_t	buf_align;
316a6d768d8SVincenzo Maffione 
31717885a7bSLuigi Rizzo 	/* opaque room for a mutex or similar object */
31837e3a6d3SLuigi Rizzo #if !defined(_WIN32) || defined(__CYGWIN__)
31937e3a6d3SLuigi Rizzo 	uint8_t	__attribute__((__aligned__(NM_CACHE_ALIGN))) sem[128];
32037e3a6d3SLuigi Rizzo #else
32137e3a6d3SLuigi Rizzo 	uint8_t	__declspec(align(NM_CACHE_ALIGN)) sem[128];
32237e3a6d3SLuigi Rizzo #endif
32368b8534bSLuigi Rizzo 
32468b8534bSLuigi Rizzo 	/* the slots follow. This struct has variable size */
32568b8534bSLuigi Rizzo 	struct netmap_slot slot[0];	/* array of slots. */
32668b8534bSLuigi Rizzo };
32768b8534bSLuigi Rizzo 
328a6d768d8SVincenzo Maffione 
32968b8534bSLuigi Rizzo /*
33017885a7bSLuigi Rizzo  * RING FLAGS
33117885a7bSLuigi Rizzo  */
33217885a7bSLuigi Rizzo #define	NR_TIMESTAMP	0x0002		/* set timestamp on *sync() */
33317885a7bSLuigi Rizzo 	/*
33417885a7bSLuigi Rizzo 	 * updates the 'ts' field on each netmap syscall. This saves
33517885a7bSLuigi Rizzo 	 * saves a separate gettimeofday(), and is not much worse than
33617885a7bSLuigi Rizzo 	 * software timestamps generated in the interrupt handler.
33717885a7bSLuigi Rizzo 	 */
33817885a7bSLuigi Rizzo 
33917885a7bSLuigi Rizzo #define	NR_FORWARD	0x0004		/* enable NS_FORWARD for ring */
34017885a7bSLuigi Rizzo  	/*
34117885a7bSLuigi Rizzo 	 * Enables the NS_FORWARD slot flag for the ring.
34217885a7bSLuigi Rizzo 	 */
34317885a7bSLuigi Rizzo 
3442ff91c17SVincenzo Maffione /*
3452ff91c17SVincenzo Maffione  * Helper functions for kernel and userspace
3462ff91c17SVincenzo Maffione  */
3472ff91c17SVincenzo Maffione 
3482ff91c17SVincenzo Maffione /*
349b6e66be2SVincenzo Maffione  * Check if space is available in the ring. We use ring->head, which
350b6e66be2SVincenzo Maffione  * points to the next netmap slot to be published to netmap. It is
351b6e66be2SVincenzo Maffione  * possible that the applications moves ring->cur ahead of ring->tail
352b6e66be2SVincenzo Maffione  * (e.g., by setting ring->cur <== ring->tail), if it wants more slots
353b6e66be2SVincenzo Maffione  * than the ones currently available, and it wants to be notified when
354b6e66be2SVincenzo Maffione  * more arrive. See netmap(4) for more details and examples.
3552ff91c17SVincenzo Maffione  */
3562ff91c17SVincenzo Maffione static inline int
nm_ring_empty(struct netmap_ring * ring)3572ff91c17SVincenzo Maffione nm_ring_empty(struct netmap_ring *ring)
3582ff91c17SVincenzo Maffione {
359b6e66be2SVincenzo Maffione 	return (ring->head == ring->tail);
3602ff91c17SVincenzo Maffione }
36117885a7bSLuigi Rizzo 
36217885a7bSLuigi Rizzo /*
36368b8534bSLuigi Rizzo  * Netmap representation of an interface and its queue(s).
364ce3ee1e7SLuigi Rizzo  * This is initialized by the kernel when binding a file
365ce3ee1e7SLuigi Rizzo  * descriptor to a port, and should be considered as readonly
366ce3ee1e7SLuigi Rizzo  * by user programs. The kernel never uses it.
367ce3ee1e7SLuigi Rizzo  *
36868b8534bSLuigi Rizzo  * There is one netmap_if for each file descriptor on which we want
369ce3ee1e7SLuigi Rizzo  * to select/poll.
37068b8534bSLuigi Rizzo  * select/poll operates on one or all pairs depending on the value of
37168b8534bSLuigi Rizzo  * nmr_queueid passed on the ioctl.
37268b8534bSLuigi Rizzo  */
37368b8534bSLuigi Rizzo struct netmap_if {
37468b8534bSLuigi Rizzo 	char		ni_name[IFNAMSIZ]; /* name of the interface. */
375ce3ee1e7SLuigi Rizzo 	const uint32_t	ni_version;	/* API version, currently unused */
376ce3ee1e7SLuigi Rizzo 	const uint32_t	ni_flags;	/* properties */
377ce3ee1e7SLuigi Rizzo #define	NI_PRIV_MEM	0x1		/* private memory region */
378ce3ee1e7SLuigi Rizzo 
37917885a7bSLuigi Rizzo 	/*
38017885a7bSLuigi Rizzo 	 * The number of packet rings available in netmap mode.
38117885a7bSLuigi Rizzo 	 * Physical NICs can have different numbers of tx and rx rings.
382d12354a5SVincenzo Maffione 	 * Physical NICs also have at least a 'host' rings pair.
38317885a7bSLuigi Rizzo 	 * Additionally, clients can request additional ring pairs to
38417885a7bSLuigi Rizzo 	 * be used for internal communication.
38517885a7bSLuigi Rizzo 	 */
38617885a7bSLuigi Rizzo 	const uint32_t	ni_tx_rings;	/* number of HW tx rings */
38717885a7bSLuigi Rizzo 	const uint32_t	ni_rx_rings;	/* number of HW rx rings */
38817885a7bSLuigi Rizzo 
389f0ea3689SLuigi Rizzo 	uint32_t	ni_bufs_head;	/* head index for extra bufs */
390d12354a5SVincenzo Maffione 	const uint32_t	ni_host_tx_rings; /* number of SW tx rings */
391d12354a5SVincenzo Maffione 	const uint32_t	ni_host_rx_rings; /* number of SW rx rings */
392d12354a5SVincenzo Maffione 	uint32_t	ni_spare1[3];
39368b8534bSLuigi Rizzo 	/*
39464ae02c3SLuigi Rizzo 	 * The following array contains the offset of each netmap ring
39517885a7bSLuigi Rizzo 	 * from this structure, in the following order:
396d12354a5SVincenzo Maffione 	 *     - NIC tx rings (ni_tx_rings);
397d12354a5SVincenzo Maffione 	 *     - host tx rings (ni_host_tx_rings);
398d12354a5SVincenzo Maffione 	 *     - NIC rx rings (ni_rx_rings);
399d12354a5SVincenzo Maffione 	 *     - host rx ring (ni_host_rx_rings);
40017885a7bSLuigi Rizzo 	 *
401d12354a5SVincenzo Maffione 	 * The area is filled up by the kernel on NETMAP_REQ_REGISTER,
40268b8534bSLuigi Rizzo 	 * and then only read by userspace code.
40368b8534bSLuigi Rizzo 	 */
40468b8534bSLuigi Rizzo 	const ssize_t	ring_ofs[0];
40568b8534bSLuigi Rizzo };
40668b8534bSLuigi Rizzo 
4072ff91c17SVincenzo Maffione /* Legacy interface to interact with a netmap control device.
4082ff91c17SVincenzo Maffione  * Included for backward compatibility. The user should not include this
4092ff91c17SVincenzo Maffione  * file directly. */
4102ff91c17SVincenzo Maffione #include "netmap_legacy.h"
41117885a7bSLuigi Rizzo 
41268b8534bSLuigi Rizzo /*
4132ff91c17SVincenzo Maffione  * New API to control netmap control devices. New applications should only use
4142ff91c17SVincenzo Maffione  * nmreq_xyz structs with the NIOCCTRL ioctl() command.
41568b8534bSLuigi Rizzo  *
4162ff91c17SVincenzo Maffione  * NIOCCTRL takes a nmreq_header struct, which contains the required
4172ff91c17SVincenzo Maffione  * API version, the name of a netmap port, a command type, and pointers
4182ff91c17SVincenzo Maffione  * to request body and options.
419f18be576SLuigi Rizzo  *
42017885a7bSLuigi Rizzo  *	nr_name	(in)
4212ff91c17SVincenzo Maffione  *		The name of the port (em0, valeXXX:YYY, eth0{pn1 etc.)
422ce3ee1e7SLuigi Rizzo  *
42317885a7bSLuigi Rizzo  *	nr_version (in/out)
42417885a7bSLuigi Rizzo  *		Must match NETMAP_API as used in the kernel, error otherwise.
42517885a7bSLuigi Rizzo  *		Always returns the desired value on output.
42617885a7bSLuigi Rizzo  *
4272ff91c17SVincenzo Maffione  *	nr_reqtype (in)
4282ff91c17SVincenzo Maffione  *		One of the NETMAP_REQ_* command types below
4292ff91c17SVincenzo Maffione  *
4302ff91c17SVincenzo Maffione  *	nr_body (in)
4312ff91c17SVincenzo Maffione  *		Pointer to a command-specific struct, described by one
4322ff91c17SVincenzo Maffione  *		of the struct nmreq_xyz below.
4332ff91c17SVincenzo Maffione  *
4342ff91c17SVincenzo Maffione  *	nr_options (in)
4352ff91c17SVincenzo Maffione  *		Command specific options, if any.
4362ff91c17SVincenzo Maffione  *
4372ff91c17SVincenzo Maffione  * A NETMAP_REQ_REGISTER command activates netmap mode on the netmap
4382ff91c17SVincenzo Maffione  * port (e.g. physical interface) specified by nmreq_header.nr_name.
4392ff91c17SVincenzo Maffione  * The request body (struct nmreq_register) has several arguments to
4402ff91c17SVincenzo Maffione  * specify how the port is to be registered.
4412ff91c17SVincenzo Maffione  *
442d12354a5SVincenzo Maffione  *	nr_tx_slots, nr_tx_slots, nr_tx_rings, nr_rx_rings,
443d12354a5SVincenzo Maffione  *	nr_host_tx_rings, nr_host_rx_rings (in/out)
44417885a7bSLuigi Rizzo  *		On input, non-zero values may be used to reconfigure the port
44517885a7bSLuigi Rizzo  *		according to the requested values, but this is not guaranteed.
44617885a7bSLuigi Rizzo  *		On output the actual values in use are reported.
44717885a7bSLuigi Rizzo  *
4482ff91c17SVincenzo Maffione  *	nr_mode (in)
4492ff91c17SVincenzo Maffione  *		Indicate what set of rings must be bound to the netmap
4502ff91c17SVincenzo Maffione  *		device (e.g. all NIC rings, host rings only, NIC and
4512ff91c17SVincenzo Maffione  *		host rings, ...). Values are in NR_REG_*.
4522ff91c17SVincenzo Maffione  *
45317885a7bSLuigi Rizzo  *	nr_ringid (in)
4542ff91c17SVincenzo Maffione  *		If nr_mode == NR_REG_ONE_NIC (only a single couple of TX/RX
4552ff91c17SVincenzo Maffione  *		rings), indicate which NIC TX and/or RX ring is to be bound
4562ff91c17SVincenzo Maffione  *		(0..nr_*x_rings-1).
457f0ea3689SLuigi Rizzo  *
4582ff91c17SVincenzo Maffione  *	nr_flags (in)
4592ff91c17SVincenzo Maffione  *		Indicate special options for how to open the port.
460ce3ee1e7SLuigi Rizzo  *
4612ff91c17SVincenzo Maffione  *		NR_NO_TX_POLL can be OR-ed to make select()/poll() push
46217885a7bSLuigi Rizzo  *			packets on tx rings only if POLLOUT is set.
46317885a7bSLuigi Rizzo  *			The default is to push any pending packet.
464ce3ee1e7SLuigi Rizzo  *
4652ff91c17SVincenzo Maffione  *		NR_DO_RX_POLL can be OR-ed to make select()/poll() release
466f0ea3689SLuigi Rizzo  *			packets on rx rings also when POLLIN is NOT set.
467f0ea3689SLuigi Rizzo  *			The default is to touch the rx ring only with POLLIN.
468f0ea3689SLuigi Rizzo  *			Note that this is the opposite of TX because it
469f0ea3689SLuigi Rizzo  *			reflects the common usage.
470f0ea3689SLuigi Rizzo  *
4712ff91c17SVincenzo Maffione  *		Other options are NR_MONITOR_TX, NR_MONITOR_RX, NR_ZCOPY_MON,
4722ff91c17SVincenzo Maffione  *		NR_EXCLUSIVE, NR_RX_RINGS_ONLY, NR_TX_RINGS_ONLY and
4732ff91c17SVincenzo Maffione  *		NR_ACCEPT_VNET_HDR.
474f0ea3689SLuigi Rizzo  *
4752ff91c17SVincenzo Maffione  *	nr_mem_id (in/out)
4762ff91c17SVincenzo Maffione  *		The identity of the memory region used.
477f0ea3689SLuigi Rizzo  *		On input, 0 means the system decides autonomously,
478f0ea3689SLuigi Rizzo  *		other values may try to select a specific region.
479f0ea3689SLuigi Rizzo  *		On return the actual value is reported.
480f0ea3689SLuigi Rizzo  *		Region '1' is the global allocator, normally shared
481f0ea3689SLuigi Rizzo  *		by all interfaces. Other values are private regions.
482f0ea3689SLuigi Rizzo  *		If two ports the same region zero-copy is possible.
483f0ea3689SLuigi Rizzo  *
4842ff91c17SVincenzo Maffione  *	nr_extra_bufs (in/out)
4852ff91c17SVincenzo Maffione  *		Number of extra buffers to be allocated.
486f0ea3689SLuigi Rizzo  *
4872ff91c17SVincenzo Maffione  * The other NETMAP_REQ_* commands are described below.
48817885a7bSLuigi Rizzo  *
48968b8534bSLuigi Rizzo  */
49068b8534bSLuigi Rizzo 
4912ff91c17SVincenzo Maffione /* maximum size of a request, including all options */
4922ff91c17SVincenzo Maffione #define NETMAP_REQ_MAXSIZE	4096
4932ff91c17SVincenzo Maffione 
4942ff91c17SVincenzo Maffione /* Header common to all request options. */
4952ff91c17SVincenzo Maffione struct nmreq_option {
49645c67e8fSVincenzo Maffione 	/* Pointer to the next option. */
4972ff91c17SVincenzo Maffione 	uint64_t		nro_next;
4982ff91c17SVincenzo Maffione 	/* Option type. */
4992ff91c17SVincenzo Maffione 	uint32_t		nro_reqtype;
5002ff91c17SVincenzo Maffione 	/* (out) status of the option:
5012ff91c17SVincenzo Maffione 	 * 0: recognized and processed
5022ff91c17SVincenzo Maffione 	 * !=0: errno value
5032ff91c17SVincenzo Maffione 	 */
5042ff91c17SVincenzo Maffione 	uint32_t		nro_status;
505b6e66be2SVincenzo Maffione 	/* Option size, used only for options that can have variable size
506b6e66be2SVincenzo Maffione 	 * (e.g. because they contain arrays). For fixed-size options this
507b6e66be2SVincenzo Maffione 	 * field should be set to zero. */
508b6e66be2SVincenzo Maffione 	uint64_t		nro_size;
5092ff91c17SVincenzo Maffione };
5102ff91c17SVincenzo Maffione 
5112ff91c17SVincenzo Maffione /* Header common to all requests. Do not reorder these fields, as we need
5122ff91c17SVincenzo Maffione  * the second one (nr_reqtype) to know how much to copy from/to userspace. */
5132ff91c17SVincenzo Maffione struct nmreq_header {
5142ff91c17SVincenzo Maffione 	uint16_t		nr_version;	/* API version */
5152ff91c17SVincenzo Maffione 	uint16_t		nr_reqtype;	/* nmreq type (NETMAP_REQ_*) */
5162ff91c17SVincenzo Maffione 	uint32_t		nr_reserved;	/* must be zero */
5172ff91c17SVincenzo Maffione #define NETMAP_REQ_IFNAMSIZ	64
5182ff91c17SVincenzo Maffione 	char			nr_name[NETMAP_REQ_IFNAMSIZ]; /* port name */
5192ff91c17SVincenzo Maffione 	uint64_t		nr_options;	/* command-specific options */
5202ff91c17SVincenzo Maffione 	uint64_t		nr_body;	/* ptr to nmreq_xyz struct */
5212ff91c17SVincenzo Maffione };
5222ff91c17SVincenzo Maffione 
5232ff91c17SVincenzo Maffione enum {
5242ff91c17SVincenzo Maffione 	/* Register a netmap port with the device. */
5252ff91c17SVincenzo Maffione 	NETMAP_REQ_REGISTER = 1,
5262ff91c17SVincenzo Maffione 	/* Get information from a netmap port. */
5272ff91c17SVincenzo Maffione 	NETMAP_REQ_PORT_INFO_GET,
5282ff91c17SVincenzo Maffione 	/* Attach a netmap port to a VALE switch. */
5292ff91c17SVincenzo Maffione 	NETMAP_REQ_VALE_ATTACH,
5302ff91c17SVincenzo Maffione 	/* Detach a netmap port from a VALE switch. */
5312ff91c17SVincenzo Maffione 	NETMAP_REQ_VALE_DETACH,
5322ff91c17SVincenzo Maffione 	/* List the ports attached to a VALE switch. */
5332ff91c17SVincenzo Maffione 	NETMAP_REQ_VALE_LIST,
5342ff91c17SVincenzo Maffione 	/* Set the port header length (was virtio-net header length). */
5352ff91c17SVincenzo Maffione 	NETMAP_REQ_PORT_HDR_SET,
5362ff91c17SVincenzo Maffione 	/* Get the port header length (was virtio-net header length). */
5372ff91c17SVincenzo Maffione 	NETMAP_REQ_PORT_HDR_GET,
5382ff91c17SVincenzo Maffione 	/* Create a new persistent VALE port. */
5392ff91c17SVincenzo Maffione 	NETMAP_REQ_VALE_NEWIF,
5402ff91c17SVincenzo Maffione 	/* Delete a persistent VALE port. */
5412ff91c17SVincenzo Maffione 	NETMAP_REQ_VALE_DELIF,
5422ff91c17SVincenzo Maffione 	/* Enable polling kernel thread(s) on an attached VALE port. */
5432ff91c17SVincenzo Maffione 	NETMAP_REQ_VALE_POLLING_ENABLE,
5442ff91c17SVincenzo Maffione 	/* Disable polling kernel thread(s) on an attached VALE port. */
5452ff91c17SVincenzo Maffione 	NETMAP_REQ_VALE_POLLING_DISABLE,
5462ff91c17SVincenzo Maffione 	/* Get info about the pools of a memory allocator. */
5472ff91c17SVincenzo Maffione 	NETMAP_REQ_POOLS_INFO_GET,
548b6e66be2SVincenzo Maffione 	/* Start an in-kernel loop that syncs the rings periodically or
549b6e66be2SVincenzo Maffione 	 * on notifications. The loop runs in the context of the ioctl
550b6e66be2SVincenzo Maffione 	 * syscall, and only stops on NETMAP_REQ_SYNC_KLOOP_STOP. */
551b6e66be2SVincenzo Maffione 	NETMAP_REQ_SYNC_KLOOP_START,
552b6e66be2SVincenzo Maffione 	/* Stops the thread executing the in-kernel loop. The thread
553b6e66be2SVincenzo Maffione 	 * returns from the ioctl syscall. */
554b6e66be2SVincenzo Maffione 	NETMAP_REQ_SYNC_KLOOP_STOP,
555b6e66be2SVincenzo Maffione 	/* Enable CSB mode on a registered netmap control device. */
556b6e66be2SVincenzo Maffione 	NETMAP_REQ_CSB_ENABLE,
5572ff91c17SVincenzo Maffione };
5582ff91c17SVincenzo Maffione 
5592ff91c17SVincenzo Maffione enum {
5602ff91c17SVincenzo Maffione 	/* On NETMAP_REQ_REGISTER, ask netmap to use memory allocated
5615faab778SVincenzo Maffione 	 * from user-space allocated memory pools (e.g. hugepages).
5625faab778SVincenzo Maffione 	 */
5632ff91c17SVincenzo Maffione 	NETMAP_REQ_OPT_EXTMEM = 1,
564b6e66be2SVincenzo Maffione 
565b6e66be2SVincenzo Maffione 	/* ON NETMAP_REQ_SYNC_KLOOP_START, ask netmap to use eventfd-based
566b6e66be2SVincenzo Maffione 	 * notifications to synchronize the kernel loop with the application.
567b6e66be2SVincenzo Maffione 	 */
568b6e66be2SVincenzo Maffione 	NETMAP_REQ_OPT_SYNC_KLOOP_EVENTFDS,
569b6e66be2SVincenzo Maffione 
570b6e66be2SVincenzo Maffione 	/* On NETMAP_REQ_REGISTER, ask netmap to work in CSB mode, where
571b6e66be2SVincenzo Maffione 	 * head, cur and tail pointers are not exchanged through the
572b6e66be2SVincenzo Maffione 	 * struct netmap_ring header, but rather using an user-provided
5735faab778SVincenzo Maffione 	 * memory area (see struct nm_csb_atok and struct nm_csb_ktoa).
5745faab778SVincenzo Maffione 	 */
575b6e66be2SVincenzo Maffione 	NETMAP_REQ_OPT_CSB,
5765faab778SVincenzo Maffione 
5775faab778SVincenzo Maffione 	/* An extension to NETMAP_REQ_OPT_SYNC_KLOOP_EVENTFDS, which specifies
5785faab778SVincenzo Maffione 	 * if the TX and/or RX rings are synced in the context of the VM exit.
5795faab778SVincenzo Maffione 	 * This requires the 'ioeventfd' fields to be valid (cannot be < 0).
5805faab778SVincenzo Maffione 	 */
5815faab778SVincenzo Maffione 	NETMAP_REQ_OPT_SYNC_KLOOP_MODE,
582253b2ec1SVincenzo Maffione 
583a6d768d8SVincenzo Maffione 	/* On NETMAP_REQ_REGISTER, ask for (part of) the ptr field in the
584a6d768d8SVincenzo Maffione 	 * slots of the registered rings to be used as an offset field
585a6d768d8SVincenzo Maffione 	 * for the start of the packets inside the netmap buffer.
586a6d768d8SVincenzo Maffione 	 */
587a6d768d8SVincenzo Maffione 	NETMAP_REQ_OPT_OFFSETS,
588a6d768d8SVincenzo Maffione 
589253b2ec1SVincenzo Maffione 	/* This is a marker to count the number of available options.
590253b2ec1SVincenzo Maffione 	 * New options must be added above it. */
591253b2ec1SVincenzo Maffione 	NETMAP_REQ_OPT_MAX,
5922ff91c17SVincenzo Maffione };
59317885a7bSLuigi Rizzo 
59468b8534bSLuigi Rizzo /*
5952ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_REGISTER
5962ff91c17SVincenzo Maffione  * Bind (register) a netmap port to this control device.
59768b8534bSLuigi Rizzo  */
5982ff91c17SVincenzo Maffione struct nmreq_register {
5992ff91c17SVincenzo Maffione 	uint64_t	nr_offset;	/* nifp offset in the shared region */
6002ff91c17SVincenzo Maffione 	uint64_t	nr_memsize;	/* size of the shared region */
60164ae02c3SLuigi Rizzo 	uint32_t	nr_tx_slots;	/* slots in tx rings */
60264ae02c3SLuigi Rizzo 	uint32_t	nr_rx_slots;	/* slots in rx rings */
60364ae02c3SLuigi Rizzo 	uint16_t	nr_tx_rings;	/* number of tx rings */
60464ae02c3SLuigi Rizzo 	uint16_t	nr_rx_rings;	/* number of rx rings */
605d12354a5SVincenzo Maffione 	uint16_t	nr_host_tx_rings; /* number of host tx rings */
606d12354a5SVincenzo Maffione 	uint16_t	nr_host_rx_rings; /* number of host rx rings */
607f0ea3689SLuigi Rizzo 
6082ff91c17SVincenzo Maffione 	uint16_t	nr_mem_id;	/* id of the memory allocator */
60968b8534bSLuigi Rizzo 	uint16_t	nr_ringid;	/* ring(s) we care about */
6102ff91c17SVincenzo Maffione 	uint32_t	nr_mode;	/* specify NR_REG_* modes */
611b6e66be2SVincenzo Maffione 	uint32_t	nr_extra_bufs;	/* number of requested extra buffers */
612f0ea3689SLuigi Rizzo 
6132ff91c17SVincenzo Maffione 	uint64_t	nr_flags;	/* additional flags (see below) */
6142ff91c17SVincenzo Maffione /* monitors use nr_ringid and nr_mode to select the rings to monitor */
615f0ea3689SLuigi Rizzo #define NR_MONITOR_TX	0x100
616f0ea3689SLuigi Rizzo #define NR_MONITOR_RX	0x200
617847bf383SLuigi Rizzo #define NR_ZCOPY_MON	0x400
618847bf383SLuigi Rizzo /* request exclusive access to the selected rings */
619847bf383SLuigi Rizzo #define NR_EXCLUSIVE	0x800
620b6e66be2SVincenzo Maffione /* 0x1000 unused */
62137e3a6d3SLuigi Rizzo #define NR_RX_RINGS_ONLY	0x2000
62237e3a6d3SLuigi Rizzo #define NR_TX_RINGS_ONLY	0x4000
62337e3a6d3SLuigi Rizzo /* Applications set this flag if they are able to deal with virtio-net headers,
62437e3a6d3SLuigi Rizzo  * that is send/receive frames that start with a virtio-net header.
625d12354a5SVincenzo Maffione  * If not set, NETMAP_REQ_REGISTER will fail with netmap ports that require
626d12354a5SVincenzo Maffione  * applications to use those headers. If the flag is set, the application can
627d12354a5SVincenzo Maffione  * use the NETMAP_VNET_HDR_GET command to figure out the header length. */
62837e3a6d3SLuigi Rizzo #define NR_ACCEPT_VNET_HDR	0x8000
6292ff91c17SVincenzo Maffione /* The following two have the same meaning of NETMAP_NO_TX_POLL and
6302ff91c17SVincenzo Maffione  * NETMAP_DO_RX_POLL. */
6312ff91c17SVincenzo Maffione #define NR_DO_RX_POLL		0x10000
6322ff91c17SVincenzo Maffione #define NR_NO_TX_POLL		0x20000
6332ff91c17SVincenzo Maffione };
6342ff91c17SVincenzo Maffione 
6352ff91c17SVincenzo Maffione /* Valid values for nmreq_register.nr_mode (see above). */
6362ff91c17SVincenzo Maffione enum {	NR_REG_DEFAULT	= 0,	/* backward compat, should not be used. */
6372ff91c17SVincenzo Maffione 	NR_REG_ALL_NIC	= 1,
6382ff91c17SVincenzo Maffione 	NR_REG_SW	= 2,
6392ff91c17SVincenzo Maffione 	NR_REG_NIC_SW	= 3,
6402ff91c17SVincenzo Maffione 	NR_REG_ONE_NIC	= 4,
6412ff91c17SVincenzo Maffione 	NR_REG_PIPE_MASTER = 5, /* deprecated, use "x{y" port name syntax */
6422ff91c17SVincenzo Maffione 	NR_REG_PIPE_SLAVE = 6,  /* deprecated, use "x}y" port name syntax */
643b6e66be2SVincenzo Maffione 	NR_REG_NULL     = 7,
644d12354a5SVincenzo Maffione 	NR_REG_ONE_SW	= 8,
6452ff91c17SVincenzo Maffione };
6462ff91c17SVincenzo Maffione 
6472ff91c17SVincenzo Maffione /* A single ioctl number is shared by all the new API command.
648b6e66be2SVincenzo Maffione  * Demultiplexing is done using the hdr.nr_reqtype field.
6492ff91c17SVincenzo Maffione  * FreeBSD uses the size value embedded in the _IOWR to determine
6502ff91c17SVincenzo Maffione  * how much to copy in/out, so we define the ioctl() command
6512ff91c17SVincenzo Maffione  * specifying only nmreq_header, and copyin/copyout the rest. */
6522ff91c17SVincenzo Maffione #define NIOCCTRL	_IOWR('i', 151, struct nmreq_header)
6532ff91c17SVincenzo Maffione 
6542ff91c17SVincenzo Maffione /* The ioctl commands to sync TX/RX netmap rings.
6552ff91c17SVincenzo Maffione  * NIOCTXSYNC, NIOCRXSYNC synchronize tx or rx queues,
656d12354a5SVincenzo Maffione  *	whose identity is set in NETMAP_REQ_REGISTER through nr_ringid.
6572ff91c17SVincenzo Maffione  *	These are non blocking and take no argument. */
6582ff91c17SVincenzo Maffione #define NIOCTXSYNC	_IO('i', 148) /* sync tx queues */
6592ff91c17SVincenzo Maffione #define NIOCRXSYNC	_IO('i', 149) /* sync rx queues */
6602ff91c17SVincenzo Maffione 
6612ff91c17SVincenzo Maffione /*
6622ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_PORT_INFO_GET
6632ff91c17SVincenzo Maffione  * Get information about a netmap port, including number of rings.
664b6e66be2SVincenzo Maffione  * slots per ring, id of the memory allocator, etc. The netmap
665b6e66be2SVincenzo Maffione  * control device used for this operation does not need to be bound
666b6e66be2SVincenzo Maffione  * to a netmap port.
6672ff91c17SVincenzo Maffione  */
6682ff91c17SVincenzo Maffione struct nmreq_port_info_get {
6692ff91c17SVincenzo Maffione 	uint64_t	nr_memsize;	/* size of the shared region */
6702ff91c17SVincenzo Maffione 	uint32_t	nr_tx_slots;	/* slots in tx rings */
6712ff91c17SVincenzo Maffione 	uint32_t	nr_rx_slots;	/* slots in rx rings */
6722ff91c17SVincenzo Maffione 	uint16_t	nr_tx_rings;	/* number of tx rings */
6732ff91c17SVincenzo Maffione 	uint16_t	nr_rx_rings;	/* number of rx rings */
674d12354a5SVincenzo Maffione 	uint16_t	nr_host_tx_rings; /* number of host tx rings */
675d12354a5SVincenzo Maffione 	uint16_t	nr_host_rx_rings; /* number of host rx rings */
676b6e66be2SVincenzo Maffione 	uint16_t	nr_mem_id;	/* memory allocator id (in/out) */
677d12354a5SVincenzo Maffione 	uint16_t	pad[3];
6782ff91c17SVincenzo Maffione };
679f0ea3689SLuigi Rizzo 
68037e3a6d3SLuigi Rizzo #define	NM_BDG_NAME		"vale"	/* prefix for bridge port name */
68137e3a6d3SLuigi Rizzo 
68237e3a6d3SLuigi Rizzo /*
6832ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_VALE_ATTACH
6842ff91c17SVincenzo Maffione  * Attach a netmap port to a VALE switch. Both the name of the netmap
6852ff91c17SVincenzo Maffione  * port and the VALE switch are specified through the nr_name argument.
6862ff91c17SVincenzo Maffione  * The attach operation could need to register a port, so at least
6872ff91c17SVincenzo Maffione  * the same arguments are available.
6882ff91c17SVincenzo Maffione  * port_index will contain the index where the port has been attached.
68937e3a6d3SLuigi Rizzo  */
6902ff91c17SVincenzo Maffione struct nmreq_vale_attach {
6912ff91c17SVincenzo Maffione 	struct nmreq_register reg;
6922ff91c17SVincenzo Maffione 	uint32_t port_index;
693b6e66be2SVincenzo Maffione 	uint32_t pad1;
6942ff91c17SVincenzo Maffione };
69517885a7bSLuigi Rizzo 
69664ae02c3SLuigi Rizzo /*
6972ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_VALE_DETACH
6982ff91c17SVincenzo Maffione  * Detach a netmap port from a VALE switch. Both the name of the netmap
6992ff91c17SVincenzo Maffione  * port and the VALE switch are specified through the nr_name argument.
7002ff91c17SVincenzo Maffione  * port_index will contain the index where the port was attached.
70164ae02c3SLuigi Rizzo  */
7022ff91c17SVincenzo Maffione struct nmreq_vale_detach {
7032ff91c17SVincenzo Maffione 	uint32_t port_index;
704b6e66be2SVincenzo Maffione 	uint32_t pad1;
7052ff91c17SVincenzo Maffione };
70617885a7bSLuigi Rizzo 
70717885a7bSLuigi Rizzo /*
7082ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_VALE_LIST
7092ff91c17SVincenzo Maffione  * List the ports of a VALE switch.
71017885a7bSLuigi Rizzo  */
7112ff91c17SVincenzo Maffione struct nmreq_vale_list {
7122ff91c17SVincenzo Maffione 	/* Name of the VALE port (valeXXX:YYY) or empty. */
7132ff91c17SVincenzo Maffione 	uint16_t	nr_bridge_idx;
714b6e66be2SVincenzo Maffione 	uint16_t	pad1;
7152ff91c17SVincenzo Maffione 	uint32_t	nr_port_idx;
7162ff91c17SVincenzo Maffione };
71717885a7bSLuigi Rizzo 
71817885a7bSLuigi Rizzo /*
7192ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_PORT_HDR_SET or NETMAP_REQ_PORT_HDR_GET
720b6e66be2SVincenzo Maffione  * Set or get the port header length of the port identified by hdr.nr_name.
721b6e66be2SVincenzo Maffione  * The control device does not need to be bound to a netmap port.
72217885a7bSLuigi Rizzo  */
7232ff91c17SVincenzo Maffione struct nmreq_port_hdr {
7242ff91c17SVincenzo Maffione 	uint32_t	nr_hdr_len;
725b6e66be2SVincenzo Maffione 	uint32_t	pad1;
7262ff91c17SVincenzo Maffione };
72717885a7bSLuigi Rizzo 
7284bf50f18SLuigi Rizzo /*
7292ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_VALE_NEWIF
7302ff91c17SVincenzo Maffione  * Create a new persistent VALE port.
7314bf50f18SLuigi Rizzo  */
7322ff91c17SVincenzo Maffione struct nmreq_vale_newif {
7332ff91c17SVincenzo Maffione 	uint32_t	nr_tx_slots;	/* slots in tx rings */
7342ff91c17SVincenzo Maffione 	uint32_t	nr_rx_slots;	/* slots in rx rings */
7352ff91c17SVincenzo Maffione 	uint16_t	nr_tx_rings;	/* number of tx rings */
7362ff91c17SVincenzo Maffione 	uint16_t	nr_rx_rings;	/* number of rx rings */
7372ff91c17SVincenzo Maffione 	uint16_t	nr_mem_id;	/* id of the memory allocator */
738b6e66be2SVincenzo Maffione 	uint16_t	pad1;
7392ff91c17SVincenzo Maffione };
7402ff91c17SVincenzo Maffione 
7412ff91c17SVincenzo Maffione /*
7422ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_VALE_POLLING_ENABLE or NETMAP_REQ_VALE_POLLING_DISABLE
7432ff91c17SVincenzo Maffione  * Enable or disable polling kthreads on a VALE port.
7442ff91c17SVincenzo Maffione  */
7452ff91c17SVincenzo Maffione struct nmreq_vale_polling {
7462ff91c17SVincenzo Maffione 	uint32_t	nr_mode;
7472ff91c17SVincenzo Maffione #define NETMAP_POLLING_MODE_SINGLE_CPU 1
7482ff91c17SVincenzo Maffione #define NETMAP_POLLING_MODE_MULTI_CPU 2
7492ff91c17SVincenzo Maffione 	uint32_t	nr_first_cpu_id;
7502ff91c17SVincenzo Maffione 	uint32_t	nr_num_polling_cpus;
751b6e66be2SVincenzo Maffione 	uint32_t	pad1;
7522ff91c17SVincenzo Maffione };
7532ff91c17SVincenzo Maffione 
7542ff91c17SVincenzo Maffione /*
7552ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_POOLS_INFO_GET
756b6e66be2SVincenzo Maffione  * Get info about the pools of the memory allocator of the netmap
757b6e66be2SVincenzo Maffione  * port specified by hdr.nr_name and nr_mem_id. The netmap control
758b6e66be2SVincenzo Maffione  * device used for this operation does not need to be bound to a netmap
759b6e66be2SVincenzo Maffione  * port.
7602ff91c17SVincenzo Maffione  */
7612ff91c17SVincenzo Maffione struct nmreq_pools_info {
7622ff91c17SVincenzo Maffione 	uint64_t	nr_memsize;
763b6e66be2SVincenzo Maffione 	uint16_t	nr_mem_id; /* in/out argument */
764b6e66be2SVincenzo Maffione 	uint16_t	pad1[3];
7652ff91c17SVincenzo Maffione 	uint64_t	nr_if_pool_offset;
7662ff91c17SVincenzo Maffione 	uint32_t	nr_if_pool_objtotal;
7672ff91c17SVincenzo Maffione 	uint32_t	nr_if_pool_objsize;
7682ff91c17SVincenzo Maffione 	uint64_t	nr_ring_pool_offset;
7692ff91c17SVincenzo Maffione 	uint32_t	nr_ring_pool_objtotal;
7702ff91c17SVincenzo Maffione 	uint32_t	nr_ring_pool_objsize;
7712ff91c17SVincenzo Maffione 	uint64_t	nr_buf_pool_offset;
7722ff91c17SVincenzo Maffione 	uint32_t	nr_buf_pool_objtotal;
7732ff91c17SVincenzo Maffione 	uint32_t	nr_buf_pool_objsize;
7742ff91c17SVincenzo Maffione };
7752ff91c17SVincenzo Maffione 
7762ff91c17SVincenzo Maffione /*
777b6e66be2SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_SYNC_KLOOP_START
778b6e66be2SVincenzo Maffione  * Start an in-kernel loop that syncs the rings periodically or on
779b6e66be2SVincenzo Maffione  * notifications. The loop runs in the context of the ioctl syscall,
780b6e66be2SVincenzo Maffione  * and only stops on NETMAP_REQ_SYNC_KLOOP_STOP.
781b6e66be2SVincenzo Maffione  * The registered netmap port must be open in CSB mode.
782b6e66be2SVincenzo Maffione  */
783b6e66be2SVincenzo Maffione struct nmreq_sync_kloop_start {
784b6e66be2SVincenzo Maffione 	/* Sleeping is the default synchronization method for the kloop.
7858ba2beacSGordon Bergling 	 * The 'sleep_us' field specifies how many microseconds to sleep for
786b6e66be2SVincenzo Maffione 	 * when there is no work to do, before doing another kloop iteration.
787b6e66be2SVincenzo Maffione 	 */
788b6e66be2SVincenzo Maffione 	uint32_t	sleep_us;
789b6e66be2SVincenzo Maffione 	uint32_t	pad1;
790b6e66be2SVincenzo Maffione };
791b6e66be2SVincenzo Maffione 
792b6e66be2SVincenzo Maffione /* A CSB entry for the application --> kernel direction. */
793b6e66be2SVincenzo Maffione struct nm_csb_atok {
794b6e66be2SVincenzo Maffione 	uint32_t head;		  /* AW+ KR+ the head of the appl netmap_ring */
795b6e66be2SVincenzo Maffione 	uint32_t cur;		  /* AW+ KR+ the cur of the appl netmap_ring */
796b6e66be2SVincenzo Maffione 	uint32_t appl_need_kick;  /* AW+ KR+ kern --> appl notification enable */
797b6e66be2SVincenzo Maffione 	uint32_t sync_flags;	  /* AW+ KR+ the flags of the appl [tx|rx]sync() */
798b6e66be2SVincenzo Maffione 	uint32_t pad[12];	  /* pad to a 64 bytes cacheline */
799b6e66be2SVincenzo Maffione };
800b6e66be2SVincenzo Maffione 
801b6e66be2SVincenzo Maffione /* A CSB entry for the application <-- kernel direction. */
802b6e66be2SVincenzo Maffione struct nm_csb_ktoa {
803b6e66be2SVincenzo Maffione 	uint32_t hwcur;		  /* AR+ KW+ the hwcur of the kern netmap_kring */
804b6e66be2SVincenzo Maffione 	uint32_t hwtail;	  /* AR+ KW+ the hwtail of the kern netmap_kring */
805b6e66be2SVincenzo Maffione 	uint32_t kern_need_kick;  /* AR+ KW+ appl-->kern notification enable */
806b6e66be2SVincenzo Maffione 	uint32_t pad[13];
807b6e66be2SVincenzo Maffione };
808b6e66be2SVincenzo Maffione 
809b6e66be2SVincenzo Maffione #ifdef __linux__
810b6e66be2SVincenzo Maffione 
811b6e66be2SVincenzo Maffione #ifdef __KERNEL__
812b6e66be2SVincenzo Maffione #define nm_stst_barrier smp_wmb
813f79ba6d7SVincenzo Maffione #define nm_ldld_barrier smp_rmb
814f79ba6d7SVincenzo Maffione #define nm_stld_barrier smp_mb
815b6e66be2SVincenzo Maffione #else  /* !__KERNEL__ */
nm_stst_barrier(void)816b6e66be2SVincenzo Maffione static inline void nm_stst_barrier(void)
817b6e66be2SVincenzo Maffione {
818b6e66be2SVincenzo Maffione 	/* A memory barrier with release semantic has the combined
819b6e66be2SVincenzo Maffione 	 * effect of a store-store barrier and a load-store barrier,
820b6e66be2SVincenzo Maffione 	 * which is fine for us. */
821b6e66be2SVincenzo Maffione 	__atomic_thread_fence(__ATOMIC_RELEASE);
822b6e66be2SVincenzo Maffione }
nm_ldld_barrier(void)823f79ba6d7SVincenzo Maffione static inline void nm_ldld_barrier(void)
824f79ba6d7SVincenzo Maffione {
825f79ba6d7SVincenzo Maffione 	/* A memory barrier with acquire semantic has the combined
826f79ba6d7SVincenzo Maffione 	 * effect of a load-load barrier and a store-load barrier,
827f79ba6d7SVincenzo Maffione 	 * which is fine for us. */
828f79ba6d7SVincenzo Maffione 	__atomic_thread_fence(__ATOMIC_ACQUIRE);
829f79ba6d7SVincenzo Maffione }
830b6e66be2SVincenzo Maffione #endif /* !__KERNEL__ */
831b6e66be2SVincenzo Maffione 
832b6e66be2SVincenzo Maffione #elif defined(__FreeBSD__)
833b6e66be2SVincenzo Maffione 
834b6e66be2SVincenzo Maffione #ifdef _KERNEL
835b6e66be2SVincenzo Maffione #define nm_stst_barrier	atomic_thread_fence_rel
836f79ba6d7SVincenzo Maffione #define nm_ldld_barrier	atomic_thread_fence_acq
837f79ba6d7SVincenzo Maffione #define nm_stld_barrier	atomic_thread_fence_seq_cst
838b6e66be2SVincenzo Maffione #else  /* !_KERNEL */
839a6d768d8SVincenzo Maffione 
840a6d768d8SVincenzo Maffione #ifdef __cplusplus
841a6d768d8SVincenzo Maffione #include <atomic>
842a6d768d8SVincenzo Maffione using std::memory_order_release;
843a6d768d8SVincenzo Maffione using std::memory_order_acquire;
844a6d768d8SVincenzo Maffione 
845a6d768d8SVincenzo Maffione #else /* __cplusplus */
8461d9ec3eeSVincenzo Maffione #include <stdatomic.h>
847a6d768d8SVincenzo Maffione #endif /* __cplusplus */
848a6d768d8SVincenzo Maffione 
nm_stst_barrier(void)849b6e66be2SVincenzo Maffione static inline void nm_stst_barrier(void)
850b6e66be2SVincenzo Maffione {
8511d9ec3eeSVincenzo Maffione 	atomic_thread_fence(memory_order_release);
852b6e66be2SVincenzo Maffione }
nm_ldld_barrier(void)853f79ba6d7SVincenzo Maffione static inline void nm_ldld_barrier(void)
854f79ba6d7SVincenzo Maffione {
855f79ba6d7SVincenzo Maffione 	atomic_thread_fence(memory_order_acquire);
856f79ba6d7SVincenzo Maffione }
857b6e66be2SVincenzo Maffione #endif /* !_KERNEL */
858b6e66be2SVincenzo Maffione 
859b6e66be2SVincenzo Maffione #else  /* !__linux__ && !__FreeBSD__ */
860b6e66be2SVincenzo Maffione #error "OS not supported"
861b6e66be2SVincenzo Maffione #endif /* !__linux__ && !__FreeBSD__ */
862b6e66be2SVincenzo Maffione 
863b6e66be2SVincenzo Maffione /* Application side of sync-kloop: Write ring pointers (cur, head) to the CSB.
864b6e66be2SVincenzo Maffione  * This routine is coupled with sync_kloop_kernel_read(). */
865b6e66be2SVincenzo Maffione static inline void
nm_sync_kloop_appl_write(struct nm_csb_atok * atok,uint32_t cur,uint32_t head)866b6e66be2SVincenzo Maffione nm_sync_kloop_appl_write(struct nm_csb_atok *atok, uint32_t cur,
867b6e66be2SVincenzo Maffione 			 uint32_t head)
868b6e66be2SVincenzo Maffione {
869f79ba6d7SVincenzo Maffione 	/* Issue a first store-store barrier to make sure writes to the
870f79ba6d7SVincenzo Maffione 	 * netmap ring do not overcome updates on atok->cur and atok->head. */
871f79ba6d7SVincenzo Maffione 	nm_stst_barrier();
872f79ba6d7SVincenzo Maffione 
873b6e66be2SVincenzo Maffione 	/*
874b6e66be2SVincenzo Maffione 	 * We need to write cur and head to the CSB but we cannot do it atomically.
875b6e66be2SVincenzo Maffione 	 * There is no way we can prevent the host from reading the updated value
876b6e66be2SVincenzo Maffione 	 * of one of the two and the old value of the other. However, if we make
877b6e66be2SVincenzo Maffione 	 * sure that the host never reads a value of head more recent than the
878b6e66be2SVincenzo Maffione 	 * value of cur we are safe. We can allow the host to read a value of cur
879b6e66be2SVincenzo Maffione 	 * more recent than the value of head, since in the netmap ring cur can be
880b6e66be2SVincenzo Maffione 	 * ahead of head and cur cannot wrap around head because it must be behind
881b6e66be2SVincenzo Maffione 	 * tail. Inverting the order of writes below could instead result into the
882b6e66be2SVincenzo Maffione 	 * host to think head went ahead of cur, which would cause the sync
883b6e66be2SVincenzo Maffione 	 * prologue to fail.
884b6e66be2SVincenzo Maffione 	 *
885b6e66be2SVincenzo Maffione 	 * The following memory barrier scheme is used to make this happen:
886b6e66be2SVincenzo Maffione 	 *
887b6e66be2SVincenzo Maffione 	 *          Guest                Host
888b6e66be2SVincenzo Maffione 	 *
889b6e66be2SVincenzo Maffione 	 *          STORE(cur)           LOAD(head)
890f79ba6d7SVincenzo Maffione 	 *          wmb() <----------->  rmb()
891b6e66be2SVincenzo Maffione 	 *          STORE(head)          LOAD(cur)
892b6e66be2SVincenzo Maffione 	 *
893b6e66be2SVincenzo Maffione 	 */
894b6e66be2SVincenzo Maffione 	atok->cur = cur;
895b6e66be2SVincenzo Maffione 	nm_stst_barrier();
896b6e66be2SVincenzo Maffione 	atok->head = head;
897b6e66be2SVincenzo Maffione }
898b6e66be2SVincenzo Maffione 
899b6e66be2SVincenzo Maffione /* Application side of sync-kloop: Read kring pointers (hwcur, hwtail) from
900b6e66be2SVincenzo Maffione  * the CSB. This routine is coupled with sync_kloop_kernel_write(). */
901b6e66be2SVincenzo Maffione static inline void
nm_sync_kloop_appl_read(struct nm_csb_ktoa * ktoa,uint32_t * hwtail,uint32_t * hwcur)902b6e66be2SVincenzo Maffione nm_sync_kloop_appl_read(struct nm_csb_ktoa *ktoa, uint32_t *hwtail,
903b6e66be2SVincenzo Maffione 			uint32_t *hwcur)
904b6e66be2SVincenzo Maffione {
905b6e66be2SVincenzo Maffione 	/*
906b6e66be2SVincenzo Maffione 	 * We place a memory barrier to make sure that the update of hwtail never
907b6e66be2SVincenzo Maffione 	 * overtakes the update of hwcur.
908b6e66be2SVincenzo Maffione 	 * (see explanation in sync_kloop_kernel_write).
909b6e66be2SVincenzo Maffione 	 */
910b6e66be2SVincenzo Maffione 	*hwtail = ktoa->hwtail;
911f79ba6d7SVincenzo Maffione 	nm_ldld_barrier();
912b6e66be2SVincenzo Maffione 	*hwcur = ktoa->hwcur;
913f79ba6d7SVincenzo Maffione 
914f79ba6d7SVincenzo Maffione 	/* Make sure that loads from ktoa->hwtail and ktoa->hwcur are not delayed
915f79ba6d7SVincenzo Maffione 	 * after the loads from the netmap ring. */
916f79ba6d7SVincenzo Maffione 	nm_ldld_barrier();
917b6e66be2SVincenzo Maffione }
918b6e66be2SVincenzo Maffione 
919b6e66be2SVincenzo Maffione /*
9202ff91c17SVincenzo Maffione  * data for NETMAP_REQ_OPT_* options
9212ff91c17SVincenzo Maffione  */
9222ff91c17SVincenzo Maffione 
923b6e66be2SVincenzo Maffione struct nmreq_opt_sync_kloop_eventfds {
924b6e66be2SVincenzo Maffione 	struct nmreq_option	nro_opt;	/* common header */
925b6e66be2SVincenzo Maffione 	/* An array of N entries for bidirectional notifications between
926b6e66be2SVincenzo Maffione 	 * the kernel loop and the application. The number of entries and
927b6e66be2SVincenzo Maffione 	 * their order must agree with the CSB arrays passed in the
928b6e66be2SVincenzo Maffione 	 * NETMAP_REQ_OPT_CSB option. Each entry contains a file descriptor
929b6e66be2SVincenzo Maffione 	 * backed by an eventfd.
9305faab778SVincenzo Maffione 	 *
9315faab778SVincenzo Maffione 	 * If any of the 'ioeventfd' entries is < 0, the event loop uses
9325faab778SVincenzo Maffione 	 * the sleeping synchronization strategy (according to sleep_us),
9335faab778SVincenzo Maffione 	 * and keeps kern_need_kick always disabled.
9345faab778SVincenzo Maffione 	 * Each 'irqfd' can be < 0, and in that case the corresponding queue
9355faab778SVincenzo Maffione 	 * is never notified.
936b6e66be2SVincenzo Maffione 	 */
937b6e66be2SVincenzo Maffione 	struct {
938b6e66be2SVincenzo Maffione 		/* Notifier for the application --> kernel loop direction. */
939b6e66be2SVincenzo Maffione 		int32_t ioeventfd;
940b6e66be2SVincenzo Maffione 		/* Notifier for the kernel loop --> application direction. */
941b6e66be2SVincenzo Maffione 		int32_t irqfd;
942b6e66be2SVincenzo Maffione 	} eventfds[0];
943b6e66be2SVincenzo Maffione };
944b6e66be2SVincenzo Maffione 
9455faab778SVincenzo Maffione struct nmreq_opt_sync_kloop_mode {
9465faab778SVincenzo Maffione 	struct nmreq_option	nro_opt;	/* common header */
9475faab778SVincenzo Maffione #define NM_OPT_SYNC_KLOOP_DIRECT_TX (1 << 0)
9485faab778SVincenzo Maffione #define NM_OPT_SYNC_KLOOP_DIRECT_RX (1 << 1)
9495faab778SVincenzo Maffione 	uint32_t mode;
9505faab778SVincenzo Maffione };
9515faab778SVincenzo Maffione 
9522ff91c17SVincenzo Maffione struct nmreq_opt_extmem {
9532ff91c17SVincenzo Maffione 	struct nmreq_option	nro_opt;	/* common header */
9542ff91c17SVincenzo Maffione 	uint64_t		nro_usrptr;	/* (in) ptr to usr memory */
9552ff91c17SVincenzo Maffione 	struct nmreq_pools_info	nro_info;	/* (in/out) */
9564bf50f18SLuigi Rizzo };
9574bf50f18SLuigi Rizzo 
958b6e66be2SVincenzo Maffione struct nmreq_opt_csb {
959b6e66be2SVincenzo Maffione 	struct nmreq_option	nro_opt;
960b6e66be2SVincenzo Maffione 
961b6e66be2SVincenzo Maffione 	/* Array of CSB entries for application --> kernel communication
962b6e66be2SVincenzo Maffione 	 * (N entries). */
963b6e66be2SVincenzo Maffione 	uint64_t		csb_atok;
964b6e66be2SVincenzo Maffione 
965b6e66be2SVincenzo Maffione 	/* Array of CSB entries for kernel --> application communication
966b6e66be2SVincenzo Maffione 	 * (N entries). */
967b6e66be2SVincenzo Maffione 	uint64_t		csb_ktoa;
968b6e66be2SVincenzo Maffione };
969b6e66be2SVincenzo Maffione 
970a6d768d8SVincenzo Maffione /* option NETMAP_REQ_OPT_OFFSETS */
971a6d768d8SVincenzo Maffione struct nmreq_opt_offsets {
972a6d768d8SVincenzo Maffione 	struct nmreq_option	nro_opt;
973a6d768d8SVincenzo Maffione 	/* the user must declare the maximum offset value that she is
974a6d768d8SVincenzo Maffione 	 * going to put into the offset slot-fields. Any larger value
975a6d768d8SVincenzo Maffione 	 * found at runtime will be cropped. On output the (possibly
976a6d768d8SVincenzo Maffione 	 * higher) effective max value is returned.
977a6d768d8SVincenzo Maffione 	 */
978a6d768d8SVincenzo Maffione 	uint64_t		nro_max_offset;
979a6d768d8SVincenzo Maffione 	/* optional initial offset value, to be set in all slots. */
980a6d768d8SVincenzo Maffione 	uint64_t		nro_initial_offset;
981a6d768d8SVincenzo Maffione 	/* number of bits in the lower part of the 'ptr' field to be
98245c67e8fSVincenzo Maffione 	 * used as the offset field. On output the (possibly larger)
983a6d768d8SVincenzo Maffione 	 * effective number of bits is returned.
984a6d768d8SVincenzo Maffione 	 * 0 means: use the whole ptr field.
985a6d768d8SVincenzo Maffione 	 */
986a6d768d8SVincenzo Maffione 	uint32_t		nro_offset_bits;
987a6d768d8SVincenzo Maffione 	/* required alignment for the beginning of the packets
988a6d768d8SVincenzo Maffione 	 * (base of the buffer plus offset) in the TX slots.
989a6d768d8SVincenzo Maffione 	 */
990a6d768d8SVincenzo Maffione 	uint32_t		nro_tx_align;
991a6d768d8SVincenzo Maffione 	/* Reserved: set to zero. */
992a6d768d8SVincenzo Maffione 	uint64_t		nro_min_gap;
993a6d768d8SVincenzo Maffione };
994a6d768d8SVincenzo Maffione 
99568b8534bSLuigi Rizzo #endif /* _NET_NETMAP_H_ */
996