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> 9268b8534bSLuigi Rizzo #include <net/bpf.h> /* BIOCIMMEDIATE */ 93506cc70cSLuigi Rizzo #include <net/vnet.h> 9468b8534bSLuigi Rizzo #include <machine/bus.h> /* bus_dmamap_* */ 9568b8534bSLuigi Rizzo 9668b8534bSLuigi Rizzo MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map"); 97f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */ 9868b8534bSLuigi Rizzo 990b8ed8e0SLuigi Rizzo #include <net/netmap.h> 1000b8ed8e0SLuigi Rizzo #include <dev/netmap/netmap_kern.h> 1010b8ed8e0SLuigi Rizzo 102d4b42e08SLuigi Rizzo /* XXX the following variables must be deprecated and included in nm_mem */ 1035819da83SLuigi Rizzo u_int netmap_total_buffers; 1048241616dSLuigi Rizzo u_int netmap_buf_size; 1055819da83SLuigi Rizzo char *netmap_buffer_base; /* address of an invalid buffer */ 1065819da83SLuigi Rizzo 1075819da83SLuigi Rizzo /* user-controlled variables */ 1085819da83SLuigi Rizzo int netmap_verbose; 1095819da83SLuigi Rizzo 1105819da83SLuigi Rizzo static int netmap_no_timestamp; /* don't timestamp on rxsync */ 1115819da83SLuigi Rizzo 1125819da83SLuigi Rizzo SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args"); 1135819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose, 1145819da83SLuigi Rizzo CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode"); 1155819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp, 1165819da83SLuigi Rizzo CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp"); 1175819da83SLuigi Rizzo int netmap_mitigate = 1; 1185819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mitigate, CTLFLAG_RW, &netmap_mitigate, 0, ""); 119c85cb1a0SLuigi Rizzo int netmap_no_pendintr = 1; 1205819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr, 1215819da83SLuigi Rizzo CTLFLAG_RW, &netmap_no_pendintr, 0, "Always look for new received packets."); 122*f18be576SLuigi Rizzo int netmap_txsync_retry = 2; 123*f18be576SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, txsync_retry, CTLFLAG_RW, 124*f18be576SLuigi Rizzo &netmap_txsync_retry, 0 , "Number of txsync loops in bridge's flush."); 1255819da83SLuigi Rizzo 126f196ce38SLuigi Rizzo int netmap_drop = 0; /* debugging */ 127f196ce38SLuigi Rizzo int netmap_flags = 0; /* debug flags */ 128091fd0abSLuigi Rizzo int netmap_fwd = 0; /* force transparent mode */ 129f196ce38SLuigi Rizzo 130f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, drop, CTLFLAG_RW, &netmap_drop, 0 , ""); 131f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, flags, CTLFLAG_RW, &netmap_flags, 0 , ""); 132091fd0abSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, fwd, CTLFLAG_RW, &netmap_fwd, 0 , ""); 133f196ce38SLuigi Rizzo 134*f18be576SLuigi Rizzo #ifdef NM_BRIDGE /* support for netmap virtual switch, called VALE */ 135f196ce38SLuigi Rizzo 136f196ce38SLuigi Rizzo /* 137*f18be576SLuigi Rizzo * system parameters (most of them in netmap_kern.h) 138*f18be576SLuigi Rizzo * NM_NAME prefix for switch port names, default "vale" 139*f18be576SLuigi Rizzo * NM_MAXPORTS number of ports 140*f18be576SLuigi Rizzo * NM_BRIDGES max number of switches in the system. 141*f18be576SLuigi Rizzo * XXX should become a sysctl or tunable 142f196ce38SLuigi Rizzo * 143*f18be576SLuigi Rizzo * Switch ports are named valeX:Y where X is the switch name and Y 144*f18be576SLuigi Rizzo * is the port. If Y matches a physical interface name, the port is 145*f18be576SLuigi Rizzo * connected to a physical device. 146*f18be576SLuigi Rizzo * 147*f18be576SLuigi Rizzo * Unlike physical interfaces, switch ports use their own memory region 148*f18be576SLuigi Rizzo * for rings and buffers. 149f196ce38SLuigi Rizzo * The virtual interfaces use per-queue lock instead of core lock. 150f196ce38SLuigi Rizzo * In the tx loop, we aggregate traffic in batches to make all operations 151f196ce38SLuigi Rizzo * faster. The batch size is NM_BDG_BATCH 152f196ce38SLuigi Rizzo */ 153*f18be576SLuigi Rizzo #define NM_BDG_MAXRINGS 16 /* XXX unclear how many. */ 154f196ce38SLuigi Rizzo #define NM_BRIDGE_RINGSIZE 1024 /* in the device */ 155f196ce38SLuigi Rizzo #define NM_BDG_HASH 1024 /* forwarding table entries */ 156f196ce38SLuigi Rizzo #define NM_BDG_BATCH 1024 /* entries in the forwarding buffer */ 157*f18be576SLuigi Rizzo #define NM_BRIDGES 8 /* number of bridges */ 158d4b42e08SLuigi Rizzo 159d4b42e08SLuigi Rizzo 160f196ce38SLuigi Rizzo int netmap_bridge = NM_BDG_BATCH; /* bridge batch size */ 161f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, bridge, CTLFLAG_RW, &netmap_bridge, 0 , ""); 16201c7d25fSLuigi Rizzo 163f196ce38SLuigi Rizzo #ifdef linux 164849bec0eSLuigi Rizzo 165849bec0eSLuigi Rizzo #define refcount_acquire(_a) atomic_add(1, (atomic_t *)_a) 166849bec0eSLuigi Rizzo #define refcount_release(_a) atomic_dec_and_test((atomic_t *)_a) 167849bec0eSLuigi Rizzo 168f196ce38SLuigi Rizzo #else /* !linux */ 169849bec0eSLuigi Rizzo 170f196ce38SLuigi Rizzo #ifdef __FreeBSD__ 171f196ce38SLuigi Rizzo #include <sys/endian.h> 172f196ce38SLuigi Rizzo #include <sys/refcount.h> 173f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */ 174849bec0eSLuigi Rizzo 17501c7d25fSLuigi Rizzo #define prefetch(x) __builtin_prefetch(x) 176849bec0eSLuigi Rizzo 177f196ce38SLuigi Rizzo #endif /* !linux */ 178f196ce38SLuigi Rizzo 179849bec0eSLuigi Rizzo /* 180849bec0eSLuigi Rizzo * These are used to handle reference counters for bridge ports. 181849bec0eSLuigi Rizzo */ 182849bec0eSLuigi Rizzo #define ADD_BDG_REF(ifp) refcount_acquire(&NA(ifp)->na_bdg_refcount) 183849bec0eSLuigi Rizzo #define DROP_BDG_REF(ifp) refcount_release(&NA(ifp)->na_bdg_refcount) 184849bec0eSLuigi Rizzo 185*f18be576SLuigi Rizzo static void bdg_netmap_attach(struct netmap_adapter *); 186f196ce38SLuigi Rizzo static int bdg_netmap_reg(struct ifnet *ifp, int onoff); 187*f18be576SLuigi Rizzo static int kern_netmap_regif(struct nmreq *nmr); 188*f18be576SLuigi Rizzo 189f196ce38SLuigi Rizzo /* per-tx-queue entry */ 190f196ce38SLuigi Rizzo struct nm_bdg_fwd { /* forwarding entry for a bridge */ 191f196ce38SLuigi Rizzo void *buf; 192*f18be576SLuigi Rizzo uint32_t ft_dst; /* dst port */ 193*f18be576SLuigi Rizzo uint16_t ft_len; /* src len */ 194*f18be576SLuigi Rizzo uint16_t ft_next; /* next packet to same destination */ 195*f18be576SLuigi Rizzo }; 196*f18be576SLuigi Rizzo 197*f18be576SLuigi Rizzo /* We need to build a list of buffers going to each destination. 198*f18be576SLuigi Rizzo * Each buffer is in one entry of struct nm_bdg_fwd, we use ft_next 199*f18be576SLuigi Rizzo * to build the list, and struct nm_bdg_q below for the queue. 200*f18be576SLuigi Rizzo * The structure should compact because potentially we have a lot 201*f18be576SLuigi Rizzo * of destinations. 202*f18be576SLuigi Rizzo */ 203*f18be576SLuigi Rizzo struct nm_bdg_q { 204*f18be576SLuigi Rizzo uint16_t bq_head; 205*f18be576SLuigi Rizzo uint16_t bq_tail; 206f196ce38SLuigi Rizzo }; 207f196ce38SLuigi Rizzo 208f196ce38SLuigi Rizzo struct nm_hash_ent { 209f196ce38SLuigi Rizzo uint64_t mac; /* the top 2 bytes are the epoch */ 210f196ce38SLuigi Rizzo uint64_t ports; 211f196ce38SLuigi Rizzo }; 212f196ce38SLuigi Rizzo 213f196ce38SLuigi Rizzo /* 214849bec0eSLuigi Rizzo * Interfaces for a bridge are all in bdg_ports[]. 215f196ce38SLuigi Rizzo * The array has fixed size, an empty entry does not terminate 216849bec0eSLuigi Rizzo * the search. But lookups only occur on attach/detach so we 217849bec0eSLuigi Rizzo * don't mind if they are slow. 218849bec0eSLuigi Rizzo * 219849bec0eSLuigi Rizzo * The bridge is non blocking on the transmit ports. 220849bec0eSLuigi Rizzo * 221849bec0eSLuigi Rizzo * bdg_lock protects accesses to the bdg_ports array. 222*f18be576SLuigi Rizzo * This is a rw lock (or equivalent). 223f196ce38SLuigi Rizzo */ 224f196ce38SLuigi Rizzo struct nm_bridge { 225*f18be576SLuigi Rizzo int namelen; /* 0 means free */ 226*f18be576SLuigi Rizzo 227*f18be576SLuigi Rizzo /* XXX what is the proper alignment/layout ? */ 228*f18be576SLuigi Rizzo NM_RWLOCK_T bdg_lock; /* protects bdg_ports */ 229*f18be576SLuigi Rizzo struct netmap_adapter *bdg_ports[NM_BDG_MAXPORTS]; 230*f18be576SLuigi Rizzo 231*f18be576SLuigi Rizzo char basename[IFNAMSIZ]; 232*f18be576SLuigi Rizzo /* 233*f18be576SLuigi Rizzo * The function to decide the destination port. 234*f18be576SLuigi Rizzo * It returns either of an index of the destination port, 235*f18be576SLuigi Rizzo * NM_BDG_BROADCAST to broadcast this packet, or NM_BDG_NOPORT not to 236*f18be576SLuigi Rizzo * forward this packet. ring_nr is the source ring index, and the 237*f18be576SLuigi Rizzo * function may overwrite this value to forward this packet to a 238*f18be576SLuigi Rizzo * different ring index. 239*f18be576SLuigi Rizzo * This function must be set by netmap_bdgctl(). 240*f18be576SLuigi Rizzo */ 241*f18be576SLuigi Rizzo bdg_lookup_fn_t nm_bdg_lookup; 242f196ce38SLuigi Rizzo 243f196ce38SLuigi Rizzo /* the forwarding table, MAC+ports */ 244f196ce38SLuigi Rizzo struct nm_hash_ent ht[NM_BDG_HASH]; 245f196ce38SLuigi Rizzo }; 246f196ce38SLuigi Rizzo 247f196ce38SLuigi Rizzo struct nm_bridge nm_bridges[NM_BRIDGES]; 248*f18be576SLuigi Rizzo NM_LOCK_T netmap_bridge_mutex; 249f196ce38SLuigi Rizzo 250*f18be576SLuigi Rizzo /* other OS will have these macros defined in their own glue code. */ 251*f18be576SLuigi Rizzo 252*f18be576SLuigi Rizzo #ifdef __FreeBSD__ 253*f18be576SLuigi Rizzo #define BDG_LOCK() mtx_lock(&netmap_bridge_mutex) 254*f18be576SLuigi Rizzo #define BDG_UNLOCK() mtx_unlock(&netmap_bridge_mutex) 255*f18be576SLuigi Rizzo #define BDG_WLOCK(b) rw_wlock(&(b)->bdg_lock) 256*f18be576SLuigi Rizzo #define BDG_WUNLOCK(b) rw_wunlock(&(b)->bdg_lock) 257*f18be576SLuigi Rizzo #define BDG_RLOCK(b) rw_rlock(&(b)->bdg_lock) 258*f18be576SLuigi Rizzo #define BDG_RUNLOCK(b) rw_runlock(&(b)->bdg_lock) 259*f18be576SLuigi Rizzo 260*f18be576SLuigi Rizzo /* set/get variables. OS-specific macros may wrap these 261*f18be576SLuigi Rizzo * assignments into read/write lock or similar 262*f18be576SLuigi Rizzo */ 263*f18be576SLuigi Rizzo #define BDG_SET_VAR(lval, p) (lval = p) 264*f18be576SLuigi Rizzo #define BDG_GET_VAR(lval) (lval) 265*f18be576SLuigi Rizzo #define BDG_FREE(p) free(p, M_DEVBUF) 266*f18be576SLuigi Rizzo #endif /* __FreeBSD__ */ 267*f18be576SLuigi Rizzo 268*f18be576SLuigi Rizzo static __inline int 269*f18be576SLuigi Rizzo nma_is_vp(struct netmap_adapter *na) 270*f18be576SLuigi Rizzo { 271*f18be576SLuigi Rizzo return na->nm_register == bdg_netmap_reg; 272*f18be576SLuigi Rizzo } 273*f18be576SLuigi Rizzo static __inline int 274*f18be576SLuigi Rizzo nma_is_host(struct netmap_adapter *na) 275*f18be576SLuigi Rizzo { 276*f18be576SLuigi Rizzo return na->nm_register == NULL; 277*f18be576SLuigi Rizzo } 278*f18be576SLuigi Rizzo static __inline int 279*f18be576SLuigi Rizzo nma_is_hw(struct netmap_adapter *na) 280*f18be576SLuigi Rizzo { 281*f18be576SLuigi Rizzo /* In case of sw adapter, nm_register is NULL */ 282*f18be576SLuigi Rizzo return !nma_is_vp(na) && !nma_is_host(na); 283*f18be576SLuigi Rizzo } 284*f18be576SLuigi Rizzo 285*f18be576SLuigi Rizzo /* 286*f18be576SLuigi Rizzo * Regarding holding a NIC, if the NIC is owned by the kernel 287*f18be576SLuigi Rizzo * (i.e., bridge), neither another bridge nor user can use it; 288*f18be576SLuigi Rizzo * if the NIC is owned by a user, only users can share it. 289*f18be576SLuigi Rizzo * Evaluation must be done under NMA_LOCK(). 290*f18be576SLuigi Rizzo */ 291*f18be576SLuigi Rizzo #define NETMAP_OWNED_BY_KERN(ifp) (!nma_is_vp(NA(ifp)) && NA(ifp)->na_bdg) 292*f18be576SLuigi Rizzo #define NETMAP_OWNED_BY_ANY(ifp) \ 293*f18be576SLuigi Rizzo (NETMAP_OWNED_BY_KERN(ifp) || (NA(ifp)->refcount > 0)) 294f196ce38SLuigi Rizzo 295f196ce38SLuigi Rizzo /* 296f196ce38SLuigi Rizzo * NA(ifp)->bdg_port port index 297f196ce38SLuigi Rizzo */ 298f196ce38SLuigi Rizzo 299f196ce38SLuigi Rizzo // XXX only for multiples of 64 bytes, non overlapped. 300f196ce38SLuigi Rizzo static inline void 301f196ce38SLuigi Rizzo pkt_copy(void *_src, void *_dst, int l) 302f196ce38SLuigi Rizzo { 303f196ce38SLuigi Rizzo uint64_t *src = _src; 304f196ce38SLuigi Rizzo uint64_t *dst = _dst; 305f196ce38SLuigi Rizzo if (unlikely(l >= 1024)) { 306f196ce38SLuigi Rizzo bcopy(src, dst, l); 307f196ce38SLuigi Rizzo return; 308f196ce38SLuigi Rizzo } 309f196ce38SLuigi Rizzo for (; likely(l > 0); l-=64) { 310f196ce38SLuigi Rizzo *dst++ = *src++; 311f196ce38SLuigi Rizzo *dst++ = *src++; 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 } 319f196ce38SLuigi Rizzo } 320f196ce38SLuigi Rizzo 321*f18be576SLuigi Rizzo 322f196ce38SLuigi Rizzo /* 323f196ce38SLuigi Rizzo * locate a bridge among the existing ones. 324f196ce38SLuigi Rizzo * a ':' in the name terminates the bridge name. Otherwise, just NM_NAME. 325f196ce38SLuigi Rizzo * We assume that this is called with a name of at least NM_NAME chars. 326f196ce38SLuigi Rizzo */ 327f196ce38SLuigi Rizzo static struct nm_bridge * 328*f18be576SLuigi Rizzo nm_find_bridge(const char *name, int create) 329f196ce38SLuigi Rizzo { 330*f18be576SLuigi Rizzo int i, l, namelen; 331f196ce38SLuigi Rizzo struct nm_bridge *b = NULL; 332f196ce38SLuigi Rizzo 333f196ce38SLuigi Rizzo namelen = strlen(NM_NAME); /* base length */ 334f196ce38SLuigi Rizzo l = strlen(name); /* actual length */ 335f196ce38SLuigi Rizzo for (i = namelen + 1; i < l; i++) { 336f196ce38SLuigi Rizzo if (name[i] == ':') { 337f196ce38SLuigi Rizzo namelen = i; 338f196ce38SLuigi Rizzo break; 339f196ce38SLuigi Rizzo } 340f196ce38SLuigi Rizzo } 341f196ce38SLuigi Rizzo if (namelen >= IFNAMSIZ) 342f196ce38SLuigi Rizzo namelen = IFNAMSIZ; 343f196ce38SLuigi Rizzo ND("--- prefix is '%.*s' ---", namelen, name); 344f196ce38SLuigi Rizzo 345*f18be576SLuigi Rizzo BDG_LOCK(); 346*f18be576SLuigi Rizzo /* lookup the name, remember empty slot if there is one */ 347*f18be576SLuigi Rizzo for (i = 0; i < NM_BRIDGES; i++) { 348*f18be576SLuigi Rizzo struct nm_bridge *x = nm_bridges + i; 349*f18be576SLuigi Rizzo 350*f18be576SLuigi Rizzo if (x->namelen == 0) { 351*f18be576SLuigi Rizzo if (create && b == NULL) 352*f18be576SLuigi Rizzo b = x; /* record empty slot */ 353*f18be576SLuigi Rizzo } else if (x->namelen != namelen) { 354*f18be576SLuigi Rizzo continue; 355*f18be576SLuigi Rizzo } else if (strncmp(name, x->basename, namelen) == 0) { 356f196ce38SLuigi Rizzo ND("found '%.*s' at %d", namelen, name, i); 357*f18be576SLuigi Rizzo b = x; 358f196ce38SLuigi Rizzo break; 359f196ce38SLuigi Rizzo } 360f196ce38SLuigi Rizzo } 361*f18be576SLuigi Rizzo if (i == NM_BRIDGES && b) { /* name not found, can create entry */ 362f196ce38SLuigi Rizzo strncpy(b->basename, name, namelen); 363f196ce38SLuigi Rizzo b->namelen = namelen; 364*f18be576SLuigi Rizzo /* set the default function */ 365*f18be576SLuigi Rizzo b->nm_bdg_lookup = netmap_bdg_learning; 366*f18be576SLuigi Rizzo /* reset the MAC address table */ 367*f18be576SLuigi Rizzo bzero(b->ht, sizeof(struct nm_hash_ent) * NM_BDG_HASH); 368f196ce38SLuigi Rizzo } 369*f18be576SLuigi Rizzo BDG_UNLOCK(); 370f196ce38SLuigi Rizzo return b; 371f196ce38SLuigi Rizzo } 372*f18be576SLuigi Rizzo 373*f18be576SLuigi Rizzo 374*f18be576SLuigi Rizzo /* 375*f18be576SLuigi Rizzo * Free the forwarding tables for rings attached to switch ports. 376*f18be576SLuigi Rizzo */ 377*f18be576SLuigi Rizzo static void 378*f18be576SLuigi Rizzo nm_free_bdgfwd(struct netmap_adapter *na) 379*f18be576SLuigi Rizzo { 380*f18be576SLuigi Rizzo int nrings, i; 381*f18be576SLuigi Rizzo struct netmap_kring *kring; 382*f18be576SLuigi Rizzo 383*f18be576SLuigi Rizzo nrings = nma_is_vp(na) ? na->num_tx_rings : na->num_rx_rings; 384*f18be576SLuigi Rizzo kring = nma_is_vp(na) ? na->tx_rings : na->rx_rings; 385*f18be576SLuigi Rizzo for (i = 0; i < nrings; i++) { 386*f18be576SLuigi Rizzo if (kring[i].nkr_ft) { 387*f18be576SLuigi Rizzo free(kring[i].nkr_ft, M_DEVBUF); 388*f18be576SLuigi Rizzo kring[i].nkr_ft = NULL; /* protect from freeing twice */ 389*f18be576SLuigi Rizzo } 390*f18be576SLuigi Rizzo } 391*f18be576SLuigi Rizzo if (nma_is_hw(na)) 392*f18be576SLuigi Rizzo nm_free_bdgfwd(SWNA(na->ifp)); 393*f18be576SLuigi Rizzo } 394*f18be576SLuigi Rizzo 395*f18be576SLuigi Rizzo 396*f18be576SLuigi Rizzo /* 397*f18be576SLuigi Rizzo * Allocate the forwarding tables for the rings attached to the bridge ports. 398*f18be576SLuigi Rizzo */ 399*f18be576SLuigi Rizzo static int 400*f18be576SLuigi Rizzo nm_alloc_bdgfwd(struct netmap_adapter *na) 401*f18be576SLuigi Rizzo { 402*f18be576SLuigi Rizzo int nrings, l, i, num_dstq; 403*f18be576SLuigi Rizzo struct netmap_kring *kring; 404*f18be576SLuigi Rizzo 405*f18be576SLuigi Rizzo /* all port:rings + broadcast */ 406*f18be576SLuigi Rizzo num_dstq = NM_BDG_MAXPORTS * NM_BDG_MAXRINGS + 1; 407*f18be576SLuigi Rizzo l = sizeof(struct nm_bdg_fwd) * NM_BDG_BATCH; 408*f18be576SLuigi Rizzo l += sizeof(struct nm_bdg_q) * num_dstq; 409*f18be576SLuigi Rizzo l += sizeof(uint16_t) * NM_BDG_BATCH; 410*f18be576SLuigi Rizzo 411*f18be576SLuigi Rizzo nrings = nma_is_vp(na) ? na->num_tx_rings : na->num_rx_rings; 412*f18be576SLuigi Rizzo kring = nma_is_vp(na) ? na->tx_rings : na->rx_rings; 413*f18be576SLuigi Rizzo for (i = 0; i < nrings; i++) { 414*f18be576SLuigi Rizzo struct nm_bdg_fwd *ft; 415*f18be576SLuigi Rizzo struct nm_bdg_q *dstq; 416*f18be576SLuigi Rizzo int j; 417*f18be576SLuigi Rizzo 418*f18be576SLuigi Rizzo ft = malloc(l, M_DEVBUF, M_NOWAIT | M_ZERO); 419*f18be576SLuigi Rizzo if (!ft) { 420*f18be576SLuigi Rizzo nm_free_bdgfwd(na); 421*f18be576SLuigi Rizzo return ENOMEM; 422*f18be576SLuigi Rizzo } 423*f18be576SLuigi Rizzo dstq = (struct nm_bdg_q *)(ft + NM_BDG_BATCH); 424*f18be576SLuigi Rizzo for (j = 0; j < num_dstq; j++) 425*f18be576SLuigi Rizzo dstq[j].bq_head = dstq[j].bq_tail = NM_BDG_BATCH; 426*f18be576SLuigi Rizzo kring[i].nkr_ft = ft; 427*f18be576SLuigi Rizzo } 428*f18be576SLuigi Rizzo if (nma_is_hw(na)) 429*f18be576SLuigi Rizzo nm_alloc_bdgfwd(SWNA(na->ifp)); 430*f18be576SLuigi Rizzo return 0; 431*f18be576SLuigi Rizzo } 432*f18be576SLuigi Rizzo 433f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */ 4345819da83SLuigi Rizzo 435ae10d1afSLuigi Rizzo 436ae10d1afSLuigi Rizzo /* 437ae10d1afSLuigi Rizzo * Fetch configuration from the device, to cope with dynamic 438ae10d1afSLuigi Rizzo * reconfigurations after loading the module. 439ae10d1afSLuigi Rizzo */ 440ae10d1afSLuigi Rizzo static int 441ae10d1afSLuigi Rizzo netmap_update_config(struct netmap_adapter *na) 442ae10d1afSLuigi Rizzo { 443ae10d1afSLuigi Rizzo struct ifnet *ifp = na->ifp; 444ae10d1afSLuigi Rizzo u_int txr, txd, rxr, rxd; 445ae10d1afSLuigi Rizzo 446ae10d1afSLuigi Rizzo txr = txd = rxr = rxd = 0; 447ae10d1afSLuigi Rizzo if (na->nm_config) { 448ae10d1afSLuigi Rizzo na->nm_config(ifp, &txr, &txd, &rxr, &rxd); 449ae10d1afSLuigi Rizzo } else { 450ae10d1afSLuigi Rizzo /* take whatever we had at init time */ 451ae10d1afSLuigi Rizzo txr = na->num_tx_rings; 452ae10d1afSLuigi Rizzo txd = na->num_tx_desc; 453ae10d1afSLuigi Rizzo rxr = na->num_rx_rings; 454ae10d1afSLuigi Rizzo rxd = na->num_rx_desc; 455ae10d1afSLuigi Rizzo } 456ae10d1afSLuigi Rizzo 457ae10d1afSLuigi Rizzo if (na->num_tx_rings == txr && na->num_tx_desc == txd && 458ae10d1afSLuigi Rizzo na->num_rx_rings == rxr && na->num_rx_desc == rxd) 459ae10d1afSLuigi Rizzo return 0; /* nothing changed */ 460ae10d1afSLuigi Rizzo if (netmap_verbose || na->refcount > 0) { 461ae10d1afSLuigi Rizzo D("stored config %s: txring %d x %d, rxring %d x %d", 462ae10d1afSLuigi Rizzo ifp->if_xname, 463ae10d1afSLuigi Rizzo na->num_tx_rings, na->num_tx_desc, 464ae10d1afSLuigi Rizzo na->num_rx_rings, na->num_rx_desc); 465ae10d1afSLuigi Rizzo D("new config %s: txring %d x %d, rxring %d x %d", 466ae10d1afSLuigi Rizzo ifp->if_xname, txr, txd, rxr, rxd); 467ae10d1afSLuigi Rizzo } 468ae10d1afSLuigi Rizzo if (na->refcount == 0) { 469ae10d1afSLuigi Rizzo D("configuration changed (but fine)"); 470ae10d1afSLuigi Rizzo na->num_tx_rings = txr; 471ae10d1afSLuigi Rizzo na->num_tx_desc = txd; 472ae10d1afSLuigi Rizzo na->num_rx_rings = rxr; 473ae10d1afSLuigi Rizzo na->num_rx_desc = rxd; 474ae10d1afSLuigi Rizzo return 0; 475ae10d1afSLuigi Rizzo } 476ae10d1afSLuigi Rizzo D("configuration changed while active, this is bad..."); 477ae10d1afSLuigi Rizzo return 1; 478ae10d1afSLuigi Rizzo } 479ae10d1afSLuigi Rizzo 4803c0caf6cSLuigi Rizzo /*------------- memory allocator -----------------*/ 4813c0caf6cSLuigi Rizzo #include "netmap_mem2.c" 4823c0caf6cSLuigi Rizzo /*------------ end of memory allocator ----------*/ 48368b8534bSLuigi Rizzo 4848241616dSLuigi Rizzo 4858241616dSLuigi Rizzo /* Structure associated to each thread which registered an interface. 4868241616dSLuigi Rizzo * 4878241616dSLuigi Rizzo * The first 4 fields of this structure are written by NIOCREGIF and 4888241616dSLuigi Rizzo * read by poll() and NIOC?XSYNC. 4898241616dSLuigi Rizzo * There is low contention among writers (actually, a correct user program 4908241616dSLuigi Rizzo * should have no contention among writers) and among writers and readers, 4918241616dSLuigi Rizzo * so we use a single global lock to protect the structure initialization. 4928241616dSLuigi Rizzo * Since initialization involves the allocation of memory, we reuse the memory 4938241616dSLuigi Rizzo * allocator lock. 4948241616dSLuigi Rizzo * Read access to the structure is lock free. Readers must check that 4958241616dSLuigi Rizzo * np_nifp is not NULL before using the other fields. 4968241616dSLuigi Rizzo * If np_nifp is NULL initialization has not been performed, so they should 4978241616dSLuigi Rizzo * return an error to userlevel. 4988241616dSLuigi Rizzo * 4998241616dSLuigi Rizzo * The ref_done field is used to regulate access to the refcount in the 5008241616dSLuigi Rizzo * memory allocator. The refcount must be incremented at most once for 5018241616dSLuigi Rizzo * each open("/dev/netmap"). The increment is performed by the first 5028241616dSLuigi Rizzo * function that calls netmap_get_memory() (currently called by 5038241616dSLuigi Rizzo * mmap(), NIOCGINFO and NIOCREGIF). 5048241616dSLuigi Rizzo * If the refcount is incremented, it is then decremented when the 5058241616dSLuigi Rizzo * private structure is destroyed. 5068241616dSLuigi Rizzo */ 50768b8534bSLuigi Rizzo struct netmap_priv_d { 5088241616dSLuigi Rizzo struct netmap_if * volatile np_nifp; /* netmap interface descriptor. */ 50968b8534bSLuigi Rizzo 51068b8534bSLuigi Rizzo struct ifnet *np_ifp; /* device for which we hold a reference */ 51168b8534bSLuigi Rizzo int np_ringid; /* from the ioctl */ 51268b8534bSLuigi Rizzo u_int np_qfirst, np_qlast; /* range of rings to scan */ 51368b8534bSLuigi Rizzo uint16_t np_txpoll; 5148241616dSLuigi Rizzo 5158241616dSLuigi Rizzo unsigned long ref_done; /* use with NMA_LOCK held */ 51668b8534bSLuigi Rizzo }; 51768b8534bSLuigi Rizzo 51868b8534bSLuigi Rizzo 5198241616dSLuigi Rizzo static int 5208241616dSLuigi Rizzo netmap_get_memory(struct netmap_priv_d* p) 5218241616dSLuigi Rizzo { 5228241616dSLuigi Rizzo int error = 0; 5238241616dSLuigi Rizzo NMA_LOCK(); 5248241616dSLuigi Rizzo if (!p->ref_done) { 5258241616dSLuigi Rizzo error = netmap_memory_finalize(); 5268241616dSLuigi Rizzo if (!error) 5278241616dSLuigi Rizzo p->ref_done = 1; 5288241616dSLuigi Rizzo } 5298241616dSLuigi Rizzo NMA_UNLOCK(); 5308241616dSLuigi Rizzo return error; 5318241616dSLuigi Rizzo } 5328241616dSLuigi Rizzo 53368b8534bSLuigi Rizzo /* 53468b8534bSLuigi Rizzo * File descriptor's private data destructor. 53568b8534bSLuigi Rizzo * 53668b8534bSLuigi Rizzo * Call nm_register(ifp,0) to stop netmap mode on the interface and 53768b8534bSLuigi Rizzo * revert to normal operation. We expect that np_ifp has not gone. 53868b8534bSLuigi Rizzo */ 5398241616dSLuigi Rizzo /* call with NMA_LOCK held */ 54068b8534bSLuigi Rizzo static void 5415819da83SLuigi Rizzo netmap_dtor_locked(void *data) 54268b8534bSLuigi Rizzo { 54368b8534bSLuigi Rizzo struct netmap_priv_d *priv = data; 54468b8534bSLuigi Rizzo struct ifnet *ifp = priv->np_ifp; 54568b8534bSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 54668b8534bSLuigi Rizzo struct netmap_if *nifp = priv->np_nifp; 54768b8534bSLuigi Rizzo 54868b8534bSLuigi Rizzo na->refcount--; 54968b8534bSLuigi Rizzo if (na->refcount <= 0) { /* last instance */ 55064ae02c3SLuigi Rizzo u_int i, j, lim; 55168b8534bSLuigi Rizzo 552ae10d1afSLuigi Rizzo if (netmap_verbose) 553ae10d1afSLuigi Rizzo D("deleting last instance for %s", ifp->if_xname); 55468b8534bSLuigi Rizzo /* 555*f18be576SLuigi Rizzo * (TO CHECK) This function is only called 556*f18be576SLuigi Rizzo * when the last reference to this file descriptor goes 557*f18be576SLuigi Rizzo * away. This means we cannot have any pending poll() 558*f18be576SLuigi Rizzo * or interrupt routine operating on the structure. 55968b8534bSLuigi Rizzo */ 56068b8534bSLuigi Rizzo na->nm_register(ifp, 0); /* off, clear IFCAP_NETMAP */ 56168b8534bSLuigi Rizzo /* Wake up any sleeping threads. netmap_poll will 56268b8534bSLuigi Rizzo * then return POLLERR 56368b8534bSLuigi Rizzo */ 564d76bf4ffSLuigi Rizzo for (i = 0; i < na->num_tx_rings + 1; i++) 56568b8534bSLuigi Rizzo selwakeuppri(&na->tx_rings[i].si, PI_NET); 566d76bf4ffSLuigi Rizzo for (i = 0; i < na->num_rx_rings + 1; i++) 56768b8534bSLuigi Rizzo selwakeuppri(&na->rx_rings[i].si, PI_NET); 56864ae02c3SLuigi Rizzo selwakeuppri(&na->tx_si, PI_NET); 56964ae02c3SLuigi Rizzo selwakeuppri(&na->rx_si, PI_NET); 570*f18be576SLuigi Rizzo #ifdef NM_BRIDGE 571*f18be576SLuigi Rizzo nm_free_bdgfwd(na); 572*f18be576SLuigi Rizzo #endif /* NM_BRIDGE */ 57368b8534bSLuigi Rizzo /* release all buffers */ 574d76bf4ffSLuigi Rizzo for (i = 0; i < na->num_tx_rings + 1; i++) { 57564ae02c3SLuigi Rizzo struct netmap_ring *ring = na->tx_rings[i].ring; 57668b8534bSLuigi Rizzo lim = na->tx_rings[i].nkr_num_slots; 57768b8534bSLuigi Rizzo for (j = 0; j < lim; j++) 578446ee301SLuigi Rizzo netmap_free_buf(nifp, ring->slot[j].buf_idx); 5792f70fca5SEd Maste /* knlist_destroy(&na->tx_rings[i].si.si_note); */ 5802f70fca5SEd Maste mtx_destroy(&na->tx_rings[i].q_lock); 58164ae02c3SLuigi Rizzo } 582d76bf4ffSLuigi Rizzo for (i = 0; i < na->num_rx_rings + 1; i++) { 58364ae02c3SLuigi Rizzo struct netmap_ring *ring = na->rx_rings[i].ring; 58468b8534bSLuigi Rizzo lim = na->rx_rings[i].nkr_num_slots; 58568b8534bSLuigi Rizzo for (j = 0; j < lim; j++) 586446ee301SLuigi Rizzo netmap_free_buf(nifp, ring->slot[j].buf_idx); 5872f70fca5SEd Maste /* knlist_destroy(&na->rx_rings[i].si.si_note); */ 5882f70fca5SEd Maste mtx_destroy(&na->rx_rings[i].q_lock); 58968b8534bSLuigi Rizzo } 5902f70fca5SEd Maste /* XXX kqueue(9) needed; these will mirror knlist_init. */ 5912f70fca5SEd Maste /* knlist_destroy(&na->tx_si.si_note); */ 5922f70fca5SEd Maste /* knlist_destroy(&na->rx_si.si_note); */ 5936e10c8b8SLuigi Rizzo netmap_free_rings(na); 594*f18be576SLuigi Rizzo if (nma_is_hw(na)) 595*f18be576SLuigi Rizzo SWNA(ifp)->tx_rings = SWNA(ifp)->rx_rings = NULL; 59668b8534bSLuigi Rizzo } 5976e10c8b8SLuigi Rizzo netmap_if_free(nifp); 5985819da83SLuigi Rizzo } 59968b8534bSLuigi Rizzo 600*f18be576SLuigi Rizzo 601*f18be576SLuigi Rizzo /* we assume netmap adapter exists */ 602f196ce38SLuigi Rizzo static void 603f196ce38SLuigi Rizzo nm_if_rele(struct ifnet *ifp) 604f196ce38SLuigi Rizzo { 605f196ce38SLuigi Rizzo #ifndef NM_BRIDGE 606f196ce38SLuigi Rizzo if_rele(ifp); 607f196ce38SLuigi Rizzo #else /* NM_BRIDGE */ 608*f18be576SLuigi Rizzo int i, full = 0, is_hw; 609f196ce38SLuigi Rizzo struct nm_bridge *b; 610*f18be576SLuigi Rizzo struct netmap_adapter *na; 611f196ce38SLuigi Rizzo 612*f18be576SLuigi Rizzo /* I can be called not only for get_ifp()-ed references where netmap's 613*f18be576SLuigi Rizzo * capability is guaranteed, but also for non-netmap-capable NICs. 614*f18be576SLuigi Rizzo */ 615*f18be576SLuigi Rizzo if (!NETMAP_CAPABLE(ifp) || !NA(ifp)->na_bdg) { 616f196ce38SLuigi Rizzo if_rele(ifp); 617f196ce38SLuigi Rizzo return; 618f196ce38SLuigi Rizzo } 619f196ce38SLuigi Rizzo if (!DROP_BDG_REF(ifp)) 620f196ce38SLuigi Rizzo return; 621*f18be576SLuigi Rizzo 622*f18be576SLuigi Rizzo na = NA(ifp); 623*f18be576SLuigi Rizzo b = na->na_bdg; 624*f18be576SLuigi Rizzo is_hw = nma_is_hw(na); 625*f18be576SLuigi Rizzo 626*f18be576SLuigi Rizzo BDG_WLOCK(b); 627f196ce38SLuigi Rizzo ND("want to disconnect %s from the bridge", ifp->if_xname); 628f196ce38SLuigi Rizzo full = 0; 629*f18be576SLuigi Rizzo /* remove the entry from the bridge, also check 630*f18be576SLuigi Rizzo * if there are any leftover interfaces 631*f18be576SLuigi Rizzo * XXX we should optimize this code, e.g. going directly 632*f18be576SLuigi Rizzo * to na->bdg_port, and having a counter of ports that 633*f18be576SLuigi Rizzo * are connected. But it is not in a critical path. 634*f18be576SLuigi Rizzo * In NIC's case, index of sw na is always higher than hw na 635*f18be576SLuigi Rizzo */ 636f196ce38SLuigi Rizzo for (i = 0; i < NM_BDG_MAXPORTS; i++) { 637*f18be576SLuigi Rizzo struct netmap_adapter *tmp = BDG_GET_VAR(b->bdg_ports[i]); 638*f18be576SLuigi Rizzo 639*f18be576SLuigi Rizzo if (tmp == na) { 640*f18be576SLuigi Rizzo /* disconnect from bridge */ 641*f18be576SLuigi Rizzo BDG_SET_VAR(b->bdg_ports[i], NULL); 642*f18be576SLuigi Rizzo na->na_bdg = NULL; 643*f18be576SLuigi Rizzo if (is_hw && SWNA(ifp)->na_bdg) { 644*f18be576SLuigi Rizzo /* disconnect sw adapter too */ 645*f18be576SLuigi Rizzo int j = SWNA(ifp)->bdg_port; 646*f18be576SLuigi Rizzo BDG_SET_VAR(b->bdg_ports[j], NULL); 647*f18be576SLuigi Rizzo SWNA(ifp)->na_bdg = NULL; 648f196ce38SLuigi Rizzo } 649*f18be576SLuigi Rizzo } else if (tmp != NULL) { 650f196ce38SLuigi Rizzo full = 1; 651f196ce38SLuigi Rizzo } 652f196ce38SLuigi Rizzo } 653*f18be576SLuigi Rizzo BDG_WUNLOCK(b); 654*f18be576SLuigi Rizzo if (full == 0) { 655*f18be576SLuigi Rizzo ND("marking bridge %d as free", b - nm_bridges); 656*f18be576SLuigi Rizzo b->namelen = 0; 657*f18be576SLuigi Rizzo b->nm_bdg_lookup = NULL; 658*f18be576SLuigi Rizzo } 659*f18be576SLuigi Rizzo if (na->na_bdg) { /* still attached to the bridge */ 660f196ce38SLuigi Rizzo D("ouch, cannot find ifp to remove"); 661*f18be576SLuigi Rizzo } else if (is_hw) { 662*f18be576SLuigi Rizzo if_rele(ifp); 663*f18be576SLuigi Rizzo } else { 664*f18be576SLuigi Rizzo bzero(na, sizeof(*na)); 665*f18be576SLuigi Rizzo free(na, M_DEVBUF); 666*f18be576SLuigi Rizzo bzero(ifp, sizeof(*ifp)); 667*f18be576SLuigi Rizzo free(ifp, M_DEVBUF); 668*f18be576SLuigi Rizzo } 669f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */ 670f196ce38SLuigi Rizzo } 6715819da83SLuigi Rizzo 6725819da83SLuigi Rizzo static void 6735819da83SLuigi Rizzo netmap_dtor(void *data) 6745819da83SLuigi Rizzo { 6755819da83SLuigi Rizzo struct netmap_priv_d *priv = data; 6765819da83SLuigi Rizzo struct ifnet *ifp = priv->np_ifp; 6775819da83SLuigi Rizzo 6788241616dSLuigi Rizzo NMA_LOCK(); 6798241616dSLuigi Rizzo if (ifp) { 6802579e2d7SLuigi Rizzo struct netmap_adapter *na = NA(ifp); 6812579e2d7SLuigi Rizzo 682*f18be576SLuigi Rizzo if (na->na_bdg) 683*f18be576SLuigi Rizzo BDG_WLOCK(na->na_bdg); 6841a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_REG_LOCK, 0); 6855819da83SLuigi Rizzo netmap_dtor_locked(data); 6861a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0); 687*f18be576SLuigi Rizzo if (na->na_bdg) 688*f18be576SLuigi Rizzo BDG_WUNLOCK(na->na_bdg); 68968b8534bSLuigi Rizzo 6902579e2d7SLuigi Rizzo nm_if_rele(ifp); /* might also destroy *na */ 6918241616dSLuigi Rizzo } 6928241616dSLuigi Rizzo if (priv->ref_done) { 6938241616dSLuigi Rizzo netmap_memory_deref(); 6948241616dSLuigi Rizzo } 6958241616dSLuigi Rizzo NMA_UNLOCK(); 69668b8534bSLuigi Rizzo bzero(priv, sizeof(*priv)); /* XXX for safety */ 69768b8534bSLuigi Rizzo free(priv, M_DEVBUF); 69868b8534bSLuigi Rizzo } 69968b8534bSLuigi Rizzo 700*f18be576SLuigi Rizzo 7018241616dSLuigi Rizzo #ifdef __FreeBSD__ 7028241616dSLuigi Rizzo #include <vm/vm.h> 7038241616dSLuigi Rizzo #include <vm/vm_param.h> 7048241616dSLuigi Rizzo #include <vm/vm_object.h> 7058241616dSLuigi Rizzo #include <vm/vm_page.h> 7068241616dSLuigi Rizzo #include <vm/vm_pager.h> 7078241616dSLuigi Rizzo #include <vm/uma.h> 7088241616dSLuigi Rizzo 709*f18be576SLuigi Rizzo /* 710*f18be576SLuigi Rizzo * In order to track whether pages are still mapped, we hook into 711*f18be576SLuigi Rizzo * the standard cdev_pager and intercept the constructor and 712*f18be576SLuigi Rizzo * destructor. 713*f18be576SLuigi Rizzo * XXX but then ? Do we really use the information ? 714*f18be576SLuigi Rizzo * Need to investigate. 715*f18be576SLuigi Rizzo */ 7168241616dSLuigi Rizzo static struct cdev_pager_ops saved_cdev_pager_ops; 7178241616dSLuigi Rizzo 718*f18be576SLuigi Rizzo 7198241616dSLuigi Rizzo static int 7208241616dSLuigi Rizzo netmap_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot, 7218241616dSLuigi Rizzo vm_ooffset_t foff, struct ucred *cred, u_short *color) 7228241616dSLuigi Rizzo { 723ae10d1afSLuigi Rizzo if (netmap_verbose) 7248241616dSLuigi Rizzo D("first mmap for %p", handle); 7258241616dSLuigi Rizzo return saved_cdev_pager_ops.cdev_pg_ctor(handle, 7268241616dSLuigi Rizzo size, prot, foff, cred, color); 7278241616dSLuigi Rizzo } 7288241616dSLuigi Rizzo 729*f18be576SLuigi Rizzo 7308241616dSLuigi Rizzo static void 7318241616dSLuigi Rizzo netmap_dev_pager_dtor(void *handle) 7328241616dSLuigi Rizzo { 7338241616dSLuigi Rizzo saved_cdev_pager_ops.cdev_pg_dtor(handle); 734ae10d1afSLuigi Rizzo ND("ready to release memory for %p", handle); 7358241616dSLuigi Rizzo } 7368241616dSLuigi Rizzo 7378241616dSLuigi Rizzo 7388241616dSLuigi Rizzo static struct cdev_pager_ops netmap_cdev_pager_ops = { 7398241616dSLuigi Rizzo .cdev_pg_ctor = netmap_dev_pager_ctor, 7408241616dSLuigi Rizzo .cdev_pg_dtor = netmap_dev_pager_dtor, 7418241616dSLuigi Rizzo .cdev_pg_fault = NULL, 7428241616dSLuigi Rizzo }; 7438241616dSLuigi Rizzo 744*f18be576SLuigi Rizzo 745*f18be576SLuigi Rizzo // XXX check whether we need netmap_mmap_single _and_ netmap_mmap 7468241616dSLuigi Rizzo static int 7478241616dSLuigi Rizzo netmap_mmap_single(struct cdev *cdev, vm_ooffset_t *foff, 7488241616dSLuigi Rizzo vm_size_t objsize, vm_object_t *objp, int prot) 7498241616dSLuigi Rizzo { 7508241616dSLuigi Rizzo vm_object_t obj; 7518241616dSLuigi Rizzo 752ae10d1afSLuigi Rizzo ND("cdev %p foff %jd size %jd objp %p prot %d", cdev, 75388f79057SGleb Smirnoff (intmax_t )*foff, (intmax_t )objsize, objp, prot); 7548241616dSLuigi Rizzo obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff, 7558241616dSLuigi Rizzo curthread->td_ucred); 7568241616dSLuigi Rizzo ND("returns obj %p", obj); 7578241616dSLuigi Rizzo if (obj == NULL) 7588241616dSLuigi Rizzo return EINVAL; 7598241616dSLuigi Rizzo if (saved_cdev_pager_ops.cdev_pg_fault == NULL) { 760ae10d1afSLuigi Rizzo ND("initialize cdev_pager_ops"); 7618241616dSLuigi Rizzo saved_cdev_pager_ops = *(obj->un_pager.devp.ops); 7628241616dSLuigi Rizzo netmap_cdev_pager_ops.cdev_pg_fault = 7638241616dSLuigi Rizzo saved_cdev_pager_ops.cdev_pg_fault; 7648241616dSLuigi Rizzo }; 7658241616dSLuigi Rizzo obj->un_pager.devp.ops = &netmap_cdev_pager_ops; 7668241616dSLuigi Rizzo *objp = obj; 7678241616dSLuigi Rizzo return 0; 7688241616dSLuigi Rizzo } 7698241616dSLuigi Rizzo #endif /* __FreeBSD__ */ 7708241616dSLuigi Rizzo 77168b8534bSLuigi Rizzo 77268b8534bSLuigi Rizzo /* 77368b8534bSLuigi Rizzo * mmap(2) support for the "netmap" device. 77468b8534bSLuigi Rizzo * 77568b8534bSLuigi Rizzo * Expose all the memory previously allocated by our custom memory 77668b8534bSLuigi Rizzo * allocator: this way the user has only to issue a single mmap(2), and 77768b8534bSLuigi Rizzo * can work on all the data structures flawlessly. 77868b8534bSLuigi Rizzo * 77968b8534bSLuigi Rizzo * Return 0 on success, -1 otherwise. 78068b8534bSLuigi Rizzo */ 781babc7c12SLuigi Rizzo 782f196ce38SLuigi Rizzo #ifdef __FreeBSD__ 78368b8534bSLuigi Rizzo static int 784babc7c12SLuigi Rizzo netmap_mmap(__unused struct cdev *dev, 78568b8534bSLuigi Rizzo #if __FreeBSD_version < 900000 786babc7c12SLuigi Rizzo vm_offset_t offset, vm_paddr_t *paddr, int nprot 78768b8534bSLuigi Rizzo #else 788babc7c12SLuigi Rizzo vm_ooffset_t offset, vm_paddr_t *paddr, int nprot, 789babc7c12SLuigi Rizzo __unused vm_memattr_t *memattr 79068b8534bSLuigi Rizzo #endif 791babc7c12SLuigi Rizzo ) 79268b8534bSLuigi Rizzo { 7938241616dSLuigi Rizzo int error = 0; 7948241616dSLuigi Rizzo struct netmap_priv_d *priv; 7958241616dSLuigi Rizzo 79668b8534bSLuigi Rizzo if (nprot & PROT_EXEC) 79768b8534bSLuigi Rizzo return (-1); // XXX -1 or EINVAL ? 798446ee301SLuigi Rizzo 7998241616dSLuigi Rizzo error = devfs_get_cdevpriv((void **)&priv); 8008241616dSLuigi Rizzo if (error == EBADF) { /* called on fault, memory is initialized */ 8018241616dSLuigi Rizzo ND(5, "handling fault at ofs 0x%x", offset); 8028241616dSLuigi Rizzo error = 0; 8038241616dSLuigi Rizzo } else if (error == 0) /* make sure memory is set */ 8048241616dSLuigi Rizzo error = netmap_get_memory(priv); 8058241616dSLuigi Rizzo if (error) 8068241616dSLuigi Rizzo return (error); 8078241616dSLuigi Rizzo 80868b8534bSLuigi Rizzo ND("request for offset 0x%x", (uint32_t)offset); 809446ee301SLuigi Rizzo *paddr = netmap_ofstophys(offset); 81068b8534bSLuigi Rizzo 8118241616dSLuigi Rizzo return (*paddr ? 0 : ENOMEM); 8128241616dSLuigi Rizzo } 8138241616dSLuigi Rizzo 814*f18be576SLuigi Rizzo 8158241616dSLuigi Rizzo static int 8168241616dSLuigi Rizzo netmap_close(struct cdev *dev, int fflag, int devtype, struct thread *td) 8178241616dSLuigi Rizzo { 818ae10d1afSLuigi Rizzo if (netmap_verbose) 819ae10d1afSLuigi Rizzo D("dev %p fflag 0x%x devtype %d td %p", 820ae10d1afSLuigi Rizzo dev, fflag, devtype, td); 8218241616dSLuigi Rizzo return 0; 8228241616dSLuigi Rizzo } 8238241616dSLuigi Rizzo 824*f18be576SLuigi Rizzo 8258241616dSLuigi Rizzo static int 8268241616dSLuigi Rizzo netmap_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 8278241616dSLuigi Rizzo { 8288241616dSLuigi Rizzo struct netmap_priv_d *priv; 8298241616dSLuigi Rizzo int error; 8308241616dSLuigi Rizzo 8318241616dSLuigi Rizzo priv = malloc(sizeof(struct netmap_priv_d), M_DEVBUF, 8328241616dSLuigi Rizzo M_NOWAIT | M_ZERO); 8338241616dSLuigi Rizzo if (priv == NULL) 8348241616dSLuigi Rizzo return ENOMEM; 8358241616dSLuigi Rizzo 8368241616dSLuigi Rizzo error = devfs_set_cdevpriv(priv, netmap_dtor); 8378241616dSLuigi Rizzo if (error) 8388241616dSLuigi Rizzo return error; 8398241616dSLuigi Rizzo 8408241616dSLuigi Rizzo return 0; 84168b8534bSLuigi Rizzo } 842f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */ 84368b8534bSLuigi Rizzo 84468b8534bSLuigi Rizzo 84568b8534bSLuigi Rizzo /* 84602ad4083SLuigi Rizzo * Handlers for synchronization of the queues from/to the host. 847091fd0abSLuigi Rizzo * Netmap has two operating modes: 848091fd0abSLuigi Rizzo * - in the default mode, the rings connected to the host stack are 849091fd0abSLuigi Rizzo * just another ring pair managed by userspace; 850091fd0abSLuigi Rizzo * - in transparent mode (XXX to be defined) incoming packets 851091fd0abSLuigi Rizzo * (from the host or the NIC) are marked as NS_FORWARD upon 852091fd0abSLuigi Rizzo * arrival, and the user application has a chance to reset the 853091fd0abSLuigi Rizzo * flag for packets that should be dropped. 854091fd0abSLuigi Rizzo * On the RXSYNC or poll(), packets in RX rings between 855091fd0abSLuigi Rizzo * kring->nr_kcur and ring->cur with NS_FORWARD still set are moved 856091fd0abSLuigi Rizzo * to the other side. 857091fd0abSLuigi Rizzo * The transfer NIC --> host is relatively easy, just encapsulate 858091fd0abSLuigi Rizzo * into mbufs and we are done. The host --> NIC side is slightly 859091fd0abSLuigi Rizzo * harder because there might not be room in the tx ring so it 860091fd0abSLuigi Rizzo * might take a while before releasing the buffer. 861091fd0abSLuigi Rizzo */ 862091fd0abSLuigi Rizzo 863*f18be576SLuigi Rizzo 864091fd0abSLuigi Rizzo /* 865091fd0abSLuigi Rizzo * pass a chain of buffers to the host stack as coming from 'dst' 866091fd0abSLuigi Rizzo */ 867091fd0abSLuigi Rizzo static void 868091fd0abSLuigi Rizzo netmap_send_up(struct ifnet *dst, struct mbuf *head) 869091fd0abSLuigi Rizzo { 870091fd0abSLuigi Rizzo struct mbuf *m; 871091fd0abSLuigi Rizzo 872091fd0abSLuigi Rizzo /* send packets up, outside the lock */ 873091fd0abSLuigi Rizzo while ((m = head) != NULL) { 874091fd0abSLuigi Rizzo head = head->m_nextpkt; 875091fd0abSLuigi Rizzo m->m_nextpkt = NULL; 876091fd0abSLuigi Rizzo if (netmap_verbose & NM_VERB_HOST) 877091fd0abSLuigi Rizzo D("sending up pkt %p size %d", m, MBUF_LEN(m)); 878091fd0abSLuigi Rizzo NM_SEND_UP(dst, m); 879091fd0abSLuigi Rizzo } 880091fd0abSLuigi Rizzo } 881091fd0abSLuigi Rizzo 882091fd0abSLuigi Rizzo struct mbq { 883091fd0abSLuigi Rizzo struct mbuf *head; 884091fd0abSLuigi Rizzo struct mbuf *tail; 885091fd0abSLuigi Rizzo int count; 886091fd0abSLuigi Rizzo }; 887091fd0abSLuigi Rizzo 888*f18be576SLuigi Rizzo 889091fd0abSLuigi Rizzo /* 890091fd0abSLuigi Rizzo * put a copy of the buffers marked NS_FORWARD into an mbuf chain. 891091fd0abSLuigi Rizzo * Run from hwcur to cur - reserved 892091fd0abSLuigi Rizzo */ 893091fd0abSLuigi Rizzo static void 894091fd0abSLuigi Rizzo netmap_grab_packets(struct netmap_kring *kring, struct mbq *q, int force) 895091fd0abSLuigi Rizzo { 896091fd0abSLuigi Rizzo /* Take packets from hwcur to cur-reserved and pass them up. 897091fd0abSLuigi Rizzo * In case of no buffers we give up. At the end of the loop, 898091fd0abSLuigi Rizzo * the queue is drained in all cases. 899091fd0abSLuigi Rizzo * XXX handle reserved 900091fd0abSLuigi Rizzo */ 901091fd0abSLuigi Rizzo int k = kring->ring->cur - kring->ring->reserved; 902091fd0abSLuigi Rizzo u_int n, lim = kring->nkr_num_slots - 1; 903091fd0abSLuigi Rizzo struct mbuf *m, *tail = q->tail; 904091fd0abSLuigi Rizzo 905091fd0abSLuigi Rizzo if (k < 0) 906091fd0abSLuigi Rizzo k = k + kring->nkr_num_slots; 907091fd0abSLuigi Rizzo for (n = kring->nr_hwcur; n != k;) { 908091fd0abSLuigi Rizzo struct netmap_slot *slot = &kring->ring->slot[n]; 909091fd0abSLuigi Rizzo 910091fd0abSLuigi Rizzo n = (n == lim) ? 0 : n + 1; 911091fd0abSLuigi Rizzo if ((slot->flags & NS_FORWARD) == 0 && !force) 912091fd0abSLuigi Rizzo continue; 913091fd0abSLuigi Rizzo if (slot->len < 14 || slot->len > NETMAP_BUF_SIZE) { 914091fd0abSLuigi Rizzo D("bad pkt at %d len %d", n, slot->len); 915091fd0abSLuigi Rizzo continue; 916091fd0abSLuigi Rizzo } 917091fd0abSLuigi Rizzo slot->flags &= ~NS_FORWARD; // XXX needed ? 918091fd0abSLuigi Rizzo m = m_devget(NMB(slot), slot->len, 0, kring->na->ifp, NULL); 919091fd0abSLuigi Rizzo 920091fd0abSLuigi Rizzo if (m == NULL) 921091fd0abSLuigi Rizzo break; 922091fd0abSLuigi Rizzo if (tail) 923091fd0abSLuigi Rizzo tail->m_nextpkt = m; 924091fd0abSLuigi Rizzo else 925091fd0abSLuigi Rizzo q->head = m; 926091fd0abSLuigi Rizzo tail = m; 927091fd0abSLuigi Rizzo q->count++; 928091fd0abSLuigi Rizzo m->m_nextpkt = NULL; 929091fd0abSLuigi Rizzo } 930091fd0abSLuigi Rizzo q->tail = tail; 931091fd0abSLuigi Rizzo } 932091fd0abSLuigi Rizzo 933*f18be576SLuigi Rizzo 934091fd0abSLuigi Rizzo /* 935091fd0abSLuigi Rizzo * called under main lock to send packets from the host to the NIC 936091fd0abSLuigi Rizzo * The host ring has packets from nr_hwcur to (cur - reserved) 937091fd0abSLuigi Rizzo * to be sent down. We scan the tx rings, which have just been 938091fd0abSLuigi Rizzo * flushed so nr_hwcur == cur. Pushing packets down means 939091fd0abSLuigi Rizzo * increment cur and decrement avail. 940091fd0abSLuigi Rizzo * XXX to be verified 941091fd0abSLuigi Rizzo */ 942091fd0abSLuigi Rizzo static void 943091fd0abSLuigi Rizzo netmap_sw_to_nic(struct netmap_adapter *na) 944091fd0abSLuigi Rizzo { 945091fd0abSLuigi Rizzo struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings]; 946091fd0abSLuigi Rizzo struct netmap_kring *k1 = &na->tx_rings[0]; 947091fd0abSLuigi Rizzo int i, howmany, src_lim, dst_lim; 948091fd0abSLuigi Rizzo 949091fd0abSLuigi Rizzo howmany = kring->nr_hwavail; /* XXX otherwise cur - reserved - nr_hwcur */ 950091fd0abSLuigi Rizzo 951091fd0abSLuigi Rizzo src_lim = kring->nkr_num_slots; 952091fd0abSLuigi Rizzo for (i = 0; howmany > 0 && i < na->num_tx_rings; i++, k1++) { 953091fd0abSLuigi Rizzo ND("%d packets left to ring %d (space %d)", howmany, i, k1->nr_hwavail); 954091fd0abSLuigi Rizzo dst_lim = k1->nkr_num_slots; 955091fd0abSLuigi Rizzo while (howmany > 0 && k1->ring->avail > 0) { 956091fd0abSLuigi Rizzo struct netmap_slot *src, *dst, tmp; 957091fd0abSLuigi Rizzo src = &kring->ring->slot[kring->nr_hwcur]; 958091fd0abSLuigi Rizzo dst = &k1->ring->slot[k1->ring->cur]; 959091fd0abSLuigi Rizzo tmp = *src; 960091fd0abSLuigi Rizzo src->buf_idx = dst->buf_idx; 961091fd0abSLuigi Rizzo src->flags = NS_BUF_CHANGED; 962091fd0abSLuigi Rizzo 963091fd0abSLuigi Rizzo dst->buf_idx = tmp.buf_idx; 964091fd0abSLuigi Rizzo dst->len = tmp.len; 965091fd0abSLuigi Rizzo dst->flags = NS_BUF_CHANGED; 966091fd0abSLuigi Rizzo ND("out len %d buf %d from %d to %d", 967091fd0abSLuigi Rizzo dst->len, dst->buf_idx, 968091fd0abSLuigi Rizzo kring->nr_hwcur, k1->ring->cur); 969091fd0abSLuigi Rizzo 970091fd0abSLuigi Rizzo if (++kring->nr_hwcur >= src_lim) 971091fd0abSLuigi Rizzo kring->nr_hwcur = 0; 972091fd0abSLuigi Rizzo howmany--; 973091fd0abSLuigi Rizzo kring->nr_hwavail--; 974091fd0abSLuigi Rizzo if (++k1->ring->cur >= dst_lim) 975091fd0abSLuigi Rizzo k1->ring->cur = 0; 976091fd0abSLuigi Rizzo k1->ring->avail--; 977091fd0abSLuigi Rizzo } 978091fd0abSLuigi Rizzo kring->ring->cur = kring->nr_hwcur; // XXX 979091fd0abSLuigi Rizzo k1++; 980091fd0abSLuigi Rizzo } 981091fd0abSLuigi Rizzo } 982091fd0abSLuigi Rizzo 983*f18be576SLuigi Rizzo 984091fd0abSLuigi Rizzo /* 98502ad4083SLuigi Rizzo * netmap_sync_to_host() passes packets up. We are called from a 98602ad4083SLuigi Rizzo * system call in user process context, and the only contention 98702ad4083SLuigi Rizzo * can be among multiple user threads erroneously calling 988091fd0abSLuigi Rizzo * this routine concurrently. 98968b8534bSLuigi Rizzo */ 99068b8534bSLuigi Rizzo static void 99168b8534bSLuigi Rizzo netmap_sync_to_host(struct netmap_adapter *na) 99268b8534bSLuigi Rizzo { 993d76bf4ffSLuigi Rizzo struct netmap_kring *kring = &na->tx_rings[na->num_tx_rings]; 99468b8534bSLuigi Rizzo struct netmap_ring *ring = kring->ring; 995091fd0abSLuigi Rizzo u_int k, lim = kring->nkr_num_slots - 1; 996091fd0abSLuigi Rizzo struct mbq q = { NULL, NULL }; 99768b8534bSLuigi Rizzo 99802ad4083SLuigi Rizzo k = ring->cur; 99902ad4083SLuigi Rizzo if (k > lim) { 100002ad4083SLuigi Rizzo netmap_ring_reinit(kring); 100102ad4083SLuigi Rizzo return; 100202ad4083SLuigi Rizzo } 10031a26580eSLuigi Rizzo // na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0); 100468b8534bSLuigi Rizzo 100568b8534bSLuigi Rizzo /* Take packets from hwcur to cur and pass them up. 100668b8534bSLuigi Rizzo * In case of no buffers we give up. At the end of the loop, 100768b8534bSLuigi Rizzo * the queue is drained in all cases. 100868b8534bSLuigi Rizzo */ 1009091fd0abSLuigi Rizzo netmap_grab_packets(kring, &q, 1); 101002ad4083SLuigi Rizzo kring->nr_hwcur = k; 101168b8534bSLuigi Rizzo kring->nr_hwavail = ring->avail = lim; 10121a26580eSLuigi Rizzo // na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0); 101368b8534bSLuigi Rizzo 1014091fd0abSLuigi Rizzo netmap_send_up(na->ifp, q.head); 101568b8534bSLuigi Rizzo } 101668b8534bSLuigi Rizzo 1017*f18be576SLuigi Rizzo 1018*f18be576SLuigi Rizzo /* SWNA(ifp)->txrings[0] is always NA(ifp)->txrings[NA(ifp)->num_txrings] */ 1019*f18be576SLuigi Rizzo static int 1020*f18be576SLuigi Rizzo netmap_bdg_to_host(struct ifnet *ifp, u_int ring_nr, int do_lock) 1021*f18be576SLuigi Rizzo { 1022*f18be576SLuigi Rizzo (void)ring_nr; 1023*f18be576SLuigi Rizzo (void)do_lock; 1024*f18be576SLuigi Rizzo netmap_sync_to_host(NA(ifp)); 1025*f18be576SLuigi Rizzo return 0; 1026*f18be576SLuigi Rizzo } 1027*f18be576SLuigi Rizzo 1028*f18be576SLuigi Rizzo 102968b8534bSLuigi Rizzo /* 103002ad4083SLuigi Rizzo * rxsync backend for packets coming from the host stack. 103102ad4083SLuigi Rizzo * They have been put in the queue by netmap_start() so we 103202ad4083SLuigi Rizzo * need to protect access to the kring using a lock. 103302ad4083SLuigi Rizzo * 103468b8534bSLuigi Rizzo * This routine also does the selrecord if called from the poll handler 103568b8534bSLuigi Rizzo * (we know because td != NULL). 103601c7d25fSLuigi Rizzo * 103701c7d25fSLuigi Rizzo * NOTE: on linux, selrecord() is defined as a macro and uses pwait 103801c7d25fSLuigi Rizzo * as an additional hidden argument. 103968b8534bSLuigi Rizzo */ 104068b8534bSLuigi Rizzo static void 104101c7d25fSLuigi Rizzo netmap_sync_from_host(struct netmap_adapter *na, struct thread *td, void *pwait) 104268b8534bSLuigi Rizzo { 1043d76bf4ffSLuigi Rizzo struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings]; 104468b8534bSLuigi Rizzo struct netmap_ring *ring = kring->ring; 104564ae02c3SLuigi Rizzo u_int j, n, lim = kring->nkr_num_slots; 104664ae02c3SLuigi Rizzo u_int k = ring->cur, resvd = ring->reserved; 104768b8534bSLuigi Rizzo 104801c7d25fSLuigi Rizzo (void)pwait; /* disable unused warnings */ 10491a26580eSLuigi Rizzo na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0); 105064ae02c3SLuigi Rizzo if (k >= lim) { 105164ae02c3SLuigi Rizzo netmap_ring_reinit(kring); 105264ae02c3SLuigi Rizzo return; 105364ae02c3SLuigi Rizzo } 105464ae02c3SLuigi Rizzo /* new packets are already set in nr_hwavail */ 105564ae02c3SLuigi Rizzo /* skip past packets that userspace has released */ 105664ae02c3SLuigi Rizzo j = kring->nr_hwcur; 105764ae02c3SLuigi Rizzo if (resvd > 0) { 105864ae02c3SLuigi Rizzo if (resvd + ring->avail >= lim + 1) { 105964ae02c3SLuigi Rizzo D("XXX invalid reserve/avail %d %d", resvd, ring->avail); 106064ae02c3SLuigi Rizzo ring->reserved = resvd = 0; // XXX panic... 106164ae02c3SLuigi Rizzo } 106264ae02c3SLuigi Rizzo k = (k >= resvd) ? k - resvd : k + lim - resvd; 106364ae02c3SLuigi Rizzo } 106464ae02c3SLuigi Rizzo if (j != k) { 106564ae02c3SLuigi Rizzo n = k >= j ? k - j : k + lim - j; 106664ae02c3SLuigi Rizzo kring->nr_hwavail -= n; 106702ad4083SLuigi Rizzo kring->nr_hwcur = k; 106864ae02c3SLuigi Rizzo } 106964ae02c3SLuigi Rizzo k = ring->avail = kring->nr_hwavail - resvd; 107002ad4083SLuigi Rizzo if (k == 0 && td) 107168b8534bSLuigi Rizzo selrecord(td, &kring->si); 107202ad4083SLuigi Rizzo if (k && (netmap_verbose & NM_VERB_HOST)) 107302ad4083SLuigi Rizzo D("%d pkts from stack", k); 10741a26580eSLuigi Rizzo na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0); 107568b8534bSLuigi Rizzo } 107668b8534bSLuigi Rizzo 107768b8534bSLuigi Rizzo 107868b8534bSLuigi Rizzo /* 107968b8534bSLuigi Rizzo * get a refcounted reference to an interface. 108068b8534bSLuigi Rizzo * Return ENXIO if the interface does not exist, EINVAL if netmap 108168b8534bSLuigi Rizzo * is not supported by the interface. 108268b8534bSLuigi Rizzo * If successful, hold a reference. 1083*f18be576SLuigi Rizzo * 1084*f18be576SLuigi Rizzo * During the NIC is attached to a bridge, reference is managed 1085*f18be576SLuigi Rizzo * at na->na_bdg_refcount using ADD/DROP_BDG_REF() as well as 1086*f18be576SLuigi Rizzo * virtual ports. Hence, on the final DROP_BDG_REF(), the NIC 1087*f18be576SLuigi Rizzo * is detached from the bridge, then ifp's refcount is dropped (this 1088*f18be576SLuigi Rizzo * is equivalent to that ifp is destroyed in case of virtual ports. 1089*f18be576SLuigi Rizzo * 1090*f18be576SLuigi Rizzo * This function uses if_rele() when we want to prevent the NIC from 1091*f18be576SLuigi Rizzo * being detached from the bridge in error handling. But once refcount 1092*f18be576SLuigi Rizzo * is acquired by this function, it must be released using nm_if_rele(). 109368b8534bSLuigi Rizzo */ 109468b8534bSLuigi Rizzo static int 1095*f18be576SLuigi Rizzo get_ifp(struct nmreq *nmr, struct ifnet **ifp) 109668b8534bSLuigi Rizzo { 1097*f18be576SLuigi Rizzo const char *name = nmr->nr_name; 1098*f18be576SLuigi Rizzo int namelen = strlen(name); 1099f196ce38SLuigi Rizzo #ifdef NM_BRIDGE 1100f196ce38SLuigi Rizzo struct ifnet *iter = NULL; 1101*f18be576SLuigi Rizzo int no_prefix = 0; 1102f196ce38SLuigi Rizzo 1103f196ce38SLuigi Rizzo do { 1104f196ce38SLuigi Rizzo struct nm_bridge *b; 1105*f18be576SLuigi Rizzo struct netmap_adapter *na; 1106*f18be576SLuigi Rizzo int i, cand = -1, cand2 = -1; 1107f196ce38SLuigi Rizzo 1108*f18be576SLuigi Rizzo if (strncmp(name, NM_NAME, sizeof(NM_NAME) - 1)) { 1109*f18be576SLuigi Rizzo no_prefix = 1; 1110f196ce38SLuigi Rizzo break; 1111*f18be576SLuigi Rizzo } 1112*f18be576SLuigi Rizzo b = nm_find_bridge(name, 1 /* create a new one if no exist */ ); 1113f196ce38SLuigi Rizzo if (b == NULL) { 1114f196ce38SLuigi Rizzo D("no bridges available for '%s'", name); 1115f196ce38SLuigi Rizzo return (ENXIO); 1116f196ce38SLuigi Rizzo } 1117*f18be576SLuigi Rizzo /* Now we are sure that name starts with the bridge's name */ 1118*f18be576SLuigi Rizzo BDG_WLOCK(b); 1119f196ce38SLuigi Rizzo /* lookup in the local list of ports */ 1120f196ce38SLuigi Rizzo for (i = 0; i < NM_BDG_MAXPORTS; i++) { 1121*f18be576SLuigi Rizzo na = BDG_GET_VAR(b->bdg_ports[i]); 1122*f18be576SLuigi Rizzo if (na == NULL) { 1123f196ce38SLuigi Rizzo if (cand == -1) 1124f196ce38SLuigi Rizzo cand = i; /* potential insert point */ 1125*f18be576SLuigi Rizzo else if (cand2 == -1) 1126*f18be576SLuigi Rizzo cand2 = i; /* for host stack */ 1127f196ce38SLuigi Rizzo continue; 1128f196ce38SLuigi Rizzo } 1129*f18be576SLuigi Rizzo iter = na->ifp; 1130*f18be576SLuigi Rizzo /* XXX make sure the name only contains one : */ 1131*f18be576SLuigi Rizzo if (!strcmp(iter->if_xname, name) /* virtual port */ || 1132*f18be576SLuigi Rizzo (namelen > b->namelen && !strcmp(iter->if_xname, 1133*f18be576SLuigi Rizzo name + b->namelen + 1)) /* NIC */) { 1134f196ce38SLuigi Rizzo ADD_BDG_REF(iter); 1135f196ce38SLuigi Rizzo ND("found existing interface"); 1136*f18be576SLuigi Rizzo BDG_WUNLOCK(b); 1137f196ce38SLuigi Rizzo break; 1138f196ce38SLuigi Rizzo } 1139f196ce38SLuigi Rizzo } 1140f196ce38SLuigi Rizzo if (i < NM_BDG_MAXPORTS) /* already unlocked */ 1141f196ce38SLuigi Rizzo break; 1142f196ce38SLuigi Rizzo if (cand == -1) { 1143f196ce38SLuigi Rizzo D("bridge full, cannot create new port"); 1144f196ce38SLuigi Rizzo no_port: 1145*f18be576SLuigi Rizzo BDG_WUNLOCK(b); 1146f196ce38SLuigi Rizzo *ifp = NULL; 1147f196ce38SLuigi Rizzo return EINVAL; 1148f196ce38SLuigi Rizzo } 1149f196ce38SLuigi Rizzo ND("create new bridge port %s", name); 1150*f18be576SLuigi Rizzo /* 1151*f18be576SLuigi Rizzo * create a struct ifnet for the new port. 1152*f18be576SLuigi Rizzo * The forwarding table is attached to the kring(s). 1153*f18be576SLuigi Rizzo */ 1154*f18be576SLuigi Rizzo /* 1155*f18be576SLuigi Rizzo * try see if there is a matching NIC with this name 1156*f18be576SLuigi Rizzo * (after the bridge's name) 1157*f18be576SLuigi Rizzo */ 1158*f18be576SLuigi Rizzo iter = ifunit_ref(name + b->namelen + 1); 1159*f18be576SLuigi Rizzo if (!iter) { /* this is a virtual port */ 1160*f18be576SLuigi Rizzo /* Create a temporary NA with arguments, then 1161*f18be576SLuigi Rizzo * bdg_netmap_attach() will allocate the real one 1162*f18be576SLuigi Rizzo * and attach it to the ifp 1163*f18be576SLuigi Rizzo */ 1164*f18be576SLuigi Rizzo struct netmap_adapter tmp_na; 1165*f18be576SLuigi Rizzo 1166*f18be576SLuigi Rizzo if (nmr->nr_cmd) /* nr_cmd must be for a NIC */ 1167*f18be576SLuigi Rizzo goto no_port; 1168*f18be576SLuigi Rizzo bzero(&tmp_na, sizeof(tmp_na)); 1169*f18be576SLuigi Rizzo /* bound checking */ 1170*f18be576SLuigi Rizzo if (nmr->nr_tx_rings < 1) 1171*f18be576SLuigi Rizzo nmr->nr_tx_rings = 1; 1172*f18be576SLuigi Rizzo if (nmr->nr_tx_rings > NM_BDG_MAXRINGS) 1173*f18be576SLuigi Rizzo nmr->nr_tx_rings = NM_BDG_MAXRINGS; 1174*f18be576SLuigi Rizzo tmp_na.num_tx_rings = nmr->nr_tx_rings; 1175*f18be576SLuigi Rizzo if (nmr->nr_rx_rings < 1) 1176*f18be576SLuigi Rizzo nmr->nr_rx_rings = 1; 1177*f18be576SLuigi Rizzo if (nmr->nr_rx_rings > NM_BDG_MAXRINGS) 1178*f18be576SLuigi Rizzo nmr->nr_rx_rings = NM_BDG_MAXRINGS; 1179*f18be576SLuigi Rizzo tmp_na.num_rx_rings = nmr->nr_rx_rings; 1180*f18be576SLuigi Rizzo 1181*f18be576SLuigi Rizzo iter = malloc(sizeof(*iter), M_DEVBUF, M_NOWAIT | M_ZERO); 1182f196ce38SLuigi Rizzo if (!iter) 1183f196ce38SLuigi Rizzo goto no_port; 1184f196ce38SLuigi Rizzo strcpy(iter->if_xname, name); 1185*f18be576SLuigi Rizzo tmp_na.ifp = iter; 1186*f18be576SLuigi Rizzo /* bdg_netmap_attach creates a struct netmap_adapter */ 1187*f18be576SLuigi Rizzo bdg_netmap_attach(&tmp_na); 1188*f18be576SLuigi Rizzo } else if (NETMAP_CAPABLE(iter)) { /* this is a NIC */ 1189*f18be576SLuigi Rizzo /* cannot attach the NIC that any user or another 1190*f18be576SLuigi Rizzo * bridge already holds. 1191*f18be576SLuigi Rizzo */ 1192*f18be576SLuigi Rizzo if (NETMAP_OWNED_BY_ANY(iter) || cand2 == -1) { 1193*f18be576SLuigi Rizzo ifunit_rele: 1194*f18be576SLuigi Rizzo if_rele(iter); /* don't detach from bridge */ 1195*f18be576SLuigi Rizzo goto no_port; 1196*f18be576SLuigi Rizzo } 1197*f18be576SLuigi Rizzo /* bind the host stack to the bridge */ 1198*f18be576SLuigi Rizzo if (nmr->nr_arg1 == NETMAP_BDG_HOST) { 1199*f18be576SLuigi Rizzo BDG_SET_VAR(b->bdg_ports[cand2], SWNA(iter)); 1200*f18be576SLuigi Rizzo SWNA(iter)->bdg_port = cand2; 1201*f18be576SLuigi Rizzo SWNA(iter)->na_bdg = b; 1202*f18be576SLuigi Rizzo } 1203*f18be576SLuigi Rizzo } else /* not a netmap-capable NIC */ 1204*f18be576SLuigi Rizzo goto ifunit_rele; 1205*f18be576SLuigi Rizzo na = NA(iter); 1206*f18be576SLuigi Rizzo na->bdg_port = cand; 1207*f18be576SLuigi Rizzo /* bind the port to the bridge (virtual ports are not active) */ 1208*f18be576SLuigi Rizzo BDG_SET_VAR(b->bdg_ports[cand], na); 1209*f18be576SLuigi Rizzo na->na_bdg = b; 1210f196ce38SLuigi Rizzo ADD_BDG_REF(iter); 1211*f18be576SLuigi Rizzo BDG_WUNLOCK(b); 1212f196ce38SLuigi Rizzo ND("attaching virtual bridge %p", b); 1213f196ce38SLuigi Rizzo } while (0); 1214f196ce38SLuigi Rizzo *ifp = iter; 1215f196ce38SLuigi Rizzo if (! *ifp) 1216f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */ 121768b8534bSLuigi Rizzo *ifp = ifunit_ref(name); 121868b8534bSLuigi Rizzo if (*ifp == NULL) 121968b8534bSLuigi Rizzo return (ENXIO); 122068b8534bSLuigi Rizzo /* can do this if the capability exists and if_pspare[0] 122168b8534bSLuigi Rizzo * points to the netmap descriptor. 122268b8534bSLuigi Rizzo */ 1223*f18be576SLuigi Rizzo if (NETMAP_CAPABLE(*ifp)) { 1224*f18be576SLuigi Rizzo #ifdef NM_BRIDGE 1225*f18be576SLuigi Rizzo /* Users cannot use the NIC attached to a bridge directly */ 1226*f18be576SLuigi Rizzo if (no_prefix && NETMAP_OWNED_BY_KERN(*ifp)) { 1227*f18be576SLuigi Rizzo if_rele(*ifp); /* don't detach from bridge */ 1228*f18be576SLuigi Rizzo return EINVAL; 1229*f18be576SLuigi Rizzo } else 1230*f18be576SLuigi Rizzo #endif /* NM_BRIDGE */ 123168b8534bSLuigi Rizzo return 0; /* valid pointer, we hold the refcount */ 1232*f18be576SLuigi Rizzo } 1233f196ce38SLuigi Rizzo nm_if_rele(*ifp); 123468b8534bSLuigi Rizzo return EINVAL; // not NETMAP capable 123568b8534bSLuigi Rizzo } 123668b8534bSLuigi Rizzo 123768b8534bSLuigi Rizzo 123868b8534bSLuigi Rizzo /* 123968b8534bSLuigi Rizzo * Error routine called when txsync/rxsync detects an error. 124068b8534bSLuigi Rizzo * Can't do much more than resetting cur = hwcur, avail = hwavail. 124168b8534bSLuigi Rizzo * Return 1 on reinit. 1242506cc70cSLuigi Rizzo * 1243506cc70cSLuigi Rizzo * This routine is only called by the upper half of the kernel. 1244506cc70cSLuigi Rizzo * It only reads hwcur (which is changed only by the upper half, too) 1245506cc70cSLuigi Rizzo * and hwavail (which may be changed by the lower half, but only on 1246506cc70cSLuigi Rizzo * a tx ring and only to increase it, so any error will be recovered 1247506cc70cSLuigi Rizzo * on the next call). For the above, we don't strictly need to call 1248506cc70cSLuigi Rizzo * it under lock. 124968b8534bSLuigi Rizzo */ 125068b8534bSLuigi Rizzo int 125168b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring) 125268b8534bSLuigi Rizzo { 125368b8534bSLuigi Rizzo struct netmap_ring *ring = kring->ring; 125468b8534bSLuigi Rizzo u_int i, lim = kring->nkr_num_slots - 1; 125568b8534bSLuigi Rizzo int errors = 0; 125668b8534bSLuigi Rizzo 12578241616dSLuigi Rizzo RD(10, "called for %s", kring->na->ifp->if_xname); 125868b8534bSLuigi Rizzo if (ring->cur > lim) 125968b8534bSLuigi Rizzo errors++; 126068b8534bSLuigi Rizzo for (i = 0; i <= lim; i++) { 126168b8534bSLuigi Rizzo u_int idx = ring->slot[i].buf_idx; 126268b8534bSLuigi Rizzo u_int len = ring->slot[i].len; 126368b8534bSLuigi Rizzo if (idx < 2 || idx >= netmap_total_buffers) { 126468b8534bSLuigi Rizzo if (!errors++) 126568b8534bSLuigi Rizzo D("bad buffer at slot %d idx %d len %d ", i, idx, len); 126668b8534bSLuigi Rizzo ring->slot[i].buf_idx = 0; 126768b8534bSLuigi Rizzo ring->slot[i].len = 0; 126868b8534bSLuigi Rizzo } else if (len > NETMAP_BUF_SIZE) { 126968b8534bSLuigi Rizzo ring->slot[i].len = 0; 127068b8534bSLuigi Rizzo if (!errors++) 127168b8534bSLuigi Rizzo D("bad len %d at slot %d idx %d", 127268b8534bSLuigi Rizzo len, i, idx); 127368b8534bSLuigi Rizzo } 127468b8534bSLuigi Rizzo } 127568b8534bSLuigi Rizzo if (errors) { 127668b8534bSLuigi Rizzo int pos = kring - kring->na->tx_rings; 1277d76bf4ffSLuigi Rizzo int n = kring->na->num_tx_rings + 1; 127868b8534bSLuigi Rizzo 12798241616dSLuigi Rizzo RD(10, "total %d errors", errors); 128068b8534bSLuigi Rizzo errors++; 12818241616dSLuigi Rizzo RD(10, "%s %s[%d] reinit, cur %d -> %d avail %d -> %d", 128268b8534bSLuigi Rizzo kring->na->ifp->if_xname, 128368b8534bSLuigi Rizzo pos < n ? "TX" : "RX", pos < n ? pos : pos - n, 128468b8534bSLuigi Rizzo ring->cur, kring->nr_hwcur, 128568b8534bSLuigi Rizzo ring->avail, kring->nr_hwavail); 128668b8534bSLuigi Rizzo ring->cur = kring->nr_hwcur; 128768b8534bSLuigi Rizzo ring->avail = kring->nr_hwavail; 128868b8534bSLuigi Rizzo } 128968b8534bSLuigi Rizzo return (errors ? 1 : 0); 129068b8534bSLuigi Rizzo } 129168b8534bSLuigi Rizzo 129268b8534bSLuigi Rizzo 129368b8534bSLuigi Rizzo /* 129468b8534bSLuigi Rizzo * Set the ring ID. For devices with a single queue, a request 129568b8534bSLuigi Rizzo * for all rings is the same as a single ring. 129668b8534bSLuigi Rizzo */ 129768b8534bSLuigi Rizzo static int 129868b8534bSLuigi Rizzo netmap_set_ringid(struct netmap_priv_d *priv, u_int ringid) 129968b8534bSLuigi Rizzo { 130068b8534bSLuigi Rizzo struct ifnet *ifp = priv->np_ifp; 130168b8534bSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 130268b8534bSLuigi Rizzo u_int i = ringid & NETMAP_RING_MASK; 130364ae02c3SLuigi Rizzo /* initially (np_qfirst == np_qlast) we don't want to lock */ 130468b8534bSLuigi Rizzo int need_lock = (priv->np_qfirst != priv->np_qlast); 1305d76bf4ffSLuigi Rizzo int lim = na->num_rx_rings; 130668b8534bSLuigi Rizzo 1307d76bf4ffSLuigi Rizzo if (na->num_tx_rings > lim) 1308d76bf4ffSLuigi Rizzo lim = na->num_tx_rings; 130964ae02c3SLuigi Rizzo if ( (ringid & NETMAP_HW_RING) && i >= lim) { 131068b8534bSLuigi Rizzo D("invalid ring id %d", i); 131168b8534bSLuigi Rizzo return (EINVAL); 131268b8534bSLuigi Rizzo } 131368b8534bSLuigi Rizzo if (need_lock) 13141a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_LOCK, 0); 131568b8534bSLuigi Rizzo priv->np_ringid = ringid; 131668b8534bSLuigi Rizzo if (ringid & NETMAP_SW_RING) { 131764ae02c3SLuigi Rizzo priv->np_qfirst = NETMAP_SW_RING; 131864ae02c3SLuigi Rizzo priv->np_qlast = 0; 131968b8534bSLuigi Rizzo } else if (ringid & NETMAP_HW_RING) { 132068b8534bSLuigi Rizzo priv->np_qfirst = i; 132168b8534bSLuigi Rizzo priv->np_qlast = i + 1; 132268b8534bSLuigi Rizzo } else { 132368b8534bSLuigi Rizzo priv->np_qfirst = 0; 132464ae02c3SLuigi Rizzo priv->np_qlast = NETMAP_HW_RING ; 132568b8534bSLuigi Rizzo } 132668b8534bSLuigi Rizzo priv->np_txpoll = (ringid & NETMAP_NO_TX_POLL) ? 0 : 1; 132768b8534bSLuigi Rizzo if (need_lock) 13281a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0); 1329ae10d1afSLuigi Rizzo if (netmap_verbose) { 133068b8534bSLuigi Rizzo if (ringid & NETMAP_SW_RING) 133168b8534bSLuigi Rizzo D("ringid %s set to SW RING", ifp->if_xname); 133268b8534bSLuigi Rizzo else if (ringid & NETMAP_HW_RING) 133368b8534bSLuigi Rizzo D("ringid %s set to HW RING %d", ifp->if_xname, 133468b8534bSLuigi Rizzo priv->np_qfirst); 133568b8534bSLuigi Rizzo else 133664ae02c3SLuigi Rizzo D("ringid %s set to all %d HW RINGS", ifp->if_xname, lim); 1337ae10d1afSLuigi Rizzo } 133868b8534bSLuigi Rizzo return 0; 133968b8534bSLuigi Rizzo } 134068b8534bSLuigi Rizzo 1341*f18be576SLuigi Rizzo 1342*f18be576SLuigi Rizzo /* 1343*f18be576SLuigi Rizzo * possibly move the interface to netmap-mode. 1344*f18be576SLuigi Rizzo * If success it returns a pointer to netmap_if, otherwise NULL. 1345*f18be576SLuigi Rizzo * This must be called with NMA_LOCK held. 1346*f18be576SLuigi Rizzo */ 1347*f18be576SLuigi Rizzo static struct netmap_if * 1348*f18be576SLuigi Rizzo netmap_do_regif(struct netmap_priv_d *priv, struct ifnet *ifp, 1349*f18be576SLuigi Rizzo uint16_t ringid, int *err) 1350*f18be576SLuigi Rizzo { 1351*f18be576SLuigi Rizzo struct netmap_adapter *na = NA(ifp); 1352*f18be576SLuigi Rizzo struct netmap_if *nifp = NULL; 1353*f18be576SLuigi Rizzo int i, error; 1354*f18be576SLuigi Rizzo 1355*f18be576SLuigi Rizzo if (na->na_bdg) 1356*f18be576SLuigi Rizzo BDG_WLOCK(na->na_bdg); 1357*f18be576SLuigi Rizzo na->nm_lock(ifp, NETMAP_REG_LOCK, 0); 1358*f18be576SLuigi Rizzo 1359*f18be576SLuigi Rizzo /* ring configuration may have changed, fetch from the card */ 1360*f18be576SLuigi Rizzo netmap_update_config(na); 1361*f18be576SLuigi Rizzo priv->np_ifp = ifp; /* store the reference */ 1362*f18be576SLuigi Rizzo error = netmap_set_ringid(priv, ringid); 1363*f18be576SLuigi Rizzo if (error) 1364*f18be576SLuigi Rizzo goto out; 1365*f18be576SLuigi Rizzo nifp = netmap_if_new(ifp->if_xname, na); 1366*f18be576SLuigi Rizzo if (nifp == NULL) { /* allocation failed */ 1367*f18be576SLuigi Rizzo error = ENOMEM; 1368*f18be576SLuigi Rizzo } else if (ifp->if_capenable & IFCAP_NETMAP) { 1369*f18be576SLuigi Rizzo /* was already set */ 1370*f18be576SLuigi Rizzo } else { 1371*f18be576SLuigi Rizzo /* Otherwise set the card in netmap mode 1372*f18be576SLuigi Rizzo * and make it use the shared buffers. 1373*f18be576SLuigi Rizzo */ 1374*f18be576SLuigi Rizzo for (i = 0 ; i < na->num_tx_rings + 1; i++) 1375*f18be576SLuigi Rizzo mtx_init(&na->tx_rings[i].q_lock, "nm_txq_lock", 1376*f18be576SLuigi Rizzo MTX_NETWORK_LOCK, MTX_DEF); 1377*f18be576SLuigi Rizzo for (i = 0 ; i < na->num_rx_rings + 1; i++) { 1378*f18be576SLuigi Rizzo mtx_init(&na->rx_rings[i].q_lock, "nm_rxq_lock", 1379*f18be576SLuigi Rizzo MTX_NETWORK_LOCK, MTX_DEF); 1380*f18be576SLuigi Rizzo } 1381*f18be576SLuigi Rizzo if (nma_is_hw(na)) { 1382*f18be576SLuigi Rizzo SWNA(ifp)->tx_rings = &na->tx_rings[na->num_tx_rings]; 1383*f18be576SLuigi Rizzo SWNA(ifp)->rx_rings = &na->rx_rings[na->num_rx_rings]; 1384*f18be576SLuigi Rizzo } 1385*f18be576SLuigi Rizzo error = na->nm_register(ifp, 1); /* mode on */ 1386*f18be576SLuigi Rizzo #ifdef NM_BRIDGE 1387*f18be576SLuigi Rizzo if (!error) 1388*f18be576SLuigi Rizzo error = nm_alloc_bdgfwd(na); 1389*f18be576SLuigi Rizzo #endif /* NM_BRIDGE */ 1390*f18be576SLuigi Rizzo if (error) { 1391*f18be576SLuigi Rizzo netmap_dtor_locked(priv); 1392*f18be576SLuigi Rizzo /* nifp is not yet in priv, so free it separately */ 1393*f18be576SLuigi Rizzo netmap_if_free(nifp); 1394*f18be576SLuigi Rizzo nifp = NULL; 1395*f18be576SLuigi Rizzo } 1396*f18be576SLuigi Rizzo 1397*f18be576SLuigi Rizzo } 1398*f18be576SLuigi Rizzo out: 1399*f18be576SLuigi Rizzo *err = error; 1400*f18be576SLuigi Rizzo na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0); 1401*f18be576SLuigi Rizzo if (na->na_bdg) 1402*f18be576SLuigi Rizzo BDG_WUNLOCK(na->na_bdg); 1403*f18be576SLuigi Rizzo return nifp; 1404*f18be576SLuigi Rizzo } 1405*f18be576SLuigi Rizzo 1406*f18be576SLuigi Rizzo 1407*f18be576SLuigi Rizzo /* Process NETMAP_BDG_ATTACH and NETMAP_BDG_DETACH */ 1408*f18be576SLuigi Rizzo static int 1409*f18be576SLuigi Rizzo kern_netmap_regif(struct nmreq *nmr) 1410*f18be576SLuigi Rizzo { 1411*f18be576SLuigi Rizzo struct ifnet *ifp; 1412*f18be576SLuigi Rizzo struct netmap_if *nifp; 1413*f18be576SLuigi Rizzo struct netmap_priv_d *npriv; 1414*f18be576SLuigi Rizzo int error; 1415*f18be576SLuigi Rizzo 1416*f18be576SLuigi Rizzo npriv = malloc(sizeof(*npriv), M_DEVBUF, M_NOWAIT|M_ZERO); 1417*f18be576SLuigi Rizzo if (npriv == NULL) 1418*f18be576SLuigi Rizzo return ENOMEM; 1419*f18be576SLuigi Rizzo error = netmap_get_memory(npriv); 1420*f18be576SLuigi Rizzo if (error) { 1421*f18be576SLuigi Rizzo free_exit: 1422*f18be576SLuigi Rizzo bzero(npriv, sizeof(*npriv)); 1423*f18be576SLuigi Rizzo free(npriv, M_DEVBUF); 1424*f18be576SLuigi Rizzo return error; 1425*f18be576SLuigi Rizzo } 1426*f18be576SLuigi Rizzo 1427*f18be576SLuigi Rizzo NMA_LOCK(); 1428*f18be576SLuigi Rizzo error = get_ifp(nmr, &ifp); 1429*f18be576SLuigi Rizzo if (error) { /* no device, or another bridge or user owns the device */ 1430*f18be576SLuigi Rizzo NMA_UNLOCK(); 1431*f18be576SLuigi Rizzo goto free_exit; 1432*f18be576SLuigi Rizzo } else if (!NETMAP_OWNED_BY_KERN(ifp)) { 1433*f18be576SLuigi Rizzo /* got reference to a virtual port or direct access to a NIC. 1434*f18be576SLuigi Rizzo * perhaps specified no bridge's prefix or wrong NIC's name 1435*f18be576SLuigi Rizzo */ 1436*f18be576SLuigi Rizzo error = EINVAL; 1437*f18be576SLuigi Rizzo unref_exit: 1438*f18be576SLuigi Rizzo nm_if_rele(ifp); 1439*f18be576SLuigi Rizzo NMA_UNLOCK(); 1440*f18be576SLuigi Rizzo goto free_exit; 1441*f18be576SLuigi Rizzo } 1442*f18be576SLuigi Rizzo 1443*f18be576SLuigi Rizzo if (nmr->nr_cmd == NETMAP_BDG_DETACH) { 1444*f18be576SLuigi Rizzo if (NA(ifp)->refcount == 0) { /* not registered */ 1445*f18be576SLuigi Rizzo error = EINVAL; 1446*f18be576SLuigi Rizzo goto unref_exit; 1447*f18be576SLuigi Rizzo } 1448*f18be576SLuigi Rizzo NMA_UNLOCK(); 1449*f18be576SLuigi Rizzo 1450*f18be576SLuigi Rizzo netmap_dtor(NA(ifp)->na_kpriv); /* unregister */ 1451*f18be576SLuigi Rizzo NA(ifp)->na_kpriv = NULL; 1452*f18be576SLuigi Rizzo nm_if_rele(ifp); /* detach from the bridge */ 1453*f18be576SLuigi Rizzo goto free_exit; 1454*f18be576SLuigi Rizzo } else if (NA(ifp)->refcount > 0) { /* already registered */ 1455*f18be576SLuigi Rizzo error = EINVAL; 1456*f18be576SLuigi Rizzo goto unref_exit; 1457*f18be576SLuigi Rizzo } 1458*f18be576SLuigi Rizzo 1459*f18be576SLuigi Rizzo nifp = netmap_do_regif(npriv, ifp, nmr->nr_ringid, &error); 1460*f18be576SLuigi Rizzo if (!nifp) 1461*f18be576SLuigi Rizzo goto unref_exit; 1462*f18be576SLuigi Rizzo wmb(); // XXX do we need it ? 1463*f18be576SLuigi Rizzo npriv->np_nifp = nifp; 1464*f18be576SLuigi Rizzo NA(ifp)->na_kpriv = npriv; 1465*f18be576SLuigi Rizzo NMA_UNLOCK(); 1466*f18be576SLuigi Rizzo D("registered %s to netmap-mode", ifp->if_xname); 1467*f18be576SLuigi Rizzo return 0; 1468*f18be576SLuigi Rizzo } 1469*f18be576SLuigi Rizzo 1470*f18be576SLuigi Rizzo 1471*f18be576SLuigi Rizzo /* CORE_LOCK is not necessary */ 1472*f18be576SLuigi Rizzo static void 1473*f18be576SLuigi Rizzo netmap_swlock_wrapper(struct ifnet *dev, int what, u_int queueid) 1474*f18be576SLuigi Rizzo { 1475*f18be576SLuigi Rizzo struct netmap_adapter *na = SWNA(dev); 1476*f18be576SLuigi Rizzo 1477*f18be576SLuigi Rizzo switch (what) { 1478*f18be576SLuigi Rizzo case NETMAP_TX_LOCK: 1479*f18be576SLuigi Rizzo mtx_lock(&na->tx_rings[queueid].q_lock); 1480*f18be576SLuigi Rizzo break; 1481*f18be576SLuigi Rizzo 1482*f18be576SLuigi Rizzo case NETMAP_TX_UNLOCK: 1483*f18be576SLuigi Rizzo mtx_unlock(&na->tx_rings[queueid].q_lock); 1484*f18be576SLuigi Rizzo break; 1485*f18be576SLuigi Rizzo 1486*f18be576SLuigi Rizzo case NETMAP_RX_LOCK: 1487*f18be576SLuigi Rizzo mtx_lock(&na->rx_rings[queueid].q_lock); 1488*f18be576SLuigi Rizzo break; 1489*f18be576SLuigi Rizzo 1490*f18be576SLuigi Rizzo case NETMAP_RX_UNLOCK: 1491*f18be576SLuigi Rizzo mtx_unlock(&na->rx_rings[queueid].q_lock); 1492*f18be576SLuigi Rizzo break; 1493*f18be576SLuigi Rizzo } 1494*f18be576SLuigi Rizzo } 1495*f18be576SLuigi Rizzo 1496*f18be576SLuigi Rizzo 1497*f18be576SLuigi Rizzo /* Initialize necessary fields of sw adapter located in right after hw's 1498*f18be576SLuigi Rizzo * one. sw adapter attaches a pair of sw rings of the netmap-mode NIC. 1499*f18be576SLuigi Rizzo * It is always activated and deactivated at the same tie with the hw's one. 1500*f18be576SLuigi Rizzo * Thus we don't need refcounting on the sw adapter. 1501*f18be576SLuigi Rizzo * Regardless of NIC's feature we use separate lock so that anybody can lock 1502*f18be576SLuigi Rizzo * me independently from the hw adapter. 1503*f18be576SLuigi Rizzo * Make sure nm_register is NULL to be handled as FALSE in nma_is_hw 1504*f18be576SLuigi Rizzo */ 1505*f18be576SLuigi Rizzo static void 1506*f18be576SLuigi Rizzo netmap_attach_sw(struct ifnet *ifp) 1507*f18be576SLuigi Rizzo { 1508*f18be576SLuigi Rizzo struct netmap_adapter *hw_na = NA(ifp); 1509*f18be576SLuigi Rizzo struct netmap_adapter *na = SWNA(ifp); 1510*f18be576SLuigi Rizzo 1511*f18be576SLuigi Rizzo na->ifp = ifp; 1512*f18be576SLuigi Rizzo na->separate_locks = 1; 1513*f18be576SLuigi Rizzo na->nm_lock = netmap_swlock_wrapper; 1514*f18be576SLuigi Rizzo na->num_rx_rings = na->num_tx_rings = 1; 1515*f18be576SLuigi Rizzo na->num_tx_desc = hw_na->num_tx_desc; 1516*f18be576SLuigi Rizzo na->num_rx_desc = hw_na->num_rx_desc; 1517*f18be576SLuigi Rizzo na->nm_txsync = netmap_bdg_to_host; 1518*f18be576SLuigi Rizzo } 1519*f18be576SLuigi Rizzo 1520*f18be576SLuigi Rizzo 1521*f18be576SLuigi Rizzo /* exported to kernel callers */ 1522*f18be576SLuigi Rizzo int 1523*f18be576SLuigi Rizzo netmap_bdg_ctl(struct nmreq *nmr, bdg_lookup_fn_t func) 1524*f18be576SLuigi Rizzo { 1525*f18be576SLuigi Rizzo struct nm_bridge *b; 1526*f18be576SLuigi Rizzo struct netmap_adapter *na; 1527*f18be576SLuigi Rizzo struct ifnet *iter; 1528*f18be576SLuigi Rizzo char *name = nmr->nr_name; 1529*f18be576SLuigi Rizzo int cmd = nmr->nr_cmd, namelen = strlen(name); 1530*f18be576SLuigi Rizzo int error = 0, i, j; 1531*f18be576SLuigi Rizzo 1532*f18be576SLuigi Rizzo switch (cmd) { 1533*f18be576SLuigi Rizzo case NETMAP_BDG_ATTACH: 1534*f18be576SLuigi Rizzo case NETMAP_BDG_DETACH: 1535*f18be576SLuigi Rizzo error = kern_netmap_regif(nmr); 1536*f18be576SLuigi Rizzo break; 1537*f18be576SLuigi Rizzo 1538*f18be576SLuigi Rizzo case NETMAP_BDG_LIST: 1539*f18be576SLuigi Rizzo /* this is used to enumerate bridges and ports */ 1540*f18be576SLuigi Rizzo if (namelen) { /* look up indexes of bridge and port */ 1541*f18be576SLuigi Rizzo if (strncmp(name, NM_NAME, strlen(NM_NAME))) { 1542*f18be576SLuigi Rizzo error = EINVAL; 1543*f18be576SLuigi Rizzo break; 1544*f18be576SLuigi Rizzo } 1545*f18be576SLuigi Rizzo b = nm_find_bridge(name, 0 /* don't create */); 1546*f18be576SLuigi Rizzo if (!b) { 1547*f18be576SLuigi Rizzo error = ENOENT; 1548*f18be576SLuigi Rizzo break; 1549*f18be576SLuigi Rizzo } 1550*f18be576SLuigi Rizzo 1551*f18be576SLuigi Rizzo BDG_RLOCK(b); 1552*f18be576SLuigi Rizzo error = ENOENT; 1553*f18be576SLuigi Rizzo for (i = 0; i < NM_BDG_MAXPORTS; i++) { 1554*f18be576SLuigi Rizzo na = BDG_GET_VAR(b->bdg_ports[i]); 1555*f18be576SLuigi Rizzo if (na == NULL) 1556*f18be576SLuigi Rizzo continue; 1557*f18be576SLuigi Rizzo iter = na->ifp; 1558*f18be576SLuigi Rizzo /* the former and the latter identify a 1559*f18be576SLuigi Rizzo * virtual port and a NIC, respectively 1560*f18be576SLuigi Rizzo */ 1561*f18be576SLuigi Rizzo if (!strcmp(iter->if_xname, name) || 1562*f18be576SLuigi Rizzo (namelen > b->namelen && 1563*f18be576SLuigi Rizzo !strcmp(iter->if_xname, 1564*f18be576SLuigi Rizzo name + b->namelen + 1))) { 1565*f18be576SLuigi Rizzo /* bridge index */ 1566*f18be576SLuigi Rizzo nmr->nr_arg1 = b - nm_bridges; 1567*f18be576SLuigi Rizzo nmr->nr_arg2 = i; /* port index */ 1568*f18be576SLuigi Rizzo error = 0; 1569*f18be576SLuigi Rizzo break; 1570*f18be576SLuigi Rizzo } 1571*f18be576SLuigi Rizzo } 1572*f18be576SLuigi Rizzo BDG_RUNLOCK(b); 1573*f18be576SLuigi Rizzo } else { 1574*f18be576SLuigi Rizzo /* return the first non-empty entry starting from 1575*f18be576SLuigi Rizzo * bridge nr_arg1 and port nr_arg2. 1576*f18be576SLuigi Rizzo * 1577*f18be576SLuigi Rizzo * Users can detect the end of the same bridge by 1578*f18be576SLuigi Rizzo * seeing the new and old value of nr_arg1, and can 1579*f18be576SLuigi Rizzo * detect the end of all the bridge by error != 0 1580*f18be576SLuigi Rizzo */ 1581*f18be576SLuigi Rizzo i = nmr->nr_arg1; 1582*f18be576SLuigi Rizzo j = nmr->nr_arg2; 1583*f18be576SLuigi Rizzo 1584*f18be576SLuigi Rizzo for (error = ENOENT; error && i < NM_BRIDGES; i++) { 1585*f18be576SLuigi Rizzo b = nm_bridges + i; 1586*f18be576SLuigi Rizzo BDG_RLOCK(b); 1587*f18be576SLuigi Rizzo for (; j < NM_BDG_MAXPORTS; j++) { 1588*f18be576SLuigi Rizzo na = BDG_GET_VAR(b->bdg_ports[j]); 1589*f18be576SLuigi Rizzo if (na == NULL) 1590*f18be576SLuigi Rizzo continue; 1591*f18be576SLuigi Rizzo iter = na->ifp; 1592*f18be576SLuigi Rizzo nmr->nr_arg1 = i; 1593*f18be576SLuigi Rizzo nmr->nr_arg2 = j; 1594*f18be576SLuigi Rizzo strncpy(name, iter->if_xname, IFNAMSIZ); 1595*f18be576SLuigi Rizzo error = 0; 1596*f18be576SLuigi Rizzo break; 1597*f18be576SLuigi Rizzo } 1598*f18be576SLuigi Rizzo BDG_RUNLOCK(b); 1599*f18be576SLuigi Rizzo j = 0; /* following bridges scan from 0 */ 1600*f18be576SLuigi Rizzo } 1601*f18be576SLuigi Rizzo } 1602*f18be576SLuigi Rizzo break; 1603*f18be576SLuigi Rizzo 1604*f18be576SLuigi Rizzo case NETMAP_BDG_LOOKUP_REG: 1605*f18be576SLuigi Rizzo /* register a lookup function to the given bridge. 1606*f18be576SLuigi Rizzo * nmr->nr_name may be just bridge's name (including ':' 1607*f18be576SLuigi Rizzo * if it is not just NM_NAME). 1608*f18be576SLuigi Rizzo */ 1609*f18be576SLuigi Rizzo if (!func) { 1610*f18be576SLuigi Rizzo error = EINVAL; 1611*f18be576SLuigi Rizzo break; 1612*f18be576SLuigi Rizzo } 1613*f18be576SLuigi Rizzo b = nm_find_bridge(name, 0 /* don't create */); 1614*f18be576SLuigi Rizzo if (!b) { 1615*f18be576SLuigi Rizzo error = EINVAL; 1616*f18be576SLuigi Rizzo break; 1617*f18be576SLuigi Rizzo } 1618*f18be576SLuigi Rizzo BDG_WLOCK(b); 1619*f18be576SLuigi Rizzo b->nm_bdg_lookup = func; 1620*f18be576SLuigi Rizzo BDG_WUNLOCK(b); 1621*f18be576SLuigi Rizzo break; 1622*f18be576SLuigi Rizzo default: 1623*f18be576SLuigi Rizzo D("invalid cmd (nmr->nr_cmd) (0x%x)", cmd); 1624*f18be576SLuigi Rizzo error = EINVAL; 1625*f18be576SLuigi Rizzo break; 1626*f18be576SLuigi Rizzo } 1627*f18be576SLuigi Rizzo return error; 1628*f18be576SLuigi Rizzo } 1629*f18be576SLuigi Rizzo 1630*f18be576SLuigi Rizzo 163168b8534bSLuigi Rizzo /* 163268b8534bSLuigi Rizzo * ioctl(2) support for the "netmap" device. 163368b8534bSLuigi Rizzo * 163468b8534bSLuigi Rizzo * Following a list of accepted commands: 163568b8534bSLuigi Rizzo * - NIOCGINFO 163668b8534bSLuigi Rizzo * - SIOCGIFADDR just for convenience 163768b8534bSLuigi Rizzo * - NIOCREGIF 163868b8534bSLuigi Rizzo * - NIOCUNREGIF 163968b8534bSLuigi Rizzo * - NIOCTXSYNC 164068b8534bSLuigi Rizzo * - NIOCRXSYNC 164168b8534bSLuigi Rizzo * 164268b8534bSLuigi Rizzo * Return 0 on success, errno otherwise. 164368b8534bSLuigi Rizzo */ 164468b8534bSLuigi Rizzo static int 16450b8ed8e0SLuigi Rizzo netmap_ioctl(struct cdev *dev, u_long cmd, caddr_t data, 16460b8ed8e0SLuigi Rizzo int fflag, struct thread *td) 164768b8534bSLuigi Rizzo { 164868b8534bSLuigi Rizzo struct netmap_priv_d *priv = NULL; 164968b8534bSLuigi Rizzo struct ifnet *ifp; 165068b8534bSLuigi Rizzo struct nmreq *nmr = (struct nmreq *) data; 165168b8534bSLuigi Rizzo struct netmap_adapter *na; 165268b8534bSLuigi Rizzo int error; 165364ae02c3SLuigi Rizzo u_int i, lim; 165468b8534bSLuigi Rizzo struct netmap_if *nifp; 165568b8534bSLuigi Rizzo 16560b8ed8e0SLuigi Rizzo (void)dev; /* UNUSED */ 16570b8ed8e0SLuigi Rizzo (void)fflag; /* UNUSED */ 1658f196ce38SLuigi Rizzo #ifdef linux 1659f196ce38SLuigi Rizzo #define devfs_get_cdevpriv(pp) \ 1660f196ce38SLuigi Rizzo ({ *(struct netmap_priv_d **)pp = ((struct file *)td)->private_data; \ 1661f196ce38SLuigi Rizzo (*pp ? 0 : ENOENT); }) 1662f196ce38SLuigi Rizzo 1663f196ce38SLuigi Rizzo /* devfs_set_cdevpriv cannot fail on linux */ 1664f196ce38SLuigi Rizzo #define devfs_set_cdevpriv(p, fn) \ 1665f196ce38SLuigi Rizzo ({ ((struct file *)td)->private_data = p; (p ? 0 : EINVAL); }) 1666f196ce38SLuigi Rizzo 1667f196ce38SLuigi Rizzo 1668f196ce38SLuigi Rizzo #define devfs_clear_cdevpriv() do { \ 1669f196ce38SLuigi Rizzo netmap_dtor(priv); ((struct file *)td)->private_data = 0; \ 1670f196ce38SLuigi Rizzo } while (0) 1671f196ce38SLuigi Rizzo #endif /* linux */ 1672f196ce38SLuigi Rizzo 1673506cc70cSLuigi Rizzo CURVNET_SET(TD_TO_VNET(td)); 1674506cc70cSLuigi Rizzo 167568b8534bSLuigi Rizzo error = devfs_get_cdevpriv((void **)&priv); 16768241616dSLuigi Rizzo if (error) { 1677506cc70cSLuigi Rizzo CURVNET_RESTORE(); 16788241616dSLuigi Rizzo /* XXX ENOENT should be impossible, since the priv 16798241616dSLuigi Rizzo * is now created in the open */ 16808241616dSLuigi Rizzo return (error == ENOENT ? ENXIO : error); 1681506cc70cSLuigi Rizzo } 168268b8534bSLuigi Rizzo 1683f196ce38SLuigi Rizzo nmr->nr_name[sizeof(nmr->nr_name) - 1] = '\0'; /* truncate name */ 168468b8534bSLuigi Rizzo switch (cmd) { 168568b8534bSLuigi Rizzo case NIOCGINFO: /* return capabilities etc */ 168664ae02c3SLuigi Rizzo if (nmr->nr_version != NETMAP_API) { 168764ae02c3SLuigi Rizzo D("API mismatch got %d have %d", 168864ae02c3SLuigi Rizzo nmr->nr_version, NETMAP_API); 168964ae02c3SLuigi Rizzo nmr->nr_version = NETMAP_API; 169064ae02c3SLuigi Rizzo error = EINVAL; 169164ae02c3SLuigi Rizzo break; 169264ae02c3SLuigi Rizzo } 1693*f18be576SLuigi Rizzo if (nmr->nr_cmd == NETMAP_BDG_LIST) { 1694*f18be576SLuigi Rizzo error = netmap_bdg_ctl(nmr, NULL); 1695*f18be576SLuigi Rizzo break; 1696*f18be576SLuigi Rizzo } 16978241616dSLuigi Rizzo /* update configuration */ 16988241616dSLuigi Rizzo error = netmap_get_memory(priv); 16998241616dSLuigi Rizzo ND("get_memory returned %d", error); 17008241616dSLuigi Rizzo if (error) 17018241616dSLuigi Rizzo break; 17028241616dSLuigi Rizzo /* memsize is always valid */ 17038241616dSLuigi Rizzo nmr->nr_memsize = nm_mem.nm_totalsize; 17048241616dSLuigi Rizzo nmr->nr_offset = 0; 17058241616dSLuigi Rizzo nmr->nr_rx_slots = nmr->nr_tx_slots = 0; 170668b8534bSLuigi Rizzo if (nmr->nr_name[0] == '\0') /* just get memory info */ 170768b8534bSLuigi Rizzo break; 1708*f18be576SLuigi Rizzo /* lock because get_ifp and update_config see na->refcount */ 1709*f18be576SLuigi Rizzo NMA_LOCK(); 1710*f18be576SLuigi Rizzo error = get_ifp(nmr, &ifp); /* get a refcount */ 1711*f18be576SLuigi Rizzo if (error) { 1712*f18be576SLuigi Rizzo NMA_UNLOCK(); 171368b8534bSLuigi Rizzo break; 1714*f18be576SLuigi Rizzo } 171568b8534bSLuigi Rizzo na = NA(ifp); /* retrieve netmap_adapter */ 1716ae10d1afSLuigi Rizzo netmap_update_config(na); 1717*f18be576SLuigi Rizzo NMA_UNLOCK(); 1718d76bf4ffSLuigi Rizzo nmr->nr_rx_rings = na->num_rx_rings; 1719d76bf4ffSLuigi Rizzo nmr->nr_tx_rings = na->num_tx_rings; 172064ae02c3SLuigi Rizzo nmr->nr_rx_slots = na->num_rx_desc; 172164ae02c3SLuigi Rizzo nmr->nr_tx_slots = na->num_tx_desc; 1722f196ce38SLuigi Rizzo nm_if_rele(ifp); /* return the refcount */ 172368b8534bSLuigi Rizzo break; 172468b8534bSLuigi Rizzo 172568b8534bSLuigi Rizzo case NIOCREGIF: 172664ae02c3SLuigi Rizzo if (nmr->nr_version != NETMAP_API) { 172764ae02c3SLuigi Rizzo nmr->nr_version = NETMAP_API; 172864ae02c3SLuigi Rizzo error = EINVAL; 172964ae02c3SLuigi Rizzo break; 173064ae02c3SLuigi Rizzo } 1731*f18be576SLuigi Rizzo /* possibly attach/detach NIC and VALE switch */ 1732*f18be576SLuigi Rizzo i = nmr->nr_cmd; 1733*f18be576SLuigi Rizzo if (i == NETMAP_BDG_ATTACH || i == NETMAP_BDG_DETACH) { 1734*f18be576SLuigi Rizzo error = netmap_bdg_ctl(nmr, NULL); 1735*f18be576SLuigi Rizzo break; 1736*f18be576SLuigi Rizzo } else if (i != 0) { 1737*f18be576SLuigi Rizzo D("nr_cmd must be 0 not %d", i); 1738*f18be576SLuigi Rizzo error = EINVAL; 1739*f18be576SLuigi Rizzo break; 1740*f18be576SLuigi Rizzo } 1741*f18be576SLuigi Rizzo 17428241616dSLuigi Rizzo /* ensure allocators are ready */ 17438241616dSLuigi Rizzo error = netmap_get_memory(priv); 17448241616dSLuigi Rizzo ND("get_memory returned %d", error); 17458241616dSLuigi Rizzo if (error) 17468241616dSLuigi Rizzo break; 17478241616dSLuigi Rizzo 17488241616dSLuigi Rizzo /* protect access to priv from concurrent NIOCREGIF */ 17498241616dSLuigi Rizzo NMA_LOCK(); 17508241616dSLuigi Rizzo if (priv->np_ifp != NULL) { /* thread already registered */ 1751506cc70cSLuigi Rizzo error = netmap_set_ringid(priv, nmr->nr_ringid); 1752*f18be576SLuigi Rizzo unlock_out: 17538241616dSLuigi Rizzo NMA_UNLOCK(); 1754506cc70cSLuigi Rizzo break; 1755506cc70cSLuigi Rizzo } 175668b8534bSLuigi Rizzo /* find the interface and a reference */ 1757*f18be576SLuigi Rizzo error = get_ifp(nmr, &ifp); /* keep reference */ 175868b8534bSLuigi Rizzo if (error) 1759*f18be576SLuigi Rizzo goto unlock_out; 1760*f18be576SLuigi Rizzo else if (NETMAP_OWNED_BY_KERN(ifp)) { 1761*f18be576SLuigi Rizzo nm_if_rele(ifp); 1762*f18be576SLuigi Rizzo goto unlock_out; 1763f196ce38SLuigi Rizzo } 1764*f18be576SLuigi Rizzo nifp = netmap_do_regif(priv, ifp, nmr->nr_ringid, &error); 1765*f18be576SLuigi Rizzo if (!nifp) { /* reg. failed, release priv and ref */ 1766f196ce38SLuigi Rizzo nm_if_rele(ifp); /* return the refcount */ 17678241616dSLuigi Rizzo priv->np_ifp = NULL; 17688241616dSLuigi Rizzo priv->np_nifp = NULL; 1769*f18be576SLuigi Rizzo goto unlock_out; 177068b8534bSLuigi Rizzo } 177168b8534bSLuigi Rizzo 17728241616dSLuigi Rizzo /* the following assignment is a commitment. 17738241616dSLuigi Rizzo * Readers (i.e., poll and *SYNC) check for 17748241616dSLuigi Rizzo * np_nifp != NULL without locking 177568b8534bSLuigi Rizzo */ 17768241616dSLuigi Rizzo wmb(); /* make sure previous writes are visible to all CPUs */ 17778241616dSLuigi Rizzo priv->np_nifp = nifp; 17788241616dSLuigi Rizzo NMA_UNLOCK(); 177968b8534bSLuigi Rizzo 178068b8534bSLuigi Rizzo /* return the offset of the netmap_if object */ 1781*f18be576SLuigi Rizzo na = NA(ifp); /* retrieve netmap adapter */ 1782d76bf4ffSLuigi Rizzo nmr->nr_rx_rings = na->num_rx_rings; 1783d76bf4ffSLuigi Rizzo nmr->nr_tx_rings = na->num_tx_rings; 178464ae02c3SLuigi Rizzo nmr->nr_rx_slots = na->num_rx_desc; 178564ae02c3SLuigi Rizzo nmr->nr_tx_slots = na->num_tx_desc; 17868241616dSLuigi Rizzo nmr->nr_memsize = nm_mem.nm_totalsize; 1787446ee301SLuigi Rizzo nmr->nr_offset = netmap_if_offset(nifp); 178868b8534bSLuigi Rizzo break; 178968b8534bSLuigi Rizzo 179068b8534bSLuigi Rizzo case NIOCUNREGIF: 17918241616dSLuigi Rizzo // XXX we have no data here ? 17928241616dSLuigi Rizzo D("deprecated, data is %p", nmr); 17938241616dSLuigi Rizzo error = EINVAL; 179468b8534bSLuigi Rizzo break; 179568b8534bSLuigi Rizzo 179668b8534bSLuigi Rizzo case NIOCTXSYNC: 179768b8534bSLuigi Rizzo case NIOCRXSYNC: 17988241616dSLuigi Rizzo nifp = priv->np_nifp; 17998241616dSLuigi Rizzo 18008241616dSLuigi Rizzo if (nifp == NULL) { 1801506cc70cSLuigi Rizzo error = ENXIO; 1802506cc70cSLuigi Rizzo break; 1803506cc70cSLuigi Rizzo } 18048241616dSLuigi Rizzo rmb(); /* make sure following reads are not from cache */ 18058241616dSLuigi Rizzo 18068241616dSLuigi Rizzo 180768b8534bSLuigi Rizzo ifp = priv->np_ifp; /* we have a reference */ 18088241616dSLuigi Rizzo 18098241616dSLuigi Rizzo if (ifp == NULL) { 18108241616dSLuigi Rizzo D("Internal error: nifp != NULL && ifp == NULL"); 18118241616dSLuigi Rizzo error = ENXIO; 18128241616dSLuigi Rizzo break; 18138241616dSLuigi Rizzo } 18148241616dSLuigi Rizzo 181568b8534bSLuigi Rizzo na = NA(ifp); /* retrieve netmap adapter */ 181664ae02c3SLuigi Rizzo if (priv->np_qfirst == NETMAP_SW_RING) { /* host rings */ 181768b8534bSLuigi Rizzo if (cmd == NIOCTXSYNC) 181868b8534bSLuigi Rizzo netmap_sync_to_host(na); 181968b8534bSLuigi Rizzo else 182001c7d25fSLuigi Rizzo netmap_sync_from_host(na, NULL, NULL); 1821506cc70cSLuigi Rizzo break; 182268b8534bSLuigi Rizzo } 182364ae02c3SLuigi Rizzo /* find the last ring to scan */ 182464ae02c3SLuigi Rizzo lim = priv->np_qlast; 182564ae02c3SLuigi Rizzo if (lim == NETMAP_HW_RING) 18263c0caf6cSLuigi Rizzo lim = (cmd == NIOCTXSYNC) ? 1827d76bf4ffSLuigi Rizzo na->num_tx_rings : na->num_rx_rings; 182868b8534bSLuigi Rizzo 182964ae02c3SLuigi Rizzo for (i = priv->np_qfirst; i < lim; i++) { 183068b8534bSLuigi Rizzo if (cmd == NIOCTXSYNC) { 183168b8534bSLuigi Rizzo struct netmap_kring *kring = &na->tx_rings[i]; 183268b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_TXSYNC) 18333c0caf6cSLuigi Rizzo D("pre txsync ring %d cur %d hwcur %d", 183468b8534bSLuigi Rizzo i, kring->ring->cur, 183568b8534bSLuigi Rizzo kring->nr_hwcur); 18361a26580eSLuigi Rizzo na->nm_txsync(ifp, i, 1 /* do lock */); 183768b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_TXSYNC) 18383c0caf6cSLuigi Rizzo D("post txsync ring %d cur %d hwcur %d", 183968b8534bSLuigi Rizzo i, kring->ring->cur, 184068b8534bSLuigi Rizzo kring->nr_hwcur); 184168b8534bSLuigi Rizzo } else { 18421a26580eSLuigi Rizzo na->nm_rxsync(ifp, i, 1 /* do lock */); 184368b8534bSLuigi Rizzo microtime(&na->rx_rings[i].ring->ts); 184468b8534bSLuigi Rizzo } 184568b8534bSLuigi Rizzo } 184668b8534bSLuigi Rizzo 184768b8534bSLuigi Rizzo break; 184868b8534bSLuigi Rizzo 1849f196ce38SLuigi Rizzo #ifdef __FreeBSD__ 185068b8534bSLuigi Rizzo case BIOCIMMEDIATE: 185168b8534bSLuigi Rizzo case BIOCGHDRCMPLT: 185268b8534bSLuigi Rizzo case BIOCSHDRCMPLT: 185368b8534bSLuigi Rizzo case BIOCSSEESENT: 185468b8534bSLuigi Rizzo D("ignore BIOCIMMEDIATE/BIOCSHDRCMPLT/BIOCSHDRCMPLT/BIOCSSEESENT"); 185568b8534bSLuigi Rizzo break; 185668b8534bSLuigi Rizzo 1857babc7c12SLuigi Rizzo default: /* allow device-specific ioctls */ 185868b8534bSLuigi Rizzo { 185968b8534bSLuigi Rizzo struct socket so; 186068b8534bSLuigi Rizzo bzero(&so, sizeof(so)); 1861*f18be576SLuigi Rizzo error = get_ifp(nmr, &ifp); /* keep reference */ 186268b8534bSLuigi Rizzo if (error) 186368b8534bSLuigi Rizzo break; 186468b8534bSLuigi Rizzo so.so_vnet = ifp->if_vnet; 186568b8534bSLuigi Rizzo // so->so_proto not null. 186668b8534bSLuigi Rizzo error = ifioctl(&so, cmd, data, td); 1867f196ce38SLuigi Rizzo nm_if_rele(ifp); 1868babc7c12SLuigi Rizzo break; 186968b8534bSLuigi Rizzo } 1870f196ce38SLuigi Rizzo 1871f196ce38SLuigi Rizzo #else /* linux */ 1872f196ce38SLuigi Rizzo default: 1873f196ce38SLuigi Rizzo error = EOPNOTSUPP; 1874f196ce38SLuigi Rizzo #endif /* linux */ 187568b8534bSLuigi Rizzo } 187668b8534bSLuigi Rizzo 1877506cc70cSLuigi Rizzo CURVNET_RESTORE(); 187868b8534bSLuigi Rizzo return (error); 187968b8534bSLuigi Rizzo } 188068b8534bSLuigi Rizzo 188168b8534bSLuigi Rizzo 188268b8534bSLuigi Rizzo /* 188368b8534bSLuigi Rizzo * select(2) and poll(2) handlers for the "netmap" device. 188468b8534bSLuigi Rizzo * 188568b8534bSLuigi Rizzo * Can be called for one or more queues. 188668b8534bSLuigi Rizzo * Return true the event mask corresponding to ready events. 188768b8534bSLuigi Rizzo * If there are no ready events, do a selrecord on either individual 188868b8534bSLuigi Rizzo * selfd or on the global one. 188968b8534bSLuigi Rizzo * Device-dependent parts (locking and sync of tx/rx rings) 189068b8534bSLuigi Rizzo * are done through callbacks. 1891f196ce38SLuigi Rizzo * 189201c7d25fSLuigi Rizzo * On linux, arguments are really pwait, the poll table, and 'td' is struct file * 189301c7d25fSLuigi Rizzo * The first one is remapped to pwait as selrecord() uses the name as an 189401c7d25fSLuigi Rizzo * hidden argument. 189568b8534bSLuigi Rizzo */ 189668b8534bSLuigi Rizzo static int 189701c7d25fSLuigi Rizzo netmap_poll(struct cdev *dev, int events, struct thread *td) 189868b8534bSLuigi Rizzo { 189968b8534bSLuigi Rizzo struct netmap_priv_d *priv = NULL; 190068b8534bSLuigi Rizzo struct netmap_adapter *na; 190168b8534bSLuigi Rizzo struct ifnet *ifp; 190268b8534bSLuigi Rizzo struct netmap_kring *kring; 1903d0c7b075SLuigi Rizzo u_int core_lock, i, check_all, want_tx, want_rx, revents = 0; 1904091fd0abSLuigi Rizzo u_int lim_tx, lim_rx, host_forwarded = 0; 1905091fd0abSLuigi Rizzo struct mbq q = { NULL, NULL, 0 }; 1906bcda432eSLuigi Rizzo enum {NO_CL, NEED_CL, LOCKED_CL }; /* see below */ 190701c7d25fSLuigi Rizzo void *pwait = dev; /* linux compatibility */ 190801c7d25fSLuigi Rizzo 190901c7d25fSLuigi Rizzo (void)pwait; 191068b8534bSLuigi Rizzo 191168b8534bSLuigi Rizzo if (devfs_get_cdevpriv((void **)&priv) != 0 || priv == NULL) 191268b8534bSLuigi Rizzo return POLLERR; 191368b8534bSLuigi Rizzo 19148241616dSLuigi Rizzo if (priv->np_nifp == NULL) { 19158241616dSLuigi Rizzo D("No if registered"); 19168241616dSLuigi Rizzo return POLLERR; 19178241616dSLuigi Rizzo } 19188241616dSLuigi Rizzo rmb(); /* make sure following reads are not from cache */ 19198241616dSLuigi Rizzo 192068b8534bSLuigi Rizzo ifp = priv->np_ifp; 192168b8534bSLuigi Rizzo // XXX check for deleting() ? 192268b8534bSLuigi Rizzo if ( (ifp->if_capenable & IFCAP_NETMAP) == 0) 192368b8534bSLuigi Rizzo return POLLERR; 192468b8534bSLuigi Rizzo 192568b8534bSLuigi Rizzo if (netmap_verbose & 0x8000) 192668b8534bSLuigi Rizzo D("device %s events 0x%x", ifp->if_xname, events); 192768b8534bSLuigi Rizzo want_tx = events & (POLLOUT | POLLWRNORM); 192868b8534bSLuigi Rizzo want_rx = events & (POLLIN | POLLRDNORM); 192968b8534bSLuigi Rizzo 193068b8534bSLuigi Rizzo na = NA(ifp); /* retrieve netmap adapter */ 193168b8534bSLuigi Rizzo 1932d76bf4ffSLuigi Rizzo lim_tx = na->num_tx_rings; 1933d76bf4ffSLuigi Rizzo lim_rx = na->num_rx_rings; 193468b8534bSLuigi Rizzo /* how many queues we are scanning */ 193564ae02c3SLuigi Rizzo if (priv->np_qfirst == NETMAP_SW_RING) { 193668b8534bSLuigi Rizzo if (priv->np_txpoll || want_tx) { 193768b8534bSLuigi Rizzo /* push any packets up, then we are always ready */ 193868b8534bSLuigi Rizzo netmap_sync_to_host(na); 193968b8534bSLuigi Rizzo revents |= want_tx; 194068b8534bSLuigi Rizzo } 194168b8534bSLuigi Rizzo if (want_rx) { 194264ae02c3SLuigi Rizzo kring = &na->rx_rings[lim_rx]; 194368b8534bSLuigi Rizzo if (kring->ring->avail == 0) 194401c7d25fSLuigi Rizzo netmap_sync_from_host(na, td, dev); 194568b8534bSLuigi Rizzo if (kring->ring->avail > 0) { 194668b8534bSLuigi Rizzo revents |= want_rx; 194768b8534bSLuigi Rizzo } 194868b8534bSLuigi Rizzo } 194968b8534bSLuigi Rizzo return (revents); 195068b8534bSLuigi Rizzo } 195168b8534bSLuigi Rizzo 1952091fd0abSLuigi Rizzo /* if we are in transparent mode, check also the host rx ring */ 1953091fd0abSLuigi Rizzo kring = &na->rx_rings[lim_rx]; 1954091fd0abSLuigi Rizzo if ( (priv->np_qlast == NETMAP_HW_RING) // XXX check_all 1955091fd0abSLuigi Rizzo && want_rx 1956091fd0abSLuigi Rizzo && (netmap_fwd || kring->ring->flags & NR_FORWARD) ) { 1957091fd0abSLuigi Rizzo if (kring->ring->avail == 0) 1958091fd0abSLuigi Rizzo netmap_sync_from_host(na, td, dev); 1959091fd0abSLuigi Rizzo if (kring->ring->avail > 0) 1960091fd0abSLuigi Rizzo revents |= want_rx; 1961091fd0abSLuigi Rizzo } 1962091fd0abSLuigi Rizzo 196368b8534bSLuigi Rizzo /* 196468b8534bSLuigi Rizzo * check_all is set if the card has more than one queue and 196568b8534bSLuigi Rizzo * the client is polling all of them. If true, we sleep on 196668b8534bSLuigi Rizzo * the "global" selfd, otherwise we sleep on individual selfd 196768b8534bSLuigi Rizzo * (we can only sleep on one of them per direction). 196868b8534bSLuigi Rizzo * The interrupt routine in the driver should always wake on 196968b8534bSLuigi Rizzo * the individual selfd, and also on the global one if the card 197068b8534bSLuigi Rizzo * has more than one ring. 197168b8534bSLuigi Rizzo * 197268b8534bSLuigi Rizzo * If the card has only one lock, we just use that. 197368b8534bSLuigi Rizzo * If the card has separate ring locks, we just use those 197468b8534bSLuigi Rizzo * unless we are doing check_all, in which case the whole 197568b8534bSLuigi Rizzo * loop is wrapped by the global lock. 197668b8534bSLuigi Rizzo * We acquire locks only when necessary: if poll is called 197768b8534bSLuigi Rizzo * when buffers are available, we can just return without locks. 197868b8534bSLuigi Rizzo * 197968b8534bSLuigi Rizzo * rxsync() is only called if we run out of buffers on a POLLIN. 198068b8534bSLuigi Rizzo * txsync() is called if we run out of buffers on POLLOUT, or 198168b8534bSLuigi Rizzo * there are pending packets to send. The latter can be disabled 198268b8534bSLuigi Rizzo * passing NETMAP_NO_TX_POLL in the NIOCREG call. 198368b8534bSLuigi Rizzo */ 198464ae02c3SLuigi Rizzo check_all = (priv->np_qlast == NETMAP_HW_RING) && (lim_tx > 1 || lim_rx > 1); 198568b8534bSLuigi Rizzo 198668b8534bSLuigi Rizzo /* 198768b8534bSLuigi Rizzo * core_lock indicates what to do with the core lock. 198868b8534bSLuigi Rizzo * The core lock is used when either the card has no individual 198968b8534bSLuigi Rizzo * locks, or it has individual locks but we are cheking all 199068b8534bSLuigi Rizzo * rings so we need the core lock to avoid missing wakeup events. 199168b8534bSLuigi Rizzo * 199268b8534bSLuigi Rizzo * It has three possible states: 199368b8534bSLuigi Rizzo * NO_CL we don't need to use the core lock, e.g. 199468b8534bSLuigi Rizzo * because we are protected by individual locks. 199568b8534bSLuigi Rizzo * NEED_CL we need the core lock. In this case, when we 199668b8534bSLuigi Rizzo * call the lock routine, move to LOCKED_CL 199768b8534bSLuigi Rizzo * to remember to release the lock once done. 199868b8534bSLuigi Rizzo * LOCKED_CL core lock is set, so we need to release it. 199968b8534bSLuigi Rizzo */ 2000d0c7b075SLuigi Rizzo core_lock = (check_all || !na->separate_locks) ? NEED_CL : NO_CL; 2001f196ce38SLuigi Rizzo #ifdef NM_BRIDGE 2002f196ce38SLuigi Rizzo /* the bridge uses separate locks */ 2003f196ce38SLuigi Rizzo if (na->nm_register == bdg_netmap_reg) { 2004f196ce38SLuigi Rizzo ND("not using core lock for %s", ifp->if_xname); 2005f196ce38SLuigi Rizzo core_lock = NO_CL; 2006f196ce38SLuigi Rizzo } 2007f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */ 200864ae02c3SLuigi Rizzo if (priv->np_qlast != NETMAP_HW_RING) { 200964ae02c3SLuigi Rizzo lim_tx = lim_rx = priv->np_qlast; 201064ae02c3SLuigi Rizzo } 201164ae02c3SLuigi Rizzo 201268b8534bSLuigi Rizzo /* 201368b8534bSLuigi Rizzo * We start with a lock free round which is good if we have 201468b8534bSLuigi Rizzo * data available. If this fails, then lock and call the sync 201568b8534bSLuigi Rizzo * routines. 201668b8534bSLuigi Rizzo */ 201764ae02c3SLuigi Rizzo for (i = priv->np_qfirst; want_rx && i < lim_rx; i++) { 201868b8534bSLuigi Rizzo kring = &na->rx_rings[i]; 201968b8534bSLuigi Rizzo if (kring->ring->avail > 0) { 202068b8534bSLuigi Rizzo revents |= want_rx; 202168b8534bSLuigi Rizzo want_rx = 0; /* also breaks the loop */ 202268b8534bSLuigi Rizzo } 202368b8534bSLuigi Rizzo } 202464ae02c3SLuigi Rizzo for (i = priv->np_qfirst; want_tx && i < lim_tx; i++) { 202568b8534bSLuigi Rizzo kring = &na->tx_rings[i]; 202668b8534bSLuigi Rizzo if (kring->ring->avail > 0) { 202768b8534bSLuigi Rizzo revents |= want_tx; 202868b8534bSLuigi Rizzo want_tx = 0; /* also breaks the loop */ 202968b8534bSLuigi Rizzo } 203068b8534bSLuigi Rizzo } 203168b8534bSLuigi Rizzo 203268b8534bSLuigi Rizzo /* 203368b8534bSLuigi Rizzo * If we to push packets out (priv->np_txpoll) or want_tx is 203468b8534bSLuigi Rizzo * still set, we do need to run the txsync calls (on all rings, 203568b8534bSLuigi Rizzo * to avoid that the tx rings stall). 203668b8534bSLuigi Rizzo */ 203768b8534bSLuigi Rizzo if (priv->np_txpoll || want_tx) { 2038091fd0abSLuigi Rizzo flush_tx: 203964ae02c3SLuigi Rizzo for (i = priv->np_qfirst; i < lim_tx; i++) { 204068b8534bSLuigi Rizzo kring = &na->tx_rings[i]; 20415819da83SLuigi Rizzo /* 20425819da83SLuigi Rizzo * Skip the current ring if want_tx == 0 20435819da83SLuigi Rizzo * (we have already done a successful sync on 20445819da83SLuigi Rizzo * a previous ring) AND kring->cur == kring->hwcur 20455819da83SLuigi Rizzo * (there are no pending transmissions for this ring). 20465819da83SLuigi Rizzo */ 204768b8534bSLuigi Rizzo if (!want_tx && kring->ring->cur == kring->nr_hwcur) 204868b8534bSLuigi Rizzo continue; 204968b8534bSLuigi Rizzo if (core_lock == NEED_CL) { 20501a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_LOCK, 0); 205168b8534bSLuigi Rizzo core_lock = LOCKED_CL; 205268b8534bSLuigi Rizzo } 205368b8534bSLuigi Rizzo if (na->separate_locks) 20541a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_TX_LOCK, i); 205568b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_TXSYNC) 205668b8534bSLuigi Rizzo D("send %d on %s %d", 205768b8534bSLuigi Rizzo kring->ring->cur, 205868b8534bSLuigi Rizzo ifp->if_xname, i); 20591a26580eSLuigi Rizzo if (na->nm_txsync(ifp, i, 0 /* no lock */)) 206068b8534bSLuigi Rizzo revents |= POLLERR; 206168b8534bSLuigi Rizzo 20625819da83SLuigi Rizzo /* Check avail/call selrecord only if called with POLLOUT */ 206368b8534bSLuigi Rizzo if (want_tx) { 206468b8534bSLuigi Rizzo if (kring->ring->avail > 0) { 206568b8534bSLuigi Rizzo /* stop at the first ring. We don't risk 206668b8534bSLuigi Rizzo * starvation. 206768b8534bSLuigi Rizzo */ 206868b8534bSLuigi Rizzo revents |= want_tx; 206968b8534bSLuigi Rizzo want_tx = 0; 207068b8534bSLuigi Rizzo } else if (!check_all) 207168b8534bSLuigi Rizzo selrecord(td, &kring->si); 207268b8534bSLuigi Rizzo } 207368b8534bSLuigi Rizzo if (na->separate_locks) 20741a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_TX_UNLOCK, i); 207568b8534bSLuigi Rizzo } 207668b8534bSLuigi Rizzo } 207768b8534bSLuigi Rizzo 207868b8534bSLuigi Rizzo /* 207968b8534bSLuigi Rizzo * now if want_rx is still set we need to lock and rxsync. 208068b8534bSLuigi Rizzo * Do it on all rings because otherwise we starve. 208168b8534bSLuigi Rizzo */ 208268b8534bSLuigi Rizzo if (want_rx) { 208364ae02c3SLuigi Rizzo for (i = priv->np_qfirst; i < lim_rx; i++) { 208468b8534bSLuigi Rizzo kring = &na->rx_rings[i]; 208568b8534bSLuigi Rizzo if (core_lock == NEED_CL) { 20861a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_LOCK, 0); 208768b8534bSLuigi Rizzo core_lock = LOCKED_CL; 208868b8534bSLuigi Rizzo } 208968b8534bSLuigi Rizzo if (na->separate_locks) 20901a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_RX_LOCK, i); 2091091fd0abSLuigi Rizzo if (netmap_fwd ||kring->ring->flags & NR_FORWARD) { 2092091fd0abSLuigi Rizzo ND(10, "forwarding some buffers up %d to %d", 2093091fd0abSLuigi Rizzo kring->nr_hwcur, kring->ring->cur); 2094091fd0abSLuigi Rizzo netmap_grab_packets(kring, &q, netmap_fwd); 2095091fd0abSLuigi Rizzo } 209668b8534bSLuigi Rizzo 20971a26580eSLuigi Rizzo if (na->nm_rxsync(ifp, i, 0 /* no lock */)) 209868b8534bSLuigi Rizzo revents |= POLLERR; 20995819da83SLuigi Rizzo if (netmap_no_timestamp == 0 || 21005819da83SLuigi Rizzo kring->ring->flags & NR_TIMESTAMP) { 210168b8534bSLuigi Rizzo microtime(&kring->ring->ts); 21025819da83SLuigi Rizzo } 210368b8534bSLuigi Rizzo 210468b8534bSLuigi Rizzo if (kring->ring->avail > 0) 210568b8534bSLuigi Rizzo revents |= want_rx; 210668b8534bSLuigi Rizzo else if (!check_all) 210768b8534bSLuigi Rizzo selrecord(td, &kring->si); 210868b8534bSLuigi Rizzo if (na->separate_locks) 21091a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_RX_UNLOCK, i); 211068b8534bSLuigi Rizzo } 211168b8534bSLuigi Rizzo } 211264ae02c3SLuigi Rizzo if (check_all && revents == 0) { /* signal on the global queue */ 211368b8534bSLuigi Rizzo if (want_tx) 211464ae02c3SLuigi Rizzo selrecord(td, &na->tx_si); 211568b8534bSLuigi Rizzo if (want_rx) 211664ae02c3SLuigi Rizzo selrecord(td, &na->rx_si); 211768b8534bSLuigi Rizzo } 2118091fd0abSLuigi Rizzo 2119091fd0abSLuigi Rizzo /* forward host to the netmap ring */ 2120091fd0abSLuigi Rizzo kring = &na->rx_rings[lim_rx]; 2121091fd0abSLuigi Rizzo if (kring->nr_hwavail > 0) 2122091fd0abSLuigi Rizzo ND("host rx %d has %d packets", lim_rx, kring->nr_hwavail); 2123091fd0abSLuigi Rizzo if ( (priv->np_qlast == NETMAP_HW_RING) // XXX check_all 2124091fd0abSLuigi Rizzo && (netmap_fwd || kring->ring->flags & NR_FORWARD) 2125091fd0abSLuigi Rizzo && kring->nr_hwavail > 0 && !host_forwarded) { 2126091fd0abSLuigi Rizzo if (core_lock == NEED_CL) { 2127091fd0abSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_LOCK, 0); 2128091fd0abSLuigi Rizzo core_lock = LOCKED_CL; 2129091fd0abSLuigi Rizzo } 2130091fd0abSLuigi Rizzo netmap_sw_to_nic(na); 2131091fd0abSLuigi Rizzo host_forwarded = 1; /* prevent another pass */ 2132091fd0abSLuigi Rizzo want_rx = 0; 2133091fd0abSLuigi Rizzo goto flush_tx; 2134091fd0abSLuigi Rizzo } 2135091fd0abSLuigi Rizzo 213668b8534bSLuigi Rizzo if (core_lock == LOCKED_CL) 21371a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0); 2138091fd0abSLuigi Rizzo if (q.head) 2139091fd0abSLuigi Rizzo netmap_send_up(na->ifp, q.head); 214068b8534bSLuigi Rizzo 214168b8534bSLuigi Rizzo return (revents); 214268b8534bSLuigi Rizzo } 214368b8534bSLuigi Rizzo 214468b8534bSLuigi Rizzo /*------- driver support routines ------*/ 214568b8534bSLuigi Rizzo 2146*f18be576SLuigi Rizzo 214768b8534bSLuigi Rizzo /* 2148babc7c12SLuigi Rizzo * default lock wrapper. 21491a26580eSLuigi Rizzo */ 21501a26580eSLuigi Rizzo static void 2151babc7c12SLuigi Rizzo netmap_lock_wrapper(struct ifnet *dev, int what, u_int queueid) 21521a26580eSLuigi Rizzo { 2153babc7c12SLuigi Rizzo struct netmap_adapter *na = NA(dev); 21541a26580eSLuigi Rizzo 21551a26580eSLuigi Rizzo switch (what) { 2156babc7c12SLuigi Rizzo #ifdef linux /* some system do not need lock on register */ 21571a26580eSLuigi Rizzo case NETMAP_REG_LOCK: 21581a26580eSLuigi Rizzo case NETMAP_REG_UNLOCK: 21591a26580eSLuigi Rizzo break; 2160babc7c12SLuigi Rizzo #endif /* linux */ 21611a26580eSLuigi Rizzo 21621a26580eSLuigi Rizzo case NETMAP_CORE_LOCK: 21631a26580eSLuigi Rizzo mtx_lock(&na->core_lock); 21641a26580eSLuigi Rizzo break; 21651a26580eSLuigi Rizzo 21661a26580eSLuigi Rizzo case NETMAP_CORE_UNLOCK: 21671a26580eSLuigi Rizzo mtx_unlock(&na->core_lock); 21681a26580eSLuigi Rizzo break; 21691a26580eSLuigi Rizzo 21701a26580eSLuigi Rizzo case NETMAP_TX_LOCK: 21711a26580eSLuigi Rizzo mtx_lock(&na->tx_rings[queueid].q_lock); 21721a26580eSLuigi Rizzo break; 21731a26580eSLuigi Rizzo 21741a26580eSLuigi Rizzo case NETMAP_TX_UNLOCK: 21751a26580eSLuigi Rizzo mtx_unlock(&na->tx_rings[queueid].q_lock); 21761a26580eSLuigi Rizzo break; 21771a26580eSLuigi Rizzo 21781a26580eSLuigi Rizzo case NETMAP_RX_LOCK: 21791a26580eSLuigi Rizzo mtx_lock(&na->rx_rings[queueid].q_lock); 21801a26580eSLuigi Rizzo break; 21811a26580eSLuigi Rizzo 21821a26580eSLuigi Rizzo case NETMAP_RX_UNLOCK: 21831a26580eSLuigi Rizzo mtx_unlock(&na->rx_rings[queueid].q_lock); 21841a26580eSLuigi Rizzo break; 21851a26580eSLuigi Rizzo } 21861a26580eSLuigi Rizzo } 21871a26580eSLuigi Rizzo 21881a26580eSLuigi Rizzo 21891a26580eSLuigi Rizzo /* 219068b8534bSLuigi Rizzo * Initialize a ``netmap_adapter`` object created by driver on attach. 219168b8534bSLuigi Rizzo * We allocate a block of memory with room for a struct netmap_adapter 219268b8534bSLuigi Rizzo * plus two sets of N+2 struct netmap_kring (where N is the number 219368b8534bSLuigi Rizzo * of hardware rings): 219468b8534bSLuigi Rizzo * krings 0..N-1 are for the hardware queues. 219568b8534bSLuigi Rizzo * kring N is for the host stack queue 219668b8534bSLuigi Rizzo * kring N+1 is only used for the selinfo for all queues. 219768b8534bSLuigi Rizzo * Return 0 on success, ENOMEM otherwise. 219864ae02c3SLuigi Rizzo * 21990bf88954SEd Maste * By default the receive and transmit adapter ring counts are both initialized 22000bf88954SEd Maste * to num_queues. na->num_tx_rings can be set for cards with different tx/rx 220124e57ec9SEd Maste * setups. 220268b8534bSLuigi Rizzo */ 220368b8534bSLuigi Rizzo int 2204ae10d1afSLuigi Rizzo netmap_attach(struct netmap_adapter *arg, int num_queues) 220568b8534bSLuigi Rizzo { 2206ae10d1afSLuigi Rizzo struct netmap_adapter *na = NULL; 2207ae10d1afSLuigi Rizzo struct ifnet *ifp = arg ? arg->ifp : NULL; 2208*f18be576SLuigi Rizzo int len; 220968b8534bSLuigi Rizzo 2210ae10d1afSLuigi Rizzo if (arg == NULL || ifp == NULL) 2211ae10d1afSLuigi Rizzo goto fail; 2212*f18be576SLuigi Rizzo len = nma_is_vp(arg) ? sizeof(*na) : sizeof(*na) * 2; 2213*f18be576SLuigi Rizzo na = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO); 2214ae10d1afSLuigi Rizzo if (na == NULL) 2215ae10d1afSLuigi Rizzo goto fail; 2216ae10d1afSLuigi Rizzo WNA(ifp) = na; 2217ae10d1afSLuigi Rizzo *na = *arg; /* copy everything, trust the driver to not pass junk */ 2218ae10d1afSLuigi Rizzo NETMAP_SET_CAPABLE(ifp); 2219d76bf4ffSLuigi Rizzo if (na->num_tx_rings == 0) 2220d76bf4ffSLuigi Rizzo na->num_tx_rings = num_queues; 2221d76bf4ffSLuigi Rizzo na->num_rx_rings = num_queues; 2222ae10d1afSLuigi Rizzo na->refcount = na->na_single = na->na_multi = 0; 2223ae10d1afSLuigi Rizzo /* Core lock initialized here, others after netmap_if_new. */ 2224ae10d1afSLuigi Rizzo mtx_init(&na->core_lock, "netmap core lock", MTX_NETWORK_LOCK, MTX_DEF); 2225f196ce38SLuigi Rizzo if (na->nm_lock == NULL) { 2226f196ce38SLuigi Rizzo ND("using default locks for %s", ifp->if_xname); 22271a26580eSLuigi Rizzo na->nm_lock = netmap_lock_wrapper; 2228f196ce38SLuigi Rizzo } 222964ae02c3SLuigi Rizzo #ifdef linux 2230*f18be576SLuigi Rizzo if (ifp->netdev_ops) { 2231*f18be576SLuigi Rizzo ND("netdev_ops %p", ifp->netdev_ops); 2232*f18be576SLuigi Rizzo /* prepare a clone of the netdev ops */ 2233*f18be576SLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28) 2234*f18be576SLuigi Rizzo na->nm_ndo.ndo_start_xmit = ifp->netdev_ops; 2235*f18be576SLuigi Rizzo #else 2236849bec0eSLuigi Rizzo na->nm_ndo = *ifp->netdev_ops; 2237*f18be576SLuigi Rizzo #endif 2238*f18be576SLuigi Rizzo } 223942a3a5bdSLuigi Rizzo na->nm_ndo.ndo_start_xmit = linux_netmap_start; 2240*f18be576SLuigi Rizzo #endif 2241*f18be576SLuigi Rizzo if (!nma_is_vp(arg)) 2242*f18be576SLuigi Rizzo netmap_attach_sw(ifp); 2243ae10d1afSLuigi Rizzo D("success for %s", ifp->if_xname); 2244ae10d1afSLuigi Rizzo return 0; 224568b8534bSLuigi Rizzo 2246ae10d1afSLuigi Rizzo fail: 2247ae10d1afSLuigi Rizzo D("fail, arg %p ifp %p na %p", arg, ifp, na); 2248849bec0eSLuigi Rizzo netmap_detach(ifp); 2249ae10d1afSLuigi Rizzo return (na ? EINVAL : ENOMEM); 225068b8534bSLuigi Rizzo } 225168b8534bSLuigi Rizzo 225268b8534bSLuigi Rizzo 225368b8534bSLuigi Rizzo /* 225468b8534bSLuigi Rizzo * Free the allocated memory linked to the given ``netmap_adapter`` 225568b8534bSLuigi Rizzo * object. 225668b8534bSLuigi Rizzo */ 225768b8534bSLuigi Rizzo void 225868b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp) 225968b8534bSLuigi Rizzo { 226068b8534bSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 226168b8534bSLuigi Rizzo 226268b8534bSLuigi Rizzo if (!na) 226368b8534bSLuigi Rizzo return; 226468b8534bSLuigi Rizzo 22652f70fca5SEd Maste mtx_destroy(&na->core_lock); 22662f70fca5SEd Maste 2267ae10d1afSLuigi Rizzo if (na->tx_rings) { /* XXX should not happen */ 2268ae10d1afSLuigi Rizzo D("freeing leftover tx_rings"); 2269ae10d1afSLuigi Rizzo free(na->tx_rings, M_DEVBUF); 2270ae10d1afSLuigi Rizzo } 227168b8534bSLuigi Rizzo bzero(na, sizeof(*na)); 2272d0c7b075SLuigi Rizzo WNA(ifp) = NULL; 227368b8534bSLuigi Rizzo free(na, M_DEVBUF); 227468b8534bSLuigi Rizzo } 227568b8534bSLuigi Rizzo 227668b8534bSLuigi Rizzo 2277*f18be576SLuigi Rizzo int 2278*f18be576SLuigi Rizzo nm_bdg_flush(struct nm_bdg_fwd *ft, int n, struct netmap_adapter *na, u_int ring_nr); 2279*f18be576SLuigi Rizzo 2280*f18be576SLuigi Rizzo /* we don't need to lock myself */ 2281*f18be576SLuigi Rizzo static int 2282*f18be576SLuigi Rizzo bdg_netmap_start(struct ifnet *ifp, struct mbuf *m) 2283*f18be576SLuigi Rizzo { 2284*f18be576SLuigi Rizzo struct netmap_adapter *na = SWNA(ifp); 2285*f18be576SLuigi Rizzo struct nm_bdg_fwd *ft = na->rx_rings[0].nkr_ft; 2286*f18be576SLuigi Rizzo char *buf = NMB(&na->rx_rings[0].ring->slot[0]); 2287*f18be576SLuigi Rizzo u_int len = MBUF_LEN(m); 2288*f18be576SLuigi Rizzo 2289*f18be576SLuigi Rizzo if (!na->na_bdg) /* SWNA is not configured to be attached */ 2290*f18be576SLuigi Rizzo return EBUSY; 2291*f18be576SLuigi Rizzo m_copydata(m, 0, len, buf); 2292*f18be576SLuigi Rizzo ft->ft_len = len; 2293*f18be576SLuigi Rizzo ft->buf = buf; 2294*f18be576SLuigi Rizzo nm_bdg_flush(ft, 1, na, 0); 2295*f18be576SLuigi Rizzo 2296*f18be576SLuigi Rizzo /* release the mbuf in either cases of success or failure. As an 2297*f18be576SLuigi Rizzo * alternative, put the mbuf in a free list and free the list 2298*f18be576SLuigi Rizzo * only when really necessary. 2299*f18be576SLuigi Rizzo */ 2300*f18be576SLuigi Rizzo m_freem(m); 2301*f18be576SLuigi Rizzo 2302*f18be576SLuigi Rizzo return (0); 2303*f18be576SLuigi Rizzo } 2304*f18be576SLuigi Rizzo 2305*f18be576SLuigi Rizzo 230668b8534bSLuigi Rizzo /* 230702ad4083SLuigi Rizzo * Intercept packets from the network stack and pass them 230802ad4083SLuigi Rizzo * to netmap as incoming packets on the 'software' ring. 230968b8534bSLuigi Rizzo * We are not locked when called. 231068b8534bSLuigi Rizzo */ 231168b8534bSLuigi Rizzo int 231268b8534bSLuigi Rizzo netmap_start(struct ifnet *ifp, struct mbuf *m) 231368b8534bSLuigi Rizzo { 231468b8534bSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 2315d76bf4ffSLuigi Rizzo struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings]; 23161a26580eSLuigi Rizzo u_int i, len = MBUF_LEN(m); 2317b3d53016SLuigi Rizzo u_int error = EBUSY, lim = kring->nkr_num_slots - 1; 231868b8534bSLuigi Rizzo struct netmap_slot *slot; 231968b8534bSLuigi Rizzo 232068b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_HOST) 232168b8534bSLuigi Rizzo D("%s packet %d len %d from the stack", ifp->if_xname, 232268b8534bSLuigi Rizzo kring->nr_hwcur + kring->nr_hwavail, len); 2323849bec0eSLuigi Rizzo if (len > NETMAP_BUF_SIZE) { /* too long for us */ 2324849bec0eSLuigi Rizzo D("%s from_host, drop packet size %d > %d", ifp->if_xname, 2325849bec0eSLuigi Rizzo len, NETMAP_BUF_SIZE); 2326849bec0eSLuigi Rizzo m_freem(m); 2327849bec0eSLuigi Rizzo return EINVAL; 2328849bec0eSLuigi Rizzo } 2329*f18be576SLuigi Rizzo if (na->na_bdg) 2330*f18be576SLuigi Rizzo return bdg_netmap_start(ifp, m); 2331*f18be576SLuigi Rizzo 23321a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_LOCK, 0); 233302ad4083SLuigi Rizzo if (kring->nr_hwavail >= lim) { 23345b248374SLuigi Rizzo if (netmap_verbose) 233568b8534bSLuigi Rizzo D("stack ring %s full\n", ifp->if_xname); 233668b8534bSLuigi Rizzo goto done; /* no space */ 233768b8534bSLuigi Rizzo } 233868b8534bSLuigi Rizzo 233968b8534bSLuigi Rizzo /* compute the insert position */ 234068b8534bSLuigi Rizzo i = kring->nr_hwcur + kring->nr_hwavail; 234102ad4083SLuigi Rizzo if (i > lim) 234202ad4083SLuigi Rizzo i -= lim + 1; 234368b8534bSLuigi Rizzo slot = &kring->ring->slot[i]; 234468b8534bSLuigi Rizzo m_copydata(m, 0, len, NMB(slot)); 234568b8534bSLuigi Rizzo slot->len = len; 2346091fd0abSLuigi Rizzo slot->flags = kring->nkr_slot_flags; 234768b8534bSLuigi Rizzo kring->nr_hwavail++; 234868b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_HOST) 2349d76bf4ffSLuigi Rizzo D("wake up host ring %s %d", na->ifp->if_xname, na->num_rx_rings); 235068b8534bSLuigi Rizzo selwakeuppri(&kring->si, PI_NET); 235168b8534bSLuigi Rizzo error = 0; 235268b8534bSLuigi Rizzo done: 23531a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0); 235468b8534bSLuigi Rizzo 235568b8534bSLuigi Rizzo /* release the mbuf in either cases of success or failure. As an 235668b8534bSLuigi Rizzo * alternative, put the mbuf in a free list and free the list 235768b8534bSLuigi Rizzo * only when really necessary. 235868b8534bSLuigi Rizzo */ 235968b8534bSLuigi Rizzo m_freem(m); 236068b8534bSLuigi Rizzo 236168b8534bSLuigi Rizzo return (error); 236268b8534bSLuigi Rizzo } 236368b8534bSLuigi Rizzo 236468b8534bSLuigi Rizzo 236568b8534bSLuigi Rizzo /* 236668b8534bSLuigi Rizzo * netmap_reset() is called by the driver routines when reinitializing 236768b8534bSLuigi Rizzo * a ring. The driver is in charge of locking to protect the kring. 236868b8534bSLuigi Rizzo * If netmap mode is not set just return NULL. 236968b8534bSLuigi Rizzo */ 237068b8534bSLuigi Rizzo struct netmap_slot * 237168b8534bSLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, int n, 237268b8534bSLuigi Rizzo u_int new_cur) 237368b8534bSLuigi Rizzo { 237468b8534bSLuigi Rizzo struct netmap_kring *kring; 2375506cc70cSLuigi Rizzo int new_hwofs, lim; 237668b8534bSLuigi Rizzo 237768b8534bSLuigi Rizzo if (na == NULL) 237868b8534bSLuigi Rizzo return NULL; /* no netmap support here */ 237968b8534bSLuigi Rizzo if (!(na->ifp->if_capenable & IFCAP_NETMAP)) 238068b8534bSLuigi Rizzo return NULL; /* nothing to reinitialize */ 238168b8534bSLuigi Rizzo 238264ae02c3SLuigi Rizzo if (tx == NR_TX) { 23838241616dSLuigi Rizzo if (n >= na->num_tx_rings) 23848241616dSLuigi Rizzo return NULL; 238564ae02c3SLuigi Rizzo kring = na->tx_rings + n; 2386506cc70cSLuigi Rizzo new_hwofs = kring->nr_hwcur - new_cur; 238764ae02c3SLuigi Rizzo } else { 23888241616dSLuigi Rizzo if (n >= na->num_rx_rings) 23898241616dSLuigi Rizzo return NULL; 239064ae02c3SLuigi Rizzo kring = na->rx_rings + n; 2391506cc70cSLuigi Rizzo new_hwofs = kring->nr_hwcur + kring->nr_hwavail - new_cur; 239264ae02c3SLuigi Rizzo } 239364ae02c3SLuigi Rizzo lim = kring->nkr_num_slots - 1; 2394506cc70cSLuigi Rizzo if (new_hwofs > lim) 2395506cc70cSLuigi Rizzo new_hwofs -= lim + 1; 2396506cc70cSLuigi Rizzo 2397506cc70cSLuigi Rizzo /* Alwayws set the new offset value and realign the ring. */ 2398506cc70cSLuigi Rizzo kring->nkr_hwofs = new_hwofs; 2399506cc70cSLuigi Rizzo if (tx == NR_TX) 2400506cc70cSLuigi Rizzo kring->nr_hwavail = kring->nkr_num_slots - 1; 24018241616dSLuigi Rizzo ND(10, "new hwofs %d on %s %s[%d]", 2402506cc70cSLuigi Rizzo kring->nkr_hwofs, na->ifp->if_xname, 2403506cc70cSLuigi Rizzo tx == NR_TX ? "TX" : "RX", n); 2404506cc70cSLuigi Rizzo 2405f196ce38SLuigi Rizzo #if 0 // def linux 2406f196ce38SLuigi Rizzo /* XXX check that the mappings are correct */ 2407f196ce38SLuigi Rizzo /* need ring_nr, adapter->pdev, direction */ 2408f196ce38SLuigi Rizzo buffer_info->dma = dma_map_single(&pdev->dev, addr, adapter->rx_buffer_len, DMA_FROM_DEVICE); 2409f196ce38SLuigi Rizzo if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) { 2410f196ce38SLuigi Rizzo D("error mapping rx netmap buffer %d", i); 2411f196ce38SLuigi Rizzo // XXX fix error handling 2412f196ce38SLuigi Rizzo } 2413f196ce38SLuigi Rizzo 2414f196ce38SLuigi Rizzo #endif /* linux */ 241568b8534bSLuigi Rizzo /* 241664ae02c3SLuigi Rizzo * Wakeup on the individual and global lock 2417506cc70cSLuigi Rizzo * We do the wakeup here, but the ring is not yet reconfigured. 2418506cc70cSLuigi Rizzo * However, we are under lock so there are no races. 241968b8534bSLuigi Rizzo */ 242068b8534bSLuigi Rizzo selwakeuppri(&kring->si, PI_NET); 242164ae02c3SLuigi Rizzo selwakeuppri(tx == NR_TX ? &na->tx_si : &na->rx_si, PI_NET); 242268b8534bSLuigi Rizzo return kring->ring->slot; 242368b8534bSLuigi Rizzo } 242468b8534bSLuigi Rizzo 242568b8534bSLuigi Rizzo 2426*f18be576SLuigi Rizzo /* returns the next position in the ring */ 2427*f18be576SLuigi Rizzo static int 2428*f18be576SLuigi Rizzo nm_bdg_preflush(struct netmap_adapter *na, u_int ring_nr, 2429*f18be576SLuigi Rizzo struct netmap_kring *kring, u_int end) 2430*f18be576SLuigi Rizzo { 2431*f18be576SLuigi Rizzo struct netmap_ring *ring = kring->ring; 2432*f18be576SLuigi Rizzo struct nm_bdg_fwd *ft = kring->nkr_ft; 2433*f18be576SLuigi Rizzo u_int j = kring->nr_hwcur, lim = kring->nkr_num_slots - 1; 2434*f18be576SLuigi Rizzo u_int ft_i = 0; /* start from 0 */ 2435*f18be576SLuigi Rizzo 2436*f18be576SLuigi Rizzo for (; likely(j != end); j = unlikely(j == lim) ? 0 : j+1) { 2437*f18be576SLuigi Rizzo struct netmap_slot *slot = &ring->slot[j]; 2438*f18be576SLuigi Rizzo int len = ft[ft_i].ft_len = slot->len; 2439*f18be576SLuigi Rizzo char *buf = ft[ft_i].buf = NMB(slot); 2440*f18be576SLuigi Rizzo 2441*f18be576SLuigi Rizzo prefetch(buf); 2442*f18be576SLuigi Rizzo if (unlikely(len < 14)) 2443*f18be576SLuigi Rizzo continue; 2444*f18be576SLuigi Rizzo if (unlikely(++ft_i == netmap_bridge)) 2445*f18be576SLuigi Rizzo ft_i = nm_bdg_flush(ft, ft_i, na, ring_nr); 2446*f18be576SLuigi Rizzo } 2447*f18be576SLuigi Rizzo if (ft_i) 2448*f18be576SLuigi Rizzo ft_i = nm_bdg_flush(ft, ft_i, na, ring_nr); 2449*f18be576SLuigi Rizzo return j; 2450*f18be576SLuigi Rizzo } 2451*f18be576SLuigi Rizzo 2452*f18be576SLuigi Rizzo 2453*f18be576SLuigi Rizzo /* 2454*f18be576SLuigi Rizzo * Pass packets from nic to the bridge. Must be called with 2455*f18be576SLuigi Rizzo * proper locks on the source interface. 2456*f18be576SLuigi Rizzo * Note, no user process can access this NIC so we can ignore 2457*f18be576SLuigi Rizzo * the info in the 'ring'. 2458*f18be576SLuigi Rizzo */ 2459*f18be576SLuigi Rizzo static void 2460*f18be576SLuigi Rizzo netmap_nic_to_bdg(struct ifnet *ifp, u_int ring_nr) 2461*f18be576SLuigi Rizzo { 2462*f18be576SLuigi Rizzo struct netmap_adapter *na = NA(ifp); 2463*f18be576SLuigi Rizzo struct netmap_kring *kring = &na->rx_rings[ring_nr]; 2464*f18be576SLuigi Rizzo struct netmap_ring *ring = kring->ring; 2465*f18be576SLuigi Rizzo int j, k, lim = kring->nkr_num_slots - 1; 2466*f18be576SLuigi Rizzo 2467*f18be576SLuigi Rizzo /* fetch packets that have arrived */ 2468*f18be576SLuigi Rizzo na->nm_rxsync(ifp, ring_nr, 0); 2469*f18be576SLuigi Rizzo /* XXX we don't count reserved, but it should be 0 */ 2470*f18be576SLuigi Rizzo j = kring->nr_hwcur; 2471*f18be576SLuigi Rizzo k = j + kring->nr_hwavail; 2472*f18be576SLuigi Rizzo if (k > lim) 2473*f18be576SLuigi Rizzo k -= lim + 1; 2474*f18be576SLuigi Rizzo if (k == j && netmap_verbose) { 2475*f18be576SLuigi Rizzo D("how strange, interrupt with no packets on %s", 2476*f18be576SLuigi Rizzo ifp->if_xname); 2477*f18be576SLuigi Rizzo return; 2478*f18be576SLuigi Rizzo } 2479*f18be576SLuigi Rizzo 2480*f18be576SLuigi Rizzo j = nm_bdg_preflush(na, ring_nr, kring, k); 2481*f18be576SLuigi Rizzo 2482*f18be576SLuigi Rizzo /* we consume everything, but we cannot update kring directly 2483*f18be576SLuigi Rizzo * because the nic may have destroyed the info in the NIC ring. 2484*f18be576SLuigi Rizzo * So we need to call rxsync again to restore it. 2485*f18be576SLuigi Rizzo */ 2486*f18be576SLuigi Rizzo ring->cur = j; 2487*f18be576SLuigi Rizzo ring->avail = 0; 2488*f18be576SLuigi Rizzo na->nm_rxsync(ifp, ring_nr, 0); 2489*f18be576SLuigi Rizzo return; 2490*f18be576SLuigi Rizzo } 2491*f18be576SLuigi Rizzo 2492*f18be576SLuigi Rizzo 249368b8534bSLuigi Rizzo /* 24941a26580eSLuigi Rizzo * Default functions to handle rx/tx interrupts 24951a26580eSLuigi Rizzo * we have 4 cases: 24961a26580eSLuigi Rizzo * 1 ring, single lock: 24971a26580eSLuigi Rizzo * lock(core); wake(i=0); unlock(core) 24981a26580eSLuigi Rizzo * N rings, single lock: 24991a26580eSLuigi Rizzo * lock(core); wake(i); wake(N+1) unlock(core) 25001a26580eSLuigi Rizzo * 1 ring, separate locks: (i=0) 25011a26580eSLuigi Rizzo * lock(i); wake(i); unlock(i) 25021a26580eSLuigi Rizzo * N rings, separate locks: 25031a26580eSLuigi Rizzo * lock(i); wake(i); unlock(i); lock(core) wake(N+1) unlock(core) 250464ae02c3SLuigi Rizzo * work_done is non-null on the RX path. 2505849bec0eSLuigi Rizzo * 2506849bec0eSLuigi Rizzo * The 'q' argument also includes flag to tell whether the queue is 2507849bec0eSLuigi Rizzo * already locked on enter, and whether it should remain locked on exit. 2508849bec0eSLuigi Rizzo * This helps adapting to different defaults in drivers and OSes. 25091a26580eSLuigi Rizzo */ 2510babc7c12SLuigi Rizzo int 2511babc7c12SLuigi Rizzo netmap_rx_irq(struct ifnet *ifp, int q, int *work_done) 25121a26580eSLuigi Rizzo { 25131a26580eSLuigi Rizzo struct netmap_adapter *na; 25141a26580eSLuigi Rizzo struct netmap_kring *r; 251564ae02c3SLuigi Rizzo NM_SELINFO_T *main_wq; 2516*f18be576SLuigi Rizzo int locktype, unlocktype, nic_to_bridge, lock; 25171a26580eSLuigi Rizzo 25181a26580eSLuigi Rizzo if (!(ifp->if_capenable & IFCAP_NETMAP)) 25191a26580eSLuigi Rizzo return 0; 2520849bec0eSLuigi Rizzo 2521849bec0eSLuigi Rizzo lock = q & (NETMAP_LOCKED_ENTER | NETMAP_LOCKED_EXIT); 2522849bec0eSLuigi Rizzo q = q & NETMAP_RING_MASK; 2523849bec0eSLuigi Rizzo 25248241616dSLuigi Rizzo ND(5, "received %s queue %d", work_done ? "RX" : "TX" , q); 25251a26580eSLuigi Rizzo na = NA(ifp); 25268241616dSLuigi Rizzo if (na->na_flags & NAF_SKIP_INTR) { 25278241616dSLuigi Rizzo ND("use regular interrupt"); 25288241616dSLuigi Rizzo return 0; 25298241616dSLuigi Rizzo } 25308241616dSLuigi Rizzo 253164ae02c3SLuigi Rizzo if (work_done) { /* RX path */ 25328241616dSLuigi Rizzo if (q >= na->num_rx_rings) 2533849bec0eSLuigi Rizzo return 0; // not a physical queue 253464ae02c3SLuigi Rizzo r = na->rx_rings + q; 253564ae02c3SLuigi Rizzo r->nr_kflags |= NKR_PENDINTR; 2536d76bf4ffSLuigi Rizzo main_wq = (na->num_rx_rings > 1) ? &na->rx_si : NULL; 2537*f18be576SLuigi Rizzo /* set a flag if the NIC is attached to a VALE switch */ 2538*f18be576SLuigi Rizzo nic_to_bridge = (na->na_bdg != NULL); 2539849bec0eSLuigi Rizzo locktype = NETMAP_RX_LOCK; 2540849bec0eSLuigi Rizzo unlocktype = NETMAP_RX_UNLOCK; 2541849bec0eSLuigi Rizzo } else { /* TX path */ 25428241616dSLuigi Rizzo if (q >= na->num_tx_rings) 2543849bec0eSLuigi Rizzo return 0; // not a physical queue 254464ae02c3SLuigi Rizzo r = na->tx_rings + q; 2545d76bf4ffSLuigi Rizzo main_wq = (na->num_tx_rings > 1) ? &na->tx_si : NULL; 254664ae02c3SLuigi Rizzo work_done = &q; /* dummy */ 2547*f18be576SLuigi Rizzo nic_to_bridge = 0; 2548849bec0eSLuigi Rizzo locktype = NETMAP_TX_LOCK; 2549849bec0eSLuigi Rizzo unlocktype = NETMAP_TX_UNLOCK; 255064ae02c3SLuigi Rizzo } 25511a26580eSLuigi Rizzo if (na->separate_locks) { 2552849bec0eSLuigi Rizzo if (!(lock & NETMAP_LOCKED_ENTER)) 2553849bec0eSLuigi Rizzo na->nm_lock(ifp, locktype, q); 2554*f18be576SLuigi Rizzo /* If a NIC is attached to a bridge, flush packets 2555*f18be576SLuigi Rizzo * (and no need to wakeup anyone). Otherwise, wakeup 2556*f18be576SLuigi Rizzo * possible processes waiting for packets. 2557*f18be576SLuigi Rizzo */ 2558*f18be576SLuigi Rizzo if (nic_to_bridge) 2559*f18be576SLuigi Rizzo netmap_nic_to_bdg(ifp, q); 2560*f18be576SLuigi Rizzo else 256164ae02c3SLuigi Rizzo selwakeuppri(&r->si, PI_NET); 2562849bec0eSLuigi Rizzo na->nm_lock(ifp, unlocktype, q); 2563*f18be576SLuigi Rizzo if (main_wq && !nic_to_bridge) { 2564849bec0eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_LOCK, 0); 256564ae02c3SLuigi Rizzo selwakeuppri(main_wq, PI_NET); 2566849bec0eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0); 25671a26580eSLuigi Rizzo } 2568849bec0eSLuigi Rizzo /* lock the queue again if requested */ 2569849bec0eSLuigi Rizzo if (lock & NETMAP_LOCKED_EXIT) 2570849bec0eSLuigi Rizzo na->nm_lock(ifp, locktype, q); 25711a26580eSLuigi Rizzo } else { 2572849bec0eSLuigi Rizzo if (!(lock & NETMAP_LOCKED_ENTER)) 2573849bec0eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_LOCK, 0); 2574*f18be576SLuigi Rizzo if (nic_to_bridge) 2575*f18be576SLuigi Rizzo netmap_nic_to_bdg(ifp, q); 2576*f18be576SLuigi Rizzo else { 257764ae02c3SLuigi Rizzo selwakeuppri(&r->si, PI_NET); 257864ae02c3SLuigi Rizzo if (main_wq) 257964ae02c3SLuigi Rizzo selwakeuppri(main_wq, PI_NET); 2580*f18be576SLuigi Rizzo } 2581849bec0eSLuigi Rizzo if (!(lock & NETMAP_LOCKED_EXIT)) 2582849bec0eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0); 25831a26580eSLuigi Rizzo } 25841a26580eSLuigi Rizzo *work_done = 1; /* do not fire napi again */ 25851a26580eSLuigi Rizzo return 1; 25861a26580eSLuigi Rizzo } 25871a26580eSLuigi Rizzo 258864ae02c3SLuigi Rizzo 258901c7d25fSLuigi Rizzo #ifdef linux /* linux-specific routines */ 259001c7d25fSLuigi Rizzo 2591*f18be576SLuigi Rizzo 259201c7d25fSLuigi Rizzo /* 259301c7d25fSLuigi Rizzo * Remap linux arguments into the FreeBSD call. 259401c7d25fSLuigi Rizzo * - pwait is the poll table, passed as 'dev'; 259501c7d25fSLuigi Rizzo * If pwait == NULL someone else already woke up before. We can report 259601c7d25fSLuigi Rizzo * events but they are filtered upstream. 259701c7d25fSLuigi Rizzo * If pwait != NULL, then pwait->key contains the list of events. 259801c7d25fSLuigi Rizzo * - events is computed from pwait as above. 259901c7d25fSLuigi Rizzo * - file is passed as 'td'; 260001c7d25fSLuigi Rizzo */ 260101c7d25fSLuigi Rizzo static u_int 260201c7d25fSLuigi Rizzo linux_netmap_poll(struct file * file, struct poll_table_struct *pwait) 260301c7d25fSLuigi Rizzo { 2604849bec0eSLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28) 2605849bec0eSLuigi Rizzo int events = POLLIN | POLLOUT; /* XXX maybe... */ 2606849bec0eSLuigi Rizzo #elif LINUX_VERSION_CODE < KERNEL_VERSION(3,4,0) 260701c7d25fSLuigi Rizzo int events = pwait ? pwait->key : POLLIN | POLLOUT; 260801c7d25fSLuigi Rizzo #else /* in 3.4.0 field 'key' was renamed to '_key' */ 260901c7d25fSLuigi Rizzo int events = pwait ? pwait->_key : POLLIN | POLLOUT; 261001c7d25fSLuigi Rizzo #endif 261101c7d25fSLuigi Rizzo return netmap_poll((void *)pwait, events, (void *)file); 261201c7d25fSLuigi Rizzo } 261301c7d25fSLuigi Rizzo 2614*f18be576SLuigi Rizzo 261501c7d25fSLuigi Rizzo static int 261642a3a5bdSLuigi Rizzo linux_netmap_mmap(struct file *f, struct vm_area_struct *vma) 261701c7d25fSLuigi Rizzo { 261801c7d25fSLuigi Rizzo int lut_skip, i, j; 261901c7d25fSLuigi Rizzo int user_skip = 0; 262001c7d25fSLuigi Rizzo struct lut_entry *l_entry; 26218241616dSLuigi Rizzo int error = 0; 26228241616dSLuigi Rizzo unsigned long off, tomap; 262301c7d25fSLuigi Rizzo /* 262401c7d25fSLuigi Rizzo * vma->vm_start: start of mapping user address space 262501c7d25fSLuigi Rizzo * vma->vm_end: end of the mapping user address space 26268241616dSLuigi Rizzo * vma->vm_pfoff: offset of first page in the device 262701c7d25fSLuigi Rizzo */ 262801c7d25fSLuigi Rizzo 262901c7d25fSLuigi Rizzo // XXX security checks 263001c7d25fSLuigi Rizzo 26318241616dSLuigi Rizzo error = netmap_get_memory(f->private_data); 26328241616dSLuigi Rizzo ND("get_memory returned %d", error); 26338241616dSLuigi Rizzo if (error) 26348241616dSLuigi Rizzo return -error; 26358241616dSLuigi Rizzo 26368241616dSLuigi Rizzo off = vma->vm_pgoff << PAGE_SHIFT; /* offset in bytes */ 26378241616dSLuigi Rizzo tomap = vma->vm_end - vma->vm_start; 26388241616dSLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { /* loop through obj_pools */ 26398241616dSLuigi Rizzo const struct netmap_obj_pool *p = &nm_mem.pools[i]; 264001c7d25fSLuigi Rizzo /* 264101c7d25fSLuigi Rizzo * In each pool memory is allocated in clusters 264201c7d25fSLuigi Rizzo * of size _clustsize, each containing clustentries 264301c7d25fSLuigi Rizzo * entries. For each object k we already store the 26448241616dSLuigi Rizzo * vtophys mapping in lut[k] so we use that, scanning 264501c7d25fSLuigi Rizzo * the lut[] array in steps of clustentries, 264601c7d25fSLuigi Rizzo * and we map each cluster (not individual pages, 2647849bec0eSLuigi Rizzo * it would be overkill -- XXX slow ? 20130415). 264801c7d25fSLuigi Rizzo */ 26498241616dSLuigi Rizzo 26508241616dSLuigi Rizzo /* 26518241616dSLuigi Rizzo * We interpret vm_pgoff as an offset into the whole 26528241616dSLuigi Rizzo * netmap memory, as if all clusters where contiguous. 26538241616dSLuigi Rizzo */ 26548241616dSLuigi Rizzo for (lut_skip = 0, j = 0; j < p->_numclusters; j++, lut_skip += p->clustentries) { 26558241616dSLuigi Rizzo unsigned long paddr, mapsize; 26568241616dSLuigi Rizzo if (p->_clustsize <= off) { 26578241616dSLuigi Rizzo off -= p->_clustsize; 26588241616dSLuigi Rizzo continue; 26598241616dSLuigi Rizzo } 26608241616dSLuigi Rizzo l_entry = &p->lut[lut_skip]; /* first obj in the cluster */ 26618241616dSLuigi Rizzo paddr = l_entry->paddr + off; 26628241616dSLuigi Rizzo mapsize = p->_clustsize - off; 26638241616dSLuigi Rizzo off = 0; 26648241616dSLuigi Rizzo if (mapsize > tomap) 26658241616dSLuigi Rizzo mapsize = tomap; 26668241616dSLuigi Rizzo ND("remap_pfn_range(%lx, %lx, %lx)", 26678241616dSLuigi Rizzo vma->vm_start + user_skip, 26688241616dSLuigi Rizzo paddr >> PAGE_SHIFT, mapsize); 266901c7d25fSLuigi Rizzo if (remap_pfn_range(vma, vma->vm_start + user_skip, 26708241616dSLuigi Rizzo paddr >> PAGE_SHIFT, mapsize, 267101c7d25fSLuigi Rizzo vma->vm_page_prot)) 267201c7d25fSLuigi Rizzo return -EAGAIN; // XXX check return value 26738241616dSLuigi Rizzo user_skip += mapsize; 26748241616dSLuigi Rizzo tomap -= mapsize; 26758241616dSLuigi Rizzo if (tomap == 0) 26768241616dSLuigi Rizzo goto done; 267701c7d25fSLuigi Rizzo } 267801c7d25fSLuigi Rizzo } 26798241616dSLuigi Rizzo done: 268001c7d25fSLuigi Rizzo 268101c7d25fSLuigi Rizzo return 0; 268201c7d25fSLuigi Rizzo } 268301c7d25fSLuigi Rizzo 2684*f18be576SLuigi Rizzo 268501c7d25fSLuigi Rizzo static netdev_tx_t 268642a3a5bdSLuigi Rizzo linux_netmap_start(struct sk_buff *skb, struct net_device *dev) 268701c7d25fSLuigi Rizzo { 268801c7d25fSLuigi Rizzo netmap_start(dev, skb); 268901c7d25fSLuigi Rizzo return (NETDEV_TX_OK); 269001c7d25fSLuigi Rizzo } 269101c7d25fSLuigi Rizzo 269201c7d25fSLuigi Rizzo 26930b8ed8e0SLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37) // XXX was 38 269401c7d25fSLuigi Rizzo #define LIN_IOCTL_NAME .ioctl 269501c7d25fSLuigi Rizzo int 269601c7d25fSLuigi Rizzo linux_netmap_ioctl(struct inode *inode, struct file *file, u_int cmd, u_long data /* arg */) 269701c7d25fSLuigi Rizzo #else 269801c7d25fSLuigi Rizzo #define LIN_IOCTL_NAME .unlocked_ioctl 269901c7d25fSLuigi Rizzo long 270001c7d25fSLuigi Rizzo linux_netmap_ioctl(struct file *file, u_int cmd, u_long data /* arg */) 270101c7d25fSLuigi Rizzo #endif 270201c7d25fSLuigi Rizzo { 270301c7d25fSLuigi Rizzo int ret; 270401c7d25fSLuigi Rizzo struct nmreq nmr; 270501c7d25fSLuigi Rizzo bzero(&nmr, sizeof(nmr)); 270601c7d25fSLuigi Rizzo 270701c7d25fSLuigi Rizzo if (data && copy_from_user(&nmr, (void *)data, sizeof(nmr) ) != 0) 270801c7d25fSLuigi Rizzo return -EFAULT; 270901c7d25fSLuigi Rizzo ret = netmap_ioctl(NULL, cmd, (caddr_t)&nmr, 0, (void *)file); 271001c7d25fSLuigi Rizzo if (data && copy_to_user((void*)data, &nmr, sizeof(nmr) ) != 0) 271101c7d25fSLuigi Rizzo return -EFAULT; 271201c7d25fSLuigi Rizzo return -ret; 271301c7d25fSLuigi Rizzo } 271401c7d25fSLuigi Rizzo 271501c7d25fSLuigi Rizzo 271601c7d25fSLuigi Rizzo static int 27170b8ed8e0SLuigi Rizzo netmap_release(struct inode *inode, struct file *file) 271801c7d25fSLuigi Rizzo { 27190b8ed8e0SLuigi Rizzo (void)inode; /* UNUSED */ 272001c7d25fSLuigi Rizzo if (file->private_data) 272101c7d25fSLuigi Rizzo netmap_dtor(file->private_data); 272201c7d25fSLuigi Rizzo return (0); 272301c7d25fSLuigi Rizzo } 272401c7d25fSLuigi Rizzo 2725*f18be576SLuigi Rizzo 27268241616dSLuigi Rizzo static int 27278241616dSLuigi Rizzo linux_netmap_open(struct inode *inode, struct file *file) 27288241616dSLuigi Rizzo { 27298241616dSLuigi Rizzo struct netmap_priv_d *priv; 27308241616dSLuigi Rizzo (void)inode; /* UNUSED */ 27318241616dSLuigi Rizzo 27328241616dSLuigi Rizzo priv = malloc(sizeof(struct netmap_priv_d), M_DEVBUF, 27338241616dSLuigi Rizzo M_NOWAIT | M_ZERO); 27348241616dSLuigi Rizzo if (priv == NULL) 27358241616dSLuigi Rizzo return -ENOMEM; 27368241616dSLuigi Rizzo 27378241616dSLuigi Rizzo file->private_data = priv; 27388241616dSLuigi Rizzo 27398241616dSLuigi Rizzo return (0); 27408241616dSLuigi Rizzo } 274101c7d25fSLuigi Rizzo 2742*f18be576SLuigi Rizzo 274301c7d25fSLuigi Rizzo static struct file_operations netmap_fops = { 2744*f18be576SLuigi Rizzo .owner = THIS_MODULE, 27458241616dSLuigi Rizzo .open = linux_netmap_open, 274642a3a5bdSLuigi Rizzo .mmap = linux_netmap_mmap, 274701c7d25fSLuigi Rizzo LIN_IOCTL_NAME = linux_netmap_ioctl, 274801c7d25fSLuigi Rizzo .poll = linux_netmap_poll, 274901c7d25fSLuigi Rizzo .release = netmap_release, 275001c7d25fSLuigi Rizzo }; 275101c7d25fSLuigi Rizzo 2752*f18be576SLuigi Rizzo 275301c7d25fSLuigi Rizzo static struct miscdevice netmap_cdevsw = { /* same name as FreeBSD */ 275401c7d25fSLuigi Rizzo MISC_DYNAMIC_MINOR, 275501c7d25fSLuigi Rizzo "netmap", 275601c7d25fSLuigi Rizzo &netmap_fops, 275701c7d25fSLuigi Rizzo }; 275801c7d25fSLuigi Rizzo 275901c7d25fSLuigi Rizzo static int netmap_init(void); 276001c7d25fSLuigi Rizzo static void netmap_fini(void); 276101c7d25fSLuigi Rizzo 2762*f18be576SLuigi Rizzo 276342a3a5bdSLuigi Rizzo /* Errors have negative values on linux */ 276442a3a5bdSLuigi Rizzo static int linux_netmap_init(void) 276542a3a5bdSLuigi Rizzo { 276642a3a5bdSLuigi Rizzo return -netmap_init(); 276742a3a5bdSLuigi Rizzo } 276842a3a5bdSLuigi Rizzo 276942a3a5bdSLuigi Rizzo module_init(linux_netmap_init); 277001c7d25fSLuigi Rizzo module_exit(netmap_fini); 277101c7d25fSLuigi Rizzo /* export certain symbols to other modules */ 277201c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_attach); // driver attach routines 277301c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_detach); // driver detach routines 277401c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_ring_reinit); // ring init on error 277501c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_buffer_lut); 277601c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_total_buffers); // index check 277701c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_buffer_base); 277801c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_reset); // ring init routines 277901c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_buf_size); 278001c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_rx_irq); // default irq handler 278101c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_no_pendintr); // XXX mitigation - should go away 2782*f18be576SLuigi Rizzo EXPORT_SYMBOL(netmap_bdg_ctl); // bridge configuration routine 2783*f18be576SLuigi Rizzo EXPORT_SYMBOL(netmap_bdg_learning); // the default lookup function 278401c7d25fSLuigi Rizzo 278501c7d25fSLuigi Rizzo 278601c7d25fSLuigi Rizzo MODULE_AUTHOR("http://info.iet.unipi.it/~luigi/netmap/"); 278701c7d25fSLuigi Rizzo MODULE_DESCRIPTION("The netmap packet I/O framework"); 278801c7d25fSLuigi Rizzo MODULE_LICENSE("Dual BSD/GPL"); /* the code here is all BSD. */ 278901c7d25fSLuigi Rizzo 279001c7d25fSLuigi Rizzo #else /* __FreeBSD__ */ 279101c7d25fSLuigi Rizzo 2792*f18be576SLuigi Rizzo 2793babc7c12SLuigi Rizzo static struct cdevsw netmap_cdevsw = { 2794babc7c12SLuigi Rizzo .d_version = D_VERSION, 2795babc7c12SLuigi Rizzo .d_name = "netmap", 27968241616dSLuigi Rizzo .d_open = netmap_open, 2797babc7c12SLuigi Rizzo .d_mmap = netmap_mmap, 27988241616dSLuigi Rizzo .d_mmap_single = netmap_mmap_single, 2799babc7c12SLuigi Rizzo .d_ioctl = netmap_ioctl, 2800babc7c12SLuigi Rizzo .d_poll = netmap_poll, 28018241616dSLuigi Rizzo .d_close = netmap_close, 2802babc7c12SLuigi Rizzo }; 280301c7d25fSLuigi Rizzo #endif /* __FreeBSD__ */ 2804babc7c12SLuigi Rizzo 2805f196ce38SLuigi Rizzo #ifdef NM_BRIDGE 2806f196ce38SLuigi Rizzo /* 2807f196ce38SLuigi Rizzo *---- support for virtual bridge ----- 2808f196ce38SLuigi Rizzo */ 2809f196ce38SLuigi Rizzo 2810f196ce38SLuigi Rizzo /* ----- FreeBSD if_bridge hash function ------- */ 2811f196ce38SLuigi Rizzo 2812f196ce38SLuigi Rizzo /* 2813f196ce38SLuigi Rizzo * The following hash function is adapted from "Hash Functions" by Bob Jenkins 2814f196ce38SLuigi Rizzo * ("Algorithm Alley", Dr. Dobbs Journal, September 1997). 2815f196ce38SLuigi Rizzo * 2816f196ce38SLuigi Rizzo * http://www.burtleburtle.net/bob/hash/spooky.html 2817f196ce38SLuigi Rizzo */ 2818f196ce38SLuigi Rizzo #define mix(a, b, c) \ 2819f196ce38SLuigi Rizzo do { \ 2820f196ce38SLuigi Rizzo a -= b; a -= c; a ^= (c >> 13); \ 2821f196ce38SLuigi Rizzo b -= c; b -= a; b ^= (a << 8); \ 2822f196ce38SLuigi Rizzo c -= a; c -= b; c ^= (b >> 13); \ 2823f196ce38SLuigi Rizzo a -= b; a -= c; a ^= (c >> 12); \ 2824f196ce38SLuigi Rizzo b -= c; b -= a; b ^= (a << 16); \ 2825f196ce38SLuigi Rizzo c -= a; c -= b; c ^= (b >> 5); \ 2826f196ce38SLuigi Rizzo a -= b; a -= c; a ^= (c >> 3); \ 2827f196ce38SLuigi Rizzo b -= c; b -= a; b ^= (a << 10); \ 2828f196ce38SLuigi Rizzo c -= a; c -= b; c ^= (b >> 15); \ 2829f196ce38SLuigi Rizzo } while (/*CONSTCOND*/0) 2830f196ce38SLuigi Rizzo 2831f196ce38SLuigi Rizzo static __inline uint32_t 2832f196ce38SLuigi Rizzo nm_bridge_rthash(const uint8_t *addr) 2833f196ce38SLuigi Rizzo { 2834f196ce38SLuigi Rizzo uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = 0; // hask key 2835f196ce38SLuigi Rizzo 2836f196ce38SLuigi Rizzo b += addr[5] << 8; 2837f196ce38SLuigi Rizzo b += addr[4]; 2838f196ce38SLuigi Rizzo a += addr[3] << 24; 2839f196ce38SLuigi Rizzo a += addr[2] << 16; 2840f196ce38SLuigi Rizzo a += addr[1] << 8; 2841f196ce38SLuigi Rizzo a += addr[0]; 2842f196ce38SLuigi Rizzo 2843f196ce38SLuigi Rizzo mix(a, b, c); 2844f196ce38SLuigi Rizzo #define BRIDGE_RTHASH_MASK (NM_BDG_HASH-1) 2845f196ce38SLuigi Rizzo return (c & BRIDGE_RTHASH_MASK); 2846f196ce38SLuigi Rizzo } 2847f196ce38SLuigi Rizzo 2848f196ce38SLuigi Rizzo #undef mix 2849f196ce38SLuigi Rizzo 2850f196ce38SLuigi Rizzo 2851f196ce38SLuigi Rizzo static int 2852f196ce38SLuigi Rizzo bdg_netmap_reg(struct ifnet *ifp, int onoff) 2853f196ce38SLuigi Rizzo { 2854*f18be576SLuigi Rizzo // struct nm_bridge *b = NA(ifp)->na_bdg; 2855f196ce38SLuigi Rizzo 2856*f18be576SLuigi Rizzo /* the interface is already attached to the bridge, 2857*f18be576SLuigi Rizzo * so we only need to toggle IFCAP_NETMAP. 2858*f18be576SLuigi Rizzo * Locking is not necessary (we are already under 2859*f18be576SLuigi Rizzo * NMA_LOCK, and the port is not in use during this call). 2860f196ce38SLuigi Rizzo */ 2861*f18be576SLuigi Rizzo /* BDG_WLOCK(b); */ 2862*f18be576SLuigi Rizzo if (onoff) { 2863f196ce38SLuigi Rizzo ifp->if_capenable |= IFCAP_NETMAP; 2864f196ce38SLuigi Rizzo } else { 2865f196ce38SLuigi Rizzo ifp->if_capenable &= ~IFCAP_NETMAP; 2866f196ce38SLuigi Rizzo } 2867*f18be576SLuigi Rizzo /* BDG_WUNLOCK(b); */ 2868*f18be576SLuigi Rizzo return 0; 2869f196ce38SLuigi Rizzo } 2870f196ce38SLuigi Rizzo 2871f196ce38SLuigi Rizzo 2872*f18be576SLuigi Rizzo /* 2873*f18be576SLuigi Rizzo * Lookup function for a learning bridge. 2874*f18be576SLuigi Rizzo * Update the hash table with the source address, 2875*f18be576SLuigi Rizzo * and then returns the destination port index, and the 2876*f18be576SLuigi Rizzo * ring in *dst_ring (at the moment, always use ring 0) 2877*f18be576SLuigi Rizzo */ 2878*f18be576SLuigi Rizzo u_int 2879*f18be576SLuigi Rizzo netmap_bdg_learning(char *buf, u_int len, uint8_t *dst_ring, 2880*f18be576SLuigi Rizzo struct netmap_adapter *na) 2881f196ce38SLuigi Rizzo { 2882*f18be576SLuigi Rizzo struct nm_hash_ent *ht = na->na_bdg->ht; 2883f196ce38SLuigi Rizzo uint32_t sh, dh; 2884*f18be576SLuigi Rizzo u_int dst, mysrc = na->bdg_port; 2885f196ce38SLuigi Rizzo uint64_t smac, dmac; 2886f196ce38SLuigi Rizzo 2887f196ce38SLuigi Rizzo dmac = le64toh(*(uint64_t *)(buf)) & 0xffffffffffff; 2888f196ce38SLuigi Rizzo smac = le64toh(*(uint64_t *)(buf + 4)); 2889f196ce38SLuigi Rizzo smac >>= 16; 2890*f18be576SLuigi Rizzo 2891f196ce38SLuigi Rizzo /* 2892f196ce38SLuigi Rizzo * The hash is somewhat expensive, there might be some 2893f196ce38SLuigi Rizzo * worthwhile optimizations here. 2894f196ce38SLuigi Rizzo */ 2895f196ce38SLuigi Rizzo if ((buf[6] & 1) == 0) { /* valid src */ 2896f196ce38SLuigi Rizzo uint8_t *s = buf+6; 2897f196ce38SLuigi Rizzo sh = nm_bridge_rthash(buf+6); // XXX hash of source 2898f196ce38SLuigi Rizzo /* update source port forwarding entry */ 2899*f18be576SLuigi Rizzo ht[sh].mac = smac; /* XXX expire ? */ 2900*f18be576SLuigi Rizzo ht[sh].ports = mysrc; 2901f196ce38SLuigi Rizzo if (netmap_verbose) 2902f196ce38SLuigi Rizzo D("src %02x:%02x:%02x:%02x:%02x:%02x on port %d", 2903*f18be576SLuigi Rizzo s[0], s[1], s[2], s[3], s[4], s[5], mysrc); 2904f196ce38SLuigi Rizzo } 2905*f18be576SLuigi Rizzo dst = NM_BDG_BROADCAST; 2906f196ce38SLuigi Rizzo if ((buf[0] & 1) == 0) { /* unicast */ 2907f196ce38SLuigi Rizzo dh = nm_bridge_rthash(buf); // XXX hash of dst 2908*f18be576SLuigi Rizzo if (ht[dh].mac == dmac) { /* found dst */ 2909*f18be576SLuigi Rizzo dst = ht[dh].ports; 2910f196ce38SLuigi Rizzo } 2911*f18be576SLuigi Rizzo /* XXX otherwise return NM_BDG_UNKNOWN ? */ 2912f196ce38SLuigi Rizzo } 2913*f18be576SLuigi Rizzo *dst_ring = 0; 2914*f18be576SLuigi Rizzo return dst; 2915f196ce38SLuigi Rizzo } 2916f196ce38SLuigi Rizzo 2917*f18be576SLuigi Rizzo 2918*f18be576SLuigi Rizzo /* 2919*f18be576SLuigi Rizzo * This flush routine supports only unicast and broadcast but a large 2920*f18be576SLuigi Rizzo * number of ports, and lets us replace the learn and dispatch functions. 2921*f18be576SLuigi Rizzo */ 2922*f18be576SLuigi Rizzo int 2923*f18be576SLuigi Rizzo nm_bdg_flush(struct nm_bdg_fwd *ft, int n, struct netmap_adapter *na, 2924*f18be576SLuigi Rizzo u_int ring_nr) 2925*f18be576SLuigi Rizzo { 2926*f18be576SLuigi Rizzo struct nm_bdg_q *dst_ents, *brddst; 2927*f18be576SLuigi Rizzo uint16_t num_dsts = 0, *dsts; 2928*f18be576SLuigi Rizzo struct nm_bridge *b = na->na_bdg; 2929*f18be576SLuigi Rizzo u_int i, me = na->bdg_port; 2930*f18be576SLuigi Rizzo 2931*f18be576SLuigi Rizzo dst_ents = (struct nm_bdg_q *)(ft + NM_BDG_BATCH); 2932*f18be576SLuigi Rizzo dsts = (uint16_t *)(dst_ents + NM_BDG_MAXPORTS * NM_BDG_MAXRINGS + 1); 2933*f18be576SLuigi Rizzo 2934*f18be576SLuigi Rizzo BDG_RLOCK(b); 2935*f18be576SLuigi Rizzo 2936*f18be576SLuigi Rizzo /* first pass: find a destination */ 2937*f18be576SLuigi Rizzo for (i = 0; likely(i < n); i++) { 2938*f18be576SLuigi Rizzo uint8_t *buf = ft[i].buf; 2939*f18be576SLuigi Rizzo uint8_t dst_ring = ring_nr; 2940*f18be576SLuigi Rizzo uint16_t dst_port, d_i; 2941*f18be576SLuigi Rizzo struct nm_bdg_q *d; 2942*f18be576SLuigi Rizzo 2943*f18be576SLuigi Rizzo dst_port = b->nm_bdg_lookup(buf, ft[i].ft_len, &dst_ring, na); 2944*f18be576SLuigi Rizzo if (dst_port == NM_BDG_NOPORT) { 2945*f18be576SLuigi Rizzo continue; /* this packet is identified to be dropped */ 2946*f18be576SLuigi Rizzo } else if (unlikely(dst_port > NM_BDG_MAXPORTS)) { 2947*f18be576SLuigi Rizzo continue; 2948*f18be576SLuigi Rizzo } else if (dst_port == NM_BDG_BROADCAST) { 2949*f18be576SLuigi Rizzo dst_ring = 0; /* broadcasts always go to ring 0 */ 2950*f18be576SLuigi Rizzo } else if (unlikely(dst_port == me || 2951*f18be576SLuigi Rizzo !BDG_GET_VAR(b->bdg_ports[dst_port]))) { 2952*f18be576SLuigi Rizzo continue; 2953*f18be576SLuigi Rizzo } 2954*f18be576SLuigi Rizzo 2955*f18be576SLuigi Rizzo /* get a position in the scratch pad */ 2956*f18be576SLuigi Rizzo d_i = dst_port * NM_BDG_MAXRINGS + dst_ring; 2957*f18be576SLuigi Rizzo d = dst_ents + d_i; 2958*f18be576SLuigi Rizzo if (d->bq_head == NM_BDG_BATCH) { /* new destination */ 2959*f18be576SLuigi Rizzo d->bq_head = d->bq_tail = i; 2960*f18be576SLuigi Rizzo /* remember this position to be scanned later */ 2961*f18be576SLuigi Rizzo if (dst_port != NM_BDG_BROADCAST) 2962*f18be576SLuigi Rizzo dsts[num_dsts++] = d_i; 2963*f18be576SLuigi Rizzo } 2964*f18be576SLuigi Rizzo ft[d->bq_tail].ft_next = i; 2965*f18be576SLuigi Rizzo d->bq_tail = i; 2966*f18be576SLuigi Rizzo } 2967*f18be576SLuigi Rizzo 2968*f18be576SLuigi Rizzo /* if there is a broadcast, set ring 0 of all ports to be scanned 2969*f18be576SLuigi Rizzo * XXX This would be optimized by recording the highest index of active 2970*f18be576SLuigi Rizzo * ports. 2971*f18be576SLuigi Rizzo */ 2972*f18be576SLuigi Rizzo brddst = dst_ents + NM_BDG_BROADCAST * NM_BDG_MAXRINGS; 2973*f18be576SLuigi Rizzo if (brddst->bq_head != NM_BDG_BATCH) { 2974*f18be576SLuigi Rizzo for (i = 0; likely(i < NM_BDG_MAXPORTS); i++) { 2975*f18be576SLuigi Rizzo uint16_t d_i = i * NM_BDG_MAXRINGS; 2976*f18be576SLuigi Rizzo if (unlikely(i == me) || !BDG_GET_VAR(b->bdg_ports[i])) 2977*f18be576SLuigi Rizzo continue; 2978*f18be576SLuigi Rizzo else if (dst_ents[d_i].bq_head == NM_BDG_BATCH) 2979*f18be576SLuigi Rizzo dsts[num_dsts++] = d_i; 2980*f18be576SLuigi Rizzo } 2981*f18be576SLuigi Rizzo } 2982*f18be576SLuigi Rizzo 2983*f18be576SLuigi Rizzo /* second pass: scan destinations (XXX will be modular somehow) */ 2984*f18be576SLuigi Rizzo for (i = 0; i < num_dsts; i++) { 2985*f18be576SLuigi Rizzo struct ifnet *dst_ifp; 2986*f18be576SLuigi Rizzo struct netmap_adapter *dst_na; 2987f196ce38SLuigi Rizzo struct netmap_kring *kring; 2988f196ce38SLuigi Rizzo struct netmap_ring *ring; 2989*f18be576SLuigi Rizzo u_int dst_nr, is_vp, lim, j, sent = 0, d_i, next, brd_next; 2990*f18be576SLuigi Rizzo int howmany, retry = netmap_txsync_retry; 2991*f18be576SLuigi Rizzo struct nm_bdg_q *d; 2992f196ce38SLuigi Rizzo 2993*f18be576SLuigi Rizzo d_i = dsts[i]; 2994*f18be576SLuigi Rizzo d = dst_ents + d_i; 2995*f18be576SLuigi Rizzo dst_na = BDG_GET_VAR(b->bdg_ports[d_i/NM_BDG_MAXRINGS]); 2996*f18be576SLuigi Rizzo /* protect from the lookup function returning an inactive 2997*f18be576SLuigi Rizzo * destination port 2998*f18be576SLuigi Rizzo */ 2999*f18be576SLuigi Rizzo if (unlikely(dst_na == NULL)) 3000f196ce38SLuigi Rizzo continue; 3001*f18be576SLuigi Rizzo else if (dst_na->na_flags & NAF_SW_ONLY) 3002f196ce38SLuigi Rizzo continue; 3003*f18be576SLuigi Rizzo dst_ifp = dst_na->ifp; 3004*f18be576SLuigi Rizzo /* 3005*f18be576SLuigi Rizzo * The interface may be in !netmap mode in two cases: 3006*f18be576SLuigi Rizzo * - when na is attached but not activated yet; 3007*f18be576SLuigi Rizzo * - when na is being deactivated but is still attached. 3008*f18be576SLuigi Rizzo */ 3009*f18be576SLuigi Rizzo if (unlikely(!(dst_ifp->if_capenable & IFCAP_NETMAP))) 3010*f18be576SLuigi Rizzo continue; 3011f196ce38SLuigi Rizzo 3012*f18be576SLuigi Rizzo /* there is at least one either unicast or broadcast packet */ 3013*f18be576SLuigi Rizzo brd_next = brddst->bq_head; 3014*f18be576SLuigi Rizzo next = d->bq_head; 3015*f18be576SLuigi Rizzo 3016*f18be576SLuigi Rizzo is_vp = nma_is_vp(dst_na); 3017*f18be576SLuigi Rizzo dst_nr = d_i & (NM_BDG_MAXRINGS-1); 3018*f18be576SLuigi Rizzo if (is_vp) { /* virtual port */ 3019*f18be576SLuigi Rizzo if (dst_nr >= dst_na->num_rx_rings) 3020*f18be576SLuigi Rizzo dst_nr = dst_nr % dst_na->num_rx_rings; 3021*f18be576SLuigi Rizzo kring = &dst_na->rx_rings[dst_nr]; 3022f196ce38SLuigi Rizzo ring = kring->ring; 3023f196ce38SLuigi Rizzo lim = kring->nkr_num_slots - 1; 3024*f18be576SLuigi Rizzo dst_na->nm_lock(dst_ifp, NETMAP_RX_LOCK, dst_nr); 3025f196ce38SLuigi Rizzo j = kring->nr_hwcur + kring->nr_hwavail; 3026f196ce38SLuigi Rizzo if (j > lim) 3027f196ce38SLuigi Rizzo j -= kring->nkr_num_slots; 3028*f18be576SLuigi Rizzo howmany = lim - kring->nr_hwavail; 3029*f18be576SLuigi Rizzo } else { /* hw or sw adapter */ 3030*f18be576SLuigi Rizzo if (dst_nr >= dst_na->num_tx_rings) 3031*f18be576SLuigi Rizzo dst_nr = dst_nr % dst_na->num_tx_rings; 3032*f18be576SLuigi Rizzo kring = &dst_na->tx_rings[dst_nr]; 3033*f18be576SLuigi Rizzo ring = kring->ring; 3034*f18be576SLuigi Rizzo lim = kring->nkr_num_slots - 1; 3035*f18be576SLuigi Rizzo dst_na->nm_lock(dst_ifp, NETMAP_TX_LOCK, dst_nr); 3036*f18be576SLuigi Rizzo retry: 3037*f18be576SLuigi Rizzo dst_na->nm_txsync(dst_ifp, dst_nr, 0); 3038*f18be576SLuigi Rizzo /* see nm_bdg_flush() */ 3039*f18be576SLuigi Rizzo j = kring->nr_hwcur; 3040*f18be576SLuigi Rizzo howmany = kring->nr_hwavail; 3041*f18be576SLuigi Rizzo } 3042*f18be576SLuigi Rizzo while (howmany-- > 0) { 3043*f18be576SLuigi Rizzo struct netmap_slot *slot; 3044*f18be576SLuigi Rizzo struct nm_bdg_fwd *ft_p; 3045*f18be576SLuigi Rizzo 3046*f18be576SLuigi Rizzo if (next < brd_next) { 3047*f18be576SLuigi Rizzo ft_p = ft + next; 3048*f18be576SLuigi Rizzo next = ft_p->ft_next; 3049*f18be576SLuigi Rizzo } else { /* insert broadcast */ 3050*f18be576SLuigi Rizzo ft_p = ft + brd_next; 3051*f18be576SLuigi Rizzo brd_next = ft_p->ft_next; 3052*f18be576SLuigi Rizzo } 3053f196ce38SLuigi Rizzo slot = &ring->slot[j]; 3054*f18be576SLuigi Rizzo ND("send %d %d bytes at %s:%d", i, ft_p->ft_len, dst_ifp->if_xname, j); 3055*f18be576SLuigi Rizzo pkt_copy(ft_p->buf, NMB(slot), ft_p->ft_len); 3056*f18be576SLuigi Rizzo slot->len = ft_p->ft_len; 3057*f18be576SLuigi Rizzo j = (j == lim) ? 0: j + 1; /* XXX to be macro-ed */ 3058f196ce38SLuigi Rizzo sent++; 3059*f18be576SLuigi Rizzo if (next == d->bq_tail && brd_next == brddst->bq_tail) 3060*f18be576SLuigi Rizzo break; 3061f196ce38SLuigi Rizzo } 3062*f18be576SLuigi Rizzo if (netmap_verbose && (howmany < 0)) 3063*f18be576SLuigi Rizzo D("rx ring full on %s", dst_ifp->if_xname); 3064*f18be576SLuigi Rizzo if (is_vp) { 3065*f18be576SLuigi Rizzo if (sent) { 3066*f18be576SLuigi Rizzo kring->nr_hwavail += sent; 3067f196ce38SLuigi Rizzo selwakeuppri(&kring->si, PI_NET); 3068f196ce38SLuigi Rizzo } 3069*f18be576SLuigi Rizzo dst_na->nm_lock(dst_ifp, NETMAP_RX_UNLOCK, dst_nr); 3070*f18be576SLuigi Rizzo } else { 3071*f18be576SLuigi Rizzo if (sent) { 3072*f18be576SLuigi Rizzo ring->avail -= sent; 3073*f18be576SLuigi Rizzo ring->cur = j; 3074*f18be576SLuigi Rizzo dst_na->nm_txsync(dst_ifp, dst_nr, 0); 3075f196ce38SLuigi Rizzo } 3076*f18be576SLuigi Rizzo /* retry to send more packets */ 3077*f18be576SLuigi Rizzo if (nma_is_hw(dst_na) && howmany < 0 && retry--) 3078*f18be576SLuigi Rizzo goto retry; 3079*f18be576SLuigi Rizzo dst_na->nm_lock(dst_ifp, NETMAP_TX_UNLOCK, dst_nr); 3080*f18be576SLuigi Rizzo } 3081*f18be576SLuigi Rizzo d->bq_head = d->bq_tail = NM_BDG_BATCH; /* cleanup */ 3082*f18be576SLuigi Rizzo } 3083*f18be576SLuigi Rizzo brddst->bq_head = brddst->bq_tail = NM_BDG_BATCH; /* cleanup */ 3084*f18be576SLuigi Rizzo BDG_RUNLOCK(b); 3085f196ce38SLuigi Rizzo return 0; 3086f196ce38SLuigi Rizzo } 3087f196ce38SLuigi Rizzo 3088*f18be576SLuigi Rizzo 3089f196ce38SLuigi Rizzo /* 3090f196ce38SLuigi Rizzo * main dispatch routine 3091f196ce38SLuigi Rizzo */ 3092f196ce38SLuigi Rizzo static int 3093f196ce38SLuigi Rizzo bdg_netmap_txsync(struct ifnet *ifp, u_int ring_nr, int do_lock) 3094f196ce38SLuigi Rizzo { 3095f196ce38SLuigi Rizzo struct netmap_adapter *na = NA(ifp); 3096f196ce38SLuigi Rizzo struct netmap_kring *kring = &na->tx_rings[ring_nr]; 3097f196ce38SLuigi Rizzo struct netmap_ring *ring = kring->ring; 3098f196ce38SLuigi Rizzo int i, j, k, lim = kring->nkr_num_slots - 1; 3099f196ce38SLuigi Rizzo 3100f196ce38SLuigi Rizzo k = ring->cur; 3101f196ce38SLuigi Rizzo if (k > lim) 3102f196ce38SLuigi Rizzo return netmap_ring_reinit(kring); 3103f196ce38SLuigi Rizzo if (do_lock) 3104f196ce38SLuigi Rizzo na->nm_lock(ifp, NETMAP_TX_LOCK, ring_nr); 3105f196ce38SLuigi Rizzo 3106f196ce38SLuigi Rizzo if (netmap_bridge <= 0) { /* testing only */ 3107f196ce38SLuigi Rizzo j = k; // used all 3108f196ce38SLuigi Rizzo goto done; 3109f196ce38SLuigi Rizzo } 3110f196ce38SLuigi Rizzo if (netmap_bridge > NM_BDG_BATCH) 3111f196ce38SLuigi Rizzo netmap_bridge = NM_BDG_BATCH; 3112f196ce38SLuigi Rizzo 3113*f18be576SLuigi Rizzo j = nm_bdg_preflush(na, ring_nr, kring, k); 3114f196ce38SLuigi Rizzo i = k - j; 3115f196ce38SLuigi Rizzo if (i < 0) 3116f196ce38SLuigi Rizzo i += kring->nkr_num_slots; 3117f196ce38SLuigi Rizzo kring->nr_hwavail = kring->nkr_num_slots - 1 - i; 3118f196ce38SLuigi Rizzo if (j != k) 3119f196ce38SLuigi Rizzo D("early break at %d/ %d, avail %d", j, k, kring->nr_hwavail); 3120f196ce38SLuigi Rizzo 3121f196ce38SLuigi Rizzo done: 3122f196ce38SLuigi Rizzo kring->nr_hwcur = j; 3123f196ce38SLuigi Rizzo ring->avail = kring->nr_hwavail; 3124f196ce38SLuigi Rizzo if (do_lock) 3125f196ce38SLuigi Rizzo na->nm_lock(ifp, NETMAP_TX_UNLOCK, ring_nr); 3126f196ce38SLuigi Rizzo 3127f196ce38SLuigi Rizzo if (netmap_verbose) 3128f196ce38SLuigi Rizzo D("%s ring %d lock %d", ifp->if_xname, ring_nr, do_lock); 3129f196ce38SLuigi Rizzo return 0; 3130f196ce38SLuigi Rizzo } 3131f196ce38SLuigi Rizzo 3132*f18be576SLuigi Rizzo 3133f196ce38SLuigi Rizzo static int 3134f196ce38SLuigi Rizzo bdg_netmap_rxsync(struct ifnet *ifp, u_int ring_nr, int do_lock) 3135f196ce38SLuigi Rizzo { 3136f196ce38SLuigi Rizzo struct netmap_adapter *na = NA(ifp); 3137f196ce38SLuigi Rizzo struct netmap_kring *kring = &na->rx_rings[ring_nr]; 3138f196ce38SLuigi Rizzo struct netmap_ring *ring = kring->ring; 3139*f18be576SLuigi Rizzo u_int j, lim = kring->nkr_num_slots - 1; 3140f196ce38SLuigi Rizzo u_int k = ring->cur, resvd = ring->reserved; 3141*f18be576SLuigi Rizzo int n; 3142f196ce38SLuigi Rizzo 3143f196ce38SLuigi Rizzo ND("%s ring %d lock %d avail %d", 3144f196ce38SLuigi Rizzo ifp->if_xname, ring_nr, do_lock, kring->nr_hwavail); 3145f196ce38SLuigi Rizzo 3146f196ce38SLuigi Rizzo if (k > lim) 3147f196ce38SLuigi Rizzo return netmap_ring_reinit(kring); 3148f196ce38SLuigi Rizzo if (do_lock) 3149f196ce38SLuigi Rizzo na->nm_lock(ifp, NETMAP_RX_LOCK, ring_nr); 3150f196ce38SLuigi Rizzo 3151f196ce38SLuigi Rizzo /* skip past packets that userspace has released */ 3152f196ce38SLuigi Rizzo j = kring->nr_hwcur; /* netmap ring index */ 3153f196ce38SLuigi Rizzo if (resvd > 0) { 3154f196ce38SLuigi Rizzo if (resvd + ring->avail >= lim + 1) { 3155f196ce38SLuigi Rizzo D("XXX invalid reserve/avail %d %d", resvd, ring->avail); 3156f196ce38SLuigi Rizzo ring->reserved = resvd = 0; // XXX panic... 3157f196ce38SLuigi Rizzo } 3158f196ce38SLuigi Rizzo k = (k >= resvd) ? k - resvd : k + lim + 1 - resvd; 3159f196ce38SLuigi Rizzo } 3160f196ce38SLuigi Rizzo 3161f196ce38SLuigi Rizzo if (j != k) { /* userspace has released some packets. */ 3162f196ce38SLuigi Rizzo n = k - j; 3163f196ce38SLuigi Rizzo if (n < 0) 3164f196ce38SLuigi Rizzo n += kring->nkr_num_slots; 3165f196ce38SLuigi Rizzo ND("userspace releases %d packets", n); 3166f196ce38SLuigi Rizzo for (n = 0; likely(j != k); n++) { 3167f196ce38SLuigi Rizzo struct netmap_slot *slot = &ring->slot[j]; 3168f196ce38SLuigi Rizzo void *addr = NMB(slot); 3169f196ce38SLuigi Rizzo 3170f196ce38SLuigi Rizzo if (addr == netmap_buffer_base) { /* bad buf */ 3171f196ce38SLuigi Rizzo if (do_lock) 3172f196ce38SLuigi Rizzo na->nm_lock(ifp, NETMAP_RX_UNLOCK, ring_nr); 3173f196ce38SLuigi Rizzo return netmap_ring_reinit(kring); 3174f196ce38SLuigi Rizzo } 3175f196ce38SLuigi Rizzo /* decrease refcount for buffer */ 3176f196ce38SLuigi Rizzo 3177f196ce38SLuigi Rizzo slot->flags &= ~NS_BUF_CHANGED; 3178f196ce38SLuigi Rizzo j = unlikely(j == lim) ? 0 : j + 1; 3179f196ce38SLuigi Rizzo } 3180f196ce38SLuigi Rizzo kring->nr_hwavail -= n; 3181f196ce38SLuigi Rizzo kring->nr_hwcur = k; 3182f196ce38SLuigi Rizzo } 3183f196ce38SLuigi Rizzo /* tell userspace that there are new packets */ 3184f196ce38SLuigi Rizzo ring->avail = kring->nr_hwavail - resvd; 3185f196ce38SLuigi Rizzo 3186f196ce38SLuigi Rizzo if (do_lock) 3187f196ce38SLuigi Rizzo na->nm_lock(ifp, NETMAP_RX_UNLOCK, ring_nr); 3188f196ce38SLuigi Rizzo return 0; 3189f196ce38SLuigi Rizzo } 3190f196ce38SLuigi Rizzo 3191*f18be576SLuigi Rizzo 3192f196ce38SLuigi Rizzo static void 3193*f18be576SLuigi Rizzo bdg_netmap_attach(struct netmap_adapter *arg) 3194f196ce38SLuigi Rizzo { 3195f196ce38SLuigi Rizzo struct netmap_adapter na; 3196f196ce38SLuigi Rizzo 3197f196ce38SLuigi Rizzo ND("attaching virtual bridge"); 3198f196ce38SLuigi Rizzo bzero(&na, sizeof(na)); 3199f196ce38SLuigi Rizzo 3200*f18be576SLuigi Rizzo na.ifp = arg->ifp; 3201f196ce38SLuigi Rizzo na.separate_locks = 1; 3202*f18be576SLuigi Rizzo na.num_tx_rings = arg->num_tx_rings; 3203*f18be576SLuigi Rizzo na.num_rx_rings = arg->num_rx_rings; 3204f196ce38SLuigi Rizzo na.num_tx_desc = NM_BRIDGE_RINGSIZE; 3205f196ce38SLuigi Rizzo na.num_rx_desc = NM_BRIDGE_RINGSIZE; 3206f196ce38SLuigi Rizzo na.nm_txsync = bdg_netmap_txsync; 3207f196ce38SLuigi Rizzo na.nm_rxsync = bdg_netmap_rxsync; 3208f196ce38SLuigi Rizzo na.nm_register = bdg_netmap_reg; 3209*f18be576SLuigi Rizzo netmap_attach(&na, na.num_tx_rings); 3210f196ce38SLuigi Rizzo } 3211f196ce38SLuigi Rizzo 3212f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */ 3213babc7c12SLuigi Rizzo 3214babc7c12SLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */ 3215babc7c12SLuigi Rizzo 3216babc7c12SLuigi Rizzo 32171a26580eSLuigi Rizzo /* 321868b8534bSLuigi Rizzo * Module loader. 321968b8534bSLuigi Rizzo * 322068b8534bSLuigi Rizzo * Create the /dev/netmap device and initialize all global 322168b8534bSLuigi Rizzo * variables. 322268b8534bSLuigi Rizzo * 322368b8534bSLuigi Rizzo * Return 0 on success, errno on failure. 322468b8534bSLuigi Rizzo */ 322568b8534bSLuigi Rizzo static int 322668b8534bSLuigi Rizzo netmap_init(void) 322768b8534bSLuigi Rizzo { 322868b8534bSLuigi Rizzo int error; 322968b8534bSLuigi Rizzo 323068b8534bSLuigi Rizzo error = netmap_memory_init(); 323168b8534bSLuigi Rizzo if (error != 0) { 323242a3a5bdSLuigi Rizzo printf("netmap: unable to initialize the memory allocator.\n"); 323368b8534bSLuigi Rizzo return (error); 323468b8534bSLuigi Rizzo } 32358241616dSLuigi Rizzo printf("netmap: loaded module\n"); 323668b8534bSLuigi Rizzo netmap_dev = make_dev(&netmap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660, 323768b8534bSLuigi Rizzo "netmap"); 3238f196ce38SLuigi Rizzo 3239f196ce38SLuigi Rizzo #ifdef NM_BRIDGE 3240f196ce38SLuigi Rizzo { 3241f196ce38SLuigi Rizzo int i; 3242*f18be576SLuigi Rizzo mtx_init(&netmap_bridge_mutex, "netmap_bridge_mutex", 3243*f18be576SLuigi Rizzo MTX_NETWORK_LOCK, MTX_DEF); 3244*f18be576SLuigi Rizzo bzero(nm_bridges, sizeof(struct nm_bridge) * NM_BRIDGES); /* safety */ 3245f196ce38SLuigi Rizzo for (i = 0; i < NM_BRIDGES; i++) 3246*f18be576SLuigi Rizzo rw_init(&nm_bridges[i].bdg_lock, "bdg lock"); 3247f196ce38SLuigi Rizzo } 3248f196ce38SLuigi Rizzo #endif 3249babc7c12SLuigi Rizzo return (error); 325068b8534bSLuigi Rizzo } 325168b8534bSLuigi Rizzo 325268b8534bSLuigi Rizzo 325368b8534bSLuigi Rizzo /* 325468b8534bSLuigi Rizzo * Module unloader. 325568b8534bSLuigi Rizzo * 325668b8534bSLuigi Rizzo * Free all the memory, and destroy the ``/dev/netmap`` device. 325768b8534bSLuigi Rizzo */ 325868b8534bSLuigi Rizzo static void 325968b8534bSLuigi Rizzo netmap_fini(void) 326068b8534bSLuigi Rizzo { 326168b8534bSLuigi Rizzo destroy_dev(netmap_dev); 326268b8534bSLuigi Rizzo netmap_memory_fini(); 326368b8534bSLuigi Rizzo printf("netmap: unloaded module.\n"); 326468b8534bSLuigi Rizzo } 326568b8534bSLuigi Rizzo 326668b8534bSLuigi Rizzo 3267f196ce38SLuigi Rizzo #ifdef __FreeBSD__ 326868b8534bSLuigi Rizzo /* 326968b8534bSLuigi Rizzo * Kernel entry point. 327068b8534bSLuigi Rizzo * 327168b8534bSLuigi Rizzo * Initialize/finalize the module and return. 327268b8534bSLuigi Rizzo * 327368b8534bSLuigi Rizzo * Return 0 on success, errno on failure. 327468b8534bSLuigi Rizzo */ 327568b8534bSLuigi Rizzo static int 327668b8534bSLuigi Rizzo netmap_loader(__unused struct module *module, int event, __unused void *arg) 327768b8534bSLuigi Rizzo { 327868b8534bSLuigi Rizzo int error = 0; 327968b8534bSLuigi Rizzo 328068b8534bSLuigi Rizzo switch (event) { 328168b8534bSLuigi Rizzo case MOD_LOAD: 328268b8534bSLuigi Rizzo error = netmap_init(); 328368b8534bSLuigi Rizzo break; 328468b8534bSLuigi Rizzo 328568b8534bSLuigi Rizzo case MOD_UNLOAD: 328668b8534bSLuigi Rizzo netmap_fini(); 328768b8534bSLuigi Rizzo break; 328868b8534bSLuigi Rizzo 328968b8534bSLuigi Rizzo default: 329068b8534bSLuigi Rizzo error = EOPNOTSUPP; 329168b8534bSLuigi Rizzo break; 329268b8534bSLuigi Rizzo } 329368b8534bSLuigi Rizzo 329468b8534bSLuigi Rizzo return (error); 329568b8534bSLuigi Rizzo } 329668b8534bSLuigi Rizzo 329768b8534bSLuigi Rizzo 329868b8534bSLuigi Rizzo DEV_MODULE(netmap, netmap_loader, NULL); 3299f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */ 3300