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