1 /* 2 * Copyright (C) 2011 Matteo Landi, Luigi Rizzo. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 /* 27 * $FreeBSD$ 28 * $Id: netmap_kern.h 9795 2011-12-02 11:39:08Z luigi $ 29 * 30 * The header contains the definitions of constants and function 31 * prototypes used only in kernelspace. 32 */ 33 34 #ifndef _NET_NETMAP_KERN_H_ 35 #define _NET_NETMAP_KERN_H_ 36 37 #ifdef MALLOC_DECLARE 38 MALLOC_DECLARE(M_NETMAP); 39 #endif 40 41 #define ND(format, ...) 42 #define D(format, ...) \ 43 do { \ 44 struct timeval __xxts; \ 45 microtime(&__xxts); \ 46 printf("%03d.%06d %s [%d] " format "\n", \ 47 (int)__xxts.tv_sec % 1000, (int)__xxts.tv_usec, \ 48 __FUNCTION__, __LINE__, ##__VA_ARGS__); \ 49 } while (0) 50 51 struct netmap_adapter; 52 53 /* 54 * private, kernel view of a ring. 55 * 56 * XXX 20110627-todo 57 * The index in the NIC and netmap ring is offset by nkr_hwofs slots. 58 * This is so that, on a reset, buffers owned by userspace are not 59 * modified by the kernel. In particular: 60 * RX rings: the next empty buffer (hwcur + hwavail + hwofs) coincides 61 * the next empty buffer as known by the hardware (next_to_check or so). 62 * TX rings: hwcur + hwofs coincides with next_to_send 63 */ 64 struct netmap_kring { 65 struct netmap_ring *ring; 66 u_int nr_hwcur; 67 int nr_hwavail; 68 u_int nr_kflags; /* private driver flags */ 69 #define NKR_PENDINTR 0x1 // Pending interrupt. 70 u_int nkr_num_slots; 71 72 int nkr_hwofs; /* offset between NIC and netmap ring */ 73 struct netmap_adapter *na; // debugging 74 struct selinfo si; /* poll/select wait queue */ 75 } __attribute__((__aligned__(64))); 76 77 /* 78 * This struct is part of and extends the 'struct adapter' (or 79 * equivalent) device descriptor. It contains all fields needed to 80 * support netmap operation. 81 */ 82 struct netmap_adapter { 83 int refcount; /* number of user-space descriptors using this 84 interface, which is equal to the number of 85 struct netmap_if objs in the mapped region. */ 86 87 int separate_locks; /* set if the interface suports different 88 locks for rx, tx and core. */ 89 90 u_int num_queues; /* number of tx/rx queue pairs: this is 91 a duplicate field needed to simplify the 92 signature of ``netmap_detach``. */ 93 94 u_int num_tx_desc; /* number of descriptor in each queue */ 95 u_int num_rx_desc; 96 u_int buff_size; 97 98 u_int flags; 99 /* tx_rings and rx_rings are private but allocated 100 * as a contiguous chunk of memory. Each array has 101 * N+1 entries, for the adapter queues and for the host queue. 102 */ 103 struct netmap_kring *tx_rings; /* array of TX rings. */ 104 struct netmap_kring *rx_rings; /* array of RX rings. */ 105 106 /* copy of if_qflush and if_transmit pointers, to intercept 107 * packets from the network stack when netmap is active. 108 * XXX probably if_qflush is not necessary. 109 */ 110 void (*if_qflush)(struct ifnet *); 111 int (*if_transmit)(struct ifnet *, struct mbuf *); 112 113 /* references to the ifnet and device routines, used by 114 * the generic netmap functions. 115 */ 116 struct ifnet *ifp; /* adapter is ifp->if_softc */ 117 118 int (*nm_register)(struct ifnet *, int onoff); 119 void (*nm_lock)(void *, int what, u_int ringid); 120 int (*nm_txsync)(void *, u_int ring, int lock); 121 int (*nm_rxsync)(void *, u_int ring, int lock); 122 }; 123 124 /* 125 * The combination of "enable" (ifp->if_capabilities &IFCAP_NETMAP) 126 * and refcount gives the status of the interface, namely: 127 * 128 * enable refcount Status 129 * 130 * FALSE 0 normal operation 131 * FALSE != 0 -- (impossible) 132 * TRUE 1 netmap mode 133 * TRUE 0 being deleted. 134 */ 135 136 #define NETMAP_DELETING(_na) ( ((_na)->refcount == 0) && \ 137 ( (_na)->ifp->if_capenable & IFCAP_NETMAP) ) 138 139 /* 140 * parameters for (*nm_lock)(adapter, what, index) 141 */ 142 enum { 143 NETMAP_NO_LOCK = 0, 144 NETMAP_CORE_LOCK, NETMAP_CORE_UNLOCK, 145 NETMAP_TX_LOCK, NETMAP_TX_UNLOCK, 146 NETMAP_RX_LOCK, NETMAP_RX_UNLOCK, 147 }; 148 149 /* 150 * The following are support routines used by individual drivers to 151 * support netmap operation. 152 * 153 * netmap_attach() initializes a struct netmap_adapter, allocating the 154 * struct netmap_ring's and the struct selinfo. 155 * 156 * netmap_detach() frees the memory allocated by netmap_attach(). 157 * 158 * netmap_start() replaces the if_transmit routine of the interface, 159 * and is used to intercept packets coming from the stack. 160 * 161 * netmap_load_map/netmap_reload_map are helper routines to set/reset 162 * the dmamap for a packet buffer 163 * 164 * netmap_reset() is a helper routine to be called in the driver 165 * when reinitializing a ring. 166 */ 167 int netmap_attach(struct netmap_adapter *, int); 168 void netmap_detach(struct ifnet *); 169 int netmap_start(struct ifnet *, struct mbuf *); 170 enum txrx { NR_RX = 0, NR_TX = 1 }; 171 struct netmap_slot *netmap_reset(struct netmap_adapter *na, 172 enum txrx tx, int n, u_int new_cur); 173 int netmap_ring_reinit(struct netmap_kring *); 174 175 extern int netmap_mitigate; 176 extern int netmap_skip_txsync, netmap_skip_rxsync; 177 extern u_int netmap_total_buffers; 178 extern char *netmap_buffer_base; 179 extern int netmap_verbose; // XXX debugging 180 enum { /* verbose flags */ 181 NM_VERB_ON = 1, /* generic verbose */ 182 NM_VERB_HOST = 0x2, /* verbose host stack */ 183 NM_VERB_RXSYNC = 0x10, /* verbose on rxsync/txsync */ 184 NM_VERB_TXSYNC = 0x20, 185 NM_VERB_RXINTR = 0x100, /* verbose on rx/tx intr (driver) */ 186 NM_VERB_TXINTR = 0x200, 187 NM_VERB_NIC_RXSYNC = 0x1000, /* verbose on rx/tx intr (driver) */ 188 NM_VERB_NIC_TXSYNC = 0x2000, 189 }; 190 191 /* 192 * NA returns a pointer to the struct netmap adapter from the ifp, 193 * WNA is used to write it. 194 */ 195 #ifndef WNA 196 #define WNA(_ifp) (_ifp)->if_pspare[0] 197 #endif 198 #define NA(_ifp) ((struct netmap_adapter *)WNA(_ifp)) 199 200 201 /* Callback invoked by the dma machinery after a successfull dmamap_load */ 202 static void netmap_dmamap_cb(__unused void *arg, 203 __unused bus_dma_segment_t * segs, __unused int nseg, __unused int error) 204 { 205 } 206 207 /* bus_dmamap_load wrapper: call aforementioned function if map != NULL. 208 * XXX can we do it without a callback ? 209 */ 210 static inline void 211 netmap_load_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf) 212 { 213 if (map) 214 bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE, 215 netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT); 216 } 217 218 /* update the map when a buffer changes. */ 219 static inline void 220 netmap_reload_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf) 221 { 222 if (map) { 223 bus_dmamap_unload(tag, map); 224 bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE, 225 netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT); 226 } 227 } 228 229 230 /* 231 * NMB return the virtual address of a buffer (buffer 0 on bad index) 232 * PNMB also fills the physical address 233 */ 234 static inline void * 235 NMB(struct netmap_slot *slot) 236 { 237 uint32_t i = slot->buf_idx; 238 return (i >= netmap_total_buffers) ? netmap_buffer_base : 239 #if NETMAP_BUF_SIZE == 2048 240 netmap_buffer_base + (i << 11); 241 #else 242 netmap_buffer_base + (i *NETMAP_BUF_SIZE); 243 #endif 244 } 245 246 static inline void * 247 PNMB(struct netmap_slot *slot, uint64_t *pp) 248 { 249 uint32_t i = slot->buf_idx; 250 void *ret = (i >= netmap_total_buffers) ? netmap_buffer_base : 251 #if NETMAP_BUF_SIZE == 2048 252 netmap_buffer_base + (i << 11); 253 #else 254 netmap_buffer_base + (i *NETMAP_BUF_SIZE); 255 #endif 256 *pp = vtophys(ret); 257 return ret; 258 } 259 260 #endif /* _NET_NETMAP_KERN_H_ */ 261