xref: /freebsd/sys/net/netmap.h (revision 8d20be1e22095c27faf8fe8b2f0d089739cc742e)
1 /*
2  * Copyright (C) 2011-2013 Matteo Landi, Luigi Rizzo. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *   1. Redistributions of source code must retain the above copyright
9  *      notice, this list of conditions and the following disclaimer.
10  *
11  *   2. Redistributions in binary form must reproduce the above copyright
12  *      notice, this list of conditions and the following disclaimer in the
13  *      documentation and/or other materials provided with the
14  *      distribution.
15  *
16  *   3. Neither the name of the authors nor the names of their contributors
17  *      may be used to endorse or promote products derived from this
18  *      software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY MATTEO LANDI AND CONTRIBUTORS "AS IS" AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTEO LANDI OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * $FreeBSD$
35  *
36  * Definitions of constants and the structures used by the netmap
37  * framework, for the part visible to both kernel and userspace.
38  * Detailed info on netmap is available with "man netmap" or at
39  *
40  *	http://info.iet.unipi.it/~luigi/netmap/
41  */
42 
43 #ifndef _NET_NETMAP_H_
44 #define _NET_NETMAP_H_
45 
46 /*
47  * --- Netmap data structures ---
48  *
49  * The data structures used by netmap are shown below. Those in
50  * capital letters are in an mmapp()ed area shared with userspace,
51  * while others are private to the kernel.
52  * Shared structures do not contain pointers but only memory
53  * offsets, so that addressing is portable between kernel and userspace.
54 
55 
56  softc
57 +----------------+
58 | standard fields|
59 | if_pspare[0] ----------+
60 +----------------+       |
61                          |
62 +----------------+<------+
63 |(netmap_adapter)|
64 |                |                             netmap_kring
65 | tx_rings *--------------------------------->+---------------+
66 |                |       netmap_kring         | ring    *---------.
67 | rx_rings *--------->+---------------+       | nr_hwcur      |   |
68 +----------------+    | ring    *--------.    | nr_hwavail    |   V
69                       | nr_hwcur      |  |    | selinfo       |   |
70                       | nr_hwavail    |  |    +---------------+   .
71                       | selinfo       |  |    |     ...       |   .
72                       +---------------+  |    |(ntx+1 entries)|
73                       |    ....       |  |    |               |
74                       |(nrx+1 entries)|  |    +---------------+
75                       |               |  |
76    KERNEL             +---------------+  |
77                                          |
78   ====================================================================
79                                          |
80    USERSPACE                             |      NETMAP_RING
81                                          +---->+-------------+
82                                              / | cur         |
83    NETMAP_IF  (nifp, one per file desc.)    /  | avail       |
84     +---------------+                      /   | buf_ofs     |
85     | ni_tx_rings   |                     /    +=============+
86     | ni_rx_rings   |                    /     | buf_idx     | slot[0]
87     |               |                   /      | len, flags  |
88     |               |                  /       +-------------+
89     +===============+                 /        | buf_idx     | slot[1]
90     | txring_ofs[0] | (rel.to nifp)--'         | len, flags  |
91     | txring_ofs[1] |                          +-------------+
92   (num_rings+1 entries)                     (nr_num_slots entries)
93     | txring_ofs[n] |                          | buf_idx     | slot[n-1]
94     +---------------+                          | len, flags  |
95     | rxring_ofs[0] |                          +-------------+
96     | rxring_ofs[1] |
97   (num_rings+1 entries)
98     | txring_ofs[n] |
99     +---------------+
100 
101  * The private descriptor ('softc' or 'adapter') of each interface
102  * is extended with a "struct netmap_adapter" containing netmap-related
103  * info (see description in dev/netmap/netmap_kernel.h.
104  * Among other things, tx_rings and rx_rings point to the arrays of
105  * "struct netmap_kring" which in turn reache the various
106  * "struct netmap_ring", shared with userspace.
107 
108  * The NETMAP_RING is the userspace-visible replica of the NIC ring.
109  * Each slot has the index of a buffer, its length and some flags.
110  * In user space, the buffer address is computed as
111  *	(char *)ring + buf_ofs + index*NETMAP_BUF_SIZE
112  * In the kernel, buffers do not necessarily need to be contiguous,
113  * and the virtual and physical addresses are derived through
114  * a lookup table.
115  *
116  * struct netmap_slot:
117  *
118  * buf_idx	is the index of the buffer associated to the slot.
119  * len		is the length of the payload
120  * NS_BUF_CHANGED	must be set whenever userspace wants
121  *		to change buf_idx (it might be necessary to
122  *		reprogram the NIC slot)
123  * NS_REPORT	must be set if we want the NIC to generate an interrupt
124  *		when this slot is used. Leaving it to 0 improves
125  *		performance.
126  * NS_FORWARD	if set on a receive ring, and the device is in
127  *		transparent mode, buffers released with the flag set
128  *		will be forwarded to the 'other' side (host stack
129  *		or NIC, respectively) on the next select() or ioctl()
130  *
131  *		The following will be supported from NETMAP_API = 5
132  * NS_NO_LEARN	on a VALE switch, do not 'learn' the source port for
133  *		this packet.
134  * NS_INDIRECT	the netmap buffer contains a 64-bit pointer to
135  *		the actual userspace buffer. This may be useful
136  *		to reduce copies in a VM environment.
137  * NS_MOREFRAG	Part of a multi-segment frame. The last (or only)
138  *		segment must not have this flag.
139  * NS_PORT_MASK	the high 8 bits of the flag, if not zero, indicate the
140  *		destination port for the VALE switch, overriding
141  *		the lookup table.
142  */
143 
144 struct netmap_slot {
145 	uint32_t buf_idx; /* buffer index */
146 	uint16_t len;	/* packet length, to be copied to/from the hw ring */
147 	uint16_t flags;	/* buf changed, etc. */
148 #define	NS_BUF_CHANGED	0x0001	/* must resync the map, buffer changed */
149 #define	NS_REPORT	0x0002	/* ask the hardware to report results
150 				 * e.g. by generating an interrupt
151 				 */
152 #define	NS_FORWARD	0x0004	/* pass packet to the other endpoint
153 				 * (host stack or device)
154 				 */
155 #define	NS_NO_LEARN	0x0008
156 #define	NS_INDIRECT	0x0010
157 #define	NS_MOREFRAG	0x0020
158 #define	NS_PORT_SHIFT	8
159 #define	NS_PORT_MASK	(0xff << NS_PORT_SHIFT)
160 };
161 
162 /*
163  * Netmap representation of a TX or RX ring (also known as "queue").
164  * This is a queue implemented as a fixed-size circular array.
165  * At the software level, two fields are important: avail and cur.
166  *
167  * In TX rings:
168  *	avail	indicates the number of slots available for transmission.
169  *		It is updated by the kernel after every netmap system call.
170  *		It MUST BE decremented by the application when it appends a
171  *		packet.
172  *	cur	indicates the slot to use for the next packet
173  *		to send (i.e. the "tail" of the queue).
174  *		It MUST BE incremented by the application before
175  *		netmap system calls to reflect the number of newly
176  *		sent packets.
177  *		It is checked by the kernel on netmap system calls
178  *		(normally unmodified by the kernel unless invalid).
179  *
180  *   The kernel side of netmap uses two additional fields in its own
181  *   private ring structure, netmap_kring:
182  *	nr_hwcur is a copy of nr_cur on an NIOCTXSYNC.
183  *	nr_hwavail is the number of slots known as available by the
184  *		hardware. It is updated on an INTR (inc by the
185  *		number of packets sent) and on a NIOCTXSYNC
186  *		(decrease by nr_cur - nr_hwcur)
187  *		A special case, nr_hwavail is -1 if the transmit
188  *		side is idle (no pending transmits).
189  *
190  * In RX rings:
191  *	avail	is the number of packets available (possibly 0).
192  *		It MUST BE decremented by the application when it consumes
193  *		a packet, and it is updated to nr_hwavail on a NIOCRXSYNC
194  *	cur	indicates the first slot that contains a packet not
195  *		processed yet (the "head" of the queue).
196  *		It MUST BE incremented by the software when it consumes
197  *		a packet.
198  *	reserved	indicates the number of buffers before 'cur'
199  *		that the application has still in use. Normally 0,
200  *		it MUST BE incremented by the application when it
201  *		does not return the buffer immediately, and decremented
202  *		when the buffer is finally freed.
203  *
204  *   The kernel side of netmap uses two additional fields in the kring:
205  *	nr_hwcur is a copy of nr_cur on an NIOCRXSYNC
206  *	nr_hwavail is the number of packets available. It is updated
207  *		on INTR (inc by the number of new packets arrived)
208  *		and on NIOCRXSYNC (decreased by nr_cur - nr_hwcur).
209  *
210  * DATA OWNERSHIP/LOCKING:
211  *	The netmap_ring is owned by the user program and it is only
212  *	accessed or modified in the upper half of the kernel during
213  *	a system call.
214  *
215  *	The netmap_kring is only modified by the upper half of the kernel.
216  *
217  * FLAGS
218  *	NR_TIMESTAMP	updates the 'ts' field on each syscall. This is
219  *			a global timestamp for all packets.
220  *	NR_RX_TSTMP	if set, the last 64 byte in each buffer will
221  *			contain a timestamp for the frame supplied by
222  *			the hardware (if supported)
223  *	NR_FORWARD	if set, the NS_FORWARD flag in each slot of the
224  *			RX ring is checked, and if set the packet is
225  *			passed to the other side (host stack or device,
226  *			respectively). This permits bpf-like behaviour
227  *			or transparency for selected packets.
228  */
229 struct netmap_ring {
230 	/*
231 	 * nr_buf_base_ofs is meant to be used through macros.
232 	 * It contains the offset of the buffer region from this
233 	 * descriptor.
234 	 */
235 	const ssize_t	buf_ofs;
236 	const uint32_t	num_slots;	/* number of slots in the ring. */
237 	uint32_t	avail;		/* number of usable slots */
238 	uint32_t        cur;		/* 'current' r/w position */
239 	uint32_t	reserved;	/* not refilled before current */
240 
241 	const uint16_t	nr_buf_size;
242 	uint16_t	flags;
243 #define	NR_TIMESTAMP	0x0002		/* set timestamp on *sync() */
244 #define	NR_FORWARD	0x0004		/* enable NS_FORWARD for ring */
245 #define	NR_RX_TSTMP	0x0008		/* set rx timestamp in slots */
246 
247 	struct timeval	ts;		/* time of last *sync() */
248 
249 	/* the slots follow. This struct has variable size */
250 	struct netmap_slot slot[0];	/* array of slots. */
251 };
252 
253 
254 /*
255  * Netmap representation of an interface and its queue(s).
256  * There is one netmap_if for each file descriptor on which we want
257  * to select/poll.  We assume that on each interface has the same number
258  * of receive and transmit queues.
259  * select/poll operates on one or all pairs depending on the value of
260  * nmr_queueid passed on the ioctl.
261  */
262 struct netmap_if {
263 	char		ni_name[IFNAMSIZ]; /* name of the interface. */
264 	const u_int	ni_version;	/* API version, currently unused */
265 	const u_int	ni_rx_rings;	/* number of rx rings */
266 	const u_int	ni_tx_rings;	/* if zero, same as ni_rx_rings */
267 	/*
268 	 * The following array contains the offset of each netmap ring
269 	 * from this structure. The first ni_tx_queues+1 entries refer
270 	 * to the tx rings, the next ni_rx_queues+1 refer to the rx rings
271 	 * (the last entry in each block refers to the host stack rings).
272 	 * The area is filled up by the kernel on NIOCREG,
273 	 * and then only read by userspace code.
274 	 */
275 	const ssize_t	ring_ofs[0];
276 };
277 
278 #ifndef NIOCREGIF
279 /*
280  * ioctl names and related fields
281  *
282  * NIOCGINFO takes a struct ifreq, the interface name is the input,
283  *	the outputs are number of queues and number of descriptor
284  *	for each queue (useful to set number of threads etc.).
285  *
286  * NIOCREGIF takes an interface name within a struct ifreq,
287  *	and activates netmap mode on the interface (if possible).
288  *
289  *	For vale ports, starting with NETMAP_API = 5,
290  *	nr_tx_rings and nr_rx_rings specify how many software rings
291  *	are created (0 means 1).
292  *
293  *	NIOCREGIF is also used to attach a NIC to a VALE switch.
294  *	In this case the name is vale*:ifname, and "nr_cmd"
295  *	is set to 'NETMAP_BDG_ATTACH' or 'NETMAP_BDG_DETACH'.
296  *	nr_ringid specifies which rings should be attached, 0 means all,
297  *	NETMAP_HW_RING + n means only the n-th ring.
298  *	The process can terminate after the interface has been attached.
299  *
300  * NIOCUNREGIF unregisters the interface associated to the fd.
301  *	this is deprecated and will go away.
302  *
303  * NIOCTXSYNC, NIOCRXSYNC synchronize tx or rx queues,
304  *	whose identity is set in NIOCREGIF through nr_ringid
305  *
306  * NETMAP_API is the API version.
307  */
308 
309 /*
310  * struct nmreq overlays a struct ifreq
311  */
312 struct nmreq {
313 	char		nr_name[IFNAMSIZ];
314 	uint32_t	nr_version;	/* API version */
315 #define	NETMAP_API	4		/* current version */
316 	uint32_t	nr_offset;	/* nifp offset in the shared region */
317 	uint32_t	nr_memsize;	/* size of the shared region */
318 	uint32_t	nr_tx_slots;	/* slots in tx rings */
319 	uint32_t	nr_rx_slots;	/* slots in rx rings */
320 	uint16_t	nr_tx_rings;	/* number of tx rings */
321 	uint16_t	nr_rx_rings;	/* number of rx rings */
322 	uint16_t	nr_ringid;	/* ring(s) we care about */
323 #define NETMAP_HW_RING	0x4000		/* low bits indicate one hw ring */
324 #define NETMAP_SW_RING	0x2000		/* process the sw ring */
325 #define NETMAP_NO_TX_POLL	0x1000	/* no automatic txsync on poll */
326 #define NETMAP_RING_MASK 0xfff		/* the ring number */
327 	uint16_t	nr_cmd;
328 #define NETMAP_BDG_ATTACH	1	/* attach the NIC */
329 #define NETMAP_BDG_DETACH	2	/* detach the NIC */
330 #define NETMAP_BDG_LOOKUP_REG	3	/* register lookup function */
331 #define NETMAP_BDG_LIST		4	/* get bridge's info */
332 	uint16_t	nr_arg1;
333 #define NETMAP_BDG_HOST		1	/* attach the host stack on ATTACH */
334 	uint16_t	nr_arg2;
335 	uint32_t	spare2[3];
336 };
337 
338 /*
339  * FreeBSD uses the size value embedded in the _IOWR to determine
340  * how much to copy in/out. So we need it to match the actual
341  * data structure we pass. We put some spares in the structure
342  * to ease compatibility with other versions
343  */
344 #define NIOCGINFO	_IOWR('i', 145, struct nmreq) /* return IF info */
345 #define NIOCREGIF	_IOWR('i', 146, struct nmreq) /* interface register */
346 #define NIOCUNREGIF	_IO('i', 147) /* interface unregister */
347 #define NIOCTXSYNC	_IO('i', 148) /* sync tx queues */
348 #define NIOCRXSYNC	_IO('i', 149) /* sync rx queues */
349 #endif /* !NIOCREGIF */
350 
351 #endif /* _NET_NETMAP_H_ */
352