1 /* 2 * Copyright (C) 2011-2012 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 11829 2012-09-26 04:06:34Z 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 #define NETMAP_MEM2 // use the new memory allocator 38 39 #if defined(__FreeBSD__) 40 #define likely(x) __builtin_expect(!!(x), 1) 41 #define unlikely(x) __builtin_expect(!!(x), 0) 42 43 #define NM_LOCK_T struct mtx 44 #define NM_SELINFO_T struct selinfo 45 #define MBUF_LEN(m) ((m)->m_pkthdr.len) 46 #define NM_SEND_UP(ifp, m) ((ifp)->if_input)(ifp, m) 47 #elif defined (linux) 48 #define NM_LOCK_T spinlock_t 49 #define NM_SELINFO_T wait_queue_head_t 50 #define MBUF_LEN(m) ((m)->len) 51 #define NM_SEND_UP(ifp, m) netif_rx(m) 52 53 #ifndef DEV_NETMAP 54 #define DEV_NETMAP 55 #endif 56 57 /* 58 * IFCAP_NETMAP goes into net_device's priv_flags (if_capenable). 59 * This was 16 bits up to linux 2.6.36, so we need a 16 bit value on older 60 * platforms and tolerate the clash with IFF_DYNAMIC and IFF_BRIDGE_PORT. 61 * For the 32-bit value, 0x100000 has no clashes until at least 3.5.1 62 */ 63 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37) 64 #define IFCAP_NETMAP 0x8000 65 #else 66 #define IFCAP_NETMAP 0x100000 67 #endif 68 69 #elif defined (__APPLE__) 70 #warning apple support is incomplete. 71 #define likely(x) __builtin_expect(!!(x), 1) 72 #define unlikely(x) __builtin_expect(!!(x), 0) 73 #define NM_LOCK_T IOLock * 74 #define NM_SELINFO_T struct selinfo 75 #define MBUF_LEN(m) ((m)->m_pkthdr.len) 76 #define NM_SEND_UP(ifp, m) ((ifp)->if_input)(ifp, m) 77 78 #else 79 #error unsupported platform 80 #endif 81 82 #define ND(format, ...) 83 #define D(format, ...) \ 84 do { \ 85 struct timeval __xxts; \ 86 microtime(&__xxts); \ 87 printf("%03d.%06d %s [%d] " format "\n", \ 88 (int)__xxts.tv_sec % 1000, (int)__xxts.tv_usec, \ 89 __FUNCTION__, __LINE__, ##__VA_ARGS__); \ 90 } while (0) 91 92 /* rate limited, lps indicates how many per second */ 93 #define RD(lps, format, ...) \ 94 do { \ 95 static int t0, __cnt; \ 96 if (t0 != time_second) { \ 97 t0 = time_second; \ 98 __cnt = 0; \ 99 } \ 100 if (__cnt++ < lps) \ 101 D(format, ##__VA_ARGS__); \ 102 } while (0) 103 104 struct netmap_adapter; 105 106 /* 107 * private, kernel view of a ring. Keeps track of the status of 108 * a ring across system calls. 109 * 110 * nr_hwcur index of the next buffer to refill. 111 * It corresponds to ring->cur - ring->reserved 112 * 113 * nr_hwavail the number of slots "owned" by userspace. 114 * nr_hwavail =:= ring->avail + ring->reserved 115 * 116 * The indexes in the NIC and netmap rings are offset by nkr_hwofs slots. 117 * This is so that, on a reset, buffers owned by userspace are not 118 * modified by the kernel. In particular: 119 * RX rings: the next empty buffer (hwcur + hwavail + hwofs) coincides with 120 * the next empty buffer as known by the hardware (next_to_check or so). 121 * TX rings: hwcur + hwofs coincides with next_to_send 122 * 123 * For received packets, slot->flags is set to nkr_slot_flags 124 * so we can provide a proper initial value (e.g. set NS_FORWARD 125 * when operating in 'transparent' mode). 126 */ 127 struct netmap_kring { 128 struct netmap_ring *ring; 129 u_int nr_hwcur; 130 int nr_hwavail; 131 u_int nr_kflags; /* private driver flags */ 132 #define NKR_PENDINTR 0x1 // Pending interrupt. 133 u_int nkr_num_slots; 134 135 uint16_t nkr_slot_flags; /* initial value for flags */ 136 int nkr_hwofs; /* offset between NIC and netmap ring */ 137 struct netmap_adapter *na; 138 NM_SELINFO_T si; /* poll/select wait queue */ 139 NM_LOCK_T q_lock; /* used if no device lock available */ 140 } __attribute__((__aligned__(64))); 141 142 /* 143 * This struct extends the 'struct adapter' (or 144 * equivalent) device descriptor. It contains all fields needed to 145 * support netmap operation. 146 */ 147 struct netmap_adapter { 148 /* 149 * On linux we do not have a good way to tell if an interface 150 * is netmap-capable. So we use the following trick: 151 * NA(ifp) points here, and the first entry (which hopefully 152 * always exists and is at least 32 bits) contains a magic 153 * value which we can use to detect that the interface is good. 154 */ 155 uint32_t magic; 156 uint32_t na_flags; /* future place for IFCAP_NETMAP */ 157 #define NAF_SKIP_INTR 1 /* use the regular interrupt handler. 158 * useful during initialization 159 */ 160 int refcount; /* number of user-space descriptors using this 161 interface, which is equal to the number of 162 struct netmap_if objs in the mapped region. */ 163 /* 164 * The selwakeup in the interrupt thread can use per-ring 165 * and/or global wait queues. We track how many clients 166 * of each type we have so we can optimize the drivers, 167 * and especially avoid huge contention on the locks. 168 */ 169 int na_single; /* threads attached to a single hw queue */ 170 int na_multi; /* threads attached to multiple hw queues */ 171 172 int separate_locks; /* set if the interface suports different 173 locks for rx, tx and core. */ 174 175 u_int num_rx_rings; /* number of adapter receive rings */ 176 u_int num_tx_rings; /* number of adapter transmit rings */ 177 178 u_int num_tx_desc; /* number of descriptor in each queue */ 179 u_int num_rx_desc; 180 181 /* tx_rings and rx_rings are private but allocated 182 * as a contiguous chunk of memory. Each array has 183 * N+1 entries, for the adapter queues and for the host queue. 184 */ 185 struct netmap_kring *tx_rings; /* array of TX rings. */ 186 struct netmap_kring *rx_rings; /* array of RX rings. */ 187 188 NM_SELINFO_T tx_si, rx_si; /* global wait queues */ 189 190 /* copy of if_qflush and if_transmit pointers, to intercept 191 * packets from the network stack when netmap is active. 192 */ 193 int (*if_transmit)(struct ifnet *, struct mbuf *); 194 195 /* references to the ifnet and device routines, used by 196 * the generic netmap functions. 197 */ 198 struct ifnet *ifp; /* adapter is ifp->if_softc */ 199 200 NM_LOCK_T core_lock; /* used if no device lock available */ 201 202 int (*nm_register)(struct ifnet *, int onoff); 203 void (*nm_lock)(struct ifnet *, int what, u_int ringid); 204 int (*nm_txsync)(struct ifnet *, u_int ring, int lock); 205 int (*nm_rxsync)(struct ifnet *, u_int ring, int lock); 206 207 int bdg_port; 208 #ifdef linux 209 struct net_device_ops nm_ndo; 210 int if_refcount; // XXX additions for bridge 211 #endif /* linux */ 212 }; 213 214 /* 215 * The combination of "enable" (ifp->if_capenable & IFCAP_NETMAP) 216 * and refcount gives the status of the interface, namely: 217 * 218 * enable refcount Status 219 * 220 * FALSE 0 normal operation 221 * FALSE != 0 -- (impossible) 222 * TRUE 1 netmap mode 223 * TRUE 0 being deleted. 224 */ 225 226 #define NETMAP_DELETING(_na) ( ((_na)->refcount == 0) && \ 227 ( (_na)->ifp->if_capenable & IFCAP_NETMAP) ) 228 229 /* 230 * parameters for (*nm_lock)(adapter, what, index) 231 */ 232 enum { 233 NETMAP_NO_LOCK = 0, 234 NETMAP_CORE_LOCK, NETMAP_CORE_UNLOCK, 235 NETMAP_TX_LOCK, NETMAP_TX_UNLOCK, 236 NETMAP_RX_LOCK, NETMAP_RX_UNLOCK, 237 #ifdef __FreeBSD__ 238 #define NETMAP_REG_LOCK NETMAP_CORE_LOCK 239 #define NETMAP_REG_UNLOCK NETMAP_CORE_UNLOCK 240 #else 241 NETMAP_REG_LOCK, NETMAP_REG_UNLOCK 242 #endif 243 }; 244 245 /* 246 * The following are support routines used by individual drivers to 247 * support netmap operation. 248 * 249 * netmap_attach() initializes a struct netmap_adapter, allocating the 250 * struct netmap_ring's and the struct selinfo. 251 * 252 * netmap_detach() frees the memory allocated by netmap_attach(). 253 * 254 * netmap_start() replaces the if_transmit routine of the interface, 255 * and is used to intercept packets coming from the stack. 256 * 257 * netmap_load_map/netmap_reload_map are helper routines to set/reset 258 * the dmamap for a packet buffer 259 * 260 * netmap_reset() is a helper routine to be called in the driver 261 * when reinitializing a ring. 262 */ 263 int netmap_attach(struct netmap_adapter *, int); 264 void netmap_detach(struct ifnet *); 265 int netmap_start(struct ifnet *, struct mbuf *); 266 enum txrx { NR_RX = 0, NR_TX = 1 }; 267 struct netmap_slot *netmap_reset(struct netmap_adapter *na, 268 enum txrx tx, int n, u_int new_cur); 269 int netmap_ring_reinit(struct netmap_kring *); 270 271 extern u_int netmap_buf_size; 272 #define NETMAP_BUF_SIZE netmap_buf_size 273 extern int netmap_mitigate; 274 extern int netmap_no_pendintr; 275 extern u_int netmap_total_buffers; 276 extern char *netmap_buffer_base; 277 extern int netmap_verbose; // XXX debugging 278 enum { /* verbose flags */ 279 NM_VERB_ON = 1, /* generic verbose */ 280 NM_VERB_HOST = 0x2, /* verbose host stack */ 281 NM_VERB_RXSYNC = 0x10, /* verbose on rxsync/txsync */ 282 NM_VERB_TXSYNC = 0x20, 283 NM_VERB_RXINTR = 0x100, /* verbose on rx/tx intr (driver) */ 284 NM_VERB_TXINTR = 0x200, 285 NM_VERB_NIC_RXSYNC = 0x1000, /* verbose on rx/tx intr (driver) */ 286 NM_VERB_NIC_TXSYNC = 0x2000, 287 }; 288 289 /* 290 * NA returns a pointer to the struct netmap adapter from the ifp, 291 * WNA is used to write it. 292 */ 293 #ifndef WNA 294 #define WNA(_ifp) (_ifp)->if_pspare[0] 295 #endif 296 #define NA(_ifp) ((struct netmap_adapter *)WNA(_ifp)) 297 298 /* 299 * Macros to determine if an interface is netmap capable or netmap enabled. 300 * See the magic field in struct netmap_adapter. 301 */ 302 #ifdef __FreeBSD__ 303 /* 304 * on FreeBSD just use if_capabilities and if_capenable. 305 */ 306 #define NETMAP_CAPABLE(ifp) (NA(ifp) && \ 307 (ifp)->if_capabilities & IFCAP_NETMAP ) 308 309 #define NETMAP_SET_CAPABLE(ifp) \ 310 (ifp)->if_capabilities |= IFCAP_NETMAP 311 312 #else /* linux */ 313 314 /* 315 * on linux: 316 * we check if NA(ifp) is set and its first element has a related 317 * magic value. The capenable is within the struct netmap_adapter. 318 */ 319 #define NETMAP_MAGIC 0x52697a7a 320 321 #define NETMAP_CAPABLE(ifp) (NA(ifp) && \ 322 ((uint32_t)(uintptr_t)NA(ifp) ^ NA(ifp)->magic) == NETMAP_MAGIC ) 323 324 #define NETMAP_SET_CAPABLE(ifp) \ 325 NA(ifp)->magic = ((uint32_t)(uintptr_t)NA(ifp)) ^ NETMAP_MAGIC 326 327 #endif /* linux */ 328 329 #ifdef __FreeBSD__ 330 /* Callback invoked by the dma machinery after a successfull dmamap_load */ 331 static void netmap_dmamap_cb(__unused void *arg, 332 __unused bus_dma_segment_t * segs, __unused int nseg, __unused int error) 333 { 334 } 335 336 /* bus_dmamap_load wrapper: call aforementioned function if map != NULL. 337 * XXX can we do it without a callback ? 338 */ 339 static inline void 340 netmap_load_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf) 341 { 342 if (map) 343 bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE, 344 netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT); 345 } 346 347 /* update the map when a buffer changes. */ 348 static inline void 349 netmap_reload_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf) 350 { 351 if (map) { 352 bus_dmamap_unload(tag, map); 353 bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE, 354 netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT); 355 } 356 } 357 #else /* linux */ 358 359 /* 360 * XXX How do we redefine these functions: 361 * 362 * on linux we need 363 * dma_map_single(&pdev->dev, virt_addr, len, direction) 364 * dma_unmap_single(&adapter->pdev->dev, phys_addr, len, direction 365 * The len can be implicit (on netmap it is NETMAP_BUF_SIZE) 366 * unfortunately the direction is not, so we need to change 367 * something to have a cross API 368 */ 369 #define netmap_load_map(_t, _m, _b) 370 #define netmap_reload_map(_t, _m, _b) 371 #if 0 372 struct e1000_buffer *buffer_info = &tx_ring->buffer_info[l]; 373 /* set time_stamp *before* dma to help avoid a possible race */ 374 buffer_info->time_stamp = jiffies; 375 buffer_info->mapped_as_page = false; 376 buffer_info->length = len; 377 //buffer_info->next_to_watch = l; 378 /* reload dma map */ 379 dma_unmap_single(&adapter->pdev->dev, buffer_info->dma, 380 NETMAP_BUF_SIZE, DMA_TO_DEVICE); 381 buffer_info->dma = dma_map_single(&adapter->pdev->dev, 382 addr, NETMAP_BUF_SIZE, DMA_TO_DEVICE); 383 384 if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) { 385 D("dma mapping error"); 386 /* goto dma_error; See e1000_put_txbuf() */ 387 /* XXX reset */ 388 } 389 tx_desc->buffer_addr = htole64(buffer_info->dma); //XXX 390 391 #endif 392 393 /* 394 * The bus_dmamap_sync() can be one of wmb() or rmb() depending on direction. 395 */ 396 #define bus_dmamap_sync(_a, _b, _c) 397 398 #endif /* linux */ 399 400 /* 401 * functions to map NIC to KRING indexes (n2k) and vice versa (k2n) 402 */ 403 static inline int 404 netmap_idx_n2k(struct netmap_kring *kr, int idx) 405 { 406 int n = kr->nkr_num_slots; 407 idx += kr->nkr_hwofs; 408 if (idx < 0) 409 return idx + n; 410 else if (idx < n) 411 return idx; 412 else 413 return idx - n; 414 } 415 416 417 static inline int 418 netmap_idx_k2n(struct netmap_kring *kr, int idx) 419 { 420 int n = kr->nkr_num_slots; 421 idx -= kr->nkr_hwofs; 422 if (idx < 0) 423 return idx + n; 424 else if (idx < n) 425 return idx; 426 else 427 return idx - n; 428 } 429 430 431 #ifdef NETMAP_MEM2 432 /* Entries of the look-up table. */ 433 struct lut_entry { 434 void *vaddr; /* virtual address. */ 435 vm_paddr_t paddr; /* phisical address. */ 436 }; 437 438 struct netmap_obj_pool; 439 extern struct lut_entry *netmap_buffer_lut; 440 #define NMB_VA(i) (netmap_buffer_lut[i].vaddr) 441 #define NMB_PA(i) (netmap_buffer_lut[i].paddr) 442 #else /* NETMAP_MEM1 */ 443 #define NMB_VA(i) (netmap_buffer_base + (i * NETMAP_BUF_SIZE) ) 444 #endif /* NETMAP_MEM2 */ 445 446 /* 447 * NMB return the virtual address of a buffer (buffer 0 on bad index) 448 * PNMB also fills the physical address 449 */ 450 static inline void * 451 NMB(struct netmap_slot *slot) 452 { 453 uint32_t i = slot->buf_idx; 454 return (unlikely(i >= netmap_total_buffers)) ? NMB_VA(0) : NMB_VA(i); 455 } 456 457 static inline void * 458 PNMB(struct netmap_slot *slot, uint64_t *pp) 459 { 460 uint32_t i = slot->buf_idx; 461 void *ret = (i >= netmap_total_buffers) ? NMB_VA(0) : NMB_VA(i); 462 #ifdef NETMAP_MEM2 463 *pp = (i >= netmap_total_buffers) ? NMB_PA(0) : NMB_PA(i); 464 #else 465 *pp = vtophys(ret); 466 #endif 467 return ret; 468 } 469 470 /* default functions to handle rx/tx interrupts */ 471 int netmap_rx_irq(struct ifnet *, int, int *); 472 #define netmap_tx_irq(_n, _q) netmap_rx_irq(_n, _q, NULL) 473 474 extern int netmap_copy; 475 #endif /* _NET_NETMAP_KERN_H_ */ 476