xref: /freebsd/sys/net/netmap.h (revision 45c67e8f6b56b9744f01142747fadf291fe3fad2)
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 
44d12354a5SVincenzo Maffione #define	NETMAP_API	14		/* current API version */
4517885a7bSLuigi Rizzo 
46d12354a5SVincenzo Maffione #define	NETMAP_MIN_API	14		/* 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       |
72d12354a5SVincenzo Maffione     +----------------+                      /   | other fields  |
7317885a7bSLuigi Rizzo     | ni_tx_rings    |                     /    +===============+
74ce3ee1e7SLuigi Rizzo     | ni_rx_rings    |                    /     | buf_idx, len  | slot[0]
75ce3ee1e7SLuigi Rizzo     |                |                   /      | flags, ptr    |
7617885a7bSLuigi Rizzo     |                |                  /       +---------------+
77d12354a5SVincenzo Maffione     +================+                 /        | buf_idx, len  | slot[1]
78ce3ee1e7SLuigi Rizzo     | txring_ofs[0]  | (rel.to nifp)--'         | flags, ptr    |
7917885a7bSLuigi Rizzo     | txring_ofs[1]  |                          +---------------+
80d12354a5SVincenzo Maffione      (tx+htx entries)                           (num_slots entries)
81ce3ee1e7SLuigi Rizzo     | txring_ofs[t]  |                          | buf_idx, len  | slot[n-1]
82d12354a5SVincenzo Maffione     +----------------+                          | flags, ptr    |
8317885a7bSLuigi Rizzo     | rxring_ofs[0]  |                          +---------------+
8468b8534bSLuigi Rizzo     | rxring_ofs[1]  |
85d12354a5SVincenzo Maffione      (rx+hrx entries)
86ce3ee1e7SLuigi Rizzo     | rxring_ofs[r]  |
87d12354a5SVincenzo Maffione     +----------------+
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  *
93d12354a5SVincenzo Maffione  * There is one netmap_ring per physical NIC ring, plus at least one tx/rx ring
94d12354a5SVincenzo Maffione  * pair attached to the host stack (these pairs are 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 attach to PIPE rings sharing the same memory
121f0ea3689SLuigi Rizzo  *   space with a parent device. The ifname indicates the parent device,
122f0ea3689SLuigi Rizzo  *   which must already exist. Flags in nr_flags indicate if we want to
123f0ea3689SLuigi Rizzo  *   bind the master or slave side, the index (from nr_ringid)
1245c8c1004SLuigi Rizzo  *   is just a cookie and does not need to be sequential.
125f0ea3689SLuigi Rizzo  *
126f0ea3689SLuigi Rizzo  * + NIOCREGIF can also attach to 'monitor' rings that replicate
127f0ea3689SLuigi Rizzo  *   the content of specific rings, also from the same memory space.
128f0ea3689SLuigi Rizzo  *
129f0ea3689SLuigi Rizzo  *   Extra flags in nr_flags support the above functions.
130f0ea3689SLuigi Rizzo  *   Application libraries may use the following naming scheme:
131d12354a5SVincenzo Maffione  *	netmap:foo			all NIC rings pairs
132d12354a5SVincenzo Maffione  *	netmap:foo^			only host rings pairs
133d12354a5SVincenzo Maffione  *	netmap:foo^k			the k-th host rings pair
134d12354a5SVincenzo Maffione  *	netmap:foo+			all NIC rings + host rings pairs
135d12354a5SVincenzo Maffione  *	netmap:foo-k			the k-th NIC rings pair
136d12354a5SVincenzo Maffione  *	netmap:foo{k			PIPE rings pair k, master side
137d12354a5SVincenzo Maffione  *	netmap:foo}k			PIPE rings pair k, slave side
13837e3a6d3SLuigi Rizzo  *
13937e3a6d3SLuigi Rizzo  * Some notes about host rings:
14037e3a6d3SLuigi Rizzo  *
141d12354a5SVincenzo Maffione  * + The RX host rings are used to store those packets that the host network
14237e3a6d3SLuigi Rizzo  *   stack is trying to transmit through a NIC queue, but only if that queue
14337e3a6d3SLuigi Rizzo  *   is currently in netmap mode. Netmap will not intercept host stack mbufs
14437e3a6d3SLuigi Rizzo  *   designated to NIC queues that are not in netmap mode. As a consequence,
14537e3a6d3SLuigi Rizzo  *   registering a netmap port with netmap:foo^ is not enough to intercept
146d12354a5SVincenzo Maffione  *   mbufs in the RX host rings; the netmap port should be registered with
14737e3a6d3SLuigi Rizzo  *   netmap:foo*, or another registration should be done to open at least a
14837e3a6d3SLuigi Rizzo  *   NIC TX queue in netmap mode.
14937e3a6d3SLuigi Rizzo  *
150*45c67e8fSVincenzo Maffione  * + Netmap is not currently able to deal with intercepted transmit mbufs which
15137e3a6d3SLuigi Rizzo  *   require offloadings like TSO, UFO, checksumming offloadings, etc. It is
15237e3a6d3SLuigi Rizzo  *   responsibility of the user to disable those offloadings (e.g. using
15337e3a6d3SLuigi Rizzo  *   ifconfig on FreeBSD or ethtool -K on Linux) for an interface that is being
15437e3a6d3SLuigi Rizzo  *   used in netmap mode. If the offloadings are not disabled, GSO and/or
15537e3a6d3SLuigi Rizzo  *   unchecksummed packets may be dropped immediately or end up in the host RX
156d12354a5SVincenzo Maffione  *   rings, and will be dropped as soon as the packet reaches another netmap
15737e3a6d3SLuigi Rizzo  *   adapter.
158ce3ee1e7SLuigi Rizzo  */
159ce3ee1e7SLuigi Rizzo 
160ce3ee1e7SLuigi Rizzo /*
161ce3ee1e7SLuigi Rizzo  * struct netmap_slot is a buffer descriptor
16217885a7bSLuigi Rizzo  */
16317885a7bSLuigi Rizzo struct netmap_slot {
16417885a7bSLuigi Rizzo 	uint32_t buf_idx;	/* buffer index */
16517885a7bSLuigi Rizzo 	uint16_t len;		/* length for this slot */
16617885a7bSLuigi Rizzo 	uint16_t flags;		/* buf changed, etc. */
16717885a7bSLuigi Rizzo 	uint64_t ptr;		/* pointer for indirect buffers */
16817885a7bSLuigi Rizzo };
16917885a7bSLuigi Rizzo 
17017885a7bSLuigi Rizzo /*
17117885a7bSLuigi Rizzo  * The following flags control how the slot is used
17217885a7bSLuigi Rizzo  */
17317885a7bSLuigi Rizzo 
17417885a7bSLuigi Rizzo #define	NS_BUF_CHANGED	0x0001	/* buf_idx changed */
17517885a7bSLuigi Rizzo 	/*
17617885a7bSLuigi Rizzo 	 * must be set whenever buf_idx is changed (as it might be
17717885a7bSLuigi Rizzo 	 * necessary to recompute the physical address and mapping)
178847bf383SLuigi Rizzo 	 *
179847bf383SLuigi Rizzo 	 * It is also set by the kernel whenever the buf_idx is
180847bf383SLuigi Rizzo 	 * changed internally (e.g., by pipes). Applications may
181847bf383SLuigi Rizzo 	 * use this information to know when they can reuse the
182847bf383SLuigi Rizzo 	 * contents of previously prepared buffers.
18317885a7bSLuigi Rizzo 	 */
18417885a7bSLuigi Rizzo 
18517885a7bSLuigi Rizzo #define	NS_REPORT	0x0002	/* ask the hardware to report results */
18617885a7bSLuigi Rizzo 	/*
18717885a7bSLuigi Rizzo 	 * Request notification when slot is used by the hardware.
18817885a7bSLuigi Rizzo 	 * Normally transmit completions are handled lazily and
18917885a7bSLuigi Rizzo 	 * may be unreported. This flag lets us know when a slot
19017885a7bSLuigi Rizzo 	 * has been sent (e.g. to terminate the sender).
19117885a7bSLuigi Rizzo 	 */
19217885a7bSLuigi Rizzo 
19317885a7bSLuigi Rizzo #define	NS_FORWARD	0x0004	/* pass packet 'forward' */
19417885a7bSLuigi Rizzo 	/*
19517885a7bSLuigi Rizzo 	 * (Only for physical ports, rx rings with NR_FORWARD set).
19617885a7bSLuigi Rizzo 	 * Slot released to the kernel (i.e. before ring->head) with
19717885a7bSLuigi Rizzo 	 * this flag set are passed to the peer ring (host/NIC),
19817885a7bSLuigi Rizzo 	 * thus restoring the host-NIC connection for these slots.
19917885a7bSLuigi Rizzo 	 * This supports efficient traffic monitoring or firewalling.
20017885a7bSLuigi Rizzo 	 */
20117885a7bSLuigi Rizzo 
20217885a7bSLuigi Rizzo #define	NS_NO_LEARN	0x0008	/* disable bridge learning */
20317885a7bSLuigi Rizzo  	/*
20417885a7bSLuigi Rizzo 	 * On a VALE switch, do not 'learn' the source port for
20517885a7bSLuigi Rizzo  	 * this buffer.
20617885a7bSLuigi Rizzo 	 */
20717885a7bSLuigi Rizzo 
20817885a7bSLuigi Rizzo #define	NS_INDIRECT	0x0010	/* userspace buffer */
20917885a7bSLuigi Rizzo  	/*
21017885a7bSLuigi Rizzo 	 * (VALE tx rings only) data is in a userspace buffer,
21117885a7bSLuigi Rizzo 	 * whose address is in the 'ptr' field in the slot.
21217885a7bSLuigi Rizzo 	 */
21317885a7bSLuigi Rizzo 
21417885a7bSLuigi Rizzo #define	NS_MOREFRAG	0x0020	/* packet has more fragments */
21517885a7bSLuigi Rizzo  	/*
2164f80b14cSVincenzo Maffione 	 * (VALE ports, ptnetmap ports and some NIC ports, e.g.
2174f80b14cSVincenzo Maffione          * ixgbe and i40e on Linux)
21817885a7bSLuigi Rizzo 	 * Set on all but the last slot of a multi-segment packet.
21917885a7bSLuigi Rizzo 	 * The 'len' field refers to the individual fragment.
22017885a7bSLuigi Rizzo 	 */
22117885a7bSLuigi Rizzo 
222660a47cbSVincenzo Maffione #define NS_TXMON	0x0040
223660a47cbSVincenzo Maffione 	/* (monitor ports only) the packet comes from the TX
224660a47cbSVincenzo Maffione 	 * ring of the monitored port
225660a47cbSVincenzo Maffione 	 */
226660a47cbSVincenzo Maffione 
22717885a7bSLuigi Rizzo #define	NS_PORT_SHIFT	8
22817885a7bSLuigi Rizzo #define	NS_PORT_MASK	(0xff << NS_PORT_SHIFT)
22917885a7bSLuigi Rizzo 	/*
23017885a7bSLuigi Rizzo  	 * The high 8 bits of the flag, if not zero, indicate the
23101c039a1SLuigi Rizzo 	 * destination port for the VALE switch, overriding
23201c039a1SLuigi Rizzo  	 * the lookup table.
23368b8534bSLuigi Rizzo  	 */
23401c039a1SLuigi Rizzo 
23517885a7bSLuigi Rizzo #define	NS_RFRAGS(_slot)	( ((_slot)->flags >> 8) & 0xff)
236ce3ee1e7SLuigi Rizzo 	/*
23717885a7bSLuigi Rizzo 	 * (VALE rx rings only) the high 8 bits
238ce3ee1e7SLuigi Rizzo 	 *  are the number of fragments.
239ce3ee1e7SLuigi Rizzo 	 */
24017885a7bSLuigi Rizzo 
2412a7db7a6SVincenzo Maffione #define NETMAP_MAX_FRAGS	64	/* max number of fragments */
2422a7db7a6SVincenzo Maffione 
243a6d768d8SVincenzo Maffione 
24468b8534bSLuigi Rizzo /*
245ce3ee1e7SLuigi Rizzo  * struct netmap_ring
246ce3ee1e7SLuigi Rizzo  *
24768b8534bSLuigi Rizzo  * Netmap representation of a TX or RX ring (also known as "queue").
24868b8534bSLuigi Rizzo  * This is a queue implemented as a fixed-size circular array.
24917885a7bSLuigi Rizzo  * At the software level the important fields are: head, cur, tail.
25068b8534bSLuigi Rizzo  *
25168b8534bSLuigi Rizzo  * In TX rings:
252ce3ee1e7SLuigi Rizzo  *
25317885a7bSLuigi Rizzo  *	head	first slot available for transmission.
25417885a7bSLuigi Rizzo  *	cur	wakeup point. select() and poll() will unblock
25517885a7bSLuigi Rizzo  *		when 'tail' moves past 'cur'
25617885a7bSLuigi Rizzo  *	tail	(readonly) first slot reserved to the kernel
257ce3ee1e7SLuigi Rizzo  *
25817885a7bSLuigi Rizzo  *	[head .. tail-1] can be used for new packets to send;
25917885a7bSLuigi Rizzo  *	'head' and 'cur' must be incremented as slots are filled
26017885a7bSLuigi Rizzo  *	    with new packets to be sent;
26117885a7bSLuigi Rizzo  *	'cur' can be moved further ahead if we need more space
2625c8c1004SLuigi Rizzo  *	for new transmissions. XXX todo (2014-03-12)
26368b8534bSLuigi Rizzo  *
26468b8534bSLuigi Rizzo  * In RX rings:
265ce3ee1e7SLuigi Rizzo  *
26617885a7bSLuigi Rizzo  *	head	first valid received packet
26717885a7bSLuigi Rizzo  *	cur	wakeup point. select() and poll() will unblock
26817885a7bSLuigi Rizzo  *		when 'tail' moves past 'cur'
26917885a7bSLuigi Rizzo  *	tail	(readonly) first slot reserved to the kernel
270ce3ee1e7SLuigi Rizzo  *
27117885a7bSLuigi Rizzo  *	[head .. tail-1] contain received packets;
27217885a7bSLuigi Rizzo  *	'head' and 'cur' must be incremented as slots are consumed
27317885a7bSLuigi Rizzo  *		and can be returned to the kernel;
27417885a7bSLuigi Rizzo  *	'cur' can be moved further ahead if we want to wait for
27517885a7bSLuigi Rizzo  *		new packets without returning the previous ones.
27668b8534bSLuigi Rizzo  *
27768b8534bSLuigi Rizzo  * DATA OWNERSHIP/LOCKING:
27817885a7bSLuigi Rizzo  *	The netmap_ring, and all slots and buffers in the range
27917885a7bSLuigi Rizzo  *	[head .. tail-1] are owned by the user program;
28017885a7bSLuigi Rizzo  *	the kernel only accesses them during a netmap system call
28117885a7bSLuigi Rizzo  *	and in the user thread context.
28201c039a1SLuigi Rizzo  *
28317885a7bSLuigi Rizzo  *	Other slots and buffers are reserved for use by the kernel
28468b8534bSLuigi Rizzo  */
28568b8534bSLuigi Rizzo struct netmap_ring {
28668b8534bSLuigi Rizzo 	/*
287ce3ee1e7SLuigi Rizzo 	 * buf_ofs is meant to be used through macros.
28868b8534bSLuigi Rizzo 	 * It contains the offset of the buffer region from this
28968b8534bSLuigi Rizzo 	 * descriptor.
29068b8534bSLuigi Rizzo 	 */
29117885a7bSLuigi Rizzo 	const int64_t	buf_ofs;
29268b8534bSLuigi Rizzo 	const uint32_t	num_slots;	/* number of slots in the ring. */
29317885a7bSLuigi Rizzo 	const uint32_t	nr_buf_size;
29417885a7bSLuigi Rizzo 	const uint16_t	ringid;
29517885a7bSLuigi Rizzo 	const uint16_t	dir;		/* 0: tx, 1: rx */
29668b8534bSLuigi Rizzo 
29717885a7bSLuigi Rizzo 	uint32_t        head;		/* (u) first user slot */
29817885a7bSLuigi Rizzo 	uint32_t        cur;		/* (u) wakeup point */
29917885a7bSLuigi Rizzo 	uint32_t	tail;		/* (k) first kernel slot */
30068b8534bSLuigi Rizzo 
30117885a7bSLuigi Rizzo 	uint32_t	flags;
30217885a7bSLuigi Rizzo 
30317885a7bSLuigi Rizzo 	struct timeval	ts;		/* (k) time of last *sync() */
30417885a7bSLuigi Rizzo 
305a6d768d8SVincenzo Maffione 	/* offset_mask is used to isolate the part of the ptr field
306a6d768d8SVincenzo Maffione 	 * in the slots used to contain an offset in the buffer.
307a6d768d8SVincenzo Maffione 	 * It is zero if the ring has not be opened using the
308a6d768d8SVincenzo Maffione 	 * NETMAP_REQ_OPT_OFFSETS option.
309a6d768d8SVincenzo Maffione 	 */
310a6d768d8SVincenzo Maffione 	const uint64_t	offset_mask;
311a6d768d8SVincenzo Maffione 	/* the alignment requirement, in bytes, for the start
312a6d768d8SVincenzo Maffione 	 * of the packets inside the buffers.
313a6d768d8SVincenzo Maffione 	 * User programs should take this alignment into
314*45c67e8fSVincenzo Maffione 	 * account when specifying buffer-offsets in TX slots.
315a6d768d8SVincenzo Maffione 	 */
316a6d768d8SVincenzo Maffione 	const uint64_t	buf_align;
317a6d768d8SVincenzo Maffione 
31817885a7bSLuigi Rizzo 	/* opaque room for a mutex or similar object */
31937e3a6d3SLuigi Rizzo #if !defined(_WIN32) || defined(__CYGWIN__)
32037e3a6d3SLuigi Rizzo 	uint8_t	__attribute__((__aligned__(NM_CACHE_ALIGN))) sem[128];
32137e3a6d3SLuigi Rizzo #else
32237e3a6d3SLuigi Rizzo 	uint8_t	__declspec(align(NM_CACHE_ALIGN)) sem[128];
32337e3a6d3SLuigi Rizzo #endif
32468b8534bSLuigi Rizzo 
32568b8534bSLuigi Rizzo 	/* the slots follow. This struct has variable size */
32668b8534bSLuigi Rizzo 	struct netmap_slot slot[0];	/* array of slots. */
32768b8534bSLuigi Rizzo };
32868b8534bSLuigi Rizzo 
329a6d768d8SVincenzo Maffione 
33068b8534bSLuigi Rizzo /*
33117885a7bSLuigi Rizzo  * RING FLAGS
33217885a7bSLuigi Rizzo  */
33317885a7bSLuigi Rizzo #define	NR_TIMESTAMP	0x0002		/* set timestamp on *sync() */
33417885a7bSLuigi Rizzo 	/*
33517885a7bSLuigi Rizzo 	 * updates the 'ts' field on each netmap syscall. This saves
33617885a7bSLuigi Rizzo 	 * saves a separate gettimeofday(), and is not much worse than
33717885a7bSLuigi Rizzo 	 * software timestamps generated in the interrupt handler.
33817885a7bSLuigi Rizzo 	 */
33917885a7bSLuigi Rizzo 
34017885a7bSLuigi Rizzo #define	NR_FORWARD	0x0004		/* enable NS_FORWARD for ring */
34117885a7bSLuigi Rizzo  	/*
34217885a7bSLuigi Rizzo 	 * Enables the NS_FORWARD slot flag for the ring.
34317885a7bSLuigi Rizzo 	 */
34417885a7bSLuigi Rizzo 
3452ff91c17SVincenzo Maffione /*
3462ff91c17SVincenzo Maffione  * Helper functions for kernel and userspace
3472ff91c17SVincenzo Maffione  */
3482ff91c17SVincenzo Maffione 
3492ff91c17SVincenzo Maffione /*
350b6e66be2SVincenzo Maffione  * Check if space is available in the ring. We use ring->head, which
351b6e66be2SVincenzo Maffione  * points to the next netmap slot to be published to netmap. It is
352b6e66be2SVincenzo Maffione  * possible that the applications moves ring->cur ahead of ring->tail
353b6e66be2SVincenzo Maffione  * (e.g., by setting ring->cur <== ring->tail), if it wants more slots
354b6e66be2SVincenzo Maffione  * than the ones currently available, and it wants to be notified when
355b6e66be2SVincenzo Maffione  * more arrive. See netmap(4) for more details and examples.
3562ff91c17SVincenzo Maffione  */
3572ff91c17SVincenzo Maffione static inline int
3582ff91c17SVincenzo Maffione nm_ring_empty(struct netmap_ring *ring)
3592ff91c17SVincenzo Maffione {
360b6e66be2SVincenzo Maffione 	return (ring->head == ring->tail);
3612ff91c17SVincenzo Maffione }
36217885a7bSLuigi Rizzo 
36317885a7bSLuigi Rizzo /*
36468b8534bSLuigi Rizzo  * Netmap representation of an interface and its queue(s).
365ce3ee1e7SLuigi Rizzo  * This is initialized by the kernel when binding a file
366ce3ee1e7SLuigi Rizzo  * descriptor to a port, and should be considered as readonly
367ce3ee1e7SLuigi Rizzo  * by user programs. The kernel never uses it.
368ce3ee1e7SLuigi Rizzo  *
36968b8534bSLuigi Rizzo  * There is one netmap_if for each file descriptor on which we want
370ce3ee1e7SLuigi Rizzo  * to select/poll.
37168b8534bSLuigi Rizzo  * select/poll operates on one or all pairs depending on the value of
37268b8534bSLuigi Rizzo  * nmr_queueid passed on the ioctl.
37368b8534bSLuigi Rizzo  */
37468b8534bSLuigi Rizzo struct netmap_if {
37568b8534bSLuigi Rizzo 	char		ni_name[IFNAMSIZ]; /* name of the interface. */
376ce3ee1e7SLuigi Rizzo 	const uint32_t	ni_version;	/* API version, currently unused */
377ce3ee1e7SLuigi Rizzo 	const uint32_t	ni_flags;	/* properties */
378ce3ee1e7SLuigi Rizzo #define	NI_PRIV_MEM	0x1		/* private memory region */
379ce3ee1e7SLuigi Rizzo 
38017885a7bSLuigi Rizzo 	/*
38117885a7bSLuigi Rizzo 	 * The number of packet rings available in netmap mode.
38217885a7bSLuigi Rizzo 	 * Physical NICs can have different numbers of tx and rx rings.
383d12354a5SVincenzo Maffione 	 * Physical NICs also have at least a 'host' rings pair.
38417885a7bSLuigi Rizzo 	 * Additionally, clients can request additional ring pairs to
38517885a7bSLuigi Rizzo 	 * be used for internal communication.
38617885a7bSLuigi Rizzo 	 */
38717885a7bSLuigi Rizzo 	const uint32_t	ni_tx_rings;	/* number of HW tx rings */
38817885a7bSLuigi Rizzo 	const uint32_t	ni_rx_rings;	/* number of HW rx rings */
38917885a7bSLuigi Rizzo 
390f0ea3689SLuigi Rizzo 	uint32_t	ni_bufs_head;	/* head index for extra bufs */
391d12354a5SVincenzo Maffione 	const uint32_t	ni_host_tx_rings; /* number of SW tx rings */
392d12354a5SVincenzo Maffione 	const uint32_t	ni_host_rx_rings; /* number of SW rx rings */
393d12354a5SVincenzo Maffione 	uint32_t	ni_spare1[3];
39468b8534bSLuigi Rizzo 	/*
39564ae02c3SLuigi Rizzo 	 * The following array contains the offset of each netmap ring
39617885a7bSLuigi Rizzo 	 * from this structure, in the following order:
397d12354a5SVincenzo Maffione 	 *     - NIC tx rings (ni_tx_rings);
398d12354a5SVincenzo Maffione 	 *     - host tx rings (ni_host_tx_rings);
399d12354a5SVincenzo Maffione 	 *     - NIC rx rings (ni_rx_rings);
400d12354a5SVincenzo Maffione 	 *     - host rx ring (ni_host_rx_rings);
40117885a7bSLuigi Rizzo 	 *
402d12354a5SVincenzo Maffione 	 * The area is filled up by the kernel on NETMAP_REQ_REGISTER,
40368b8534bSLuigi Rizzo 	 * and then only read by userspace code.
40468b8534bSLuigi Rizzo 	 */
40568b8534bSLuigi Rizzo 	const ssize_t	ring_ofs[0];
40668b8534bSLuigi Rizzo };
40768b8534bSLuigi Rizzo 
4082ff91c17SVincenzo Maffione /* Legacy interface to interact with a netmap control device.
4092ff91c17SVincenzo Maffione  * Included for backward compatibility. The user should not include this
4102ff91c17SVincenzo Maffione  * file directly. */
4112ff91c17SVincenzo Maffione #include "netmap_legacy.h"
41217885a7bSLuigi Rizzo 
41368b8534bSLuigi Rizzo /*
4142ff91c17SVincenzo Maffione  * New API to control netmap control devices. New applications should only use
4152ff91c17SVincenzo Maffione  * nmreq_xyz structs with the NIOCCTRL ioctl() command.
41668b8534bSLuigi Rizzo  *
4172ff91c17SVincenzo Maffione  * NIOCCTRL takes a nmreq_header struct, which contains the required
4182ff91c17SVincenzo Maffione  * API version, the name of a netmap port, a command type, and pointers
4192ff91c17SVincenzo Maffione  * to request body and options.
420f18be576SLuigi Rizzo  *
42117885a7bSLuigi Rizzo  *	nr_name	(in)
4222ff91c17SVincenzo Maffione  *		The name of the port (em0, valeXXX:YYY, eth0{pn1 etc.)
423ce3ee1e7SLuigi Rizzo  *
42417885a7bSLuigi Rizzo  *	nr_version (in/out)
42517885a7bSLuigi Rizzo  *		Must match NETMAP_API as used in the kernel, error otherwise.
42617885a7bSLuigi Rizzo  *		Always returns the desired value on output.
42717885a7bSLuigi Rizzo  *
4282ff91c17SVincenzo Maffione  *	nr_reqtype (in)
4292ff91c17SVincenzo Maffione  *		One of the NETMAP_REQ_* command types below
4302ff91c17SVincenzo Maffione  *
4312ff91c17SVincenzo Maffione  *	nr_body (in)
4322ff91c17SVincenzo Maffione  *		Pointer to a command-specific struct, described by one
4332ff91c17SVincenzo Maffione  *		of the struct nmreq_xyz below.
4342ff91c17SVincenzo Maffione  *
4352ff91c17SVincenzo Maffione  *	nr_options (in)
4362ff91c17SVincenzo Maffione  *		Command specific options, if any.
4372ff91c17SVincenzo Maffione  *
4382ff91c17SVincenzo Maffione  * A NETMAP_REQ_REGISTER command activates netmap mode on the netmap
4392ff91c17SVincenzo Maffione  * port (e.g. physical interface) specified by nmreq_header.nr_name.
4402ff91c17SVincenzo Maffione  * The request body (struct nmreq_register) has several arguments to
4412ff91c17SVincenzo Maffione  * specify how the port is to be registered.
4422ff91c17SVincenzo Maffione  *
443d12354a5SVincenzo Maffione  *	nr_tx_slots, nr_tx_slots, nr_tx_rings, nr_rx_rings,
444d12354a5SVincenzo Maffione  *	nr_host_tx_rings, nr_host_rx_rings (in/out)
44517885a7bSLuigi Rizzo  *		On input, non-zero values may be used to reconfigure the port
44617885a7bSLuigi Rizzo  *		according to the requested values, but this is not guaranteed.
44717885a7bSLuigi Rizzo  *		On output the actual values in use are reported.
44817885a7bSLuigi Rizzo  *
4492ff91c17SVincenzo Maffione  *	nr_mode (in)
4502ff91c17SVincenzo Maffione  *		Indicate what set of rings must be bound to the netmap
4512ff91c17SVincenzo Maffione  *		device (e.g. all NIC rings, host rings only, NIC and
4522ff91c17SVincenzo Maffione  *		host rings, ...). Values are in NR_REG_*.
4532ff91c17SVincenzo Maffione  *
45417885a7bSLuigi Rizzo  *	nr_ringid (in)
4552ff91c17SVincenzo Maffione  *		If nr_mode == NR_REG_ONE_NIC (only a single couple of TX/RX
4562ff91c17SVincenzo Maffione  *		rings), indicate which NIC TX and/or RX ring is to be bound
4572ff91c17SVincenzo Maffione  *		(0..nr_*x_rings-1).
458f0ea3689SLuigi Rizzo  *
4592ff91c17SVincenzo Maffione  *	nr_flags (in)
4602ff91c17SVincenzo Maffione  *		Indicate special options for how to open the port.
461ce3ee1e7SLuigi Rizzo  *
4622ff91c17SVincenzo Maffione  *		NR_NO_TX_POLL can be OR-ed to make select()/poll() push
46317885a7bSLuigi Rizzo  *			packets on tx rings only if POLLOUT is set.
46417885a7bSLuigi Rizzo  *			The default is to push any pending packet.
465ce3ee1e7SLuigi Rizzo  *
4662ff91c17SVincenzo Maffione  *		NR_DO_RX_POLL can be OR-ed to make select()/poll() release
467f0ea3689SLuigi Rizzo  *			packets on rx rings also when POLLIN is NOT set.
468f0ea3689SLuigi Rizzo  *			The default is to touch the rx ring only with POLLIN.
469f0ea3689SLuigi Rizzo  *			Note that this is the opposite of TX because it
470f0ea3689SLuigi Rizzo  *			reflects the common usage.
471f0ea3689SLuigi Rizzo  *
4722ff91c17SVincenzo Maffione  *		Other options are NR_MONITOR_TX, NR_MONITOR_RX, NR_ZCOPY_MON,
4732ff91c17SVincenzo Maffione  *		NR_EXCLUSIVE, NR_RX_RINGS_ONLY, NR_TX_RINGS_ONLY and
4742ff91c17SVincenzo Maffione  *		NR_ACCEPT_VNET_HDR.
475f0ea3689SLuigi Rizzo  *
4762ff91c17SVincenzo Maffione  *	nr_mem_id (in/out)
4772ff91c17SVincenzo Maffione  *		The identity of the memory region used.
478f0ea3689SLuigi Rizzo  *		On input, 0 means the system decides autonomously,
479f0ea3689SLuigi Rizzo  *		other values may try to select a specific region.
480f0ea3689SLuigi Rizzo  *		On return the actual value is reported.
481f0ea3689SLuigi Rizzo  *		Region '1' is the global allocator, normally shared
482f0ea3689SLuigi Rizzo  *		by all interfaces. Other values are private regions.
483f0ea3689SLuigi Rizzo  *		If two ports the same region zero-copy is possible.
484f0ea3689SLuigi Rizzo  *
4852ff91c17SVincenzo Maffione  *	nr_extra_bufs (in/out)
4862ff91c17SVincenzo Maffione  *		Number of extra buffers to be allocated.
487f0ea3689SLuigi Rizzo  *
4882ff91c17SVincenzo Maffione  * The other NETMAP_REQ_* commands are described below.
48917885a7bSLuigi Rizzo  *
49068b8534bSLuigi Rizzo  */
49168b8534bSLuigi Rizzo 
4922ff91c17SVincenzo Maffione /* maximum size of a request, including all options */
4932ff91c17SVincenzo Maffione #define NETMAP_REQ_MAXSIZE	4096
4942ff91c17SVincenzo Maffione 
4952ff91c17SVincenzo Maffione /* Header common to all request options. */
4962ff91c17SVincenzo Maffione struct nmreq_option {
497*45c67e8fSVincenzo Maffione 	/* Pointer to the next option. */
4982ff91c17SVincenzo Maffione 	uint64_t		nro_next;
4992ff91c17SVincenzo Maffione 	/* Option type. */
5002ff91c17SVincenzo Maffione 	uint32_t		nro_reqtype;
5012ff91c17SVincenzo Maffione 	/* (out) status of the option:
5022ff91c17SVincenzo Maffione 	 * 0: recognized and processed
5032ff91c17SVincenzo Maffione 	 * !=0: errno value
5042ff91c17SVincenzo Maffione 	 */
5052ff91c17SVincenzo Maffione 	uint32_t		nro_status;
506b6e66be2SVincenzo Maffione 	/* Option size, used only for options that can have variable size
507b6e66be2SVincenzo Maffione 	 * (e.g. because they contain arrays). For fixed-size options this
508b6e66be2SVincenzo Maffione 	 * field should be set to zero. */
509b6e66be2SVincenzo Maffione 	uint64_t		nro_size;
5102ff91c17SVincenzo Maffione };
5112ff91c17SVincenzo Maffione 
5122ff91c17SVincenzo Maffione /* Header common to all requests. Do not reorder these fields, as we need
5132ff91c17SVincenzo Maffione  * the second one (nr_reqtype) to know how much to copy from/to userspace. */
5142ff91c17SVincenzo Maffione struct nmreq_header {
5152ff91c17SVincenzo Maffione 	uint16_t		nr_version;	/* API version */
5162ff91c17SVincenzo Maffione 	uint16_t		nr_reqtype;	/* nmreq type (NETMAP_REQ_*) */
5172ff91c17SVincenzo Maffione 	uint32_t		nr_reserved;	/* must be zero */
5182ff91c17SVincenzo Maffione #define NETMAP_REQ_IFNAMSIZ	64
5192ff91c17SVincenzo Maffione 	char			nr_name[NETMAP_REQ_IFNAMSIZ]; /* port name */
5202ff91c17SVincenzo Maffione 	uint64_t		nr_options;	/* command-specific options */
5212ff91c17SVincenzo Maffione 	uint64_t		nr_body;	/* ptr to nmreq_xyz struct */
5222ff91c17SVincenzo Maffione };
5232ff91c17SVincenzo Maffione 
5242ff91c17SVincenzo Maffione enum {
5252ff91c17SVincenzo Maffione 	/* Register a netmap port with the device. */
5262ff91c17SVincenzo Maffione 	NETMAP_REQ_REGISTER = 1,
5272ff91c17SVincenzo Maffione 	/* Get information from a netmap port. */
5282ff91c17SVincenzo Maffione 	NETMAP_REQ_PORT_INFO_GET,
5292ff91c17SVincenzo Maffione 	/* Attach a netmap port to a VALE switch. */
5302ff91c17SVincenzo Maffione 	NETMAP_REQ_VALE_ATTACH,
5312ff91c17SVincenzo Maffione 	/* Detach a netmap port from a VALE switch. */
5322ff91c17SVincenzo Maffione 	NETMAP_REQ_VALE_DETACH,
5332ff91c17SVincenzo Maffione 	/* List the ports attached to a VALE switch. */
5342ff91c17SVincenzo Maffione 	NETMAP_REQ_VALE_LIST,
5352ff91c17SVincenzo Maffione 	/* Set the port header length (was virtio-net header length). */
5362ff91c17SVincenzo Maffione 	NETMAP_REQ_PORT_HDR_SET,
5372ff91c17SVincenzo Maffione 	/* Get the port header length (was virtio-net header length). */
5382ff91c17SVincenzo Maffione 	NETMAP_REQ_PORT_HDR_GET,
5392ff91c17SVincenzo Maffione 	/* Create a new persistent VALE port. */
5402ff91c17SVincenzo Maffione 	NETMAP_REQ_VALE_NEWIF,
5412ff91c17SVincenzo Maffione 	/* Delete a persistent VALE port. */
5422ff91c17SVincenzo Maffione 	NETMAP_REQ_VALE_DELIF,
5432ff91c17SVincenzo Maffione 	/* Enable polling kernel thread(s) on an attached VALE port. */
5442ff91c17SVincenzo Maffione 	NETMAP_REQ_VALE_POLLING_ENABLE,
5452ff91c17SVincenzo Maffione 	/* Disable polling kernel thread(s) on an attached VALE port. */
5462ff91c17SVincenzo Maffione 	NETMAP_REQ_VALE_POLLING_DISABLE,
5472ff91c17SVincenzo Maffione 	/* Get info about the pools of a memory allocator. */
5482ff91c17SVincenzo Maffione 	NETMAP_REQ_POOLS_INFO_GET,
549b6e66be2SVincenzo Maffione 	/* Start an in-kernel loop that syncs the rings periodically or
550b6e66be2SVincenzo Maffione 	 * on notifications. The loop runs in the context of the ioctl
551b6e66be2SVincenzo Maffione 	 * syscall, and only stops on NETMAP_REQ_SYNC_KLOOP_STOP. */
552b6e66be2SVincenzo Maffione 	NETMAP_REQ_SYNC_KLOOP_START,
553b6e66be2SVincenzo Maffione 	/* Stops the thread executing the in-kernel loop. The thread
554b6e66be2SVincenzo Maffione 	 * returns from the ioctl syscall. */
555b6e66be2SVincenzo Maffione 	NETMAP_REQ_SYNC_KLOOP_STOP,
556b6e66be2SVincenzo Maffione 	/* Enable CSB mode on a registered netmap control device. */
557b6e66be2SVincenzo Maffione 	NETMAP_REQ_CSB_ENABLE,
5582ff91c17SVincenzo Maffione };
5592ff91c17SVincenzo Maffione 
5602ff91c17SVincenzo Maffione enum {
5612ff91c17SVincenzo Maffione 	/* On NETMAP_REQ_REGISTER, ask netmap to use memory allocated
5625faab778SVincenzo Maffione 	 * from user-space allocated memory pools (e.g. hugepages).
5635faab778SVincenzo Maffione 	 */
5642ff91c17SVincenzo Maffione 	NETMAP_REQ_OPT_EXTMEM = 1,
565b6e66be2SVincenzo Maffione 
566b6e66be2SVincenzo Maffione 	/* ON NETMAP_REQ_SYNC_KLOOP_START, ask netmap to use eventfd-based
567b6e66be2SVincenzo Maffione 	 * notifications to synchronize the kernel loop with the application.
568b6e66be2SVincenzo Maffione 	 */
569b6e66be2SVincenzo Maffione 	NETMAP_REQ_OPT_SYNC_KLOOP_EVENTFDS,
570b6e66be2SVincenzo Maffione 
571b6e66be2SVincenzo Maffione 	/* On NETMAP_REQ_REGISTER, ask netmap to work in CSB mode, where
572b6e66be2SVincenzo Maffione 	 * head, cur and tail pointers are not exchanged through the
573b6e66be2SVincenzo Maffione 	 * struct netmap_ring header, but rather using an user-provided
5745faab778SVincenzo Maffione 	 * memory area (see struct nm_csb_atok and struct nm_csb_ktoa).
5755faab778SVincenzo Maffione 	 */
576b6e66be2SVincenzo Maffione 	NETMAP_REQ_OPT_CSB,
5775faab778SVincenzo Maffione 
5785faab778SVincenzo Maffione 	/* An extension to NETMAP_REQ_OPT_SYNC_KLOOP_EVENTFDS, which specifies
5795faab778SVincenzo Maffione 	 * if the TX and/or RX rings are synced in the context of the VM exit.
5805faab778SVincenzo Maffione 	 * This requires the 'ioeventfd' fields to be valid (cannot be < 0).
5815faab778SVincenzo Maffione 	 */
5825faab778SVincenzo Maffione 	NETMAP_REQ_OPT_SYNC_KLOOP_MODE,
583253b2ec1SVincenzo Maffione 
584a6d768d8SVincenzo Maffione 	/* On NETMAP_REQ_REGISTER, ask for (part of) the ptr field in the
585a6d768d8SVincenzo Maffione 	 * slots of the registered rings to be used as an offset field
586a6d768d8SVincenzo Maffione 	 * for the start of the packets inside the netmap buffer.
587a6d768d8SVincenzo Maffione 	 */
588a6d768d8SVincenzo Maffione 	NETMAP_REQ_OPT_OFFSETS,
589a6d768d8SVincenzo Maffione 
590253b2ec1SVincenzo Maffione 	/* This is a marker to count the number of available options.
591253b2ec1SVincenzo Maffione 	 * New options must be added above it. */
592253b2ec1SVincenzo Maffione 	NETMAP_REQ_OPT_MAX,
5932ff91c17SVincenzo Maffione };
59417885a7bSLuigi Rizzo 
59568b8534bSLuigi Rizzo /*
5962ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_REGISTER
5972ff91c17SVincenzo Maffione  * Bind (register) a netmap port to this control device.
59868b8534bSLuigi Rizzo  */
5992ff91c17SVincenzo Maffione struct nmreq_register {
6002ff91c17SVincenzo Maffione 	uint64_t	nr_offset;	/* nifp offset in the shared region */
6012ff91c17SVincenzo Maffione 	uint64_t	nr_memsize;	/* size of the shared region */
60264ae02c3SLuigi Rizzo 	uint32_t	nr_tx_slots;	/* slots in tx rings */
60364ae02c3SLuigi Rizzo 	uint32_t	nr_rx_slots;	/* slots in rx rings */
60464ae02c3SLuigi Rizzo 	uint16_t	nr_tx_rings;	/* number of tx rings */
60564ae02c3SLuigi Rizzo 	uint16_t	nr_rx_rings;	/* number of rx rings */
606d12354a5SVincenzo Maffione 	uint16_t	nr_host_tx_rings; /* number of host tx rings */
607d12354a5SVincenzo Maffione 	uint16_t	nr_host_rx_rings; /* number of host rx rings */
608f0ea3689SLuigi Rizzo 
6092ff91c17SVincenzo Maffione 	uint16_t	nr_mem_id;	/* id of the memory allocator */
61068b8534bSLuigi Rizzo 	uint16_t	nr_ringid;	/* ring(s) we care about */
6112ff91c17SVincenzo Maffione 	uint32_t	nr_mode;	/* specify NR_REG_* modes */
612b6e66be2SVincenzo Maffione 	uint32_t	nr_extra_bufs;	/* number of requested extra buffers */
613f0ea3689SLuigi Rizzo 
6142ff91c17SVincenzo Maffione 	uint64_t	nr_flags;	/* additional flags (see below) */
6152ff91c17SVincenzo Maffione /* monitors use nr_ringid and nr_mode to select the rings to monitor */
616f0ea3689SLuigi Rizzo #define NR_MONITOR_TX	0x100
617f0ea3689SLuigi Rizzo #define NR_MONITOR_RX	0x200
618847bf383SLuigi Rizzo #define NR_ZCOPY_MON	0x400
619847bf383SLuigi Rizzo /* request exclusive access to the selected rings */
620847bf383SLuigi Rizzo #define NR_EXCLUSIVE	0x800
621b6e66be2SVincenzo Maffione /* 0x1000 unused */
62237e3a6d3SLuigi Rizzo #define NR_RX_RINGS_ONLY	0x2000
62337e3a6d3SLuigi Rizzo #define NR_TX_RINGS_ONLY	0x4000
62437e3a6d3SLuigi Rizzo /* Applications set this flag if they are able to deal with virtio-net headers,
62537e3a6d3SLuigi Rizzo  * that is send/receive frames that start with a virtio-net header.
626d12354a5SVincenzo Maffione  * If not set, NETMAP_REQ_REGISTER will fail with netmap ports that require
627d12354a5SVincenzo Maffione  * applications to use those headers. If the flag is set, the application can
628d12354a5SVincenzo Maffione  * use the NETMAP_VNET_HDR_GET command to figure out the header length. */
62937e3a6d3SLuigi Rizzo #define NR_ACCEPT_VNET_HDR	0x8000
6302ff91c17SVincenzo Maffione /* The following two have the same meaning of NETMAP_NO_TX_POLL and
6312ff91c17SVincenzo Maffione  * NETMAP_DO_RX_POLL. */
6322ff91c17SVincenzo Maffione #define NR_DO_RX_POLL		0x10000
6332ff91c17SVincenzo Maffione #define NR_NO_TX_POLL		0x20000
6342ff91c17SVincenzo Maffione };
6352ff91c17SVincenzo Maffione 
6362ff91c17SVincenzo Maffione /* Valid values for nmreq_register.nr_mode (see above). */
6372ff91c17SVincenzo Maffione enum {	NR_REG_DEFAULT	= 0,	/* backward compat, should not be used. */
6382ff91c17SVincenzo Maffione 	NR_REG_ALL_NIC	= 1,
6392ff91c17SVincenzo Maffione 	NR_REG_SW	= 2,
6402ff91c17SVincenzo Maffione 	NR_REG_NIC_SW	= 3,
6412ff91c17SVincenzo Maffione 	NR_REG_ONE_NIC	= 4,
6422ff91c17SVincenzo Maffione 	NR_REG_PIPE_MASTER = 5, /* deprecated, use "x{y" port name syntax */
6432ff91c17SVincenzo Maffione 	NR_REG_PIPE_SLAVE = 6,  /* deprecated, use "x}y" port name syntax */
644b6e66be2SVincenzo Maffione 	NR_REG_NULL     = 7,
645d12354a5SVincenzo Maffione 	NR_REG_ONE_SW	= 8,
6462ff91c17SVincenzo Maffione };
6472ff91c17SVincenzo Maffione 
6482ff91c17SVincenzo Maffione /* A single ioctl number is shared by all the new API command.
649b6e66be2SVincenzo Maffione  * Demultiplexing is done using the hdr.nr_reqtype field.
6502ff91c17SVincenzo Maffione  * FreeBSD uses the size value embedded in the _IOWR to determine
6512ff91c17SVincenzo Maffione  * how much to copy in/out, so we define the ioctl() command
6522ff91c17SVincenzo Maffione  * specifying only nmreq_header, and copyin/copyout the rest. */
6532ff91c17SVincenzo Maffione #define NIOCCTRL	_IOWR('i', 151, struct nmreq_header)
6542ff91c17SVincenzo Maffione 
6552ff91c17SVincenzo Maffione /* The ioctl commands to sync TX/RX netmap rings.
6562ff91c17SVincenzo Maffione  * NIOCTXSYNC, NIOCRXSYNC synchronize tx or rx queues,
657d12354a5SVincenzo Maffione  *	whose identity is set in NETMAP_REQ_REGISTER through nr_ringid.
6582ff91c17SVincenzo Maffione  *	These are non blocking and take no argument. */
6592ff91c17SVincenzo Maffione #define NIOCTXSYNC	_IO('i', 148) /* sync tx queues */
6602ff91c17SVincenzo Maffione #define NIOCRXSYNC	_IO('i', 149) /* sync rx queues */
6612ff91c17SVincenzo Maffione 
6622ff91c17SVincenzo Maffione /*
6632ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_PORT_INFO_GET
6642ff91c17SVincenzo Maffione  * Get information about a netmap port, including number of rings.
665b6e66be2SVincenzo Maffione  * slots per ring, id of the memory allocator, etc. The netmap
666b6e66be2SVincenzo Maffione  * control device used for this operation does not need to be bound
667b6e66be2SVincenzo Maffione  * to a netmap port.
6682ff91c17SVincenzo Maffione  */
6692ff91c17SVincenzo Maffione struct nmreq_port_info_get {
6702ff91c17SVincenzo Maffione 	uint64_t	nr_memsize;	/* size of the shared region */
6712ff91c17SVincenzo Maffione 	uint32_t	nr_tx_slots;	/* slots in tx rings */
6722ff91c17SVincenzo Maffione 	uint32_t	nr_rx_slots;	/* slots in rx rings */
6732ff91c17SVincenzo Maffione 	uint16_t	nr_tx_rings;	/* number of tx rings */
6742ff91c17SVincenzo Maffione 	uint16_t	nr_rx_rings;	/* number of rx rings */
675d12354a5SVincenzo Maffione 	uint16_t	nr_host_tx_rings; /* number of host tx rings */
676d12354a5SVincenzo Maffione 	uint16_t	nr_host_rx_rings; /* number of host rx rings */
677b6e66be2SVincenzo Maffione 	uint16_t	nr_mem_id;	/* memory allocator id (in/out) */
678d12354a5SVincenzo Maffione 	uint16_t	pad[3];
6792ff91c17SVincenzo Maffione };
680f0ea3689SLuigi Rizzo 
68137e3a6d3SLuigi Rizzo #define	NM_BDG_NAME		"vale"	/* prefix for bridge port name */
68237e3a6d3SLuigi Rizzo 
68337e3a6d3SLuigi Rizzo /*
6842ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_VALE_ATTACH
6852ff91c17SVincenzo Maffione  * Attach a netmap port to a VALE switch. Both the name of the netmap
6862ff91c17SVincenzo Maffione  * port and the VALE switch are specified through the nr_name argument.
6872ff91c17SVincenzo Maffione  * The attach operation could need to register a port, so at least
6882ff91c17SVincenzo Maffione  * the same arguments are available.
6892ff91c17SVincenzo Maffione  * port_index will contain the index where the port has been attached.
69037e3a6d3SLuigi Rizzo  */
6912ff91c17SVincenzo Maffione struct nmreq_vale_attach {
6922ff91c17SVincenzo Maffione 	struct nmreq_register reg;
6932ff91c17SVincenzo Maffione 	uint32_t port_index;
694b6e66be2SVincenzo Maffione 	uint32_t pad1;
6952ff91c17SVincenzo Maffione };
69617885a7bSLuigi Rizzo 
69764ae02c3SLuigi Rizzo /*
6982ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_VALE_DETACH
6992ff91c17SVincenzo Maffione  * Detach a netmap port from a VALE switch. Both the name of the netmap
7002ff91c17SVincenzo Maffione  * port and the VALE switch are specified through the nr_name argument.
7012ff91c17SVincenzo Maffione  * port_index will contain the index where the port was attached.
70264ae02c3SLuigi Rizzo  */
7032ff91c17SVincenzo Maffione struct nmreq_vale_detach {
7042ff91c17SVincenzo Maffione 	uint32_t port_index;
705b6e66be2SVincenzo Maffione 	uint32_t pad1;
7062ff91c17SVincenzo Maffione };
70717885a7bSLuigi Rizzo 
70817885a7bSLuigi Rizzo /*
7092ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_VALE_LIST
7102ff91c17SVincenzo Maffione  * List the ports of a VALE switch.
71117885a7bSLuigi Rizzo  */
7122ff91c17SVincenzo Maffione struct nmreq_vale_list {
7132ff91c17SVincenzo Maffione 	/* Name of the VALE port (valeXXX:YYY) or empty. */
7142ff91c17SVincenzo Maffione 	uint16_t	nr_bridge_idx;
715b6e66be2SVincenzo Maffione 	uint16_t	pad1;
7162ff91c17SVincenzo Maffione 	uint32_t	nr_port_idx;
7172ff91c17SVincenzo Maffione };
71817885a7bSLuigi Rizzo 
71917885a7bSLuigi Rizzo /*
7202ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_PORT_HDR_SET or NETMAP_REQ_PORT_HDR_GET
721b6e66be2SVincenzo Maffione  * Set or get the port header length of the port identified by hdr.nr_name.
722b6e66be2SVincenzo Maffione  * The control device does not need to be bound to a netmap port.
72317885a7bSLuigi Rizzo  */
7242ff91c17SVincenzo Maffione struct nmreq_port_hdr {
7252ff91c17SVincenzo Maffione 	uint32_t	nr_hdr_len;
726b6e66be2SVincenzo Maffione 	uint32_t	pad1;
7272ff91c17SVincenzo Maffione };
72817885a7bSLuigi Rizzo 
7294bf50f18SLuigi Rizzo /*
7302ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_VALE_NEWIF
7312ff91c17SVincenzo Maffione  * Create a new persistent VALE port.
7324bf50f18SLuigi Rizzo  */
7332ff91c17SVincenzo Maffione struct nmreq_vale_newif {
7342ff91c17SVincenzo Maffione 	uint32_t	nr_tx_slots;	/* slots in tx rings */
7352ff91c17SVincenzo Maffione 	uint32_t	nr_rx_slots;	/* slots in rx rings */
7362ff91c17SVincenzo Maffione 	uint16_t	nr_tx_rings;	/* number of tx rings */
7372ff91c17SVincenzo Maffione 	uint16_t	nr_rx_rings;	/* number of rx rings */
7382ff91c17SVincenzo Maffione 	uint16_t	nr_mem_id;	/* id of the memory allocator */
739b6e66be2SVincenzo Maffione 	uint16_t	pad1;
7402ff91c17SVincenzo Maffione };
7412ff91c17SVincenzo Maffione 
7422ff91c17SVincenzo Maffione /*
7432ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_VALE_POLLING_ENABLE or NETMAP_REQ_VALE_POLLING_DISABLE
7442ff91c17SVincenzo Maffione  * Enable or disable polling kthreads on a VALE port.
7452ff91c17SVincenzo Maffione  */
7462ff91c17SVincenzo Maffione struct nmreq_vale_polling {
7472ff91c17SVincenzo Maffione 	uint32_t	nr_mode;
7482ff91c17SVincenzo Maffione #define NETMAP_POLLING_MODE_SINGLE_CPU 1
7492ff91c17SVincenzo Maffione #define NETMAP_POLLING_MODE_MULTI_CPU 2
7502ff91c17SVincenzo Maffione 	uint32_t	nr_first_cpu_id;
7512ff91c17SVincenzo Maffione 	uint32_t	nr_num_polling_cpus;
752b6e66be2SVincenzo Maffione 	uint32_t	pad1;
7532ff91c17SVincenzo Maffione };
7542ff91c17SVincenzo Maffione 
7552ff91c17SVincenzo Maffione /*
7562ff91c17SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_POOLS_INFO_GET
757b6e66be2SVincenzo Maffione  * Get info about the pools of the memory allocator of the netmap
758b6e66be2SVincenzo Maffione  * port specified by hdr.nr_name and nr_mem_id. The netmap control
759b6e66be2SVincenzo Maffione  * device used for this operation does not need to be bound to a netmap
760b6e66be2SVincenzo Maffione  * port.
7612ff91c17SVincenzo Maffione  */
7622ff91c17SVincenzo Maffione struct nmreq_pools_info {
7632ff91c17SVincenzo Maffione 	uint64_t	nr_memsize;
764b6e66be2SVincenzo Maffione 	uint16_t	nr_mem_id; /* in/out argument */
765b6e66be2SVincenzo Maffione 	uint16_t	pad1[3];
7662ff91c17SVincenzo Maffione 	uint64_t	nr_if_pool_offset;
7672ff91c17SVincenzo Maffione 	uint32_t	nr_if_pool_objtotal;
7682ff91c17SVincenzo Maffione 	uint32_t	nr_if_pool_objsize;
7692ff91c17SVincenzo Maffione 	uint64_t	nr_ring_pool_offset;
7702ff91c17SVincenzo Maffione 	uint32_t	nr_ring_pool_objtotal;
7712ff91c17SVincenzo Maffione 	uint32_t	nr_ring_pool_objsize;
7722ff91c17SVincenzo Maffione 	uint64_t	nr_buf_pool_offset;
7732ff91c17SVincenzo Maffione 	uint32_t	nr_buf_pool_objtotal;
7742ff91c17SVincenzo Maffione 	uint32_t	nr_buf_pool_objsize;
7752ff91c17SVincenzo Maffione };
7762ff91c17SVincenzo Maffione 
7772ff91c17SVincenzo Maffione /*
778b6e66be2SVincenzo Maffione  * nr_reqtype: NETMAP_REQ_SYNC_KLOOP_START
779b6e66be2SVincenzo Maffione  * Start an in-kernel loop that syncs the rings periodically or on
780b6e66be2SVincenzo Maffione  * notifications. The loop runs in the context of the ioctl syscall,
781b6e66be2SVincenzo Maffione  * and only stops on NETMAP_REQ_SYNC_KLOOP_STOP.
782b6e66be2SVincenzo Maffione  * The registered netmap port must be open in CSB mode.
783b6e66be2SVincenzo Maffione  */
784b6e66be2SVincenzo Maffione struct nmreq_sync_kloop_start {
785b6e66be2SVincenzo Maffione 	/* Sleeping is the default synchronization method for the kloop.
786b6e66be2SVincenzo Maffione 	 * The 'sleep_us' field specifies how many microsconds to sleep for
787b6e66be2SVincenzo Maffione 	 * when there is no work to do, before doing another kloop iteration.
788b6e66be2SVincenzo Maffione 	 */
789b6e66be2SVincenzo Maffione 	uint32_t	sleep_us;
790b6e66be2SVincenzo Maffione 	uint32_t	pad1;
791b6e66be2SVincenzo Maffione };
792b6e66be2SVincenzo Maffione 
793b6e66be2SVincenzo Maffione /* A CSB entry for the application --> kernel direction. */
794b6e66be2SVincenzo Maffione struct nm_csb_atok {
795b6e66be2SVincenzo Maffione 	uint32_t head;		  /* AW+ KR+ the head of the appl netmap_ring */
796b6e66be2SVincenzo Maffione 	uint32_t cur;		  /* AW+ KR+ the cur of the appl netmap_ring */
797b6e66be2SVincenzo Maffione 	uint32_t appl_need_kick;  /* AW+ KR+ kern --> appl notification enable */
798b6e66be2SVincenzo Maffione 	uint32_t sync_flags;	  /* AW+ KR+ the flags of the appl [tx|rx]sync() */
799b6e66be2SVincenzo Maffione 	uint32_t pad[12];	  /* pad to a 64 bytes cacheline */
800b6e66be2SVincenzo Maffione };
801b6e66be2SVincenzo Maffione 
802b6e66be2SVincenzo Maffione /* A CSB entry for the application <-- kernel direction. */
803b6e66be2SVincenzo Maffione struct nm_csb_ktoa {
804b6e66be2SVincenzo Maffione 	uint32_t hwcur;		  /* AR+ KW+ the hwcur of the kern netmap_kring */
805b6e66be2SVincenzo Maffione 	uint32_t hwtail;	  /* AR+ KW+ the hwtail of the kern netmap_kring */
806b6e66be2SVincenzo Maffione 	uint32_t kern_need_kick;  /* AR+ KW+ appl-->kern notification enable */
807b6e66be2SVincenzo Maffione 	uint32_t pad[13];
808b6e66be2SVincenzo Maffione };
809b6e66be2SVincenzo Maffione 
810b6e66be2SVincenzo Maffione #ifdef __linux__
811b6e66be2SVincenzo Maffione 
812b6e66be2SVincenzo Maffione #ifdef __KERNEL__
813b6e66be2SVincenzo Maffione #define nm_stst_barrier smp_wmb
814f79ba6d7SVincenzo Maffione #define nm_ldld_barrier smp_rmb
815f79ba6d7SVincenzo Maffione #define nm_stld_barrier smp_mb
816b6e66be2SVincenzo Maffione #else  /* !__KERNEL__ */
817b6e66be2SVincenzo Maffione static inline void nm_stst_barrier(void)
818b6e66be2SVincenzo Maffione {
819b6e66be2SVincenzo Maffione 	/* A memory barrier with release semantic has the combined
820b6e66be2SVincenzo Maffione 	 * effect of a store-store barrier and a load-store barrier,
821b6e66be2SVincenzo Maffione 	 * which is fine for us. */
822b6e66be2SVincenzo Maffione 	__atomic_thread_fence(__ATOMIC_RELEASE);
823b6e66be2SVincenzo Maffione }
824f79ba6d7SVincenzo Maffione static inline void nm_ldld_barrier(void)
825f79ba6d7SVincenzo Maffione {
826f79ba6d7SVincenzo Maffione 	/* A memory barrier with acquire semantic has the combined
827f79ba6d7SVincenzo Maffione 	 * effect of a load-load barrier and a store-load barrier,
828f79ba6d7SVincenzo Maffione 	 * which is fine for us. */
829f79ba6d7SVincenzo Maffione 	__atomic_thread_fence(__ATOMIC_ACQUIRE);
830f79ba6d7SVincenzo Maffione }
831b6e66be2SVincenzo Maffione #endif /* !__KERNEL__ */
832b6e66be2SVincenzo Maffione 
833b6e66be2SVincenzo Maffione #elif defined(__FreeBSD__)
834b6e66be2SVincenzo Maffione 
835b6e66be2SVincenzo Maffione #ifdef _KERNEL
836b6e66be2SVincenzo Maffione #define nm_stst_barrier	atomic_thread_fence_rel
837f79ba6d7SVincenzo Maffione #define nm_ldld_barrier	atomic_thread_fence_acq
838f79ba6d7SVincenzo Maffione #define nm_stld_barrier	atomic_thread_fence_seq_cst
839b6e66be2SVincenzo Maffione #else  /* !_KERNEL */
840a6d768d8SVincenzo Maffione 
841a6d768d8SVincenzo Maffione #ifdef __cplusplus
842a6d768d8SVincenzo Maffione #include <atomic>
843a6d768d8SVincenzo Maffione using std::memory_order_release;
844a6d768d8SVincenzo Maffione using std::memory_order_acquire;
845a6d768d8SVincenzo Maffione 
846a6d768d8SVincenzo Maffione #else /* __cplusplus */
8471d9ec3eeSVincenzo Maffione #include <stdatomic.h>
848a6d768d8SVincenzo Maffione #endif /* __cplusplus */
849a6d768d8SVincenzo Maffione 
850b6e66be2SVincenzo Maffione static inline void nm_stst_barrier(void)
851b6e66be2SVincenzo Maffione {
8521d9ec3eeSVincenzo Maffione 	atomic_thread_fence(memory_order_release);
853b6e66be2SVincenzo Maffione }
854f79ba6d7SVincenzo Maffione static inline void nm_ldld_barrier(void)
855f79ba6d7SVincenzo Maffione {
856f79ba6d7SVincenzo Maffione 	atomic_thread_fence(memory_order_acquire);
857f79ba6d7SVincenzo Maffione }
858b6e66be2SVincenzo Maffione #endif /* !_KERNEL */
859b6e66be2SVincenzo Maffione 
860b6e66be2SVincenzo Maffione #else  /* !__linux__ && !__FreeBSD__ */
861b6e66be2SVincenzo Maffione #error "OS not supported"
862b6e66be2SVincenzo Maffione #endif /* !__linux__ && !__FreeBSD__ */
863b6e66be2SVincenzo Maffione 
864b6e66be2SVincenzo Maffione /* Application side of sync-kloop: Write ring pointers (cur, head) to the CSB.
865b6e66be2SVincenzo Maffione  * This routine is coupled with sync_kloop_kernel_read(). */
866b6e66be2SVincenzo Maffione static inline void
867b6e66be2SVincenzo Maffione nm_sync_kloop_appl_write(struct nm_csb_atok *atok, uint32_t cur,
868b6e66be2SVincenzo Maffione 			 uint32_t head)
869b6e66be2SVincenzo Maffione {
870f79ba6d7SVincenzo Maffione 	/* Issue a first store-store barrier to make sure writes to the
871f79ba6d7SVincenzo Maffione 	 * netmap ring do not overcome updates on atok->cur and atok->head. */
872f79ba6d7SVincenzo Maffione 	nm_stst_barrier();
873f79ba6d7SVincenzo Maffione 
874b6e66be2SVincenzo Maffione 	/*
875b6e66be2SVincenzo Maffione 	 * We need to write cur and head to the CSB but we cannot do it atomically.
876b6e66be2SVincenzo Maffione 	 * There is no way we can prevent the host from reading the updated value
877b6e66be2SVincenzo Maffione 	 * of one of the two and the old value of the other. However, if we make
878b6e66be2SVincenzo Maffione 	 * sure that the host never reads a value of head more recent than the
879b6e66be2SVincenzo Maffione 	 * value of cur we are safe. We can allow the host to read a value of cur
880b6e66be2SVincenzo Maffione 	 * more recent than the value of head, since in the netmap ring cur can be
881b6e66be2SVincenzo Maffione 	 * ahead of head and cur cannot wrap around head because it must be behind
882b6e66be2SVincenzo Maffione 	 * tail. Inverting the order of writes below could instead result into the
883b6e66be2SVincenzo Maffione 	 * host to think head went ahead of cur, which would cause the sync
884b6e66be2SVincenzo Maffione 	 * prologue to fail.
885b6e66be2SVincenzo Maffione 	 *
886b6e66be2SVincenzo Maffione 	 * The following memory barrier scheme is used to make this happen:
887b6e66be2SVincenzo Maffione 	 *
888b6e66be2SVincenzo Maffione 	 *          Guest                Host
889b6e66be2SVincenzo Maffione 	 *
890b6e66be2SVincenzo Maffione 	 *          STORE(cur)           LOAD(head)
891f79ba6d7SVincenzo Maffione 	 *          wmb() <----------->  rmb()
892b6e66be2SVincenzo Maffione 	 *          STORE(head)          LOAD(cur)
893b6e66be2SVincenzo Maffione 	 *
894b6e66be2SVincenzo Maffione 	 */
895b6e66be2SVincenzo Maffione 	atok->cur = cur;
896b6e66be2SVincenzo Maffione 	nm_stst_barrier();
897b6e66be2SVincenzo Maffione 	atok->head = head;
898b6e66be2SVincenzo Maffione }
899b6e66be2SVincenzo Maffione 
900b6e66be2SVincenzo Maffione /* Application side of sync-kloop: Read kring pointers (hwcur, hwtail) from
901b6e66be2SVincenzo Maffione  * the CSB. This routine is coupled with sync_kloop_kernel_write(). */
902b6e66be2SVincenzo Maffione static inline void
903b6e66be2SVincenzo Maffione nm_sync_kloop_appl_read(struct nm_csb_ktoa *ktoa, uint32_t *hwtail,
904b6e66be2SVincenzo Maffione 			uint32_t *hwcur)
905b6e66be2SVincenzo Maffione {
906b6e66be2SVincenzo Maffione 	/*
907b6e66be2SVincenzo Maffione 	 * We place a memory barrier to make sure that the update of hwtail never
908b6e66be2SVincenzo Maffione 	 * overtakes the update of hwcur.
909b6e66be2SVincenzo Maffione 	 * (see explanation in sync_kloop_kernel_write).
910b6e66be2SVincenzo Maffione 	 */
911b6e66be2SVincenzo Maffione 	*hwtail = ktoa->hwtail;
912f79ba6d7SVincenzo Maffione 	nm_ldld_barrier();
913b6e66be2SVincenzo Maffione 	*hwcur = ktoa->hwcur;
914f79ba6d7SVincenzo Maffione 
915f79ba6d7SVincenzo Maffione 	/* Make sure that loads from ktoa->hwtail and ktoa->hwcur are not delayed
916f79ba6d7SVincenzo Maffione 	 * after the loads from the netmap ring. */
917f79ba6d7SVincenzo Maffione 	nm_ldld_barrier();
918b6e66be2SVincenzo Maffione }
919b6e66be2SVincenzo Maffione 
920b6e66be2SVincenzo Maffione /*
9212ff91c17SVincenzo Maffione  * data for NETMAP_REQ_OPT_* options
9222ff91c17SVincenzo Maffione  */
9232ff91c17SVincenzo Maffione 
924b6e66be2SVincenzo Maffione struct nmreq_opt_sync_kloop_eventfds {
925b6e66be2SVincenzo Maffione 	struct nmreq_option	nro_opt;	/* common header */
926b6e66be2SVincenzo Maffione 	/* An array of N entries for bidirectional notifications between
927b6e66be2SVincenzo Maffione 	 * the kernel loop and the application. The number of entries and
928b6e66be2SVincenzo Maffione 	 * their order must agree with the CSB arrays passed in the
929b6e66be2SVincenzo Maffione 	 * NETMAP_REQ_OPT_CSB option. Each entry contains a file descriptor
930b6e66be2SVincenzo Maffione 	 * backed by an eventfd.
9315faab778SVincenzo Maffione 	 *
9325faab778SVincenzo Maffione 	 * If any of the 'ioeventfd' entries is < 0, the event loop uses
9335faab778SVincenzo Maffione 	 * the sleeping synchronization strategy (according to sleep_us),
9345faab778SVincenzo Maffione 	 * and keeps kern_need_kick always disabled.
9355faab778SVincenzo Maffione 	 * Each 'irqfd' can be < 0, and in that case the corresponding queue
9365faab778SVincenzo Maffione 	 * is never notified.
937b6e66be2SVincenzo Maffione 	 */
938b6e66be2SVincenzo Maffione 	struct {
939b6e66be2SVincenzo Maffione 		/* Notifier for the application --> kernel loop direction. */
940b6e66be2SVincenzo Maffione 		int32_t ioeventfd;
941b6e66be2SVincenzo Maffione 		/* Notifier for the kernel loop --> application direction. */
942b6e66be2SVincenzo Maffione 		int32_t irqfd;
943b6e66be2SVincenzo Maffione 	} eventfds[0];
944b6e66be2SVincenzo Maffione };
945b6e66be2SVincenzo Maffione 
9465faab778SVincenzo Maffione struct nmreq_opt_sync_kloop_mode {
9475faab778SVincenzo Maffione 	struct nmreq_option	nro_opt;	/* common header */
9485faab778SVincenzo Maffione #define NM_OPT_SYNC_KLOOP_DIRECT_TX (1 << 0)
9495faab778SVincenzo Maffione #define NM_OPT_SYNC_KLOOP_DIRECT_RX (1 << 1)
9505faab778SVincenzo Maffione 	uint32_t mode;
9515faab778SVincenzo Maffione };
9525faab778SVincenzo Maffione 
9532ff91c17SVincenzo Maffione struct nmreq_opt_extmem {
9542ff91c17SVincenzo Maffione 	struct nmreq_option	nro_opt;	/* common header */
9552ff91c17SVincenzo Maffione 	uint64_t		nro_usrptr;	/* (in) ptr to usr memory */
9562ff91c17SVincenzo Maffione 	struct nmreq_pools_info	nro_info;	/* (in/out) */
9574bf50f18SLuigi Rizzo };
9584bf50f18SLuigi Rizzo 
959b6e66be2SVincenzo Maffione struct nmreq_opt_csb {
960b6e66be2SVincenzo Maffione 	struct nmreq_option	nro_opt;
961b6e66be2SVincenzo Maffione 
962b6e66be2SVincenzo Maffione 	/* Array of CSB entries for application --> kernel communication
963b6e66be2SVincenzo Maffione 	 * (N entries). */
964b6e66be2SVincenzo Maffione 	uint64_t		csb_atok;
965b6e66be2SVincenzo Maffione 
966b6e66be2SVincenzo Maffione 	/* Array of CSB entries for kernel --> application communication
967b6e66be2SVincenzo Maffione 	 * (N entries). */
968b6e66be2SVincenzo Maffione 	uint64_t		csb_ktoa;
969b6e66be2SVincenzo Maffione };
970b6e66be2SVincenzo Maffione 
971a6d768d8SVincenzo Maffione /* option NETMAP_REQ_OPT_OFFSETS */
972a6d768d8SVincenzo Maffione struct nmreq_opt_offsets {
973a6d768d8SVincenzo Maffione 	struct nmreq_option	nro_opt;
974a6d768d8SVincenzo Maffione 	/* the user must declare the maximum offset value that she is
975a6d768d8SVincenzo Maffione 	 * going to put into the offset slot-fields. Any larger value
976a6d768d8SVincenzo Maffione 	 * found at runtime will be cropped. On output the (possibly
977a6d768d8SVincenzo Maffione 	 * higher) effective max value is returned.
978a6d768d8SVincenzo Maffione 	 */
979a6d768d8SVincenzo Maffione 	uint64_t		nro_max_offset;
980a6d768d8SVincenzo Maffione 	/* optional initial offset value, to be set in all slots. */
981a6d768d8SVincenzo Maffione 	uint64_t		nro_initial_offset;
982a6d768d8SVincenzo Maffione 	/* number of bits in the lower part of the 'ptr' field to be
983*45c67e8fSVincenzo Maffione 	 * used as the offset field. On output the (possibly larger)
984a6d768d8SVincenzo Maffione 	 * effective number of bits is returned.
985a6d768d8SVincenzo Maffione 	 * 0 means: use the whole ptr field.
986a6d768d8SVincenzo Maffione 	 */
987a6d768d8SVincenzo Maffione 	uint32_t		nro_offset_bits;
988a6d768d8SVincenzo Maffione 	/* required alignment for the beginning of the packets
989a6d768d8SVincenzo Maffione 	 * (base of the buffer plus offset) in the TX slots.
990a6d768d8SVincenzo Maffione 	 */
991a6d768d8SVincenzo Maffione 	uint32_t		nro_tx_align;
992a6d768d8SVincenzo Maffione 	/* Reserved: set to zero. */
993a6d768d8SVincenzo Maffione 	uint64_t		nro_min_gap;
994a6d768d8SVincenzo Maffione };
995a6d768d8SVincenzo Maffione 
99668b8534bSLuigi Rizzo #endif /* _NET_NETMAP_H_ */
997