168b8534bSLuigi Rizzo /* 2f196ce38SLuigi Rizzo * Copyright (C) 2011-2012 Matteo Landi, Luigi Rizzo. All rights reserved. 368b8534bSLuigi Rizzo * 468b8534bSLuigi Rizzo * Redistribution and use in source and binary forms, with or without 568b8534bSLuigi Rizzo * modification, are permitted provided that the following conditions 668b8534bSLuigi Rizzo * are met: 768b8534bSLuigi Rizzo * 1. Redistributions of source code must retain the above copyright 868b8534bSLuigi Rizzo * notice, this list of conditions and the following disclaimer. 968b8534bSLuigi Rizzo * 2. Redistributions in binary form must reproduce the above copyright 1068b8534bSLuigi Rizzo * notice, this list of conditions and the following disclaimer in the 1168b8534bSLuigi Rizzo * documentation and/or other materials provided with the distribution. 1268b8534bSLuigi Rizzo * 1368b8534bSLuigi Rizzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1468b8534bSLuigi Rizzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1568b8534bSLuigi Rizzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1668b8534bSLuigi Rizzo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1768b8534bSLuigi Rizzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1868b8534bSLuigi Rizzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 1968b8534bSLuigi Rizzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2068b8534bSLuigi Rizzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2168b8534bSLuigi Rizzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2268b8534bSLuigi Rizzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2368b8534bSLuigi Rizzo * SUCH DAMAGE. 2468b8534bSLuigi Rizzo */ 2568b8534bSLuigi Rizzo 26f196ce38SLuigi Rizzo #define NM_BRIDGE 27f196ce38SLuigi Rizzo 2868b8534bSLuigi Rizzo /* 2968b8534bSLuigi Rizzo * This module supports memory mapped access to network devices, 3068b8534bSLuigi Rizzo * see netmap(4). 3168b8534bSLuigi Rizzo * 3268b8534bSLuigi Rizzo * The module uses a large, memory pool allocated by the kernel 3368b8534bSLuigi Rizzo * and accessible as mmapped memory by multiple userspace threads/processes. 3468b8534bSLuigi Rizzo * The memory pool contains packet buffers and "netmap rings", 3568b8534bSLuigi Rizzo * i.e. user-accessible copies of the interface's queues. 3668b8534bSLuigi Rizzo * 3768b8534bSLuigi Rizzo * Access to the network card works like this: 3868b8534bSLuigi Rizzo * 1. a process/thread issues one or more open() on /dev/netmap, to create 3968b8534bSLuigi Rizzo * select()able file descriptor on which events are reported. 4068b8534bSLuigi Rizzo * 2. on each descriptor, the process issues an ioctl() to identify 4168b8534bSLuigi Rizzo * the interface that should report events to the file descriptor. 4268b8534bSLuigi Rizzo * 3. on each descriptor, the process issues an mmap() request to 4368b8534bSLuigi Rizzo * map the shared memory region within the process' address space. 4468b8534bSLuigi Rizzo * The list of interesting queues is indicated by a location in 4568b8534bSLuigi Rizzo * the shared memory region. 4668b8534bSLuigi Rizzo * 4. using the functions in the netmap(4) userspace API, a process 4768b8534bSLuigi Rizzo * can look up the occupation state of a queue, access memory buffers, 4868b8534bSLuigi Rizzo * and retrieve received packets or enqueue packets to transmit. 4968b8534bSLuigi Rizzo * 5. using some ioctl()s the process can synchronize the userspace view 5068b8534bSLuigi Rizzo * of the queue with the actual status in the kernel. This includes both 5168b8534bSLuigi Rizzo * receiving the notification of new packets, and transmitting new 5268b8534bSLuigi Rizzo * packets on the output interface. 5368b8534bSLuigi Rizzo * 6. select() or poll() can be used to wait for events on individual 5468b8534bSLuigi Rizzo * transmit or receive queues (or all queues for a given interface). 5568b8534bSLuigi Rizzo */ 5668b8534bSLuigi Rizzo 57f196ce38SLuigi Rizzo #ifdef linux 58f196ce38SLuigi Rizzo #include "bsd_glue.h" 59f196ce38SLuigi Rizzo static netdev_tx_t netmap_start_linux(struct sk_buff *skb, struct net_device *dev); 60f196ce38SLuigi Rizzo #endif /* linux */ 61*01c7d25fSLuigi Rizzo 62f196ce38SLuigi Rizzo #ifdef __APPLE__ 63f196ce38SLuigi Rizzo #include "osx_glue.h" 64*01c7d25fSLuigi Rizzo #endif /* __APPLE__ */ 65*01c7d25fSLuigi Rizzo 66f196ce38SLuigi Rizzo #ifdef __FreeBSD__ 6768b8534bSLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */ 6868b8534bSLuigi Rizzo __FBSDID("$FreeBSD$"); 6968b8534bSLuigi Rizzo 7068b8534bSLuigi Rizzo #include <sys/types.h> 7168b8534bSLuigi Rizzo #include <sys/module.h> 7268b8534bSLuigi Rizzo #include <sys/errno.h> 7368b8534bSLuigi Rizzo #include <sys/param.h> /* defines used in kernel.h */ 74506cc70cSLuigi Rizzo #include <sys/jail.h> 7568b8534bSLuigi Rizzo #include <sys/kernel.h> /* types used in module initialization */ 7668b8534bSLuigi Rizzo #include <sys/conf.h> /* cdevsw struct */ 7768b8534bSLuigi Rizzo #include <sys/uio.h> /* uio struct */ 7868b8534bSLuigi Rizzo #include <sys/sockio.h> 7968b8534bSLuigi Rizzo #include <sys/socketvar.h> /* struct socket */ 8068b8534bSLuigi Rizzo #include <sys/malloc.h> 8168b8534bSLuigi Rizzo #include <sys/mman.h> /* PROT_EXEC */ 8268b8534bSLuigi Rizzo #include <sys/poll.h> 83506cc70cSLuigi Rizzo #include <sys/proc.h> 8468b8534bSLuigi Rizzo #include <vm/vm.h> /* vtophys */ 8568b8534bSLuigi Rizzo #include <vm/pmap.h> /* vtophys */ 8668b8534bSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */ 8768b8534bSLuigi Rizzo #include <machine/bus.h> 8868b8534bSLuigi Rizzo #include <sys/selinfo.h> 8968b8534bSLuigi Rizzo #include <sys/sysctl.h> 9068b8534bSLuigi Rizzo #include <net/if.h> 9168b8534bSLuigi Rizzo #include <net/bpf.h> /* BIOCIMMEDIATE */ 92506cc70cSLuigi Rizzo #include <net/vnet.h> 9368b8534bSLuigi Rizzo #include <net/netmap.h> 9468b8534bSLuigi Rizzo #include <dev/netmap/netmap_kern.h> 9568b8534bSLuigi Rizzo #include <machine/bus.h> /* bus_dmamap_* */ 9668b8534bSLuigi Rizzo 9768b8534bSLuigi Rizzo MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map"); 98f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */ 9968b8534bSLuigi Rizzo 10068b8534bSLuigi Rizzo /* 10168b8534bSLuigi Rizzo * lock and unlock for the netmap memory allocator 10268b8534bSLuigi Rizzo */ 10364ae02c3SLuigi Rizzo #define NMA_LOCK() mtx_lock(&nm_mem->nm_mtx); 10464ae02c3SLuigi Rizzo #define NMA_UNLOCK() mtx_unlock(&nm_mem->nm_mtx); 1055819da83SLuigi Rizzo struct netmap_mem_d; 10664ae02c3SLuigi Rizzo static struct netmap_mem_d *nm_mem; /* Our memory allocator. */ 1075819da83SLuigi Rizzo 1085819da83SLuigi Rizzo u_int netmap_total_buffers; 1095819da83SLuigi Rizzo char *netmap_buffer_base; /* address of an invalid buffer */ 1105819da83SLuigi Rizzo 1115819da83SLuigi Rizzo /* user-controlled variables */ 1125819da83SLuigi Rizzo int netmap_verbose; 1135819da83SLuigi Rizzo 1145819da83SLuigi Rizzo static int netmap_no_timestamp; /* don't timestamp on rxsync */ 1155819da83SLuigi Rizzo 1165819da83SLuigi Rizzo SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args"); 1175819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose, 1185819da83SLuigi Rizzo CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode"); 1195819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp, 1205819da83SLuigi Rizzo CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp"); 1215819da83SLuigi Rizzo int netmap_buf_size = 2048; 1225819da83SLuigi Rizzo TUNABLE_INT("hw.netmap.buf_size", &netmap_buf_size); 1235819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, buf_size, 1245819da83SLuigi Rizzo CTLFLAG_RD, &netmap_buf_size, 0, "Size of packet buffers"); 1255819da83SLuigi Rizzo int netmap_mitigate = 1; 1265819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mitigate, CTLFLAG_RW, &netmap_mitigate, 0, ""); 127c85cb1a0SLuigi Rizzo int netmap_no_pendintr = 1; 1285819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr, 1295819da83SLuigi Rizzo CTLFLAG_RW, &netmap_no_pendintr, 0, "Always look for new received packets."); 1305819da83SLuigi Rizzo 131f196ce38SLuigi Rizzo int netmap_drop = 0; /* debugging */ 132f196ce38SLuigi Rizzo int netmap_flags = 0; /* debug flags */ 133f196ce38SLuigi Rizzo int netmap_copy = 0; /* debugging, copy content */ 134f196ce38SLuigi Rizzo 135f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, drop, CTLFLAG_RW, &netmap_drop, 0 , ""); 136f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, flags, CTLFLAG_RW, &netmap_flags, 0 , ""); 137f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, copy, CTLFLAG_RW, &netmap_copy, 0 , ""); 138f196ce38SLuigi Rizzo 139f196ce38SLuigi Rizzo #ifdef NM_BRIDGE /* support for netmap bridge */ 140f196ce38SLuigi Rizzo 141f196ce38SLuigi Rizzo /* 142f196ce38SLuigi Rizzo * system parameters. 143f196ce38SLuigi Rizzo * 144f196ce38SLuigi Rizzo * All switched ports have prefix NM_NAME. 145f196ce38SLuigi Rizzo * The switch has a max of NM_BDG_MAXPORTS ports (often stored in a bitmap, 146f196ce38SLuigi Rizzo * so a practical upper bound is 64). 147f196ce38SLuigi Rizzo * Each tx ring is read-write, whereas rx rings are readonly (XXX not done yet). 148f196ce38SLuigi Rizzo * The virtual interfaces use per-queue lock instead of core lock. 149f196ce38SLuigi Rizzo * In the tx loop, we aggregate traffic in batches to make all operations 150f196ce38SLuigi Rizzo * faster. The batch size is NM_BDG_BATCH 151f196ce38SLuigi Rizzo */ 152f196ce38SLuigi Rizzo #define NM_NAME "vale" /* prefix for the interface */ 153f196ce38SLuigi Rizzo #define NM_BDG_MAXPORTS 16 /* up to 64 ? */ 154f196ce38SLuigi Rizzo #define NM_BRIDGE_RINGSIZE 1024 /* in the device */ 155f196ce38SLuigi Rizzo #define NM_BDG_HASH 1024 /* forwarding table entries */ 156f196ce38SLuigi Rizzo #define NM_BDG_BATCH 1024 /* entries in the forwarding buffer */ 157f196ce38SLuigi Rizzo #define NM_BRIDGES 4 /* number of bridges */ 158f196ce38SLuigi Rizzo int netmap_bridge = NM_BDG_BATCH; /* bridge batch size */ 159f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, bridge, CTLFLAG_RW, &netmap_bridge, 0 , ""); 160*01c7d25fSLuigi Rizzo 161f196ce38SLuigi Rizzo #ifdef linux 162f196ce38SLuigi Rizzo #define ADD_BDG_REF(ifp) (NA(ifp)->if_refcount++) 163f196ce38SLuigi Rizzo #define DROP_BDG_REF(ifp) (NA(ifp)->if_refcount-- <= 1) 164f196ce38SLuigi Rizzo #else /* !linux */ 165f196ce38SLuigi Rizzo #define ADD_BDG_REF(ifp) (ifp)->if_refcount++ 166f196ce38SLuigi Rizzo #define DROP_BDG_REF(ifp) refcount_release(&(ifp)->if_refcount) 167f196ce38SLuigi Rizzo #ifdef __FreeBSD__ 168f196ce38SLuigi Rizzo #include <sys/endian.h> 169f196ce38SLuigi Rizzo #include <sys/refcount.h> 170f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */ 171*01c7d25fSLuigi Rizzo #define prefetch(x) __builtin_prefetch(x) 172f196ce38SLuigi Rizzo #endif /* !linux */ 173f196ce38SLuigi Rizzo 174f196ce38SLuigi Rizzo static void bdg_netmap_attach(struct ifnet *ifp); 175f196ce38SLuigi Rizzo static int bdg_netmap_reg(struct ifnet *ifp, int onoff); 176f196ce38SLuigi Rizzo /* per-tx-queue entry */ 177f196ce38SLuigi Rizzo struct nm_bdg_fwd { /* forwarding entry for a bridge */ 178f196ce38SLuigi Rizzo void *buf; 179f196ce38SLuigi Rizzo uint64_t dst; /* dst mask */ 180f196ce38SLuigi Rizzo uint32_t src; /* src index ? */ 181f196ce38SLuigi Rizzo uint16_t len; /* src len */ 182f196ce38SLuigi Rizzo }; 183f196ce38SLuigi Rizzo 184f196ce38SLuigi Rizzo struct nm_hash_ent { 185f196ce38SLuigi Rizzo uint64_t mac; /* the top 2 bytes are the epoch */ 186f196ce38SLuigi Rizzo uint64_t ports; 187f196ce38SLuigi Rizzo }; 188f196ce38SLuigi Rizzo 189f196ce38SLuigi Rizzo /* 190f196ce38SLuigi Rizzo * Interfaces for a bridge are all in ports[]. 191f196ce38SLuigi Rizzo * The array has fixed size, an empty entry does not terminate 192f196ce38SLuigi Rizzo * the search. 193f196ce38SLuigi Rizzo */ 194f196ce38SLuigi Rizzo struct nm_bridge { 195f196ce38SLuigi Rizzo struct ifnet *bdg_ports[NM_BDG_MAXPORTS]; 196f196ce38SLuigi Rizzo int n_ports; 197f196ce38SLuigi Rizzo uint64_t act_ports; 198f196ce38SLuigi Rizzo int freelist; /* first buffer index */ 199f196ce38SLuigi Rizzo NM_SELINFO_T si; /* poll/select wait queue */ 200f196ce38SLuigi Rizzo NM_LOCK_T bdg_lock; /* protect the selinfo ? */ 201f196ce38SLuigi Rizzo 202f196ce38SLuigi Rizzo /* the forwarding table, MAC+ports */ 203f196ce38SLuigi Rizzo struct nm_hash_ent ht[NM_BDG_HASH]; 204f196ce38SLuigi Rizzo 205f196ce38SLuigi Rizzo int namelen; /* 0 means free */ 206f196ce38SLuigi Rizzo char basename[IFNAMSIZ]; 207f196ce38SLuigi Rizzo }; 208f196ce38SLuigi Rizzo 209f196ce38SLuigi Rizzo struct nm_bridge nm_bridges[NM_BRIDGES]; 210f196ce38SLuigi Rizzo 211f196ce38SLuigi Rizzo #define BDG_LOCK(b) mtx_lock(&(b)->bdg_lock) 212f196ce38SLuigi Rizzo #define BDG_UNLOCK(b) mtx_unlock(&(b)->bdg_lock) 213f196ce38SLuigi Rizzo 214f196ce38SLuigi Rizzo /* 215f196ce38SLuigi Rizzo * NA(ifp)->bdg_port port index 216f196ce38SLuigi Rizzo */ 217f196ce38SLuigi Rizzo 218f196ce38SLuigi Rizzo // XXX only for multiples of 64 bytes, non overlapped. 219f196ce38SLuigi Rizzo static inline void 220f196ce38SLuigi Rizzo pkt_copy(void *_src, void *_dst, int l) 221f196ce38SLuigi Rizzo { 222f196ce38SLuigi Rizzo uint64_t *src = _src; 223f196ce38SLuigi Rizzo uint64_t *dst = _dst; 224f196ce38SLuigi Rizzo if (unlikely(l >= 1024)) { 225f196ce38SLuigi Rizzo bcopy(src, dst, l); 226f196ce38SLuigi Rizzo return; 227f196ce38SLuigi Rizzo } 228f196ce38SLuigi Rizzo for (; likely(l > 0); l-=64) { 229f196ce38SLuigi Rizzo *dst++ = *src++; 230f196ce38SLuigi Rizzo *dst++ = *src++; 231f196ce38SLuigi Rizzo *dst++ = *src++; 232f196ce38SLuigi Rizzo *dst++ = *src++; 233f196ce38SLuigi Rizzo *dst++ = *src++; 234f196ce38SLuigi Rizzo *dst++ = *src++; 235f196ce38SLuigi Rizzo *dst++ = *src++; 236f196ce38SLuigi Rizzo *dst++ = *src++; 237f196ce38SLuigi Rizzo } 238f196ce38SLuigi Rizzo } 239f196ce38SLuigi Rizzo 240f196ce38SLuigi Rizzo /* 241f196ce38SLuigi Rizzo * locate a bridge among the existing ones. 242f196ce38SLuigi Rizzo * a ':' in the name terminates the bridge name. Otherwise, just NM_NAME. 243f196ce38SLuigi Rizzo * We assume that this is called with a name of at least NM_NAME chars. 244f196ce38SLuigi Rizzo */ 245f196ce38SLuigi Rizzo static struct nm_bridge * 246f196ce38SLuigi Rizzo nm_find_bridge(const char *name) 247f196ce38SLuigi Rizzo { 248f196ce38SLuigi Rizzo int i, l, namelen, e; 249f196ce38SLuigi Rizzo struct nm_bridge *b = NULL; 250f196ce38SLuigi Rizzo 251f196ce38SLuigi Rizzo namelen = strlen(NM_NAME); /* base length */ 252f196ce38SLuigi Rizzo l = strlen(name); /* actual length */ 253f196ce38SLuigi Rizzo for (i = namelen + 1; i < l; i++) { 254f196ce38SLuigi Rizzo if (name[i] == ':') { 255f196ce38SLuigi Rizzo namelen = i; 256f196ce38SLuigi Rizzo break; 257f196ce38SLuigi Rizzo } 258f196ce38SLuigi Rizzo } 259f196ce38SLuigi Rizzo if (namelen >= IFNAMSIZ) 260f196ce38SLuigi Rizzo namelen = IFNAMSIZ; 261f196ce38SLuigi Rizzo ND("--- prefix is '%.*s' ---", namelen, name); 262f196ce38SLuigi Rizzo 263f196ce38SLuigi Rizzo /* use the first entry for locking */ 264f196ce38SLuigi Rizzo BDG_LOCK(nm_bridges); // XXX do better 265f196ce38SLuigi Rizzo for (e = -1, i = 1; i < NM_BRIDGES; i++) { 266f196ce38SLuigi Rizzo b = nm_bridges + i; 267f196ce38SLuigi Rizzo if (b->namelen == 0) 268f196ce38SLuigi Rizzo e = i; /* record empty slot */ 269f196ce38SLuigi Rizzo else if (strncmp(name, b->basename, namelen) == 0) { 270f196ce38SLuigi Rizzo ND("found '%.*s' at %d", namelen, name, i); 271f196ce38SLuigi Rizzo break; 272f196ce38SLuigi Rizzo } 273f196ce38SLuigi Rizzo } 274f196ce38SLuigi Rizzo if (i == NM_BRIDGES) { /* all full */ 275f196ce38SLuigi Rizzo if (e == -1) { /* no empty slot */ 276f196ce38SLuigi Rizzo b = NULL; 277f196ce38SLuigi Rizzo } else { 278f196ce38SLuigi Rizzo b = nm_bridges + e; 279f196ce38SLuigi Rizzo strncpy(b->basename, name, namelen); 280f196ce38SLuigi Rizzo b->namelen = namelen; 281f196ce38SLuigi Rizzo } 282f196ce38SLuigi Rizzo } 283f196ce38SLuigi Rizzo BDG_UNLOCK(nm_bridges); 284f196ce38SLuigi Rizzo return b; 285f196ce38SLuigi Rizzo } 286f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */ 2875819da83SLuigi Rizzo 2883c0caf6cSLuigi Rizzo /*------------- memory allocator -----------------*/ 2893c0caf6cSLuigi Rizzo #ifdef NETMAP_MEM2 2903c0caf6cSLuigi Rizzo #include "netmap_mem2.c" 2913c0caf6cSLuigi Rizzo #else /* !NETMAP_MEM2 */ 2923c0caf6cSLuigi Rizzo #include "netmap_mem1.c" 2933c0caf6cSLuigi Rizzo #endif /* !NETMAP_MEM2 */ 2943c0caf6cSLuigi Rizzo /*------------ end of memory allocator ----------*/ 29568b8534bSLuigi Rizzo 29668b8534bSLuigi Rizzo /* Structure associated to each thread which registered an interface. */ 29768b8534bSLuigi Rizzo struct netmap_priv_d { 29868b8534bSLuigi Rizzo struct netmap_if *np_nifp; /* netmap interface descriptor. */ 29968b8534bSLuigi Rizzo 30068b8534bSLuigi Rizzo struct ifnet *np_ifp; /* device for which we hold a reference */ 30168b8534bSLuigi Rizzo int np_ringid; /* from the ioctl */ 30268b8534bSLuigi Rizzo u_int np_qfirst, np_qlast; /* range of rings to scan */ 30368b8534bSLuigi Rizzo uint16_t np_txpoll; 30468b8534bSLuigi Rizzo }; 30568b8534bSLuigi Rizzo 30668b8534bSLuigi Rizzo 30768b8534bSLuigi Rizzo /* 30868b8534bSLuigi Rizzo * File descriptor's private data destructor. 30968b8534bSLuigi Rizzo * 31068b8534bSLuigi Rizzo * Call nm_register(ifp,0) to stop netmap mode on the interface and 31168b8534bSLuigi Rizzo * revert to normal operation. We expect that np_ifp has not gone. 31268b8534bSLuigi Rizzo */ 31368b8534bSLuigi Rizzo static void 3145819da83SLuigi Rizzo netmap_dtor_locked(void *data) 31568b8534bSLuigi Rizzo { 31668b8534bSLuigi Rizzo struct netmap_priv_d *priv = data; 31768b8534bSLuigi Rizzo struct ifnet *ifp = priv->np_ifp; 31868b8534bSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 31968b8534bSLuigi Rizzo struct netmap_if *nifp = priv->np_nifp; 32068b8534bSLuigi Rizzo 32168b8534bSLuigi Rizzo na->refcount--; 32268b8534bSLuigi Rizzo if (na->refcount <= 0) { /* last instance */ 32364ae02c3SLuigi Rizzo u_int i, j, lim; 32468b8534bSLuigi Rizzo 32568b8534bSLuigi Rizzo D("deleting last netmap instance for %s", ifp->if_xname); 32668b8534bSLuigi Rizzo /* 32768b8534bSLuigi Rizzo * there is a race here with *_netmap_task() and 3281a26580eSLuigi Rizzo * netmap_poll(), which don't run under NETMAP_REG_LOCK. 32968b8534bSLuigi Rizzo * na->refcount == 0 && na->ifp->if_capenable & IFCAP_NETMAP 33068b8534bSLuigi Rizzo * (aka NETMAP_DELETING(na)) are a unique marker that the 33168b8534bSLuigi Rizzo * device is dying. 33268b8534bSLuigi Rizzo * Before destroying stuff we sleep a bit, and then complete 33368b8534bSLuigi Rizzo * the job. NIOCREG should realize the condition and 33468b8534bSLuigi Rizzo * loop until they can continue; the other routines 33568b8534bSLuigi Rizzo * should check the condition at entry and quit if 33668b8534bSLuigi Rizzo * they cannot run. 33768b8534bSLuigi Rizzo */ 3381a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0); 33968b8534bSLuigi Rizzo tsleep(na, 0, "NIOCUNREG", 4); 3401a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_REG_LOCK, 0); 34168b8534bSLuigi Rizzo na->nm_register(ifp, 0); /* off, clear IFCAP_NETMAP */ 34268b8534bSLuigi Rizzo /* Wake up any sleeping threads. netmap_poll will 34368b8534bSLuigi Rizzo * then return POLLERR 34468b8534bSLuigi Rizzo */ 345d76bf4ffSLuigi Rizzo for (i = 0; i < na->num_tx_rings + 1; i++) 34668b8534bSLuigi Rizzo selwakeuppri(&na->tx_rings[i].si, PI_NET); 347d76bf4ffSLuigi Rizzo for (i = 0; i < na->num_rx_rings + 1; i++) 34868b8534bSLuigi Rizzo selwakeuppri(&na->rx_rings[i].si, PI_NET); 34964ae02c3SLuigi Rizzo selwakeuppri(&na->tx_si, PI_NET); 35064ae02c3SLuigi Rizzo selwakeuppri(&na->rx_si, PI_NET); 35168b8534bSLuigi Rizzo /* release all buffers */ 35268b8534bSLuigi Rizzo NMA_LOCK(); 353d76bf4ffSLuigi Rizzo for (i = 0; i < na->num_tx_rings + 1; i++) { 35464ae02c3SLuigi Rizzo struct netmap_ring *ring = na->tx_rings[i].ring; 35568b8534bSLuigi Rizzo lim = na->tx_rings[i].nkr_num_slots; 35668b8534bSLuigi Rizzo for (j = 0; j < lim; j++) 357446ee301SLuigi Rizzo netmap_free_buf(nifp, ring->slot[j].buf_idx); 35864ae02c3SLuigi Rizzo } 359d76bf4ffSLuigi Rizzo for (i = 0; i < na->num_rx_rings + 1; i++) { 36064ae02c3SLuigi Rizzo struct netmap_ring *ring = na->rx_rings[i].ring; 36168b8534bSLuigi Rizzo lim = na->rx_rings[i].nkr_num_slots; 36268b8534bSLuigi Rizzo for (j = 0; j < lim; j++) 363446ee301SLuigi Rizzo netmap_free_buf(nifp, ring->slot[j].buf_idx); 36468b8534bSLuigi Rizzo } 36568b8534bSLuigi Rizzo NMA_UNLOCK(); 3666e10c8b8SLuigi Rizzo netmap_free_rings(na); 36768b8534bSLuigi Rizzo wakeup(na); 36868b8534bSLuigi Rizzo } 3696e10c8b8SLuigi Rizzo netmap_if_free(nifp); 3705819da83SLuigi Rizzo } 37168b8534bSLuigi Rizzo 372f196ce38SLuigi Rizzo static void 373f196ce38SLuigi Rizzo nm_if_rele(struct ifnet *ifp) 374f196ce38SLuigi Rizzo { 375f196ce38SLuigi Rizzo #ifndef NM_BRIDGE 376f196ce38SLuigi Rizzo if_rele(ifp); 377f196ce38SLuigi Rizzo #else /* NM_BRIDGE */ 378f196ce38SLuigi Rizzo int i, full; 379f196ce38SLuigi Rizzo struct nm_bridge *b; 380f196ce38SLuigi Rizzo 381f196ce38SLuigi Rizzo if (strncmp(ifp->if_xname, NM_NAME, sizeof(NM_NAME) - 1)) { 382f196ce38SLuigi Rizzo if_rele(ifp); 383f196ce38SLuigi Rizzo return; 384f196ce38SLuigi Rizzo } 385f196ce38SLuigi Rizzo if (!DROP_BDG_REF(ifp)) 386f196ce38SLuigi Rizzo return; 387f196ce38SLuigi Rizzo b = ifp->if_bridge; 388f196ce38SLuigi Rizzo BDG_LOCK(nm_bridges); 389f196ce38SLuigi Rizzo BDG_LOCK(b); 390f196ce38SLuigi Rizzo ND("want to disconnect %s from the bridge", ifp->if_xname); 391f196ce38SLuigi Rizzo full = 0; 392f196ce38SLuigi Rizzo for (i = 0; i < NM_BDG_MAXPORTS; i++) { 393f196ce38SLuigi Rizzo if (b->bdg_ports[i] == ifp) { 394f196ce38SLuigi Rizzo b->bdg_ports[i] = NULL; 395f196ce38SLuigi Rizzo bzero(ifp, sizeof(*ifp)); 396f196ce38SLuigi Rizzo free(ifp, M_DEVBUF); 397f196ce38SLuigi Rizzo break; 398f196ce38SLuigi Rizzo } 399f196ce38SLuigi Rizzo else if (b->bdg_ports[i] != NULL) 400f196ce38SLuigi Rizzo full = 1; 401f196ce38SLuigi Rizzo } 402f196ce38SLuigi Rizzo BDG_UNLOCK(b); 403f196ce38SLuigi Rizzo if (full == 0) { 404f196ce38SLuigi Rizzo ND("freeing bridge %d", b - nm_bridges); 405f196ce38SLuigi Rizzo b->namelen = 0; 406f196ce38SLuigi Rizzo } 407f196ce38SLuigi Rizzo BDG_UNLOCK(nm_bridges); 408f196ce38SLuigi Rizzo if (i == NM_BDG_MAXPORTS) 409f196ce38SLuigi Rizzo D("ouch, cannot find ifp to remove"); 410f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */ 411f196ce38SLuigi Rizzo } 4125819da83SLuigi Rizzo 4135819da83SLuigi Rizzo static void 4145819da83SLuigi Rizzo netmap_dtor(void *data) 4155819da83SLuigi Rizzo { 4165819da83SLuigi Rizzo struct netmap_priv_d *priv = data; 4175819da83SLuigi Rizzo struct ifnet *ifp = priv->np_ifp; 4185819da83SLuigi Rizzo struct netmap_adapter *na = NA(ifp); 4195819da83SLuigi Rizzo 4201a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_REG_LOCK, 0); 4215819da83SLuigi Rizzo netmap_dtor_locked(data); 4221a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0); 42368b8534bSLuigi Rizzo 424f196ce38SLuigi Rizzo nm_if_rele(ifp); 42568b8534bSLuigi Rizzo bzero(priv, sizeof(*priv)); /* XXX for safety */ 42668b8534bSLuigi Rizzo free(priv, M_DEVBUF); 42768b8534bSLuigi Rizzo } 42868b8534bSLuigi Rizzo 42968b8534bSLuigi Rizzo 43068b8534bSLuigi Rizzo /* 43168b8534bSLuigi Rizzo * mmap(2) support for the "netmap" device. 43268b8534bSLuigi Rizzo * 43368b8534bSLuigi Rizzo * Expose all the memory previously allocated by our custom memory 43468b8534bSLuigi Rizzo * allocator: this way the user has only to issue a single mmap(2), and 43568b8534bSLuigi Rizzo * can work on all the data structures flawlessly. 43668b8534bSLuigi Rizzo * 43768b8534bSLuigi Rizzo * Return 0 on success, -1 otherwise. 43868b8534bSLuigi Rizzo */ 439babc7c12SLuigi Rizzo 440f196ce38SLuigi Rizzo #ifdef __FreeBSD__ 44168b8534bSLuigi Rizzo static int 442babc7c12SLuigi Rizzo netmap_mmap(__unused struct cdev *dev, 44368b8534bSLuigi Rizzo #if __FreeBSD_version < 900000 444babc7c12SLuigi Rizzo vm_offset_t offset, vm_paddr_t *paddr, int nprot 44568b8534bSLuigi Rizzo #else 446babc7c12SLuigi Rizzo vm_ooffset_t offset, vm_paddr_t *paddr, int nprot, 447babc7c12SLuigi Rizzo __unused vm_memattr_t *memattr 44868b8534bSLuigi Rizzo #endif 449babc7c12SLuigi Rizzo ) 45068b8534bSLuigi Rizzo { 45168b8534bSLuigi Rizzo if (nprot & PROT_EXEC) 45268b8534bSLuigi Rizzo return (-1); // XXX -1 or EINVAL ? 453446ee301SLuigi Rizzo 45468b8534bSLuigi Rizzo ND("request for offset 0x%x", (uint32_t)offset); 455446ee301SLuigi Rizzo *paddr = netmap_ofstophys(offset); 45668b8534bSLuigi Rizzo 45768b8534bSLuigi Rizzo return (0); 45868b8534bSLuigi Rizzo } 459f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */ 46068b8534bSLuigi Rizzo 46168b8534bSLuigi Rizzo 46268b8534bSLuigi Rizzo /* 46302ad4083SLuigi Rizzo * Handlers for synchronization of the queues from/to the host. 46402ad4083SLuigi Rizzo * 46502ad4083SLuigi Rizzo * netmap_sync_to_host() passes packets up. We are called from a 46602ad4083SLuigi Rizzo * system call in user process context, and the only contention 46702ad4083SLuigi Rizzo * can be among multiple user threads erroneously calling 46802ad4083SLuigi Rizzo * this routine concurrently. In principle we should not even 46902ad4083SLuigi Rizzo * need to lock. 47068b8534bSLuigi Rizzo */ 47168b8534bSLuigi Rizzo static void 47268b8534bSLuigi Rizzo netmap_sync_to_host(struct netmap_adapter *na) 47368b8534bSLuigi Rizzo { 474d76bf4ffSLuigi Rizzo struct netmap_kring *kring = &na->tx_rings[na->num_tx_rings]; 47568b8534bSLuigi Rizzo struct netmap_ring *ring = kring->ring; 47668b8534bSLuigi Rizzo struct mbuf *head = NULL, *tail = NULL, *m; 47702ad4083SLuigi Rizzo u_int k, n, lim = kring->nkr_num_slots - 1; 47868b8534bSLuigi Rizzo 47902ad4083SLuigi Rizzo k = ring->cur; 48002ad4083SLuigi Rizzo if (k > lim) { 48102ad4083SLuigi Rizzo netmap_ring_reinit(kring); 48202ad4083SLuigi Rizzo return; 48302ad4083SLuigi Rizzo } 4841a26580eSLuigi Rizzo // na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0); 48568b8534bSLuigi Rizzo 48668b8534bSLuigi Rizzo /* Take packets from hwcur to cur and pass them up. 48768b8534bSLuigi Rizzo * In case of no buffers we give up. At the end of the loop, 48868b8534bSLuigi Rizzo * the queue is drained in all cases. 48968b8534bSLuigi Rizzo */ 49002ad4083SLuigi Rizzo for (n = kring->nr_hwcur; n != k;) { 49168b8534bSLuigi Rizzo struct netmap_slot *slot = &ring->slot[n]; 49268b8534bSLuigi Rizzo 49368b8534bSLuigi Rizzo n = (n == lim) ? 0 : n + 1; 49468b8534bSLuigi Rizzo if (slot->len < 14 || slot->len > NETMAP_BUF_SIZE) { 49568b8534bSLuigi Rizzo D("bad pkt at %d len %d", n, slot->len); 49668b8534bSLuigi Rizzo continue; 49768b8534bSLuigi Rizzo } 49868b8534bSLuigi Rizzo m = m_devget(NMB(slot), slot->len, 0, na->ifp, NULL); 49968b8534bSLuigi Rizzo 50068b8534bSLuigi Rizzo if (m == NULL) 50168b8534bSLuigi Rizzo break; 50268b8534bSLuigi Rizzo if (tail) 50368b8534bSLuigi Rizzo tail->m_nextpkt = m; 50468b8534bSLuigi Rizzo else 50568b8534bSLuigi Rizzo head = m; 50668b8534bSLuigi Rizzo tail = m; 50768b8534bSLuigi Rizzo m->m_nextpkt = NULL; 50868b8534bSLuigi Rizzo } 50902ad4083SLuigi Rizzo kring->nr_hwcur = k; 51068b8534bSLuigi Rizzo kring->nr_hwavail = ring->avail = lim; 5111a26580eSLuigi Rizzo // na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0); 51268b8534bSLuigi Rizzo 51368b8534bSLuigi Rizzo /* send packets up, outside the lock */ 51468b8534bSLuigi Rizzo while ((m = head) != NULL) { 51568b8534bSLuigi Rizzo head = head->m_nextpkt; 51668b8534bSLuigi Rizzo m->m_nextpkt = NULL; 51768b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_HOST) 5181a26580eSLuigi Rizzo D("sending up pkt %p size %d", m, MBUF_LEN(m)); 5191a26580eSLuigi Rizzo NM_SEND_UP(na->ifp, m); 52068b8534bSLuigi Rizzo } 52168b8534bSLuigi Rizzo } 52268b8534bSLuigi Rizzo 52368b8534bSLuigi Rizzo /* 52402ad4083SLuigi Rizzo * rxsync backend for packets coming from the host stack. 52502ad4083SLuigi Rizzo * They have been put in the queue by netmap_start() so we 52602ad4083SLuigi Rizzo * need to protect access to the kring using a lock. 52702ad4083SLuigi Rizzo * 52868b8534bSLuigi Rizzo * This routine also does the selrecord if called from the poll handler 52968b8534bSLuigi Rizzo * (we know because td != NULL). 530*01c7d25fSLuigi Rizzo * 531*01c7d25fSLuigi Rizzo * NOTE: on linux, selrecord() is defined as a macro and uses pwait 532*01c7d25fSLuigi Rizzo * as an additional hidden argument. 53368b8534bSLuigi Rizzo */ 53468b8534bSLuigi Rizzo static void 535*01c7d25fSLuigi Rizzo netmap_sync_from_host(struct netmap_adapter *na, struct thread *td, void *pwait) 53668b8534bSLuigi Rizzo { 537d76bf4ffSLuigi Rizzo struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings]; 53868b8534bSLuigi Rizzo struct netmap_ring *ring = kring->ring; 53964ae02c3SLuigi Rizzo u_int j, n, lim = kring->nkr_num_slots; 54064ae02c3SLuigi Rizzo u_int k = ring->cur, resvd = ring->reserved; 54168b8534bSLuigi Rizzo 542*01c7d25fSLuigi Rizzo (void)pwait; /* disable unused warnings */ 5431a26580eSLuigi Rizzo na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0); 54464ae02c3SLuigi Rizzo if (k >= lim) { 54564ae02c3SLuigi Rizzo netmap_ring_reinit(kring); 54664ae02c3SLuigi Rizzo return; 54764ae02c3SLuigi Rizzo } 54864ae02c3SLuigi Rizzo /* new packets are already set in nr_hwavail */ 54964ae02c3SLuigi Rizzo /* skip past packets that userspace has released */ 55064ae02c3SLuigi Rizzo j = kring->nr_hwcur; 55164ae02c3SLuigi Rizzo if (resvd > 0) { 55264ae02c3SLuigi Rizzo if (resvd + ring->avail >= lim + 1) { 55364ae02c3SLuigi Rizzo D("XXX invalid reserve/avail %d %d", resvd, ring->avail); 55464ae02c3SLuigi Rizzo ring->reserved = resvd = 0; // XXX panic... 55564ae02c3SLuigi Rizzo } 55664ae02c3SLuigi Rizzo k = (k >= resvd) ? k - resvd : k + lim - resvd; 55764ae02c3SLuigi Rizzo } 55864ae02c3SLuigi Rizzo if (j != k) { 55964ae02c3SLuigi Rizzo n = k >= j ? k - j : k + lim - j; 56064ae02c3SLuigi Rizzo kring->nr_hwavail -= n; 56102ad4083SLuigi Rizzo kring->nr_hwcur = k; 56264ae02c3SLuigi Rizzo } 56364ae02c3SLuigi Rizzo k = ring->avail = kring->nr_hwavail - resvd; 56402ad4083SLuigi Rizzo if (k == 0 && td) 56568b8534bSLuigi Rizzo selrecord(td, &kring->si); 56602ad4083SLuigi Rizzo if (k && (netmap_verbose & NM_VERB_HOST)) 56702ad4083SLuigi Rizzo D("%d pkts from stack", k); 5681a26580eSLuigi Rizzo na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0); 56968b8534bSLuigi Rizzo } 57068b8534bSLuigi Rizzo 57168b8534bSLuigi Rizzo 57268b8534bSLuigi Rizzo /* 57368b8534bSLuigi Rizzo * get a refcounted reference to an interface. 57468b8534bSLuigi Rizzo * Return ENXIO if the interface does not exist, EINVAL if netmap 57568b8534bSLuigi Rizzo * is not supported by the interface. 57668b8534bSLuigi Rizzo * If successful, hold a reference. 57768b8534bSLuigi Rizzo */ 57868b8534bSLuigi Rizzo static int 57968b8534bSLuigi Rizzo get_ifp(const char *name, struct ifnet **ifp) 58068b8534bSLuigi Rizzo { 581f196ce38SLuigi Rizzo #ifdef NM_BRIDGE 582f196ce38SLuigi Rizzo struct ifnet *iter = NULL; 583f196ce38SLuigi Rizzo 584f196ce38SLuigi Rizzo do { 585f196ce38SLuigi Rizzo struct nm_bridge *b; 586f196ce38SLuigi Rizzo int i, l, cand = -1; 587f196ce38SLuigi Rizzo 588f196ce38SLuigi Rizzo if (strncmp(name, NM_NAME, sizeof(NM_NAME) - 1)) 589f196ce38SLuigi Rizzo break; 590f196ce38SLuigi Rizzo b = nm_find_bridge(name); 591f196ce38SLuigi Rizzo if (b == NULL) { 592f196ce38SLuigi Rizzo D("no bridges available for '%s'", name); 593f196ce38SLuigi Rizzo return (ENXIO); 594f196ce38SLuigi Rizzo } 595f196ce38SLuigi Rizzo /* XXX locking */ 596f196ce38SLuigi Rizzo BDG_LOCK(b); 597f196ce38SLuigi Rizzo /* lookup in the local list of ports */ 598f196ce38SLuigi Rizzo for (i = 0; i < NM_BDG_MAXPORTS; i++) { 599f196ce38SLuigi Rizzo iter = b->bdg_ports[i]; 600f196ce38SLuigi Rizzo if (iter == NULL) { 601f196ce38SLuigi Rizzo if (cand == -1) 602f196ce38SLuigi Rizzo cand = i; /* potential insert point */ 603f196ce38SLuigi Rizzo continue; 604f196ce38SLuigi Rizzo } 605f196ce38SLuigi Rizzo if (!strcmp(iter->if_xname, name)) { 606f196ce38SLuigi Rizzo ADD_BDG_REF(iter); 607f196ce38SLuigi Rizzo ND("found existing interface"); 608f196ce38SLuigi Rizzo BDG_UNLOCK(b); 609f196ce38SLuigi Rizzo break; 610f196ce38SLuigi Rizzo } 611f196ce38SLuigi Rizzo } 612f196ce38SLuigi Rizzo if (i < NM_BDG_MAXPORTS) /* already unlocked */ 613f196ce38SLuigi Rizzo break; 614f196ce38SLuigi Rizzo if (cand == -1) { 615f196ce38SLuigi Rizzo D("bridge full, cannot create new port"); 616f196ce38SLuigi Rizzo no_port: 617f196ce38SLuigi Rizzo BDG_UNLOCK(b); 618f196ce38SLuigi Rizzo *ifp = NULL; 619f196ce38SLuigi Rizzo return EINVAL; 620f196ce38SLuigi Rizzo } 621f196ce38SLuigi Rizzo ND("create new bridge port %s", name); 622f196ce38SLuigi Rizzo /* space for forwarding list after the ifnet */ 623f196ce38SLuigi Rizzo l = sizeof(*iter) + 624f196ce38SLuigi Rizzo sizeof(struct nm_bdg_fwd)*NM_BDG_BATCH ; 625f196ce38SLuigi Rizzo iter = malloc(l, M_DEVBUF, M_NOWAIT | M_ZERO); 626f196ce38SLuigi Rizzo if (!iter) 627f196ce38SLuigi Rizzo goto no_port; 628f196ce38SLuigi Rizzo strcpy(iter->if_xname, name); 629f196ce38SLuigi Rizzo bdg_netmap_attach(iter); 630f196ce38SLuigi Rizzo b->bdg_ports[cand] = iter; 631f196ce38SLuigi Rizzo iter->if_bridge = b; 632f196ce38SLuigi Rizzo ADD_BDG_REF(iter); 633f196ce38SLuigi Rizzo BDG_UNLOCK(b); 634f196ce38SLuigi Rizzo ND("attaching virtual bridge %p", b); 635f196ce38SLuigi Rizzo } while (0); 636f196ce38SLuigi Rizzo *ifp = iter; 637f196ce38SLuigi Rizzo if (! *ifp) 638f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */ 63968b8534bSLuigi Rizzo *ifp = ifunit_ref(name); 64068b8534bSLuigi Rizzo if (*ifp == NULL) 64168b8534bSLuigi Rizzo return (ENXIO); 64268b8534bSLuigi Rizzo /* can do this if the capability exists and if_pspare[0] 64368b8534bSLuigi Rizzo * points to the netmap descriptor. 64468b8534bSLuigi Rizzo */ 64568b8534bSLuigi Rizzo if ((*ifp)->if_capabilities & IFCAP_NETMAP && NA(*ifp)) 64668b8534bSLuigi Rizzo return 0; /* valid pointer, we hold the refcount */ 647f196ce38SLuigi Rizzo nm_if_rele(*ifp); 64868b8534bSLuigi Rizzo return EINVAL; // not NETMAP capable 64968b8534bSLuigi Rizzo } 65068b8534bSLuigi Rizzo 65168b8534bSLuigi Rizzo 65268b8534bSLuigi Rizzo /* 65368b8534bSLuigi Rizzo * Error routine called when txsync/rxsync detects an error. 65468b8534bSLuigi Rizzo * Can't do much more than resetting cur = hwcur, avail = hwavail. 65568b8534bSLuigi Rizzo * Return 1 on reinit. 656506cc70cSLuigi Rizzo * 657506cc70cSLuigi Rizzo * This routine is only called by the upper half of the kernel. 658506cc70cSLuigi Rizzo * It only reads hwcur (which is changed only by the upper half, too) 659506cc70cSLuigi Rizzo * and hwavail (which may be changed by the lower half, but only on 660506cc70cSLuigi Rizzo * a tx ring and only to increase it, so any error will be recovered 661506cc70cSLuigi Rizzo * on the next call). For the above, we don't strictly need to call 662506cc70cSLuigi Rizzo * it under lock. 66368b8534bSLuigi Rizzo */ 66468b8534bSLuigi Rizzo int 66568b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring) 66668b8534bSLuigi Rizzo { 66768b8534bSLuigi Rizzo struct netmap_ring *ring = kring->ring; 66868b8534bSLuigi Rizzo u_int i, lim = kring->nkr_num_slots - 1; 66968b8534bSLuigi Rizzo int errors = 0; 67068b8534bSLuigi Rizzo 67168b8534bSLuigi Rizzo D("called for %s", kring->na->ifp->if_xname); 67268b8534bSLuigi Rizzo if (ring->cur > lim) 67368b8534bSLuigi Rizzo errors++; 67468b8534bSLuigi Rizzo for (i = 0; i <= lim; i++) { 67568b8534bSLuigi Rizzo u_int idx = ring->slot[i].buf_idx; 67668b8534bSLuigi Rizzo u_int len = ring->slot[i].len; 67768b8534bSLuigi Rizzo if (idx < 2 || idx >= netmap_total_buffers) { 67868b8534bSLuigi Rizzo if (!errors++) 67968b8534bSLuigi Rizzo D("bad buffer at slot %d idx %d len %d ", i, idx, len); 68068b8534bSLuigi Rizzo ring->slot[i].buf_idx = 0; 68168b8534bSLuigi Rizzo ring->slot[i].len = 0; 68268b8534bSLuigi Rizzo } else if (len > NETMAP_BUF_SIZE) { 68368b8534bSLuigi Rizzo ring->slot[i].len = 0; 68468b8534bSLuigi Rizzo if (!errors++) 68568b8534bSLuigi Rizzo D("bad len %d at slot %d idx %d", 68668b8534bSLuigi Rizzo len, i, idx); 68768b8534bSLuigi Rizzo } 68868b8534bSLuigi Rizzo } 68968b8534bSLuigi Rizzo if (errors) { 69068b8534bSLuigi Rizzo int pos = kring - kring->na->tx_rings; 691d76bf4ffSLuigi Rizzo int n = kring->na->num_tx_rings + 1; 69268b8534bSLuigi Rizzo 69368b8534bSLuigi Rizzo D("total %d errors", errors); 69468b8534bSLuigi Rizzo errors++; 69568b8534bSLuigi Rizzo D("%s %s[%d] reinit, cur %d -> %d avail %d -> %d", 69668b8534bSLuigi Rizzo kring->na->ifp->if_xname, 69768b8534bSLuigi Rizzo pos < n ? "TX" : "RX", pos < n ? pos : pos - n, 69868b8534bSLuigi Rizzo ring->cur, kring->nr_hwcur, 69968b8534bSLuigi Rizzo ring->avail, kring->nr_hwavail); 70068b8534bSLuigi Rizzo ring->cur = kring->nr_hwcur; 70168b8534bSLuigi Rizzo ring->avail = kring->nr_hwavail; 70268b8534bSLuigi Rizzo } 70368b8534bSLuigi Rizzo return (errors ? 1 : 0); 70468b8534bSLuigi Rizzo } 70568b8534bSLuigi Rizzo 70668b8534bSLuigi Rizzo 70768b8534bSLuigi Rizzo /* 70868b8534bSLuigi Rizzo * Set the ring ID. For devices with a single queue, a request 70968b8534bSLuigi Rizzo * for all rings is the same as a single ring. 71068b8534bSLuigi Rizzo */ 71168b8534bSLuigi Rizzo static int 71268b8534bSLuigi Rizzo netmap_set_ringid(struct netmap_priv_d *priv, u_int ringid) 71368b8534bSLuigi Rizzo { 71468b8534bSLuigi Rizzo struct ifnet *ifp = priv->np_ifp; 71568b8534bSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 71668b8534bSLuigi Rizzo u_int i = ringid & NETMAP_RING_MASK; 71764ae02c3SLuigi Rizzo /* initially (np_qfirst == np_qlast) we don't want to lock */ 71868b8534bSLuigi Rizzo int need_lock = (priv->np_qfirst != priv->np_qlast); 719d76bf4ffSLuigi Rizzo int lim = na->num_rx_rings; 72068b8534bSLuigi Rizzo 721d76bf4ffSLuigi Rizzo if (na->num_tx_rings > lim) 722d76bf4ffSLuigi Rizzo lim = na->num_tx_rings; 72364ae02c3SLuigi Rizzo if ( (ringid & NETMAP_HW_RING) && i >= lim) { 72468b8534bSLuigi Rizzo D("invalid ring id %d", i); 72568b8534bSLuigi Rizzo return (EINVAL); 72668b8534bSLuigi Rizzo } 72768b8534bSLuigi Rizzo if (need_lock) 7281a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_LOCK, 0); 72968b8534bSLuigi Rizzo priv->np_ringid = ringid; 73068b8534bSLuigi Rizzo if (ringid & NETMAP_SW_RING) { 73164ae02c3SLuigi Rizzo priv->np_qfirst = NETMAP_SW_RING; 73264ae02c3SLuigi Rizzo priv->np_qlast = 0; 73368b8534bSLuigi Rizzo } else if (ringid & NETMAP_HW_RING) { 73468b8534bSLuigi Rizzo priv->np_qfirst = i; 73568b8534bSLuigi Rizzo priv->np_qlast = i + 1; 73668b8534bSLuigi Rizzo } else { 73768b8534bSLuigi Rizzo priv->np_qfirst = 0; 73864ae02c3SLuigi Rizzo priv->np_qlast = NETMAP_HW_RING ; 73968b8534bSLuigi Rizzo } 74068b8534bSLuigi Rizzo priv->np_txpoll = (ringid & NETMAP_NO_TX_POLL) ? 0 : 1; 74168b8534bSLuigi Rizzo if (need_lock) 7421a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0); 74368b8534bSLuigi Rizzo if (ringid & NETMAP_SW_RING) 74468b8534bSLuigi Rizzo D("ringid %s set to SW RING", ifp->if_xname); 74568b8534bSLuigi Rizzo else if (ringid & NETMAP_HW_RING) 74668b8534bSLuigi Rizzo D("ringid %s set to HW RING %d", ifp->if_xname, 74768b8534bSLuigi Rizzo priv->np_qfirst); 74868b8534bSLuigi Rizzo else 74964ae02c3SLuigi Rizzo D("ringid %s set to all %d HW RINGS", ifp->if_xname, lim); 75068b8534bSLuigi Rizzo return 0; 75168b8534bSLuigi Rizzo } 75268b8534bSLuigi Rizzo 75368b8534bSLuigi Rizzo /* 75468b8534bSLuigi Rizzo * ioctl(2) support for the "netmap" device. 75568b8534bSLuigi Rizzo * 75668b8534bSLuigi Rizzo * Following a list of accepted commands: 75768b8534bSLuigi Rizzo * - NIOCGINFO 75868b8534bSLuigi Rizzo * - SIOCGIFADDR just for convenience 75968b8534bSLuigi Rizzo * - NIOCREGIF 76068b8534bSLuigi Rizzo * - NIOCUNREGIF 76168b8534bSLuigi Rizzo * - NIOCTXSYNC 76268b8534bSLuigi Rizzo * - NIOCRXSYNC 76368b8534bSLuigi Rizzo * 76468b8534bSLuigi Rizzo * Return 0 on success, errno otherwise. 76568b8534bSLuigi Rizzo */ 76668b8534bSLuigi Rizzo static int 76768b8534bSLuigi Rizzo netmap_ioctl(__unused struct cdev *dev, u_long cmd, caddr_t data, 768506cc70cSLuigi Rizzo __unused int fflag, struct thread *td) 76968b8534bSLuigi Rizzo { 77068b8534bSLuigi Rizzo struct netmap_priv_d *priv = NULL; 77168b8534bSLuigi Rizzo struct ifnet *ifp; 77268b8534bSLuigi Rizzo struct nmreq *nmr = (struct nmreq *) data; 77368b8534bSLuigi Rizzo struct netmap_adapter *na; 77468b8534bSLuigi Rizzo int error; 77564ae02c3SLuigi Rizzo u_int i, lim; 77668b8534bSLuigi Rizzo struct netmap_if *nifp; 77768b8534bSLuigi Rizzo 778f196ce38SLuigi Rizzo #ifdef linux 779f196ce38SLuigi Rizzo #define devfs_get_cdevpriv(pp) \ 780f196ce38SLuigi Rizzo ({ *(struct netmap_priv_d **)pp = ((struct file *)td)->private_data; \ 781f196ce38SLuigi Rizzo (*pp ? 0 : ENOENT); }) 782f196ce38SLuigi Rizzo 783f196ce38SLuigi Rizzo /* devfs_set_cdevpriv cannot fail on linux */ 784f196ce38SLuigi Rizzo #define devfs_set_cdevpriv(p, fn) \ 785f196ce38SLuigi Rizzo ({ ((struct file *)td)->private_data = p; (p ? 0 : EINVAL); }) 786f196ce38SLuigi Rizzo 787f196ce38SLuigi Rizzo 788f196ce38SLuigi Rizzo #define devfs_clear_cdevpriv() do { \ 789f196ce38SLuigi Rizzo netmap_dtor(priv); ((struct file *)td)->private_data = 0; \ 790f196ce38SLuigi Rizzo } while (0) 791f196ce38SLuigi Rizzo #endif /* linux */ 792f196ce38SLuigi Rizzo 793506cc70cSLuigi Rizzo CURVNET_SET(TD_TO_VNET(td)); 794506cc70cSLuigi Rizzo 79568b8534bSLuigi Rizzo error = devfs_get_cdevpriv((void **)&priv); 796506cc70cSLuigi Rizzo if (error != ENOENT && error != 0) { 797506cc70cSLuigi Rizzo CURVNET_RESTORE(); 79868b8534bSLuigi Rizzo return (error); 799506cc70cSLuigi Rizzo } 80068b8534bSLuigi Rizzo 80168b8534bSLuigi Rizzo error = 0; /* Could be ENOENT */ 802f196ce38SLuigi Rizzo nmr->nr_name[sizeof(nmr->nr_name) - 1] = '\0'; /* truncate name */ 80368b8534bSLuigi Rizzo switch (cmd) { 80468b8534bSLuigi Rizzo case NIOCGINFO: /* return capabilities etc */ 80568b8534bSLuigi Rizzo /* memsize is always valid */ 80664ae02c3SLuigi Rizzo nmr->nr_memsize = nm_mem->nm_totalsize; 80768b8534bSLuigi Rizzo nmr->nr_offset = 0; 80864ae02c3SLuigi Rizzo nmr->nr_rx_rings = nmr->nr_tx_rings = 0; 80964ae02c3SLuigi Rizzo nmr->nr_rx_slots = nmr->nr_tx_slots = 0; 81064ae02c3SLuigi Rizzo if (nmr->nr_version != NETMAP_API) { 81164ae02c3SLuigi Rizzo D("API mismatch got %d have %d", 81264ae02c3SLuigi Rizzo nmr->nr_version, NETMAP_API); 81364ae02c3SLuigi Rizzo nmr->nr_version = NETMAP_API; 81464ae02c3SLuigi Rizzo error = EINVAL; 81564ae02c3SLuigi Rizzo break; 81664ae02c3SLuigi Rizzo } 81768b8534bSLuigi Rizzo if (nmr->nr_name[0] == '\0') /* just get memory info */ 81868b8534bSLuigi Rizzo break; 81968b8534bSLuigi Rizzo error = get_ifp(nmr->nr_name, &ifp); /* get a refcount */ 82068b8534bSLuigi Rizzo if (error) 82168b8534bSLuigi Rizzo break; 82268b8534bSLuigi Rizzo na = NA(ifp); /* retrieve netmap_adapter */ 823d76bf4ffSLuigi Rizzo nmr->nr_rx_rings = na->num_rx_rings; 824d76bf4ffSLuigi Rizzo nmr->nr_tx_rings = na->num_tx_rings; 82564ae02c3SLuigi Rizzo nmr->nr_rx_slots = na->num_rx_desc; 82664ae02c3SLuigi Rizzo nmr->nr_tx_slots = na->num_tx_desc; 827f196ce38SLuigi Rizzo nm_if_rele(ifp); /* return the refcount */ 82868b8534bSLuigi Rizzo break; 82968b8534bSLuigi Rizzo 83068b8534bSLuigi Rizzo case NIOCREGIF: 83164ae02c3SLuigi Rizzo if (nmr->nr_version != NETMAP_API) { 83264ae02c3SLuigi Rizzo nmr->nr_version = NETMAP_API; 83364ae02c3SLuigi Rizzo error = EINVAL; 83464ae02c3SLuigi Rizzo break; 83564ae02c3SLuigi Rizzo } 836506cc70cSLuigi Rizzo if (priv != NULL) { /* thread already registered */ 837506cc70cSLuigi Rizzo error = netmap_set_ringid(priv, nmr->nr_ringid); 838506cc70cSLuigi Rizzo break; 839506cc70cSLuigi Rizzo } 84068b8534bSLuigi Rizzo /* find the interface and a reference */ 84168b8534bSLuigi Rizzo error = get_ifp(nmr->nr_name, &ifp); /* keep reference */ 84268b8534bSLuigi Rizzo if (error) 84368b8534bSLuigi Rizzo break; 84468b8534bSLuigi Rizzo na = NA(ifp); /* retrieve netmap adapter */ 84568b8534bSLuigi Rizzo /* 84668b8534bSLuigi Rizzo * Allocate the private per-thread structure. 84768b8534bSLuigi Rizzo * XXX perhaps we can use a blocking malloc ? 84868b8534bSLuigi Rizzo */ 84968b8534bSLuigi Rizzo priv = malloc(sizeof(struct netmap_priv_d), M_DEVBUF, 85068b8534bSLuigi Rizzo M_NOWAIT | M_ZERO); 85168b8534bSLuigi Rizzo if (priv == NULL) { 85268b8534bSLuigi Rizzo error = ENOMEM; 853f196ce38SLuigi Rizzo nm_if_rele(ifp); /* return the refcount */ 85468b8534bSLuigi Rizzo break; 85568b8534bSLuigi Rizzo } 85668b8534bSLuigi Rizzo 85768b8534bSLuigi Rizzo for (i = 10; i > 0; i--) { 8581a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_REG_LOCK, 0); 85968b8534bSLuigi Rizzo if (!NETMAP_DELETING(na)) 86068b8534bSLuigi Rizzo break; 8611a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0); 86268b8534bSLuigi Rizzo tsleep(na, 0, "NIOCREGIF", hz/10); 86368b8534bSLuigi Rizzo } 86468b8534bSLuigi Rizzo if (i == 0) { 86568b8534bSLuigi Rizzo D("too many NIOCREGIF attempts, give up"); 86668b8534bSLuigi Rizzo error = EINVAL; 86768b8534bSLuigi Rizzo free(priv, M_DEVBUF); 868f196ce38SLuigi Rizzo nm_if_rele(ifp); /* return the refcount */ 86968b8534bSLuigi Rizzo break; 87068b8534bSLuigi Rizzo } 87168b8534bSLuigi Rizzo 87268b8534bSLuigi Rizzo priv->np_ifp = ifp; /* store the reference */ 87368b8534bSLuigi Rizzo error = netmap_set_ringid(priv, nmr->nr_ringid); 87468b8534bSLuigi Rizzo if (error) 87568b8534bSLuigi Rizzo goto error; 87668b8534bSLuigi Rizzo priv->np_nifp = nifp = netmap_if_new(nmr->nr_name, na); 87768b8534bSLuigi Rizzo if (nifp == NULL) { /* allocation failed */ 87868b8534bSLuigi Rizzo error = ENOMEM; 87968b8534bSLuigi Rizzo } else if (ifp->if_capenable & IFCAP_NETMAP) { 88068b8534bSLuigi Rizzo /* was already set */ 88168b8534bSLuigi Rizzo } else { 88268b8534bSLuigi Rizzo /* Otherwise set the card in netmap mode 88368b8534bSLuigi Rizzo * and make it use the shared buffers. 88468b8534bSLuigi Rizzo */ 885f196ce38SLuigi Rizzo for (i = 0 ; i < na->num_tx_rings + 1; i++) 886f196ce38SLuigi Rizzo mtx_init(&na->tx_rings[i].q_lock, "nm_txq_lock", MTX_NETWORK_LOCK, MTX_DEF); 887f196ce38SLuigi Rizzo for (i = 0 ; i < na->num_rx_rings + 1; i++) { 888f196ce38SLuigi Rizzo mtx_init(&na->rx_rings[i].q_lock, "nm_rxq_lock", MTX_NETWORK_LOCK, MTX_DEF); 889f196ce38SLuigi Rizzo } 89068b8534bSLuigi Rizzo error = na->nm_register(ifp, 1); /* mode on */ 8915819da83SLuigi Rizzo if (error) 8925819da83SLuigi Rizzo netmap_dtor_locked(priv); 89368b8534bSLuigi Rizzo } 89468b8534bSLuigi Rizzo 89568b8534bSLuigi Rizzo if (error) { /* reg. failed, release priv and ref */ 89668b8534bSLuigi Rizzo error: 8971a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0); 898f196ce38SLuigi Rizzo nm_if_rele(ifp); /* return the refcount */ 8995819da83SLuigi Rizzo bzero(priv, sizeof(*priv)); 9005819da83SLuigi Rizzo free(priv, M_DEVBUF); 90168b8534bSLuigi Rizzo break; 90268b8534bSLuigi Rizzo } 90368b8534bSLuigi Rizzo 9041a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0); 90568b8534bSLuigi Rizzo error = devfs_set_cdevpriv(priv, netmap_dtor); 90668b8534bSLuigi Rizzo 90768b8534bSLuigi Rizzo if (error != 0) { 90868b8534bSLuigi Rizzo /* could not assign the private storage for the 90968b8534bSLuigi Rizzo * thread, call the destructor explicitly. 91068b8534bSLuigi Rizzo */ 91168b8534bSLuigi Rizzo netmap_dtor(priv); 91268b8534bSLuigi Rizzo break; 91368b8534bSLuigi Rizzo } 91468b8534bSLuigi Rizzo 91568b8534bSLuigi Rizzo /* return the offset of the netmap_if object */ 916d76bf4ffSLuigi Rizzo nmr->nr_rx_rings = na->num_rx_rings; 917d76bf4ffSLuigi Rizzo nmr->nr_tx_rings = na->num_tx_rings; 91864ae02c3SLuigi Rizzo nmr->nr_rx_slots = na->num_rx_desc; 91964ae02c3SLuigi Rizzo nmr->nr_tx_slots = na->num_tx_desc; 92064ae02c3SLuigi Rizzo nmr->nr_memsize = nm_mem->nm_totalsize; 921446ee301SLuigi Rizzo nmr->nr_offset = netmap_if_offset(nifp); 92268b8534bSLuigi Rizzo break; 92368b8534bSLuigi Rizzo 92468b8534bSLuigi Rizzo case NIOCUNREGIF: 925506cc70cSLuigi Rizzo if (priv == NULL) { 926506cc70cSLuigi Rizzo error = ENXIO; 927506cc70cSLuigi Rizzo break; 928506cc70cSLuigi Rizzo } 92968b8534bSLuigi Rizzo 93068b8534bSLuigi Rizzo /* the interface is unregistered inside the 93168b8534bSLuigi Rizzo destructor of the private data. */ 93268b8534bSLuigi Rizzo devfs_clear_cdevpriv(); 93368b8534bSLuigi Rizzo break; 93468b8534bSLuigi Rizzo 93568b8534bSLuigi Rizzo case NIOCTXSYNC: 93668b8534bSLuigi Rizzo case NIOCRXSYNC: 937506cc70cSLuigi Rizzo if (priv == NULL) { 938506cc70cSLuigi Rizzo error = ENXIO; 939506cc70cSLuigi Rizzo break; 940506cc70cSLuigi Rizzo } 94168b8534bSLuigi Rizzo ifp = priv->np_ifp; /* we have a reference */ 94268b8534bSLuigi Rizzo na = NA(ifp); /* retrieve netmap adapter */ 94364ae02c3SLuigi Rizzo if (priv->np_qfirst == NETMAP_SW_RING) { /* host rings */ 94468b8534bSLuigi Rizzo if (cmd == NIOCTXSYNC) 94568b8534bSLuigi Rizzo netmap_sync_to_host(na); 94668b8534bSLuigi Rizzo else 947*01c7d25fSLuigi Rizzo netmap_sync_from_host(na, NULL, NULL); 948506cc70cSLuigi Rizzo break; 94968b8534bSLuigi Rizzo } 95064ae02c3SLuigi Rizzo /* find the last ring to scan */ 95164ae02c3SLuigi Rizzo lim = priv->np_qlast; 95264ae02c3SLuigi Rizzo if (lim == NETMAP_HW_RING) 9533c0caf6cSLuigi Rizzo lim = (cmd == NIOCTXSYNC) ? 954d76bf4ffSLuigi Rizzo na->num_tx_rings : na->num_rx_rings; 95568b8534bSLuigi Rizzo 95664ae02c3SLuigi Rizzo for (i = priv->np_qfirst; i < lim; i++) { 95768b8534bSLuigi Rizzo if (cmd == NIOCTXSYNC) { 95868b8534bSLuigi Rizzo struct netmap_kring *kring = &na->tx_rings[i]; 95968b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_TXSYNC) 9603c0caf6cSLuigi Rizzo D("pre txsync ring %d cur %d hwcur %d", 96168b8534bSLuigi Rizzo i, kring->ring->cur, 96268b8534bSLuigi Rizzo kring->nr_hwcur); 9631a26580eSLuigi Rizzo na->nm_txsync(ifp, i, 1 /* do lock */); 96468b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_TXSYNC) 9653c0caf6cSLuigi Rizzo D("post txsync ring %d cur %d hwcur %d", 96668b8534bSLuigi Rizzo i, kring->ring->cur, 96768b8534bSLuigi Rizzo kring->nr_hwcur); 96868b8534bSLuigi Rizzo } else { 9691a26580eSLuigi Rizzo na->nm_rxsync(ifp, i, 1 /* do lock */); 97068b8534bSLuigi Rizzo microtime(&na->rx_rings[i].ring->ts); 97168b8534bSLuigi Rizzo } 97268b8534bSLuigi Rizzo } 97368b8534bSLuigi Rizzo 97468b8534bSLuigi Rizzo break; 97568b8534bSLuigi Rizzo 976f196ce38SLuigi Rizzo #ifdef __FreeBSD__ 97768b8534bSLuigi Rizzo case BIOCIMMEDIATE: 97868b8534bSLuigi Rizzo case BIOCGHDRCMPLT: 97968b8534bSLuigi Rizzo case BIOCSHDRCMPLT: 98068b8534bSLuigi Rizzo case BIOCSSEESENT: 98168b8534bSLuigi Rizzo D("ignore BIOCIMMEDIATE/BIOCSHDRCMPLT/BIOCSHDRCMPLT/BIOCSSEESENT"); 98268b8534bSLuigi Rizzo break; 98368b8534bSLuigi Rizzo 984babc7c12SLuigi Rizzo default: /* allow device-specific ioctls */ 98568b8534bSLuigi Rizzo { 98668b8534bSLuigi Rizzo struct socket so; 98768b8534bSLuigi Rizzo bzero(&so, sizeof(so)); 98868b8534bSLuigi Rizzo error = get_ifp(nmr->nr_name, &ifp); /* keep reference */ 98968b8534bSLuigi Rizzo if (error) 99068b8534bSLuigi Rizzo break; 99168b8534bSLuigi Rizzo so.so_vnet = ifp->if_vnet; 99268b8534bSLuigi Rizzo // so->so_proto not null. 99368b8534bSLuigi Rizzo error = ifioctl(&so, cmd, data, td); 994f196ce38SLuigi Rizzo nm_if_rele(ifp); 995babc7c12SLuigi Rizzo break; 99668b8534bSLuigi Rizzo } 997f196ce38SLuigi Rizzo 998f196ce38SLuigi Rizzo #else /* linux */ 999f196ce38SLuigi Rizzo default: 1000f196ce38SLuigi Rizzo error = EOPNOTSUPP; 1001f196ce38SLuigi Rizzo #endif /* linux */ 100268b8534bSLuigi Rizzo } 100368b8534bSLuigi Rizzo 1004506cc70cSLuigi Rizzo CURVNET_RESTORE(); 100568b8534bSLuigi Rizzo return (error); 100668b8534bSLuigi Rizzo } 100768b8534bSLuigi Rizzo 100868b8534bSLuigi Rizzo 100968b8534bSLuigi Rizzo /* 101068b8534bSLuigi Rizzo * select(2) and poll(2) handlers for the "netmap" device. 101168b8534bSLuigi Rizzo * 101268b8534bSLuigi Rizzo * Can be called for one or more queues. 101368b8534bSLuigi Rizzo * Return true the event mask corresponding to ready events. 101468b8534bSLuigi Rizzo * If there are no ready events, do a selrecord on either individual 101568b8534bSLuigi Rizzo * selfd or on the global one. 101668b8534bSLuigi Rizzo * Device-dependent parts (locking and sync of tx/rx rings) 101768b8534bSLuigi Rizzo * are done through callbacks. 1018f196ce38SLuigi Rizzo * 1019*01c7d25fSLuigi Rizzo * On linux, arguments are really pwait, the poll table, and 'td' is struct file * 1020*01c7d25fSLuigi Rizzo * The first one is remapped to pwait as selrecord() uses the name as an 1021*01c7d25fSLuigi Rizzo * hidden argument. 102268b8534bSLuigi Rizzo */ 102368b8534bSLuigi Rizzo static int 1024*01c7d25fSLuigi Rizzo netmap_poll(struct cdev *dev, int events, struct thread *td) 102568b8534bSLuigi Rizzo { 102668b8534bSLuigi Rizzo struct netmap_priv_d *priv = NULL; 102768b8534bSLuigi Rizzo struct netmap_adapter *na; 102868b8534bSLuigi Rizzo struct ifnet *ifp; 102968b8534bSLuigi Rizzo struct netmap_kring *kring; 1030d0c7b075SLuigi Rizzo u_int core_lock, i, check_all, want_tx, want_rx, revents = 0; 103164ae02c3SLuigi Rizzo u_int lim_tx, lim_rx; 1032bcda432eSLuigi Rizzo enum {NO_CL, NEED_CL, LOCKED_CL }; /* see below */ 1033*01c7d25fSLuigi Rizzo void *pwait = dev; /* linux compatibility */ 1034*01c7d25fSLuigi Rizzo 1035*01c7d25fSLuigi Rizzo (void)pwait; 103668b8534bSLuigi Rizzo 103768b8534bSLuigi Rizzo if (devfs_get_cdevpriv((void **)&priv) != 0 || priv == NULL) 103868b8534bSLuigi Rizzo return POLLERR; 103968b8534bSLuigi Rizzo 104068b8534bSLuigi Rizzo ifp = priv->np_ifp; 104168b8534bSLuigi Rizzo // XXX check for deleting() ? 104268b8534bSLuigi Rizzo if ( (ifp->if_capenable & IFCAP_NETMAP) == 0) 104368b8534bSLuigi Rizzo return POLLERR; 104468b8534bSLuigi Rizzo 104568b8534bSLuigi Rizzo if (netmap_verbose & 0x8000) 104668b8534bSLuigi Rizzo D("device %s events 0x%x", ifp->if_xname, events); 104768b8534bSLuigi Rizzo want_tx = events & (POLLOUT | POLLWRNORM); 104868b8534bSLuigi Rizzo want_rx = events & (POLLIN | POLLRDNORM); 104968b8534bSLuigi Rizzo 105068b8534bSLuigi Rizzo na = NA(ifp); /* retrieve netmap adapter */ 105168b8534bSLuigi Rizzo 1052d76bf4ffSLuigi Rizzo lim_tx = na->num_tx_rings; 1053d76bf4ffSLuigi Rizzo lim_rx = na->num_rx_rings; 105468b8534bSLuigi Rizzo /* how many queues we are scanning */ 105564ae02c3SLuigi Rizzo if (priv->np_qfirst == NETMAP_SW_RING) { 105668b8534bSLuigi Rizzo if (priv->np_txpoll || want_tx) { 105768b8534bSLuigi Rizzo /* push any packets up, then we are always ready */ 105864ae02c3SLuigi Rizzo kring = &na->tx_rings[lim_tx]; 105968b8534bSLuigi Rizzo netmap_sync_to_host(na); 106068b8534bSLuigi Rizzo revents |= want_tx; 106168b8534bSLuigi Rizzo } 106268b8534bSLuigi Rizzo if (want_rx) { 106364ae02c3SLuigi Rizzo kring = &na->rx_rings[lim_rx]; 106468b8534bSLuigi Rizzo if (kring->ring->avail == 0) 1065*01c7d25fSLuigi Rizzo netmap_sync_from_host(na, td, dev); 106668b8534bSLuigi Rizzo if (kring->ring->avail > 0) { 106768b8534bSLuigi Rizzo revents |= want_rx; 106868b8534bSLuigi Rizzo } 106968b8534bSLuigi Rizzo } 107068b8534bSLuigi Rizzo return (revents); 107168b8534bSLuigi Rizzo } 107268b8534bSLuigi Rizzo 107368b8534bSLuigi Rizzo /* 107468b8534bSLuigi Rizzo * check_all is set if the card has more than one queue and 107568b8534bSLuigi Rizzo * the client is polling all of them. If true, we sleep on 107668b8534bSLuigi Rizzo * the "global" selfd, otherwise we sleep on individual selfd 107768b8534bSLuigi Rizzo * (we can only sleep on one of them per direction). 107868b8534bSLuigi Rizzo * The interrupt routine in the driver should always wake on 107968b8534bSLuigi Rizzo * the individual selfd, and also on the global one if the card 108068b8534bSLuigi Rizzo * has more than one ring. 108168b8534bSLuigi Rizzo * 108268b8534bSLuigi Rizzo * If the card has only one lock, we just use that. 108368b8534bSLuigi Rizzo * If the card has separate ring locks, we just use those 108468b8534bSLuigi Rizzo * unless we are doing check_all, in which case the whole 108568b8534bSLuigi Rizzo * loop is wrapped by the global lock. 108668b8534bSLuigi Rizzo * We acquire locks only when necessary: if poll is called 108768b8534bSLuigi Rizzo * when buffers are available, we can just return without locks. 108868b8534bSLuigi Rizzo * 108968b8534bSLuigi Rizzo * rxsync() is only called if we run out of buffers on a POLLIN. 109068b8534bSLuigi Rizzo * txsync() is called if we run out of buffers on POLLOUT, or 109168b8534bSLuigi Rizzo * there are pending packets to send. The latter can be disabled 109268b8534bSLuigi Rizzo * passing NETMAP_NO_TX_POLL in the NIOCREG call. 109368b8534bSLuigi Rizzo */ 109464ae02c3SLuigi Rizzo check_all = (priv->np_qlast == NETMAP_HW_RING) && (lim_tx > 1 || lim_rx > 1); 109568b8534bSLuigi Rizzo 109668b8534bSLuigi Rizzo /* 109768b8534bSLuigi Rizzo * core_lock indicates what to do with the core lock. 109868b8534bSLuigi Rizzo * The core lock is used when either the card has no individual 109968b8534bSLuigi Rizzo * locks, or it has individual locks but we are cheking all 110068b8534bSLuigi Rizzo * rings so we need the core lock to avoid missing wakeup events. 110168b8534bSLuigi Rizzo * 110268b8534bSLuigi Rizzo * It has three possible states: 110368b8534bSLuigi Rizzo * NO_CL we don't need to use the core lock, e.g. 110468b8534bSLuigi Rizzo * because we are protected by individual locks. 110568b8534bSLuigi Rizzo * NEED_CL we need the core lock. In this case, when we 110668b8534bSLuigi Rizzo * call the lock routine, move to LOCKED_CL 110768b8534bSLuigi Rizzo * to remember to release the lock once done. 110868b8534bSLuigi Rizzo * LOCKED_CL core lock is set, so we need to release it. 110968b8534bSLuigi Rizzo */ 1110d0c7b075SLuigi Rizzo core_lock = (check_all || !na->separate_locks) ? NEED_CL : NO_CL; 1111f196ce38SLuigi Rizzo #ifdef NM_BRIDGE 1112f196ce38SLuigi Rizzo /* the bridge uses separate locks */ 1113f196ce38SLuigi Rizzo if (na->nm_register == bdg_netmap_reg) { 1114f196ce38SLuigi Rizzo ND("not using core lock for %s", ifp->if_xname); 1115f196ce38SLuigi Rizzo core_lock = NO_CL; 1116f196ce38SLuigi Rizzo } 1117f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */ 111864ae02c3SLuigi Rizzo if (priv->np_qlast != NETMAP_HW_RING) { 111964ae02c3SLuigi Rizzo lim_tx = lim_rx = priv->np_qlast; 112064ae02c3SLuigi Rizzo } 112164ae02c3SLuigi Rizzo 112268b8534bSLuigi Rizzo /* 112368b8534bSLuigi Rizzo * We start with a lock free round which is good if we have 112468b8534bSLuigi Rizzo * data available. If this fails, then lock and call the sync 112568b8534bSLuigi Rizzo * routines. 112668b8534bSLuigi Rizzo */ 112764ae02c3SLuigi Rizzo for (i = priv->np_qfirst; want_rx && i < lim_rx; i++) { 112868b8534bSLuigi Rizzo kring = &na->rx_rings[i]; 112968b8534bSLuigi Rizzo if (kring->ring->avail > 0) { 113068b8534bSLuigi Rizzo revents |= want_rx; 113168b8534bSLuigi Rizzo want_rx = 0; /* also breaks the loop */ 113268b8534bSLuigi Rizzo } 113368b8534bSLuigi Rizzo } 113464ae02c3SLuigi Rizzo for (i = priv->np_qfirst; want_tx && i < lim_tx; i++) { 113568b8534bSLuigi Rizzo kring = &na->tx_rings[i]; 113668b8534bSLuigi Rizzo if (kring->ring->avail > 0) { 113768b8534bSLuigi Rizzo revents |= want_tx; 113868b8534bSLuigi Rizzo want_tx = 0; /* also breaks the loop */ 113968b8534bSLuigi Rizzo } 114068b8534bSLuigi Rizzo } 114168b8534bSLuigi Rizzo 114268b8534bSLuigi Rizzo /* 114368b8534bSLuigi Rizzo * If we to push packets out (priv->np_txpoll) or want_tx is 114468b8534bSLuigi Rizzo * still set, we do need to run the txsync calls (on all rings, 114568b8534bSLuigi Rizzo * to avoid that the tx rings stall). 114668b8534bSLuigi Rizzo */ 114768b8534bSLuigi Rizzo if (priv->np_txpoll || want_tx) { 114864ae02c3SLuigi Rizzo for (i = priv->np_qfirst; i < lim_tx; i++) { 114968b8534bSLuigi Rizzo kring = &na->tx_rings[i]; 11505819da83SLuigi Rizzo /* 11515819da83SLuigi Rizzo * Skip the current ring if want_tx == 0 11525819da83SLuigi Rizzo * (we have already done a successful sync on 11535819da83SLuigi Rizzo * a previous ring) AND kring->cur == kring->hwcur 11545819da83SLuigi Rizzo * (there are no pending transmissions for this ring). 11555819da83SLuigi Rizzo */ 115668b8534bSLuigi Rizzo if (!want_tx && kring->ring->cur == kring->nr_hwcur) 115768b8534bSLuigi Rizzo continue; 115868b8534bSLuigi Rizzo if (core_lock == NEED_CL) { 11591a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_LOCK, 0); 116068b8534bSLuigi Rizzo core_lock = LOCKED_CL; 116168b8534bSLuigi Rizzo } 116268b8534bSLuigi Rizzo if (na->separate_locks) 11631a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_TX_LOCK, i); 116468b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_TXSYNC) 116568b8534bSLuigi Rizzo D("send %d on %s %d", 116668b8534bSLuigi Rizzo kring->ring->cur, 116768b8534bSLuigi Rizzo ifp->if_xname, i); 11681a26580eSLuigi Rizzo if (na->nm_txsync(ifp, i, 0 /* no lock */)) 116968b8534bSLuigi Rizzo revents |= POLLERR; 117068b8534bSLuigi Rizzo 11715819da83SLuigi Rizzo /* Check avail/call selrecord only if called with POLLOUT */ 117268b8534bSLuigi Rizzo if (want_tx) { 117368b8534bSLuigi Rizzo if (kring->ring->avail > 0) { 117468b8534bSLuigi Rizzo /* stop at the first ring. We don't risk 117568b8534bSLuigi Rizzo * starvation. 117668b8534bSLuigi Rizzo */ 117768b8534bSLuigi Rizzo revents |= want_tx; 117868b8534bSLuigi Rizzo want_tx = 0; 117968b8534bSLuigi Rizzo } else if (!check_all) 118068b8534bSLuigi Rizzo selrecord(td, &kring->si); 118168b8534bSLuigi Rizzo } 118268b8534bSLuigi Rizzo if (na->separate_locks) 11831a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_TX_UNLOCK, i); 118468b8534bSLuigi Rizzo } 118568b8534bSLuigi Rizzo } 118668b8534bSLuigi Rizzo 118768b8534bSLuigi Rizzo /* 118868b8534bSLuigi Rizzo * now if want_rx is still set we need to lock and rxsync. 118968b8534bSLuigi Rizzo * Do it on all rings because otherwise we starve. 119068b8534bSLuigi Rizzo */ 119168b8534bSLuigi Rizzo if (want_rx) { 119264ae02c3SLuigi Rizzo for (i = priv->np_qfirst; i < lim_rx; i++) { 119368b8534bSLuigi Rizzo kring = &na->rx_rings[i]; 119468b8534bSLuigi Rizzo if (core_lock == NEED_CL) { 11951a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_LOCK, 0); 119668b8534bSLuigi Rizzo core_lock = LOCKED_CL; 119768b8534bSLuigi Rizzo } 119868b8534bSLuigi Rizzo if (na->separate_locks) 11991a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_RX_LOCK, i); 120068b8534bSLuigi Rizzo 12011a26580eSLuigi Rizzo if (na->nm_rxsync(ifp, i, 0 /* no lock */)) 120268b8534bSLuigi Rizzo revents |= POLLERR; 12035819da83SLuigi Rizzo if (netmap_no_timestamp == 0 || 12045819da83SLuigi Rizzo kring->ring->flags & NR_TIMESTAMP) { 120568b8534bSLuigi Rizzo microtime(&kring->ring->ts); 12065819da83SLuigi Rizzo } 120768b8534bSLuigi Rizzo 120868b8534bSLuigi Rizzo if (kring->ring->avail > 0) 120968b8534bSLuigi Rizzo revents |= want_rx; 121068b8534bSLuigi Rizzo else if (!check_all) 121168b8534bSLuigi Rizzo selrecord(td, &kring->si); 121268b8534bSLuigi Rizzo if (na->separate_locks) 12131a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_RX_UNLOCK, i); 121468b8534bSLuigi Rizzo } 121568b8534bSLuigi Rizzo } 121664ae02c3SLuigi Rizzo if (check_all && revents == 0) { /* signal on the global queue */ 121768b8534bSLuigi Rizzo if (want_tx) 121864ae02c3SLuigi Rizzo selrecord(td, &na->tx_si); 121968b8534bSLuigi Rizzo if (want_rx) 122064ae02c3SLuigi Rizzo selrecord(td, &na->rx_si); 122168b8534bSLuigi Rizzo } 122268b8534bSLuigi Rizzo if (core_lock == LOCKED_CL) 12231a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0); 122468b8534bSLuigi Rizzo 122568b8534bSLuigi Rizzo return (revents); 122668b8534bSLuigi Rizzo } 122768b8534bSLuigi Rizzo 122868b8534bSLuigi Rizzo /*------- driver support routines ------*/ 122968b8534bSLuigi Rizzo 123068b8534bSLuigi Rizzo /* 1231babc7c12SLuigi Rizzo * default lock wrapper. 12321a26580eSLuigi Rizzo */ 12331a26580eSLuigi Rizzo static void 1234babc7c12SLuigi Rizzo netmap_lock_wrapper(struct ifnet *dev, int what, u_int queueid) 12351a26580eSLuigi Rizzo { 1236babc7c12SLuigi Rizzo struct netmap_adapter *na = NA(dev); 12371a26580eSLuigi Rizzo 12381a26580eSLuigi Rizzo switch (what) { 1239babc7c12SLuigi Rizzo #ifdef linux /* some system do not need lock on register */ 12401a26580eSLuigi Rizzo case NETMAP_REG_LOCK: 12411a26580eSLuigi Rizzo case NETMAP_REG_UNLOCK: 12421a26580eSLuigi Rizzo break; 1243babc7c12SLuigi Rizzo #endif /* linux */ 12441a26580eSLuigi Rizzo 12451a26580eSLuigi Rizzo case NETMAP_CORE_LOCK: 12461a26580eSLuigi Rizzo mtx_lock(&na->core_lock); 12471a26580eSLuigi Rizzo break; 12481a26580eSLuigi Rizzo 12491a26580eSLuigi Rizzo case NETMAP_CORE_UNLOCK: 12501a26580eSLuigi Rizzo mtx_unlock(&na->core_lock); 12511a26580eSLuigi Rizzo break; 12521a26580eSLuigi Rizzo 12531a26580eSLuigi Rizzo case NETMAP_TX_LOCK: 12541a26580eSLuigi Rizzo mtx_lock(&na->tx_rings[queueid].q_lock); 12551a26580eSLuigi Rizzo break; 12561a26580eSLuigi Rizzo 12571a26580eSLuigi Rizzo case NETMAP_TX_UNLOCK: 12581a26580eSLuigi Rizzo mtx_unlock(&na->tx_rings[queueid].q_lock); 12591a26580eSLuigi Rizzo break; 12601a26580eSLuigi Rizzo 12611a26580eSLuigi Rizzo case NETMAP_RX_LOCK: 12621a26580eSLuigi Rizzo mtx_lock(&na->rx_rings[queueid].q_lock); 12631a26580eSLuigi Rizzo break; 12641a26580eSLuigi Rizzo 12651a26580eSLuigi Rizzo case NETMAP_RX_UNLOCK: 12661a26580eSLuigi Rizzo mtx_unlock(&na->rx_rings[queueid].q_lock); 12671a26580eSLuigi Rizzo break; 12681a26580eSLuigi Rizzo } 12691a26580eSLuigi Rizzo } 12701a26580eSLuigi Rizzo 12711a26580eSLuigi Rizzo 12721a26580eSLuigi Rizzo /* 127368b8534bSLuigi Rizzo * Initialize a ``netmap_adapter`` object created by driver on attach. 127468b8534bSLuigi Rizzo * We allocate a block of memory with room for a struct netmap_adapter 127568b8534bSLuigi Rizzo * plus two sets of N+2 struct netmap_kring (where N is the number 127668b8534bSLuigi Rizzo * of hardware rings): 127768b8534bSLuigi Rizzo * krings 0..N-1 are for the hardware queues. 127868b8534bSLuigi Rizzo * kring N is for the host stack queue 127968b8534bSLuigi Rizzo * kring N+1 is only used for the selinfo for all queues. 128068b8534bSLuigi Rizzo * Return 0 on success, ENOMEM otherwise. 128164ae02c3SLuigi Rizzo * 1282d76bf4ffSLuigi Rizzo * na->num_tx_rings can be set for cards with different tx/rx setups 128368b8534bSLuigi Rizzo */ 128468b8534bSLuigi Rizzo int 128568b8534bSLuigi Rizzo netmap_attach(struct netmap_adapter *na, int num_queues) 128668b8534bSLuigi Rizzo { 1287f196ce38SLuigi Rizzo int n, size; 128868b8534bSLuigi Rizzo void *buf; 128968b8534bSLuigi Rizzo struct ifnet *ifp = na->ifp; 129068b8534bSLuigi Rizzo 129168b8534bSLuigi Rizzo if (ifp == NULL) { 129268b8534bSLuigi Rizzo D("ifp not set, giving up"); 129368b8534bSLuigi Rizzo return EINVAL; 129468b8534bSLuigi Rizzo } 129564ae02c3SLuigi Rizzo /* clear other fields ? */ 129668b8534bSLuigi Rizzo na->refcount = 0; 1297d76bf4ffSLuigi Rizzo if (na->num_tx_rings == 0) 1298d76bf4ffSLuigi Rizzo na->num_tx_rings = num_queues; 1299d76bf4ffSLuigi Rizzo na->num_rx_rings = num_queues; 130064ae02c3SLuigi Rizzo /* on each direction we have N+1 resources 130164ae02c3SLuigi Rizzo * 0..n-1 are the hardware rings 130264ae02c3SLuigi Rizzo * n is the ring attached to the stack. 130364ae02c3SLuigi Rizzo */ 1304d76bf4ffSLuigi Rizzo n = na->num_rx_rings + na->num_tx_rings + 2; 130564ae02c3SLuigi Rizzo size = sizeof(*na) + n * sizeof(struct netmap_kring); 130668b8534bSLuigi Rizzo 130768b8534bSLuigi Rizzo buf = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO); 130868b8534bSLuigi Rizzo if (buf) { 1309d0c7b075SLuigi Rizzo WNA(ifp) = buf; 131068b8534bSLuigi Rizzo na->tx_rings = (void *)((char *)buf + sizeof(*na)); 1311d76bf4ffSLuigi Rizzo na->rx_rings = na->tx_rings + na->num_tx_rings + 1; 131268b8534bSLuigi Rizzo bcopy(na, buf, sizeof(*na)); 131368b8534bSLuigi Rizzo ifp->if_capabilities |= IFCAP_NETMAP; 13141a26580eSLuigi Rizzo 13151a26580eSLuigi Rizzo na = buf; 1316f196ce38SLuigi Rizzo if (na->nm_lock == NULL) { 1317f196ce38SLuigi Rizzo ND("using default locks for %s", ifp->if_xname); 13181a26580eSLuigi Rizzo na->nm_lock = netmap_lock_wrapper; 1319f196ce38SLuigi Rizzo /* core lock initialized here. 1320f196ce38SLuigi Rizzo * others initialized after netmap_if_new 1321f196ce38SLuigi Rizzo */ 1322f196ce38SLuigi Rizzo mtx_init(&na->core_lock, "netmap core lock", MTX_NETWORK_LOCK, MTX_DEF); 1323f196ce38SLuigi Rizzo } 132468b8534bSLuigi Rizzo } 132564ae02c3SLuigi Rizzo #ifdef linux 1326f196ce38SLuigi Rizzo if (ifp->netdev_ops) { 132764ae02c3SLuigi Rizzo D("netdev_ops %p", ifp->netdev_ops); 132864ae02c3SLuigi Rizzo /* prepare a clone of the netdev ops */ 132964ae02c3SLuigi Rizzo na->nm_ndo = *ifp->netdev_ops; 1330f196ce38SLuigi Rizzo } 133164ae02c3SLuigi Rizzo na->nm_ndo.ndo_start_xmit = netmap_start_linux; 133264ae02c3SLuigi Rizzo #endif 133368b8534bSLuigi Rizzo D("%s for %s", buf ? "ok" : "failed", ifp->if_xname); 133468b8534bSLuigi Rizzo 133568b8534bSLuigi Rizzo return (buf ? 0 : ENOMEM); 133668b8534bSLuigi Rizzo } 133768b8534bSLuigi Rizzo 133868b8534bSLuigi Rizzo 133968b8534bSLuigi Rizzo /* 134068b8534bSLuigi Rizzo * Free the allocated memory linked to the given ``netmap_adapter`` 134168b8534bSLuigi Rizzo * object. 134268b8534bSLuigi Rizzo */ 134368b8534bSLuigi Rizzo void 134468b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp) 134568b8534bSLuigi Rizzo { 134668b8534bSLuigi Rizzo u_int i; 134768b8534bSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 134868b8534bSLuigi Rizzo 134968b8534bSLuigi Rizzo if (!na) 135068b8534bSLuigi Rizzo return; 135168b8534bSLuigi Rizzo 1352d76bf4ffSLuigi Rizzo for (i = 0; i < na->num_tx_rings + 1; i++) { 135368b8534bSLuigi Rizzo knlist_destroy(&na->tx_rings[i].si.si_note); 135464ae02c3SLuigi Rizzo mtx_destroy(&na->tx_rings[i].q_lock); 135568b8534bSLuigi Rizzo } 1356d76bf4ffSLuigi Rizzo for (i = 0; i < na->num_rx_rings + 1; i++) { 135764ae02c3SLuigi Rizzo knlist_destroy(&na->rx_rings[i].si.si_note); 135864ae02c3SLuigi Rizzo mtx_destroy(&na->rx_rings[i].q_lock); 135964ae02c3SLuigi Rizzo } 136064ae02c3SLuigi Rizzo knlist_destroy(&na->tx_si.si_note); 136164ae02c3SLuigi Rizzo knlist_destroy(&na->rx_si.si_note); 136268b8534bSLuigi Rizzo bzero(na, sizeof(*na)); 1363d0c7b075SLuigi Rizzo WNA(ifp) = NULL; 136468b8534bSLuigi Rizzo free(na, M_DEVBUF); 136568b8534bSLuigi Rizzo } 136668b8534bSLuigi Rizzo 136768b8534bSLuigi Rizzo 136868b8534bSLuigi Rizzo /* 136902ad4083SLuigi Rizzo * Intercept packets from the network stack and pass them 137002ad4083SLuigi Rizzo * to netmap as incoming packets on the 'software' ring. 137168b8534bSLuigi Rizzo * We are not locked when called. 137268b8534bSLuigi Rizzo */ 137368b8534bSLuigi Rizzo int 137468b8534bSLuigi Rizzo netmap_start(struct ifnet *ifp, struct mbuf *m) 137568b8534bSLuigi Rizzo { 137668b8534bSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 1377d76bf4ffSLuigi Rizzo struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings]; 13781a26580eSLuigi Rizzo u_int i, len = MBUF_LEN(m); 137902ad4083SLuigi Rizzo int error = EBUSY, lim = kring->nkr_num_slots - 1; 138068b8534bSLuigi Rizzo struct netmap_slot *slot; 138168b8534bSLuigi Rizzo 138268b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_HOST) 138368b8534bSLuigi Rizzo D("%s packet %d len %d from the stack", ifp->if_xname, 138468b8534bSLuigi Rizzo kring->nr_hwcur + kring->nr_hwavail, len); 13851a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_LOCK, 0); 138602ad4083SLuigi Rizzo if (kring->nr_hwavail >= lim) { 13875b248374SLuigi Rizzo if (netmap_verbose) 138868b8534bSLuigi Rizzo D("stack ring %s full\n", ifp->if_xname); 138968b8534bSLuigi Rizzo goto done; /* no space */ 139068b8534bSLuigi Rizzo } 139164ae02c3SLuigi Rizzo if (len > NETMAP_BUF_SIZE) { 139264ae02c3SLuigi Rizzo D("drop packet size %d > %d", len, NETMAP_BUF_SIZE); 139368b8534bSLuigi Rizzo goto done; /* too long for us */ 139468b8534bSLuigi Rizzo } 139568b8534bSLuigi Rizzo 139668b8534bSLuigi Rizzo /* compute the insert position */ 139768b8534bSLuigi Rizzo i = kring->nr_hwcur + kring->nr_hwavail; 139802ad4083SLuigi Rizzo if (i > lim) 139902ad4083SLuigi Rizzo i -= lim + 1; 140068b8534bSLuigi Rizzo slot = &kring->ring->slot[i]; 140168b8534bSLuigi Rizzo m_copydata(m, 0, len, NMB(slot)); 140268b8534bSLuigi Rizzo slot->len = len; 140368b8534bSLuigi Rizzo kring->nr_hwavail++; 140468b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_HOST) 1405d76bf4ffSLuigi Rizzo D("wake up host ring %s %d", na->ifp->if_xname, na->num_rx_rings); 140668b8534bSLuigi Rizzo selwakeuppri(&kring->si, PI_NET); 140768b8534bSLuigi Rizzo error = 0; 140868b8534bSLuigi Rizzo done: 14091a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0); 141068b8534bSLuigi Rizzo 141168b8534bSLuigi Rizzo /* release the mbuf in either cases of success or failure. As an 141268b8534bSLuigi Rizzo * alternative, put the mbuf in a free list and free the list 141368b8534bSLuigi Rizzo * only when really necessary. 141468b8534bSLuigi Rizzo */ 141568b8534bSLuigi Rizzo m_freem(m); 141668b8534bSLuigi Rizzo 141768b8534bSLuigi Rizzo return (error); 141868b8534bSLuigi Rizzo } 141968b8534bSLuigi Rizzo 142068b8534bSLuigi Rizzo 142168b8534bSLuigi Rizzo /* 142268b8534bSLuigi Rizzo * netmap_reset() is called by the driver routines when reinitializing 142368b8534bSLuigi Rizzo * a ring. The driver is in charge of locking to protect the kring. 142468b8534bSLuigi Rizzo * If netmap mode is not set just return NULL. 142568b8534bSLuigi Rizzo */ 142668b8534bSLuigi Rizzo struct netmap_slot * 142768b8534bSLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, int n, 142868b8534bSLuigi Rizzo u_int new_cur) 142968b8534bSLuigi Rizzo { 143068b8534bSLuigi Rizzo struct netmap_kring *kring; 1431506cc70cSLuigi Rizzo int new_hwofs, lim; 143268b8534bSLuigi Rizzo 143368b8534bSLuigi Rizzo if (na == NULL) 143468b8534bSLuigi Rizzo return NULL; /* no netmap support here */ 143568b8534bSLuigi Rizzo if (!(na->ifp->if_capenable & IFCAP_NETMAP)) 143668b8534bSLuigi Rizzo return NULL; /* nothing to reinitialize */ 143768b8534bSLuigi Rizzo 143864ae02c3SLuigi Rizzo if (tx == NR_TX) { 143964ae02c3SLuigi Rizzo kring = na->tx_rings + n; 1440506cc70cSLuigi Rizzo new_hwofs = kring->nr_hwcur - new_cur; 144164ae02c3SLuigi Rizzo } else { 144264ae02c3SLuigi Rizzo kring = na->rx_rings + n; 1443506cc70cSLuigi Rizzo new_hwofs = kring->nr_hwcur + kring->nr_hwavail - new_cur; 144464ae02c3SLuigi Rizzo } 144564ae02c3SLuigi Rizzo lim = kring->nkr_num_slots - 1; 1446506cc70cSLuigi Rizzo if (new_hwofs > lim) 1447506cc70cSLuigi Rizzo new_hwofs -= lim + 1; 1448506cc70cSLuigi Rizzo 1449506cc70cSLuigi Rizzo /* Alwayws set the new offset value and realign the ring. */ 1450506cc70cSLuigi Rizzo kring->nkr_hwofs = new_hwofs; 1451506cc70cSLuigi Rizzo if (tx == NR_TX) 1452506cc70cSLuigi Rizzo kring->nr_hwavail = kring->nkr_num_slots - 1; 1453506cc70cSLuigi Rizzo D("new hwofs %d on %s %s[%d]", 1454506cc70cSLuigi Rizzo kring->nkr_hwofs, na->ifp->if_xname, 1455506cc70cSLuigi Rizzo tx == NR_TX ? "TX" : "RX", n); 1456506cc70cSLuigi Rizzo 1457f196ce38SLuigi Rizzo #if 0 // def linux 1458f196ce38SLuigi Rizzo /* XXX check that the mappings are correct */ 1459f196ce38SLuigi Rizzo /* need ring_nr, adapter->pdev, direction */ 1460f196ce38SLuigi Rizzo buffer_info->dma = dma_map_single(&pdev->dev, addr, adapter->rx_buffer_len, DMA_FROM_DEVICE); 1461f196ce38SLuigi Rizzo if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) { 1462f196ce38SLuigi Rizzo D("error mapping rx netmap buffer %d", i); 1463f196ce38SLuigi Rizzo // XXX fix error handling 1464f196ce38SLuigi Rizzo } 1465f196ce38SLuigi Rizzo 1466f196ce38SLuigi Rizzo #endif /* linux */ 146768b8534bSLuigi Rizzo /* 146864ae02c3SLuigi Rizzo * Wakeup on the individual and global lock 1469506cc70cSLuigi Rizzo * We do the wakeup here, but the ring is not yet reconfigured. 1470506cc70cSLuigi Rizzo * However, we are under lock so there are no races. 147168b8534bSLuigi Rizzo */ 147268b8534bSLuigi Rizzo selwakeuppri(&kring->si, PI_NET); 147364ae02c3SLuigi Rizzo selwakeuppri(tx == NR_TX ? &na->tx_si : &na->rx_si, PI_NET); 147468b8534bSLuigi Rizzo return kring->ring->slot; 147568b8534bSLuigi Rizzo } 147668b8534bSLuigi Rizzo 147768b8534bSLuigi Rizzo 147868b8534bSLuigi Rizzo /* 14791a26580eSLuigi Rizzo * Default functions to handle rx/tx interrupts 14801a26580eSLuigi Rizzo * we have 4 cases: 14811a26580eSLuigi Rizzo * 1 ring, single lock: 14821a26580eSLuigi Rizzo * lock(core); wake(i=0); unlock(core) 14831a26580eSLuigi Rizzo * N rings, single lock: 14841a26580eSLuigi Rizzo * lock(core); wake(i); wake(N+1) unlock(core) 14851a26580eSLuigi Rizzo * 1 ring, separate locks: (i=0) 14861a26580eSLuigi Rizzo * lock(i); wake(i); unlock(i) 14871a26580eSLuigi Rizzo * N rings, separate locks: 14881a26580eSLuigi Rizzo * lock(i); wake(i); unlock(i); lock(core) wake(N+1) unlock(core) 148964ae02c3SLuigi Rizzo * work_done is non-null on the RX path. 14901a26580eSLuigi Rizzo */ 1491babc7c12SLuigi Rizzo int 1492babc7c12SLuigi Rizzo netmap_rx_irq(struct ifnet *ifp, int q, int *work_done) 14931a26580eSLuigi Rizzo { 14941a26580eSLuigi Rizzo struct netmap_adapter *na; 14951a26580eSLuigi Rizzo struct netmap_kring *r; 149664ae02c3SLuigi Rizzo NM_SELINFO_T *main_wq; 14971a26580eSLuigi Rizzo 14981a26580eSLuigi Rizzo if (!(ifp->if_capenable & IFCAP_NETMAP)) 14991a26580eSLuigi Rizzo return 0; 15001a26580eSLuigi Rizzo na = NA(ifp); 150164ae02c3SLuigi Rizzo if (work_done) { /* RX path */ 150264ae02c3SLuigi Rizzo r = na->rx_rings + q; 150364ae02c3SLuigi Rizzo r->nr_kflags |= NKR_PENDINTR; 1504d76bf4ffSLuigi Rizzo main_wq = (na->num_rx_rings > 1) ? &na->rx_si : NULL; 150564ae02c3SLuigi Rizzo } else { /* tx path */ 150664ae02c3SLuigi Rizzo r = na->tx_rings + q; 1507d76bf4ffSLuigi Rizzo main_wq = (na->num_tx_rings > 1) ? &na->tx_si : NULL; 150864ae02c3SLuigi Rizzo work_done = &q; /* dummy */ 150964ae02c3SLuigi Rizzo } 15101a26580eSLuigi Rizzo if (na->separate_locks) { 151164ae02c3SLuigi Rizzo mtx_lock(&r->q_lock); 151264ae02c3SLuigi Rizzo selwakeuppri(&r->si, PI_NET); 151364ae02c3SLuigi Rizzo mtx_unlock(&r->q_lock); 151464ae02c3SLuigi Rizzo if (main_wq) { 15151a26580eSLuigi Rizzo mtx_lock(&na->core_lock); 151664ae02c3SLuigi Rizzo selwakeuppri(main_wq, PI_NET); 15171a26580eSLuigi Rizzo mtx_unlock(&na->core_lock); 15181a26580eSLuigi Rizzo } 15191a26580eSLuigi Rizzo } else { 15201a26580eSLuigi Rizzo mtx_lock(&na->core_lock); 152164ae02c3SLuigi Rizzo selwakeuppri(&r->si, PI_NET); 152264ae02c3SLuigi Rizzo if (main_wq) 152364ae02c3SLuigi Rizzo selwakeuppri(main_wq, PI_NET); 15241a26580eSLuigi Rizzo mtx_unlock(&na->core_lock); 15251a26580eSLuigi Rizzo } 15261a26580eSLuigi Rizzo *work_done = 1; /* do not fire napi again */ 15271a26580eSLuigi Rizzo return 1; 15281a26580eSLuigi Rizzo } 15291a26580eSLuigi Rizzo 153064ae02c3SLuigi Rizzo 1531*01c7d25fSLuigi Rizzo #ifdef linux /* linux-specific routines */ 1532*01c7d25fSLuigi Rizzo 1533*01c7d25fSLuigi Rizzo /* 1534*01c7d25fSLuigi Rizzo * Remap linux arguments into the FreeBSD call. 1535*01c7d25fSLuigi Rizzo * - pwait is the poll table, passed as 'dev'; 1536*01c7d25fSLuigi Rizzo * If pwait == NULL someone else already woke up before. We can report 1537*01c7d25fSLuigi Rizzo * events but they are filtered upstream. 1538*01c7d25fSLuigi Rizzo * If pwait != NULL, then pwait->key contains the list of events. 1539*01c7d25fSLuigi Rizzo * - events is computed from pwait as above. 1540*01c7d25fSLuigi Rizzo * - file is passed as 'td'; 1541*01c7d25fSLuigi Rizzo */ 1542*01c7d25fSLuigi Rizzo static u_int 1543*01c7d25fSLuigi Rizzo linux_netmap_poll(struct file * file, struct poll_table_struct *pwait) 1544*01c7d25fSLuigi Rizzo { 1545*01c7d25fSLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(3,4,0) 1546*01c7d25fSLuigi Rizzo int events = pwait ? pwait->key : POLLIN | POLLOUT; 1547*01c7d25fSLuigi Rizzo #else /* in 3.4.0 field 'key' was renamed to '_key' */ 1548*01c7d25fSLuigi Rizzo int events = pwait ? pwait->_key : POLLIN | POLLOUT; 1549*01c7d25fSLuigi Rizzo #endif 1550*01c7d25fSLuigi Rizzo return netmap_poll((void *)pwait, events, (void *)file); 1551*01c7d25fSLuigi Rizzo } 1552*01c7d25fSLuigi Rizzo 1553*01c7d25fSLuigi Rizzo static int 1554*01c7d25fSLuigi Rizzo netmap_mmap(__unused struct file *f, struct vm_area_struct *vma) 1555*01c7d25fSLuigi Rizzo { 1556*01c7d25fSLuigi Rizzo int lut_skip, i, j; 1557*01c7d25fSLuigi Rizzo int user_skip = 0; 1558*01c7d25fSLuigi Rizzo struct lut_entry *l_entry; 1559*01c7d25fSLuigi Rizzo const struct netmap_obj_pool *p[] = { 1560*01c7d25fSLuigi Rizzo nm_mem->nm_if_pool, 1561*01c7d25fSLuigi Rizzo nm_mem->nm_ring_pool, 1562*01c7d25fSLuigi Rizzo nm_mem->nm_buf_pool }; 1563*01c7d25fSLuigi Rizzo /* 1564*01c7d25fSLuigi Rizzo * vma->vm_start: start of mapping user address space 1565*01c7d25fSLuigi Rizzo * vma->vm_end: end of the mapping user address space 1566*01c7d25fSLuigi Rizzo */ 1567*01c7d25fSLuigi Rizzo 1568*01c7d25fSLuigi Rizzo // XXX security checks 1569*01c7d25fSLuigi Rizzo 1570*01c7d25fSLuigi Rizzo for (i = 0; i < 3; i++) { /* loop through obj_pools */ 1571*01c7d25fSLuigi Rizzo /* 1572*01c7d25fSLuigi Rizzo * In each pool memory is allocated in clusters 1573*01c7d25fSLuigi Rizzo * of size _clustsize , each containing clustentries 1574*01c7d25fSLuigi Rizzo * entries. For each object k we already store the 1575*01c7d25fSLuigi Rizzo * vtophys malling in lut[k] so we use that, scanning 1576*01c7d25fSLuigi Rizzo * the lut[] array in steps of clustentries, 1577*01c7d25fSLuigi Rizzo * and we map each cluster (not individual pages, 1578*01c7d25fSLuigi Rizzo * it would be overkill). 1579*01c7d25fSLuigi Rizzo */ 1580*01c7d25fSLuigi Rizzo for (lut_skip = 0, j = 0; j < p[i]->_numclusters; j++) { 1581*01c7d25fSLuigi Rizzo l_entry = &p[i]->lut[lut_skip]; 1582*01c7d25fSLuigi Rizzo if (remap_pfn_range(vma, vma->vm_start + user_skip, 1583*01c7d25fSLuigi Rizzo l_entry->paddr >> PAGE_SHIFT, p[i]->_clustsize, 1584*01c7d25fSLuigi Rizzo vma->vm_page_prot)) 1585*01c7d25fSLuigi Rizzo return -EAGAIN; // XXX check return value 1586*01c7d25fSLuigi Rizzo lut_skip += p[i]->clustentries; 1587*01c7d25fSLuigi Rizzo user_skip += p[i]->_clustsize; 1588*01c7d25fSLuigi Rizzo } 1589*01c7d25fSLuigi Rizzo } 1590*01c7d25fSLuigi Rizzo 1591*01c7d25fSLuigi Rizzo return 0; 1592*01c7d25fSLuigi Rizzo } 1593*01c7d25fSLuigi Rizzo 1594*01c7d25fSLuigi Rizzo static netdev_tx_t 1595*01c7d25fSLuigi Rizzo netmap_start_linux(struct sk_buff *skb, struct net_device *dev) 1596*01c7d25fSLuigi Rizzo { 1597*01c7d25fSLuigi Rizzo netmap_start(dev, skb); 1598*01c7d25fSLuigi Rizzo return (NETDEV_TX_OK); 1599*01c7d25fSLuigi Rizzo } 1600*01c7d25fSLuigi Rizzo 1601*01c7d25fSLuigi Rizzo 1602*01c7d25fSLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38) 1603*01c7d25fSLuigi Rizzo #define LIN_IOCTL_NAME .ioctl 1604*01c7d25fSLuigi Rizzo int 1605*01c7d25fSLuigi Rizzo linux_netmap_ioctl(struct inode *inode, struct file *file, u_int cmd, u_long data /* arg */) 1606*01c7d25fSLuigi Rizzo #else 1607*01c7d25fSLuigi Rizzo #define LIN_IOCTL_NAME .unlocked_ioctl 1608*01c7d25fSLuigi Rizzo long 1609*01c7d25fSLuigi Rizzo linux_netmap_ioctl(struct file *file, u_int cmd, u_long data /* arg */) 1610*01c7d25fSLuigi Rizzo #endif 1611*01c7d25fSLuigi Rizzo { 1612*01c7d25fSLuigi Rizzo int ret; 1613*01c7d25fSLuigi Rizzo struct nmreq nmr; 1614*01c7d25fSLuigi Rizzo bzero(&nmr, sizeof(nmr)); 1615*01c7d25fSLuigi Rizzo 1616*01c7d25fSLuigi Rizzo if (data && copy_from_user(&nmr, (void *)data, sizeof(nmr) ) != 0) 1617*01c7d25fSLuigi Rizzo return -EFAULT; 1618*01c7d25fSLuigi Rizzo ret = netmap_ioctl(NULL, cmd, (caddr_t)&nmr, 0, (void *)file); 1619*01c7d25fSLuigi Rizzo if (data && copy_to_user((void*)data, &nmr, sizeof(nmr) ) != 0) 1620*01c7d25fSLuigi Rizzo return -EFAULT; 1621*01c7d25fSLuigi Rizzo return -ret; 1622*01c7d25fSLuigi Rizzo } 1623*01c7d25fSLuigi Rizzo 1624*01c7d25fSLuigi Rizzo 1625*01c7d25fSLuigi Rizzo static int 1626*01c7d25fSLuigi Rizzo netmap_release(__unused struct inode *inode, struct file *file) 1627*01c7d25fSLuigi Rizzo { 1628*01c7d25fSLuigi Rizzo if (file->private_data) 1629*01c7d25fSLuigi Rizzo netmap_dtor(file->private_data); 1630*01c7d25fSLuigi Rizzo return (0); 1631*01c7d25fSLuigi Rizzo } 1632*01c7d25fSLuigi Rizzo 1633*01c7d25fSLuigi Rizzo 1634*01c7d25fSLuigi Rizzo static struct file_operations netmap_fops = { 1635*01c7d25fSLuigi Rizzo .mmap = netmap_mmap, 1636*01c7d25fSLuigi Rizzo LIN_IOCTL_NAME = linux_netmap_ioctl, 1637*01c7d25fSLuigi Rizzo .poll = linux_netmap_poll, 1638*01c7d25fSLuigi Rizzo .release = netmap_release, 1639*01c7d25fSLuigi Rizzo }; 1640*01c7d25fSLuigi Rizzo 1641*01c7d25fSLuigi Rizzo static struct miscdevice netmap_cdevsw = { /* same name as FreeBSD */ 1642*01c7d25fSLuigi Rizzo MISC_DYNAMIC_MINOR, 1643*01c7d25fSLuigi Rizzo "netmap", 1644*01c7d25fSLuigi Rizzo &netmap_fops, 1645*01c7d25fSLuigi Rizzo }; 1646*01c7d25fSLuigi Rizzo 1647*01c7d25fSLuigi Rizzo static int netmap_init(void); 1648*01c7d25fSLuigi Rizzo static void netmap_fini(void); 1649*01c7d25fSLuigi Rizzo 1650*01c7d25fSLuigi Rizzo module_init(netmap_init); 1651*01c7d25fSLuigi Rizzo module_exit(netmap_fini); 1652*01c7d25fSLuigi Rizzo /* export certain symbols to other modules */ 1653*01c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_attach); // driver attach routines 1654*01c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_detach); // driver detach routines 1655*01c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_ring_reinit); // ring init on error 1656*01c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_buffer_lut); 1657*01c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_total_buffers); // index check 1658*01c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_buffer_base); 1659*01c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_reset); // ring init routines 1660*01c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_buf_size); 1661*01c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_rx_irq); // default irq handler 1662*01c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_no_pendintr); // XXX mitigation - should go away 1663*01c7d25fSLuigi Rizzo 1664*01c7d25fSLuigi Rizzo 1665*01c7d25fSLuigi Rizzo MODULE_AUTHOR("http://info.iet.unipi.it/~luigi/netmap/"); 1666*01c7d25fSLuigi Rizzo MODULE_DESCRIPTION("The netmap packet I/O framework"); 1667*01c7d25fSLuigi Rizzo MODULE_LICENSE("Dual BSD/GPL"); /* the code here is all BSD. */ 1668*01c7d25fSLuigi Rizzo 1669*01c7d25fSLuigi Rizzo #else /* __FreeBSD__ */ 1670*01c7d25fSLuigi Rizzo 1671babc7c12SLuigi Rizzo static struct cdevsw netmap_cdevsw = { 1672babc7c12SLuigi Rizzo .d_version = D_VERSION, 1673babc7c12SLuigi Rizzo .d_name = "netmap", 1674babc7c12SLuigi Rizzo .d_mmap = netmap_mmap, 1675babc7c12SLuigi Rizzo .d_ioctl = netmap_ioctl, 1676babc7c12SLuigi Rizzo .d_poll = netmap_poll, 1677babc7c12SLuigi Rizzo }; 1678*01c7d25fSLuigi Rizzo #endif /* __FreeBSD__ */ 1679babc7c12SLuigi Rizzo 1680f196ce38SLuigi Rizzo #ifdef NM_BRIDGE 1681f196ce38SLuigi Rizzo /* 1682f196ce38SLuigi Rizzo *---- support for virtual bridge ----- 1683f196ce38SLuigi Rizzo */ 1684f196ce38SLuigi Rizzo 1685f196ce38SLuigi Rizzo /* ----- FreeBSD if_bridge hash function ------- */ 1686f196ce38SLuigi Rizzo 1687f196ce38SLuigi Rizzo /* 1688f196ce38SLuigi Rizzo * The following hash function is adapted from "Hash Functions" by Bob Jenkins 1689f196ce38SLuigi Rizzo * ("Algorithm Alley", Dr. Dobbs Journal, September 1997). 1690f196ce38SLuigi Rizzo * 1691f196ce38SLuigi Rizzo * http://www.burtleburtle.net/bob/hash/spooky.html 1692f196ce38SLuigi Rizzo */ 1693f196ce38SLuigi Rizzo #define mix(a, b, c) \ 1694f196ce38SLuigi Rizzo do { \ 1695f196ce38SLuigi Rizzo a -= b; a -= c; a ^= (c >> 13); \ 1696f196ce38SLuigi Rizzo b -= c; b -= a; b ^= (a << 8); \ 1697f196ce38SLuigi Rizzo c -= a; c -= b; c ^= (b >> 13); \ 1698f196ce38SLuigi Rizzo a -= b; a -= c; a ^= (c >> 12); \ 1699f196ce38SLuigi Rizzo b -= c; b -= a; b ^= (a << 16); \ 1700f196ce38SLuigi Rizzo c -= a; c -= b; c ^= (b >> 5); \ 1701f196ce38SLuigi Rizzo a -= b; a -= c; a ^= (c >> 3); \ 1702f196ce38SLuigi Rizzo b -= c; b -= a; b ^= (a << 10); \ 1703f196ce38SLuigi Rizzo c -= a; c -= b; c ^= (b >> 15); \ 1704f196ce38SLuigi Rizzo } while (/*CONSTCOND*/0) 1705f196ce38SLuigi Rizzo 1706f196ce38SLuigi Rizzo static __inline uint32_t 1707f196ce38SLuigi Rizzo nm_bridge_rthash(const uint8_t *addr) 1708f196ce38SLuigi Rizzo { 1709f196ce38SLuigi Rizzo uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = 0; // hask key 1710f196ce38SLuigi Rizzo 1711f196ce38SLuigi Rizzo b += addr[5] << 8; 1712f196ce38SLuigi Rizzo b += addr[4]; 1713f196ce38SLuigi Rizzo a += addr[3] << 24; 1714f196ce38SLuigi Rizzo a += addr[2] << 16; 1715f196ce38SLuigi Rizzo a += addr[1] << 8; 1716f196ce38SLuigi Rizzo a += addr[0]; 1717f196ce38SLuigi Rizzo 1718f196ce38SLuigi Rizzo mix(a, b, c); 1719f196ce38SLuigi Rizzo #define BRIDGE_RTHASH_MASK (NM_BDG_HASH-1) 1720f196ce38SLuigi Rizzo return (c & BRIDGE_RTHASH_MASK); 1721f196ce38SLuigi Rizzo } 1722f196ce38SLuigi Rizzo 1723f196ce38SLuigi Rizzo #undef mix 1724f196ce38SLuigi Rizzo 1725f196ce38SLuigi Rizzo 1726f196ce38SLuigi Rizzo static int 1727f196ce38SLuigi Rizzo bdg_netmap_reg(struct ifnet *ifp, int onoff) 1728f196ce38SLuigi Rizzo { 1729f196ce38SLuigi Rizzo int i, err = 0; 1730f196ce38SLuigi Rizzo struct nm_bridge *b = ifp->if_bridge; 1731f196ce38SLuigi Rizzo 1732f196ce38SLuigi Rizzo BDG_LOCK(b); 1733f196ce38SLuigi Rizzo if (onoff) { 1734f196ce38SLuigi Rizzo /* the interface must be already in the list. 1735f196ce38SLuigi Rizzo * only need to mark the port as active 1736f196ce38SLuigi Rizzo */ 1737f196ce38SLuigi Rizzo ND("should attach %s to the bridge", ifp->if_xname); 1738f196ce38SLuigi Rizzo for (i=0; i < NM_BDG_MAXPORTS; i++) 1739f196ce38SLuigi Rizzo if (b->bdg_ports[i] == ifp) 1740f196ce38SLuigi Rizzo break; 1741f196ce38SLuigi Rizzo if (i == NM_BDG_MAXPORTS) { 1742f196ce38SLuigi Rizzo D("no more ports available"); 1743f196ce38SLuigi Rizzo err = EINVAL; 1744f196ce38SLuigi Rizzo goto done; 1745f196ce38SLuigi Rizzo } 1746f196ce38SLuigi Rizzo ND("setting %s in netmap mode", ifp->if_xname); 1747f196ce38SLuigi Rizzo ifp->if_capenable |= IFCAP_NETMAP; 1748f196ce38SLuigi Rizzo NA(ifp)->bdg_port = i; 1749f196ce38SLuigi Rizzo b->act_ports |= (1<<i); 1750f196ce38SLuigi Rizzo b->bdg_ports[i] = ifp; 1751f196ce38SLuigi Rizzo } else { 1752f196ce38SLuigi Rizzo /* should be in the list, too -- remove from the mask */ 1753f196ce38SLuigi Rizzo ND("removing %s from netmap mode", ifp->if_xname); 1754f196ce38SLuigi Rizzo ifp->if_capenable &= ~IFCAP_NETMAP; 1755f196ce38SLuigi Rizzo i = NA(ifp)->bdg_port; 1756f196ce38SLuigi Rizzo b->act_ports &= ~(1<<i); 1757f196ce38SLuigi Rizzo } 1758f196ce38SLuigi Rizzo done: 1759f196ce38SLuigi Rizzo BDG_UNLOCK(b); 1760f196ce38SLuigi Rizzo return err; 1761f196ce38SLuigi Rizzo } 1762f196ce38SLuigi Rizzo 1763f196ce38SLuigi Rizzo 1764f196ce38SLuigi Rizzo static int 1765f196ce38SLuigi Rizzo nm_bdg_flush(struct nm_bdg_fwd *ft, int n, struct ifnet *ifp) 1766f196ce38SLuigi Rizzo { 1767f196ce38SLuigi Rizzo int i, ifn; 1768f196ce38SLuigi Rizzo uint64_t all_dst, dst; 1769f196ce38SLuigi Rizzo uint32_t sh, dh; 1770f196ce38SLuigi Rizzo uint64_t mysrc = 1 << NA(ifp)->bdg_port; 1771f196ce38SLuigi Rizzo uint64_t smac, dmac; 1772f196ce38SLuigi Rizzo struct netmap_slot *slot; 1773f196ce38SLuigi Rizzo struct nm_bridge *b = ifp->if_bridge; 1774f196ce38SLuigi Rizzo 1775f196ce38SLuigi Rizzo ND("prepare to send %d packets, act_ports 0x%x", n, b->act_ports); 1776f196ce38SLuigi Rizzo /* only consider valid destinations */ 1777f196ce38SLuigi Rizzo all_dst = (b->act_ports & ~mysrc); 1778f196ce38SLuigi Rizzo /* first pass: hash and find destinations */ 1779f196ce38SLuigi Rizzo for (i = 0; likely(i < n); i++) { 1780f196ce38SLuigi Rizzo uint8_t *buf = ft[i].buf; 1781f196ce38SLuigi Rizzo dmac = le64toh(*(uint64_t *)(buf)) & 0xffffffffffff; 1782f196ce38SLuigi Rizzo smac = le64toh(*(uint64_t *)(buf + 4)); 1783f196ce38SLuigi Rizzo smac >>= 16; 1784f196ce38SLuigi Rizzo if (unlikely(netmap_verbose)) { 1785f196ce38SLuigi Rizzo uint8_t *s = buf+6, *d = buf; 1786f196ce38SLuigi Rizzo D("%d len %4d %02x:%02x:%02x:%02x:%02x:%02x -> %02x:%02x:%02x:%02x:%02x:%02x", 1787f196ce38SLuigi Rizzo i, 1788f196ce38SLuigi Rizzo ft[i].len, 1789f196ce38SLuigi Rizzo s[0], s[1], s[2], s[3], s[4], s[5], 1790f196ce38SLuigi Rizzo d[0], d[1], d[2], d[3], d[4], d[5]); 1791f196ce38SLuigi Rizzo } 1792f196ce38SLuigi Rizzo /* 1793f196ce38SLuigi Rizzo * The hash is somewhat expensive, there might be some 1794f196ce38SLuigi Rizzo * worthwhile optimizations here. 1795f196ce38SLuigi Rizzo */ 1796f196ce38SLuigi Rizzo if ((buf[6] & 1) == 0) { /* valid src */ 1797f196ce38SLuigi Rizzo uint8_t *s = buf+6; 1798f196ce38SLuigi Rizzo sh = nm_bridge_rthash(buf+6); // XXX hash of source 1799f196ce38SLuigi Rizzo /* update source port forwarding entry */ 1800f196ce38SLuigi Rizzo b->ht[sh].mac = smac; /* XXX expire ? */ 1801f196ce38SLuigi Rizzo b->ht[sh].ports = mysrc; 1802f196ce38SLuigi Rizzo if (netmap_verbose) 1803f196ce38SLuigi Rizzo D("src %02x:%02x:%02x:%02x:%02x:%02x on port %d", 1804f196ce38SLuigi Rizzo s[0], s[1], s[2], s[3], s[4], s[5], NA(ifp)->bdg_port); 1805f196ce38SLuigi Rizzo } 1806f196ce38SLuigi Rizzo dst = 0; 1807f196ce38SLuigi Rizzo if ( (buf[0] & 1) == 0) { /* unicast */ 1808f196ce38SLuigi Rizzo uint8_t *d = buf; 1809f196ce38SLuigi Rizzo dh = nm_bridge_rthash(buf); // XXX hash of dst 1810f196ce38SLuigi Rizzo if (b->ht[dh].mac == dmac) { /* found dst */ 1811f196ce38SLuigi Rizzo dst = b->ht[dh].ports; 1812f196ce38SLuigi Rizzo if (netmap_verbose) 1813f196ce38SLuigi Rizzo D("dst %02x:%02x:%02x:%02x:%02x:%02x to port %x", 1814f196ce38SLuigi Rizzo d[0], d[1], d[2], d[3], d[4], d[5], (uint32_t)(dst >> 16)); 1815f196ce38SLuigi Rizzo } 1816f196ce38SLuigi Rizzo } 1817f196ce38SLuigi Rizzo if (dst == 0) 1818f196ce38SLuigi Rizzo dst = all_dst; 1819f196ce38SLuigi Rizzo dst &= all_dst; /* only consider valid ports */ 1820f196ce38SLuigi Rizzo if (unlikely(netmap_verbose)) 1821f196ce38SLuigi Rizzo D("pkt goes to ports 0x%x", (uint32_t)dst); 1822f196ce38SLuigi Rizzo ft[i].dst = dst; 1823f196ce38SLuigi Rizzo } 1824f196ce38SLuigi Rizzo 1825f196ce38SLuigi Rizzo /* second pass, scan interfaces and forward */ 1826f196ce38SLuigi Rizzo all_dst = (b->act_ports & ~mysrc); 1827f196ce38SLuigi Rizzo for (ifn = 0; all_dst; ifn++) { 1828f196ce38SLuigi Rizzo struct ifnet *dst_ifp = b->bdg_ports[ifn]; 1829f196ce38SLuigi Rizzo struct netmap_adapter *na; 1830f196ce38SLuigi Rizzo struct netmap_kring *kring; 1831f196ce38SLuigi Rizzo struct netmap_ring *ring; 1832f196ce38SLuigi Rizzo int j, lim, sent, locked; 1833f196ce38SLuigi Rizzo 1834f196ce38SLuigi Rizzo if (!dst_ifp) 1835f196ce38SLuigi Rizzo continue; 1836f196ce38SLuigi Rizzo ND("scan port %d %s", ifn, dst_ifp->if_xname); 1837f196ce38SLuigi Rizzo dst = 1 << ifn; 1838f196ce38SLuigi Rizzo if ((dst & all_dst) == 0) /* skip if not set */ 1839f196ce38SLuigi Rizzo continue; 1840f196ce38SLuigi Rizzo all_dst &= ~dst; /* clear current node */ 1841f196ce38SLuigi Rizzo na = NA(dst_ifp); 1842f196ce38SLuigi Rizzo 1843f196ce38SLuigi Rizzo ring = NULL; 1844f196ce38SLuigi Rizzo kring = NULL; 1845f196ce38SLuigi Rizzo lim = sent = locked = 0; 1846f196ce38SLuigi Rizzo /* inside, scan slots */ 1847f196ce38SLuigi Rizzo for (i = 0; likely(i < n); i++) { 1848f196ce38SLuigi Rizzo if ((ft[i].dst & dst) == 0) 1849f196ce38SLuigi Rizzo continue; /* not here */ 1850f196ce38SLuigi Rizzo if (!locked) { 1851f196ce38SLuigi Rizzo kring = &na->rx_rings[0]; 1852f196ce38SLuigi Rizzo ring = kring->ring; 1853f196ce38SLuigi Rizzo lim = kring->nkr_num_slots - 1; 1854f196ce38SLuigi Rizzo na->nm_lock(dst_ifp, NETMAP_RX_LOCK, 0); 1855f196ce38SLuigi Rizzo locked = 1; 1856f196ce38SLuigi Rizzo } 1857f196ce38SLuigi Rizzo if (unlikely(kring->nr_hwavail >= lim)) { 1858f196ce38SLuigi Rizzo if (netmap_verbose) 1859f196ce38SLuigi Rizzo D("rx ring full on %s", ifp->if_xname); 1860f196ce38SLuigi Rizzo break; 1861f196ce38SLuigi Rizzo } 1862f196ce38SLuigi Rizzo j = kring->nr_hwcur + kring->nr_hwavail; 1863f196ce38SLuigi Rizzo if (j > lim) 1864f196ce38SLuigi Rizzo j -= kring->nkr_num_slots; 1865f196ce38SLuigi Rizzo slot = &ring->slot[j]; 1866f196ce38SLuigi Rizzo ND("send %d %d bytes at %s:%d", i, ft[i].len, dst_ifp->if_xname, j); 1867f196ce38SLuigi Rizzo pkt_copy(ft[i].buf, NMB(slot), ft[i].len); 1868f196ce38SLuigi Rizzo slot->len = ft[i].len; 1869f196ce38SLuigi Rizzo kring->nr_hwavail++; 1870f196ce38SLuigi Rizzo sent++; 1871f196ce38SLuigi Rizzo } 1872f196ce38SLuigi Rizzo if (locked) { 1873f196ce38SLuigi Rizzo ND("sent %d on %s", sent, dst_ifp->if_xname); 1874f196ce38SLuigi Rizzo if (sent) 1875f196ce38SLuigi Rizzo selwakeuppri(&kring->si, PI_NET); 1876f196ce38SLuigi Rizzo na->nm_lock(dst_ifp, NETMAP_RX_UNLOCK, 0); 1877f196ce38SLuigi Rizzo } 1878f196ce38SLuigi Rizzo } 1879f196ce38SLuigi Rizzo return 0; 1880f196ce38SLuigi Rizzo } 1881f196ce38SLuigi Rizzo 1882f196ce38SLuigi Rizzo /* 1883f196ce38SLuigi Rizzo * main dispatch routine 1884f196ce38SLuigi Rizzo */ 1885f196ce38SLuigi Rizzo static int 1886f196ce38SLuigi Rizzo bdg_netmap_txsync(struct ifnet *ifp, u_int ring_nr, int do_lock) 1887f196ce38SLuigi Rizzo { 1888f196ce38SLuigi Rizzo struct netmap_adapter *na = NA(ifp); 1889f196ce38SLuigi Rizzo struct netmap_kring *kring = &na->tx_rings[ring_nr]; 1890f196ce38SLuigi Rizzo struct netmap_ring *ring = kring->ring; 1891f196ce38SLuigi Rizzo int i, j, k, lim = kring->nkr_num_slots - 1; 1892f196ce38SLuigi Rizzo struct nm_bdg_fwd *ft = (struct nm_bdg_fwd *)(ifp + 1); 1893f196ce38SLuigi Rizzo int ft_i; /* position in the forwarding table */ 1894f196ce38SLuigi Rizzo 1895f196ce38SLuigi Rizzo k = ring->cur; 1896f196ce38SLuigi Rizzo if (k > lim) 1897f196ce38SLuigi Rizzo return netmap_ring_reinit(kring); 1898f196ce38SLuigi Rizzo if (do_lock) 1899f196ce38SLuigi Rizzo na->nm_lock(ifp, NETMAP_TX_LOCK, ring_nr); 1900f196ce38SLuigi Rizzo 1901f196ce38SLuigi Rizzo if (netmap_bridge <= 0) { /* testing only */ 1902f196ce38SLuigi Rizzo j = k; // used all 1903f196ce38SLuigi Rizzo goto done; 1904f196ce38SLuigi Rizzo } 1905f196ce38SLuigi Rizzo if (netmap_bridge > NM_BDG_BATCH) 1906f196ce38SLuigi Rizzo netmap_bridge = NM_BDG_BATCH; 1907f196ce38SLuigi Rizzo 1908f196ce38SLuigi Rizzo ft_i = 0; /* start from 0 */ 1909f196ce38SLuigi Rizzo for (j = kring->nr_hwcur; likely(j != k); j = unlikely(j == lim) ? 0 : j+1) { 1910f196ce38SLuigi Rizzo struct netmap_slot *slot = &ring->slot[j]; 1911f196ce38SLuigi Rizzo int len = ft[ft_i].len = slot->len; 1912f196ce38SLuigi Rizzo char *buf = ft[ft_i].buf = NMB(slot); 1913f196ce38SLuigi Rizzo 1914f196ce38SLuigi Rizzo prefetch(buf); 1915f196ce38SLuigi Rizzo if (unlikely(len < 14)) 1916f196ce38SLuigi Rizzo continue; 1917f196ce38SLuigi Rizzo if (unlikely(++ft_i == netmap_bridge)) 1918f196ce38SLuigi Rizzo ft_i = nm_bdg_flush(ft, ft_i, ifp); 1919f196ce38SLuigi Rizzo } 1920f196ce38SLuigi Rizzo if (ft_i) 1921f196ce38SLuigi Rizzo ft_i = nm_bdg_flush(ft, ft_i, ifp); 1922f196ce38SLuigi Rizzo /* count how many packets we sent */ 1923f196ce38SLuigi Rizzo i = k - j; 1924f196ce38SLuigi Rizzo if (i < 0) 1925f196ce38SLuigi Rizzo i += kring->nkr_num_slots; 1926f196ce38SLuigi Rizzo kring->nr_hwavail = kring->nkr_num_slots - 1 - i; 1927f196ce38SLuigi Rizzo if (j != k) 1928f196ce38SLuigi Rizzo D("early break at %d/ %d, avail %d", j, k, kring->nr_hwavail); 1929f196ce38SLuigi Rizzo 1930f196ce38SLuigi Rizzo done: 1931f196ce38SLuigi Rizzo kring->nr_hwcur = j; 1932f196ce38SLuigi Rizzo ring->avail = kring->nr_hwavail; 1933f196ce38SLuigi Rizzo if (do_lock) 1934f196ce38SLuigi Rizzo na->nm_lock(ifp, NETMAP_TX_UNLOCK, ring_nr); 1935f196ce38SLuigi Rizzo 1936f196ce38SLuigi Rizzo if (netmap_verbose) 1937f196ce38SLuigi Rizzo D("%s ring %d lock %d", ifp->if_xname, ring_nr, do_lock); 1938f196ce38SLuigi Rizzo return 0; 1939f196ce38SLuigi Rizzo } 1940f196ce38SLuigi Rizzo 1941f196ce38SLuigi Rizzo static int 1942f196ce38SLuigi Rizzo bdg_netmap_rxsync(struct ifnet *ifp, u_int ring_nr, int do_lock) 1943f196ce38SLuigi Rizzo { 1944f196ce38SLuigi Rizzo struct netmap_adapter *na = NA(ifp); 1945f196ce38SLuigi Rizzo struct netmap_kring *kring = &na->rx_rings[ring_nr]; 1946f196ce38SLuigi Rizzo struct netmap_ring *ring = kring->ring; 1947f196ce38SLuigi Rizzo int j, n, lim = kring->nkr_num_slots - 1; 1948f196ce38SLuigi Rizzo u_int k = ring->cur, resvd = ring->reserved; 1949f196ce38SLuigi Rizzo 1950f196ce38SLuigi Rizzo ND("%s ring %d lock %d avail %d", 1951f196ce38SLuigi Rizzo ifp->if_xname, ring_nr, do_lock, kring->nr_hwavail); 1952f196ce38SLuigi Rizzo 1953f196ce38SLuigi Rizzo if (k > lim) 1954f196ce38SLuigi Rizzo return netmap_ring_reinit(kring); 1955f196ce38SLuigi Rizzo if (do_lock) 1956f196ce38SLuigi Rizzo na->nm_lock(ifp, NETMAP_RX_LOCK, ring_nr); 1957f196ce38SLuigi Rizzo 1958f196ce38SLuigi Rizzo /* skip past packets that userspace has released */ 1959f196ce38SLuigi Rizzo j = kring->nr_hwcur; /* netmap ring index */ 1960f196ce38SLuigi Rizzo if (resvd > 0) { 1961f196ce38SLuigi Rizzo if (resvd + ring->avail >= lim + 1) { 1962f196ce38SLuigi Rizzo D("XXX invalid reserve/avail %d %d", resvd, ring->avail); 1963f196ce38SLuigi Rizzo ring->reserved = resvd = 0; // XXX panic... 1964f196ce38SLuigi Rizzo } 1965f196ce38SLuigi Rizzo k = (k >= resvd) ? k - resvd : k + lim + 1 - resvd; 1966f196ce38SLuigi Rizzo } 1967f196ce38SLuigi Rizzo 1968f196ce38SLuigi Rizzo if (j != k) { /* userspace has released some packets. */ 1969f196ce38SLuigi Rizzo n = k - j; 1970f196ce38SLuigi Rizzo if (n < 0) 1971f196ce38SLuigi Rizzo n += kring->nkr_num_slots; 1972f196ce38SLuigi Rizzo ND("userspace releases %d packets", n); 1973f196ce38SLuigi Rizzo for (n = 0; likely(j != k); n++) { 1974f196ce38SLuigi Rizzo struct netmap_slot *slot = &ring->slot[j]; 1975f196ce38SLuigi Rizzo void *addr = NMB(slot); 1976f196ce38SLuigi Rizzo 1977f196ce38SLuigi Rizzo if (addr == netmap_buffer_base) { /* bad buf */ 1978f196ce38SLuigi Rizzo if (do_lock) 1979f196ce38SLuigi Rizzo na->nm_lock(ifp, NETMAP_RX_UNLOCK, ring_nr); 1980f196ce38SLuigi Rizzo return netmap_ring_reinit(kring); 1981f196ce38SLuigi Rizzo } 1982f196ce38SLuigi Rizzo /* decrease refcount for buffer */ 1983f196ce38SLuigi Rizzo 1984f196ce38SLuigi Rizzo slot->flags &= ~NS_BUF_CHANGED; 1985f196ce38SLuigi Rizzo j = unlikely(j == lim) ? 0 : j + 1; 1986f196ce38SLuigi Rizzo } 1987f196ce38SLuigi Rizzo kring->nr_hwavail -= n; 1988f196ce38SLuigi Rizzo kring->nr_hwcur = k; 1989f196ce38SLuigi Rizzo } 1990f196ce38SLuigi Rizzo /* tell userspace that there are new packets */ 1991f196ce38SLuigi Rizzo ring->avail = kring->nr_hwavail - resvd; 1992f196ce38SLuigi Rizzo 1993f196ce38SLuigi Rizzo if (do_lock) 1994f196ce38SLuigi Rizzo na->nm_lock(ifp, NETMAP_RX_UNLOCK, ring_nr); 1995f196ce38SLuigi Rizzo return 0; 1996f196ce38SLuigi Rizzo } 1997f196ce38SLuigi Rizzo 1998f196ce38SLuigi Rizzo static void 1999f196ce38SLuigi Rizzo bdg_netmap_attach(struct ifnet *ifp) 2000f196ce38SLuigi Rizzo { 2001f196ce38SLuigi Rizzo struct netmap_adapter na; 2002f196ce38SLuigi Rizzo 2003f196ce38SLuigi Rizzo ND("attaching virtual bridge"); 2004f196ce38SLuigi Rizzo bzero(&na, sizeof(na)); 2005f196ce38SLuigi Rizzo 2006f196ce38SLuigi Rizzo na.ifp = ifp; 2007f196ce38SLuigi Rizzo na.separate_locks = 1; 2008f196ce38SLuigi Rizzo na.num_tx_desc = NM_BRIDGE_RINGSIZE; 2009f196ce38SLuigi Rizzo na.num_rx_desc = NM_BRIDGE_RINGSIZE; 2010f196ce38SLuigi Rizzo na.nm_txsync = bdg_netmap_txsync; 2011f196ce38SLuigi Rizzo na.nm_rxsync = bdg_netmap_rxsync; 2012f196ce38SLuigi Rizzo na.nm_register = bdg_netmap_reg; 2013f196ce38SLuigi Rizzo netmap_attach(&na, 1); 2014f196ce38SLuigi Rizzo } 2015f196ce38SLuigi Rizzo 2016f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */ 2017babc7c12SLuigi Rizzo 2018babc7c12SLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */ 2019babc7c12SLuigi Rizzo 2020babc7c12SLuigi Rizzo 20211a26580eSLuigi Rizzo /* 202268b8534bSLuigi Rizzo * Module loader. 202368b8534bSLuigi Rizzo * 202468b8534bSLuigi Rizzo * Create the /dev/netmap device and initialize all global 202568b8534bSLuigi Rizzo * variables. 202668b8534bSLuigi Rizzo * 202768b8534bSLuigi Rizzo * Return 0 on success, errno on failure. 202868b8534bSLuigi Rizzo */ 202968b8534bSLuigi Rizzo static int 203068b8534bSLuigi Rizzo netmap_init(void) 203168b8534bSLuigi Rizzo { 203268b8534bSLuigi Rizzo int error; 203368b8534bSLuigi Rizzo 203468b8534bSLuigi Rizzo error = netmap_memory_init(); 203568b8534bSLuigi Rizzo if (error != 0) { 203668b8534bSLuigi Rizzo printf("netmap: unable to initialize the memory allocator."); 203768b8534bSLuigi Rizzo return (error); 203868b8534bSLuigi Rizzo } 203968b8534bSLuigi Rizzo printf("netmap: loaded module with %d Mbytes\n", 204064ae02c3SLuigi Rizzo (int)(nm_mem->nm_totalsize >> 20)); 204168b8534bSLuigi Rizzo netmap_dev = make_dev(&netmap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660, 204268b8534bSLuigi Rizzo "netmap"); 2043f196ce38SLuigi Rizzo 2044f196ce38SLuigi Rizzo #ifdef NM_BRIDGE 2045f196ce38SLuigi Rizzo { 2046f196ce38SLuigi Rizzo int i; 2047f196ce38SLuigi Rizzo for (i = 0; i < NM_BRIDGES; i++) 2048f196ce38SLuigi Rizzo mtx_init(&nm_bridges[i].bdg_lock, "bdg lock", "bdg_lock", MTX_DEF); 2049f196ce38SLuigi Rizzo } 2050f196ce38SLuigi Rizzo #endif 2051babc7c12SLuigi Rizzo return (error); 205268b8534bSLuigi Rizzo } 205368b8534bSLuigi Rizzo 205468b8534bSLuigi Rizzo 205568b8534bSLuigi Rizzo /* 205668b8534bSLuigi Rizzo * Module unloader. 205768b8534bSLuigi Rizzo * 205868b8534bSLuigi Rizzo * Free all the memory, and destroy the ``/dev/netmap`` device. 205968b8534bSLuigi Rizzo */ 206068b8534bSLuigi Rizzo static void 206168b8534bSLuigi Rizzo netmap_fini(void) 206268b8534bSLuigi Rizzo { 206368b8534bSLuigi Rizzo destroy_dev(netmap_dev); 206468b8534bSLuigi Rizzo netmap_memory_fini(); 206568b8534bSLuigi Rizzo printf("netmap: unloaded module.\n"); 206668b8534bSLuigi Rizzo } 206768b8534bSLuigi Rizzo 206868b8534bSLuigi Rizzo 2069f196ce38SLuigi Rizzo #ifdef __FreeBSD__ 207068b8534bSLuigi Rizzo /* 207168b8534bSLuigi Rizzo * Kernel entry point. 207268b8534bSLuigi Rizzo * 207368b8534bSLuigi Rizzo * Initialize/finalize the module and return. 207468b8534bSLuigi Rizzo * 207568b8534bSLuigi Rizzo * Return 0 on success, errno on failure. 207668b8534bSLuigi Rizzo */ 207768b8534bSLuigi Rizzo static int 207868b8534bSLuigi Rizzo netmap_loader(__unused struct module *module, int event, __unused void *arg) 207968b8534bSLuigi Rizzo { 208068b8534bSLuigi Rizzo int error = 0; 208168b8534bSLuigi Rizzo 208268b8534bSLuigi Rizzo switch (event) { 208368b8534bSLuigi Rizzo case MOD_LOAD: 208468b8534bSLuigi Rizzo error = netmap_init(); 208568b8534bSLuigi Rizzo break; 208668b8534bSLuigi Rizzo 208768b8534bSLuigi Rizzo case MOD_UNLOAD: 208868b8534bSLuigi Rizzo netmap_fini(); 208968b8534bSLuigi Rizzo break; 209068b8534bSLuigi Rizzo 209168b8534bSLuigi Rizzo default: 209268b8534bSLuigi Rizzo error = EOPNOTSUPP; 209368b8534bSLuigi Rizzo break; 209468b8534bSLuigi Rizzo } 209568b8534bSLuigi Rizzo 209668b8534bSLuigi Rizzo return (error); 209768b8534bSLuigi Rizzo } 209868b8534bSLuigi Rizzo 209968b8534bSLuigi Rizzo 210068b8534bSLuigi Rizzo DEV_MODULE(netmap, netmap_loader, NULL); 2101f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */ 2102