1fe267a55SPedro F. Giffuni /*- 2fe267a55SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3fe267a55SPedro F. Giffuni * 417885a7bSLuigi Rizzo * Copyright (C) 2011-2014 Matteo Landi, Luigi Rizzo. All rights reserved. 568b8534bSLuigi Rizzo * 668b8534bSLuigi Rizzo * Redistribution and use in source and binary forms, with or without 7f9790aebSLuigi Rizzo * modification, are permitted provided that the following conditions 8f9790aebSLuigi Rizzo * are met: 968b8534bSLuigi Rizzo * 1068b8534bSLuigi Rizzo * 1. Redistributions of source code must retain the above copyright 1168b8534bSLuigi Rizzo * notice, this list of conditions and the following disclaimer. 1268b8534bSLuigi Rizzo * 2. Redistributions in binary form must reproduce the above copyright 1368b8534bSLuigi Rizzo * notice, this list of conditions and the following disclaimer in the 14f9790aebSLuigi Rizzo * documentation and/or other materials provided with the distribution. 1568b8534bSLuigi Rizzo * 16f9790aebSLuigi Rizzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``S IS''AND 1768b8534bSLuigi Rizzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18f9790aebSLuigi Rizzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19f9790aebSLuigi Rizzo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20f9790aebSLuigi Rizzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21f9790aebSLuigi Rizzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22f9790aebSLuigi Rizzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23f9790aebSLuigi Rizzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24f9790aebSLuigi Rizzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25f9790aebSLuigi Rizzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26f9790aebSLuigi Rizzo * SUCH DAMAGE. 2768b8534bSLuigi Rizzo */ 2868b8534bSLuigi Rizzo 2968b8534bSLuigi Rizzo /* 3068b8534bSLuigi Rizzo * $FreeBSD$ 3168b8534bSLuigi Rizzo * 3264ae02c3SLuigi Rizzo * Definitions of constants and the structures used by the netmap 3364ae02c3SLuigi Rizzo * framework, for the part visible to both kernel and userspace. 3464ae02c3SLuigi Rizzo * Detailed info on netmap is available with "man netmap" or at 3564ae02c3SLuigi Rizzo * 3664ae02c3SLuigi Rizzo * http://info.iet.unipi.it/~luigi/netmap/ 37ce3ee1e7SLuigi Rizzo * 38ce3ee1e7SLuigi Rizzo * This API is also used to communicate with the VALE software switch 3968b8534bSLuigi Rizzo */ 4068b8534bSLuigi Rizzo 4168b8534bSLuigi Rizzo #ifndef _NET_NETMAP_H_ 4268b8534bSLuigi Rizzo #define _NET_NETMAP_H_ 4368b8534bSLuigi Rizzo 44d12354a5SVincenzo Maffione #define NETMAP_API 14 /* current API version */ 4517885a7bSLuigi Rizzo 46d12354a5SVincenzo Maffione #define NETMAP_MIN_API 14 /* min and max versions accepted */ 47f0ea3689SLuigi Rizzo #define NETMAP_MAX_API 15 4817885a7bSLuigi Rizzo /* 4917885a7bSLuigi Rizzo * Some fields should be cache-aligned to reduce contention. 5017885a7bSLuigi Rizzo * The alignment is architecture and OS dependent, but rather than 5117885a7bSLuigi Rizzo * digging into OS headers to find the exact value we use an estimate 5217885a7bSLuigi Rizzo * that should cover most architectures. 5317885a7bSLuigi Rizzo */ 5417885a7bSLuigi Rizzo #define NM_CACHE_ALIGN 128 5517885a7bSLuigi Rizzo 5668b8534bSLuigi Rizzo /* 5768b8534bSLuigi Rizzo * --- Netmap data structures --- 5868b8534bSLuigi Rizzo * 59ce3ee1e7SLuigi Rizzo * The userspace data structures used by netmap are shown below. 60ce3ee1e7SLuigi Rizzo * They are allocated by the kernel and mmap()ed by userspace threads. 61ce3ee1e7SLuigi Rizzo * Pointers are implemented as memory offsets or indexes, 62ce3ee1e7SLuigi Rizzo * so that they can be easily dereferenced in kernel and userspace. 6368b8534bSLuigi Rizzo 64ce3ee1e7SLuigi Rizzo KERNEL (opaque, obviously) 6568b8534bSLuigi Rizzo 6664ae02c3SLuigi Rizzo ==================================================================== 6764ae02c3SLuigi Rizzo | 68ce3ee1e7SLuigi Rizzo USERSPACE | struct netmap_ring 6917885a7bSLuigi Rizzo +---->+---------------+ 7017885a7bSLuigi Rizzo / | head,cur,tail | 7117885a7bSLuigi Rizzo struct netmap_if (nifp, 1 per fd) / | buf_ofs | 72d12354a5SVincenzo Maffione +----------------+ / | other fields | 7317885a7bSLuigi Rizzo | ni_tx_rings | / +===============+ 74ce3ee1e7SLuigi Rizzo | ni_rx_rings | / | buf_idx, len | slot[0] 75ce3ee1e7SLuigi Rizzo | | / | flags, ptr | 7617885a7bSLuigi Rizzo | | / +---------------+ 77d12354a5SVincenzo Maffione +================+ / | buf_idx, len | slot[1] 78ce3ee1e7SLuigi Rizzo | txring_ofs[0] | (rel.to nifp)--' | flags, ptr | 7917885a7bSLuigi Rizzo | txring_ofs[1] | +---------------+ 80d12354a5SVincenzo Maffione (tx+htx entries) (num_slots entries) 81ce3ee1e7SLuigi Rizzo | txring_ofs[t] | | buf_idx, len | slot[n-1] 82d12354a5SVincenzo Maffione +----------------+ | flags, ptr | 8317885a7bSLuigi Rizzo | rxring_ofs[0] | +---------------+ 8468b8534bSLuigi Rizzo | rxring_ofs[1] | 85d12354a5SVincenzo Maffione (rx+hrx entries) 86ce3ee1e7SLuigi Rizzo | rxring_ofs[r] | 87d12354a5SVincenzo Maffione +----------------+ 8868b8534bSLuigi Rizzo 89f0ea3689SLuigi Rizzo * For each "interface" (NIC, host stack, PIPE, VALE switch port) bound to 90f0ea3689SLuigi Rizzo * a file descriptor, the mmap()ed region contains a (logically readonly) 91ce3ee1e7SLuigi Rizzo * struct netmap_if pointing to struct netmap_ring's. 92f0ea3689SLuigi Rizzo * 93d12354a5SVincenzo Maffione * There is one netmap_ring per physical NIC ring, plus at least one tx/rx ring 94d12354a5SVincenzo Maffione * pair attached to the host stack (these pairs are unused for non-NIC ports). 95ce3ee1e7SLuigi Rizzo * 96ce3ee1e7SLuigi Rizzo * All physical/host stack ports share the same memory region, 97ce3ee1e7SLuigi Rizzo * so that zero-copy can be implemented between them. 98ce3ee1e7SLuigi Rizzo * VALE switch ports instead have separate memory regions. 99ce3ee1e7SLuigi Rizzo * 100ce3ee1e7SLuigi Rizzo * The netmap_ring is the userspace-visible replica of the NIC ring. 101ce3ee1e7SLuigi Rizzo * Each slot has the index of a buffer (MTU-sized and residing in the 102ce3ee1e7SLuigi Rizzo * mmapped region), its length and some flags. An extra 64-bit pointer 103ce3ee1e7SLuigi Rizzo * is provided for user-supplied buffers in the tx path. 104ce3ee1e7SLuigi Rizzo * 10568b8534bSLuigi Rizzo * In user space, the buffer address is computed as 10664ae02c3SLuigi Rizzo * (char *)ring + buf_ofs + index * NETMAP_BUF_SIZE 107f0ea3689SLuigi Rizzo * 108f0ea3689SLuigi Rizzo * Added in NETMAP_API 11: 109f0ea3689SLuigi Rizzo * 110f0ea3689SLuigi Rizzo * + NIOCREGIF can request the allocation of extra spare buffers from 111f0ea3689SLuigi Rizzo * the same memory pool. The desired number of buffers must be in 112f0ea3689SLuigi Rizzo * nr_arg3. The ioctl may return fewer buffers, depending on memory 113f0ea3689SLuigi Rizzo * availability. nr_arg3 will return the actual value, and, once 114f0ea3689SLuigi Rizzo * mapped, nifp->ni_bufs_head will be the index of the first buffer. 115f0ea3689SLuigi Rizzo * 116f0ea3689SLuigi Rizzo * The buffers are linked to each other using the first uint32_t 117f0ea3689SLuigi Rizzo * as the index. On close, ni_bufs_head must point to the list of 118f0ea3689SLuigi Rizzo * buffers to be released. 119f0ea3689SLuigi Rizzo * 120f0ea3689SLuigi Rizzo * + NIOCREGIF can attach to PIPE rings sharing the same memory 121f0ea3689SLuigi Rizzo * space with a parent device. The ifname indicates the parent device, 122f0ea3689SLuigi Rizzo * which must already exist. Flags in nr_flags indicate if we want to 123f0ea3689SLuigi Rizzo * bind the master or slave side, the index (from nr_ringid) 1245c8c1004SLuigi Rizzo * is just a cookie and does not need to be sequential. 125f0ea3689SLuigi Rizzo * 126f0ea3689SLuigi Rizzo * + NIOCREGIF can also attach to 'monitor' rings that replicate 127f0ea3689SLuigi Rizzo * the content of specific rings, also from the same memory space. 128f0ea3689SLuigi Rizzo * 129f0ea3689SLuigi Rizzo * Extra flags in nr_flags support the above functions. 130f0ea3689SLuigi Rizzo * Application libraries may use the following naming scheme: 131d12354a5SVincenzo Maffione * netmap:foo all NIC rings pairs 132d12354a5SVincenzo Maffione * netmap:foo^ only host rings pairs 133d12354a5SVincenzo Maffione * netmap:foo^k the k-th host rings pair 134d12354a5SVincenzo Maffione * netmap:foo+ all NIC rings + host rings pairs 135d12354a5SVincenzo Maffione * netmap:foo-k the k-th NIC rings pair 136d12354a5SVincenzo Maffione * netmap:foo{k PIPE rings pair k, master side 137d12354a5SVincenzo Maffione * netmap:foo}k PIPE rings pair k, slave side 13837e3a6d3SLuigi Rizzo * 13937e3a6d3SLuigi Rizzo * Some notes about host rings: 14037e3a6d3SLuigi Rizzo * 141d12354a5SVincenzo Maffione * + The RX host rings are used to store those packets that the host network 14237e3a6d3SLuigi Rizzo * stack is trying to transmit through a NIC queue, but only if that queue 14337e3a6d3SLuigi Rizzo * is currently in netmap mode. Netmap will not intercept host stack mbufs 14437e3a6d3SLuigi Rizzo * designated to NIC queues that are not in netmap mode. As a consequence, 14537e3a6d3SLuigi Rizzo * registering a netmap port with netmap:foo^ is not enough to intercept 146d12354a5SVincenzo Maffione * mbufs in the RX host rings; the netmap port should be registered with 14737e3a6d3SLuigi Rizzo * netmap:foo*, or another registration should be done to open at least a 14837e3a6d3SLuigi Rizzo * NIC TX queue in netmap mode. 14937e3a6d3SLuigi Rizzo * 15037e3a6d3SLuigi Rizzo * + Netmap is not currently able to deal with intercepted trasmit mbufs which 15137e3a6d3SLuigi Rizzo * require offloadings like TSO, UFO, checksumming offloadings, etc. It is 15237e3a6d3SLuigi Rizzo * responsibility of the user to disable those offloadings (e.g. using 15337e3a6d3SLuigi Rizzo * ifconfig on FreeBSD or ethtool -K on Linux) for an interface that is being 15437e3a6d3SLuigi Rizzo * used in netmap mode. If the offloadings are not disabled, GSO and/or 15537e3a6d3SLuigi Rizzo * unchecksummed packets may be dropped immediately or end up in the host RX 156d12354a5SVincenzo Maffione * rings, and will be dropped as soon as the packet reaches another netmap 15737e3a6d3SLuigi Rizzo * adapter. 158ce3ee1e7SLuigi Rizzo */ 159ce3ee1e7SLuigi Rizzo 160ce3ee1e7SLuigi Rizzo /* 161ce3ee1e7SLuigi Rizzo * struct netmap_slot is a buffer descriptor 16217885a7bSLuigi Rizzo */ 16317885a7bSLuigi Rizzo struct netmap_slot { 16417885a7bSLuigi Rizzo uint32_t buf_idx; /* buffer index */ 16517885a7bSLuigi Rizzo uint16_t len; /* length for this slot */ 16617885a7bSLuigi Rizzo uint16_t flags; /* buf changed, etc. */ 16717885a7bSLuigi Rizzo uint64_t ptr; /* pointer for indirect buffers */ 16817885a7bSLuigi Rizzo }; 16917885a7bSLuigi Rizzo 17017885a7bSLuigi Rizzo /* 17117885a7bSLuigi Rizzo * The following flags control how the slot is used 17217885a7bSLuigi Rizzo */ 17317885a7bSLuigi Rizzo 17417885a7bSLuigi Rizzo #define NS_BUF_CHANGED 0x0001 /* buf_idx changed */ 17517885a7bSLuigi Rizzo /* 17617885a7bSLuigi Rizzo * must be set whenever buf_idx is changed (as it might be 17717885a7bSLuigi Rizzo * necessary to recompute the physical address and mapping) 178847bf383SLuigi Rizzo * 179847bf383SLuigi Rizzo * It is also set by the kernel whenever the buf_idx is 180847bf383SLuigi Rizzo * changed internally (e.g., by pipes). Applications may 181847bf383SLuigi Rizzo * use this information to know when they can reuse the 182847bf383SLuigi Rizzo * contents of previously prepared buffers. 18317885a7bSLuigi Rizzo */ 18417885a7bSLuigi Rizzo 18517885a7bSLuigi Rizzo #define NS_REPORT 0x0002 /* ask the hardware to report results */ 18617885a7bSLuigi Rizzo /* 18717885a7bSLuigi Rizzo * Request notification when slot is used by the hardware. 18817885a7bSLuigi Rizzo * Normally transmit completions are handled lazily and 18917885a7bSLuigi Rizzo * may be unreported. This flag lets us know when a slot 19017885a7bSLuigi Rizzo * has been sent (e.g. to terminate the sender). 19117885a7bSLuigi Rizzo */ 19217885a7bSLuigi Rizzo 19317885a7bSLuigi Rizzo #define NS_FORWARD 0x0004 /* pass packet 'forward' */ 19417885a7bSLuigi Rizzo /* 19517885a7bSLuigi Rizzo * (Only for physical ports, rx rings with NR_FORWARD set). 19617885a7bSLuigi Rizzo * Slot released to the kernel (i.e. before ring->head) with 19717885a7bSLuigi Rizzo * this flag set are passed to the peer ring (host/NIC), 19817885a7bSLuigi Rizzo * thus restoring the host-NIC connection for these slots. 19917885a7bSLuigi Rizzo * This supports efficient traffic monitoring or firewalling. 20017885a7bSLuigi Rizzo */ 20117885a7bSLuigi Rizzo 20217885a7bSLuigi Rizzo #define NS_NO_LEARN 0x0008 /* disable bridge learning */ 20317885a7bSLuigi Rizzo /* 20417885a7bSLuigi Rizzo * On a VALE switch, do not 'learn' the source port for 20517885a7bSLuigi Rizzo * this buffer. 20617885a7bSLuigi Rizzo */ 20717885a7bSLuigi Rizzo 20817885a7bSLuigi Rizzo #define NS_INDIRECT 0x0010 /* userspace buffer */ 20917885a7bSLuigi Rizzo /* 21017885a7bSLuigi Rizzo * (VALE tx rings only) data is in a userspace buffer, 21117885a7bSLuigi Rizzo * whose address is in the 'ptr' field in the slot. 21217885a7bSLuigi Rizzo */ 21317885a7bSLuigi Rizzo 21417885a7bSLuigi Rizzo #define NS_MOREFRAG 0x0020 /* packet has more fragments */ 21517885a7bSLuigi Rizzo /* 2164f80b14cSVincenzo Maffione * (VALE ports, ptnetmap ports and some NIC ports, e.g. 2174f80b14cSVincenzo Maffione * ixgbe and i40e on Linux) 21817885a7bSLuigi Rizzo * Set on all but the last slot of a multi-segment packet. 21917885a7bSLuigi Rizzo * The 'len' field refers to the individual fragment. 22017885a7bSLuigi Rizzo */ 22117885a7bSLuigi Rizzo 22217885a7bSLuigi Rizzo #define NS_PORT_SHIFT 8 22317885a7bSLuigi Rizzo #define NS_PORT_MASK (0xff << NS_PORT_SHIFT) 22417885a7bSLuigi Rizzo /* 22517885a7bSLuigi Rizzo * The high 8 bits of the flag, if not zero, indicate the 22601c039a1SLuigi Rizzo * destination port for the VALE switch, overriding 22701c039a1SLuigi Rizzo * the lookup table. 22868b8534bSLuigi Rizzo */ 22901c039a1SLuigi Rizzo 23017885a7bSLuigi Rizzo #define NS_RFRAGS(_slot) ( ((_slot)->flags >> 8) & 0xff) 231ce3ee1e7SLuigi Rizzo /* 23217885a7bSLuigi Rizzo * (VALE rx rings only) the high 8 bits 233ce3ee1e7SLuigi Rizzo * are the number of fragments. 234ce3ee1e7SLuigi Rizzo */ 23517885a7bSLuigi Rizzo 2362a7db7a6SVincenzo Maffione #define NETMAP_MAX_FRAGS 64 /* max number of fragments */ 2372a7db7a6SVincenzo Maffione 23868b8534bSLuigi Rizzo 23968b8534bSLuigi Rizzo /* 240ce3ee1e7SLuigi Rizzo * struct netmap_ring 241ce3ee1e7SLuigi Rizzo * 24268b8534bSLuigi Rizzo * Netmap representation of a TX or RX ring (also known as "queue"). 24368b8534bSLuigi Rizzo * This is a queue implemented as a fixed-size circular array. 24417885a7bSLuigi Rizzo * At the software level the important fields are: head, cur, tail. 24568b8534bSLuigi Rizzo * 24668b8534bSLuigi Rizzo * In TX rings: 247ce3ee1e7SLuigi Rizzo * 24817885a7bSLuigi Rizzo * head first slot available for transmission. 24917885a7bSLuigi Rizzo * cur wakeup point. select() and poll() will unblock 25017885a7bSLuigi Rizzo * when 'tail' moves past 'cur' 25117885a7bSLuigi Rizzo * tail (readonly) first slot reserved to the kernel 252ce3ee1e7SLuigi Rizzo * 25317885a7bSLuigi Rizzo * [head .. tail-1] can be used for new packets to send; 25417885a7bSLuigi Rizzo * 'head' and 'cur' must be incremented as slots are filled 25517885a7bSLuigi Rizzo * with new packets to be sent; 25617885a7bSLuigi Rizzo * 'cur' can be moved further ahead if we need more space 2575c8c1004SLuigi Rizzo * for new transmissions. XXX todo (2014-03-12) 25868b8534bSLuigi Rizzo * 25968b8534bSLuigi Rizzo * In RX rings: 260ce3ee1e7SLuigi Rizzo * 26117885a7bSLuigi Rizzo * head first valid received packet 26217885a7bSLuigi Rizzo * cur wakeup point. select() and poll() will unblock 26317885a7bSLuigi Rizzo * when 'tail' moves past 'cur' 26417885a7bSLuigi Rizzo * tail (readonly) first slot reserved to the kernel 265ce3ee1e7SLuigi Rizzo * 26617885a7bSLuigi Rizzo * [head .. tail-1] contain received packets; 26717885a7bSLuigi Rizzo * 'head' and 'cur' must be incremented as slots are consumed 26817885a7bSLuigi Rizzo * and can be returned to the kernel; 26917885a7bSLuigi Rizzo * 'cur' can be moved further ahead if we want to wait for 27017885a7bSLuigi Rizzo * new packets without returning the previous ones. 27168b8534bSLuigi Rizzo * 27268b8534bSLuigi Rizzo * DATA OWNERSHIP/LOCKING: 27317885a7bSLuigi Rizzo * The netmap_ring, and all slots and buffers in the range 27417885a7bSLuigi Rizzo * [head .. tail-1] are owned by the user program; 27517885a7bSLuigi Rizzo * the kernel only accesses them during a netmap system call 27617885a7bSLuigi Rizzo * and in the user thread context. 27701c039a1SLuigi Rizzo * 27817885a7bSLuigi Rizzo * Other slots and buffers are reserved for use by the kernel 27968b8534bSLuigi Rizzo */ 28068b8534bSLuigi Rizzo struct netmap_ring { 28168b8534bSLuigi Rizzo /* 282ce3ee1e7SLuigi Rizzo * buf_ofs is meant to be used through macros. 28368b8534bSLuigi Rizzo * It contains the offset of the buffer region from this 28468b8534bSLuigi Rizzo * descriptor. 28568b8534bSLuigi Rizzo */ 28617885a7bSLuigi Rizzo const int64_t buf_ofs; 28768b8534bSLuigi Rizzo const uint32_t num_slots; /* number of slots in the ring. */ 28817885a7bSLuigi Rizzo const uint32_t nr_buf_size; 28917885a7bSLuigi Rizzo const uint16_t ringid; 29017885a7bSLuigi Rizzo const uint16_t dir; /* 0: tx, 1: rx */ 29168b8534bSLuigi Rizzo 29217885a7bSLuigi Rizzo uint32_t head; /* (u) first user slot */ 29317885a7bSLuigi Rizzo uint32_t cur; /* (u) wakeup point */ 29417885a7bSLuigi Rizzo uint32_t tail; /* (k) first kernel slot */ 29568b8534bSLuigi Rizzo 29617885a7bSLuigi Rizzo uint32_t flags; 29717885a7bSLuigi Rizzo 29817885a7bSLuigi Rizzo struct timeval ts; /* (k) time of last *sync() */ 29917885a7bSLuigi Rizzo 30017885a7bSLuigi Rizzo /* opaque room for a mutex or similar object */ 30137e3a6d3SLuigi Rizzo #if !defined(_WIN32) || defined(__CYGWIN__) 30237e3a6d3SLuigi Rizzo uint8_t __attribute__((__aligned__(NM_CACHE_ALIGN))) sem[128]; 30337e3a6d3SLuigi Rizzo #else 30437e3a6d3SLuigi Rizzo uint8_t __declspec(align(NM_CACHE_ALIGN)) sem[128]; 30537e3a6d3SLuigi Rizzo #endif 30668b8534bSLuigi Rizzo 30768b8534bSLuigi Rizzo /* the slots follow. This struct has variable size */ 30868b8534bSLuigi Rizzo struct netmap_slot slot[0]; /* array of slots. */ 30968b8534bSLuigi Rizzo }; 31068b8534bSLuigi Rizzo 31168b8534bSLuigi Rizzo 31268b8534bSLuigi Rizzo /* 31317885a7bSLuigi Rizzo * RING FLAGS 31417885a7bSLuigi Rizzo */ 31517885a7bSLuigi Rizzo #define NR_TIMESTAMP 0x0002 /* set timestamp on *sync() */ 31617885a7bSLuigi Rizzo /* 31717885a7bSLuigi Rizzo * updates the 'ts' field on each netmap syscall. This saves 31817885a7bSLuigi Rizzo * saves a separate gettimeofday(), and is not much worse than 31917885a7bSLuigi Rizzo * software timestamps generated in the interrupt handler. 32017885a7bSLuigi Rizzo */ 32117885a7bSLuigi Rizzo 32217885a7bSLuigi Rizzo #define NR_FORWARD 0x0004 /* enable NS_FORWARD for ring */ 32317885a7bSLuigi Rizzo /* 32417885a7bSLuigi Rizzo * Enables the NS_FORWARD slot flag for the ring. 32517885a7bSLuigi Rizzo */ 32617885a7bSLuigi Rizzo 3272ff91c17SVincenzo Maffione /* 3282ff91c17SVincenzo Maffione * Helper functions for kernel and userspace 3292ff91c17SVincenzo Maffione */ 3302ff91c17SVincenzo Maffione 3312ff91c17SVincenzo Maffione /* 332b6e66be2SVincenzo Maffione * Check if space is available in the ring. We use ring->head, which 333b6e66be2SVincenzo Maffione * points to the next netmap slot to be published to netmap. It is 334b6e66be2SVincenzo Maffione * possible that the applications moves ring->cur ahead of ring->tail 335b6e66be2SVincenzo Maffione * (e.g., by setting ring->cur <== ring->tail), if it wants more slots 336b6e66be2SVincenzo Maffione * than the ones currently available, and it wants to be notified when 337b6e66be2SVincenzo Maffione * more arrive. See netmap(4) for more details and examples. 3382ff91c17SVincenzo Maffione */ 3392ff91c17SVincenzo Maffione static inline int 3402ff91c17SVincenzo Maffione nm_ring_empty(struct netmap_ring *ring) 3412ff91c17SVincenzo Maffione { 342b6e66be2SVincenzo Maffione return (ring->head == ring->tail); 3432ff91c17SVincenzo Maffione } 34417885a7bSLuigi Rizzo 34517885a7bSLuigi Rizzo /* 34668b8534bSLuigi Rizzo * Netmap representation of an interface and its queue(s). 347ce3ee1e7SLuigi Rizzo * This is initialized by the kernel when binding a file 348ce3ee1e7SLuigi Rizzo * descriptor to a port, and should be considered as readonly 349ce3ee1e7SLuigi Rizzo * by user programs. The kernel never uses it. 350ce3ee1e7SLuigi Rizzo * 35168b8534bSLuigi Rizzo * There is one netmap_if for each file descriptor on which we want 352ce3ee1e7SLuigi Rizzo * to select/poll. 35368b8534bSLuigi Rizzo * select/poll operates on one or all pairs depending on the value of 35468b8534bSLuigi Rizzo * nmr_queueid passed on the ioctl. 35568b8534bSLuigi Rizzo */ 35668b8534bSLuigi Rizzo struct netmap_if { 35768b8534bSLuigi Rizzo char ni_name[IFNAMSIZ]; /* name of the interface. */ 358ce3ee1e7SLuigi Rizzo const uint32_t ni_version; /* API version, currently unused */ 359ce3ee1e7SLuigi Rizzo const uint32_t ni_flags; /* properties */ 360ce3ee1e7SLuigi Rizzo #define NI_PRIV_MEM 0x1 /* private memory region */ 361ce3ee1e7SLuigi Rizzo 36217885a7bSLuigi Rizzo /* 36317885a7bSLuigi Rizzo * The number of packet rings available in netmap mode. 36417885a7bSLuigi Rizzo * Physical NICs can have different numbers of tx and rx rings. 365d12354a5SVincenzo Maffione * Physical NICs also have at least a 'host' rings pair. 36617885a7bSLuigi Rizzo * Additionally, clients can request additional ring pairs to 36717885a7bSLuigi Rizzo * be used for internal communication. 36817885a7bSLuigi Rizzo */ 36917885a7bSLuigi Rizzo const uint32_t ni_tx_rings; /* number of HW tx rings */ 37017885a7bSLuigi Rizzo const uint32_t ni_rx_rings; /* number of HW rx rings */ 37117885a7bSLuigi Rizzo 372f0ea3689SLuigi Rizzo uint32_t ni_bufs_head; /* head index for extra bufs */ 373d12354a5SVincenzo Maffione const uint32_t ni_host_tx_rings; /* number of SW tx rings */ 374d12354a5SVincenzo Maffione const uint32_t ni_host_rx_rings; /* number of SW rx rings */ 375d12354a5SVincenzo Maffione uint32_t ni_spare1[3]; 37668b8534bSLuigi Rizzo /* 37764ae02c3SLuigi Rizzo * The following array contains the offset of each netmap ring 37817885a7bSLuigi Rizzo * from this structure, in the following order: 379d12354a5SVincenzo Maffione * - NIC tx rings (ni_tx_rings); 380d12354a5SVincenzo Maffione * - host tx rings (ni_host_tx_rings); 381d12354a5SVincenzo Maffione * - NIC rx rings (ni_rx_rings); 382d12354a5SVincenzo Maffione * - host rx ring (ni_host_rx_rings); 38317885a7bSLuigi Rizzo * 384d12354a5SVincenzo Maffione * The area is filled up by the kernel on NETMAP_REQ_REGISTER, 38568b8534bSLuigi Rizzo * and then only read by userspace code. 38668b8534bSLuigi Rizzo */ 38768b8534bSLuigi Rizzo const ssize_t ring_ofs[0]; 38868b8534bSLuigi Rizzo }; 38968b8534bSLuigi Rizzo 3902ff91c17SVincenzo Maffione /* Legacy interface to interact with a netmap control device. 3912ff91c17SVincenzo Maffione * Included for backward compatibility. The user should not include this 3922ff91c17SVincenzo Maffione * file directly. */ 3932ff91c17SVincenzo Maffione #include "netmap_legacy.h" 39417885a7bSLuigi Rizzo 39568b8534bSLuigi Rizzo /* 3962ff91c17SVincenzo Maffione * New API to control netmap control devices. New applications should only use 3972ff91c17SVincenzo Maffione * nmreq_xyz structs with the NIOCCTRL ioctl() command. 39868b8534bSLuigi Rizzo * 3992ff91c17SVincenzo Maffione * NIOCCTRL takes a nmreq_header struct, which contains the required 4002ff91c17SVincenzo Maffione * API version, the name of a netmap port, a command type, and pointers 4012ff91c17SVincenzo Maffione * to request body and options. 402f18be576SLuigi Rizzo * 40317885a7bSLuigi Rizzo * nr_name (in) 4042ff91c17SVincenzo Maffione * The name of the port (em0, valeXXX:YYY, eth0{pn1 etc.) 405ce3ee1e7SLuigi Rizzo * 40617885a7bSLuigi Rizzo * nr_version (in/out) 40717885a7bSLuigi Rizzo * Must match NETMAP_API as used in the kernel, error otherwise. 40817885a7bSLuigi Rizzo * Always returns the desired value on output. 40917885a7bSLuigi Rizzo * 4102ff91c17SVincenzo Maffione * nr_reqtype (in) 4112ff91c17SVincenzo Maffione * One of the NETMAP_REQ_* command types below 4122ff91c17SVincenzo Maffione * 4132ff91c17SVincenzo Maffione * nr_body (in) 4142ff91c17SVincenzo Maffione * Pointer to a command-specific struct, described by one 4152ff91c17SVincenzo Maffione * of the struct nmreq_xyz below. 4162ff91c17SVincenzo Maffione * 4172ff91c17SVincenzo Maffione * nr_options (in) 4182ff91c17SVincenzo Maffione * Command specific options, if any. 4192ff91c17SVincenzo Maffione * 4202ff91c17SVincenzo Maffione * A NETMAP_REQ_REGISTER command activates netmap mode on the netmap 4212ff91c17SVincenzo Maffione * port (e.g. physical interface) specified by nmreq_header.nr_name. 4222ff91c17SVincenzo Maffione * The request body (struct nmreq_register) has several arguments to 4232ff91c17SVincenzo Maffione * specify how the port is to be registered. 4242ff91c17SVincenzo Maffione * 425d12354a5SVincenzo Maffione * nr_tx_slots, nr_tx_slots, nr_tx_rings, nr_rx_rings, 426d12354a5SVincenzo Maffione * nr_host_tx_rings, nr_host_rx_rings (in/out) 42717885a7bSLuigi Rizzo * On input, non-zero values may be used to reconfigure the port 42817885a7bSLuigi Rizzo * according to the requested values, but this is not guaranteed. 42917885a7bSLuigi Rizzo * On output the actual values in use are reported. 43017885a7bSLuigi Rizzo * 4312ff91c17SVincenzo Maffione * nr_mode (in) 4322ff91c17SVincenzo Maffione * Indicate what set of rings must be bound to the netmap 4332ff91c17SVincenzo Maffione * device (e.g. all NIC rings, host rings only, NIC and 4342ff91c17SVincenzo Maffione * host rings, ...). Values are in NR_REG_*. 4352ff91c17SVincenzo Maffione * 43617885a7bSLuigi Rizzo * nr_ringid (in) 4372ff91c17SVincenzo Maffione * If nr_mode == NR_REG_ONE_NIC (only a single couple of TX/RX 4382ff91c17SVincenzo Maffione * rings), indicate which NIC TX and/or RX ring is to be bound 4392ff91c17SVincenzo Maffione * (0..nr_*x_rings-1). 440f0ea3689SLuigi Rizzo * 4412ff91c17SVincenzo Maffione * nr_flags (in) 4422ff91c17SVincenzo Maffione * Indicate special options for how to open the port. 443ce3ee1e7SLuigi Rizzo * 4442ff91c17SVincenzo Maffione * NR_NO_TX_POLL can be OR-ed to make select()/poll() push 44517885a7bSLuigi Rizzo * packets on tx rings only if POLLOUT is set. 44617885a7bSLuigi Rizzo * The default is to push any pending packet. 447ce3ee1e7SLuigi Rizzo * 4482ff91c17SVincenzo Maffione * NR_DO_RX_POLL can be OR-ed to make select()/poll() release 449f0ea3689SLuigi Rizzo * packets on rx rings also when POLLIN is NOT set. 450f0ea3689SLuigi Rizzo * The default is to touch the rx ring only with POLLIN. 451f0ea3689SLuigi Rizzo * Note that this is the opposite of TX because it 452f0ea3689SLuigi Rizzo * reflects the common usage. 453f0ea3689SLuigi Rizzo * 4542ff91c17SVincenzo Maffione * Other options are NR_MONITOR_TX, NR_MONITOR_RX, NR_ZCOPY_MON, 4552ff91c17SVincenzo Maffione * NR_EXCLUSIVE, NR_RX_RINGS_ONLY, NR_TX_RINGS_ONLY and 4562ff91c17SVincenzo Maffione * NR_ACCEPT_VNET_HDR. 457f0ea3689SLuigi Rizzo * 4582ff91c17SVincenzo Maffione * nr_mem_id (in/out) 4592ff91c17SVincenzo Maffione * The identity of the memory region used. 460f0ea3689SLuigi Rizzo * On input, 0 means the system decides autonomously, 461f0ea3689SLuigi Rizzo * other values may try to select a specific region. 462f0ea3689SLuigi Rizzo * On return the actual value is reported. 463f0ea3689SLuigi Rizzo * Region '1' is the global allocator, normally shared 464f0ea3689SLuigi Rizzo * by all interfaces. Other values are private regions. 465f0ea3689SLuigi Rizzo * If two ports the same region zero-copy is possible. 466f0ea3689SLuigi Rizzo * 4672ff91c17SVincenzo Maffione * nr_extra_bufs (in/out) 4682ff91c17SVincenzo Maffione * Number of extra buffers to be allocated. 469f0ea3689SLuigi Rizzo * 4702ff91c17SVincenzo Maffione * The other NETMAP_REQ_* commands are described below. 47117885a7bSLuigi Rizzo * 47268b8534bSLuigi Rizzo */ 47368b8534bSLuigi Rizzo 4742ff91c17SVincenzo Maffione /* maximum size of a request, including all options */ 4752ff91c17SVincenzo Maffione #define NETMAP_REQ_MAXSIZE 4096 4762ff91c17SVincenzo Maffione 4772ff91c17SVincenzo Maffione /* Header common to all request options. */ 4782ff91c17SVincenzo Maffione struct nmreq_option { 4792ff91c17SVincenzo Maffione /* Pointer ot the next option. */ 4802ff91c17SVincenzo Maffione uint64_t nro_next; 4812ff91c17SVincenzo Maffione /* Option type. */ 4822ff91c17SVincenzo Maffione uint32_t nro_reqtype; 4832ff91c17SVincenzo Maffione /* (out) status of the option: 4842ff91c17SVincenzo Maffione * 0: recognized and processed 4852ff91c17SVincenzo Maffione * !=0: errno value 4862ff91c17SVincenzo Maffione */ 4872ff91c17SVincenzo Maffione uint32_t nro_status; 488b6e66be2SVincenzo Maffione /* Option size, used only for options that can have variable size 489b6e66be2SVincenzo Maffione * (e.g. because they contain arrays). For fixed-size options this 490b6e66be2SVincenzo Maffione * field should be set to zero. */ 491b6e66be2SVincenzo Maffione uint64_t nro_size; 4922ff91c17SVincenzo Maffione }; 4932ff91c17SVincenzo Maffione 4942ff91c17SVincenzo Maffione /* Header common to all requests. Do not reorder these fields, as we need 4952ff91c17SVincenzo Maffione * the second one (nr_reqtype) to know how much to copy from/to userspace. */ 4962ff91c17SVincenzo Maffione struct nmreq_header { 4972ff91c17SVincenzo Maffione uint16_t nr_version; /* API version */ 4982ff91c17SVincenzo Maffione uint16_t nr_reqtype; /* nmreq type (NETMAP_REQ_*) */ 4992ff91c17SVincenzo Maffione uint32_t nr_reserved; /* must be zero */ 5002ff91c17SVincenzo Maffione #define NETMAP_REQ_IFNAMSIZ 64 5012ff91c17SVincenzo Maffione char nr_name[NETMAP_REQ_IFNAMSIZ]; /* port name */ 5022ff91c17SVincenzo Maffione uint64_t nr_options; /* command-specific options */ 5032ff91c17SVincenzo Maffione uint64_t nr_body; /* ptr to nmreq_xyz struct */ 5042ff91c17SVincenzo Maffione }; 5052ff91c17SVincenzo Maffione 5062ff91c17SVincenzo Maffione enum { 5072ff91c17SVincenzo Maffione /* Register a netmap port with the device. */ 5082ff91c17SVincenzo Maffione NETMAP_REQ_REGISTER = 1, 5092ff91c17SVincenzo Maffione /* Get information from a netmap port. */ 5102ff91c17SVincenzo Maffione NETMAP_REQ_PORT_INFO_GET, 5112ff91c17SVincenzo Maffione /* Attach a netmap port to a VALE switch. */ 5122ff91c17SVincenzo Maffione NETMAP_REQ_VALE_ATTACH, 5132ff91c17SVincenzo Maffione /* Detach a netmap port from a VALE switch. */ 5142ff91c17SVincenzo Maffione NETMAP_REQ_VALE_DETACH, 5152ff91c17SVincenzo Maffione /* List the ports attached to a VALE switch. */ 5162ff91c17SVincenzo Maffione NETMAP_REQ_VALE_LIST, 5172ff91c17SVincenzo Maffione /* Set the port header length (was virtio-net header length). */ 5182ff91c17SVincenzo Maffione NETMAP_REQ_PORT_HDR_SET, 5192ff91c17SVincenzo Maffione /* Get the port header length (was virtio-net header length). */ 5202ff91c17SVincenzo Maffione NETMAP_REQ_PORT_HDR_GET, 5212ff91c17SVincenzo Maffione /* Create a new persistent VALE port. */ 5222ff91c17SVincenzo Maffione NETMAP_REQ_VALE_NEWIF, 5232ff91c17SVincenzo Maffione /* Delete a persistent VALE port. */ 5242ff91c17SVincenzo Maffione NETMAP_REQ_VALE_DELIF, 5252ff91c17SVincenzo Maffione /* Enable polling kernel thread(s) on an attached VALE port. */ 5262ff91c17SVincenzo Maffione NETMAP_REQ_VALE_POLLING_ENABLE, 5272ff91c17SVincenzo Maffione /* Disable polling kernel thread(s) on an attached VALE port. */ 5282ff91c17SVincenzo Maffione NETMAP_REQ_VALE_POLLING_DISABLE, 5292ff91c17SVincenzo Maffione /* Get info about the pools of a memory allocator. */ 5302ff91c17SVincenzo Maffione NETMAP_REQ_POOLS_INFO_GET, 531b6e66be2SVincenzo Maffione /* Start an in-kernel loop that syncs the rings periodically or 532b6e66be2SVincenzo Maffione * on notifications. The loop runs in the context of the ioctl 533b6e66be2SVincenzo Maffione * syscall, and only stops on NETMAP_REQ_SYNC_KLOOP_STOP. */ 534b6e66be2SVincenzo Maffione NETMAP_REQ_SYNC_KLOOP_START, 535b6e66be2SVincenzo Maffione /* Stops the thread executing the in-kernel loop. The thread 536b6e66be2SVincenzo Maffione * returns from the ioctl syscall. */ 537b6e66be2SVincenzo Maffione NETMAP_REQ_SYNC_KLOOP_STOP, 538b6e66be2SVincenzo Maffione /* Enable CSB mode on a registered netmap control device. */ 539b6e66be2SVincenzo Maffione NETMAP_REQ_CSB_ENABLE, 5402ff91c17SVincenzo Maffione }; 5412ff91c17SVincenzo Maffione 5422ff91c17SVincenzo Maffione enum { 5432ff91c17SVincenzo Maffione /* On NETMAP_REQ_REGISTER, ask netmap to use memory allocated 5445faab778SVincenzo Maffione * from user-space allocated memory pools (e.g. hugepages). 5455faab778SVincenzo Maffione */ 5462ff91c17SVincenzo Maffione NETMAP_REQ_OPT_EXTMEM = 1, 547b6e66be2SVincenzo Maffione 548b6e66be2SVincenzo Maffione /* ON NETMAP_REQ_SYNC_KLOOP_START, ask netmap to use eventfd-based 549b6e66be2SVincenzo Maffione * notifications to synchronize the kernel loop with the application. 550b6e66be2SVincenzo Maffione */ 551b6e66be2SVincenzo Maffione NETMAP_REQ_OPT_SYNC_KLOOP_EVENTFDS, 552b6e66be2SVincenzo Maffione 553b6e66be2SVincenzo Maffione /* On NETMAP_REQ_REGISTER, ask netmap to work in CSB mode, where 554b6e66be2SVincenzo Maffione * head, cur and tail pointers are not exchanged through the 555b6e66be2SVincenzo Maffione * struct netmap_ring header, but rather using an user-provided 5565faab778SVincenzo Maffione * memory area (see struct nm_csb_atok and struct nm_csb_ktoa). 5575faab778SVincenzo Maffione */ 558b6e66be2SVincenzo Maffione NETMAP_REQ_OPT_CSB, 5595faab778SVincenzo Maffione 5605faab778SVincenzo Maffione /* An extension to NETMAP_REQ_OPT_SYNC_KLOOP_EVENTFDS, which specifies 5615faab778SVincenzo Maffione * if the TX and/or RX rings are synced in the context of the VM exit. 5625faab778SVincenzo Maffione * This requires the 'ioeventfd' fields to be valid (cannot be < 0). 5635faab778SVincenzo Maffione */ 5645faab778SVincenzo Maffione NETMAP_REQ_OPT_SYNC_KLOOP_MODE, 565*253b2ec1SVincenzo Maffione 566*253b2ec1SVincenzo Maffione /* This is a marker to count the number of available options. 567*253b2ec1SVincenzo Maffione * New options must be added above it. */ 568*253b2ec1SVincenzo Maffione NETMAP_REQ_OPT_MAX, 5692ff91c17SVincenzo Maffione }; 57017885a7bSLuigi Rizzo 57168b8534bSLuigi Rizzo /* 5722ff91c17SVincenzo Maffione * nr_reqtype: NETMAP_REQ_REGISTER 5732ff91c17SVincenzo Maffione * Bind (register) a netmap port to this control device. 57468b8534bSLuigi Rizzo */ 5752ff91c17SVincenzo Maffione struct nmreq_register { 5762ff91c17SVincenzo Maffione uint64_t nr_offset; /* nifp offset in the shared region */ 5772ff91c17SVincenzo Maffione uint64_t nr_memsize; /* size of the shared region */ 57864ae02c3SLuigi Rizzo uint32_t nr_tx_slots; /* slots in tx rings */ 57964ae02c3SLuigi Rizzo uint32_t nr_rx_slots; /* slots in rx rings */ 58064ae02c3SLuigi Rizzo uint16_t nr_tx_rings; /* number of tx rings */ 58164ae02c3SLuigi Rizzo uint16_t nr_rx_rings; /* number of rx rings */ 582d12354a5SVincenzo Maffione uint16_t nr_host_tx_rings; /* number of host tx rings */ 583d12354a5SVincenzo Maffione uint16_t nr_host_rx_rings; /* number of host rx rings */ 584f0ea3689SLuigi Rizzo 5852ff91c17SVincenzo Maffione uint16_t nr_mem_id; /* id of the memory allocator */ 58668b8534bSLuigi Rizzo uint16_t nr_ringid; /* ring(s) we care about */ 5872ff91c17SVincenzo Maffione uint32_t nr_mode; /* specify NR_REG_* modes */ 588b6e66be2SVincenzo Maffione uint32_t nr_extra_bufs; /* number of requested extra buffers */ 589f0ea3689SLuigi Rizzo 5902ff91c17SVincenzo Maffione uint64_t nr_flags; /* additional flags (see below) */ 5912ff91c17SVincenzo Maffione /* monitors use nr_ringid and nr_mode to select the rings to monitor */ 592f0ea3689SLuigi Rizzo #define NR_MONITOR_TX 0x100 593f0ea3689SLuigi Rizzo #define NR_MONITOR_RX 0x200 594847bf383SLuigi Rizzo #define NR_ZCOPY_MON 0x400 595847bf383SLuigi Rizzo /* request exclusive access to the selected rings */ 596847bf383SLuigi Rizzo #define NR_EXCLUSIVE 0x800 597b6e66be2SVincenzo Maffione /* 0x1000 unused */ 59837e3a6d3SLuigi Rizzo #define NR_RX_RINGS_ONLY 0x2000 59937e3a6d3SLuigi Rizzo #define NR_TX_RINGS_ONLY 0x4000 60037e3a6d3SLuigi Rizzo /* Applications set this flag if they are able to deal with virtio-net headers, 60137e3a6d3SLuigi Rizzo * that is send/receive frames that start with a virtio-net header. 602d12354a5SVincenzo Maffione * If not set, NETMAP_REQ_REGISTER will fail with netmap ports that require 603d12354a5SVincenzo Maffione * applications to use those headers. If the flag is set, the application can 604d12354a5SVincenzo Maffione * use the NETMAP_VNET_HDR_GET command to figure out the header length. */ 60537e3a6d3SLuigi Rizzo #define NR_ACCEPT_VNET_HDR 0x8000 6062ff91c17SVincenzo Maffione /* The following two have the same meaning of NETMAP_NO_TX_POLL and 6072ff91c17SVincenzo Maffione * NETMAP_DO_RX_POLL. */ 6082ff91c17SVincenzo Maffione #define NR_DO_RX_POLL 0x10000 6092ff91c17SVincenzo Maffione #define NR_NO_TX_POLL 0x20000 6102ff91c17SVincenzo Maffione }; 6112ff91c17SVincenzo Maffione 6122ff91c17SVincenzo Maffione /* Valid values for nmreq_register.nr_mode (see above). */ 6132ff91c17SVincenzo Maffione enum { NR_REG_DEFAULT = 0, /* backward compat, should not be used. */ 6142ff91c17SVincenzo Maffione NR_REG_ALL_NIC = 1, 6152ff91c17SVincenzo Maffione NR_REG_SW = 2, 6162ff91c17SVincenzo Maffione NR_REG_NIC_SW = 3, 6172ff91c17SVincenzo Maffione NR_REG_ONE_NIC = 4, 6182ff91c17SVincenzo Maffione NR_REG_PIPE_MASTER = 5, /* deprecated, use "x{y" port name syntax */ 6192ff91c17SVincenzo Maffione NR_REG_PIPE_SLAVE = 6, /* deprecated, use "x}y" port name syntax */ 620b6e66be2SVincenzo Maffione NR_REG_NULL = 7, 621d12354a5SVincenzo Maffione NR_REG_ONE_SW = 8, 6222ff91c17SVincenzo Maffione }; 6232ff91c17SVincenzo Maffione 6242ff91c17SVincenzo Maffione /* A single ioctl number is shared by all the new API command. 625b6e66be2SVincenzo Maffione * Demultiplexing is done using the hdr.nr_reqtype field. 6262ff91c17SVincenzo Maffione * FreeBSD uses the size value embedded in the _IOWR to determine 6272ff91c17SVincenzo Maffione * how much to copy in/out, so we define the ioctl() command 6282ff91c17SVincenzo Maffione * specifying only nmreq_header, and copyin/copyout the rest. */ 6292ff91c17SVincenzo Maffione #define NIOCCTRL _IOWR('i', 151, struct nmreq_header) 6302ff91c17SVincenzo Maffione 6312ff91c17SVincenzo Maffione /* The ioctl commands to sync TX/RX netmap rings. 6322ff91c17SVincenzo Maffione * NIOCTXSYNC, NIOCRXSYNC synchronize tx or rx queues, 633d12354a5SVincenzo Maffione * whose identity is set in NETMAP_REQ_REGISTER through nr_ringid. 6342ff91c17SVincenzo Maffione * These are non blocking and take no argument. */ 6352ff91c17SVincenzo Maffione #define NIOCTXSYNC _IO('i', 148) /* sync tx queues */ 6362ff91c17SVincenzo Maffione #define NIOCRXSYNC _IO('i', 149) /* sync rx queues */ 6372ff91c17SVincenzo Maffione 6382ff91c17SVincenzo Maffione /* 6392ff91c17SVincenzo Maffione * nr_reqtype: NETMAP_REQ_PORT_INFO_GET 6402ff91c17SVincenzo Maffione * Get information about a netmap port, including number of rings. 641b6e66be2SVincenzo Maffione * slots per ring, id of the memory allocator, etc. The netmap 642b6e66be2SVincenzo Maffione * control device used for this operation does not need to be bound 643b6e66be2SVincenzo Maffione * to a netmap port. 6442ff91c17SVincenzo Maffione */ 6452ff91c17SVincenzo Maffione struct nmreq_port_info_get { 6462ff91c17SVincenzo Maffione uint64_t nr_memsize; /* size of the shared region */ 6472ff91c17SVincenzo Maffione uint32_t nr_tx_slots; /* slots in tx rings */ 6482ff91c17SVincenzo Maffione uint32_t nr_rx_slots; /* slots in rx rings */ 6492ff91c17SVincenzo Maffione uint16_t nr_tx_rings; /* number of tx rings */ 6502ff91c17SVincenzo Maffione uint16_t nr_rx_rings; /* number of rx rings */ 651d12354a5SVincenzo Maffione uint16_t nr_host_tx_rings; /* number of host tx rings */ 652d12354a5SVincenzo Maffione uint16_t nr_host_rx_rings; /* number of host rx rings */ 653b6e66be2SVincenzo Maffione uint16_t nr_mem_id; /* memory allocator id (in/out) */ 654d12354a5SVincenzo Maffione uint16_t pad[3]; 6552ff91c17SVincenzo Maffione }; 656f0ea3689SLuigi Rizzo 65737e3a6d3SLuigi Rizzo #define NM_BDG_NAME "vale" /* prefix for bridge port name */ 65837e3a6d3SLuigi Rizzo 65937e3a6d3SLuigi Rizzo /* 6602ff91c17SVincenzo Maffione * nr_reqtype: NETMAP_REQ_VALE_ATTACH 6612ff91c17SVincenzo Maffione * Attach a netmap port to a VALE switch. Both the name of the netmap 6622ff91c17SVincenzo Maffione * port and the VALE switch are specified through the nr_name argument. 6632ff91c17SVincenzo Maffione * The attach operation could need to register a port, so at least 6642ff91c17SVincenzo Maffione * the same arguments are available. 6652ff91c17SVincenzo Maffione * port_index will contain the index where the port has been attached. 66637e3a6d3SLuigi Rizzo */ 6672ff91c17SVincenzo Maffione struct nmreq_vale_attach { 6682ff91c17SVincenzo Maffione struct nmreq_register reg; 6692ff91c17SVincenzo Maffione uint32_t port_index; 670b6e66be2SVincenzo Maffione uint32_t pad1; 6712ff91c17SVincenzo Maffione }; 67217885a7bSLuigi Rizzo 67364ae02c3SLuigi Rizzo /* 6742ff91c17SVincenzo Maffione * nr_reqtype: NETMAP_REQ_VALE_DETACH 6752ff91c17SVincenzo Maffione * Detach a netmap port from a VALE switch. Both the name of the netmap 6762ff91c17SVincenzo Maffione * port and the VALE switch are specified through the nr_name argument. 6772ff91c17SVincenzo Maffione * port_index will contain the index where the port was attached. 67864ae02c3SLuigi Rizzo */ 6792ff91c17SVincenzo Maffione struct nmreq_vale_detach { 6802ff91c17SVincenzo Maffione uint32_t port_index; 681b6e66be2SVincenzo Maffione uint32_t pad1; 6822ff91c17SVincenzo Maffione }; 68317885a7bSLuigi Rizzo 68417885a7bSLuigi Rizzo /* 6852ff91c17SVincenzo Maffione * nr_reqtype: NETMAP_REQ_VALE_LIST 6862ff91c17SVincenzo Maffione * List the ports of a VALE switch. 68717885a7bSLuigi Rizzo */ 6882ff91c17SVincenzo Maffione struct nmreq_vale_list { 6892ff91c17SVincenzo Maffione /* Name of the VALE port (valeXXX:YYY) or empty. */ 6902ff91c17SVincenzo Maffione uint16_t nr_bridge_idx; 691b6e66be2SVincenzo Maffione uint16_t pad1; 6922ff91c17SVincenzo Maffione uint32_t nr_port_idx; 6932ff91c17SVincenzo Maffione }; 69417885a7bSLuigi Rizzo 69517885a7bSLuigi Rizzo /* 6962ff91c17SVincenzo Maffione * nr_reqtype: NETMAP_REQ_PORT_HDR_SET or NETMAP_REQ_PORT_HDR_GET 697b6e66be2SVincenzo Maffione * Set or get the port header length of the port identified by hdr.nr_name. 698b6e66be2SVincenzo Maffione * The control device does not need to be bound to a netmap port. 69917885a7bSLuigi Rizzo */ 7002ff91c17SVincenzo Maffione struct nmreq_port_hdr { 7012ff91c17SVincenzo Maffione uint32_t nr_hdr_len; 702b6e66be2SVincenzo Maffione uint32_t pad1; 7032ff91c17SVincenzo Maffione }; 70417885a7bSLuigi Rizzo 7054bf50f18SLuigi Rizzo /* 7062ff91c17SVincenzo Maffione * nr_reqtype: NETMAP_REQ_VALE_NEWIF 7072ff91c17SVincenzo Maffione * Create a new persistent VALE port. 7084bf50f18SLuigi Rizzo */ 7092ff91c17SVincenzo Maffione struct nmreq_vale_newif { 7102ff91c17SVincenzo Maffione uint32_t nr_tx_slots; /* slots in tx rings */ 7112ff91c17SVincenzo Maffione uint32_t nr_rx_slots; /* slots in rx rings */ 7122ff91c17SVincenzo Maffione uint16_t nr_tx_rings; /* number of tx rings */ 7132ff91c17SVincenzo Maffione uint16_t nr_rx_rings; /* number of rx rings */ 7142ff91c17SVincenzo Maffione uint16_t nr_mem_id; /* id of the memory allocator */ 715b6e66be2SVincenzo Maffione uint16_t pad1; 7162ff91c17SVincenzo Maffione }; 7172ff91c17SVincenzo Maffione 7182ff91c17SVincenzo Maffione /* 7192ff91c17SVincenzo Maffione * nr_reqtype: NETMAP_REQ_VALE_POLLING_ENABLE or NETMAP_REQ_VALE_POLLING_DISABLE 7202ff91c17SVincenzo Maffione * Enable or disable polling kthreads on a VALE port. 7212ff91c17SVincenzo Maffione */ 7222ff91c17SVincenzo Maffione struct nmreq_vale_polling { 7232ff91c17SVincenzo Maffione uint32_t nr_mode; 7242ff91c17SVincenzo Maffione #define NETMAP_POLLING_MODE_SINGLE_CPU 1 7252ff91c17SVincenzo Maffione #define NETMAP_POLLING_MODE_MULTI_CPU 2 7262ff91c17SVincenzo Maffione uint32_t nr_first_cpu_id; 7272ff91c17SVincenzo Maffione uint32_t nr_num_polling_cpus; 728b6e66be2SVincenzo Maffione uint32_t pad1; 7292ff91c17SVincenzo Maffione }; 7302ff91c17SVincenzo Maffione 7312ff91c17SVincenzo Maffione /* 7322ff91c17SVincenzo Maffione * nr_reqtype: NETMAP_REQ_POOLS_INFO_GET 733b6e66be2SVincenzo Maffione * Get info about the pools of the memory allocator of the netmap 734b6e66be2SVincenzo Maffione * port specified by hdr.nr_name and nr_mem_id. The netmap control 735b6e66be2SVincenzo Maffione * device used for this operation does not need to be bound to a netmap 736b6e66be2SVincenzo Maffione * port. 7372ff91c17SVincenzo Maffione */ 7382ff91c17SVincenzo Maffione struct nmreq_pools_info { 7392ff91c17SVincenzo Maffione uint64_t nr_memsize; 740b6e66be2SVincenzo Maffione uint16_t nr_mem_id; /* in/out argument */ 741b6e66be2SVincenzo Maffione uint16_t pad1[3]; 7422ff91c17SVincenzo Maffione uint64_t nr_if_pool_offset; 7432ff91c17SVincenzo Maffione uint32_t nr_if_pool_objtotal; 7442ff91c17SVincenzo Maffione uint32_t nr_if_pool_objsize; 7452ff91c17SVincenzo Maffione uint64_t nr_ring_pool_offset; 7462ff91c17SVincenzo Maffione uint32_t nr_ring_pool_objtotal; 7472ff91c17SVincenzo Maffione uint32_t nr_ring_pool_objsize; 7482ff91c17SVincenzo Maffione uint64_t nr_buf_pool_offset; 7492ff91c17SVincenzo Maffione uint32_t nr_buf_pool_objtotal; 7502ff91c17SVincenzo Maffione uint32_t nr_buf_pool_objsize; 7512ff91c17SVincenzo Maffione }; 7522ff91c17SVincenzo Maffione 7532ff91c17SVincenzo Maffione /* 754b6e66be2SVincenzo Maffione * nr_reqtype: NETMAP_REQ_SYNC_KLOOP_START 755b6e66be2SVincenzo Maffione * Start an in-kernel loop that syncs the rings periodically or on 756b6e66be2SVincenzo Maffione * notifications. The loop runs in the context of the ioctl syscall, 757b6e66be2SVincenzo Maffione * and only stops on NETMAP_REQ_SYNC_KLOOP_STOP. 758b6e66be2SVincenzo Maffione * The registered netmap port must be open in CSB mode. 759b6e66be2SVincenzo Maffione */ 760b6e66be2SVincenzo Maffione struct nmreq_sync_kloop_start { 761b6e66be2SVincenzo Maffione /* Sleeping is the default synchronization method for the kloop. 762b6e66be2SVincenzo Maffione * The 'sleep_us' field specifies how many microsconds to sleep for 763b6e66be2SVincenzo Maffione * when there is no work to do, before doing another kloop iteration. 764b6e66be2SVincenzo Maffione */ 765b6e66be2SVincenzo Maffione uint32_t sleep_us; 766b6e66be2SVincenzo Maffione uint32_t pad1; 767b6e66be2SVincenzo Maffione }; 768b6e66be2SVincenzo Maffione 769b6e66be2SVincenzo Maffione /* A CSB entry for the application --> kernel direction. */ 770b6e66be2SVincenzo Maffione struct nm_csb_atok { 771b6e66be2SVincenzo Maffione uint32_t head; /* AW+ KR+ the head of the appl netmap_ring */ 772b6e66be2SVincenzo Maffione uint32_t cur; /* AW+ KR+ the cur of the appl netmap_ring */ 773b6e66be2SVincenzo Maffione uint32_t appl_need_kick; /* AW+ KR+ kern --> appl notification enable */ 774b6e66be2SVincenzo Maffione uint32_t sync_flags; /* AW+ KR+ the flags of the appl [tx|rx]sync() */ 775b6e66be2SVincenzo Maffione uint32_t pad[12]; /* pad to a 64 bytes cacheline */ 776b6e66be2SVincenzo Maffione }; 777b6e66be2SVincenzo Maffione 778b6e66be2SVincenzo Maffione /* A CSB entry for the application <-- kernel direction. */ 779b6e66be2SVincenzo Maffione struct nm_csb_ktoa { 780b6e66be2SVincenzo Maffione uint32_t hwcur; /* AR+ KW+ the hwcur of the kern netmap_kring */ 781b6e66be2SVincenzo Maffione uint32_t hwtail; /* AR+ KW+ the hwtail of the kern netmap_kring */ 782b6e66be2SVincenzo Maffione uint32_t kern_need_kick; /* AR+ KW+ appl-->kern notification enable */ 783b6e66be2SVincenzo Maffione uint32_t pad[13]; 784b6e66be2SVincenzo Maffione }; 785b6e66be2SVincenzo Maffione 786b6e66be2SVincenzo Maffione #ifdef __linux__ 787b6e66be2SVincenzo Maffione 788b6e66be2SVincenzo Maffione #ifdef __KERNEL__ 789b6e66be2SVincenzo Maffione #define nm_stst_barrier smp_wmb 790f79ba6d7SVincenzo Maffione #define nm_ldld_barrier smp_rmb 791f79ba6d7SVincenzo Maffione #define nm_stld_barrier smp_mb 792b6e66be2SVincenzo Maffione #else /* !__KERNEL__ */ 793b6e66be2SVincenzo Maffione static inline void nm_stst_barrier(void) 794b6e66be2SVincenzo Maffione { 795b6e66be2SVincenzo Maffione /* A memory barrier with release semantic has the combined 796b6e66be2SVincenzo Maffione * effect of a store-store barrier and a load-store barrier, 797b6e66be2SVincenzo Maffione * which is fine for us. */ 798b6e66be2SVincenzo Maffione __atomic_thread_fence(__ATOMIC_RELEASE); 799b6e66be2SVincenzo Maffione } 800f79ba6d7SVincenzo Maffione static inline void nm_ldld_barrier(void) 801f79ba6d7SVincenzo Maffione { 802f79ba6d7SVincenzo Maffione /* A memory barrier with acquire semantic has the combined 803f79ba6d7SVincenzo Maffione * effect of a load-load barrier and a store-load barrier, 804f79ba6d7SVincenzo Maffione * which is fine for us. */ 805f79ba6d7SVincenzo Maffione __atomic_thread_fence(__ATOMIC_ACQUIRE); 806f79ba6d7SVincenzo Maffione } 807b6e66be2SVincenzo Maffione #endif /* !__KERNEL__ */ 808b6e66be2SVincenzo Maffione 809b6e66be2SVincenzo Maffione #elif defined(__FreeBSD__) 810b6e66be2SVincenzo Maffione 811b6e66be2SVincenzo Maffione #ifdef _KERNEL 812b6e66be2SVincenzo Maffione #define nm_stst_barrier atomic_thread_fence_rel 813f79ba6d7SVincenzo Maffione #define nm_ldld_barrier atomic_thread_fence_acq 814f79ba6d7SVincenzo Maffione #define nm_stld_barrier atomic_thread_fence_seq_cst 815b6e66be2SVincenzo Maffione #else /* !_KERNEL */ 8161d9ec3eeSVincenzo Maffione #include <stdatomic.h> 817b6e66be2SVincenzo Maffione static inline void nm_stst_barrier(void) 818b6e66be2SVincenzo Maffione { 8191d9ec3eeSVincenzo Maffione atomic_thread_fence(memory_order_release); 820b6e66be2SVincenzo Maffione } 821f79ba6d7SVincenzo Maffione static inline void nm_ldld_barrier(void) 822f79ba6d7SVincenzo Maffione { 823f79ba6d7SVincenzo Maffione atomic_thread_fence(memory_order_acquire); 824f79ba6d7SVincenzo Maffione } 825b6e66be2SVincenzo Maffione #endif /* !_KERNEL */ 826b6e66be2SVincenzo Maffione 827b6e66be2SVincenzo Maffione #else /* !__linux__ && !__FreeBSD__ */ 828b6e66be2SVincenzo Maffione #error "OS not supported" 829b6e66be2SVincenzo Maffione #endif /* !__linux__ && !__FreeBSD__ */ 830b6e66be2SVincenzo Maffione 831b6e66be2SVincenzo Maffione /* Application side of sync-kloop: Write ring pointers (cur, head) to the CSB. 832b6e66be2SVincenzo Maffione * This routine is coupled with sync_kloop_kernel_read(). */ 833b6e66be2SVincenzo Maffione static inline void 834b6e66be2SVincenzo Maffione nm_sync_kloop_appl_write(struct nm_csb_atok *atok, uint32_t cur, 835b6e66be2SVincenzo Maffione uint32_t head) 836b6e66be2SVincenzo Maffione { 837f79ba6d7SVincenzo Maffione /* Issue a first store-store barrier to make sure writes to the 838f79ba6d7SVincenzo Maffione * netmap ring do not overcome updates on atok->cur and atok->head. */ 839f79ba6d7SVincenzo Maffione nm_stst_barrier(); 840f79ba6d7SVincenzo Maffione 841b6e66be2SVincenzo Maffione /* 842b6e66be2SVincenzo Maffione * We need to write cur and head to the CSB but we cannot do it atomically. 843b6e66be2SVincenzo Maffione * There is no way we can prevent the host from reading the updated value 844b6e66be2SVincenzo Maffione * of one of the two and the old value of the other. However, if we make 845b6e66be2SVincenzo Maffione * sure that the host never reads a value of head more recent than the 846b6e66be2SVincenzo Maffione * value of cur we are safe. We can allow the host to read a value of cur 847b6e66be2SVincenzo Maffione * more recent than the value of head, since in the netmap ring cur can be 848b6e66be2SVincenzo Maffione * ahead of head and cur cannot wrap around head because it must be behind 849b6e66be2SVincenzo Maffione * tail. Inverting the order of writes below could instead result into the 850b6e66be2SVincenzo Maffione * host to think head went ahead of cur, which would cause the sync 851b6e66be2SVincenzo Maffione * prologue to fail. 852b6e66be2SVincenzo Maffione * 853b6e66be2SVincenzo Maffione * The following memory barrier scheme is used to make this happen: 854b6e66be2SVincenzo Maffione * 855b6e66be2SVincenzo Maffione * Guest Host 856b6e66be2SVincenzo Maffione * 857b6e66be2SVincenzo Maffione * STORE(cur) LOAD(head) 858f79ba6d7SVincenzo Maffione * wmb() <-----------> rmb() 859b6e66be2SVincenzo Maffione * STORE(head) LOAD(cur) 860b6e66be2SVincenzo Maffione * 861b6e66be2SVincenzo Maffione */ 862b6e66be2SVincenzo Maffione atok->cur = cur; 863b6e66be2SVincenzo Maffione nm_stst_barrier(); 864b6e66be2SVincenzo Maffione atok->head = head; 865b6e66be2SVincenzo Maffione } 866b6e66be2SVincenzo Maffione 867b6e66be2SVincenzo Maffione /* Application side of sync-kloop: Read kring pointers (hwcur, hwtail) from 868b6e66be2SVincenzo Maffione * the CSB. This routine is coupled with sync_kloop_kernel_write(). */ 869b6e66be2SVincenzo Maffione static inline void 870b6e66be2SVincenzo Maffione nm_sync_kloop_appl_read(struct nm_csb_ktoa *ktoa, uint32_t *hwtail, 871b6e66be2SVincenzo Maffione uint32_t *hwcur) 872b6e66be2SVincenzo Maffione { 873b6e66be2SVincenzo Maffione /* 874b6e66be2SVincenzo Maffione * We place a memory barrier to make sure that the update of hwtail never 875b6e66be2SVincenzo Maffione * overtakes the update of hwcur. 876b6e66be2SVincenzo Maffione * (see explanation in sync_kloop_kernel_write). 877b6e66be2SVincenzo Maffione */ 878b6e66be2SVincenzo Maffione *hwtail = ktoa->hwtail; 879f79ba6d7SVincenzo Maffione nm_ldld_barrier(); 880b6e66be2SVincenzo Maffione *hwcur = ktoa->hwcur; 881f79ba6d7SVincenzo Maffione 882f79ba6d7SVincenzo Maffione /* Make sure that loads from ktoa->hwtail and ktoa->hwcur are not delayed 883f79ba6d7SVincenzo Maffione * after the loads from the netmap ring. */ 884f79ba6d7SVincenzo Maffione nm_ldld_barrier(); 885b6e66be2SVincenzo Maffione } 886b6e66be2SVincenzo Maffione 887b6e66be2SVincenzo Maffione /* 8882ff91c17SVincenzo Maffione * data for NETMAP_REQ_OPT_* options 8892ff91c17SVincenzo Maffione */ 8902ff91c17SVincenzo Maffione 891b6e66be2SVincenzo Maffione struct nmreq_opt_sync_kloop_eventfds { 892b6e66be2SVincenzo Maffione struct nmreq_option nro_opt; /* common header */ 893b6e66be2SVincenzo Maffione /* An array of N entries for bidirectional notifications between 894b6e66be2SVincenzo Maffione * the kernel loop and the application. The number of entries and 895b6e66be2SVincenzo Maffione * their order must agree with the CSB arrays passed in the 896b6e66be2SVincenzo Maffione * NETMAP_REQ_OPT_CSB option. Each entry contains a file descriptor 897b6e66be2SVincenzo Maffione * backed by an eventfd. 8985faab778SVincenzo Maffione * 8995faab778SVincenzo Maffione * If any of the 'ioeventfd' entries is < 0, the event loop uses 9005faab778SVincenzo Maffione * the sleeping synchronization strategy (according to sleep_us), 9015faab778SVincenzo Maffione * and keeps kern_need_kick always disabled. 9025faab778SVincenzo Maffione * Each 'irqfd' can be < 0, and in that case the corresponding queue 9035faab778SVincenzo Maffione * is never notified. 904b6e66be2SVincenzo Maffione */ 905b6e66be2SVincenzo Maffione struct { 906b6e66be2SVincenzo Maffione /* Notifier for the application --> kernel loop direction. */ 907b6e66be2SVincenzo Maffione int32_t ioeventfd; 908b6e66be2SVincenzo Maffione /* Notifier for the kernel loop --> application direction. */ 909b6e66be2SVincenzo Maffione int32_t irqfd; 910b6e66be2SVincenzo Maffione } eventfds[0]; 911b6e66be2SVincenzo Maffione }; 912b6e66be2SVincenzo Maffione 9135faab778SVincenzo Maffione struct nmreq_opt_sync_kloop_mode { 9145faab778SVincenzo Maffione struct nmreq_option nro_opt; /* common header */ 9155faab778SVincenzo Maffione #define NM_OPT_SYNC_KLOOP_DIRECT_TX (1 << 0) 9165faab778SVincenzo Maffione #define NM_OPT_SYNC_KLOOP_DIRECT_RX (1 << 1) 9175faab778SVincenzo Maffione uint32_t mode; 9185faab778SVincenzo Maffione }; 9195faab778SVincenzo Maffione 9202ff91c17SVincenzo Maffione struct nmreq_opt_extmem { 9212ff91c17SVincenzo Maffione struct nmreq_option nro_opt; /* common header */ 9222ff91c17SVincenzo Maffione uint64_t nro_usrptr; /* (in) ptr to usr memory */ 9232ff91c17SVincenzo Maffione struct nmreq_pools_info nro_info; /* (in/out) */ 9244bf50f18SLuigi Rizzo }; 9254bf50f18SLuigi Rizzo 926b6e66be2SVincenzo Maffione struct nmreq_opt_csb { 927b6e66be2SVincenzo Maffione struct nmreq_option nro_opt; 928b6e66be2SVincenzo Maffione 929b6e66be2SVincenzo Maffione /* Array of CSB entries for application --> kernel communication 930b6e66be2SVincenzo Maffione * (N entries). */ 931b6e66be2SVincenzo Maffione uint64_t csb_atok; 932b6e66be2SVincenzo Maffione 933b6e66be2SVincenzo Maffione /* Array of CSB entries for kernel --> application communication 934b6e66be2SVincenzo Maffione * (N entries). */ 935b6e66be2SVincenzo Maffione uint64_t csb_ktoa; 936b6e66be2SVincenzo Maffione }; 937b6e66be2SVincenzo Maffione 93868b8534bSLuigi Rizzo #endif /* _NET_NETMAP_H_ */ 939