1fb578518SFranco Fichtner /* 2fb578518SFranco Fichtner * Copyright (C) 2011-2013 Matteo Landi, Luigi Rizzo. All rights reserved. 3fb578518SFranco Fichtner * Copyright (C) 2013 Universita` di Pisa. All rights reserved. 4fb578518SFranco Fichtner * 5fb578518SFranco Fichtner * Redistribution and use in source and binary forms, with or without 6fb578518SFranco Fichtner * modification, are permitted provided that the following conditions 7fb578518SFranco Fichtner * are met: 8fb578518SFranco Fichtner * 1. Redistributions of source code must retain the above copyright 9fb578518SFranco Fichtner * notice, this list of conditions and the following disclaimer. 10fb578518SFranco Fichtner * 2. Redistributions in binary form must reproduce the above copyright 11fb578518SFranco Fichtner * notice, this list of conditions and the following disclaimer in the 12fb578518SFranco Fichtner * documentation and/or other materials provided with the distribution. 13fb578518SFranco Fichtner * 14fb578518SFranco Fichtner * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15fb578518SFranco Fichtner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16fb578518SFranco Fichtner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17fb578518SFranco Fichtner * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18fb578518SFranco Fichtner * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19fb578518SFranco Fichtner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20fb578518SFranco Fichtner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21fb578518SFranco Fichtner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22fb578518SFranco Fichtner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23fb578518SFranco Fichtner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24fb578518SFranco Fichtner * SUCH DAMAGE. 25fb578518SFranco Fichtner */ 26fb578518SFranco Fichtner 27fb578518SFranco Fichtner /* 28fb578518SFranco Fichtner * $FreeBSD: head/sys/dev/netmap/netmap_kern.h 238985 2012-08-02 11:59:43Z luigi $ 29fb578518SFranco Fichtner * 30fb578518SFranco Fichtner * The header contains the definitions of constants and function 31fb578518SFranco Fichtner * prototypes used only in kernelspace. 32fb578518SFranco Fichtner */ 33fb578518SFranco Fichtner 34fb578518SFranco Fichtner #ifndef _NET_NETMAP_KERN_H_ 35fb578518SFranco Fichtner #define _NET_NETMAP_KERN_H_ 36fb578518SFranco Fichtner 37fb578518SFranco Fichtner #define WITH_VALE // comment out to disable VALE support 38fb578518SFranco Fichtner 39fb578518SFranco Fichtner #define likely(x) __builtin_expect((long)!!(x), 1L) 40fb578518SFranco Fichtner #define unlikely(x) __builtin_expect((long)!!(x), 0L) 41fb578518SFranco Fichtner 42*ed9bd855SFranco Fichtner #define NM_LOCK_T struct lock 43*ed9bd855SFranco Fichtner #define NMG_LOCK_T struct lock 44*ed9bd855SFranco Fichtner #define NMG_LOCK_INIT() lockinit(&netmap_global_lock, \ 45*ed9bd855SFranco Fichtner "netmap global lock", 0, 0) 46*ed9bd855SFranco Fichtner #define NMG_LOCK_DESTROY() lockuninit(&netmap_global_lock) 47*ed9bd855SFranco Fichtner #define NMG_LOCK() lockmgr(&netmap_global_lock, LK_EXCLUSIVE) 48*ed9bd855SFranco Fichtner #define NMG_UNLOCK() lockmgr(&netmap_global_lock, LK_RELEASE) 49*ed9bd855SFranco Fichtner #define NMG_LOCK_ASSERT() KKASSERT(lockcountnb(&netmap_global_lock)) 50fb578518SFranco Fichtner 51*ed9bd855SFranco Fichtner #define NM_SELINFO_T struct taskqueue * 52fb578518SFranco Fichtner #define MBUF_LEN(m) ((m)->m_pkthdr.len) 53fb578518SFranco Fichtner #define MBUF_IFP(m) ((m)->m_pkthdr.rcvif) 54fb578518SFranco Fichtner #define NM_SEND_UP(ifp, m) ((ifp)->if_input)(ifp, m) 55fb578518SFranco Fichtner 56fb578518SFranco Fichtner #define NM_ATOMIC_T volatile int // XXX ? 57fb578518SFranco Fichtner /* atomic operations */ 58fb578518SFranco Fichtner #include <machine/atomic.h> 59fb578518SFranco Fichtner #define NM_ATOMIC_TEST_AND_SET(p) (!atomic_cmpset_acq_int((p), 0, 1)) 60fb578518SFranco Fichtner #define NM_ATOMIC_CLEAR(p) atomic_store_rel_int((p), 0) 61fb578518SFranco Fichtner 62fb578518SFranco Fichtner #define prefetch(x) __builtin_prefetch(x) 63fb578518SFranco Fichtner 64fb578518SFranco Fichtner MALLOC_DECLARE(M_NETMAP); 65fb578518SFranco Fichtner 66fb578518SFranco Fichtner // XXX linux struct, not used in FreeBSD 67fb578518SFranco Fichtner struct net_device_ops { 68fb578518SFranco Fichtner }; 69fb578518SFranco Fichtner struct hrtimer { 70fb578518SFranco Fichtner }; 71fb578518SFranco Fichtner 72*ed9bd855SFranco Fichtner #define IFCAP_NETMAP 0x8000 /* XXX move to <net/if.h> */ 73fb578518SFranco Fichtner 74fb578518SFranco Fichtner #define ND(format, ...) 75fb578518SFranco Fichtner #define D(format, ...) \ 76fb578518SFranco Fichtner do { \ 77fb578518SFranco Fichtner struct timeval __xxts; \ 78fb578518SFranco Fichtner microtime(&__xxts); \ 79*ed9bd855SFranco Fichtner kprintf("%03d.%06d %s [%d] " format "\n", \ 80fb578518SFranco Fichtner (int)__xxts.tv_sec % 1000, (int)__xxts.tv_usec, \ 81fb578518SFranco Fichtner __FUNCTION__, __LINE__, ##__VA_ARGS__); \ 82fb578518SFranco Fichtner } while (0) 83fb578518SFranco Fichtner 84fb578518SFranco Fichtner /* rate limited, lps indicates how many per second */ 85fb578518SFranco Fichtner #define RD(lps, format, ...) \ 86fb578518SFranco Fichtner do { \ 87fb578518SFranco Fichtner static int t0, __cnt; \ 88fb578518SFranco Fichtner if (t0 != time_second) { \ 89fb578518SFranco Fichtner t0 = time_second; \ 90fb578518SFranco Fichtner __cnt = 0; \ 91fb578518SFranco Fichtner } \ 92fb578518SFranco Fichtner if (__cnt++ < lps) \ 93fb578518SFranco Fichtner D(format, ##__VA_ARGS__); \ 94fb578518SFranco Fichtner } while (0) 95fb578518SFranco Fichtner 96fb578518SFranco Fichtner struct netmap_adapter; 97fb578518SFranco Fichtner struct nm_bdg_fwd; 98fb578518SFranco Fichtner struct nm_bridge; 99fb578518SFranco Fichtner struct netmap_priv_d; 100fb578518SFranco Fichtner 101fb578518SFranco Fichtner const char *nm_dump_buf(char *p, int len, int lim, char *dst); 102fb578518SFranco Fichtner 103fb578518SFranco Fichtner #include "netmap_mbq.h" 104fb578518SFranco Fichtner 105fb578518SFranco Fichtner extern NMG_LOCK_T netmap_global_lock; 106fb578518SFranco Fichtner 107fb578518SFranco Fichtner /* 108fb578518SFranco Fichtner * private, kernel view of a ring. Keeps track of the status of 109fb578518SFranco Fichtner * a ring across system calls. 110fb578518SFranco Fichtner * 111fb578518SFranco Fichtner * nr_hwcur index of the next buffer to refill. 112fb578518SFranco Fichtner * It corresponds to ring->cur - ring->reserved 113fb578518SFranco Fichtner * 114fb578518SFranco Fichtner * nr_hwavail the number of slots "owned" by userspace. 115fb578518SFranco Fichtner * nr_hwavail =:= ring->avail + ring->reserved 116fb578518SFranco Fichtner * 117fb578518SFranco Fichtner * The indexes in the NIC and netmap rings are offset by nkr_hwofs slots. 118fb578518SFranco Fichtner * This is so that, on a reset, buffers owned by userspace are not 119fb578518SFranco Fichtner * modified by the kernel. In particular: 120fb578518SFranco Fichtner * RX rings: the next empty buffer (hwcur + hwavail + hwofs) coincides with 121fb578518SFranco Fichtner * the next empty buffer as known by the hardware (next_to_check or so). 122fb578518SFranco Fichtner * TX rings: hwcur + hwofs coincides with next_to_send 123fb578518SFranco Fichtner * 124fb578518SFranco Fichtner * Clients cannot issue concurrent syscall on a ring. The system 125fb578518SFranco Fichtner * detects this and reports an error using two flags, 126fb578518SFranco Fichtner * NKR_WBUSY and NKR_RBUSY 127fb578518SFranco Fichtner * For received packets, slot->flags is set to nkr_slot_flags 128fb578518SFranco Fichtner * so we can provide a proper initial value (e.g. set NS_FORWARD 129fb578518SFranco Fichtner * when operating in 'transparent' mode). 130fb578518SFranco Fichtner * 131fb578518SFranco Fichtner * The following fields are used to implement lock-free copy of packets 132fb578518SFranco Fichtner * from input to output ports in VALE switch: 133fb578518SFranco Fichtner * nkr_hwlease buffer after the last one being copied. 134fb578518SFranco Fichtner * A writer in nm_bdg_flush reserves N buffers 135fb578518SFranco Fichtner * from nr_hwlease, advances it, then does the 136fb578518SFranco Fichtner * copy outside the lock. 137fb578518SFranco Fichtner * In RX rings (used for VALE ports), 138fb578518SFranco Fichtner * nkr_hwcur + nkr_hwavail <= nkr_hwlease < nkr_hwcur+N-1 139fb578518SFranco Fichtner * In TX rings (used for NIC or host stack ports) 140fb578518SFranco Fichtner * nkr_hwcur <= nkr_hwlease < nkr_hwcur+ nkr_hwavail 141fb578518SFranco Fichtner * nkr_leases array of nkr_num_slots where writers can report 142fb578518SFranco Fichtner * completion of their block. NR_NOSLOT (~0) indicates 143fb578518SFranco Fichtner * that the writer has not finished yet 144fb578518SFranco Fichtner * nkr_lease_idx index of next free slot in nr_leases, to be assigned 145fb578518SFranco Fichtner * 146fb578518SFranco Fichtner * The kring is manipulated by txsync/rxsync and generic netmap function. 147fb578518SFranco Fichtner * q_lock is used to arbitrate access to the kring from within the netmap 148fb578518SFranco Fichtner * code, and this and other protections guarantee that there is never 149fb578518SFranco Fichtner * more than 1 concurrent call to txsync or rxsync. So we are free 150fb578518SFranco Fichtner * to manipulate the kring from within txsync/rxsync without any extra 151fb578518SFranco Fichtner * locks. 152fb578518SFranco Fichtner */ 153fb578518SFranco Fichtner struct netmap_kring { 154fb578518SFranco Fichtner struct netmap_ring *ring; 155fb578518SFranco Fichtner uint32_t nr_hwcur; 156fb578518SFranco Fichtner uint32_t nr_hwavail; 157fb578518SFranco Fichtner uint32_t nr_kflags; /* private driver flags */ 158fb578518SFranco Fichtner int32_t nr_hwreserved; 159fb578518SFranco Fichtner #define NKR_PENDINTR 0x1 // Pending interrupt. 160fb578518SFranco Fichtner uint32_t nkr_num_slots; 161fb578518SFranco Fichtner int32_t nkr_hwofs; /* offset between NIC and netmap ring */ 162fb578518SFranco Fichtner 163fb578518SFranco Fichtner uint16_t nkr_slot_flags; /* initial value for flags */ 164fb578518SFranco Fichtner struct netmap_adapter *na; 165fb578518SFranco Fichtner struct nm_bdg_fwd *nkr_ft; 166fb578518SFranco Fichtner uint32_t *nkr_leases; 167fb578518SFranco Fichtner #define NR_NOSLOT ((uint32_t)~0) 168fb578518SFranco Fichtner uint32_t nkr_hwlease; 169fb578518SFranco Fichtner uint32_t nkr_lease_idx; 170fb578518SFranco Fichtner 171fb578518SFranco Fichtner NM_SELINFO_T si; /* poll/select wait queue */ 172fb578518SFranco Fichtner NM_LOCK_T q_lock; /* protects kring and ring. */ 173fb578518SFranco Fichtner NM_ATOMIC_T nr_busy; /* prevent concurrent syscalls */ 174fb578518SFranco Fichtner 175fb578518SFranco Fichtner volatile int nkr_stopped; 176fb578518SFranco Fichtner 177fb578518SFranco Fichtner /* support for adapters without native netmap support. 178fb578518SFranco Fichtner * On tx rings we preallocate an array of tx buffers 179fb578518SFranco Fichtner * (same size as the netmap ring), on rx rings we 180fb578518SFranco Fichtner * store incoming packets in a queue. 181fb578518SFranco Fichtner * XXX who writes to the rx queue ? 182fb578518SFranco Fichtner */ 183fb578518SFranco Fichtner struct mbuf **tx_pool; 184fb578518SFranco Fichtner u_int nr_ntc; /* Emulation of a next-to-clean RX ring pointer. */ 185fb578518SFranco Fichtner struct mbq rx_queue; /* A queue for intercepted rx mbufs. */ 186fb578518SFranco Fichtner 187fb578518SFranco Fichtner } __attribute__((__aligned__(64))); 188fb578518SFranco Fichtner 189fb578518SFranco Fichtner 190fb578518SFranco Fichtner /* return the next index, with wraparound */ 191fb578518SFranco Fichtner static inline uint32_t 192fb578518SFranco Fichtner nm_next(uint32_t i, uint32_t lim) 193fb578518SFranco Fichtner { 194fb578518SFranco Fichtner return unlikely (i == lim) ? 0 : i + 1; 195fb578518SFranco Fichtner } 196fb578518SFranco Fichtner 197fb578518SFranco Fichtner /* 198fb578518SFranco Fichtner * 199fb578518SFranco Fichtner * Here is the layout for the Rx and Tx rings. 200fb578518SFranco Fichtner 201fb578518SFranco Fichtner RxRING TxRING 202fb578518SFranco Fichtner 203fb578518SFranco Fichtner +-----------------+ +-----------------+ 204fb578518SFranco Fichtner | | | | 205fb578518SFranco Fichtner |XXX free slot XXX| |XXX free slot XXX| 206fb578518SFranco Fichtner +-----------------+ +-----------------+ 207fb578518SFranco Fichtner | |<-hwcur | |<-hwcur 208fb578518SFranco Fichtner | reserved h | | (ready | 209fb578518SFranco Fichtner +----------- w -+ | to be | 210fb578518SFranco Fichtner cur->| a | | sent) h | 211fb578518SFranco Fichtner | v | +---------- w | 212fb578518SFranco Fichtner | a | cur->| (being a | 213fb578518SFranco Fichtner | i | | prepared) v | 214fb578518SFranco Fichtner | avail l | | a | 215fb578518SFranco Fichtner +-----------------+ + a ------ i + 216fb578518SFranco Fichtner | | ... | v l |<-hwlease 217fb578518SFranco Fichtner | (being | ... | a | ... 218fb578518SFranco Fichtner | prepared) | ... | i | ... 219fb578518SFranco Fichtner +-----------------+ ... | l | ... 220fb578518SFranco Fichtner | |<-hwlease +-----------------+ 221fb578518SFranco Fichtner | | | | 222fb578518SFranco Fichtner | | | | 223fb578518SFranco Fichtner | | | | 224fb578518SFranco Fichtner | | | | 225fb578518SFranco Fichtner +-----------------+ +-----------------+ 226fb578518SFranco Fichtner 227fb578518SFranco Fichtner * The cur/avail (user view) and hwcur/hwavail (kernel view) 228fb578518SFranco Fichtner * are used in the normal operation of the card. 229fb578518SFranco Fichtner * 230fb578518SFranco Fichtner * When a ring is the output of a switch port (Rx ring for 231fb578518SFranco Fichtner * a VALE port, Tx ring for the host stack or NIC), slots 232fb578518SFranco Fichtner * are reserved in blocks through 'hwlease' which points 233fb578518SFranco Fichtner * to the next unused slot. 234fb578518SFranco Fichtner * On an Rx ring, hwlease is always after hwavail, 235fb578518SFranco Fichtner * and completions cause avail to advance. 236fb578518SFranco Fichtner * On a Tx ring, hwlease is always between cur and hwavail, 237fb578518SFranco Fichtner * and completions cause cur to advance. 238fb578518SFranco Fichtner * 239fb578518SFranco Fichtner * nm_kr_space() returns the maximum number of slots that 240fb578518SFranco Fichtner * can be assigned. 241fb578518SFranco Fichtner * nm_kr_lease() reserves the required number of buffers, 242fb578518SFranco Fichtner * advances nkr_hwlease and also returns an entry in 243fb578518SFranco Fichtner * a circular array where completions should be reported. 244fb578518SFranco Fichtner */ 245fb578518SFranco Fichtner 246fb578518SFranco Fichtner 247fb578518SFranco Fichtner 248fb578518SFranco Fichtner 249fb578518SFranco Fichtner enum txrx { NR_RX = 0, NR_TX = 1 }; 250fb578518SFranco Fichtner 251fb578518SFranco Fichtner /* 252fb578518SFranco Fichtner * The "struct netmap_adapter" extends the "struct adapter" 253fb578518SFranco Fichtner * (or equivalent) device descriptor. 254fb578518SFranco Fichtner * It contains all base fields needed to support netmap operation. 255fb578518SFranco Fichtner * There are in fact different types of netmap adapters 256fb578518SFranco Fichtner * (native, generic, VALE switch...) so a netmap_adapter is 257fb578518SFranco Fichtner * just the first field in the derived type. 258fb578518SFranco Fichtner */ 259fb578518SFranco Fichtner struct netmap_adapter { 260fb578518SFranco Fichtner /* 261fb578518SFranco Fichtner * On linux we do not have a good way to tell if an interface 262fb578518SFranco Fichtner * is netmap-capable. So we use the following trick: 263fb578518SFranco Fichtner * NA(ifp) points here, and the first entry (which hopefully 264fb578518SFranco Fichtner * always exists and is at least 32 bits) contains a magic 265fb578518SFranco Fichtner * value which we can use to detect that the interface is good. 266fb578518SFranco Fichtner */ 267fb578518SFranco Fichtner uint32_t magic; 268fb578518SFranco Fichtner uint32_t na_flags; /* future place for IFCAP_NETMAP */ 269fb578518SFranco Fichtner #define NAF_SKIP_INTR 1 /* use the regular interrupt handler. 270fb578518SFranco Fichtner * useful during initialization 271fb578518SFranco Fichtner */ 272fb578518SFranco Fichtner #define NAF_SW_ONLY 2 /* forward packets only to sw adapter */ 273fb578518SFranco Fichtner #define NAF_BDG_MAYSLEEP 4 /* the bridge is allowed to sleep when 274fb578518SFranco Fichtner * forwarding packets coming from this 275fb578518SFranco Fichtner * interface 276fb578518SFranco Fichtner */ 277fb578518SFranco Fichtner #define NAF_MEM_OWNER 8 /* the adapter is responsible for the 278fb578518SFranco Fichtner * deallocation of the memory allocator 279fb578518SFranco Fichtner */ 280fb578518SFranco Fichtner #define NAF_NATIVE_ON 16 /* the adapter is native and the attached 281fb578518SFranco Fichtner * interface is in netmap mode 282fb578518SFranco Fichtner */ 283fb578518SFranco Fichtner int active_fds; /* number of user-space descriptors using this 284fb578518SFranco Fichtner interface, which is equal to the number of 285fb578518SFranco Fichtner struct netmap_if objs in the mapped region. */ 286fb578518SFranco Fichtner 287fb578518SFranco Fichtner u_int num_rx_rings; /* number of adapter receive rings */ 288fb578518SFranco Fichtner u_int num_tx_rings; /* number of adapter transmit rings */ 289fb578518SFranco Fichtner 290fb578518SFranco Fichtner u_int num_tx_desc; /* number of descriptor in each queue */ 291fb578518SFranco Fichtner u_int num_rx_desc; 292fb578518SFranco Fichtner 293fb578518SFranco Fichtner /* tx_rings and rx_rings are private but allocated 294fb578518SFranco Fichtner * as a contiguous chunk of memory. Each array has 295fb578518SFranco Fichtner * N+1 entries, for the adapter queues and for the host queue. 296fb578518SFranco Fichtner */ 297fb578518SFranco Fichtner struct netmap_kring *tx_rings; /* array of TX rings. */ 298fb578518SFranco Fichtner struct netmap_kring *rx_rings; /* array of RX rings. */ 299fb578518SFranco Fichtner void *tailroom; /* space below the rings array */ 300fb578518SFranco Fichtner /* (used for leases) */ 301fb578518SFranco Fichtner 302*ed9bd855SFranco Fichtner NM_SELINFO_T tx_si; /* global tx wait queue */ 303*ed9bd855SFranco Fichtner NM_SELINFO_T rx_si; /* global rx wait queue */ 304fb578518SFranco Fichtner 305fb578518SFranco Fichtner /* copy of if_qflush and if_transmit pointers, to intercept 306fb578518SFranco Fichtner * packets from the network stack when netmap is active. 307fb578518SFranco Fichtner */ 308fb578518SFranco Fichtner int (*if_transmit)(struct ifnet *, struct mbuf *); 309fb578518SFranco Fichtner 310fb578518SFranco Fichtner /* references to the ifnet and device routines, used by 311fb578518SFranco Fichtner * the generic netmap functions. 312fb578518SFranco Fichtner */ 313fb578518SFranco Fichtner struct ifnet *ifp; /* adapter is ifp->if_softc */ 314fb578518SFranco Fichtner 315fb578518SFranco Fichtner /* private cleanup */ 316fb578518SFranco Fichtner void (*nm_dtor)(struct netmap_adapter *); 317fb578518SFranco Fichtner 318fb578518SFranco Fichtner int (*nm_register)(struct netmap_adapter *, int onoff); 319fb578518SFranco Fichtner 320fb578518SFranco Fichtner int (*nm_txsync)(struct netmap_adapter *, u_int ring, int flags); 321fb578518SFranco Fichtner int (*nm_rxsync)(struct netmap_adapter *, u_int ring, int flags); 322fb578518SFranco Fichtner #define NAF_FORCE_READ 1 323fb578518SFranco Fichtner #define NAF_FORCE_RECLAIM 2 324fb578518SFranco Fichtner /* return configuration information */ 325fb578518SFranco Fichtner int (*nm_config)(struct netmap_adapter *, 326fb578518SFranco Fichtner u_int *txr, u_int *txd, u_int *rxr, u_int *rxd); 327fb578518SFranco Fichtner int (*nm_krings_create)(struct netmap_adapter *); 328fb578518SFranco Fichtner void (*nm_krings_delete)(struct netmap_adapter *); 329fb578518SFranco Fichtner int (*nm_notify)(struct netmap_adapter *, 330fb578518SFranco Fichtner u_int ring, enum txrx, int flags); 331fb578518SFranco Fichtner #define NAF_GLOBAL_NOTIFY 4 332fb578518SFranco Fichtner #define NAF_DISABLE_NOTIFY 8 333fb578518SFranco Fichtner 334fb578518SFranco Fichtner /* standard refcount to control the lifetime of the adapter 335fb578518SFranco Fichtner * (it should be equal to the lifetime of the corresponding ifp) 336fb578518SFranco Fichtner */ 337fb578518SFranco Fichtner int na_refcount; 338fb578518SFranco Fichtner 339fb578518SFranco Fichtner /* memory allocator (opaque) 340fb578518SFranco Fichtner * We also cache a pointer to the lut_entry for translating 341fb578518SFranco Fichtner * buffer addresses, and the total number of buffers. 342fb578518SFranco Fichtner */ 343fb578518SFranco Fichtner struct netmap_mem_d *nm_mem; 344fb578518SFranco Fichtner struct lut_entry *na_lut; 345fb578518SFranco Fichtner uint32_t na_lut_objtotal; /* max buffer index */ 346fb578518SFranco Fichtner 347fb578518SFranco Fichtner /* used internally. If non-null, the interface cannot be bound 348fb578518SFranco Fichtner * from userspace 349fb578518SFranco Fichtner */ 350fb578518SFranco Fichtner void *na_private; 351fb578518SFranco Fichtner }; 352fb578518SFranco Fichtner 353fb578518SFranco Fichtner /* 354fb578518SFranco Fichtner * If the NIC is owned by the kernel 355fb578518SFranco Fichtner * (i.e., bridge), neither another bridge nor user can use it; 356fb578518SFranco Fichtner * if the NIC is owned by a user, only users can share it. 357fb578518SFranco Fichtner * Evaluation must be done under NMG_LOCK(). 358fb578518SFranco Fichtner */ 359fb578518SFranco Fichtner #define NETMAP_OWNED_BY_KERN(na) (na->na_private) 360fb578518SFranco Fichtner #define NETMAP_OWNED_BY_ANY(na) \ 361fb578518SFranco Fichtner (NETMAP_OWNED_BY_KERN(na) || (na->active_fds > 0)) 362fb578518SFranco Fichtner 363fb578518SFranco Fichtner 364fb578518SFranco Fichtner /* 365fb578518SFranco Fichtner * derived netmap adapters for various types of ports 366fb578518SFranco Fichtner */ 367fb578518SFranco Fichtner struct netmap_vp_adapter { /* VALE software port */ 368fb578518SFranco Fichtner struct netmap_adapter up; 369fb578518SFranco Fichtner 370fb578518SFranco Fichtner /* 371fb578518SFranco Fichtner * Bridge support: 372fb578518SFranco Fichtner * 373fb578518SFranco Fichtner * bdg_port is the port number used in the bridge; 374fb578518SFranco Fichtner * na_bdg points to the bridge this NA is attached to. 375fb578518SFranco Fichtner */ 376fb578518SFranco Fichtner int bdg_port; 377fb578518SFranco Fichtner struct nm_bridge *na_bdg; 378fb578518SFranco Fichtner int retry; 379fb578518SFranco Fichtner }; 380fb578518SFranco Fichtner 381fb578518SFranco Fichtner struct netmap_hw_adapter { /* physical device */ 382fb578518SFranco Fichtner struct netmap_adapter up; 383fb578518SFranco Fichtner 384fb578518SFranco Fichtner struct net_device_ops nm_ndo; // XXX linux only 385fb578518SFranco Fichtner }; 386fb578518SFranco Fichtner 387fb578518SFranco Fichtner struct netmap_generic_adapter { /* non-native device */ 388fb578518SFranco Fichtner struct netmap_hw_adapter up; 389fb578518SFranco Fichtner 390fb578518SFranco Fichtner /* Pointer to a previously used netmap adapter. */ 391fb578518SFranco Fichtner struct netmap_adapter *prev; 392fb578518SFranco Fichtner 393fb578518SFranco Fichtner /* generic netmap adapters support: 394fb578518SFranco Fichtner * a net_device_ops struct overrides ndo_select_queue(), 395fb578518SFranco Fichtner * save_if_input saves the if_input hook (FreeBSD), 396fb578518SFranco Fichtner * mit_timer and mit_pending implement rx interrupt mitigation, 397fb578518SFranco Fichtner */ 398fb578518SFranco Fichtner struct net_device_ops generic_ndo; 399fb578518SFranco Fichtner void (*save_if_input)(struct ifnet *, struct mbuf *); 400fb578518SFranco Fichtner 401fb578518SFranco Fichtner struct hrtimer mit_timer; 402fb578518SFranco Fichtner int mit_pending; 403fb578518SFranco Fichtner }; 404fb578518SFranco Fichtner 405fb578518SFranco Fichtner #ifdef WITH_VALE 406fb578518SFranco Fichtner 407fb578518SFranco Fichtner /* bridge wrapper for non VALE ports. It is used to connect real devices to the bridge. 408fb578518SFranco Fichtner * 409fb578518SFranco Fichtner * The real device must already have its own netmap adapter (hwna). The 410fb578518SFranco Fichtner * bridge wrapper and the hwna adapter share the same set of netmap rings and 411fb578518SFranco Fichtner * buffers, but they have two separate sets of krings descriptors, with tx/rx 412fb578518SFranco Fichtner * meanings swapped: 413fb578518SFranco Fichtner * 414fb578518SFranco Fichtner * netmap 415fb578518SFranco Fichtner * bwrap krings rings krings hwna 416fb578518SFranco Fichtner * +------+ +------+ +-----+ +------+ +------+ 417fb578518SFranco Fichtner * |tx_rings->| |\ /| |----| |<-tx_rings| 418fb578518SFranco Fichtner * | | +------+ \ / +-----+ +------+ | | 419fb578518SFranco Fichtner * | | X | | 420fb578518SFranco Fichtner * | | / \ | | 421fb578518SFranco Fichtner * | | +------+/ \+-----+ +------+ | | 422fb578518SFranco Fichtner * |rx_rings->| | | |----| |<-rx_rings| 423fb578518SFranco Fichtner * | | +------+ +-----+ +------+ | | 424fb578518SFranco Fichtner * +------+ +------+ 425fb578518SFranco Fichtner * 426fb578518SFranco Fichtner * - packets coming from the bridge go to the brwap rx rings, which are also the 427fb578518SFranco Fichtner * hwna tx rings. The bwrap notify callback will then complete the hwna tx 428fb578518SFranco Fichtner * (see netmap_bwrap_notify). 429fb578518SFranco Fichtner * - packets coming from the outside go to the hwna rx rings, which are also the 430fb578518SFranco Fichtner * bwrap tx rings. The (overwritten) hwna notify method will then complete 431fb578518SFranco Fichtner * the bridge tx (see netmap_bwrap_intr_notify). 432fb578518SFranco Fichtner * 433fb578518SFranco Fichtner * The bridge wrapper may optionally connect the hwna 'host' rings to the 434fb578518SFranco Fichtner * bridge. This is done by using a second port in the bridge and connecting it 435fb578518SFranco Fichtner * to the 'host' netmap_vp_adapter contained in the netmap_bwrap_adapter. 436fb578518SFranco Fichtner * The brwap host adapter cross-links the hwna host rings in the same way as shown above. 437fb578518SFranco Fichtner * 438fb578518SFranco Fichtner * - packets coming from the bridge and directed to host stack are handled by the 439fb578518SFranco Fichtner * bwrap host notify callback (see netmap_bwrap_host_notify) 440fb578518SFranco Fichtner * - packets coming from the host stack are still handled by the overwritten 441fb578518SFranco Fichtner * hwna notify callback (netmap_bwrap_intr_notify), but are diverted to the 442fb578518SFranco Fichtner * host adapter depending on the ring number. 443fb578518SFranco Fichtner * 444fb578518SFranco Fichtner */ 445fb578518SFranco Fichtner struct netmap_bwrap_adapter { 446fb578518SFranco Fichtner struct netmap_vp_adapter up; 447fb578518SFranco Fichtner struct netmap_vp_adapter host; /* for host rings */ 448fb578518SFranco Fichtner struct netmap_adapter *hwna; /* the underlying device */ 449fb578518SFranco Fichtner 450fb578518SFranco Fichtner /* backup of the hwna notify callback */ 451fb578518SFranco Fichtner int (*save_notify)(struct netmap_adapter *, 452fb578518SFranco Fichtner u_int ring, enum txrx, int flags); 453fb578518SFranco Fichtner /* When we attach a physical interface to the bridge, we 454fb578518SFranco Fichtner * allow the controlling process to terminate, so we need 455fb578518SFranco Fichtner * a place to store the netmap_priv_d data structure. 456fb578518SFranco Fichtner * This is only done when physical interfaces are attached to a bridge. 457fb578518SFranco Fichtner */ 458fb578518SFranco Fichtner struct netmap_priv_d *na_kpriv; 459fb578518SFranco Fichtner }; 460fb578518SFranco Fichtner 461fb578518SFranco Fichtner 462fb578518SFranco Fichtner /* 463fb578518SFranco Fichtner * Available space in the ring. Only used in VALE code 464fb578518SFranco Fichtner */ 465fb578518SFranco Fichtner static inline uint32_t 466fb578518SFranco Fichtner nm_kr_space(struct netmap_kring *k, int is_rx) 467fb578518SFranco Fichtner { 468fb578518SFranco Fichtner int space; 469fb578518SFranco Fichtner 470fb578518SFranco Fichtner if (is_rx) { 471fb578518SFranco Fichtner int busy = k->nkr_hwlease - k->nr_hwcur + k->nr_hwreserved; 472fb578518SFranco Fichtner if (busy < 0) 473fb578518SFranco Fichtner busy += k->nkr_num_slots; 474fb578518SFranco Fichtner space = k->nkr_num_slots - 1 - busy; 475fb578518SFranco Fichtner } else { 476fb578518SFranco Fichtner space = k->nr_hwcur + k->nr_hwavail - k->nkr_hwlease; 477fb578518SFranco Fichtner if (space < 0) 478fb578518SFranco Fichtner space += k->nkr_num_slots; 479fb578518SFranco Fichtner } 480fb578518SFranco Fichtner #if 0 481fb578518SFranco Fichtner // sanity check 482fb578518SFranco Fichtner if (k->nkr_hwlease >= k->nkr_num_slots || 483fb578518SFranco Fichtner k->nr_hwcur >= k->nkr_num_slots || 484fb578518SFranco Fichtner k->nr_hwavail >= k->nkr_num_slots || 485fb578518SFranco Fichtner busy < 0 || 486fb578518SFranco Fichtner busy >= k->nkr_num_slots) { 487fb578518SFranco Fichtner D("invalid kring, cur %d avail %d lease %d lease_idx %d lim %d", k->nr_hwcur, k->nr_hwavail, k->nkr_hwlease, 488fb578518SFranco Fichtner k->nkr_lease_idx, k->nkr_num_slots); 489fb578518SFranco Fichtner } 490fb578518SFranco Fichtner #endif 491fb578518SFranco Fichtner return space; 492fb578518SFranco Fichtner } 493fb578518SFranco Fichtner 494fb578518SFranco Fichtner 495fb578518SFranco Fichtner 496fb578518SFranco Fichtner 497fb578518SFranco Fichtner /* make a lease on the kring for N positions. return the 498fb578518SFranco Fichtner * lease index 499fb578518SFranco Fichtner */ 500fb578518SFranco Fichtner static inline uint32_t 501fb578518SFranco Fichtner nm_kr_lease(struct netmap_kring *k, u_int n, int is_rx) 502fb578518SFranco Fichtner { 503fb578518SFranco Fichtner uint32_t lim = k->nkr_num_slots - 1; 504fb578518SFranco Fichtner uint32_t lease_idx = k->nkr_lease_idx; 505fb578518SFranco Fichtner 506fb578518SFranco Fichtner k->nkr_leases[lease_idx] = NR_NOSLOT; 507fb578518SFranco Fichtner k->nkr_lease_idx = nm_next(lease_idx, lim); 508fb578518SFranco Fichtner 509fb578518SFranco Fichtner if (n > nm_kr_space(k, is_rx)) { 510fb578518SFranco Fichtner D("invalid request for %d slots", n); 511fb578518SFranco Fichtner panic("x"); 512fb578518SFranco Fichtner } 513fb578518SFranco Fichtner /* XXX verify that there are n slots */ 514fb578518SFranco Fichtner k->nkr_hwlease += n; 515fb578518SFranco Fichtner if (k->nkr_hwlease > lim) 516fb578518SFranco Fichtner k->nkr_hwlease -= lim + 1; 517fb578518SFranco Fichtner 518fb578518SFranco Fichtner if (k->nkr_hwlease >= k->nkr_num_slots || 519fb578518SFranco Fichtner k->nr_hwcur >= k->nkr_num_slots || 520fb578518SFranco Fichtner k->nr_hwavail >= k->nkr_num_slots || 521fb578518SFranco Fichtner k->nkr_lease_idx >= k->nkr_num_slots) { 522fb578518SFranco Fichtner D("invalid kring %s, cur %d avail %d lease %d lease_idx %d lim %d", 523fb578518SFranco Fichtner k->na->ifp->if_xname, 524fb578518SFranco Fichtner k->nr_hwcur, k->nr_hwavail, k->nkr_hwlease, 525fb578518SFranco Fichtner k->nkr_lease_idx, k->nkr_num_slots); 526fb578518SFranco Fichtner } 527fb578518SFranco Fichtner return lease_idx; 528fb578518SFranco Fichtner } 529fb578518SFranco Fichtner 530fb578518SFranco Fichtner #endif /* WITH_VALE */ 531fb578518SFranco Fichtner 532fb578518SFranco Fichtner /* return update position */ 533fb578518SFranco Fichtner static inline uint32_t 534fb578518SFranco Fichtner nm_kr_rxpos(struct netmap_kring *k) 535fb578518SFranco Fichtner { 536fb578518SFranco Fichtner uint32_t pos = k->nr_hwcur + k->nr_hwavail; 537fb578518SFranco Fichtner if (pos >= k->nkr_num_slots) 538fb578518SFranco Fichtner pos -= k->nkr_num_slots; 539fb578518SFranco Fichtner #if 0 540fb578518SFranco Fichtner if (pos >= k->nkr_num_slots || 541fb578518SFranco Fichtner k->nkr_hwlease >= k->nkr_num_slots || 542fb578518SFranco Fichtner k->nr_hwcur >= k->nkr_num_slots || 543fb578518SFranco Fichtner k->nr_hwavail >= k->nkr_num_slots || 544fb578518SFranco Fichtner k->nkr_lease_idx >= k->nkr_num_slots) { 545fb578518SFranco Fichtner D("invalid kring, cur %d avail %d lease %d lease_idx %d lim %d", k->nr_hwcur, k->nr_hwavail, k->nkr_hwlease, 546fb578518SFranco Fichtner k->nkr_lease_idx, k->nkr_num_slots); 547fb578518SFranco Fichtner } 548fb578518SFranco Fichtner #endif 549fb578518SFranco Fichtner return pos; 550fb578518SFranco Fichtner } 551fb578518SFranco Fichtner 552fb578518SFranco Fichtner 553fb578518SFranco Fichtner /* 554fb578518SFranco Fichtner * protect against multiple threads using the same ring. 555fb578518SFranco Fichtner * also check that the ring has not been stopped. 556fb578518SFranco Fichtner * We only care for 0 or !=0 as a return code. 557fb578518SFranco Fichtner */ 558fb578518SFranco Fichtner #define NM_KR_BUSY 1 559fb578518SFranco Fichtner #define NM_KR_STOPPED 2 560fb578518SFranco Fichtner 561fb578518SFranco Fichtner static __inline void nm_kr_put(struct netmap_kring *kr) 562fb578518SFranco Fichtner { 563fb578518SFranco Fichtner NM_ATOMIC_CLEAR(&kr->nr_busy); 564fb578518SFranco Fichtner } 565fb578518SFranco Fichtner 566fb578518SFranco Fichtner static __inline int nm_kr_tryget(struct netmap_kring *kr) 567fb578518SFranco Fichtner { 568fb578518SFranco Fichtner /* check a first time without taking the lock 569fb578518SFranco Fichtner * to avoid starvation for nm_kr_get() 570fb578518SFranco Fichtner */ 571fb578518SFranco Fichtner if (unlikely(kr->nkr_stopped)) { 572fb578518SFranco Fichtner ND("ring %p stopped (%d)", kr, kr->nkr_stopped); 573fb578518SFranco Fichtner return NM_KR_STOPPED; 574fb578518SFranco Fichtner } 575fb578518SFranco Fichtner if (unlikely(NM_ATOMIC_TEST_AND_SET(&kr->nr_busy))) 576fb578518SFranco Fichtner return NM_KR_BUSY; 577fb578518SFranco Fichtner /* check a second time with lock held */ 578fb578518SFranco Fichtner if (unlikely(kr->nkr_stopped)) { 579fb578518SFranco Fichtner ND("ring %p stopped (%d)", kr, kr->nkr_stopped); 580fb578518SFranco Fichtner nm_kr_put(kr); 581fb578518SFranco Fichtner return NM_KR_STOPPED; 582fb578518SFranco Fichtner } 583fb578518SFranco Fichtner return 0; 584fb578518SFranco Fichtner } 585fb578518SFranco Fichtner 586fb578518SFranco Fichtner 587fb578518SFranco Fichtner /* 588fb578518SFranco Fichtner * The following are support routines used by individual drivers to 589fb578518SFranco Fichtner * support netmap operation. 590fb578518SFranco Fichtner * 591fb578518SFranco Fichtner * netmap_attach() initializes a struct netmap_adapter, allocating the 592fb578518SFranco Fichtner * struct netmap_ring's and the struct selinfo. 593fb578518SFranco Fichtner * 594fb578518SFranco Fichtner * netmap_detach() frees the memory allocated by netmap_attach(). 595fb578518SFranco Fichtner * 596fb578518SFranco Fichtner * netmap_transmit() replaces the if_transmit routine of the interface, 597fb578518SFranco Fichtner * and is used to intercept packets coming from the stack. 598fb578518SFranco Fichtner * 599fb578518SFranco Fichtner * netmap_load_map/netmap_reload_map are helper routines to set/reset 600fb578518SFranco Fichtner * the dmamap for a packet buffer 601fb578518SFranco Fichtner * 602fb578518SFranco Fichtner * netmap_reset() is a helper routine to be called in the driver 603fb578518SFranco Fichtner * when reinitializing a ring. 604fb578518SFranco Fichtner */ 605fb578518SFranco Fichtner int netmap_attach(struct netmap_adapter *); 606fb578518SFranco Fichtner int netmap_attach_common(struct netmap_adapter *); 607fb578518SFranco Fichtner void netmap_detach_common(struct netmap_adapter *na); 608fb578518SFranco Fichtner void netmap_detach(struct ifnet *); 609fb578518SFranco Fichtner int netmap_transmit(struct ifnet *, struct mbuf *); 610fb578518SFranco Fichtner struct netmap_slot *netmap_reset(struct netmap_adapter *na, 611fb578518SFranco Fichtner enum txrx tx, u_int n, u_int new_cur); 612fb578518SFranco Fichtner int netmap_ring_reinit(struct netmap_kring *); 613fb578518SFranco Fichtner 614fb578518SFranco Fichtner 615fb578518SFranco Fichtner /* 616fb578518SFranco Fichtner * Support routines to be used with the VALE switch 617fb578518SFranco Fichtner */ 618fb578518SFranco Fichtner int netmap_update_config(struct netmap_adapter *na); 619fb578518SFranco Fichtner int netmap_krings_create(struct netmap_adapter *na, u_int ntx, u_int nrx, u_int tailroom); 620fb578518SFranco Fichtner void netmap_krings_delete(struct netmap_adapter *na); 621fb578518SFranco Fichtner 622fb578518SFranco Fichtner struct netmap_if * 623fb578518SFranco Fichtner netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na, 624fb578518SFranco Fichtner uint16_t ringid, int *err); 625fb578518SFranco Fichtner 626fb578518SFranco Fichtner 627fb578518SFranco Fichtner 628fb578518SFranco Fichtner u_int nm_bound_var(u_int *v, u_int dflt, u_int lo, u_int hi, const char *msg); 629fb578518SFranco Fichtner int netmap_get_na(struct nmreq *nmr, struct netmap_adapter **na, int create); 630fb578518SFranco Fichtner int netmap_get_hw_na(struct ifnet *ifp, struct netmap_adapter **na); 631fb578518SFranco Fichtner 632fb578518SFranco Fichtner #ifdef WITH_VALE 633fb578518SFranco Fichtner /* 634fb578518SFranco Fichtner * The following bridge-related interfaces are used by other kernel modules 635fb578518SFranco Fichtner * In the version that only supports unicast or broadcast, the lookup 636fb578518SFranco Fichtner * function can return 0 .. NM_BDG_MAXPORTS-1 for regular ports, 637fb578518SFranco Fichtner * NM_BDG_MAXPORTS for broadcast, NM_BDG_MAXPORTS+1 for unknown. 638fb578518SFranco Fichtner * XXX in practice "unknown" might be handled same as broadcast. 639fb578518SFranco Fichtner */ 640fb578518SFranco Fichtner typedef u_int (*bdg_lookup_fn_t)(char *buf, u_int len, 641fb578518SFranco Fichtner uint8_t *ring_nr, struct netmap_vp_adapter *); 642fb578518SFranco Fichtner u_int netmap_bdg_learning(char *, u_int, uint8_t *, 643fb578518SFranco Fichtner struct netmap_vp_adapter *); 644fb578518SFranco Fichtner 645fb578518SFranco Fichtner #define NM_BDG_MAXPORTS 254 /* up to 254 */ 646fb578518SFranco Fichtner #define NM_BDG_BROADCAST NM_BDG_MAXPORTS 647fb578518SFranco Fichtner #define NM_BDG_NOPORT (NM_BDG_MAXPORTS+1) 648fb578518SFranco Fichtner 649fb578518SFranco Fichtner #define NM_NAME "vale" /* prefix for bridge port name */ 650fb578518SFranco Fichtner 651fb578518SFranco Fichtner 652fb578518SFranco Fichtner /* these are redefined in case of no VALE support */ 653fb578518SFranco Fichtner int netmap_get_bdg_na(struct nmreq *nmr, struct netmap_adapter **na, int create); 654fb578518SFranco Fichtner void netmap_init_bridges(void); 655fb578518SFranco Fichtner int netmap_bdg_ctl(struct nmreq *nmr, bdg_lookup_fn_t func); 656fb578518SFranco Fichtner 657fb578518SFranco Fichtner #else /* !WITH_VALE */ 658fb578518SFranco Fichtner #define netmap_get_bdg_na(_1, _2, _3) 0 659fb578518SFranco Fichtner #define netmap_init_bridges(_1) 660fb578518SFranco Fichtner #define netmap_bdg_ctl(_1, _2) EINVAL 661fb578518SFranco Fichtner #endif /* !WITH_VALE */ 662fb578518SFranco Fichtner 663fb578518SFranco Fichtner /* Various prototypes */ 664fb578518SFranco Fichtner int netmap_poll(struct cdev *dev, int events, struct thread *td); 665fb578518SFranco Fichtner 666fb578518SFranco Fichtner 667fb578518SFranco Fichtner int netmap_init(void); 668fb578518SFranco Fichtner void netmap_fini(void); 669fb578518SFranco Fichtner int netmap_get_memory(struct netmap_priv_d* p); 670fb578518SFranco Fichtner void netmap_dtor(void *data); 671fb578518SFranco Fichtner int netmap_dtor_locked(struct netmap_priv_d *priv); 672fb578518SFranco Fichtner 673fb578518SFranco Fichtner int netmap_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td); 674fb578518SFranco Fichtner 675fb578518SFranco Fichtner /* netmap_adapter creation/destruction */ 676fb578518SFranco Fichtner #define NM_IFPNAME(ifp) ((ifp) ? (ifp)->if_xname : "zombie") 677fb578518SFranco Fichtner #define NM_DEBUG_PUTGET 1 678fb578518SFranco Fichtner 679fb578518SFranco Fichtner #ifdef NM_DEBUG_PUTGET 680fb578518SFranco Fichtner 681fb578518SFranco Fichtner #define NM_DBG(f) __##f 682fb578518SFranco Fichtner 683fb578518SFranco Fichtner void __netmap_adapter_get(struct netmap_adapter *na); 684fb578518SFranco Fichtner 685fb578518SFranco Fichtner #define netmap_adapter_get(na) \ 686fb578518SFranco Fichtner do { \ 687fb578518SFranco Fichtner struct netmap_adapter *__na = na; \ 688fb578518SFranco Fichtner D("getting %p:%s (%d)", __na, NM_IFPNAME(__na->ifp), __na->na_refcount); \ 689fb578518SFranco Fichtner __netmap_adapter_get(__na); \ 690fb578518SFranco Fichtner } while (0) 691fb578518SFranco Fichtner 692fb578518SFranco Fichtner int __netmap_adapter_put(struct netmap_adapter *na); 693fb578518SFranco Fichtner 694fb578518SFranco Fichtner #define netmap_adapter_put(na) \ 695fb578518SFranco Fichtner do { \ 696fb578518SFranco Fichtner struct netmap_adapter *__na = na; \ 697fb578518SFranco Fichtner D("putting %p:%s (%d)", __na, NM_IFPNAME(__na->ifp), __na->na_refcount); \ 698fb578518SFranco Fichtner __netmap_adapter_put(__na); \ 699fb578518SFranco Fichtner } while (0) 700fb578518SFranco Fichtner 701fb578518SFranco Fichtner #else /* !NM_DEBUG_PUTGET */ 702fb578518SFranco Fichtner 703fb578518SFranco Fichtner #define NM_DBG(f) f 704fb578518SFranco Fichtner void netmap_adapter_get(struct netmap_adapter *na); 705fb578518SFranco Fichtner int netmap_adapter_put(struct netmap_adapter *na); 706fb578518SFranco Fichtner 707fb578518SFranco Fichtner #endif /* !NM_DEBUG_PUTGET */ 708fb578518SFranco Fichtner 709fb578518SFranco Fichtner 710fb578518SFranco Fichtner 711fb578518SFranco Fichtner extern u_int netmap_buf_size; 712fb578518SFranco Fichtner #define NETMAP_BUF_SIZE netmap_buf_size // XXX remove 713fb578518SFranco Fichtner extern int netmap_mitigate; 714fb578518SFranco Fichtner extern int netmap_no_pendintr; 715fb578518SFranco Fichtner extern u_int netmap_total_buffers; 716fb578518SFranco Fichtner extern char *netmap_buffer_base; 717fb578518SFranco Fichtner extern int netmap_verbose; // XXX debugging 718fb578518SFranco Fichtner enum { /* verbose flags */ 719fb578518SFranco Fichtner NM_VERB_ON = 1, /* generic verbose */ 720fb578518SFranco Fichtner NM_VERB_HOST = 0x2, /* verbose host stack */ 721fb578518SFranco Fichtner NM_VERB_RXSYNC = 0x10, /* verbose on rxsync/txsync */ 722fb578518SFranco Fichtner NM_VERB_TXSYNC = 0x20, 723fb578518SFranco Fichtner NM_VERB_RXINTR = 0x100, /* verbose on rx/tx intr (driver) */ 724fb578518SFranco Fichtner NM_VERB_TXINTR = 0x200, 725fb578518SFranco Fichtner NM_VERB_NIC_RXSYNC = 0x1000, /* verbose on rx/tx intr (driver) */ 726fb578518SFranco Fichtner NM_VERB_NIC_TXSYNC = 0x2000, 727fb578518SFranco Fichtner }; 728fb578518SFranco Fichtner 729fb578518SFranco Fichtner extern int netmap_txsync_retry; 730fb578518SFranco Fichtner extern int netmap_generic_mit; 731fb578518SFranco Fichtner extern int netmap_generic_ringsize; 732fb578518SFranco Fichtner 733fb578518SFranco Fichtner /* 734fb578518SFranco Fichtner * NA returns a pointer to the struct netmap adapter from the ifp, 735fb578518SFranco Fichtner * WNA is used to write it. 736fb578518SFranco Fichtner */ 737fb578518SFranco Fichtner #ifndef WNA 738*ed9bd855SFranco Fichtner #define WNA(_ifp) (_ifp)->if_unused7 /* XXX better name ;) */ 739fb578518SFranco Fichtner #endif 740fb578518SFranco Fichtner #define NA(_ifp) ((struct netmap_adapter *)WNA(_ifp)) 741fb578518SFranco Fichtner 742fb578518SFranco Fichtner /* 743fb578518SFranco Fichtner * Macros to determine if an interface is netmap capable or netmap enabled. 744fb578518SFranco Fichtner * See the magic field in struct netmap_adapter. 745fb578518SFranco Fichtner */ 746fb578518SFranco Fichtner /* 747fb578518SFranco Fichtner * on FreeBSD just use if_capabilities and if_capenable. 748fb578518SFranco Fichtner */ 749fb578518SFranco Fichtner #define NETMAP_CAPABLE(ifp) (NA(ifp) && \ 750fb578518SFranco Fichtner (ifp)->if_capabilities & IFCAP_NETMAP ) 751fb578518SFranco Fichtner 752fb578518SFranco Fichtner #define NETMAP_SET_CAPABLE(ifp) \ 753fb578518SFranco Fichtner (ifp)->if_capabilities |= IFCAP_NETMAP 754fb578518SFranco Fichtner 755fb578518SFranco Fichtner /* Callback invoked by the dma machinery after a successfull dmamap_load */ 756fb578518SFranco Fichtner static void netmap_dmamap_cb(__unused void *arg, 757fb578518SFranco Fichtner __unused bus_dma_segment_t * segs, __unused int nseg, __unused int error) 758fb578518SFranco Fichtner { 759fb578518SFranco Fichtner } 760fb578518SFranco Fichtner 761fb578518SFranco Fichtner /* bus_dmamap_load wrapper: call aforementioned function if map != NULL. 762fb578518SFranco Fichtner * XXX can we do it without a callback ? 763fb578518SFranco Fichtner */ 764fb578518SFranco Fichtner static inline void 765fb578518SFranco Fichtner netmap_load_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf) 766fb578518SFranco Fichtner { 767fb578518SFranco Fichtner if (map) 768fb578518SFranco Fichtner bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE, 769fb578518SFranco Fichtner netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT); 770fb578518SFranco Fichtner } 771fb578518SFranco Fichtner 772fb578518SFranco Fichtner /* update the map when a buffer changes. */ 773fb578518SFranco Fichtner static inline void 774fb578518SFranco Fichtner netmap_reload_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf) 775fb578518SFranco Fichtner { 776fb578518SFranco Fichtner if (map) { 777fb578518SFranco Fichtner bus_dmamap_unload(tag, map); 778fb578518SFranco Fichtner bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE, 779fb578518SFranco Fichtner netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT); 780fb578518SFranco Fichtner } 781fb578518SFranco Fichtner } 782fb578518SFranco Fichtner 783fb578518SFranco Fichtner /* 784fb578518SFranco Fichtner * functions to map NIC to KRING indexes (n2k) and vice versa (k2n) 785fb578518SFranco Fichtner */ 786fb578518SFranco Fichtner static inline int 787fb578518SFranco Fichtner netmap_idx_n2k(struct netmap_kring *kr, int idx) 788fb578518SFranco Fichtner { 789fb578518SFranco Fichtner int n = kr->nkr_num_slots; 790fb578518SFranco Fichtner idx += kr->nkr_hwofs; 791fb578518SFranco Fichtner if (idx < 0) 792fb578518SFranco Fichtner return idx + n; 793fb578518SFranco Fichtner else if (idx < n) 794fb578518SFranco Fichtner return idx; 795fb578518SFranco Fichtner else 796fb578518SFranco Fichtner return idx - n; 797fb578518SFranco Fichtner } 798fb578518SFranco Fichtner 799fb578518SFranco Fichtner 800fb578518SFranco Fichtner static inline int 801fb578518SFranco Fichtner netmap_idx_k2n(struct netmap_kring *kr, int idx) 802fb578518SFranco Fichtner { 803fb578518SFranco Fichtner int n = kr->nkr_num_slots; 804fb578518SFranco Fichtner idx -= kr->nkr_hwofs; 805fb578518SFranco Fichtner if (idx < 0) 806fb578518SFranco Fichtner return idx + n; 807fb578518SFranco Fichtner else if (idx < n) 808fb578518SFranco Fichtner return idx; 809fb578518SFranco Fichtner else 810fb578518SFranco Fichtner return idx - n; 811fb578518SFranco Fichtner } 812fb578518SFranco Fichtner 813fb578518SFranco Fichtner 814fb578518SFranco Fichtner /* Entries of the look-up table. */ 815fb578518SFranco Fichtner struct lut_entry { 816fb578518SFranco Fichtner void *vaddr; /* virtual address. */ 817fb578518SFranco Fichtner vm_paddr_t paddr; /* physical address. */ 818fb578518SFranco Fichtner }; 819fb578518SFranco Fichtner 820fb578518SFranco Fichtner struct netmap_obj_pool; 821fb578518SFranco Fichtner extern struct lut_entry *netmap_buffer_lut; 822fb578518SFranco Fichtner #define NMB_VA(i) (netmap_buffer_lut[i].vaddr) 823fb578518SFranco Fichtner #define NMB_PA(i) (netmap_buffer_lut[i].paddr) 824fb578518SFranco Fichtner 825fb578518SFranco Fichtner /* 826fb578518SFranco Fichtner * NMB return the virtual address of a buffer (buffer 0 on bad index) 827fb578518SFranco Fichtner * PNMB also fills the physical address 828fb578518SFranco Fichtner */ 829fb578518SFranco Fichtner static inline void * 830fb578518SFranco Fichtner NMB(struct netmap_slot *slot) 831fb578518SFranco Fichtner { 832fb578518SFranco Fichtner uint32_t i = slot->buf_idx; 833fb578518SFranco Fichtner return (unlikely(i >= netmap_total_buffers)) ? NMB_VA(0) : NMB_VA(i); 834fb578518SFranco Fichtner } 835fb578518SFranco Fichtner 836fb578518SFranco Fichtner static inline void * 837fb578518SFranco Fichtner PNMB(struct netmap_slot *slot, uint64_t *pp) 838fb578518SFranco Fichtner { 839fb578518SFranco Fichtner uint32_t i = slot->buf_idx; 840fb578518SFranco Fichtner void *ret = (i >= netmap_total_buffers) ? NMB_VA(0) : NMB_VA(i); 841fb578518SFranco Fichtner 842fb578518SFranco Fichtner *pp = (i >= netmap_total_buffers) ? NMB_PA(0) : NMB_PA(i); 843fb578518SFranco Fichtner return ret; 844fb578518SFranco Fichtner } 845fb578518SFranco Fichtner 846fb578518SFranco Fichtner /* Generic version of NMB, which uses device-specific memory. */ 847fb578518SFranco Fichtner static inline void * 848fb578518SFranco Fichtner BDG_NMB(struct netmap_adapter *na, struct netmap_slot *slot) 849fb578518SFranco Fichtner { 850fb578518SFranco Fichtner struct lut_entry *lut = na->na_lut; 851fb578518SFranco Fichtner uint32_t i = slot->buf_idx; 852fb578518SFranco Fichtner return (unlikely(i >= na->na_lut_objtotal)) ? 853fb578518SFranco Fichtner lut[0].vaddr : lut[i].vaddr; 854fb578518SFranco Fichtner } 855fb578518SFranco Fichtner 856fb578518SFranco Fichtner /* default functions to handle rx/tx interrupts */ 857fb578518SFranco Fichtner int netmap_rx_irq(struct ifnet *, u_int, u_int *); 858fb578518SFranco Fichtner #define netmap_tx_irq(_n, _q) netmap_rx_irq(_n, _q, NULL) 859fb578518SFranco Fichtner int netmap_common_irq(struct ifnet *, u_int, u_int *work_done); 860fb578518SFranco Fichtner 861fb578518SFranco Fichtner 862fb578518SFranco Fichtner void netmap_txsync_to_host(struct netmap_adapter *na); 863fb578518SFranco Fichtner void netmap_disable_all_rings(struct ifnet *); 864fb578518SFranco Fichtner void netmap_enable_all_rings(struct ifnet *); 865fb578518SFranco Fichtner void netmap_disable_ring(struct netmap_kring *kr); 866fb578518SFranco Fichtner 867fb578518SFranco Fichtner 868fb578518SFranco Fichtner /* Structure associated to each thread which registered an interface. 869fb578518SFranco Fichtner * 870fb578518SFranco Fichtner * The first 4 fields of this structure are written by NIOCREGIF and 871fb578518SFranco Fichtner * read by poll() and NIOC?XSYNC. 872fb578518SFranco Fichtner * There is low contention among writers (actually, a correct user program 873fb578518SFranco Fichtner * should have no contention among writers) and among writers and readers, 874fb578518SFranco Fichtner * so we use a single global lock to protect the structure initialization. 875fb578518SFranco Fichtner * Since initialization involves the allocation of memory, we reuse the memory 876fb578518SFranco Fichtner * allocator lock. 877fb578518SFranco Fichtner * Read access to the structure is lock free. Readers must check that 878fb578518SFranco Fichtner * np_nifp is not NULL before using the other fields. 879fb578518SFranco Fichtner * If np_nifp is NULL initialization has not been performed, so they should 880fb578518SFranco Fichtner * return an error to userlevel. 881fb578518SFranco Fichtner * 882fb578518SFranco Fichtner * The ref_done field is used to regulate access to the refcount in the 883fb578518SFranco Fichtner * memory allocator. The refcount must be incremented at most once for 884fb578518SFranco Fichtner * each open("/dev/netmap"). The increment is performed by the first 885fb578518SFranco Fichtner * function that calls netmap_get_memory() (currently called by 886fb578518SFranco Fichtner * mmap(), NIOCGINFO and NIOCREGIF). 887fb578518SFranco Fichtner * If the refcount is incremented, it is then decremented when the 888fb578518SFranco Fichtner * private structure is destroyed. 889fb578518SFranco Fichtner */ 890fb578518SFranco Fichtner struct netmap_priv_d { 891fb578518SFranco Fichtner struct netmap_if * volatile np_nifp; /* netmap if descriptor. */ 892fb578518SFranco Fichtner 893fb578518SFranco Fichtner struct netmap_adapter *np_na; 894fb578518SFranco Fichtner int np_ringid; /* from the ioctl */ 895fb578518SFranco Fichtner u_int np_qfirst, np_qlast; /* range of rings to scan */ 896fb578518SFranco Fichtner uint16_t np_txpoll; 897fb578518SFranco Fichtner 898fb578518SFranco Fichtner struct netmap_mem_d *np_mref; /* use with NMG_LOCK held */ 899fb578518SFranco Fichtner /* np_refcount is only used on FreeBSD */ 900fb578518SFranco Fichtner int np_refcount; /* use with NMG_LOCK held */ 901fb578518SFranco Fichtner }; 902fb578518SFranco Fichtner 903fb578518SFranco Fichtner 904fb578518SFranco Fichtner /* 905fb578518SFranco Fichtner * generic netmap emulation for devices that do not have 906fb578518SFranco Fichtner * native netmap support. 907fb578518SFranco Fichtner * XXX generic_netmap_register() is only exported to implement 908fb578518SFranco Fichtner * nma_is_generic(). 909fb578518SFranco Fichtner */ 910fb578518SFranco Fichtner int generic_netmap_register(struct netmap_adapter *na, int enable); 911fb578518SFranco Fichtner int generic_netmap_attach(struct ifnet *ifp); 912fb578518SFranco Fichtner 913fb578518SFranco Fichtner int netmap_catch_rx(struct netmap_adapter *na, int intercept); 914fb578518SFranco Fichtner void generic_rx_handler(struct ifnet *ifp, struct mbuf *m);; 915fb578518SFranco Fichtner void netmap_catch_packet_steering(struct netmap_generic_adapter *na, int enable); 916fb578518SFranco Fichtner int generic_xmit_frame(struct ifnet *ifp, struct mbuf *m, void *addr, u_int len, u_int ring_nr); 917fb578518SFranco Fichtner int generic_find_num_desc(struct ifnet *ifp, u_int *tx, u_int *rx); 918fb578518SFranco Fichtner void generic_find_num_queues(struct ifnet *ifp, u_int *txq, u_int *rxq); 919fb578518SFranco Fichtner 920fb578518SFranco Fichtner static __inline int 921fb578518SFranco Fichtner nma_is_generic(struct netmap_adapter *na) 922fb578518SFranco Fichtner { 923fb578518SFranco Fichtner return na->nm_register == generic_netmap_register; 924fb578518SFranco Fichtner } 925fb578518SFranco Fichtner 926fb578518SFranco Fichtner /* 927fb578518SFranco Fichtner * netmap_mitigation API. This is used by the generic adapter 928fb578518SFranco Fichtner * to reduce the number of interrupt requests/selwakeup 929fb578518SFranco Fichtner * to clients on incoming packets. 930fb578518SFranco Fichtner */ 931fb578518SFranco Fichtner void netmap_mitigation_init(struct netmap_generic_adapter *na); 932fb578518SFranco Fichtner void netmap_mitigation_start(struct netmap_generic_adapter *na); 933fb578518SFranco Fichtner void netmap_mitigation_restart(struct netmap_generic_adapter *na); 934fb578518SFranco Fichtner int netmap_mitigation_active(struct netmap_generic_adapter *na); 935fb578518SFranco Fichtner void netmap_mitigation_cleanup(struct netmap_generic_adapter *na); 936fb578518SFranco Fichtner 937fb578518SFranco Fichtner // int generic_timer_handler(struct hrtimer *t); 938fb578518SFranco Fichtner 939fb578518SFranco Fichtner #endif /* _NET_NETMAP_KERN_H_ */ 940