1*718cf2ccSPedro F. Giffuni /*- 2*718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*718cf2ccSPedro F. Giffuni * 437e3a6d3SLuigi Rizzo * Copyright (C) 2011-2014 Matteo Landi, Luigi Rizzo 537e3a6d3SLuigi Rizzo * Copyright (C) 2013-2016 Universita` di Pisa 637e3a6d3SLuigi Rizzo * All rights reserved. 768b8534bSLuigi Rizzo * 868b8534bSLuigi Rizzo * Redistribution and use in source and binary forms, with or without 968b8534bSLuigi Rizzo * modification, are permitted provided that the following conditions 1068b8534bSLuigi Rizzo * are met: 1168b8534bSLuigi Rizzo * 1. Redistributions of source code must retain the above copyright 1268b8534bSLuigi Rizzo * notice, this list of conditions and the following disclaimer. 1368b8534bSLuigi Rizzo * 2. Redistributions in binary form must reproduce the above copyright 1468b8534bSLuigi Rizzo * notice, this list of conditions and the following disclaimer in the 1568b8534bSLuigi Rizzo * documentation and/or other materials provided with the distribution. 1668b8534bSLuigi Rizzo * 1768b8534bSLuigi Rizzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1868b8534bSLuigi Rizzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1968b8534bSLuigi Rizzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2068b8534bSLuigi Rizzo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2168b8534bSLuigi Rizzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2268b8534bSLuigi Rizzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2368b8534bSLuigi Rizzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2468b8534bSLuigi Rizzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2568b8534bSLuigi Rizzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2668b8534bSLuigi Rizzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2768b8534bSLuigi Rizzo * SUCH DAMAGE. 2868b8534bSLuigi Rizzo */ 2968b8534bSLuigi Rizzo 3068b8534bSLuigi Rizzo /* 3168b8534bSLuigi Rizzo * $FreeBSD$ 3268b8534bSLuigi Rizzo * 3368b8534bSLuigi Rizzo * The header contains the definitions of constants and function 3468b8534bSLuigi Rizzo * prototypes used only in kernelspace. 3568b8534bSLuigi Rizzo */ 3668b8534bSLuigi Rizzo 3768b8534bSLuigi Rizzo #ifndef _NET_NETMAP_KERN_H_ 3868b8534bSLuigi Rizzo #define _NET_NETMAP_KERN_H_ 3968b8534bSLuigi Rizzo 40847bf383SLuigi Rizzo #if defined(linux) 41847bf383SLuigi Rizzo 42847bf383SLuigi Rizzo #if defined(CONFIG_NETMAP_VALE) 43847bf383SLuigi Rizzo #define WITH_VALE 44847bf383SLuigi Rizzo #endif 45847bf383SLuigi Rizzo #if defined(CONFIG_NETMAP_PIPE) 46847bf383SLuigi Rizzo #define WITH_PIPES 47847bf383SLuigi Rizzo #endif 48847bf383SLuigi Rizzo #if defined(CONFIG_NETMAP_MONITOR) 49847bf383SLuigi Rizzo #define WITH_MONITOR 50847bf383SLuigi Rizzo #endif 51847bf383SLuigi Rizzo #if defined(CONFIG_NETMAP_GENERIC) 52847bf383SLuigi Rizzo #define WITH_GENERIC 53847bf383SLuigi Rizzo #endif 5437e3a6d3SLuigi Rizzo #if defined(CONFIG_NETMAP_PTNETMAP_GUEST) 5537e3a6d3SLuigi Rizzo #define WITH_PTNETMAP_GUEST 5637e3a6d3SLuigi Rizzo #endif 5737e3a6d3SLuigi Rizzo #if defined(CONFIG_NETMAP_PTNETMAP_HOST) 5837e3a6d3SLuigi Rizzo #define WITH_PTNETMAP_HOST 59847bf383SLuigi Rizzo #endif 60c3e9b4dbSLuiz Otavio O Souza #if defined(CONFIG_NETMAP_SINK) 61c3e9b4dbSLuiz Otavio O Souza #define WITH_SINK 62c3e9b4dbSLuiz Otavio O Souza #endif 63847bf383SLuigi Rizzo 6437e3a6d3SLuigi Rizzo #elif defined (_WIN32) 65f9790aebSLuigi Rizzo #define WITH_VALE // comment out to disable VALE support 66f0ea3689SLuigi Rizzo #define WITH_PIPES 674bf50f18SLuigi Rizzo #define WITH_MONITOR 68039dd540SLuigi Rizzo #define WITH_GENERIC 69f9790aebSLuigi Rizzo 7037e3a6d3SLuigi Rizzo #else /* neither linux nor windows */ 7137e3a6d3SLuigi Rizzo #define WITH_VALE // comment out to disable VALE support 7237e3a6d3SLuigi Rizzo #define WITH_PIPES 7337e3a6d3SLuigi Rizzo #define WITH_MONITOR 7437e3a6d3SLuigi Rizzo #define WITH_GENERIC 7537e3a6d3SLuigi Rizzo #define WITH_PTNETMAP_HOST /* ptnetmap host support */ 7637e3a6d3SLuigi Rizzo #define WITH_PTNETMAP_GUEST /* ptnetmap guest support */ 7737e3a6d3SLuigi Rizzo 78847bf383SLuigi Rizzo #endif 79847bf383SLuigi Rizzo 801a26580eSLuigi Rizzo #if defined(__FreeBSD__) 81225d33ffSSean Bruno #include <sys/selinfo.h> 82d4b42e08SLuigi Rizzo 83ce3ee1e7SLuigi Rizzo #define likely(x) __builtin_expect((long)!!(x), 1L) 84ce3ee1e7SLuigi Rizzo #define unlikely(x) __builtin_expect((long)!!(x), 0L) 8537e3a6d3SLuigi Rizzo #define __user 86f196ce38SLuigi Rizzo 87847bf383SLuigi Rizzo #define NM_LOCK_T struct mtx /* low level spinlock, used to protect queues */ 88039dd540SLuigi Rizzo 89847bf383SLuigi Rizzo #define NM_MTX_T struct sx /* OS-specific mutex (sleepable) */ 90847bf383SLuigi Rizzo #define NM_MTX_INIT(m) sx_init(&(m), #m) 91847bf383SLuigi Rizzo #define NM_MTX_DESTROY(m) sx_destroy(&(m)) 92847bf383SLuigi Rizzo #define NM_MTX_LOCK(m) sx_xlock(&(m)) 93847bf383SLuigi Rizzo #define NM_MTX_UNLOCK(m) sx_xunlock(&(m)) 94847bf383SLuigi Rizzo #define NM_MTX_ASSERT(m) sx_assert(&(m), SA_XLOCKED) 95f9790aebSLuigi Rizzo 960e73f29aSLuigi Rizzo #define NM_SELINFO_T struct nm_selinfo 9737e3a6d3SLuigi Rizzo #define NM_SELRECORD_T struct thread 981a26580eSLuigi Rizzo #define MBUF_LEN(m) ((m)->m_pkthdr.len) 9937e3a6d3SLuigi Rizzo #define MBUF_TXQ(m) ((m)->m_pkthdr.flowid) 10037e3a6d3SLuigi Rizzo #define MBUF_TRANSMIT(na, ifp, m) ((na)->if_transmit(ifp, m)) 10137e3a6d3SLuigi Rizzo #define GEN_TX_MBUF_IFP(m) ((m)->m_pkthdr.rcvif) 102d4b42e08SLuigi Rizzo 103f9790aebSLuigi Rizzo #define NM_ATOMIC_T volatile int // XXX ? 104f9790aebSLuigi Rizzo /* atomic operations */ 105f9790aebSLuigi Rizzo #include <machine/atomic.h> 106f9790aebSLuigi Rizzo #define NM_ATOMIC_TEST_AND_SET(p) (!atomic_cmpset_acq_int((p), 0, 1)) 107f9790aebSLuigi Rizzo #define NM_ATOMIC_CLEAR(p) atomic_store_rel_int((p), 0) 108f9790aebSLuigi Rizzo 1097f154b71SLuigi Rizzo #if __FreeBSD_version >= 1100030 1107f154b71SLuigi Rizzo #define WNA(_ifp) (_ifp)->if_netmap 1117f154b71SLuigi Rizzo #else /* older FreeBSD */ 1127f154b71SLuigi Rizzo #define WNA(_ifp) (_ifp)->if_pspare[0] 1137f154b71SLuigi Rizzo #endif /* older FreeBSD */ 1147f154b71SLuigi Rizzo 11546aa1303SLuigi Rizzo #if __FreeBSD_version >= 1100005 11646aa1303SLuigi Rizzo struct netmap_adapter *netmap_getna(if_t ifp); 11746aa1303SLuigi Rizzo #endif 118f9790aebSLuigi Rizzo 1194bf50f18SLuigi Rizzo #if __FreeBSD_version >= 1100027 12037e3a6d3SLuigi Rizzo #define MBUF_REFCNT(m) ((m)->m_ext.ext_count) 12137e3a6d3SLuigi Rizzo #define SET_MBUF_REFCNT(m, x) (m)->m_ext.ext_count = x 1224bf50f18SLuigi Rizzo #else 12337e3a6d3SLuigi Rizzo #define MBUF_REFCNT(m) ((m)->m_ext.ref_cnt ? *((m)->m_ext.ref_cnt) : -1) 1244bf50f18SLuigi Rizzo #define SET_MBUF_REFCNT(m, x) *((m)->m_ext.ref_cnt) = x 1254bf50f18SLuigi Rizzo #endif 1264bf50f18SLuigi Rizzo 12737e3a6d3SLuigi Rizzo #define MBUF_QUEUED(m) 1 128f9790aebSLuigi Rizzo 1290e73f29aSLuigi Rizzo struct nm_selinfo { 1300e73f29aSLuigi Rizzo struct selinfo si; 1310e73f29aSLuigi Rizzo struct mtx m; 1320e73f29aSLuigi Rizzo }; 1330e73f29aSLuigi Rizzo 1340e73f29aSLuigi Rizzo 135f9790aebSLuigi Rizzo // XXX linux struct, not used in FreeBSD 136f9790aebSLuigi Rizzo struct net_device_ops { 137f9790aebSLuigi Rizzo }; 1384bf50f18SLuigi Rizzo struct ethtool_ops { 1394bf50f18SLuigi Rizzo }; 140f9790aebSLuigi Rizzo struct hrtimer { 141f9790aebSLuigi Rizzo }; 142847bf383SLuigi Rizzo #define NM_BNS_GET(b) 143847bf383SLuigi Rizzo #define NM_BNS_PUT(b) 144ce3ee1e7SLuigi Rizzo 14564ae02c3SLuigi Rizzo #elif defined (linux) 146d4b42e08SLuigi Rizzo 1472579e2d7SLuigi Rizzo #define NM_LOCK_T safe_spinlock_t // see bsd_glue.h 1481a26580eSLuigi Rizzo #define NM_SELINFO_T wait_queue_head_t 1491a26580eSLuigi Rizzo #define MBUF_LEN(m) ((m)->len) 15037e3a6d3SLuigi Rizzo #define MBUF_TRANSMIT(na, ifp, m) \ 15137e3a6d3SLuigi Rizzo ({ \ 15237e3a6d3SLuigi Rizzo /* Avoid infinite recursion with generic. */ \ 15337e3a6d3SLuigi Rizzo m->priority = NM_MAGIC_PRIORITY_TX; \ 15437e3a6d3SLuigi Rizzo (((struct net_device_ops *)(na)->if_transmit)->ndo_start_xmit(m, ifp)); \ 15537e3a6d3SLuigi Rizzo 0; \ 15637e3a6d3SLuigi Rizzo }) 15737e3a6d3SLuigi Rizzo 15837e3a6d3SLuigi Rizzo /* See explanation in nm_os_generic_xmit_frame. */ 15937e3a6d3SLuigi Rizzo #define GEN_TX_MBUF_IFP(m) ((struct ifnet *)skb_shinfo(m)->destructor_arg) 160f196ce38SLuigi Rizzo 161ce3ee1e7SLuigi Rizzo #define NM_ATOMIC_T volatile long unsigned int 162ce3ee1e7SLuigi Rizzo 163847bf383SLuigi Rizzo #define NM_MTX_T struct mutex /* OS-specific sleepable lock */ 164847bf383SLuigi Rizzo #define NM_MTX_INIT(m) mutex_init(&(m)) 165847bf383SLuigi Rizzo #define NM_MTX_DESTROY(m) do { (void)(m); } while (0) 166039dd540SLuigi Rizzo #define NM_MTX_LOCK(m) mutex_lock(&(m)) 167039dd540SLuigi Rizzo #define NM_MTX_UNLOCK(m) mutex_unlock(&(m)) 168847bf383SLuigi Rizzo #define NM_MTX_ASSERT(m) mutex_is_locked(&(m)) 169f9790aebSLuigi Rizzo 170f196ce38SLuigi Rizzo #ifndef DEV_NETMAP 171f196ce38SLuigi Rizzo #define DEV_NETMAP 172ce3ee1e7SLuigi Rizzo #endif /* DEV_NETMAP */ 173f196ce38SLuigi Rizzo 174f196ce38SLuigi Rizzo #elif defined (__APPLE__) 175d4b42e08SLuigi Rizzo 1768241616dSLuigi Rizzo #warning apple support is incomplete. 177f196ce38SLuigi Rizzo #define likely(x) __builtin_expect(!!(x), 1) 178f196ce38SLuigi Rizzo #define unlikely(x) __builtin_expect(!!(x), 0) 179f196ce38SLuigi Rizzo #define NM_LOCK_T IOLock * 180f196ce38SLuigi Rizzo #define NM_SELINFO_T struct selinfo 181f196ce38SLuigi Rizzo #define MBUF_LEN(m) ((m)->m_pkthdr.len) 18237e3a6d3SLuigi Rizzo 18337e3a6d3SLuigi Rizzo #elif defined (_WIN32) 18437e3a6d3SLuigi Rizzo #include "../../../WINDOWS/win_glue.h" 18537e3a6d3SLuigi Rizzo 18637e3a6d3SLuigi Rizzo #define NM_SELRECORD_T IO_STACK_LOCATION 18737e3a6d3SLuigi Rizzo #define NM_SELINFO_T win_SELINFO // see win_glue.h 18837e3a6d3SLuigi Rizzo #define NM_LOCK_T win_spinlock_t // see win_glue.h 18937e3a6d3SLuigi Rizzo #define NM_MTX_T KGUARDED_MUTEX /* OS-specific mutex (sleepable) */ 19037e3a6d3SLuigi Rizzo 19137e3a6d3SLuigi Rizzo #define NM_MTX_INIT(m) KeInitializeGuardedMutex(&m); 19237e3a6d3SLuigi Rizzo #define NM_MTX_DESTROY(m) do { (void)(m); } while (0) 19337e3a6d3SLuigi Rizzo #define NM_MTX_LOCK(m) KeAcquireGuardedMutex(&(m)) 19437e3a6d3SLuigi Rizzo #define NM_MTX_UNLOCK(m) KeReleaseGuardedMutex(&(m)) 19537e3a6d3SLuigi Rizzo #define NM_MTX_ASSERT(m) assert(&m.Count>0) 19637e3a6d3SLuigi Rizzo 19737e3a6d3SLuigi Rizzo //These linknames are for the NDIS driver 19837e3a6d3SLuigi Rizzo #define NETMAP_NDIS_LINKNAME_STRING L"\\DosDevices\\NMAPNDIS" 19937e3a6d3SLuigi Rizzo #define NETMAP_NDIS_NTDEVICE_STRING L"\\Device\\NMAPNDIS" 20037e3a6d3SLuigi Rizzo 20137e3a6d3SLuigi Rizzo //Definition of internal driver-to-driver ioctl codes 20237e3a6d3SLuigi Rizzo #define NETMAP_KERNEL_XCHANGE_POINTERS _IO('i', 180) 20337e3a6d3SLuigi Rizzo #define NETMAP_KERNEL_SEND_SHUTDOWN_SIGNAL _IO_direct('i', 195) 20437e3a6d3SLuigi Rizzo 20537e3a6d3SLuigi Rizzo //Empty data structures are not permitted by MSVC compiler 20637e3a6d3SLuigi Rizzo //XXX_ale, try to solve this problem 20737e3a6d3SLuigi Rizzo struct net_device_ops{ 20837e3a6d3SLuigi Rizzo char data[1]; 20937e3a6d3SLuigi Rizzo }; 21037e3a6d3SLuigi Rizzo typedef struct ethtool_ops{ 21137e3a6d3SLuigi Rizzo char data[1]; 21237e3a6d3SLuigi Rizzo }; 21337e3a6d3SLuigi Rizzo typedef struct hrtimer{ 21437e3a6d3SLuigi Rizzo KTIMER timer; 21537e3a6d3SLuigi Rizzo BOOLEAN active; 21637e3a6d3SLuigi Rizzo KDPC deferred_proc; 21737e3a6d3SLuigi Rizzo }; 21837e3a6d3SLuigi Rizzo 21937e3a6d3SLuigi Rizzo /* MSVC does not have likely/unlikely support */ 22037e3a6d3SLuigi Rizzo #ifdef _MSC_VER 22137e3a6d3SLuigi Rizzo #define likely(x) (x) 22237e3a6d3SLuigi Rizzo #define unlikely(x) (x) 22337e3a6d3SLuigi Rizzo #else 22437e3a6d3SLuigi Rizzo #define likely(x) __builtin_expect((long)!!(x), 1L) 22537e3a6d3SLuigi Rizzo #define unlikely(x) __builtin_expect((long)!!(x), 0L) 22637e3a6d3SLuigi Rizzo #endif //_MSC_VER 227f196ce38SLuigi Rizzo 2281a26580eSLuigi Rizzo #else 229d4b42e08SLuigi Rizzo 2301a26580eSLuigi Rizzo #error unsupported platform 231d4b42e08SLuigi Rizzo 232d4b42e08SLuigi Rizzo #endif /* end - platform-specific code */ 2331a26580eSLuigi Rizzo 23437e3a6d3SLuigi Rizzo #ifndef _WIN32 /* support for emulated sysctl */ 23537e3a6d3SLuigi Rizzo #define SYSBEGIN(x) 23637e3a6d3SLuigi Rizzo #define SYSEND 23737e3a6d3SLuigi Rizzo #endif /* _WIN32 */ 23837e3a6d3SLuigi Rizzo 23937e3a6d3SLuigi Rizzo #define NM_ACCESS_ONCE(x) (*(volatile __typeof__(x) *)&(x)) 24037e3a6d3SLuigi Rizzo 241847bf383SLuigi Rizzo #define NMG_LOCK_T NM_MTX_T 242847bf383SLuigi Rizzo #define NMG_LOCK_INIT() NM_MTX_INIT(netmap_global_lock) 243847bf383SLuigi Rizzo #define NMG_LOCK_DESTROY() NM_MTX_DESTROY(netmap_global_lock) 244847bf383SLuigi Rizzo #define NMG_LOCK() NM_MTX_LOCK(netmap_global_lock) 245847bf383SLuigi Rizzo #define NMG_UNLOCK() NM_MTX_UNLOCK(netmap_global_lock) 246847bf383SLuigi Rizzo #define NMG_LOCK_ASSERT() NM_MTX_ASSERT(netmap_global_lock) 247847bf383SLuigi Rizzo 248c3e9b4dbSLuiz Otavio O Souza #if defined(__FreeBSD__) 249c3e9b4dbSLuiz Otavio O Souza #define nm_prerr printf 250c3e9b4dbSLuiz Otavio O Souza #define nm_prinf printf 251c3e9b4dbSLuiz Otavio O Souza #elif defined (_WIN32) 252c3e9b4dbSLuiz Otavio O Souza #define nm_prerr DbgPrint 253c3e9b4dbSLuiz Otavio O Souza #define nm_prinf DbgPrint 254c3e9b4dbSLuiz Otavio O Souza #elif defined(linux) 255c3e9b4dbSLuiz Otavio O Souza #define nm_prerr(fmt, arg...) printk(KERN_ERR fmt, ##arg) 256c3e9b4dbSLuiz Otavio O Souza #define nm_prinf(fmt, arg...) printk(KERN_INFO fmt, ##arg) 257c3e9b4dbSLuiz Otavio O Souza #endif 258c3e9b4dbSLuiz Otavio O Souza 25968b8534bSLuigi Rizzo #define ND(format, ...) 26068b8534bSLuigi Rizzo #define D(format, ...) \ 26168b8534bSLuigi Rizzo do { \ 26268b8534bSLuigi Rizzo struct timeval __xxts; \ 26368b8534bSLuigi Rizzo microtime(&__xxts); \ 264c3e9b4dbSLuiz Otavio O Souza nm_prerr("%03d.%06d [%4d] %-25s " format "\n", \ 26568b8534bSLuigi Rizzo (int)__xxts.tv_sec % 1000, (int)__xxts.tv_usec, \ 26617885a7bSLuigi Rizzo __LINE__, __FUNCTION__, ##__VA_ARGS__); \ 26768b8534bSLuigi Rizzo } while (0) 26868b8534bSLuigi Rizzo 2698241616dSLuigi Rizzo /* rate limited, lps indicates how many per second */ 2708241616dSLuigi Rizzo #define RD(lps, format, ...) \ 2718241616dSLuigi Rizzo do { \ 2728241616dSLuigi Rizzo static int t0, __cnt; \ 2738241616dSLuigi Rizzo if (t0 != time_second) { \ 2748241616dSLuigi Rizzo t0 = time_second; \ 2758241616dSLuigi Rizzo __cnt = 0; \ 2768241616dSLuigi Rizzo } \ 2778241616dSLuigi Rizzo if (__cnt++ < lps) \ 2788241616dSLuigi Rizzo D(format, ##__VA_ARGS__); \ 2798241616dSLuigi Rizzo } while (0) 2808241616dSLuigi Rizzo 28168b8534bSLuigi Rizzo struct netmap_adapter; 282f18be576SLuigi Rizzo struct nm_bdg_fwd; 283f18be576SLuigi Rizzo struct nm_bridge; 284f18be576SLuigi Rizzo struct netmap_priv_d; 28568b8534bSLuigi Rizzo 28637e3a6d3SLuigi Rizzo /* os-specific NM_SELINFO_T initialzation/destruction functions */ 28737e3a6d3SLuigi Rizzo void nm_os_selinfo_init(NM_SELINFO_T *); 28837e3a6d3SLuigi Rizzo void nm_os_selinfo_uninit(NM_SELINFO_T *); 28937e3a6d3SLuigi Rizzo 290ce3ee1e7SLuigi Rizzo const char *nm_dump_buf(char *p, int len, int lim, char *dst); 291ce3ee1e7SLuigi Rizzo 29237e3a6d3SLuigi Rizzo void nm_os_selwakeup(NM_SELINFO_T *si); 29337e3a6d3SLuigi Rizzo void nm_os_selrecord(NM_SELRECORD_T *sr, NM_SELINFO_T *si); 29437e3a6d3SLuigi Rizzo 29537e3a6d3SLuigi Rizzo int nm_os_ifnet_init(void); 29637e3a6d3SLuigi Rizzo void nm_os_ifnet_fini(void); 29737e3a6d3SLuigi Rizzo void nm_os_ifnet_lock(void); 29837e3a6d3SLuigi Rizzo void nm_os_ifnet_unlock(void); 29937e3a6d3SLuigi Rizzo 30037e3a6d3SLuigi Rizzo void nm_os_get_module(void); 30137e3a6d3SLuigi Rizzo void nm_os_put_module(void); 30237e3a6d3SLuigi Rizzo 30337e3a6d3SLuigi Rizzo void netmap_make_zombie(struct ifnet *); 30437e3a6d3SLuigi Rizzo void netmap_undo_zombie(struct ifnet *); 30537e3a6d3SLuigi Rizzo 306c3e9b4dbSLuiz Otavio O Souza /* os independent alloc/realloc/free */ 307c3e9b4dbSLuiz Otavio O Souza void *nm_os_malloc(size_t); 308c3e9b4dbSLuiz Otavio O Souza void *nm_os_realloc(void *, size_t new_size, size_t old_size); 309c3e9b4dbSLuiz Otavio O Souza void nm_os_free(void *); 310c3e9b4dbSLuiz Otavio O Souza 31137e3a6d3SLuigi Rizzo /* passes a packet up to the host stack. 31237e3a6d3SLuigi Rizzo * If the packet is sent (or dropped) immediately it returns NULL, 31337e3a6d3SLuigi Rizzo * otherwise it links the packet to prev and returns m. 31437e3a6d3SLuigi Rizzo * In this case, a final call with m=NULL and prev != NULL will send up 31537e3a6d3SLuigi Rizzo * the entire chain to the host stack. 31637e3a6d3SLuigi Rizzo */ 31737e3a6d3SLuigi Rizzo void *nm_os_send_up(struct ifnet *, struct mbuf *m, struct mbuf *prev); 31837e3a6d3SLuigi Rizzo 31937e3a6d3SLuigi Rizzo int nm_os_mbuf_has_offld(struct mbuf *m); 32037e3a6d3SLuigi Rizzo 321f9790aebSLuigi Rizzo #include "netmap_mbq.h" 322f9790aebSLuigi Rizzo 323f9790aebSLuigi Rizzo extern NMG_LOCK_T netmap_global_lock; 324f9790aebSLuigi Rizzo 325847bf383SLuigi Rizzo enum txrx { NR_RX = 0, NR_TX = 1, NR_TXRX }; 326847bf383SLuigi Rizzo 327847bf383SLuigi Rizzo static __inline const char* 328847bf383SLuigi Rizzo nm_txrx2str(enum txrx t) 329847bf383SLuigi Rizzo { 330847bf383SLuigi Rizzo return (t== NR_RX ? "RX" : "TX"); 331847bf383SLuigi Rizzo } 332847bf383SLuigi Rizzo 333847bf383SLuigi Rizzo static __inline enum txrx 334847bf383SLuigi Rizzo nm_txrx_swap(enum txrx t) 335847bf383SLuigi Rizzo { 336847bf383SLuigi Rizzo return (t== NR_RX ? NR_TX : NR_RX); 337847bf383SLuigi Rizzo } 338847bf383SLuigi Rizzo 339847bf383SLuigi Rizzo #define for_rx_tx(t) for ((t) = 0; (t) < NR_TXRX; (t)++) 340847bf383SLuigi Rizzo 341c3e9b4dbSLuiz Otavio O Souza #ifdef WITH_MONITOR 342c3e9b4dbSLuiz Otavio O Souza struct netmap_zmon_list { 343c3e9b4dbSLuiz Otavio O Souza struct netmap_kring *next; 344c3e9b4dbSLuiz Otavio O Souza struct netmap_kring *prev; 345c3e9b4dbSLuiz Otavio O Souza }; 346c3e9b4dbSLuiz Otavio O Souza #endif /* WITH_MONITOR */ 347847bf383SLuigi Rizzo 34868b8534bSLuigi Rizzo /* 34964ae02c3SLuigi Rizzo * private, kernel view of a ring. Keeps track of the status of 35064ae02c3SLuigi Rizzo * a ring across system calls. 35164ae02c3SLuigi Rizzo * 35264ae02c3SLuigi Rizzo * nr_hwcur index of the next buffer to refill. 35317885a7bSLuigi Rizzo * It corresponds to ring->head 35417885a7bSLuigi Rizzo * at the time the system call returns. 35564ae02c3SLuigi Rizzo * 35617885a7bSLuigi Rizzo * nr_hwtail index of the first buffer owned by the kernel. 35717885a7bSLuigi Rizzo * On RX, hwcur->hwtail are receive buffers 35817885a7bSLuigi Rizzo * not yet released. hwcur is advanced following 35917885a7bSLuigi Rizzo * ring->head, hwtail is advanced on incoming packets, 36017885a7bSLuigi Rizzo * and a wakeup is generated when hwtail passes ring->cur 36117885a7bSLuigi Rizzo * On TX, hwcur->rcur have been filled by the sender 36217885a7bSLuigi Rizzo * but not sent yet to the NIC; rcur->hwtail are available 36317885a7bSLuigi Rizzo * for new transmissions, and hwtail->hwcur-1 are pending 36417885a7bSLuigi Rizzo * transmissions not yet acknowledged. 36568b8534bSLuigi Rizzo * 3661a26580eSLuigi Rizzo * The indexes in the NIC and netmap rings are offset by nkr_hwofs slots. 36768b8534bSLuigi Rizzo * This is so that, on a reset, buffers owned by userspace are not 36868b8534bSLuigi Rizzo * modified by the kernel. In particular: 36917885a7bSLuigi Rizzo * RX rings: the next empty buffer (hwtail + hwofs) coincides with 37068b8534bSLuigi Rizzo * the next empty buffer as known by the hardware (next_to_check or so). 37168b8534bSLuigi Rizzo * TX rings: hwcur + hwofs coincides with next_to_send 3721dce924dSLuigi Rizzo * 3731dce924dSLuigi Rizzo * For received packets, slot->flags is set to nkr_slot_flags 3741dce924dSLuigi Rizzo * so we can provide a proper initial value (e.g. set NS_FORWARD 3751dce924dSLuigi Rizzo * when operating in 'transparent' mode). 376ce3ee1e7SLuigi Rizzo * 377ce3ee1e7SLuigi Rizzo * The following fields are used to implement lock-free copy of packets 378ce3ee1e7SLuigi Rizzo * from input to output ports in VALE switch: 379ce3ee1e7SLuigi Rizzo * nkr_hwlease buffer after the last one being copied. 380ce3ee1e7SLuigi Rizzo * A writer in nm_bdg_flush reserves N buffers 381ce3ee1e7SLuigi Rizzo * from nr_hwlease, advances it, then does the 382ce3ee1e7SLuigi Rizzo * copy outside the lock. 383ce3ee1e7SLuigi Rizzo * In RX rings (used for VALE ports), 38417885a7bSLuigi Rizzo * nkr_hwtail <= nkr_hwlease < nkr_hwcur+N-1 385ce3ee1e7SLuigi Rizzo * In TX rings (used for NIC or host stack ports) 38617885a7bSLuigi Rizzo * nkr_hwcur <= nkr_hwlease < nkr_hwtail 387ce3ee1e7SLuigi Rizzo * nkr_leases array of nkr_num_slots where writers can report 388ce3ee1e7SLuigi Rizzo * completion of their block. NR_NOSLOT (~0) indicates 389ce3ee1e7SLuigi Rizzo * that the writer has not finished yet 390ce3ee1e7SLuigi Rizzo * nkr_lease_idx index of next free slot in nr_leases, to be assigned 391ce3ee1e7SLuigi Rizzo * 392ce3ee1e7SLuigi Rizzo * The kring is manipulated by txsync/rxsync and generic netmap function. 39317885a7bSLuigi Rizzo * 39417885a7bSLuigi Rizzo * Concurrent rxsync or txsync on the same ring are prevented through 39589cc2556SLuigi Rizzo * by nm_kr_(try)lock() which in turn uses nr_busy. This is all we need 39617885a7bSLuigi Rizzo * for NIC rings, and for TX rings attached to the host stack. 39717885a7bSLuigi Rizzo * 39817885a7bSLuigi Rizzo * RX rings attached to the host stack use an mbq (rx_queue) on both 39917885a7bSLuigi Rizzo * rxsync_from_host() and netmap_transmit(). The mbq is protected 40017885a7bSLuigi Rizzo * by its internal lock. 40117885a7bSLuigi Rizzo * 4024bf50f18SLuigi Rizzo * RX rings attached to the VALE switch are accessed by both senders 40317885a7bSLuigi Rizzo * and receiver. They are protected through the q_lock on the RX ring. 40468b8534bSLuigi Rizzo */ 40568b8534bSLuigi Rizzo struct netmap_kring { 40668b8534bSLuigi Rizzo struct netmap_ring *ring; 40717885a7bSLuigi Rizzo 408ce3ee1e7SLuigi Rizzo uint32_t nr_hwcur; 40917885a7bSLuigi Rizzo uint32_t nr_hwtail; 41017885a7bSLuigi Rizzo 41117885a7bSLuigi Rizzo /* 41217885a7bSLuigi Rizzo * Copies of values in user rings, so we do not need to look 41317885a7bSLuigi Rizzo * at the ring (which could be modified). These are set in the 41417885a7bSLuigi Rizzo * *sync_prologue()/finalize() routines. 41517885a7bSLuigi Rizzo */ 41617885a7bSLuigi Rizzo uint32_t rhead; 41717885a7bSLuigi Rizzo uint32_t rcur; 41817885a7bSLuigi Rizzo uint32_t rtail; 41917885a7bSLuigi Rizzo 420ce3ee1e7SLuigi Rizzo uint32_t nr_kflags; /* private driver flags */ 4212157a17cSLuigi Rizzo #define NKR_PENDINTR 0x1 // Pending interrupt. 422847bf383SLuigi Rizzo #define NKR_EXCLUSIVE 0x2 /* exclusive binding */ 42337e3a6d3SLuigi Rizzo #define NKR_FORWARD 0x4 /* (host ring only) there are 42437e3a6d3SLuigi Rizzo packets to forward 42537e3a6d3SLuigi Rizzo */ 42637e3a6d3SLuigi Rizzo #define NKR_NEEDRING 0x8 /* ring needed even if users==0 42737e3a6d3SLuigi Rizzo * (used internally by pipes and 42837e3a6d3SLuigi Rizzo * by ptnetmap host ports) 42937e3a6d3SLuigi Rizzo */ 43037e3a6d3SLuigi Rizzo 43137e3a6d3SLuigi Rizzo uint32_t nr_mode; 43237e3a6d3SLuigi Rizzo uint32_t nr_pending_mode; 43337e3a6d3SLuigi Rizzo #define NKR_NETMAP_OFF 0x0 43437e3a6d3SLuigi Rizzo #define NKR_NETMAP_ON 0x1 43537e3a6d3SLuigi Rizzo 436ce3ee1e7SLuigi Rizzo uint32_t nkr_num_slots; 43717885a7bSLuigi Rizzo 43817885a7bSLuigi Rizzo /* 43917885a7bSLuigi Rizzo * On a NIC reset, the NIC ring indexes may be reset but the 44017885a7bSLuigi Rizzo * indexes in the netmap rings remain the same. nkr_hwofs 44117885a7bSLuigi Rizzo * keeps track of the offset between the two. 44217885a7bSLuigi Rizzo */ 44317885a7bSLuigi Rizzo int32_t nkr_hwofs; 44468b8534bSLuigi Rizzo 4451dce924dSLuigi Rizzo uint16_t nkr_slot_flags; /* initial value for flags */ 44617885a7bSLuigi Rizzo 44717885a7bSLuigi Rizzo /* last_reclaim is opaque marker to help reduce the frequency 44817885a7bSLuigi Rizzo * of operations such as reclaiming tx buffers. A possible use 44917885a7bSLuigi Rizzo * is set it to ticks and do the reclaim only once per tick. 45017885a7bSLuigi Rizzo */ 45117885a7bSLuigi Rizzo uint64_t last_reclaim; 45217885a7bSLuigi Rizzo 453ce3ee1e7SLuigi Rizzo 4541a26580eSLuigi Rizzo NM_SELINFO_T si; /* poll/select wait queue */ 455ce3ee1e7SLuigi Rizzo NM_LOCK_T q_lock; /* protects kring and ring. */ 456ce3ee1e7SLuigi Rizzo NM_ATOMIC_T nr_busy; /* prevent concurrent syscalls */ 457ce3ee1e7SLuigi Rizzo 45817885a7bSLuigi Rizzo struct netmap_adapter *na; 45917885a7bSLuigi Rizzo 4606435a0dcSLuigi Rizzo /* The following fields are for VALE switch support */ 46117885a7bSLuigi Rizzo struct nm_bdg_fwd *nkr_ft; 46217885a7bSLuigi Rizzo uint32_t *nkr_leases; 46317885a7bSLuigi Rizzo #define NR_NOSLOT ((uint32_t)~0) /* used in nkr_*lease* */ 46417885a7bSLuigi Rizzo uint32_t nkr_hwlease; 46517885a7bSLuigi Rizzo uint32_t nkr_lease_idx; 46617885a7bSLuigi Rizzo 4674bf50f18SLuigi Rizzo /* while nkr_stopped is set, no new [tr]xsync operations can 4684bf50f18SLuigi Rizzo * be started on this kring. 4694bf50f18SLuigi Rizzo * This is used by netmap_disable_all_rings() 4704bf50f18SLuigi Rizzo * to find a synchronization point where critical data 4714bf50f18SLuigi Rizzo * structures pointed to by the kring can be added or removed 4724bf50f18SLuigi Rizzo */ 4734bf50f18SLuigi Rizzo volatile int nkr_stopped; 474f9790aebSLuigi Rizzo 475f0ea3689SLuigi Rizzo /* Support for adapters without native netmap support. 476f9790aebSLuigi Rizzo * On tx rings we preallocate an array of tx buffers 477f9790aebSLuigi Rizzo * (same size as the netmap ring), on rx rings we 478f0ea3689SLuigi Rizzo * store incoming mbufs in a queue that is drained by 479f0ea3689SLuigi Rizzo * a rxsync. 480f9790aebSLuigi Rizzo */ 481f9790aebSLuigi Rizzo struct mbuf **tx_pool; 48237e3a6d3SLuigi Rizzo struct mbuf *tx_event; /* TX event used as a notification */ 48337e3a6d3SLuigi Rizzo NM_LOCK_T tx_event_lock; /* protects the tx_event mbuf */ 48417885a7bSLuigi Rizzo struct mbq rx_queue; /* intercepted rx mbufs. */ 48517885a7bSLuigi Rizzo 486847bf383SLuigi Rizzo uint32_t users; /* existing bindings for this ring */ 487847bf383SLuigi Rizzo 48837e3a6d3SLuigi Rizzo uint32_t ring_id; /* kring identifier */ 489847bf383SLuigi Rizzo enum txrx tx; /* kind of ring (tx or rx) */ 49017885a7bSLuigi Rizzo char name[64]; /* diagnostic */ 491f9790aebSLuigi Rizzo 4924bf50f18SLuigi Rizzo /* [tx]sync callback for this kring. 4934bf50f18SLuigi Rizzo * The default nm_kring_create callback (netmap_krings_create) 4944bf50f18SLuigi Rizzo * sets the nm_sync callback of each hardware tx(rx) kring to 4954bf50f18SLuigi Rizzo * the corresponding nm_txsync(nm_rxsync) taken from the 4964bf50f18SLuigi Rizzo * netmap_adapter; moreover, it sets the sync callback 4974bf50f18SLuigi Rizzo * of the host tx(rx) ring to netmap_txsync_to_host 4984bf50f18SLuigi Rizzo * (netmap_rxsync_from_host). 4994bf50f18SLuigi Rizzo * 5004bf50f18SLuigi Rizzo * Overrides: the above configuration is not changed by 5014bf50f18SLuigi Rizzo * any of the nm_krings_create callbacks. 5024bf50f18SLuigi Rizzo */ 503f0ea3689SLuigi Rizzo int (*nm_sync)(struct netmap_kring *kring, int flags); 504847bf383SLuigi Rizzo int (*nm_notify)(struct netmap_kring *kring, int flags); 505f0ea3689SLuigi Rizzo 506f0ea3689SLuigi Rizzo #ifdef WITH_PIPES 5074bf50f18SLuigi Rizzo struct netmap_kring *pipe; /* if this is a pipe ring, 5084bf50f18SLuigi Rizzo * pointer to the other end 5094bf50f18SLuigi Rizzo */ 510f0ea3689SLuigi Rizzo #endif /* WITH_PIPES */ 511f0ea3689SLuigi Rizzo 512847bf383SLuigi Rizzo #ifdef WITH_VALE 513847bf383SLuigi Rizzo int (*save_notify)(struct netmap_kring *kring, int flags); 514847bf383SLuigi Rizzo #endif 515847bf383SLuigi Rizzo 5164bf50f18SLuigi Rizzo #ifdef WITH_MONITOR 517847bf383SLuigi Rizzo /* array of krings that are monitoring this kring */ 518847bf383SLuigi Rizzo struct netmap_kring **monitors; 519847bf383SLuigi Rizzo uint32_t max_monitors; /* current size of the monitors array */ 520847bf383SLuigi Rizzo uint32_t n_monitors; /* next unused entry in the monitor array */ 521c3e9b4dbSLuiz Otavio O Souza uint32_t mon_pos[NR_TXRX]; /* index of this ring in the monitored ring array */ 522c3e9b4dbSLuiz Otavio O Souza uint32_t mon_tail; /* last seen slot on rx */ 523c3e9b4dbSLuiz Otavio O Souza 524c3e9b4dbSLuiz Otavio O Souza /* circular list of zero-copy monitors */ 525c3e9b4dbSLuiz Otavio O Souza struct netmap_zmon_list zmon_list[NR_TXRX]; 526c3e9b4dbSLuiz Otavio O Souza 5274bf50f18SLuigi Rizzo /* 528847bf383SLuigi Rizzo * Monitors work by intercepting the sync and notify callbacks of the 529847bf383SLuigi Rizzo * monitored krings. This is implemented by replacing the pointers 530847bf383SLuigi Rizzo * above and saving the previous ones in mon_* pointers below 5314bf50f18SLuigi Rizzo */ 532847bf383SLuigi Rizzo int (*mon_sync)(struct netmap_kring *kring, int flags); 533847bf383SLuigi Rizzo int (*mon_notify)(struct netmap_kring *kring, int flags); 534847bf383SLuigi Rizzo 5354bf50f18SLuigi Rizzo #endif 53637e3a6d3SLuigi Rizzo } 53737e3a6d3SLuigi Rizzo #ifdef _WIN32 53837e3a6d3SLuigi Rizzo __declspec(align(64)); 53937e3a6d3SLuigi Rizzo #else 54037e3a6d3SLuigi Rizzo __attribute__((__aligned__(64))); 54137e3a6d3SLuigi Rizzo #endif 54268b8534bSLuigi Rizzo 54337e3a6d3SLuigi Rizzo /* return 1 iff the kring needs to be turned on */ 54437e3a6d3SLuigi Rizzo static inline int 54537e3a6d3SLuigi Rizzo nm_kring_pending_on(struct netmap_kring *kring) 54637e3a6d3SLuigi Rizzo { 54737e3a6d3SLuigi Rizzo return kring->nr_pending_mode == NKR_NETMAP_ON && 54837e3a6d3SLuigi Rizzo kring->nr_mode == NKR_NETMAP_OFF; 54937e3a6d3SLuigi Rizzo } 55037e3a6d3SLuigi Rizzo 55137e3a6d3SLuigi Rizzo /* return 1 iff the kring needs to be turned off */ 55237e3a6d3SLuigi Rizzo static inline int 55337e3a6d3SLuigi Rizzo nm_kring_pending_off(struct netmap_kring *kring) 55437e3a6d3SLuigi Rizzo { 55537e3a6d3SLuigi Rizzo return kring->nr_pending_mode == NKR_NETMAP_OFF && 55637e3a6d3SLuigi Rizzo kring->nr_mode == NKR_NETMAP_ON; 55737e3a6d3SLuigi Rizzo } 558ce3ee1e7SLuigi Rizzo 559ce3ee1e7SLuigi Rizzo /* return the next index, with wraparound */ 560ce3ee1e7SLuigi Rizzo static inline uint32_t 561ce3ee1e7SLuigi Rizzo nm_next(uint32_t i, uint32_t lim) 562ce3ee1e7SLuigi Rizzo { 563ce3ee1e7SLuigi Rizzo return unlikely (i == lim) ? 0 : i + 1; 564ce3ee1e7SLuigi Rizzo } 565ce3ee1e7SLuigi Rizzo 56617885a7bSLuigi Rizzo 56717885a7bSLuigi Rizzo /* return the previous index, with wraparound */ 56817885a7bSLuigi Rizzo static inline uint32_t 56917885a7bSLuigi Rizzo nm_prev(uint32_t i, uint32_t lim) 57017885a7bSLuigi Rizzo { 57117885a7bSLuigi Rizzo return unlikely (i == 0) ? lim : i - 1; 57217885a7bSLuigi Rizzo } 57317885a7bSLuigi Rizzo 57417885a7bSLuigi Rizzo 575ce3ee1e7SLuigi Rizzo /* 576ce3ee1e7SLuigi Rizzo * 577ce3ee1e7SLuigi Rizzo * Here is the layout for the Rx and Tx rings. 578ce3ee1e7SLuigi Rizzo 579ce3ee1e7SLuigi Rizzo RxRING TxRING 580ce3ee1e7SLuigi Rizzo 581ce3ee1e7SLuigi Rizzo +-----------------+ +-----------------+ 582ce3ee1e7SLuigi Rizzo | | | | 583ce3ee1e7SLuigi Rizzo |XXX free slot XXX| |XXX free slot XXX| 584ce3ee1e7SLuigi Rizzo +-----------------+ +-----------------+ 58517885a7bSLuigi Rizzo head->| owned by user |<-hwcur | not sent to nic |<-hwcur 58617885a7bSLuigi Rizzo | | | yet | 58717885a7bSLuigi Rizzo +-----------------+ | | 58817885a7bSLuigi Rizzo cur->| available to | | | 58917885a7bSLuigi Rizzo | user, not read | +-----------------+ 59017885a7bSLuigi Rizzo | yet | cur->| (being | 59117885a7bSLuigi Rizzo | | | prepared) | 592ce3ee1e7SLuigi Rizzo | | | | 59317885a7bSLuigi Rizzo +-----------------+ + ------ + 59417885a7bSLuigi Rizzo tail->| |<-hwtail | |<-hwlease 59517885a7bSLuigi Rizzo | (being | ... | | ... 59617885a7bSLuigi Rizzo | prepared) | ... | | ... 59717885a7bSLuigi Rizzo +-----------------+ ... | | ... 59817885a7bSLuigi Rizzo | |<-hwlease +-----------------+ 59917885a7bSLuigi Rizzo | | tail->| |<-hwtail 600ce3ee1e7SLuigi Rizzo | | | | 601ce3ee1e7SLuigi Rizzo | | | | 602ce3ee1e7SLuigi Rizzo | | | | 603ce3ee1e7SLuigi Rizzo +-----------------+ +-----------------+ 604ce3ee1e7SLuigi Rizzo 60517885a7bSLuigi Rizzo * The cur/tail (user view) and hwcur/hwtail (kernel view) 606ce3ee1e7SLuigi Rizzo * are used in the normal operation of the card. 607ce3ee1e7SLuigi Rizzo * 608ce3ee1e7SLuigi Rizzo * When a ring is the output of a switch port (Rx ring for 609ce3ee1e7SLuigi Rizzo * a VALE port, Tx ring for the host stack or NIC), slots 610ce3ee1e7SLuigi Rizzo * are reserved in blocks through 'hwlease' which points 611ce3ee1e7SLuigi Rizzo * to the next unused slot. 61217885a7bSLuigi Rizzo * On an Rx ring, hwlease is always after hwtail, 61317885a7bSLuigi Rizzo * and completions cause hwtail to advance. 61417885a7bSLuigi Rizzo * On a Tx ring, hwlease is always between cur and hwtail, 615ce3ee1e7SLuigi Rizzo * and completions cause cur to advance. 616ce3ee1e7SLuigi Rizzo * 617ce3ee1e7SLuigi Rizzo * nm_kr_space() returns the maximum number of slots that 618ce3ee1e7SLuigi Rizzo * can be assigned. 619ce3ee1e7SLuigi Rizzo * nm_kr_lease() reserves the required number of buffers, 620ce3ee1e7SLuigi Rizzo * advances nkr_hwlease and also returns an entry in 621ce3ee1e7SLuigi Rizzo * a circular array where completions should be reported. 622ce3ee1e7SLuigi Rizzo */ 623ce3ee1e7SLuigi Rizzo 624ce3ee1e7SLuigi Rizzo 625847bf383SLuigi Rizzo struct netmap_lut { 626847bf383SLuigi Rizzo struct lut_entry *lut; 627847bf383SLuigi Rizzo uint32_t objtotal; /* max buffer index */ 628847bf383SLuigi Rizzo uint32_t objsize; /* buffer size */ 629847bf383SLuigi Rizzo }; 630ce3ee1e7SLuigi Rizzo 6314bf50f18SLuigi Rizzo struct netmap_vp_adapter; // forward 6324bf50f18SLuigi Rizzo 63368b8534bSLuigi Rizzo /* 634f9790aebSLuigi Rizzo * The "struct netmap_adapter" extends the "struct adapter" 635f9790aebSLuigi Rizzo * (or equivalent) device descriptor. 636f9790aebSLuigi Rizzo * It contains all base fields needed to support netmap operation. 637f9790aebSLuigi Rizzo * There are in fact different types of netmap adapters 638f9790aebSLuigi Rizzo * (native, generic, VALE switch...) so a netmap_adapter is 639f9790aebSLuigi Rizzo * just the first field in the derived type. 64068b8534bSLuigi Rizzo */ 64168b8534bSLuigi Rizzo struct netmap_adapter { 6428241616dSLuigi Rizzo /* 6438241616dSLuigi Rizzo * On linux we do not have a good way to tell if an interface 644f9790aebSLuigi Rizzo * is netmap-capable. So we always use the following trick: 6458241616dSLuigi Rizzo * NA(ifp) points here, and the first entry (which hopefully 6468241616dSLuigi Rizzo * always exists and is at least 32 bits) contains a magic 6478241616dSLuigi Rizzo * value which we can use to detect that the interface is good. 6488241616dSLuigi Rizzo */ 6498241616dSLuigi Rizzo uint32_t magic; 650f9790aebSLuigi Rizzo uint32_t na_flags; /* enabled, and other flags */ 6518241616dSLuigi Rizzo #define NAF_SKIP_INTR 1 /* use the regular interrupt handler. 6528241616dSLuigi Rizzo * useful during initialization 6538241616dSLuigi Rizzo */ 654f18be576SLuigi Rizzo #define NAF_SW_ONLY 2 /* forward packets only to sw adapter */ 655ce3ee1e7SLuigi Rizzo #define NAF_BDG_MAYSLEEP 4 /* the bridge is allowed to sleep when 656ce3ee1e7SLuigi Rizzo * forwarding packets coming from this 657ce3ee1e7SLuigi Rizzo * interface 658ce3ee1e7SLuigi Rizzo */ 659847bf383SLuigi Rizzo #define NAF_MEM_OWNER 8 /* the adapter uses its own memory area 660847bf383SLuigi Rizzo * that cannot be changed 661ce3ee1e7SLuigi Rizzo */ 662847bf383SLuigi Rizzo #define NAF_NATIVE 16 /* the adapter is native. 66385fe4e7cSLuigi Rizzo * Virtual ports (non persistent vale ports, 66485fe4e7cSLuigi Rizzo * pipes, monitors...) should never use 66585fe4e7cSLuigi Rizzo * this flag. 666f9790aebSLuigi Rizzo */ 667f9790aebSLuigi Rizzo #define NAF_NETMAP_ON 32 /* netmap is active (either native or 6684bf50f18SLuigi Rizzo * emulated). Where possible (e.g. FreeBSD) 669f9790aebSLuigi Rizzo * IFCAP_NETMAP also mirrors this flag. 670f9790aebSLuigi Rizzo */ 671f0ea3689SLuigi Rizzo #define NAF_HOST_RINGS 64 /* the adapter supports the host rings */ 6724bf50f18SLuigi Rizzo #define NAF_FORCE_NATIVE 128 /* the adapter is always NATIVE */ 67337e3a6d3SLuigi Rizzo #define NAF_PTNETMAP_HOST 256 /* the adapter supports ptnetmap in the host */ 67437e3a6d3SLuigi Rizzo #define NAF_ZOMBIE (1U<<30) /* the nic driver has been unloaded */ 6754bf50f18SLuigi Rizzo #define NAF_BUSY (1U<<31) /* the adapter is used internally and 6764bf50f18SLuigi Rizzo * cannot be registered from userspace 6774bf50f18SLuigi Rizzo */ 678f9790aebSLuigi Rizzo int active_fds; /* number of user-space descriptors using this 67968b8534bSLuigi Rizzo interface, which is equal to the number of 68068b8534bSLuigi Rizzo struct netmap_if objs in the mapped region. */ 68168b8534bSLuigi Rizzo 68224e57ec9SEd Maste u_int num_rx_rings; /* number of adapter receive rings */ 68324e57ec9SEd Maste u_int num_tx_rings; /* number of adapter transmit rings */ 68468b8534bSLuigi Rizzo 68568b8534bSLuigi Rizzo u_int num_tx_desc; /* number of descriptor in each queue */ 68668b8534bSLuigi Rizzo u_int num_rx_desc; 68768b8534bSLuigi Rizzo 68868b8534bSLuigi Rizzo /* tx_rings and rx_rings are private but allocated 68968b8534bSLuigi Rizzo * as a contiguous chunk of memory. Each array has 69068b8534bSLuigi Rizzo * N+1 entries, for the adapter queues and for the host queue. 69168b8534bSLuigi Rizzo */ 69268b8534bSLuigi Rizzo struct netmap_kring *tx_rings; /* array of TX rings. */ 69368b8534bSLuigi Rizzo struct netmap_kring *rx_rings; /* array of RX rings. */ 69417885a7bSLuigi Rizzo 695f9790aebSLuigi Rizzo void *tailroom; /* space below the rings array */ 696f9790aebSLuigi Rizzo /* (used for leases) */ 697f9790aebSLuigi Rizzo 69868b8534bSLuigi Rizzo 699847bf383SLuigi Rizzo NM_SELINFO_T si[NR_TXRX]; /* global wait queues */ 70064ae02c3SLuigi Rizzo 701f0ea3689SLuigi Rizzo /* count users of the global wait queues */ 702847bf383SLuigi Rizzo int si_users[NR_TXRX]; 703f0ea3689SLuigi Rizzo 7044bf50f18SLuigi Rizzo void *pdev; /* used to store pci device */ 7054bf50f18SLuigi Rizzo 70668b8534bSLuigi Rizzo /* copy of if_qflush and if_transmit pointers, to intercept 70768b8534bSLuigi Rizzo * packets from the network stack when netmap is active. 70868b8534bSLuigi Rizzo */ 70968b8534bSLuigi Rizzo int (*if_transmit)(struct ifnet *, struct mbuf *); 71068b8534bSLuigi Rizzo 71117885a7bSLuigi Rizzo /* copy of if_input for netmap_send_up() */ 71217885a7bSLuigi Rizzo void (*if_input)(struct ifnet *, struct mbuf *); 71317885a7bSLuigi Rizzo 71468b8534bSLuigi Rizzo /* references to the ifnet and device routines, used by 71568b8534bSLuigi Rizzo * the generic netmap functions. 71668b8534bSLuigi Rizzo */ 71768b8534bSLuigi Rizzo struct ifnet *ifp; /* adapter is ifp->if_softc */ 71868b8534bSLuigi Rizzo 71917885a7bSLuigi Rizzo /*---- callbacks for this netmap adapter -----*/ 72017885a7bSLuigi Rizzo /* 72117885a7bSLuigi Rizzo * nm_dtor() is the cleanup routine called when destroying 72217885a7bSLuigi Rizzo * the adapter. 72389cc2556SLuigi Rizzo * Called with NMG_LOCK held. 72417885a7bSLuigi Rizzo * 72517885a7bSLuigi Rizzo * nm_register() is called on NIOCREGIF and close() to enter 72617885a7bSLuigi Rizzo * or exit netmap mode on the NIC 7274bf50f18SLuigi Rizzo * Called with NNG_LOCK held. 72817885a7bSLuigi Rizzo * 72917885a7bSLuigi Rizzo * nm_txsync() pushes packets to the underlying hw/switch 73017885a7bSLuigi Rizzo * 73117885a7bSLuigi Rizzo * nm_rxsync() collects packets from the underlying hw/switch 73217885a7bSLuigi Rizzo * 73317885a7bSLuigi Rizzo * nm_config() returns configuration information from the OS 73489cc2556SLuigi Rizzo * Called with NMG_LOCK held. 73517885a7bSLuigi Rizzo * 7364bf50f18SLuigi Rizzo * nm_krings_create() create and init the tx_rings and 7374bf50f18SLuigi Rizzo * rx_rings arrays of kring structures. In particular, 7384bf50f18SLuigi Rizzo * set the nm_sync callbacks for each ring. 7394bf50f18SLuigi Rizzo * There is no need to also allocate the corresponding 7404bf50f18SLuigi Rizzo * netmap_rings, since netmap_mem_rings_create() will always 7414bf50f18SLuigi Rizzo * be called to provide the missing ones. 7424bf50f18SLuigi Rizzo * Called with NNG_LOCK held. 74317885a7bSLuigi Rizzo * 7444bf50f18SLuigi Rizzo * nm_krings_delete() cleanup and delete the tx_rings and rx_rings 7454bf50f18SLuigi Rizzo * arrays 7464bf50f18SLuigi Rizzo * Called with NMG_LOCK held. 74717885a7bSLuigi Rizzo * 74889cc2556SLuigi Rizzo * nm_notify() is used to act after data have become available 74989cc2556SLuigi Rizzo * (or the stopped state of the ring has changed) 75017885a7bSLuigi Rizzo * For hw devices this is typically a selwakeup(), 75117885a7bSLuigi Rizzo * but for NIC/host ports attached to a switch (or vice-versa) 75217885a7bSLuigi Rizzo * we also need to invoke the 'txsync' code downstream. 75337e3a6d3SLuigi Rizzo * This callback pointer is actually used only to initialize 75437e3a6d3SLuigi Rizzo * kring->nm_notify. 75537e3a6d3SLuigi Rizzo * Return values are the same as for netmap_rx_irq(). 75617885a7bSLuigi Rizzo */ 757f9790aebSLuigi Rizzo void (*nm_dtor)(struct netmap_adapter *); 7581a26580eSLuigi Rizzo 759f9790aebSLuigi Rizzo int (*nm_register)(struct netmap_adapter *, int onoff); 76037e3a6d3SLuigi Rizzo void (*nm_intr)(struct netmap_adapter *, int onoff); 761ce3ee1e7SLuigi Rizzo 7624bf50f18SLuigi Rizzo int (*nm_txsync)(struct netmap_kring *kring, int flags); 7634bf50f18SLuigi Rizzo int (*nm_rxsync)(struct netmap_kring *kring, int flags); 764847bf383SLuigi Rizzo int (*nm_notify)(struct netmap_kring *kring, int flags); 765ce3ee1e7SLuigi Rizzo #define NAF_FORCE_READ 1 766ce3ee1e7SLuigi Rizzo #define NAF_FORCE_RECLAIM 2 767c3e9b4dbSLuiz Otavio O Souza #define NAF_CAN_FORWARD_DOWN 4 768ae10d1afSLuigi Rizzo /* return configuration information */ 769f9790aebSLuigi Rizzo int (*nm_config)(struct netmap_adapter *, 770f9790aebSLuigi Rizzo u_int *txr, u_int *txd, u_int *rxr, u_int *rxd); 771f9790aebSLuigi Rizzo int (*nm_krings_create)(struct netmap_adapter *); 772f9790aebSLuigi Rizzo void (*nm_krings_delete)(struct netmap_adapter *); 7734bf50f18SLuigi Rizzo #ifdef WITH_VALE 7744bf50f18SLuigi Rizzo /* 7754bf50f18SLuigi Rizzo * nm_bdg_attach() initializes the na_vp field to point 7764bf50f18SLuigi Rizzo * to an adapter that can be attached to a VALE switch. If the 7774bf50f18SLuigi Rizzo * current adapter is already a VALE port, na_vp is simply a cast; 7784bf50f18SLuigi Rizzo * otherwise, na_vp points to a netmap_bwrap_adapter. 7794bf50f18SLuigi Rizzo * If applicable, this callback also initializes na_hostvp, 7804bf50f18SLuigi Rizzo * that can be used to connect the adapter host rings to the 7814bf50f18SLuigi Rizzo * switch. 7824bf50f18SLuigi Rizzo * Called with NMG_LOCK held. 7834bf50f18SLuigi Rizzo * 7844bf50f18SLuigi Rizzo * nm_bdg_ctl() is called on the actual attach/detach to/from 7854bf50f18SLuigi Rizzo * to/from the switch, to perform adapter-specific 7864bf50f18SLuigi Rizzo * initializations 7874bf50f18SLuigi Rizzo * Called with NMG_LOCK held. 7884bf50f18SLuigi Rizzo */ 7894bf50f18SLuigi Rizzo int (*nm_bdg_attach)(const char *bdg_name, struct netmap_adapter *); 7904bf50f18SLuigi Rizzo int (*nm_bdg_ctl)(struct netmap_adapter *, struct nmreq *, int); 7914bf50f18SLuigi Rizzo 7924bf50f18SLuigi Rizzo /* adapter used to attach this adapter to a VALE switch (if any) */ 7934bf50f18SLuigi Rizzo struct netmap_vp_adapter *na_vp; 7944bf50f18SLuigi Rizzo /* adapter used to attach the host rings of this adapter 7954bf50f18SLuigi Rizzo * to a VALE switch (if any) */ 7964bf50f18SLuigi Rizzo struct netmap_vp_adapter *na_hostvp; 7974bf50f18SLuigi Rizzo #endif 798f9790aebSLuigi Rizzo 799f9790aebSLuigi Rizzo /* standard refcount to control the lifetime of the adapter 800f9790aebSLuigi Rizzo * (it should be equal to the lifetime of the corresponding ifp) 801f9790aebSLuigi Rizzo */ 802f9790aebSLuigi Rizzo int na_refcount; 803f9790aebSLuigi Rizzo 804f9790aebSLuigi Rizzo /* memory allocator (opaque) 805f9790aebSLuigi Rizzo * We also cache a pointer to the lut_entry for translating 80637e3a6d3SLuigi Rizzo * buffer addresses, the total number of buffers and the buffer size. 807f9790aebSLuigi Rizzo */ 808f9790aebSLuigi Rizzo struct netmap_mem_d *nm_mem; 809847bf383SLuigi Rizzo struct netmap_lut na_lut; 810f9790aebSLuigi Rizzo 8114bf50f18SLuigi Rizzo /* additional information attached to this adapter 8124bf50f18SLuigi Rizzo * by other netmap subsystems. Currently used by 81337e3a6d3SLuigi Rizzo * bwrap, LINUX/v1000 and ptnetmap 814f9790aebSLuigi Rizzo */ 815f9790aebSLuigi Rizzo void *na_private; 816f0ea3689SLuigi Rizzo 8174bf50f18SLuigi Rizzo /* array of pipes that have this adapter as a parent */ 818f0ea3689SLuigi Rizzo struct netmap_pipe_adapter **na_pipes; 8194bf50f18SLuigi Rizzo int na_next_pipe; /* next free slot in the array */ 8204bf50f18SLuigi Rizzo int na_max_pipes; /* size of the array */ 8214bf50f18SLuigi Rizzo 82237e3a6d3SLuigi Rizzo /* Offset of ethernet header for each packet. */ 82337e3a6d3SLuigi Rizzo u_int virt_hdr_len; 82437e3a6d3SLuigi Rizzo 8254bf50f18SLuigi Rizzo char name[64]; 826f9790aebSLuigi Rizzo }; 827f9790aebSLuigi Rizzo 828847bf383SLuigi Rizzo static __inline u_int 829847bf383SLuigi Rizzo nma_get_ndesc(struct netmap_adapter *na, enum txrx t) 830847bf383SLuigi Rizzo { 831847bf383SLuigi Rizzo return (t == NR_TX ? na->num_tx_desc : na->num_rx_desc); 832847bf383SLuigi Rizzo } 833847bf383SLuigi Rizzo 834847bf383SLuigi Rizzo static __inline void 835847bf383SLuigi Rizzo nma_set_ndesc(struct netmap_adapter *na, enum txrx t, u_int v) 836847bf383SLuigi Rizzo { 837847bf383SLuigi Rizzo if (t == NR_TX) 838847bf383SLuigi Rizzo na->num_tx_desc = v; 839847bf383SLuigi Rizzo else 840847bf383SLuigi Rizzo na->num_rx_desc = v; 841847bf383SLuigi Rizzo } 842847bf383SLuigi Rizzo 843847bf383SLuigi Rizzo static __inline u_int 844847bf383SLuigi Rizzo nma_get_nrings(struct netmap_adapter *na, enum txrx t) 845847bf383SLuigi Rizzo { 846847bf383SLuigi Rizzo return (t == NR_TX ? na->num_tx_rings : na->num_rx_rings); 847847bf383SLuigi Rizzo } 848847bf383SLuigi Rizzo 849847bf383SLuigi Rizzo static __inline void 850847bf383SLuigi Rizzo nma_set_nrings(struct netmap_adapter *na, enum txrx t, u_int v) 851847bf383SLuigi Rizzo { 852847bf383SLuigi Rizzo if (t == NR_TX) 853847bf383SLuigi Rizzo na->num_tx_rings = v; 854847bf383SLuigi Rizzo else 855847bf383SLuigi Rizzo na->num_rx_rings = v; 856847bf383SLuigi Rizzo } 857847bf383SLuigi Rizzo 858847bf383SLuigi Rizzo static __inline struct netmap_kring* 859847bf383SLuigi Rizzo NMR(struct netmap_adapter *na, enum txrx t) 860847bf383SLuigi Rizzo { 861847bf383SLuigi Rizzo return (t == NR_TX ? na->tx_rings : na->rx_rings); 862847bf383SLuigi Rizzo } 86317885a7bSLuigi Rizzo 864f9790aebSLuigi Rizzo /* 865f9790aebSLuigi Rizzo * If the NIC is owned by the kernel 866f9790aebSLuigi Rizzo * (i.e., bridge), neither another bridge nor user can use it; 867f9790aebSLuigi Rizzo * if the NIC is owned by a user, only users can share it. 868f9790aebSLuigi Rizzo * Evaluation must be done under NMG_LOCK(). 869f9790aebSLuigi Rizzo */ 8704bf50f18SLuigi Rizzo #define NETMAP_OWNED_BY_KERN(na) ((na)->na_flags & NAF_BUSY) 871f9790aebSLuigi Rizzo #define NETMAP_OWNED_BY_ANY(na) \ 8724bf50f18SLuigi Rizzo (NETMAP_OWNED_BY_KERN(na) || ((na)->active_fds > 0)) 873f9790aebSLuigi Rizzo 874f9790aebSLuigi Rizzo /* 875f9790aebSLuigi Rizzo * derived netmap adapters for various types of ports 876f9790aebSLuigi Rizzo */ 877f9790aebSLuigi Rizzo struct netmap_vp_adapter { /* VALE software port */ 878f9790aebSLuigi Rizzo struct netmap_adapter up; 879f196ce38SLuigi Rizzo 880849bec0eSLuigi Rizzo /* 881849bec0eSLuigi Rizzo * Bridge support: 882849bec0eSLuigi Rizzo * 883849bec0eSLuigi Rizzo * bdg_port is the port number used in the bridge; 884f18be576SLuigi Rizzo * na_bdg points to the bridge this NA is attached to. 885849bec0eSLuigi Rizzo */ 886f196ce38SLuigi Rizzo int bdg_port; 887f18be576SLuigi Rizzo struct nm_bridge *na_bdg; 888f9790aebSLuigi Rizzo int retry; 889c3e9b4dbSLuiz Otavio O Souza int autodelete; /* remove the ifp on last reference */ 890f9790aebSLuigi Rizzo 891f0ea3689SLuigi Rizzo /* Maximum Frame Size, used in bdg_mismatch_datapath() */ 892f0ea3689SLuigi Rizzo u_int mfs; 893847bf383SLuigi Rizzo /* Last source MAC on this port */ 894847bf383SLuigi Rizzo uint64_t last_smac; 895f9790aebSLuigi Rizzo }; 896f9790aebSLuigi Rizzo 89717885a7bSLuigi Rizzo 898f9790aebSLuigi Rizzo struct netmap_hw_adapter { /* physical device */ 899f9790aebSLuigi Rizzo struct netmap_adapter up; 900f9790aebSLuigi Rizzo 901f9790aebSLuigi Rizzo struct net_device_ops nm_ndo; // XXX linux only 9024bf50f18SLuigi Rizzo struct ethtool_ops nm_eto; // XXX linux only 9034bf50f18SLuigi Rizzo const struct ethtool_ops* save_ethtool; 9044bf50f18SLuigi Rizzo 9054bf50f18SLuigi Rizzo int (*nm_hw_register)(struct netmap_adapter *, int onoff); 906f9790aebSLuigi Rizzo }; 907f9790aebSLuigi Rizzo 908039dd540SLuigi Rizzo #ifdef WITH_GENERIC 909f0ea3689SLuigi Rizzo /* Mitigation support. */ 910f0ea3689SLuigi Rizzo struct nm_generic_mit { 911f0ea3689SLuigi Rizzo struct hrtimer mit_timer; 912f0ea3689SLuigi Rizzo int mit_pending; 9134bf50f18SLuigi Rizzo int mit_ring_idx; /* index of the ring being mitigated */ 914f0ea3689SLuigi Rizzo struct netmap_adapter *mit_na; /* backpointer */ 915f0ea3689SLuigi Rizzo }; 91617885a7bSLuigi Rizzo 91717885a7bSLuigi Rizzo struct netmap_generic_adapter { /* emulated device */ 918f9790aebSLuigi Rizzo struct netmap_hw_adapter up; 919f9790aebSLuigi Rizzo 920f9790aebSLuigi Rizzo /* Pointer to a previously used netmap adapter. */ 921f9790aebSLuigi Rizzo struct netmap_adapter *prev; 922f9790aebSLuigi Rizzo 923f9790aebSLuigi Rizzo /* generic netmap adapters support: 924f9790aebSLuigi Rizzo * a net_device_ops struct overrides ndo_select_queue(), 925f9790aebSLuigi Rizzo * save_if_input saves the if_input hook (FreeBSD), 926f0ea3689SLuigi Rizzo * mit implements rx interrupt mitigation, 927f9790aebSLuigi Rizzo */ 928f9790aebSLuigi Rizzo struct net_device_ops generic_ndo; 929f9790aebSLuigi Rizzo void (*save_if_input)(struct ifnet *, struct mbuf *); 930f9790aebSLuigi Rizzo 931f0ea3689SLuigi Rizzo struct nm_generic_mit *mit; 93217885a7bSLuigi Rizzo #ifdef linux 93317885a7bSLuigi Rizzo netdev_tx_t (*save_start_xmit)(struct mbuf *, struct ifnet *); 93417885a7bSLuigi Rizzo #endif 93537e3a6d3SLuigi Rizzo /* Is the adapter able to use multiple RX slots to scatter 93637e3a6d3SLuigi Rizzo * each packet pushed up by the driver? */ 93737e3a6d3SLuigi Rizzo int rxsg; 93837e3a6d3SLuigi Rizzo 93937e3a6d3SLuigi Rizzo /* Is the transmission path controlled by a netmap-aware 94037e3a6d3SLuigi Rizzo * device queue (i.e. qdisc on linux)? */ 94137e3a6d3SLuigi Rizzo int txqdisc; 942f9790aebSLuigi Rizzo }; 943039dd540SLuigi Rizzo #endif /* WITH_GENERIC */ 944f9790aebSLuigi Rizzo 945f0ea3689SLuigi Rizzo static __inline int 946847bf383SLuigi Rizzo netmap_real_rings(struct netmap_adapter *na, enum txrx t) 947f0ea3689SLuigi Rizzo { 948847bf383SLuigi Rizzo return nma_get_nrings(na, t) + !!(na->na_flags & NAF_HOST_RINGS); 949f0ea3689SLuigi Rizzo } 950f0ea3689SLuigi Rizzo 951f9790aebSLuigi Rizzo #ifdef WITH_VALE 95237e3a6d3SLuigi Rizzo struct nm_bdg_polling_state; 95317885a7bSLuigi Rizzo /* 95417885a7bSLuigi Rizzo * Bridge wrapper for non VALE ports attached to a VALE switch. 955f9790aebSLuigi Rizzo * 95617885a7bSLuigi Rizzo * The real device must already have its own netmap adapter (hwna). 95717885a7bSLuigi Rizzo * The bridge wrapper and the hwna adapter share the same set of 95817885a7bSLuigi Rizzo * netmap rings and buffers, but they have two separate sets of 95917885a7bSLuigi Rizzo * krings descriptors, with tx/rx meanings swapped: 960f9790aebSLuigi Rizzo * 961f9790aebSLuigi Rizzo * netmap 962f9790aebSLuigi Rizzo * bwrap krings rings krings hwna 963f9790aebSLuigi Rizzo * +------+ +------+ +-----+ +------+ +------+ 964f9790aebSLuigi Rizzo * |tx_rings->| |\ /| |----| |<-tx_rings| 965f9790aebSLuigi Rizzo * | | +------+ \ / +-----+ +------+ | | 966f9790aebSLuigi Rizzo * | | X | | 967f9790aebSLuigi Rizzo * | | / \ | | 968f9790aebSLuigi Rizzo * | | +------+/ \+-----+ +------+ | | 969f9790aebSLuigi Rizzo * |rx_rings->| | | |----| |<-rx_rings| 970f9790aebSLuigi Rizzo * | | +------+ +-----+ +------+ | | 971f9790aebSLuigi Rizzo * +------+ +------+ 972f9790aebSLuigi Rizzo * 97317885a7bSLuigi Rizzo * - packets coming from the bridge go to the brwap rx rings, 97417885a7bSLuigi Rizzo * which are also the hwna tx rings. The bwrap notify callback 97517885a7bSLuigi Rizzo * will then complete the hwna tx (see netmap_bwrap_notify). 976f9790aebSLuigi Rizzo * 97717885a7bSLuigi Rizzo * - packets coming from the outside go to the hwna rx rings, 97817885a7bSLuigi Rizzo * which are also the bwrap tx rings. The (overwritten) hwna 97917885a7bSLuigi Rizzo * notify method will then complete the bridge tx 98017885a7bSLuigi Rizzo * (see netmap_bwrap_intr_notify). 981f9790aebSLuigi Rizzo * 98217885a7bSLuigi Rizzo * The bridge wrapper may optionally connect the hwna 'host' rings 98317885a7bSLuigi Rizzo * to the bridge. This is done by using a second port in the 98417885a7bSLuigi Rizzo * bridge and connecting it to the 'host' netmap_vp_adapter 98517885a7bSLuigi Rizzo * contained in the netmap_bwrap_adapter. The brwap host adapter 98617885a7bSLuigi Rizzo * cross-links the hwna host rings in the same way as shown above. 98717885a7bSLuigi Rizzo * 98817885a7bSLuigi Rizzo * - packets coming from the bridge and directed to the host stack 98917885a7bSLuigi Rizzo * are handled by the bwrap host notify callback 99017885a7bSLuigi Rizzo * (see netmap_bwrap_host_notify) 99117885a7bSLuigi Rizzo * 99217885a7bSLuigi Rizzo * - packets coming from the host stack are still handled by the 99317885a7bSLuigi Rizzo * overwritten hwna notify callback (netmap_bwrap_intr_notify), 99417885a7bSLuigi Rizzo * but are diverted to the host adapter depending on the ring number. 995f9790aebSLuigi Rizzo * 996f9790aebSLuigi Rizzo */ 997f9790aebSLuigi Rizzo struct netmap_bwrap_adapter { 998f9790aebSLuigi Rizzo struct netmap_vp_adapter up; 999f9790aebSLuigi Rizzo struct netmap_vp_adapter host; /* for host rings */ 1000f9790aebSLuigi Rizzo struct netmap_adapter *hwna; /* the underlying device */ 1001f9790aebSLuigi Rizzo 100217885a7bSLuigi Rizzo /* 100317885a7bSLuigi Rizzo * When we attach a physical interface to the bridge, we 1004f18be576SLuigi Rizzo * allow the controlling process to terminate, so we need 10054bf50f18SLuigi Rizzo * a place to store the n_detmap_priv_d data structure. 100617885a7bSLuigi Rizzo * This is only done when physical interfaces 100717885a7bSLuigi Rizzo * are attached to a bridge. 1008f18be576SLuigi Rizzo */ 1009f18be576SLuigi Rizzo struct netmap_priv_d *na_kpriv; 101037e3a6d3SLuigi Rizzo struct nm_bdg_polling_state *na_polling_state; 101168b8534bSLuigi Rizzo }; 10124bf50f18SLuigi Rizzo int netmap_bwrap_attach(const char *name, struct netmap_adapter *); 1013c3e9b4dbSLuiz Otavio O Souza int netmap_vi_create(struct nmreq *, int); 101468b8534bSLuigi Rizzo 1015c3e9b4dbSLuiz Otavio O Souza #else /* !WITH_VALE */ 1016c3e9b4dbSLuiz Otavio O Souza #define netmap_vi_create(nmr, a) (EOPNOTSUPP) 101717885a7bSLuigi Rizzo #endif /* WITH_VALE */ 1018ce3ee1e7SLuigi Rizzo 1019f0ea3689SLuigi Rizzo #ifdef WITH_PIPES 1020f0ea3689SLuigi Rizzo 1021f0ea3689SLuigi Rizzo #define NM_MAXPIPES 64 /* max number of pipes per adapter */ 1022f0ea3689SLuigi Rizzo 1023f0ea3689SLuigi Rizzo struct netmap_pipe_adapter { 1024f0ea3689SLuigi Rizzo struct netmap_adapter up; 1025f0ea3689SLuigi Rizzo 1026f0ea3689SLuigi Rizzo u_int id; /* pipe identifier */ 1027f0ea3689SLuigi Rizzo int role; /* either NR_REG_PIPE_MASTER or NR_REG_PIPE_SLAVE */ 1028f0ea3689SLuigi Rizzo 1029f0ea3689SLuigi Rizzo struct netmap_adapter *parent; /* adapter that owns the memory */ 1030f0ea3689SLuigi Rizzo struct netmap_pipe_adapter *peer; /* the other end of the pipe */ 1031f0ea3689SLuigi Rizzo int peer_ref; /* 1 iff we are holding a ref to the peer */ 1032c3e9b4dbSLuiz Otavio O Souza struct ifnet *parent_ifp; /* maybe null */ 1033f0ea3689SLuigi Rizzo 1034f0ea3689SLuigi Rizzo u_int parent_slot; /* index in the parent pipe array */ 1035f0ea3689SLuigi Rizzo }; 1036f0ea3689SLuigi Rizzo 1037f0ea3689SLuigi Rizzo #endif /* WITH_PIPES */ 1038f0ea3689SLuigi Rizzo 103917885a7bSLuigi Rizzo 104017885a7bSLuigi Rizzo /* return slots reserved to rx clients; used in drivers */ 104117885a7bSLuigi Rizzo static inline uint32_t 104217885a7bSLuigi Rizzo nm_kr_rxspace(struct netmap_kring *k) 104317885a7bSLuigi Rizzo { 104417885a7bSLuigi Rizzo int space = k->nr_hwtail - k->nr_hwcur; 1045ce3ee1e7SLuigi Rizzo if (space < 0) 1046ce3ee1e7SLuigi Rizzo space += k->nkr_num_slots; 104717885a7bSLuigi Rizzo ND("preserving %d rx slots %d -> %d", space, k->nr_hwcur, k->nr_hwtail); 104817885a7bSLuigi Rizzo 1049ce3ee1e7SLuigi Rizzo return space; 1050ce3ee1e7SLuigi Rizzo } 1051ce3ee1e7SLuigi Rizzo 105237e3a6d3SLuigi Rizzo /* return slots reserved to tx clients */ 105337e3a6d3SLuigi Rizzo #define nm_kr_txspace(_k) nm_kr_rxspace(_k) 1054ce3ee1e7SLuigi Rizzo 105537e3a6d3SLuigi Rizzo 105637e3a6d3SLuigi Rizzo /* True if no space in the tx ring, only valid after txsync_prologue */ 105717885a7bSLuigi Rizzo static inline int 105817885a7bSLuigi Rizzo nm_kr_txempty(struct netmap_kring *kring) 1059ce3ee1e7SLuigi Rizzo { 106017885a7bSLuigi Rizzo return kring->rcur == kring->nr_hwtail; 1061f9790aebSLuigi Rizzo } 1062f9790aebSLuigi Rizzo 106337e3a6d3SLuigi Rizzo /* True if no more completed slots in the rx ring, only valid after 106437e3a6d3SLuigi Rizzo * rxsync_prologue */ 106537e3a6d3SLuigi Rizzo #define nm_kr_rxempty(_k) nm_kr_txempty(_k) 1066ce3ee1e7SLuigi Rizzo 1067ce3ee1e7SLuigi Rizzo /* 1068f9790aebSLuigi Rizzo * protect against multiple threads using the same ring. 106937e3a6d3SLuigi Rizzo * also check that the ring has not been stopped or locked 107068b8534bSLuigi Rizzo */ 107137e3a6d3SLuigi Rizzo #define NM_KR_BUSY 1 /* some other thread is syncing the ring */ 107237e3a6d3SLuigi Rizzo #define NM_KR_STOPPED 2 /* unbounded stop (ifconfig down or driver unload) */ 107337e3a6d3SLuigi Rizzo #define NM_KR_LOCKED 3 /* bounded, brief stop for mutual exclusion */ 107468b8534bSLuigi Rizzo 107517885a7bSLuigi Rizzo 107637e3a6d3SLuigi Rizzo /* release the previously acquired right to use the *sync() methods of the ring */ 1077f9790aebSLuigi Rizzo static __inline void nm_kr_put(struct netmap_kring *kr) 1078f9790aebSLuigi Rizzo { 1079f9790aebSLuigi Rizzo NM_ATOMIC_CLEAR(&kr->nr_busy); 1080f9790aebSLuigi Rizzo } 1081f9790aebSLuigi Rizzo 108217885a7bSLuigi Rizzo 108337e3a6d3SLuigi Rizzo /* true if the ifp that backed the adapter has disappeared (e.g., the 108437e3a6d3SLuigi Rizzo * driver has been unloaded) 108537e3a6d3SLuigi Rizzo */ 108637e3a6d3SLuigi Rizzo static inline int nm_iszombie(struct netmap_adapter *na); 108737e3a6d3SLuigi Rizzo 108837e3a6d3SLuigi Rizzo /* try to obtain exclusive right to issue the *sync() operations on the ring. 108937e3a6d3SLuigi Rizzo * The right is obtained and must be later relinquished via nm_kr_put() if and 109037e3a6d3SLuigi Rizzo * only if nm_kr_tryget() returns 0. 109137e3a6d3SLuigi Rizzo * If can_sleep is 1 there are only two other possible outcomes: 109237e3a6d3SLuigi Rizzo * - the function returns NM_KR_BUSY 109337e3a6d3SLuigi Rizzo * - the function returns NM_KR_STOPPED and sets the POLLERR bit in *perr 109437e3a6d3SLuigi Rizzo * (if non-null) 109537e3a6d3SLuigi Rizzo * In both cases the caller will typically skip the ring, possibly collecting 109637e3a6d3SLuigi Rizzo * errors along the way. 109737e3a6d3SLuigi Rizzo * If the calling context does not allow sleeping, the caller must pass 0 in can_sleep. 109837e3a6d3SLuigi Rizzo * In the latter case, the function may also return NM_KR_LOCKED and leave *perr 109937e3a6d3SLuigi Rizzo * untouched: ideally, the caller should try again at a later time. 110037e3a6d3SLuigi Rizzo */ 110137e3a6d3SLuigi Rizzo static __inline int nm_kr_tryget(struct netmap_kring *kr, int can_sleep, int *perr) 1102f9790aebSLuigi Rizzo { 110337e3a6d3SLuigi Rizzo int busy = 1, stopped; 1104f9790aebSLuigi Rizzo /* check a first time without taking the lock 1105f9790aebSLuigi Rizzo * to avoid starvation for nm_kr_get() 1106f9790aebSLuigi Rizzo */ 110737e3a6d3SLuigi Rizzo retry: 110837e3a6d3SLuigi Rizzo stopped = kr->nkr_stopped; 110937e3a6d3SLuigi Rizzo if (unlikely(stopped)) { 111037e3a6d3SLuigi Rizzo goto stop; 1111f9790aebSLuigi Rizzo } 111237e3a6d3SLuigi Rizzo busy = NM_ATOMIC_TEST_AND_SET(&kr->nr_busy); 111337e3a6d3SLuigi Rizzo /* we should not return NM_KR_BUSY if the ring was 111437e3a6d3SLuigi Rizzo * actually stopped, so check another time after 111537e3a6d3SLuigi Rizzo * the barrier provided by the atomic operation 111637e3a6d3SLuigi Rizzo */ 111737e3a6d3SLuigi Rizzo stopped = kr->nkr_stopped; 111837e3a6d3SLuigi Rizzo if (unlikely(stopped)) { 111937e3a6d3SLuigi Rizzo goto stop; 1120f9790aebSLuigi Rizzo } 112168b8534bSLuigi Rizzo 112237e3a6d3SLuigi Rizzo if (unlikely(nm_iszombie(kr->na))) { 112337e3a6d3SLuigi Rizzo stopped = NM_KR_STOPPED; 112437e3a6d3SLuigi Rizzo goto stop; 112537e3a6d3SLuigi Rizzo } 112637e3a6d3SLuigi Rizzo 112737e3a6d3SLuigi Rizzo return unlikely(busy) ? NM_KR_BUSY : 0; 112837e3a6d3SLuigi Rizzo 112937e3a6d3SLuigi Rizzo stop: 113037e3a6d3SLuigi Rizzo if (!busy) 113137e3a6d3SLuigi Rizzo nm_kr_put(kr); 113237e3a6d3SLuigi Rizzo if (stopped == NM_KR_STOPPED) { 113337e3a6d3SLuigi Rizzo /* if POLLERR is defined we want to use it to simplify netmap_poll(). 113437e3a6d3SLuigi Rizzo * Otherwise, any non-zero value will do. 113537e3a6d3SLuigi Rizzo */ 113637e3a6d3SLuigi Rizzo #ifdef POLLERR 113737e3a6d3SLuigi Rizzo #define NM_POLLERR POLLERR 113837e3a6d3SLuigi Rizzo #else 113937e3a6d3SLuigi Rizzo #define NM_POLLERR 1 114037e3a6d3SLuigi Rizzo #endif /* POLLERR */ 114137e3a6d3SLuigi Rizzo if (perr) 114237e3a6d3SLuigi Rizzo *perr |= NM_POLLERR; 114337e3a6d3SLuigi Rizzo #undef NM_POLLERR 114437e3a6d3SLuigi Rizzo } else if (can_sleep) { 114537e3a6d3SLuigi Rizzo tsleep(kr, 0, "NM_KR_TRYGET", 4); 114637e3a6d3SLuigi Rizzo goto retry; 114737e3a6d3SLuigi Rizzo } 114837e3a6d3SLuigi Rizzo return stopped; 114937e3a6d3SLuigi Rizzo } 115037e3a6d3SLuigi Rizzo 115137e3a6d3SLuigi Rizzo /* put the ring in the 'stopped' state and wait for the current user (if any) to 115237e3a6d3SLuigi Rizzo * notice. stopped must be either NM_KR_STOPPED or NM_KR_LOCKED 115337e3a6d3SLuigi Rizzo */ 115437e3a6d3SLuigi Rizzo static __inline void nm_kr_stop(struct netmap_kring *kr, int stopped) 1155847bf383SLuigi Rizzo { 115637e3a6d3SLuigi Rizzo kr->nkr_stopped = stopped; 1157847bf383SLuigi Rizzo while (NM_ATOMIC_TEST_AND_SET(&kr->nr_busy)) 1158847bf383SLuigi Rizzo tsleep(kr, 0, "NM_KR_GET", 4); 1159847bf383SLuigi Rizzo } 1160847bf383SLuigi Rizzo 116137e3a6d3SLuigi Rizzo /* restart a ring after a stop */ 116237e3a6d3SLuigi Rizzo static __inline void nm_kr_start(struct netmap_kring *kr) 116337e3a6d3SLuigi Rizzo { 116437e3a6d3SLuigi Rizzo kr->nkr_stopped = 0; 116537e3a6d3SLuigi Rizzo nm_kr_put(kr); 116637e3a6d3SLuigi Rizzo } 116737e3a6d3SLuigi Rizzo 1168847bf383SLuigi Rizzo 116968b8534bSLuigi Rizzo /* 117017885a7bSLuigi Rizzo * The following functions are used by individual drivers to 117168b8534bSLuigi Rizzo * support netmap operation. 117268b8534bSLuigi Rizzo * 117368b8534bSLuigi Rizzo * netmap_attach() initializes a struct netmap_adapter, allocating the 117468b8534bSLuigi Rizzo * struct netmap_ring's and the struct selinfo. 117568b8534bSLuigi Rizzo * 117668b8534bSLuigi Rizzo * netmap_detach() frees the memory allocated by netmap_attach(). 117768b8534bSLuigi Rizzo * 1178ce3ee1e7SLuigi Rizzo * netmap_transmit() replaces the if_transmit routine of the interface, 117968b8534bSLuigi Rizzo * and is used to intercept packets coming from the stack. 118068b8534bSLuigi Rizzo * 118168b8534bSLuigi Rizzo * netmap_load_map/netmap_reload_map are helper routines to set/reset 118268b8534bSLuigi Rizzo * the dmamap for a packet buffer 118368b8534bSLuigi Rizzo * 11844bf50f18SLuigi Rizzo * netmap_reset() is a helper routine to be called in the hw driver 11854bf50f18SLuigi Rizzo * when reinitializing a ring. It should not be called by 11864bf50f18SLuigi Rizzo * virtual ports (vale, pipes, monitor) 118768b8534bSLuigi Rizzo */ 1188f9790aebSLuigi Rizzo int netmap_attach(struct netmap_adapter *); 1189c3e9b4dbSLuiz Otavio O Souza int netmap_attach_ext(struct netmap_adapter *, size_t size); 119068b8534bSLuigi Rizzo void netmap_detach(struct ifnet *); 1191ce3ee1e7SLuigi Rizzo int netmap_transmit(struct ifnet *, struct mbuf *); 119268b8534bSLuigi Rizzo struct netmap_slot *netmap_reset(struct netmap_adapter *na, 1193ce3ee1e7SLuigi Rizzo enum txrx tx, u_int n, u_int new_cur); 119468b8534bSLuigi Rizzo int netmap_ring_reinit(struct netmap_kring *); 119568b8534bSLuigi Rizzo 119637e3a6d3SLuigi Rizzo /* Return codes for netmap_*x_irq. */ 119737e3a6d3SLuigi Rizzo enum { 119837e3a6d3SLuigi Rizzo /* Driver should do normal interrupt processing, e.g. because 119937e3a6d3SLuigi Rizzo * the interface is not in netmap mode. */ 120037e3a6d3SLuigi Rizzo NM_IRQ_PASS = 0, 120137e3a6d3SLuigi Rizzo /* Port is in netmap mode, and the interrupt work has been 120237e3a6d3SLuigi Rizzo * completed. The driver does not have to notify netmap 120337e3a6d3SLuigi Rizzo * again before the next interrupt. */ 120437e3a6d3SLuigi Rizzo NM_IRQ_COMPLETED = -1, 120537e3a6d3SLuigi Rizzo /* Port is in netmap mode, but the interrupt work has not been 120637e3a6d3SLuigi Rizzo * completed. The driver has to make sure netmap will be 120737e3a6d3SLuigi Rizzo * notified again soon, even if no more interrupts come (e.g. 120837e3a6d3SLuigi Rizzo * on Linux the driver should not call napi_complete()). */ 120937e3a6d3SLuigi Rizzo NM_IRQ_RESCHED = -2, 121037e3a6d3SLuigi Rizzo }; 121137e3a6d3SLuigi Rizzo 121217885a7bSLuigi Rizzo /* default functions to handle rx/tx interrupts */ 121317885a7bSLuigi Rizzo int netmap_rx_irq(struct ifnet *, u_int, u_int *); 121417885a7bSLuigi Rizzo #define netmap_tx_irq(_n, _q) netmap_rx_irq(_n, _q, NULL) 121537e3a6d3SLuigi Rizzo int netmap_common_irq(struct netmap_adapter *, u_int, u_int *work_done); 121617885a7bSLuigi Rizzo 121717885a7bSLuigi Rizzo 12184bf50f18SLuigi Rizzo #ifdef WITH_VALE 12194bf50f18SLuigi Rizzo /* functions used by external modules to interface with VALE */ 12204bf50f18SLuigi Rizzo #define netmap_vp_to_ifp(_vp) ((_vp)->up.ifp) 12214bf50f18SLuigi Rizzo #define netmap_ifp_to_vp(_ifp) (NA(_ifp)->na_vp) 12224bf50f18SLuigi Rizzo #define netmap_ifp_to_host_vp(_ifp) (NA(_ifp)->na_hostvp) 12234bf50f18SLuigi Rizzo #define netmap_bdg_idx(_vp) ((_vp)->bdg_port) 12244bf50f18SLuigi Rizzo const char *netmap_bdg_name(struct netmap_vp_adapter *); 12254bf50f18SLuigi Rizzo #else /* !WITH_VALE */ 12264bf50f18SLuigi Rizzo #define netmap_vp_to_ifp(_vp) NULL 12274bf50f18SLuigi Rizzo #define netmap_ifp_to_vp(_ifp) NULL 12284bf50f18SLuigi Rizzo #define netmap_ifp_to_host_vp(_ifp) NULL 12294bf50f18SLuigi Rizzo #define netmap_bdg_idx(_vp) -1 12304bf50f18SLuigi Rizzo #define netmap_bdg_name(_vp) NULL 12314bf50f18SLuigi Rizzo #endif /* WITH_VALE */ 12324bf50f18SLuigi Rizzo 12334bf50f18SLuigi Rizzo static inline int 12344bf50f18SLuigi Rizzo nm_netmap_on(struct netmap_adapter *na) 12354bf50f18SLuigi Rizzo { 12364bf50f18SLuigi Rizzo return na && na->na_flags & NAF_NETMAP_ON; 12374bf50f18SLuigi Rizzo } 123817885a7bSLuigi Rizzo 1239847bf383SLuigi Rizzo static inline int 1240847bf383SLuigi Rizzo nm_native_on(struct netmap_adapter *na) 1241847bf383SLuigi Rizzo { 1242847bf383SLuigi Rizzo return nm_netmap_on(na) && (na->na_flags & NAF_NATIVE); 1243847bf383SLuigi Rizzo } 1244847bf383SLuigi Rizzo 124537e3a6d3SLuigi Rizzo static inline int 124637e3a6d3SLuigi Rizzo nm_iszombie(struct netmap_adapter *na) 124737e3a6d3SLuigi Rizzo { 124837e3a6d3SLuigi Rizzo return na == NULL || (na->na_flags & NAF_ZOMBIE); 124937e3a6d3SLuigi Rizzo } 125037e3a6d3SLuigi Rizzo 125137e3a6d3SLuigi Rizzo static inline void 125237e3a6d3SLuigi Rizzo nm_update_hostrings_mode(struct netmap_adapter *na) 125337e3a6d3SLuigi Rizzo { 125437e3a6d3SLuigi Rizzo /* Process nr_mode and nr_pending_mode for host rings. */ 125537e3a6d3SLuigi Rizzo na->tx_rings[na->num_tx_rings].nr_mode = 125637e3a6d3SLuigi Rizzo na->tx_rings[na->num_tx_rings].nr_pending_mode; 125737e3a6d3SLuigi Rizzo na->rx_rings[na->num_rx_rings].nr_mode = 125837e3a6d3SLuigi Rizzo na->rx_rings[na->num_rx_rings].nr_pending_mode; 125937e3a6d3SLuigi Rizzo } 126037e3a6d3SLuigi Rizzo 126117885a7bSLuigi Rizzo /* set/clear native flags and if_transmit/netdev_ops */ 1262f9790aebSLuigi Rizzo static inline void 1263f9790aebSLuigi Rizzo nm_set_native_flags(struct netmap_adapter *na) 1264f9790aebSLuigi Rizzo { 1265f9790aebSLuigi Rizzo struct ifnet *ifp = na->ifp; 1266ce3ee1e7SLuigi Rizzo 126737e3a6d3SLuigi Rizzo /* We do the setup for intercepting packets only if we are the 126837e3a6d3SLuigi Rizzo * first user of this adapapter. */ 126937e3a6d3SLuigi Rizzo if (na->active_fds > 0) { 127037e3a6d3SLuigi Rizzo return; 127137e3a6d3SLuigi Rizzo } 127237e3a6d3SLuigi Rizzo 1273847bf383SLuigi Rizzo na->na_flags |= NAF_NETMAP_ON; 1274f9790aebSLuigi Rizzo #ifdef IFCAP_NETMAP /* or FreeBSD ? */ 1275f9790aebSLuigi Rizzo ifp->if_capenable |= IFCAP_NETMAP; 1276f9790aebSLuigi Rizzo #endif 127737e3a6d3SLuigi Rizzo #if defined (__FreeBSD__) 1278f9790aebSLuigi Rizzo na->if_transmit = ifp->if_transmit; 1279f9790aebSLuigi Rizzo ifp->if_transmit = netmap_transmit; 128037e3a6d3SLuigi Rizzo #elif defined (_WIN32) 128137e3a6d3SLuigi Rizzo (void)ifp; /* prevent a warning */ 128237e3a6d3SLuigi Rizzo //XXX_ale can we just comment those? 128337e3a6d3SLuigi Rizzo //na->if_transmit = ifp->if_transmit; 128437e3a6d3SLuigi Rizzo //ifp->if_transmit = netmap_transmit; 1285f9790aebSLuigi Rizzo #else 1286f9790aebSLuigi Rizzo na->if_transmit = (void *)ifp->netdev_ops; 1287f9790aebSLuigi Rizzo ifp->netdev_ops = &((struct netmap_hw_adapter *)na)->nm_ndo; 12884bf50f18SLuigi Rizzo ((struct netmap_hw_adapter *)na)->save_ethtool = ifp->ethtool_ops; 12894bf50f18SLuigi Rizzo ifp->ethtool_ops = &((struct netmap_hw_adapter*)na)->nm_eto; 1290f9790aebSLuigi Rizzo #endif 129137e3a6d3SLuigi Rizzo nm_update_hostrings_mode(na); 1292f9790aebSLuigi Rizzo } 1293f9790aebSLuigi Rizzo 1294f9790aebSLuigi Rizzo static inline void 1295f9790aebSLuigi Rizzo nm_clear_native_flags(struct netmap_adapter *na) 1296f9790aebSLuigi Rizzo { 1297f9790aebSLuigi Rizzo struct ifnet *ifp = na->ifp; 1298f9790aebSLuigi Rizzo 129937e3a6d3SLuigi Rizzo /* We undo the setup for intercepting packets only if we are the 130037e3a6d3SLuigi Rizzo * last user of this adapapter. */ 130137e3a6d3SLuigi Rizzo if (na->active_fds > 0) { 130237e3a6d3SLuigi Rizzo return; 130337e3a6d3SLuigi Rizzo } 130437e3a6d3SLuigi Rizzo 130537e3a6d3SLuigi Rizzo nm_update_hostrings_mode(na); 130637e3a6d3SLuigi Rizzo 130737e3a6d3SLuigi Rizzo #if defined(__FreeBSD__) 1308f9790aebSLuigi Rizzo ifp->if_transmit = na->if_transmit; 130937e3a6d3SLuigi Rizzo #elif defined(_WIN32) 131037e3a6d3SLuigi Rizzo (void)ifp; /* prevent a warning */ 131137e3a6d3SLuigi Rizzo //XXX_ale can we just comment those? 131237e3a6d3SLuigi Rizzo //ifp->if_transmit = na->if_transmit; 1313f9790aebSLuigi Rizzo #else 1314f9790aebSLuigi Rizzo ifp->netdev_ops = (void *)na->if_transmit; 13154bf50f18SLuigi Rizzo ifp->ethtool_ops = ((struct netmap_hw_adapter*)na)->save_ethtool; 1316f9790aebSLuigi Rizzo #endif 1317847bf383SLuigi Rizzo na->na_flags &= ~NAF_NETMAP_ON; 1318f9790aebSLuigi Rizzo #ifdef IFCAP_NETMAP /* or FreeBSD ? */ 1319f9790aebSLuigi Rizzo ifp->if_capenable &= ~IFCAP_NETMAP; 1320f9790aebSLuigi Rizzo #endif 1321f9790aebSLuigi Rizzo } 1322f9790aebSLuigi Rizzo 132337e3a6d3SLuigi Rizzo /* 132437e3a6d3SLuigi Rizzo * nm_*sync_prologue() functions are used in ioctl/poll and ptnetmap 132537e3a6d3SLuigi Rizzo * kthreads. 132637e3a6d3SLuigi Rizzo * We need netmap_ring* parameter, because in ptnetmap it is decoupled 132737e3a6d3SLuigi Rizzo * from host kring. 132837e3a6d3SLuigi Rizzo * The user-space ring pointers (head/cur/tail) are shared through 132937e3a6d3SLuigi Rizzo * CSB between host and guest. 133037e3a6d3SLuigi Rizzo */ 133137e3a6d3SLuigi Rizzo 133237e3a6d3SLuigi Rizzo /* 133337e3a6d3SLuigi Rizzo * validates parameters in the ring/kring, returns a value for head 133437e3a6d3SLuigi Rizzo * If any error, returns ring_size to force a reinit. 133537e3a6d3SLuigi Rizzo */ 133637e3a6d3SLuigi Rizzo uint32_t nm_txsync_prologue(struct netmap_kring *, struct netmap_ring *); 133737e3a6d3SLuigi Rizzo 133837e3a6d3SLuigi Rizzo 133937e3a6d3SLuigi Rizzo /* 134037e3a6d3SLuigi Rizzo * validates parameters in the ring/kring, returns a value for head 134137e3a6d3SLuigi Rizzo * If any error, returns ring_size lim to force a reinit. 134237e3a6d3SLuigi Rizzo */ 134337e3a6d3SLuigi Rizzo uint32_t nm_rxsync_prologue(struct netmap_kring *, struct netmap_ring *); 134437e3a6d3SLuigi Rizzo 1345f9790aebSLuigi Rizzo 1346f9790aebSLuigi Rizzo /* check/fix address and len in tx rings */ 1347f9790aebSLuigi Rizzo #if 1 /* debug version */ 13484bf50f18SLuigi Rizzo #define NM_CHECK_ADDR_LEN(_na, _a, _l) do { \ 13494bf50f18SLuigi Rizzo if (_a == NETMAP_BUF_BASE(_na) || _l > NETMAP_BUF_SIZE(_na)) { \ 1350f9790aebSLuigi Rizzo RD(5, "bad addr/len ring %d slot %d idx %d len %d", \ 13514bf50f18SLuigi Rizzo kring->ring_id, nm_i, slot->buf_idx, len); \ 13524bf50f18SLuigi Rizzo if (_l > NETMAP_BUF_SIZE(_na)) \ 13534bf50f18SLuigi Rizzo _l = NETMAP_BUF_SIZE(_na); \ 1354f9790aebSLuigi Rizzo } } while (0) 1355f9790aebSLuigi Rizzo #else /* no debug version */ 13564bf50f18SLuigi Rizzo #define NM_CHECK_ADDR_LEN(_na, _a, _l) do { \ 13574bf50f18SLuigi Rizzo if (_l > NETMAP_BUF_SIZE(_na)) \ 13584bf50f18SLuigi Rizzo _l = NETMAP_BUF_SIZE(_na); \ 1359f9790aebSLuigi Rizzo } while (0) 1360f9790aebSLuigi Rizzo #endif 1361f9790aebSLuigi Rizzo 1362f9790aebSLuigi Rizzo 1363f9790aebSLuigi Rizzo /*---------------------------------------------------------------*/ 1364f9790aebSLuigi Rizzo /* 13654bf50f18SLuigi Rizzo * Support routines used by netmap subsystems 13664bf50f18SLuigi Rizzo * (native drivers, VALE, generic, pipes, monitors, ...) 13674bf50f18SLuigi Rizzo */ 13684bf50f18SLuigi Rizzo 13694bf50f18SLuigi Rizzo 13704bf50f18SLuigi Rizzo /* common routine for all functions that create a netmap adapter. It performs 13714bf50f18SLuigi Rizzo * two main tasks: 13724bf50f18SLuigi Rizzo * - if the na points to an ifp, mark the ifp as netmap capable 13734bf50f18SLuigi Rizzo * using na as its native adapter; 13744bf50f18SLuigi Rizzo * - provide defaults for the setup callbacks and the memory allocator 13754bf50f18SLuigi Rizzo */ 13764bf50f18SLuigi Rizzo int netmap_attach_common(struct netmap_adapter *); 13774bf50f18SLuigi Rizzo /* common actions to be performed on netmap adapter destruction */ 13784bf50f18SLuigi Rizzo void netmap_detach_common(struct netmap_adapter *); 13794bf50f18SLuigi Rizzo /* fill priv->np_[tr]xq{first,last} using the ringid and flags information 13804bf50f18SLuigi Rizzo * coming from a struct nmreq 13814bf50f18SLuigi Rizzo */ 13824bf50f18SLuigi Rizzo int netmap_interp_ringid(struct netmap_priv_d *priv, uint16_t ringid, uint32_t flags); 13834bf50f18SLuigi Rizzo /* update the ring parameters (number and size of tx and rx rings). 13844bf50f18SLuigi Rizzo * It calls the nm_config callback, if available. 1385f9790aebSLuigi Rizzo */ 1386f9790aebSLuigi Rizzo int netmap_update_config(struct netmap_adapter *na); 13874bf50f18SLuigi Rizzo /* create and initialize the common fields of the krings array. 13884bf50f18SLuigi Rizzo * using the information that must be already available in the na. 13894bf50f18SLuigi Rizzo * tailroom can be used to request the allocation of additional 13904bf50f18SLuigi Rizzo * tailroom bytes after the krings array. This is used by 13914bf50f18SLuigi Rizzo * netmap_vp_adapter's (i.e., VALE ports) to make room for 13924bf50f18SLuigi Rizzo * leasing-related data structures 13934bf50f18SLuigi Rizzo */ 1394f0ea3689SLuigi Rizzo int netmap_krings_create(struct netmap_adapter *na, u_int tailroom); 13954bf50f18SLuigi Rizzo /* deletes the kring array of the adapter. The array must have 13964bf50f18SLuigi Rizzo * been created using netmap_krings_create 13974bf50f18SLuigi Rizzo */ 1398f9790aebSLuigi Rizzo void netmap_krings_delete(struct netmap_adapter *na); 139917885a7bSLuigi Rizzo 140037e3a6d3SLuigi Rizzo int netmap_hw_krings_create(struct netmap_adapter *na); 140137e3a6d3SLuigi Rizzo void netmap_hw_krings_delete(struct netmap_adapter *na); 140237e3a6d3SLuigi Rizzo 14034bf50f18SLuigi Rizzo /* set the stopped/enabled status of ring 14044bf50f18SLuigi Rizzo * When stopping, they also wait for all current activity on the ring to 14054bf50f18SLuigi Rizzo * terminate. The status change is then notified using the na nm_notify 14064bf50f18SLuigi Rizzo * callback. 14074bf50f18SLuigi Rizzo */ 1408847bf383SLuigi Rizzo void netmap_set_ring(struct netmap_adapter *, u_int ring_id, enum txrx, int stopped); 14094bf50f18SLuigi Rizzo /* set the stopped/enabled status of all rings of the adapter. */ 14104bf50f18SLuigi Rizzo void netmap_set_all_rings(struct netmap_adapter *, int stopped); 141137e3a6d3SLuigi Rizzo /* convenience wrappers for netmap_set_all_rings */ 14124bf50f18SLuigi Rizzo void netmap_disable_all_rings(struct ifnet *); 14134bf50f18SLuigi Rizzo void netmap_enable_all_rings(struct ifnet *); 14144bf50f18SLuigi Rizzo 14158fd44c93SLuigi Rizzo int netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na, 1416847bf383SLuigi Rizzo uint16_t ringid, uint32_t flags); 141737e3a6d3SLuigi Rizzo void netmap_do_unregif(struct netmap_priv_d *priv); 1418f9790aebSLuigi Rizzo 1419f9790aebSLuigi Rizzo u_int nm_bound_var(u_int *v, u_int dflt, u_int lo, u_int hi, const char *msg); 142037e3a6d3SLuigi Rizzo int netmap_get_na(struct nmreq *nmr, struct netmap_adapter **na, 1421c3e9b4dbSLuiz Otavio O Souza struct ifnet **ifp, struct netmap_mem_d *nmd, int create); 142237e3a6d3SLuigi Rizzo void netmap_unget_na(struct netmap_adapter *na, struct ifnet *ifp); 1423c3e9b4dbSLuiz Otavio O Souza int netmap_get_hw_na(struct ifnet *ifp, 1424c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d *nmd, struct netmap_adapter **na); 1425f9790aebSLuigi Rizzo 142617885a7bSLuigi Rizzo 1427f9790aebSLuigi Rizzo #ifdef WITH_VALE 1428f18be576SLuigi Rizzo /* 142917885a7bSLuigi Rizzo * The following bridge-related functions are used by other 143017885a7bSLuigi Rizzo * kernel modules. 143117885a7bSLuigi Rizzo * 143217885a7bSLuigi Rizzo * VALE only supports unicast or broadcast. The lookup 1433f18be576SLuigi Rizzo * function can return 0 .. NM_BDG_MAXPORTS-1 for regular ports, 1434f18be576SLuigi Rizzo * NM_BDG_MAXPORTS for broadcast, NM_BDG_MAXPORTS+1 for unknown. 1435f18be576SLuigi Rizzo * XXX in practice "unknown" might be handled same as broadcast. 1436f18be576SLuigi Rizzo */ 14374bf50f18SLuigi Rizzo typedef u_int (*bdg_lookup_fn_t)(struct nm_bdg_fwd *ft, uint8_t *ring_nr, 1438847bf383SLuigi Rizzo struct netmap_vp_adapter *); 14394bf50f18SLuigi Rizzo typedef int (*bdg_config_fn_t)(struct nm_ifreq *); 14404bf50f18SLuigi Rizzo typedef void (*bdg_dtor_fn_t)(const struct netmap_vp_adapter *); 14414bf50f18SLuigi Rizzo struct netmap_bdg_ops { 14424bf50f18SLuigi Rizzo bdg_lookup_fn_t lookup; 14434bf50f18SLuigi Rizzo bdg_config_fn_t config; 14444bf50f18SLuigi Rizzo bdg_dtor_fn_t dtor; 14454bf50f18SLuigi Rizzo }; 14464bf50f18SLuigi Rizzo 14474bf50f18SLuigi Rizzo u_int netmap_bdg_learning(struct nm_bdg_fwd *ft, uint8_t *dst_ring, 1448847bf383SLuigi Rizzo struct netmap_vp_adapter *); 1449f9790aebSLuigi Rizzo 145037e3a6d3SLuigi Rizzo #define NM_BRIDGES 8 /* number of bridges */ 1451f9790aebSLuigi Rizzo #define NM_BDG_MAXPORTS 254 /* up to 254 */ 1452f18be576SLuigi Rizzo #define NM_BDG_BROADCAST NM_BDG_MAXPORTS 1453f18be576SLuigi Rizzo #define NM_BDG_NOPORT (NM_BDG_MAXPORTS+1) 1454f18be576SLuigi Rizzo 1455f9790aebSLuigi Rizzo /* these are redefined in case of no VALE support */ 1456c3e9b4dbSLuiz Otavio O Souza int netmap_get_bdg_na(struct nmreq *nmr, struct netmap_adapter **na, 1457c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d *nmd, int create); 1458847bf383SLuigi Rizzo struct nm_bridge *netmap_init_bridges2(u_int); 1459847bf383SLuigi Rizzo void netmap_uninit_bridges2(struct nm_bridge *, u_int); 1460847bf383SLuigi Rizzo int netmap_init_bridges(void); 1461847bf383SLuigi Rizzo void netmap_uninit_bridges(void); 14624bf50f18SLuigi Rizzo int netmap_bdg_ctl(struct nmreq *nmr, struct netmap_bdg_ops *bdg_ops); 14634bf50f18SLuigi Rizzo int netmap_bdg_config(struct nmreq *nmr); 1464f9790aebSLuigi Rizzo 1465f9790aebSLuigi Rizzo #else /* !WITH_VALE */ 1466c3e9b4dbSLuiz Otavio O Souza #define netmap_get_bdg_na(_1, _2, _3, _4) 0 1467847bf383SLuigi Rizzo #define netmap_init_bridges(_1) 0 1468847bf383SLuigi Rizzo #define netmap_uninit_bridges() 1469f9790aebSLuigi Rizzo #define netmap_bdg_ctl(_1, _2) EINVAL 1470f9790aebSLuigi Rizzo #endif /* !WITH_VALE */ 1471f9790aebSLuigi Rizzo 1472f0ea3689SLuigi Rizzo #ifdef WITH_PIPES 1473f0ea3689SLuigi Rizzo /* max number of pipes per device */ 1474f0ea3689SLuigi Rizzo #define NM_MAXPIPES 64 /* XXX how many? */ 1475f0ea3689SLuigi Rizzo void netmap_pipe_dealloc(struct netmap_adapter *); 1476c3e9b4dbSLuiz Otavio O Souza int netmap_get_pipe_na(struct nmreq *nmr, struct netmap_adapter **na, 1477c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d *nmd, int create); 1478f0ea3689SLuigi Rizzo #else /* !WITH_PIPES */ 1479f0ea3689SLuigi Rizzo #define NM_MAXPIPES 0 1480847bf383SLuigi Rizzo #define netmap_pipe_alloc(_1, _2) 0 1481f0ea3689SLuigi Rizzo #define netmap_pipe_dealloc(_1) 1482c3e9b4dbSLuiz Otavio O Souza #define netmap_get_pipe_na(nmr, _2, _3, _4) \ 1483847bf383SLuigi Rizzo ({ int role__ = (nmr)->nr_flags & NR_REG_MASK; \ 1484847bf383SLuigi Rizzo (role__ == NR_REG_PIPE_MASTER || \ 1485847bf383SLuigi Rizzo role__ == NR_REG_PIPE_SLAVE) ? EOPNOTSUPP : 0; }) 1486f0ea3689SLuigi Rizzo #endif 1487f0ea3689SLuigi Rizzo 14884bf50f18SLuigi Rizzo #ifdef WITH_MONITOR 1489c3e9b4dbSLuiz Otavio O Souza int netmap_get_monitor_na(struct nmreq *nmr, struct netmap_adapter **na, 1490c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d *nmd, int create); 1491847bf383SLuigi Rizzo void netmap_monitor_stop(struct netmap_adapter *na); 14924bf50f18SLuigi Rizzo #else 1493c3e9b4dbSLuiz Otavio O Souza #define netmap_get_monitor_na(nmr, _2, _3, _4) \ 1494847bf383SLuigi Rizzo ((nmr)->nr_flags & (NR_MONITOR_TX | NR_MONITOR_RX) ? EOPNOTSUPP : 0) 1495847bf383SLuigi Rizzo #endif 1496847bf383SLuigi Rizzo 1497847bf383SLuigi Rizzo #ifdef CONFIG_NET_NS 1498847bf383SLuigi Rizzo struct net *netmap_bns_get(void); 1499847bf383SLuigi Rizzo void netmap_bns_put(struct net *); 1500847bf383SLuigi Rizzo void netmap_bns_getbridges(struct nm_bridge **, u_int *); 1501847bf383SLuigi Rizzo #else 1502847bf383SLuigi Rizzo #define netmap_bns_get() 1503847bf383SLuigi Rizzo #define netmap_bns_put(_1) 1504847bf383SLuigi Rizzo #define netmap_bns_getbridges(b, n) \ 1505847bf383SLuigi Rizzo do { *b = nm_bridges; *n = NM_BRIDGES; } while (0) 15064bf50f18SLuigi Rizzo #endif 15074bf50f18SLuigi Rizzo 1508f9790aebSLuigi Rizzo /* Various prototypes */ 150937e3a6d3SLuigi Rizzo int netmap_poll(struct netmap_priv_d *, int events, NM_SELRECORD_T *td); 1510f9790aebSLuigi Rizzo int netmap_init(void); 1511f9790aebSLuigi Rizzo void netmap_fini(void); 1512f9790aebSLuigi Rizzo int netmap_get_memory(struct netmap_priv_d* p); 1513f9790aebSLuigi Rizzo void netmap_dtor(void *data); 1514f9790aebSLuigi Rizzo 151537e3a6d3SLuigi Rizzo int netmap_ioctl(struct netmap_priv_d *priv, u_long cmd, caddr_t data, struct thread *); 1516f9790aebSLuigi Rizzo 1517f9790aebSLuigi Rizzo /* netmap_adapter creation/destruction */ 151817885a7bSLuigi Rizzo 151917885a7bSLuigi Rizzo // #define NM_DEBUG_PUTGET 1 1520f9790aebSLuigi Rizzo 1521f9790aebSLuigi Rizzo #ifdef NM_DEBUG_PUTGET 1522f9790aebSLuigi Rizzo 1523f9790aebSLuigi Rizzo #define NM_DBG(f) __##f 1524f9790aebSLuigi Rizzo 1525f9790aebSLuigi Rizzo void __netmap_adapter_get(struct netmap_adapter *na); 1526f9790aebSLuigi Rizzo 1527f9790aebSLuigi Rizzo #define netmap_adapter_get(na) \ 1528f9790aebSLuigi Rizzo do { \ 1529f9790aebSLuigi Rizzo struct netmap_adapter *__na = na; \ 15304bf50f18SLuigi Rizzo D("getting %p:%s (%d)", __na, (__na)->name, (__na)->na_refcount); \ 1531f9790aebSLuigi Rizzo __netmap_adapter_get(__na); \ 1532f9790aebSLuigi Rizzo } while (0) 1533f9790aebSLuigi Rizzo 1534f9790aebSLuigi Rizzo int __netmap_adapter_put(struct netmap_adapter *na); 1535f9790aebSLuigi Rizzo 1536f9790aebSLuigi Rizzo #define netmap_adapter_put(na) \ 1537fb25194fSLuigi Rizzo ({ \ 1538f9790aebSLuigi Rizzo struct netmap_adapter *__na = na; \ 15394bf50f18SLuigi Rizzo D("putting %p:%s (%d)", __na, (__na)->name, (__na)->na_refcount); \ 1540f9790aebSLuigi Rizzo __netmap_adapter_put(__na); \ 1541fb25194fSLuigi Rizzo }) 1542f9790aebSLuigi Rizzo 1543f9790aebSLuigi Rizzo #else /* !NM_DEBUG_PUTGET */ 1544f9790aebSLuigi Rizzo 1545f9790aebSLuigi Rizzo #define NM_DBG(f) f 1546f9790aebSLuigi Rizzo void netmap_adapter_get(struct netmap_adapter *na); 1547f9790aebSLuigi Rizzo int netmap_adapter_put(struct netmap_adapter *na); 1548f9790aebSLuigi Rizzo 1549f9790aebSLuigi Rizzo #endif /* !NM_DEBUG_PUTGET */ 1550f9790aebSLuigi Rizzo 1551f9790aebSLuigi Rizzo 155217885a7bSLuigi Rizzo /* 155317885a7bSLuigi Rizzo * module variables 155417885a7bSLuigi Rizzo */ 155537e3a6d3SLuigi Rizzo #define NETMAP_BUF_BASE(_na) ((_na)->na_lut.lut[0].vaddr) 155637e3a6d3SLuigi Rizzo #define NETMAP_BUF_SIZE(_na) ((_na)->na_lut.objsize) 15575819da83SLuigi Rizzo extern int netmap_no_pendintr; 1558a2a74091SLuigi Rizzo extern int netmap_mitigate; 1559a2a74091SLuigi Rizzo extern int netmap_verbose; /* for debugging */ 156068b8534bSLuigi Rizzo enum { /* verbose flags */ 156168b8534bSLuigi Rizzo NM_VERB_ON = 1, /* generic verbose */ 156268b8534bSLuigi Rizzo NM_VERB_HOST = 0x2, /* verbose host stack */ 156368b8534bSLuigi Rizzo NM_VERB_RXSYNC = 0x10, /* verbose on rxsync/txsync */ 156468b8534bSLuigi Rizzo NM_VERB_TXSYNC = 0x20, 156568b8534bSLuigi Rizzo NM_VERB_RXINTR = 0x100, /* verbose on rx/tx intr (driver) */ 156668b8534bSLuigi Rizzo NM_VERB_TXINTR = 0x200, 156768b8534bSLuigi Rizzo NM_VERB_NIC_RXSYNC = 0x1000, /* verbose on rx/tx intr (driver) */ 156868b8534bSLuigi Rizzo NM_VERB_NIC_TXSYNC = 0x2000, 156968b8534bSLuigi Rizzo }; 157068b8534bSLuigi Rizzo 1571f9790aebSLuigi Rizzo extern int netmap_txsync_retry; 157237e3a6d3SLuigi Rizzo extern int netmap_flags; 1573f9790aebSLuigi Rizzo extern int netmap_generic_mit; 1574f9790aebSLuigi Rizzo extern int netmap_generic_ringsize; 1575f0ea3689SLuigi Rizzo extern int netmap_generic_rings; 157637e3a6d3SLuigi Rizzo extern int netmap_generic_txqdisc; 1577c3e9b4dbSLuiz Otavio O Souza extern int ptnetmap_tx_workers; 1578f9790aebSLuigi Rizzo 157968b8534bSLuigi Rizzo /* 1580d0c7b075SLuigi Rizzo * NA returns a pointer to the struct netmap adapter from the ifp, 1581d0c7b075SLuigi Rizzo * WNA is used to write it. 158268b8534bSLuigi Rizzo */ 1583d0c7b075SLuigi Rizzo #define NA(_ifp) ((struct netmap_adapter *)WNA(_ifp)) 158468b8534bSLuigi Rizzo 15858241616dSLuigi Rizzo /* 158637e3a6d3SLuigi Rizzo * On old versions of FreeBSD, NA(ifp) is a pspare. On linux we 158737e3a6d3SLuigi Rizzo * overload another pointer in the netdev. 158837e3a6d3SLuigi Rizzo * 158937e3a6d3SLuigi Rizzo * We check if NA(ifp) is set and its first element has a related 15908241616dSLuigi Rizzo * magic value. The capenable is within the struct netmap_adapter. 15918241616dSLuigi Rizzo */ 15928241616dSLuigi Rizzo #define NETMAP_MAGIC 0x52697a7a 15938241616dSLuigi Rizzo 159437e3a6d3SLuigi Rizzo #define NM_NA_VALID(ifp) (NA(ifp) && \ 15958241616dSLuigi Rizzo ((uint32_t)(uintptr_t)NA(ifp) ^ NA(ifp)->magic) == NETMAP_MAGIC ) 15968241616dSLuigi Rizzo 159737e3a6d3SLuigi Rizzo #define NM_ATTACH_NA(ifp, na) do { \ 159837e3a6d3SLuigi Rizzo WNA(ifp) = na; \ 159937e3a6d3SLuigi Rizzo if (NA(ifp)) \ 160037e3a6d3SLuigi Rizzo NA(ifp)->magic = \ 160137e3a6d3SLuigi Rizzo ((uint32_t)(uintptr_t)NA(ifp)) ^ NETMAP_MAGIC; \ 160237e3a6d3SLuigi Rizzo } while(0) 16038241616dSLuigi Rizzo 160437e3a6d3SLuigi Rizzo #define NM_IS_NATIVE(ifp) (NM_NA_VALID(ifp) && NA(ifp)->nm_dtor == netmap_hw_dtor) 160568b8534bSLuigi Rizzo 160637e3a6d3SLuigi Rizzo #if defined(__FreeBSD__) 1607f9790aebSLuigi Rizzo 16084bf50f18SLuigi Rizzo /* Assigns the device IOMMU domain to an allocator. 16094bf50f18SLuigi Rizzo * Returns -ENOMEM in case the domain is different */ 16104bf50f18SLuigi Rizzo #define nm_iommu_group_id(dev) (0) 16114bf50f18SLuigi Rizzo 161217885a7bSLuigi Rizzo /* Callback invoked by the dma machinery after a successful dmamap_load */ 16136dba29a2SLuigi Rizzo static void netmap_dmamap_cb(__unused void *arg, 16146dba29a2SLuigi Rizzo __unused bus_dma_segment_t * segs, __unused int nseg, __unused int error) 16156dba29a2SLuigi Rizzo { 16166dba29a2SLuigi Rizzo } 16176dba29a2SLuigi Rizzo 16186dba29a2SLuigi Rizzo /* bus_dmamap_load wrapper: call aforementioned function if map != NULL. 16196dba29a2SLuigi Rizzo * XXX can we do it without a callback ? 16206dba29a2SLuigi Rizzo */ 16216dba29a2SLuigi Rizzo static inline void 16224bf50f18SLuigi Rizzo netmap_load_map(struct netmap_adapter *na, 16234bf50f18SLuigi Rizzo bus_dma_tag_t tag, bus_dmamap_t map, void *buf) 16246dba29a2SLuigi Rizzo { 16256dba29a2SLuigi Rizzo if (map) 16264bf50f18SLuigi Rizzo bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE(na), 16276dba29a2SLuigi Rizzo netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT); 16286dba29a2SLuigi Rizzo } 16296dba29a2SLuigi Rizzo 16304bf50f18SLuigi Rizzo static inline void 16314bf50f18SLuigi Rizzo netmap_unload_map(struct netmap_adapter *na, 16324bf50f18SLuigi Rizzo bus_dma_tag_t tag, bus_dmamap_t map) 16334bf50f18SLuigi Rizzo { 16344bf50f18SLuigi Rizzo if (map) 16354bf50f18SLuigi Rizzo bus_dmamap_unload(tag, map); 16364bf50f18SLuigi Rizzo } 16374bf50f18SLuigi Rizzo 16386dba29a2SLuigi Rizzo /* update the map when a buffer changes. */ 16396dba29a2SLuigi Rizzo static inline void 16404bf50f18SLuigi Rizzo netmap_reload_map(struct netmap_adapter *na, 16414bf50f18SLuigi Rizzo bus_dma_tag_t tag, bus_dmamap_t map, void *buf) 16426dba29a2SLuigi Rizzo { 16436dba29a2SLuigi Rizzo if (map) { 16446dba29a2SLuigi Rizzo bus_dmamap_unload(tag, map); 16454bf50f18SLuigi Rizzo bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE(na), 16466dba29a2SLuigi Rizzo netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT); 16476dba29a2SLuigi Rizzo } 16486dba29a2SLuigi Rizzo } 1649f9790aebSLuigi Rizzo 165037e3a6d3SLuigi Rizzo #elif defined(_WIN32) 165137e3a6d3SLuigi Rizzo 1652f196ce38SLuigi Rizzo #else /* linux */ 1653f196ce38SLuigi Rizzo 16544bf50f18SLuigi Rizzo int nm_iommu_group_id(bus_dma_tag_t dev); 16554bf50f18SLuigi Rizzo #include <linux/dma-mapping.h> 16564bf50f18SLuigi Rizzo 16574bf50f18SLuigi Rizzo static inline void 16584bf50f18SLuigi Rizzo netmap_load_map(struct netmap_adapter *na, 16594bf50f18SLuigi Rizzo bus_dma_tag_t tag, bus_dmamap_t map, void *buf) 16604bf50f18SLuigi Rizzo { 1661847bf383SLuigi Rizzo if (0 && map) { 166237e3a6d3SLuigi Rizzo *map = dma_map_single(na->pdev, buf, NETMAP_BUF_SIZE(na), 16634bf50f18SLuigi Rizzo DMA_BIDIRECTIONAL); 16644bf50f18SLuigi Rizzo } 16654bf50f18SLuigi Rizzo } 16664bf50f18SLuigi Rizzo 16674bf50f18SLuigi Rizzo static inline void 16684bf50f18SLuigi Rizzo netmap_unload_map(struct netmap_adapter *na, 16694bf50f18SLuigi Rizzo bus_dma_tag_t tag, bus_dmamap_t map) 16704bf50f18SLuigi Rizzo { 167137e3a6d3SLuigi Rizzo u_int sz = NETMAP_BUF_SIZE(na); 16724bf50f18SLuigi Rizzo 16734bf50f18SLuigi Rizzo if (*map) { 16744bf50f18SLuigi Rizzo dma_unmap_single(na->pdev, *map, sz, 16754bf50f18SLuigi Rizzo DMA_BIDIRECTIONAL); 16764bf50f18SLuigi Rizzo } 16774bf50f18SLuigi Rizzo } 16784bf50f18SLuigi Rizzo 16794bf50f18SLuigi Rizzo static inline void 16804bf50f18SLuigi Rizzo netmap_reload_map(struct netmap_adapter *na, 16814bf50f18SLuigi Rizzo bus_dma_tag_t tag, bus_dmamap_t map, void *buf) 16824bf50f18SLuigi Rizzo { 168337e3a6d3SLuigi Rizzo u_int sz = NETMAP_BUF_SIZE(na); 16844bf50f18SLuigi Rizzo 16854bf50f18SLuigi Rizzo if (*map) { 16864bf50f18SLuigi Rizzo dma_unmap_single(na->pdev, *map, sz, 16874bf50f18SLuigi Rizzo DMA_BIDIRECTIONAL); 16884bf50f18SLuigi Rizzo } 16894bf50f18SLuigi Rizzo 16904bf50f18SLuigi Rizzo *map = dma_map_single(na->pdev, buf, sz, 16914bf50f18SLuigi Rizzo DMA_BIDIRECTIONAL); 16924bf50f18SLuigi Rizzo } 16934bf50f18SLuigi Rizzo 1694f196ce38SLuigi Rizzo /* 1695f196ce38SLuigi Rizzo * XXX How do we redefine these functions: 1696f196ce38SLuigi Rizzo * 1697f196ce38SLuigi Rizzo * on linux we need 1698f196ce38SLuigi Rizzo * dma_map_single(&pdev->dev, virt_addr, len, direction) 1699f196ce38SLuigi Rizzo * dma_unmap_single(&adapter->pdev->dev, phys_addr, len, direction 1700f196ce38SLuigi Rizzo * The len can be implicit (on netmap it is NETMAP_BUF_SIZE) 1701f196ce38SLuigi Rizzo * unfortunately the direction is not, so we need to change 1702f196ce38SLuigi Rizzo * something to have a cross API 1703f196ce38SLuigi Rizzo */ 17044bf50f18SLuigi Rizzo 1705f196ce38SLuigi Rizzo #if 0 1706f196ce38SLuigi Rizzo struct e1000_buffer *buffer_info = &tx_ring->buffer_info[l]; 1707f196ce38SLuigi Rizzo /* set time_stamp *before* dma to help avoid a possible race */ 1708f196ce38SLuigi Rizzo buffer_info->time_stamp = jiffies; 1709f196ce38SLuigi Rizzo buffer_info->mapped_as_page = false; 1710f196ce38SLuigi Rizzo buffer_info->length = len; 1711f196ce38SLuigi Rizzo //buffer_info->next_to_watch = l; 1712f196ce38SLuigi Rizzo /* reload dma map */ 1713f196ce38SLuigi Rizzo dma_unmap_single(&adapter->pdev->dev, buffer_info->dma, 1714f196ce38SLuigi Rizzo NETMAP_BUF_SIZE, DMA_TO_DEVICE); 1715f196ce38SLuigi Rizzo buffer_info->dma = dma_map_single(&adapter->pdev->dev, 1716f196ce38SLuigi Rizzo addr, NETMAP_BUF_SIZE, DMA_TO_DEVICE); 1717f196ce38SLuigi Rizzo 1718f196ce38SLuigi Rizzo if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) { 1719f196ce38SLuigi Rizzo D("dma mapping error"); 1720f196ce38SLuigi Rizzo /* goto dma_error; See e1000_put_txbuf() */ 1721f196ce38SLuigi Rizzo /* XXX reset */ 1722f196ce38SLuigi Rizzo } 1723f196ce38SLuigi Rizzo tx_desc->buffer_addr = htole64(buffer_info->dma); //XXX 1724f196ce38SLuigi Rizzo 1725f196ce38SLuigi Rizzo #endif 1726f196ce38SLuigi Rizzo 1727f196ce38SLuigi Rizzo /* 1728f196ce38SLuigi Rizzo * The bus_dmamap_sync() can be one of wmb() or rmb() depending on direction. 1729f196ce38SLuigi Rizzo */ 1730f196ce38SLuigi Rizzo #define bus_dmamap_sync(_a, _b, _c) 1731f196ce38SLuigi Rizzo 1732f196ce38SLuigi Rizzo #endif /* linux */ 17336dba29a2SLuigi Rizzo 1734ce3ee1e7SLuigi Rizzo 17355644ccecSLuigi Rizzo /* 17365644ccecSLuigi Rizzo * functions to map NIC to KRING indexes (n2k) and vice versa (k2n) 17375644ccecSLuigi Rizzo */ 17385644ccecSLuigi Rizzo static inline int 173964ae02c3SLuigi Rizzo netmap_idx_n2k(struct netmap_kring *kr, int idx) 17405644ccecSLuigi Rizzo { 174164ae02c3SLuigi Rizzo int n = kr->nkr_num_slots; 174264ae02c3SLuigi Rizzo idx += kr->nkr_hwofs; 174364ae02c3SLuigi Rizzo if (idx < 0) 174464ae02c3SLuigi Rizzo return idx + n; 174564ae02c3SLuigi Rizzo else if (idx < n) 174664ae02c3SLuigi Rizzo return idx; 17475644ccecSLuigi Rizzo else 174864ae02c3SLuigi Rizzo return idx - n; 17495644ccecSLuigi Rizzo } 17505644ccecSLuigi Rizzo 17515644ccecSLuigi Rizzo 17525644ccecSLuigi Rizzo static inline int 175364ae02c3SLuigi Rizzo netmap_idx_k2n(struct netmap_kring *kr, int idx) 17545644ccecSLuigi Rizzo { 175564ae02c3SLuigi Rizzo int n = kr->nkr_num_slots; 175664ae02c3SLuigi Rizzo idx -= kr->nkr_hwofs; 175764ae02c3SLuigi Rizzo if (idx < 0) 175864ae02c3SLuigi Rizzo return idx + n; 175964ae02c3SLuigi Rizzo else if (idx < n) 176064ae02c3SLuigi Rizzo return idx; 17615644ccecSLuigi Rizzo else 176264ae02c3SLuigi Rizzo return idx - n; 17635644ccecSLuigi Rizzo } 17645644ccecSLuigi Rizzo 17655644ccecSLuigi Rizzo 1766d76bf4ffSLuigi Rizzo /* Entries of the look-up table. */ 1767d76bf4ffSLuigi Rizzo struct lut_entry { 1768d76bf4ffSLuigi Rizzo void *vaddr; /* virtual address. */ 1769849bec0eSLuigi Rizzo vm_paddr_t paddr; /* physical address. */ 1770d76bf4ffSLuigi Rizzo }; 1771d76bf4ffSLuigi Rizzo 1772d76bf4ffSLuigi Rizzo struct netmap_obj_pool; 1773d76bf4ffSLuigi Rizzo 177468b8534bSLuigi Rizzo /* 17756e10c8b8SLuigi Rizzo * NMB return the virtual address of a buffer (buffer 0 on bad index) 17766e10c8b8SLuigi Rizzo * PNMB also fills the physical address 177768b8534bSLuigi Rizzo */ 17786e10c8b8SLuigi Rizzo static inline void * 17794bf50f18SLuigi Rizzo NMB(struct netmap_adapter *na, struct netmap_slot *slot) 1780f9790aebSLuigi Rizzo { 1781847bf383SLuigi Rizzo struct lut_entry *lut = na->na_lut.lut; 1782f9790aebSLuigi Rizzo uint32_t i = slot->buf_idx; 1783847bf383SLuigi Rizzo return (unlikely(i >= na->na_lut.objtotal)) ? 1784f9790aebSLuigi Rizzo lut[0].vaddr : lut[i].vaddr; 1785f9790aebSLuigi Rizzo } 1786f9790aebSLuigi Rizzo 17874bf50f18SLuigi Rizzo static inline void * 17884bf50f18SLuigi Rizzo PNMB(struct netmap_adapter *na, struct netmap_slot *slot, uint64_t *pp) 17894bf50f18SLuigi Rizzo { 17904bf50f18SLuigi Rizzo uint32_t i = slot->buf_idx; 1791847bf383SLuigi Rizzo struct lut_entry *lut = na->na_lut.lut; 1792847bf383SLuigi Rizzo void *ret = (i >= na->na_lut.objtotal) ? lut[0].vaddr : lut[i].vaddr; 17934bf50f18SLuigi Rizzo 179437e3a6d3SLuigi Rizzo #ifndef _WIN32 1795847bf383SLuigi Rizzo *pp = (i >= na->na_lut.objtotal) ? lut[0].paddr : lut[i].paddr; 179637e3a6d3SLuigi Rizzo #else 179737e3a6d3SLuigi Rizzo *pp = (i >= na->na_lut.objtotal) ? (uint64_t)lut[0].paddr.QuadPart : (uint64_t)lut[i].paddr.QuadPart; 179837e3a6d3SLuigi Rizzo #endif 17994bf50f18SLuigi Rizzo return ret; 18004bf50f18SLuigi Rizzo } 18014bf50f18SLuigi Rizzo 1802f9790aebSLuigi Rizzo 180317885a7bSLuigi Rizzo /* 18048fd44c93SLuigi Rizzo * Structure associated to each netmap file descriptor. 18058fd44c93SLuigi Rizzo * It is created on open and left unbound (np_nifp == NULL). 18068fd44c93SLuigi Rizzo * A successful NIOCREGIF will set np_nifp and the first few fields; 18078fd44c93SLuigi Rizzo * this is protected by a global lock (NMG_LOCK) due to low contention. 1808f9790aebSLuigi Rizzo * 18098fd44c93SLuigi Rizzo * np_refs counts the number of references to the structure: one for the fd, 18108fd44c93SLuigi Rizzo * plus (on FreeBSD) one for each active mmap which we track ourselves 181185fe4e7cSLuigi Rizzo * (linux automatically tracks them, but FreeBSD does not). 18128fd44c93SLuigi Rizzo * np_refs is protected by NMG_LOCK. 181317885a7bSLuigi Rizzo * 18148fd44c93SLuigi Rizzo * Read access to the structure is lock free, because ni_nifp once set 18158fd44c93SLuigi Rizzo * can only go to 0 when nobody is using the entry anymore. Readers 18168fd44c93SLuigi Rizzo * must check that np_nifp != NULL before using the other fields. 1817f9790aebSLuigi Rizzo */ 1818f9790aebSLuigi Rizzo struct netmap_priv_d { 1819f9790aebSLuigi Rizzo struct netmap_if * volatile np_nifp; /* netmap if descriptor. */ 1820f9790aebSLuigi Rizzo 1821f9790aebSLuigi Rizzo struct netmap_adapter *np_na; 182237e3a6d3SLuigi Rizzo struct ifnet *np_ifp; 1823f0ea3689SLuigi Rizzo uint32_t np_flags; /* from the ioctl */ 1824847bf383SLuigi Rizzo u_int np_qfirst[NR_TXRX], 1825847bf383SLuigi Rizzo np_qlast[NR_TXRX]; /* range of tx/rx rings to scan */ 1826f0ea3689SLuigi Rizzo uint16_t np_txpoll; /* XXX and also np_rxpoll ? */ 1827c3e9b4dbSLuiz Otavio O Souza int np_sync_flags; /* to be passed to nm_sync */ 1828f9790aebSLuigi Rizzo 18298fd44c93SLuigi Rizzo int np_refs; /* use with NMG_LOCK held */ 1830f0ea3689SLuigi Rizzo 1831f0ea3689SLuigi Rizzo /* pointers to the selinfo to be used for selrecord. 1832f0ea3689SLuigi Rizzo * Either the local or the global one depending on the 1833f0ea3689SLuigi Rizzo * number of rings. 1834f0ea3689SLuigi Rizzo */ 1835847bf383SLuigi Rizzo NM_SELINFO_T *np_si[NR_TXRX]; 1836f0ea3689SLuigi Rizzo struct thread *np_td; /* kqueue, just debugging */ 1837f9790aebSLuigi Rizzo }; 1838f9790aebSLuigi Rizzo 183937e3a6d3SLuigi Rizzo struct netmap_priv_d *netmap_priv_new(void); 184037e3a6d3SLuigi Rizzo void netmap_priv_delete(struct netmap_priv_d *); 184137e3a6d3SLuigi Rizzo 184237e3a6d3SLuigi Rizzo static inline int nm_kring_pending(struct netmap_priv_d *np) 184337e3a6d3SLuigi Rizzo { 184437e3a6d3SLuigi Rizzo struct netmap_adapter *na = np->np_na; 184537e3a6d3SLuigi Rizzo enum txrx t; 184637e3a6d3SLuigi Rizzo int i; 184737e3a6d3SLuigi Rizzo 184837e3a6d3SLuigi Rizzo for_rx_tx(t) { 184937e3a6d3SLuigi Rizzo for (i = np->np_qfirst[t]; i < np->np_qlast[t]; i++) { 185037e3a6d3SLuigi Rizzo struct netmap_kring *kring = &NMR(na, t)[i]; 185137e3a6d3SLuigi Rizzo if (kring->nr_mode != kring->nr_pending_mode) { 185237e3a6d3SLuigi Rizzo return 1; 185337e3a6d3SLuigi Rizzo } 185437e3a6d3SLuigi Rizzo } 185537e3a6d3SLuigi Rizzo } 185637e3a6d3SLuigi Rizzo return 0; 185737e3a6d3SLuigi Rizzo } 185837e3a6d3SLuigi Rizzo 1859c3e9b4dbSLuiz Otavio O Souza #ifdef WITH_PIPES 1860c3e9b4dbSLuiz Otavio O Souza int netmap_pipe_txsync(struct netmap_kring *txkring, int flags); 1861c3e9b4dbSLuiz Otavio O Souza int netmap_pipe_rxsync(struct netmap_kring *rxkring, int flags); 1862c3e9b4dbSLuiz Otavio O Souza #endif /* WITH_PIPES */ 1863c3e9b4dbSLuiz Otavio O Souza 18644bf50f18SLuigi Rizzo #ifdef WITH_MONITOR 18654bf50f18SLuigi Rizzo 18664bf50f18SLuigi Rizzo struct netmap_monitor_adapter { 18674bf50f18SLuigi Rizzo struct netmap_adapter up; 18684bf50f18SLuigi Rizzo 18694bf50f18SLuigi Rizzo struct netmap_priv_d priv; 18704bf50f18SLuigi Rizzo uint32_t flags; 18714bf50f18SLuigi Rizzo }; 18724bf50f18SLuigi Rizzo 18734bf50f18SLuigi Rizzo #endif /* WITH_MONITOR */ 18744bf50f18SLuigi Rizzo 1875f9790aebSLuigi Rizzo 1876039dd540SLuigi Rizzo #ifdef WITH_GENERIC 1877f9790aebSLuigi Rizzo /* 1878f9790aebSLuigi Rizzo * generic netmap emulation for devices that do not have 1879f9790aebSLuigi Rizzo * native netmap support. 1880f9790aebSLuigi Rizzo */ 1881f9790aebSLuigi Rizzo int generic_netmap_attach(struct ifnet *ifp); 188237e3a6d3SLuigi Rizzo int generic_rx_handler(struct ifnet *ifp, struct mbuf *m);; 1883f9790aebSLuigi Rizzo 188437e3a6d3SLuigi Rizzo int nm_os_catch_rx(struct netmap_generic_adapter *gna, int intercept); 188537e3a6d3SLuigi Rizzo int nm_os_catch_tx(struct netmap_generic_adapter *gna, int intercept); 188637e3a6d3SLuigi Rizzo 1887c3e9b4dbSLuiz Otavio O Souza int na_is_generic(struct netmap_adapter *na); 1888c3e9b4dbSLuiz Otavio O Souza 188937e3a6d3SLuigi Rizzo /* 189037e3a6d3SLuigi Rizzo * the generic transmit routine is passed a structure to optionally 189137e3a6d3SLuigi Rizzo * build a queue of descriptors, in an OS-specific way. 189237e3a6d3SLuigi Rizzo * The payload is at addr, if non-null, and the routine should send or queue 189337e3a6d3SLuigi Rizzo * the packet, returning 0 if successful, 1 on failure. 189437e3a6d3SLuigi Rizzo * 189537e3a6d3SLuigi Rizzo * At the end, if head is non-null, there will be an additional call 189637e3a6d3SLuigi Rizzo * to the function with addr = NULL; this should tell the OS-specific 189737e3a6d3SLuigi Rizzo * routine to send the queue and free any resources. Failure is ignored. 189837e3a6d3SLuigi Rizzo */ 189937e3a6d3SLuigi Rizzo struct nm_os_gen_arg { 190037e3a6d3SLuigi Rizzo struct ifnet *ifp; 190137e3a6d3SLuigi Rizzo void *m; /* os-specific mbuf-like object */ 190237e3a6d3SLuigi Rizzo void *head, *tail; /* tailq, if the OS-specific routine needs to build one */ 190337e3a6d3SLuigi Rizzo void *addr; /* payload of current packet */ 190437e3a6d3SLuigi Rizzo u_int len; /* packet length */ 190537e3a6d3SLuigi Rizzo u_int ring_nr; /* packet length */ 190637e3a6d3SLuigi Rizzo u_int qevent; /* in txqdisc mode, place an event on this mbuf */ 190737e3a6d3SLuigi Rizzo }; 190837e3a6d3SLuigi Rizzo 190937e3a6d3SLuigi Rizzo int nm_os_generic_xmit_frame(struct nm_os_gen_arg *); 191037e3a6d3SLuigi Rizzo int nm_os_generic_find_num_desc(struct ifnet *ifp, u_int *tx, u_int *rx); 191137e3a6d3SLuigi Rizzo void nm_os_generic_find_num_queues(struct ifnet *ifp, u_int *txq, u_int *rxq); 191237e3a6d3SLuigi Rizzo void nm_os_generic_set_features(struct netmap_generic_adapter *gna); 191337e3a6d3SLuigi Rizzo 1914847bf383SLuigi Rizzo static inline struct ifnet* 1915847bf383SLuigi Rizzo netmap_generic_getifp(struct netmap_generic_adapter *gna) 1916847bf383SLuigi Rizzo { 1917847bf383SLuigi Rizzo if (gna->prev) 1918847bf383SLuigi Rizzo return gna->prev->ifp; 1919847bf383SLuigi Rizzo 1920847bf383SLuigi Rizzo return gna->up.up.ifp; 1921847bf383SLuigi Rizzo } 1922f9790aebSLuigi Rizzo 192337e3a6d3SLuigi Rizzo void netmap_generic_irq(struct netmap_adapter *na, u_int q, u_int *work_done); 192437e3a6d3SLuigi Rizzo 19254bf50f18SLuigi Rizzo //#define RATE_GENERIC /* Enables communication statistics for generic. */ 19264bf50f18SLuigi Rizzo #ifdef RATE_GENERIC 19274bf50f18SLuigi Rizzo void generic_rate(int txp, int txs, int txi, int rxp, int rxs, int rxi); 19284bf50f18SLuigi Rizzo #else 19294bf50f18SLuigi Rizzo #define generic_rate(txp, txs, txi, rxp, rxs, rxi) 19304bf50f18SLuigi Rizzo #endif 19314bf50f18SLuigi Rizzo 1932f9790aebSLuigi Rizzo /* 1933f9790aebSLuigi Rizzo * netmap_mitigation API. This is used by the generic adapter 1934f9790aebSLuigi Rizzo * to reduce the number of interrupt requests/selwakeup 1935f9790aebSLuigi Rizzo * to clients on incoming packets. 1936f9790aebSLuigi Rizzo */ 193737e3a6d3SLuigi Rizzo void nm_os_mitigation_init(struct nm_generic_mit *mit, int idx, 19384bf50f18SLuigi Rizzo struct netmap_adapter *na); 193937e3a6d3SLuigi Rizzo void nm_os_mitigation_start(struct nm_generic_mit *mit); 194037e3a6d3SLuigi Rizzo void nm_os_mitigation_restart(struct nm_generic_mit *mit); 194137e3a6d3SLuigi Rizzo int nm_os_mitigation_active(struct nm_generic_mit *mit); 194237e3a6d3SLuigi Rizzo void nm_os_mitigation_cleanup(struct nm_generic_mit *mit); 194337e3a6d3SLuigi Rizzo #else /* !WITH_GENERIC */ 194437e3a6d3SLuigi Rizzo #define generic_netmap_attach(ifp) (EOPNOTSUPP) 1945c3e9b4dbSLuiz Otavio O Souza #define na_is_generic(na) (0) 1946039dd540SLuigi Rizzo #endif /* WITH_GENERIC */ 1947f0ea3689SLuigi Rizzo 1948f0ea3689SLuigi Rizzo /* Shared declarations for the VALE switch. */ 1949f0ea3689SLuigi Rizzo 1950f0ea3689SLuigi Rizzo /* 1951f0ea3689SLuigi Rizzo * Each transmit queue accumulates a batch of packets into 1952f0ea3689SLuigi Rizzo * a structure before forwarding. Packets to the same 1953f0ea3689SLuigi Rizzo * destination are put in a list using ft_next as a link field. 1954f0ea3689SLuigi Rizzo * ft_frags and ft_next are valid only on the first fragment. 1955f0ea3689SLuigi Rizzo */ 1956f0ea3689SLuigi Rizzo struct nm_bdg_fwd { /* forwarding entry for a bridge */ 1957f0ea3689SLuigi Rizzo void *ft_buf; /* netmap or indirect buffer */ 1958f0ea3689SLuigi Rizzo uint8_t ft_frags; /* how many fragments (only on 1st frag) */ 1959f0ea3689SLuigi Rizzo uint8_t _ft_port; /* dst port (unused) */ 1960f0ea3689SLuigi Rizzo uint16_t ft_flags; /* flags, e.g. indirect */ 1961f0ea3689SLuigi Rizzo uint16_t ft_len; /* src fragment len */ 1962f0ea3689SLuigi Rizzo uint16_t ft_next; /* next packet to same destination */ 1963f0ea3689SLuigi Rizzo }; 1964f0ea3689SLuigi Rizzo 1965f0ea3689SLuigi Rizzo /* struct 'virtio_net_hdr' from linux. */ 1966f0ea3689SLuigi Rizzo struct nm_vnet_hdr { 1967f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_F_NEEDS_CSUM 1 /* Use csum_start, csum_offset */ 1968f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_F_DATA_VALID 2 /* Csum is valid */ 1969f0ea3689SLuigi Rizzo uint8_t flags; 1970f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_NONE 0 /* Not a GSO frame */ 1971f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_TCPV4 1 /* GSO frame, IPv4 TCP (TSO) */ 1972f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_UDP 3 /* GSO frame, IPv4 UDP (UFO) */ 1973f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_TCPV6 4 /* GSO frame, IPv6 TCP */ 1974f0ea3689SLuigi Rizzo #define VIRTIO_NET_HDR_GSO_ECN 0x80 /* TCP has ECN set */ 1975f0ea3689SLuigi Rizzo uint8_t gso_type; 1976f0ea3689SLuigi Rizzo uint16_t hdr_len; 1977f0ea3689SLuigi Rizzo uint16_t gso_size; 1978f0ea3689SLuigi Rizzo uint16_t csum_start; 1979f0ea3689SLuigi Rizzo uint16_t csum_offset; 1980f0ea3689SLuigi Rizzo }; 1981f0ea3689SLuigi Rizzo 1982f0ea3689SLuigi Rizzo #define WORST_CASE_GSO_HEADER (14+40+60) /* IPv6 + TCP */ 1983f0ea3689SLuigi Rizzo 1984f0ea3689SLuigi Rizzo /* Private definitions for IPv4, IPv6, UDP and TCP headers. */ 1985f0ea3689SLuigi Rizzo 1986f0ea3689SLuigi Rizzo struct nm_iphdr { 1987f0ea3689SLuigi Rizzo uint8_t version_ihl; 1988f0ea3689SLuigi Rizzo uint8_t tos; 1989f0ea3689SLuigi Rizzo uint16_t tot_len; 1990f0ea3689SLuigi Rizzo uint16_t id; 1991f0ea3689SLuigi Rizzo uint16_t frag_off; 1992f0ea3689SLuigi Rizzo uint8_t ttl; 1993f0ea3689SLuigi Rizzo uint8_t protocol; 1994f0ea3689SLuigi Rizzo uint16_t check; 1995f0ea3689SLuigi Rizzo uint32_t saddr; 1996f0ea3689SLuigi Rizzo uint32_t daddr; 1997f0ea3689SLuigi Rizzo /*The options start here. */ 1998f0ea3689SLuigi Rizzo }; 1999f0ea3689SLuigi Rizzo 2000f0ea3689SLuigi Rizzo struct nm_tcphdr { 2001f0ea3689SLuigi Rizzo uint16_t source; 2002f0ea3689SLuigi Rizzo uint16_t dest; 2003f0ea3689SLuigi Rizzo uint32_t seq; 2004f0ea3689SLuigi Rizzo uint32_t ack_seq; 2005f0ea3689SLuigi Rizzo uint8_t doff; /* Data offset + Reserved */ 2006f0ea3689SLuigi Rizzo uint8_t flags; 2007f0ea3689SLuigi Rizzo uint16_t window; 2008f0ea3689SLuigi Rizzo uint16_t check; 2009f0ea3689SLuigi Rizzo uint16_t urg_ptr; 2010f0ea3689SLuigi Rizzo }; 2011f0ea3689SLuigi Rizzo 2012f0ea3689SLuigi Rizzo struct nm_udphdr { 2013f0ea3689SLuigi Rizzo uint16_t source; 2014f0ea3689SLuigi Rizzo uint16_t dest; 2015f0ea3689SLuigi Rizzo uint16_t len; 2016f0ea3689SLuigi Rizzo uint16_t check; 2017f0ea3689SLuigi Rizzo }; 2018f0ea3689SLuigi Rizzo 2019f0ea3689SLuigi Rizzo struct nm_ipv6hdr { 2020f0ea3689SLuigi Rizzo uint8_t priority_version; 2021f0ea3689SLuigi Rizzo uint8_t flow_lbl[3]; 2022f0ea3689SLuigi Rizzo 2023f0ea3689SLuigi Rizzo uint16_t payload_len; 2024f0ea3689SLuigi Rizzo uint8_t nexthdr; 2025f0ea3689SLuigi Rizzo uint8_t hop_limit; 2026f0ea3689SLuigi Rizzo 2027f0ea3689SLuigi Rizzo uint8_t saddr[16]; 2028f0ea3689SLuigi Rizzo uint8_t daddr[16]; 2029f0ea3689SLuigi Rizzo }; 2030f0ea3689SLuigi Rizzo 2031f0ea3689SLuigi Rizzo /* Type used to store a checksum (in host byte order) that hasn't been 2032f0ea3689SLuigi Rizzo * folded yet. 2033f0ea3689SLuigi Rizzo */ 2034f0ea3689SLuigi Rizzo #define rawsum_t uint32_t 2035f0ea3689SLuigi Rizzo 203637e3a6d3SLuigi Rizzo rawsum_t nm_os_csum_raw(uint8_t *data, size_t len, rawsum_t cur_sum); 203737e3a6d3SLuigi Rizzo uint16_t nm_os_csum_ipv4(struct nm_iphdr *iph); 203837e3a6d3SLuigi Rizzo void nm_os_csum_tcpudp_ipv4(struct nm_iphdr *iph, void *data, 2039f0ea3689SLuigi Rizzo size_t datalen, uint16_t *check); 204037e3a6d3SLuigi Rizzo void nm_os_csum_tcpudp_ipv6(struct nm_ipv6hdr *ip6h, void *data, 2041f0ea3689SLuigi Rizzo size_t datalen, uint16_t *check); 204237e3a6d3SLuigi Rizzo uint16_t nm_os_csum_fold(rawsum_t cur_sum); 2043f0ea3689SLuigi Rizzo 2044f0ea3689SLuigi Rizzo void bdg_mismatch_datapath(struct netmap_vp_adapter *na, 2045f0ea3689SLuigi Rizzo struct netmap_vp_adapter *dst_na, 204637e3a6d3SLuigi Rizzo const struct nm_bdg_fwd *ft_p, 204737e3a6d3SLuigi Rizzo struct netmap_ring *dst_ring, 2048f0ea3689SLuigi Rizzo u_int *j, u_int lim, u_int *howmany); 20494bf50f18SLuigi Rizzo 20504bf50f18SLuigi Rizzo /* persistent virtual port routines */ 205137e3a6d3SLuigi Rizzo int nm_os_vi_persist(const char *, struct ifnet **); 205237e3a6d3SLuigi Rizzo void nm_os_vi_detach(struct ifnet *); 205337e3a6d3SLuigi Rizzo void nm_os_vi_init_index(void); 205437e3a6d3SLuigi Rizzo 205537e3a6d3SLuigi Rizzo /* 205637e3a6d3SLuigi Rizzo * kernel thread routines 205737e3a6d3SLuigi Rizzo */ 2058c3e9b4dbSLuiz Otavio O Souza struct nm_kctx; /* OS-specific kernel context - opaque */ 2059c3e9b4dbSLuiz Otavio O Souza typedef void (*nm_kctx_worker_fn_t)(void *data, int is_kthread); 2060c3e9b4dbSLuiz Otavio O Souza typedef void (*nm_kctx_notify_fn_t)(void *data); 206137e3a6d3SLuigi Rizzo 206237e3a6d3SLuigi Rizzo /* kthread configuration */ 2063c3e9b4dbSLuiz Otavio O Souza struct nm_kctx_cfg { 206437e3a6d3SLuigi Rizzo long type; /* kthread type/identifier */ 2065c3e9b4dbSLuiz Otavio O Souza nm_kctx_worker_fn_t worker_fn; /* worker function */ 206637e3a6d3SLuigi Rizzo void *worker_private;/* worker parameter */ 2067c3e9b4dbSLuiz Otavio O Souza nm_kctx_notify_fn_t notify_fn; /* notify function */ 206837e3a6d3SLuigi Rizzo int attach_user; /* attach kthread to user process */ 2069c3e9b4dbSLuiz Otavio O Souza int use_kthread; /* use a kthread for the context */ 207037e3a6d3SLuigi Rizzo }; 207137e3a6d3SLuigi Rizzo /* kthread configuration */ 2072c3e9b4dbSLuiz Otavio O Souza struct nm_kctx *nm_os_kctx_create(struct nm_kctx_cfg *cfg, 2073844a6f0cSLuigi Rizzo unsigned int cfgtype, 2074844a6f0cSLuigi Rizzo void *opaque); 2075c3e9b4dbSLuiz Otavio O Souza int nm_os_kctx_worker_start(struct nm_kctx *); 2076c3e9b4dbSLuiz Otavio O Souza void nm_os_kctx_worker_stop(struct nm_kctx *); 2077c3e9b4dbSLuiz Otavio O Souza void nm_os_kctx_destroy(struct nm_kctx *); 2078c3e9b4dbSLuiz Otavio O Souza void nm_os_kctx_worker_wakeup(struct nm_kctx *nmk); 2079c3e9b4dbSLuiz Otavio O Souza void nm_os_kctx_send_irq(struct nm_kctx *); 2080c3e9b4dbSLuiz Otavio O Souza void nm_os_kctx_worker_setaff(struct nm_kctx *, int); 208137e3a6d3SLuigi Rizzo u_int nm_os_ncpus(void); 208237e3a6d3SLuigi Rizzo 208337e3a6d3SLuigi Rizzo #ifdef WITH_PTNETMAP_HOST 208437e3a6d3SLuigi Rizzo /* 208537e3a6d3SLuigi Rizzo * netmap adapter for host ptnetmap ports 208637e3a6d3SLuigi Rizzo */ 208737e3a6d3SLuigi Rizzo struct netmap_pt_host_adapter { 208837e3a6d3SLuigi Rizzo struct netmap_adapter up; 208937e3a6d3SLuigi Rizzo 2090c3e9b4dbSLuiz Otavio O Souza /* the passed-through adapter */ 209137e3a6d3SLuigi Rizzo struct netmap_adapter *parent; 2092c3e9b4dbSLuiz Otavio O Souza /* parent->na_flags, saved at NETMAP_PT_HOST_CREATE time, 2093c3e9b4dbSLuiz Otavio O Souza * and restored at NETMAP_PT_HOST_DELETE time */ 2094c3e9b4dbSLuiz Otavio O Souza uint32_t parent_na_flags; 2095c3e9b4dbSLuiz Otavio O Souza 209637e3a6d3SLuigi Rizzo int (*parent_nm_notify)(struct netmap_kring *kring, int flags); 209737e3a6d3SLuigi Rizzo void *ptns; 209837e3a6d3SLuigi Rizzo }; 209937e3a6d3SLuigi Rizzo /* ptnetmap HOST routines */ 2100c3e9b4dbSLuiz Otavio O Souza int netmap_get_pt_host_na(struct nmreq *nmr, struct netmap_adapter **na, 2101c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d * nmd, int create); 210237e3a6d3SLuigi Rizzo int ptnetmap_ctl(struct nmreq *nmr, struct netmap_adapter *na); 210337e3a6d3SLuigi Rizzo static inline int 210437e3a6d3SLuigi Rizzo nm_ptnetmap_host_on(struct netmap_adapter *na) 210537e3a6d3SLuigi Rizzo { 210637e3a6d3SLuigi Rizzo return na && na->na_flags & NAF_PTNETMAP_HOST; 210737e3a6d3SLuigi Rizzo } 210837e3a6d3SLuigi Rizzo #else /* !WITH_PTNETMAP_HOST */ 2109c3e9b4dbSLuiz Otavio O Souza #define netmap_get_pt_host_na(nmr, _2, _3, _4) \ 211037e3a6d3SLuigi Rizzo ((nmr)->nr_flags & (NR_PTNETMAP_HOST) ? EOPNOTSUPP : 0) 211137e3a6d3SLuigi Rizzo #define ptnetmap_ctl(_1, _2) EINVAL 211237e3a6d3SLuigi Rizzo #define nm_ptnetmap_host_on(_1) EINVAL 211337e3a6d3SLuigi Rizzo #endif /* !WITH_PTNETMAP_HOST */ 211437e3a6d3SLuigi Rizzo 211537e3a6d3SLuigi Rizzo #ifdef WITH_PTNETMAP_GUEST 211637e3a6d3SLuigi Rizzo /* ptnetmap GUEST routines */ 211737e3a6d3SLuigi Rizzo 211837e3a6d3SLuigi Rizzo /* 211937e3a6d3SLuigi Rizzo * netmap adapter for guest ptnetmap ports 212037e3a6d3SLuigi Rizzo */ 212137e3a6d3SLuigi Rizzo struct netmap_pt_guest_adapter { 212237e3a6d3SLuigi Rizzo /* The netmap adapter to be used by netmap applications. 212337e3a6d3SLuigi Rizzo * This field must be the first, to allow upcast. */ 212437e3a6d3SLuigi Rizzo struct netmap_hw_adapter hwup; 212537e3a6d3SLuigi Rizzo 212637e3a6d3SLuigi Rizzo /* The netmap adapter to be used by the driver. */ 212737e3a6d3SLuigi Rizzo struct netmap_hw_adapter dr; 212837e3a6d3SLuigi Rizzo 212937e3a6d3SLuigi Rizzo void *csb; 213037e3a6d3SLuigi Rizzo 213137e3a6d3SLuigi Rizzo /* Reference counter to track users of backend netmap port: the 213237e3a6d3SLuigi Rizzo * network stack and netmap clients. 213337e3a6d3SLuigi Rizzo * Used to decide when we need (de)allocate krings/rings and 213437e3a6d3SLuigi Rizzo * start (stop) ptnetmap kthreads. */ 213537e3a6d3SLuigi Rizzo int backend_regifs; 213637e3a6d3SLuigi Rizzo 213737e3a6d3SLuigi Rizzo }; 213837e3a6d3SLuigi Rizzo 2139844a6f0cSLuigi Rizzo int netmap_pt_guest_attach(struct netmap_adapter *na, void *csb, 2140844a6f0cSLuigi Rizzo unsigned int nifp_offset, unsigned int memid); 214137e3a6d3SLuigi Rizzo struct ptnet_ring; 214237e3a6d3SLuigi Rizzo bool netmap_pt_guest_txsync(struct ptnet_ring *ptring, struct netmap_kring *kring, 214337e3a6d3SLuigi Rizzo int flags); 214437e3a6d3SLuigi Rizzo bool netmap_pt_guest_rxsync(struct ptnet_ring *ptring, struct netmap_kring *kring, 214537e3a6d3SLuigi Rizzo int flags); 214637e3a6d3SLuigi Rizzo int ptnet_nm_krings_create(struct netmap_adapter *na); 214737e3a6d3SLuigi Rizzo void ptnet_nm_krings_delete(struct netmap_adapter *na); 214837e3a6d3SLuigi Rizzo void ptnet_nm_dtor(struct netmap_adapter *na); 214937e3a6d3SLuigi Rizzo #endif /* WITH_PTNETMAP_GUEST */ 21504bf50f18SLuigi Rizzo 215168b8534bSLuigi Rizzo #endif /* _NET_NETMAP_KERN_H_ */ 2152