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