168b8534bSLuigi Rizzo /* 217885a7bSLuigi Rizzo * Copyright (C) 2011-2014 Matteo Landi, Luigi Rizzo. All rights reserved. 368b8534bSLuigi Rizzo * 468b8534bSLuigi Rizzo * Redistribution and use in source and binary forms, with or without 5f9790aebSLuigi Rizzo * modification, are permitted provided that the following conditions 6f9790aebSLuigi Rizzo * are met: 768b8534bSLuigi Rizzo * 868b8534bSLuigi Rizzo * 1. Redistributions of source code must retain the above copyright 968b8534bSLuigi Rizzo * notice, this list of conditions and the following disclaimer. 1068b8534bSLuigi Rizzo * 2. Redistributions in binary form must reproduce the above copyright 1168b8534bSLuigi Rizzo * notice, this list of conditions and the following disclaimer in the 12f9790aebSLuigi Rizzo * documentation and/or other materials provided with the distribution. 1368b8534bSLuigi Rizzo * 14f9790aebSLuigi Rizzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``S IS''AND 1568b8534bSLuigi Rizzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16f9790aebSLuigi Rizzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17f9790aebSLuigi Rizzo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18f9790aebSLuigi Rizzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19f9790aebSLuigi Rizzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20f9790aebSLuigi Rizzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21f9790aebSLuigi Rizzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22f9790aebSLuigi Rizzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23f9790aebSLuigi Rizzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24f9790aebSLuigi Rizzo * SUCH DAMAGE. 2568b8534bSLuigi Rizzo */ 2668b8534bSLuigi Rizzo 2768b8534bSLuigi Rizzo /* 2868b8534bSLuigi Rizzo * $FreeBSD$ 2968b8534bSLuigi Rizzo * 3064ae02c3SLuigi Rizzo * Definitions of constants and the structures used by the netmap 3164ae02c3SLuigi Rizzo * framework, for the part visible to both kernel and userspace. 3264ae02c3SLuigi Rizzo * Detailed info on netmap is available with "man netmap" or at 3364ae02c3SLuigi Rizzo * 3464ae02c3SLuigi Rizzo * http://info.iet.unipi.it/~luigi/netmap/ 35ce3ee1e7SLuigi Rizzo * 36ce3ee1e7SLuigi Rizzo * This API is also used to communicate with the VALE software switch 3768b8534bSLuigi Rizzo */ 3868b8534bSLuigi Rizzo 3968b8534bSLuigi Rizzo #ifndef _NET_NETMAP_H_ 4068b8534bSLuigi Rizzo #define _NET_NETMAP_H_ 4168b8534bSLuigi Rizzo 42f0ea3689SLuigi Rizzo #define NETMAP_API 11 /* current API version */ 4317885a7bSLuigi Rizzo 44f0ea3689SLuigi Rizzo #define NETMAP_MIN_API 11 /* min and max versions accepted */ 45f0ea3689SLuigi Rizzo #define NETMAP_MAX_API 15 4617885a7bSLuigi Rizzo /* 4717885a7bSLuigi Rizzo * Some fields should be cache-aligned to reduce contention. 4817885a7bSLuigi Rizzo * The alignment is architecture and OS dependent, but rather than 4917885a7bSLuigi Rizzo * digging into OS headers to find the exact value we use an estimate 5017885a7bSLuigi Rizzo * that should cover most architectures. 5117885a7bSLuigi Rizzo */ 5217885a7bSLuigi Rizzo #define NM_CACHE_ALIGN 128 5317885a7bSLuigi Rizzo 5468b8534bSLuigi Rizzo /* 5568b8534bSLuigi Rizzo * --- Netmap data structures --- 5668b8534bSLuigi Rizzo * 57ce3ee1e7SLuigi Rizzo * The userspace data structures used by netmap are shown below. 58ce3ee1e7SLuigi Rizzo * They are allocated by the kernel and mmap()ed by userspace threads. 59ce3ee1e7SLuigi Rizzo * Pointers are implemented as memory offsets or indexes, 60ce3ee1e7SLuigi Rizzo * so that they can be easily dereferenced in kernel and userspace. 6168b8534bSLuigi Rizzo 62ce3ee1e7SLuigi Rizzo KERNEL (opaque, obviously) 6368b8534bSLuigi Rizzo 6464ae02c3SLuigi Rizzo ==================================================================== 6564ae02c3SLuigi Rizzo | 66ce3ee1e7SLuigi Rizzo USERSPACE | struct netmap_ring 6717885a7bSLuigi Rizzo +---->+---------------+ 6817885a7bSLuigi Rizzo / | head,cur,tail | 6917885a7bSLuigi Rizzo struct netmap_if (nifp, 1 per fd) / | buf_ofs | 7017885a7bSLuigi Rizzo +---------------+ / | other fields | 7117885a7bSLuigi Rizzo | ni_tx_rings | / +===============+ 72ce3ee1e7SLuigi Rizzo | ni_rx_rings | / | buf_idx, len | slot[0] 73ce3ee1e7SLuigi Rizzo | | / | flags, ptr | 7417885a7bSLuigi Rizzo | | / +---------------+ 75ce3ee1e7SLuigi Rizzo +===============+ / | buf_idx, len | slot[1] 76ce3ee1e7SLuigi Rizzo | txring_ofs[0] | (rel.to nifp)--' | flags, ptr | 7717885a7bSLuigi Rizzo | txring_ofs[1] | +---------------+ 78f0ea3689SLuigi Rizzo (tx+1 entries) (num_slots entries) 79ce3ee1e7SLuigi Rizzo | txring_ofs[t] | | buf_idx, len | slot[n-1] 80ce3ee1e7SLuigi Rizzo +---------------+ | flags, ptr | 8117885a7bSLuigi Rizzo | rxring_ofs[0] | +---------------+ 8268b8534bSLuigi Rizzo | rxring_ofs[1] | 83f0ea3689SLuigi Rizzo (rx+1 entries) 84ce3ee1e7SLuigi Rizzo | rxring_ofs[r] | 8568b8534bSLuigi Rizzo +---------------+ 8668b8534bSLuigi Rizzo 87f0ea3689SLuigi Rizzo * For each "interface" (NIC, host stack, PIPE, VALE switch port) bound to 88f0ea3689SLuigi Rizzo * a file descriptor, the mmap()ed region contains a (logically readonly) 89ce3ee1e7SLuigi Rizzo * struct netmap_if pointing to struct netmap_ring's. 90f0ea3689SLuigi Rizzo * 91ce3ee1e7SLuigi Rizzo * There is one netmap_ring per physical NIC ring, plus one tx/rx ring 92f0ea3689SLuigi Rizzo * pair attached to the host stack (this pair is unused for non-NIC ports). 93ce3ee1e7SLuigi Rizzo * 94ce3ee1e7SLuigi Rizzo * All physical/host stack ports share the same memory region, 95ce3ee1e7SLuigi Rizzo * so that zero-copy can be implemented between them. 96ce3ee1e7SLuigi Rizzo * VALE switch ports instead have separate memory regions. 97ce3ee1e7SLuigi Rizzo * 98ce3ee1e7SLuigi Rizzo * The netmap_ring is the userspace-visible replica of the NIC ring. 99ce3ee1e7SLuigi Rizzo * Each slot has the index of a buffer (MTU-sized and residing in the 100ce3ee1e7SLuigi Rizzo * mmapped region), its length and some flags. An extra 64-bit pointer 101ce3ee1e7SLuigi Rizzo * is provided for user-supplied buffers in the tx path. 102ce3ee1e7SLuigi Rizzo * 10368b8534bSLuigi Rizzo * In user space, the buffer address is computed as 10464ae02c3SLuigi Rizzo * (char *)ring + buf_ofs + index * NETMAP_BUF_SIZE 105f0ea3689SLuigi Rizzo * 106f0ea3689SLuigi Rizzo * Added in NETMAP_API 11: 107f0ea3689SLuigi Rizzo * 108f0ea3689SLuigi Rizzo * + NIOCREGIF can request the allocation of extra spare buffers from 109f0ea3689SLuigi Rizzo * the same memory pool. The desired number of buffers must be in 110f0ea3689SLuigi Rizzo * nr_arg3. The ioctl may return fewer buffers, depending on memory 111f0ea3689SLuigi Rizzo * availability. nr_arg3 will return the actual value, and, once 112f0ea3689SLuigi Rizzo * mapped, nifp->ni_bufs_head will be the index of the first buffer. 113f0ea3689SLuigi Rizzo * 114f0ea3689SLuigi Rizzo * The buffers are linked to each other using the first uint32_t 115f0ea3689SLuigi Rizzo * as the index. On close, ni_bufs_head must point to the list of 116f0ea3689SLuigi Rizzo * buffers to be released. 117f0ea3689SLuigi Rizzo * 118f0ea3689SLuigi Rizzo * + NIOCREGIF can request space for extra rings (and buffers) 119f0ea3689SLuigi Rizzo * allocated in the same memory space. The number of extra rings 120f0ea3689SLuigi Rizzo * is in nr_arg1, and is advisory. This is a no-op on NICs where 121f0ea3689SLuigi Rizzo * the size of the memory space is fixed. 122f0ea3689SLuigi Rizzo * 123f0ea3689SLuigi Rizzo * + NIOCREGIF can attach to PIPE rings sharing the same memory 124f0ea3689SLuigi Rizzo * space with a parent device. The ifname indicates the parent device, 125f0ea3689SLuigi Rizzo * which must already exist. Flags in nr_flags indicate if we want to 126f0ea3689SLuigi Rizzo * bind the master or slave side, the index (from nr_ringid) 1275c8c1004SLuigi Rizzo * is just a cookie and does not need to be sequential. 128f0ea3689SLuigi Rizzo * 129f0ea3689SLuigi Rizzo * + NIOCREGIF can also attach to 'monitor' rings that replicate 130f0ea3689SLuigi Rizzo * the content of specific rings, also from the same memory space. 131f0ea3689SLuigi Rizzo * 132f0ea3689SLuigi Rizzo * Extra flags in nr_flags support the above functions. 133f0ea3689SLuigi Rizzo * Application libraries may use the following naming scheme: 134f0ea3689SLuigi Rizzo * netmap:foo all NIC ring pairs 135f0ea3689SLuigi Rizzo * netmap:foo^ only host ring pair 136f0ea3689SLuigi Rizzo * netmap:foo+ all NIC ring + host ring pairs 137f0ea3689SLuigi Rizzo * netmap:foo-k the k-th NIC ring pair 138f0ea3689SLuigi Rizzo * netmap:foo{k PIPE ring pair k, master side 139f0ea3689SLuigi Rizzo * netmap:foo}k PIPE ring pair k, slave side 140ce3ee1e7SLuigi Rizzo */ 141ce3ee1e7SLuigi Rizzo 142ce3ee1e7SLuigi Rizzo /* 143ce3ee1e7SLuigi Rizzo * struct netmap_slot is a buffer descriptor 14417885a7bSLuigi Rizzo */ 14517885a7bSLuigi Rizzo struct netmap_slot { 14617885a7bSLuigi Rizzo uint32_t buf_idx; /* buffer index */ 14717885a7bSLuigi Rizzo uint16_t len; /* length for this slot */ 14817885a7bSLuigi Rizzo uint16_t flags; /* buf changed, etc. */ 14917885a7bSLuigi Rizzo uint64_t ptr; /* pointer for indirect buffers */ 15017885a7bSLuigi Rizzo }; 15117885a7bSLuigi Rizzo 15217885a7bSLuigi Rizzo /* 15317885a7bSLuigi Rizzo * The following flags control how the slot is used 15417885a7bSLuigi Rizzo */ 15517885a7bSLuigi Rizzo 15617885a7bSLuigi Rizzo #define NS_BUF_CHANGED 0x0001 /* buf_idx changed */ 15717885a7bSLuigi Rizzo /* 15817885a7bSLuigi Rizzo * must be set whenever buf_idx is changed (as it might be 15917885a7bSLuigi Rizzo * necessary to recompute the physical address and mapping) 160*847bf383SLuigi Rizzo * 161*847bf383SLuigi Rizzo * It is also set by the kernel whenever the buf_idx is 162*847bf383SLuigi Rizzo * changed internally (e.g., by pipes). Applications may 163*847bf383SLuigi Rizzo * use this information to know when they can reuse the 164*847bf383SLuigi Rizzo * contents of previously prepared buffers. 16517885a7bSLuigi Rizzo */ 16617885a7bSLuigi Rizzo 16717885a7bSLuigi Rizzo #define NS_REPORT 0x0002 /* ask the hardware to report results */ 16817885a7bSLuigi Rizzo /* 16917885a7bSLuigi Rizzo * Request notification when slot is used by the hardware. 17017885a7bSLuigi Rizzo * Normally transmit completions are handled lazily and 17117885a7bSLuigi Rizzo * may be unreported. This flag lets us know when a slot 17217885a7bSLuigi Rizzo * has been sent (e.g. to terminate the sender). 17317885a7bSLuigi Rizzo */ 17417885a7bSLuigi Rizzo 17517885a7bSLuigi Rizzo #define NS_FORWARD 0x0004 /* pass packet 'forward' */ 17617885a7bSLuigi Rizzo /* 17717885a7bSLuigi Rizzo * (Only for physical ports, rx rings with NR_FORWARD set). 17817885a7bSLuigi Rizzo * Slot released to the kernel (i.e. before ring->head) with 17917885a7bSLuigi Rizzo * this flag set are passed to the peer ring (host/NIC), 18017885a7bSLuigi Rizzo * thus restoring the host-NIC connection for these slots. 18117885a7bSLuigi Rizzo * This supports efficient traffic monitoring or firewalling. 18217885a7bSLuigi Rizzo */ 18317885a7bSLuigi Rizzo 18417885a7bSLuigi Rizzo #define NS_NO_LEARN 0x0008 /* disable bridge learning */ 18517885a7bSLuigi Rizzo /* 18617885a7bSLuigi Rizzo * On a VALE switch, do not 'learn' the source port for 18717885a7bSLuigi Rizzo * this buffer. 18817885a7bSLuigi Rizzo */ 18917885a7bSLuigi Rizzo 19017885a7bSLuigi Rizzo #define NS_INDIRECT 0x0010 /* userspace buffer */ 19117885a7bSLuigi Rizzo /* 19217885a7bSLuigi Rizzo * (VALE tx rings only) data is in a userspace buffer, 19317885a7bSLuigi Rizzo * whose address is in the 'ptr' field in the slot. 19417885a7bSLuigi Rizzo */ 19517885a7bSLuigi Rizzo 19617885a7bSLuigi Rizzo #define NS_MOREFRAG 0x0020 /* packet has more fragments */ 19717885a7bSLuigi Rizzo /* 19817885a7bSLuigi Rizzo * (VALE ports only) 19917885a7bSLuigi Rizzo * Set on all but the last slot of a multi-segment packet. 20017885a7bSLuigi Rizzo * The 'len' field refers to the individual fragment. 20117885a7bSLuigi Rizzo */ 20217885a7bSLuigi Rizzo 20317885a7bSLuigi Rizzo #define NS_PORT_SHIFT 8 20417885a7bSLuigi Rizzo #define NS_PORT_MASK (0xff << NS_PORT_SHIFT) 20517885a7bSLuigi Rizzo /* 20617885a7bSLuigi Rizzo * The high 8 bits of the flag, if not zero, indicate the 20701c039a1SLuigi Rizzo * destination port for the VALE switch, overriding 20801c039a1SLuigi Rizzo * the lookup table. 20968b8534bSLuigi Rizzo */ 21001c039a1SLuigi Rizzo 21117885a7bSLuigi Rizzo #define NS_RFRAGS(_slot) ( ((_slot)->flags >> 8) & 0xff) 212ce3ee1e7SLuigi Rizzo /* 21317885a7bSLuigi Rizzo * (VALE rx rings only) the high 8 bits 214ce3ee1e7SLuigi Rizzo * are the number of fragments. 215ce3ee1e7SLuigi Rizzo */ 21617885a7bSLuigi Rizzo 21768b8534bSLuigi Rizzo 21868b8534bSLuigi Rizzo /* 219ce3ee1e7SLuigi Rizzo * struct netmap_ring 220ce3ee1e7SLuigi Rizzo * 22168b8534bSLuigi Rizzo * Netmap representation of a TX or RX ring (also known as "queue"). 22268b8534bSLuigi Rizzo * This is a queue implemented as a fixed-size circular array. 22317885a7bSLuigi Rizzo * At the software level the important fields are: head, cur, tail. 22468b8534bSLuigi Rizzo * 22568b8534bSLuigi Rizzo * In TX rings: 226ce3ee1e7SLuigi Rizzo * 22717885a7bSLuigi Rizzo * head first slot available for transmission. 22817885a7bSLuigi Rizzo * cur wakeup point. select() and poll() will unblock 22917885a7bSLuigi Rizzo * when 'tail' moves past 'cur' 23017885a7bSLuigi Rizzo * tail (readonly) first slot reserved to the kernel 231ce3ee1e7SLuigi Rizzo * 23217885a7bSLuigi Rizzo * [head .. tail-1] can be used for new packets to send; 23317885a7bSLuigi Rizzo * 'head' and 'cur' must be incremented as slots are filled 23417885a7bSLuigi Rizzo * with new packets to be sent; 23517885a7bSLuigi Rizzo * 'cur' can be moved further ahead if we need more space 2365c8c1004SLuigi Rizzo * for new transmissions. XXX todo (2014-03-12) 23768b8534bSLuigi Rizzo * 23868b8534bSLuigi Rizzo * In RX rings: 239ce3ee1e7SLuigi Rizzo * 24017885a7bSLuigi Rizzo * head first valid received packet 24117885a7bSLuigi Rizzo * cur wakeup point. select() and poll() will unblock 24217885a7bSLuigi Rizzo * when 'tail' moves past 'cur' 24317885a7bSLuigi Rizzo * tail (readonly) first slot reserved to the kernel 244ce3ee1e7SLuigi Rizzo * 24517885a7bSLuigi Rizzo * [head .. tail-1] contain received packets; 24617885a7bSLuigi Rizzo * 'head' and 'cur' must be incremented as slots are consumed 24717885a7bSLuigi Rizzo * and can be returned to the kernel; 24817885a7bSLuigi Rizzo * 'cur' can be moved further ahead if we want to wait for 24917885a7bSLuigi Rizzo * new packets without returning the previous ones. 25068b8534bSLuigi Rizzo * 25168b8534bSLuigi Rizzo * DATA OWNERSHIP/LOCKING: 25217885a7bSLuigi Rizzo * The netmap_ring, and all slots and buffers in the range 25317885a7bSLuigi Rizzo * [head .. tail-1] are owned by the user program; 25417885a7bSLuigi Rizzo * the kernel only accesses them during a netmap system call 25517885a7bSLuigi Rizzo * and in the user thread context. 25601c039a1SLuigi Rizzo * 25717885a7bSLuigi Rizzo * Other slots and buffers are reserved for use by the kernel 25868b8534bSLuigi Rizzo */ 25968b8534bSLuigi Rizzo struct netmap_ring { 26068b8534bSLuigi Rizzo /* 261ce3ee1e7SLuigi Rizzo * buf_ofs is meant to be used through macros. 26268b8534bSLuigi Rizzo * It contains the offset of the buffer region from this 26368b8534bSLuigi Rizzo * descriptor. 26468b8534bSLuigi Rizzo */ 26517885a7bSLuigi Rizzo const int64_t buf_ofs; 26668b8534bSLuigi Rizzo const uint32_t num_slots; /* number of slots in the ring. */ 26717885a7bSLuigi Rizzo const uint32_t nr_buf_size; 26817885a7bSLuigi Rizzo const uint16_t ringid; 26917885a7bSLuigi Rizzo const uint16_t dir; /* 0: tx, 1: rx */ 27068b8534bSLuigi Rizzo 27117885a7bSLuigi Rizzo uint32_t head; /* (u) first user slot */ 27217885a7bSLuigi Rizzo uint32_t cur; /* (u) wakeup point */ 27317885a7bSLuigi Rizzo uint32_t tail; /* (k) first kernel slot */ 27468b8534bSLuigi Rizzo 27517885a7bSLuigi Rizzo uint32_t flags; 27617885a7bSLuigi Rizzo 27717885a7bSLuigi Rizzo struct timeval ts; /* (k) time of last *sync() */ 27817885a7bSLuigi Rizzo 27917885a7bSLuigi Rizzo /* opaque room for a mutex or similar object */ 28017885a7bSLuigi Rizzo uint8_t sem[128] __attribute__((__aligned__(NM_CACHE_ALIGN))); 28168b8534bSLuigi Rizzo 28268b8534bSLuigi Rizzo /* the slots follow. This struct has variable size */ 28368b8534bSLuigi Rizzo struct netmap_slot slot[0]; /* array of slots. */ 28468b8534bSLuigi Rizzo }; 28568b8534bSLuigi Rizzo 28668b8534bSLuigi Rizzo 28768b8534bSLuigi Rizzo /* 28817885a7bSLuigi Rizzo * RING FLAGS 28917885a7bSLuigi Rizzo */ 29017885a7bSLuigi Rizzo #define NR_TIMESTAMP 0x0002 /* set timestamp on *sync() */ 29117885a7bSLuigi Rizzo /* 29217885a7bSLuigi Rizzo * updates the 'ts' field on each netmap syscall. This saves 29317885a7bSLuigi Rizzo * saves a separate gettimeofday(), and is not much worse than 29417885a7bSLuigi Rizzo * software timestamps generated in the interrupt handler. 29517885a7bSLuigi Rizzo */ 29617885a7bSLuigi Rizzo 29717885a7bSLuigi Rizzo #define NR_FORWARD 0x0004 /* enable NS_FORWARD for ring */ 29817885a7bSLuigi Rizzo /* 29917885a7bSLuigi Rizzo * Enables the NS_FORWARD slot flag for the ring. 30017885a7bSLuigi Rizzo */ 30117885a7bSLuigi Rizzo 30217885a7bSLuigi Rizzo 30317885a7bSLuigi Rizzo /* 30468b8534bSLuigi Rizzo * Netmap representation of an interface and its queue(s). 305ce3ee1e7SLuigi Rizzo * This is initialized by the kernel when binding a file 306ce3ee1e7SLuigi Rizzo * descriptor to a port, and should be considered as readonly 307ce3ee1e7SLuigi Rizzo * by user programs. The kernel never uses it. 308ce3ee1e7SLuigi Rizzo * 30968b8534bSLuigi Rizzo * There is one netmap_if for each file descriptor on which we want 310ce3ee1e7SLuigi Rizzo * to select/poll. 31168b8534bSLuigi Rizzo * select/poll operates on one or all pairs depending on the value of 31268b8534bSLuigi Rizzo * nmr_queueid passed on the ioctl. 31368b8534bSLuigi Rizzo */ 31468b8534bSLuigi Rizzo struct netmap_if { 31568b8534bSLuigi Rizzo char ni_name[IFNAMSIZ]; /* name of the interface. */ 316ce3ee1e7SLuigi Rizzo const uint32_t ni_version; /* API version, currently unused */ 317ce3ee1e7SLuigi Rizzo const uint32_t ni_flags; /* properties */ 318ce3ee1e7SLuigi Rizzo #define NI_PRIV_MEM 0x1 /* private memory region */ 319ce3ee1e7SLuigi Rizzo 32017885a7bSLuigi Rizzo /* 32117885a7bSLuigi Rizzo * The number of packet rings available in netmap mode. 32217885a7bSLuigi Rizzo * Physical NICs can have different numbers of tx and rx rings. 32317885a7bSLuigi Rizzo * Physical NICs also have a 'host' ring pair. 32417885a7bSLuigi Rizzo * Additionally, clients can request additional ring pairs to 32517885a7bSLuigi Rizzo * be used for internal communication. 32617885a7bSLuigi Rizzo */ 32717885a7bSLuigi Rizzo const uint32_t ni_tx_rings; /* number of HW tx rings */ 32817885a7bSLuigi Rizzo const uint32_t ni_rx_rings; /* number of HW rx rings */ 32917885a7bSLuigi Rizzo 330f0ea3689SLuigi Rizzo uint32_t ni_bufs_head; /* head index for extra bufs */ 331f0ea3689SLuigi Rizzo uint32_t ni_spare1[5]; 33268b8534bSLuigi Rizzo /* 33364ae02c3SLuigi Rizzo * The following array contains the offset of each netmap ring 33417885a7bSLuigi Rizzo * from this structure, in the following order: 33517885a7bSLuigi Rizzo * NIC tx rings (ni_tx_rings); host tx ring (1); extra tx rings; 33617885a7bSLuigi Rizzo * NIC rx rings (ni_rx_rings); host tx ring (1); extra rx rings. 33717885a7bSLuigi Rizzo * 338ce3ee1e7SLuigi Rizzo * The area is filled up by the kernel on NIOCREGIF, 33968b8534bSLuigi Rizzo * and then only read by userspace code. 34068b8534bSLuigi Rizzo */ 34168b8534bSLuigi Rizzo const ssize_t ring_ofs[0]; 34268b8534bSLuigi Rizzo }; 34368b8534bSLuigi Rizzo 34417885a7bSLuigi Rizzo 34568b8534bSLuigi Rizzo #ifndef NIOCREGIF 34668b8534bSLuigi Rizzo /* 34768b8534bSLuigi Rizzo * ioctl names and related fields 34868b8534bSLuigi Rizzo * 34917885a7bSLuigi Rizzo * NIOCTXSYNC, NIOCRXSYNC synchronize tx or rx queues, 35017885a7bSLuigi Rizzo * whose identity is set in NIOCREGIF through nr_ringid. 35117885a7bSLuigi Rizzo * These are non blocking and take no argument. 35217885a7bSLuigi Rizzo * 35368b8534bSLuigi Rizzo * NIOCGINFO takes a struct ifreq, the interface name is the input, 35468b8534bSLuigi Rizzo * the outputs are number of queues and number of descriptor 35568b8534bSLuigi Rizzo * for each queue (useful to set number of threads etc.). 356ce3ee1e7SLuigi Rizzo * The info returned is only advisory and may change before 357ce3ee1e7SLuigi Rizzo * the interface is bound to a file descriptor. 35868b8534bSLuigi Rizzo * 35917885a7bSLuigi Rizzo * NIOCREGIF takes an interface name within a struct nmre, 36068b8534bSLuigi Rizzo * and activates netmap mode on the interface (if possible). 36168b8534bSLuigi Rizzo * 36217885a7bSLuigi Rizzo * The argument to NIOCGINFO/NIOCREGIF overlays struct ifreq so we 36317885a7bSLuigi Rizzo * can pass it down to other NIC-related ioctls. 364f18be576SLuigi Rizzo * 36517885a7bSLuigi Rizzo * The actual argument (struct nmreq) has a number of options to request 36617885a7bSLuigi Rizzo * different functions. 367f0ea3689SLuigi Rizzo * The following are used in NIOCREGIF when nr_cmd == 0: 368f18be576SLuigi Rizzo * 36917885a7bSLuigi Rizzo * nr_name (in) 37017885a7bSLuigi Rizzo * The name of the port (em0, valeXXX:YYY, etc.) 37117885a7bSLuigi Rizzo * limited to IFNAMSIZ for backward compatibility. 372ce3ee1e7SLuigi Rizzo * 37317885a7bSLuigi Rizzo * nr_version (in/out) 37417885a7bSLuigi Rizzo * Must match NETMAP_API as used in the kernel, error otherwise. 37517885a7bSLuigi Rizzo * Always returns the desired value on output. 37617885a7bSLuigi Rizzo * 37717885a7bSLuigi Rizzo * nr_tx_slots, nr_tx_slots, nr_tx_rings, nr_rx_rings (in/out) 37817885a7bSLuigi Rizzo * On input, non-zero values may be used to reconfigure the port 37917885a7bSLuigi Rizzo * according to the requested values, but this is not guaranteed. 38017885a7bSLuigi Rizzo * On output the actual values in use are reported. 38117885a7bSLuigi Rizzo * 38217885a7bSLuigi Rizzo * nr_ringid (in) 38317885a7bSLuigi Rizzo * Indicates how rings should be bound to the file descriptors. 384f0ea3689SLuigi Rizzo * If nr_flags != 0, then the low bits (in NETMAP_RING_MASK) 385f0ea3689SLuigi Rizzo * are used to indicate the ring number, and nr_flags specifies 386f0ea3689SLuigi Rizzo * the actual rings to bind. NETMAP_NO_TX_POLL is unaffected. 387f0ea3689SLuigi Rizzo * 388f0ea3689SLuigi Rizzo * NOTE: THE FOLLOWING (nr_flags == 0) IS DEPRECATED: 389f0ea3689SLuigi Rizzo * If nr_flags == 0, NETMAP_HW_RING and NETMAP_SW_RING control 390f0ea3689SLuigi Rizzo * the binding as follows: 39117885a7bSLuigi Rizzo * 0 (default) binds all physical rings 39217885a7bSLuigi Rizzo * NETMAP_HW_RING | ring number binds a single ring pair 393ce3ee1e7SLuigi Rizzo * NETMAP_SW_RING binds only the host tx/rx rings 394ce3ee1e7SLuigi Rizzo * 39517885a7bSLuigi Rizzo * NETMAP_NO_TX_POLL can be OR-ed to make select()/poll() push 39617885a7bSLuigi Rizzo * packets on tx rings only if POLLOUT is set. 39717885a7bSLuigi Rizzo * The default is to push any pending packet. 398ce3ee1e7SLuigi Rizzo * 399f0ea3689SLuigi Rizzo * NETMAP_DO_RX_POLL can be OR-ed to make select()/poll() release 400f0ea3689SLuigi Rizzo * packets on rx rings also when POLLIN is NOT set. 401f0ea3689SLuigi Rizzo * The default is to touch the rx ring only with POLLIN. 402f0ea3689SLuigi Rizzo * Note that this is the opposite of TX because it 403f0ea3689SLuigi Rizzo * reflects the common usage. 404f0ea3689SLuigi Rizzo * 405f0ea3689SLuigi Rizzo * NOTE: NETMAP_PRIV_MEM IS DEPRECATED, use nr_arg2 instead. 406f0ea3689SLuigi Rizzo * NETMAP_PRIV_MEM is set on return for ports that do not use 407f0ea3689SLuigi Rizzo * the global memory allocator. 408f0ea3689SLuigi Rizzo * This information is not significant and applications 409f0ea3689SLuigi Rizzo * should look at the region id in nr_arg2 410f0ea3689SLuigi Rizzo * 411f0ea3689SLuigi Rizzo * nr_flags is the recommended mode to indicate which rings should 412f0ea3689SLuigi Rizzo * be bound to a file descriptor. Values are NR_REG_* 413f0ea3689SLuigi Rizzo * 414f0ea3689SLuigi Rizzo * nr_arg1 (in) The number of extra rings to be reserved. 415f0ea3689SLuigi Rizzo * Especially when allocating a VALE port the system only 416f0ea3689SLuigi Rizzo * allocates the amount of memory needed for the port. 417f0ea3689SLuigi Rizzo * If more shared memory rings are desired (e.g. for pipes), 418f0ea3689SLuigi Rizzo * the first invocation for the same basename/allocator 419f0ea3689SLuigi Rizzo * should specify a suitable number. Memory cannot be 420f0ea3689SLuigi Rizzo * extended after the first allocation without closing 421f0ea3689SLuigi Rizzo * all ports on the same region. 422f0ea3689SLuigi Rizzo * 423f0ea3689SLuigi Rizzo * nr_arg2 (in/out) The identity of the memory region used. 424f0ea3689SLuigi Rizzo * On input, 0 means the system decides autonomously, 425f0ea3689SLuigi Rizzo * other values may try to select a specific region. 426f0ea3689SLuigi Rizzo * On return the actual value is reported. 427f0ea3689SLuigi Rizzo * Region '1' is the global allocator, normally shared 428f0ea3689SLuigi Rizzo * by all interfaces. Other values are private regions. 429f0ea3689SLuigi Rizzo * If two ports the same region zero-copy is possible. 430f0ea3689SLuigi Rizzo * 431f0ea3689SLuigi Rizzo * nr_arg3 (in/out) number of extra buffers to be allocated. 432f0ea3689SLuigi Rizzo * 433f0ea3689SLuigi Rizzo * 434ce3ee1e7SLuigi Rizzo * 43517885a7bSLuigi Rizzo * nr_cmd (in) if non-zero indicates a special command: 43617885a7bSLuigi Rizzo * NETMAP_BDG_ATTACH and nr_name = vale*:ifname 43717885a7bSLuigi Rizzo * attaches the NIC to the switch; nr_ringid specifies 43817885a7bSLuigi Rizzo * which rings to use. Used by vale-ctl -a ... 43917885a7bSLuigi Rizzo * nr_arg1 = NETMAP_BDG_HOST also attaches the host port 44017885a7bSLuigi Rizzo * as in vale-ctl -h ... 441ce3ee1e7SLuigi Rizzo * 44217885a7bSLuigi Rizzo * NETMAP_BDG_DETACH and nr_name = vale*:ifname 44317885a7bSLuigi Rizzo * disconnects a previously attached NIC. 44417885a7bSLuigi Rizzo * Used by vale-ctl -d ... 445ce3ee1e7SLuigi Rizzo * 44617885a7bSLuigi Rizzo * NETMAP_BDG_LIST 44717885a7bSLuigi Rizzo * list the configuration of VALE switches. 44868b8534bSLuigi Rizzo * 449f0ea3689SLuigi Rizzo * NETMAP_BDG_VNET_HDR 450f0ea3689SLuigi Rizzo * Set the virtio-net header length used by the client 451f0ea3689SLuigi Rizzo * of a VALE switch port. 452f18be576SLuigi Rizzo * 4534bf50f18SLuigi Rizzo * NETMAP_BDG_NEWIF 4544bf50f18SLuigi Rizzo * create a persistent VALE port with name nr_name. 4554bf50f18SLuigi Rizzo * Used by vale-ctl -n ... 4564bf50f18SLuigi Rizzo * 4574bf50f18SLuigi Rizzo * NETMAP_BDG_DELIF 4584bf50f18SLuigi Rizzo * delete a persistent VALE port. Used by vale-ctl -d ... 4594bf50f18SLuigi Rizzo * 460f0ea3689SLuigi Rizzo * nr_arg1, nr_arg2, nr_arg3 (in/out) command specific 461f0ea3689SLuigi Rizzo * 462f0ea3689SLuigi Rizzo * 46317885a7bSLuigi Rizzo * 46468b8534bSLuigi Rizzo */ 46568b8534bSLuigi Rizzo 46617885a7bSLuigi Rizzo 46768b8534bSLuigi Rizzo /* 468f0ea3689SLuigi Rizzo * struct nmreq overlays a struct ifreq (just the name) 46968b8534bSLuigi Rizzo */ 47068b8534bSLuigi Rizzo struct nmreq { 47168b8534bSLuigi Rizzo char nr_name[IFNAMSIZ]; 47264ae02c3SLuigi Rizzo uint32_t nr_version; /* API version */ 47368b8534bSLuigi Rizzo uint32_t nr_offset; /* nifp offset in the shared region */ 47468b8534bSLuigi Rizzo uint32_t nr_memsize; /* size of the shared region */ 47564ae02c3SLuigi Rizzo uint32_t nr_tx_slots; /* slots in tx rings */ 47664ae02c3SLuigi Rizzo uint32_t nr_rx_slots; /* slots in rx rings */ 47764ae02c3SLuigi Rizzo uint16_t nr_tx_rings; /* number of tx rings */ 47864ae02c3SLuigi Rizzo uint16_t nr_rx_rings; /* number of rx rings */ 479f0ea3689SLuigi Rizzo 48068b8534bSLuigi Rizzo uint16_t nr_ringid; /* ring(s) we care about */ 481f0ea3689SLuigi Rizzo #define NETMAP_HW_RING 0x4000 /* single NIC ring pair */ 482f0ea3689SLuigi Rizzo #define NETMAP_SW_RING 0x2000 /* only host ring pair */ 483f0ea3689SLuigi Rizzo 484f0ea3689SLuigi Rizzo #define NETMAP_RING_MASK 0x0fff /* the ring number */ 485f0ea3689SLuigi Rizzo 48664ae02c3SLuigi Rizzo #define NETMAP_NO_TX_POLL 0x1000 /* no automatic txsync on poll */ 487f0ea3689SLuigi Rizzo 488f0ea3689SLuigi Rizzo #define NETMAP_DO_RX_POLL 0x8000 /* DO automatic rxsync on poll */ 48917885a7bSLuigi Rizzo 490f18be576SLuigi Rizzo uint16_t nr_cmd; 491f18be576SLuigi Rizzo #define NETMAP_BDG_ATTACH 1 /* attach the NIC */ 492f18be576SLuigi Rizzo #define NETMAP_BDG_DETACH 2 /* detach the NIC */ 4934bf50f18SLuigi Rizzo #define NETMAP_BDG_REGOPS 3 /* register bridge callbacks */ 494f18be576SLuigi Rizzo #define NETMAP_BDG_LIST 4 /* get bridge's info */ 495f0ea3689SLuigi Rizzo #define NETMAP_BDG_VNET_HDR 5 /* set the port virtio-net-hdr length */ 496f0ea3689SLuigi Rizzo #define NETMAP_BDG_OFFSET NETMAP_BDG_VNET_HDR /* deprecated alias */ 4974bf50f18SLuigi Rizzo #define NETMAP_BDG_NEWIF 6 /* create a virtual port */ 4984bf50f18SLuigi Rizzo #define NETMAP_BDG_DELIF 7 /* destroy a virtual port */ 499f0ea3689SLuigi Rizzo uint16_t nr_arg1; /* reserve extra rings in NIOCREGIF */ 500f18be576SLuigi Rizzo #define NETMAP_BDG_HOST 1 /* attach the host stack on ATTACH */ 50117885a7bSLuigi Rizzo 502f18be576SLuigi Rizzo uint16_t nr_arg2; 503f0ea3689SLuigi Rizzo uint32_t nr_arg3; /* req. extra buffers in NIOCREGIF */ 504f0ea3689SLuigi Rizzo uint32_t nr_flags; 505f0ea3689SLuigi Rizzo /* various modes, extends nr_ringid */ 506f0ea3689SLuigi Rizzo uint32_t spare2[1]; 50768b8534bSLuigi Rizzo }; 50868b8534bSLuigi Rizzo 509f0ea3689SLuigi Rizzo #define NR_REG_MASK 0xf /* values for nr_flags */ 510f0ea3689SLuigi Rizzo enum { NR_REG_DEFAULT = 0, /* backward compat, should not be used. */ 511f0ea3689SLuigi Rizzo NR_REG_ALL_NIC = 1, 512f0ea3689SLuigi Rizzo NR_REG_SW = 2, 513f0ea3689SLuigi Rizzo NR_REG_NIC_SW = 3, 514f0ea3689SLuigi Rizzo NR_REG_ONE_NIC = 4, 515f0ea3689SLuigi Rizzo NR_REG_PIPE_MASTER = 5, 516f0ea3689SLuigi Rizzo NR_REG_PIPE_SLAVE = 6, 517f0ea3689SLuigi Rizzo }; 518f0ea3689SLuigi Rizzo /* monitor uses the NR_REG to select the rings to monitor */ 519f0ea3689SLuigi Rizzo #define NR_MONITOR_TX 0x100 520f0ea3689SLuigi Rizzo #define NR_MONITOR_RX 0x200 521*847bf383SLuigi Rizzo #define NR_ZCOPY_MON 0x400 522*847bf383SLuigi Rizzo /* request exclusive access to the selected rings */ 523*847bf383SLuigi Rizzo #define NR_EXCLUSIVE 0x800 524f0ea3689SLuigi Rizzo 52517885a7bSLuigi Rizzo 52664ae02c3SLuigi Rizzo /* 52764ae02c3SLuigi Rizzo * FreeBSD uses the size value embedded in the _IOWR to determine 52864ae02c3SLuigi Rizzo * how much to copy in/out. So we need it to match the actual 52964ae02c3SLuigi Rizzo * data structure we pass. We put some spares in the structure 53064ae02c3SLuigi Rizzo * to ease compatibility with other versions 53164ae02c3SLuigi Rizzo */ 53268b8534bSLuigi Rizzo #define NIOCGINFO _IOWR('i', 145, struct nmreq) /* return IF info */ 53368b8534bSLuigi Rizzo #define NIOCREGIF _IOWR('i', 146, struct nmreq) /* interface register */ 53468b8534bSLuigi Rizzo #define NIOCTXSYNC _IO('i', 148) /* sync tx queues */ 53568b8534bSLuigi Rizzo #define NIOCRXSYNC _IO('i', 149) /* sync rx queues */ 5364bf50f18SLuigi Rizzo #define NIOCCONFIG _IOWR('i',150, struct nm_ifreq) /* for ext. modules */ 53768b8534bSLuigi Rizzo #endif /* !NIOCREGIF */ 53868b8534bSLuigi Rizzo 53917885a7bSLuigi Rizzo 54017885a7bSLuigi Rizzo /* 54117885a7bSLuigi Rizzo * Helper functions for kernel and userspace 54217885a7bSLuigi Rizzo */ 54317885a7bSLuigi Rizzo 54417885a7bSLuigi Rizzo /* 54517885a7bSLuigi Rizzo * check if space is available in the ring. 54617885a7bSLuigi Rizzo */ 54717885a7bSLuigi Rizzo static inline int 54817885a7bSLuigi Rizzo nm_ring_empty(struct netmap_ring *ring) 54917885a7bSLuigi Rizzo { 55017885a7bSLuigi Rizzo return (ring->cur == ring->tail); 55117885a7bSLuigi Rizzo } 55217885a7bSLuigi Rizzo 5534bf50f18SLuigi Rizzo /* 5544bf50f18SLuigi Rizzo * Opaque structure that is passed to an external kernel 5554bf50f18SLuigi Rizzo * module via ioctl(fd, NIOCCONFIG, req) for a user-owned 5564bf50f18SLuigi Rizzo * bridge port (at this point ephemeral VALE interface). 5574bf50f18SLuigi Rizzo */ 5584bf50f18SLuigi Rizzo #define NM_IFRDATA_LEN 256 5594bf50f18SLuigi Rizzo struct nm_ifreq { 5604bf50f18SLuigi Rizzo char nifr_name[IFNAMSIZ]; 5614bf50f18SLuigi Rizzo char data[NM_IFRDATA_LEN]; 5624bf50f18SLuigi Rizzo }; 5634bf50f18SLuigi Rizzo 56468b8534bSLuigi Rizzo #endif /* _NET_NETMAP_H_ */ 565