168b8534bSLuigi Rizzo /* 2849bec0eSLuigi Rizzo * Copyright (C) 2011-2013 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" 5942a3a5bdSLuigi Rizzo static netdev_tx_t linux_netmap_start(struct sk_buff *skb, struct net_device *dev); 60f196ce38SLuigi Rizzo #endif /* linux */ 6101c7d25fSLuigi Rizzo 62f196ce38SLuigi Rizzo #ifdef __APPLE__ 63f196ce38SLuigi Rizzo #include "osx_glue.h" 6401c7d25fSLuigi Rizzo #endif /* __APPLE__ */ 6501c7d25fSLuigi 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> 8489f6b863SAttilio Rao #include <sys/rwlock.h> 8568b8534bSLuigi Rizzo #include <vm/vm.h> /* vtophys */ 8668b8534bSLuigi Rizzo #include <vm/pmap.h> /* vtophys */ 8768b8534bSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */ 8868b8534bSLuigi Rizzo #include <machine/bus.h> 8968b8534bSLuigi Rizzo #include <sys/selinfo.h> 9068b8534bSLuigi Rizzo #include <sys/sysctl.h> 9168b8534bSLuigi Rizzo #include <net/if.h> 92*76039bc8SGleb Smirnoff #include <net/if_var.h> 9368b8534bSLuigi Rizzo #include <net/bpf.h> /* BIOCIMMEDIATE */ 94506cc70cSLuigi Rizzo #include <net/vnet.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 1000b8ed8e0SLuigi Rizzo #include <net/netmap.h> 1010b8ed8e0SLuigi Rizzo #include <dev/netmap/netmap_kern.h> 1020b8ed8e0SLuigi Rizzo 103d4b42e08SLuigi Rizzo /* XXX the following variables must be deprecated and included in nm_mem */ 1045819da83SLuigi Rizzo u_int netmap_total_buffers; 1058241616dSLuigi Rizzo u_int netmap_buf_size; 1065819da83SLuigi Rizzo char *netmap_buffer_base; /* address of an invalid buffer */ 1075819da83SLuigi Rizzo 1085819da83SLuigi Rizzo /* user-controlled variables */ 1095819da83SLuigi Rizzo int netmap_verbose; 1105819da83SLuigi Rizzo 1115819da83SLuigi Rizzo static int netmap_no_timestamp; /* don't timestamp on rxsync */ 1125819da83SLuigi Rizzo 1135819da83SLuigi Rizzo SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args"); 1145819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose, 1155819da83SLuigi Rizzo CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode"); 1165819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp, 1175819da83SLuigi Rizzo CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp"); 1185819da83SLuigi Rizzo int netmap_mitigate = 1; 1195819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mitigate, CTLFLAG_RW, &netmap_mitigate, 0, ""); 120c85cb1a0SLuigi Rizzo int netmap_no_pendintr = 1; 1215819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr, 1225819da83SLuigi Rizzo CTLFLAG_RW, &netmap_no_pendintr, 0, "Always look for new received packets."); 123f18be576SLuigi Rizzo int netmap_txsync_retry = 2; 124f18be576SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, txsync_retry, CTLFLAG_RW, 125f18be576SLuigi Rizzo &netmap_txsync_retry, 0 , "Number of txsync loops in bridge's flush."); 1265819da83SLuigi Rizzo 127f196ce38SLuigi Rizzo int netmap_drop = 0; /* debugging */ 128f196ce38SLuigi Rizzo int netmap_flags = 0; /* debug flags */ 129091fd0abSLuigi Rizzo int netmap_fwd = 0; /* force transparent mode */ 130f196ce38SLuigi Rizzo 131f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, drop, CTLFLAG_RW, &netmap_drop, 0 , ""); 132f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, flags, CTLFLAG_RW, &netmap_flags, 0 , ""); 133091fd0abSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, fwd, CTLFLAG_RW, &netmap_fwd, 0 , ""); 134f196ce38SLuigi Rizzo 135f18be576SLuigi Rizzo #ifdef NM_BRIDGE /* support for netmap virtual switch, called VALE */ 136f196ce38SLuigi Rizzo 137f196ce38SLuigi Rizzo /* 138f18be576SLuigi Rizzo * system parameters (most of them in netmap_kern.h) 139f18be576SLuigi Rizzo * NM_NAME prefix for switch port names, default "vale" 140f18be576SLuigi Rizzo * NM_MAXPORTS number of ports 141f18be576SLuigi Rizzo * NM_BRIDGES max number of switches in the system. 142f18be576SLuigi Rizzo * XXX should become a sysctl or tunable 143f196ce38SLuigi Rizzo * 144f18be576SLuigi Rizzo * Switch ports are named valeX:Y where X is the switch name and Y 145f18be576SLuigi Rizzo * is the port. If Y matches a physical interface name, the port is 146f18be576SLuigi Rizzo * connected to a physical device. 147f18be576SLuigi Rizzo * 148f18be576SLuigi Rizzo * Unlike physical interfaces, switch ports use their own memory region 149f18be576SLuigi Rizzo * for rings and buffers. 150f196ce38SLuigi Rizzo * The virtual interfaces use per-queue lock instead of core lock. 151f196ce38SLuigi Rizzo * In the tx loop, we aggregate traffic in batches to make all operations 152f196ce38SLuigi Rizzo * faster. The batch size is NM_BDG_BATCH 153f196ce38SLuigi Rizzo */ 154f18be576SLuigi Rizzo #define NM_BDG_MAXRINGS 16 /* XXX unclear how many. */ 155f196ce38SLuigi Rizzo #define NM_BRIDGE_RINGSIZE 1024 /* in the device */ 156f196ce38SLuigi Rizzo #define NM_BDG_HASH 1024 /* forwarding table entries */ 157f196ce38SLuigi Rizzo #define NM_BDG_BATCH 1024 /* entries in the forwarding buffer */ 158f18be576SLuigi Rizzo #define NM_BRIDGES 8 /* number of bridges */ 159d4b42e08SLuigi Rizzo 160d4b42e08SLuigi Rizzo 161f196ce38SLuigi Rizzo int netmap_bridge = NM_BDG_BATCH; /* bridge batch size */ 162f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, bridge, CTLFLAG_RW, &netmap_bridge, 0 , ""); 16301c7d25fSLuigi Rizzo 164f196ce38SLuigi Rizzo #ifdef linux 165849bec0eSLuigi Rizzo 166849bec0eSLuigi Rizzo #define refcount_acquire(_a) atomic_add(1, (atomic_t *)_a) 167849bec0eSLuigi Rizzo #define refcount_release(_a) atomic_dec_and_test((atomic_t *)_a) 168849bec0eSLuigi Rizzo 169f196ce38SLuigi Rizzo #else /* !linux */ 170849bec0eSLuigi Rizzo 171f196ce38SLuigi Rizzo #ifdef __FreeBSD__ 172f196ce38SLuigi Rizzo #include <sys/endian.h> 173f196ce38SLuigi Rizzo #include <sys/refcount.h> 174f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */ 175849bec0eSLuigi Rizzo 17601c7d25fSLuigi Rizzo #define prefetch(x) __builtin_prefetch(x) 177849bec0eSLuigi Rizzo 178f196ce38SLuigi Rizzo #endif /* !linux */ 179f196ce38SLuigi Rizzo 180849bec0eSLuigi Rizzo /* 181849bec0eSLuigi Rizzo * These are used to handle reference counters for bridge ports. 182849bec0eSLuigi Rizzo */ 183849bec0eSLuigi Rizzo #define ADD_BDG_REF(ifp) refcount_acquire(&NA(ifp)->na_bdg_refcount) 184849bec0eSLuigi Rizzo #define DROP_BDG_REF(ifp) refcount_release(&NA(ifp)->na_bdg_refcount) 185849bec0eSLuigi Rizzo 186f18be576SLuigi Rizzo static void bdg_netmap_attach(struct netmap_adapter *); 187f196ce38SLuigi Rizzo static int bdg_netmap_reg(struct ifnet *ifp, int onoff); 188f18be576SLuigi Rizzo static int kern_netmap_regif(struct nmreq *nmr); 189f18be576SLuigi Rizzo 190f196ce38SLuigi Rizzo /* per-tx-queue entry */ 191f196ce38SLuigi Rizzo struct nm_bdg_fwd { /* forwarding entry for a bridge */ 19285233a7dSLuigi Rizzo void *ft_buf; 19385233a7dSLuigi Rizzo uint16_t _ft_dst; /* dst port, unused */ 19485233a7dSLuigi Rizzo uint16_t ft_flags; /* flags, e.g. indirect */ 195f18be576SLuigi Rizzo uint16_t ft_len; /* src len */ 196f18be576SLuigi Rizzo uint16_t ft_next; /* next packet to same destination */ 197f18be576SLuigi Rizzo }; 198f18be576SLuigi Rizzo 199f18be576SLuigi Rizzo /* We need to build a list of buffers going to each destination. 200f18be576SLuigi Rizzo * Each buffer is in one entry of struct nm_bdg_fwd, we use ft_next 201f18be576SLuigi Rizzo * to build the list, and struct nm_bdg_q below for the queue. 202f18be576SLuigi Rizzo * The structure should compact because potentially we have a lot 203f18be576SLuigi Rizzo * of destinations. 204f18be576SLuigi Rizzo */ 205f18be576SLuigi Rizzo struct nm_bdg_q { 206f18be576SLuigi Rizzo uint16_t bq_head; 207f18be576SLuigi Rizzo uint16_t bq_tail; 208f196ce38SLuigi Rizzo }; 209f196ce38SLuigi Rizzo 210f196ce38SLuigi Rizzo struct nm_hash_ent { 211f196ce38SLuigi Rizzo uint64_t mac; /* the top 2 bytes are the epoch */ 212f196ce38SLuigi Rizzo uint64_t ports; 213f196ce38SLuigi Rizzo }; 214f196ce38SLuigi Rizzo 215f196ce38SLuigi Rizzo /* 216849bec0eSLuigi Rizzo * Interfaces for a bridge are all in bdg_ports[]. 217f196ce38SLuigi Rizzo * The array has fixed size, an empty entry does not terminate 218849bec0eSLuigi Rizzo * the search. But lookups only occur on attach/detach so we 219849bec0eSLuigi Rizzo * don't mind if they are slow. 220849bec0eSLuigi Rizzo * 221849bec0eSLuigi Rizzo * The bridge is non blocking on the transmit ports. 222849bec0eSLuigi Rizzo * 223849bec0eSLuigi Rizzo * bdg_lock protects accesses to the bdg_ports array. 224f18be576SLuigi Rizzo * This is a rw lock (or equivalent). 225f196ce38SLuigi Rizzo */ 226f196ce38SLuigi Rizzo struct nm_bridge { 227f18be576SLuigi Rizzo int namelen; /* 0 means free */ 228f18be576SLuigi Rizzo 229f18be576SLuigi Rizzo /* XXX what is the proper alignment/layout ? */ 230f18be576SLuigi Rizzo NM_RWLOCK_T bdg_lock; /* protects bdg_ports */ 231f18be576SLuigi Rizzo struct netmap_adapter *bdg_ports[NM_BDG_MAXPORTS]; 232f18be576SLuigi Rizzo 233f18be576SLuigi Rizzo char basename[IFNAMSIZ]; 234f18be576SLuigi Rizzo /* 235f18be576SLuigi Rizzo * The function to decide the destination port. 236f18be576SLuigi Rizzo * It returns either of an index of the destination port, 237f18be576SLuigi Rizzo * NM_BDG_BROADCAST to broadcast this packet, or NM_BDG_NOPORT not to 238f18be576SLuigi Rizzo * forward this packet. ring_nr is the source ring index, and the 239f18be576SLuigi Rizzo * function may overwrite this value to forward this packet to a 240f18be576SLuigi Rizzo * different ring index. 241f18be576SLuigi Rizzo * This function must be set by netmap_bdgctl(). 242f18be576SLuigi Rizzo */ 243f18be576SLuigi Rizzo bdg_lookup_fn_t nm_bdg_lookup; 244f196ce38SLuigi Rizzo 245f196ce38SLuigi Rizzo /* the forwarding table, MAC+ports */ 246f196ce38SLuigi Rizzo struct nm_hash_ent ht[NM_BDG_HASH]; 247f196ce38SLuigi Rizzo }; 248f196ce38SLuigi Rizzo 249f196ce38SLuigi Rizzo struct nm_bridge nm_bridges[NM_BRIDGES]; 250f18be576SLuigi Rizzo NM_LOCK_T netmap_bridge_mutex; 251f196ce38SLuigi Rizzo 252f18be576SLuigi Rizzo /* other OS will have these macros defined in their own glue code. */ 253f18be576SLuigi Rizzo 254f18be576SLuigi Rizzo #ifdef __FreeBSD__ 255f18be576SLuigi Rizzo #define BDG_LOCK() mtx_lock(&netmap_bridge_mutex) 256f18be576SLuigi Rizzo #define BDG_UNLOCK() mtx_unlock(&netmap_bridge_mutex) 257f18be576SLuigi Rizzo #define BDG_WLOCK(b) rw_wlock(&(b)->bdg_lock) 258f18be576SLuigi Rizzo #define BDG_WUNLOCK(b) rw_wunlock(&(b)->bdg_lock) 259f18be576SLuigi Rizzo #define BDG_RLOCK(b) rw_rlock(&(b)->bdg_lock) 260f18be576SLuigi Rizzo #define BDG_RUNLOCK(b) rw_runlock(&(b)->bdg_lock) 261f18be576SLuigi Rizzo 262f18be576SLuigi Rizzo /* set/get variables. OS-specific macros may wrap these 263f18be576SLuigi Rizzo * assignments into read/write lock or similar 264f18be576SLuigi Rizzo */ 265f18be576SLuigi Rizzo #define BDG_SET_VAR(lval, p) (lval = p) 266f18be576SLuigi Rizzo #define BDG_GET_VAR(lval) (lval) 267f18be576SLuigi Rizzo #define BDG_FREE(p) free(p, M_DEVBUF) 268f18be576SLuigi Rizzo #endif /* __FreeBSD__ */ 269f18be576SLuigi Rizzo 270f18be576SLuigi Rizzo static __inline int 271f18be576SLuigi Rizzo nma_is_vp(struct netmap_adapter *na) 272f18be576SLuigi Rizzo { 273f18be576SLuigi Rizzo return na->nm_register == bdg_netmap_reg; 274f18be576SLuigi Rizzo } 275f18be576SLuigi Rizzo static __inline int 276f18be576SLuigi Rizzo nma_is_host(struct netmap_adapter *na) 277f18be576SLuigi Rizzo { 278f18be576SLuigi Rizzo return na->nm_register == NULL; 279f18be576SLuigi Rizzo } 280f18be576SLuigi Rizzo static __inline int 281f18be576SLuigi Rizzo nma_is_hw(struct netmap_adapter *na) 282f18be576SLuigi Rizzo { 283f18be576SLuigi Rizzo /* In case of sw adapter, nm_register is NULL */ 284f18be576SLuigi Rizzo return !nma_is_vp(na) && !nma_is_host(na); 285f18be576SLuigi Rizzo } 286f18be576SLuigi Rizzo 287f18be576SLuigi Rizzo /* 288f18be576SLuigi Rizzo * Regarding holding a NIC, if the NIC is owned by the kernel 289f18be576SLuigi Rizzo * (i.e., bridge), neither another bridge nor user can use it; 290f18be576SLuigi Rizzo * if the NIC is owned by a user, only users can share it. 291f18be576SLuigi Rizzo * Evaluation must be done under NMA_LOCK(). 292f18be576SLuigi Rizzo */ 293f18be576SLuigi Rizzo #define NETMAP_OWNED_BY_KERN(ifp) (!nma_is_vp(NA(ifp)) && NA(ifp)->na_bdg) 294f18be576SLuigi Rizzo #define NETMAP_OWNED_BY_ANY(ifp) \ 295f18be576SLuigi Rizzo (NETMAP_OWNED_BY_KERN(ifp) || (NA(ifp)->refcount > 0)) 296f196ce38SLuigi Rizzo 297f196ce38SLuigi Rizzo /* 298f196ce38SLuigi Rizzo * NA(ifp)->bdg_port port index 299f196ce38SLuigi Rizzo */ 300f196ce38SLuigi Rizzo 301f196ce38SLuigi Rizzo // XXX only for multiples of 64 bytes, non overlapped. 302f196ce38SLuigi Rizzo static inline void 303f196ce38SLuigi Rizzo pkt_copy(void *_src, void *_dst, int l) 304f196ce38SLuigi Rizzo { 305f196ce38SLuigi Rizzo uint64_t *src = _src; 306f196ce38SLuigi Rizzo uint64_t *dst = _dst; 307f196ce38SLuigi Rizzo if (unlikely(l >= 1024)) { 308f196ce38SLuigi Rizzo bcopy(src, dst, l); 309f196ce38SLuigi Rizzo return; 310f196ce38SLuigi Rizzo } 311f196ce38SLuigi Rizzo for (; likely(l > 0); l-=64) { 312f196ce38SLuigi Rizzo *dst++ = *src++; 313f196ce38SLuigi Rizzo *dst++ = *src++; 314f196ce38SLuigi Rizzo *dst++ = *src++; 315f196ce38SLuigi Rizzo *dst++ = *src++; 316f196ce38SLuigi Rizzo *dst++ = *src++; 317f196ce38SLuigi Rizzo *dst++ = *src++; 318f196ce38SLuigi Rizzo *dst++ = *src++; 319f196ce38SLuigi Rizzo *dst++ = *src++; 320f196ce38SLuigi Rizzo } 321f196ce38SLuigi Rizzo } 322f196ce38SLuigi Rizzo 323f18be576SLuigi Rizzo 324f196ce38SLuigi Rizzo /* 325f196ce38SLuigi Rizzo * locate a bridge among the existing ones. 326f196ce38SLuigi Rizzo * a ':' in the name terminates the bridge name. Otherwise, just NM_NAME. 327f196ce38SLuigi Rizzo * We assume that this is called with a name of at least NM_NAME chars. 328f196ce38SLuigi Rizzo */ 329f196ce38SLuigi Rizzo static struct nm_bridge * 330f18be576SLuigi Rizzo nm_find_bridge(const char *name, int create) 331f196ce38SLuigi Rizzo { 332f18be576SLuigi Rizzo int i, l, namelen; 333f196ce38SLuigi Rizzo struct nm_bridge *b = NULL; 334f196ce38SLuigi Rizzo 335f196ce38SLuigi Rizzo namelen = strlen(NM_NAME); /* base length */ 336f196ce38SLuigi Rizzo l = strlen(name); /* actual length */ 337f196ce38SLuigi Rizzo for (i = namelen + 1; i < l; i++) { 338f196ce38SLuigi Rizzo if (name[i] == ':') { 339f196ce38SLuigi Rizzo namelen = i; 340f196ce38SLuigi Rizzo break; 341f196ce38SLuigi Rizzo } 342f196ce38SLuigi Rizzo } 343f196ce38SLuigi Rizzo if (namelen >= IFNAMSIZ) 344f196ce38SLuigi Rizzo namelen = IFNAMSIZ; 345f196ce38SLuigi Rizzo ND("--- prefix is '%.*s' ---", namelen, name); 346f196ce38SLuigi Rizzo 347f18be576SLuigi Rizzo BDG_LOCK(); 348f18be576SLuigi Rizzo /* lookup the name, remember empty slot if there is one */ 349f18be576SLuigi Rizzo for (i = 0; i < NM_BRIDGES; i++) { 350f18be576SLuigi Rizzo struct nm_bridge *x = nm_bridges + i; 351f18be576SLuigi Rizzo 352f18be576SLuigi Rizzo if (x->namelen == 0) { 353f18be576SLuigi Rizzo if (create && b == NULL) 354f18be576SLuigi Rizzo b = x; /* record empty slot */ 355f18be576SLuigi Rizzo } else if (x->namelen != namelen) { 356f18be576SLuigi Rizzo continue; 357f18be576SLuigi Rizzo } else if (strncmp(name, x->basename, namelen) == 0) { 358f196ce38SLuigi Rizzo ND("found '%.*s' at %d", namelen, name, i); 359f18be576SLuigi Rizzo b = x; 360f196ce38SLuigi Rizzo break; 361f196ce38SLuigi Rizzo } 362f196ce38SLuigi Rizzo } 363f18be576SLuigi Rizzo if (i == NM_BRIDGES && b) { /* name not found, can create entry */ 364f196ce38SLuigi Rizzo strncpy(b->basename, name, namelen); 365f196ce38SLuigi Rizzo b->namelen = namelen; 366f18be576SLuigi Rizzo /* set the default function */ 367f18be576SLuigi Rizzo b->nm_bdg_lookup = netmap_bdg_learning; 368f18be576SLuigi Rizzo /* reset the MAC address table */ 369f18be576SLuigi Rizzo bzero(b->ht, sizeof(struct nm_hash_ent) * NM_BDG_HASH); 370f196ce38SLuigi Rizzo } 371f18be576SLuigi Rizzo BDG_UNLOCK(); 372f196ce38SLuigi Rizzo return b; 373f196ce38SLuigi Rizzo } 374f18be576SLuigi Rizzo 375f18be576SLuigi Rizzo 376f18be576SLuigi Rizzo /* 377f18be576SLuigi Rizzo * Free the forwarding tables for rings attached to switch ports. 378f18be576SLuigi Rizzo */ 379f18be576SLuigi Rizzo static void 380f18be576SLuigi Rizzo nm_free_bdgfwd(struct netmap_adapter *na) 381f18be576SLuigi Rizzo { 382f18be576SLuigi Rizzo int nrings, i; 383f18be576SLuigi Rizzo struct netmap_kring *kring; 384f18be576SLuigi Rizzo 385f18be576SLuigi Rizzo nrings = nma_is_vp(na) ? na->num_tx_rings : na->num_rx_rings; 386f18be576SLuigi Rizzo kring = nma_is_vp(na) ? na->tx_rings : na->rx_rings; 387f18be576SLuigi Rizzo for (i = 0; i < nrings; i++) { 388f18be576SLuigi Rizzo if (kring[i].nkr_ft) { 389f18be576SLuigi Rizzo free(kring[i].nkr_ft, M_DEVBUF); 390f18be576SLuigi Rizzo kring[i].nkr_ft = NULL; /* protect from freeing twice */ 391f18be576SLuigi Rizzo } 392f18be576SLuigi Rizzo } 393f18be576SLuigi Rizzo if (nma_is_hw(na)) 394f18be576SLuigi Rizzo nm_free_bdgfwd(SWNA(na->ifp)); 395f18be576SLuigi Rizzo } 396f18be576SLuigi Rizzo 397f18be576SLuigi Rizzo 398f18be576SLuigi Rizzo /* 399f18be576SLuigi Rizzo * Allocate the forwarding tables for the rings attached to the bridge ports. 400f18be576SLuigi Rizzo */ 401f18be576SLuigi Rizzo static int 402f18be576SLuigi Rizzo nm_alloc_bdgfwd(struct netmap_adapter *na) 403f18be576SLuigi Rizzo { 404f18be576SLuigi Rizzo int nrings, l, i, num_dstq; 405f18be576SLuigi Rizzo struct netmap_kring *kring; 406f18be576SLuigi Rizzo 407f18be576SLuigi Rizzo /* all port:rings + broadcast */ 408f18be576SLuigi Rizzo num_dstq = NM_BDG_MAXPORTS * NM_BDG_MAXRINGS + 1; 409f18be576SLuigi Rizzo l = sizeof(struct nm_bdg_fwd) * NM_BDG_BATCH; 410f18be576SLuigi Rizzo l += sizeof(struct nm_bdg_q) * num_dstq; 411f18be576SLuigi Rizzo l += sizeof(uint16_t) * NM_BDG_BATCH; 412f18be576SLuigi Rizzo 413f18be576SLuigi Rizzo nrings = nma_is_vp(na) ? na->num_tx_rings : na->num_rx_rings; 414f18be576SLuigi Rizzo kring = nma_is_vp(na) ? na->tx_rings : na->rx_rings; 415f18be576SLuigi Rizzo for (i = 0; i < nrings; i++) { 416f18be576SLuigi Rizzo struct nm_bdg_fwd *ft; 417f18be576SLuigi Rizzo struct nm_bdg_q *dstq; 418f18be576SLuigi Rizzo int j; 419f18be576SLuigi Rizzo 420f18be576SLuigi Rizzo ft = malloc(l, M_DEVBUF, M_NOWAIT | M_ZERO); 421f18be576SLuigi Rizzo if (!ft) { 422f18be576SLuigi Rizzo nm_free_bdgfwd(na); 423f18be576SLuigi Rizzo return ENOMEM; 424f18be576SLuigi Rizzo } 425f18be576SLuigi Rizzo dstq = (struct nm_bdg_q *)(ft + NM_BDG_BATCH); 426f18be576SLuigi Rizzo for (j = 0; j < num_dstq; j++) 427f18be576SLuigi Rizzo dstq[j].bq_head = dstq[j].bq_tail = NM_BDG_BATCH; 428f18be576SLuigi Rizzo kring[i].nkr_ft = ft; 429f18be576SLuigi Rizzo } 430f18be576SLuigi Rizzo if (nma_is_hw(na)) 431f18be576SLuigi Rizzo nm_alloc_bdgfwd(SWNA(na->ifp)); 432f18be576SLuigi Rizzo return 0; 433f18be576SLuigi Rizzo } 434f18be576SLuigi Rizzo 435f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */ 4365819da83SLuigi Rizzo 437ae10d1afSLuigi Rizzo 438ae10d1afSLuigi Rizzo /* 439ae10d1afSLuigi Rizzo * Fetch configuration from the device, to cope with dynamic 440ae10d1afSLuigi Rizzo * reconfigurations after loading the module. 441ae10d1afSLuigi Rizzo */ 442ae10d1afSLuigi Rizzo static int 443ae10d1afSLuigi Rizzo netmap_update_config(struct netmap_adapter *na) 444ae10d1afSLuigi Rizzo { 445ae10d1afSLuigi Rizzo struct ifnet *ifp = na->ifp; 446ae10d1afSLuigi Rizzo u_int txr, txd, rxr, rxd; 447ae10d1afSLuigi Rizzo 448ae10d1afSLuigi Rizzo txr = txd = rxr = rxd = 0; 449ae10d1afSLuigi Rizzo if (na->nm_config) { 450ae10d1afSLuigi Rizzo na->nm_config(ifp, &txr, &txd, &rxr, &rxd); 451ae10d1afSLuigi Rizzo } else { 452ae10d1afSLuigi Rizzo /* take whatever we had at init time */ 453ae10d1afSLuigi Rizzo txr = na->num_tx_rings; 454ae10d1afSLuigi Rizzo txd = na->num_tx_desc; 455ae10d1afSLuigi Rizzo rxr = na->num_rx_rings; 456ae10d1afSLuigi Rizzo rxd = na->num_rx_desc; 457ae10d1afSLuigi Rizzo } 458ae10d1afSLuigi Rizzo 459ae10d1afSLuigi Rizzo if (na->num_tx_rings == txr && na->num_tx_desc == txd && 460ae10d1afSLuigi Rizzo na->num_rx_rings == rxr && na->num_rx_desc == rxd) 461ae10d1afSLuigi Rizzo return 0; /* nothing changed */ 462ae10d1afSLuigi Rizzo if (netmap_verbose || na->refcount > 0) { 463ae10d1afSLuigi Rizzo D("stored config %s: txring %d x %d, rxring %d x %d", 464ae10d1afSLuigi Rizzo ifp->if_xname, 465ae10d1afSLuigi Rizzo na->num_tx_rings, na->num_tx_desc, 466ae10d1afSLuigi Rizzo na->num_rx_rings, na->num_rx_desc); 467ae10d1afSLuigi Rizzo D("new config %s: txring %d x %d, rxring %d x %d", 468ae10d1afSLuigi Rizzo ifp->if_xname, txr, txd, rxr, rxd); 469ae10d1afSLuigi Rizzo } 470ae10d1afSLuigi Rizzo if (na->refcount == 0) { 471ae10d1afSLuigi Rizzo D("configuration changed (but fine)"); 472ae10d1afSLuigi Rizzo na->num_tx_rings = txr; 473ae10d1afSLuigi Rizzo na->num_tx_desc = txd; 474ae10d1afSLuigi Rizzo na->num_rx_rings = rxr; 475ae10d1afSLuigi Rizzo na->num_rx_desc = rxd; 476ae10d1afSLuigi Rizzo return 0; 477ae10d1afSLuigi Rizzo } 478ae10d1afSLuigi Rizzo D("configuration changed while active, this is bad..."); 479ae10d1afSLuigi Rizzo return 1; 480ae10d1afSLuigi Rizzo } 481ae10d1afSLuigi Rizzo 4823c0caf6cSLuigi Rizzo /*------------- memory allocator -----------------*/ 4833c0caf6cSLuigi Rizzo #include "netmap_mem2.c" 4843c0caf6cSLuigi Rizzo /*------------ end of memory allocator ----------*/ 48568b8534bSLuigi Rizzo 4868241616dSLuigi Rizzo 4878241616dSLuigi Rizzo /* Structure associated to each thread which registered an interface. 4888241616dSLuigi Rizzo * 4898241616dSLuigi Rizzo * The first 4 fields of this structure are written by NIOCREGIF and 4908241616dSLuigi Rizzo * read by poll() and NIOC?XSYNC. 4918241616dSLuigi Rizzo * There is low contention among writers (actually, a correct user program 4928241616dSLuigi Rizzo * should have no contention among writers) and among writers and readers, 4938241616dSLuigi Rizzo * so we use a single global lock to protect the structure initialization. 4948241616dSLuigi Rizzo * Since initialization involves the allocation of memory, we reuse the memory 4958241616dSLuigi Rizzo * allocator lock. 4968241616dSLuigi Rizzo * Read access to the structure is lock free. Readers must check that 4978241616dSLuigi Rizzo * np_nifp is not NULL before using the other fields. 4988241616dSLuigi Rizzo * If np_nifp is NULL initialization has not been performed, so they should 4998241616dSLuigi Rizzo * return an error to userlevel. 5008241616dSLuigi Rizzo * 5018241616dSLuigi Rizzo * The ref_done field is used to regulate access to the refcount in the 5028241616dSLuigi Rizzo * memory allocator. The refcount must be incremented at most once for 5038241616dSLuigi Rizzo * each open("/dev/netmap"). The increment is performed by the first 5048241616dSLuigi Rizzo * function that calls netmap_get_memory() (currently called by 5058241616dSLuigi Rizzo * mmap(), NIOCGINFO and NIOCREGIF). 5068241616dSLuigi Rizzo * If the refcount is incremented, it is then decremented when the 5078241616dSLuigi Rizzo * private structure is destroyed. 5088241616dSLuigi Rizzo */ 50968b8534bSLuigi Rizzo struct netmap_priv_d { 5108241616dSLuigi Rizzo struct netmap_if * volatile np_nifp; /* netmap interface descriptor. */ 51168b8534bSLuigi Rizzo 51268b8534bSLuigi Rizzo struct ifnet *np_ifp; /* device for which we hold a reference */ 51368b8534bSLuigi Rizzo int np_ringid; /* from the ioctl */ 51468b8534bSLuigi Rizzo u_int np_qfirst, np_qlast; /* range of rings to scan */ 51568b8534bSLuigi Rizzo uint16_t np_txpoll; 5168241616dSLuigi Rizzo 5178241616dSLuigi Rizzo unsigned long ref_done; /* use with NMA_LOCK held */ 51868b8534bSLuigi Rizzo }; 51968b8534bSLuigi Rizzo 52068b8534bSLuigi Rizzo 5218241616dSLuigi Rizzo static int 5228241616dSLuigi Rizzo netmap_get_memory(struct netmap_priv_d* p) 5238241616dSLuigi Rizzo { 5248241616dSLuigi Rizzo int error = 0; 5258241616dSLuigi Rizzo NMA_LOCK(); 5268241616dSLuigi Rizzo if (!p->ref_done) { 5278241616dSLuigi Rizzo error = netmap_memory_finalize(); 5288241616dSLuigi Rizzo if (!error) 5298241616dSLuigi Rizzo p->ref_done = 1; 5308241616dSLuigi Rizzo } 5318241616dSLuigi Rizzo NMA_UNLOCK(); 5328241616dSLuigi Rizzo return error; 5338241616dSLuigi Rizzo } 5348241616dSLuigi Rizzo 53568b8534bSLuigi Rizzo /* 53668b8534bSLuigi Rizzo * File descriptor's private data destructor. 53768b8534bSLuigi Rizzo * 53868b8534bSLuigi Rizzo * Call nm_register(ifp,0) to stop netmap mode on the interface and 53968b8534bSLuigi Rizzo * revert to normal operation. We expect that np_ifp has not gone. 54068b8534bSLuigi Rizzo */ 5418241616dSLuigi Rizzo /* call with NMA_LOCK held */ 54268b8534bSLuigi Rizzo static void 5435819da83SLuigi Rizzo netmap_dtor_locked(void *data) 54468b8534bSLuigi Rizzo { 54568b8534bSLuigi Rizzo struct netmap_priv_d *priv = data; 54668b8534bSLuigi Rizzo struct ifnet *ifp = priv->np_ifp; 54768b8534bSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 54868b8534bSLuigi Rizzo struct netmap_if *nifp = priv->np_nifp; 54968b8534bSLuigi Rizzo 55068b8534bSLuigi Rizzo na->refcount--; 55168b8534bSLuigi Rizzo if (na->refcount <= 0) { /* last instance */ 55264ae02c3SLuigi Rizzo u_int i, j, lim; 55368b8534bSLuigi Rizzo 554ae10d1afSLuigi Rizzo if (netmap_verbose) 555ae10d1afSLuigi Rizzo D("deleting last instance for %s", ifp->if_xname); 55668b8534bSLuigi Rizzo /* 557f18be576SLuigi Rizzo * (TO CHECK) This function is only called 558f18be576SLuigi Rizzo * when the last reference to this file descriptor goes 559f18be576SLuigi Rizzo * away. This means we cannot have any pending poll() 560f18be576SLuigi Rizzo * or interrupt routine operating on the structure. 56168b8534bSLuigi Rizzo */ 56268b8534bSLuigi Rizzo na->nm_register(ifp, 0); /* off, clear IFCAP_NETMAP */ 56368b8534bSLuigi Rizzo /* Wake up any sleeping threads. netmap_poll will 56468b8534bSLuigi Rizzo * then return POLLERR 56568b8534bSLuigi Rizzo */ 566d76bf4ffSLuigi Rizzo for (i = 0; i < na->num_tx_rings + 1; i++) 56768b8534bSLuigi Rizzo selwakeuppri(&na->tx_rings[i].si, PI_NET); 568d76bf4ffSLuigi Rizzo for (i = 0; i < na->num_rx_rings + 1; i++) 56968b8534bSLuigi Rizzo selwakeuppri(&na->rx_rings[i].si, PI_NET); 57064ae02c3SLuigi Rizzo selwakeuppri(&na->tx_si, PI_NET); 57164ae02c3SLuigi Rizzo selwakeuppri(&na->rx_si, PI_NET); 572f18be576SLuigi Rizzo #ifdef NM_BRIDGE 573f18be576SLuigi Rizzo nm_free_bdgfwd(na); 574f18be576SLuigi Rizzo #endif /* NM_BRIDGE */ 57568b8534bSLuigi Rizzo /* release all buffers */ 576d76bf4ffSLuigi Rizzo for (i = 0; i < na->num_tx_rings + 1; i++) { 57764ae02c3SLuigi Rizzo struct netmap_ring *ring = na->tx_rings[i].ring; 57868b8534bSLuigi Rizzo lim = na->tx_rings[i].nkr_num_slots; 57968b8534bSLuigi Rizzo for (j = 0; j < lim; j++) 580446ee301SLuigi Rizzo netmap_free_buf(nifp, ring->slot[j].buf_idx); 5812f70fca5SEd Maste /* knlist_destroy(&na->tx_rings[i].si.si_note); */ 5822f70fca5SEd Maste mtx_destroy(&na->tx_rings[i].q_lock); 58364ae02c3SLuigi Rizzo } 584d76bf4ffSLuigi Rizzo for (i = 0; i < na->num_rx_rings + 1; i++) { 58564ae02c3SLuigi Rizzo struct netmap_ring *ring = na->rx_rings[i].ring; 58668b8534bSLuigi Rizzo lim = na->rx_rings[i].nkr_num_slots; 58768b8534bSLuigi Rizzo for (j = 0; j < lim; j++) 588446ee301SLuigi Rizzo netmap_free_buf(nifp, ring->slot[j].buf_idx); 5892f70fca5SEd Maste /* knlist_destroy(&na->rx_rings[i].si.si_note); */ 5902f70fca5SEd Maste mtx_destroy(&na->rx_rings[i].q_lock); 59168b8534bSLuigi Rizzo } 5922f70fca5SEd Maste /* XXX kqueue(9) needed; these will mirror knlist_init. */ 5932f70fca5SEd Maste /* knlist_destroy(&na->tx_si.si_note); */ 5942f70fca5SEd Maste /* knlist_destroy(&na->rx_si.si_note); */ 5956e10c8b8SLuigi Rizzo netmap_free_rings(na); 596f18be576SLuigi Rizzo if (nma_is_hw(na)) 597f18be576SLuigi Rizzo SWNA(ifp)->tx_rings = SWNA(ifp)->rx_rings = NULL; 59868b8534bSLuigi Rizzo } 5996e10c8b8SLuigi Rizzo netmap_if_free(nifp); 6005819da83SLuigi Rizzo } 60168b8534bSLuigi Rizzo 602f18be576SLuigi Rizzo 603f18be576SLuigi Rizzo /* we assume netmap adapter exists */ 604f196ce38SLuigi Rizzo static void 605f196ce38SLuigi Rizzo nm_if_rele(struct ifnet *ifp) 606f196ce38SLuigi Rizzo { 607f196ce38SLuigi Rizzo #ifndef NM_BRIDGE 608f196ce38SLuigi Rizzo if_rele(ifp); 609f196ce38SLuigi Rizzo #else /* NM_BRIDGE */ 610f18be576SLuigi Rizzo int i, full = 0, is_hw; 611f196ce38SLuigi Rizzo struct nm_bridge *b; 612f18be576SLuigi Rizzo struct netmap_adapter *na; 613f196ce38SLuigi Rizzo 614f18be576SLuigi Rizzo /* I can be called not only for get_ifp()-ed references where netmap's 615f18be576SLuigi Rizzo * capability is guaranteed, but also for non-netmap-capable NICs. 616f18be576SLuigi Rizzo */ 617f18be576SLuigi Rizzo if (!NETMAP_CAPABLE(ifp) || !NA(ifp)->na_bdg) { 618f196ce38SLuigi Rizzo if_rele(ifp); 619f196ce38SLuigi Rizzo return; 620f196ce38SLuigi Rizzo } 621f196ce38SLuigi Rizzo if (!DROP_BDG_REF(ifp)) 622f196ce38SLuigi Rizzo return; 623f18be576SLuigi Rizzo 624f18be576SLuigi Rizzo na = NA(ifp); 625f18be576SLuigi Rizzo b = na->na_bdg; 626f18be576SLuigi Rizzo is_hw = nma_is_hw(na); 627f18be576SLuigi Rizzo 628f18be576SLuigi Rizzo BDG_WLOCK(b); 629f196ce38SLuigi Rizzo ND("want to disconnect %s from the bridge", ifp->if_xname); 630f196ce38SLuigi Rizzo full = 0; 631f18be576SLuigi Rizzo /* remove the entry from the bridge, also check 632f18be576SLuigi Rizzo * if there are any leftover interfaces 633f18be576SLuigi Rizzo * XXX we should optimize this code, e.g. going directly 634f18be576SLuigi Rizzo * to na->bdg_port, and having a counter of ports that 635f18be576SLuigi Rizzo * are connected. But it is not in a critical path. 636f18be576SLuigi Rizzo * In NIC's case, index of sw na is always higher than hw na 637f18be576SLuigi Rizzo */ 638f196ce38SLuigi Rizzo for (i = 0; i < NM_BDG_MAXPORTS; i++) { 639f18be576SLuigi Rizzo struct netmap_adapter *tmp = BDG_GET_VAR(b->bdg_ports[i]); 640f18be576SLuigi Rizzo 641f18be576SLuigi Rizzo if (tmp == na) { 642f18be576SLuigi Rizzo /* disconnect from bridge */ 643f18be576SLuigi Rizzo BDG_SET_VAR(b->bdg_ports[i], NULL); 644f18be576SLuigi Rizzo na->na_bdg = NULL; 645f18be576SLuigi Rizzo if (is_hw && SWNA(ifp)->na_bdg) { 646f18be576SLuigi Rizzo /* disconnect sw adapter too */ 647f18be576SLuigi Rizzo int j = SWNA(ifp)->bdg_port; 648f18be576SLuigi Rizzo BDG_SET_VAR(b->bdg_ports[j], NULL); 649f18be576SLuigi Rizzo SWNA(ifp)->na_bdg = NULL; 650f196ce38SLuigi Rizzo } 651f18be576SLuigi Rizzo } else if (tmp != NULL) { 652f196ce38SLuigi Rizzo full = 1; 653f196ce38SLuigi Rizzo } 654f196ce38SLuigi Rizzo } 655f18be576SLuigi Rizzo BDG_WUNLOCK(b); 656f18be576SLuigi Rizzo if (full == 0) { 657f18be576SLuigi Rizzo ND("marking bridge %d as free", b - nm_bridges); 658f18be576SLuigi Rizzo b->namelen = 0; 659f18be576SLuigi Rizzo b->nm_bdg_lookup = NULL; 660f18be576SLuigi Rizzo } 661f18be576SLuigi Rizzo if (na->na_bdg) { /* still attached to the bridge */ 662f196ce38SLuigi Rizzo D("ouch, cannot find ifp to remove"); 663f18be576SLuigi Rizzo } else if (is_hw) { 664f18be576SLuigi Rizzo if_rele(ifp); 665f18be576SLuigi Rizzo } else { 666f18be576SLuigi Rizzo bzero(na, sizeof(*na)); 667f18be576SLuigi Rizzo free(na, M_DEVBUF); 668f18be576SLuigi Rizzo bzero(ifp, sizeof(*ifp)); 669f18be576SLuigi Rizzo free(ifp, M_DEVBUF); 670f18be576SLuigi Rizzo } 671f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */ 672f196ce38SLuigi Rizzo } 6735819da83SLuigi Rizzo 6745819da83SLuigi Rizzo static void 6755819da83SLuigi Rizzo netmap_dtor(void *data) 6765819da83SLuigi Rizzo { 6775819da83SLuigi Rizzo struct netmap_priv_d *priv = data; 6785819da83SLuigi Rizzo struct ifnet *ifp = priv->np_ifp; 6795819da83SLuigi Rizzo 6808241616dSLuigi Rizzo NMA_LOCK(); 6818241616dSLuigi Rizzo if (ifp) { 6822579e2d7SLuigi Rizzo struct netmap_adapter *na = NA(ifp); 6832579e2d7SLuigi Rizzo 684f18be576SLuigi Rizzo if (na->na_bdg) 685f18be576SLuigi Rizzo BDG_WLOCK(na->na_bdg); 6861a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_REG_LOCK, 0); 6875819da83SLuigi Rizzo netmap_dtor_locked(data); 6881a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0); 689f18be576SLuigi Rizzo if (na->na_bdg) 690f18be576SLuigi Rizzo BDG_WUNLOCK(na->na_bdg); 69168b8534bSLuigi Rizzo 6922579e2d7SLuigi Rizzo nm_if_rele(ifp); /* might also destroy *na */ 6938241616dSLuigi Rizzo } 6948241616dSLuigi Rizzo if (priv->ref_done) { 6958241616dSLuigi Rizzo netmap_memory_deref(); 6968241616dSLuigi Rizzo } 6978241616dSLuigi Rizzo NMA_UNLOCK(); 69868b8534bSLuigi Rizzo bzero(priv, sizeof(*priv)); /* XXX for safety */ 69968b8534bSLuigi Rizzo free(priv, M_DEVBUF); 70068b8534bSLuigi Rizzo } 70168b8534bSLuigi Rizzo 702f18be576SLuigi Rizzo 7038241616dSLuigi Rizzo #ifdef __FreeBSD__ 7048241616dSLuigi Rizzo #include <vm/vm.h> 7058241616dSLuigi Rizzo #include <vm/vm_param.h> 7068241616dSLuigi Rizzo #include <vm/vm_object.h> 7078241616dSLuigi Rizzo #include <vm/vm_page.h> 7088241616dSLuigi Rizzo #include <vm/vm_pager.h> 7098241616dSLuigi Rizzo #include <vm/uma.h> 7108241616dSLuigi Rizzo 711f18be576SLuigi Rizzo /* 712f18be576SLuigi Rizzo * In order to track whether pages are still mapped, we hook into 713f18be576SLuigi Rizzo * the standard cdev_pager and intercept the constructor and 714f18be576SLuigi Rizzo * destructor. 715f18be576SLuigi Rizzo * XXX but then ? Do we really use the information ? 716f18be576SLuigi Rizzo * Need to investigate. 717f18be576SLuigi Rizzo */ 7188241616dSLuigi Rizzo static struct cdev_pager_ops saved_cdev_pager_ops; 7198241616dSLuigi Rizzo 720f18be576SLuigi Rizzo 7218241616dSLuigi Rizzo static int 7228241616dSLuigi Rizzo netmap_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot, 7238241616dSLuigi Rizzo vm_ooffset_t foff, struct ucred *cred, u_short *color) 7248241616dSLuigi Rizzo { 725ae10d1afSLuigi Rizzo if (netmap_verbose) 7268241616dSLuigi Rizzo D("first mmap for %p", handle); 7278241616dSLuigi Rizzo return saved_cdev_pager_ops.cdev_pg_ctor(handle, 7288241616dSLuigi Rizzo size, prot, foff, cred, color); 7298241616dSLuigi Rizzo } 7308241616dSLuigi Rizzo 731f18be576SLuigi Rizzo 7328241616dSLuigi Rizzo static void 7338241616dSLuigi Rizzo netmap_dev_pager_dtor(void *handle) 7348241616dSLuigi Rizzo { 7358241616dSLuigi Rizzo saved_cdev_pager_ops.cdev_pg_dtor(handle); 736ae10d1afSLuigi Rizzo ND("ready to release memory for %p", handle); 7378241616dSLuigi Rizzo } 7388241616dSLuigi Rizzo 7398241616dSLuigi Rizzo 7408241616dSLuigi Rizzo static struct cdev_pager_ops netmap_cdev_pager_ops = { 7418241616dSLuigi Rizzo .cdev_pg_ctor = netmap_dev_pager_ctor, 7428241616dSLuigi Rizzo .cdev_pg_dtor = netmap_dev_pager_dtor, 7438241616dSLuigi Rizzo .cdev_pg_fault = NULL, 7448241616dSLuigi Rizzo }; 7458241616dSLuigi Rizzo 746f18be576SLuigi Rizzo 747f18be576SLuigi Rizzo // XXX check whether we need netmap_mmap_single _and_ netmap_mmap 7488241616dSLuigi Rizzo static int 7498241616dSLuigi Rizzo netmap_mmap_single(struct cdev *cdev, vm_ooffset_t *foff, 7508241616dSLuigi Rizzo vm_size_t objsize, vm_object_t *objp, int prot) 7518241616dSLuigi Rizzo { 7528241616dSLuigi Rizzo vm_object_t obj; 7538241616dSLuigi Rizzo 754ae10d1afSLuigi Rizzo ND("cdev %p foff %jd size %jd objp %p prot %d", cdev, 75588f79057SGleb Smirnoff (intmax_t )*foff, (intmax_t )objsize, objp, prot); 7568241616dSLuigi Rizzo obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff, 7578241616dSLuigi Rizzo curthread->td_ucred); 7588241616dSLuigi Rizzo ND("returns obj %p", obj); 7598241616dSLuigi Rizzo if (obj == NULL) 7608241616dSLuigi Rizzo return EINVAL; 7618241616dSLuigi Rizzo if (saved_cdev_pager_ops.cdev_pg_fault == NULL) { 762ae10d1afSLuigi Rizzo ND("initialize cdev_pager_ops"); 7638241616dSLuigi Rizzo saved_cdev_pager_ops = *(obj->un_pager.devp.ops); 7648241616dSLuigi Rizzo netmap_cdev_pager_ops.cdev_pg_fault = 7658241616dSLuigi Rizzo saved_cdev_pager_ops.cdev_pg_fault; 7668241616dSLuigi Rizzo }; 7678241616dSLuigi Rizzo obj->un_pager.devp.ops = &netmap_cdev_pager_ops; 7688241616dSLuigi Rizzo *objp = obj; 7698241616dSLuigi Rizzo return 0; 7708241616dSLuigi Rizzo } 7718241616dSLuigi Rizzo #endif /* __FreeBSD__ */ 7728241616dSLuigi Rizzo 77368b8534bSLuigi Rizzo 77468b8534bSLuigi Rizzo /* 77568b8534bSLuigi Rizzo * mmap(2) support for the "netmap" device. 77668b8534bSLuigi Rizzo * 77768b8534bSLuigi Rizzo * Expose all the memory previously allocated by our custom memory 77868b8534bSLuigi Rizzo * allocator: this way the user has only to issue a single mmap(2), and 77968b8534bSLuigi Rizzo * can work on all the data structures flawlessly. 78068b8534bSLuigi Rizzo * 78168b8534bSLuigi Rizzo * Return 0 on success, -1 otherwise. 78268b8534bSLuigi Rizzo */ 783babc7c12SLuigi Rizzo 784f196ce38SLuigi Rizzo #ifdef __FreeBSD__ 78568b8534bSLuigi Rizzo static int 786babc7c12SLuigi Rizzo netmap_mmap(__unused struct cdev *dev, 78768b8534bSLuigi Rizzo #if __FreeBSD_version < 900000 788babc7c12SLuigi Rizzo vm_offset_t offset, vm_paddr_t *paddr, int nprot 78968b8534bSLuigi Rizzo #else 790babc7c12SLuigi Rizzo vm_ooffset_t offset, vm_paddr_t *paddr, int nprot, 791babc7c12SLuigi Rizzo __unused vm_memattr_t *memattr 79268b8534bSLuigi Rizzo #endif 793babc7c12SLuigi Rizzo ) 79468b8534bSLuigi Rizzo { 7958241616dSLuigi Rizzo int error = 0; 7968241616dSLuigi Rizzo struct netmap_priv_d *priv; 7978241616dSLuigi Rizzo 79868b8534bSLuigi Rizzo if (nprot & PROT_EXEC) 79968b8534bSLuigi Rizzo return (-1); // XXX -1 or EINVAL ? 800446ee301SLuigi Rizzo 8018241616dSLuigi Rizzo error = devfs_get_cdevpriv((void **)&priv); 8028241616dSLuigi Rizzo if (error == EBADF) { /* called on fault, memory is initialized */ 8038241616dSLuigi Rizzo ND(5, "handling fault at ofs 0x%x", offset); 8048241616dSLuigi Rizzo error = 0; 8058241616dSLuigi Rizzo } else if (error == 0) /* make sure memory is set */ 8068241616dSLuigi Rizzo error = netmap_get_memory(priv); 8078241616dSLuigi Rizzo if (error) 8088241616dSLuigi Rizzo return (error); 8098241616dSLuigi Rizzo 81068b8534bSLuigi Rizzo ND("request for offset 0x%x", (uint32_t)offset); 811446ee301SLuigi Rizzo *paddr = netmap_ofstophys(offset); 81268b8534bSLuigi Rizzo 8138241616dSLuigi Rizzo return (*paddr ? 0 : ENOMEM); 8148241616dSLuigi Rizzo } 8158241616dSLuigi Rizzo 816f18be576SLuigi Rizzo 8178241616dSLuigi Rizzo static int 8188241616dSLuigi Rizzo netmap_close(struct cdev *dev, int fflag, int devtype, struct thread *td) 8198241616dSLuigi Rizzo { 820ae10d1afSLuigi Rizzo if (netmap_verbose) 821ae10d1afSLuigi Rizzo D("dev %p fflag 0x%x devtype %d td %p", 822ae10d1afSLuigi Rizzo dev, fflag, devtype, td); 8238241616dSLuigi Rizzo return 0; 8248241616dSLuigi Rizzo } 8258241616dSLuigi Rizzo 826f18be576SLuigi Rizzo 8278241616dSLuigi Rizzo static int 8288241616dSLuigi Rizzo netmap_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 8298241616dSLuigi Rizzo { 8308241616dSLuigi Rizzo struct netmap_priv_d *priv; 8318241616dSLuigi Rizzo int error; 8328241616dSLuigi Rizzo 8338241616dSLuigi Rizzo priv = malloc(sizeof(struct netmap_priv_d), M_DEVBUF, 8348241616dSLuigi Rizzo M_NOWAIT | M_ZERO); 8358241616dSLuigi Rizzo if (priv == NULL) 8368241616dSLuigi Rizzo return ENOMEM; 8378241616dSLuigi Rizzo 8388241616dSLuigi Rizzo error = devfs_set_cdevpriv(priv, netmap_dtor); 8398241616dSLuigi Rizzo if (error) 8408241616dSLuigi Rizzo return error; 8418241616dSLuigi Rizzo 8428241616dSLuigi Rizzo return 0; 84368b8534bSLuigi Rizzo } 844f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */ 84568b8534bSLuigi Rizzo 84668b8534bSLuigi Rizzo 84768b8534bSLuigi Rizzo /* 84802ad4083SLuigi Rizzo * Handlers for synchronization of the queues from/to the host. 849091fd0abSLuigi Rizzo * Netmap has two operating modes: 850091fd0abSLuigi Rizzo * - in the default mode, the rings connected to the host stack are 851091fd0abSLuigi Rizzo * just another ring pair managed by userspace; 852091fd0abSLuigi Rizzo * - in transparent mode (XXX to be defined) incoming packets 853091fd0abSLuigi Rizzo * (from the host or the NIC) are marked as NS_FORWARD upon 854091fd0abSLuigi Rizzo * arrival, and the user application has a chance to reset the 855091fd0abSLuigi Rizzo * flag for packets that should be dropped. 856091fd0abSLuigi Rizzo * On the RXSYNC or poll(), packets in RX rings between 857091fd0abSLuigi Rizzo * kring->nr_kcur and ring->cur with NS_FORWARD still set are moved 858091fd0abSLuigi Rizzo * to the other side. 859091fd0abSLuigi Rizzo * The transfer NIC --> host is relatively easy, just encapsulate 860091fd0abSLuigi Rizzo * into mbufs and we are done. The host --> NIC side is slightly 861091fd0abSLuigi Rizzo * harder because there might not be room in the tx ring so it 862091fd0abSLuigi Rizzo * might take a while before releasing the buffer. 863091fd0abSLuigi Rizzo */ 864091fd0abSLuigi Rizzo 865f18be576SLuigi Rizzo 866091fd0abSLuigi Rizzo /* 867091fd0abSLuigi Rizzo * pass a chain of buffers to the host stack as coming from 'dst' 868091fd0abSLuigi Rizzo */ 869091fd0abSLuigi Rizzo static void 870091fd0abSLuigi Rizzo netmap_send_up(struct ifnet *dst, struct mbuf *head) 871091fd0abSLuigi Rizzo { 872091fd0abSLuigi Rizzo struct mbuf *m; 873091fd0abSLuigi Rizzo 874091fd0abSLuigi Rizzo /* send packets up, outside the lock */ 875091fd0abSLuigi Rizzo while ((m = head) != NULL) { 876091fd0abSLuigi Rizzo head = head->m_nextpkt; 877091fd0abSLuigi Rizzo m->m_nextpkt = NULL; 878091fd0abSLuigi Rizzo if (netmap_verbose & NM_VERB_HOST) 879091fd0abSLuigi Rizzo D("sending up pkt %p size %d", m, MBUF_LEN(m)); 880091fd0abSLuigi Rizzo NM_SEND_UP(dst, m); 881091fd0abSLuigi Rizzo } 882091fd0abSLuigi Rizzo } 883091fd0abSLuigi Rizzo 884091fd0abSLuigi Rizzo struct mbq { 885091fd0abSLuigi Rizzo struct mbuf *head; 886091fd0abSLuigi Rizzo struct mbuf *tail; 887091fd0abSLuigi Rizzo int count; 888091fd0abSLuigi Rizzo }; 889091fd0abSLuigi Rizzo 890f18be576SLuigi Rizzo 891091fd0abSLuigi Rizzo /* 892091fd0abSLuigi Rizzo * put a copy of the buffers marked NS_FORWARD into an mbuf chain. 893091fd0abSLuigi Rizzo * Run from hwcur to cur - reserved 894091fd0abSLuigi Rizzo */ 895091fd0abSLuigi Rizzo static void 896091fd0abSLuigi Rizzo netmap_grab_packets(struct netmap_kring *kring, struct mbq *q, int force) 897091fd0abSLuigi Rizzo { 898091fd0abSLuigi Rizzo /* Take packets from hwcur to cur-reserved and pass them up. 899091fd0abSLuigi Rizzo * In case of no buffers we give up. At the end of the loop, 900091fd0abSLuigi Rizzo * the queue is drained in all cases. 901091fd0abSLuigi Rizzo * XXX handle reserved 902091fd0abSLuigi Rizzo */ 903091fd0abSLuigi Rizzo int k = kring->ring->cur - kring->ring->reserved; 904091fd0abSLuigi Rizzo u_int n, lim = kring->nkr_num_slots - 1; 905091fd0abSLuigi Rizzo struct mbuf *m, *tail = q->tail; 906091fd0abSLuigi Rizzo 907091fd0abSLuigi Rizzo if (k < 0) 908091fd0abSLuigi Rizzo k = k + kring->nkr_num_slots; 909091fd0abSLuigi Rizzo for (n = kring->nr_hwcur; n != k;) { 910091fd0abSLuigi Rizzo struct netmap_slot *slot = &kring->ring->slot[n]; 911091fd0abSLuigi Rizzo 912091fd0abSLuigi Rizzo n = (n == lim) ? 0 : n + 1; 913091fd0abSLuigi Rizzo if ((slot->flags & NS_FORWARD) == 0 && !force) 914091fd0abSLuigi Rizzo continue; 915091fd0abSLuigi Rizzo if (slot->len < 14 || slot->len > NETMAP_BUF_SIZE) { 916091fd0abSLuigi Rizzo D("bad pkt at %d len %d", n, slot->len); 917091fd0abSLuigi Rizzo continue; 918091fd0abSLuigi Rizzo } 919091fd0abSLuigi Rizzo slot->flags &= ~NS_FORWARD; // XXX needed ? 920091fd0abSLuigi Rizzo m = m_devget(NMB(slot), slot->len, 0, kring->na->ifp, NULL); 921091fd0abSLuigi Rizzo 922091fd0abSLuigi Rizzo if (m == NULL) 923091fd0abSLuigi Rizzo break; 924091fd0abSLuigi Rizzo if (tail) 925091fd0abSLuigi Rizzo tail->m_nextpkt = m; 926091fd0abSLuigi Rizzo else 927091fd0abSLuigi Rizzo q->head = m; 928091fd0abSLuigi Rizzo tail = m; 929091fd0abSLuigi Rizzo q->count++; 930091fd0abSLuigi Rizzo m->m_nextpkt = NULL; 931091fd0abSLuigi Rizzo } 932091fd0abSLuigi Rizzo q->tail = tail; 933091fd0abSLuigi Rizzo } 934091fd0abSLuigi Rizzo 935f18be576SLuigi Rizzo 936091fd0abSLuigi Rizzo /* 937091fd0abSLuigi Rizzo * called under main lock to send packets from the host to the NIC 938091fd0abSLuigi Rizzo * The host ring has packets from nr_hwcur to (cur - reserved) 939091fd0abSLuigi Rizzo * to be sent down. We scan the tx rings, which have just been 940091fd0abSLuigi Rizzo * flushed so nr_hwcur == cur. Pushing packets down means 941091fd0abSLuigi Rizzo * increment cur and decrement avail. 942091fd0abSLuigi Rizzo * XXX to be verified 943091fd0abSLuigi Rizzo */ 944091fd0abSLuigi Rizzo static void 945091fd0abSLuigi Rizzo netmap_sw_to_nic(struct netmap_adapter *na) 946091fd0abSLuigi Rizzo { 947091fd0abSLuigi Rizzo struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings]; 948091fd0abSLuigi Rizzo struct netmap_kring *k1 = &na->tx_rings[0]; 949091fd0abSLuigi Rizzo int i, howmany, src_lim, dst_lim; 950091fd0abSLuigi Rizzo 951091fd0abSLuigi Rizzo howmany = kring->nr_hwavail; /* XXX otherwise cur - reserved - nr_hwcur */ 952091fd0abSLuigi Rizzo 953091fd0abSLuigi Rizzo src_lim = kring->nkr_num_slots; 954091fd0abSLuigi Rizzo for (i = 0; howmany > 0 && i < na->num_tx_rings; i++, k1++) { 955091fd0abSLuigi Rizzo ND("%d packets left to ring %d (space %d)", howmany, i, k1->nr_hwavail); 956091fd0abSLuigi Rizzo dst_lim = k1->nkr_num_slots; 957091fd0abSLuigi Rizzo while (howmany > 0 && k1->ring->avail > 0) { 958091fd0abSLuigi Rizzo struct netmap_slot *src, *dst, tmp; 959091fd0abSLuigi Rizzo src = &kring->ring->slot[kring->nr_hwcur]; 960091fd0abSLuigi Rizzo dst = &k1->ring->slot[k1->ring->cur]; 961091fd0abSLuigi Rizzo tmp = *src; 962091fd0abSLuigi Rizzo src->buf_idx = dst->buf_idx; 963091fd0abSLuigi Rizzo src->flags = NS_BUF_CHANGED; 964091fd0abSLuigi Rizzo 965091fd0abSLuigi Rizzo dst->buf_idx = tmp.buf_idx; 966091fd0abSLuigi Rizzo dst->len = tmp.len; 967091fd0abSLuigi Rizzo dst->flags = NS_BUF_CHANGED; 968091fd0abSLuigi Rizzo ND("out len %d buf %d from %d to %d", 969091fd0abSLuigi Rizzo dst->len, dst->buf_idx, 970091fd0abSLuigi Rizzo kring->nr_hwcur, k1->ring->cur); 971091fd0abSLuigi Rizzo 972091fd0abSLuigi Rizzo if (++kring->nr_hwcur >= src_lim) 973091fd0abSLuigi Rizzo kring->nr_hwcur = 0; 974091fd0abSLuigi Rizzo howmany--; 975091fd0abSLuigi Rizzo kring->nr_hwavail--; 976091fd0abSLuigi Rizzo if (++k1->ring->cur >= dst_lim) 977091fd0abSLuigi Rizzo k1->ring->cur = 0; 978091fd0abSLuigi Rizzo k1->ring->avail--; 979091fd0abSLuigi Rizzo } 980091fd0abSLuigi Rizzo kring->ring->cur = kring->nr_hwcur; // XXX 981091fd0abSLuigi Rizzo k1++; 982091fd0abSLuigi Rizzo } 983091fd0abSLuigi Rizzo } 984091fd0abSLuigi Rizzo 985f18be576SLuigi Rizzo 986091fd0abSLuigi Rizzo /* 98702ad4083SLuigi Rizzo * netmap_sync_to_host() passes packets up. We are called from a 98802ad4083SLuigi Rizzo * system call in user process context, and the only contention 98902ad4083SLuigi Rizzo * can be among multiple user threads erroneously calling 990091fd0abSLuigi Rizzo * this routine concurrently. 99168b8534bSLuigi Rizzo */ 99268b8534bSLuigi Rizzo static void 99368b8534bSLuigi Rizzo netmap_sync_to_host(struct netmap_adapter *na) 99468b8534bSLuigi Rizzo { 995d76bf4ffSLuigi Rizzo struct netmap_kring *kring = &na->tx_rings[na->num_tx_rings]; 99668b8534bSLuigi Rizzo struct netmap_ring *ring = kring->ring; 997091fd0abSLuigi Rizzo u_int k, lim = kring->nkr_num_slots - 1; 998091fd0abSLuigi Rizzo struct mbq q = { NULL, NULL }; 99968b8534bSLuigi Rizzo 100002ad4083SLuigi Rizzo k = ring->cur; 100102ad4083SLuigi Rizzo if (k > lim) { 100202ad4083SLuigi Rizzo netmap_ring_reinit(kring); 100302ad4083SLuigi Rizzo return; 100402ad4083SLuigi Rizzo } 10051a26580eSLuigi Rizzo // na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0); 100668b8534bSLuigi Rizzo 100768b8534bSLuigi Rizzo /* Take packets from hwcur to cur and pass them up. 100868b8534bSLuigi Rizzo * In case of no buffers we give up. At the end of the loop, 100968b8534bSLuigi Rizzo * the queue is drained in all cases. 101068b8534bSLuigi Rizzo */ 1011091fd0abSLuigi Rizzo netmap_grab_packets(kring, &q, 1); 101202ad4083SLuigi Rizzo kring->nr_hwcur = k; 101368b8534bSLuigi Rizzo kring->nr_hwavail = ring->avail = lim; 10141a26580eSLuigi Rizzo // na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0); 101568b8534bSLuigi Rizzo 1016091fd0abSLuigi Rizzo netmap_send_up(na->ifp, q.head); 101768b8534bSLuigi Rizzo } 101868b8534bSLuigi Rizzo 1019f18be576SLuigi Rizzo 1020f18be576SLuigi Rizzo /* SWNA(ifp)->txrings[0] is always NA(ifp)->txrings[NA(ifp)->num_txrings] */ 1021f18be576SLuigi Rizzo static int 1022f18be576SLuigi Rizzo netmap_bdg_to_host(struct ifnet *ifp, u_int ring_nr, int do_lock) 1023f18be576SLuigi Rizzo { 1024f18be576SLuigi Rizzo (void)ring_nr; 1025f18be576SLuigi Rizzo (void)do_lock; 1026f18be576SLuigi Rizzo netmap_sync_to_host(NA(ifp)); 1027f18be576SLuigi Rizzo return 0; 1028f18be576SLuigi Rizzo } 1029f18be576SLuigi Rizzo 1030f18be576SLuigi Rizzo 103168b8534bSLuigi Rizzo /* 103202ad4083SLuigi Rizzo * rxsync backend for packets coming from the host stack. 103302ad4083SLuigi Rizzo * They have been put in the queue by netmap_start() so we 103402ad4083SLuigi Rizzo * need to protect access to the kring using a lock. 103502ad4083SLuigi Rizzo * 103668b8534bSLuigi Rizzo * This routine also does the selrecord if called from the poll handler 103768b8534bSLuigi Rizzo * (we know because td != NULL). 103801c7d25fSLuigi Rizzo * 103901c7d25fSLuigi Rizzo * NOTE: on linux, selrecord() is defined as a macro and uses pwait 104001c7d25fSLuigi Rizzo * as an additional hidden argument. 104168b8534bSLuigi Rizzo */ 104268b8534bSLuigi Rizzo static void 104301c7d25fSLuigi Rizzo netmap_sync_from_host(struct netmap_adapter *na, struct thread *td, void *pwait) 104468b8534bSLuigi Rizzo { 1045d76bf4ffSLuigi Rizzo struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings]; 104668b8534bSLuigi Rizzo struct netmap_ring *ring = kring->ring; 104764ae02c3SLuigi Rizzo u_int j, n, lim = kring->nkr_num_slots; 104864ae02c3SLuigi Rizzo u_int k = ring->cur, resvd = ring->reserved; 104968b8534bSLuigi Rizzo 105001c7d25fSLuigi Rizzo (void)pwait; /* disable unused warnings */ 10511a26580eSLuigi Rizzo na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0); 105264ae02c3SLuigi Rizzo if (k >= lim) { 105364ae02c3SLuigi Rizzo netmap_ring_reinit(kring); 105464ae02c3SLuigi Rizzo return; 105564ae02c3SLuigi Rizzo } 105664ae02c3SLuigi Rizzo /* new packets are already set in nr_hwavail */ 105764ae02c3SLuigi Rizzo /* skip past packets that userspace has released */ 105864ae02c3SLuigi Rizzo j = kring->nr_hwcur; 105964ae02c3SLuigi Rizzo if (resvd > 0) { 106064ae02c3SLuigi Rizzo if (resvd + ring->avail >= lim + 1) { 106164ae02c3SLuigi Rizzo D("XXX invalid reserve/avail %d %d", resvd, ring->avail); 106264ae02c3SLuigi Rizzo ring->reserved = resvd = 0; // XXX panic... 106364ae02c3SLuigi Rizzo } 106464ae02c3SLuigi Rizzo k = (k >= resvd) ? k - resvd : k + lim - resvd; 106564ae02c3SLuigi Rizzo } 106664ae02c3SLuigi Rizzo if (j != k) { 106764ae02c3SLuigi Rizzo n = k >= j ? k - j : k + lim - j; 106864ae02c3SLuigi Rizzo kring->nr_hwavail -= n; 106902ad4083SLuigi Rizzo kring->nr_hwcur = k; 107064ae02c3SLuigi Rizzo } 107164ae02c3SLuigi Rizzo k = ring->avail = kring->nr_hwavail - resvd; 107202ad4083SLuigi Rizzo if (k == 0 && td) 107368b8534bSLuigi Rizzo selrecord(td, &kring->si); 107402ad4083SLuigi Rizzo if (k && (netmap_verbose & NM_VERB_HOST)) 107502ad4083SLuigi Rizzo D("%d pkts from stack", k); 10761a26580eSLuigi Rizzo na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0); 107768b8534bSLuigi Rizzo } 107868b8534bSLuigi Rizzo 107968b8534bSLuigi Rizzo 108068b8534bSLuigi Rizzo /* 108168b8534bSLuigi Rizzo * get a refcounted reference to an interface. 108268b8534bSLuigi Rizzo * Return ENXIO if the interface does not exist, EINVAL if netmap 108368b8534bSLuigi Rizzo * is not supported by the interface. 108468b8534bSLuigi Rizzo * If successful, hold a reference. 1085f18be576SLuigi Rizzo * 1086f18be576SLuigi Rizzo * During the NIC is attached to a bridge, reference is managed 1087f18be576SLuigi Rizzo * at na->na_bdg_refcount using ADD/DROP_BDG_REF() as well as 1088f18be576SLuigi Rizzo * virtual ports. Hence, on the final DROP_BDG_REF(), the NIC 1089f18be576SLuigi Rizzo * is detached from the bridge, then ifp's refcount is dropped (this 1090f18be576SLuigi Rizzo * is equivalent to that ifp is destroyed in case of virtual ports. 1091f18be576SLuigi Rizzo * 1092f18be576SLuigi Rizzo * This function uses if_rele() when we want to prevent the NIC from 1093f18be576SLuigi Rizzo * being detached from the bridge in error handling. But once refcount 1094f18be576SLuigi Rizzo * is acquired by this function, it must be released using nm_if_rele(). 109568b8534bSLuigi Rizzo */ 109668b8534bSLuigi Rizzo static int 1097f18be576SLuigi Rizzo get_ifp(struct nmreq *nmr, struct ifnet **ifp) 109868b8534bSLuigi Rizzo { 1099f18be576SLuigi Rizzo const char *name = nmr->nr_name; 1100f18be576SLuigi Rizzo int namelen = strlen(name); 1101f196ce38SLuigi Rizzo #ifdef NM_BRIDGE 1102f196ce38SLuigi Rizzo struct ifnet *iter = NULL; 1103f18be576SLuigi Rizzo int no_prefix = 0; 1104f196ce38SLuigi Rizzo 1105f196ce38SLuigi Rizzo do { 1106f196ce38SLuigi Rizzo struct nm_bridge *b; 1107f18be576SLuigi Rizzo struct netmap_adapter *na; 1108f18be576SLuigi Rizzo int i, cand = -1, cand2 = -1; 1109f196ce38SLuigi Rizzo 1110f18be576SLuigi Rizzo if (strncmp(name, NM_NAME, sizeof(NM_NAME) - 1)) { 1111f18be576SLuigi Rizzo no_prefix = 1; 1112f196ce38SLuigi Rizzo break; 1113f18be576SLuigi Rizzo } 1114f18be576SLuigi Rizzo b = nm_find_bridge(name, 1 /* create a new one if no exist */ ); 1115f196ce38SLuigi Rizzo if (b == NULL) { 1116f196ce38SLuigi Rizzo D("no bridges available for '%s'", name); 1117f196ce38SLuigi Rizzo return (ENXIO); 1118f196ce38SLuigi Rizzo } 1119f18be576SLuigi Rizzo /* Now we are sure that name starts with the bridge's name */ 1120f18be576SLuigi Rizzo BDG_WLOCK(b); 1121f196ce38SLuigi Rizzo /* lookup in the local list of ports */ 1122f196ce38SLuigi Rizzo for (i = 0; i < NM_BDG_MAXPORTS; i++) { 1123f18be576SLuigi Rizzo na = BDG_GET_VAR(b->bdg_ports[i]); 1124f18be576SLuigi Rizzo if (na == NULL) { 1125f196ce38SLuigi Rizzo if (cand == -1) 1126f196ce38SLuigi Rizzo cand = i; /* potential insert point */ 1127f18be576SLuigi Rizzo else if (cand2 == -1) 1128f18be576SLuigi Rizzo cand2 = i; /* for host stack */ 1129f196ce38SLuigi Rizzo continue; 1130f196ce38SLuigi Rizzo } 1131f18be576SLuigi Rizzo iter = na->ifp; 1132f18be576SLuigi Rizzo /* XXX make sure the name only contains one : */ 1133f18be576SLuigi Rizzo if (!strcmp(iter->if_xname, name) /* virtual port */ || 1134f18be576SLuigi Rizzo (namelen > b->namelen && !strcmp(iter->if_xname, 1135f18be576SLuigi Rizzo name + b->namelen + 1)) /* NIC */) { 1136f196ce38SLuigi Rizzo ADD_BDG_REF(iter); 1137f196ce38SLuigi Rizzo ND("found existing interface"); 1138f18be576SLuigi Rizzo BDG_WUNLOCK(b); 1139f196ce38SLuigi Rizzo break; 1140f196ce38SLuigi Rizzo } 1141f196ce38SLuigi Rizzo } 1142f196ce38SLuigi Rizzo if (i < NM_BDG_MAXPORTS) /* already unlocked */ 1143f196ce38SLuigi Rizzo break; 1144f196ce38SLuigi Rizzo if (cand == -1) { 1145f196ce38SLuigi Rizzo D("bridge full, cannot create new port"); 1146f196ce38SLuigi Rizzo no_port: 1147f18be576SLuigi Rizzo BDG_WUNLOCK(b); 1148f196ce38SLuigi Rizzo *ifp = NULL; 1149f196ce38SLuigi Rizzo return EINVAL; 1150f196ce38SLuigi Rizzo } 1151f196ce38SLuigi Rizzo ND("create new bridge port %s", name); 1152f18be576SLuigi Rizzo /* 1153f18be576SLuigi Rizzo * create a struct ifnet for the new port. 1154f18be576SLuigi Rizzo * The forwarding table is attached to the kring(s). 1155f18be576SLuigi Rizzo */ 1156f18be576SLuigi Rizzo /* 1157f18be576SLuigi Rizzo * try see if there is a matching NIC with this name 1158f18be576SLuigi Rizzo * (after the bridge's name) 1159f18be576SLuigi Rizzo */ 1160f18be576SLuigi Rizzo iter = ifunit_ref(name + b->namelen + 1); 1161f18be576SLuigi Rizzo if (!iter) { /* this is a virtual port */ 1162f18be576SLuigi Rizzo /* Create a temporary NA with arguments, then 1163f18be576SLuigi Rizzo * bdg_netmap_attach() will allocate the real one 1164f18be576SLuigi Rizzo * and attach it to the ifp 1165f18be576SLuigi Rizzo */ 1166f18be576SLuigi Rizzo struct netmap_adapter tmp_na; 1167f18be576SLuigi Rizzo 1168f18be576SLuigi Rizzo if (nmr->nr_cmd) /* nr_cmd must be for a NIC */ 1169f18be576SLuigi Rizzo goto no_port; 1170f18be576SLuigi Rizzo bzero(&tmp_na, sizeof(tmp_na)); 1171f18be576SLuigi Rizzo /* bound checking */ 1172f18be576SLuigi Rizzo if (nmr->nr_tx_rings < 1) 1173f18be576SLuigi Rizzo nmr->nr_tx_rings = 1; 1174f18be576SLuigi Rizzo if (nmr->nr_tx_rings > NM_BDG_MAXRINGS) 1175f18be576SLuigi Rizzo nmr->nr_tx_rings = NM_BDG_MAXRINGS; 1176f18be576SLuigi Rizzo tmp_na.num_tx_rings = nmr->nr_tx_rings; 1177f18be576SLuigi Rizzo if (nmr->nr_rx_rings < 1) 1178f18be576SLuigi Rizzo nmr->nr_rx_rings = 1; 1179f18be576SLuigi Rizzo if (nmr->nr_rx_rings > NM_BDG_MAXRINGS) 1180f18be576SLuigi Rizzo nmr->nr_rx_rings = NM_BDG_MAXRINGS; 1181f18be576SLuigi Rizzo tmp_na.num_rx_rings = nmr->nr_rx_rings; 1182f18be576SLuigi Rizzo 1183f18be576SLuigi Rizzo iter = malloc(sizeof(*iter), M_DEVBUF, M_NOWAIT | M_ZERO); 1184f196ce38SLuigi Rizzo if (!iter) 1185f196ce38SLuigi Rizzo goto no_port; 1186f196ce38SLuigi Rizzo strcpy(iter->if_xname, name); 1187f18be576SLuigi Rizzo tmp_na.ifp = iter; 1188f18be576SLuigi Rizzo /* bdg_netmap_attach creates a struct netmap_adapter */ 1189f18be576SLuigi Rizzo bdg_netmap_attach(&tmp_na); 1190f18be576SLuigi Rizzo } else if (NETMAP_CAPABLE(iter)) { /* this is a NIC */ 1191f18be576SLuigi Rizzo /* cannot attach the NIC that any user or another 1192f18be576SLuigi Rizzo * bridge already holds. 1193f18be576SLuigi Rizzo */ 1194f18be576SLuigi Rizzo if (NETMAP_OWNED_BY_ANY(iter) || cand2 == -1) { 1195f18be576SLuigi Rizzo ifunit_rele: 1196f18be576SLuigi Rizzo if_rele(iter); /* don't detach from bridge */ 1197f18be576SLuigi Rizzo goto no_port; 1198f18be576SLuigi Rizzo } 1199f18be576SLuigi Rizzo /* bind the host stack to the bridge */ 1200f18be576SLuigi Rizzo if (nmr->nr_arg1 == NETMAP_BDG_HOST) { 1201f18be576SLuigi Rizzo BDG_SET_VAR(b->bdg_ports[cand2], SWNA(iter)); 1202f18be576SLuigi Rizzo SWNA(iter)->bdg_port = cand2; 1203f18be576SLuigi Rizzo SWNA(iter)->na_bdg = b; 1204f18be576SLuigi Rizzo } 1205f18be576SLuigi Rizzo } else /* not a netmap-capable NIC */ 1206f18be576SLuigi Rizzo goto ifunit_rele; 1207f18be576SLuigi Rizzo na = NA(iter); 1208f18be576SLuigi Rizzo na->bdg_port = cand; 1209f18be576SLuigi Rizzo /* bind the port to the bridge (virtual ports are not active) */ 1210f18be576SLuigi Rizzo BDG_SET_VAR(b->bdg_ports[cand], na); 1211f18be576SLuigi Rizzo na->na_bdg = b; 1212f196ce38SLuigi Rizzo ADD_BDG_REF(iter); 1213f18be576SLuigi Rizzo BDG_WUNLOCK(b); 1214f196ce38SLuigi Rizzo ND("attaching virtual bridge %p", b); 1215f196ce38SLuigi Rizzo } while (0); 1216f196ce38SLuigi Rizzo *ifp = iter; 1217f196ce38SLuigi Rizzo if (! *ifp) 1218f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */ 121968b8534bSLuigi Rizzo *ifp = ifunit_ref(name); 122068b8534bSLuigi Rizzo if (*ifp == NULL) 122168b8534bSLuigi Rizzo return (ENXIO); 122268b8534bSLuigi Rizzo /* can do this if the capability exists and if_pspare[0] 122368b8534bSLuigi Rizzo * points to the netmap descriptor. 122468b8534bSLuigi Rizzo */ 1225f18be576SLuigi Rizzo if (NETMAP_CAPABLE(*ifp)) { 1226f18be576SLuigi Rizzo #ifdef NM_BRIDGE 1227f18be576SLuigi Rizzo /* Users cannot use the NIC attached to a bridge directly */ 1228f18be576SLuigi Rizzo if (no_prefix && NETMAP_OWNED_BY_KERN(*ifp)) { 1229f18be576SLuigi Rizzo if_rele(*ifp); /* don't detach from bridge */ 1230f18be576SLuigi Rizzo return EINVAL; 1231f18be576SLuigi Rizzo } else 1232f18be576SLuigi Rizzo #endif /* NM_BRIDGE */ 123368b8534bSLuigi Rizzo return 0; /* valid pointer, we hold the refcount */ 1234f18be576SLuigi Rizzo } 1235f196ce38SLuigi Rizzo nm_if_rele(*ifp); 123668b8534bSLuigi Rizzo return EINVAL; // not NETMAP capable 123768b8534bSLuigi Rizzo } 123868b8534bSLuigi Rizzo 123968b8534bSLuigi Rizzo 124068b8534bSLuigi Rizzo /* 124168b8534bSLuigi Rizzo * Error routine called when txsync/rxsync detects an error. 124268b8534bSLuigi Rizzo * Can't do much more than resetting cur = hwcur, avail = hwavail. 124368b8534bSLuigi Rizzo * Return 1 on reinit. 1244506cc70cSLuigi Rizzo * 1245506cc70cSLuigi Rizzo * This routine is only called by the upper half of the kernel. 1246506cc70cSLuigi Rizzo * It only reads hwcur (which is changed only by the upper half, too) 1247506cc70cSLuigi Rizzo * and hwavail (which may be changed by the lower half, but only on 1248506cc70cSLuigi Rizzo * a tx ring and only to increase it, so any error will be recovered 1249506cc70cSLuigi Rizzo * on the next call). For the above, we don't strictly need to call 1250506cc70cSLuigi Rizzo * it under lock. 125168b8534bSLuigi Rizzo */ 125268b8534bSLuigi Rizzo int 125368b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring) 125468b8534bSLuigi Rizzo { 125568b8534bSLuigi Rizzo struct netmap_ring *ring = kring->ring; 125668b8534bSLuigi Rizzo u_int i, lim = kring->nkr_num_slots - 1; 125768b8534bSLuigi Rizzo int errors = 0; 125868b8534bSLuigi Rizzo 12598241616dSLuigi Rizzo RD(10, "called for %s", kring->na->ifp->if_xname); 126068b8534bSLuigi Rizzo if (ring->cur > lim) 126168b8534bSLuigi Rizzo errors++; 126268b8534bSLuigi Rizzo for (i = 0; i <= lim; i++) { 126368b8534bSLuigi Rizzo u_int idx = ring->slot[i].buf_idx; 126468b8534bSLuigi Rizzo u_int len = ring->slot[i].len; 126568b8534bSLuigi Rizzo if (idx < 2 || idx >= netmap_total_buffers) { 126668b8534bSLuigi Rizzo if (!errors++) 126768b8534bSLuigi Rizzo D("bad buffer at slot %d idx %d len %d ", i, idx, len); 126868b8534bSLuigi Rizzo ring->slot[i].buf_idx = 0; 126968b8534bSLuigi Rizzo ring->slot[i].len = 0; 127068b8534bSLuigi Rizzo } else if (len > NETMAP_BUF_SIZE) { 127168b8534bSLuigi Rizzo ring->slot[i].len = 0; 127268b8534bSLuigi Rizzo if (!errors++) 127368b8534bSLuigi Rizzo D("bad len %d at slot %d idx %d", 127468b8534bSLuigi Rizzo len, i, idx); 127568b8534bSLuigi Rizzo } 127668b8534bSLuigi Rizzo } 127768b8534bSLuigi Rizzo if (errors) { 127868b8534bSLuigi Rizzo int pos = kring - kring->na->tx_rings; 1279d76bf4ffSLuigi Rizzo int n = kring->na->num_tx_rings + 1; 128068b8534bSLuigi Rizzo 12818241616dSLuigi Rizzo RD(10, "total %d errors", errors); 128268b8534bSLuigi Rizzo errors++; 12838241616dSLuigi Rizzo RD(10, "%s %s[%d] reinit, cur %d -> %d avail %d -> %d", 128468b8534bSLuigi Rizzo kring->na->ifp->if_xname, 128568b8534bSLuigi Rizzo pos < n ? "TX" : "RX", pos < n ? pos : pos - n, 128668b8534bSLuigi Rizzo ring->cur, kring->nr_hwcur, 128768b8534bSLuigi Rizzo ring->avail, kring->nr_hwavail); 128868b8534bSLuigi Rizzo ring->cur = kring->nr_hwcur; 128968b8534bSLuigi Rizzo ring->avail = kring->nr_hwavail; 129068b8534bSLuigi Rizzo } 129168b8534bSLuigi Rizzo return (errors ? 1 : 0); 129268b8534bSLuigi Rizzo } 129368b8534bSLuigi Rizzo 129468b8534bSLuigi Rizzo 129568b8534bSLuigi Rizzo /* 129668b8534bSLuigi Rizzo * Set the ring ID. For devices with a single queue, a request 129768b8534bSLuigi Rizzo * for all rings is the same as a single ring. 129868b8534bSLuigi Rizzo */ 129968b8534bSLuigi Rizzo static int 130068b8534bSLuigi Rizzo netmap_set_ringid(struct netmap_priv_d *priv, u_int ringid) 130168b8534bSLuigi Rizzo { 130268b8534bSLuigi Rizzo struct ifnet *ifp = priv->np_ifp; 130368b8534bSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 130468b8534bSLuigi Rizzo u_int i = ringid & NETMAP_RING_MASK; 130564ae02c3SLuigi Rizzo /* initially (np_qfirst == np_qlast) we don't want to lock */ 130668b8534bSLuigi Rizzo int need_lock = (priv->np_qfirst != priv->np_qlast); 1307d76bf4ffSLuigi Rizzo int lim = na->num_rx_rings; 130868b8534bSLuigi Rizzo 1309d76bf4ffSLuigi Rizzo if (na->num_tx_rings > lim) 1310d76bf4ffSLuigi Rizzo lim = na->num_tx_rings; 131164ae02c3SLuigi Rizzo if ( (ringid & NETMAP_HW_RING) && i >= lim) { 131268b8534bSLuigi Rizzo D("invalid ring id %d", i); 131368b8534bSLuigi Rizzo return (EINVAL); 131468b8534bSLuigi Rizzo } 131568b8534bSLuigi Rizzo if (need_lock) 13161a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_LOCK, 0); 131768b8534bSLuigi Rizzo priv->np_ringid = ringid; 131868b8534bSLuigi Rizzo if (ringid & NETMAP_SW_RING) { 131964ae02c3SLuigi Rizzo priv->np_qfirst = NETMAP_SW_RING; 132064ae02c3SLuigi Rizzo priv->np_qlast = 0; 132168b8534bSLuigi Rizzo } else if (ringid & NETMAP_HW_RING) { 132268b8534bSLuigi Rizzo priv->np_qfirst = i; 132368b8534bSLuigi Rizzo priv->np_qlast = i + 1; 132468b8534bSLuigi Rizzo } else { 132568b8534bSLuigi Rizzo priv->np_qfirst = 0; 132664ae02c3SLuigi Rizzo priv->np_qlast = NETMAP_HW_RING ; 132768b8534bSLuigi Rizzo } 132868b8534bSLuigi Rizzo priv->np_txpoll = (ringid & NETMAP_NO_TX_POLL) ? 0 : 1; 132968b8534bSLuigi Rizzo if (need_lock) 13301a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0); 1331ae10d1afSLuigi Rizzo if (netmap_verbose) { 133268b8534bSLuigi Rizzo if (ringid & NETMAP_SW_RING) 133368b8534bSLuigi Rizzo D("ringid %s set to SW RING", ifp->if_xname); 133468b8534bSLuigi Rizzo else if (ringid & NETMAP_HW_RING) 133568b8534bSLuigi Rizzo D("ringid %s set to HW RING %d", ifp->if_xname, 133668b8534bSLuigi Rizzo priv->np_qfirst); 133768b8534bSLuigi Rizzo else 133864ae02c3SLuigi Rizzo D("ringid %s set to all %d HW RINGS", ifp->if_xname, lim); 1339ae10d1afSLuigi Rizzo } 134068b8534bSLuigi Rizzo return 0; 134168b8534bSLuigi Rizzo } 134268b8534bSLuigi Rizzo 1343f18be576SLuigi Rizzo 1344f18be576SLuigi Rizzo /* 1345f18be576SLuigi Rizzo * possibly move the interface to netmap-mode. 1346f18be576SLuigi Rizzo * If success it returns a pointer to netmap_if, otherwise NULL. 1347f18be576SLuigi Rizzo * This must be called with NMA_LOCK held. 1348f18be576SLuigi Rizzo */ 1349f18be576SLuigi Rizzo static struct netmap_if * 1350f18be576SLuigi Rizzo netmap_do_regif(struct netmap_priv_d *priv, struct ifnet *ifp, 1351f18be576SLuigi Rizzo uint16_t ringid, int *err) 1352f18be576SLuigi Rizzo { 1353f18be576SLuigi Rizzo struct netmap_adapter *na = NA(ifp); 1354f18be576SLuigi Rizzo struct netmap_if *nifp = NULL; 1355f18be576SLuigi Rizzo int i, error; 1356f18be576SLuigi Rizzo 1357f18be576SLuigi Rizzo if (na->na_bdg) 1358f18be576SLuigi Rizzo BDG_WLOCK(na->na_bdg); 1359f18be576SLuigi Rizzo na->nm_lock(ifp, NETMAP_REG_LOCK, 0); 1360f18be576SLuigi Rizzo 1361f18be576SLuigi Rizzo /* ring configuration may have changed, fetch from the card */ 1362f18be576SLuigi Rizzo netmap_update_config(na); 1363f18be576SLuigi Rizzo priv->np_ifp = ifp; /* store the reference */ 1364f18be576SLuigi Rizzo error = netmap_set_ringid(priv, ringid); 1365f18be576SLuigi Rizzo if (error) 1366f18be576SLuigi Rizzo goto out; 1367f18be576SLuigi Rizzo nifp = netmap_if_new(ifp->if_xname, na); 1368f18be576SLuigi Rizzo if (nifp == NULL) { /* allocation failed */ 1369f18be576SLuigi Rizzo error = ENOMEM; 1370f18be576SLuigi Rizzo } else if (ifp->if_capenable & IFCAP_NETMAP) { 1371f18be576SLuigi Rizzo /* was already set */ 1372f18be576SLuigi Rizzo } else { 1373f18be576SLuigi Rizzo /* Otherwise set the card in netmap mode 1374f18be576SLuigi Rizzo * and make it use the shared buffers. 1375f18be576SLuigi Rizzo */ 1376f18be576SLuigi Rizzo for (i = 0 ; i < na->num_tx_rings + 1; i++) 1377f18be576SLuigi Rizzo mtx_init(&na->tx_rings[i].q_lock, "nm_txq_lock", 1378f18be576SLuigi Rizzo MTX_NETWORK_LOCK, MTX_DEF); 1379f18be576SLuigi Rizzo for (i = 0 ; i < na->num_rx_rings + 1; i++) { 1380f18be576SLuigi Rizzo mtx_init(&na->rx_rings[i].q_lock, "nm_rxq_lock", 1381f18be576SLuigi Rizzo MTX_NETWORK_LOCK, MTX_DEF); 1382f18be576SLuigi Rizzo } 1383f18be576SLuigi Rizzo if (nma_is_hw(na)) { 1384f18be576SLuigi Rizzo SWNA(ifp)->tx_rings = &na->tx_rings[na->num_tx_rings]; 1385f18be576SLuigi Rizzo SWNA(ifp)->rx_rings = &na->rx_rings[na->num_rx_rings]; 1386f18be576SLuigi Rizzo } 1387f18be576SLuigi Rizzo error = na->nm_register(ifp, 1); /* mode on */ 1388f18be576SLuigi Rizzo #ifdef NM_BRIDGE 1389f18be576SLuigi Rizzo if (!error) 1390f18be576SLuigi Rizzo error = nm_alloc_bdgfwd(na); 1391f18be576SLuigi Rizzo #endif /* NM_BRIDGE */ 1392f18be576SLuigi Rizzo if (error) { 1393f18be576SLuigi Rizzo netmap_dtor_locked(priv); 1394f18be576SLuigi Rizzo /* nifp is not yet in priv, so free it separately */ 1395f18be576SLuigi Rizzo netmap_if_free(nifp); 1396f18be576SLuigi Rizzo nifp = NULL; 1397f18be576SLuigi Rizzo } 1398f18be576SLuigi Rizzo 1399f18be576SLuigi Rizzo } 1400f18be576SLuigi Rizzo out: 1401f18be576SLuigi Rizzo *err = error; 1402f18be576SLuigi Rizzo na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0); 1403f18be576SLuigi Rizzo if (na->na_bdg) 1404f18be576SLuigi Rizzo BDG_WUNLOCK(na->na_bdg); 1405f18be576SLuigi Rizzo return nifp; 1406f18be576SLuigi Rizzo } 1407f18be576SLuigi Rizzo 1408f18be576SLuigi Rizzo 1409f18be576SLuigi Rizzo /* Process NETMAP_BDG_ATTACH and NETMAP_BDG_DETACH */ 1410f18be576SLuigi Rizzo static int 1411f18be576SLuigi Rizzo kern_netmap_regif(struct nmreq *nmr) 1412f18be576SLuigi Rizzo { 1413f18be576SLuigi Rizzo struct ifnet *ifp; 1414f18be576SLuigi Rizzo struct netmap_if *nifp; 1415f18be576SLuigi Rizzo struct netmap_priv_d *npriv; 1416f18be576SLuigi Rizzo int error; 1417f18be576SLuigi Rizzo 1418f18be576SLuigi Rizzo npriv = malloc(sizeof(*npriv), M_DEVBUF, M_NOWAIT|M_ZERO); 1419f18be576SLuigi Rizzo if (npriv == NULL) 1420f18be576SLuigi Rizzo return ENOMEM; 1421f18be576SLuigi Rizzo error = netmap_get_memory(npriv); 1422f18be576SLuigi Rizzo if (error) { 1423f18be576SLuigi Rizzo free_exit: 1424f18be576SLuigi Rizzo bzero(npriv, sizeof(*npriv)); 1425f18be576SLuigi Rizzo free(npriv, M_DEVBUF); 1426f18be576SLuigi Rizzo return error; 1427f18be576SLuigi Rizzo } 1428f18be576SLuigi Rizzo 1429f18be576SLuigi Rizzo NMA_LOCK(); 1430f18be576SLuigi Rizzo error = get_ifp(nmr, &ifp); 1431f18be576SLuigi Rizzo if (error) { /* no device, or another bridge or user owns the device */ 1432f18be576SLuigi Rizzo NMA_UNLOCK(); 1433f18be576SLuigi Rizzo goto free_exit; 1434f18be576SLuigi Rizzo } else if (!NETMAP_OWNED_BY_KERN(ifp)) { 1435f18be576SLuigi Rizzo /* got reference to a virtual port or direct access to a NIC. 1436f18be576SLuigi Rizzo * perhaps specified no bridge's prefix or wrong NIC's name 1437f18be576SLuigi Rizzo */ 1438f18be576SLuigi Rizzo error = EINVAL; 1439f18be576SLuigi Rizzo unref_exit: 1440f18be576SLuigi Rizzo nm_if_rele(ifp); 1441f18be576SLuigi Rizzo NMA_UNLOCK(); 1442f18be576SLuigi Rizzo goto free_exit; 1443f18be576SLuigi Rizzo } 1444f18be576SLuigi Rizzo 1445f18be576SLuigi Rizzo if (nmr->nr_cmd == NETMAP_BDG_DETACH) { 1446f18be576SLuigi Rizzo if (NA(ifp)->refcount == 0) { /* not registered */ 1447f18be576SLuigi Rizzo error = EINVAL; 1448f18be576SLuigi Rizzo goto unref_exit; 1449f18be576SLuigi Rizzo } 1450f18be576SLuigi Rizzo NMA_UNLOCK(); 1451f18be576SLuigi Rizzo 1452f18be576SLuigi Rizzo netmap_dtor(NA(ifp)->na_kpriv); /* unregister */ 1453f18be576SLuigi Rizzo NA(ifp)->na_kpriv = NULL; 1454f18be576SLuigi Rizzo nm_if_rele(ifp); /* detach from the bridge */ 1455f18be576SLuigi Rizzo goto free_exit; 1456f18be576SLuigi Rizzo } else if (NA(ifp)->refcount > 0) { /* already registered */ 1457f18be576SLuigi Rizzo error = EINVAL; 1458f18be576SLuigi Rizzo goto unref_exit; 1459f18be576SLuigi Rizzo } 1460f18be576SLuigi Rizzo 1461f18be576SLuigi Rizzo nifp = netmap_do_regif(npriv, ifp, nmr->nr_ringid, &error); 1462f18be576SLuigi Rizzo if (!nifp) 1463f18be576SLuigi Rizzo goto unref_exit; 1464f18be576SLuigi Rizzo wmb(); // XXX do we need it ? 1465f18be576SLuigi Rizzo npriv->np_nifp = nifp; 1466f18be576SLuigi Rizzo NA(ifp)->na_kpriv = npriv; 1467f18be576SLuigi Rizzo NMA_UNLOCK(); 1468f18be576SLuigi Rizzo D("registered %s to netmap-mode", ifp->if_xname); 1469f18be576SLuigi Rizzo return 0; 1470f18be576SLuigi Rizzo } 1471f18be576SLuigi Rizzo 1472f18be576SLuigi Rizzo 1473f18be576SLuigi Rizzo /* CORE_LOCK is not necessary */ 1474f18be576SLuigi Rizzo static void 1475f18be576SLuigi Rizzo netmap_swlock_wrapper(struct ifnet *dev, int what, u_int queueid) 1476f18be576SLuigi Rizzo { 1477f18be576SLuigi Rizzo struct netmap_adapter *na = SWNA(dev); 1478f18be576SLuigi Rizzo 1479f18be576SLuigi Rizzo switch (what) { 1480f18be576SLuigi Rizzo case NETMAP_TX_LOCK: 1481f18be576SLuigi Rizzo mtx_lock(&na->tx_rings[queueid].q_lock); 1482f18be576SLuigi Rizzo break; 1483f18be576SLuigi Rizzo 1484f18be576SLuigi Rizzo case NETMAP_TX_UNLOCK: 1485f18be576SLuigi Rizzo mtx_unlock(&na->tx_rings[queueid].q_lock); 1486f18be576SLuigi Rizzo break; 1487f18be576SLuigi Rizzo 1488f18be576SLuigi Rizzo case NETMAP_RX_LOCK: 1489f18be576SLuigi Rizzo mtx_lock(&na->rx_rings[queueid].q_lock); 1490f18be576SLuigi Rizzo break; 1491f18be576SLuigi Rizzo 1492f18be576SLuigi Rizzo case NETMAP_RX_UNLOCK: 1493f18be576SLuigi Rizzo mtx_unlock(&na->rx_rings[queueid].q_lock); 1494f18be576SLuigi Rizzo break; 1495f18be576SLuigi Rizzo } 1496f18be576SLuigi Rizzo } 1497f18be576SLuigi Rizzo 1498f18be576SLuigi Rizzo 1499f18be576SLuigi Rizzo /* Initialize necessary fields of sw adapter located in right after hw's 1500f18be576SLuigi Rizzo * one. sw adapter attaches a pair of sw rings of the netmap-mode NIC. 1501f18be576SLuigi Rizzo * It is always activated and deactivated at the same tie with the hw's one. 1502f18be576SLuigi Rizzo * Thus we don't need refcounting on the sw adapter. 1503f18be576SLuigi Rizzo * Regardless of NIC's feature we use separate lock so that anybody can lock 1504f18be576SLuigi Rizzo * me independently from the hw adapter. 1505f18be576SLuigi Rizzo * Make sure nm_register is NULL to be handled as FALSE in nma_is_hw 1506f18be576SLuigi Rizzo */ 1507f18be576SLuigi Rizzo static void 1508f18be576SLuigi Rizzo netmap_attach_sw(struct ifnet *ifp) 1509f18be576SLuigi Rizzo { 1510f18be576SLuigi Rizzo struct netmap_adapter *hw_na = NA(ifp); 1511f18be576SLuigi Rizzo struct netmap_adapter *na = SWNA(ifp); 1512f18be576SLuigi Rizzo 1513f18be576SLuigi Rizzo na->ifp = ifp; 1514f18be576SLuigi Rizzo na->separate_locks = 1; 1515f18be576SLuigi Rizzo na->nm_lock = netmap_swlock_wrapper; 1516f18be576SLuigi Rizzo na->num_rx_rings = na->num_tx_rings = 1; 1517f18be576SLuigi Rizzo na->num_tx_desc = hw_na->num_tx_desc; 1518f18be576SLuigi Rizzo na->num_rx_desc = hw_na->num_rx_desc; 1519f18be576SLuigi Rizzo na->nm_txsync = netmap_bdg_to_host; 1520f18be576SLuigi Rizzo } 1521f18be576SLuigi Rizzo 1522f18be576SLuigi Rizzo 1523f18be576SLuigi Rizzo /* exported to kernel callers */ 1524f18be576SLuigi Rizzo int 1525f18be576SLuigi Rizzo netmap_bdg_ctl(struct nmreq *nmr, bdg_lookup_fn_t func) 1526f18be576SLuigi Rizzo { 1527f18be576SLuigi Rizzo struct nm_bridge *b; 1528f18be576SLuigi Rizzo struct netmap_adapter *na; 1529f18be576SLuigi Rizzo struct ifnet *iter; 1530f18be576SLuigi Rizzo char *name = nmr->nr_name; 1531f18be576SLuigi Rizzo int cmd = nmr->nr_cmd, namelen = strlen(name); 1532f18be576SLuigi Rizzo int error = 0, i, j; 1533f18be576SLuigi Rizzo 1534f18be576SLuigi Rizzo switch (cmd) { 1535f18be576SLuigi Rizzo case NETMAP_BDG_ATTACH: 1536f18be576SLuigi Rizzo case NETMAP_BDG_DETACH: 1537f18be576SLuigi Rizzo error = kern_netmap_regif(nmr); 1538f18be576SLuigi Rizzo break; 1539f18be576SLuigi Rizzo 1540f18be576SLuigi Rizzo case NETMAP_BDG_LIST: 1541f18be576SLuigi Rizzo /* this is used to enumerate bridges and ports */ 1542f18be576SLuigi Rizzo if (namelen) { /* look up indexes of bridge and port */ 1543f18be576SLuigi Rizzo if (strncmp(name, NM_NAME, strlen(NM_NAME))) { 1544f18be576SLuigi Rizzo error = EINVAL; 1545f18be576SLuigi Rizzo break; 1546f18be576SLuigi Rizzo } 1547f18be576SLuigi Rizzo b = nm_find_bridge(name, 0 /* don't create */); 1548f18be576SLuigi Rizzo if (!b) { 1549f18be576SLuigi Rizzo error = ENOENT; 1550f18be576SLuigi Rizzo break; 1551f18be576SLuigi Rizzo } 1552f18be576SLuigi Rizzo 1553f18be576SLuigi Rizzo BDG_RLOCK(b); 1554f18be576SLuigi Rizzo error = ENOENT; 1555f18be576SLuigi Rizzo for (i = 0; i < NM_BDG_MAXPORTS; i++) { 1556f18be576SLuigi Rizzo na = BDG_GET_VAR(b->bdg_ports[i]); 1557f18be576SLuigi Rizzo if (na == NULL) 1558f18be576SLuigi Rizzo continue; 1559f18be576SLuigi Rizzo iter = na->ifp; 1560f18be576SLuigi Rizzo /* the former and the latter identify a 1561f18be576SLuigi Rizzo * virtual port and a NIC, respectively 1562f18be576SLuigi Rizzo */ 1563f18be576SLuigi Rizzo if (!strcmp(iter->if_xname, name) || 1564f18be576SLuigi Rizzo (namelen > b->namelen && 1565f18be576SLuigi Rizzo !strcmp(iter->if_xname, 1566f18be576SLuigi Rizzo name + b->namelen + 1))) { 1567f18be576SLuigi Rizzo /* bridge index */ 1568f18be576SLuigi Rizzo nmr->nr_arg1 = b - nm_bridges; 1569f18be576SLuigi Rizzo nmr->nr_arg2 = i; /* port index */ 1570f18be576SLuigi Rizzo error = 0; 1571f18be576SLuigi Rizzo break; 1572f18be576SLuigi Rizzo } 1573f18be576SLuigi Rizzo } 1574f18be576SLuigi Rizzo BDG_RUNLOCK(b); 1575f18be576SLuigi Rizzo } else { 1576f18be576SLuigi Rizzo /* return the first non-empty entry starting from 1577f18be576SLuigi Rizzo * bridge nr_arg1 and port nr_arg2. 1578f18be576SLuigi Rizzo * 1579f18be576SLuigi Rizzo * Users can detect the end of the same bridge by 1580f18be576SLuigi Rizzo * seeing the new and old value of nr_arg1, and can 1581f18be576SLuigi Rizzo * detect the end of all the bridge by error != 0 1582f18be576SLuigi Rizzo */ 1583f18be576SLuigi Rizzo i = nmr->nr_arg1; 1584f18be576SLuigi Rizzo j = nmr->nr_arg2; 1585f18be576SLuigi Rizzo 1586f18be576SLuigi Rizzo for (error = ENOENT; error && i < NM_BRIDGES; i++) { 1587f18be576SLuigi Rizzo b = nm_bridges + i; 1588f18be576SLuigi Rizzo BDG_RLOCK(b); 1589f18be576SLuigi Rizzo for (; j < NM_BDG_MAXPORTS; j++) { 1590f18be576SLuigi Rizzo na = BDG_GET_VAR(b->bdg_ports[j]); 1591f18be576SLuigi Rizzo if (na == NULL) 1592f18be576SLuigi Rizzo continue; 1593f18be576SLuigi Rizzo iter = na->ifp; 1594f18be576SLuigi Rizzo nmr->nr_arg1 = i; 1595f18be576SLuigi Rizzo nmr->nr_arg2 = j; 1596f18be576SLuigi Rizzo strncpy(name, iter->if_xname, IFNAMSIZ); 1597f18be576SLuigi Rizzo error = 0; 1598f18be576SLuigi Rizzo break; 1599f18be576SLuigi Rizzo } 1600f18be576SLuigi Rizzo BDG_RUNLOCK(b); 1601f18be576SLuigi Rizzo j = 0; /* following bridges scan from 0 */ 1602f18be576SLuigi Rizzo } 1603f18be576SLuigi Rizzo } 1604f18be576SLuigi Rizzo break; 1605f18be576SLuigi Rizzo 1606f18be576SLuigi Rizzo case NETMAP_BDG_LOOKUP_REG: 1607f18be576SLuigi Rizzo /* register a lookup function to the given bridge. 1608f18be576SLuigi Rizzo * nmr->nr_name may be just bridge's name (including ':' 1609f18be576SLuigi Rizzo * if it is not just NM_NAME). 1610f18be576SLuigi Rizzo */ 1611f18be576SLuigi Rizzo if (!func) { 1612f18be576SLuigi Rizzo error = EINVAL; 1613f18be576SLuigi Rizzo break; 1614f18be576SLuigi Rizzo } 1615f18be576SLuigi Rizzo b = nm_find_bridge(name, 0 /* don't create */); 1616f18be576SLuigi Rizzo if (!b) { 1617f18be576SLuigi Rizzo error = EINVAL; 1618f18be576SLuigi Rizzo break; 1619f18be576SLuigi Rizzo } 1620f18be576SLuigi Rizzo BDG_WLOCK(b); 1621f18be576SLuigi Rizzo b->nm_bdg_lookup = func; 1622f18be576SLuigi Rizzo BDG_WUNLOCK(b); 1623f18be576SLuigi Rizzo break; 1624f18be576SLuigi Rizzo default: 1625f18be576SLuigi Rizzo D("invalid cmd (nmr->nr_cmd) (0x%x)", cmd); 1626f18be576SLuigi Rizzo error = EINVAL; 1627f18be576SLuigi Rizzo break; 1628f18be576SLuigi Rizzo } 1629f18be576SLuigi Rizzo return error; 1630f18be576SLuigi Rizzo } 1631f18be576SLuigi Rizzo 1632f18be576SLuigi Rizzo 163368b8534bSLuigi Rizzo /* 163468b8534bSLuigi Rizzo * ioctl(2) support for the "netmap" device. 163568b8534bSLuigi Rizzo * 163668b8534bSLuigi Rizzo * Following a list of accepted commands: 163768b8534bSLuigi Rizzo * - NIOCGINFO 163868b8534bSLuigi Rizzo * - SIOCGIFADDR just for convenience 163968b8534bSLuigi Rizzo * - NIOCREGIF 164068b8534bSLuigi Rizzo * - NIOCUNREGIF 164168b8534bSLuigi Rizzo * - NIOCTXSYNC 164268b8534bSLuigi Rizzo * - NIOCRXSYNC 164368b8534bSLuigi Rizzo * 164468b8534bSLuigi Rizzo * Return 0 on success, errno otherwise. 164568b8534bSLuigi Rizzo */ 164668b8534bSLuigi Rizzo static int 16470b8ed8e0SLuigi Rizzo netmap_ioctl(struct cdev *dev, u_long cmd, caddr_t data, 16480b8ed8e0SLuigi Rizzo int fflag, struct thread *td) 164968b8534bSLuigi Rizzo { 165068b8534bSLuigi Rizzo struct netmap_priv_d *priv = NULL; 165168b8534bSLuigi Rizzo struct ifnet *ifp; 165268b8534bSLuigi Rizzo struct nmreq *nmr = (struct nmreq *) data; 165368b8534bSLuigi Rizzo struct netmap_adapter *na; 165468b8534bSLuigi Rizzo int error; 165564ae02c3SLuigi Rizzo u_int i, lim; 165668b8534bSLuigi Rizzo struct netmap_if *nifp; 165768b8534bSLuigi Rizzo 16580b8ed8e0SLuigi Rizzo (void)dev; /* UNUSED */ 16590b8ed8e0SLuigi Rizzo (void)fflag; /* UNUSED */ 1660f196ce38SLuigi Rizzo #ifdef linux 1661f196ce38SLuigi Rizzo #define devfs_get_cdevpriv(pp) \ 1662f196ce38SLuigi Rizzo ({ *(struct netmap_priv_d **)pp = ((struct file *)td)->private_data; \ 1663f196ce38SLuigi Rizzo (*pp ? 0 : ENOENT); }) 1664f196ce38SLuigi Rizzo 1665f196ce38SLuigi Rizzo /* devfs_set_cdevpriv cannot fail on linux */ 1666f196ce38SLuigi Rizzo #define devfs_set_cdevpriv(p, fn) \ 1667f196ce38SLuigi Rizzo ({ ((struct file *)td)->private_data = p; (p ? 0 : EINVAL); }) 1668f196ce38SLuigi Rizzo 1669f196ce38SLuigi Rizzo 1670f196ce38SLuigi Rizzo #define devfs_clear_cdevpriv() do { \ 1671f196ce38SLuigi Rizzo netmap_dtor(priv); ((struct file *)td)->private_data = 0; \ 1672f196ce38SLuigi Rizzo } while (0) 1673f196ce38SLuigi Rizzo #endif /* linux */ 1674f196ce38SLuigi Rizzo 1675506cc70cSLuigi Rizzo CURVNET_SET(TD_TO_VNET(td)); 1676506cc70cSLuigi Rizzo 167768b8534bSLuigi Rizzo error = devfs_get_cdevpriv((void **)&priv); 16788241616dSLuigi Rizzo if (error) { 1679506cc70cSLuigi Rizzo CURVNET_RESTORE(); 16808241616dSLuigi Rizzo /* XXX ENOENT should be impossible, since the priv 16818241616dSLuigi Rizzo * is now created in the open */ 16828241616dSLuigi Rizzo return (error == ENOENT ? ENXIO : error); 1683506cc70cSLuigi Rizzo } 168468b8534bSLuigi Rizzo 1685f196ce38SLuigi Rizzo nmr->nr_name[sizeof(nmr->nr_name) - 1] = '\0'; /* truncate name */ 168668b8534bSLuigi Rizzo switch (cmd) { 168768b8534bSLuigi Rizzo case NIOCGINFO: /* return capabilities etc */ 168864ae02c3SLuigi Rizzo if (nmr->nr_version != NETMAP_API) { 168964ae02c3SLuigi Rizzo D("API mismatch got %d have %d", 169064ae02c3SLuigi Rizzo nmr->nr_version, NETMAP_API); 169164ae02c3SLuigi Rizzo nmr->nr_version = NETMAP_API; 169264ae02c3SLuigi Rizzo error = EINVAL; 169364ae02c3SLuigi Rizzo break; 169464ae02c3SLuigi Rizzo } 1695f18be576SLuigi Rizzo if (nmr->nr_cmd == NETMAP_BDG_LIST) { 1696f18be576SLuigi Rizzo error = netmap_bdg_ctl(nmr, NULL); 1697f18be576SLuigi Rizzo break; 1698f18be576SLuigi Rizzo } 16998241616dSLuigi Rizzo /* update configuration */ 17008241616dSLuigi Rizzo error = netmap_get_memory(priv); 17018241616dSLuigi Rizzo ND("get_memory returned %d", error); 17028241616dSLuigi Rizzo if (error) 17038241616dSLuigi Rizzo break; 17048241616dSLuigi Rizzo /* memsize is always valid */ 17058241616dSLuigi Rizzo nmr->nr_memsize = nm_mem.nm_totalsize; 17068241616dSLuigi Rizzo nmr->nr_offset = 0; 17078241616dSLuigi Rizzo nmr->nr_rx_slots = nmr->nr_tx_slots = 0; 170868b8534bSLuigi Rizzo if (nmr->nr_name[0] == '\0') /* just get memory info */ 170968b8534bSLuigi Rizzo break; 1710f18be576SLuigi Rizzo /* lock because get_ifp and update_config see na->refcount */ 1711f18be576SLuigi Rizzo NMA_LOCK(); 1712f18be576SLuigi Rizzo error = get_ifp(nmr, &ifp); /* get a refcount */ 1713f18be576SLuigi Rizzo if (error) { 1714f18be576SLuigi Rizzo NMA_UNLOCK(); 171568b8534bSLuigi Rizzo break; 1716f18be576SLuigi Rizzo } 171768b8534bSLuigi Rizzo na = NA(ifp); /* retrieve netmap_adapter */ 1718ae10d1afSLuigi Rizzo netmap_update_config(na); 1719f18be576SLuigi Rizzo NMA_UNLOCK(); 1720d76bf4ffSLuigi Rizzo nmr->nr_rx_rings = na->num_rx_rings; 1721d76bf4ffSLuigi Rizzo nmr->nr_tx_rings = na->num_tx_rings; 172264ae02c3SLuigi Rizzo nmr->nr_rx_slots = na->num_rx_desc; 172364ae02c3SLuigi Rizzo nmr->nr_tx_slots = na->num_tx_desc; 1724f196ce38SLuigi Rizzo nm_if_rele(ifp); /* return the refcount */ 172568b8534bSLuigi Rizzo break; 172668b8534bSLuigi Rizzo 172768b8534bSLuigi Rizzo case NIOCREGIF: 172864ae02c3SLuigi Rizzo if (nmr->nr_version != NETMAP_API) { 172964ae02c3SLuigi Rizzo nmr->nr_version = NETMAP_API; 173064ae02c3SLuigi Rizzo error = EINVAL; 173164ae02c3SLuigi Rizzo break; 173264ae02c3SLuigi Rizzo } 1733f18be576SLuigi Rizzo /* possibly attach/detach NIC and VALE switch */ 1734f18be576SLuigi Rizzo i = nmr->nr_cmd; 1735f18be576SLuigi Rizzo if (i == NETMAP_BDG_ATTACH || i == NETMAP_BDG_DETACH) { 1736f18be576SLuigi Rizzo error = netmap_bdg_ctl(nmr, NULL); 1737f18be576SLuigi Rizzo break; 1738f18be576SLuigi Rizzo } else if (i != 0) { 1739f18be576SLuigi Rizzo D("nr_cmd must be 0 not %d", i); 1740f18be576SLuigi Rizzo error = EINVAL; 1741f18be576SLuigi Rizzo break; 1742f18be576SLuigi Rizzo } 1743f18be576SLuigi Rizzo 17448241616dSLuigi Rizzo /* ensure allocators are ready */ 17458241616dSLuigi Rizzo error = netmap_get_memory(priv); 17468241616dSLuigi Rizzo ND("get_memory returned %d", error); 17478241616dSLuigi Rizzo if (error) 17488241616dSLuigi Rizzo break; 17498241616dSLuigi Rizzo 17508241616dSLuigi Rizzo /* protect access to priv from concurrent NIOCREGIF */ 17518241616dSLuigi Rizzo NMA_LOCK(); 17528241616dSLuigi Rizzo if (priv->np_ifp != NULL) { /* thread already registered */ 1753506cc70cSLuigi Rizzo error = netmap_set_ringid(priv, nmr->nr_ringid); 1754f18be576SLuigi Rizzo unlock_out: 17558241616dSLuigi Rizzo NMA_UNLOCK(); 1756506cc70cSLuigi Rizzo break; 1757506cc70cSLuigi Rizzo } 175868b8534bSLuigi Rizzo /* find the interface and a reference */ 1759f18be576SLuigi Rizzo error = get_ifp(nmr, &ifp); /* keep reference */ 176068b8534bSLuigi Rizzo if (error) 1761f18be576SLuigi Rizzo goto unlock_out; 1762f18be576SLuigi Rizzo else if (NETMAP_OWNED_BY_KERN(ifp)) { 1763f18be576SLuigi Rizzo nm_if_rele(ifp); 1764f18be576SLuigi Rizzo goto unlock_out; 1765f196ce38SLuigi Rizzo } 1766f18be576SLuigi Rizzo nifp = netmap_do_regif(priv, ifp, nmr->nr_ringid, &error); 1767f18be576SLuigi Rizzo if (!nifp) { /* reg. failed, release priv and ref */ 1768f196ce38SLuigi Rizzo nm_if_rele(ifp); /* return the refcount */ 17698241616dSLuigi Rizzo priv->np_ifp = NULL; 17708241616dSLuigi Rizzo priv->np_nifp = NULL; 1771f18be576SLuigi Rizzo goto unlock_out; 177268b8534bSLuigi Rizzo } 177368b8534bSLuigi Rizzo 17748241616dSLuigi Rizzo /* the following assignment is a commitment. 17758241616dSLuigi Rizzo * Readers (i.e., poll and *SYNC) check for 17768241616dSLuigi Rizzo * np_nifp != NULL without locking 177768b8534bSLuigi Rizzo */ 17788241616dSLuigi Rizzo wmb(); /* make sure previous writes are visible to all CPUs */ 17798241616dSLuigi Rizzo priv->np_nifp = nifp; 17808241616dSLuigi Rizzo NMA_UNLOCK(); 178168b8534bSLuigi Rizzo 178268b8534bSLuigi Rizzo /* return the offset of the netmap_if object */ 1783f18be576SLuigi Rizzo na = NA(ifp); /* retrieve netmap adapter */ 1784d76bf4ffSLuigi Rizzo nmr->nr_rx_rings = na->num_rx_rings; 1785d76bf4ffSLuigi Rizzo nmr->nr_tx_rings = na->num_tx_rings; 178664ae02c3SLuigi Rizzo nmr->nr_rx_slots = na->num_rx_desc; 178764ae02c3SLuigi Rizzo nmr->nr_tx_slots = na->num_tx_desc; 17888241616dSLuigi Rizzo nmr->nr_memsize = nm_mem.nm_totalsize; 1789446ee301SLuigi Rizzo nmr->nr_offset = netmap_if_offset(nifp); 179068b8534bSLuigi Rizzo break; 179168b8534bSLuigi Rizzo 179268b8534bSLuigi Rizzo case NIOCUNREGIF: 17938241616dSLuigi Rizzo // XXX we have no data here ? 17948241616dSLuigi Rizzo D("deprecated, data is %p", nmr); 17958241616dSLuigi Rizzo error = EINVAL; 179668b8534bSLuigi Rizzo break; 179768b8534bSLuigi Rizzo 179868b8534bSLuigi Rizzo case NIOCTXSYNC: 179968b8534bSLuigi Rizzo case NIOCRXSYNC: 18008241616dSLuigi Rizzo nifp = priv->np_nifp; 18018241616dSLuigi Rizzo 18028241616dSLuigi Rizzo if (nifp == NULL) { 1803506cc70cSLuigi Rizzo error = ENXIO; 1804506cc70cSLuigi Rizzo break; 1805506cc70cSLuigi Rizzo } 18068241616dSLuigi Rizzo rmb(); /* make sure following reads are not from cache */ 18078241616dSLuigi Rizzo 18088241616dSLuigi Rizzo 180968b8534bSLuigi Rizzo ifp = priv->np_ifp; /* we have a reference */ 18108241616dSLuigi Rizzo 18118241616dSLuigi Rizzo if (ifp == NULL) { 18128241616dSLuigi Rizzo D("Internal error: nifp != NULL && ifp == NULL"); 18138241616dSLuigi Rizzo error = ENXIO; 18148241616dSLuigi Rizzo break; 18158241616dSLuigi Rizzo } 18168241616dSLuigi Rizzo 181768b8534bSLuigi Rizzo na = NA(ifp); /* retrieve netmap adapter */ 181864ae02c3SLuigi Rizzo if (priv->np_qfirst == NETMAP_SW_RING) { /* host rings */ 181968b8534bSLuigi Rizzo if (cmd == NIOCTXSYNC) 182068b8534bSLuigi Rizzo netmap_sync_to_host(na); 182168b8534bSLuigi Rizzo else 182201c7d25fSLuigi Rizzo netmap_sync_from_host(na, NULL, NULL); 1823506cc70cSLuigi Rizzo break; 182468b8534bSLuigi Rizzo } 182564ae02c3SLuigi Rizzo /* find the last ring to scan */ 182664ae02c3SLuigi Rizzo lim = priv->np_qlast; 182764ae02c3SLuigi Rizzo if (lim == NETMAP_HW_RING) 18283c0caf6cSLuigi Rizzo lim = (cmd == NIOCTXSYNC) ? 1829d76bf4ffSLuigi Rizzo na->num_tx_rings : na->num_rx_rings; 183068b8534bSLuigi Rizzo 183164ae02c3SLuigi Rizzo for (i = priv->np_qfirst; i < lim; i++) { 183268b8534bSLuigi Rizzo if (cmd == NIOCTXSYNC) { 183368b8534bSLuigi Rizzo struct netmap_kring *kring = &na->tx_rings[i]; 183468b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_TXSYNC) 18353c0caf6cSLuigi Rizzo D("pre txsync ring %d cur %d hwcur %d", 183668b8534bSLuigi Rizzo i, kring->ring->cur, 183768b8534bSLuigi Rizzo kring->nr_hwcur); 18381a26580eSLuigi Rizzo na->nm_txsync(ifp, i, 1 /* do lock */); 183968b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_TXSYNC) 18403c0caf6cSLuigi Rizzo D("post txsync ring %d cur %d hwcur %d", 184168b8534bSLuigi Rizzo i, kring->ring->cur, 184268b8534bSLuigi Rizzo kring->nr_hwcur); 184368b8534bSLuigi Rizzo } else { 18441a26580eSLuigi Rizzo na->nm_rxsync(ifp, i, 1 /* do lock */); 184568b8534bSLuigi Rizzo microtime(&na->rx_rings[i].ring->ts); 184668b8534bSLuigi Rizzo } 184768b8534bSLuigi Rizzo } 184868b8534bSLuigi Rizzo 184968b8534bSLuigi Rizzo break; 185068b8534bSLuigi Rizzo 1851f196ce38SLuigi Rizzo #ifdef __FreeBSD__ 185268b8534bSLuigi Rizzo case BIOCIMMEDIATE: 185368b8534bSLuigi Rizzo case BIOCGHDRCMPLT: 185468b8534bSLuigi Rizzo case BIOCSHDRCMPLT: 185568b8534bSLuigi Rizzo case BIOCSSEESENT: 185668b8534bSLuigi Rizzo D("ignore BIOCIMMEDIATE/BIOCSHDRCMPLT/BIOCSHDRCMPLT/BIOCSSEESENT"); 185768b8534bSLuigi Rizzo break; 185868b8534bSLuigi Rizzo 1859babc7c12SLuigi Rizzo default: /* allow device-specific ioctls */ 186068b8534bSLuigi Rizzo { 186168b8534bSLuigi Rizzo struct socket so; 186268b8534bSLuigi Rizzo bzero(&so, sizeof(so)); 1863f18be576SLuigi Rizzo error = get_ifp(nmr, &ifp); /* keep reference */ 186468b8534bSLuigi Rizzo if (error) 186568b8534bSLuigi Rizzo break; 186668b8534bSLuigi Rizzo so.so_vnet = ifp->if_vnet; 186768b8534bSLuigi Rizzo // so->so_proto not null. 186868b8534bSLuigi Rizzo error = ifioctl(&so, cmd, data, td); 1869f196ce38SLuigi Rizzo nm_if_rele(ifp); 1870babc7c12SLuigi Rizzo break; 187168b8534bSLuigi Rizzo } 1872f196ce38SLuigi Rizzo 1873f196ce38SLuigi Rizzo #else /* linux */ 1874f196ce38SLuigi Rizzo default: 1875f196ce38SLuigi Rizzo error = EOPNOTSUPP; 1876f196ce38SLuigi Rizzo #endif /* linux */ 187768b8534bSLuigi Rizzo } 187868b8534bSLuigi Rizzo 1879506cc70cSLuigi Rizzo CURVNET_RESTORE(); 188068b8534bSLuigi Rizzo return (error); 188168b8534bSLuigi Rizzo } 188268b8534bSLuigi Rizzo 188368b8534bSLuigi Rizzo 188468b8534bSLuigi Rizzo /* 188568b8534bSLuigi Rizzo * select(2) and poll(2) handlers for the "netmap" device. 188668b8534bSLuigi Rizzo * 188768b8534bSLuigi Rizzo * Can be called for one or more queues. 188868b8534bSLuigi Rizzo * Return true the event mask corresponding to ready events. 188968b8534bSLuigi Rizzo * If there are no ready events, do a selrecord on either individual 189068b8534bSLuigi Rizzo * selfd or on the global one. 189168b8534bSLuigi Rizzo * Device-dependent parts (locking and sync of tx/rx rings) 189268b8534bSLuigi Rizzo * are done through callbacks. 1893f196ce38SLuigi Rizzo * 189401c7d25fSLuigi Rizzo * On linux, arguments are really pwait, the poll table, and 'td' is struct file * 189501c7d25fSLuigi Rizzo * The first one is remapped to pwait as selrecord() uses the name as an 189601c7d25fSLuigi Rizzo * hidden argument. 189768b8534bSLuigi Rizzo */ 189868b8534bSLuigi Rizzo static int 189901c7d25fSLuigi Rizzo netmap_poll(struct cdev *dev, int events, struct thread *td) 190068b8534bSLuigi Rizzo { 190168b8534bSLuigi Rizzo struct netmap_priv_d *priv = NULL; 190268b8534bSLuigi Rizzo struct netmap_adapter *na; 190368b8534bSLuigi Rizzo struct ifnet *ifp; 190468b8534bSLuigi Rizzo struct netmap_kring *kring; 1905d0c7b075SLuigi Rizzo u_int core_lock, i, check_all, want_tx, want_rx, revents = 0; 1906091fd0abSLuigi Rizzo u_int lim_tx, lim_rx, host_forwarded = 0; 1907091fd0abSLuigi Rizzo struct mbq q = { NULL, NULL, 0 }; 1908bcda432eSLuigi Rizzo enum {NO_CL, NEED_CL, LOCKED_CL }; /* see below */ 190901c7d25fSLuigi Rizzo void *pwait = dev; /* linux compatibility */ 191001c7d25fSLuigi Rizzo 191101c7d25fSLuigi Rizzo (void)pwait; 191268b8534bSLuigi Rizzo 191368b8534bSLuigi Rizzo if (devfs_get_cdevpriv((void **)&priv) != 0 || priv == NULL) 191468b8534bSLuigi Rizzo return POLLERR; 191568b8534bSLuigi Rizzo 19168241616dSLuigi Rizzo if (priv->np_nifp == NULL) { 19178241616dSLuigi Rizzo D("No if registered"); 19188241616dSLuigi Rizzo return POLLERR; 19198241616dSLuigi Rizzo } 19208241616dSLuigi Rizzo rmb(); /* make sure following reads are not from cache */ 19218241616dSLuigi Rizzo 192268b8534bSLuigi Rizzo ifp = priv->np_ifp; 192368b8534bSLuigi Rizzo // XXX check for deleting() ? 192468b8534bSLuigi Rizzo if ( (ifp->if_capenable & IFCAP_NETMAP) == 0) 192568b8534bSLuigi Rizzo return POLLERR; 192668b8534bSLuigi Rizzo 192768b8534bSLuigi Rizzo if (netmap_verbose & 0x8000) 192868b8534bSLuigi Rizzo D("device %s events 0x%x", ifp->if_xname, events); 192968b8534bSLuigi Rizzo want_tx = events & (POLLOUT | POLLWRNORM); 193068b8534bSLuigi Rizzo want_rx = events & (POLLIN | POLLRDNORM); 193168b8534bSLuigi Rizzo 193268b8534bSLuigi Rizzo na = NA(ifp); /* retrieve netmap adapter */ 193368b8534bSLuigi Rizzo 1934d76bf4ffSLuigi Rizzo lim_tx = na->num_tx_rings; 1935d76bf4ffSLuigi Rizzo lim_rx = na->num_rx_rings; 193668b8534bSLuigi Rizzo /* how many queues we are scanning */ 193764ae02c3SLuigi Rizzo if (priv->np_qfirst == NETMAP_SW_RING) { 193868b8534bSLuigi Rizzo if (priv->np_txpoll || want_tx) { 193968b8534bSLuigi Rizzo /* push any packets up, then we are always ready */ 194068b8534bSLuigi Rizzo netmap_sync_to_host(na); 194168b8534bSLuigi Rizzo revents |= want_tx; 194268b8534bSLuigi Rizzo } 194368b8534bSLuigi Rizzo if (want_rx) { 194464ae02c3SLuigi Rizzo kring = &na->rx_rings[lim_rx]; 194568b8534bSLuigi Rizzo if (kring->ring->avail == 0) 194601c7d25fSLuigi Rizzo netmap_sync_from_host(na, td, dev); 194768b8534bSLuigi Rizzo if (kring->ring->avail > 0) { 194868b8534bSLuigi Rizzo revents |= want_rx; 194968b8534bSLuigi Rizzo } 195068b8534bSLuigi Rizzo } 195168b8534bSLuigi Rizzo return (revents); 195268b8534bSLuigi Rizzo } 195368b8534bSLuigi Rizzo 1954091fd0abSLuigi Rizzo /* if we are in transparent mode, check also the host rx ring */ 1955091fd0abSLuigi Rizzo kring = &na->rx_rings[lim_rx]; 1956091fd0abSLuigi Rizzo if ( (priv->np_qlast == NETMAP_HW_RING) // XXX check_all 1957091fd0abSLuigi Rizzo && want_rx 1958091fd0abSLuigi Rizzo && (netmap_fwd || kring->ring->flags & NR_FORWARD) ) { 1959091fd0abSLuigi Rizzo if (kring->ring->avail == 0) 1960091fd0abSLuigi Rizzo netmap_sync_from_host(na, td, dev); 1961091fd0abSLuigi Rizzo if (kring->ring->avail > 0) 1962091fd0abSLuigi Rizzo revents |= want_rx; 1963091fd0abSLuigi Rizzo } 1964091fd0abSLuigi Rizzo 196568b8534bSLuigi Rizzo /* 196668b8534bSLuigi Rizzo * check_all is set if the card has more than one queue and 196768b8534bSLuigi Rizzo * the client is polling all of them. If true, we sleep on 196868b8534bSLuigi Rizzo * the "global" selfd, otherwise we sleep on individual selfd 196968b8534bSLuigi Rizzo * (we can only sleep on one of them per direction). 197068b8534bSLuigi Rizzo * The interrupt routine in the driver should always wake on 197168b8534bSLuigi Rizzo * the individual selfd, and also on the global one if the card 197268b8534bSLuigi Rizzo * has more than one ring. 197368b8534bSLuigi Rizzo * 197468b8534bSLuigi Rizzo * If the card has only one lock, we just use that. 197568b8534bSLuigi Rizzo * If the card has separate ring locks, we just use those 197668b8534bSLuigi Rizzo * unless we are doing check_all, in which case the whole 197768b8534bSLuigi Rizzo * loop is wrapped by the global lock. 197868b8534bSLuigi Rizzo * We acquire locks only when necessary: if poll is called 197968b8534bSLuigi Rizzo * when buffers are available, we can just return without locks. 198068b8534bSLuigi Rizzo * 198168b8534bSLuigi Rizzo * rxsync() is only called if we run out of buffers on a POLLIN. 198268b8534bSLuigi Rizzo * txsync() is called if we run out of buffers on POLLOUT, or 198368b8534bSLuigi Rizzo * there are pending packets to send. The latter can be disabled 198468b8534bSLuigi Rizzo * passing NETMAP_NO_TX_POLL in the NIOCREG call. 198568b8534bSLuigi Rizzo */ 198664ae02c3SLuigi Rizzo check_all = (priv->np_qlast == NETMAP_HW_RING) && (lim_tx > 1 || lim_rx > 1); 198768b8534bSLuigi Rizzo 198868b8534bSLuigi Rizzo /* 198968b8534bSLuigi Rizzo * core_lock indicates what to do with the core lock. 199068b8534bSLuigi Rizzo * The core lock is used when either the card has no individual 199168b8534bSLuigi Rizzo * locks, or it has individual locks but we are cheking all 199268b8534bSLuigi Rizzo * rings so we need the core lock to avoid missing wakeup events. 199368b8534bSLuigi Rizzo * 199468b8534bSLuigi Rizzo * It has three possible states: 199568b8534bSLuigi Rizzo * NO_CL we don't need to use the core lock, e.g. 199668b8534bSLuigi Rizzo * because we are protected by individual locks. 199768b8534bSLuigi Rizzo * NEED_CL we need the core lock. In this case, when we 199868b8534bSLuigi Rizzo * call the lock routine, move to LOCKED_CL 199968b8534bSLuigi Rizzo * to remember to release the lock once done. 200068b8534bSLuigi Rizzo * LOCKED_CL core lock is set, so we need to release it. 200168b8534bSLuigi Rizzo */ 2002d0c7b075SLuigi Rizzo core_lock = (check_all || !na->separate_locks) ? NEED_CL : NO_CL; 2003f196ce38SLuigi Rizzo #ifdef NM_BRIDGE 2004f196ce38SLuigi Rizzo /* the bridge uses separate locks */ 2005f196ce38SLuigi Rizzo if (na->nm_register == bdg_netmap_reg) { 2006f196ce38SLuigi Rizzo ND("not using core lock for %s", ifp->if_xname); 2007f196ce38SLuigi Rizzo core_lock = NO_CL; 2008f196ce38SLuigi Rizzo } 2009f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */ 201064ae02c3SLuigi Rizzo if (priv->np_qlast != NETMAP_HW_RING) { 201164ae02c3SLuigi Rizzo lim_tx = lim_rx = priv->np_qlast; 201264ae02c3SLuigi Rizzo } 201364ae02c3SLuigi Rizzo 201468b8534bSLuigi Rizzo /* 201568b8534bSLuigi Rizzo * We start with a lock free round which is good if we have 201668b8534bSLuigi Rizzo * data available. If this fails, then lock and call the sync 201768b8534bSLuigi Rizzo * routines. 201868b8534bSLuigi Rizzo */ 201964ae02c3SLuigi Rizzo for (i = priv->np_qfirst; want_rx && i < lim_rx; i++) { 202068b8534bSLuigi Rizzo kring = &na->rx_rings[i]; 202168b8534bSLuigi Rizzo if (kring->ring->avail > 0) { 202268b8534bSLuigi Rizzo revents |= want_rx; 202368b8534bSLuigi Rizzo want_rx = 0; /* also breaks the loop */ 202468b8534bSLuigi Rizzo } 202568b8534bSLuigi Rizzo } 202664ae02c3SLuigi Rizzo for (i = priv->np_qfirst; want_tx && i < lim_tx; i++) { 202768b8534bSLuigi Rizzo kring = &na->tx_rings[i]; 202868b8534bSLuigi Rizzo if (kring->ring->avail > 0) { 202968b8534bSLuigi Rizzo revents |= want_tx; 203068b8534bSLuigi Rizzo want_tx = 0; /* also breaks the loop */ 203168b8534bSLuigi Rizzo } 203268b8534bSLuigi Rizzo } 203368b8534bSLuigi Rizzo 203468b8534bSLuigi Rizzo /* 203568b8534bSLuigi Rizzo * If we to push packets out (priv->np_txpoll) or want_tx is 203668b8534bSLuigi Rizzo * still set, we do need to run the txsync calls (on all rings, 203768b8534bSLuigi Rizzo * to avoid that the tx rings stall). 203868b8534bSLuigi Rizzo */ 203968b8534bSLuigi Rizzo if (priv->np_txpoll || want_tx) { 2040091fd0abSLuigi Rizzo flush_tx: 204164ae02c3SLuigi Rizzo for (i = priv->np_qfirst; i < lim_tx; i++) { 204268b8534bSLuigi Rizzo kring = &na->tx_rings[i]; 20435819da83SLuigi Rizzo /* 20445819da83SLuigi Rizzo * Skip the current ring if want_tx == 0 20455819da83SLuigi Rizzo * (we have already done a successful sync on 20465819da83SLuigi Rizzo * a previous ring) AND kring->cur == kring->hwcur 20475819da83SLuigi Rizzo * (there are no pending transmissions for this ring). 20485819da83SLuigi Rizzo */ 204968b8534bSLuigi Rizzo if (!want_tx && kring->ring->cur == kring->nr_hwcur) 205068b8534bSLuigi Rizzo continue; 205168b8534bSLuigi Rizzo if (core_lock == NEED_CL) { 20521a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_LOCK, 0); 205368b8534bSLuigi Rizzo core_lock = LOCKED_CL; 205468b8534bSLuigi Rizzo } 205568b8534bSLuigi Rizzo if (na->separate_locks) 20561a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_TX_LOCK, i); 205768b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_TXSYNC) 205868b8534bSLuigi Rizzo D("send %d on %s %d", 205968b8534bSLuigi Rizzo kring->ring->cur, 206068b8534bSLuigi Rizzo ifp->if_xname, i); 20611a26580eSLuigi Rizzo if (na->nm_txsync(ifp, i, 0 /* no lock */)) 206268b8534bSLuigi Rizzo revents |= POLLERR; 206368b8534bSLuigi Rizzo 20645819da83SLuigi Rizzo /* Check avail/call selrecord only if called with POLLOUT */ 206568b8534bSLuigi Rizzo if (want_tx) { 206668b8534bSLuigi Rizzo if (kring->ring->avail > 0) { 206768b8534bSLuigi Rizzo /* stop at the first ring. We don't risk 206868b8534bSLuigi Rizzo * starvation. 206968b8534bSLuigi Rizzo */ 207068b8534bSLuigi Rizzo revents |= want_tx; 207168b8534bSLuigi Rizzo want_tx = 0; 207268b8534bSLuigi Rizzo } else if (!check_all) 207368b8534bSLuigi Rizzo selrecord(td, &kring->si); 207468b8534bSLuigi Rizzo } 207568b8534bSLuigi Rizzo if (na->separate_locks) 20761a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_TX_UNLOCK, i); 207768b8534bSLuigi Rizzo } 207868b8534bSLuigi Rizzo } 207968b8534bSLuigi Rizzo 208068b8534bSLuigi Rizzo /* 208168b8534bSLuigi Rizzo * now if want_rx is still set we need to lock and rxsync. 208268b8534bSLuigi Rizzo * Do it on all rings because otherwise we starve. 208368b8534bSLuigi Rizzo */ 208468b8534bSLuigi Rizzo if (want_rx) { 208564ae02c3SLuigi Rizzo for (i = priv->np_qfirst; i < lim_rx; i++) { 208668b8534bSLuigi Rizzo kring = &na->rx_rings[i]; 208768b8534bSLuigi Rizzo if (core_lock == NEED_CL) { 20881a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_LOCK, 0); 208968b8534bSLuigi Rizzo core_lock = LOCKED_CL; 209068b8534bSLuigi Rizzo } 209168b8534bSLuigi Rizzo if (na->separate_locks) 20921a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_RX_LOCK, i); 2093091fd0abSLuigi Rizzo if (netmap_fwd ||kring->ring->flags & NR_FORWARD) { 2094091fd0abSLuigi Rizzo ND(10, "forwarding some buffers up %d to %d", 2095091fd0abSLuigi Rizzo kring->nr_hwcur, kring->ring->cur); 2096091fd0abSLuigi Rizzo netmap_grab_packets(kring, &q, netmap_fwd); 2097091fd0abSLuigi Rizzo } 209868b8534bSLuigi Rizzo 20991a26580eSLuigi Rizzo if (na->nm_rxsync(ifp, i, 0 /* no lock */)) 210068b8534bSLuigi Rizzo revents |= POLLERR; 21015819da83SLuigi Rizzo if (netmap_no_timestamp == 0 || 21025819da83SLuigi Rizzo kring->ring->flags & NR_TIMESTAMP) { 210368b8534bSLuigi Rizzo microtime(&kring->ring->ts); 21045819da83SLuigi Rizzo } 210568b8534bSLuigi Rizzo 210668b8534bSLuigi Rizzo if (kring->ring->avail > 0) 210768b8534bSLuigi Rizzo revents |= want_rx; 210868b8534bSLuigi Rizzo else if (!check_all) 210968b8534bSLuigi Rizzo selrecord(td, &kring->si); 211068b8534bSLuigi Rizzo if (na->separate_locks) 21111a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_RX_UNLOCK, i); 211268b8534bSLuigi Rizzo } 211368b8534bSLuigi Rizzo } 211464ae02c3SLuigi Rizzo if (check_all && revents == 0) { /* signal on the global queue */ 211568b8534bSLuigi Rizzo if (want_tx) 211664ae02c3SLuigi Rizzo selrecord(td, &na->tx_si); 211768b8534bSLuigi Rizzo if (want_rx) 211864ae02c3SLuigi Rizzo selrecord(td, &na->rx_si); 211968b8534bSLuigi Rizzo } 2120091fd0abSLuigi Rizzo 2121091fd0abSLuigi Rizzo /* forward host to the netmap ring */ 2122091fd0abSLuigi Rizzo kring = &na->rx_rings[lim_rx]; 2123091fd0abSLuigi Rizzo if (kring->nr_hwavail > 0) 2124091fd0abSLuigi Rizzo ND("host rx %d has %d packets", lim_rx, kring->nr_hwavail); 2125091fd0abSLuigi Rizzo if ( (priv->np_qlast == NETMAP_HW_RING) // XXX check_all 2126091fd0abSLuigi Rizzo && (netmap_fwd || kring->ring->flags & NR_FORWARD) 2127091fd0abSLuigi Rizzo && kring->nr_hwavail > 0 && !host_forwarded) { 2128091fd0abSLuigi Rizzo if (core_lock == NEED_CL) { 2129091fd0abSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_LOCK, 0); 2130091fd0abSLuigi Rizzo core_lock = LOCKED_CL; 2131091fd0abSLuigi Rizzo } 2132091fd0abSLuigi Rizzo netmap_sw_to_nic(na); 2133091fd0abSLuigi Rizzo host_forwarded = 1; /* prevent another pass */ 2134091fd0abSLuigi Rizzo want_rx = 0; 2135091fd0abSLuigi Rizzo goto flush_tx; 2136091fd0abSLuigi Rizzo } 2137091fd0abSLuigi Rizzo 213868b8534bSLuigi Rizzo if (core_lock == LOCKED_CL) 21391a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0); 2140091fd0abSLuigi Rizzo if (q.head) 2141091fd0abSLuigi Rizzo netmap_send_up(na->ifp, q.head); 214268b8534bSLuigi Rizzo 214368b8534bSLuigi Rizzo return (revents); 214468b8534bSLuigi Rizzo } 214568b8534bSLuigi Rizzo 214668b8534bSLuigi Rizzo /*------- driver support routines ------*/ 214768b8534bSLuigi Rizzo 2148f18be576SLuigi Rizzo 214968b8534bSLuigi Rizzo /* 2150babc7c12SLuigi Rizzo * default lock wrapper. 21511a26580eSLuigi Rizzo */ 21521a26580eSLuigi Rizzo static void 2153babc7c12SLuigi Rizzo netmap_lock_wrapper(struct ifnet *dev, int what, u_int queueid) 21541a26580eSLuigi Rizzo { 2155babc7c12SLuigi Rizzo struct netmap_adapter *na = NA(dev); 21561a26580eSLuigi Rizzo 21571a26580eSLuigi Rizzo switch (what) { 2158babc7c12SLuigi Rizzo #ifdef linux /* some system do not need lock on register */ 21591a26580eSLuigi Rizzo case NETMAP_REG_LOCK: 21601a26580eSLuigi Rizzo case NETMAP_REG_UNLOCK: 21611a26580eSLuigi Rizzo break; 2162babc7c12SLuigi Rizzo #endif /* linux */ 21631a26580eSLuigi Rizzo 21641a26580eSLuigi Rizzo case NETMAP_CORE_LOCK: 21651a26580eSLuigi Rizzo mtx_lock(&na->core_lock); 21661a26580eSLuigi Rizzo break; 21671a26580eSLuigi Rizzo 21681a26580eSLuigi Rizzo case NETMAP_CORE_UNLOCK: 21691a26580eSLuigi Rizzo mtx_unlock(&na->core_lock); 21701a26580eSLuigi Rizzo break; 21711a26580eSLuigi Rizzo 21721a26580eSLuigi Rizzo case NETMAP_TX_LOCK: 21731a26580eSLuigi Rizzo mtx_lock(&na->tx_rings[queueid].q_lock); 21741a26580eSLuigi Rizzo break; 21751a26580eSLuigi Rizzo 21761a26580eSLuigi Rizzo case NETMAP_TX_UNLOCK: 21771a26580eSLuigi Rizzo mtx_unlock(&na->tx_rings[queueid].q_lock); 21781a26580eSLuigi Rizzo break; 21791a26580eSLuigi Rizzo 21801a26580eSLuigi Rizzo case NETMAP_RX_LOCK: 21811a26580eSLuigi Rizzo mtx_lock(&na->rx_rings[queueid].q_lock); 21821a26580eSLuigi Rizzo break; 21831a26580eSLuigi Rizzo 21841a26580eSLuigi Rizzo case NETMAP_RX_UNLOCK: 21851a26580eSLuigi Rizzo mtx_unlock(&na->rx_rings[queueid].q_lock); 21861a26580eSLuigi Rizzo break; 21871a26580eSLuigi Rizzo } 21881a26580eSLuigi Rizzo } 21891a26580eSLuigi Rizzo 21901a26580eSLuigi Rizzo 21911a26580eSLuigi Rizzo /* 219268b8534bSLuigi Rizzo * Initialize a ``netmap_adapter`` object created by driver on attach. 219368b8534bSLuigi Rizzo * We allocate a block of memory with room for a struct netmap_adapter 219468b8534bSLuigi Rizzo * plus two sets of N+2 struct netmap_kring (where N is the number 219568b8534bSLuigi Rizzo * of hardware rings): 219668b8534bSLuigi Rizzo * krings 0..N-1 are for the hardware queues. 219768b8534bSLuigi Rizzo * kring N is for the host stack queue 219868b8534bSLuigi Rizzo * kring N+1 is only used for the selinfo for all queues. 219968b8534bSLuigi Rizzo * Return 0 on success, ENOMEM otherwise. 220064ae02c3SLuigi Rizzo * 22010bf88954SEd Maste * By default the receive and transmit adapter ring counts are both initialized 22020bf88954SEd Maste * to num_queues. na->num_tx_rings can be set for cards with different tx/rx 220324e57ec9SEd Maste * setups. 220468b8534bSLuigi Rizzo */ 220568b8534bSLuigi Rizzo int 2206ae10d1afSLuigi Rizzo netmap_attach(struct netmap_adapter *arg, int num_queues) 220768b8534bSLuigi Rizzo { 2208ae10d1afSLuigi Rizzo struct netmap_adapter *na = NULL; 2209ae10d1afSLuigi Rizzo struct ifnet *ifp = arg ? arg->ifp : NULL; 2210f18be576SLuigi Rizzo int len; 221168b8534bSLuigi Rizzo 2212ae10d1afSLuigi Rizzo if (arg == NULL || ifp == NULL) 2213ae10d1afSLuigi Rizzo goto fail; 2214f18be576SLuigi Rizzo len = nma_is_vp(arg) ? sizeof(*na) : sizeof(*na) * 2; 2215f18be576SLuigi Rizzo na = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO); 2216ae10d1afSLuigi Rizzo if (na == NULL) 2217ae10d1afSLuigi Rizzo goto fail; 2218ae10d1afSLuigi Rizzo WNA(ifp) = na; 2219ae10d1afSLuigi Rizzo *na = *arg; /* copy everything, trust the driver to not pass junk */ 2220ae10d1afSLuigi Rizzo NETMAP_SET_CAPABLE(ifp); 2221d76bf4ffSLuigi Rizzo if (na->num_tx_rings == 0) 2222d76bf4ffSLuigi Rizzo na->num_tx_rings = num_queues; 2223d76bf4ffSLuigi Rizzo na->num_rx_rings = num_queues; 2224ae10d1afSLuigi Rizzo na->refcount = na->na_single = na->na_multi = 0; 2225ae10d1afSLuigi Rizzo /* Core lock initialized here, others after netmap_if_new. */ 2226ae10d1afSLuigi Rizzo mtx_init(&na->core_lock, "netmap core lock", MTX_NETWORK_LOCK, MTX_DEF); 2227f196ce38SLuigi Rizzo if (na->nm_lock == NULL) { 2228f196ce38SLuigi Rizzo ND("using default locks for %s", ifp->if_xname); 22291a26580eSLuigi Rizzo na->nm_lock = netmap_lock_wrapper; 2230f196ce38SLuigi Rizzo } 223164ae02c3SLuigi Rizzo #ifdef linux 2232f18be576SLuigi Rizzo if (ifp->netdev_ops) { 2233f18be576SLuigi Rizzo ND("netdev_ops %p", ifp->netdev_ops); 2234f18be576SLuigi Rizzo /* prepare a clone of the netdev ops */ 2235f18be576SLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28) 2236f18be576SLuigi Rizzo na->nm_ndo.ndo_start_xmit = ifp->netdev_ops; 2237f18be576SLuigi Rizzo #else 2238849bec0eSLuigi Rizzo na->nm_ndo = *ifp->netdev_ops; 2239f18be576SLuigi Rizzo #endif 2240f18be576SLuigi Rizzo } 224142a3a5bdSLuigi Rizzo na->nm_ndo.ndo_start_xmit = linux_netmap_start; 2242f18be576SLuigi Rizzo #endif 2243f18be576SLuigi Rizzo if (!nma_is_vp(arg)) 2244f18be576SLuigi Rizzo netmap_attach_sw(ifp); 2245ae10d1afSLuigi Rizzo D("success for %s", ifp->if_xname); 2246ae10d1afSLuigi Rizzo return 0; 224768b8534bSLuigi Rizzo 2248ae10d1afSLuigi Rizzo fail: 2249ae10d1afSLuigi Rizzo D("fail, arg %p ifp %p na %p", arg, ifp, na); 2250849bec0eSLuigi Rizzo netmap_detach(ifp); 2251ae10d1afSLuigi Rizzo return (na ? EINVAL : ENOMEM); 225268b8534bSLuigi Rizzo } 225368b8534bSLuigi Rizzo 225468b8534bSLuigi Rizzo 225568b8534bSLuigi Rizzo /* 225668b8534bSLuigi Rizzo * Free the allocated memory linked to the given ``netmap_adapter`` 225768b8534bSLuigi Rizzo * object. 225868b8534bSLuigi Rizzo */ 225968b8534bSLuigi Rizzo void 226068b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp) 226168b8534bSLuigi Rizzo { 226268b8534bSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 226368b8534bSLuigi Rizzo 226468b8534bSLuigi Rizzo if (!na) 226568b8534bSLuigi Rizzo return; 226668b8534bSLuigi Rizzo 22672f70fca5SEd Maste mtx_destroy(&na->core_lock); 22682f70fca5SEd Maste 2269ae10d1afSLuigi Rizzo if (na->tx_rings) { /* XXX should not happen */ 2270ae10d1afSLuigi Rizzo D("freeing leftover tx_rings"); 2271ae10d1afSLuigi Rizzo free(na->tx_rings, M_DEVBUF); 2272ae10d1afSLuigi Rizzo } 227368b8534bSLuigi Rizzo bzero(na, sizeof(*na)); 2274d0c7b075SLuigi Rizzo WNA(ifp) = NULL; 227568b8534bSLuigi Rizzo free(na, M_DEVBUF); 227668b8534bSLuigi Rizzo } 227768b8534bSLuigi Rizzo 227868b8534bSLuigi Rizzo 2279f18be576SLuigi Rizzo int 2280f18be576SLuigi Rizzo nm_bdg_flush(struct nm_bdg_fwd *ft, int n, struct netmap_adapter *na, u_int ring_nr); 2281f18be576SLuigi Rizzo 2282f18be576SLuigi Rizzo /* we don't need to lock myself */ 2283f18be576SLuigi Rizzo static int 2284f18be576SLuigi Rizzo bdg_netmap_start(struct ifnet *ifp, struct mbuf *m) 2285f18be576SLuigi Rizzo { 2286f18be576SLuigi Rizzo struct netmap_adapter *na = SWNA(ifp); 2287f18be576SLuigi Rizzo struct nm_bdg_fwd *ft = na->rx_rings[0].nkr_ft; 2288f18be576SLuigi Rizzo char *buf = NMB(&na->rx_rings[0].ring->slot[0]); 2289f18be576SLuigi Rizzo u_int len = MBUF_LEN(m); 2290f18be576SLuigi Rizzo 2291f18be576SLuigi Rizzo if (!na->na_bdg) /* SWNA is not configured to be attached */ 2292f18be576SLuigi Rizzo return EBUSY; 2293f18be576SLuigi Rizzo m_copydata(m, 0, len, buf); 229485233a7dSLuigi Rizzo ft->ft_flags = 0; // XXX could be indirect ? 2295f18be576SLuigi Rizzo ft->ft_len = len; 229685233a7dSLuigi Rizzo ft->ft_buf = buf; 229785233a7dSLuigi Rizzo ft->ft_next = NM_BDG_BATCH; // XXX is it needed ? 2298f18be576SLuigi Rizzo nm_bdg_flush(ft, 1, na, 0); 2299f18be576SLuigi Rizzo 2300f18be576SLuigi Rizzo /* release the mbuf in either cases of success or failure. As an 2301f18be576SLuigi Rizzo * alternative, put the mbuf in a free list and free the list 2302f18be576SLuigi Rizzo * only when really necessary. 2303f18be576SLuigi Rizzo */ 2304f18be576SLuigi Rizzo m_freem(m); 2305f18be576SLuigi Rizzo 2306f18be576SLuigi Rizzo return (0); 2307f18be576SLuigi Rizzo } 2308f18be576SLuigi Rizzo 2309f18be576SLuigi Rizzo 231068b8534bSLuigi Rizzo /* 231102ad4083SLuigi Rizzo * Intercept packets from the network stack and pass them 231202ad4083SLuigi Rizzo * to netmap as incoming packets on the 'software' ring. 231368b8534bSLuigi Rizzo * We are not locked when called. 231468b8534bSLuigi Rizzo */ 231568b8534bSLuigi Rizzo int 231668b8534bSLuigi Rizzo netmap_start(struct ifnet *ifp, struct mbuf *m) 231768b8534bSLuigi Rizzo { 231868b8534bSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 2319d76bf4ffSLuigi Rizzo struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings]; 23201a26580eSLuigi Rizzo u_int i, len = MBUF_LEN(m); 2321b3d53016SLuigi Rizzo u_int error = EBUSY, lim = kring->nkr_num_slots - 1; 232268b8534bSLuigi Rizzo struct netmap_slot *slot; 232368b8534bSLuigi Rizzo 232468b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_HOST) 232568b8534bSLuigi Rizzo D("%s packet %d len %d from the stack", ifp->if_xname, 232668b8534bSLuigi Rizzo kring->nr_hwcur + kring->nr_hwavail, len); 2327849bec0eSLuigi Rizzo if (len > NETMAP_BUF_SIZE) { /* too long for us */ 2328849bec0eSLuigi Rizzo D("%s from_host, drop packet size %d > %d", ifp->if_xname, 2329849bec0eSLuigi Rizzo len, NETMAP_BUF_SIZE); 2330849bec0eSLuigi Rizzo m_freem(m); 2331849bec0eSLuigi Rizzo return EINVAL; 2332849bec0eSLuigi Rizzo } 2333f18be576SLuigi Rizzo if (na->na_bdg) 2334f18be576SLuigi Rizzo return bdg_netmap_start(ifp, m); 2335f18be576SLuigi Rizzo 23361a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_LOCK, 0); 233702ad4083SLuigi Rizzo if (kring->nr_hwavail >= lim) { 23385b248374SLuigi Rizzo if (netmap_verbose) 233968b8534bSLuigi Rizzo D("stack ring %s full\n", ifp->if_xname); 234068b8534bSLuigi Rizzo goto done; /* no space */ 234168b8534bSLuigi Rizzo } 234268b8534bSLuigi Rizzo 234368b8534bSLuigi Rizzo /* compute the insert position */ 234468b8534bSLuigi Rizzo i = kring->nr_hwcur + kring->nr_hwavail; 234502ad4083SLuigi Rizzo if (i > lim) 234602ad4083SLuigi Rizzo i -= lim + 1; 234768b8534bSLuigi Rizzo slot = &kring->ring->slot[i]; 234868b8534bSLuigi Rizzo m_copydata(m, 0, len, NMB(slot)); 234968b8534bSLuigi Rizzo slot->len = len; 2350091fd0abSLuigi Rizzo slot->flags = kring->nkr_slot_flags; 235168b8534bSLuigi Rizzo kring->nr_hwavail++; 235268b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_HOST) 2353d76bf4ffSLuigi Rizzo D("wake up host ring %s %d", na->ifp->if_xname, na->num_rx_rings); 235468b8534bSLuigi Rizzo selwakeuppri(&kring->si, PI_NET); 235568b8534bSLuigi Rizzo error = 0; 235668b8534bSLuigi Rizzo done: 23571a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0); 235868b8534bSLuigi Rizzo 235968b8534bSLuigi Rizzo /* release the mbuf in either cases of success or failure. As an 236068b8534bSLuigi Rizzo * alternative, put the mbuf in a free list and free the list 236168b8534bSLuigi Rizzo * only when really necessary. 236268b8534bSLuigi Rizzo */ 236368b8534bSLuigi Rizzo m_freem(m); 236468b8534bSLuigi Rizzo 236568b8534bSLuigi Rizzo return (error); 236668b8534bSLuigi Rizzo } 236768b8534bSLuigi Rizzo 236868b8534bSLuigi Rizzo 236968b8534bSLuigi Rizzo /* 237068b8534bSLuigi Rizzo * netmap_reset() is called by the driver routines when reinitializing 237168b8534bSLuigi Rizzo * a ring. The driver is in charge of locking to protect the kring. 237268b8534bSLuigi Rizzo * If netmap mode is not set just return NULL. 237368b8534bSLuigi Rizzo */ 237468b8534bSLuigi Rizzo struct netmap_slot * 237568b8534bSLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, int n, 237668b8534bSLuigi Rizzo u_int new_cur) 237768b8534bSLuigi Rizzo { 237868b8534bSLuigi Rizzo struct netmap_kring *kring; 2379506cc70cSLuigi Rizzo int new_hwofs, lim; 238068b8534bSLuigi Rizzo 238168b8534bSLuigi Rizzo if (na == NULL) 238268b8534bSLuigi Rizzo return NULL; /* no netmap support here */ 238368b8534bSLuigi Rizzo if (!(na->ifp->if_capenable & IFCAP_NETMAP)) 238468b8534bSLuigi Rizzo return NULL; /* nothing to reinitialize */ 238568b8534bSLuigi Rizzo 238664ae02c3SLuigi Rizzo if (tx == NR_TX) { 23878241616dSLuigi Rizzo if (n >= na->num_tx_rings) 23888241616dSLuigi Rizzo return NULL; 238964ae02c3SLuigi Rizzo kring = na->tx_rings + n; 2390506cc70cSLuigi Rizzo new_hwofs = kring->nr_hwcur - new_cur; 239164ae02c3SLuigi Rizzo } else { 23928241616dSLuigi Rizzo if (n >= na->num_rx_rings) 23938241616dSLuigi Rizzo return NULL; 239464ae02c3SLuigi Rizzo kring = na->rx_rings + n; 2395506cc70cSLuigi Rizzo new_hwofs = kring->nr_hwcur + kring->nr_hwavail - new_cur; 239664ae02c3SLuigi Rizzo } 239764ae02c3SLuigi Rizzo lim = kring->nkr_num_slots - 1; 2398506cc70cSLuigi Rizzo if (new_hwofs > lim) 2399506cc70cSLuigi Rizzo new_hwofs -= lim + 1; 2400506cc70cSLuigi Rizzo 2401506cc70cSLuigi Rizzo /* Alwayws set the new offset value and realign the ring. */ 2402506cc70cSLuigi Rizzo kring->nkr_hwofs = new_hwofs; 2403506cc70cSLuigi Rizzo if (tx == NR_TX) 2404506cc70cSLuigi Rizzo kring->nr_hwavail = kring->nkr_num_slots - 1; 24058241616dSLuigi Rizzo ND(10, "new hwofs %d on %s %s[%d]", 2406506cc70cSLuigi Rizzo kring->nkr_hwofs, na->ifp->if_xname, 2407506cc70cSLuigi Rizzo tx == NR_TX ? "TX" : "RX", n); 2408506cc70cSLuigi Rizzo 2409f196ce38SLuigi Rizzo #if 0 // def linux 2410f196ce38SLuigi Rizzo /* XXX check that the mappings are correct */ 2411f196ce38SLuigi Rizzo /* need ring_nr, adapter->pdev, direction */ 2412f196ce38SLuigi Rizzo buffer_info->dma = dma_map_single(&pdev->dev, addr, adapter->rx_buffer_len, DMA_FROM_DEVICE); 2413f196ce38SLuigi Rizzo if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) { 2414f196ce38SLuigi Rizzo D("error mapping rx netmap buffer %d", i); 2415f196ce38SLuigi Rizzo // XXX fix error handling 2416f196ce38SLuigi Rizzo } 2417f196ce38SLuigi Rizzo 2418f196ce38SLuigi Rizzo #endif /* linux */ 241968b8534bSLuigi Rizzo /* 242064ae02c3SLuigi Rizzo * Wakeup on the individual and global lock 2421506cc70cSLuigi Rizzo * We do the wakeup here, but the ring is not yet reconfigured. 2422506cc70cSLuigi Rizzo * However, we are under lock so there are no races. 242368b8534bSLuigi Rizzo */ 242468b8534bSLuigi Rizzo selwakeuppri(&kring->si, PI_NET); 242564ae02c3SLuigi Rizzo selwakeuppri(tx == NR_TX ? &na->tx_si : &na->rx_si, PI_NET); 242668b8534bSLuigi Rizzo return kring->ring->slot; 242768b8534bSLuigi Rizzo } 242868b8534bSLuigi Rizzo 242968b8534bSLuigi Rizzo 2430f18be576SLuigi Rizzo /* returns the next position in the ring */ 2431f18be576SLuigi Rizzo static int 2432f18be576SLuigi Rizzo nm_bdg_preflush(struct netmap_adapter *na, u_int ring_nr, 2433f18be576SLuigi Rizzo struct netmap_kring *kring, u_int end) 2434f18be576SLuigi Rizzo { 2435f18be576SLuigi Rizzo struct netmap_ring *ring = kring->ring; 2436f18be576SLuigi Rizzo struct nm_bdg_fwd *ft = kring->nkr_ft; 2437f18be576SLuigi Rizzo u_int j = kring->nr_hwcur, lim = kring->nkr_num_slots - 1; 2438f18be576SLuigi Rizzo u_int ft_i = 0; /* start from 0 */ 2439f18be576SLuigi Rizzo 2440f18be576SLuigi Rizzo for (; likely(j != end); j = unlikely(j == lim) ? 0 : j+1) { 2441f18be576SLuigi Rizzo struct netmap_slot *slot = &ring->slot[j]; 244285233a7dSLuigi Rizzo char *buf = NMB(slot); 2443f18be576SLuigi Rizzo int len = ft[ft_i].ft_len = slot->len; 2444f18be576SLuigi Rizzo 244585233a7dSLuigi Rizzo ft[ft_i].ft_flags = slot->flags; 244685233a7dSLuigi Rizzo 244785233a7dSLuigi Rizzo ND("flags is 0x%x", slot->flags); 244885233a7dSLuigi Rizzo /* this slot goes into a list so initialize the link field */ 244985233a7dSLuigi Rizzo ft[ft_i].ft_next = NM_BDG_BATCH; /* equivalent to NULL */ 2450f18be576SLuigi Rizzo if (unlikely(len < 14)) 2451f18be576SLuigi Rizzo continue; 245285233a7dSLuigi Rizzo buf = ft[ft_i].ft_buf = (slot->flags & NS_INDIRECT) ? 245385233a7dSLuigi Rizzo *((void **)buf) : buf; 245485233a7dSLuigi Rizzo prefetch(buf); 2455f18be576SLuigi Rizzo if (unlikely(++ft_i == netmap_bridge)) 2456f18be576SLuigi Rizzo ft_i = nm_bdg_flush(ft, ft_i, na, ring_nr); 2457f18be576SLuigi Rizzo } 2458f18be576SLuigi Rizzo if (ft_i) 2459f18be576SLuigi Rizzo ft_i = nm_bdg_flush(ft, ft_i, na, ring_nr); 2460f18be576SLuigi Rizzo return j; 2461f18be576SLuigi Rizzo } 2462f18be576SLuigi Rizzo 2463f18be576SLuigi Rizzo 2464f18be576SLuigi Rizzo /* 2465f18be576SLuigi Rizzo * Pass packets from nic to the bridge. Must be called with 2466f18be576SLuigi Rizzo * proper locks on the source interface. 2467f18be576SLuigi Rizzo * Note, no user process can access this NIC so we can ignore 2468f18be576SLuigi Rizzo * the info in the 'ring'. 2469f18be576SLuigi Rizzo */ 2470f18be576SLuigi Rizzo static void 2471f18be576SLuigi Rizzo netmap_nic_to_bdg(struct ifnet *ifp, u_int ring_nr) 2472f18be576SLuigi Rizzo { 2473f18be576SLuigi Rizzo struct netmap_adapter *na = NA(ifp); 2474f18be576SLuigi Rizzo struct netmap_kring *kring = &na->rx_rings[ring_nr]; 2475f18be576SLuigi Rizzo struct netmap_ring *ring = kring->ring; 2476f18be576SLuigi Rizzo int j, k, lim = kring->nkr_num_slots - 1; 2477f18be576SLuigi Rizzo 2478f18be576SLuigi Rizzo /* fetch packets that have arrived */ 2479f18be576SLuigi Rizzo na->nm_rxsync(ifp, ring_nr, 0); 2480f18be576SLuigi Rizzo /* XXX we don't count reserved, but it should be 0 */ 2481f18be576SLuigi Rizzo j = kring->nr_hwcur; 2482f18be576SLuigi Rizzo k = j + kring->nr_hwavail; 2483f18be576SLuigi Rizzo if (k > lim) 2484f18be576SLuigi Rizzo k -= lim + 1; 2485f18be576SLuigi Rizzo if (k == j && netmap_verbose) { 2486f18be576SLuigi Rizzo D("how strange, interrupt with no packets on %s", 2487f18be576SLuigi Rizzo ifp->if_xname); 2488f18be576SLuigi Rizzo return; 2489f18be576SLuigi Rizzo } 2490f18be576SLuigi Rizzo 2491f18be576SLuigi Rizzo j = nm_bdg_preflush(na, ring_nr, kring, k); 2492f18be576SLuigi Rizzo 2493f18be576SLuigi Rizzo /* we consume everything, but we cannot update kring directly 2494f18be576SLuigi Rizzo * because the nic may have destroyed the info in the NIC ring. 2495f18be576SLuigi Rizzo * So we need to call rxsync again to restore it. 2496f18be576SLuigi Rizzo */ 2497f18be576SLuigi Rizzo ring->cur = j; 2498f18be576SLuigi Rizzo ring->avail = 0; 2499f18be576SLuigi Rizzo na->nm_rxsync(ifp, ring_nr, 0); 2500f18be576SLuigi Rizzo return; 2501f18be576SLuigi Rizzo } 2502f18be576SLuigi Rizzo 2503f18be576SLuigi Rizzo 250468b8534bSLuigi Rizzo /* 25051a26580eSLuigi Rizzo * Default functions to handle rx/tx interrupts 25061a26580eSLuigi Rizzo * we have 4 cases: 25071a26580eSLuigi Rizzo * 1 ring, single lock: 25081a26580eSLuigi Rizzo * lock(core); wake(i=0); unlock(core) 25091a26580eSLuigi Rizzo * N rings, single lock: 25101a26580eSLuigi Rizzo * lock(core); wake(i); wake(N+1) unlock(core) 25111a26580eSLuigi Rizzo * 1 ring, separate locks: (i=0) 25121a26580eSLuigi Rizzo * lock(i); wake(i); unlock(i) 25131a26580eSLuigi Rizzo * N rings, separate locks: 25141a26580eSLuigi Rizzo * lock(i); wake(i); unlock(i); lock(core) wake(N+1) unlock(core) 251564ae02c3SLuigi Rizzo * work_done is non-null on the RX path. 2516849bec0eSLuigi Rizzo * 2517849bec0eSLuigi Rizzo * The 'q' argument also includes flag to tell whether the queue is 2518849bec0eSLuigi Rizzo * already locked on enter, and whether it should remain locked on exit. 2519849bec0eSLuigi Rizzo * This helps adapting to different defaults in drivers and OSes. 25201a26580eSLuigi Rizzo */ 2521babc7c12SLuigi Rizzo int 2522babc7c12SLuigi Rizzo netmap_rx_irq(struct ifnet *ifp, int q, int *work_done) 25231a26580eSLuigi Rizzo { 25241a26580eSLuigi Rizzo struct netmap_adapter *na; 25251a26580eSLuigi Rizzo struct netmap_kring *r; 252664ae02c3SLuigi Rizzo NM_SELINFO_T *main_wq; 2527f18be576SLuigi Rizzo int locktype, unlocktype, nic_to_bridge, lock; 25281a26580eSLuigi Rizzo 25291a26580eSLuigi Rizzo if (!(ifp->if_capenable & IFCAP_NETMAP)) 25301a26580eSLuigi Rizzo return 0; 2531849bec0eSLuigi Rizzo 2532849bec0eSLuigi Rizzo lock = q & (NETMAP_LOCKED_ENTER | NETMAP_LOCKED_EXIT); 2533849bec0eSLuigi Rizzo q = q & NETMAP_RING_MASK; 2534849bec0eSLuigi Rizzo 25358241616dSLuigi Rizzo ND(5, "received %s queue %d", work_done ? "RX" : "TX" , q); 25361a26580eSLuigi Rizzo na = NA(ifp); 25378241616dSLuigi Rizzo if (na->na_flags & NAF_SKIP_INTR) { 25388241616dSLuigi Rizzo ND("use regular interrupt"); 25398241616dSLuigi Rizzo return 0; 25408241616dSLuigi Rizzo } 25418241616dSLuigi Rizzo 254264ae02c3SLuigi Rizzo if (work_done) { /* RX path */ 25438241616dSLuigi Rizzo if (q >= na->num_rx_rings) 2544849bec0eSLuigi Rizzo return 0; // not a physical queue 254564ae02c3SLuigi Rizzo r = na->rx_rings + q; 254664ae02c3SLuigi Rizzo r->nr_kflags |= NKR_PENDINTR; 2547d76bf4ffSLuigi Rizzo main_wq = (na->num_rx_rings > 1) ? &na->rx_si : NULL; 2548f18be576SLuigi Rizzo /* set a flag if the NIC is attached to a VALE switch */ 2549f18be576SLuigi Rizzo nic_to_bridge = (na->na_bdg != NULL); 2550849bec0eSLuigi Rizzo locktype = NETMAP_RX_LOCK; 2551849bec0eSLuigi Rizzo unlocktype = NETMAP_RX_UNLOCK; 2552849bec0eSLuigi Rizzo } else { /* TX path */ 25538241616dSLuigi Rizzo if (q >= na->num_tx_rings) 2554849bec0eSLuigi Rizzo return 0; // not a physical queue 255564ae02c3SLuigi Rizzo r = na->tx_rings + q; 2556d76bf4ffSLuigi Rizzo main_wq = (na->num_tx_rings > 1) ? &na->tx_si : NULL; 255764ae02c3SLuigi Rizzo work_done = &q; /* dummy */ 2558f18be576SLuigi Rizzo nic_to_bridge = 0; 2559849bec0eSLuigi Rizzo locktype = NETMAP_TX_LOCK; 2560849bec0eSLuigi Rizzo unlocktype = NETMAP_TX_UNLOCK; 256164ae02c3SLuigi Rizzo } 25621a26580eSLuigi Rizzo if (na->separate_locks) { 2563849bec0eSLuigi Rizzo if (!(lock & NETMAP_LOCKED_ENTER)) 2564849bec0eSLuigi Rizzo na->nm_lock(ifp, locktype, q); 2565f18be576SLuigi Rizzo /* If a NIC is attached to a bridge, flush packets 2566f18be576SLuigi Rizzo * (and no need to wakeup anyone). Otherwise, wakeup 2567f18be576SLuigi Rizzo * possible processes waiting for packets. 2568f18be576SLuigi Rizzo */ 2569f18be576SLuigi Rizzo if (nic_to_bridge) 2570f18be576SLuigi Rizzo netmap_nic_to_bdg(ifp, q); 2571f18be576SLuigi Rizzo else 257264ae02c3SLuigi Rizzo selwakeuppri(&r->si, PI_NET); 2573849bec0eSLuigi Rizzo na->nm_lock(ifp, unlocktype, q); 2574f18be576SLuigi Rizzo if (main_wq && !nic_to_bridge) { 2575849bec0eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_LOCK, 0); 257664ae02c3SLuigi Rizzo selwakeuppri(main_wq, PI_NET); 2577849bec0eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0); 25781a26580eSLuigi Rizzo } 2579849bec0eSLuigi Rizzo /* lock the queue again if requested */ 2580849bec0eSLuigi Rizzo if (lock & NETMAP_LOCKED_EXIT) 2581849bec0eSLuigi Rizzo na->nm_lock(ifp, locktype, q); 25821a26580eSLuigi Rizzo } else { 2583849bec0eSLuigi Rizzo if (!(lock & NETMAP_LOCKED_ENTER)) 2584849bec0eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_LOCK, 0); 2585f18be576SLuigi Rizzo if (nic_to_bridge) 2586f18be576SLuigi Rizzo netmap_nic_to_bdg(ifp, q); 2587f18be576SLuigi Rizzo else { 258864ae02c3SLuigi Rizzo selwakeuppri(&r->si, PI_NET); 258964ae02c3SLuigi Rizzo if (main_wq) 259064ae02c3SLuigi Rizzo selwakeuppri(main_wq, PI_NET); 2591f18be576SLuigi Rizzo } 2592849bec0eSLuigi Rizzo if (!(lock & NETMAP_LOCKED_EXIT)) 2593849bec0eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0); 25941a26580eSLuigi Rizzo } 25951a26580eSLuigi Rizzo *work_done = 1; /* do not fire napi again */ 25961a26580eSLuigi Rizzo return 1; 25971a26580eSLuigi Rizzo } 25981a26580eSLuigi Rizzo 259964ae02c3SLuigi Rizzo 260001c7d25fSLuigi Rizzo #ifdef linux /* linux-specific routines */ 260101c7d25fSLuigi Rizzo 2602f18be576SLuigi Rizzo 260301c7d25fSLuigi Rizzo /* 260401c7d25fSLuigi Rizzo * Remap linux arguments into the FreeBSD call. 260501c7d25fSLuigi Rizzo * - pwait is the poll table, passed as 'dev'; 260601c7d25fSLuigi Rizzo * If pwait == NULL someone else already woke up before. We can report 260701c7d25fSLuigi Rizzo * events but they are filtered upstream. 260801c7d25fSLuigi Rizzo * If pwait != NULL, then pwait->key contains the list of events. 260901c7d25fSLuigi Rizzo * - events is computed from pwait as above. 261001c7d25fSLuigi Rizzo * - file is passed as 'td'; 261101c7d25fSLuigi Rizzo */ 261201c7d25fSLuigi Rizzo static u_int 261301c7d25fSLuigi Rizzo linux_netmap_poll(struct file * file, struct poll_table_struct *pwait) 261401c7d25fSLuigi Rizzo { 2615849bec0eSLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28) 2616849bec0eSLuigi Rizzo int events = POLLIN | POLLOUT; /* XXX maybe... */ 2617849bec0eSLuigi Rizzo #elif LINUX_VERSION_CODE < KERNEL_VERSION(3,4,0) 261801c7d25fSLuigi Rizzo int events = pwait ? pwait->key : POLLIN | POLLOUT; 261901c7d25fSLuigi Rizzo #else /* in 3.4.0 field 'key' was renamed to '_key' */ 262001c7d25fSLuigi Rizzo int events = pwait ? pwait->_key : POLLIN | POLLOUT; 262101c7d25fSLuigi Rizzo #endif 262201c7d25fSLuigi Rizzo return netmap_poll((void *)pwait, events, (void *)file); 262301c7d25fSLuigi Rizzo } 262401c7d25fSLuigi Rizzo 2625f18be576SLuigi Rizzo 262601c7d25fSLuigi Rizzo static int 262742a3a5bdSLuigi Rizzo linux_netmap_mmap(struct file *f, struct vm_area_struct *vma) 262801c7d25fSLuigi Rizzo { 262901c7d25fSLuigi Rizzo int lut_skip, i, j; 263001c7d25fSLuigi Rizzo int user_skip = 0; 263101c7d25fSLuigi Rizzo struct lut_entry *l_entry; 26328241616dSLuigi Rizzo int error = 0; 26338241616dSLuigi Rizzo unsigned long off, tomap; 263401c7d25fSLuigi Rizzo /* 263501c7d25fSLuigi Rizzo * vma->vm_start: start of mapping user address space 263601c7d25fSLuigi Rizzo * vma->vm_end: end of the mapping user address space 26378241616dSLuigi Rizzo * vma->vm_pfoff: offset of first page in the device 263801c7d25fSLuigi Rizzo */ 263901c7d25fSLuigi Rizzo 264001c7d25fSLuigi Rizzo // XXX security checks 264101c7d25fSLuigi Rizzo 26428241616dSLuigi Rizzo error = netmap_get_memory(f->private_data); 26438241616dSLuigi Rizzo ND("get_memory returned %d", error); 26448241616dSLuigi Rizzo if (error) 26458241616dSLuigi Rizzo return -error; 26468241616dSLuigi Rizzo 26478241616dSLuigi Rizzo off = vma->vm_pgoff << PAGE_SHIFT; /* offset in bytes */ 26488241616dSLuigi Rizzo tomap = vma->vm_end - vma->vm_start; 26498241616dSLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { /* loop through obj_pools */ 26508241616dSLuigi Rizzo const struct netmap_obj_pool *p = &nm_mem.pools[i]; 265101c7d25fSLuigi Rizzo /* 265201c7d25fSLuigi Rizzo * In each pool memory is allocated in clusters 265301c7d25fSLuigi Rizzo * of size _clustsize, each containing clustentries 265401c7d25fSLuigi Rizzo * entries. For each object k we already store the 26558241616dSLuigi Rizzo * vtophys mapping in lut[k] so we use that, scanning 265601c7d25fSLuigi Rizzo * the lut[] array in steps of clustentries, 265701c7d25fSLuigi Rizzo * and we map each cluster (not individual pages, 2658849bec0eSLuigi Rizzo * it would be overkill -- XXX slow ? 20130415). 265901c7d25fSLuigi Rizzo */ 26608241616dSLuigi Rizzo 26618241616dSLuigi Rizzo /* 26628241616dSLuigi Rizzo * We interpret vm_pgoff as an offset into the whole 26638241616dSLuigi Rizzo * netmap memory, as if all clusters where contiguous. 26648241616dSLuigi Rizzo */ 26658241616dSLuigi Rizzo for (lut_skip = 0, j = 0; j < p->_numclusters; j++, lut_skip += p->clustentries) { 26668241616dSLuigi Rizzo unsigned long paddr, mapsize; 26678241616dSLuigi Rizzo if (p->_clustsize <= off) { 26688241616dSLuigi Rizzo off -= p->_clustsize; 26698241616dSLuigi Rizzo continue; 26708241616dSLuigi Rizzo } 26718241616dSLuigi Rizzo l_entry = &p->lut[lut_skip]; /* first obj in the cluster */ 26728241616dSLuigi Rizzo paddr = l_entry->paddr + off; 26738241616dSLuigi Rizzo mapsize = p->_clustsize - off; 26748241616dSLuigi Rizzo off = 0; 26758241616dSLuigi Rizzo if (mapsize > tomap) 26768241616dSLuigi Rizzo mapsize = tomap; 26778241616dSLuigi Rizzo ND("remap_pfn_range(%lx, %lx, %lx)", 26788241616dSLuigi Rizzo vma->vm_start + user_skip, 26798241616dSLuigi Rizzo paddr >> PAGE_SHIFT, mapsize); 268001c7d25fSLuigi Rizzo if (remap_pfn_range(vma, vma->vm_start + user_skip, 26818241616dSLuigi Rizzo paddr >> PAGE_SHIFT, mapsize, 268201c7d25fSLuigi Rizzo vma->vm_page_prot)) 268301c7d25fSLuigi Rizzo return -EAGAIN; // XXX check return value 26848241616dSLuigi Rizzo user_skip += mapsize; 26858241616dSLuigi Rizzo tomap -= mapsize; 26868241616dSLuigi Rizzo if (tomap == 0) 26878241616dSLuigi Rizzo goto done; 268801c7d25fSLuigi Rizzo } 268901c7d25fSLuigi Rizzo } 26908241616dSLuigi Rizzo done: 269101c7d25fSLuigi Rizzo 269201c7d25fSLuigi Rizzo return 0; 269301c7d25fSLuigi Rizzo } 269401c7d25fSLuigi Rizzo 2695f18be576SLuigi Rizzo 269601c7d25fSLuigi Rizzo static netdev_tx_t 269742a3a5bdSLuigi Rizzo linux_netmap_start(struct sk_buff *skb, struct net_device *dev) 269801c7d25fSLuigi Rizzo { 269901c7d25fSLuigi Rizzo netmap_start(dev, skb); 270001c7d25fSLuigi Rizzo return (NETDEV_TX_OK); 270101c7d25fSLuigi Rizzo } 270201c7d25fSLuigi Rizzo 270301c7d25fSLuigi Rizzo 27040b8ed8e0SLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37) // XXX was 38 270501c7d25fSLuigi Rizzo #define LIN_IOCTL_NAME .ioctl 270601c7d25fSLuigi Rizzo int 270701c7d25fSLuigi Rizzo linux_netmap_ioctl(struct inode *inode, struct file *file, u_int cmd, u_long data /* arg */) 270801c7d25fSLuigi Rizzo #else 270901c7d25fSLuigi Rizzo #define LIN_IOCTL_NAME .unlocked_ioctl 271001c7d25fSLuigi Rizzo long 271101c7d25fSLuigi Rizzo linux_netmap_ioctl(struct file *file, u_int cmd, u_long data /* arg */) 271201c7d25fSLuigi Rizzo #endif 271301c7d25fSLuigi Rizzo { 271401c7d25fSLuigi Rizzo int ret; 271501c7d25fSLuigi Rizzo struct nmreq nmr; 271601c7d25fSLuigi Rizzo bzero(&nmr, sizeof(nmr)); 271701c7d25fSLuigi Rizzo 271801c7d25fSLuigi Rizzo if (data && copy_from_user(&nmr, (void *)data, sizeof(nmr) ) != 0) 271901c7d25fSLuigi Rizzo return -EFAULT; 272001c7d25fSLuigi Rizzo ret = netmap_ioctl(NULL, cmd, (caddr_t)&nmr, 0, (void *)file); 272101c7d25fSLuigi Rizzo if (data && copy_to_user((void*)data, &nmr, sizeof(nmr) ) != 0) 272201c7d25fSLuigi Rizzo return -EFAULT; 272301c7d25fSLuigi Rizzo return -ret; 272401c7d25fSLuigi Rizzo } 272501c7d25fSLuigi Rizzo 272601c7d25fSLuigi Rizzo 272701c7d25fSLuigi Rizzo static int 27280b8ed8e0SLuigi Rizzo netmap_release(struct inode *inode, struct file *file) 272901c7d25fSLuigi Rizzo { 27300b8ed8e0SLuigi Rizzo (void)inode; /* UNUSED */ 273101c7d25fSLuigi Rizzo if (file->private_data) 273201c7d25fSLuigi Rizzo netmap_dtor(file->private_data); 273301c7d25fSLuigi Rizzo return (0); 273401c7d25fSLuigi Rizzo } 273501c7d25fSLuigi Rizzo 2736f18be576SLuigi Rizzo 27378241616dSLuigi Rizzo static int 27388241616dSLuigi Rizzo linux_netmap_open(struct inode *inode, struct file *file) 27398241616dSLuigi Rizzo { 27408241616dSLuigi Rizzo struct netmap_priv_d *priv; 27418241616dSLuigi Rizzo (void)inode; /* UNUSED */ 27428241616dSLuigi Rizzo 27438241616dSLuigi Rizzo priv = malloc(sizeof(struct netmap_priv_d), M_DEVBUF, 27448241616dSLuigi Rizzo M_NOWAIT | M_ZERO); 27458241616dSLuigi Rizzo if (priv == NULL) 27468241616dSLuigi Rizzo return -ENOMEM; 27478241616dSLuigi Rizzo 27488241616dSLuigi Rizzo file->private_data = priv; 27498241616dSLuigi Rizzo 27508241616dSLuigi Rizzo return (0); 27518241616dSLuigi Rizzo } 275201c7d25fSLuigi Rizzo 2753f18be576SLuigi Rizzo 275401c7d25fSLuigi Rizzo static struct file_operations netmap_fops = { 2755f18be576SLuigi Rizzo .owner = THIS_MODULE, 27568241616dSLuigi Rizzo .open = linux_netmap_open, 275742a3a5bdSLuigi Rizzo .mmap = linux_netmap_mmap, 275801c7d25fSLuigi Rizzo LIN_IOCTL_NAME = linux_netmap_ioctl, 275901c7d25fSLuigi Rizzo .poll = linux_netmap_poll, 276001c7d25fSLuigi Rizzo .release = netmap_release, 276101c7d25fSLuigi Rizzo }; 276201c7d25fSLuigi Rizzo 2763f18be576SLuigi Rizzo 276401c7d25fSLuigi Rizzo static struct miscdevice netmap_cdevsw = { /* same name as FreeBSD */ 276501c7d25fSLuigi Rizzo MISC_DYNAMIC_MINOR, 276601c7d25fSLuigi Rizzo "netmap", 276701c7d25fSLuigi Rizzo &netmap_fops, 276801c7d25fSLuigi Rizzo }; 276901c7d25fSLuigi Rizzo 277001c7d25fSLuigi Rizzo static int netmap_init(void); 277101c7d25fSLuigi Rizzo static void netmap_fini(void); 277201c7d25fSLuigi Rizzo 2773f18be576SLuigi Rizzo 277442a3a5bdSLuigi Rizzo /* Errors have negative values on linux */ 277542a3a5bdSLuigi Rizzo static int linux_netmap_init(void) 277642a3a5bdSLuigi Rizzo { 277742a3a5bdSLuigi Rizzo return -netmap_init(); 277842a3a5bdSLuigi Rizzo } 277942a3a5bdSLuigi Rizzo 278042a3a5bdSLuigi Rizzo module_init(linux_netmap_init); 278101c7d25fSLuigi Rizzo module_exit(netmap_fini); 278201c7d25fSLuigi Rizzo /* export certain symbols to other modules */ 278301c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_attach); // driver attach routines 278401c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_detach); // driver detach routines 278501c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_ring_reinit); // ring init on error 278601c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_buffer_lut); 278701c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_total_buffers); // index check 278801c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_buffer_base); 278901c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_reset); // ring init routines 279001c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_buf_size); 279101c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_rx_irq); // default irq handler 279201c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_no_pendintr); // XXX mitigation - should go away 2793f18be576SLuigi Rizzo EXPORT_SYMBOL(netmap_bdg_ctl); // bridge configuration routine 2794f18be576SLuigi Rizzo EXPORT_SYMBOL(netmap_bdg_learning); // the default lookup function 279501c7d25fSLuigi Rizzo 279601c7d25fSLuigi Rizzo 279701c7d25fSLuigi Rizzo MODULE_AUTHOR("http://info.iet.unipi.it/~luigi/netmap/"); 279801c7d25fSLuigi Rizzo MODULE_DESCRIPTION("The netmap packet I/O framework"); 279901c7d25fSLuigi Rizzo MODULE_LICENSE("Dual BSD/GPL"); /* the code here is all BSD. */ 280001c7d25fSLuigi Rizzo 280101c7d25fSLuigi Rizzo #else /* __FreeBSD__ */ 280201c7d25fSLuigi Rizzo 2803f18be576SLuigi Rizzo 2804babc7c12SLuigi Rizzo static struct cdevsw netmap_cdevsw = { 2805babc7c12SLuigi Rizzo .d_version = D_VERSION, 2806babc7c12SLuigi Rizzo .d_name = "netmap", 28078241616dSLuigi Rizzo .d_open = netmap_open, 2808babc7c12SLuigi Rizzo .d_mmap = netmap_mmap, 28098241616dSLuigi Rizzo .d_mmap_single = netmap_mmap_single, 2810babc7c12SLuigi Rizzo .d_ioctl = netmap_ioctl, 2811babc7c12SLuigi Rizzo .d_poll = netmap_poll, 28128241616dSLuigi Rizzo .d_close = netmap_close, 2813babc7c12SLuigi Rizzo }; 281401c7d25fSLuigi Rizzo #endif /* __FreeBSD__ */ 2815babc7c12SLuigi Rizzo 2816f196ce38SLuigi Rizzo #ifdef NM_BRIDGE 2817f196ce38SLuigi Rizzo /* 2818f196ce38SLuigi Rizzo *---- support for virtual bridge ----- 2819f196ce38SLuigi Rizzo */ 2820f196ce38SLuigi Rizzo 2821f196ce38SLuigi Rizzo /* ----- FreeBSD if_bridge hash function ------- */ 2822f196ce38SLuigi Rizzo 2823f196ce38SLuigi Rizzo /* 2824f196ce38SLuigi Rizzo * The following hash function is adapted from "Hash Functions" by Bob Jenkins 2825f196ce38SLuigi Rizzo * ("Algorithm Alley", Dr. Dobbs Journal, September 1997). 2826f196ce38SLuigi Rizzo * 2827f196ce38SLuigi Rizzo * http://www.burtleburtle.net/bob/hash/spooky.html 2828f196ce38SLuigi Rizzo */ 2829f196ce38SLuigi Rizzo #define mix(a, b, c) \ 2830f196ce38SLuigi Rizzo do { \ 2831f196ce38SLuigi Rizzo a -= b; a -= c; a ^= (c >> 13); \ 2832f196ce38SLuigi Rizzo b -= c; b -= a; b ^= (a << 8); \ 2833f196ce38SLuigi Rizzo c -= a; c -= b; c ^= (b >> 13); \ 2834f196ce38SLuigi Rizzo a -= b; a -= c; a ^= (c >> 12); \ 2835f196ce38SLuigi Rizzo b -= c; b -= a; b ^= (a << 16); \ 2836f196ce38SLuigi Rizzo c -= a; c -= b; c ^= (b >> 5); \ 2837f196ce38SLuigi Rizzo a -= b; a -= c; a ^= (c >> 3); \ 2838f196ce38SLuigi Rizzo b -= c; b -= a; b ^= (a << 10); \ 2839f196ce38SLuigi Rizzo c -= a; c -= b; c ^= (b >> 15); \ 2840f196ce38SLuigi Rizzo } while (/*CONSTCOND*/0) 2841f196ce38SLuigi Rizzo 2842f196ce38SLuigi Rizzo static __inline uint32_t 2843f196ce38SLuigi Rizzo nm_bridge_rthash(const uint8_t *addr) 2844f196ce38SLuigi Rizzo { 2845f196ce38SLuigi Rizzo uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = 0; // hask key 2846f196ce38SLuigi Rizzo 2847f196ce38SLuigi Rizzo b += addr[5] << 8; 2848f196ce38SLuigi Rizzo b += addr[4]; 2849f196ce38SLuigi Rizzo a += addr[3] << 24; 2850f196ce38SLuigi Rizzo a += addr[2] << 16; 2851f196ce38SLuigi Rizzo a += addr[1] << 8; 2852f196ce38SLuigi Rizzo a += addr[0]; 2853f196ce38SLuigi Rizzo 2854f196ce38SLuigi Rizzo mix(a, b, c); 2855f196ce38SLuigi Rizzo #define BRIDGE_RTHASH_MASK (NM_BDG_HASH-1) 2856f196ce38SLuigi Rizzo return (c & BRIDGE_RTHASH_MASK); 2857f196ce38SLuigi Rizzo } 2858f196ce38SLuigi Rizzo 2859f196ce38SLuigi Rizzo #undef mix 2860f196ce38SLuigi Rizzo 2861f196ce38SLuigi Rizzo 2862f196ce38SLuigi Rizzo static int 2863f196ce38SLuigi Rizzo bdg_netmap_reg(struct ifnet *ifp, int onoff) 2864f196ce38SLuigi Rizzo { 2865f18be576SLuigi Rizzo // struct nm_bridge *b = NA(ifp)->na_bdg; 2866f196ce38SLuigi Rizzo 2867f18be576SLuigi Rizzo /* the interface is already attached to the bridge, 2868f18be576SLuigi Rizzo * so we only need to toggle IFCAP_NETMAP. 2869f18be576SLuigi Rizzo * Locking is not necessary (we are already under 2870f18be576SLuigi Rizzo * NMA_LOCK, and the port is not in use during this call). 2871f196ce38SLuigi Rizzo */ 2872f18be576SLuigi Rizzo /* BDG_WLOCK(b); */ 2873f18be576SLuigi Rizzo if (onoff) { 2874f196ce38SLuigi Rizzo ifp->if_capenable |= IFCAP_NETMAP; 2875f196ce38SLuigi Rizzo } else { 2876f196ce38SLuigi Rizzo ifp->if_capenable &= ~IFCAP_NETMAP; 2877f196ce38SLuigi Rizzo } 2878f18be576SLuigi Rizzo /* BDG_WUNLOCK(b); */ 2879f18be576SLuigi Rizzo return 0; 2880f196ce38SLuigi Rizzo } 2881f196ce38SLuigi Rizzo 2882f196ce38SLuigi Rizzo 2883f18be576SLuigi Rizzo /* 2884f18be576SLuigi Rizzo * Lookup function for a learning bridge. 2885f18be576SLuigi Rizzo * Update the hash table with the source address, 2886f18be576SLuigi Rizzo * and then returns the destination port index, and the 2887f18be576SLuigi Rizzo * ring in *dst_ring (at the moment, always use ring 0) 2888f18be576SLuigi Rizzo */ 2889f18be576SLuigi Rizzo u_int 2890f18be576SLuigi Rizzo netmap_bdg_learning(char *buf, u_int len, uint8_t *dst_ring, 2891f18be576SLuigi Rizzo struct netmap_adapter *na) 2892f196ce38SLuigi Rizzo { 2893f18be576SLuigi Rizzo struct nm_hash_ent *ht = na->na_bdg->ht; 2894f196ce38SLuigi Rizzo uint32_t sh, dh; 2895f18be576SLuigi Rizzo u_int dst, mysrc = na->bdg_port; 2896f196ce38SLuigi Rizzo uint64_t smac, dmac; 2897f196ce38SLuigi Rizzo 2898f196ce38SLuigi Rizzo dmac = le64toh(*(uint64_t *)(buf)) & 0xffffffffffff; 2899f196ce38SLuigi Rizzo smac = le64toh(*(uint64_t *)(buf + 4)); 2900f196ce38SLuigi Rizzo smac >>= 16; 2901f18be576SLuigi Rizzo 2902f196ce38SLuigi Rizzo /* 2903f196ce38SLuigi Rizzo * The hash is somewhat expensive, there might be some 2904f196ce38SLuigi Rizzo * worthwhile optimizations here. 2905f196ce38SLuigi Rizzo */ 2906f196ce38SLuigi Rizzo if ((buf[6] & 1) == 0) { /* valid src */ 2907f196ce38SLuigi Rizzo uint8_t *s = buf+6; 2908f196ce38SLuigi Rizzo sh = nm_bridge_rthash(buf+6); // XXX hash of source 2909f196ce38SLuigi Rizzo /* update source port forwarding entry */ 2910f18be576SLuigi Rizzo ht[sh].mac = smac; /* XXX expire ? */ 2911f18be576SLuigi Rizzo ht[sh].ports = mysrc; 2912f196ce38SLuigi Rizzo if (netmap_verbose) 2913f196ce38SLuigi Rizzo D("src %02x:%02x:%02x:%02x:%02x:%02x on port %d", 2914f18be576SLuigi Rizzo s[0], s[1], s[2], s[3], s[4], s[5], mysrc); 2915f196ce38SLuigi Rizzo } 2916f18be576SLuigi Rizzo dst = NM_BDG_BROADCAST; 2917f196ce38SLuigi Rizzo if ((buf[0] & 1) == 0) { /* unicast */ 2918f196ce38SLuigi Rizzo dh = nm_bridge_rthash(buf); // XXX hash of dst 2919f18be576SLuigi Rizzo if (ht[dh].mac == dmac) { /* found dst */ 2920f18be576SLuigi Rizzo dst = ht[dh].ports; 2921f196ce38SLuigi Rizzo } 2922f18be576SLuigi Rizzo /* XXX otherwise return NM_BDG_UNKNOWN ? */ 2923f196ce38SLuigi Rizzo } 2924f18be576SLuigi Rizzo *dst_ring = 0; 2925f18be576SLuigi Rizzo return dst; 2926f196ce38SLuigi Rizzo } 2927f196ce38SLuigi Rizzo 2928f18be576SLuigi Rizzo 2929f18be576SLuigi Rizzo /* 2930f18be576SLuigi Rizzo * This flush routine supports only unicast and broadcast but a large 2931f18be576SLuigi Rizzo * number of ports, and lets us replace the learn and dispatch functions. 2932f18be576SLuigi Rizzo */ 2933f18be576SLuigi Rizzo int 2934f18be576SLuigi Rizzo nm_bdg_flush(struct nm_bdg_fwd *ft, int n, struct netmap_adapter *na, 2935f18be576SLuigi Rizzo u_int ring_nr) 2936f18be576SLuigi Rizzo { 2937f18be576SLuigi Rizzo struct nm_bdg_q *dst_ents, *brddst; 2938f18be576SLuigi Rizzo uint16_t num_dsts = 0, *dsts; 2939f18be576SLuigi Rizzo struct nm_bridge *b = na->na_bdg; 2940f18be576SLuigi Rizzo u_int i, me = na->bdg_port; 2941f18be576SLuigi Rizzo 2942f18be576SLuigi Rizzo dst_ents = (struct nm_bdg_q *)(ft + NM_BDG_BATCH); 2943f18be576SLuigi Rizzo dsts = (uint16_t *)(dst_ents + NM_BDG_MAXPORTS * NM_BDG_MAXRINGS + 1); 2944f18be576SLuigi Rizzo 2945f18be576SLuigi Rizzo BDG_RLOCK(b); 2946f18be576SLuigi Rizzo 2947f18be576SLuigi Rizzo /* first pass: find a destination */ 2948f18be576SLuigi Rizzo for (i = 0; likely(i < n); i++) { 294985233a7dSLuigi Rizzo uint8_t *buf = ft[i].ft_buf; 2950f18be576SLuigi Rizzo uint8_t dst_ring = ring_nr; 2951f18be576SLuigi Rizzo uint16_t dst_port, d_i; 2952f18be576SLuigi Rizzo struct nm_bdg_q *d; 2953f18be576SLuigi Rizzo 2954f18be576SLuigi Rizzo dst_port = b->nm_bdg_lookup(buf, ft[i].ft_len, &dst_ring, na); 2955f18be576SLuigi Rizzo if (dst_port == NM_BDG_NOPORT) { 2956f18be576SLuigi Rizzo continue; /* this packet is identified to be dropped */ 2957f18be576SLuigi Rizzo } else if (unlikely(dst_port > NM_BDG_MAXPORTS)) { 2958f18be576SLuigi Rizzo continue; 2959f18be576SLuigi Rizzo } else if (dst_port == NM_BDG_BROADCAST) { 2960f18be576SLuigi Rizzo dst_ring = 0; /* broadcasts always go to ring 0 */ 2961f18be576SLuigi Rizzo } else if (unlikely(dst_port == me || 2962f18be576SLuigi Rizzo !BDG_GET_VAR(b->bdg_ports[dst_port]))) { 2963f18be576SLuigi Rizzo continue; 2964f18be576SLuigi Rizzo } 2965f18be576SLuigi Rizzo 2966f18be576SLuigi Rizzo /* get a position in the scratch pad */ 2967f18be576SLuigi Rizzo d_i = dst_port * NM_BDG_MAXRINGS + dst_ring; 2968f18be576SLuigi Rizzo d = dst_ents + d_i; 2969f18be576SLuigi Rizzo if (d->bq_head == NM_BDG_BATCH) { /* new destination */ 2970f18be576SLuigi Rizzo d->bq_head = d->bq_tail = i; 2971f18be576SLuigi Rizzo /* remember this position to be scanned later */ 2972f18be576SLuigi Rizzo if (dst_port != NM_BDG_BROADCAST) 2973f18be576SLuigi Rizzo dsts[num_dsts++] = d_i; 297485233a7dSLuigi Rizzo } else { 2975f18be576SLuigi Rizzo ft[d->bq_tail].ft_next = i; 2976f18be576SLuigi Rizzo d->bq_tail = i; 2977f18be576SLuigi Rizzo } 297885233a7dSLuigi Rizzo } 2979f18be576SLuigi Rizzo 2980f18be576SLuigi Rizzo /* if there is a broadcast, set ring 0 of all ports to be scanned 2981f18be576SLuigi Rizzo * XXX This would be optimized by recording the highest index of active 2982f18be576SLuigi Rizzo * ports. 2983f18be576SLuigi Rizzo */ 2984f18be576SLuigi Rizzo brddst = dst_ents + NM_BDG_BROADCAST * NM_BDG_MAXRINGS; 2985f18be576SLuigi Rizzo if (brddst->bq_head != NM_BDG_BATCH) { 2986f18be576SLuigi Rizzo for (i = 0; likely(i < NM_BDG_MAXPORTS); i++) { 2987f18be576SLuigi Rizzo uint16_t d_i = i * NM_BDG_MAXRINGS; 2988f18be576SLuigi Rizzo if (unlikely(i == me) || !BDG_GET_VAR(b->bdg_ports[i])) 2989f18be576SLuigi Rizzo continue; 2990f18be576SLuigi Rizzo else if (dst_ents[d_i].bq_head == NM_BDG_BATCH) 2991f18be576SLuigi Rizzo dsts[num_dsts++] = d_i; 2992f18be576SLuigi Rizzo } 2993f18be576SLuigi Rizzo } 2994f18be576SLuigi Rizzo 2995f18be576SLuigi Rizzo /* second pass: scan destinations (XXX will be modular somehow) */ 2996f18be576SLuigi Rizzo for (i = 0; i < num_dsts; i++) { 2997f18be576SLuigi Rizzo struct ifnet *dst_ifp; 2998f18be576SLuigi Rizzo struct netmap_adapter *dst_na; 2999f196ce38SLuigi Rizzo struct netmap_kring *kring; 3000f196ce38SLuigi Rizzo struct netmap_ring *ring; 3001f18be576SLuigi Rizzo u_int dst_nr, is_vp, lim, j, sent = 0, d_i, next, brd_next; 3002f18be576SLuigi Rizzo int howmany, retry = netmap_txsync_retry; 3003f18be576SLuigi Rizzo struct nm_bdg_q *d; 3004f196ce38SLuigi Rizzo 3005f18be576SLuigi Rizzo d_i = dsts[i]; 3006f18be576SLuigi Rizzo d = dst_ents + d_i; 3007f18be576SLuigi Rizzo dst_na = BDG_GET_VAR(b->bdg_ports[d_i/NM_BDG_MAXRINGS]); 3008f18be576SLuigi Rizzo /* protect from the lookup function returning an inactive 3009f18be576SLuigi Rizzo * destination port 3010f18be576SLuigi Rizzo */ 3011f18be576SLuigi Rizzo if (unlikely(dst_na == NULL)) 3012f196ce38SLuigi Rizzo continue; 3013f18be576SLuigi Rizzo else if (dst_na->na_flags & NAF_SW_ONLY) 3014f196ce38SLuigi Rizzo continue; 3015f18be576SLuigi Rizzo dst_ifp = dst_na->ifp; 3016f18be576SLuigi Rizzo /* 3017f18be576SLuigi Rizzo * The interface may be in !netmap mode in two cases: 3018f18be576SLuigi Rizzo * - when na is attached but not activated yet; 3019f18be576SLuigi Rizzo * - when na is being deactivated but is still attached. 3020f18be576SLuigi Rizzo */ 3021f18be576SLuigi Rizzo if (unlikely(!(dst_ifp->if_capenable & IFCAP_NETMAP))) 3022f18be576SLuigi Rizzo continue; 3023f196ce38SLuigi Rizzo 3024f18be576SLuigi Rizzo /* there is at least one either unicast or broadcast packet */ 3025f18be576SLuigi Rizzo brd_next = brddst->bq_head; 3026f18be576SLuigi Rizzo next = d->bq_head; 3027f18be576SLuigi Rizzo 3028f18be576SLuigi Rizzo is_vp = nma_is_vp(dst_na); 3029f18be576SLuigi Rizzo dst_nr = d_i & (NM_BDG_MAXRINGS-1); 3030f18be576SLuigi Rizzo if (is_vp) { /* virtual port */ 3031f18be576SLuigi Rizzo if (dst_nr >= dst_na->num_rx_rings) 3032f18be576SLuigi Rizzo dst_nr = dst_nr % dst_na->num_rx_rings; 3033f18be576SLuigi Rizzo kring = &dst_na->rx_rings[dst_nr]; 3034f196ce38SLuigi Rizzo ring = kring->ring; 3035f196ce38SLuigi Rizzo lim = kring->nkr_num_slots - 1; 3036f18be576SLuigi Rizzo dst_na->nm_lock(dst_ifp, NETMAP_RX_LOCK, dst_nr); 3037f196ce38SLuigi Rizzo j = kring->nr_hwcur + kring->nr_hwavail; 3038f196ce38SLuigi Rizzo if (j > lim) 3039f196ce38SLuigi Rizzo j -= kring->nkr_num_slots; 3040f18be576SLuigi Rizzo howmany = lim - kring->nr_hwavail; 3041f18be576SLuigi Rizzo } else { /* hw or sw adapter */ 3042f18be576SLuigi Rizzo if (dst_nr >= dst_na->num_tx_rings) 3043f18be576SLuigi Rizzo dst_nr = dst_nr % dst_na->num_tx_rings; 3044f18be576SLuigi Rizzo kring = &dst_na->tx_rings[dst_nr]; 3045f18be576SLuigi Rizzo ring = kring->ring; 3046f18be576SLuigi Rizzo lim = kring->nkr_num_slots - 1; 3047f18be576SLuigi Rizzo dst_na->nm_lock(dst_ifp, NETMAP_TX_LOCK, dst_nr); 3048f18be576SLuigi Rizzo retry: 3049f18be576SLuigi Rizzo dst_na->nm_txsync(dst_ifp, dst_nr, 0); 3050f18be576SLuigi Rizzo /* see nm_bdg_flush() */ 3051f18be576SLuigi Rizzo j = kring->nr_hwcur; 3052f18be576SLuigi Rizzo howmany = kring->nr_hwavail; 3053f18be576SLuigi Rizzo } 3054f18be576SLuigi Rizzo while (howmany-- > 0) { 3055f18be576SLuigi Rizzo struct netmap_slot *slot; 3056f18be576SLuigi Rizzo struct nm_bdg_fwd *ft_p; 3057f18be576SLuigi Rizzo 305885233a7dSLuigi Rizzo /* our 'NULL' is always higher than valid indexes 305985233a7dSLuigi Rizzo * so we never dereference it if the other list 306085233a7dSLuigi Rizzo * has packets (and if both are NULL we never 306185233a7dSLuigi Rizzo * get here). 306285233a7dSLuigi Rizzo */ 3063f18be576SLuigi Rizzo if (next < brd_next) { 3064f18be576SLuigi Rizzo ft_p = ft + next; 3065f18be576SLuigi Rizzo next = ft_p->ft_next; 306685233a7dSLuigi Rizzo ND("j %d uni %d next %d %d", 306785233a7dSLuigi Rizzo j, ft_p - ft, next, brd_next); 3068f18be576SLuigi Rizzo } else { /* insert broadcast */ 3069f18be576SLuigi Rizzo ft_p = ft + brd_next; 3070f18be576SLuigi Rizzo brd_next = ft_p->ft_next; 307185233a7dSLuigi Rizzo ND("j %d brd %d next %d %d", 307285233a7dSLuigi Rizzo j, ft_p - ft, next, brd_next); 3073f18be576SLuigi Rizzo } 3074f196ce38SLuigi Rizzo slot = &ring->slot[j]; 3075f18be576SLuigi Rizzo ND("send %d %d bytes at %s:%d", i, ft_p->ft_len, dst_ifp->if_xname, j); 307685233a7dSLuigi Rizzo if (ft_p->ft_flags & NS_INDIRECT) { 307785233a7dSLuigi Rizzo ND("copying from INDIRECT source"); 307885233a7dSLuigi Rizzo copyin(ft_p->ft_buf, NMB(slot), 307985233a7dSLuigi Rizzo (ft_p->ft_len + 63) & ~63); 308085233a7dSLuigi Rizzo } else { 308185233a7dSLuigi Rizzo pkt_copy(ft_p->ft_buf, NMB(slot), ft_p->ft_len); 308285233a7dSLuigi Rizzo } 3083f18be576SLuigi Rizzo slot->len = ft_p->ft_len; 308485233a7dSLuigi Rizzo j = unlikely(j == lim) ? 0: j + 1; /* XXX to be macro-ed */ 3085f196ce38SLuigi Rizzo sent++; 308685233a7dSLuigi Rizzo /* are we done ? */ 308785233a7dSLuigi Rizzo if (next == NM_BDG_BATCH && brd_next == NM_BDG_BATCH) 3088f18be576SLuigi Rizzo break; 3089f196ce38SLuigi Rizzo } 3090f18be576SLuigi Rizzo if (netmap_verbose && (howmany < 0)) 3091f18be576SLuigi Rizzo D("rx ring full on %s", dst_ifp->if_xname); 3092f18be576SLuigi Rizzo if (is_vp) { 3093f18be576SLuigi Rizzo if (sent) { 3094f18be576SLuigi Rizzo kring->nr_hwavail += sent; 3095f196ce38SLuigi Rizzo selwakeuppri(&kring->si, PI_NET); 3096f196ce38SLuigi Rizzo } 3097f18be576SLuigi Rizzo dst_na->nm_lock(dst_ifp, NETMAP_RX_UNLOCK, dst_nr); 3098f18be576SLuigi Rizzo } else { 3099f18be576SLuigi Rizzo if (sent) { 3100f18be576SLuigi Rizzo ring->avail -= sent; 3101f18be576SLuigi Rizzo ring->cur = j; 3102f18be576SLuigi Rizzo dst_na->nm_txsync(dst_ifp, dst_nr, 0); 3103f196ce38SLuigi Rizzo } 3104f18be576SLuigi Rizzo /* retry to send more packets */ 3105f18be576SLuigi Rizzo if (nma_is_hw(dst_na) && howmany < 0 && retry--) 3106f18be576SLuigi Rizzo goto retry; 3107f18be576SLuigi Rizzo dst_na->nm_lock(dst_ifp, NETMAP_TX_UNLOCK, dst_nr); 3108f18be576SLuigi Rizzo } 310985233a7dSLuigi Rizzo /* NM_BDG_BATCH means 'no packet' */ 3110f18be576SLuigi Rizzo d->bq_head = d->bq_tail = NM_BDG_BATCH; /* cleanup */ 3111f18be576SLuigi Rizzo } 3112f18be576SLuigi Rizzo brddst->bq_head = brddst->bq_tail = NM_BDG_BATCH; /* cleanup */ 3113f18be576SLuigi Rizzo BDG_RUNLOCK(b); 3114f196ce38SLuigi Rizzo return 0; 3115f196ce38SLuigi Rizzo } 3116f196ce38SLuigi Rizzo 3117f18be576SLuigi Rizzo 3118f196ce38SLuigi Rizzo /* 3119f196ce38SLuigi Rizzo * main dispatch routine 3120f196ce38SLuigi Rizzo */ 3121f196ce38SLuigi Rizzo static int 3122f196ce38SLuigi Rizzo bdg_netmap_txsync(struct ifnet *ifp, u_int ring_nr, int do_lock) 3123f196ce38SLuigi Rizzo { 3124f196ce38SLuigi Rizzo struct netmap_adapter *na = NA(ifp); 3125f196ce38SLuigi Rizzo struct netmap_kring *kring = &na->tx_rings[ring_nr]; 3126f196ce38SLuigi Rizzo struct netmap_ring *ring = kring->ring; 3127f196ce38SLuigi Rizzo int i, j, k, lim = kring->nkr_num_slots - 1; 3128f196ce38SLuigi Rizzo 3129f196ce38SLuigi Rizzo k = ring->cur; 3130f196ce38SLuigi Rizzo if (k > lim) 3131f196ce38SLuigi Rizzo return netmap_ring_reinit(kring); 3132f196ce38SLuigi Rizzo if (do_lock) 3133f196ce38SLuigi Rizzo na->nm_lock(ifp, NETMAP_TX_LOCK, ring_nr); 3134f196ce38SLuigi Rizzo 3135f196ce38SLuigi Rizzo if (netmap_bridge <= 0) { /* testing only */ 3136f196ce38SLuigi Rizzo j = k; // used all 3137f196ce38SLuigi Rizzo goto done; 3138f196ce38SLuigi Rizzo } 3139f196ce38SLuigi Rizzo if (netmap_bridge > NM_BDG_BATCH) 3140f196ce38SLuigi Rizzo netmap_bridge = NM_BDG_BATCH; 3141f196ce38SLuigi Rizzo 3142f18be576SLuigi Rizzo j = nm_bdg_preflush(na, ring_nr, kring, k); 3143f196ce38SLuigi Rizzo i = k - j; 3144f196ce38SLuigi Rizzo if (i < 0) 3145f196ce38SLuigi Rizzo i += kring->nkr_num_slots; 3146f196ce38SLuigi Rizzo kring->nr_hwavail = kring->nkr_num_slots - 1 - i; 3147f196ce38SLuigi Rizzo if (j != k) 3148f196ce38SLuigi Rizzo D("early break at %d/ %d, avail %d", j, k, kring->nr_hwavail); 3149f196ce38SLuigi Rizzo 3150f196ce38SLuigi Rizzo done: 3151f196ce38SLuigi Rizzo kring->nr_hwcur = j; 3152f196ce38SLuigi Rizzo ring->avail = kring->nr_hwavail; 3153f196ce38SLuigi Rizzo if (do_lock) 3154f196ce38SLuigi Rizzo na->nm_lock(ifp, NETMAP_TX_UNLOCK, ring_nr); 3155f196ce38SLuigi Rizzo 3156f196ce38SLuigi Rizzo if (netmap_verbose) 3157f196ce38SLuigi Rizzo D("%s ring %d lock %d", ifp->if_xname, ring_nr, do_lock); 3158f196ce38SLuigi Rizzo return 0; 3159f196ce38SLuigi Rizzo } 3160f196ce38SLuigi Rizzo 3161f18be576SLuigi Rizzo 3162f196ce38SLuigi Rizzo static int 3163f196ce38SLuigi Rizzo bdg_netmap_rxsync(struct ifnet *ifp, u_int ring_nr, int do_lock) 3164f196ce38SLuigi Rizzo { 3165f196ce38SLuigi Rizzo struct netmap_adapter *na = NA(ifp); 3166f196ce38SLuigi Rizzo struct netmap_kring *kring = &na->rx_rings[ring_nr]; 3167f196ce38SLuigi Rizzo struct netmap_ring *ring = kring->ring; 3168f18be576SLuigi Rizzo u_int j, lim = kring->nkr_num_slots - 1; 3169f196ce38SLuigi Rizzo u_int k = ring->cur, resvd = ring->reserved; 3170f18be576SLuigi Rizzo int n; 3171f196ce38SLuigi Rizzo 3172f196ce38SLuigi Rizzo ND("%s ring %d lock %d avail %d", 3173f196ce38SLuigi Rizzo ifp->if_xname, ring_nr, do_lock, kring->nr_hwavail); 3174f196ce38SLuigi Rizzo 3175f196ce38SLuigi Rizzo if (k > lim) 3176f196ce38SLuigi Rizzo return netmap_ring_reinit(kring); 3177f196ce38SLuigi Rizzo if (do_lock) 3178f196ce38SLuigi Rizzo na->nm_lock(ifp, NETMAP_RX_LOCK, ring_nr); 3179f196ce38SLuigi Rizzo 3180f196ce38SLuigi Rizzo /* skip past packets that userspace has released */ 3181f196ce38SLuigi Rizzo j = kring->nr_hwcur; /* netmap ring index */ 3182f196ce38SLuigi Rizzo if (resvd > 0) { 3183f196ce38SLuigi Rizzo if (resvd + ring->avail >= lim + 1) { 3184f196ce38SLuigi Rizzo D("XXX invalid reserve/avail %d %d", resvd, ring->avail); 3185f196ce38SLuigi Rizzo ring->reserved = resvd = 0; // XXX panic... 3186f196ce38SLuigi Rizzo } 3187f196ce38SLuigi Rizzo k = (k >= resvd) ? k - resvd : k + lim + 1 - resvd; 3188f196ce38SLuigi Rizzo } 3189f196ce38SLuigi Rizzo 3190f196ce38SLuigi Rizzo if (j != k) { /* userspace has released some packets. */ 3191f196ce38SLuigi Rizzo n = k - j; 3192f196ce38SLuigi Rizzo if (n < 0) 3193f196ce38SLuigi Rizzo n += kring->nkr_num_slots; 3194f196ce38SLuigi Rizzo ND("userspace releases %d packets", n); 3195f196ce38SLuigi Rizzo for (n = 0; likely(j != k); n++) { 3196f196ce38SLuigi Rizzo struct netmap_slot *slot = &ring->slot[j]; 3197f196ce38SLuigi Rizzo void *addr = NMB(slot); 3198f196ce38SLuigi Rizzo 3199f196ce38SLuigi Rizzo if (addr == netmap_buffer_base) { /* bad buf */ 3200f196ce38SLuigi Rizzo if (do_lock) 3201f196ce38SLuigi Rizzo na->nm_lock(ifp, NETMAP_RX_UNLOCK, ring_nr); 3202f196ce38SLuigi Rizzo return netmap_ring_reinit(kring); 3203f196ce38SLuigi Rizzo } 3204f196ce38SLuigi Rizzo /* decrease refcount for buffer */ 3205f196ce38SLuigi Rizzo 3206f196ce38SLuigi Rizzo slot->flags &= ~NS_BUF_CHANGED; 3207f196ce38SLuigi Rizzo j = unlikely(j == lim) ? 0 : j + 1; 3208f196ce38SLuigi Rizzo } 3209f196ce38SLuigi Rizzo kring->nr_hwavail -= n; 3210f196ce38SLuigi Rizzo kring->nr_hwcur = k; 3211f196ce38SLuigi Rizzo } 3212f196ce38SLuigi Rizzo /* tell userspace that there are new packets */ 3213f196ce38SLuigi Rizzo ring->avail = kring->nr_hwavail - resvd; 3214f196ce38SLuigi Rizzo 3215f196ce38SLuigi Rizzo if (do_lock) 3216f196ce38SLuigi Rizzo na->nm_lock(ifp, NETMAP_RX_UNLOCK, ring_nr); 3217f196ce38SLuigi Rizzo return 0; 3218f196ce38SLuigi Rizzo } 3219f196ce38SLuigi Rizzo 3220f18be576SLuigi Rizzo 3221f196ce38SLuigi Rizzo static void 3222f18be576SLuigi Rizzo bdg_netmap_attach(struct netmap_adapter *arg) 3223f196ce38SLuigi Rizzo { 3224f196ce38SLuigi Rizzo struct netmap_adapter na; 3225f196ce38SLuigi Rizzo 3226f196ce38SLuigi Rizzo ND("attaching virtual bridge"); 3227f196ce38SLuigi Rizzo bzero(&na, sizeof(na)); 3228f196ce38SLuigi Rizzo 3229f18be576SLuigi Rizzo na.ifp = arg->ifp; 3230f196ce38SLuigi Rizzo na.separate_locks = 1; 3231f18be576SLuigi Rizzo na.num_tx_rings = arg->num_tx_rings; 3232f18be576SLuigi Rizzo na.num_rx_rings = arg->num_rx_rings; 3233f196ce38SLuigi Rizzo na.num_tx_desc = NM_BRIDGE_RINGSIZE; 3234f196ce38SLuigi Rizzo na.num_rx_desc = NM_BRIDGE_RINGSIZE; 3235f196ce38SLuigi Rizzo na.nm_txsync = bdg_netmap_txsync; 3236f196ce38SLuigi Rizzo na.nm_rxsync = bdg_netmap_rxsync; 3237f196ce38SLuigi Rizzo na.nm_register = bdg_netmap_reg; 3238f18be576SLuigi Rizzo netmap_attach(&na, na.num_tx_rings); 3239f196ce38SLuigi Rizzo } 3240f196ce38SLuigi Rizzo 3241f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */ 3242babc7c12SLuigi Rizzo 3243babc7c12SLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */ 3244babc7c12SLuigi Rizzo 3245babc7c12SLuigi Rizzo 32461a26580eSLuigi Rizzo /* 324768b8534bSLuigi Rizzo * Module loader. 324868b8534bSLuigi Rizzo * 324968b8534bSLuigi Rizzo * Create the /dev/netmap device and initialize all global 325068b8534bSLuigi Rizzo * variables. 325168b8534bSLuigi Rizzo * 325268b8534bSLuigi Rizzo * Return 0 on success, errno on failure. 325368b8534bSLuigi Rizzo */ 325468b8534bSLuigi Rizzo static int 325568b8534bSLuigi Rizzo netmap_init(void) 325668b8534bSLuigi Rizzo { 325768b8534bSLuigi Rizzo int error; 325868b8534bSLuigi Rizzo 325968b8534bSLuigi Rizzo error = netmap_memory_init(); 326068b8534bSLuigi Rizzo if (error != 0) { 326142a3a5bdSLuigi Rizzo printf("netmap: unable to initialize the memory allocator.\n"); 326268b8534bSLuigi Rizzo return (error); 326368b8534bSLuigi Rizzo } 32648241616dSLuigi Rizzo printf("netmap: loaded module\n"); 326568b8534bSLuigi Rizzo netmap_dev = make_dev(&netmap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660, 326668b8534bSLuigi Rizzo "netmap"); 3267f196ce38SLuigi Rizzo 3268f196ce38SLuigi Rizzo #ifdef NM_BRIDGE 3269f196ce38SLuigi Rizzo { 3270f196ce38SLuigi Rizzo int i; 3271f18be576SLuigi Rizzo mtx_init(&netmap_bridge_mutex, "netmap_bridge_mutex", 3272f18be576SLuigi Rizzo MTX_NETWORK_LOCK, MTX_DEF); 3273f18be576SLuigi Rizzo bzero(nm_bridges, sizeof(struct nm_bridge) * NM_BRIDGES); /* safety */ 3274f196ce38SLuigi Rizzo for (i = 0; i < NM_BRIDGES; i++) 3275f18be576SLuigi Rizzo rw_init(&nm_bridges[i].bdg_lock, "bdg lock"); 3276f196ce38SLuigi Rizzo } 3277f196ce38SLuigi Rizzo #endif 3278babc7c12SLuigi Rizzo return (error); 327968b8534bSLuigi Rizzo } 328068b8534bSLuigi Rizzo 328168b8534bSLuigi Rizzo 328268b8534bSLuigi Rizzo /* 328368b8534bSLuigi Rizzo * Module unloader. 328468b8534bSLuigi Rizzo * 328568b8534bSLuigi Rizzo * Free all the memory, and destroy the ``/dev/netmap`` device. 328668b8534bSLuigi Rizzo */ 328768b8534bSLuigi Rizzo static void 328868b8534bSLuigi Rizzo netmap_fini(void) 328968b8534bSLuigi Rizzo { 329068b8534bSLuigi Rizzo destroy_dev(netmap_dev); 329168b8534bSLuigi Rizzo netmap_memory_fini(); 329268b8534bSLuigi Rizzo printf("netmap: unloaded module.\n"); 329368b8534bSLuigi Rizzo } 329468b8534bSLuigi Rizzo 329568b8534bSLuigi Rizzo 3296f196ce38SLuigi Rizzo #ifdef __FreeBSD__ 329768b8534bSLuigi Rizzo /* 329868b8534bSLuigi Rizzo * Kernel entry point. 329968b8534bSLuigi Rizzo * 330068b8534bSLuigi Rizzo * Initialize/finalize the module and return. 330168b8534bSLuigi Rizzo * 330268b8534bSLuigi Rizzo * Return 0 on success, errno on failure. 330368b8534bSLuigi Rizzo */ 330468b8534bSLuigi Rizzo static int 330568b8534bSLuigi Rizzo netmap_loader(__unused struct module *module, int event, __unused void *arg) 330668b8534bSLuigi Rizzo { 330768b8534bSLuigi Rizzo int error = 0; 330868b8534bSLuigi Rizzo 330968b8534bSLuigi Rizzo switch (event) { 331068b8534bSLuigi Rizzo case MOD_LOAD: 331168b8534bSLuigi Rizzo error = netmap_init(); 331268b8534bSLuigi Rizzo break; 331368b8534bSLuigi Rizzo 331468b8534bSLuigi Rizzo case MOD_UNLOAD: 331568b8534bSLuigi Rizzo netmap_fini(); 331668b8534bSLuigi Rizzo break; 331768b8534bSLuigi Rizzo 331868b8534bSLuigi Rizzo default: 331968b8534bSLuigi Rizzo error = EOPNOTSUPP; 332068b8534bSLuigi Rizzo break; 332168b8534bSLuigi Rizzo } 332268b8534bSLuigi Rizzo 332368b8534bSLuigi Rizzo return (error); 332468b8534bSLuigi Rizzo } 332568b8534bSLuigi Rizzo 332668b8534bSLuigi Rizzo 332768b8534bSLuigi Rizzo DEV_MODULE(netmap, netmap_loader, NULL); 3328f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */ 3329