xref: /freebsd/sys/net/netmap.h (revision 4f80b14ce2b17100b12dc3a346fb9e6e76764e11)
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 
44f0ea3689SLuigi Rizzo #define	NETMAP_API	11		/* current API version */
4517885a7bSLuigi Rizzo 
46f0ea3689SLuigi Rizzo #define	NETMAP_MIN_API	11		/* min and max versions accepted */
47f0ea3689SLuigi Rizzo #define	NETMAP_MAX_API	15
4817885a7bSLuigi Rizzo /*
4917885a7bSLuigi Rizzo  * Some fields should be cache-aligned to reduce contention.
5017885a7bSLuigi Rizzo  * The alignment is architecture and OS dependent, but rather than
5117885a7bSLuigi Rizzo  * digging into OS headers to find the exact value we use an estimate
5217885a7bSLuigi Rizzo  * that should cover most architectures.
5317885a7bSLuigi Rizzo  */
5417885a7bSLuigi Rizzo #define NM_CACHE_ALIGN	128
5517885a7bSLuigi Rizzo 
5668b8534bSLuigi Rizzo /*
5768b8534bSLuigi Rizzo  * --- Netmap data structures ---
5868b8534bSLuigi Rizzo  *
59ce3ee1e7SLuigi Rizzo  * The userspace data structures used by netmap are shown below.
60ce3ee1e7SLuigi Rizzo  * They are allocated by the kernel and mmap()ed by userspace threads.
61ce3ee1e7SLuigi Rizzo  * Pointers are implemented as memory offsets or indexes,
62ce3ee1e7SLuigi Rizzo  * so that they can be easily dereferenced in kernel and userspace.
6368b8534bSLuigi Rizzo 
64ce3ee1e7SLuigi Rizzo    KERNEL (opaque, obviously)
6568b8534bSLuigi Rizzo 
6664ae02c3SLuigi Rizzo   ====================================================================
6764ae02c3SLuigi Rizzo                                          |
68ce3ee1e7SLuigi Rizzo    USERSPACE                             |      struct netmap_ring
6917885a7bSLuigi Rizzo                                          +---->+---------------+
7017885a7bSLuigi Rizzo                                              / | head,cur,tail |
7117885a7bSLuigi Rizzo    struct netmap_if (nifp, 1 per fd)        /  | buf_ofs       |
7217885a7bSLuigi Rizzo     +---------------+                      /   | other fields  |
7317885a7bSLuigi Rizzo     | ni_tx_rings   |                     /    +===============+
74ce3ee1e7SLuigi Rizzo     | ni_rx_rings   |                    /     | buf_idx, len  | slot[0]
75ce3ee1e7SLuigi Rizzo     |               |                   /      | flags, ptr    |
7617885a7bSLuigi Rizzo     |               |                  /       +---------------+
77ce3ee1e7SLuigi Rizzo     +===============+                 /        | buf_idx, len  | slot[1]
78ce3ee1e7SLuigi Rizzo     | txring_ofs[0] | (rel.to nifp)--'         | flags, ptr    |
7917885a7bSLuigi Rizzo     | txring_ofs[1] |                          +---------------+
80f0ea3689SLuigi Rizzo      (tx+1 entries)                           (num_slots entries)
81ce3ee1e7SLuigi Rizzo     | txring_ofs[t] |                          | buf_idx, len  | slot[n-1]
82ce3ee1e7SLuigi Rizzo     +---------------+                          | flags, ptr    |
8317885a7bSLuigi Rizzo     | rxring_ofs[0] |                          +---------------+
8468b8534bSLuigi Rizzo     | rxring_ofs[1] |
85f0ea3689SLuigi Rizzo      (rx+1 entries)
86ce3ee1e7SLuigi Rizzo     | rxring_ofs[r] |
8768b8534bSLuigi Rizzo     +---------------+
8868b8534bSLuigi Rizzo 
89f0ea3689SLuigi Rizzo  * For each "interface" (NIC, host stack, PIPE, VALE switch port) bound to
90f0ea3689SLuigi Rizzo  * a file descriptor, the mmap()ed region contains a (logically readonly)
91ce3ee1e7SLuigi Rizzo  * struct netmap_if pointing to struct netmap_ring's.
92f0ea3689SLuigi Rizzo  *
93ce3ee1e7SLuigi Rizzo  * There is one netmap_ring per physical NIC ring, plus one tx/rx ring
94f0ea3689SLuigi Rizzo  * pair attached to the host stack (this pair is unused for non-NIC ports).
95ce3ee1e7SLuigi Rizzo  *
96ce3ee1e7SLuigi Rizzo  * All physical/host stack ports share the same memory region,
97ce3ee1e7SLuigi Rizzo  * so that zero-copy can be implemented between them.
98ce3ee1e7SLuigi Rizzo  * VALE switch ports instead have separate memory regions.
99ce3ee1e7SLuigi Rizzo  *
100ce3ee1e7SLuigi Rizzo  * The netmap_ring is the userspace-visible replica of the NIC ring.
101ce3ee1e7SLuigi Rizzo  * Each slot has the index of a buffer (MTU-sized and residing in the
102ce3ee1e7SLuigi Rizzo  * mmapped region), its length and some flags. An extra 64-bit pointer
103ce3ee1e7SLuigi Rizzo  * is provided for user-supplied buffers in the tx path.
104ce3ee1e7SLuigi Rizzo  *
10568b8534bSLuigi Rizzo  * In user space, the buffer address is computed as
10664ae02c3SLuigi Rizzo  *	(char *)ring + buf_ofs + index * NETMAP_BUF_SIZE
107f0ea3689SLuigi Rizzo  *
108f0ea3689SLuigi Rizzo  * Added in NETMAP_API 11:
109f0ea3689SLuigi Rizzo  *
110f0ea3689SLuigi Rizzo  * + NIOCREGIF can request the allocation of extra spare buffers from
111f0ea3689SLuigi Rizzo  *   the same memory pool. The desired number of buffers must be in
112f0ea3689SLuigi Rizzo  *   nr_arg3. The ioctl may return fewer buffers, depending on memory
113f0ea3689SLuigi Rizzo  *   availability. nr_arg3 will return the actual value, and, once
114f0ea3689SLuigi Rizzo  *   mapped, nifp->ni_bufs_head will be the index of the first buffer.
115f0ea3689SLuigi Rizzo  *
116f0ea3689SLuigi Rizzo  *   The buffers are linked to each other using the first uint32_t
117f0ea3689SLuigi Rizzo  *   as the index. On close, ni_bufs_head must point to the list of
118f0ea3689SLuigi Rizzo  *   buffers to be released.
119f0ea3689SLuigi Rizzo  *
120f0ea3689SLuigi Rizzo  * + NIOCREGIF can request space for extra rings (and buffers)
121f0ea3689SLuigi Rizzo  *   allocated in the same memory space. The number of extra rings
122f0ea3689SLuigi Rizzo  *   is in nr_arg1, and is advisory. This is a no-op on NICs where
123f0ea3689SLuigi Rizzo  *   the size of the memory space is fixed.
124f0ea3689SLuigi Rizzo  *
125f0ea3689SLuigi Rizzo  * + NIOCREGIF can attach to PIPE rings sharing the same memory
126f0ea3689SLuigi Rizzo  *   space with a parent device. The ifname indicates the parent device,
127f0ea3689SLuigi Rizzo  *   which must already exist. Flags in nr_flags indicate if we want to
128f0ea3689SLuigi Rizzo  *   bind the master or slave side, the index (from nr_ringid)
1295c8c1004SLuigi Rizzo  *   is just a cookie and does not need to be sequential.
130f0ea3689SLuigi Rizzo  *
131f0ea3689SLuigi Rizzo  * + NIOCREGIF can also attach to 'monitor' rings that replicate
132f0ea3689SLuigi Rizzo  *   the content of specific rings, also from the same memory space.
133f0ea3689SLuigi Rizzo  *
134f0ea3689SLuigi Rizzo  *   Extra flags in nr_flags support the above functions.
135f0ea3689SLuigi Rizzo  *   Application libraries may use the following naming scheme:
136f0ea3689SLuigi Rizzo  *	netmap:foo			all NIC ring pairs
137f0ea3689SLuigi Rizzo  *	netmap:foo^			only host ring pair
138f0ea3689SLuigi Rizzo  *	netmap:foo+			all NIC ring + host ring pairs
139f0ea3689SLuigi Rizzo  *	netmap:foo-k			the k-th NIC ring pair
140f0ea3689SLuigi Rizzo  *	netmap:foo{k			PIPE ring pair k, master side
141f0ea3689SLuigi Rizzo  *	netmap:foo}k			PIPE ring pair k, slave side
14237e3a6d3SLuigi Rizzo  *
14337e3a6d3SLuigi Rizzo  * Some notes about host rings:
14437e3a6d3SLuigi Rizzo  *
14537e3a6d3SLuigi Rizzo  * + The RX host ring is used to store those packets that the host network
14637e3a6d3SLuigi Rizzo  *   stack is trying to transmit through a NIC queue, but only if that queue
14737e3a6d3SLuigi Rizzo  *   is currently in netmap mode. Netmap will not intercept host stack mbufs
14837e3a6d3SLuigi Rizzo  *   designated to NIC queues that are not in netmap mode. As a consequence,
14937e3a6d3SLuigi Rizzo  *   registering a netmap port with netmap:foo^ is not enough to intercept
15037e3a6d3SLuigi Rizzo  *   mbufs in the RX host ring; the netmap port should be registered with
15137e3a6d3SLuigi Rizzo  *   netmap:foo*, or another registration should be done to open at least a
15237e3a6d3SLuigi Rizzo  *   NIC TX queue in netmap mode.
15337e3a6d3SLuigi Rizzo  *
15437e3a6d3SLuigi Rizzo  * + Netmap is not currently able to deal with intercepted trasmit mbufs which
15537e3a6d3SLuigi Rizzo  *   require offloadings like TSO, UFO, checksumming offloadings, etc. It is
15637e3a6d3SLuigi Rizzo  *   responsibility of the user to disable those offloadings (e.g. using
15737e3a6d3SLuigi Rizzo  *   ifconfig on FreeBSD or ethtool -K on Linux) for an interface that is being
15837e3a6d3SLuigi Rizzo  *   used in netmap mode. If the offloadings are not disabled, GSO and/or
15937e3a6d3SLuigi Rizzo  *   unchecksummed packets may be dropped immediately or end up in the host RX
16037e3a6d3SLuigi Rizzo  *   ring, and will be dropped as soon as the packet reaches another netmap
16137e3a6d3SLuigi Rizzo  *   adapter.
162ce3ee1e7SLuigi Rizzo  */
163ce3ee1e7SLuigi Rizzo 
164ce3ee1e7SLuigi Rizzo /*
165ce3ee1e7SLuigi Rizzo  * struct netmap_slot is a buffer descriptor
16617885a7bSLuigi Rizzo  */
16717885a7bSLuigi Rizzo struct netmap_slot {
16817885a7bSLuigi Rizzo 	uint32_t buf_idx;	/* buffer index */
16917885a7bSLuigi Rizzo 	uint16_t len;		/* length for this slot */
17017885a7bSLuigi Rizzo 	uint16_t flags;		/* buf changed, etc. */
17117885a7bSLuigi Rizzo 	uint64_t ptr;		/* pointer for indirect buffers */
17217885a7bSLuigi Rizzo };
17317885a7bSLuigi Rizzo 
17417885a7bSLuigi Rizzo /*
17517885a7bSLuigi Rizzo  * The following flags control how the slot is used
17617885a7bSLuigi Rizzo  */
17717885a7bSLuigi Rizzo 
17817885a7bSLuigi Rizzo #define	NS_BUF_CHANGED	0x0001	/* buf_idx changed */
17917885a7bSLuigi Rizzo 	/*
18017885a7bSLuigi Rizzo 	 * must be set whenever buf_idx is changed (as it might be
18117885a7bSLuigi Rizzo 	 * necessary to recompute the physical address and mapping)
182847bf383SLuigi Rizzo 	 *
183847bf383SLuigi Rizzo 	 * It is also set by the kernel whenever the buf_idx is
184847bf383SLuigi Rizzo 	 * changed internally (e.g., by pipes). Applications may
185847bf383SLuigi Rizzo 	 * use this information to know when they can reuse the
186847bf383SLuigi Rizzo 	 * contents of previously prepared buffers.
18717885a7bSLuigi Rizzo 	 */
18817885a7bSLuigi Rizzo 
18917885a7bSLuigi Rizzo #define	NS_REPORT	0x0002	/* ask the hardware to report results */
19017885a7bSLuigi Rizzo 	/*
19117885a7bSLuigi Rizzo 	 * Request notification when slot is used by the hardware.
19217885a7bSLuigi Rizzo 	 * Normally transmit completions are handled lazily and
19317885a7bSLuigi Rizzo 	 * may be unreported. This flag lets us know when a slot
19417885a7bSLuigi Rizzo 	 * has been sent (e.g. to terminate the sender).
19517885a7bSLuigi Rizzo 	 */
19617885a7bSLuigi Rizzo 
19717885a7bSLuigi Rizzo #define	NS_FORWARD	0x0004	/* pass packet 'forward' */
19817885a7bSLuigi Rizzo 	/*
19917885a7bSLuigi Rizzo 	 * (Only for physical ports, rx rings with NR_FORWARD set).
20017885a7bSLuigi Rizzo 	 * Slot released to the kernel (i.e. before ring->head) with
20117885a7bSLuigi Rizzo 	 * this flag set are passed to the peer ring (host/NIC),
20217885a7bSLuigi Rizzo 	 * thus restoring the host-NIC connection for these slots.
20317885a7bSLuigi Rizzo 	 * This supports efficient traffic monitoring or firewalling.
20417885a7bSLuigi Rizzo 	 */
20517885a7bSLuigi Rizzo 
20617885a7bSLuigi Rizzo #define	NS_NO_LEARN	0x0008	/* disable bridge learning */
20717885a7bSLuigi Rizzo  	/*
20817885a7bSLuigi Rizzo 	 * On a VALE switch, do not 'learn' the source port for
20917885a7bSLuigi Rizzo  	 * this buffer.
21017885a7bSLuigi Rizzo 	 */
21117885a7bSLuigi Rizzo 
21217885a7bSLuigi Rizzo #define	NS_INDIRECT	0x0010	/* userspace buffer */
21317885a7bSLuigi Rizzo  	/*
21417885a7bSLuigi Rizzo 	 * (VALE tx rings only) data is in a userspace buffer,
21517885a7bSLuigi Rizzo 	 * whose address is in the 'ptr' field in the slot.
21617885a7bSLuigi Rizzo 	 */
21717885a7bSLuigi Rizzo 
21817885a7bSLuigi Rizzo #define	NS_MOREFRAG	0x0020	/* packet has more fragments */
21917885a7bSLuigi Rizzo  	/*
220*4f80b14cSVincenzo Maffione 	 * (VALE ports, ptnetmap ports and some NIC ports, e.g.
221*4f80b14cSVincenzo Maffione          * ixgbe and i40e on Linux)
22217885a7bSLuigi Rizzo 	 * Set on all but the last slot of a multi-segment packet.
22317885a7bSLuigi Rizzo 	 * The 'len' field refers to the individual fragment.
22417885a7bSLuigi Rizzo 	 */
22517885a7bSLuigi Rizzo 
22617885a7bSLuigi Rizzo #define	NS_PORT_SHIFT	8
22717885a7bSLuigi Rizzo #define	NS_PORT_MASK	(0xff << NS_PORT_SHIFT)
22817885a7bSLuigi Rizzo 	/*
22917885a7bSLuigi Rizzo  	 * The high 8 bits of the flag, if not zero, indicate the
23001c039a1SLuigi Rizzo 	 * destination port for the VALE switch, overriding
23101c039a1SLuigi Rizzo  	 * the lookup table.
23268b8534bSLuigi Rizzo  	 */
23301c039a1SLuigi Rizzo 
23417885a7bSLuigi Rizzo #define	NS_RFRAGS(_slot)	( ((_slot)->flags >> 8) & 0xff)
235ce3ee1e7SLuigi Rizzo 	/*
23617885a7bSLuigi Rizzo 	 * (VALE rx rings only) the high 8 bits
237ce3ee1e7SLuigi Rizzo 	 *  are the number of fragments.
238ce3ee1e7SLuigi Rizzo 	 */
23917885a7bSLuigi Rizzo 
24068b8534bSLuigi Rizzo 
24168b8534bSLuigi Rizzo /*
242ce3ee1e7SLuigi Rizzo  * struct netmap_ring
243ce3ee1e7SLuigi Rizzo  *
24468b8534bSLuigi Rizzo  * Netmap representation of a TX or RX ring (also known as "queue").
24568b8534bSLuigi Rizzo  * This is a queue implemented as a fixed-size circular array.
24617885a7bSLuigi Rizzo  * At the software level the important fields are: head, cur, tail.
24768b8534bSLuigi Rizzo  *
24868b8534bSLuigi Rizzo  * In TX rings:
249ce3ee1e7SLuigi Rizzo  *
25017885a7bSLuigi Rizzo  *	head	first slot available for transmission.
25117885a7bSLuigi Rizzo  *	cur	wakeup point. select() and poll() will unblock
25217885a7bSLuigi Rizzo  *		when 'tail' moves past 'cur'
25317885a7bSLuigi Rizzo  *	tail	(readonly) first slot reserved to the kernel
254ce3ee1e7SLuigi Rizzo  *
25517885a7bSLuigi Rizzo  *	[head .. tail-1] can be used for new packets to send;
25617885a7bSLuigi Rizzo  *	'head' and 'cur' must be incremented as slots are filled
25717885a7bSLuigi Rizzo  *	    with new packets to be sent;
25817885a7bSLuigi Rizzo  *	'cur' can be moved further ahead if we need more space
2595c8c1004SLuigi Rizzo  *	for new transmissions. XXX todo (2014-03-12)
26068b8534bSLuigi Rizzo  *
26168b8534bSLuigi Rizzo  * In RX rings:
262ce3ee1e7SLuigi Rizzo  *
26317885a7bSLuigi Rizzo  *	head	first valid received packet
26417885a7bSLuigi Rizzo  *	cur	wakeup point. select() and poll() will unblock
26517885a7bSLuigi Rizzo  *		when 'tail' moves past 'cur'
26617885a7bSLuigi Rizzo  *	tail	(readonly) first slot reserved to the kernel
267ce3ee1e7SLuigi Rizzo  *
26817885a7bSLuigi Rizzo  *	[head .. tail-1] contain received packets;
26917885a7bSLuigi Rizzo  *	'head' and 'cur' must be incremented as slots are consumed
27017885a7bSLuigi Rizzo  *		and can be returned to the kernel;
27117885a7bSLuigi Rizzo  *	'cur' can be moved further ahead if we want to wait for
27217885a7bSLuigi Rizzo  *		new packets without returning the previous ones.
27368b8534bSLuigi Rizzo  *
27468b8534bSLuigi Rizzo  * DATA OWNERSHIP/LOCKING:
27517885a7bSLuigi Rizzo  *	The netmap_ring, and all slots and buffers in the range
27617885a7bSLuigi Rizzo  *	[head .. tail-1] are owned by the user program;
27717885a7bSLuigi Rizzo  *	the kernel only accesses them during a netmap system call
27817885a7bSLuigi Rizzo  *	and in the user thread context.
27901c039a1SLuigi Rizzo  *
28017885a7bSLuigi Rizzo  *	Other slots and buffers are reserved for use by the kernel
28168b8534bSLuigi Rizzo  */
28268b8534bSLuigi Rizzo struct netmap_ring {
28368b8534bSLuigi Rizzo 	/*
284ce3ee1e7SLuigi Rizzo 	 * buf_ofs is meant to be used through macros.
28568b8534bSLuigi Rizzo 	 * It contains the offset of the buffer region from this
28668b8534bSLuigi Rizzo 	 * descriptor.
28768b8534bSLuigi Rizzo 	 */
28817885a7bSLuigi Rizzo 	const int64_t	buf_ofs;
28968b8534bSLuigi Rizzo 	const uint32_t	num_slots;	/* number of slots in the ring. */
29017885a7bSLuigi Rizzo 	const uint32_t	nr_buf_size;
29117885a7bSLuigi Rizzo 	const uint16_t	ringid;
29217885a7bSLuigi Rizzo 	const uint16_t	dir;		/* 0: tx, 1: rx */
29368b8534bSLuigi Rizzo 
29417885a7bSLuigi Rizzo 	uint32_t        head;		/* (u) first user slot */
29517885a7bSLuigi Rizzo 	uint32_t        cur;		/* (u) wakeup point */
29617885a7bSLuigi Rizzo 	uint32_t	tail;		/* (k) first kernel slot */
29768b8534bSLuigi Rizzo 
29817885a7bSLuigi Rizzo 	uint32_t	flags;
29917885a7bSLuigi Rizzo 
30017885a7bSLuigi Rizzo 	struct timeval	ts;		/* (k) time of last *sync() */
30117885a7bSLuigi Rizzo 
30217885a7bSLuigi Rizzo 	/* opaque room for a mutex or similar object */
30337e3a6d3SLuigi Rizzo #if !defined(_WIN32) || defined(__CYGWIN__)
30437e3a6d3SLuigi Rizzo 	uint8_t	__attribute__((__aligned__(NM_CACHE_ALIGN))) sem[128];
30537e3a6d3SLuigi Rizzo #else
30637e3a6d3SLuigi Rizzo 	uint8_t	__declspec(align(NM_CACHE_ALIGN)) sem[128];
30737e3a6d3SLuigi Rizzo #endif
30868b8534bSLuigi Rizzo 
30968b8534bSLuigi Rizzo 	/* the slots follow. This struct has variable size */
31068b8534bSLuigi Rizzo 	struct netmap_slot slot[0];	/* array of slots. */
31168b8534bSLuigi Rizzo };
31268b8534bSLuigi Rizzo 
31368b8534bSLuigi Rizzo 
31468b8534bSLuigi Rizzo /*
31517885a7bSLuigi Rizzo  * RING FLAGS
31617885a7bSLuigi Rizzo  */
31717885a7bSLuigi Rizzo #define	NR_TIMESTAMP	0x0002		/* set timestamp on *sync() */
31817885a7bSLuigi Rizzo 	/*
31917885a7bSLuigi Rizzo 	 * updates the 'ts' field on each netmap syscall. This saves
32017885a7bSLuigi Rizzo 	 * saves a separate gettimeofday(), and is not much worse than
32117885a7bSLuigi Rizzo 	 * software timestamps generated in the interrupt handler.
32217885a7bSLuigi Rizzo 	 */
32317885a7bSLuigi Rizzo 
32417885a7bSLuigi Rizzo #define	NR_FORWARD	0x0004		/* enable NS_FORWARD for ring */
32517885a7bSLuigi Rizzo  	/*
32617885a7bSLuigi Rizzo 	 * Enables the NS_FORWARD slot flag for the ring.
32717885a7bSLuigi Rizzo 	 */
32817885a7bSLuigi Rizzo 
32917885a7bSLuigi Rizzo 
33017885a7bSLuigi Rizzo /*
33168b8534bSLuigi Rizzo  * Netmap representation of an interface and its queue(s).
332ce3ee1e7SLuigi Rizzo  * This is initialized by the kernel when binding a file
333ce3ee1e7SLuigi Rizzo  * descriptor to a port, and should be considered as readonly
334ce3ee1e7SLuigi Rizzo  * by user programs. The kernel never uses it.
335ce3ee1e7SLuigi Rizzo  *
33668b8534bSLuigi Rizzo  * There is one netmap_if for each file descriptor on which we want
337ce3ee1e7SLuigi Rizzo  * to select/poll.
33868b8534bSLuigi Rizzo  * select/poll operates on one or all pairs depending on the value of
33968b8534bSLuigi Rizzo  * nmr_queueid passed on the ioctl.
34068b8534bSLuigi Rizzo  */
34168b8534bSLuigi Rizzo struct netmap_if {
34268b8534bSLuigi Rizzo 	char		ni_name[IFNAMSIZ]; /* name of the interface. */
343ce3ee1e7SLuigi Rizzo 	const uint32_t	ni_version;	/* API version, currently unused */
344ce3ee1e7SLuigi Rizzo 	const uint32_t	ni_flags;	/* properties */
345ce3ee1e7SLuigi Rizzo #define	NI_PRIV_MEM	0x1		/* private memory region */
346ce3ee1e7SLuigi Rizzo 
34717885a7bSLuigi Rizzo 	/*
34817885a7bSLuigi Rizzo 	 * The number of packet rings available in netmap mode.
34917885a7bSLuigi Rizzo 	 * Physical NICs can have different numbers of tx and rx rings.
35017885a7bSLuigi Rizzo 	 * Physical NICs also have a 'host' ring pair.
35117885a7bSLuigi Rizzo 	 * Additionally, clients can request additional ring pairs to
35217885a7bSLuigi Rizzo 	 * be used for internal communication.
35317885a7bSLuigi Rizzo 	 */
35417885a7bSLuigi Rizzo 	const uint32_t	ni_tx_rings;	/* number of HW tx rings */
35517885a7bSLuigi Rizzo 	const uint32_t	ni_rx_rings;	/* number of HW rx rings */
35617885a7bSLuigi Rizzo 
357f0ea3689SLuigi Rizzo 	uint32_t	ni_bufs_head;	/* head index for extra bufs */
358f0ea3689SLuigi Rizzo 	uint32_t	ni_spare1[5];
35968b8534bSLuigi Rizzo 	/*
36064ae02c3SLuigi Rizzo 	 * The following array contains the offset of each netmap ring
36117885a7bSLuigi Rizzo 	 * from this structure, in the following order:
36217885a7bSLuigi Rizzo 	 * NIC tx rings (ni_tx_rings); host tx ring (1); extra tx rings;
36317885a7bSLuigi Rizzo 	 * NIC rx rings (ni_rx_rings); host tx ring (1); extra rx rings.
36417885a7bSLuigi Rizzo 	 *
365ce3ee1e7SLuigi Rizzo 	 * The area is filled up by the kernel on NIOCREGIF,
36668b8534bSLuigi Rizzo 	 * and then only read by userspace code.
36768b8534bSLuigi Rizzo 	 */
36868b8534bSLuigi Rizzo 	const ssize_t	ring_ofs[0];
36968b8534bSLuigi Rizzo };
37068b8534bSLuigi Rizzo 
37117885a7bSLuigi Rizzo 
37268b8534bSLuigi Rizzo #ifndef NIOCREGIF
37368b8534bSLuigi Rizzo /*
37468b8534bSLuigi Rizzo  * ioctl names and related fields
37568b8534bSLuigi Rizzo  *
37617885a7bSLuigi Rizzo  * NIOCTXSYNC, NIOCRXSYNC synchronize tx or rx queues,
37717885a7bSLuigi Rizzo  *	whose identity is set in NIOCREGIF through nr_ringid.
37817885a7bSLuigi Rizzo  *	These are non blocking and take no argument.
37917885a7bSLuigi Rizzo  *
38068b8534bSLuigi Rizzo  * NIOCGINFO takes a struct ifreq, the interface name is the input,
38168b8534bSLuigi Rizzo  *	the outputs are number of queues and number of descriptor
38268b8534bSLuigi Rizzo  *	for each queue (useful to set number of threads etc.).
383ce3ee1e7SLuigi Rizzo  *	The info returned is only advisory and may change before
384ce3ee1e7SLuigi Rizzo  *	the interface is bound to a file descriptor.
38568b8534bSLuigi Rizzo  *
38617885a7bSLuigi Rizzo  * NIOCREGIF takes an interface name within a struct nmre,
38768b8534bSLuigi Rizzo  *	and activates netmap mode on the interface (if possible).
38868b8534bSLuigi Rizzo  *
38917885a7bSLuigi Rizzo  * The argument to NIOCGINFO/NIOCREGIF overlays struct ifreq so we
39017885a7bSLuigi Rizzo  * can pass it down to other NIC-related ioctls.
391f18be576SLuigi Rizzo  *
39217885a7bSLuigi Rizzo  * The actual argument (struct nmreq) has a number of options to request
39317885a7bSLuigi Rizzo  * different functions.
394f0ea3689SLuigi Rizzo  * The following are used in NIOCREGIF when nr_cmd == 0:
395f18be576SLuigi Rizzo  *
39617885a7bSLuigi Rizzo  * nr_name	(in)
39717885a7bSLuigi Rizzo  *	The name of the port (em0, valeXXX:YYY, etc.)
39817885a7bSLuigi Rizzo  *	limited to IFNAMSIZ for backward compatibility.
399ce3ee1e7SLuigi Rizzo  *
40017885a7bSLuigi Rizzo  * nr_version	(in/out)
40117885a7bSLuigi Rizzo  *	Must match NETMAP_API as used in the kernel, error otherwise.
40217885a7bSLuigi Rizzo  *	Always returns the desired value on output.
40317885a7bSLuigi Rizzo  *
40417885a7bSLuigi Rizzo  * nr_tx_slots, nr_tx_slots, nr_tx_rings, nr_rx_rings (in/out)
40517885a7bSLuigi Rizzo  *	On input, non-zero values may be used to reconfigure the port
40617885a7bSLuigi Rizzo  *	according to the requested values, but this is not guaranteed.
40717885a7bSLuigi Rizzo  *	On output the actual values in use are reported.
40817885a7bSLuigi Rizzo  *
40917885a7bSLuigi Rizzo  * nr_ringid (in)
41017885a7bSLuigi Rizzo  *	Indicates how rings should be bound to the file descriptors.
411f0ea3689SLuigi Rizzo  *	If nr_flags != 0, then the low bits (in NETMAP_RING_MASK)
412f0ea3689SLuigi Rizzo  *	are used to indicate the ring number, and nr_flags specifies
413f0ea3689SLuigi Rizzo  *	the actual rings to bind. NETMAP_NO_TX_POLL is unaffected.
414f0ea3689SLuigi Rizzo  *
415f0ea3689SLuigi Rizzo  *	NOTE: THE FOLLOWING (nr_flags == 0) IS DEPRECATED:
416f0ea3689SLuigi Rizzo  *	If nr_flags == 0, NETMAP_HW_RING and NETMAP_SW_RING control
417f0ea3689SLuigi Rizzo  *	the binding as follows:
41817885a7bSLuigi Rizzo  *	0 (default)			binds all physical rings
41917885a7bSLuigi Rizzo  *	NETMAP_HW_RING | ring number	binds a single ring pair
420ce3ee1e7SLuigi Rizzo  *	NETMAP_SW_RING			binds only the host tx/rx rings
421ce3ee1e7SLuigi Rizzo  *
42217885a7bSLuigi Rizzo  *	NETMAP_NO_TX_POLL can be OR-ed to make select()/poll() push
42317885a7bSLuigi Rizzo  *		packets on tx rings only if POLLOUT is set.
42417885a7bSLuigi Rizzo  *		The default is to push any pending packet.
425ce3ee1e7SLuigi Rizzo  *
426f0ea3689SLuigi Rizzo  *	NETMAP_DO_RX_POLL can be OR-ed to make select()/poll() release
427f0ea3689SLuigi Rizzo  *		packets on rx rings also when POLLIN is NOT set.
428f0ea3689SLuigi Rizzo  *		The default is to touch the rx ring only with POLLIN.
429f0ea3689SLuigi Rizzo  *		Note that this is the opposite of TX because it
430f0ea3689SLuigi Rizzo  *		reflects the common usage.
431f0ea3689SLuigi Rizzo  *
432f0ea3689SLuigi Rizzo  *	NOTE: NETMAP_PRIV_MEM IS DEPRECATED, use nr_arg2 instead.
433f0ea3689SLuigi Rizzo  *	NETMAP_PRIV_MEM is set on return for ports that do not use
434f0ea3689SLuigi Rizzo  *		the global memory allocator.
435f0ea3689SLuigi Rizzo  *		This information is not significant and applications
436f0ea3689SLuigi Rizzo  *		should look at the region id in nr_arg2
437f0ea3689SLuigi Rizzo  *
438f0ea3689SLuigi Rizzo  * nr_flags	is the recommended mode to indicate which rings should
439f0ea3689SLuigi Rizzo  *		be bound to a file descriptor. Values are NR_REG_*
440f0ea3689SLuigi Rizzo  *
441f0ea3689SLuigi Rizzo  * nr_arg1 (in)	The number of extra rings to be reserved.
442f0ea3689SLuigi Rizzo  *		Especially when allocating a VALE port the system only
443f0ea3689SLuigi Rizzo  *		allocates the amount of memory needed for the port.
444f0ea3689SLuigi Rizzo  *		If more shared memory rings are desired (e.g. for pipes),
445f0ea3689SLuigi Rizzo  *		the first invocation for the same basename/allocator
446f0ea3689SLuigi Rizzo  *		should specify a suitable number. Memory cannot be
447f0ea3689SLuigi Rizzo  *		extended after the first allocation without closing
448f0ea3689SLuigi Rizzo  *		all ports on the same region.
449f0ea3689SLuigi Rizzo  *
450f0ea3689SLuigi Rizzo  * nr_arg2 (in/out) The identity of the memory region used.
451f0ea3689SLuigi Rizzo  *		On input, 0 means the system decides autonomously,
452f0ea3689SLuigi Rizzo  *		other values may try to select a specific region.
453f0ea3689SLuigi Rizzo  *		On return the actual value is reported.
454f0ea3689SLuigi Rizzo  *		Region '1' is the global allocator, normally shared
455f0ea3689SLuigi Rizzo  *		by all interfaces. Other values are private regions.
456f0ea3689SLuigi Rizzo  *		If two ports the same region zero-copy is possible.
457f0ea3689SLuigi Rizzo  *
458f0ea3689SLuigi Rizzo  * nr_arg3 (in/out)	number of extra buffers to be allocated.
459f0ea3689SLuigi Rizzo  *
460f0ea3689SLuigi Rizzo  *
461ce3ee1e7SLuigi Rizzo  *
46217885a7bSLuigi Rizzo  * nr_cmd (in)	if non-zero indicates a special command:
46317885a7bSLuigi Rizzo  *	NETMAP_BDG_ATTACH	 and nr_name = vale*:ifname
46417885a7bSLuigi Rizzo  *		attaches the NIC to the switch; nr_ringid specifies
46517885a7bSLuigi Rizzo  *		which rings to use. Used by vale-ctl -a ...
46617885a7bSLuigi Rizzo  *	    nr_arg1 = NETMAP_BDG_HOST also attaches the host port
46717885a7bSLuigi Rizzo  *		as in vale-ctl -h ...
468ce3ee1e7SLuigi Rizzo  *
46917885a7bSLuigi Rizzo  *	NETMAP_BDG_DETACH	and nr_name = vale*:ifname
47017885a7bSLuigi Rizzo  *		disconnects a previously attached NIC.
47117885a7bSLuigi Rizzo  *		Used by vale-ctl -d ...
472ce3ee1e7SLuigi Rizzo  *
47317885a7bSLuigi Rizzo  *	NETMAP_BDG_LIST
47417885a7bSLuigi Rizzo  *		list the configuration of VALE switches.
47568b8534bSLuigi Rizzo  *
476f0ea3689SLuigi Rizzo  *	NETMAP_BDG_VNET_HDR
477f0ea3689SLuigi Rizzo  *		Set the virtio-net header length used by the client
478f0ea3689SLuigi Rizzo  *		of a VALE switch port.
479f18be576SLuigi Rizzo  *
4804bf50f18SLuigi Rizzo  *	NETMAP_BDG_NEWIF
4814bf50f18SLuigi Rizzo  *		create a persistent VALE port with name nr_name.
4824bf50f18SLuigi Rizzo  *		Used by vale-ctl -n ...
4834bf50f18SLuigi Rizzo  *
4844bf50f18SLuigi Rizzo  *	NETMAP_BDG_DELIF
4854bf50f18SLuigi Rizzo  *		delete a persistent VALE port. Used by vale-ctl -d ...
4864bf50f18SLuigi Rizzo  *
487f0ea3689SLuigi Rizzo  * nr_arg1, nr_arg2, nr_arg3  (in/out)		command specific
488f0ea3689SLuigi Rizzo  *
489f0ea3689SLuigi Rizzo  *
49017885a7bSLuigi Rizzo  *
49168b8534bSLuigi Rizzo  */
49268b8534bSLuigi Rizzo 
49317885a7bSLuigi Rizzo 
49468b8534bSLuigi Rizzo /*
495f0ea3689SLuigi Rizzo  * struct nmreq overlays a struct ifreq (just the name)
49668b8534bSLuigi Rizzo  */
49768b8534bSLuigi Rizzo struct nmreq {
49868b8534bSLuigi Rizzo 	char		nr_name[IFNAMSIZ];
49964ae02c3SLuigi Rizzo 	uint32_t	nr_version;	/* API version */
50068b8534bSLuigi Rizzo 	uint32_t	nr_offset;	/* nifp offset in the shared region */
50168b8534bSLuigi Rizzo 	uint32_t	nr_memsize;	/* size of the shared region */
50264ae02c3SLuigi Rizzo 	uint32_t	nr_tx_slots;	/* slots in tx rings */
50364ae02c3SLuigi Rizzo 	uint32_t	nr_rx_slots;	/* slots in rx rings */
50464ae02c3SLuigi Rizzo 	uint16_t	nr_tx_rings;	/* number of tx rings */
50564ae02c3SLuigi Rizzo 	uint16_t	nr_rx_rings;	/* number of rx rings */
506f0ea3689SLuigi Rizzo 
50768b8534bSLuigi Rizzo 	uint16_t	nr_ringid;	/* ring(s) we care about */
508f0ea3689SLuigi Rizzo #define NETMAP_HW_RING		0x4000	/* single NIC ring pair */
509f0ea3689SLuigi Rizzo #define NETMAP_SW_RING		0x2000	/* only host ring pair */
510f0ea3689SLuigi Rizzo 
511f0ea3689SLuigi Rizzo #define NETMAP_RING_MASK	0x0fff	/* the ring number */
512f0ea3689SLuigi Rizzo 
51364ae02c3SLuigi Rizzo #define NETMAP_NO_TX_POLL	0x1000	/* no automatic txsync on poll */
514f0ea3689SLuigi Rizzo 
515f0ea3689SLuigi Rizzo #define NETMAP_DO_RX_POLL	0x8000	/* DO automatic rxsync on poll */
51617885a7bSLuigi Rizzo 
517f18be576SLuigi Rizzo 	uint16_t	nr_cmd;
518f18be576SLuigi Rizzo #define NETMAP_BDG_ATTACH	1	/* attach the NIC */
519f18be576SLuigi Rizzo #define NETMAP_BDG_DETACH	2	/* detach the NIC */
5204bf50f18SLuigi Rizzo #define NETMAP_BDG_REGOPS	3	/* register bridge callbacks */
521f18be576SLuigi Rizzo #define NETMAP_BDG_LIST		4	/* get bridge's info */
522f0ea3689SLuigi Rizzo #define NETMAP_BDG_VNET_HDR     5       /* set the port virtio-net-hdr length */
523f0ea3689SLuigi Rizzo #define NETMAP_BDG_OFFSET	NETMAP_BDG_VNET_HDR	/* deprecated alias */
5244bf50f18SLuigi Rizzo #define NETMAP_BDG_NEWIF	6	/* create a virtual port */
5254bf50f18SLuigi Rizzo #define NETMAP_BDG_DELIF	7	/* destroy a virtual port */
52637e3a6d3SLuigi Rizzo #define NETMAP_PT_HOST_CREATE	8	/* create ptnetmap kthreads */
52737e3a6d3SLuigi Rizzo #define NETMAP_PT_HOST_DELETE	9	/* delete ptnetmap kthreads */
52837e3a6d3SLuigi Rizzo #define NETMAP_BDG_POLLING_ON	10	/* delete polling kthread */
52937e3a6d3SLuigi Rizzo #define NETMAP_BDG_POLLING_OFF	11	/* delete polling kthread */
53037e3a6d3SLuigi Rizzo #define NETMAP_VNET_HDR_GET	12      /* get the port virtio-net-hdr length */
531844a6f0cSLuigi Rizzo #define NETMAP_POOLS_INFO_GET	13	/* get memory allocator pools info */
532*4f80b14cSVincenzo Maffione #define NETMAP_POOLS_CREATE	14	/* create a new memory allocator */
533f0ea3689SLuigi Rizzo 	uint16_t	nr_arg1;	/* reserve extra rings in NIOCREGIF */
534f18be576SLuigi Rizzo #define NETMAP_BDG_HOST		1	/* attach the host stack on ATTACH */
53517885a7bSLuigi Rizzo 
536f18be576SLuigi Rizzo 	uint16_t	nr_arg2;
537f0ea3689SLuigi Rizzo 	uint32_t	nr_arg3;	/* req. extra buffers in NIOCREGIF */
538f0ea3689SLuigi Rizzo 	uint32_t	nr_flags;
539f0ea3689SLuigi Rizzo 	/* various modes, extends nr_ringid */
540f0ea3689SLuigi Rizzo 	uint32_t	spare2[1];
54168b8534bSLuigi Rizzo };
54268b8534bSLuigi Rizzo 
543f0ea3689SLuigi Rizzo #define NR_REG_MASK		0xf /* values for nr_flags */
544f0ea3689SLuigi Rizzo enum {	NR_REG_DEFAULT	= 0,	/* backward compat, should not be used. */
545f0ea3689SLuigi Rizzo 	NR_REG_ALL_NIC	= 1,
546f0ea3689SLuigi Rizzo 	NR_REG_SW	= 2,
547f0ea3689SLuigi Rizzo 	NR_REG_NIC_SW	= 3,
548f0ea3689SLuigi Rizzo 	NR_REG_ONE_NIC	= 4,
549f0ea3689SLuigi Rizzo 	NR_REG_PIPE_MASTER = 5,
550f0ea3689SLuigi Rizzo 	NR_REG_PIPE_SLAVE = 6,
551f0ea3689SLuigi Rizzo };
552f0ea3689SLuigi Rizzo /* monitor uses the NR_REG to select the rings to monitor */
553f0ea3689SLuigi Rizzo #define NR_MONITOR_TX	0x100
554f0ea3689SLuigi Rizzo #define NR_MONITOR_RX	0x200
555847bf383SLuigi Rizzo #define NR_ZCOPY_MON	0x400
556847bf383SLuigi Rizzo /* request exclusive access to the selected rings */
557847bf383SLuigi Rizzo #define NR_EXCLUSIVE	0x800
55837e3a6d3SLuigi Rizzo /* request ptnetmap host support */
55937e3a6d3SLuigi Rizzo #define NR_PASSTHROUGH_HOST	NR_PTNETMAP_HOST /* deprecated */
56037e3a6d3SLuigi Rizzo #define NR_PTNETMAP_HOST	0x1000
56137e3a6d3SLuigi Rizzo #define NR_RX_RINGS_ONLY	0x2000
56237e3a6d3SLuigi Rizzo #define NR_TX_RINGS_ONLY	0x4000
56337e3a6d3SLuigi Rizzo /* Applications set this flag if they are able to deal with virtio-net headers,
56437e3a6d3SLuigi Rizzo  * that is send/receive frames that start with a virtio-net header.
56537e3a6d3SLuigi Rizzo  * If not set, NIOCREGIF will fail with netmap ports that require applications
56637e3a6d3SLuigi Rizzo  * to use those headers. If the flag is set, the application can use the
56737e3a6d3SLuigi Rizzo  * NETMAP_VNET_HDR_GET command to figure out the header length. */
56837e3a6d3SLuigi Rizzo #define NR_ACCEPT_VNET_HDR	0x8000
569f0ea3689SLuigi Rizzo 
57037e3a6d3SLuigi Rizzo #define	NM_BDG_NAME		"vale"	/* prefix for bridge port name */
57137e3a6d3SLuigi Rizzo 
572*4f80b14cSVincenzo Maffione #ifdef _WIN32
57337e3a6d3SLuigi Rizzo /*
57437e3a6d3SLuigi Rizzo  * Windows does not have _IOWR(). _IO(), _IOW() and _IOR() are defined
57537e3a6d3SLuigi Rizzo  * in ws2def.h but not sure if they are in the form we need.
576*4f80b14cSVincenzo Maffione  * We therefore redefine them in a convenient way to use for DeviceIoControl
577*4f80b14cSVincenzo Maffione  * signatures.
57837e3a6d3SLuigi Rizzo  */
57937e3a6d3SLuigi Rizzo #undef _IO	// ws2def.h
58037e3a6d3SLuigi Rizzo #define _WIN_NM_IOCTL_TYPE 40000
58137e3a6d3SLuigi Rizzo #define _IO(_c, _n)	CTL_CODE(_WIN_NM_IOCTL_TYPE, ((_n) + 0x800) , \
58237e3a6d3SLuigi Rizzo 		METHOD_BUFFERED, FILE_ANY_ACCESS  )
58337e3a6d3SLuigi Rizzo #define _IO_direct(_c, _n)	CTL_CODE(_WIN_NM_IOCTL_TYPE, ((_n) + 0x800) , \
58437e3a6d3SLuigi Rizzo 		METHOD_OUT_DIRECT, FILE_ANY_ACCESS  )
58537e3a6d3SLuigi Rizzo 
58637e3a6d3SLuigi Rizzo #define _IOWR(_c, _n, _s)	_IO(_c, _n)
58737e3a6d3SLuigi Rizzo 
58837e3a6d3SLuigi Rizzo /* We havesome internal sysctl in addition to the externally visible ones */
58937e3a6d3SLuigi Rizzo #define NETMAP_MMAP _IO_direct('i', 160)	// note METHOD_OUT_DIRECT
59037e3a6d3SLuigi Rizzo #define NETMAP_POLL _IO('i', 162)
59137e3a6d3SLuigi Rizzo 
59237e3a6d3SLuigi Rizzo /* and also two setsockopt for sysctl emulation */
59337e3a6d3SLuigi Rizzo #define NETMAP_SETSOCKOPT _IO('i', 140)
59437e3a6d3SLuigi Rizzo #define NETMAP_GETSOCKOPT _IO('i', 141)
59537e3a6d3SLuigi Rizzo 
59637e3a6d3SLuigi Rizzo 
59737e3a6d3SLuigi Rizzo //These linknames are for the Netmap Core Driver
59837e3a6d3SLuigi Rizzo #define NETMAP_NT_DEVICE_NAME			L"\\Device\\NETMAP"
59937e3a6d3SLuigi Rizzo #define NETMAP_DOS_DEVICE_NAME			L"\\DosDevices\\netmap"
60037e3a6d3SLuigi Rizzo 
60137e3a6d3SLuigi Rizzo //Definition of a structure used to pass a virtual address within an IOCTL
60237e3a6d3SLuigi Rizzo typedef struct _MEMORY_ENTRY {
60337e3a6d3SLuigi Rizzo 	PVOID       pUsermodeVirtualAddress;
60437e3a6d3SLuigi Rizzo } MEMORY_ENTRY, *PMEMORY_ENTRY;
60537e3a6d3SLuigi Rizzo 
60637e3a6d3SLuigi Rizzo typedef struct _POLL_REQUEST_DATA {
60737e3a6d3SLuigi Rizzo 	int events;
60837e3a6d3SLuigi Rizzo 	int timeout;
60937e3a6d3SLuigi Rizzo 	int revents;
61037e3a6d3SLuigi Rizzo } POLL_REQUEST_DATA;
61137e3a6d3SLuigi Rizzo 
61237e3a6d3SLuigi Rizzo #endif /* _WIN32 */
61317885a7bSLuigi Rizzo 
61464ae02c3SLuigi Rizzo /*
61564ae02c3SLuigi Rizzo  * FreeBSD uses the size value embedded in the _IOWR to determine
61664ae02c3SLuigi Rizzo  * how much to copy in/out. So we need it to match the actual
61764ae02c3SLuigi Rizzo  * data structure we pass. We put some spares in the structure
61864ae02c3SLuigi Rizzo  * to ease compatibility with other versions
61964ae02c3SLuigi Rizzo  */
62068b8534bSLuigi Rizzo #define NIOCGINFO	_IOWR('i', 145, struct nmreq) /* return IF info */
62168b8534bSLuigi Rizzo #define NIOCREGIF	_IOWR('i', 146, struct nmreq) /* interface register */
62268b8534bSLuigi Rizzo #define NIOCTXSYNC	_IO('i', 148) /* sync tx queues */
62368b8534bSLuigi Rizzo #define NIOCRXSYNC	_IO('i', 149) /* sync rx queues */
6244bf50f18SLuigi Rizzo #define NIOCCONFIG	_IOWR('i',150, struct nm_ifreq) /* for ext. modules */
62568b8534bSLuigi Rizzo #endif /* !NIOCREGIF */
62668b8534bSLuigi Rizzo 
62717885a7bSLuigi Rizzo 
62817885a7bSLuigi Rizzo /*
62917885a7bSLuigi Rizzo  * Helper functions for kernel and userspace
63017885a7bSLuigi Rizzo  */
63117885a7bSLuigi Rizzo 
63217885a7bSLuigi Rizzo /*
63317885a7bSLuigi Rizzo  * check if space is available in the ring.
63417885a7bSLuigi Rizzo  */
63517885a7bSLuigi Rizzo static inline int
63617885a7bSLuigi Rizzo nm_ring_empty(struct netmap_ring *ring)
63717885a7bSLuigi Rizzo {
63817885a7bSLuigi Rizzo 	return (ring->cur == ring->tail);
63917885a7bSLuigi Rizzo }
64017885a7bSLuigi Rizzo 
6414bf50f18SLuigi Rizzo /*
6424bf50f18SLuigi Rizzo  * Opaque structure that is passed to an external kernel
6434bf50f18SLuigi Rizzo  * module via ioctl(fd, NIOCCONFIG, req) for a user-owned
6444bf50f18SLuigi Rizzo  * bridge port (at this point ephemeral VALE interface).
6454bf50f18SLuigi Rizzo  */
6464bf50f18SLuigi Rizzo #define NM_IFRDATA_LEN 256
6474bf50f18SLuigi Rizzo struct nm_ifreq {
6484bf50f18SLuigi Rizzo 	char nifr_name[IFNAMSIZ];
6494bf50f18SLuigi Rizzo 	char data[NM_IFRDATA_LEN];
6504bf50f18SLuigi Rizzo };
6514bf50f18SLuigi Rizzo 
65268b8534bSLuigi Rizzo #endif /* _NET_NETMAP_H_ */
653