xref: /freebsd/sys/net/netmap.h (revision d184218c)
1 /*
2  * Copyright (C) 2011 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  * $Id: netmap.h 11997 2013-01-17 21:59:12Z luigi $
36  *
37  * Definitions of constants and the structures used by the netmap
38  * framework, for the part visible to both kernel and userspace.
39  * Detailed info on netmap is available with "man netmap" or at
40  *
41  *	http://info.iet.unipi.it/~luigi/netmap/
42  */
43 
44 #ifndef _NET_NETMAP_H_
45 #define _NET_NETMAP_H_
46 
47 /*
48  * --- Netmap data structures ---
49  *
50  * The data structures used by netmap are shown below. Those in
51  * capital letters are in an mmapp()ed area shared with userspace,
52  * while others are private to the kernel.
53  * Shared structures do not contain pointers but only memory
54  * offsets, so that addressing is portable between kernel and userspace.
55 
56 
57  softc
58 +----------------+
59 | standard fields|
60 | if_pspare[0] ----------+
61 +----------------+       |
62                          |
63 +----------------+<------+
64 |(netmap_adapter)|
65 |                |                             netmap_kring
66 | tx_rings *--------------------------------->+---------------+
67 |                |       netmap_kring         | ring    *---------.
68 | rx_rings *--------->+---------------+       | nr_hwcur      |   |
69 +----------------+    | ring    *--------.    | nr_hwavail    |   V
70                       | nr_hwcur      |  |    | selinfo       |   |
71                       | nr_hwavail    |  |    +---------------+   .
72                       | selinfo       |  |    |     ...       |   .
73                       +---------------+  |    |(ntx+1 entries)|
74                       |    ....       |  |    |               |
75                       |(nrx+1 entries)|  |    +---------------+
76                       |               |  |
77    KERNEL             +---------------+  |
78                                          |
79   ====================================================================
80                                          |
81    USERSPACE                             |      NETMAP_RING
82                                          +---->+-------------+
83                                              / | cur         |
84    NETMAP_IF  (nifp, one per file desc.)    /  | avail       |
85     +---------------+                      /   | buf_ofs     |
86     | ni_tx_rings   |                     /    +=============+
87     | ni_rx_rings   |                    /     | buf_idx     | slot[0]
88     |               |                   /      | len, flags  |
89     |               |                  /       +-------------+
90     +===============+                 /        | buf_idx     | slot[1]
91     | txring_ofs[0] | (rel.to nifp)--'         | len, flags  |
92     | txring_ofs[1] |                          +-------------+
93   (num_rings+1 entries)                     (nr_num_slots entries)
94     | txring_ofs[n] |                          | buf_idx     | slot[n-1]
95     +---------------+                          | len, flags  |
96     | rxring_ofs[0] |                          +-------------+
97     | rxring_ofs[1] |
98   (num_rings+1 entries)
99     | txring_ofs[n] |
100     +---------------+
101 
102  * The private descriptor ('softc' or 'adapter') of each interface
103  * is extended with a "struct netmap_adapter" containing netmap-related
104  * info (see description in dev/netmap/netmap_kernel.h.
105  * Among other things, tx_rings and rx_rings point to the arrays of
106  * "struct netmap_kring" which in turn reache the various
107  * "struct netmap_ring", shared with userspace.
108 
109  * The NETMAP_RING is the userspace-visible replica of the NIC ring.
110  * Each slot has the index of a buffer, its length and some flags.
111  * In user space, the buffer address is computed as
112  *	(char *)ring + buf_ofs + index*NETMAP_BUF_SIZE
113  * In the kernel, buffers do not necessarily need to be contiguous,
114  * and the virtual and physical addresses are derived through
115  * a lookup table.
116  *
117  * struct netmap_slot:
118  *
119  * buf_idx	is the index of the buffer associated to the slot.
120  * len		is the length of the payload
121  * NS_BUF_CHANGED	must be set whenever userspace wants
122  *		to change buf_idx (it might be necessary to
123  *		reprogram the NIC slot)
124  * NS_REPORT	must be set if we want the NIC to generate an interrupt
125  *		when this slot is used. Leaving it to 0 improves
126  *		performance.
127  * NS_FORWARD	if set on a receive ring, and the device is in
128  *		transparent mode, buffers released with the flag set
129  *		will be forwarded to the 'other' side (host stack
130  *		or NIC, respectively) on the next select() or ioctl()
131  * NS_NO_LEARN	on a VALE switch, do not 'learn' the source port for
132  *		this packet.
133  * NS_PORT_MASK	the high 8 bits of the flag, if not zero, indicate the
134  *		destination port for the VALE switch, overriding
135  *		the lookup table.
136  */
137 
138 struct netmap_slot {
139 	uint32_t buf_idx; /* buffer index */
140 	uint16_t len;	/* packet length, to be copied to/from the hw ring */
141 	uint16_t flags;	/* buf changed, etc. */
142 #define	NS_BUF_CHANGED	0x0001	/* must resync the map, buffer changed */
143 #define	NS_REPORT	0x0002	/* ask the hardware to report results
144 				 * e.g. by generating an interrupt
145 				 */
146 #define	NS_FORWARD	0x0004	/* pass packet to the other endpoint
147 				 * (host stack or device)
148 				 */
149 #define	NS_NO_LEARN	0x0008
150 #define	NS_PORT_SHIFT	8
151 #define	NS_PORT_MASK	(0xff << NS_PORT_SHIFT)
152 };
153 
154 /*
155  * Netmap representation of a TX or RX ring (also known as "queue").
156  * This is a queue implemented as a fixed-size circular array.
157  * At the software level, two fields are important: avail and cur.
158  *
159  * In TX rings:
160  *	avail	indicates the number of slots available for transmission.
161  *		It is updated by the kernel after every netmap system call.
162  *		It MUST BE decremented by the application when it appends a
163  *		packet.
164  *	cur	indicates the slot to use for the next packet
165  *		to send (i.e. the "tail" of the queue).
166  *		It MUST BE incremented by the application before
167  *		netmap system calls to reflect the number of newly
168  *		sent packets.
169  *		It is checked by the kernel on netmap system calls
170  *		(normally unmodified by the kernel unless invalid).
171  *
172  *   The kernel side of netmap uses two additional fields in its own
173  *   private ring structure, netmap_kring:
174  *	nr_hwcur is a copy of nr_cur on an NIOCTXSYNC.
175  *	nr_hwavail is the number of slots known as available by the
176  *		hardware. It is updated on an INTR (inc by the
177  *		number of packets sent) and on a NIOCTXSYNC
178  *		(decrease by nr_cur - nr_hwcur)
179  *		A special case, nr_hwavail is -1 if the transmit
180  *		side is idle (no pending transmits).
181  *
182  * In RX rings:
183  *	avail	is the number of packets available (possibly 0).
184  *		It MUST BE decremented by the application when it consumes
185  *		a packet, and it is updated to nr_hwavail on a NIOCRXSYNC
186  *	cur	indicates the first slot that contains a packet not
187  *		processed yet (the "head" of the queue).
188  *		It MUST BE incremented by the software when it consumes
189  *		a packet.
190  *	reserved	indicates the number of buffers before 'cur'
191  *		that the application has still in use. Normally 0,
192  *		it MUST BE incremented by the application when it
193  *		does not return the buffer immediately, and decremented
194  *		when the buffer is finally freed.
195  *
196  *   The kernel side of netmap uses two additional fields in the kring:
197  *	nr_hwcur is a copy of nr_cur on an NIOCRXSYNC
198  *	nr_hwavail is the number of packets available. It is updated
199  *		on INTR (inc by the number of new packets arrived)
200  *		and on NIOCRXSYNC (decreased by nr_cur - nr_hwcur).
201  *
202  * DATA OWNERSHIP/LOCKING:
203  *	The netmap_ring is owned by the user program and it is only
204  *	accessed or modified in the upper half of the kernel during
205  *	a system call.
206  *
207  *	The netmap_kring is only modified by the upper half of the kernel.
208  *
209  * FLAGS
210  *	NR_TIMESTAMP	updates the 'ts' field on each syscall. This is
211  *			a global timestamp for all packets.
212  *	NR_RX_TSTMP	if set, the last 64 byte in each buffer will
213  *			contain a timestamp for the frame supplied by
214  *			the hardware (if supported)
215  *	NR_FORWARD	if set, the NS_FORWARD flag in each slot of the
216  *			RX ring is checked, and if set the packet is
217  *			passed to the other side (host stack or device,
218  *			respectively). This permits bpf-like behaviour
219  *			or transparency for selected packets.
220  */
221 struct netmap_ring {
222 	/*
223 	 * nr_buf_base_ofs is meant to be used through macros.
224 	 * It contains the offset of the buffer region from this
225 	 * descriptor.
226 	 */
227 	const ssize_t	buf_ofs;
228 	const uint32_t	num_slots;	/* number of slots in the ring. */
229 	uint32_t	avail;		/* number of usable slots */
230 	uint32_t        cur;		/* 'current' r/w position */
231 	uint32_t	reserved;	/* not refilled before current */
232 
233 	const uint16_t	nr_buf_size;
234 	uint16_t	flags;
235 #define	NR_TIMESTAMP	0x0002		/* set timestamp on *sync() */
236 #define	NR_FORWARD	0x0004		/* enable NS_FORWARD for ring */
237 #define	NR_RX_TSTMP	0x0008		/* set rx timestamp in slots */
238 
239 	struct timeval	ts;		/* time of last *sync() */
240 
241 	/* the slots follow. This struct has variable size */
242 	struct netmap_slot slot[0];	/* array of slots. */
243 };
244 
245 
246 /*
247  * Netmap representation of an interface and its queue(s).
248  * There is one netmap_if for each file descriptor on which we want
249  * to select/poll.  We assume that on each interface has the same number
250  * of receive and transmit queues.
251  * select/poll operates on one or all pairs depending on the value of
252  * nmr_queueid passed on the ioctl.
253  */
254 struct netmap_if {
255 	char		ni_name[IFNAMSIZ]; /* name of the interface. */
256 	const u_int	ni_version;	/* API version, currently unused */
257 	const u_int	ni_rx_rings;	/* number of rx rings */
258 	const u_int	ni_tx_rings;	/* if zero, same as ni_rx_rings */
259 	/*
260 	 * The following array contains the offset of each netmap ring
261 	 * from this structure. The first ni_tx_queues+1 entries refer
262 	 * to the tx rings, the next ni_rx_queues+1 refer to the rx rings
263 	 * (the last entry in each block refers to the host stack rings).
264 	 * The area is filled up by the kernel on NIOCREG,
265 	 * and then only read by userspace code.
266 	 */
267 	const ssize_t	ring_ofs[0];
268 };
269 
270 #ifndef NIOCREGIF
271 /*
272  * ioctl names and related fields
273  *
274  * NIOCGINFO takes a struct ifreq, the interface name is the input,
275  *	the outputs are number of queues and number of descriptor
276  *	for each queue (useful to set number of threads etc.).
277  *
278  * NIOCREGIF takes an interface name within a struct ifreq,
279  *	and activates netmap mode on the interface (if possible).
280  *
281  * NIOCUNREGIF unregisters the interface associated to the fd.
282  *
283  * NIOCTXSYNC, NIOCRXSYNC synchronize tx or rx queues,
284  *	whose identity is set in NIOCREGIF through nr_ringid
285  */
286 
287 /*
288  * struct nmreq overlays a struct ifreq
289  */
290 struct nmreq {
291 	char		nr_name[IFNAMSIZ];
292 	uint32_t	nr_version;	/* API version */
293 #define	NETMAP_API	3		/* current version */
294 	uint32_t	nr_offset;	/* nifp offset in the shared region */
295 	uint32_t	nr_memsize;	/* size of the shared region */
296 	uint32_t	nr_tx_slots;	/* slots in tx rings */
297 	uint32_t	nr_rx_slots;	/* slots in rx rings */
298 	uint16_t	nr_tx_rings;	/* number of tx rings */
299 	uint16_t	nr_rx_rings;	/* number of rx rings */
300 	uint16_t	nr_ringid;	/* ring(s) we care about */
301 #define NETMAP_HW_RING	0x4000		/* low bits indicate one hw ring */
302 #define NETMAP_SW_RING	0x2000		/* process the sw ring */
303 #define NETMAP_NO_TX_POLL	0x1000	/* no automatic txsync on poll */
304 #define NETMAP_RING_MASK 0xfff		/* the ring number */
305 	uint16_t	spare1;
306 	uint32_t	spare2[4];
307 };
308 
309 /*
310  * FreeBSD uses the size value embedded in the _IOWR to determine
311  * how much to copy in/out. So we need it to match the actual
312  * data structure we pass. We put some spares in the structure
313  * to ease compatibility with other versions
314  */
315 #define NIOCGINFO	_IOWR('i', 145, struct nmreq) /* return IF info */
316 #define NIOCREGIF	_IOWR('i', 146, struct nmreq) /* interface register */
317 #define NIOCUNREGIF	_IO('i', 147) /* interface unregister */
318 #define NIOCTXSYNC	_IO('i', 148) /* sync tx queues */
319 #define NIOCRXSYNC	_IO('i', 149) /* sync rx queues */
320 #endif /* !NIOCREGIF */
321 
322 #endif /* _NET_NETMAP_H_ */
323