xref: /f-stack/freebsd/net/netmap.h (revision 22ce4aff)
1*22ce4affSfengbojiang /*-
2*22ce4affSfengbojiang  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*22ce4affSfengbojiang  *
4a9643ea8Slogwang  * Copyright (C) 2011-2014 Matteo Landi, Luigi Rizzo. All rights reserved.
5a9643ea8Slogwang  *
6a9643ea8Slogwang  * Redistribution and use in source and binary forms, with or without
7a9643ea8Slogwang  * modification, are permitted provided that the following conditions
8a9643ea8Slogwang  * are met:
9a9643ea8Slogwang  *
10a9643ea8Slogwang  *   1. Redistributions of source code must retain the above copyright
11a9643ea8Slogwang  *      notice, this list of conditions and the following disclaimer.
12a9643ea8Slogwang  *   2. Redistributions in binary form must reproduce the above copyright
13a9643ea8Slogwang  *      notice, this list of conditions and the following disclaimer in the
14a9643ea8Slogwang  *      documentation and/or other materials provided with the distribution.
15a9643ea8Slogwang  *
16a9643ea8Slogwang  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``S IS''AND
17a9643ea8Slogwang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18a9643ea8Slogwang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19a9643ea8Slogwang  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20a9643ea8Slogwang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21a9643ea8Slogwang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22a9643ea8Slogwang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23a9643ea8Slogwang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24a9643ea8Slogwang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25a9643ea8Slogwang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26a9643ea8Slogwang  * SUCH DAMAGE.
27a9643ea8Slogwang  */
28a9643ea8Slogwang 
29a9643ea8Slogwang /*
30a9643ea8Slogwang  * $FreeBSD$
31a9643ea8Slogwang  *
32a9643ea8Slogwang  * Definitions of constants and the structures used by the netmap
33a9643ea8Slogwang  * framework, for the part visible to both kernel and userspace.
34a9643ea8Slogwang  * Detailed info on netmap is available with "man netmap" or at
35a9643ea8Slogwang  *
36a9643ea8Slogwang  *	http://info.iet.unipi.it/~luigi/netmap/
37a9643ea8Slogwang  *
38a9643ea8Slogwang  * This API is also used to communicate with the VALE software switch
39a9643ea8Slogwang  */
40a9643ea8Slogwang 
41a9643ea8Slogwang #ifndef _NET_NETMAP_H_
42a9643ea8Slogwang #define _NET_NETMAP_H_
43a9643ea8Slogwang 
44*22ce4affSfengbojiang #define	NETMAP_API	14		/* current API version */
45a9643ea8Slogwang 
46*22ce4affSfengbojiang #define	NETMAP_MIN_API	14		/* min and max versions accepted */
47a9643ea8Slogwang #define	NETMAP_MAX_API	15
48a9643ea8Slogwang /*
49a9643ea8Slogwang  * Some fields should be cache-aligned to reduce contention.
50a9643ea8Slogwang  * The alignment is architecture and OS dependent, but rather than
51a9643ea8Slogwang  * digging into OS headers to find the exact value we use an estimate
52a9643ea8Slogwang  * that should cover most architectures.
53a9643ea8Slogwang  */
54a9643ea8Slogwang #define NM_CACHE_ALIGN	128
55a9643ea8Slogwang 
56a9643ea8Slogwang /*
57a9643ea8Slogwang  * --- Netmap data structures ---
58a9643ea8Slogwang  *
59a9643ea8Slogwang  * The userspace data structures used by netmap are shown below.
60a9643ea8Slogwang  * They are allocated by the kernel and mmap()ed by userspace threads.
61a9643ea8Slogwang  * Pointers are implemented as memory offsets or indexes,
62a9643ea8Slogwang  * so that they can be easily dereferenced in kernel and userspace.
63a9643ea8Slogwang 
64a9643ea8Slogwang    KERNEL (opaque, obviously)
65a9643ea8Slogwang 
66a9643ea8Slogwang   ====================================================================
67a9643ea8Slogwang                                           |
68a9643ea8Slogwang    USERSPACE                              |      struct netmap_ring
69a9643ea8Slogwang                                           +---->+---------------+
70a9643ea8Slogwang                                               / | head,cur,tail |
71a9643ea8Slogwang    struct netmap_if (nifp, 1 per fd)         /  | buf_ofs       |
72*22ce4affSfengbojiang     +----------------+                      /   | other fields  |
73a9643ea8Slogwang     | ni_tx_rings    |                     /    +===============+
74a9643ea8Slogwang     | ni_rx_rings    |                    /     | buf_idx, len  | slot[0]
75a9643ea8Slogwang     |                |                   /      | flags, ptr    |
76a9643ea8Slogwang     |                |                  /       +---------------+
77*22ce4affSfengbojiang     +================+                 /        | buf_idx, len  | slot[1]
78a9643ea8Slogwang     | txring_ofs[0]  | (rel.to nifp)--'         | flags, ptr    |
79a9643ea8Slogwang     | txring_ofs[1]  |                          +---------------+
80*22ce4affSfengbojiang      (tx+htx entries)                           (num_slots entries)
81a9643ea8Slogwang     | txring_ofs[t]  |                          | buf_idx, len  | slot[n-1]
82*22ce4affSfengbojiang     +----------------+                          | flags, ptr    |
83a9643ea8Slogwang     | rxring_ofs[0]  |                          +---------------+
84a9643ea8Slogwang     | rxring_ofs[1]  |
85*22ce4affSfengbojiang      (rx+hrx entries)
86a9643ea8Slogwang     | rxring_ofs[r]  |
87*22ce4affSfengbojiang     +----------------+
88a9643ea8Slogwang 
89a9643ea8Slogwang  * For each "interface" (NIC, host stack, PIPE, VALE switch port) bound to
90a9643ea8Slogwang  * a file descriptor, the mmap()ed region contains a (logically readonly)
91a9643ea8Slogwang  * struct netmap_if pointing to struct netmap_ring's.
92a9643ea8Slogwang  *
93*22ce4affSfengbojiang  * There is one netmap_ring per physical NIC ring, plus at least one tx/rx ring
94*22ce4affSfengbojiang  * pair attached to the host stack (these pairs are unused for non-NIC ports).
95a9643ea8Slogwang  *
96a9643ea8Slogwang  * All physical/host stack ports share the same memory region,
97a9643ea8Slogwang  * so that zero-copy can be implemented between them.
98a9643ea8Slogwang  * VALE switch ports instead have separate memory regions.
99a9643ea8Slogwang  *
100a9643ea8Slogwang  * The netmap_ring is the userspace-visible replica of the NIC ring.
101a9643ea8Slogwang  * Each slot has the index of a buffer (MTU-sized and residing in the
102a9643ea8Slogwang  * mmapped region), its length and some flags. An extra 64-bit pointer
103a9643ea8Slogwang  * is provided for user-supplied buffers in the tx path.
104a9643ea8Slogwang  *
105a9643ea8Slogwang  * In user space, the buffer address is computed as
106a9643ea8Slogwang  *	(char *)ring + buf_ofs + index * NETMAP_BUF_SIZE
107a9643ea8Slogwang  *
108a9643ea8Slogwang  * Added in NETMAP_API 11:
109a9643ea8Slogwang  *
110a9643ea8Slogwang  * + NIOCREGIF can request the allocation of extra spare buffers from
111a9643ea8Slogwang  *   the same memory pool. The desired number of buffers must be in
112a9643ea8Slogwang  *   nr_arg3. The ioctl may return fewer buffers, depending on memory
113a9643ea8Slogwang  *   availability. nr_arg3 will return the actual value, and, once
114a9643ea8Slogwang  *   mapped, nifp->ni_bufs_head will be the index of the first buffer.
115a9643ea8Slogwang  *
116a9643ea8Slogwang  *   The buffers are linked to each other using the first uint32_t
117a9643ea8Slogwang  *   as the index. On close, ni_bufs_head must point to the list of
118a9643ea8Slogwang  *   buffers to be released.
119a9643ea8Slogwang  *
120a9643ea8Slogwang  * + NIOCREGIF can attach to PIPE rings sharing the same memory
121a9643ea8Slogwang  *   space with a parent device. The ifname indicates the parent device,
122a9643ea8Slogwang  *   which must already exist. Flags in nr_flags indicate if we want to
123a9643ea8Slogwang  *   bind the master or slave side, the index (from nr_ringid)
124a9643ea8Slogwang  *   is just a cookie and does not need to be sequential.
125a9643ea8Slogwang  *
126a9643ea8Slogwang  * + NIOCREGIF can also attach to 'monitor' rings that replicate
127a9643ea8Slogwang  *   the content of specific rings, also from the same memory space.
128a9643ea8Slogwang  *
129a9643ea8Slogwang  *   Extra flags in nr_flags support the above functions.
130a9643ea8Slogwang  *   Application libraries may use the following naming scheme:
131*22ce4affSfengbojiang  *	netmap:foo			all NIC rings pairs
132*22ce4affSfengbojiang  *	netmap:foo^			only host rings pairs
133*22ce4affSfengbojiang  *	netmap:foo^k			the k-th host rings pair
134*22ce4affSfengbojiang  *	netmap:foo+			all NIC rings + host rings pairs
135*22ce4affSfengbojiang  *	netmap:foo-k			the k-th NIC rings pair
136*22ce4affSfengbojiang  *	netmap:foo{k			PIPE rings pair k, master side
137*22ce4affSfengbojiang  *	netmap:foo}k			PIPE rings pair k, slave side
138*22ce4affSfengbojiang  *
139*22ce4affSfengbojiang  * Some notes about host rings:
140*22ce4affSfengbojiang  *
141*22ce4affSfengbojiang  * + The RX host rings are used to store those packets that the host network
142*22ce4affSfengbojiang  *   stack is trying to transmit through a NIC queue, but only if that queue
143*22ce4affSfengbojiang  *   is currently in netmap mode. Netmap will not intercept host stack mbufs
144*22ce4affSfengbojiang  *   designated to NIC queues that are not in netmap mode. As a consequence,
145*22ce4affSfengbojiang  *   registering a netmap port with netmap:foo^ is not enough to intercept
146*22ce4affSfengbojiang  *   mbufs in the RX host rings; the netmap port should be registered with
147*22ce4affSfengbojiang  *   netmap:foo*, or another registration should be done to open at least a
148*22ce4affSfengbojiang  *   NIC TX queue in netmap mode.
149*22ce4affSfengbojiang  *
150*22ce4affSfengbojiang  * + Netmap is not currently able to deal with intercepted trasmit mbufs which
151*22ce4affSfengbojiang  *   require offloadings like TSO, UFO, checksumming offloadings, etc. It is
152*22ce4affSfengbojiang  *   responsibility of the user to disable those offloadings (e.g. using
153*22ce4affSfengbojiang  *   ifconfig on FreeBSD or ethtool -K on Linux) for an interface that is being
154*22ce4affSfengbojiang  *   used in netmap mode. If the offloadings are not disabled, GSO and/or
155*22ce4affSfengbojiang  *   unchecksummed packets may be dropped immediately or end up in the host RX
156*22ce4affSfengbojiang  *   rings, and will be dropped as soon as the packet reaches another netmap
157*22ce4affSfengbojiang  *   adapter.
158a9643ea8Slogwang  */
159a9643ea8Slogwang 
160a9643ea8Slogwang /*
161a9643ea8Slogwang  * struct netmap_slot is a buffer descriptor
162a9643ea8Slogwang  */
163a9643ea8Slogwang struct netmap_slot {
164a9643ea8Slogwang 	uint32_t buf_idx;	/* buffer index */
165a9643ea8Slogwang 	uint16_t len;		/* length for this slot */
166a9643ea8Slogwang 	uint16_t flags;		/* buf changed, etc. */
167a9643ea8Slogwang 	uint64_t ptr;		/* pointer for indirect buffers */
168a9643ea8Slogwang };
169a9643ea8Slogwang 
170a9643ea8Slogwang /*
171a9643ea8Slogwang  * The following flags control how the slot is used
172a9643ea8Slogwang  */
173a9643ea8Slogwang 
174a9643ea8Slogwang #define	NS_BUF_CHANGED	0x0001	/* buf_idx changed */
175a9643ea8Slogwang 	/*
176a9643ea8Slogwang 	 * must be set whenever buf_idx is changed (as it might be
177a9643ea8Slogwang 	 * necessary to recompute the physical address and mapping)
178a9643ea8Slogwang 	 *
179a9643ea8Slogwang 	 * It is also set by the kernel whenever the buf_idx is
180a9643ea8Slogwang 	 * changed internally (e.g., by pipes). Applications may
181a9643ea8Slogwang 	 * use this information to know when they can reuse the
182a9643ea8Slogwang 	 * contents of previously prepared buffers.
183a9643ea8Slogwang 	 */
184a9643ea8Slogwang 
185a9643ea8Slogwang #define	NS_REPORT	0x0002	/* ask the hardware to report results */
186a9643ea8Slogwang 	/*
187a9643ea8Slogwang 	 * Request notification when slot is used by the hardware.
188a9643ea8Slogwang 	 * Normally transmit completions are handled lazily and
189a9643ea8Slogwang 	 * may be unreported. This flag lets us know when a slot
190a9643ea8Slogwang 	 * has been sent (e.g. to terminate the sender).
191a9643ea8Slogwang 	 */
192a9643ea8Slogwang 
193a9643ea8Slogwang #define	NS_FORWARD	0x0004	/* pass packet 'forward' */
194a9643ea8Slogwang 	/*
195a9643ea8Slogwang 	 * (Only for physical ports, rx rings with NR_FORWARD set).
196a9643ea8Slogwang 	 * Slot released to the kernel (i.e. before ring->head) with
197a9643ea8Slogwang 	 * this flag set are passed to the peer ring (host/NIC),
198a9643ea8Slogwang 	 * thus restoring the host-NIC connection for these slots.
199a9643ea8Slogwang 	 * This supports efficient traffic monitoring or firewalling.
200a9643ea8Slogwang 	 */
201a9643ea8Slogwang 
202a9643ea8Slogwang #define	NS_NO_LEARN	0x0008	/* disable bridge learning */
203a9643ea8Slogwang  	/*
204a9643ea8Slogwang 	 * On a VALE switch, do not 'learn' the source port for
205a9643ea8Slogwang  	 * this buffer.
206a9643ea8Slogwang 	 */
207a9643ea8Slogwang 
208a9643ea8Slogwang #define	NS_INDIRECT	0x0010	/* userspace buffer */
209a9643ea8Slogwang  	/*
210a9643ea8Slogwang 	 * (VALE tx rings only) data is in a userspace buffer,
211a9643ea8Slogwang 	 * whose address is in the 'ptr' field in the slot.
212a9643ea8Slogwang 	 */
213a9643ea8Slogwang 
214a9643ea8Slogwang #define	NS_MOREFRAG	0x0020	/* packet has more fragments */
215a9643ea8Slogwang  	/*
216*22ce4affSfengbojiang 	 * (VALE ports, ptnetmap ports and some NIC ports, e.g.
217*22ce4affSfengbojiang          * ixgbe and i40e on Linux)
218a9643ea8Slogwang 	 * Set on all but the last slot of a multi-segment packet.
219a9643ea8Slogwang 	 * The 'len' field refers to the individual fragment.
220a9643ea8Slogwang 	 */
221a9643ea8Slogwang 
222a9643ea8Slogwang #define	NS_PORT_SHIFT	8
223a9643ea8Slogwang #define	NS_PORT_MASK	(0xff << NS_PORT_SHIFT)
224a9643ea8Slogwang 	/*
225a9643ea8Slogwang  	 * The high 8 bits of the flag, if not zero, indicate the
226a9643ea8Slogwang 	 * destination port for the VALE switch, overriding
227a9643ea8Slogwang  	 * the lookup table.
228a9643ea8Slogwang  	 */
229a9643ea8Slogwang 
230a9643ea8Slogwang #define	NS_RFRAGS(_slot)	( ((_slot)->flags >> 8) & 0xff)
231a9643ea8Slogwang 	/*
232a9643ea8Slogwang 	 * (VALE rx rings only) the high 8 bits
233a9643ea8Slogwang 	 *  are the number of fragments.
234a9643ea8Slogwang 	 */
235a9643ea8Slogwang 
236*22ce4affSfengbojiang #define NETMAP_MAX_FRAGS	64	/* max number of fragments */
237a9643ea8Slogwang 
238a9643ea8Slogwang /*
239a9643ea8Slogwang  * struct netmap_ring
240a9643ea8Slogwang  *
241a9643ea8Slogwang  * Netmap representation of a TX or RX ring (also known as "queue").
242a9643ea8Slogwang  * This is a queue implemented as a fixed-size circular array.
243a9643ea8Slogwang  * At the software level the important fields are: head, cur, tail.
244a9643ea8Slogwang  *
245a9643ea8Slogwang  * In TX rings:
246a9643ea8Slogwang  *
247a9643ea8Slogwang  *	head	first slot available for transmission.
248a9643ea8Slogwang  *	cur	wakeup point. select() and poll() will unblock
249a9643ea8Slogwang  *		when 'tail' moves past 'cur'
250a9643ea8Slogwang  *	tail	(readonly) first slot reserved to the kernel
251a9643ea8Slogwang  *
252a9643ea8Slogwang  *	[head .. tail-1] can be used for new packets to send;
253a9643ea8Slogwang  *	'head' and 'cur' must be incremented as slots are filled
254a9643ea8Slogwang  *	    with new packets to be sent;
255a9643ea8Slogwang  *	'cur' can be moved further ahead if we need more space
256a9643ea8Slogwang  *	for new transmissions. XXX todo (2014-03-12)
257a9643ea8Slogwang  *
258a9643ea8Slogwang  * In RX rings:
259a9643ea8Slogwang  *
260a9643ea8Slogwang  *	head	first valid received packet
261a9643ea8Slogwang  *	cur	wakeup point. select() and poll() will unblock
262a9643ea8Slogwang  *		when 'tail' moves past 'cur'
263a9643ea8Slogwang  *	tail	(readonly) first slot reserved to the kernel
264a9643ea8Slogwang  *
265a9643ea8Slogwang  *	[head .. tail-1] contain received packets;
266a9643ea8Slogwang  *	'head' and 'cur' must be incremented as slots are consumed
267a9643ea8Slogwang  *		and can be returned to the kernel;
268a9643ea8Slogwang  *	'cur' can be moved further ahead if we want to wait for
269a9643ea8Slogwang  *		new packets without returning the previous ones.
270a9643ea8Slogwang  *
271a9643ea8Slogwang  * DATA OWNERSHIP/LOCKING:
272a9643ea8Slogwang  *	The netmap_ring, and all slots and buffers in the range
273a9643ea8Slogwang  *	[head .. tail-1] are owned by the user program;
274a9643ea8Slogwang  *	the kernel only accesses them during a netmap system call
275a9643ea8Slogwang  *	and in the user thread context.
276a9643ea8Slogwang  *
277a9643ea8Slogwang  *	Other slots and buffers are reserved for use by the kernel
278a9643ea8Slogwang  */
279a9643ea8Slogwang struct netmap_ring {
280a9643ea8Slogwang 	/*
281a9643ea8Slogwang 	 * buf_ofs is meant to be used through macros.
282a9643ea8Slogwang 	 * It contains the offset of the buffer region from this
283a9643ea8Slogwang 	 * descriptor.
284a9643ea8Slogwang 	 */
285a9643ea8Slogwang 	const int64_t	buf_ofs;
286a9643ea8Slogwang 	const uint32_t	num_slots;	/* number of slots in the ring. */
287a9643ea8Slogwang 	const uint32_t	nr_buf_size;
288a9643ea8Slogwang 	const uint16_t	ringid;
289a9643ea8Slogwang 	const uint16_t	dir;		/* 0: tx, 1: rx */
290a9643ea8Slogwang 
291a9643ea8Slogwang 	uint32_t        head;		/* (u) first user slot */
292a9643ea8Slogwang 	uint32_t        cur;		/* (u) wakeup point */
293a9643ea8Slogwang 	uint32_t	tail;		/* (k) first kernel slot */
294a9643ea8Slogwang 
295a9643ea8Slogwang 	uint32_t	flags;
296a9643ea8Slogwang 
297a9643ea8Slogwang 	struct timeval	ts;		/* (k) time of last *sync() */
298a9643ea8Slogwang 
299a9643ea8Slogwang 	/* opaque room for a mutex or similar object */
300*22ce4affSfengbojiang #if !defined(_WIN32) || defined(__CYGWIN__)
301*22ce4affSfengbojiang 	uint8_t	__attribute__((__aligned__(NM_CACHE_ALIGN))) sem[128];
302*22ce4affSfengbojiang #else
303*22ce4affSfengbojiang 	uint8_t	__declspec(align(NM_CACHE_ALIGN)) sem[128];
304*22ce4affSfengbojiang #endif
305a9643ea8Slogwang 
306a9643ea8Slogwang 	/* the slots follow. This struct has variable size */
307a9643ea8Slogwang 	struct netmap_slot slot[0];	/* array of slots. */
308a9643ea8Slogwang };
309a9643ea8Slogwang 
310a9643ea8Slogwang /*
311a9643ea8Slogwang  * RING FLAGS
312a9643ea8Slogwang  */
313a9643ea8Slogwang #define	NR_TIMESTAMP	0x0002		/* set timestamp on *sync() */
314a9643ea8Slogwang 	/*
315a9643ea8Slogwang 	 * updates the 'ts' field on each netmap syscall. This saves
316a9643ea8Slogwang 	 * saves a separate gettimeofday(), and is not much worse than
317a9643ea8Slogwang 	 * software timestamps generated in the interrupt handler.
318a9643ea8Slogwang 	 */
319a9643ea8Slogwang 
320a9643ea8Slogwang #define	NR_FORWARD	0x0004		/* enable NS_FORWARD for ring */
321a9643ea8Slogwang  	/*
322a9643ea8Slogwang 	 * Enables the NS_FORWARD slot flag for the ring.
323a9643ea8Slogwang 	 */
324a9643ea8Slogwang 
325*22ce4affSfengbojiang /*
326*22ce4affSfengbojiang  * Helper functions for kernel and userspace
327*22ce4affSfengbojiang  */
328*22ce4affSfengbojiang 
329*22ce4affSfengbojiang /*
330*22ce4affSfengbojiang  * Check if space is available in the ring. We use ring->head, which
331*22ce4affSfengbojiang  * points to the next netmap slot to be published to netmap. It is
332*22ce4affSfengbojiang  * possible that the applications moves ring->cur ahead of ring->tail
333*22ce4affSfengbojiang  * (e.g., by setting ring->cur <== ring->tail), if it wants more slots
334*22ce4affSfengbojiang  * than the ones currently available, and it wants to be notified when
335*22ce4affSfengbojiang  * more arrive. See netmap(4) for more details and examples.
336*22ce4affSfengbojiang  */
337*22ce4affSfengbojiang static inline int
nm_ring_empty(struct netmap_ring * ring)338*22ce4affSfengbojiang nm_ring_empty(struct netmap_ring *ring)
339*22ce4affSfengbojiang {
340*22ce4affSfengbojiang 	return (ring->head == ring->tail);
341*22ce4affSfengbojiang }
342a9643ea8Slogwang 
343a9643ea8Slogwang /*
344a9643ea8Slogwang  * Netmap representation of an interface and its queue(s).
345a9643ea8Slogwang  * This is initialized by the kernel when binding a file
346a9643ea8Slogwang  * descriptor to a port, and should be considered as readonly
347a9643ea8Slogwang  * by user programs. The kernel never uses it.
348a9643ea8Slogwang  *
349a9643ea8Slogwang  * There is one netmap_if for each file descriptor on which we want
350a9643ea8Slogwang  * to select/poll.
351a9643ea8Slogwang  * select/poll operates on one or all pairs depending on the value of
352a9643ea8Slogwang  * nmr_queueid passed on the ioctl.
353a9643ea8Slogwang  */
354a9643ea8Slogwang struct netmap_if {
355a9643ea8Slogwang 	char		ni_name[IFNAMSIZ]; /* name of the interface. */
356a9643ea8Slogwang 	const uint32_t	ni_version;	/* API version, currently unused */
357a9643ea8Slogwang 	const uint32_t	ni_flags;	/* properties */
358a9643ea8Slogwang #define	NI_PRIV_MEM	0x1		/* private memory region */
359a9643ea8Slogwang 
360a9643ea8Slogwang 	/*
361a9643ea8Slogwang 	 * The number of packet rings available in netmap mode.
362a9643ea8Slogwang 	 * Physical NICs can have different numbers of tx and rx rings.
363*22ce4affSfengbojiang 	 * Physical NICs also have at least a 'host' rings pair.
364a9643ea8Slogwang 	 * Additionally, clients can request additional ring pairs to
365a9643ea8Slogwang 	 * be used for internal communication.
366a9643ea8Slogwang 	 */
367a9643ea8Slogwang 	const uint32_t	ni_tx_rings;	/* number of HW tx rings */
368a9643ea8Slogwang 	const uint32_t	ni_rx_rings;	/* number of HW rx rings */
369a9643ea8Slogwang 
370a9643ea8Slogwang 	uint32_t	ni_bufs_head;	/* head index for extra bufs */
371*22ce4affSfengbojiang 	const uint32_t	ni_host_tx_rings; /* number of SW tx rings */
372*22ce4affSfengbojiang 	const uint32_t	ni_host_rx_rings; /* number of SW rx rings */
373*22ce4affSfengbojiang 	uint32_t	ni_spare1[3];
374a9643ea8Slogwang 	/*
375a9643ea8Slogwang 	 * The following array contains the offset of each netmap ring
376a9643ea8Slogwang 	 * from this structure, in the following order:
377*22ce4affSfengbojiang 	 *     - NIC tx rings (ni_tx_rings);
378*22ce4affSfengbojiang 	 *     - host tx rings (ni_host_tx_rings);
379*22ce4affSfengbojiang 	 *     - NIC rx rings (ni_rx_rings);
380*22ce4affSfengbojiang 	 *     - host rx ring (ni_host_rx_rings);
381a9643ea8Slogwang 	 *
382*22ce4affSfengbojiang 	 * The area is filled up by the kernel on NETMAP_REQ_REGISTER,
383a9643ea8Slogwang 	 * and then only read by userspace code.
384a9643ea8Slogwang 	 */
385a9643ea8Slogwang 	const ssize_t	ring_ofs[0];
386a9643ea8Slogwang };
387a9643ea8Slogwang 
388*22ce4affSfengbojiang /* Legacy interface to interact with a netmap control device.
389*22ce4affSfengbojiang  * Included for backward compatibility. The user should not include this
390*22ce4affSfengbojiang  * file directly. */
391*22ce4affSfengbojiang #include "netmap_legacy.h"
392a9643ea8Slogwang 
393a9643ea8Slogwang /*
394*22ce4affSfengbojiang  * New API to control netmap control devices. New applications should only use
395*22ce4affSfengbojiang  * nmreq_xyz structs with the NIOCCTRL ioctl() command.
396a9643ea8Slogwang  *
397*22ce4affSfengbojiang  * NIOCCTRL takes a nmreq_header struct, which contains the required
398*22ce4affSfengbojiang  * API version, the name of a netmap port, a command type, and pointers
399*22ce4affSfengbojiang  * to request body and options.
400a9643ea8Slogwang  *
401a9643ea8Slogwang  *	nr_name	(in)
402*22ce4affSfengbojiang  *		The name of the port (em0, valeXXX:YYY, eth0{pn1 etc.)
403a9643ea8Slogwang  *
404a9643ea8Slogwang  *	nr_version (in/out)
405a9643ea8Slogwang  *		Must match NETMAP_API as used in the kernel, error otherwise.
406a9643ea8Slogwang  *		Always returns the desired value on output.
407a9643ea8Slogwang  *
408*22ce4affSfengbojiang  *	nr_reqtype (in)
409*22ce4affSfengbojiang  *		One of the NETMAP_REQ_* command types below
410*22ce4affSfengbojiang  *
411*22ce4affSfengbojiang  *	nr_body (in)
412*22ce4affSfengbojiang  *		Pointer to a command-specific struct, described by one
413*22ce4affSfengbojiang  *		of the struct nmreq_xyz below.
414*22ce4affSfengbojiang  *
415*22ce4affSfengbojiang  *	nr_options (in)
416*22ce4affSfengbojiang  *		Command specific options, if any.
417*22ce4affSfengbojiang  *
418*22ce4affSfengbojiang  * A NETMAP_REQ_REGISTER command activates netmap mode on the netmap
419*22ce4affSfengbojiang  * port (e.g. physical interface) specified by nmreq_header.nr_name.
420*22ce4affSfengbojiang  * The request body (struct nmreq_register) has several arguments to
421*22ce4affSfengbojiang  * specify how the port is to be registered.
422*22ce4affSfengbojiang  *
423*22ce4affSfengbojiang  *	nr_tx_slots, nr_tx_slots, nr_tx_rings, nr_rx_rings,
424*22ce4affSfengbojiang  *	nr_host_tx_rings, nr_host_rx_rings (in/out)
425a9643ea8Slogwang  *		On input, non-zero values may be used to reconfigure the port
426a9643ea8Slogwang  *		according to the requested values, but this is not guaranteed.
427a9643ea8Slogwang  *		On output the actual values in use are reported.
428a9643ea8Slogwang  *
429*22ce4affSfengbojiang  *	nr_mode (in)
430*22ce4affSfengbojiang  *		Indicate what set of rings must be bound to the netmap
431*22ce4affSfengbojiang  *		device (e.g. all NIC rings, host rings only, NIC and
432*22ce4affSfengbojiang  *		host rings, ...). Values are in NR_REG_*.
433*22ce4affSfengbojiang  *
434a9643ea8Slogwang  *	nr_ringid (in)
435*22ce4affSfengbojiang  *		If nr_mode == NR_REG_ONE_NIC (only a single couple of TX/RX
436*22ce4affSfengbojiang  *		rings), indicate which NIC TX and/or RX ring is to be bound
437*22ce4affSfengbojiang  *		(0..nr_*x_rings-1).
438a9643ea8Slogwang  *
439*22ce4affSfengbojiang  *	nr_flags (in)
440*22ce4affSfengbojiang  *		Indicate special options for how to open the port.
441a9643ea8Slogwang  *
442*22ce4affSfengbojiang  *		NR_NO_TX_POLL can be OR-ed to make select()/poll() push
443a9643ea8Slogwang  *			packets on tx rings only if POLLOUT is set.
444a9643ea8Slogwang  *			The default is to push any pending packet.
445a9643ea8Slogwang  *
446*22ce4affSfengbojiang  *		NR_DO_RX_POLL can be OR-ed to make select()/poll() release
447a9643ea8Slogwang  *			packets on rx rings also when POLLIN is NOT set.
448a9643ea8Slogwang  *			The default is to touch the rx ring only with POLLIN.
449a9643ea8Slogwang  *			Note that this is the opposite of TX because it
450a9643ea8Slogwang  *			reflects the common usage.
451a9643ea8Slogwang  *
452*22ce4affSfengbojiang  *		Other options are NR_MONITOR_TX, NR_MONITOR_RX, NR_ZCOPY_MON,
453*22ce4affSfengbojiang  *		NR_EXCLUSIVE, NR_RX_RINGS_ONLY, NR_TX_RINGS_ONLY and
454*22ce4affSfengbojiang  *		NR_ACCEPT_VNET_HDR.
455a9643ea8Slogwang  *
456*22ce4affSfengbojiang  *	nr_mem_id (in/out)
457*22ce4affSfengbojiang  *		The identity of the memory region used.
458a9643ea8Slogwang  *		On input, 0 means the system decides autonomously,
459a9643ea8Slogwang  *		other values may try to select a specific region.
460a9643ea8Slogwang  *		On return the actual value is reported.
461a9643ea8Slogwang  *		Region '1' is the global allocator, normally shared
462a9643ea8Slogwang  *		by all interfaces. Other values are private regions.
463a9643ea8Slogwang  *		If two ports the same region zero-copy is possible.
464a9643ea8Slogwang  *
465*22ce4affSfengbojiang  *	nr_extra_bufs (in/out)
466*22ce4affSfengbojiang  *		Number of extra buffers to be allocated.
467a9643ea8Slogwang  *
468*22ce4affSfengbojiang  * The other NETMAP_REQ_* commands are described below.
469a9643ea8Slogwang  *
470a9643ea8Slogwang  */
471a9643ea8Slogwang 
472*22ce4affSfengbojiang /* maximum size of a request, including all options */
473*22ce4affSfengbojiang #define NETMAP_REQ_MAXSIZE	4096
474*22ce4affSfengbojiang 
475*22ce4affSfengbojiang /* Header common to all request options. */
476*22ce4affSfengbojiang struct nmreq_option {
477*22ce4affSfengbojiang 	/* Pointer ot the next option. */
478*22ce4affSfengbojiang 	uint64_t		nro_next;
479*22ce4affSfengbojiang 	/* Option type. */
480*22ce4affSfengbojiang 	uint32_t		nro_reqtype;
481*22ce4affSfengbojiang 	/* (out) status of the option:
482*22ce4affSfengbojiang 	 * 0: recognized and processed
483*22ce4affSfengbojiang 	 * !=0: errno value
484*22ce4affSfengbojiang 	 */
485*22ce4affSfengbojiang 	uint32_t		nro_status;
486*22ce4affSfengbojiang 	/* Option size, used only for options that can have variable size
487*22ce4affSfengbojiang 	 * (e.g. because they contain arrays). For fixed-size options this
488*22ce4affSfengbojiang 	 * field should be set to zero. */
489*22ce4affSfengbojiang 	uint64_t		nro_size;
490*22ce4affSfengbojiang };
491*22ce4affSfengbojiang 
492*22ce4affSfengbojiang /* Header common to all requests. Do not reorder these fields, as we need
493*22ce4affSfengbojiang  * the second one (nr_reqtype) to know how much to copy from/to userspace. */
494*22ce4affSfengbojiang struct nmreq_header {
495*22ce4affSfengbojiang 	uint16_t		nr_version;	/* API version */
496*22ce4affSfengbojiang 	uint16_t		nr_reqtype;	/* nmreq type (NETMAP_REQ_*) */
497*22ce4affSfengbojiang 	uint32_t		nr_reserved;	/* must be zero */
498*22ce4affSfengbojiang #define NETMAP_REQ_IFNAMSIZ	64
499*22ce4affSfengbojiang 	char			nr_name[NETMAP_REQ_IFNAMSIZ]; /* port name */
500*22ce4affSfengbojiang 	uint64_t		nr_options;	/* command-specific options */
501*22ce4affSfengbojiang 	uint64_t		nr_body;	/* ptr to nmreq_xyz struct */
502*22ce4affSfengbojiang };
503*22ce4affSfengbojiang 
504*22ce4affSfengbojiang enum {
505*22ce4affSfengbojiang 	/* Register a netmap port with the device. */
506*22ce4affSfengbojiang 	NETMAP_REQ_REGISTER = 1,
507*22ce4affSfengbojiang 	/* Get information from a netmap port. */
508*22ce4affSfengbojiang 	NETMAP_REQ_PORT_INFO_GET,
509*22ce4affSfengbojiang 	/* Attach a netmap port to a VALE switch. */
510*22ce4affSfengbojiang 	NETMAP_REQ_VALE_ATTACH,
511*22ce4affSfengbojiang 	/* Detach a netmap port from a VALE switch. */
512*22ce4affSfengbojiang 	NETMAP_REQ_VALE_DETACH,
513*22ce4affSfengbojiang 	/* List the ports attached to a VALE switch. */
514*22ce4affSfengbojiang 	NETMAP_REQ_VALE_LIST,
515*22ce4affSfengbojiang 	/* Set the port header length (was virtio-net header length). */
516*22ce4affSfengbojiang 	NETMAP_REQ_PORT_HDR_SET,
517*22ce4affSfengbojiang 	/* Get the port header length (was virtio-net header length). */
518*22ce4affSfengbojiang 	NETMAP_REQ_PORT_HDR_GET,
519*22ce4affSfengbojiang 	/* Create a new persistent VALE port. */
520*22ce4affSfengbojiang 	NETMAP_REQ_VALE_NEWIF,
521*22ce4affSfengbojiang 	/* Delete a persistent VALE port. */
522*22ce4affSfengbojiang 	NETMAP_REQ_VALE_DELIF,
523*22ce4affSfengbojiang 	/* Enable polling kernel thread(s) on an attached VALE port. */
524*22ce4affSfengbojiang 	NETMAP_REQ_VALE_POLLING_ENABLE,
525*22ce4affSfengbojiang 	/* Disable polling kernel thread(s) on an attached VALE port. */
526*22ce4affSfengbojiang 	NETMAP_REQ_VALE_POLLING_DISABLE,
527*22ce4affSfengbojiang 	/* Get info about the pools of a memory allocator. */
528*22ce4affSfengbojiang 	NETMAP_REQ_POOLS_INFO_GET,
529*22ce4affSfengbojiang 	/* Start an in-kernel loop that syncs the rings periodically or
530*22ce4affSfengbojiang 	 * on notifications. The loop runs in the context of the ioctl
531*22ce4affSfengbojiang 	 * syscall, and only stops on NETMAP_REQ_SYNC_KLOOP_STOP. */
532*22ce4affSfengbojiang 	NETMAP_REQ_SYNC_KLOOP_START,
533*22ce4affSfengbojiang 	/* Stops the thread executing the in-kernel loop. The thread
534*22ce4affSfengbojiang 	 * returns from the ioctl syscall. */
535*22ce4affSfengbojiang 	NETMAP_REQ_SYNC_KLOOP_STOP,
536*22ce4affSfengbojiang 	/* Enable CSB mode on a registered netmap control device. */
537*22ce4affSfengbojiang 	NETMAP_REQ_CSB_ENABLE,
538*22ce4affSfengbojiang };
539*22ce4affSfengbojiang 
540*22ce4affSfengbojiang enum {
541*22ce4affSfengbojiang 	/* On NETMAP_REQ_REGISTER, ask netmap to use memory allocated
542*22ce4affSfengbojiang 	 * from user-space allocated memory pools (e.g. hugepages).
543*22ce4affSfengbojiang 	 */
544*22ce4affSfengbojiang 	NETMAP_REQ_OPT_EXTMEM = 1,
545*22ce4affSfengbojiang 
546*22ce4affSfengbojiang 	/* ON NETMAP_REQ_SYNC_KLOOP_START, ask netmap to use eventfd-based
547*22ce4affSfengbojiang 	 * notifications to synchronize the kernel loop with the application.
548*22ce4affSfengbojiang 	 */
549*22ce4affSfengbojiang 	NETMAP_REQ_OPT_SYNC_KLOOP_EVENTFDS,
550*22ce4affSfengbojiang 
551*22ce4affSfengbojiang 	/* On NETMAP_REQ_REGISTER, ask netmap to work in CSB mode, where
552*22ce4affSfengbojiang 	 * head, cur and tail pointers are not exchanged through the
553*22ce4affSfengbojiang 	 * struct netmap_ring header, but rather using an user-provided
554*22ce4affSfengbojiang 	 * memory area (see struct nm_csb_atok and struct nm_csb_ktoa).
555*22ce4affSfengbojiang 	 */
556*22ce4affSfengbojiang 	NETMAP_REQ_OPT_CSB,
557*22ce4affSfengbojiang 
558*22ce4affSfengbojiang 	/* An extension to NETMAP_REQ_OPT_SYNC_KLOOP_EVENTFDS, which specifies
559*22ce4affSfengbojiang 	 * if the TX and/or RX rings are synced in the context of the VM exit.
560*22ce4affSfengbojiang 	 * This requires the 'ioeventfd' fields to be valid (cannot be < 0).
561*22ce4affSfengbojiang 	 */
562*22ce4affSfengbojiang 	NETMAP_REQ_OPT_SYNC_KLOOP_MODE,
563*22ce4affSfengbojiang 
564*22ce4affSfengbojiang 	/* This is a marker to count the number of available options.
565*22ce4affSfengbojiang 	 * New options must be added above it. */
566*22ce4affSfengbojiang 	NETMAP_REQ_OPT_MAX,
567*22ce4affSfengbojiang };
568a9643ea8Slogwang 
569a9643ea8Slogwang /*
570*22ce4affSfengbojiang  * nr_reqtype: NETMAP_REQ_REGISTER
571*22ce4affSfengbojiang  * Bind (register) a netmap port to this control device.
572a9643ea8Slogwang  */
573*22ce4affSfengbojiang struct nmreq_register {
574*22ce4affSfengbojiang 	uint64_t	nr_offset;	/* nifp offset in the shared region */
575*22ce4affSfengbojiang 	uint64_t	nr_memsize;	/* size of the shared region */
576a9643ea8Slogwang 	uint32_t	nr_tx_slots;	/* slots in tx rings */
577a9643ea8Slogwang 	uint32_t	nr_rx_slots;	/* slots in rx rings */
578a9643ea8Slogwang 	uint16_t	nr_tx_rings;	/* number of tx rings */
579a9643ea8Slogwang 	uint16_t	nr_rx_rings;	/* number of rx rings */
580*22ce4affSfengbojiang 	uint16_t	nr_host_tx_rings; /* number of host tx rings */
581*22ce4affSfengbojiang 	uint16_t	nr_host_rx_rings; /* number of host rx rings */
582a9643ea8Slogwang 
583*22ce4affSfengbojiang 	uint16_t	nr_mem_id;	/* id of the memory allocator */
584a9643ea8Slogwang 	uint16_t	nr_ringid;	/* ring(s) we care about */
585*22ce4affSfengbojiang 	uint32_t	nr_mode;	/* specify NR_REG_* modes */
586*22ce4affSfengbojiang 	uint32_t	nr_extra_bufs;	/* number of requested extra buffers */
587a9643ea8Slogwang 
588*22ce4affSfengbojiang 	uint64_t	nr_flags;	/* additional flags (see below) */
589*22ce4affSfengbojiang /* monitors use nr_ringid and nr_mode to select the rings to monitor */
590a9643ea8Slogwang #define NR_MONITOR_TX	0x100
591a9643ea8Slogwang #define NR_MONITOR_RX	0x200
592a9643ea8Slogwang #define NR_ZCOPY_MON	0x400
593a9643ea8Slogwang /* request exclusive access to the selected rings */
594a9643ea8Slogwang #define NR_EXCLUSIVE	0x800
595*22ce4affSfengbojiang /* 0x1000 unused */
596*22ce4affSfengbojiang #define NR_RX_RINGS_ONLY	0x2000
597*22ce4affSfengbojiang #define NR_TX_RINGS_ONLY	0x4000
598*22ce4affSfengbojiang /* Applications set this flag if they are able to deal with virtio-net headers,
599*22ce4affSfengbojiang  * that is send/receive frames that start with a virtio-net header.
600*22ce4affSfengbojiang  * If not set, NETMAP_REQ_REGISTER will fail with netmap ports that require
601*22ce4affSfengbojiang  * applications to use those headers. If the flag is set, the application can
602*22ce4affSfengbojiang  * use the NETMAP_VNET_HDR_GET command to figure out the header length. */
603*22ce4affSfengbojiang #define NR_ACCEPT_VNET_HDR	0x8000
604*22ce4affSfengbojiang /* The following two have the same meaning of NETMAP_NO_TX_POLL and
605*22ce4affSfengbojiang  * NETMAP_DO_RX_POLL. */
606*22ce4affSfengbojiang #define NR_DO_RX_POLL		0x10000
607*22ce4affSfengbojiang #define NR_NO_TX_POLL		0x20000
608*22ce4affSfengbojiang };
609a9643ea8Slogwang 
610*22ce4affSfengbojiang /* Valid values for nmreq_register.nr_mode (see above). */
611*22ce4affSfengbojiang enum {	NR_REG_DEFAULT	= 0,	/* backward compat, should not be used. */
612*22ce4affSfengbojiang 	NR_REG_ALL_NIC	= 1,
613*22ce4affSfengbojiang 	NR_REG_SW	= 2,
614*22ce4affSfengbojiang 	NR_REG_NIC_SW	= 3,
615*22ce4affSfengbojiang 	NR_REG_ONE_NIC	= 4,
616*22ce4affSfengbojiang 	NR_REG_PIPE_MASTER = 5, /* deprecated, use "x{y" port name syntax */
617*22ce4affSfengbojiang 	NR_REG_PIPE_SLAVE = 6,  /* deprecated, use "x}y" port name syntax */
618*22ce4affSfengbojiang 	NR_REG_NULL     = 7,
619*22ce4affSfengbojiang 	NR_REG_ONE_SW	= 8,
620*22ce4affSfengbojiang };
621a9643ea8Slogwang 
622*22ce4affSfengbojiang /* A single ioctl number is shared by all the new API command.
623*22ce4affSfengbojiang  * Demultiplexing is done using the hdr.nr_reqtype field.
624a9643ea8Slogwang  * FreeBSD uses the size value embedded in the _IOWR to determine
625*22ce4affSfengbojiang  * how much to copy in/out, so we define the ioctl() command
626*22ce4affSfengbojiang  * specifying only nmreq_header, and copyin/copyout the rest. */
627*22ce4affSfengbojiang #define NIOCCTRL	_IOWR('i', 151, struct nmreq_header)
628*22ce4affSfengbojiang 
629*22ce4affSfengbojiang /* The ioctl commands to sync TX/RX netmap rings.
630*22ce4affSfengbojiang  * NIOCTXSYNC, NIOCRXSYNC synchronize tx or rx queues,
631*22ce4affSfengbojiang  *	whose identity is set in NETMAP_REQ_REGISTER through nr_ringid.
632*22ce4affSfengbojiang  *	These are non blocking and take no argument. */
633a9643ea8Slogwang #define NIOCTXSYNC	_IO('i', 148) /* sync tx queues */
634a9643ea8Slogwang #define NIOCRXSYNC	_IO('i', 149) /* sync rx queues */
635a9643ea8Slogwang 
636a9643ea8Slogwang /*
637*22ce4affSfengbojiang  * nr_reqtype: NETMAP_REQ_PORT_INFO_GET
638*22ce4affSfengbojiang  * Get information about a netmap port, including number of rings.
639*22ce4affSfengbojiang  * slots per ring, id of the memory allocator, etc. The netmap
640*22ce4affSfengbojiang  * control device used for this operation does not need to be bound
641*22ce4affSfengbojiang  * to a netmap port.
642a9643ea8Slogwang  */
643*22ce4affSfengbojiang struct nmreq_port_info_get {
644*22ce4affSfengbojiang 	uint64_t	nr_memsize;	/* size of the shared region */
645*22ce4affSfengbojiang 	uint32_t	nr_tx_slots;	/* slots in tx rings */
646*22ce4affSfengbojiang 	uint32_t	nr_rx_slots;	/* slots in rx rings */
647*22ce4affSfengbojiang 	uint16_t	nr_tx_rings;	/* number of tx rings */
648*22ce4affSfengbojiang 	uint16_t	nr_rx_rings;	/* number of rx rings */
649*22ce4affSfengbojiang 	uint16_t	nr_host_tx_rings; /* number of host tx rings */
650*22ce4affSfengbojiang 	uint16_t	nr_host_rx_rings; /* number of host rx rings */
651*22ce4affSfengbojiang 	uint16_t	nr_mem_id;	/* memory allocator id (in/out) */
652*22ce4affSfengbojiang 	uint16_t	pad[3];
653*22ce4affSfengbojiang };
654*22ce4affSfengbojiang 
655*22ce4affSfengbojiang #define	NM_BDG_NAME		"vale"	/* prefix for bridge port name */
656a9643ea8Slogwang 
657a9643ea8Slogwang /*
658*22ce4affSfengbojiang  * nr_reqtype: NETMAP_REQ_VALE_ATTACH
659*22ce4affSfengbojiang  * Attach a netmap port to a VALE switch. Both the name of the netmap
660*22ce4affSfengbojiang  * port and the VALE switch are specified through the nr_name argument.
661*22ce4affSfengbojiang  * The attach operation could need to register a port, so at least
662*22ce4affSfengbojiang  * the same arguments are available.
663*22ce4affSfengbojiang  * port_index will contain the index where the port has been attached.
664a9643ea8Slogwang  */
665*22ce4affSfengbojiang struct nmreq_vale_attach {
666*22ce4affSfengbojiang 	struct nmreq_register reg;
667*22ce4affSfengbojiang 	uint32_t port_index;
668*22ce4affSfengbojiang 	uint32_t pad1;
669*22ce4affSfengbojiang };
670*22ce4affSfengbojiang 
671*22ce4affSfengbojiang /*
672*22ce4affSfengbojiang  * nr_reqtype: NETMAP_REQ_VALE_DETACH
673*22ce4affSfengbojiang  * Detach a netmap port from a VALE switch. Both the name of the netmap
674*22ce4affSfengbojiang  * port and the VALE switch are specified through the nr_name argument.
675*22ce4affSfengbojiang  * port_index will contain the index where the port was attached.
676*22ce4affSfengbojiang  */
677*22ce4affSfengbojiang struct nmreq_vale_detach {
678*22ce4affSfengbojiang 	uint32_t port_index;
679*22ce4affSfengbojiang 	uint32_t pad1;
680*22ce4affSfengbojiang };
681*22ce4affSfengbojiang 
682*22ce4affSfengbojiang /*
683*22ce4affSfengbojiang  * nr_reqtype: NETMAP_REQ_VALE_LIST
684*22ce4affSfengbojiang  * List the ports of a VALE switch.
685*22ce4affSfengbojiang  */
686*22ce4affSfengbojiang struct nmreq_vale_list {
687*22ce4affSfengbojiang 	/* Name of the VALE port (valeXXX:YYY) or empty. */
688*22ce4affSfengbojiang 	uint16_t	nr_bridge_idx;
689*22ce4affSfengbojiang 	uint16_t	pad1;
690*22ce4affSfengbojiang 	uint32_t	nr_port_idx;
691*22ce4affSfengbojiang };
692*22ce4affSfengbojiang 
693*22ce4affSfengbojiang /*
694*22ce4affSfengbojiang  * nr_reqtype: NETMAP_REQ_PORT_HDR_SET or NETMAP_REQ_PORT_HDR_GET
695*22ce4affSfengbojiang  * Set or get the port header length of the port identified by hdr.nr_name.
696*22ce4affSfengbojiang  * The control device does not need to be bound to a netmap port.
697*22ce4affSfengbojiang  */
698*22ce4affSfengbojiang struct nmreq_port_hdr {
699*22ce4affSfengbojiang 	uint32_t	nr_hdr_len;
700*22ce4affSfengbojiang 	uint32_t	pad1;
701*22ce4affSfengbojiang };
702*22ce4affSfengbojiang 
703*22ce4affSfengbojiang /*
704*22ce4affSfengbojiang  * nr_reqtype: NETMAP_REQ_VALE_NEWIF
705*22ce4affSfengbojiang  * Create a new persistent VALE port.
706*22ce4affSfengbojiang  */
707*22ce4affSfengbojiang struct nmreq_vale_newif {
708*22ce4affSfengbojiang 	uint32_t	nr_tx_slots;	/* slots in tx rings */
709*22ce4affSfengbojiang 	uint32_t	nr_rx_slots;	/* slots in rx rings */
710*22ce4affSfengbojiang 	uint16_t	nr_tx_rings;	/* number of tx rings */
711*22ce4affSfengbojiang 	uint16_t	nr_rx_rings;	/* number of rx rings */
712*22ce4affSfengbojiang 	uint16_t	nr_mem_id;	/* id of the memory allocator */
713*22ce4affSfengbojiang 	uint16_t	pad1;
714*22ce4affSfengbojiang };
715*22ce4affSfengbojiang 
716*22ce4affSfengbojiang /*
717*22ce4affSfengbojiang  * nr_reqtype: NETMAP_REQ_VALE_POLLING_ENABLE or NETMAP_REQ_VALE_POLLING_DISABLE
718*22ce4affSfengbojiang  * Enable or disable polling kthreads on a VALE port.
719*22ce4affSfengbojiang  */
720*22ce4affSfengbojiang struct nmreq_vale_polling {
721*22ce4affSfengbojiang 	uint32_t	nr_mode;
722*22ce4affSfengbojiang #define NETMAP_POLLING_MODE_SINGLE_CPU 1
723*22ce4affSfengbojiang #define NETMAP_POLLING_MODE_MULTI_CPU 2
724*22ce4affSfengbojiang 	uint32_t	nr_first_cpu_id;
725*22ce4affSfengbojiang 	uint32_t	nr_num_polling_cpus;
726*22ce4affSfengbojiang 	uint32_t	pad1;
727*22ce4affSfengbojiang };
728*22ce4affSfengbojiang 
729*22ce4affSfengbojiang /*
730*22ce4affSfengbojiang  * nr_reqtype: NETMAP_REQ_POOLS_INFO_GET
731*22ce4affSfengbojiang  * Get info about the pools of the memory allocator of the netmap
732*22ce4affSfengbojiang  * port specified by hdr.nr_name and nr_mem_id. The netmap control
733*22ce4affSfengbojiang  * device used for this operation does not need to be bound to a netmap
734*22ce4affSfengbojiang  * port.
735*22ce4affSfengbojiang  */
736*22ce4affSfengbojiang struct nmreq_pools_info {
737*22ce4affSfengbojiang 	uint64_t	nr_memsize;
738*22ce4affSfengbojiang 	uint16_t	nr_mem_id; /* in/out argument */
739*22ce4affSfengbojiang 	uint16_t	pad1[3];
740*22ce4affSfengbojiang 	uint64_t	nr_if_pool_offset;
741*22ce4affSfengbojiang 	uint32_t	nr_if_pool_objtotal;
742*22ce4affSfengbojiang 	uint32_t	nr_if_pool_objsize;
743*22ce4affSfengbojiang 	uint64_t	nr_ring_pool_offset;
744*22ce4affSfengbojiang 	uint32_t	nr_ring_pool_objtotal;
745*22ce4affSfengbojiang 	uint32_t	nr_ring_pool_objsize;
746*22ce4affSfengbojiang 	uint64_t	nr_buf_pool_offset;
747*22ce4affSfengbojiang 	uint32_t	nr_buf_pool_objtotal;
748*22ce4affSfengbojiang 	uint32_t	nr_buf_pool_objsize;
749*22ce4affSfengbojiang };
750*22ce4affSfengbojiang 
751*22ce4affSfengbojiang /*
752*22ce4affSfengbojiang  * nr_reqtype: NETMAP_REQ_SYNC_KLOOP_START
753*22ce4affSfengbojiang  * Start an in-kernel loop that syncs the rings periodically or on
754*22ce4affSfengbojiang  * notifications. The loop runs in the context of the ioctl syscall,
755*22ce4affSfengbojiang  * and only stops on NETMAP_REQ_SYNC_KLOOP_STOP.
756*22ce4affSfengbojiang  * The registered netmap port must be open in CSB mode.
757*22ce4affSfengbojiang  */
758*22ce4affSfengbojiang struct nmreq_sync_kloop_start {
759*22ce4affSfengbojiang 	/* Sleeping is the default synchronization method for the kloop.
760*22ce4affSfengbojiang 	 * The 'sleep_us' field specifies how many microsconds to sleep for
761*22ce4affSfengbojiang 	 * when there is no work to do, before doing another kloop iteration.
762*22ce4affSfengbojiang 	 */
763*22ce4affSfengbojiang 	uint32_t	sleep_us;
764*22ce4affSfengbojiang 	uint32_t	pad1;
765*22ce4affSfengbojiang };
766*22ce4affSfengbojiang 
767*22ce4affSfengbojiang /* A CSB entry for the application --> kernel direction. */
768*22ce4affSfengbojiang struct nm_csb_atok {
769*22ce4affSfengbojiang 	uint32_t head;		  /* AW+ KR+ the head of the appl netmap_ring */
770*22ce4affSfengbojiang 	uint32_t cur;		  /* AW+ KR+ the cur of the appl netmap_ring */
771*22ce4affSfengbojiang 	uint32_t appl_need_kick;  /* AW+ KR+ kern --> appl notification enable */
772*22ce4affSfengbojiang 	uint32_t sync_flags;	  /* AW+ KR+ the flags of the appl [tx|rx]sync() */
773*22ce4affSfengbojiang 	uint32_t pad[12];	  /* pad to a 64 bytes cacheline */
774*22ce4affSfengbojiang };
775*22ce4affSfengbojiang 
776*22ce4affSfengbojiang /* A CSB entry for the application <-- kernel direction. */
777*22ce4affSfengbojiang struct nm_csb_ktoa {
778*22ce4affSfengbojiang 	uint32_t hwcur;		  /* AR+ KW+ the hwcur of the kern netmap_kring */
779*22ce4affSfengbojiang 	uint32_t hwtail;	  /* AR+ KW+ the hwtail of the kern netmap_kring */
780*22ce4affSfengbojiang 	uint32_t kern_need_kick;  /* AR+ KW+ appl-->kern notification enable */
781*22ce4affSfengbojiang 	uint32_t pad[13];
782*22ce4affSfengbojiang };
783*22ce4affSfengbojiang 
784*22ce4affSfengbojiang #ifdef __linux__
785*22ce4affSfengbojiang 
786*22ce4affSfengbojiang #ifdef __KERNEL__
787*22ce4affSfengbojiang #define nm_stst_barrier smp_wmb
788*22ce4affSfengbojiang #define nm_ldld_barrier smp_rmb
789*22ce4affSfengbojiang #define nm_stld_barrier smp_mb
790*22ce4affSfengbojiang #else  /* !__KERNEL__ */
nm_stst_barrier(void)791*22ce4affSfengbojiang static inline void nm_stst_barrier(void)
792a9643ea8Slogwang {
793*22ce4affSfengbojiang 	/* A memory barrier with release semantic has the combined
794*22ce4affSfengbojiang 	 * effect of a store-store barrier and a load-store barrier,
795*22ce4affSfengbojiang 	 * which is fine for us. */
796*22ce4affSfengbojiang 	__atomic_thread_fence(__ATOMIC_RELEASE);
797*22ce4affSfengbojiang }
nm_ldld_barrier(void)798*22ce4affSfengbojiang static inline void nm_ldld_barrier(void)
799*22ce4affSfengbojiang {
800*22ce4affSfengbojiang 	/* A memory barrier with acquire semantic has the combined
801*22ce4affSfengbojiang 	 * effect of a load-load barrier and a store-load barrier,
802*22ce4affSfengbojiang 	 * which is fine for us. */
803*22ce4affSfengbojiang 	__atomic_thread_fence(__ATOMIC_ACQUIRE);
804*22ce4affSfengbojiang }
805*22ce4affSfengbojiang #endif /* !__KERNEL__ */
806*22ce4affSfengbojiang 
807*22ce4affSfengbojiang #elif defined(__FreeBSD__)
808*22ce4affSfengbojiang 
809*22ce4affSfengbojiang #ifdef _KERNEL
810*22ce4affSfengbojiang #define nm_stst_barrier	atomic_thread_fence_rel
811*22ce4affSfengbojiang #define nm_ldld_barrier	atomic_thread_fence_acq
812*22ce4affSfengbojiang #define nm_stld_barrier	atomic_thread_fence_seq_cst
813*22ce4affSfengbojiang #else  /* !_KERNEL */
814*22ce4affSfengbojiang #include <stdatomic.h>
nm_stst_barrier(void)815*22ce4affSfengbojiang static inline void nm_stst_barrier(void)
816*22ce4affSfengbojiang {
817*22ce4affSfengbojiang 	atomic_thread_fence(memory_order_release);
818*22ce4affSfengbojiang }
nm_ldld_barrier(void)819*22ce4affSfengbojiang static inline void nm_ldld_barrier(void)
820*22ce4affSfengbojiang {
821*22ce4affSfengbojiang 	atomic_thread_fence(memory_order_acquire);
822*22ce4affSfengbojiang }
823*22ce4affSfengbojiang #endif /* !_KERNEL */
824*22ce4affSfengbojiang 
825*22ce4affSfengbojiang #else  /* !__linux__ && !__FreeBSD__ */
826*22ce4affSfengbojiang #error "OS not supported"
827*22ce4affSfengbojiang #endif /* !__linux__ && !__FreeBSD__ */
828*22ce4affSfengbojiang 
829*22ce4affSfengbojiang /* Application side of sync-kloop: Write ring pointers (cur, head) to the CSB.
830*22ce4affSfengbojiang  * This routine is coupled with sync_kloop_kernel_read(). */
831*22ce4affSfengbojiang static inline void
nm_sync_kloop_appl_write(struct nm_csb_atok * atok,uint32_t cur,uint32_t head)832*22ce4affSfengbojiang nm_sync_kloop_appl_write(struct nm_csb_atok *atok, uint32_t cur,
833*22ce4affSfengbojiang 			 uint32_t head)
834*22ce4affSfengbojiang {
835*22ce4affSfengbojiang 	/* Issue a first store-store barrier to make sure writes to the
836*22ce4affSfengbojiang 	 * netmap ring do not overcome updates on atok->cur and atok->head. */
837*22ce4affSfengbojiang 	nm_stst_barrier();
838*22ce4affSfengbojiang 
839*22ce4affSfengbojiang 	/*
840*22ce4affSfengbojiang 	 * We need to write cur and head to the CSB but we cannot do it atomically.
841*22ce4affSfengbojiang 	 * There is no way we can prevent the host from reading the updated value
842*22ce4affSfengbojiang 	 * of one of the two and the old value of the other. However, if we make
843*22ce4affSfengbojiang 	 * sure that the host never reads a value of head more recent than the
844*22ce4affSfengbojiang 	 * value of cur we are safe. We can allow the host to read a value of cur
845*22ce4affSfengbojiang 	 * more recent than the value of head, since in the netmap ring cur can be
846*22ce4affSfengbojiang 	 * ahead of head and cur cannot wrap around head because it must be behind
847*22ce4affSfengbojiang 	 * tail. Inverting the order of writes below could instead result into the
848*22ce4affSfengbojiang 	 * host to think head went ahead of cur, which would cause the sync
849*22ce4affSfengbojiang 	 * prologue to fail.
850*22ce4affSfengbojiang 	 *
851*22ce4affSfengbojiang 	 * The following memory barrier scheme is used to make this happen:
852*22ce4affSfengbojiang 	 *
853*22ce4affSfengbojiang 	 *          Guest                Host
854*22ce4affSfengbojiang 	 *
855*22ce4affSfengbojiang 	 *          STORE(cur)           LOAD(head)
856*22ce4affSfengbojiang 	 *          wmb() <----------->  rmb()
857*22ce4affSfengbojiang 	 *          STORE(head)          LOAD(cur)
858*22ce4affSfengbojiang 	 *
859*22ce4affSfengbojiang 	 */
860*22ce4affSfengbojiang 	atok->cur = cur;
861*22ce4affSfengbojiang 	nm_stst_barrier();
862*22ce4affSfengbojiang 	atok->head = head;
863*22ce4affSfengbojiang }
864*22ce4affSfengbojiang 
865*22ce4affSfengbojiang /* Application side of sync-kloop: Read kring pointers (hwcur, hwtail) from
866*22ce4affSfengbojiang  * the CSB. This routine is coupled with sync_kloop_kernel_write(). */
867*22ce4affSfengbojiang static inline void
nm_sync_kloop_appl_read(struct nm_csb_ktoa * ktoa,uint32_t * hwtail,uint32_t * hwcur)868*22ce4affSfengbojiang nm_sync_kloop_appl_read(struct nm_csb_ktoa *ktoa, uint32_t *hwtail,
869*22ce4affSfengbojiang 			uint32_t *hwcur)
870*22ce4affSfengbojiang {
871*22ce4affSfengbojiang 	/*
872*22ce4affSfengbojiang 	 * We place a memory barrier to make sure that the update of hwtail never
873*22ce4affSfengbojiang 	 * overtakes the update of hwcur.
874*22ce4affSfengbojiang 	 * (see explanation in sync_kloop_kernel_write).
875*22ce4affSfengbojiang 	 */
876*22ce4affSfengbojiang 	*hwtail = ktoa->hwtail;
877*22ce4affSfengbojiang 	nm_ldld_barrier();
878*22ce4affSfengbojiang 	*hwcur = ktoa->hwcur;
879*22ce4affSfengbojiang 
880*22ce4affSfengbojiang 	/* Make sure that loads from ktoa->hwtail and ktoa->hwcur are not delayed
881*22ce4affSfengbojiang 	 * after the loads from the netmap ring. */
882*22ce4affSfengbojiang 	nm_ldld_barrier();
883a9643ea8Slogwang }
884a9643ea8Slogwang 
885a9643ea8Slogwang /*
886*22ce4affSfengbojiang  * data for NETMAP_REQ_OPT_* options
887a9643ea8Slogwang  */
888*22ce4affSfengbojiang 
889*22ce4affSfengbojiang struct nmreq_opt_sync_kloop_eventfds {
890*22ce4affSfengbojiang 	struct nmreq_option	nro_opt;	/* common header */
891*22ce4affSfengbojiang 	/* An array of N entries for bidirectional notifications between
892*22ce4affSfengbojiang 	 * the kernel loop and the application. The number of entries and
893*22ce4affSfengbojiang 	 * their order must agree with the CSB arrays passed in the
894*22ce4affSfengbojiang 	 * NETMAP_REQ_OPT_CSB option. Each entry contains a file descriptor
895*22ce4affSfengbojiang 	 * backed by an eventfd.
896*22ce4affSfengbojiang 	 *
897*22ce4affSfengbojiang 	 * If any of the 'ioeventfd' entries is < 0, the event loop uses
898*22ce4affSfengbojiang 	 * the sleeping synchronization strategy (according to sleep_us),
899*22ce4affSfengbojiang 	 * and keeps kern_need_kick always disabled.
900*22ce4affSfengbojiang 	 * Each 'irqfd' can be < 0, and in that case the corresponding queue
901*22ce4affSfengbojiang 	 * is never notified.
902*22ce4affSfengbojiang 	 */
903*22ce4affSfengbojiang 	struct {
904*22ce4affSfengbojiang 		/* Notifier for the application --> kernel loop direction. */
905*22ce4affSfengbojiang 		int32_t ioeventfd;
906*22ce4affSfengbojiang 		/* Notifier for the kernel loop --> application direction. */
907*22ce4affSfengbojiang 		int32_t irqfd;
908*22ce4affSfengbojiang 	} eventfds[0];
909*22ce4affSfengbojiang };
910*22ce4affSfengbojiang 
911*22ce4affSfengbojiang struct nmreq_opt_sync_kloop_mode {
912*22ce4affSfengbojiang 	struct nmreq_option	nro_opt;	/* common header */
913*22ce4affSfengbojiang #define NM_OPT_SYNC_KLOOP_DIRECT_TX (1 << 0)
914*22ce4affSfengbojiang #define NM_OPT_SYNC_KLOOP_DIRECT_RX (1 << 1)
915*22ce4affSfengbojiang 	uint32_t mode;
916*22ce4affSfengbojiang };
917*22ce4affSfengbojiang 
918*22ce4affSfengbojiang struct nmreq_opt_extmem {
919*22ce4affSfengbojiang 	struct nmreq_option	nro_opt;	/* common header */
920*22ce4affSfengbojiang 	uint64_t		nro_usrptr;	/* (in) ptr to usr memory */
921*22ce4affSfengbojiang 	struct nmreq_pools_info	nro_info;	/* (in/out) */
922*22ce4affSfengbojiang };
923*22ce4affSfengbojiang 
924*22ce4affSfengbojiang struct nmreq_opt_csb {
925*22ce4affSfengbojiang 	struct nmreq_option	nro_opt;
926*22ce4affSfengbojiang 
927*22ce4affSfengbojiang 	/* Array of CSB entries for application --> kernel communication
928*22ce4affSfengbojiang 	 * (N entries). */
929*22ce4affSfengbojiang 	uint64_t		csb_atok;
930*22ce4affSfengbojiang 
931*22ce4affSfengbojiang 	/* Array of CSB entries for kernel --> application communication
932*22ce4affSfengbojiang 	 * (N entries). */
933*22ce4affSfengbojiang 	uint64_t		csb_ktoa;
934a9643ea8Slogwang };
935a9643ea8Slogwang 
936a9643ea8Slogwang #endif /* _NET_NETMAP_H_ */
937