xref: /freebsd/sys/net/netmap.h (revision 5dae51da3da0cc94d17bd67b308fad304ebec7e0)
1 /*
2  * Copyright (C) 2011-2014 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
6  * are met:
7  *
8  *   1. Redistributions of source code must retain the above copyright
9  *      notice, this list of conditions and the following disclaimer.
10  *   2. Redistributions in binary form must reproduce the above copyright
11  *      notice, this list of conditions and the following disclaimer in the
12  *      documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``S IS''AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * $FreeBSD$
29  *
30  * Definitions of constants and the structures used by the netmap
31  * framework, for the part visible to both kernel and userspace.
32  * Detailed info on netmap is available with "man netmap" or at
33  *
34  *	http://info.iet.unipi.it/~luigi/netmap/
35  *
36  * This API is also used to communicate with the VALE software switch
37  */
38 
39 #ifndef _NET_NETMAP_H_
40 #define _NET_NETMAP_H_
41 
42 #define	NETMAP_API	11		/* current API version */
43 
44 #define	NETMAP_MIN_API	11		/* min and max versions accepted */
45 #define	NETMAP_MAX_API	15
46 /*
47  * Some fields should be cache-aligned to reduce contention.
48  * The alignment is architecture and OS dependent, but rather than
49  * digging into OS headers to find the exact value we use an estimate
50  * that should cover most architectures.
51  */
52 #define NM_CACHE_ALIGN	128
53 
54 /*
55  * --- Netmap data structures ---
56  *
57  * The userspace data structures used by netmap are shown below.
58  * They are allocated by the kernel and mmap()ed by userspace threads.
59  * Pointers are implemented as memory offsets or indexes,
60  * so that they can be easily dereferenced in kernel and userspace.
61 
62    KERNEL (opaque, obviously)
63 
64   ====================================================================
65                                          |
66    USERSPACE                             |      struct netmap_ring
67                                          +---->+---------------+
68                                              / | head,cur,tail |
69    struct netmap_if (nifp, 1 per fd)        /  | buf_ofs       |
70     +---------------+                      /   | other fields  |
71     | ni_tx_rings   |                     /    +===============+
72     | ni_rx_rings   |                    /     | buf_idx, len  | slot[0]
73     |               |                   /      | flags, ptr    |
74     |               |                  /       +---------------+
75     +===============+                 /        | buf_idx, len  | slot[1]
76     | txring_ofs[0] | (rel.to nifp)--'         | flags, ptr    |
77     | txring_ofs[1] |                          +---------------+
78      (tx+1 entries)                           (num_slots entries)
79     | txring_ofs[t] |                          | buf_idx, len  | slot[n-1]
80     +---------------+                          | flags, ptr    |
81     | rxring_ofs[0] |                          +---------------+
82     | rxring_ofs[1] |
83      (rx+1 entries)
84     | rxring_ofs[r] |
85     +---------------+
86 
87  * For each "interface" (NIC, host stack, PIPE, VALE switch port) bound to
88  * a file descriptor, the mmap()ed region contains a (logically readonly)
89  * struct netmap_if pointing to struct netmap_ring's.
90  *
91  * There is one netmap_ring per physical NIC ring, plus one tx/rx ring
92  * pair attached to the host stack (this pair is unused for non-NIC ports).
93  *
94  * All physical/host stack ports share the same memory region,
95  * so that zero-copy can be implemented between them.
96  * VALE switch ports instead have separate memory regions.
97  *
98  * The netmap_ring is the userspace-visible replica of the NIC ring.
99  * Each slot has the index of a buffer (MTU-sized and residing in the
100  * mmapped region), its length and some flags. An extra 64-bit pointer
101  * is provided for user-supplied buffers in the tx path.
102  *
103  * In user space, the buffer address is computed as
104  *	(char *)ring + buf_ofs + index * NETMAP_BUF_SIZE
105  *
106  * Added in NETMAP_API 11:
107  *
108  * + NIOCREGIF can request the allocation of extra spare buffers from
109  *   the same memory pool. The desired number of buffers must be in
110  *   nr_arg3. The ioctl may return fewer buffers, depending on memory
111  *   availability. nr_arg3 will return the actual value, and, once
112  *   mapped, nifp->ni_bufs_head will be the index of the first buffer.
113  *
114  *   The buffers are linked to each other using the first uint32_t
115  *   as the index. On close, ni_bufs_head must point to the list of
116  *   buffers to be released.
117  *
118  * + NIOCREGIF can request space for extra rings (and buffers)
119  *   allocated in the same memory space. The number of extra rings
120  *   is in nr_arg1, and is advisory. This is a no-op on NICs where
121  *   the size of the memory space is fixed.
122  *
123  * + NIOCREGIF can attach to PIPE rings sharing the same memory
124  *   space with a parent device. The ifname indicates the parent device,
125  *   which must already exist. Flags in nr_flags indicate if we want to
126  *   bind the master or slave side, the index (from nr_ringid)
127  *   is just a cookie and does not need to be sequential.
128  *
129  * + NIOCREGIF can also attach to 'monitor' rings that replicate
130  *   the content of specific rings, also from the same memory space.
131  *
132  *   Extra flags in nr_flags support the above functions.
133  *   Application libraries may use the following naming scheme:
134  *	netmap:foo			all NIC ring pairs
135  *	netmap:foo^			only host ring pair
136  *	netmap:foo+			all NIC ring + host ring pairs
137  *	netmap:foo-k			the k-th NIC ring pair
138  *	netmap:foo{k			PIPE ring pair k, master side
139  *	netmap:foo}k			PIPE ring pair k, slave side
140  *
141  * Some notes about host rings:
142  *
143  * + The RX host ring is used to store those packets that the host network
144  *   stack is trying to transmit through a NIC queue, but only if that queue
145  *   is currently in netmap mode. Netmap will not intercept host stack mbufs
146  *   designated to NIC queues that are not in netmap mode. As a consequence,
147  *   registering a netmap port with netmap:foo^ is not enough to intercept
148  *   mbufs in the RX host ring; the netmap port should be registered with
149  *   netmap:foo*, or another registration should be done to open at least a
150  *   NIC TX queue in netmap mode.
151  *
152  * + Netmap is not currently able to deal with intercepted trasmit mbufs which
153  *   require offloadings like TSO, UFO, checksumming offloadings, etc. It is
154  *   responsibility of the user to disable those offloadings (e.g. using
155  *   ifconfig on FreeBSD or ethtool -K on Linux) for an interface that is being
156  *   used in netmap mode. If the offloadings are not disabled, GSO and/or
157  *   unchecksummed packets may be dropped immediately or end up in the host RX
158  *   ring, and will be dropped as soon as the packet reaches another netmap
159  *   adapter.
160  */
161 
162 /*
163  * struct netmap_slot is a buffer descriptor
164  */
165 struct netmap_slot {
166 	uint32_t buf_idx;	/* buffer index */
167 	uint16_t len;		/* length for this slot */
168 	uint16_t flags;		/* buf changed, etc. */
169 	uint64_t ptr;		/* pointer for indirect buffers */
170 };
171 
172 /*
173  * The following flags control how the slot is used
174  */
175 
176 #define	NS_BUF_CHANGED	0x0001	/* buf_idx changed */
177 	/*
178 	 * must be set whenever buf_idx is changed (as it might be
179 	 * necessary to recompute the physical address and mapping)
180 	 *
181 	 * It is also set by the kernel whenever the buf_idx is
182 	 * changed internally (e.g., by pipes). Applications may
183 	 * use this information to know when they can reuse the
184 	 * contents of previously prepared buffers.
185 	 */
186 
187 #define	NS_REPORT	0x0002	/* ask the hardware to report results */
188 	/*
189 	 * Request notification when slot is used by the hardware.
190 	 * Normally transmit completions are handled lazily and
191 	 * may be unreported. This flag lets us know when a slot
192 	 * has been sent (e.g. to terminate the sender).
193 	 */
194 
195 #define	NS_FORWARD	0x0004	/* pass packet 'forward' */
196 	/*
197 	 * (Only for physical ports, rx rings with NR_FORWARD set).
198 	 * Slot released to the kernel (i.e. before ring->head) with
199 	 * this flag set are passed to the peer ring (host/NIC),
200 	 * thus restoring the host-NIC connection for these slots.
201 	 * This supports efficient traffic monitoring or firewalling.
202 	 */
203 
204 #define	NS_NO_LEARN	0x0008	/* disable bridge learning */
205  	/*
206 	 * On a VALE switch, do not 'learn' the source port for
207  	 * this buffer.
208 	 */
209 
210 #define	NS_INDIRECT	0x0010	/* userspace buffer */
211  	/*
212 	 * (VALE tx rings only) data is in a userspace buffer,
213 	 * whose address is in the 'ptr' field in the slot.
214 	 */
215 
216 #define	NS_MOREFRAG	0x0020	/* packet has more fragments */
217  	/*
218 	 * (VALE ports only)
219 	 * Set on all but the last slot of a multi-segment packet.
220 	 * The 'len' field refers to the individual fragment.
221 	 */
222 
223 #define	NS_PORT_SHIFT	8
224 #define	NS_PORT_MASK	(0xff << NS_PORT_SHIFT)
225 	/*
226  	 * The high 8 bits of the flag, if not zero, indicate the
227 	 * destination port for the VALE switch, overriding
228  	 * the lookup table.
229  	 */
230 
231 #define	NS_RFRAGS(_slot)	( ((_slot)->flags >> 8) & 0xff)
232 	/*
233 	 * (VALE rx rings only) the high 8 bits
234 	 *  are the number of fragments.
235 	 */
236 
237 
238 /*
239  * struct netmap_ring
240  *
241  * Netmap representation of a TX or RX ring (also known as "queue").
242  * This is a queue implemented as a fixed-size circular array.
243  * At the software level the important fields are: head, cur, tail.
244  *
245  * In TX rings:
246  *
247  *	head	first slot available for transmission.
248  *	cur	wakeup point. select() and poll() will unblock
249  *		when 'tail' moves past 'cur'
250  *	tail	(readonly) first slot reserved to the kernel
251  *
252  *	[head .. tail-1] can be used for new packets to send;
253  *	'head' and 'cur' must be incremented as slots are filled
254  *	    with new packets to be sent;
255  *	'cur' can be moved further ahead if we need more space
256  *	for new transmissions. XXX todo (2014-03-12)
257  *
258  * In RX rings:
259  *
260  *	head	first valid received packet
261  *	cur	wakeup point. select() and poll() will unblock
262  *		when 'tail' moves past 'cur'
263  *	tail	(readonly) first slot reserved to the kernel
264  *
265  *	[head .. tail-1] contain received packets;
266  *	'head' and 'cur' must be incremented as slots are consumed
267  *		and can be returned to the kernel;
268  *	'cur' can be moved further ahead if we want to wait for
269  *		new packets without returning the previous ones.
270  *
271  * DATA OWNERSHIP/LOCKING:
272  *	The netmap_ring, and all slots and buffers in the range
273  *	[head .. tail-1] are owned by the user program;
274  *	the kernel only accesses them during a netmap system call
275  *	and in the user thread context.
276  *
277  *	Other slots and buffers are reserved for use by the kernel
278  */
279 struct netmap_ring {
280 	/*
281 	 * buf_ofs is meant to be used through macros.
282 	 * It contains the offset of the buffer region from this
283 	 * descriptor.
284 	 */
285 	const int64_t	buf_ofs;
286 	const uint32_t	num_slots;	/* number of slots in the ring. */
287 	const uint32_t	nr_buf_size;
288 	const uint16_t	ringid;
289 	const uint16_t	dir;		/* 0: tx, 1: rx */
290 
291 	uint32_t        head;		/* (u) first user slot */
292 	uint32_t        cur;		/* (u) wakeup point */
293 	uint32_t	tail;		/* (k) first kernel slot */
294 
295 	uint32_t	flags;
296 
297 	struct timeval	ts;		/* (k) time of last *sync() */
298 
299 	/* opaque room for a mutex or similar object */
300 #if !defined(_WIN32) || defined(__CYGWIN__)
301 	uint8_t	__attribute__((__aligned__(NM_CACHE_ALIGN))) sem[128];
302 #else
303 	uint8_t	__declspec(align(NM_CACHE_ALIGN)) sem[128];
304 #endif
305 
306 	/* the slots follow. This struct has variable size */
307 	struct netmap_slot slot[0];	/* array of slots. */
308 };
309 
310 
311 /*
312  * RING FLAGS
313  */
314 #define	NR_TIMESTAMP	0x0002		/* set timestamp on *sync() */
315 	/*
316 	 * updates the 'ts' field on each netmap syscall. This saves
317 	 * saves a separate gettimeofday(), and is not much worse than
318 	 * software timestamps generated in the interrupt handler.
319 	 */
320 
321 #define	NR_FORWARD	0x0004		/* enable NS_FORWARD for ring */
322  	/*
323 	 * Enables the NS_FORWARD slot flag for the ring.
324 	 */
325 
326 
327 /*
328  * Netmap representation of an interface and its queue(s).
329  * This is initialized by the kernel when binding a file
330  * descriptor to a port, and should be considered as readonly
331  * by user programs. The kernel never uses it.
332  *
333  * There is one netmap_if for each file descriptor on which we want
334  * to select/poll.
335  * select/poll operates on one or all pairs depending on the value of
336  * nmr_queueid passed on the ioctl.
337  */
338 struct netmap_if {
339 	char		ni_name[IFNAMSIZ]; /* name of the interface. */
340 	const uint32_t	ni_version;	/* API version, currently unused */
341 	const uint32_t	ni_flags;	/* properties */
342 #define	NI_PRIV_MEM	0x1		/* private memory region */
343 
344 	/*
345 	 * The number of packet rings available in netmap mode.
346 	 * Physical NICs can have different numbers of tx and rx rings.
347 	 * Physical NICs also have a 'host' ring pair.
348 	 * Additionally, clients can request additional ring pairs to
349 	 * be used for internal communication.
350 	 */
351 	const uint32_t	ni_tx_rings;	/* number of HW tx rings */
352 	const uint32_t	ni_rx_rings;	/* number of HW rx rings */
353 
354 	uint32_t	ni_bufs_head;	/* head index for extra bufs */
355 	uint32_t	ni_spare1[5];
356 	/*
357 	 * The following array contains the offset of each netmap ring
358 	 * from this structure, in the following order:
359 	 * NIC tx rings (ni_tx_rings); host tx ring (1); extra tx rings;
360 	 * NIC rx rings (ni_rx_rings); host tx ring (1); extra rx rings.
361 	 *
362 	 * The area is filled up by the kernel on NIOCREGIF,
363 	 * and then only read by userspace code.
364 	 */
365 	const ssize_t	ring_ofs[0];
366 };
367 
368 
369 #ifndef NIOCREGIF
370 /*
371  * ioctl names and related fields
372  *
373  * NIOCTXSYNC, NIOCRXSYNC synchronize tx or rx queues,
374  *	whose identity is set in NIOCREGIF through nr_ringid.
375  *	These are non blocking and take no argument.
376  *
377  * NIOCGINFO takes a struct ifreq, the interface name is the input,
378  *	the outputs are number of queues and number of descriptor
379  *	for each queue (useful to set number of threads etc.).
380  *	The info returned is only advisory and may change before
381  *	the interface is bound to a file descriptor.
382  *
383  * NIOCREGIF takes an interface name within a struct nmre,
384  *	and activates netmap mode on the interface (if possible).
385  *
386  * The argument to NIOCGINFO/NIOCREGIF overlays struct ifreq so we
387  * can pass it down to other NIC-related ioctls.
388  *
389  * The actual argument (struct nmreq) has a number of options to request
390  * different functions.
391  * The following are used in NIOCREGIF when nr_cmd == 0:
392  *
393  * nr_name	(in)
394  *	The name of the port (em0, valeXXX:YYY, etc.)
395  *	limited to IFNAMSIZ for backward compatibility.
396  *
397  * nr_version	(in/out)
398  *	Must match NETMAP_API as used in the kernel, error otherwise.
399  *	Always returns the desired value on output.
400  *
401  * nr_tx_slots, nr_tx_slots, nr_tx_rings, nr_rx_rings (in/out)
402  *	On input, non-zero values may be used to reconfigure the port
403  *	according to the requested values, but this is not guaranteed.
404  *	On output the actual values in use are reported.
405  *
406  * nr_ringid (in)
407  *	Indicates how rings should be bound to the file descriptors.
408  *	If nr_flags != 0, then the low bits (in NETMAP_RING_MASK)
409  *	are used to indicate the ring number, and nr_flags specifies
410  *	the actual rings to bind. NETMAP_NO_TX_POLL is unaffected.
411  *
412  *	NOTE: THE FOLLOWING (nr_flags == 0) IS DEPRECATED:
413  *	If nr_flags == 0, NETMAP_HW_RING and NETMAP_SW_RING control
414  *	the binding as follows:
415  *	0 (default)			binds all physical rings
416  *	NETMAP_HW_RING | ring number	binds a single ring pair
417  *	NETMAP_SW_RING			binds only the host tx/rx rings
418  *
419  *	NETMAP_NO_TX_POLL can be OR-ed to make select()/poll() push
420  *		packets on tx rings only if POLLOUT is set.
421  *		The default is to push any pending packet.
422  *
423  *	NETMAP_DO_RX_POLL can be OR-ed to make select()/poll() release
424  *		packets on rx rings also when POLLIN is NOT set.
425  *		The default is to touch the rx ring only with POLLIN.
426  *		Note that this is the opposite of TX because it
427  *		reflects the common usage.
428  *
429  *	NOTE: NETMAP_PRIV_MEM IS DEPRECATED, use nr_arg2 instead.
430  *	NETMAP_PRIV_MEM is set on return for ports that do not use
431  *		the global memory allocator.
432  *		This information is not significant and applications
433  *		should look at the region id in nr_arg2
434  *
435  * nr_flags	is the recommended mode to indicate which rings should
436  *		be bound to a file descriptor. Values are NR_REG_*
437  *
438  * nr_arg1 (in)	The number of extra rings to be reserved.
439  *		Especially when allocating a VALE port the system only
440  *		allocates the amount of memory needed for the port.
441  *		If more shared memory rings are desired (e.g. for pipes),
442  *		the first invocation for the same basename/allocator
443  *		should specify a suitable number. Memory cannot be
444  *		extended after the first allocation without closing
445  *		all ports on the same region.
446  *
447  * nr_arg2 (in/out) The identity of the memory region used.
448  *		On input, 0 means the system decides autonomously,
449  *		other values may try to select a specific region.
450  *		On return the actual value is reported.
451  *		Region '1' is the global allocator, normally shared
452  *		by all interfaces. Other values are private regions.
453  *		If two ports the same region zero-copy is possible.
454  *
455  * nr_arg3 (in/out)	number of extra buffers to be allocated.
456  *
457  *
458  *
459  * nr_cmd (in)	if non-zero indicates a special command:
460  *	NETMAP_BDG_ATTACH	 and nr_name = vale*:ifname
461  *		attaches the NIC to the switch; nr_ringid specifies
462  *		which rings to use. Used by vale-ctl -a ...
463  *	    nr_arg1 = NETMAP_BDG_HOST also attaches the host port
464  *		as in vale-ctl -h ...
465  *
466  *	NETMAP_BDG_DETACH	and nr_name = vale*:ifname
467  *		disconnects a previously attached NIC.
468  *		Used by vale-ctl -d ...
469  *
470  *	NETMAP_BDG_LIST
471  *		list the configuration of VALE switches.
472  *
473  *	NETMAP_BDG_VNET_HDR
474  *		Set the virtio-net header length used by the client
475  *		of a VALE switch port.
476  *
477  *	NETMAP_BDG_NEWIF
478  *		create a persistent VALE port with name nr_name.
479  *		Used by vale-ctl -n ...
480  *
481  *	NETMAP_BDG_DELIF
482  *		delete a persistent VALE port. Used by vale-ctl -d ...
483  *
484  * nr_arg1, nr_arg2, nr_arg3  (in/out)		command specific
485  *
486  *
487  *
488  */
489 
490 
491 /*
492  * struct nmreq overlays a struct ifreq (just the name)
493  */
494 struct nmreq {
495 	char		nr_name[IFNAMSIZ];
496 	uint32_t	nr_version;	/* API version */
497 	uint32_t	nr_offset;	/* nifp offset in the shared region */
498 	uint32_t	nr_memsize;	/* size of the shared region */
499 	uint32_t	nr_tx_slots;	/* slots in tx rings */
500 	uint32_t	nr_rx_slots;	/* slots in rx rings */
501 	uint16_t	nr_tx_rings;	/* number of tx rings */
502 	uint16_t	nr_rx_rings;	/* number of rx rings */
503 
504 	uint16_t	nr_ringid;	/* ring(s) we care about */
505 #define NETMAP_HW_RING		0x4000	/* single NIC ring pair */
506 #define NETMAP_SW_RING		0x2000	/* only host ring pair */
507 
508 #define NETMAP_RING_MASK	0x0fff	/* the ring number */
509 
510 #define NETMAP_NO_TX_POLL	0x1000	/* no automatic txsync on poll */
511 
512 #define NETMAP_DO_RX_POLL	0x8000	/* DO automatic rxsync on poll */
513 
514 	uint16_t	nr_cmd;
515 #define NETMAP_BDG_ATTACH	1	/* attach the NIC */
516 #define NETMAP_BDG_DETACH	2	/* detach the NIC */
517 #define NETMAP_BDG_REGOPS	3	/* register bridge callbacks */
518 #define NETMAP_BDG_LIST		4	/* get bridge's info */
519 #define NETMAP_BDG_VNET_HDR     5       /* set the port virtio-net-hdr length */
520 #define NETMAP_BDG_OFFSET	NETMAP_BDG_VNET_HDR	/* deprecated alias */
521 #define NETMAP_BDG_NEWIF	6	/* create a virtual port */
522 #define NETMAP_BDG_DELIF	7	/* destroy a virtual port */
523 #define NETMAP_PT_HOST_CREATE	8	/* create ptnetmap kthreads */
524 #define NETMAP_PT_HOST_DELETE	9	/* delete ptnetmap kthreads */
525 #define NETMAP_BDG_POLLING_ON	10	/* delete polling kthread */
526 #define NETMAP_BDG_POLLING_OFF	11	/* delete polling kthread */
527 #define NETMAP_VNET_HDR_GET	12      /* get the port virtio-net-hdr length */
528 #define NETMAP_POOLS_INFO_GET	13	/* get memory allocator pools info */
529 	uint16_t	nr_arg1;	/* reserve extra rings in NIOCREGIF */
530 #define NETMAP_BDG_HOST		1	/* attach the host stack on ATTACH */
531 
532 	uint16_t	nr_arg2;
533 	uint32_t	nr_arg3;	/* req. extra buffers in NIOCREGIF */
534 	uint32_t	nr_flags;
535 	/* various modes, extends nr_ringid */
536 	uint32_t	spare2[1];
537 };
538 
539 #define NR_REG_MASK		0xf /* values for nr_flags */
540 enum {	NR_REG_DEFAULT	= 0,	/* backward compat, should not be used. */
541 	NR_REG_ALL_NIC	= 1,
542 	NR_REG_SW	= 2,
543 	NR_REG_NIC_SW	= 3,
544 	NR_REG_ONE_NIC	= 4,
545 	NR_REG_PIPE_MASTER = 5,
546 	NR_REG_PIPE_SLAVE = 6,
547 };
548 /* monitor uses the NR_REG to select the rings to monitor */
549 #define NR_MONITOR_TX	0x100
550 #define NR_MONITOR_RX	0x200
551 #define NR_ZCOPY_MON	0x400
552 /* request exclusive access to the selected rings */
553 #define NR_EXCLUSIVE	0x800
554 /* request ptnetmap host support */
555 #define NR_PASSTHROUGH_HOST	NR_PTNETMAP_HOST /* deprecated */
556 #define NR_PTNETMAP_HOST	0x1000
557 #define NR_RX_RINGS_ONLY	0x2000
558 #define NR_TX_RINGS_ONLY	0x4000
559 /* Applications set this flag if they are able to deal with virtio-net headers,
560  * that is send/receive frames that start with a virtio-net header.
561  * If not set, NIOCREGIF will fail with netmap ports that require applications
562  * to use those headers. If the flag is set, the application can use the
563  * NETMAP_VNET_HDR_GET command to figure out the header length. */
564 #define NR_ACCEPT_VNET_HDR	0x8000
565 
566 #define	NM_BDG_NAME		"vale"	/* prefix for bridge port name */
567 
568 /*
569  * Windows does not have _IOWR(). _IO(), _IOW() and _IOR() are defined
570  * in ws2def.h but not sure if they are in the form we need.
571  * XXX so we redefine them
572  * in a convenient way to use for DeviceIoControl signatures
573  */
574 #ifdef _WIN32
575 #undef _IO	// ws2def.h
576 #define _WIN_NM_IOCTL_TYPE 40000
577 #define _IO(_c, _n)	CTL_CODE(_WIN_NM_IOCTL_TYPE, ((_n) + 0x800) , \
578 		METHOD_BUFFERED, FILE_ANY_ACCESS  )
579 #define _IO_direct(_c, _n)	CTL_CODE(_WIN_NM_IOCTL_TYPE, ((_n) + 0x800) , \
580 		METHOD_OUT_DIRECT, FILE_ANY_ACCESS  )
581 
582 #define _IOWR(_c, _n, _s)	_IO(_c, _n)
583 
584 /* We havesome internal sysctl in addition to the externally visible ones */
585 #define NETMAP_MMAP _IO_direct('i', 160)	// note METHOD_OUT_DIRECT
586 #define NETMAP_POLL _IO('i', 162)
587 
588 /* and also two setsockopt for sysctl emulation */
589 #define NETMAP_SETSOCKOPT _IO('i', 140)
590 #define NETMAP_GETSOCKOPT _IO('i', 141)
591 
592 
593 //These linknames are for the Netmap Core Driver
594 #define NETMAP_NT_DEVICE_NAME			L"\\Device\\NETMAP"
595 #define NETMAP_DOS_DEVICE_NAME			L"\\DosDevices\\netmap"
596 
597 //Definition of a structure used to pass a virtual address within an IOCTL
598 typedef struct _MEMORY_ENTRY {
599 	PVOID       pUsermodeVirtualAddress;
600 } MEMORY_ENTRY, *PMEMORY_ENTRY;
601 
602 typedef struct _POLL_REQUEST_DATA {
603 	int events;
604 	int timeout;
605 	int revents;
606 } POLL_REQUEST_DATA;
607 
608 #endif /* _WIN32 */
609 
610 /*
611  * FreeBSD uses the size value embedded in the _IOWR to determine
612  * how much to copy in/out. So we need it to match the actual
613  * data structure we pass. We put some spares in the structure
614  * to ease compatibility with other versions
615  */
616 #define NIOCGINFO	_IOWR('i', 145, struct nmreq) /* return IF info */
617 #define NIOCREGIF	_IOWR('i', 146, struct nmreq) /* interface register */
618 #define NIOCTXSYNC	_IO('i', 148) /* sync tx queues */
619 #define NIOCRXSYNC	_IO('i', 149) /* sync rx queues */
620 #define NIOCCONFIG	_IOWR('i',150, struct nm_ifreq) /* for ext. modules */
621 #endif /* !NIOCREGIF */
622 
623 
624 /*
625  * Helper functions for kernel and userspace
626  */
627 
628 /*
629  * check if space is available in the ring.
630  */
631 static inline int
632 nm_ring_empty(struct netmap_ring *ring)
633 {
634 	return (ring->cur == ring->tail);
635 }
636 
637 /*
638  * Opaque structure that is passed to an external kernel
639  * module via ioctl(fd, NIOCCONFIG, req) for a user-owned
640  * bridge port (at this point ephemeral VALE interface).
641  */
642 #define NM_IFRDATA_LEN 256
643 struct nm_ifreq {
644 	char nifr_name[IFNAMSIZ];
645 	char data[NM_IFRDATA_LEN];
646 };
647 
648 #endif /* _NET_NETMAP_H_ */
649