1 /* 2 * Copyright (C) 2011-2014 Matteo Landi, Luigi Rizzo 3 * Copyright (C) 2013-2016 Universita` di Pisa 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 /* 29 * $FreeBSD$ 30 * 31 * The header contains the definitions of constants and function 32 * prototypes used only in kernelspace. 33 */ 34 35 #ifndef _NET_NETMAP_KERN_H_ 36 #define _NET_NETMAP_KERN_H_ 37 38 #if defined(linux) 39 40 #if defined(CONFIG_NETMAP_VALE) 41 #define WITH_VALE 42 #endif 43 #if defined(CONFIG_NETMAP_PIPE) 44 #define WITH_PIPES 45 #endif 46 #if defined(CONFIG_NETMAP_MONITOR) 47 #define WITH_MONITOR 48 #endif 49 #if defined(CONFIG_NETMAP_GENERIC) 50 #define WITH_GENERIC 51 #endif 52 #if defined(CONFIG_NETMAP_PTNETMAP_GUEST) 53 #define WITH_PTNETMAP_GUEST 54 #endif 55 #if defined(CONFIG_NETMAP_PTNETMAP_HOST) 56 #define WITH_PTNETMAP_HOST 57 #endif 58 59 #elif defined (_WIN32) 60 #define WITH_VALE // comment out to disable VALE support 61 #define WITH_PIPES 62 #define WITH_MONITOR 63 #define WITH_GENERIC 64 65 #else /* neither linux nor windows */ 66 #define WITH_VALE // comment out to disable VALE support 67 #define WITH_PIPES 68 #define WITH_MONITOR 69 #define WITH_GENERIC 70 #define WITH_PTNETMAP_HOST /* ptnetmap host support */ 71 #define WITH_PTNETMAP_GUEST /* ptnetmap guest support */ 72 73 #endif 74 75 #if defined(__FreeBSD__) 76 77 #define likely(x) __builtin_expect((long)!!(x), 1L) 78 #define unlikely(x) __builtin_expect((long)!!(x), 0L) 79 #define __user 80 81 #define NM_LOCK_T struct mtx /* low level spinlock, used to protect queues */ 82 83 #define NM_MTX_T struct sx /* OS-specific mutex (sleepable) */ 84 #define NM_MTX_INIT(m) sx_init(&(m), #m) 85 #define NM_MTX_DESTROY(m) sx_destroy(&(m)) 86 #define NM_MTX_LOCK(m) sx_xlock(&(m)) 87 #define NM_MTX_UNLOCK(m) sx_xunlock(&(m)) 88 #define NM_MTX_ASSERT(m) sx_assert(&(m), SA_XLOCKED) 89 90 #define NM_SELINFO_T struct nm_selinfo 91 #define NM_SELRECORD_T struct thread 92 #define MBUF_LEN(m) ((m)->m_pkthdr.len) 93 #define MBUF_TXQ(m) ((m)->m_pkthdr.flowid) 94 #define MBUF_TRANSMIT(na, ifp, m) ((na)->if_transmit(ifp, m)) 95 #define GEN_TX_MBUF_IFP(m) ((m)->m_pkthdr.rcvif) 96 97 #define NM_ATOMIC_T volatile int // XXX ? 98 /* atomic operations */ 99 #include <machine/atomic.h> 100 #define NM_ATOMIC_TEST_AND_SET(p) (!atomic_cmpset_acq_int((p), 0, 1)) 101 #define NM_ATOMIC_CLEAR(p) atomic_store_rel_int((p), 0) 102 103 #if __FreeBSD_version >= 1100030 104 #define WNA(_ifp) (_ifp)->if_netmap 105 #else /* older FreeBSD */ 106 #define WNA(_ifp) (_ifp)->if_pspare[0] 107 #endif /* older FreeBSD */ 108 109 #if __FreeBSD_version >= 1100005 110 struct netmap_adapter *netmap_getna(if_t ifp); 111 #endif 112 113 #if __FreeBSD_version >= 1100027 114 #define MBUF_REFCNT(m) ((m)->m_ext.ext_count) 115 #define SET_MBUF_REFCNT(m, x) (m)->m_ext.ext_count = x 116 #else 117 #define MBUF_REFCNT(m) ((m)->m_ext.ref_cnt ? *((m)->m_ext.ref_cnt) : -1) 118 #define SET_MBUF_REFCNT(m, x) *((m)->m_ext.ref_cnt) = x 119 #endif 120 121 #define MBUF_QUEUED(m) 1 122 123 struct nm_selinfo { 124 struct selinfo si; 125 struct mtx m; 126 }; 127 128 129 // XXX linux struct, not used in FreeBSD 130 struct net_device_ops { 131 }; 132 struct ethtool_ops { 133 }; 134 struct hrtimer { 135 }; 136 #define NM_BNS_GET(b) 137 #define NM_BNS_PUT(b) 138 139 #elif defined (linux) 140 141 #define NM_LOCK_T safe_spinlock_t // see bsd_glue.h 142 #define NM_SELINFO_T wait_queue_head_t 143 #define MBUF_LEN(m) ((m)->len) 144 #define MBUF_TRANSMIT(na, ifp, m) \ 145 ({ \ 146 /* Avoid infinite recursion with generic. */ \ 147 m->priority = NM_MAGIC_PRIORITY_TX; \ 148 (((struct net_device_ops *)(na)->if_transmit)->ndo_start_xmit(m, ifp)); \ 149 0; \ 150 }) 151 152 /* See explanation in nm_os_generic_xmit_frame. */ 153 #define GEN_TX_MBUF_IFP(m) ((struct ifnet *)skb_shinfo(m)->destructor_arg) 154 155 #define NM_ATOMIC_T volatile long unsigned int 156 157 #define NM_MTX_T struct mutex /* OS-specific sleepable lock */ 158 #define NM_MTX_INIT(m) mutex_init(&(m)) 159 #define NM_MTX_DESTROY(m) do { (void)(m); } while (0) 160 #define NM_MTX_LOCK(m) mutex_lock(&(m)) 161 #define NM_MTX_UNLOCK(m) mutex_unlock(&(m)) 162 #define NM_MTX_ASSERT(m) mutex_is_locked(&(m)) 163 164 #ifndef DEV_NETMAP 165 #define DEV_NETMAP 166 #endif /* DEV_NETMAP */ 167 168 #elif defined (__APPLE__) 169 170 #warning apple support is incomplete. 171 #define likely(x) __builtin_expect(!!(x), 1) 172 #define unlikely(x) __builtin_expect(!!(x), 0) 173 #define NM_LOCK_T IOLock * 174 #define NM_SELINFO_T struct selinfo 175 #define MBUF_LEN(m) ((m)->m_pkthdr.len) 176 177 #elif defined (_WIN32) 178 #include "../../../WINDOWS/win_glue.h" 179 180 #define NM_SELRECORD_T IO_STACK_LOCATION 181 #define NM_SELINFO_T win_SELINFO // see win_glue.h 182 #define NM_LOCK_T win_spinlock_t // see win_glue.h 183 #define NM_MTX_T KGUARDED_MUTEX /* OS-specific mutex (sleepable) */ 184 185 #define NM_MTX_INIT(m) KeInitializeGuardedMutex(&m); 186 #define NM_MTX_DESTROY(m) do { (void)(m); } while (0) 187 #define NM_MTX_LOCK(m) KeAcquireGuardedMutex(&(m)) 188 #define NM_MTX_UNLOCK(m) KeReleaseGuardedMutex(&(m)) 189 #define NM_MTX_ASSERT(m) assert(&m.Count>0) 190 191 //These linknames are for the NDIS driver 192 #define NETMAP_NDIS_LINKNAME_STRING L"\\DosDevices\\NMAPNDIS" 193 #define NETMAP_NDIS_NTDEVICE_STRING L"\\Device\\NMAPNDIS" 194 195 //Definition of internal driver-to-driver ioctl codes 196 #define NETMAP_KERNEL_XCHANGE_POINTERS _IO('i', 180) 197 #define NETMAP_KERNEL_SEND_SHUTDOWN_SIGNAL _IO_direct('i', 195) 198 199 //Empty data structures are not permitted by MSVC compiler 200 //XXX_ale, try to solve this problem 201 struct net_device_ops{ 202 char data[1]; 203 }; 204 typedef struct ethtool_ops{ 205 char data[1]; 206 }; 207 typedef struct hrtimer{ 208 KTIMER timer; 209 BOOLEAN active; 210 KDPC deferred_proc; 211 }; 212 213 /* MSVC does not have likely/unlikely support */ 214 #ifdef _MSC_VER 215 #define likely(x) (x) 216 #define unlikely(x) (x) 217 #else 218 #define likely(x) __builtin_expect((long)!!(x), 1L) 219 #define unlikely(x) __builtin_expect((long)!!(x), 0L) 220 #endif //_MSC_VER 221 222 #else 223 224 #error unsupported platform 225 226 #endif /* end - platform-specific code */ 227 228 #ifndef _WIN32 /* support for emulated sysctl */ 229 #define SYSBEGIN(x) 230 #define SYSEND 231 #endif /* _WIN32 */ 232 233 #define NM_ACCESS_ONCE(x) (*(volatile __typeof__(x) *)&(x)) 234 235 #define NMG_LOCK_T NM_MTX_T 236 #define NMG_LOCK_INIT() NM_MTX_INIT(netmap_global_lock) 237 #define NMG_LOCK_DESTROY() NM_MTX_DESTROY(netmap_global_lock) 238 #define NMG_LOCK() NM_MTX_LOCK(netmap_global_lock) 239 #define NMG_UNLOCK() NM_MTX_UNLOCK(netmap_global_lock) 240 #define NMG_LOCK_ASSERT() NM_MTX_ASSERT(netmap_global_lock) 241 242 #define ND(format, ...) 243 #define D(format, ...) \ 244 do { \ 245 struct timeval __xxts; \ 246 microtime(&__xxts); \ 247 printf("%03d.%06d [%4d] %-25s " format "\n", \ 248 (int)__xxts.tv_sec % 1000, (int)__xxts.tv_usec, \ 249 __LINE__, __FUNCTION__, ##__VA_ARGS__); \ 250 } while (0) 251 252 /* rate limited, lps indicates how many per second */ 253 #define RD(lps, format, ...) \ 254 do { \ 255 static int t0, __cnt; \ 256 if (t0 != time_second) { \ 257 t0 = time_second; \ 258 __cnt = 0; \ 259 } \ 260 if (__cnt++ < lps) \ 261 D(format, ##__VA_ARGS__); \ 262 } while (0) 263 264 struct netmap_adapter; 265 struct nm_bdg_fwd; 266 struct nm_bridge; 267 struct netmap_priv_d; 268 269 /* os-specific NM_SELINFO_T initialzation/destruction functions */ 270 void nm_os_selinfo_init(NM_SELINFO_T *); 271 void nm_os_selinfo_uninit(NM_SELINFO_T *); 272 273 const char *nm_dump_buf(char *p, int len, int lim, char *dst); 274 275 void nm_os_selwakeup(NM_SELINFO_T *si); 276 void nm_os_selrecord(NM_SELRECORD_T *sr, NM_SELINFO_T *si); 277 278 int nm_os_ifnet_init(void); 279 void nm_os_ifnet_fini(void); 280 void nm_os_ifnet_lock(void); 281 void nm_os_ifnet_unlock(void); 282 283 void nm_os_get_module(void); 284 void nm_os_put_module(void); 285 286 void netmap_make_zombie(struct ifnet *); 287 void netmap_undo_zombie(struct ifnet *); 288 289 /* passes a packet up to the host stack. 290 * If the packet is sent (or dropped) immediately it returns NULL, 291 * otherwise it links the packet to prev and returns m. 292 * In this case, a final call with m=NULL and prev != NULL will send up 293 * the entire chain to the host stack. 294 */ 295 void *nm_os_send_up(struct ifnet *, struct mbuf *m, struct mbuf *prev); 296 297 int nm_os_mbuf_has_offld(struct mbuf *m); 298 299 #include "netmap_mbq.h" 300 301 extern NMG_LOCK_T netmap_global_lock; 302 303 enum txrx { NR_RX = 0, NR_TX = 1, NR_TXRX }; 304 305 static __inline const char* 306 nm_txrx2str(enum txrx t) 307 { 308 return (t== NR_RX ? "RX" : "TX"); 309 } 310 311 static __inline enum txrx 312 nm_txrx_swap(enum txrx t) 313 { 314 return (t== NR_RX ? NR_TX : NR_RX); 315 } 316 317 #define for_rx_tx(t) for ((t) = 0; (t) < NR_TXRX; (t)++) 318 319 320 /* 321 * private, kernel view of a ring. Keeps track of the status of 322 * a ring across system calls. 323 * 324 * nr_hwcur index of the next buffer to refill. 325 * It corresponds to ring->head 326 * at the time the system call returns. 327 * 328 * nr_hwtail index of the first buffer owned by the kernel. 329 * On RX, hwcur->hwtail are receive buffers 330 * not yet released. hwcur is advanced following 331 * ring->head, hwtail is advanced on incoming packets, 332 * and a wakeup is generated when hwtail passes ring->cur 333 * On TX, hwcur->rcur have been filled by the sender 334 * but not sent yet to the NIC; rcur->hwtail are available 335 * for new transmissions, and hwtail->hwcur-1 are pending 336 * transmissions not yet acknowledged. 337 * 338 * The indexes in the NIC and netmap rings are offset by nkr_hwofs slots. 339 * This is so that, on a reset, buffers owned by userspace are not 340 * modified by the kernel. In particular: 341 * RX rings: the next empty buffer (hwtail + hwofs) coincides with 342 * the next empty buffer as known by the hardware (next_to_check or so). 343 * TX rings: hwcur + hwofs coincides with next_to_send 344 * 345 * For received packets, slot->flags is set to nkr_slot_flags 346 * so we can provide a proper initial value (e.g. set NS_FORWARD 347 * when operating in 'transparent' mode). 348 * 349 * The following fields are used to implement lock-free copy of packets 350 * from input to output ports in VALE switch: 351 * nkr_hwlease buffer after the last one being copied. 352 * A writer in nm_bdg_flush reserves N buffers 353 * from nr_hwlease, advances it, then does the 354 * copy outside the lock. 355 * In RX rings (used for VALE ports), 356 * nkr_hwtail <= nkr_hwlease < nkr_hwcur+N-1 357 * In TX rings (used for NIC or host stack ports) 358 * nkr_hwcur <= nkr_hwlease < nkr_hwtail 359 * nkr_leases array of nkr_num_slots where writers can report 360 * completion of their block. NR_NOSLOT (~0) indicates 361 * that the writer has not finished yet 362 * nkr_lease_idx index of next free slot in nr_leases, to be assigned 363 * 364 * The kring is manipulated by txsync/rxsync and generic netmap function. 365 * 366 * Concurrent rxsync or txsync on the same ring are prevented through 367 * by nm_kr_(try)lock() which in turn uses nr_busy. This is all we need 368 * for NIC rings, and for TX rings attached to the host stack. 369 * 370 * RX rings attached to the host stack use an mbq (rx_queue) on both 371 * rxsync_from_host() and netmap_transmit(). The mbq is protected 372 * by its internal lock. 373 * 374 * RX rings attached to the VALE switch are accessed by both senders 375 * and receiver. They are protected through the q_lock on the RX ring. 376 */ 377 struct netmap_kring { 378 struct netmap_ring *ring; 379 380 uint32_t nr_hwcur; 381 uint32_t nr_hwtail; 382 383 /* 384 * Copies of values in user rings, so we do not need to look 385 * at the ring (which could be modified). These are set in the 386 * *sync_prologue()/finalize() routines. 387 */ 388 uint32_t rhead; 389 uint32_t rcur; 390 uint32_t rtail; 391 392 uint32_t nr_kflags; /* private driver flags */ 393 #define NKR_PENDINTR 0x1 // Pending interrupt. 394 #define NKR_EXCLUSIVE 0x2 /* exclusive binding */ 395 #define NKR_FORWARD 0x4 /* (host ring only) there are 396 packets to forward 397 */ 398 #define NKR_NEEDRING 0x8 /* ring needed even if users==0 399 * (used internally by pipes and 400 * by ptnetmap host ports) 401 */ 402 403 uint32_t nr_mode; 404 uint32_t nr_pending_mode; 405 #define NKR_NETMAP_OFF 0x0 406 #define NKR_NETMAP_ON 0x1 407 408 uint32_t nkr_num_slots; 409 410 /* 411 * On a NIC reset, the NIC ring indexes may be reset but the 412 * indexes in the netmap rings remain the same. nkr_hwofs 413 * keeps track of the offset between the two. 414 */ 415 int32_t nkr_hwofs; 416 417 uint16_t nkr_slot_flags; /* initial value for flags */ 418 419 /* last_reclaim is opaque marker to help reduce the frequency 420 * of operations such as reclaiming tx buffers. A possible use 421 * is set it to ticks and do the reclaim only once per tick. 422 */ 423 uint64_t last_reclaim; 424 425 426 NM_SELINFO_T si; /* poll/select wait queue */ 427 NM_LOCK_T q_lock; /* protects kring and ring. */ 428 NM_ATOMIC_T nr_busy; /* prevent concurrent syscalls */ 429 430 struct netmap_adapter *na; 431 432 /* The following fields are for VALE switch support */ 433 struct nm_bdg_fwd *nkr_ft; 434 uint32_t *nkr_leases; 435 #define NR_NOSLOT ((uint32_t)~0) /* used in nkr_*lease* */ 436 uint32_t nkr_hwlease; 437 uint32_t nkr_lease_idx; 438 439 /* while nkr_stopped is set, no new [tr]xsync operations can 440 * be started on this kring. 441 * This is used by netmap_disable_all_rings() 442 * to find a synchronization point where critical data 443 * structures pointed to by the kring can be added or removed 444 */ 445 volatile int nkr_stopped; 446 447 /* Support for adapters without native netmap support. 448 * On tx rings we preallocate an array of tx buffers 449 * (same size as the netmap ring), on rx rings we 450 * store incoming mbufs in a queue that is drained by 451 * a rxsync. 452 */ 453 struct mbuf **tx_pool; 454 struct mbuf *tx_event; /* TX event used as a notification */ 455 NM_LOCK_T tx_event_lock; /* protects the tx_event mbuf */ 456 struct mbq rx_queue; /* intercepted rx mbufs. */ 457 458 uint32_t users; /* existing bindings for this ring */ 459 460 uint32_t ring_id; /* kring identifier */ 461 enum txrx tx; /* kind of ring (tx or rx) */ 462 char name[64]; /* diagnostic */ 463 464 /* [tx]sync callback for this kring. 465 * The default nm_kring_create callback (netmap_krings_create) 466 * sets the nm_sync callback of each hardware tx(rx) kring to 467 * the corresponding nm_txsync(nm_rxsync) taken from the 468 * netmap_adapter; moreover, it sets the sync callback 469 * of the host tx(rx) ring to netmap_txsync_to_host 470 * (netmap_rxsync_from_host). 471 * 472 * Overrides: the above configuration is not changed by 473 * any of the nm_krings_create callbacks. 474 */ 475 int (*nm_sync)(struct netmap_kring *kring, int flags); 476 int (*nm_notify)(struct netmap_kring *kring, int flags); 477 478 #ifdef WITH_PIPES 479 struct netmap_kring *pipe; /* if this is a pipe ring, 480 * pointer to the other end 481 */ 482 #endif /* WITH_PIPES */ 483 484 #ifdef WITH_VALE 485 int (*save_notify)(struct netmap_kring *kring, int flags); 486 #endif 487 488 #ifdef WITH_MONITOR 489 /* array of krings that are monitoring this kring */ 490 struct netmap_kring **monitors; 491 uint32_t max_monitors; /* current size of the monitors array */ 492 uint32_t n_monitors; /* next unused entry in the monitor array */ 493 /* 494 * Monitors work by intercepting the sync and notify callbacks of the 495 * monitored krings. This is implemented by replacing the pointers 496 * above and saving the previous ones in mon_* pointers below 497 */ 498 int (*mon_sync)(struct netmap_kring *kring, int flags); 499 int (*mon_notify)(struct netmap_kring *kring, int flags); 500 501 uint32_t mon_tail; /* last seen slot on rx */ 502 uint32_t mon_pos; /* index of this ring in the monitored ring array */ 503 #endif 504 } 505 #ifdef _WIN32 506 __declspec(align(64)); 507 #else 508 __attribute__((__aligned__(64))); 509 #endif 510 511 /* return 1 iff the kring needs to be turned on */ 512 static inline int 513 nm_kring_pending_on(struct netmap_kring *kring) 514 { 515 return kring->nr_pending_mode == NKR_NETMAP_ON && 516 kring->nr_mode == NKR_NETMAP_OFF; 517 } 518 519 /* return 1 iff the kring needs to be turned off */ 520 static inline int 521 nm_kring_pending_off(struct netmap_kring *kring) 522 { 523 return kring->nr_pending_mode == NKR_NETMAP_OFF && 524 kring->nr_mode == NKR_NETMAP_ON; 525 } 526 527 /* return the next index, with wraparound */ 528 static inline uint32_t 529 nm_next(uint32_t i, uint32_t lim) 530 { 531 return unlikely (i == lim) ? 0 : i + 1; 532 } 533 534 535 /* return the previous index, with wraparound */ 536 static inline uint32_t 537 nm_prev(uint32_t i, uint32_t lim) 538 { 539 return unlikely (i == 0) ? lim : i - 1; 540 } 541 542 543 /* 544 * 545 * Here is the layout for the Rx and Tx rings. 546 547 RxRING TxRING 548 549 +-----------------+ +-----------------+ 550 | | | | 551 |XXX free slot XXX| |XXX free slot XXX| 552 +-----------------+ +-----------------+ 553 head->| owned by user |<-hwcur | not sent to nic |<-hwcur 554 | | | yet | 555 +-----------------+ | | 556 cur->| available to | | | 557 | user, not read | +-----------------+ 558 | yet | cur->| (being | 559 | | | prepared) | 560 | | | | 561 +-----------------+ + ------ + 562 tail->| |<-hwtail | |<-hwlease 563 | (being | ... | | ... 564 | prepared) | ... | | ... 565 +-----------------+ ... | | ... 566 | |<-hwlease +-----------------+ 567 | | tail->| |<-hwtail 568 | | | | 569 | | | | 570 | | | | 571 +-----------------+ +-----------------+ 572 573 * The cur/tail (user view) and hwcur/hwtail (kernel view) 574 * are used in the normal operation of the card. 575 * 576 * When a ring is the output of a switch port (Rx ring for 577 * a VALE port, Tx ring for the host stack or NIC), slots 578 * are reserved in blocks through 'hwlease' which points 579 * to the next unused slot. 580 * On an Rx ring, hwlease is always after hwtail, 581 * and completions cause hwtail to advance. 582 * On a Tx ring, hwlease is always between cur and hwtail, 583 * and completions cause cur to advance. 584 * 585 * nm_kr_space() returns the maximum number of slots that 586 * can be assigned. 587 * nm_kr_lease() reserves the required number of buffers, 588 * advances nkr_hwlease and also returns an entry in 589 * a circular array where completions should be reported. 590 */ 591 592 593 struct netmap_lut { 594 struct lut_entry *lut; 595 uint32_t objtotal; /* max buffer index */ 596 uint32_t objsize; /* buffer size */ 597 }; 598 599 struct netmap_vp_adapter; // forward 600 601 /* 602 * The "struct netmap_adapter" extends the "struct adapter" 603 * (or equivalent) device descriptor. 604 * It contains all base fields needed to support netmap operation. 605 * There are in fact different types of netmap adapters 606 * (native, generic, VALE switch...) so a netmap_adapter is 607 * just the first field in the derived type. 608 */ 609 struct netmap_adapter { 610 /* 611 * On linux we do not have a good way to tell if an interface 612 * is netmap-capable. So we always use the following trick: 613 * NA(ifp) points here, and the first entry (which hopefully 614 * always exists and is at least 32 bits) contains a magic 615 * value which we can use to detect that the interface is good. 616 */ 617 uint32_t magic; 618 uint32_t na_flags; /* enabled, and other flags */ 619 #define NAF_SKIP_INTR 1 /* use the regular interrupt handler. 620 * useful during initialization 621 */ 622 #define NAF_SW_ONLY 2 /* forward packets only to sw adapter */ 623 #define NAF_BDG_MAYSLEEP 4 /* the bridge is allowed to sleep when 624 * forwarding packets coming from this 625 * interface 626 */ 627 #define NAF_MEM_OWNER 8 /* the adapter uses its own memory area 628 * that cannot be changed 629 */ 630 #define NAF_NATIVE 16 /* the adapter is native. 631 * Virtual ports (non persistent vale ports, 632 * pipes, monitors...) should never use 633 * this flag. 634 */ 635 #define NAF_NETMAP_ON 32 /* netmap is active (either native or 636 * emulated). Where possible (e.g. FreeBSD) 637 * IFCAP_NETMAP also mirrors this flag. 638 */ 639 #define NAF_HOST_RINGS 64 /* the adapter supports the host rings */ 640 #define NAF_FORCE_NATIVE 128 /* the adapter is always NATIVE */ 641 #define NAF_PTNETMAP_HOST 256 /* the adapter supports ptnetmap in the host */ 642 #define NAF_ZOMBIE (1U<<30) /* the nic driver has been unloaded */ 643 #define NAF_BUSY (1U<<31) /* the adapter is used internally and 644 * cannot be registered from userspace 645 */ 646 int active_fds; /* number of user-space descriptors using this 647 interface, which is equal to the number of 648 struct netmap_if objs in the mapped region. */ 649 650 u_int num_rx_rings; /* number of adapter receive rings */ 651 u_int num_tx_rings; /* number of adapter transmit rings */ 652 653 u_int num_tx_desc; /* number of descriptor in each queue */ 654 u_int num_rx_desc; 655 656 /* tx_rings and rx_rings are private but allocated 657 * as a contiguous chunk of memory. Each array has 658 * N+1 entries, for the adapter queues and for the host queue. 659 */ 660 struct netmap_kring *tx_rings; /* array of TX rings. */ 661 struct netmap_kring *rx_rings; /* array of RX rings. */ 662 663 void *tailroom; /* space below the rings array */ 664 /* (used for leases) */ 665 666 667 NM_SELINFO_T si[NR_TXRX]; /* global wait queues */ 668 669 /* count users of the global wait queues */ 670 int si_users[NR_TXRX]; 671 672 void *pdev; /* used to store pci device */ 673 674 /* copy of if_qflush and if_transmit pointers, to intercept 675 * packets from the network stack when netmap is active. 676 */ 677 int (*if_transmit)(struct ifnet *, struct mbuf *); 678 679 /* copy of if_input for netmap_send_up() */ 680 void (*if_input)(struct ifnet *, struct mbuf *); 681 682 /* references to the ifnet and device routines, used by 683 * the generic netmap functions. 684 */ 685 struct ifnet *ifp; /* adapter is ifp->if_softc */ 686 687 /*---- callbacks for this netmap adapter -----*/ 688 /* 689 * nm_dtor() is the cleanup routine called when destroying 690 * the adapter. 691 * Called with NMG_LOCK held. 692 * 693 * nm_register() is called on NIOCREGIF and close() to enter 694 * or exit netmap mode on the NIC 695 * Called with NNG_LOCK held. 696 * 697 * nm_txsync() pushes packets to the underlying hw/switch 698 * 699 * nm_rxsync() collects packets from the underlying hw/switch 700 * 701 * nm_config() returns configuration information from the OS 702 * Called with NMG_LOCK held. 703 * 704 * nm_krings_create() create and init the tx_rings and 705 * rx_rings arrays of kring structures. In particular, 706 * set the nm_sync callbacks for each ring. 707 * There is no need to also allocate the corresponding 708 * netmap_rings, since netmap_mem_rings_create() will always 709 * be called to provide the missing ones. 710 * Called with NNG_LOCK held. 711 * 712 * nm_krings_delete() cleanup and delete the tx_rings and rx_rings 713 * arrays 714 * Called with NMG_LOCK held. 715 * 716 * nm_notify() is used to act after data have become available 717 * (or the stopped state of the ring has changed) 718 * For hw devices this is typically a selwakeup(), 719 * but for NIC/host ports attached to a switch (or vice-versa) 720 * we also need to invoke the 'txsync' code downstream. 721 * This callback pointer is actually used only to initialize 722 * kring->nm_notify. 723 * Return values are the same as for netmap_rx_irq(). 724 */ 725 void (*nm_dtor)(struct netmap_adapter *); 726 727 int (*nm_register)(struct netmap_adapter *, int onoff); 728 void (*nm_intr)(struct netmap_adapter *, int onoff); 729 730 int (*nm_txsync)(struct netmap_kring *kring, int flags); 731 int (*nm_rxsync)(struct netmap_kring *kring, int flags); 732 int (*nm_notify)(struct netmap_kring *kring, int flags); 733 #define NAF_FORCE_READ 1 734 #define NAF_FORCE_RECLAIM 2 735 /* return configuration information */ 736 int (*nm_config)(struct netmap_adapter *, 737 u_int *txr, u_int *txd, u_int *rxr, u_int *rxd); 738 int (*nm_krings_create)(struct netmap_adapter *); 739 void (*nm_krings_delete)(struct netmap_adapter *); 740 #ifdef WITH_VALE 741 /* 742 * nm_bdg_attach() initializes the na_vp field to point 743 * to an adapter that can be attached to a VALE switch. If the 744 * current adapter is already a VALE port, na_vp is simply a cast; 745 * otherwise, na_vp points to a netmap_bwrap_adapter. 746 * If applicable, this callback also initializes na_hostvp, 747 * that can be used to connect the adapter host rings to the 748 * switch. 749 * Called with NMG_LOCK held. 750 * 751 * nm_bdg_ctl() is called on the actual attach/detach to/from 752 * to/from the switch, to perform adapter-specific 753 * initializations 754 * Called with NMG_LOCK held. 755 */ 756 int (*nm_bdg_attach)(const char *bdg_name, struct netmap_adapter *); 757 int (*nm_bdg_ctl)(struct netmap_adapter *, struct nmreq *, int); 758 759 /* adapter used to attach this adapter to a VALE switch (if any) */ 760 struct netmap_vp_adapter *na_vp; 761 /* adapter used to attach the host rings of this adapter 762 * to a VALE switch (if any) */ 763 struct netmap_vp_adapter *na_hostvp; 764 #endif 765 766 /* standard refcount to control the lifetime of the adapter 767 * (it should be equal to the lifetime of the corresponding ifp) 768 */ 769 int na_refcount; 770 771 /* memory allocator (opaque) 772 * We also cache a pointer to the lut_entry for translating 773 * buffer addresses, the total number of buffers and the buffer size. 774 */ 775 struct netmap_mem_d *nm_mem; 776 struct netmap_lut na_lut; 777 778 /* additional information attached to this adapter 779 * by other netmap subsystems. Currently used by 780 * bwrap, LINUX/v1000 and ptnetmap 781 */ 782 void *na_private; 783 784 /* array of pipes that have this adapter as a parent */ 785 struct netmap_pipe_adapter **na_pipes; 786 int na_next_pipe; /* next free slot in the array */ 787 int na_max_pipes; /* size of the array */ 788 789 /* Offset of ethernet header for each packet. */ 790 u_int virt_hdr_len; 791 792 char name[64]; 793 }; 794 795 static __inline u_int 796 nma_get_ndesc(struct netmap_adapter *na, enum txrx t) 797 { 798 return (t == NR_TX ? na->num_tx_desc : na->num_rx_desc); 799 } 800 801 static __inline void 802 nma_set_ndesc(struct netmap_adapter *na, enum txrx t, u_int v) 803 { 804 if (t == NR_TX) 805 na->num_tx_desc = v; 806 else 807 na->num_rx_desc = v; 808 } 809 810 static __inline u_int 811 nma_get_nrings(struct netmap_adapter *na, enum txrx t) 812 { 813 return (t == NR_TX ? na->num_tx_rings : na->num_rx_rings); 814 } 815 816 static __inline void 817 nma_set_nrings(struct netmap_adapter *na, enum txrx t, u_int v) 818 { 819 if (t == NR_TX) 820 na->num_tx_rings = v; 821 else 822 na->num_rx_rings = v; 823 } 824 825 static __inline struct netmap_kring* 826 NMR(struct netmap_adapter *na, enum txrx t) 827 { 828 return (t == NR_TX ? na->tx_rings : na->rx_rings); 829 } 830 831 /* 832 * If the NIC is owned by the kernel 833 * (i.e., bridge), neither another bridge nor user can use it; 834 * if the NIC is owned by a user, only users can share it. 835 * Evaluation must be done under NMG_LOCK(). 836 */ 837 #define NETMAP_OWNED_BY_KERN(na) ((na)->na_flags & NAF_BUSY) 838 #define NETMAP_OWNED_BY_ANY(na) \ 839 (NETMAP_OWNED_BY_KERN(na) || ((na)->active_fds > 0)) 840 841 /* 842 * derived netmap adapters for various types of ports 843 */ 844 struct netmap_vp_adapter { /* VALE software port */ 845 struct netmap_adapter up; 846 847 /* 848 * Bridge support: 849 * 850 * bdg_port is the port number used in the bridge; 851 * na_bdg points to the bridge this NA is attached to. 852 */ 853 int bdg_port; 854 struct nm_bridge *na_bdg; 855 int retry; 856 857 /* Maximum Frame Size, used in bdg_mismatch_datapath() */ 858 u_int mfs; 859 /* Last source MAC on this port */ 860 uint64_t last_smac; 861 }; 862 863 864 struct netmap_hw_adapter { /* physical device */ 865 struct netmap_adapter up; 866 867 struct net_device_ops nm_ndo; // XXX linux only 868 struct ethtool_ops nm_eto; // XXX linux only 869 const struct ethtool_ops* save_ethtool; 870 871 int (*nm_hw_register)(struct netmap_adapter *, int onoff); 872 }; 873 874 #ifdef WITH_GENERIC 875 /* Mitigation support. */ 876 struct nm_generic_mit { 877 struct hrtimer mit_timer; 878 int mit_pending; 879 int mit_ring_idx; /* index of the ring being mitigated */ 880 struct netmap_adapter *mit_na; /* backpointer */ 881 }; 882 883 struct netmap_generic_adapter { /* emulated device */ 884 struct netmap_hw_adapter up; 885 886 /* Pointer to a previously used netmap adapter. */ 887 struct netmap_adapter *prev; 888 889 /* generic netmap adapters support: 890 * a net_device_ops struct overrides ndo_select_queue(), 891 * save_if_input saves the if_input hook (FreeBSD), 892 * mit implements rx interrupt mitigation, 893 */ 894 struct net_device_ops generic_ndo; 895 void (*save_if_input)(struct ifnet *, struct mbuf *); 896 897 struct nm_generic_mit *mit; 898 #ifdef linux 899 netdev_tx_t (*save_start_xmit)(struct mbuf *, struct ifnet *); 900 #endif 901 /* Is the adapter able to use multiple RX slots to scatter 902 * each packet pushed up by the driver? */ 903 int rxsg; 904 905 /* Is the transmission path controlled by a netmap-aware 906 * device queue (i.e. qdisc on linux)? */ 907 int txqdisc; 908 }; 909 #endif /* WITH_GENERIC */ 910 911 static __inline int 912 netmap_real_rings(struct netmap_adapter *na, enum txrx t) 913 { 914 return nma_get_nrings(na, t) + !!(na->na_flags & NAF_HOST_RINGS); 915 } 916 917 #ifdef WITH_VALE 918 struct nm_bdg_polling_state; 919 /* 920 * Bridge wrapper for non VALE ports attached to a VALE switch. 921 * 922 * The real device must already have its own netmap adapter (hwna). 923 * The bridge wrapper and the hwna adapter share the same set of 924 * netmap rings and buffers, but they have two separate sets of 925 * krings descriptors, with tx/rx meanings swapped: 926 * 927 * netmap 928 * bwrap krings rings krings hwna 929 * +------+ +------+ +-----+ +------+ +------+ 930 * |tx_rings->| |\ /| |----| |<-tx_rings| 931 * | | +------+ \ / +-----+ +------+ | | 932 * | | X | | 933 * | | / \ | | 934 * | | +------+/ \+-----+ +------+ | | 935 * |rx_rings->| | | |----| |<-rx_rings| 936 * | | +------+ +-----+ +------+ | | 937 * +------+ +------+ 938 * 939 * - packets coming from the bridge go to the brwap rx rings, 940 * which are also the hwna tx rings. The bwrap notify callback 941 * will then complete the hwna tx (see netmap_bwrap_notify). 942 * 943 * - packets coming from the outside go to the hwna rx rings, 944 * which are also the bwrap tx rings. The (overwritten) hwna 945 * notify method will then complete the bridge tx 946 * (see netmap_bwrap_intr_notify). 947 * 948 * The bridge wrapper may optionally connect the hwna 'host' rings 949 * to the bridge. This is done by using a second port in the 950 * bridge and connecting it to the 'host' netmap_vp_adapter 951 * contained in the netmap_bwrap_adapter. The brwap host adapter 952 * cross-links the hwna host rings in the same way as shown above. 953 * 954 * - packets coming from the bridge and directed to the host stack 955 * are handled by the bwrap host notify callback 956 * (see netmap_bwrap_host_notify) 957 * 958 * - packets coming from the host stack are still handled by the 959 * overwritten hwna notify callback (netmap_bwrap_intr_notify), 960 * but are diverted to the host adapter depending on the ring number. 961 * 962 */ 963 struct netmap_bwrap_adapter { 964 struct netmap_vp_adapter up; 965 struct netmap_vp_adapter host; /* for host rings */ 966 struct netmap_adapter *hwna; /* the underlying device */ 967 968 /* 969 * When we attach a physical interface to the bridge, we 970 * allow the controlling process to terminate, so we need 971 * a place to store the n_detmap_priv_d data structure. 972 * This is only done when physical interfaces 973 * are attached to a bridge. 974 */ 975 struct netmap_priv_d *na_kpriv; 976 struct nm_bdg_polling_state *na_polling_state; 977 }; 978 int netmap_bwrap_attach(const char *name, struct netmap_adapter *); 979 980 #endif /* WITH_VALE */ 981 982 #ifdef WITH_PIPES 983 984 #define NM_MAXPIPES 64 /* max number of pipes per adapter */ 985 986 struct netmap_pipe_adapter { 987 struct netmap_adapter up; 988 989 u_int id; /* pipe identifier */ 990 int role; /* either NR_REG_PIPE_MASTER or NR_REG_PIPE_SLAVE */ 991 992 struct netmap_adapter *parent; /* adapter that owns the memory */ 993 struct netmap_pipe_adapter *peer; /* the other end of the pipe */ 994 int peer_ref; /* 1 iff we are holding a ref to the peer */ 995 996 u_int parent_slot; /* index in the parent pipe array */ 997 }; 998 999 #endif /* WITH_PIPES */ 1000 1001 1002 /* return slots reserved to rx clients; used in drivers */ 1003 static inline uint32_t 1004 nm_kr_rxspace(struct netmap_kring *k) 1005 { 1006 int space = k->nr_hwtail - k->nr_hwcur; 1007 if (space < 0) 1008 space += k->nkr_num_slots; 1009 ND("preserving %d rx slots %d -> %d", space, k->nr_hwcur, k->nr_hwtail); 1010 1011 return space; 1012 } 1013 1014 /* return slots reserved to tx clients */ 1015 #define nm_kr_txspace(_k) nm_kr_rxspace(_k) 1016 1017 1018 /* True if no space in the tx ring, only valid after txsync_prologue */ 1019 static inline int 1020 nm_kr_txempty(struct netmap_kring *kring) 1021 { 1022 return kring->rcur == kring->nr_hwtail; 1023 } 1024 1025 /* True if no more completed slots in the rx ring, only valid after 1026 * rxsync_prologue */ 1027 #define nm_kr_rxempty(_k) nm_kr_txempty(_k) 1028 1029 /* 1030 * protect against multiple threads using the same ring. 1031 * also check that the ring has not been stopped or locked 1032 */ 1033 #define NM_KR_BUSY 1 /* some other thread is syncing the ring */ 1034 #define NM_KR_STOPPED 2 /* unbounded stop (ifconfig down or driver unload) */ 1035 #define NM_KR_LOCKED 3 /* bounded, brief stop for mutual exclusion */ 1036 1037 1038 /* release the previously acquired right to use the *sync() methods of the ring */ 1039 static __inline void nm_kr_put(struct netmap_kring *kr) 1040 { 1041 NM_ATOMIC_CLEAR(&kr->nr_busy); 1042 } 1043 1044 1045 /* true if the ifp that backed the adapter has disappeared (e.g., the 1046 * driver has been unloaded) 1047 */ 1048 static inline int nm_iszombie(struct netmap_adapter *na); 1049 1050 /* try to obtain exclusive right to issue the *sync() operations on the ring. 1051 * The right is obtained and must be later relinquished via nm_kr_put() if and 1052 * only if nm_kr_tryget() returns 0. 1053 * If can_sleep is 1 there are only two other possible outcomes: 1054 * - the function returns NM_KR_BUSY 1055 * - the function returns NM_KR_STOPPED and sets the POLLERR bit in *perr 1056 * (if non-null) 1057 * In both cases the caller will typically skip the ring, possibly collecting 1058 * errors along the way. 1059 * If the calling context does not allow sleeping, the caller must pass 0 in can_sleep. 1060 * In the latter case, the function may also return NM_KR_LOCKED and leave *perr 1061 * untouched: ideally, the caller should try again at a later time. 1062 */ 1063 static __inline int nm_kr_tryget(struct netmap_kring *kr, int can_sleep, int *perr) 1064 { 1065 int busy = 1, stopped; 1066 /* check a first time without taking the lock 1067 * to avoid starvation for nm_kr_get() 1068 */ 1069 retry: 1070 stopped = kr->nkr_stopped; 1071 if (unlikely(stopped)) { 1072 goto stop; 1073 } 1074 busy = NM_ATOMIC_TEST_AND_SET(&kr->nr_busy); 1075 /* we should not return NM_KR_BUSY if the ring was 1076 * actually stopped, so check another time after 1077 * the barrier provided by the atomic operation 1078 */ 1079 stopped = kr->nkr_stopped; 1080 if (unlikely(stopped)) { 1081 goto stop; 1082 } 1083 1084 if (unlikely(nm_iszombie(kr->na))) { 1085 stopped = NM_KR_STOPPED; 1086 goto stop; 1087 } 1088 1089 return unlikely(busy) ? NM_KR_BUSY : 0; 1090 1091 stop: 1092 if (!busy) 1093 nm_kr_put(kr); 1094 if (stopped == NM_KR_STOPPED) { 1095 /* if POLLERR is defined we want to use it to simplify netmap_poll(). 1096 * Otherwise, any non-zero value will do. 1097 */ 1098 #ifdef POLLERR 1099 #define NM_POLLERR POLLERR 1100 #else 1101 #define NM_POLLERR 1 1102 #endif /* POLLERR */ 1103 if (perr) 1104 *perr |= NM_POLLERR; 1105 #undef NM_POLLERR 1106 } else if (can_sleep) { 1107 tsleep(kr, 0, "NM_KR_TRYGET", 4); 1108 goto retry; 1109 } 1110 return stopped; 1111 } 1112 1113 /* put the ring in the 'stopped' state and wait for the current user (if any) to 1114 * notice. stopped must be either NM_KR_STOPPED or NM_KR_LOCKED 1115 */ 1116 static __inline void nm_kr_stop(struct netmap_kring *kr, int stopped) 1117 { 1118 kr->nkr_stopped = stopped; 1119 while (NM_ATOMIC_TEST_AND_SET(&kr->nr_busy)) 1120 tsleep(kr, 0, "NM_KR_GET", 4); 1121 } 1122 1123 /* restart a ring after a stop */ 1124 static __inline void nm_kr_start(struct netmap_kring *kr) 1125 { 1126 kr->nkr_stopped = 0; 1127 nm_kr_put(kr); 1128 } 1129 1130 1131 /* 1132 * The following functions are used by individual drivers to 1133 * support netmap operation. 1134 * 1135 * netmap_attach() initializes a struct netmap_adapter, allocating the 1136 * struct netmap_ring's and the struct selinfo. 1137 * 1138 * netmap_detach() frees the memory allocated by netmap_attach(). 1139 * 1140 * netmap_transmit() replaces the if_transmit routine of the interface, 1141 * and is used to intercept packets coming from the stack. 1142 * 1143 * netmap_load_map/netmap_reload_map are helper routines to set/reset 1144 * the dmamap for a packet buffer 1145 * 1146 * netmap_reset() is a helper routine to be called in the hw driver 1147 * when reinitializing a ring. It should not be called by 1148 * virtual ports (vale, pipes, monitor) 1149 */ 1150 int netmap_attach(struct netmap_adapter *); 1151 void netmap_detach(struct ifnet *); 1152 int netmap_transmit(struct ifnet *, struct mbuf *); 1153 struct netmap_slot *netmap_reset(struct netmap_adapter *na, 1154 enum txrx tx, u_int n, u_int new_cur); 1155 int netmap_ring_reinit(struct netmap_kring *); 1156 1157 /* Return codes for netmap_*x_irq. */ 1158 enum { 1159 /* Driver should do normal interrupt processing, e.g. because 1160 * the interface is not in netmap mode. */ 1161 NM_IRQ_PASS = 0, 1162 /* Port is in netmap mode, and the interrupt work has been 1163 * completed. The driver does not have to notify netmap 1164 * again before the next interrupt. */ 1165 NM_IRQ_COMPLETED = -1, 1166 /* Port is in netmap mode, but the interrupt work has not been 1167 * completed. The driver has to make sure netmap will be 1168 * notified again soon, even if no more interrupts come (e.g. 1169 * on Linux the driver should not call napi_complete()). */ 1170 NM_IRQ_RESCHED = -2, 1171 }; 1172 1173 /* default functions to handle rx/tx interrupts */ 1174 int netmap_rx_irq(struct ifnet *, u_int, u_int *); 1175 #define netmap_tx_irq(_n, _q) netmap_rx_irq(_n, _q, NULL) 1176 int netmap_common_irq(struct netmap_adapter *, u_int, u_int *work_done); 1177 1178 1179 #ifdef WITH_VALE 1180 /* functions used by external modules to interface with VALE */ 1181 #define netmap_vp_to_ifp(_vp) ((_vp)->up.ifp) 1182 #define netmap_ifp_to_vp(_ifp) (NA(_ifp)->na_vp) 1183 #define netmap_ifp_to_host_vp(_ifp) (NA(_ifp)->na_hostvp) 1184 #define netmap_bdg_idx(_vp) ((_vp)->bdg_port) 1185 const char *netmap_bdg_name(struct netmap_vp_adapter *); 1186 #else /* !WITH_VALE */ 1187 #define netmap_vp_to_ifp(_vp) NULL 1188 #define netmap_ifp_to_vp(_ifp) NULL 1189 #define netmap_ifp_to_host_vp(_ifp) NULL 1190 #define netmap_bdg_idx(_vp) -1 1191 #define netmap_bdg_name(_vp) NULL 1192 #endif /* WITH_VALE */ 1193 1194 static inline int 1195 nm_netmap_on(struct netmap_adapter *na) 1196 { 1197 return na && na->na_flags & NAF_NETMAP_ON; 1198 } 1199 1200 static inline int 1201 nm_native_on(struct netmap_adapter *na) 1202 { 1203 return nm_netmap_on(na) && (na->na_flags & NAF_NATIVE); 1204 } 1205 1206 static inline int 1207 nm_iszombie(struct netmap_adapter *na) 1208 { 1209 return na == NULL || (na->na_flags & NAF_ZOMBIE); 1210 } 1211 1212 static inline void 1213 nm_update_hostrings_mode(struct netmap_adapter *na) 1214 { 1215 /* Process nr_mode and nr_pending_mode for host rings. */ 1216 na->tx_rings[na->num_tx_rings].nr_mode = 1217 na->tx_rings[na->num_tx_rings].nr_pending_mode; 1218 na->rx_rings[na->num_rx_rings].nr_mode = 1219 na->rx_rings[na->num_rx_rings].nr_pending_mode; 1220 } 1221 1222 /* set/clear native flags and if_transmit/netdev_ops */ 1223 static inline void 1224 nm_set_native_flags(struct netmap_adapter *na) 1225 { 1226 struct ifnet *ifp = na->ifp; 1227 1228 /* We do the setup for intercepting packets only if we are the 1229 * first user of this adapapter. */ 1230 if (na->active_fds > 0) { 1231 return; 1232 } 1233 1234 na->na_flags |= NAF_NETMAP_ON; 1235 #ifdef IFCAP_NETMAP /* or FreeBSD ? */ 1236 ifp->if_capenable |= IFCAP_NETMAP; 1237 #endif 1238 #if defined (__FreeBSD__) 1239 na->if_transmit = ifp->if_transmit; 1240 ifp->if_transmit = netmap_transmit; 1241 #elif defined (_WIN32) 1242 (void)ifp; /* prevent a warning */ 1243 //XXX_ale can we just comment those? 1244 //na->if_transmit = ifp->if_transmit; 1245 //ifp->if_transmit = netmap_transmit; 1246 #else 1247 na->if_transmit = (void *)ifp->netdev_ops; 1248 ifp->netdev_ops = &((struct netmap_hw_adapter *)na)->nm_ndo; 1249 ((struct netmap_hw_adapter *)na)->save_ethtool = ifp->ethtool_ops; 1250 ifp->ethtool_ops = &((struct netmap_hw_adapter*)na)->nm_eto; 1251 #endif 1252 nm_update_hostrings_mode(na); 1253 } 1254 1255 static inline void 1256 nm_clear_native_flags(struct netmap_adapter *na) 1257 { 1258 struct ifnet *ifp = na->ifp; 1259 1260 /* We undo the setup for intercepting packets only if we are the 1261 * last user of this adapapter. */ 1262 if (na->active_fds > 0) { 1263 return; 1264 } 1265 1266 nm_update_hostrings_mode(na); 1267 1268 #if defined(__FreeBSD__) 1269 ifp->if_transmit = na->if_transmit; 1270 #elif defined(_WIN32) 1271 (void)ifp; /* prevent a warning */ 1272 //XXX_ale can we just comment those? 1273 //ifp->if_transmit = na->if_transmit; 1274 #else 1275 ifp->netdev_ops = (void *)na->if_transmit; 1276 ifp->ethtool_ops = ((struct netmap_hw_adapter*)na)->save_ethtool; 1277 #endif 1278 na->na_flags &= ~NAF_NETMAP_ON; 1279 #ifdef IFCAP_NETMAP /* or FreeBSD ? */ 1280 ifp->if_capenable &= ~IFCAP_NETMAP; 1281 #endif 1282 } 1283 1284 /* 1285 * nm_*sync_prologue() functions are used in ioctl/poll and ptnetmap 1286 * kthreads. 1287 * We need netmap_ring* parameter, because in ptnetmap it is decoupled 1288 * from host kring. 1289 * The user-space ring pointers (head/cur/tail) are shared through 1290 * CSB between host and guest. 1291 */ 1292 1293 /* 1294 * validates parameters in the ring/kring, returns a value for head 1295 * If any error, returns ring_size to force a reinit. 1296 */ 1297 uint32_t nm_txsync_prologue(struct netmap_kring *, struct netmap_ring *); 1298 1299 1300 /* 1301 * validates parameters in the ring/kring, returns a value for head 1302 * If any error, returns ring_size lim to force a reinit. 1303 */ 1304 uint32_t nm_rxsync_prologue(struct netmap_kring *, struct netmap_ring *); 1305 1306 1307 /* check/fix address and len in tx rings */ 1308 #if 1 /* debug version */ 1309 #define NM_CHECK_ADDR_LEN(_na, _a, _l) do { \ 1310 if (_a == NETMAP_BUF_BASE(_na) || _l > NETMAP_BUF_SIZE(_na)) { \ 1311 RD(5, "bad addr/len ring %d slot %d idx %d len %d", \ 1312 kring->ring_id, nm_i, slot->buf_idx, len); \ 1313 if (_l > NETMAP_BUF_SIZE(_na)) \ 1314 _l = NETMAP_BUF_SIZE(_na); \ 1315 } } while (0) 1316 #else /* no debug version */ 1317 #define NM_CHECK_ADDR_LEN(_na, _a, _l) do { \ 1318 if (_l > NETMAP_BUF_SIZE(_na)) \ 1319 _l = NETMAP_BUF_SIZE(_na); \ 1320 } while (0) 1321 #endif 1322 1323 1324 /*---------------------------------------------------------------*/ 1325 /* 1326 * Support routines used by netmap subsystems 1327 * (native drivers, VALE, generic, pipes, monitors, ...) 1328 */ 1329 1330 1331 /* common routine for all functions that create a netmap adapter. It performs 1332 * two main tasks: 1333 * - if the na points to an ifp, mark the ifp as netmap capable 1334 * using na as its native adapter; 1335 * - provide defaults for the setup callbacks and the memory allocator 1336 */ 1337 int netmap_attach_common(struct netmap_adapter *); 1338 /* common actions to be performed on netmap adapter destruction */ 1339 void netmap_detach_common(struct netmap_adapter *); 1340 /* fill priv->np_[tr]xq{first,last} using the ringid and flags information 1341 * coming from a struct nmreq 1342 */ 1343 int netmap_interp_ringid(struct netmap_priv_d *priv, uint16_t ringid, uint32_t flags); 1344 /* update the ring parameters (number and size of tx and rx rings). 1345 * It calls the nm_config callback, if available. 1346 */ 1347 int netmap_update_config(struct netmap_adapter *na); 1348 /* create and initialize the common fields of the krings array. 1349 * using the information that must be already available in the na. 1350 * tailroom can be used to request the allocation of additional 1351 * tailroom bytes after the krings array. This is used by 1352 * netmap_vp_adapter's (i.e., VALE ports) to make room for 1353 * leasing-related data structures 1354 */ 1355 int netmap_krings_create(struct netmap_adapter *na, u_int tailroom); 1356 /* deletes the kring array of the adapter. The array must have 1357 * been created using netmap_krings_create 1358 */ 1359 void netmap_krings_delete(struct netmap_adapter *na); 1360 1361 int netmap_hw_krings_create(struct netmap_adapter *na); 1362 void netmap_hw_krings_delete(struct netmap_adapter *na); 1363 1364 /* set the stopped/enabled status of ring 1365 * When stopping, they also wait for all current activity on the ring to 1366 * terminate. The status change is then notified using the na nm_notify 1367 * callback. 1368 */ 1369 void netmap_set_ring(struct netmap_adapter *, u_int ring_id, enum txrx, int stopped); 1370 /* set the stopped/enabled status of all rings of the adapter. */ 1371 void netmap_set_all_rings(struct netmap_adapter *, int stopped); 1372 /* convenience wrappers for netmap_set_all_rings */ 1373 void netmap_disable_all_rings(struct ifnet *); 1374 void netmap_enable_all_rings(struct ifnet *); 1375 1376 int netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na, 1377 uint16_t ringid, uint32_t flags); 1378 void netmap_do_unregif(struct netmap_priv_d *priv); 1379 1380 u_int nm_bound_var(u_int *v, u_int dflt, u_int lo, u_int hi, const char *msg); 1381 int netmap_get_na(struct nmreq *nmr, struct netmap_adapter **na, 1382 struct ifnet **ifp, int create); 1383 void netmap_unget_na(struct netmap_adapter *na, struct ifnet *ifp); 1384 int netmap_get_hw_na(struct ifnet *ifp, struct netmap_adapter **na); 1385 1386 1387 #ifdef WITH_VALE 1388 /* 1389 * The following bridge-related functions are used by other 1390 * kernel modules. 1391 * 1392 * VALE only supports unicast or broadcast. The lookup 1393 * function can return 0 .. NM_BDG_MAXPORTS-1 for regular ports, 1394 * NM_BDG_MAXPORTS for broadcast, NM_BDG_MAXPORTS+1 for unknown. 1395 * XXX in practice "unknown" might be handled same as broadcast. 1396 */ 1397 typedef u_int (*bdg_lookup_fn_t)(struct nm_bdg_fwd *ft, uint8_t *ring_nr, 1398 struct netmap_vp_adapter *); 1399 typedef int (*bdg_config_fn_t)(struct nm_ifreq *); 1400 typedef void (*bdg_dtor_fn_t)(const struct netmap_vp_adapter *); 1401 struct netmap_bdg_ops { 1402 bdg_lookup_fn_t lookup; 1403 bdg_config_fn_t config; 1404 bdg_dtor_fn_t dtor; 1405 }; 1406 1407 u_int netmap_bdg_learning(struct nm_bdg_fwd *ft, uint8_t *dst_ring, 1408 struct netmap_vp_adapter *); 1409 1410 #define NM_BRIDGES 8 /* number of bridges */ 1411 #define NM_BDG_MAXPORTS 254 /* up to 254 */ 1412 #define NM_BDG_BROADCAST NM_BDG_MAXPORTS 1413 #define NM_BDG_NOPORT (NM_BDG_MAXPORTS+1) 1414 1415 /* these are redefined in case of no VALE support */ 1416 int netmap_get_bdg_na(struct nmreq *nmr, struct netmap_adapter **na, int create); 1417 struct nm_bridge *netmap_init_bridges2(u_int); 1418 void netmap_uninit_bridges2(struct nm_bridge *, u_int); 1419 int netmap_init_bridges(void); 1420 void netmap_uninit_bridges(void); 1421 int netmap_bdg_ctl(struct nmreq *nmr, struct netmap_bdg_ops *bdg_ops); 1422 int netmap_bdg_config(struct nmreq *nmr); 1423 1424 #else /* !WITH_VALE */ 1425 #define netmap_get_bdg_na(_1, _2, _3) 0 1426 #define netmap_init_bridges(_1) 0 1427 #define netmap_uninit_bridges() 1428 #define netmap_bdg_ctl(_1, _2) EINVAL 1429 #endif /* !WITH_VALE */ 1430 1431 #ifdef WITH_PIPES 1432 /* max number of pipes per device */ 1433 #define NM_MAXPIPES 64 /* XXX how many? */ 1434 void netmap_pipe_dealloc(struct netmap_adapter *); 1435 int netmap_get_pipe_na(struct nmreq *nmr, struct netmap_adapter **na, int create); 1436 #else /* !WITH_PIPES */ 1437 #define NM_MAXPIPES 0 1438 #define netmap_pipe_alloc(_1, _2) 0 1439 #define netmap_pipe_dealloc(_1) 1440 #define netmap_get_pipe_na(nmr, _2, _3) \ 1441 ({ int role__ = (nmr)->nr_flags & NR_REG_MASK; \ 1442 (role__ == NR_REG_PIPE_MASTER || \ 1443 role__ == NR_REG_PIPE_SLAVE) ? EOPNOTSUPP : 0; }) 1444 #endif 1445 1446 #ifdef WITH_MONITOR 1447 int netmap_get_monitor_na(struct nmreq *nmr, struct netmap_adapter **na, int create); 1448 void netmap_monitor_stop(struct netmap_adapter *na); 1449 #else 1450 #define netmap_get_monitor_na(nmr, _2, _3) \ 1451 ((nmr)->nr_flags & (NR_MONITOR_TX | NR_MONITOR_RX) ? EOPNOTSUPP : 0) 1452 #endif 1453 1454 #ifdef CONFIG_NET_NS 1455 struct net *netmap_bns_get(void); 1456 void netmap_bns_put(struct net *); 1457 void netmap_bns_getbridges(struct nm_bridge **, u_int *); 1458 #else 1459 #define netmap_bns_get() 1460 #define netmap_bns_put(_1) 1461 #define netmap_bns_getbridges(b, n) \ 1462 do { *b = nm_bridges; *n = NM_BRIDGES; } while (0) 1463 #endif 1464 1465 /* Various prototypes */ 1466 int netmap_poll(struct netmap_priv_d *, int events, NM_SELRECORD_T *td); 1467 int netmap_init(void); 1468 void netmap_fini(void); 1469 int netmap_get_memory(struct netmap_priv_d* p); 1470 void netmap_dtor(void *data); 1471 1472 int netmap_ioctl(struct netmap_priv_d *priv, u_long cmd, caddr_t data, struct thread *); 1473 1474 /* netmap_adapter creation/destruction */ 1475 1476 // #define NM_DEBUG_PUTGET 1 1477 1478 #ifdef NM_DEBUG_PUTGET 1479 1480 #define NM_DBG(f) __##f 1481 1482 void __netmap_adapter_get(struct netmap_adapter *na); 1483 1484 #define netmap_adapter_get(na) \ 1485 do { \ 1486 struct netmap_adapter *__na = na; \ 1487 D("getting %p:%s (%d)", __na, (__na)->name, (__na)->na_refcount); \ 1488 __netmap_adapter_get(__na); \ 1489 } while (0) 1490 1491 int __netmap_adapter_put(struct netmap_adapter *na); 1492 1493 #define netmap_adapter_put(na) \ 1494 ({ \ 1495 struct netmap_adapter *__na = na; \ 1496 D("putting %p:%s (%d)", __na, (__na)->name, (__na)->na_refcount); \ 1497 __netmap_adapter_put(__na); \ 1498 }) 1499 1500 #else /* !NM_DEBUG_PUTGET */ 1501 1502 #define NM_DBG(f) f 1503 void netmap_adapter_get(struct netmap_adapter *na); 1504 int netmap_adapter_put(struct netmap_adapter *na); 1505 1506 #endif /* !NM_DEBUG_PUTGET */ 1507 1508 1509 /* 1510 * module variables 1511 */ 1512 #define NETMAP_BUF_BASE(_na) ((_na)->na_lut.lut[0].vaddr) 1513 #define NETMAP_BUF_SIZE(_na) ((_na)->na_lut.objsize) 1514 extern int netmap_mitigate; // XXX not really used 1515 extern int netmap_no_pendintr; 1516 extern int netmap_verbose; // XXX debugging 1517 enum { /* verbose flags */ 1518 NM_VERB_ON = 1, /* generic verbose */ 1519 NM_VERB_HOST = 0x2, /* verbose host stack */ 1520 NM_VERB_RXSYNC = 0x10, /* verbose on rxsync/txsync */ 1521 NM_VERB_TXSYNC = 0x20, 1522 NM_VERB_RXINTR = 0x100, /* verbose on rx/tx intr (driver) */ 1523 NM_VERB_TXINTR = 0x200, 1524 NM_VERB_NIC_RXSYNC = 0x1000, /* verbose on rx/tx intr (driver) */ 1525 NM_VERB_NIC_TXSYNC = 0x2000, 1526 }; 1527 1528 extern int netmap_txsync_retry; 1529 extern int netmap_adaptive_io; 1530 extern int netmap_flags; 1531 extern int netmap_generic_mit; 1532 extern int netmap_generic_ringsize; 1533 extern int netmap_generic_rings; 1534 extern int netmap_generic_txqdisc; 1535 1536 /* 1537 * NA returns a pointer to the struct netmap adapter from the ifp, 1538 * WNA is used to write it. 1539 */ 1540 #define NA(_ifp) ((struct netmap_adapter *)WNA(_ifp)) 1541 1542 /* 1543 * On old versions of FreeBSD, NA(ifp) is a pspare. On linux we 1544 * overload another pointer in the netdev. 1545 * 1546 * We check if NA(ifp) is set and its first element has a related 1547 * magic value. The capenable is within the struct netmap_adapter. 1548 */ 1549 #define NETMAP_MAGIC 0x52697a7a 1550 1551 #define NM_NA_VALID(ifp) (NA(ifp) && \ 1552 ((uint32_t)(uintptr_t)NA(ifp) ^ NA(ifp)->magic) == NETMAP_MAGIC ) 1553 1554 #define NM_ATTACH_NA(ifp, na) do { \ 1555 WNA(ifp) = na; \ 1556 if (NA(ifp)) \ 1557 NA(ifp)->magic = \ 1558 ((uint32_t)(uintptr_t)NA(ifp)) ^ NETMAP_MAGIC; \ 1559 } while(0) 1560 1561 #define NM_IS_NATIVE(ifp) (NM_NA_VALID(ifp) && NA(ifp)->nm_dtor == netmap_hw_dtor) 1562 1563 #if defined(__FreeBSD__) 1564 1565 /* Assigns the device IOMMU domain to an allocator. 1566 * Returns -ENOMEM in case the domain is different */ 1567 #define nm_iommu_group_id(dev) (0) 1568 1569 /* Callback invoked by the dma machinery after a successful dmamap_load */ 1570 static void netmap_dmamap_cb(__unused void *arg, 1571 __unused bus_dma_segment_t * segs, __unused int nseg, __unused int error) 1572 { 1573 } 1574 1575 /* bus_dmamap_load wrapper: call aforementioned function if map != NULL. 1576 * XXX can we do it without a callback ? 1577 */ 1578 static inline void 1579 netmap_load_map(struct netmap_adapter *na, 1580 bus_dma_tag_t tag, bus_dmamap_t map, void *buf) 1581 { 1582 if (map) 1583 bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE(na), 1584 netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT); 1585 } 1586 1587 static inline void 1588 netmap_unload_map(struct netmap_adapter *na, 1589 bus_dma_tag_t tag, bus_dmamap_t map) 1590 { 1591 if (map) 1592 bus_dmamap_unload(tag, map); 1593 } 1594 1595 /* update the map when a buffer changes. */ 1596 static inline void 1597 netmap_reload_map(struct netmap_adapter *na, 1598 bus_dma_tag_t tag, bus_dmamap_t map, void *buf) 1599 { 1600 if (map) { 1601 bus_dmamap_unload(tag, map); 1602 bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE(na), 1603 netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT); 1604 } 1605 } 1606 1607 #elif defined(_WIN32) 1608 1609 #else /* linux */ 1610 1611 int nm_iommu_group_id(bus_dma_tag_t dev); 1612 #include <linux/dma-mapping.h> 1613 1614 static inline void 1615 netmap_load_map(struct netmap_adapter *na, 1616 bus_dma_tag_t tag, bus_dmamap_t map, void *buf) 1617 { 1618 if (0 && map) { 1619 *map = dma_map_single(na->pdev, buf, NETMAP_BUF_SIZE(na), 1620 DMA_BIDIRECTIONAL); 1621 } 1622 } 1623 1624 static inline void 1625 netmap_unload_map(struct netmap_adapter *na, 1626 bus_dma_tag_t tag, bus_dmamap_t map) 1627 { 1628 u_int sz = NETMAP_BUF_SIZE(na); 1629 1630 if (*map) { 1631 dma_unmap_single(na->pdev, *map, sz, 1632 DMA_BIDIRECTIONAL); 1633 } 1634 } 1635 1636 static inline void 1637 netmap_reload_map(struct netmap_adapter *na, 1638 bus_dma_tag_t tag, bus_dmamap_t map, void *buf) 1639 { 1640 u_int sz = NETMAP_BUF_SIZE(na); 1641 1642 if (*map) { 1643 dma_unmap_single(na->pdev, *map, sz, 1644 DMA_BIDIRECTIONAL); 1645 } 1646 1647 *map = dma_map_single(na->pdev, buf, sz, 1648 DMA_BIDIRECTIONAL); 1649 } 1650 1651 /* 1652 * XXX How do we redefine these functions: 1653 * 1654 * on linux we need 1655 * dma_map_single(&pdev->dev, virt_addr, len, direction) 1656 * dma_unmap_single(&adapter->pdev->dev, phys_addr, len, direction 1657 * The len can be implicit (on netmap it is NETMAP_BUF_SIZE) 1658 * unfortunately the direction is not, so we need to change 1659 * something to have a cross API 1660 */ 1661 1662 #if 0 1663 struct e1000_buffer *buffer_info = &tx_ring->buffer_info[l]; 1664 /* set time_stamp *before* dma to help avoid a possible race */ 1665 buffer_info->time_stamp = jiffies; 1666 buffer_info->mapped_as_page = false; 1667 buffer_info->length = len; 1668 //buffer_info->next_to_watch = l; 1669 /* reload dma map */ 1670 dma_unmap_single(&adapter->pdev->dev, buffer_info->dma, 1671 NETMAP_BUF_SIZE, DMA_TO_DEVICE); 1672 buffer_info->dma = dma_map_single(&adapter->pdev->dev, 1673 addr, NETMAP_BUF_SIZE, DMA_TO_DEVICE); 1674 1675 if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) { 1676 D("dma mapping error"); 1677 /* goto dma_error; See e1000_put_txbuf() */ 1678 /* XXX reset */ 1679 } 1680 tx_desc->buffer_addr = htole64(buffer_info->dma); //XXX 1681 1682 #endif 1683 1684 /* 1685 * The bus_dmamap_sync() can be one of wmb() or rmb() depending on direction. 1686 */ 1687 #define bus_dmamap_sync(_a, _b, _c) 1688 1689 #endif /* linux */ 1690 1691 1692 /* 1693 * functions to map NIC to KRING indexes (n2k) and vice versa (k2n) 1694 */ 1695 static inline int 1696 netmap_idx_n2k(struct netmap_kring *kr, int idx) 1697 { 1698 int n = kr->nkr_num_slots; 1699 idx += kr->nkr_hwofs; 1700 if (idx < 0) 1701 return idx + n; 1702 else if (idx < n) 1703 return idx; 1704 else 1705 return idx - n; 1706 } 1707 1708 1709 static inline int 1710 netmap_idx_k2n(struct netmap_kring *kr, int idx) 1711 { 1712 int n = kr->nkr_num_slots; 1713 idx -= kr->nkr_hwofs; 1714 if (idx < 0) 1715 return idx + n; 1716 else if (idx < n) 1717 return idx; 1718 else 1719 return idx - n; 1720 } 1721 1722 1723 /* Entries of the look-up table. */ 1724 struct lut_entry { 1725 void *vaddr; /* virtual address. */ 1726 vm_paddr_t paddr; /* physical address. */ 1727 }; 1728 1729 struct netmap_obj_pool; 1730 1731 /* 1732 * NMB return the virtual address of a buffer (buffer 0 on bad index) 1733 * PNMB also fills the physical address 1734 */ 1735 static inline void * 1736 NMB(struct netmap_adapter *na, struct netmap_slot *slot) 1737 { 1738 struct lut_entry *lut = na->na_lut.lut; 1739 uint32_t i = slot->buf_idx; 1740 return (unlikely(i >= na->na_lut.objtotal)) ? 1741 lut[0].vaddr : lut[i].vaddr; 1742 } 1743 1744 static inline void * 1745 PNMB(struct netmap_adapter *na, struct netmap_slot *slot, uint64_t *pp) 1746 { 1747 uint32_t i = slot->buf_idx; 1748 struct lut_entry *lut = na->na_lut.lut; 1749 void *ret = (i >= na->na_lut.objtotal) ? lut[0].vaddr : lut[i].vaddr; 1750 1751 #ifndef _WIN32 1752 *pp = (i >= na->na_lut.objtotal) ? lut[0].paddr : lut[i].paddr; 1753 #else 1754 *pp = (i >= na->na_lut.objtotal) ? (uint64_t)lut[0].paddr.QuadPart : (uint64_t)lut[i].paddr.QuadPart; 1755 #endif 1756 return ret; 1757 } 1758 1759 1760 /* 1761 * Structure associated to each netmap file descriptor. 1762 * It is created on open and left unbound (np_nifp == NULL). 1763 * A successful NIOCREGIF will set np_nifp and the first few fields; 1764 * this is protected by a global lock (NMG_LOCK) due to low contention. 1765 * 1766 * np_refs counts the number of references to the structure: one for the fd, 1767 * plus (on FreeBSD) one for each active mmap which we track ourselves 1768 * (linux automatically tracks them, but FreeBSD does not). 1769 * np_refs is protected by NMG_LOCK. 1770 * 1771 * Read access to the structure is lock free, because ni_nifp once set 1772 * can only go to 0 when nobody is using the entry anymore. Readers 1773 * must check that np_nifp != NULL before using the other fields. 1774 */ 1775 struct netmap_priv_d { 1776 struct netmap_if * volatile np_nifp; /* netmap if descriptor. */ 1777 1778 struct netmap_adapter *np_na; 1779 struct ifnet *np_ifp; 1780 uint32_t np_flags; /* from the ioctl */ 1781 u_int np_qfirst[NR_TXRX], 1782 np_qlast[NR_TXRX]; /* range of tx/rx rings to scan */ 1783 uint16_t np_txpoll; /* XXX and also np_rxpoll ? */ 1784 1785 int np_refs; /* use with NMG_LOCK held */ 1786 1787 /* pointers to the selinfo to be used for selrecord. 1788 * Either the local or the global one depending on the 1789 * number of rings. 1790 */ 1791 NM_SELINFO_T *np_si[NR_TXRX]; 1792 struct thread *np_td; /* kqueue, just debugging */ 1793 }; 1794 1795 struct netmap_priv_d *netmap_priv_new(void); 1796 void netmap_priv_delete(struct netmap_priv_d *); 1797 1798 static inline int nm_kring_pending(struct netmap_priv_d *np) 1799 { 1800 struct netmap_adapter *na = np->np_na; 1801 enum txrx t; 1802 int i; 1803 1804 for_rx_tx(t) { 1805 for (i = np->np_qfirst[t]; i < np->np_qlast[t]; i++) { 1806 struct netmap_kring *kring = &NMR(na, t)[i]; 1807 if (kring->nr_mode != kring->nr_pending_mode) { 1808 return 1; 1809 } 1810 } 1811 } 1812 return 0; 1813 } 1814 1815 #ifdef WITH_MONITOR 1816 1817 struct netmap_monitor_adapter { 1818 struct netmap_adapter up; 1819 1820 struct netmap_priv_d priv; 1821 uint32_t flags; 1822 }; 1823 1824 #endif /* WITH_MONITOR */ 1825 1826 1827 #ifdef WITH_GENERIC 1828 /* 1829 * generic netmap emulation for devices that do not have 1830 * native netmap support. 1831 */ 1832 int generic_netmap_attach(struct ifnet *ifp); 1833 int generic_rx_handler(struct ifnet *ifp, struct mbuf *m);; 1834 1835 int nm_os_catch_rx(struct netmap_generic_adapter *gna, int intercept); 1836 int nm_os_catch_tx(struct netmap_generic_adapter *gna, int intercept); 1837 1838 /* 1839 * the generic transmit routine is passed a structure to optionally 1840 * build a queue of descriptors, in an OS-specific way. 1841 * The payload is at addr, if non-null, and the routine should send or queue 1842 * the packet, returning 0 if successful, 1 on failure. 1843 * 1844 * At the end, if head is non-null, there will be an additional call 1845 * to the function with addr = NULL; this should tell the OS-specific 1846 * routine to send the queue and free any resources. Failure is ignored. 1847 */ 1848 struct nm_os_gen_arg { 1849 struct ifnet *ifp; 1850 void *m; /* os-specific mbuf-like object */ 1851 void *head, *tail; /* tailq, if the OS-specific routine needs to build one */ 1852 void *addr; /* payload of current packet */ 1853 u_int len; /* packet length */ 1854 u_int ring_nr; /* packet length */ 1855 u_int qevent; /* in txqdisc mode, place an event on this mbuf */ 1856 }; 1857 1858 int nm_os_generic_xmit_frame(struct nm_os_gen_arg *); 1859 int nm_os_generic_find_num_desc(struct ifnet *ifp, u_int *tx, u_int *rx); 1860 void nm_os_generic_find_num_queues(struct ifnet *ifp, u_int *txq, u_int *rxq); 1861 void nm_os_generic_set_features(struct netmap_generic_adapter *gna); 1862 1863 static inline struct ifnet* 1864 netmap_generic_getifp(struct netmap_generic_adapter *gna) 1865 { 1866 if (gna->prev) 1867 return gna->prev->ifp; 1868 1869 return gna->up.up.ifp; 1870 } 1871 1872 void netmap_generic_irq(struct netmap_adapter *na, u_int q, u_int *work_done); 1873 1874 //#define RATE_GENERIC /* Enables communication statistics for generic. */ 1875 #ifdef RATE_GENERIC 1876 void generic_rate(int txp, int txs, int txi, int rxp, int rxs, int rxi); 1877 #else 1878 #define generic_rate(txp, txs, txi, rxp, rxs, rxi) 1879 #endif 1880 1881 /* 1882 * netmap_mitigation API. This is used by the generic adapter 1883 * to reduce the number of interrupt requests/selwakeup 1884 * to clients on incoming packets. 1885 */ 1886 void nm_os_mitigation_init(struct nm_generic_mit *mit, int idx, 1887 struct netmap_adapter *na); 1888 void nm_os_mitigation_start(struct nm_generic_mit *mit); 1889 void nm_os_mitigation_restart(struct nm_generic_mit *mit); 1890 int nm_os_mitigation_active(struct nm_generic_mit *mit); 1891 void nm_os_mitigation_cleanup(struct nm_generic_mit *mit); 1892 #else /* !WITH_GENERIC */ 1893 #define generic_netmap_attach(ifp) (EOPNOTSUPP) 1894 #endif /* WITH_GENERIC */ 1895 1896 /* Shared declarations for the VALE switch. */ 1897 1898 /* 1899 * Each transmit queue accumulates a batch of packets into 1900 * a structure before forwarding. Packets to the same 1901 * destination are put in a list using ft_next as a link field. 1902 * ft_frags and ft_next are valid only on the first fragment. 1903 */ 1904 struct nm_bdg_fwd { /* forwarding entry for a bridge */ 1905 void *ft_buf; /* netmap or indirect buffer */ 1906 uint8_t ft_frags; /* how many fragments (only on 1st frag) */ 1907 uint8_t _ft_port; /* dst port (unused) */ 1908 uint16_t ft_flags; /* flags, e.g. indirect */ 1909 uint16_t ft_len; /* src fragment len */ 1910 uint16_t ft_next; /* next packet to same destination */ 1911 }; 1912 1913 /* struct 'virtio_net_hdr' from linux. */ 1914 struct nm_vnet_hdr { 1915 #define VIRTIO_NET_HDR_F_NEEDS_CSUM 1 /* Use csum_start, csum_offset */ 1916 #define VIRTIO_NET_HDR_F_DATA_VALID 2 /* Csum is valid */ 1917 uint8_t flags; 1918 #define VIRTIO_NET_HDR_GSO_NONE 0 /* Not a GSO frame */ 1919 #define VIRTIO_NET_HDR_GSO_TCPV4 1 /* GSO frame, IPv4 TCP (TSO) */ 1920 #define VIRTIO_NET_HDR_GSO_UDP 3 /* GSO frame, IPv4 UDP (UFO) */ 1921 #define VIRTIO_NET_HDR_GSO_TCPV6 4 /* GSO frame, IPv6 TCP */ 1922 #define VIRTIO_NET_HDR_GSO_ECN 0x80 /* TCP has ECN set */ 1923 uint8_t gso_type; 1924 uint16_t hdr_len; 1925 uint16_t gso_size; 1926 uint16_t csum_start; 1927 uint16_t csum_offset; 1928 }; 1929 1930 #define WORST_CASE_GSO_HEADER (14+40+60) /* IPv6 + TCP */ 1931 1932 /* Private definitions for IPv4, IPv6, UDP and TCP headers. */ 1933 1934 struct nm_iphdr { 1935 uint8_t version_ihl; 1936 uint8_t tos; 1937 uint16_t tot_len; 1938 uint16_t id; 1939 uint16_t frag_off; 1940 uint8_t ttl; 1941 uint8_t protocol; 1942 uint16_t check; 1943 uint32_t saddr; 1944 uint32_t daddr; 1945 /*The options start here. */ 1946 }; 1947 1948 struct nm_tcphdr { 1949 uint16_t source; 1950 uint16_t dest; 1951 uint32_t seq; 1952 uint32_t ack_seq; 1953 uint8_t doff; /* Data offset + Reserved */ 1954 uint8_t flags; 1955 uint16_t window; 1956 uint16_t check; 1957 uint16_t urg_ptr; 1958 }; 1959 1960 struct nm_udphdr { 1961 uint16_t source; 1962 uint16_t dest; 1963 uint16_t len; 1964 uint16_t check; 1965 }; 1966 1967 struct nm_ipv6hdr { 1968 uint8_t priority_version; 1969 uint8_t flow_lbl[3]; 1970 1971 uint16_t payload_len; 1972 uint8_t nexthdr; 1973 uint8_t hop_limit; 1974 1975 uint8_t saddr[16]; 1976 uint8_t daddr[16]; 1977 }; 1978 1979 /* Type used to store a checksum (in host byte order) that hasn't been 1980 * folded yet. 1981 */ 1982 #define rawsum_t uint32_t 1983 1984 rawsum_t nm_os_csum_raw(uint8_t *data, size_t len, rawsum_t cur_sum); 1985 uint16_t nm_os_csum_ipv4(struct nm_iphdr *iph); 1986 void nm_os_csum_tcpudp_ipv4(struct nm_iphdr *iph, void *data, 1987 size_t datalen, uint16_t *check); 1988 void nm_os_csum_tcpudp_ipv6(struct nm_ipv6hdr *ip6h, void *data, 1989 size_t datalen, uint16_t *check); 1990 uint16_t nm_os_csum_fold(rawsum_t cur_sum); 1991 1992 void bdg_mismatch_datapath(struct netmap_vp_adapter *na, 1993 struct netmap_vp_adapter *dst_na, 1994 const struct nm_bdg_fwd *ft_p, 1995 struct netmap_ring *dst_ring, 1996 u_int *j, u_int lim, u_int *howmany); 1997 1998 /* persistent virtual port routines */ 1999 int nm_os_vi_persist(const char *, struct ifnet **); 2000 void nm_os_vi_detach(struct ifnet *); 2001 void nm_os_vi_init_index(void); 2002 2003 /* 2004 * kernel thread routines 2005 */ 2006 struct nm_kthread; /* OS-specific kthread - opaque */ 2007 typedef void (*nm_kthread_worker_fn_t)(void *data); 2008 2009 /* kthread configuration */ 2010 struct nm_kthread_cfg { 2011 long type; /* kthread type/identifier */ 2012 struct ptnet_ring_cfg event; /* event/ioctl fd */ 2013 nm_kthread_worker_fn_t worker_fn; /* worker function */ 2014 void *worker_private;/* worker parameter */ 2015 int attach_user; /* attach kthread to user process */ 2016 }; 2017 /* kthread configuration */ 2018 struct nm_kthread *nm_os_kthread_create(struct nm_kthread_cfg *cfg); 2019 int nm_os_kthread_start(struct nm_kthread *); 2020 void nm_os_kthread_stop(struct nm_kthread *); 2021 void nm_os_kthread_delete(struct nm_kthread *); 2022 void nm_os_kthread_wakeup_worker(struct nm_kthread *nmk); 2023 void nm_os_kthread_send_irq(struct nm_kthread *); 2024 void nm_os_kthread_set_affinity(struct nm_kthread *, int); 2025 u_int nm_os_ncpus(void); 2026 2027 #ifdef WITH_PTNETMAP_HOST 2028 /* 2029 * netmap adapter for host ptnetmap ports 2030 */ 2031 struct netmap_pt_host_adapter { 2032 struct netmap_adapter up; 2033 2034 struct netmap_adapter *parent; 2035 int (*parent_nm_notify)(struct netmap_kring *kring, int flags); 2036 void *ptns; 2037 }; 2038 /* ptnetmap HOST routines */ 2039 int netmap_get_pt_host_na(struct nmreq *nmr, struct netmap_adapter **na, int create); 2040 int ptnetmap_ctl(struct nmreq *nmr, struct netmap_adapter *na); 2041 static inline int 2042 nm_ptnetmap_host_on(struct netmap_adapter *na) 2043 { 2044 return na && na->na_flags & NAF_PTNETMAP_HOST; 2045 } 2046 #else /* !WITH_PTNETMAP_HOST */ 2047 #define netmap_get_pt_host_na(nmr, _2, _3) \ 2048 ((nmr)->nr_flags & (NR_PTNETMAP_HOST) ? EOPNOTSUPP : 0) 2049 #define ptnetmap_ctl(_1, _2) EINVAL 2050 #define nm_ptnetmap_host_on(_1) EINVAL 2051 #endif /* !WITH_PTNETMAP_HOST */ 2052 2053 #ifdef WITH_PTNETMAP_GUEST 2054 /* ptnetmap GUEST routines */ 2055 2056 typedef uint32_t (*nm_pt_guest_ptctl_t)(struct ifnet *, uint32_t); 2057 2058 /* 2059 * netmap adapter for guest ptnetmap ports 2060 */ 2061 struct netmap_pt_guest_adapter { 2062 /* The netmap adapter to be used by netmap applications. 2063 * This field must be the first, to allow upcast. */ 2064 struct netmap_hw_adapter hwup; 2065 2066 /* The netmap adapter to be used by the driver. */ 2067 struct netmap_hw_adapter dr; 2068 2069 void *csb; 2070 2071 /* Reference counter to track users of backend netmap port: the 2072 * network stack and netmap clients. 2073 * Used to decide when we need (de)allocate krings/rings and 2074 * start (stop) ptnetmap kthreads. */ 2075 int backend_regifs; 2076 2077 }; 2078 2079 int netmap_pt_guest_attach(struct netmap_adapter *, void *, 2080 unsigned int, nm_pt_guest_ptctl_t); 2081 struct ptnet_ring; 2082 bool netmap_pt_guest_txsync(struct ptnet_ring *ptring, struct netmap_kring *kring, 2083 int flags); 2084 bool netmap_pt_guest_rxsync(struct ptnet_ring *ptring, struct netmap_kring *kring, 2085 int flags); 2086 int ptnet_nm_krings_create(struct netmap_adapter *na); 2087 void ptnet_nm_krings_delete(struct netmap_adapter *na); 2088 void ptnet_nm_dtor(struct netmap_adapter *na); 2089 #endif /* WITH_PTNETMAP_GUEST */ 2090 2091 #endif /* _NET_NETMAP_KERN_H_ */ 2092