2 * Copyright (C) 2011-2014 Matteo Landi, Luigi Rizzo. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
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
34 * http://info.iet.unipi.it/~luigi/netmap/
36 * This API is also used to communicate with the VALE software switch
39 #ifndef _NET_NETMAP_H_
40 #define _NET_NETMAP_H_
42 #define NETMAP_API 11 /* current API version */
44 #define NETMAP_MIN_API 11 /* min and max versions accepted */
45 #define NETMAP_MAX_API 15
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.
52 #define NM_CACHE_ALIGN 128
55 * --- Netmap data structures ---
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.
62 KERNEL (opaque, obviously)
64 ====================================================================
66 USERSPACE | struct netmap_ring
67 +---->+---------------+
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]
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] | +---------------+
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.
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).
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.
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.
103 * In user space, the buffer address is computed as
104 * (char *)ring + buf_ofs + index * NETMAP_BUF_SIZE
106 * Added in NETMAP_API 11:
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.
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.
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.
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.
129 * + NIOCREGIF can also attach to 'monitor' rings that replicate
130 * the content of specific rings, also from the same memory space.
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
143 * struct netmap_slot is a buffer descriptor
146 uint32_t buf_idx; /* buffer index */
147 uint16_t len; /* length for this slot */
148 uint16_t flags; /* buf changed, etc. */
149 uint64_t ptr; /* pointer for indirect buffers */
153 * The following flags control how the slot is used
156 #define NS_BUF_CHANGED 0x0001 /* buf_idx changed */
158 * must be set whenever buf_idx is changed (as it might be
159 * necessary to recompute the physical address and mapping)
162 #define NS_REPORT 0x0002 /* ask the hardware to report results */
164 * Request notification when slot is used by the hardware.
165 * Normally transmit completions are handled lazily and
166 * may be unreported. This flag lets us know when a slot
167 * has been sent (e.g. to terminate the sender).
170 #define NS_FORWARD 0x0004 /* pass packet 'forward' */
172 * (Only for physical ports, rx rings with NR_FORWARD set).
173 * Slot released to the kernel (i.e. before ring->head) with
174 * this flag set are passed to the peer ring (host/NIC),
175 * thus restoring the host-NIC connection for these slots.
176 * This supports efficient traffic monitoring or firewalling.
179 #define NS_NO_LEARN 0x0008 /* disable bridge learning */
181 * On a VALE switch, do not 'learn' the source port for
185 #define NS_INDIRECT 0x0010 /* userspace buffer */
187 * (VALE tx rings only) data is in a userspace buffer,
188 * whose address is in the 'ptr' field in the slot.
191 #define NS_MOREFRAG 0x0020 /* packet has more fragments */
194 * Set on all but the last slot of a multi-segment packet.
195 * The 'len' field refers to the individual fragment.
198 #define NS_PORT_SHIFT 8
199 #define NS_PORT_MASK (0xff << NS_PORT_SHIFT)
201 * The high 8 bits of the flag, if not zero, indicate the
202 * destination port for the VALE switch, overriding
206 #define NS_RFRAGS(_slot) ( ((_slot)->flags >> 8) & 0xff)
208 * (VALE rx rings only) the high 8 bits
209 * are the number of fragments.
216 * Netmap representation of a TX or RX ring (also known as "queue").
217 * This is a queue implemented as a fixed-size circular array.
218 * At the software level the important fields are: head, cur, tail.
222 * head first slot available for transmission.
223 * cur wakeup point. select() and poll() will unblock
224 * when 'tail' moves past 'cur'
225 * tail (readonly) first slot reserved to the kernel
227 * [head .. tail-1] can be used for new packets to send;
228 * 'head' and 'cur' must be incremented as slots are filled
229 * with new packets to be sent;
230 * 'cur' can be moved further ahead if we need more space
231 * for new transmissions. XXX todo (2014-03-12)
235 * head first valid received packet
236 * cur wakeup point. select() and poll() will unblock
237 * when 'tail' moves past 'cur'
238 * tail (readonly) first slot reserved to the kernel
240 * [head .. tail-1] contain received packets;
241 * 'head' and 'cur' must be incremented as slots are consumed
242 * and can be returned to the kernel;
243 * 'cur' can be moved further ahead if we want to wait for
244 * new packets without returning the previous ones.
246 * DATA OWNERSHIP/LOCKING:
247 * The netmap_ring, and all slots and buffers in the range
248 * [head .. tail-1] are owned by the user program;
249 * the kernel only accesses them during a netmap system call
250 * and in the user thread context.
252 * Other slots and buffers are reserved for use by the kernel
256 * buf_ofs is meant to be used through macros.
257 * It contains the offset of the buffer region from this
260 const int64_t buf_ofs;
261 const uint32_t num_slots; /* number of slots in the ring. */
262 const uint32_t nr_buf_size;
263 const uint16_t ringid;
264 const uint16_t dir; /* 0: tx, 1: rx */
266 uint32_t head; /* (u) first user slot */
267 uint32_t cur; /* (u) wakeup point */
268 uint32_t tail; /* (k) first kernel slot */
272 struct timeval ts; /* (k) time of last *sync() */
274 /* opaque room for a mutex or similar object */
275 uint8_t sem[128] __attribute__((__aligned__(NM_CACHE_ALIGN)));
277 /* the slots follow. This struct has variable size */
278 struct netmap_slot slot[0]; /* array of slots. */
285 #define NR_TIMESTAMP 0x0002 /* set timestamp on *sync() */
287 * updates the 'ts' field on each netmap syscall. This saves
288 * saves a separate gettimeofday(), and is not much worse than
289 * software timestamps generated in the interrupt handler.
292 #define NR_FORWARD 0x0004 /* enable NS_FORWARD for ring */
294 * Enables the NS_FORWARD slot flag for the ring.
299 * Netmap representation of an interface and its queue(s).
300 * This is initialized by the kernel when binding a file
301 * descriptor to a port, and should be considered as readonly
302 * by user programs. The kernel never uses it.
304 * There is one netmap_if for each file descriptor on which we want
306 * select/poll operates on one or all pairs depending on the value of
307 * nmr_queueid passed on the ioctl.
310 char ni_name[IFNAMSIZ]; /* name of the interface. */
311 const uint32_t ni_version; /* API version, currently unused */
312 const uint32_t ni_flags; /* properties */
313 #define NI_PRIV_MEM 0x1 /* private memory region */
316 * The number of packet rings available in netmap mode.
317 * Physical NICs can have different numbers of tx and rx rings.
318 * Physical NICs also have a 'host' ring pair.
319 * Additionally, clients can request additional ring pairs to
320 * be used for internal communication.
322 const uint32_t ni_tx_rings; /* number of HW tx rings */
323 const uint32_t ni_rx_rings; /* number of HW rx rings */
325 uint32_t ni_bufs_head; /* head index for extra bufs */
326 uint32_t ni_spare1[5];
328 * The following array contains the offset of each netmap ring
329 * from this structure, in the following order:
330 * NIC tx rings (ni_tx_rings); host tx ring (1); extra tx rings;
331 * NIC rx rings (ni_rx_rings); host tx ring (1); extra rx rings.
333 * The area is filled up by the kernel on NIOCREGIF,
334 * and then only read by userspace code.
336 const ssize_t ring_ofs[0];
342 * ioctl names and related fields
344 * NIOCTXSYNC, NIOCRXSYNC synchronize tx or rx queues,
345 * whose identity is set in NIOCREGIF through nr_ringid.
346 * These are non blocking and take no argument.
348 * NIOCGINFO takes a struct ifreq, the interface name is the input,
349 * the outputs are number of queues and number of descriptor
350 * for each queue (useful to set number of threads etc.).
351 * The info returned is only advisory and may change before
352 * the interface is bound to a file descriptor.
354 * NIOCREGIF takes an interface name within a struct nmre,
355 * and activates netmap mode on the interface (if possible).
357 * The argument to NIOCGINFO/NIOCREGIF overlays struct ifreq so we
358 * can pass it down to other NIC-related ioctls.
360 * The actual argument (struct nmreq) has a number of options to request
361 * different functions.
362 * The following are used in NIOCREGIF when nr_cmd == 0:
365 * The name of the port (em0, valeXXX:YYY, etc.)
366 * limited to IFNAMSIZ for backward compatibility.
368 * nr_version (in/out)
369 * Must match NETMAP_API as used in the kernel, error otherwise.
370 * Always returns the desired value on output.
372 * nr_tx_slots, nr_tx_slots, nr_tx_rings, nr_rx_rings (in/out)
373 * On input, non-zero values may be used to reconfigure the port
374 * according to the requested values, but this is not guaranteed.
375 * On output the actual values in use are reported.
378 * Indicates how rings should be bound to the file descriptors.
379 * If nr_flags != 0, then the low bits (in NETMAP_RING_MASK)
380 * are used to indicate the ring number, and nr_flags specifies
381 * the actual rings to bind. NETMAP_NO_TX_POLL is unaffected.
383 * NOTE: THE FOLLOWING (nr_flags == 0) IS DEPRECATED:
384 * If nr_flags == 0, NETMAP_HW_RING and NETMAP_SW_RING control
385 * the binding as follows:
386 * 0 (default) binds all physical rings
387 * NETMAP_HW_RING | ring number binds a single ring pair
388 * NETMAP_SW_RING binds only the host tx/rx rings
390 * NETMAP_NO_TX_POLL can be OR-ed to make select()/poll() push
391 * packets on tx rings only if POLLOUT is set.
392 * The default is to push any pending packet.
394 * NETMAP_DO_RX_POLL can be OR-ed to make select()/poll() release
395 * packets on rx rings also when POLLIN is NOT set.
396 * The default is to touch the rx ring only with POLLIN.
397 * Note that this is the opposite of TX because it
398 * reflects the common usage.
400 * NOTE: NETMAP_PRIV_MEM IS DEPRECATED, use nr_arg2 instead.
401 * NETMAP_PRIV_MEM is set on return for ports that do not use
402 * the global memory allocator.
403 * This information is not significant and applications
404 * should look at the region id in nr_arg2
406 * nr_flags is the recommended mode to indicate which rings should
407 * be bound to a file descriptor. Values are NR_REG_*
409 * nr_arg1 (in) The number of extra rings to be reserved.
410 * Especially when allocating a VALE port the system only
411 * allocates the amount of memory needed for the port.
412 * If more shared memory rings are desired (e.g. for pipes),
413 * the first invocation for the same basename/allocator
414 * should specify a suitable number. Memory cannot be
415 * extended after the first allocation without closing
416 * all ports on the same region.
418 * nr_arg2 (in/out) The identity of the memory region used.
419 * On input, 0 means the system decides autonomously,
420 * other values may try to select a specific region.
421 * On return the actual value is reported.
422 * Region '1' is the global allocator, normally shared
423 * by all interfaces. Other values are private regions.
424 * If two ports the same region zero-copy is possible.
426 * nr_arg3 (in/out) number of extra buffers to be allocated.
430 * nr_cmd (in) if non-zero indicates a special command:
431 * NETMAP_BDG_ATTACH and nr_name = vale*:ifname
432 * attaches the NIC to the switch; nr_ringid specifies
433 * which rings to use. Used by vale-ctl -a ...
434 * nr_arg1 = NETMAP_BDG_HOST also attaches the host port
435 * as in vale-ctl -h ...
437 * NETMAP_BDG_DETACH and nr_name = vale*:ifname
438 * disconnects a previously attached NIC.
439 * Used by vale-ctl -d ...
442 * list the configuration of VALE switches.
444 * NETMAP_BDG_VNET_HDR
445 * Set the virtio-net header length used by the client
446 * of a VALE switch port.
448 * nr_arg1, nr_arg2, nr_arg3 (in/out) command specific
456 * struct nmreq overlays a struct ifreq (just the name)
459 char nr_name[IFNAMSIZ];
460 uint32_t nr_version; /* API version */
461 uint32_t nr_offset; /* nifp offset in the shared region */
462 uint32_t nr_memsize; /* size of the shared region */
463 uint32_t nr_tx_slots; /* slots in tx rings */
464 uint32_t nr_rx_slots; /* slots in rx rings */
465 uint16_t nr_tx_rings; /* number of tx rings */
466 uint16_t nr_rx_rings; /* number of rx rings */
468 uint16_t nr_ringid; /* ring(s) we care about */
469 #define NETMAP_HW_RING 0x4000 /* single NIC ring pair */
470 #define NETMAP_SW_RING 0x2000 /* only host ring pair */
472 #define NETMAP_RING_MASK 0x0fff /* the ring number */
474 #define NETMAP_NO_TX_POLL 0x1000 /* no automatic txsync on poll */
476 #define NETMAP_DO_RX_POLL 0x8000 /* DO automatic rxsync on poll */
479 #define NETMAP_BDG_ATTACH 1 /* attach the NIC */
480 #define NETMAP_BDG_DETACH 2 /* detach the NIC */
481 #define NETMAP_BDG_LOOKUP_REG 3 /* register lookup function */
482 #define NETMAP_BDG_LIST 4 /* get bridge's info */
483 #define NETMAP_BDG_VNET_HDR 5 /* set the port virtio-net-hdr length */
484 #define NETMAP_BDG_OFFSET NETMAP_BDG_VNET_HDR /* deprecated alias */
486 uint16_t nr_arg1; /* reserve extra rings in NIOCREGIF */
487 #define NETMAP_BDG_HOST 1 /* attach the host stack on ATTACH */
490 uint32_t nr_arg3; /* req. extra buffers in NIOCREGIF */
492 /* various modes, extends nr_ringid */
496 #define NR_REG_MASK 0xf /* values for nr_flags */
497 enum { NR_REG_DEFAULT = 0, /* backward compat, should not be used. */
502 NR_REG_PIPE_MASTER = 5,
503 NR_REG_PIPE_SLAVE = 6,
505 /* monitor uses the NR_REG to select the rings to monitor */
506 #define NR_MONITOR_TX 0x100
507 #define NR_MONITOR_RX 0x200
511 * FreeBSD uses the size value embedded in the _IOWR to determine
512 * how much to copy in/out. So we need it to match the actual
513 * data structure we pass. We put some spares in the structure
514 * to ease compatibility with other versions
516 #define NIOCGINFO _IOWR('i', 145, struct nmreq) /* return IF info */
517 #define NIOCREGIF _IOWR('i', 146, struct nmreq) /* interface register */
518 #define NIOCTXSYNC _IO('i', 148) /* sync tx queues */
519 #define NIOCRXSYNC _IO('i', 149) /* sync rx queues */
520 #endif /* !NIOCREGIF */
524 * Helper functions for kernel and userspace
528 * check if space is available in the ring.
531 nm_ring_empty(struct netmap_ring *ring)
533 return (ring->cur == ring->tail);
536 #endif /* _NET_NETMAP_H_ */