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."); 122f18be576SLuigi Rizzo int netmap_txsync_retry = 2; 123f18be576SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, txsync_retry, CTLFLAG_RW, 124f18be576SLuigi 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 134f18be576SLuigi Rizzo #ifdef NM_BRIDGE /* support for netmap virtual switch, called VALE */ 135f196ce38SLuigi Rizzo 136f196ce38SLuigi Rizzo /* 137f18be576SLuigi Rizzo * system parameters (most of them in netmap_kern.h) 138f18be576SLuigi Rizzo * NM_NAME prefix for switch port names, default "vale" 139f18be576SLuigi Rizzo * NM_MAXPORTS number of ports 140f18be576SLuigi Rizzo * NM_BRIDGES max number of switches in the system. 141f18be576SLuigi Rizzo * XXX should become a sysctl or tunable 142f196ce38SLuigi Rizzo * 143f18be576SLuigi Rizzo * Switch ports are named valeX:Y where X is the switch name and Y 144f18be576SLuigi Rizzo * is the port. If Y matches a physical interface name, the port is 145f18be576SLuigi Rizzo * connected to a physical device. 146f18be576SLuigi Rizzo * 147f18be576SLuigi Rizzo * Unlike physical interfaces, switch ports use their own memory region 148f18be576SLuigi 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 */ 153f18be576SLuigi 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 */ 157f18be576SLuigi 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 185f18be576SLuigi Rizzo static void bdg_netmap_attach(struct netmap_adapter *); 186f196ce38SLuigi Rizzo static int bdg_netmap_reg(struct ifnet *ifp, int onoff); 187f18be576SLuigi Rizzo static int kern_netmap_regif(struct nmreq *nmr); 188f18be576SLuigi Rizzo 189f196ce38SLuigi Rizzo /* per-tx-queue entry */ 190f196ce38SLuigi Rizzo struct nm_bdg_fwd { /* forwarding entry for a bridge */ 191*85233a7dSLuigi Rizzo void *ft_buf; 192*85233a7dSLuigi Rizzo uint16_t _ft_dst; /* dst port, unused */ 193*85233a7dSLuigi Rizzo uint16_t ft_flags; /* flags, e.g. indirect */ 194f18be576SLuigi Rizzo uint16_t ft_len; /* src len */ 195f18be576SLuigi Rizzo uint16_t ft_next; /* next packet to same destination */ 196f18be576SLuigi Rizzo }; 197f18be576SLuigi Rizzo 198f18be576SLuigi Rizzo /* We need to build a list of buffers going to each destination. 199f18be576SLuigi Rizzo * Each buffer is in one entry of struct nm_bdg_fwd, we use ft_next 200f18be576SLuigi Rizzo * to build the list, and struct nm_bdg_q below for the queue. 201f18be576SLuigi Rizzo * The structure should compact because potentially we have a lot 202f18be576SLuigi Rizzo * of destinations. 203f18be576SLuigi Rizzo */ 204f18be576SLuigi Rizzo struct nm_bdg_q { 205f18be576SLuigi Rizzo uint16_t bq_head; 206f18be576SLuigi Rizzo uint16_t bq_tail; 207f196ce38SLuigi Rizzo }; 208f196ce38SLuigi Rizzo 209f196ce38SLuigi Rizzo struct nm_hash_ent { 210f196ce38SLuigi Rizzo uint64_t mac; /* the top 2 bytes are the epoch */ 211f196ce38SLuigi Rizzo uint64_t ports; 212f196ce38SLuigi Rizzo }; 213f196ce38SLuigi Rizzo 214f196ce38SLuigi Rizzo /* 215849bec0eSLuigi Rizzo * Interfaces for a bridge are all in bdg_ports[]. 216f196ce38SLuigi Rizzo * The array has fixed size, an empty entry does not terminate 217849bec0eSLuigi Rizzo * the search. But lookups only occur on attach/detach so we 218849bec0eSLuigi Rizzo * don't mind if they are slow. 219849bec0eSLuigi Rizzo * 220849bec0eSLuigi Rizzo * The bridge is non blocking on the transmit ports. 221849bec0eSLuigi Rizzo * 222849bec0eSLuigi Rizzo * bdg_lock protects accesses to the bdg_ports array. 223f18be576SLuigi Rizzo * This is a rw lock (or equivalent). 224f196ce38SLuigi Rizzo */ 225f196ce38SLuigi Rizzo struct nm_bridge { 226f18be576SLuigi Rizzo int namelen; /* 0 means free */ 227f18be576SLuigi Rizzo 228f18be576SLuigi Rizzo /* XXX what is the proper alignment/layout ? */ 229f18be576SLuigi Rizzo NM_RWLOCK_T bdg_lock; /* protects bdg_ports */ 230f18be576SLuigi Rizzo struct netmap_adapter *bdg_ports[NM_BDG_MAXPORTS]; 231f18be576SLuigi Rizzo 232f18be576SLuigi Rizzo char basename[IFNAMSIZ]; 233f18be576SLuigi Rizzo /* 234f18be576SLuigi Rizzo * The function to decide the destination port. 235f18be576SLuigi Rizzo * It returns either of an index of the destination port, 236f18be576SLuigi Rizzo * NM_BDG_BROADCAST to broadcast this packet, or NM_BDG_NOPORT not to 237f18be576SLuigi Rizzo * forward this packet. ring_nr is the source ring index, and the 238f18be576SLuigi Rizzo * function may overwrite this value to forward this packet to a 239f18be576SLuigi Rizzo * different ring index. 240f18be576SLuigi Rizzo * This function must be set by netmap_bdgctl(). 241f18be576SLuigi Rizzo */ 242f18be576SLuigi Rizzo bdg_lookup_fn_t nm_bdg_lookup; 243f196ce38SLuigi Rizzo 244f196ce38SLuigi Rizzo /* the forwarding table, MAC+ports */ 245f196ce38SLuigi Rizzo struct nm_hash_ent ht[NM_BDG_HASH]; 246f196ce38SLuigi Rizzo }; 247f196ce38SLuigi Rizzo 248f196ce38SLuigi Rizzo struct nm_bridge nm_bridges[NM_BRIDGES]; 249f18be576SLuigi Rizzo NM_LOCK_T netmap_bridge_mutex; 250f196ce38SLuigi Rizzo 251f18be576SLuigi Rizzo /* other OS will have these macros defined in their own glue code. */ 252f18be576SLuigi Rizzo 253f18be576SLuigi Rizzo #ifdef __FreeBSD__ 254f18be576SLuigi Rizzo #define BDG_LOCK() mtx_lock(&netmap_bridge_mutex) 255f18be576SLuigi Rizzo #define BDG_UNLOCK() mtx_unlock(&netmap_bridge_mutex) 256f18be576SLuigi Rizzo #define BDG_WLOCK(b) rw_wlock(&(b)->bdg_lock) 257f18be576SLuigi Rizzo #define BDG_WUNLOCK(b) rw_wunlock(&(b)->bdg_lock) 258f18be576SLuigi Rizzo #define BDG_RLOCK(b) rw_rlock(&(b)->bdg_lock) 259f18be576SLuigi Rizzo #define BDG_RUNLOCK(b) rw_runlock(&(b)->bdg_lock) 260f18be576SLuigi Rizzo 261f18be576SLuigi Rizzo /* set/get variables. OS-specific macros may wrap these 262f18be576SLuigi Rizzo * assignments into read/write lock or similar 263f18be576SLuigi Rizzo */ 264f18be576SLuigi Rizzo #define BDG_SET_VAR(lval, p) (lval = p) 265f18be576SLuigi Rizzo #define BDG_GET_VAR(lval) (lval) 266f18be576SLuigi Rizzo #define BDG_FREE(p) free(p, M_DEVBUF) 267f18be576SLuigi Rizzo #endif /* __FreeBSD__ */ 268f18be576SLuigi Rizzo 269f18be576SLuigi Rizzo static __inline int 270f18be576SLuigi Rizzo nma_is_vp(struct netmap_adapter *na) 271f18be576SLuigi Rizzo { 272f18be576SLuigi Rizzo return na->nm_register == bdg_netmap_reg; 273f18be576SLuigi Rizzo } 274f18be576SLuigi Rizzo static __inline int 275f18be576SLuigi Rizzo nma_is_host(struct netmap_adapter *na) 276f18be576SLuigi Rizzo { 277f18be576SLuigi Rizzo return na->nm_register == NULL; 278f18be576SLuigi Rizzo } 279f18be576SLuigi Rizzo static __inline int 280f18be576SLuigi Rizzo nma_is_hw(struct netmap_adapter *na) 281f18be576SLuigi Rizzo { 282f18be576SLuigi Rizzo /* In case of sw adapter, nm_register is NULL */ 283f18be576SLuigi Rizzo return !nma_is_vp(na) && !nma_is_host(na); 284f18be576SLuigi Rizzo } 285f18be576SLuigi Rizzo 286f18be576SLuigi Rizzo /* 287f18be576SLuigi Rizzo * Regarding holding a NIC, if the NIC is owned by the kernel 288f18be576SLuigi Rizzo * (i.e., bridge), neither another bridge nor user can use it; 289f18be576SLuigi Rizzo * if the NIC is owned by a user, only users can share it. 290f18be576SLuigi Rizzo * Evaluation must be done under NMA_LOCK(). 291f18be576SLuigi Rizzo */ 292f18be576SLuigi Rizzo #define NETMAP_OWNED_BY_KERN(ifp) (!nma_is_vp(NA(ifp)) && NA(ifp)->na_bdg) 293f18be576SLuigi Rizzo #define NETMAP_OWNED_BY_ANY(ifp) \ 294f18be576SLuigi Rizzo (NETMAP_OWNED_BY_KERN(ifp) || (NA(ifp)->refcount > 0)) 295f196ce38SLuigi Rizzo 296f196ce38SLuigi Rizzo /* 297f196ce38SLuigi Rizzo * NA(ifp)->bdg_port port index 298f196ce38SLuigi Rizzo */ 299f196ce38SLuigi Rizzo 300f196ce38SLuigi Rizzo // XXX only for multiples of 64 bytes, non overlapped. 301f196ce38SLuigi Rizzo static inline void 302f196ce38SLuigi Rizzo pkt_copy(void *_src, void *_dst, int l) 303f196ce38SLuigi Rizzo { 304f196ce38SLuigi Rizzo uint64_t *src = _src; 305f196ce38SLuigi Rizzo uint64_t *dst = _dst; 306f196ce38SLuigi Rizzo if (unlikely(l >= 1024)) { 307f196ce38SLuigi Rizzo bcopy(src, dst, l); 308f196ce38SLuigi Rizzo return; 309f196ce38SLuigi Rizzo } 310f196ce38SLuigi Rizzo for (; likely(l > 0); l-=64) { 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 *dst++ = *src++; 319f196ce38SLuigi Rizzo } 320f196ce38SLuigi Rizzo } 321f196ce38SLuigi Rizzo 322f18be576SLuigi Rizzo 323f196ce38SLuigi Rizzo /* 324f196ce38SLuigi Rizzo * locate a bridge among the existing ones. 325f196ce38SLuigi Rizzo * a ':' in the name terminates the bridge name. Otherwise, just NM_NAME. 326f196ce38SLuigi Rizzo * We assume that this is called with a name of at least NM_NAME chars. 327f196ce38SLuigi Rizzo */ 328f196ce38SLuigi Rizzo static struct nm_bridge * 329f18be576SLuigi Rizzo nm_find_bridge(const char *name, int create) 330f196ce38SLuigi Rizzo { 331f18be576SLuigi Rizzo int i, l, namelen; 332f196ce38SLuigi Rizzo struct nm_bridge *b = NULL; 333f196ce38SLuigi Rizzo 334f196ce38SLuigi Rizzo namelen = strlen(NM_NAME); /* base length */ 335f196ce38SLuigi Rizzo l = strlen(name); /* actual length */ 336f196ce38SLuigi Rizzo for (i = namelen + 1; i < l; i++) { 337f196ce38SLuigi Rizzo if (name[i] == ':') { 338f196ce38SLuigi Rizzo namelen = i; 339f196ce38SLuigi Rizzo break; 340f196ce38SLuigi Rizzo } 341f196ce38SLuigi Rizzo } 342f196ce38SLuigi Rizzo if (namelen >= IFNAMSIZ) 343f196ce38SLuigi Rizzo namelen = IFNAMSIZ; 344f196ce38SLuigi Rizzo ND("--- prefix is '%.*s' ---", namelen, name); 345f196ce38SLuigi Rizzo 346f18be576SLuigi Rizzo BDG_LOCK(); 347f18be576SLuigi Rizzo /* lookup the name, remember empty slot if there is one */ 348f18be576SLuigi Rizzo for (i = 0; i < NM_BRIDGES; i++) { 349f18be576SLuigi Rizzo struct nm_bridge *x = nm_bridges + i; 350f18be576SLuigi Rizzo 351f18be576SLuigi Rizzo if (x->namelen == 0) { 352f18be576SLuigi Rizzo if (create && b == NULL) 353f18be576SLuigi Rizzo b = x; /* record empty slot */ 354f18be576SLuigi Rizzo } else if (x->namelen != namelen) { 355f18be576SLuigi Rizzo continue; 356f18be576SLuigi Rizzo } else if (strncmp(name, x->basename, namelen) == 0) { 357f196ce38SLuigi Rizzo ND("found '%.*s' at %d", namelen, name, i); 358f18be576SLuigi Rizzo b = x; 359f196ce38SLuigi Rizzo break; 360f196ce38SLuigi Rizzo } 361f196ce38SLuigi Rizzo } 362f18be576SLuigi Rizzo if (i == NM_BRIDGES && b) { /* name not found, can create entry */ 363f196ce38SLuigi Rizzo strncpy(b->basename, name, namelen); 364f196ce38SLuigi Rizzo b->namelen = namelen; 365f18be576SLuigi Rizzo /* set the default function */ 366f18be576SLuigi Rizzo b->nm_bdg_lookup = netmap_bdg_learning; 367f18be576SLuigi Rizzo /* reset the MAC address table */ 368f18be576SLuigi Rizzo bzero(b->ht, sizeof(struct nm_hash_ent) * NM_BDG_HASH); 369f196ce38SLuigi Rizzo } 370f18be576SLuigi Rizzo BDG_UNLOCK(); 371f196ce38SLuigi Rizzo return b; 372f196ce38SLuigi Rizzo } 373f18be576SLuigi Rizzo 374f18be576SLuigi Rizzo 375f18be576SLuigi Rizzo /* 376f18be576SLuigi Rizzo * Free the forwarding tables for rings attached to switch ports. 377f18be576SLuigi Rizzo */ 378f18be576SLuigi Rizzo static void 379f18be576SLuigi Rizzo nm_free_bdgfwd(struct netmap_adapter *na) 380f18be576SLuigi Rizzo { 381f18be576SLuigi Rizzo int nrings, i; 382f18be576SLuigi Rizzo struct netmap_kring *kring; 383f18be576SLuigi Rizzo 384f18be576SLuigi Rizzo nrings = nma_is_vp(na) ? na->num_tx_rings : na->num_rx_rings; 385f18be576SLuigi Rizzo kring = nma_is_vp(na) ? na->tx_rings : na->rx_rings; 386f18be576SLuigi Rizzo for (i = 0; i < nrings; i++) { 387f18be576SLuigi Rizzo if (kring[i].nkr_ft) { 388f18be576SLuigi Rizzo free(kring[i].nkr_ft, M_DEVBUF); 389f18be576SLuigi Rizzo kring[i].nkr_ft = NULL; /* protect from freeing twice */ 390f18be576SLuigi Rizzo } 391f18be576SLuigi Rizzo } 392f18be576SLuigi Rizzo if (nma_is_hw(na)) 393f18be576SLuigi Rizzo nm_free_bdgfwd(SWNA(na->ifp)); 394f18be576SLuigi Rizzo } 395f18be576SLuigi Rizzo 396f18be576SLuigi Rizzo 397f18be576SLuigi Rizzo /* 398f18be576SLuigi Rizzo * Allocate the forwarding tables for the rings attached to the bridge ports. 399f18be576SLuigi Rizzo */ 400f18be576SLuigi Rizzo static int 401f18be576SLuigi Rizzo nm_alloc_bdgfwd(struct netmap_adapter *na) 402f18be576SLuigi Rizzo { 403f18be576SLuigi Rizzo int nrings, l, i, num_dstq; 404f18be576SLuigi Rizzo struct netmap_kring *kring; 405f18be576SLuigi Rizzo 406f18be576SLuigi Rizzo /* all port:rings + broadcast */ 407f18be576SLuigi Rizzo num_dstq = NM_BDG_MAXPORTS * NM_BDG_MAXRINGS + 1; 408f18be576SLuigi Rizzo l = sizeof(struct nm_bdg_fwd) * NM_BDG_BATCH; 409f18be576SLuigi Rizzo l += sizeof(struct nm_bdg_q) * num_dstq; 410f18be576SLuigi Rizzo l += sizeof(uint16_t) * NM_BDG_BATCH; 411f18be576SLuigi Rizzo 412f18be576SLuigi Rizzo nrings = nma_is_vp(na) ? na->num_tx_rings : na->num_rx_rings; 413f18be576SLuigi Rizzo kring = nma_is_vp(na) ? na->tx_rings : na->rx_rings; 414f18be576SLuigi Rizzo for (i = 0; i < nrings; i++) { 415f18be576SLuigi Rizzo struct nm_bdg_fwd *ft; 416f18be576SLuigi Rizzo struct nm_bdg_q *dstq; 417f18be576SLuigi Rizzo int j; 418f18be576SLuigi Rizzo 419f18be576SLuigi Rizzo ft = malloc(l, M_DEVBUF, M_NOWAIT | M_ZERO); 420f18be576SLuigi Rizzo if (!ft) { 421f18be576SLuigi Rizzo nm_free_bdgfwd(na); 422f18be576SLuigi Rizzo return ENOMEM; 423f18be576SLuigi Rizzo } 424f18be576SLuigi Rizzo dstq = (struct nm_bdg_q *)(ft + NM_BDG_BATCH); 425f18be576SLuigi Rizzo for (j = 0; j < num_dstq; j++) 426f18be576SLuigi Rizzo dstq[j].bq_head = dstq[j].bq_tail = NM_BDG_BATCH; 427f18be576SLuigi Rizzo kring[i].nkr_ft = ft; 428f18be576SLuigi Rizzo } 429f18be576SLuigi Rizzo if (nma_is_hw(na)) 430f18be576SLuigi Rizzo nm_alloc_bdgfwd(SWNA(na->ifp)); 431f18be576SLuigi Rizzo return 0; 432f18be576SLuigi Rizzo } 433f18be576SLuigi Rizzo 434f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */ 4355819da83SLuigi Rizzo 436ae10d1afSLuigi Rizzo 437ae10d1afSLuigi Rizzo /* 438ae10d1afSLuigi Rizzo * Fetch configuration from the device, to cope with dynamic 439ae10d1afSLuigi Rizzo * reconfigurations after loading the module. 440ae10d1afSLuigi Rizzo */ 441ae10d1afSLuigi Rizzo static int 442ae10d1afSLuigi Rizzo netmap_update_config(struct netmap_adapter *na) 443ae10d1afSLuigi Rizzo { 444ae10d1afSLuigi Rizzo struct ifnet *ifp = na->ifp; 445ae10d1afSLuigi Rizzo u_int txr, txd, rxr, rxd; 446ae10d1afSLuigi Rizzo 447ae10d1afSLuigi Rizzo txr = txd = rxr = rxd = 0; 448ae10d1afSLuigi Rizzo if (na->nm_config) { 449ae10d1afSLuigi Rizzo na->nm_config(ifp, &txr, &txd, &rxr, &rxd); 450ae10d1afSLuigi Rizzo } else { 451ae10d1afSLuigi Rizzo /* take whatever we had at init time */ 452ae10d1afSLuigi Rizzo txr = na->num_tx_rings; 453ae10d1afSLuigi Rizzo txd = na->num_tx_desc; 454ae10d1afSLuigi Rizzo rxr = na->num_rx_rings; 455ae10d1afSLuigi Rizzo rxd = na->num_rx_desc; 456ae10d1afSLuigi Rizzo } 457ae10d1afSLuigi Rizzo 458ae10d1afSLuigi Rizzo if (na->num_tx_rings == txr && na->num_tx_desc == txd && 459ae10d1afSLuigi Rizzo na->num_rx_rings == rxr && na->num_rx_desc == rxd) 460ae10d1afSLuigi Rizzo return 0; /* nothing changed */ 461ae10d1afSLuigi Rizzo if (netmap_verbose || na->refcount > 0) { 462ae10d1afSLuigi Rizzo D("stored config %s: txring %d x %d, rxring %d x %d", 463ae10d1afSLuigi Rizzo ifp->if_xname, 464ae10d1afSLuigi Rizzo na->num_tx_rings, na->num_tx_desc, 465ae10d1afSLuigi Rizzo na->num_rx_rings, na->num_rx_desc); 466ae10d1afSLuigi Rizzo D("new config %s: txring %d x %d, rxring %d x %d", 467ae10d1afSLuigi Rizzo ifp->if_xname, txr, txd, rxr, rxd); 468ae10d1afSLuigi Rizzo } 469ae10d1afSLuigi Rizzo if (na->refcount == 0) { 470ae10d1afSLuigi Rizzo D("configuration changed (but fine)"); 471ae10d1afSLuigi Rizzo na->num_tx_rings = txr; 472ae10d1afSLuigi Rizzo na->num_tx_desc = txd; 473ae10d1afSLuigi Rizzo na->num_rx_rings = rxr; 474ae10d1afSLuigi Rizzo na->num_rx_desc = rxd; 475ae10d1afSLuigi Rizzo return 0; 476ae10d1afSLuigi Rizzo } 477ae10d1afSLuigi Rizzo D("configuration changed while active, this is bad..."); 478ae10d1afSLuigi Rizzo return 1; 479ae10d1afSLuigi Rizzo } 480ae10d1afSLuigi Rizzo 4813c0caf6cSLuigi Rizzo /*------------- memory allocator -----------------*/ 4823c0caf6cSLuigi Rizzo #include "netmap_mem2.c" 4833c0caf6cSLuigi Rizzo /*------------ end of memory allocator ----------*/ 48468b8534bSLuigi Rizzo 4858241616dSLuigi Rizzo 4868241616dSLuigi Rizzo /* Structure associated to each thread which registered an interface. 4878241616dSLuigi Rizzo * 4888241616dSLuigi Rizzo * The first 4 fields of this structure are written by NIOCREGIF and 4898241616dSLuigi Rizzo * read by poll() and NIOC?XSYNC. 4908241616dSLuigi Rizzo * There is low contention among writers (actually, a correct user program 4918241616dSLuigi Rizzo * should have no contention among writers) and among writers and readers, 4928241616dSLuigi Rizzo * so we use a single global lock to protect the structure initialization. 4938241616dSLuigi Rizzo * Since initialization involves the allocation of memory, we reuse the memory 4948241616dSLuigi Rizzo * allocator lock. 4958241616dSLuigi Rizzo * Read access to the structure is lock free. Readers must check that 4968241616dSLuigi Rizzo * np_nifp is not NULL before using the other fields. 4978241616dSLuigi Rizzo * If np_nifp is NULL initialization has not been performed, so they should 4988241616dSLuigi Rizzo * return an error to userlevel. 4998241616dSLuigi Rizzo * 5008241616dSLuigi Rizzo * The ref_done field is used to regulate access to the refcount in the 5018241616dSLuigi Rizzo * memory allocator. The refcount must be incremented at most once for 5028241616dSLuigi Rizzo * each open("/dev/netmap"). The increment is performed by the first 5038241616dSLuigi Rizzo * function that calls netmap_get_memory() (currently called by 5048241616dSLuigi Rizzo * mmap(), NIOCGINFO and NIOCREGIF). 5058241616dSLuigi Rizzo * If the refcount is incremented, it is then decremented when the 5068241616dSLuigi Rizzo * private structure is destroyed. 5078241616dSLuigi Rizzo */ 50868b8534bSLuigi Rizzo struct netmap_priv_d { 5098241616dSLuigi Rizzo struct netmap_if * volatile np_nifp; /* netmap interface descriptor. */ 51068b8534bSLuigi Rizzo 51168b8534bSLuigi Rizzo struct ifnet *np_ifp; /* device for which we hold a reference */ 51268b8534bSLuigi Rizzo int np_ringid; /* from the ioctl */ 51368b8534bSLuigi Rizzo u_int np_qfirst, np_qlast; /* range of rings to scan */ 51468b8534bSLuigi Rizzo uint16_t np_txpoll; 5158241616dSLuigi Rizzo 5168241616dSLuigi Rizzo unsigned long ref_done; /* use with NMA_LOCK held */ 51768b8534bSLuigi Rizzo }; 51868b8534bSLuigi Rizzo 51968b8534bSLuigi Rizzo 5208241616dSLuigi Rizzo static int 5218241616dSLuigi Rizzo netmap_get_memory(struct netmap_priv_d* p) 5228241616dSLuigi Rizzo { 5238241616dSLuigi Rizzo int error = 0; 5248241616dSLuigi Rizzo NMA_LOCK(); 5258241616dSLuigi Rizzo if (!p->ref_done) { 5268241616dSLuigi Rizzo error = netmap_memory_finalize(); 5278241616dSLuigi Rizzo if (!error) 5288241616dSLuigi Rizzo p->ref_done = 1; 5298241616dSLuigi Rizzo } 5308241616dSLuigi Rizzo NMA_UNLOCK(); 5318241616dSLuigi Rizzo return error; 5328241616dSLuigi Rizzo } 5338241616dSLuigi Rizzo 53468b8534bSLuigi Rizzo /* 53568b8534bSLuigi Rizzo * File descriptor's private data destructor. 53668b8534bSLuigi Rizzo * 53768b8534bSLuigi Rizzo * Call nm_register(ifp,0) to stop netmap mode on the interface and 53868b8534bSLuigi Rizzo * revert to normal operation. We expect that np_ifp has not gone. 53968b8534bSLuigi Rizzo */ 5408241616dSLuigi Rizzo /* call with NMA_LOCK held */ 54168b8534bSLuigi Rizzo static void 5425819da83SLuigi Rizzo netmap_dtor_locked(void *data) 54368b8534bSLuigi Rizzo { 54468b8534bSLuigi Rizzo struct netmap_priv_d *priv = data; 54568b8534bSLuigi Rizzo struct ifnet *ifp = priv->np_ifp; 54668b8534bSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 54768b8534bSLuigi Rizzo struct netmap_if *nifp = priv->np_nifp; 54868b8534bSLuigi Rizzo 54968b8534bSLuigi Rizzo na->refcount--; 55068b8534bSLuigi Rizzo if (na->refcount <= 0) { /* last instance */ 55164ae02c3SLuigi Rizzo u_int i, j, lim; 55268b8534bSLuigi Rizzo 553ae10d1afSLuigi Rizzo if (netmap_verbose) 554ae10d1afSLuigi Rizzo D("deleting last instance for %s", ifp->if_xname); 55568b8534bSLuigi Rizzo /* 556f18be576SLuigi Rizzo * (TO CHECK) This function is only called 557f18be576SLuigi Rizzo * when the last reference to this file descriptor goes 558f18be576SLuigi Rizzo * away. This means we cannot have any pending poll() 559f18be576SLuigi Rizzo * or interrupt routine operating on the structure. 56068b8534bSLuigi Rizzo */ 56168b8534bSLuigi Rizzo na->nm_register(ifp, 0); /* off, clear IFCAP_NETMAP */ 56268b8534bSLuigi Rizzo /* Wake up any sleeping threads. netmap_poll will 56368b8534bSLuigi Rizzo * then return POLLERR 56468b8534bSLuigi Rizzo */ 565d76bf4ffSLuigi Rizzo for (i = 0; i < na->num_tx_rings + 1; i++) 56668b8534bSLuigi Rizzo selwakeuppri(&na->tx_rings[i].si, PI_NET); 567d76bf4ffSLuigi Rizzo for (i = 0; i < na->num_rx_rings + 1; i++) 56868b8534bSLuigi Rizzo selwakeuppri(&na->rx_rings[i].si, PI_NET); 56964ae02c3SLuigi Rizzo selwakeuppri(&na->tx_si, PI_NET); 57064ae02c3SLuigi Rizzo selwakeuppri(&na->rx_si, PI_NET); 571f18be576SLuigi Rizzo #ifdef NM_BRIDGE 572f18be576SLuigi Rizzo nm_free_bdgfwd(na); 573f18be576SLuigi Rizzo #endif /* NM_BRIDGE */ 57468b8534bSLuigi Rizzo /* release all buffers */ 575d76bf4ffSLuigi Rizzo for (i = 0; i < na->num_tx_rings + 1; i++) { 57664ae02c3SLuigi Rizzo struct netmap_ring *ring = na->tx_rings[i].ring; 57768b8534bSLuigi Rizzo lim = na->tx_rings[i].nkr_num_slots; 57868b8534bSLuigi Rizzo for (j = 0; j < lim; j++) 579446ee301SLuigi Rizzo netmap_free_buf(nifp, ring->slot[j].buf_idx); 5802f70fca5SEd Maste /* knlist_destroy(&na->tx_rings[i].si.si_note); */ 5812f70fca5SEd Maste mtx_destroy(&na->tx_rings[i].q_lock); 58264ae02c3SLuigi Rizzo } 583d76bf4ffSLuigi Rizzo for (i = 0; i < na->num_rx_rings + 1; i++) { 58464ae02c3SLuigi Rizzo struct netmap_ring *ring = na->rx_rings[i].ring; 58568b8534bSLuigi Rizzo lim = na->rx_rings[i].nkr_num_slots; 58668b8534bSLuigi Rizzo for (j = 0; j < lim; j++) 587446ee301SLuigi Rizzo netmap_free_buf(nifp, ring->slot[j].buf_idx); 5882f70fca5SEd Maste /* knlist_destroy(&na->rx_rings[i].si.si_note); */ 5892f70fca5SEd Maste mtx_destroy(&na->rx_rings[i].q_lock); 59068b8534bSLuigi Rizzo } 5912f70fca5SEd Maste /* XXX kqueue(9) needed; these will mirror knlist_init. */ 5922f70fca5SEd Maste /* knlist_destroy(&na->tx_si.si_note); */ 5932f70fca5SEd Maste /* knlist_destroy(&na->rx_si.si_note); */ 5946e10c8b8SLuigi Rizzo netmap_free_rings(na); 595f18be576SLuigi Rizzo if (nma_is_hw(na)) 596f18be576SLuigi Rizzo SWNA(ifp)->tx_rings = SWNA(ifp)->rx_rings = NULL; 59768b8534bSLuigi Rizzo } 5986e10c8b8SLuigi Rizzo netmap_if_free(nifp); 5995819da83SLuigi Rizzo } 60068b8534bSLuigi Rizzo 601f18be576SLuigi Rizzo 602f18be576SLuigi Rizzo /* we assume netmap adapter exists */ 603f196ce38SLuigi Rizzo static void 604f196ce38SLuigi Rizzo nm_if_rele(struct ifnet *ifp) 605f196ce38SLuigi Rizzo { 606f196ce38SLuigi Rizzo #ifndef NM_BRIDGE 607f196ce38SLuigi Rizzo if_rele(ifp); 608f196ce38SLuigi Rizzo #else /* NM_BRIDGE */ 609f18be576SLuigi Rizzo int i, full = 0, is_hw; 610f196ce38SLuigi Rizzo struct nm_bridge *b; 611f18be576SLuigi Rizzo struct netmap_adapter *na; 612f196ce38SLuigi Rizzo 613f18be576SLuigi Rizzo /* I can be called not only for get_ifp()-ed references where netmap's 614f18be576SLuigi Rizzo * capability is guaranteed, but also for non-netmap-capable NICs. 615f18be576SLuigi Rizzo */ 616f18be576SLuigi Rizzo if (!NETMAP_CAPABLE(ifp) || !NA(ifp)->na_bdg) { 617f196ce38SLuigi Rizzo if_rele(ifp); 618f196ce38SLuigi Rizzo return; 619f196ce38SLuigi Rizzo } 620f196ce38SLuigi Rizzo if (!DROP_BDG_REF(ifp)) 621f196ce38SLuigi Rizzo return; 622f18be576SLuigi Rizzo 623f18be576SLuigi Rizzo na = NA(ifp); 624f18be576SLuigi Rizzo b = na->na_bdg; 625f18be576SLuigi Rizzo is_hw = nma_is_hw(na); 626f18be576SLuigi Rizzo 627f18be576SLuigi Rizzo BDG_WLOCK(b); 628f196ce38SLuigi Rizzo ND("want to disconnect %s from the bridge", ifp->if_xname); 629f196ce38SLuigi Rizzo full = 0; 630f18be576SLuigi Rizzo /* remove the entry from the bridge, also check 631f18be576SLuigi Rizzo * if there are any leftover interfaces 632f18be576SLuigi Rizzo * XXX we should optimize this code, e.g. going directly 633f18be576SLuigi Rizzo * to na->bdg_port, and having a counter of ports that 634f18be576SLuigi Rizzo * are connected. But it is not in a critical path. 635f18be576SLuigi Rizzo * In NIC's case, index of sw na is always higher than hw na 636f18be576SLuigi Rizzo */ 637f196ce38SLuigi Rizzo for (i = 0; i < NM_BDG_MAXPORTS; i++) { 638f18be576SLuigi Rizzo struct netmap_adapter *tmp = BDG_GET_VAR(b->bdg_ports[i]); 639f18be576SLuigi Rizzo 640f18be576SLuigi Rizzo if (tmp == na) { 641f18be576SLuigi Rizzo /* disconnect from bridge */ 642f18be576SLuigi Rizzo BDG_SET_VAR(b->bdg_ports[i], NULL); 643f18be576SLuigi Rizzo na->na_bdg = NULL; 644f18be576SLuigi Rizzo if (is_hw && SWNA(ifp)->na_bdg) { 645f18be576SLuigi Rizzo /* disconnect sw adapter too */ 646f18be576SLuigi Rizzo int j = SWNA(ifp)->bdg_port; 647f18be576SLuigi Rizzo BDG_SET_VAR(b->bdg_ports[j], NULL); 648f18be576SLuigi Rizzo SWNA(ifp)->na_bdg = NULL; 649f196ce38SLuigi Rizzo } 650f18be576SLuigi Rizzo } else if (tmp != NULL) { 651f196ce38SLuigi Rizzo full = 1; 652f196ce38SLuigi Rizzo } 653f196ce38SLuigi Rizzo } 654f18be576SLuigi Rizzo BDG_WUNLOCK(b); 655f18be576SLuigi Rizzo if (full == 0) { 656f18be576SLuigi Rizzo ND("marking bridge %d as free", b - nm_bridges); 657f18be576SLuigi Rizzo b->namelen = 0; 658f18be576SLuigi Rizzo b->nm_bdg_lookup = NULL; 659f18be576SLuigi Rizzo } 660f18be576SLuigi Rizzo if (na->na_bdg) { /* still attached to the bridge */ 661f196ce38SLuigi Rizzo D("ouch, cannot find ifp to remove"); 662f18be576SLuigi Rizzo } else if (is_hw) { 663f18be576SLuigi Rizzo if_rele(ifp); 664f18be576SLuigi Rizzo } else { 665f18be576SLuigi Rizzo bzero(na, sizeof(*na)); 666f18be576SLuigi Rizzo free(na, M_DEVBUF); 667f18be576SLuigi Rizzo bzero(ifp, sizeof(*ifp)); 668f18be576SLuigi Rizzo free(ifp, M_DEVBUF); 669f18be576SLuigi Rizzo } 670f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */ 671f196ce38SLuigi Rizzo } 6725819da83SLuigi Rizzo 6735819da83SLuigi Rizzo static void 6745819da83SLuigi Rizzo netmap_dtor(void *data) 6755819da83SLuigi Rizzo { 6765819da83SLuigi Rizzo struct netmap_priv_d *priv = data; 6775819da83SLuigi Rizzo struct ifnet *ifp = priv->np_ifp; 6785819da83SLuigi Rizzo 6798241616dSLuigi Rizzo NMA_LOCK(); 6808241616dSLuigi Rizzo if (ifp) { 6812579e2d7SLuigi Rizzo struct netmap_adapter *na = NA(ifp); 6822579e2d7SLuigi Rizzo 683f18be576SLuigi Rizzo if (na->na_bdg) 684f18be576SLuigi Rizzo BDG_WLOCK(na->na_bdg); 6851a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_REG_LOCK, 0); 6865819da83SLuigi Rizzo netmap_dtor_locked(data); 6871a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0); 688f18be576SLuigi Rizzo if (na->na_bdg) 689f18be576SLuigi Rizzo BDG_WUNLOCK(na->na_bdg); 69068b8534bSLuigi Rizzo 6912579e2d7SLuigi Rizzo nm_if_rele(ifp); /* might also destroy *na */ 6928241616dSLuigi Rizzo } 6938241616dSLuigi Rizzo if (priv->ref_done) { 6948241616dSLuigi Rizzo netmap_memory_deref(); 6958241616dSLuigi Rizzo } 6968241616dSLuigi Rizzo NMA_UNLOCK(); 69768b8534bSLuigi Rizzo bzero(priv, sizeof(*priv)); /* XXX for safety */ 69868b8534bSLuigi Rizzo free(priv, M_DEVBUF); 69968b8534bSLuigi Rizzo } 70068b8534bSLuigi Rizzo 701f18be576SLuigi Rizzo 7028241616dSLuigi Rizzo #ifdef __FreeBSD__ 7038241616dSLuigi Rizzo #include <vm/vm.h> 7048241616dSLuigi Rizzo #include <vm/vm_param.h> 7058241616dSLuigi Rizzo #include <vm/vm_object.h> 7068241616dSLuigi Rizzo #include <vm/vm_page.h> 7078241616dSLuigi Rizzo #include <vm/vm_pager.h> 7088241616dSLuigi Rizzo #include <vm/uma.h> 7098241616dSLuigi Rizzo 710f18be576SLuigi Rizzo /* 711f18be576SLuigi Rizzo * In order to track whether pages are still mapped, we hook into 712f18be576SLuigi Rizzo * the standard cdev_pager and intercept the constructor and 713f18be576SLuigi Rizzo * destructor. 714f18be576SLuigi Rizzo * XXX but then ? Do we really use the information ? 715f18be576SLuigi Rizzo * Need to investigate. 716f18be576SLuigi Rizzo */ 7178241616dSLuigi Rizzo static struct cdev_pager_ops saved_cdev_pager_ops; 7188241616dSLuigi Rizzo 719f18be576SLuigi Rizzo 7208241616dSLuigi Rizzo static int 7218241616dSLuigi Rizzo netmap_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot, 7228241616dSLuigi Rizzo vm_ooffset_t foff, struct ucred *cred, u_short *color) 7238241616dSLuigi Rizzo { 724ae10d1afSLuigi Rizzo if (netmap_verbose) 7258241616dSLuigi Rizzo D("first mmap for %p", handle); 7268241616dSLuigi Rizzo return saved_cdev_pager_ops.cdev_pg_ctor(handle, 7278241616dSLuigi Rizzo size, prot, foff, cred, color); 7288241616dSLuigi Rizzo } 7298241616dSLuigi Rizzo 730f18be576SLuigi Rizzo 7318241616dSLuigi Rizzo static void 7328241616dSLuigi Rizzo netmap_dev_pager_dtor(void *handle) 7338241616dSLuigi Rizzo { 7348241616dSLuigi Rizzo saved_cdev_pager_ops.cdev_pg_dtor(handle); 735ae10d1afSLuigi Rizzo ND("ready to release memory for %p", handle); 7368241616dSLuigi Rizzo } 7378241616dSLuigi Rizzo 7388241616dSLuigi Rizzo 7398241616dSLuigi Rizzo static struct cdev_pager_ops netmap_cdev_pager_ops = { 7408241616dSLuigi Rizzo .cdev_pg_ctor = netmap_dev_pager_ctor, 7418241616dSLuigi Rizzo .cdev_pg_dtor = netmap_dev_pager_dtor, 7428241616dSLuigi Rizzo .cdev_pg_fault = NULL, 7438241616dSLuigi Rizzo }; 7448241616dSLuigi Rizzo 745f18be576SLuigi Rizzo 746f18be576SLuigi Rizzo // XXX check whether we need netmap_mmap_single _and_ netmap_mmap 7478241616dSLuigi Rizzo static int 7488241616dSLuigi Rizzo netmap_mmap_single(struct cdev *cdev, vm_ooffset_t *foff, 7498241616dSLuigi Rizzo vm_size_t objsize, vm_object_t *objp, int prot) 7508241616dSLuigi Rizzo { 7518241616dSLuigi Rizzo vm_object_t obj; 7528241616dSLuigi Rizzo 753ae10d1afSLuigi Rizzo ND("cdev %p foff %jd size %jd objp %p prot %d", cdev, 75488f79057SGleb Smirnoff (intmax_t )*foff, (intmax_t )objsize, objp, prot); 7558241616dSLuigi Rizzo obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff, 7568241616dSLuigi Rizzo curthread->td_ucred); 7578241616dSLuigi Rizzo ND("returns obj %p", obj); 7588241616dSLuigi Rizzo if (obj == NULL) 7598241616dSLuigi Rizzo return EINVAL; 7608241616dSLuigi Rizzo if (saved_cdev_pager_ops.cdev_pg_fault == NULL) { 761ae10d1afSLuigi Rizzo ND("initialize cdev_pager_ops"); 7628241616dSLuigi Rizzo saved_cdev_pager_ops = *(obj->un_pager.devp.ops); 7638241616dSLuigi Rizzo netmap_cdev_pager_ops.cdev_pg_fault = 7648241616dSLuigi Rizzo saved_cdev_pager_ops.cdev_pg_fault; 7658241616dSLuigi Rizzo }; 7668241616dSLuigi Rizzo obj->un_pager.devp.ops = &netmap_cdev_pager_ops; 7678241616dSLuigi Rizzo *objp = obj; 7688241616dSLuigi Rizzo return 0; 7698241616dSLuigi Rizzo } 7708241616dSLuigi Rizzo #endif /* __FreeBSD__ */ 7718241616dSLuigi Rizzo 77268b8534bSLuigi Rizzo 77368b8534bSLuigi Rizzo /* 77468b8534bSLuigi Rizzo * mmap(2) support for the "netmap" device. 77568b8534bSLuigi Rizzo * 77668b8534bSLuigi Rizzo * Expose all the memory previously allocated by our custom memory 77768b8534bSLuigi Rizzo * allocator: this way the user has only to issue a single mmap(2), and 77868b8534bSLuigi Rizzo * can work on all the data structures flawlessly. 77968b8534bSLuigi Rizzo * 78068b8534bSLuigi Rizzo * Return 0 on success, -1 otherwise. 78168b8534bSLuigi Rizzo */ 782babc7c12SLuigi Rizzo 783f196ce38SLuigi Rizzo #ifdef __FreeBSD__ 78468b8534bSLuigi Rizzo static int 785babc7c12SLuigi Rizzo netmap_mmap(__unused struct cdev *dev, 78668b8534bSLuigi Rizzo #if __FreeBSD_version < 900000 787babc7c12SLuigi Rizzo vm_offset_t offset, vm_paddr_t *paddr, int nprot 78868b8534bSLuigi Rizzo #else 789babc7c12SLuigi Rizzo vm_ooffset_t offset, vm_paddr_t *paddr, int nprot, 790babc7c12SLuigi Rizzo __unused vm_memattr_t *memattr 79168b8534bSLuigi Rizzo #endif 792babc7c12SLuigi Rizzo ) 79368b8534bSLuigi Rizzo { 7948241616dSLuigi Rizzo int error = 0; 7958241616dSLuigi Rizzo struct netmap_priv_d *priv; 7968241616dSLuigi Rizzo 79768b8534bSLuigi Rizzo if (nprot & PROT_EXEC) 79868b8534bSLuigi Rizzo return (-1); // XXX -1 or EINVAL ? 799446ee301SLuigi Rizzo 8008241616dSLuigi Rizzo error = devfs_get_cdevpriv((void **)&priv); 8018241616dSLuigi Rizzo if (error == EBADF) { /* called on fault, memory is initialized */ 8028241616dSLuigi Rizzo ND(5, "handling fault at ofs 0x%x", offset); 8038241616dSLuigi Rizzo error = 0; 8048241616dSLuigi Rizzo } else if (error == 0) /* make sure memory is set */ 8058241616dSLuigi Rizzo error = netmap_get_memory(priv); 8068241616dSLuigi Rizzo if (error) 8078241616dSLuigi Rizzo return (error); 8088241616dSLuigi Rizzo 80968b8534bSLuigi Rizzo ND("request for offset 0x%x", (uint32_t)offset); 810446ee301SLuigi Rizzo *paddr = netmap_ofstophys(offset); 81168b8534bSLuigi Rizzo 8128241616dSLuigi Rizzo return (*paddr ? 0 : ENOMEM); 8138241616dSLuigi Rizzo } 8148241616dSLuigi Rizzo 815f18be576SLuigi Rizzo 8168241616dSLuigi Rizzo static int 8178241616dSLuigi Rizzo netmap_close(struct cdev *dev, int fflag, int devtype, struct thread *td) 8188241616dSLuigi Rizzo { 819ae10d1afSLuigi Rizzo if (netmap_verbose) 820ae10d1afSLuigi Rizzo D("dev %p fflag 0x%x devtype %d td %p", 821ae10d1afSLuigi Rizzo dev, fflag, devtype, td); 8228241616dSLuigi Rizzo return 0; 8238241616dSLuigi Rizzo } 8248241616dSLuigi Rizzo 825f18be576SLuigi Rizzo 8268241616dSLuigi Rizzo static int 8278241616dSLuigi Rizzo netmap_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 8288241616dSLuigi Rizzo { 8298241616dSLuigi Rizzo struct netmap_priv_d *priv; 8308241616dSLuigi Rizzo int error; 8318241616dSLuigi Rizzo 8328241616dSLuigi Rizzo priv = malloc(sizeof(struct netmap_priv_d), M_DEVBUF, 8338241616dSLuigi Rizzo M_NOWAIT | M_ZERO); 8348241616dSLuigi Rizzo if (priv == NULL) 8358241616dSLuigi Rizzo return ENOMEM; 8368241616dSLuigi Rizzo 8378241616dSLuigi Rizzo error = devfs_set_cdevpriv(priv, netmap_dtor); 8388241616dSLuigi Rizzo if (error) 8398241616dSLuigi Rizzo return error; 8408241616dSLuigi Rizzo 8418241616dSLuigi Rizzo return 0; 84268b8534bSLuigi Rizzo } 843f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */ 84468b8534bSLuigi Rizzo 84568b8534bSLuigi Rizzo 84668b8534bSLuigi Rizzo /* 84702ad4083SLuigi Rizzo * Handlers for synchronization of the queues from/to the host. 848091fd0abSLuigi Rizzo * Netmap has two operating modes: 849091fd0abSLuigi Rizzo * - in the default mode, the rings connected to the host stack are 850091fd0abSLuigi Rizzo * just another ring pair managed by userspace; 851091fd0abSLuigi Rizzo * - in transparent mode (XXX to be defined) incoming packets 852091fd0abSLuigi Rizzo * (from the host or the NIC) are marked as NS_FORWARD upon 853091fd0abSLuigi Rizzo * arrival, and the user application has a chance to reset the 854091fd0abSLuigi Rizzo * flag for packets that should be dropped. 855091fd0abSLuigi Rizzo * On the RXSYNC or poll(), packets in RX rings between 856091fd0abSLuigi Rizzo * kring->nr_kcur and ring->cur with NS_FORWARD still set are moved 857091fd0abSLuigi Rizzo * to the other side. 858091fd0abSLuigi Rizzo * The transfer NIC --> host is relatively easy, just encapsulate 859091fd0abSLuigi Rizzo * into mbufs and we are done. The host --> NIC side is slightly 860091fd0abSLuigi Rizzo * harder because there might not be room in the tx ring so it 861091fd0abSLuigi Rizzo * might take a while before releasing the buffer. 862091fd0abSLuigi Rizzo */ 863091fd0abSLuigi Rizzo 864f18be576SLuigi Rizzo 865091fd0abSLuigi Rizzo /* 866091fd0abSLuigi Rizzo * pass a chain of buffers to the host stack as coming from 'dst' 867091fd0abSLuigi Rizzo */ 868091fd0abSLuigi Rizzo static void 869091fd0abSLuigi Rizzo netmap_send_up(struct ifnet *dst, struct mbuf *head) 870091fd0abSLuigi Rizzo { 871091fd0abSLuigi Rizzo struct mbuf *m; 872091fd0abSLuigi Rizzo 873091fd0abSLuigi Rizzo /* send packets up, outside the lock */ 874091fd0abSLuigi Rizzo while ((m = head) != NULL) { 875091fd0abSLuigi Rizzo head = head->m_nextpkt; 876091fd0abSLuigi Rizzo m->m_nextpkt = NULL; 877091fd0abSLuigi Rizzo if (netmap_verbose & NM_VERB_HOST) 878091fd0abSLuigi Rizzo D("sending up pkt %p size %d", m, MBUF_LEN(m)); 879091fd0abSLuigi Rizzo NM_SEND_UP(dst, m); 880091fd0abSLuigi Rizzo } 881091fd0abSLuigi Rizzo } 882091fd0abSLuigi Rizzo 883091fd0abSLuigi Rizzo struct mbq { 884091fd0abSLuigi Rizzo struct mbuf *head; 885091fd0abSLuigi Rizzo struct mbuf *tail; 886091fd0abSLuigi Rizzo int count; 887091fd0abSLuigi Rizzo }; 888091fd0abSLuigi Rizzo 889f18be576SLuigi Rizzo 890091fd0abSLuigi Rizzo /* 891091fd0abSLuigi Rizzo * put a copy of the buffers marked NS_FORWARD into an mbuf chain. 892091fd0abSLuigi Rizzo * Run from hwcur to cur - reserved 893091fd0abSLuigi Rizzo */ 894091fd0abSLuigi Rizzo static void 895091fd0abSLuigi Rizzo netmap_grab_packets(struct netmap_kring *kring, struct mbq *q, int force) 896091fd0abSLuigi Rizzo { 897091fd0abSLuigi Rizzo /* Take packets from hwcur to cur-reserved and pass them up. 898091fd0abSLuigi Rizzo * In case of no buffers we give up. At the end of the loop, 899091fd0abSLuigi Rizzo * the queue is drained in all cases. 900091fd0abSLuigi Rizzo * XXX handle reserved 901091fd0abSLuigi Rizzo */ 902091fd0abSLuigi Rizzo int k = kring->ring->cur - kring->ring->reserved; 903091fd0abSLuigi Rizzo u_int n, lim = kring->nkr_num_slots - 1; 904091fd0abSLuigi Rizzo struct mbuf *m, *tail = q->tail; 905091fd0abSLuigi Rizzo 906091fd0abSLuigi Rizzo if (k < 0) 907091fd0abSLuigi Rizzo k = k + kring->nkr_num_slots; 908091fd0abSLuigi Rizzo for (n = kring->nr_hwcur; n != k;) { 909091fd0abSLuigi Rizzo struct netmap_slot *slot = &kring->ring->slot[n]; 910091fd0abSLuigi Rizzo 911091fd0abSLuigi Rizzo n = (n == lim) ? 0 : n + 1; 912091fd0abSLuigi Rizzo if ((slot->flags & NS_FORWARD) == 0 && !force) 913091fd0abSLuigi Rizzo continue; 914091fd0abSLuigi Rizzo if (slot->len < 14 || slot->len > NETMAP_BUF_SIZE) { 915091fd0abSLuigi Rizzo D("bad pkt at %d len %d", n, slot->len); 916091fd0abSLuigi Rizzo continue; 917091fd0abSLuigi Rizzo } 918091fd0abSLuigi Rizzo slot->flags &= ~NS_FORWARD; // XXX needed ? 919091fd0abSLuigi Rizzo m = m_devget(NMB(slot), slot->len, 0, kring->na->ifp, NULL); 920091fd0abSLuigi Rizzo 921091fd0abSLuigi Rizzo if (m == NULL) 922091fd0abSLuigi Rizzo break; 923091fd0abSLuigi Rizzo if (tail) 924091fd0abSLuigi Rizzo tail->m_nextpkt = m; 925091fd0abSLuigi Rizzo else 926091fd0abSLuigi Rizzo q->head = m; 927091fd0abSLuigi Rizzo tail = m; 928091fd0abSLuigi Rizzo q->count++; 929091fd0abSLuigi Rizzo m->m_nextpkt = NULL; 930091fd0abSLuigi Rizzo } 931091fd0abSLuigi Rizzo q->tail = tail; 932091fd0abSLuigi Rizzo } 933091fd0abSLuigi Rizzo 934f18be576SLuigi Rizzo 935091fd0abSLuigi Rizzo /* 936091fd0abSLuigi Rizzo * called under main lock to send packets from the host to the NIC 937091fd0abSLuigi Rizzo * The host ring has packets from nr_hwcur to (cur - reserved) 938091fd0abSLuigi Rizzo * to be sent down. We scan the tx rings, which have just been 939091fd0abSLuigi Rizzo * flushed so nr_hwcur == cur. Pushing packets down means 940091fd0abSLuigi Rizzo * increment cur and decrement avail. 941091fd0abSLuigi Rizzo * XXX to be verified 942091fd0abSLuigi Rizzo */ 943091fd0abSLuigi Rizzo static void 944091fd0abSLuigi Rizzo netmap_sw_to_nic(struct netmap_adapter *na) 945091fd0abSLuigi Rizzo { 946091fd0abSLuigi Rizzo struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings]; 947091fd0abSLuigi Rizzo struct netmap_kring *k1 = &na->tx_rings[0]; 948091fd0abSLuigi Rizzo int i, howmany, src_lim, dst_lim; 949091fd0abSLuigi Rizzo 950091fd0abSLuigi Rizzo howmany = kring->nr_hwavail; /* XXX otherwise cur - reserved - nr_hwcur */ 951091fd0abSLuigi Rizzo 952091fd0abSLuigi Rizzo src_lim = kring->nkr_num_slots; 953091fd0abSLuigi Rizzo for (i = 0; howmany > 0 && i < na->num_tx_rings; i++, k1++) { 954091fd0abSLuigi Rizzo ND("%d packets left to ring %d (space %d)", howmany, i, k1->nr_hwavail); 955091fd0abSLuigi Rizzo dst_lim = k1->nkr_num_slots; 956091fd0abSLuigi Rizzo while (howmany > 0 && k1->ring->avail > 0) { 957091fd0abSLuigi Rizzo struct netmap_slot *src, *dst, tmp; 958091fd0abSLuigi Rizzo src = &kring->ring->slot[kring->nr_hwcur]; 959091fd0abSLuigi Rizzo dst = &k1->ring->slot[k1->ring->cur]; 960091fd0abSLuigi Rizzo tmp = *src; 961091fd0abSLuigi Rizzo src->buf_idx = dst->buf_idx; 962091fd0abSLuigi Rizzo src->flags = NS_BUF_CHANGED; 963091fd0abSLuigi Rizzo 964091fd0abSLuigi Rizzo dst->buf_idx = tmp.buf_idx; 965091fd0abSLuigi Rizzo dst->len = tmp.len; 966091fd0abSLuigi Rizzo dst->flags = NS_BUF_CHANGED; 967091fd0abSLuigi Rizzo ND("out len %d buf %d from %d to %d", 968091fd0abSLuigi Rizzo dst->len, dst->buf_idx, 969091fd0abSLuigi Rizzo kring->nr_hwcur, k1->ring->cur); 970091fd0abSLuigi Rizzo 971091fd0abSLuigi Rizzo if (++kring->nr_hwcur >= src_lim) 972091fd0abSLuigi Rizzo kring->nr_hwcur = 0; 973091fd0abSLuigi Rizzo howmany--; 974091fd0abSLuigi Rizzo kring->nr_hwavail--; 975091fd0abSLuigi Rizzo if (++k1->ring->cur >= dst_lim) 976091fd0abSLuigi Rizzo k1->ring->cur = 0; 977091fd0abSLuigi Rizzo k1->ring->avail--; 978091fd0abSLuigi Rizzo } 979091fd0abSLuigi Rizzo kring->ring->cur = kring->nr_hwcur; // XXX 980091fd0abSLuigi Rizzo k1++; 981091fd0abSLuigi Rizzo } 982091fd0abSLuigi Rizzo } 983091fd0abSLuigi Rizzo 984f18be576SLuigi Rizzo 985091fd0abSLuigi Rizzo /* 98602ad4083SLuigi Rizzo * netmap_sync_to_host() passes packets up. We are called from a 98702ad4083SLuigi Rizzo * system call in user process context, and the only contention 98802ad4083SLuigi Rizzo * can be among multiple user threads erroneously calling 989091fd0abSLuigi Rizzo * this routine concurrently. 99068b8534bSLuigi Rizzo */ 99168b8534bSLuigi Rizzo static void 99268b8534bSLuigi Rizzo netmap_sync_to_host(struct netmap_adapter *na) 99368b8534bSLuigi Rizzo { 994d76bf4ffSLuigi Rizzo struct netmap_kring *kring = &na->tx_rings[na->num_tx_rings]; 99568b8534bSLuigi Rizzo struct netmap_ring *ring = kring->ring; 996091fd0abSLuigi Rizzo u_int k, lim = kring->nkr_num_slots - 1; 997091fd0abSLuigi Rizzo struct mbq q = { NULL, NULL }; 99868b8534bSLuigi Rizzo 99902ad4083SLuigi Rizzo k = ring->cur; 100002ad4083SLuigi Rizzo if (k > lim) { 100102ad4083SLuigi Rizzo netmap_ring_reinit(kring); 100202ad4083SLuigi Rizzo return; 100302ad4083SLuigi Rizzo } 10041a26580eSLuigi Rizzo // na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0); 100568b8534bSLuigi Rizzo 100668b8534bSLuigi Rizzo /* Take packets from hwcur to cur and pass them up. 100768b8534bSLuigi Rizzo * In case of no buffers we give up. At the end of the loop, 100868b8534bSLuigi Rizzo * the queue is drained in all cases. 100968b8534bSLuigi Rizzo */ 1010091fd0abSLuigi Rizzo netmap_grab_packets(kring, &q, 1); 101102ad4083SLuigi Rizzo kring->nr_hwcur = k; 101268b8534bSLuigi Rizzo kring->nr_hwavail = ring->avail = lim; 10131a26580eSLuigi Rizzo // na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0); 101468b8534bSLuigi Rizzo 1015091fd0abSLuigi Rizzo netmap_send_up(na->ifp, q.head); 101668b8534bSLuigi Rizzo } 101768b8534bSLuigi Rizzo 1018f18be576SLuigi Rizzo 1019f18be576SLuigi Rizzo /* SWNA(ifp)->txrings[0] is always NA(ifp)->txrings[NA(ifp)->num_txrings] */ 1020f18be576SLuigi Rizzo static int 1021f18be576SLuigi Rizzo netmap_bdg_to_host(struct ifnet *ifp, u_int ring_nr, int do_lock) 1022f18be576SLuigi Rizzo { 1023f18be576SLuigi Rizzo (void)ring_nr; 1024f18be576SLuigi Rizzo (void)do_lock; 1025f18be576SLuigi Rizzo netmap_sync_to_host(NA(ifp)); 1026f18be576SLuigi Rizzo return 0; 1027f18be576SLuigi Rizzo } 1028f18be576SLuigi Rizzo 1029f18be576SLuigi Rizzo 103068b8534bSLuigi Rizzo /* 103102ad4083SLuigi Rizzo * rxsync backend for packets coming from the host stack. 103202ad4083SLuigi Rizzo * They have been put in the queue by netmap_start() so we 103302ad4083SLuigi Rizzo * need to protect access to the kring using a lock. 103402ad4083SLuigi Rizzo * 103568b8534bSLuigi Rizzo * This routine also does the selrecord if called from the poll handler 103668b8534bSLuigi Rizzo * (we know because td != NULL). 103701c7d25fSLuigi Rizzo * 103801c7d25fSLuigi Rizzo * NOTE: on linux, selrecord() is defined as a macro and uses pwait 103901c7d25fSLuigi Rizzo * as an additional hidden argument. 104068b8534bSLuigi Rizzo */ 104168b8534bSLuigi Rizzo static void 104201c7d25fSLuigi Rizzo netmap_sync_from_host(struct netmap_adapter *na, struct thread *td, void *pwait) 104368b8534bSLuigi Rizzo { 1044d76bf4ffSLuigi Rizzo struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings]; 104568b8534bSLuigi Rizzo struct netmap_ring *ring = kring->ring; 104664ae02c3SLuigi Rizzo u_int j, n, lim = kring->nkr_num_slots; 104764ae02c3SLuigi Rizzo u_int k = ring->cur, resvd = ring->reserved; 104868b8534bSLuigi Rizzo 104901c7d25fSLuigi Rizzo (void)pwait; /* disable unused warnings */ 10501a26580eSLuigi Rizzo na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0); 105164ae02c3SLuigi Rizzo if (k >= lim) { 105264ae02c3SLuigi Rizzo netmap_ring_reinit(kring); 105364ae02c3SLuigi Rizzo return; 105464ae02c3SLuigi Rizzo } 105564ae02c3SLuigi Rizzo /* new packets are already set in nr_hwavail */ 105664ae02c3SLuigi Rizzo /* skip past packets that userspace has released */ 105764ae02c3SLuigi Rizzo j = kring->nr_hwcur; 105864ae02c3SLuigi Rizzo if (resvd > 0) { 105964ae02c3SLuigi Rizzo if (resvd + ring->avail >= lim + 1) { 106064ae02c3SLuigi Rizzo D("XXX invalid reserve/avail %d %d", resvd, ring->avail); 106164ae02c3SLuigi Rizzo ring->reserved = resvd = 0; // XXX panic... 106264ae02c3SLuigi Rizzo } 106364ae02c3SLuigi Rizzo k = (k >= resvd) ? k - resvd : k + lim - resvd; 106464ae02c3SLuigi Rizzo } 106564ae02c3SLuigi Rizzo if (j != k) { 106664ae02c3SLuigi Rizzo n = k >= j ? k - j : k + lim - j; 106764ae02c3SLuigi Rizzo kring->nr_hwavail -= n; 106802ad4083SLuigi Rizzo kring->nr_hwcur = k; 106964ae02c3SLuigi Rizzo } 107064ae02c3SLuigi Rizzo k = ring->avail = kring->nr_hwavail - resvd; 107102ad4083SLuigi Rizzo if (k == 0 && td) 107268b8534bSLuigi Rizzo selrecord(td, &kring->si); 107302ad4083SLuigi Rizzo if (k && (netmap_verbose & NM_VERB_HOST)) 107402ad4083SLuigi Rizzo D("%d pkts from stack", k); 10751a26580eSLuigi Rizzo na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0); 107668b8534bSLuigi Rizzo } 107768b8534bSLuigi Rizzo 107868b8534bSLuigi Rizzo 107968b8534bSLuigi Rizzo /* 108068b8534bSLuigi Rizzo * get a refcounted reference to an interface. 108168b8534bSLuigi Rizzo * Return ENXIO if the interface does not exist, EINVAL if netmap 108268b8534bSLuigi Rizzo * is not supported by the interface. 108368b8534bSLuigi Rizzo * If successful, hold a reference. 1084f18be576SLuigi Rizzo * 1085f18be576SLuigi Rizzo * During the NIC is attached to a bridge, reference is managed 1086f18be576SLuigi Rizzo * at na->na_bdg_refcount using ADD/DROP_BDG_REF() as well as 1087f18be576SLuigi Rizzo * virtual ports. Hence, on the final DROP_BDG_REF(), the NIC 1088f18be576SLuigi Rizzo * is detached from the bridge, then ifp's refcount is dropped (this 1089f18be576SLuigi Rizzo * is equivalent to that ifp is destroyed in case of virtual ports. 1090f18be576SLuigi Rizzo * 1091f18be576SLuigi Rizzo * This function uses if_rele() when we want to prevent the NIC from 1092f18be576SLuigi Rizzo * being detached from the bridge in error handling. But once refcount 1093f18be576SLuigi Rizzo * is acquired by this function, it must be released using nm_if_rele(). 109468b8534bSLuigi Rizzo */ 109568b8534bSLuigi Rizzo static int 1096f18be576SLuigi Rizzo get_ifp(struct nmreq *nmr, struct ifnet **ifp) 109768b8534bSLuigi Rizzo { 1098f18be576SLuigi Rizzo const char *name = nmr->nr_name; 1099f18be576SLuigi Rizzo int namelen = strlen(name); 1100f196ce38SLuigi Rizzo #ifdef NM_BRIDGE 1101f196ce38SLuigi Rizzo struct ifnet *iter = NULL; 1102f18be576SLuigi Rizzo int no_prefix = 0; 1103f196ce38SLuigi Rizzo 1104f196ce38SLuigi Rizzo do { 1105f196ce38SLuigi Rizzo struct nm_bridge *b; 1106f18be576SLuigi Rizzo struct netmap_adapter *na; 1107f18be576SLuigi Rizzo int i, cand = -1, cand2 = -1; 1108f196ce38SLuigi Rizzo 1109f18be576SLuigi Rizzo if (strncmp(name, NM_NAME, sizeof(NM_NAME) - 1)) { 1110f18be576SLuigi Rizzo no_prefix = 1; 1111f196ce38SLuigi Rizzo break; 1112f18be576SLuigi Rizzo } 1113f18be576SLuigi Rizzo b = nm_find_bridge(name, 1 /* create a new one if no exist */ ); 1114f196ce38SLuigi Rizzo if (b == NULL) { 1115f196ce38SLuigi Rizzo D("no bridges available for '%s'", name); 1116f196ce38SLuigi Rizzo return (ENXIO); 1117f196ce38SLuigi Rizzo } 1118f18be576SLuigi Rizzo /* Now we are sure that name starts with the bridge's name */ 1119f18be576SLuigi Rizzo BDG_WLOCK(b); 1120f196ce38SLuigi Rizzo /* lookup in the local list of ports */ 1121f196ce38SLuigi Rizzo for (i = 0; i < NM_BDG_MAXPORTS; i++) { 1122f18be576SLuigi Rizzo na = BDG_GET_VAR(b->bdg_ports[i]); 1123f18be576SLuigi Rizzo if (na == NULL) { 1124f196ce38SLuigi Rizzo if (cand == -1) 1125f196ce38SLuigi Rizzo cand = i; /* potential insert point */ 1126f18be576SLuigi Rizzo else if (cand2 == -1) 1127f18be576SLuigi Rizzo cand2 = i; /* for host stack */ 1128f196ce38SLuigi Rizzo continue; 1129f196ce38SLuigi Rizzo } 1130f18be576SLuigi Rizzo iter = na->ifp; 1131f18be576SLuigi Rizzo /* XXX make sure the name only contains one : */ 1132f18be576SLuigi Rizzo if (!strcmp(iter->if_xname, name) /* virtual port */ || 1133f18be576SLuigi Rizzo (namelen > b->namelen && !strcmp(iter->if_xname, 1134f18be576SLuigi Rizzo name + b->namelen + 1)) /* NIC */) { 1135f196ce38SLuigi Rizzo ADD_BDG_REF(iter); 1136f196ce38SLuigi Rizzo ND("found existing interface"); 1137f18be576SLuigi Rizzo BDG_WUNLOCK(b); 1138f196ce38SLuigi Rizzo break; 1139f196ce38SLuigi Rizzo } 1140f196ce38SLuigi Rizzo } 1141f196ce38SLuigi Rizzo if (i < NM_BDG_MAXPORTS) /* already unlocked */ 1142f196ce38SLuigi Rizzo break; 1143f196ce38SLuigi Rizzo if (cand == -1) { 1144f196ce38SLuigi Rizzo D("bridge full, cannot create new port"); 1145f196ce38SLuigi Rizzo no_port: 1146f18be576SLuigi Rizzo BDG_WUNLOCK(b); 1147f196ce38SLuigi Rizzo *ifp = NULL; 1148f196ce38SLuigi Rizzo return EINVAL; 1149f196ce38SLuigi Rizzo } 1150f196ce38SLuigi Rizzo ND("create new bridge port %s", name); 1151f18be576SLuigi Rizzo /* 1152f18be576SLuigi Rizzo * create a struct ifnet for the new port. 1153f18be576SLuigi Rizzo * The forwarding table is attached to the kring(s). 1154f18be576SLuigi Rizzo */ 1155f18be576SLuigi Rizzo /* 1156f18be576SLuigi Rizzo * try see if there is a matching NIC with this name 1157f18be576SLuigi Rizzo * (after the bridge's name) 1158f18be576SLuigi Rizzo */ 1159f18be576SLuigi Rizzo iter = ifunit_ref(name + b->namelen + 1); 1160f18be576SLuigi Rizzo if (!iter) { /* this is a virtual port */ 1161f18be576SLuigi Rizzo /* Create a temporary NA with arguments, then 1162f18be576SLuigi Rizzo * bdg_netmap_attach() will allocate the real one 1163f18be576SLuigi Rizzo * and attach it to the ifp 1164f18be576SLuigi Rizzo */ 1165f18be576SLuigi Rizzo struct netmap_adapter tmp_na; 1166f18be576SLuigi Rizzo 1167f18be576SLuigi Rizzo if (nmr->nr_cmd) /* nr_cmd must be for a NIC */ 1168f18be576SLuigi Rizzo goto no_port; 1169f18be576SLuigi Rizzo bzero(&tmp_na, sizeof(tmp_na)); 1170f18be576SLuigi Rizzo /* bound checking */ 1171f18be576SLuigi Rizzo if (nmr->nr_tx_rings < 1) 1172f18be576SLuigi Rizzo nmr->nr_tx_rings = 1; 1173f18be576SLuigi Rizzo if (nmr->nr_tx_rings > NM_BDG_MAXRINGS) 1174f18be576SLuigi Rizzo nmr->nr_tx_rings = NM_BDG_MAXRINGS; 1175f18be576SLuigi Rizzo tmp_na.num_tx_rings = nmr->nr_tx_rings; 1176f18be576SLuigi Rizzo if (nmr->nr_rx_rings < 1) 1177f18be576SLuigi Rizzo nmr->nr_rx_rings = 1; 1178f18be576SLuigi Rizzo if (nmr->nr_rx_rings > NM_BDG_MAXRINGS) 1179f18be576SLuigi Rizzo nmr->nr_rx_rings = NM_BDG_MAXRINGS; 1180f18be576SLuigi Rizzo tmp_na.num_rx_rings = nmr->nr_rx_rings; 1181f18be576SLuigi Rizzo 1182f18be576SLuigi Rizzo iter = malloc(sizeof(*iter), M_DEVBUF, M_NOWAIT | M_ZERO); 1183f196ce38SLuigi Rizzo if (!iter) 1184f196ce38SLuigi Rizzo goto no_port; 1185f196ce38SLuigi Rizzo strcpy(iter->if_xname, name); 1186f18be576SLuigi Rizzo tmp_na.ifp = iter; 1187f18be576SLuigi Rizzo /* bdg_netmap_attach creates a struct netmap_adapter */ 1188f18be576SLuigi Rizzo bdg_netmap_attach(&tmp_na); 1189f18be576SLuigi Rizzo } else if (NETMAP_CAPABLE(iter)) { /* this is a NIC */ 1190f18be576SLuigi Rizzo /* cannot attach the NIC that any user or another 1191f18be576SLuigi Rizzo * bridge already holds. 1192f18be576SLuigi Rizzo */ 1193f18be576SLuigi Rizzo if (NETMAP_OWNED_BY_ANY(iter) || cand2 == -1) { 1194f18be576SLuigi Rizzo ifunit_rele: 1195f18be576SLuigi Rizzo if_rele(iter); /* don't detach from bridge */ 1196f18be576SLuigi Rizzo goto no_port; 1197f18be576SLuigi Rizzo } 1198f18be576SLuigi Rizzo /* bind the host stack to the bridge */ 1199f18be576SLuigi Rizzo if (nmr->nr_arg1 == NETMAP_BDG_HOST) { 1200f18be576SLuigi Rizzo BDG_SET_VAR(b->bdg_ports[cand2], SWNA(iter)); 1201f18be576SLuigi Rizzo SWNA(iter)->bdg_port = cand2; 1202f18be576SLuigi Rizzo SWNA(iter)->na_bdg = b; 1203f18be576SLuigi Rizzo } 1204f18be576SLuigi Rizzo } else /* not a netmap-capable NIC */ 1205f18be576SLuigi Rizzo goto ifunit_rele; 1206f18be576SLuigi Rizzo na = NA(iter); 1207f18be576SLuigi Rizzo na->bdg_port = cand; 1208f18be576SLuigi Rizzo /* bind the port to the bridge (virtual ports are not active) */ 1209f18be576SLuigi Rizzo BDG_SET_VAR(b->bdg_ports[cand], na); 1210f18be576SLuigi Rizzo na->na_bdg = b; 1211f196ce38SLuigi Rizzo ADD_BDG_REF(iter); 1212f18be576SLuigi Rizzo BDG_WUNLOCK(b); 1213f196ce38SLuigi Rizzo ND("attaching virtual bridge %p", b); 1214f196ce38SLuigi Rizzo } while (0); 1215f196ce38SLuigi Rizzo *ifp = iter; 1216f196ce38SLuigi Rizzo if (! *ifp) 1217f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */ 121868b8534bSLuigi Rizzo *ifp = ifunit_ref(name); 121968b8534bSLuigi Rizzo if (*ifp == NULL) 122068b8534bSLuigi Rizzo return (ENXIO); 122168b8534bSLuigi Rizzo /* can do this if the capability exists and if_pspare[0] 122268b8534bSLuigi Rizzo * points to the netmap descriptor. 122368b8534bSLuigi Rizzo */ 1224f18be576SLuigi Rizzo if (NETMAP_CAPABLE(*ifp)) { 1225f18be576SLuigi Rizzo #ifdef NM_BRIDGE 1226f18be576SLuigi Rizzo /* Users cannot use the NIC attached to a bridge directly */ 1227f18be576SLuigi Rizzo if (no_prefix && NETMAP_OWNED_BY_KERN(*ifp)) { 1228f18be576SLuigi Rizzo if_rele(*ifp); /* don't detach from bridge */ 1229f18be576SLuigi Rizzo return EINVAL; 1230f18be576SLuigi Rizzo } else 1231f18be576SLuigi Rizzo #endif /* NM_BRIDGE */ 123268b8534bSLuigi Rizzo return 0; /* valid pointer, we hold the refcount */ 1233f18be576SLuigi Rizzo } 1234f196ce38SLuigi Rizzo nm_if_rele(*ifp); 123568b8534bSLuigi Rizzo return EINVAL; // not NETMAP capable 123668b8534bSLuigi Rizzo } 123768b8534bSLuigi Rizzo 123868b8534bSLuigi Rizzo 123968b8534bSLuigi Rizzo /* 124068b8534bSLuigi Rizzo * Error routine called when txsync/rxsync detects an error. 124168b8534bSLuigi Rizzo * Can't do much more than resetting cur = hwcur, avail = hwavail. 124268b8534bSLuigi Rizzo * Return 1 on reinit. 1243506cc70cSLuigi Rizzo * 1244506cc70cSLuigi Rizzo * This routine is only called by the upper half of the kernel. 1245506cc70cSLuigi Rizzo * It only reads hwcur (which is changed only by the upper half, too) 1246506cc70cSLuigi Rizzo * and hwavail (which may be changed by the lower half, but only on 1247506cc70cSLuigi Rizzo * a tx ring and only to increase it, so any error will be recovered 1248506cc70cSLuigi Rizzo * on the next call). For the above, we don't strictly need to call 1249506cc70cSLuigi Rizzo * it under lock. 125068b8534bSLuigi Rizzo */ 125168b8534bSLuigi Rizzo int 125268b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring) 125368b8534bSLuigi Rizzo { 125468b8534bSLuigi Rizzo struct netmap_ring *ring = kring->ring; 125568b8534bSLuigi Rizzo u_int i, lim = kring->nkr_num_slots - 1; 125668b8534bSLuigi Rizzo int errors = 0; 125768b8534bSLuigi Rizzo 12588241616dSLuigi Rizzo RD(10, "called for %s", kring->na->ifp->if_xname); 125968b8534bSLuigi Rizzo if (ring->cur > lim) 126068b8534bSLuigi Rizzo errors++; 126168b8534bSLuigi Rizzo for (i = 0; i <= lim; i++) { 126268b8534bSLuigi Rizzo u_int idx = ring->slot[i].buf_idx; 126368b8534bSLuigi Rizzo u_int len = ring->slot[i].len; 126468b8534bSLuigi Rizzo if (idx < 2 || idx >= netmap_total_buffers) { 126568b8534bSLuigi Rizzo if (!errors++) 126668b8534bSLuigi Rizzo D("bad buffer at slot %d idx %d len %d ", i, idx, len); 126768b8534bSLuigi Rizzo ring->slot[i].buf_idx = 0; 126868b8534bSLuigi Rizzo ring->slot[i].len = 0; 126968b8534bSLuigi Rizzo } else if (len > NETMAP_BUF_SIZE) { 127068b8534bSLuigi Rizzo ring->slot[i].len = 0; 127168b8534bSLuigi Rizzo if (!errors++) 127268b8534bSLuigi Rizzo D("bad len %d at slot %d idx %d", 127368b8534bSLuigi Rizzo len, i, idx); 127468b8534bSLuigi Rizzo } 127568b8534bSLuigi Rizzo } 127668b8534bSLuigi Rizzo if (errors) { 127768b8534bSLuigi Rizzo int pos = kring - kring->na->tx_rings; 1278d76bf4ffSLuigi Rizzo int n = kring->na->num_tx_rings + 1; 127968b8534bSLuigi Rizzo 12808241616dSLuigi Rizzo RD(10, "total %d errors", errors); 128168b8534bSLuigi Rizzo errors++; 12828241616dSLuigi Rizzo RD(10, "%s %s[%d] reinit, cur %d -> %d avail %d -> %d", 128368b8534bSLuigi Rizzo kring->na->ifp->if_xname, 128468b8534bSLuigi Rizzo pos < n ? "TX" : "RX", pos < n ? pos : pos - n, 128568b8534bSLuigi Rizzo ring->cur, kring->nr_hwcur, 128668b8534bSLuigi Rizzo ring->avail, kring->nr_hwavail); 128768b8534bSLuigi Rizzo ring->cur = kring->nr_hwcur; 128868b8534bSLuigi Rizzo ring->avail = kring->nr_hwavail; 128968b8534bSLuigi Rizzo } 129068b8534bSLuigi Rizzo return (errors ? 1 : 0); 129168b8534bSLuigi Rizzo } 129268b8534bSLuigi Rizzo 129368b8534bSLuigi Rizzo 129468b8534bSLuigi Rizzo /* 129568b8534bSLuigi Rizzo * Set the ring ID. For devices with a single queue, a request 129668b8534bSLuigi Rizzo * for all rings is the same as a single ring. 129768b8534bSLuigi Rizzo */ 129868b8534bSLuigi Rizzo static int 129968b8534bSLuigi Rizzo netmap_set_ringid(struct netmap_priv_d *priv, u_int ringid) 130068b8534bSLuigi Rizzo { 130168b8534bSLuigi Rizzo struct ifnet *ifp = priv->np_ifp; 130268b8534bSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 130368b8534bSLuigi Rizzo u_int i = ringid & NETMAP_RING_MASK; 130464ae02c3SLuigi Rizzo /* initially (np_qfirst == np_qlast) we don't want to lock */ 130568b8534bSLuigi Rizzo int need_lock = (priv->np_qfirst != priv->np_qlast); 1306d76bf4ffSLuigi Rizzo int lim = na->num_rx_rings; 130768b8534bSLuigi Rizzo 1308d76bf4ffSLuigi Rizzo if (na->num_tx_rings > lim) 1309d76bf4ffSLuigi Rizzo lim = na->num_tx_rings; 131064ae02c3SLuigi Rizzo if ( (ringid & NETMAP_HW_RING) && i >= lim) { 131168b8534bSLuigi Rizzo D("invalid ring id %d", i); 131268b8534bSLuigi Rizzo return (EINVAL); 131368b8534bSLuigi Rizzo } 131468b8534bSLuigi Rizzo if (need_lock) 13151a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_LOCK, 0); 131668b8534bSLuigi Rizzo priv->np_ringid = ringid; 131768b8534bSLuigi Rizzo if (ringid & NETMAP_SW_RING) { 131864ae02c3SLuigi Rizzo priv->np_qfirst = NETMAP_SW_RING; 131964ae02c3SLuigi Rizzo priv->np_qlast = 0; 132068b8534bSLuigi Rizzo } else if (ringid & NETMAP_HW_RING) { 132168b8534bSLuigi Rizzo priv->np_qfirst = i; 132268b8534bSLuigi Rizzo priv->np_qlast = i + 1; 132368b8534bSLuigi Rizzo } else { 132468b8534bSLuigi Rizzo priv->np_qfirst = 0; 132564ae02c3SLuigi Rizzo priv->np_qlast = NETMAP_HW_RING ; 132668b8534bSLuigi Rizzo } 132768b8534bSLuigi Rizzo priv->np_txpoll = (ringid & NETMAP_NO_TX_POLL) ? 0 : 1; 132868b8534bSLuigi Rizzo if (need_lock) 13291a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0); 1330ae10d1afSLuigi Rizzo if (netmap_verbose) { 133168b8534bSLuigi Rizzo if (ringid & NETMAP_SW_RING) 133268b8534bSLuigi Rizzo D("ringid %s set to SW RING", ifp->if_xname); 133368b8534bSLuigi Rizzo else if (ringid & NETMAP_HW_RING) 133468b8534bSLuigi Rizzo D("ringid %s set to HW RING %d", ifp->if_xname, 133568b8534bSLuigi Rizzo priv->np_qfirst); 133668b8534bSLuigi Rizzo else 133764ae02c3SLuigi Rizzo D("ringid %s set to all %d HW RINGS", ifp->if_xname, lim); 1338ae10d1afSLuigi Rizzo } 133968b8534bSLuigi Rizzo return 0; 134068b8534bSLuigi Rizzo } 134168b8534bSLuigi Rizzo 1342f18be576SLuigi Rizzo 1343f18be576SLuigi Rizzo /* 1344f18be576SLuigi Rizzo * possibly move the interface to netmap-mode. 1345f18be576SLuigi Rizzo * If success it returns a pointer to netmap_if, otherwise NULL. 1346f18be576SLuigi Rizzo * This must be called with NMA_LOCK held. 1347f18be576SLuigi Rizzo */ 1348f18be576SLuigi Rizzo static struct netmap_if * 1349f18be576SLuigi Rizzo netmap_do_regif(struct netmap_priv_d *priv, struct ifnet *ifp, 1350f18be576SLuigi Rizzo uint16_t ringid, int *err) 1351f18be576SLuigi Rizzo { 1352f18be576SLuigi Rizzo struct netmap_adapter *na = NA(ifp); 1353f18be576SLuigi Rizzo struct netmap_if *nifp = NULL; 1354f18be576SLuigi Rizzo int i, error; 1355f18be576SLuigi Rizzo 1356f18be576SLuigi Rizzo if (na->na_bdg) 1357f18be576SLuigi Rizzo BDG_WLOCK(na->na_bdg); 1358f18be576SLuigi Rizzo na->nm_lock(ifp, NETMAP_REG_LOCK, 0); 1359f18be576SLuigi Rizzo 1360f18be576SLuigi Rizzo /* ring configuration may have changed, fetch from the card */ 1361f18be576SLuigi Rizzo netmap_update_config(na); 1362f18be576SLuigi Rizzo priv->np_ifp = ifp; /* store the reference */ 1363f18be576SLuigi Rizzo error = netmap_set_ringid(priv, ringid); 1364f18be576SLuigi Rizzo if (error) 1365f18be576SLuigi Rizzo goto out; 1366f18be576SLuigi Rizzo nifp = netmap_if_new(ifp->if_xname, na); 1367f18be576SLuigi Rizzo if (nifp == NULL) { /* allocation failed */ 1368f18be576SLuigi Rizzo error = ENOMEM; 1369f18be576SLuigi Rizzo } else if (ifp->if_capenable & IFCAP_NETMAP) { 1370f18be576SLuigi Rizzo /* was already set */ 1371f18be576SLuigi Rizzo } else { 1372f18be576SLuigi Rizzo /* Otherwise set the card in netmap mode 1373f18be576SLuigi Rizzo * and make it use the shared buffers. 1374f18be576SLuigi Rizzo */ 1375f18be576SLuigi Rizzo for (i = 0 ; i < na->num_tx_rings + 1; i++) 1376f18be576SLuigi Rizzo mtx_init(&na->tx_rings[i].q_lock, "nm_txq_lock", 1377f18be576SLuigi Rizzo MTX_NETWORK_LOCK, MTX_DEF); 1378f18be576SLuigi Rizzo for (i = 0 ; i < na->num_rx_rings + 1; i++) { 1379f18be576SLuigi Rizzo mtx_init(&na->rx_rings[i].q_lock, "nm_rxq_lock", 1380f18be576SLuigi Rizzo MTX_NETWORK_LOCK, MTX_DEF); 1381f18be576SLuigi Rizzo } 1382f18be576SLuigi Rizzo if (nma_is_hw(na)) { 1383f18be576SLuigi Rizzo SWNA(ifp)->tx_rings = &na->tx_rings[na->num_tx_rings]; 1384f18be576SLuigi Rizzo SWNA(ifp)->rx_rings = &na->rx_rings[na->num_rx_rings]; 1385f18be576SLuigi Rizzo } 1386f18be576SLuigi Rizzo error = na->nm_register(ifp, 1); /* mode on */ 1387f18be576SLuigi Rizzo #ifdef NM_BRIDGE 1388f18be576SLuigi Rizzo if (!error) 1389f18be576SLuigi Rizzo error = nm_alloc_bdgfwd(na); 1390f18be576SLuigi Rizzo #endif /* NM_BRIDGE */ 1391f18be576SLuigi Rizzo if (error) { 1392f18be576SLuigi Rizzo netmap_dtor_locked(priv); 1393f18be576SLuigi Rizzo /* nifp is not yet in priv, so free it separately */ 1394f18be576SLuigi Rizzo netmap_if_free(nifp); 1395f18be576SLuigi Rizzo nifp = NULL; 1396f18be576SLuigi Rizzo } 1397f18be576SLuigi Rizzo 1398f18be576SLuigi Rizzo } 1399f18be576SLuigi Rizzo out: 1400f18be576SLuigi Rizzo *err = error; 1401f18be576SLuigi Rizzo na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0); 1402f18be576SLuigi Rizzo if (na->na_bdg) 1403f18be576SLuigi Rizzo BDG_WUNLOCK(na->na_bdg); 1404f18be576SLuigi Rizzo return nifp; 1405f18be576SLuigi Rizzo } 1406f18be576SLuigi Rizzo 1407f18be576SLuigi Rizzo 1408f18be576SLuigi Rizzo /* Process NETMAP_BDG_ATTACH and NETMAP_BDG_DETACH */ 1409f18be576SLuigi Rizzo static int 1410f18be576SLuigi Rizzo kern_netmap_regif(struct nmreq *nmr) 1411f18be576SLuigi Rizzo { 1412f18be576SLuigi Rizzo struct ifnet *ifp; 1413f18be576SLuigi Rizzo struct netmap_if *nifp; 1414f18be576SLuigi Rizzo struct netmap_priv_d *npriv; 1415f18be576SLuigi Rizzo int error; 1416f18be576SLuigi Rizzo 1417f18be576SLuigi Rizzo npriv = malloc(sizeof(*npriv), M_DEVBUF, M_NOWAIT|M_ZERO); 1418f18be576SLuigi Rizzo if (npriv == NULL) 1419f18be576SLuigi Rizzo return ENOMEM; 1420f18be576SLuigi Rizzo error = netmap_get_memory(npriv); 1421f18be576SLuigi Rizzo if (error) { 1422f18be576SLuigi Rizzo free_exit: 1423f18be576SLuigi Rizzo bzero(npriv, sizeof(*npriv)); 1424f18be576SLuigi Rizzo free(npriv, M_DEVBUF); 1425f18be576SLuigi Rizzo return error; 1426f18be576SLuigi Rizzo } 1427f18be576SLuigi Rizzo 1428f18be576SLuigi Rizzo NMA_LOCK(); 1429f18be576SLuigi Rizzo error = get_ifp(nmr, &ifp); 1430f18be576SLuigi Rizzo if (error) { /* no device, or another bridge or user owns the device */ 1431f18be576SLuigi Rizzo NMA_UNLOCK(); 1432f18be576SLuigi Rizzo goto free_exit; 1433f18be576SLuigi Rizzo } else if (!NETMAP_OWNED_BY_KERN(ifp)) { 1434f18be576SLuigi Rizzo /* got reference to a virtual port or direct access to a NIC. 1435f18be576SLuigi Rizzo * perhaps specified no bridge's prefix or wrong NIC's name 1436f18be576SLuigi Rizzo */ 1437f18be576SLuigi Rizzo error = EINVAL; 1438f18be576SLuigi Rizzo unref_exit: 1439f18be576SLuigi Rizzo nm_if_rele(ifp); 1440f18be576SLuigi Rizzo NMA_UNLOCK(); 1441f18be576SLuigi Rizzo goto free_exit; 1442f18be576SLuigi Rizzo } 1443f18be576SLuigi Rizzo 1444f18be576SLuigi Rizzo if (nmr->nr_cmd == NETMAP_BDG_DETACH) { 1445f18be576SLuigi Rizzo if (NA(ifp)->refcount == 0) { /* not registered */ 1446f18be576SLuigi Rizzo error = EINVAL; 1447f18be576SLuigi Rizzo goto unref_exit; 1448f18be576SLuigi Rizzo } 1449f18be576SLuigi Rizzo NMA_UNLOCK(); 1450f18be576SLuigi Rizzo 1451f18be576SLuigi Rizzo netmap_dtor(NA(ifp)->na_kpriv); /* unregister */ 1452f18be576SLuigi Rizzo NA(ifp)->na_kpriv = NULL; 1453f18be576SLuigi Rizzo nm_if_rele(ifp); /* detach from the bridge */ 1454f18be576SLuigi Rizzo goto free_exit; 1455f18be576SLuigi Rizzo } else if (NA(ifp)->refcount > 0) { /* already registered */ 1456f18be576SLuigi Rizzo error = EINVAL; 1457f18be576SLuigi Rizzo goto unref_exit; 1458f18be576SLuigi Rizzo } 1459f18be576SLuigi Rizzo 1460f18be576SLuigi Rizzo nifp = netmap_do_regif(npriv, ifp, nmr->nr_ringid, &error); 1461f18be576SLuigi Rizzo if (!nifp) 1462f18be576SLuigi Rizzo goto unref_exit; 1463f18be576SLuigi Rizzo wmb(); // XXX do we need it ? 1464f18be576SLuigi Rizzo npriv->np_nifp = nifp; 1465f18be576SLuigi Rizzo NA(ifp)->na_kpriv = npriv; 1466f18be576SLuigi Rizzo NMA_UNLOCK(); 1467f18be576SLuigi Rizzo D("registered %s to netmap-mode", ifp->if_xname); 1468f18be576SLuigi Rizzo return 0; 1469f18be576SLuigi Rizzo } 1470f18be576SLuigi Rizzo 1471f18be576SLuigi Rizzo 1472f18be576SLuigi Rizzo /* CORE_LOCK is not necessary */ 1473f18be576SLuigi Rizzo static void 1474f18be576SLuigi Rizzo netmap_swlock_wrapper(struct ifnet *dev, int what, u_int queueid) 1475f18be576SLuigi Rizzo { 1476f18be576SLuigi Rizzo struct netmap_adapter *na = SWNA(dev); 1477f18be576SLuigi Rizzo 1478f18be576SLuigi Rizzo switch (what) { 1479f18be576SLuigi Rizzo case NETMAP_TX_LOCK: 1480f18be576SLuigi Rizzo mtx_lock(&na->tx_rings[queueid].q_lock); 1481f18be576SLuigi Rizzo break; 1482f18be576SLuigi Rizzo 1483f18be576SLuigi Rizzo case NETMAP_TX_UNLOCK: 1484f18be576SLuigi Rizzo mtx_unlock(&na->tx_rings[queueid].q_lock); 1485f18be576SLuigi Rizzo break; 1486f18be576SLuigi Rizzo 1487f18be576SLuigi Rizzo case NETMAP_RX_LOCK: 1488f18be576SLuigi Rizzo mtx_lock(&na->rx_rings[queueid].q_lock); 1489f18be576SLuigi Rizzo break; 1490f18be576SLuigi Rizzo 1491f18be576SLuigi Rizzo case NETMAP_RX_UNLOCK: 1492f18be576SLuigi Rizzo mtx_unlock(&na->rx_rings[queueid].q_lock); 1493f18be576SLuigi Rizzo break; 1494f18be576SLuigi Rizzo } 1495f18be576SLuigi Rizzo } 1496f18be576SLuigi Rizzo 1497f18be576SLuigi Rizzo 1498f18be576SLuigi Rizzo /* Initialize necessary fields of sw adapter located in right after hw's 1499f18be576SLuigi Rizzo * one. sw adapter attaches a pair of sw rings of the netmap-mode NIC. 1500f18be576SLuigi Rizzo * It is always activated and deactivated at the same tie with the hw's one. 1501f18be576SLuigi Rizzo * Thus we don't need refcounting on the sw adapter. 1502f18be576SLuigi Rizzo * Regardless of NIC's feature we use separate lock so that anybody can lock 1503f18be576SLuigi Rizzo * me independently from the hw adapter. 1504f18be576SLuigi Rizzo * Make sure nm_register is NULL to be handled as FALSE in nma_is_hw 1505f18be576SLuigi Rizzo */ 1506f18be576SLuigi Rizzo static void 1507f18be576SLuigi Rizzo netmap_attach_sw(struct ifnet *ifp) 1508f18be576SLuigi Rizzo { 1509f18be576SLuigi Rizzo struct netmap_adapter *hw_na = NA(ifp); 1510f18be576SLuigi Rizzo struct netmap_adapter *na = SWNA(ifp); 1511f18be576SLuigi Rizzo 1512f18be576SLuigi Rizzo na->ifp = ifp; 1513f18be576SLuigi Rizzo na->separate_locks = 1; 1514f18be576SLuigi Rizzo na->nm_lock = netmap_swlock_wrapper; 1515f18be576SLuigi Rizzo na->num_rx_rings = na->num_tx_rings = 1; 1516f18be576SLuigi Rizzo na->num_tx_desc = hw_na->num_tx_desc; 1517f18be576SLuigi Rizzo na->num_rx_desc = hw_na->num_rx_desc; 1518f18be576SLuigi Rizzo na->nm_txsync = netmap_bdg_to_host; 1519f18be576SLuigi Rizzo } 1520f18be576SLuigi Rizzo 1521f18be576SLuigi Rizzo 1522f18be576SLuigi Rizzo /* exported to kernel callers */ 1523f18be576SLuigi Rizzo int 1524f18be576SLuigi Rizzo netmap_bdg_ctl(struct nmreq *nmr, bdg_lookup_fn_t func) 1525f18be576SLuigi Rizzo { 1526f18be576SLuigi Rizzo struct nm_bridge *b; 1527f18be576SLuigi Rizzo struct netmap_adapter *na; 1528f18be576SLuigi Rizzo struct ifnet *iter; 1529f18be576SLuigi Rizzo char *name = nmr->nr_name; 1530f18be576SLuigi Rizzo int cmd = nmr->nr_cmd, namelen = strlen(name); 1531f18be576SLuigi Rizzo int error = 0, i, j; 1532f18be576SLuigi Rizzo 1533f18be576SLuigi Rizzo switch (cmd) { 1534f18be576SLuigi Rizzo case NETMAP_BDG_ATTACH: 1535f18be576SLuigi Rizzo case NETMAP_BDG_DETACH: 1536f18be576SLuigi Rizzo error = kern_netmap_regif(nmr); 1537f18be576SLuigi Rizzo break; 1538f18be576SLuigi Rizzo 1539f18be576SLuigi Rizzo case NETMAP_BDG_LIST: 1540f18be576SLuigi Rizzo /* this is used to enumerate bridges and ports */ 1541f18be576SLuigi Rizzo if (namelen) { /* look up indexes of bridge and port */ 1542f18be576SLuigi Rizzo if (strncmp(name, NM_NAME, strlen(NM_NAME))) { 1543f18be576SLuigi Rizzo error = EINVAL; 1544f18be576SLuigi Rizzo break; 1545f18be576SLuigi Rizzo } 1546f18be576SLuigi Rizzo b = nm_find_bridge(name, 0 /* don't create */); 1547f18be576SLuigi Rizzo if (!b) { 1548f18be576SLuigi Rizzo error = ENOENT; 1549f18be576SLuigi Rizzo break; 1550f18be576SLuigi Rizzo } 1551f18be576SLuigi Rizzo 1552f18be576SLuigi Rizzo BDG_RLOCK(b); 1553f18be576SLuigi Rizzo error = ENOENT; 1554f18be576SLuigi Rizzo for (i = 0; i < NM_BDG_MAXPORTS; i++) { 1555f18be576SLuigi Rizzo na = BDG_GET_VAR(b->bdg_ports[i]); 1556f18be576SLuigi Rizzo if (na == NULL) 1557f18be576SLuigi Rizzo continue; 1558f18be576SLuigi Rizzo iter = na->ifp; 1559f18be576SLuigi Rizzo /* the former and the latter identify a 1560f18be576SLuigi Rizzo * virtual port and a NIC, respectively 1561f18be576SLuigi Rizzo */ 1562f18be576SLuigi Rizzo if (!strcmp(iter->if_xname, name) || 1563f18be576SLuigi Rizzo (namelen > b->namelen && 1564f18be576SLuigi Rizzo !strcmp(iter->if_xname, 1565f18be576SLuigi Rizzo name + b->namelen + 1))) { 1566f18be576SLuigi Rizzo /* bridge index */ 1567f18be576SLuigi Rizzo nmr->nr_arg1 = b - nm_bridges; 1568f18be576SLuigi Rizzo nmr->nr_arg2 = i; /* port index */ 1569f18be576SLuigi Rizzo error = 0; 1570f18be576SLuigi Rizzo break; 1571f18be576SLuigi Rizzo } 1572f18be576SLuigi Rizzo } 1573f18be576SLuigi Rizzo BDG_RUNLOCK(b); 1574f18be576SLuigi Rizzo } else { 1575f18be576SLuigi Rizzo /* return the first non-empty entry starting from 1576f18be576SLuigi Rizzo * bridge nr_arg1 and port nr_arg2. 1577f18be576SLuigi Rizzo * 1578f18be576SLuigi Rizzo * Users can detect the end of the same bridge by 1579f18be576SLuigi Rizzo * seeing the new and old value of nr_arg1, and can 1580f18be576SLuigi Rizzo * detect the end of all the bridge by error != 0 1581f18be576SLuigi Rizzo */ 1582f18be576SLuigi Rizzo i = nmr->nr_arg1; 1583f18be576SLuigi Rizzo j = nmr->nr_arg2; 1584f18be576SLuigi Rizzo 1585f18be576SLuigi Rizzo for (error = ENOENT; error && i < NM_BRIDGES; i++) { 1586f18be576SLuigi Rizzo b = nm_bridges + i; 1587f18be576SLuigi Rizzo BDG_RLOCK(b); 1588f18be576SLuigi Rizzo for (; j < NM_BDG_MAXPORTS; j++) { 1589f18be576SLuigi Rizzo na = BDG_GET_VAR(b->bdg_ports[j]); 1590f18be576SLuigi Rizzo if (na == NULL) 1591f18be576SLuigi Rizzo continue; 1592f18be576SLuigi Rizzo iter = na->ifp; 1593f18be576SLuigi Rizzo nmr->nr_arg1 = i; 1594f18be576SLuigi Rizzo nmr->nr_arg2 = j; 1595f18be576SLuigi Rizzo strncpy(name, iter->if_xname, IFNAMSIZ); 1596f18be576SLuigi Rizzo error = 0; 1597f18be576SLuigi Rizzo break; 1598f18be576SLuigi Rizzo } 1599f18be576SLuigi Rizzo BDG_RUNLOCK(b); 1600f18be576SLuigi Rizzo j = 0; /* following bridges scan from 0 */ 1601f18be576SLuigi Rizzo } 1602f18be576SLuigi Rizzo } 1603f18be576SLuigi Rizzo break; 1604f18be576SLuigi Rizzo 1605f18be576SLuigi Rizzo case NETMAP_BDG_LOOKUP_REG: 1606f18be576SLuigi Rizzo /* register a lookup function to the given bridge. 1607f18be576SLuigi Rizzo * nmr->nr_name may be just bridge's name (including ':' 1608f18be576SLuigi Rizzo * if it is not just NM_NAME). 1609f18be576SLuigi Rizzo */ 1610f18be576SLuigi Rizzo if (!func) { 1611f18be576SLuigi Rizzo error = EINVAL; 1612f18be576SLuigi Rizzo break; 1613f18be576SLuigi Rizzo } 1614f18be576SLuigi Rizzo b = nm_find_bridge(name, 0 /* don't create */); 1615f18be576SLuigi Rizzo if (!b) { 1616f18be576SLuigi Rizzo error = EINVAL; 1617f18be576SLuigi Rizzo break; 1618f18be576SLuigi Rizzo } 1619f18be576SLuigi Rizzo BDG_WLOCK(b); 1620f18be576SLuigi Rizzo b->nm_bdg_lookup = func; 1621f18be576SLuigi Rizzo BDG_WUNLOCK(b); 1622f18be576SLuigi Rizzo break; 1623f18be576SLuigi Rizzo default: 1624f18be576SLuigi Rizzo D("invalid cmd (nmr->nr_cmd) (0x%x)", cmd); 1625f18be576SLuigi Rizzo error = EINVAL; 1626f18be576SLuigi Rizzo break; 1627f18be576SLuigi Rizzo } 1628f18be576SLuigi Rizzo return error; 1629f18be576SLuigi Rizzo } 1630f18be576SLuigi Rizzo 1631f18be576SLuigi Rizzo 163268b8534bSLuigi Rizzo /* 163368b8534bSLuigi Rizzo * ioctl(2) support for the "netmap" device. 163468b8534bSLuigi Rizzo * 163568b8534bSLuigi Rizzo * Following a list of accepted commands: 163668b8534bSLuigi Rizzo * - NIOCGINFO 163768b8534bSLuigi Rizzo * - SIOCGIFADDR just for convenience 163868b8534bSLuigi Rizzo * - NIOCREGIF 163968b8534bSLuigi Rizzo * - NIOCUNREGIF 164068b8534bSLuigi Rizzo * - NIOCTXSYNC 164168b8534bSLuigi Rizzo * - NIOCRXSYNC 164268b8534bSLuigi Rizzo * 164368b8534bSLuigi Rizzo * Return 0 on success, errno otherwise. 164468b8534bSLuigi Rizzo */ 164568b8534bSLuigi Rizzo static int 16460b8ed8e0SLuigi Rizzo netmap_ioctl(struct cdev *dev, u_long cmd, caddr_t data, 16470b8ed8e0SLuigi Rizzo int fflag, struct thread *td) 164868b8534bSLuigi Rizzo { 164968b8534bSLuigi Rizzo struct netmap_priv_d *priv = NULL; 165068b8534bSLuigi Rizzo struct ifnet *ifp; 165168b8534bSLuigi Rizzo struct nmreq *nmr = (struct nmreq *) data; 165268b8534bSLuigi Rizzo struct netmap_adapter *na; 165368b8534bSLuigi Rizzo int error; 165464ae02c3SLuigi Rizzo u_int i, lim; 165568b8534bSLuigi Rizzo struct netmap_if *nifp; 165668b8534bSLuigi Rizzo 16570b8ed8e0SLuigi Rizzo (void)dev; /* UNUSED */ 16580b8ed8e0SLuigi Rizzo (void)fflag; /* UNUSED */ 1659f196ce38SLuigi Rizzo #ifdef linux 1660f196ce38SLuigi Rizzo #define devfs_get_cdevpriv(pp) \ 1661f196ce38SLuigi Rizzo ({ *(struct netmap_priv_d **)pp = ((struct file *)td)->private_data; \ 1662f196ce38SLuigi Rizzo (*pp ? 0 : ENOENT); }) 1663f196ce38SLuigi Rizzo 1664f196ce38SLuigi Rizzo /* devfs_set_cdevpriv cannot fail on linux */ 1665f196ce38SLuigi Rizzo #define devfs_set_cdevpriv(p, fn) \ 1666f196ce38SLuigi Rizzo ({ ((struct file *)td)->private_data = p; (p ? 0 : EINVAL); }) 1667f196ce38SLuigi Rizzo 1668f196ce38SLuigi Rizzo 1669f196ce38SLuigi Rizzo #define devfs_clear_cdevpriv() do { \ 1670f196ce38SLuigi Rizzo netmap_dtor(priv); ((struct file *)td)->private_data = 0; \ 1671f196ce38SLuigi Rizzo } while (0) 1672f196ce38SLuigi Rizzo #endif /* linux */ 1673f196ce38SLuigi Rizzo 1674506cc70cSLuigi Rizzo CURVNET_SET(TD_TO_VNET(td)); 1675506cc70cSLuigi Rizzo 167668b8534bSLuigi Rizzo error = devfs_get_cdevpriv((void **)&priv); 16778241616dSLuigi Rizzo if (error) { 1678506cc70cSLuigi Rizzo CURVNET_RESTORE(); 16798241616dSLuigi Rizzo /* XXX ENOENT should be impossible, since the priv 16808241616dSLuigi Rizzo * is now created in the open */ 16818241616dSLuigi Rizzo return (error == ENOENT ? ENXIO : error); 1682506cc70cSLuigi Rizzo } 168368b8534bSLuigi Rizzo 1684f196ce38SLuigi Rizzo nmr->nr_name[sizeof(nmr->nr_name) - 1] = '\0'; /* truncate name */ 168568b8534bSLuigi Rizzo switch (cmd) { 168668b8534bSLuigi Rizzo case NIOCGINFO: /* return capabilities etc */ 168764ae02c3SLuigi Rizzo if (nmr->nr_version != NETMAP_API) { 168864ae02c3SLuigi Rizzo D("API mismatch got %d have %d", 168964ae02c3SLuigi Rizzo nmr->nr_version, NETMAP_API); 169064ae02c3SLuigi Rizzo nmr->nr_version = NETMAP_API; 169164ae02c3SLuigi Rizzo error = EINVAL; 169264ae02c3SLuigi Rizzo break; 169364ae02c3SLuigi Rizzo } 1694f18be576SLuigi Rizzo if (nmr->nr_cmd == NETMAP_BDG_LIST) { 1695f18be576SLuigi Rizzo error = netmap_bdg_ctl(nmr, NULL); 1696f18be576SLuigi Rizzo break; 1697f18be576SLuigi Rizzo } 16988241616dSLuigi Rizzo /* update configuration */ 16998241616dSLuigi Rizzo error = netmap_get_memory(priv); 17008241616dSLuigi Rizzo ND("get_memory returned %d", error); 17018241616dSLuigi Rizzo if (error) 17028241616dSLuigi Rizzo break; 17038241616dSLuigi Rizzo /* memsize is always valid */ 17048241616dSLuigi Rizzo nmr->nr_memsize = nm_mem.nm_totalsize; 17058241616dSLuigi Rizzo nmr->nr_offset = 0; 17068241616dSLuigi Rizzo nmr->nr_rx_slots = nmr->nr_tx_slots = 0; 170768b8534bSLuigi Rizzo if (nmr->nr_name[0] == '\0') /* just get memory info */ 170868b8534bSLuigi Rizzo break; 1709f18be576SLuigi Rizzo /* lock because get_ifp and update_config see na->refcount */ 1710f18be576SLuigi Rizzo NMA_LOCK(); 1711f18be576SLuigi Rizzo error = get_ifp(nmr, &ifp); /* get a refcount */ 1712f18be576SLuigi Rizzo if (error) { 1713f18be576SLuigi Rizzo NMA_UNLOCK(); 171468b8534bSLuigi Rizzo break; 1715f18be576SLuigi Rizzo } 171668b8534bSLuigi Rizzo na = NA(ifp); /* retrieve netmap_adapter */ 1717ae10d1afSLuigi Rizzo netmap_update_config(na); 1718f18be576SLuigi Rizzo NMA_UNLOCK(); 1719d76bf4ffSLuigi Rizzo nmr->nr_rx_rings = na->num_rx_rings; 1720d76bf4ffSLuigi Rizzo nmr->nr_tx_rings = na->num_tx_rings; 172164ae02c3SLuigi Rizzo nmr->nr_rx_slots = na->num_rx_desc; 172264ae02c3SLuigi Rizzo nmr->nr_tx_slots = na->num_tx_desc; 1723f196ce38SLuigi Rizzo nm_if_rele(ifp); /* return the refcount */ 172468b8534bSLuigi Rizzo break; 172568b8534bSLuigi Rizzo 172668b8534bSLuigi Rizzo case NIOCREGIF: 172764ae02c3SLuigi Rizzo if (nmr->nr_version != NETMAP_API) { 172864ae02c3SLuigi Rizzo nmr->nr_version = NETMAP_API; 172964ae02c3SLuigi Rizzo error = EINVAL; 173064ae02c3SLuigi Rizzo break; 173164ae02c3SLuigi Rizzo } 1732f18be576SLuigi Rizzo /* possibly attach/detach NIC and VALE switch */ 1733f18be576SLuigi Rizzo i = nmr->nr_cmd; 1734f18be576SLuigi Rizzo if (i == NETMAP_BDG_ATTACH || i == NETMAP_BDG_DETACH) { 1735f18be576SLuigi Rizzo error = netmap_bdg_ctl(nmr, NULL); 1736f18be576SLuigi Rizzo break; 1737f18be576SLuigi Rizzo } else if (i != 0) { 1738f18be576SLuigi Rizzo D("nr_cmd must be 0 not %d", i); 1739f18be576SLuigi Rizzo error = EINVAL; 1740f18be576SLuigi Rizzo break; 1741f18be576SLuigi Rizzo } 1742f18be576SLuigi Rizzo 17438241616dSLuigi Rizzo /* ensure allocators are ready */ 17448241616dSLuigi Rizzo error = netmap_get_memory(priv); 17458241616dSLuigi Rizzo ND("get_memory returned %d", error); 17468241616dSLuigi Rizzo if (error) 17478241616dSLuigi Rizzo break; 17488241616dSLuigi Rizzo 17498241616dSLuigi Rizzo /* protect access to priv from concurrent NIOCREGIF */ 17508241616dSLuigi Rizzo NMA_LOCK(); 17518241616dSLuigi Rizzo if (priv->np_ifp != NULL) { /* thread already registered */ 1752506cc70cSLuigi Rizzo error = netmap_set_ringid(priv, nmr->nr_ringid); 1753f18be576SLuigi Rizzo unlock_out: 17548241616dSLuigi Rizzo NMA_UNLOCK(); 1755506cc70cSLuigi Rizzo break; 1756506cc70cSLuigi Rizzo } 175768b8534bSLuigi Rizzo /* find the interface and a reference */ 1758f18be576SLuigi Rizzo error = get_ifp(nmr, &ifp); /* keep reference */ 175968b8534bSLuigi Rizzo if (error) 1760f18be576SLuigi Rizzo goto unlock_out; 1761f18be576SLuigi Rizzo else if (NETMAP_OWNED_BY_KERN(ifp)) { 1762f18be576SLuigi Rizzo nm_if_rele(ifp); 1763f18be576SLuigi Rizzo goto unlock_out; 1764f196ce38SLuigi Rizzo } 1765f18be576SLuigi Rizzo nifp = netmap_do_regif(priv, ifp, nmr->nr_ringid, &error); 1766f18be576SLuigi Rizzo if (!nifp) { /* reg. failed, release priv and ref */ 1767f196ce38SLuigi Rizzo nm_if_rele(ifp); /* return the refcount */ 17688241616dSLuigi Rizzo priv->np_ifp = NULL; 17698241616dSLuigi Rizzo priv->np_nifp = NULL; 1770f18be576SLuigi Rizzo goto unlock_out; 177168b8534bSLuigi Rizzo } 177268b8534bSLuigi Rizzo 17738241616dSLuigi Rizzo /* the following assignment is a commitment. 17748241616dSLuigi Rizzo * Readers (i.e., poll and *SYNC) check for 17758241616dSLuigi Rizzo * np_nifp != NULL without locking 177668b8534bSLuigi Rizzo */ 17778241616dSLuigi Rizzo wmb(); /* make sure previous writes are visible to all CPUs */ 17788241616dSLuigi Rizzo priv->np_nifp = nifp; 17798241616dSLuigi Rizzo NMA_UNLOCK(); 178068b8534bSLuigi Rizzo 178168b8534bSLuigi Rizzo /* return the offset of the netmap_if object */ 1782f18be576SLuigi Rizzo na = NA(ifp); /* retrieve netmap adapter */ 1783d76bf4ffSLuigi Rizzo nmr->nr_rx_rings = na->num_rx_rings; 1784d76bf4ffSLuigi Rizzo nmr->nr_tx_rings = na->num_tx_rings; 178564ae02c3SLuigi Rizzo nmr->nr_rx_slots = na->num_rx_desc; 178664ae02c3SLuigi Rizzo nmr->nr_tx_slots = na->num_tx_desc; 17878241616dSLuigi Rizzo nmr->nr_memsize = nm_mem.nm_totalsize; 1788446ee301SLuigi Rizzo nmr->nr_offset = netmap_if_offset(nifp); 178968b8534bSLuigi Rizzo break; 179068b8534bSLuigi Rizzo 179168b8534bSLuigi Rizzo case NIOCUNREGIF: 17928241616dSLuigi Rizzo // XXX we have no data here ? 17938241616dSLuigi Rizzo D("deprecated, data is %p", nmr); 17948241616dSLuigi Rizzo error = EINVAL; 179568b8534bSLuigi Rizzo break; 179668b8534bSLuigi Rizzo 179768b8534bSLuigi Rizzo case NIOCTXSYNC: 179868b8534bSLuigi Rizzo case NIOCRXSYNC: 17998241616dSLuigi Rizzo nifp = priv->np_nifp; 18008241616dSLuigi Rizzo 18018241616dSLuigi Rizzo if (nifp == NULL) { 1802506cc70cSLuigi Rizzo error = ENXIO; 1803506cc70cSLuigi Rizzo break; 1804506cc70cSLuigi Rizzo } 18058241616dSLuigi Rizzo rmb(); /* make sure following reads are not from cache */ 18068241616dSLuigi Rizzo 18078241616dSLuigi Rizzo 180868b8534bSLuigi Rizzo ifp = priv->np_ifp; /* we have a reference */ 18098241616dSLuigi Rizzo 18108241616dSLuigi Rizzo if (ifp == NULL) { 18118241616dSLuigi Rizzo D("Internal error: nifp != NULL && ifp == NULL"); 18128241616dSLuigi Rizzo error = ENXIO; 18138241616dSLuigi Rizzo break; 18148241616dSLuigi Rizzo } 18158241616dSLuigi Rizzo 181668b8534bSLuigi Rizzo na = NA(ifp); /* retrieve netmap adapter */ 181764ae02c3SLuigi Rizzo if (priv->np_qfirst == NETMAP_SW_RING) { /* host rings */ 181868b8534bSLuigi Rizzo if (cmd == NIOCTXSYNC) 181968b8534bSLuigi Rizzo netmap_sync_to_host(na); 182068b8534bSLuigi Rizzo else 182101c7d25fSLuigi Rizzo netmap_sync_from_host(na, NULL, NULL); 1822506cc70cSLuigi Rizzo break; 182368b8534bSLuigi Rizzo } 182464ae02c3SLuigi Rizzo /* find the last ring to scan */ 182564ae02c3SLuigi Rizzo lim = priv->np_qlast; 182664ae02c3SLuigi Rizzo if (lim == NETMAP_HW_RING) 18273c0caf6cSLuigi Rizzo lim = (cmd == NIOCTXSYNC) ? 1828d76bf4ffSLuigi Rizzo na->num_tx_rings : na->num_rx_rings; 182968b8534bSLuigi Rizzo 183064ae02c3SLuigi Rizzo for (i = priv->np_qfirst; i < lim; i++) { 183168b8534bSLuigi Rizzo if (cmd == NIOCTXSYNC) { 183268b8534bSLuigi Rizzo struct netmap_kring *kring = &na->tx_rings[i]; 183368b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_TXSYNC) 18343c0caf6cSLuigi Rizzo D("pre txsync ring %d cur %d hwcur %d", 183568b8534bSLuigi Rizzo i, kring->ring->cur, 183668b8534bSLuigi Rizzo kring->nr_hwcur); 18371a26580eSLuigi Rizzo na->nm_txsync(ifp, i, 1 /* do lock */); 183868b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_TXSYNC) 18393c0caf6cSLuigi Rizzo D("post txsync ring %d cur %d hwcur %d", 184068b8534bSLuigi Rizzo i, kring->ring->cur, 184168b8534bSLuigi Rizzo kring->nr_hwcur); 184268b8534bSLuigi Rizzo } else { 18431a26580eSLuigi Rizzo na->nm_rxsync(ifp, i, 1 /* do lock */); 184468b8534bSLuigi Rizzo microtime(&na->rx_rings[i].ring->ts); 184568b8534bSLuigi Rizzo } 184668b8534bSLuigi Rizzo } 184768b8534bSLuigi Rizzo 184868b8534bSLuigi Rizzo break; 184968b8534bSLuigi Rizzo 1850f196ce38SLuigi Rizzo #ifdef __FreeBSD__ 185168b8534bSLuigi Rizzo case BIOCIMMEDIATE: 185268b8534bSLuigi Rizzo case BIOCGHDRCMPLT: 185368b8534bSLuigi Rizzo case BIOCSHDRCMPLT: 185468b8534bSLuigi Rizzo case BIOCSSEESENT: 185568b8534bSLuigi Rizzo D("ignore BIOCIMMEDIATE/BIOCSHDRCMPLT/BIOCSHDRCMPLT/BIOCSSEESENT"); 185668b8534bSLuigi Rizzo break; 185768b8534bSLuigi Rizzo 1858babc7c12SLuigi Rizzo default: /* allow device-specific ioctls */ 185968b8534bSLuigi Rizzo { 186068b8534bSLuigi Rizzo struct socket so; 186168b8534bSLuigi Rizzo bzero(&so, sizeof(so)); 1862f18be576SLuigi Rizzo error = get_ifp(nmr, &ifp); /* keep reference */ 186368b8534bSLuigi Rizzo if (error) 186468b8534bSLuigi Rizzo break; 186568b8534bSLuigi Rizzo so.so_vnet = ifp->if_vnet; 186668b8534bSLuigi Rizzo // so->so_proto not null. 186768b8534bSLuigi Rizzo error = ifioctl(&so, cmd, data, td); 1868f196ce38SLuigi Rizzo nm_if_rele(ifp); 1869babc7c12SLuigi Rizzo break; 187068b8534bSLuigi Rizzo } 1871f196ce38SLuigi Rizzo 1872f196ce38SLuigi Rizzo #else /* linux */ 1873f196ce38SLuigi Rizzo default: 1874f196ce38SLuigi Rizzo error = EOPNOTSUPP; 1875f196ce38SLuigi Rizzo #endif /* linux */ 187668b8534bSLuigi Rizzo } 187768b8534bSLuigi Rizzo 1878506cc70cSLuigi Rizzo CURVNET_RESTORE(); 187968b8534bSLuigi Rizzo return (error); 188068b8534bSLuigi Rizzo } 188168b8534bSLuigi Rizzo 188268b8534bSLuigi Rizzo 188368b8534bSLuigi Rizzo /* 188468b8534bSLuigi Rizzo * select(2) and poll(2) handlers for the "netmap" device. 188568b8534bSLuigi Rizzo * 188668b8534bSLuigi Rizzo * Can be called for one or more queues. 188768b8534bSLuigi Rizzo * Return true the event mask corresponding to ready events. 188868b8534bSLuigi Rizzo * If there are no ready events, do a selrecord on either individual 188968b8534bSLuigi Rizzo * selfd or on the global one. 189068b8534bSLuigi Rizzo * Device-dependent parts (locking and sync of tx/rx rings) 189168b8534bSLuigi Rizzo * are done through callbacks. 1892f196ce38SLuigi Rizzo * 189301c7d25fSLuigi Rizzo * On linux, arguments are really pwait, the poll table, and 'td' is struct file * 189401c7d25fSLuigi Rizzo * The first one is remapped to pwait as selrecord() uses the name as an 189501c7d25fSLuigi Rizzo * hidden argument. 189668b8534bSLuigi Rizzo */ 189768b8534bSLuigi Rizzo static int 189801c7d25fSLuigi Rizzo netmap_poll(struct cdev *dev, int events, struct thread *td) 189968b8534bSLuigi Rizzo { 190068b8534bSLuigi Rizzo struct netmap_priv_d *priv = NULL; 190168b8534bSLuigi Rizzo struct netmap_adapter *na; 190268b8534bSLuigi Rizzo struct ifnet *ifp; 190368b8534bSLuigi Rizzo struct netmap_kring *kring; 1904d0c7b075SLuigi Rizzo u_int core_lock, i, check_all, want_tx, want_rx, revents = 0; 1905091fd0abSLuigi Rizzo u_int lim_tx, lim_rx, host_forwarded = 0; 1906091fd0abSLuigi Rizzo struct mbq q = { NULL, NULL, 0 }; 1907bcda432eSLuigi Rizzo enum {NO_CL, NEED_CL, LOCKED_CL }; /* see below */ 190801c7d25fSLuigi Rizzo void *pwait = dev; /* linux compatibility */ 190901c7d25fSLuigi Rizzo 191001c7d25fSLuigi Rizzo (void)pwait; 191168b8534bSLuigi Rizzo 191268b8534bSLuigi Rizzo if (devfs_get_cdevpriv((void **)&priv) != 0 || priv == NULL) 191368b8534bSLuigi Rizzo return POLLERR; 191468b8534bSLuigi Rizzo 19158241616dSLuigi Rizzo if (priv->np_nifp == NULL) { 19168241616dSLuigi Rizzo D("No if registered"); 19178241616dSLuigi Rizzo return POLLERR; 19188241616dSLuigi Rizzo } 19198241616dSLuigi Rizzo rmb(); /* make sure following reads are not from cache */ 19208241616dSLuigi Rizzo 192168b8534bSLuigi Rizzo ifp = priv->np_ifp; 192268b8534bSLuigi Rizzo // XXX check for deleting() ? 192368b8534bSLuigi Rizzo if ( (ifp->if_capenable & IFCAP_NETMAP) == 0) 192468b8534bSLuigi Rizzo return POLLERR; 192568b8534bSLuigi Rizzo 192668b8534bSLuigi Rizzo if (netmap_verbose & 0x8000) 192768b8534bSLuigi Rizzo D("device %s events 0x%x", ifp->if_xname, events); 192868b8534bSLuigi Rizzo want_tx = events & (POLLOUT | POLLWRNORM); 192968b8534bSLuigi Rizzo want_rx = events & (POLLIN | POLLRDNORM); 193068b8534bSLuigi Rizzo 193168b8534bSLuigi Rizzo na = NA(ifp); /* retrieve netmap adapter */ 193268b8534bSLuigi Rizzo 1933d76bf4ffSLuigi Rizzo lim_tx = na->num_tx_rings; 1934d76bf4ffSLuigi Rizzo lim_rx = na->num_rx_rings; 193568b8534bSLuigi Rizzo /* how many queues we are scanning */ 193664ae02c3SLuigi Rizzo if (priv->np_qfirst == NETMAP_SW_RING) { 193768b8534bSLuigi Rizzo if (priv->np_txpoll || want_tx) { 193868b8534bSLuigi Rizzo /* push any packets up, then we are always ready */ 193968b8534bSLuigi Rizzo netmap_sync_to_host(na); 194068b8534bSLuigi Rizzo revents |= want_tx; 194168b8534bSLuigi Rizzo } 194268b8534bSLuigi Rizzo if (want_rx) { 194364ae02c3SLuigi Rizzo kring = &na->rx_rings[lim_rx]; 194468b8534bSLuigi Rizzo if (kring->ring->avail == 0) 194501c7d25fSLuigi Rizzo netmap_sync_from_host(na, td, dev); 194668b8534bSLuigi Rizzo if (kring->ring->avail > 0) { 194768b8534bSLuigi Rizzo revents |= want_rx; 194868b8534bSLuigi Rizzo } 194968b8534bSLuigi Rizzo } 195068b8534bSLuigi Rizzo return (revents); 195168b8534bSLuigi Rizzo } 195268b8534bSLuigi Rizzo 1953091fd0abSLuigi Rizzo /* if we are in transparent mode, check also the host rx ring */ 1954091fd0abSLuigi Rizzo kring = &na->rx_rings[lim_rx]; 1955091fd0abSLuigi Rizzo if ( (priv->np_qlast == NETMAP_HW_RING) // XXX check_all 1956091fd0abSLuigi Rizzo && want_rx 1957091fd0abSLuigi Rizzo && (netmap_fwd || kring->ring->flags & NR_FORWARD) ) { 1958091fd0abSLuigi Rizzo if (kring->ring->avail == 0) 1959091fd0abSLuigi Rizzo netmap_sync_from_host(na, td, dev); 1960091fd0abSLuigi Rizzo if (kring->ring->avail > 0) 1961091fd0abSLuigi Rizzo revents |= want_rx; 1962091fd0abSLuigi Rizzo } 1963091fd0abSLuigi Rizzo 196468b8534bSLuigi Rizzo /* 196568b8534bSLuigi Rizzo * check_all is set if the card has more than one queue and 196668b8534bSLuigi Rizzo * the client is polling all of them. If true, we sleep on 196768b8534bSLuigi Rizzo * the "global" selfd, otherwise we sleep on individual selfd 196868b8534bSLuigi Rizzo * (we can only sleep on one of them per direction). 196968b8534bSLuigi Rizzo * The interrupt routine in the driver should always wake on 197068b8534bSLuigi Rizzo * the individual selfd, and also on the global one if the card 197168b8534bSLuigi Rizzo * has more than one ring. 197268b8534bSLuigi Rizzo * 197368b8534bSLuigi Rizzo * If the card has only one lock, we just use that. 197468b8534bSLuigi Rizzo * If the card has separate ring locks, we just use those 197568b8534bSLuigi Rizzo * unless we are doing check_all, in which case the whole 197668b8534bSLuigi Rizzo * loop is wrapped by the global lock. 197768b8534bSLuigi Rizzo * We acquire locks only when necessary: if poll is called 197868b8534bSLuigi Rizzo * when buffers are available, we can just return without locks. 197968b8534bSLuigi Rizzo * 198068b8534bSLuigi Rizzo * rxsync() is only called if we run out of buffers on a POLLIN. 198168b8534bSLuigi Rizzo * txsync() is called if we run out of buffers on POLLOUT, or 198268b8534bSLuigi Rizzo * there are pending packets to send. The latter can be disabled 198368b8534bSLuigi Rizzo * passing NETMAP_NO_TX_POLL in the NIOCREG call. 198468b8534bSLuigi Rizzo */ 198564ae02c3SLuigi Rizzo check_all = (priv->np_qlast == NETMAP_HW_RING) && (lim_tx > 1 || lim_rx > 1); 198668b8534bSLuigi Rizzo 198768b8534bSLuigi Rizzo /* 198868b8534bSLuigi Rizzo * core_lock indicates what to do with the core lock. 198968b8534bSLuigi Rizzo * The core lock is used when either the card has no individual 199068b8534bSLuigi Rizzo * locks, or it has individual locks but we are cheking all 199168b8534bSLuigi Rizzo * rings so we need the core lock to avoid missing wakeup events. 199268b8534bSLuigi Rizzo * 199368b8534bSLuigi Rizzo * It has three possible states: 199468b8534bSLuigi Rizzo * NO_CL we don't need to use the core lock, e.g. 199568b8534bSLuigi Rizzo * because we are protected by individual locks. 199668b8534bSLuigi Rizzo * NEED_CL we need the core lock. In this case, when we 199768b8534bSLuigi Rizzo * call the lock routine, move to LOCKED_CL 199868b8534bSLuigi Rizzo * to remember to release the lock once done. 199968b8534bSLuigi Rizzo * LOCKED_CL core lock is set, so we need to release it. 200068b8534bSLuigi Rizzo */ 2001d0c7b075SLuigi Rizzo core_lock = (check_all || !na->separate_locks) ? NEED_CL : NO_CL; 2002f196ce38SLuigi Rizzo #ifdef NM_BRIDGE 2003f196ce38SLuigi Rizzo /* the bridge uses separate locks */ 2004f196ce38SLuigi Rizzo if (na->nm_register == bdg_netmap_reg) { 2005f196ce38SLuigi Rizzo ND("not using core lock for %s", ifp->if_xname); 2006f196ce38SLuigi Rizzo core_lock = NO_CL; 2007f196ce38SLuigi Rizzo } 2008f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */ 200964ae02c3SLuigi Rizzo if (priv->np_qlast != NETMAP_HW_RING) { 201064ae02c3SLuigi Rizzo lim_tx = lim_rx = priv->np_qlast; 201164ae02c3SLuigi Rizzo } 201264ae02c3SLuigi Rizzo 201368b8534bSLuigi Rizzo /* 201468b8534bSLuigi Rizzo * We start with a lock free round which is good if we have 201568b8534bSLuigi Rizzo * data available. If this fails, then lock and call the sync 201668b8534bSLuigi Rizzo * routines. 201768b8534bSLuigi Rizzo */ 201864ae02c3SLuigi Rizzo for (i = priv->np_qfirst; want_rx && i < lim_rx; i++) { 201968b8534bSLuigi Rizzo kring = &na->rx_rings[i]; 202068b8534bSLuigi Rizzo if (kring->ring->avail > 0) { 202168b8534bSLuigi Rizzo revents |= want_rx; 202268b8534bSLuigi Rizzo want_rx = 0; /* also breaks the loop */ 202368b8534bSLuigi Rizzo } 202468b8534bSLuigi Rizzo } 202564ae02c3SLuigi Rizzo for (i = priv->np_qfirst; want_tx && i < lim_tx; i++) { 202668b8534bSLuigi Rizzo kring = &na->tx_rings[i]; 202768b8534bSLuigi Rizzo if (kring->ring->avail > 0) { 202868b8534bSLuigi Rizzo revents |= want_tx; 202968b8534bSLuigi Rizzo want_tx = 0; /* also breaks the loop */ 203068b8534bSLuigi Rizzo } 203168b8534bSLuigi Rizzo } 203268b8534bSLuigi Rizzo 203368b8534bSLuigi Rizzo /* 203468b8534bSLuigi Rizzo * If we to push packets out (priv->np_txpoll) or want_tx is 203568b8534bSLuigi Rizzo * still set, we do need to run the txsync calls (on all rings, 203668b8534bSLuigi Rizzo * to avoid that the tx rings stall). 203768b8534bSLuigi Rizzo */ 203868b8534bSLuigi Rizzo if (priv->np_txpoll || want_tx) { 2039091fd0abSLuigi Rizzo flush_tx: 204064ae02c3SLuigi Rizzo for (i = priv->np_qfirst; i < lim_tx; i++) { 204168b8534bSLuigi Rizzo kring = &na->tx_rings[i]; 20425819da83SLuigi Rizzo /* 20435819da83SLuigi Rizzo * Skip the current ring if want_tx == 0 20445819da83SLuigi Rizzo * (we have already done a successful sync on 20455819da83SLuigi Rizzo * a previous ring) AND kring->cur == kring->hwcur 20465819da83SLuigi Rizzo * (there are no pending transmissions for this ring). 20475819da83SLuigi Rizzo */ 204868b8534bSLuigi Rizzo if (!want_tx && kring->ring->cur == kring->nr_hwcur) 204968b8534bSLuigi Rizzo continue; 205068b8534bSLuigi Rizzo if (core_lock == NEED_CL) { 20511a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_LOCK, 0); 205268b8534bSLuigi Rizzo core_lock = LOCKED_CL; 205368b8534bSLuigi Rizzo } 205468b8534bSLuigi Rizzo if (na->separate_locks) 20551a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_TX_LOCK, i); 205668b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_TXSYNC) 205768b8534bSLuigi Rizzo D("send %d on %s %d", 205868b8534bSLuigi Rizzo kring->ring->cur, 205968b8534bSLuigi Rizzo ifp->if_xname, i); 20601a26580eSLuigi Rizzo if (na->nm_txsync(ifp, i, 0 /* no lock */)) 206168b8534bSLuigi Rizzo revents |= POLLERR; 206268b8534bSLuigi Rizzo 20635819da83SLuigi Rizzo /* Check avail/call selrecord only if called with POLLOUT */ 206468b8534bSLuigi Rizzo if (want_tx) { 206568b8534bSLuigi Rizzo if (kring->ring->avail > 0) { 206668b8534bSLuigi Rizzo /* stop at the first ring. We don't risk 206768b8534bSLuigi Rizzo * starvation. 206868b8534bSLuigi Rizzo */ 206968b8534bSLuigi Rizzo revents |= want_tx; 207068b8534bSLuigi Rizzo want_tx = 0; 207168b8534bSLuigi Rizzo } else if (!check_all) 207268b8534bSLuigi Rizzo selrecord(td, &kring->si); 207368b8534bSLuigi Rizzo } 207468b8534bSLuigi Rizzo if (na->separate_locks) 20751a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_TX_UNLOCK, i); 207668b8534bSLuigi Rizzo } 207768b8534bSLuigi Rizzo } 207868b8534bSLuigi Rizzo 207968b8534bSLuigi Rizzo /* 208068b8534bSLuigi Rizzo * now if want_rx is still set we need to lock and rxsync. 208168b8534bSLuigi Rizzo * Do it on all rings because otherwise we starve. 208268b8534bSLuigi Rizzo */ 208368b8534bSLuigi Rizzo if (want_rx) { 208464ae02c3SLuigi Rizzo for (i = priv->np_qfirst; i < lim_rx; i++) { 208568b8534bSLuigi Rizzo kring = &na->rx_rings[i]; 208668b8534bSLuigi Rizzo if (core_lock == NEED_CL) { 20871a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_LOCK, 0); 208868b8534bSLuigi Rizzo core_lock = LOCKED_CL; 208968b8534bSLuigi Rizzo } 209068b8534bSLuigi Rizzo if (na->separate_locks) 20911a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_RX_LOCK, i); 2092091fd0abSLuigi Rizzo if (netmap_fwd ||kring->ring->flags & NR_FORWARD) { 2093091fd0abSLuigi Rizzo ND(10, "forwarding some buffers up %d to %d", 2094091fd0abSLuigi Rizzo kring->nr_hwcur, kring->ring->cur); 2095091fd0abSLuigi Rizzo netmap_grab_packets(kring, &q, netmap_fwd); 2096091fd0abSLuigi Rizzo } 209768b8534bSLuigi Rizzo 20981a26580eSLuigi Rizzo if (na->nm_rxsync(ifp, i, 0 /* no lock */)) 209968b8534bSLuigi Rizzo revents |= POLLERR; 21005819da83SLuigi Rizzo if (netmap_no_timestamp == 0 || 21015819da83SLuigi Rizzo kring->ring->flags & NR_TIMESTAMP) { 210268b8534bSLuigi Rizzo microtime(&kring->ring->ts); 21035819da83SLuigi Rizzo } 210468b8534bSLuigi Rizzo 210568b8534bSLuigi Rizzo if (kring->ring->avail > 0) 210668b8534bSLuigi Rizzo revents |= want_rx; 210768b8534bSLuigi Rizzo else if (!check_all) 210868b8534bSLuigi Rizzo selrecord(td, &kring->si); 210968b8534bSLuigi Rizzo if (na->separate_locks) 21101a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_RX_UNLOCK, i); 211168b8534bSLuigi Rizzo } 211268b8534bSLuigi Rizzo } 211364ae02c3SLuigi Rizzo if (check_all && revents == 0) { /* signal on the global queue */ 211468b8534bSLuigi Rizzo if (want_tx) 211564ae02c3SLuigi Rizzo selrecord(td, &na->tx_si); 211668b8534bSLuigi Rizzo if (want_rx) 211764ae02c3SLuigi Rizzo selrecord(td, &na->rx_si); 211868b8534bSLuigi Rizzo } 2119091fd0abSLuigi Rizzo 2120091fd0abSLuigi Rizzo /* forward host to the netmap ring */ 2121091fd0abSLuigi Rizzo kring = &na->rx_rings[lim_rx]; 2122091fd0abSLuigi Rizzo if (kring->nr_hwavail > 0) 2123091fd0abSLuigi Rizzo ND("host rx %d has %d packets", lim_rx, kring->nr_hwavail); 2124091fd0abSLuigi Rizzo if ( (priv->np_qlast == NETMAP_HW_RING) // XXX check_all 2125091fd0abSLuigi Rizzo && (netmap_fwd || kring->ring->flags & NR_FORWARD) 2126091fd0abSLuigi Rizzo && kring->nr_hwavail > 0 && !host_forwarded) { 2127091fd0abSLuigi Rizzo if (core_lock == NEED_CL) { 2128091fd0abSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_LOCK, 0); 2129091fd0abSLuigi Rizzo core_lock = LOCKED_CL; 2130091fd0abSLuigi Rizzo } 2131091fd0abSLuigi Rizzo netmap_sw_to_nic(na); 2132091fd0abSLuigi Rizzo host_forwarded = 1; /* prevent another pass */ 2133091fd0abSLuigi Rizzo want_rx = 0; 2134091fd0abSLuigi Rizzo goto flush_tx; 2135091fd0abSLuigi Rizzo } 2136091fd0abSLuigi Rizzo 213768b8534bSLuigi Rizzo if (core_lock == LOCKED_CL) 21381a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0); 2139091fd0abSLuigi Rizzo if (q.head) 2140091fd0abSLuigi Rizzo netmap_send_up(na->ifp, q.head); 214168b8534bSLuigi Rizzo 214268b8534bSLuigi Rizzo return (revents); 214368b8534bSLuigi Rizzo } 214468b8534bSLuigi Rizzo 214568b8534bSLuigi Rizzo /*------- driver support routines ------*/ 214668b8534bSLuigi Rizzo 2147f18be576SLuigi Rizzo 214868b8534bSLuigi Rizzo /* 2149babc7c12SLuigi Rizzo * default lock wrapper. 21501a26580eSLuigi Rizzo */ 21511a26580eSLuigi Rizzo static void 2152babc7c12SLuigi Rizzo netmap_lock_wrapper(struct ifnet *dev, int what, u_int queueid) 21531a26580eSLuigi Rizzo { 2154babc7c12SLuigi Rizzo struct netmap_adapter *na = NA(dev); 21551a26580eSLuigi Rizzo 21561a26580eSLuigi Rizzo switch (what) { 2157babc7c12SLuigi Rizzo #ifdef linux /* some system do not need lock on register */ 21581a26580eSLuigi Rizzo case NETMAP_REG_LOCK: 21591a26580eSLuigi Rizzo case NETMAP_REG_UNLOCK: 21601a26580eSLuigi Rizzo break; 2161babc7c12SLuigi Rizzo #endif /* linux */ 21621a26580eSLuigi Rizzo 21631a26580eSLuigi Rizzo case NETMAP_CORE_LOCK: 21641a26580eSLuigi Rizzo mtx_lock(&na->core_lock); 21651a26580eSLuigi Rizzo break; 21661a26580eSLuigi Rizzo 21671a26580eSLuigi Rizzo case NETMAP_CORE_UNLOCK: 21681a26580eSLuigi Rizzo mtx_unlock(&na->core_lock); 21691a26580eSLuigi Rizzo break; 21701a26580eSLuigi Rizzo 21711a26580eSLuigi Rizzo case NETMAP_TX_LOCK: 21721a26580eSLuigi Rizzo mtx_lock(&na->tx_rings[queueid].q_lock); 21731a26580eSLuigi Rizzo break; 21741a26580eSLuigi Rizzo 21751a26580eSLuigi Rizzo case NETMAP_TX_UNLOCK: 21761a26580eSLuigi Rizzo mtx_unlock(&na->tx_rings[queueid].q_lock); 21771a26580eSLuigi Rizzo break; 21781a26580eSLuigi Rizzo 21791a26580eSLuigi Rizzo case NETMAP_RX_LOCK: 21801a26580eSLuigi Rizzo mtx_lock(&na->rx_rings[queueid].q_lock); 21811a26580eSLuigi Rizzo break; 21821a26580eSLuigi Rizzo 21831a26580eSLuigi Rizzo case NETMAP_RX_UNLOCK: 21841a26580eSLuigi Rizzo mtx_unlock(&na->rx_rings[queueid].q_lock); 21851a26580eSLuigi Rizzo break; 21861a26580eSLuigi Rizzo } 21871a26580eSLuigi Rizzo } 21881a26580eSLuigi Rizzo 21891a26580eSLuigi Rizzo 21901a26580eSLuigi Rizzo /* 219168b8534bSLuigi Rizzo * Initialize a ``netmap_adapter`` object created by driver on attach. 219268b8534bSLuigi Rizzo * We allocate a block of memory with room for a struct netmap_adapter 219368b8534bSLuigi Rizzo * plus two sets of N+2 struct netmap_kring (where N is the number 219468b8534bSLuigi Rizzo * of hardware rings): 219568b8534bSLuigi Rizzo * krings 0..N-1 are for the hardware queues. 219668b8534bSLuigi Rizzo * kring N is for the host stack queue 219768b8534bSLuigi Rizzo * kring N+1 is only used for the selinfo for all queues. 219868b8534bSLuigi Rizzo * Return 0 on success, ENOMEM otherwise. 219964ae02c3SLuigi Rizzo * 22000bf88954SEd Maste * By default the receive and transmit adapter ring counts are both initialized 22010bf88954SEd Maste * to num_queues. na->num_tx_rings can be set for cards with different tx/rx 220224e57ec9SEd Maste * setups. 220368b8534bSLuigi Rizzo */ 220468b8534bSLuigi Rizzo int 2205ae10d1afSLuigi Rizzo netmap_attach(struct netmap_adapter *arg, int num_queues) 220668b8534bSLuigi Rizzo { 2207ae10d1afSLuigi Rizzo struct netmap_adapter *na = NULL; 2208ae10d1afSLuigi Rizzo struct ifnet *ifp = arg ? arg->ifp : NULL; 2209f18be576SLuigi Rizzo int len; 221068b8534bSLuigi Rizzo 2211ae10d1afSLuigi Rizzo if (arg == NULL || ifp == NULL) 2212ae10d1afSLuigi Rizzo goto fail; 2213f18be576SLuigi Rizzo len = nma_is_vp(arg) ? sizeof(*na) : sizeof(*na) * 2; 2214f18be576SLuigi Rizzo na = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO); 2215ae10d1afSLuigi Rizzo if (na == NULL) 2216ae10d1afSLuigi Rizzo goto fail; 2217ae10d1afSLuigi Rizzo WNA(ifp) = na; 2218ae10d1afSLuigi Rizzo *na = *arg; /* copy everything, trust the driver to not pass junk */ 2219ae10d1afSLuigi Rizzo NETMAP_SET_CAPABLE(ifp); 2220d76bf4ffSLuigi Rizzo if (na->num_tx_rings == 0) 2221d76bf4ffSLuigi Rizzo na->num_tx_rings = num_queues; 2222d76bf4ffSLuigi Rizzo na->num_rx_rings = num_queues; 2223ae10d1afSLuigi Rizzo na->refcount = na->na_single = na->na_multi = 0; 2224ae10d1afSLuigi Rizzo /* Core lock initialized here, others after netmap_if_new. */ 2225ae10d1afSLuigi Rizzo mtx_init(&na->core_lock, "netmap core lock", MTX_NETWORK_LOCK, MTX_DEF); 2226f196ce38SLuigi Rizzo if (na->nm_lock == NULL) { 2227f196ce38SLuigi Rizzo ND("using default locks for %s", ifp->if_xname); 22281a26580eSLuigi Rizzo na->nm_lock = netmap_lock_wrapper; 2229f196ce38SLuigi Rizzo } 223064ae02c3SLuigi Rizzo #ifdef linux 2231f18be576SLuigi Rizzo if (ifp->netdev_ops) { 2232f18be576SLuigi Rizzo ND("netdev_ops %p", ifp->netdev_ops); 2233f18be576SLuigi Rizzo /* prepare a clone of the netdev ops */ 2234f18be576SLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28) 2235f18be576SLuigi Rizzo na->nm_ndo.ndo_start_xmit = ifp->netdev_ops; 2236f18be576SLuigi Rizzo #else 2237849bec0eSLuigi Rizzo na->nm_ndo = *ifp->netdev_ops; 2238f18be576SLuigi Rizzo #endif 2239f18be576SLuigi Rizzo } 224042a3a5bdSLuigi Rizzo na->nm_ndo.ndo_start_xmit = linux_netmap_start; 2241f18be576SLuigi Rizzo #endif 2242f18be576SLuigi Rizzo if (!nma_is_vp(arg)) 2243f18be576SLuigi Rizzo netmap_attach_sw(ifp); 2244ae10d1afSLuigi Rizzo D("success for %s", ifp->if_xname); 2245ae10d1afSLuigi Rizzo return 0; 224668b8534bSLuigi Rizzo 2247ae10d1afSLuigi Rizzo fail: 2248ae10d1afSLuigi Rizzo D("fail, arg %p ifp %p na %p", arg, ifp, na); 2249849bec0eSLuigi Rizzo netmap_detach(ifp); 2250ae10d1afSLuigi Rizzo return (na ? EINVAL : ENOMEM); 225168b8534bSLuigi Rizzo } 225268b8534bSLuigi Rizzo 225368b8534bSLuigi Rizzo 225468b8534bSLuigi Rizzo /* 225568b8534bSLuigi Rizzo * Free the allocated memory linked to the given ``netmap_adapter`` 225668b8534bSLuigi Rizzo * object. 225768b8534bSLuigi Rizzo */ 225868b8534bSLuigi Rizzo void 225968b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp) 226068b8534bSLuigi Rizzo { 226168b8534bSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 226268b8534bSLuigi Rizzo 226368b8534bSLuigi Rizzo if (!na) 226468b8534bSLuigi Rizzo return; 226568b8534bSLuigi Rizzo 22662f70fca5SEd Maste mtx_destroy(&na->core_lock); 22672f70fca5SEd Maste 2268ae10d1afSLuigi Rizzo if (na->tx_rings) { /* XXX should not happen */ 2269ae10d1afSLuigi Rizzo D("freeing leftover tx_rings"); 2270ae10d1afSLuigi Rizzo free(na->tx_rings, M_DEVBUF); 2271ae10d1afSLuigi Rizzo } 227268b8534bSLuigi Rizzo bzero(na, sizeof(*na)); 2273d0c7b075SLuigi Rizzo WNA(ifp) = NULL; 227468b8534bSLuigi Rizzo free(na, M_DEVBUF); 227568b8534bSLuigi Rizzo } 227668b8534bSLuigi Rizzo 227768b8534bSLuigi Rizzo 2278f18be576SLuigi Rizzo int 2279f18be576SLuigi Rizzo nm_bdg_flush(struct nm_bdg_fwd *ft, int n, struct netmap_adapter *na, u_int ring_nr); 2280f18be576SLuigi Rizzo 2281f18be576SLuigi Rizzo /* we don't need to lock myself */ 2282f18be576SLuigi Rizzo static int 2283f18be576SLuigi Rizzo bdg_netmap_start(struct ifnet *ifp, struct mbuf *m) 2284f18be576SLuigi Rizzo { 2285f18be576SLuigi Rizzo struct netmap_adapter *na = SWNA(ifp); 2286f18be576SLuigi Rizzo struct nm_bdg_fwd *ft = na->rx_rings[0].nkr_ft; 2287f18be576SLuigi Rizzo char *buf = NMB(&na->rx_rings[0].ring->slot[0]); 2288f18be576SLuigi Rizzo u_int len = MBUF_LEN(m); 2289f18be576SLuigi Rizzo 2290f18be576SLuigi Rizzo if (!na->na_bdg) /* SWNA is not configured to be attached */ 2291f18be576SLuigi Rizzo return EBUSY; 2292f18be576SLuigi Rizzo m_copydata(m, 0, len, buf); 2293*85233a7dSLuigi Rizzo ft->ft_flags = 0; // XXX could be indirect ? 2294f18be576SLuigi Rizzo ft->ft_len = len; 2295*85233a7dSLuigi Rizzo ft->ft_buf = buf; 2296*85233a7dSLuigi Rizzo ft->ft_next = NM_BDG_BATCH; // XXX is it needed ? 2297f18be576SLuigi Rizzo nm_bdg_flush(ft, 1, na, 0); 2298f18be576SLuigi Rizzo 2299f18be576SLuigi Rizzo /* release the mbuf in either cases of success or failure. As an 2300f18be576SLuigi Rizzo * alternative, put the mbuf in a free list and free the list 2301f18be576SLuigi Rizzo * only when really necessary. 2302f18be576SLuigi Rizzo */ 2303f18be576SLuigi Rizzo m_freem(m); 2304f18be576SLuigi Rizzo 2305f18be576SLuigi Rizzo return (0); 2306f18be576SLuigi Rizzo } 2307f18be576SLuigi Rizzo 2308f18be576SLuigi Rizzo 230968b8534bSLuigi Rizzo /* 231002ad4083SLuigi Rizzo * Intercept packets from the network stack and pass them 231102ad4083SLuigi Rizzo * to netmap as incoming packets on the 'software' ring. 231268b8534bSLuigi Rizzo * We are not locked when called. 231368b8534bSLuigi Rizzo */ 231468b8534bSLuigi Rizzo int 231568b8534bSLuigi Rizzo netmap_start(struct ifnet *ifp, struct mbuf *m) 231668b8534bSLuigi Rizzo { 231768b8534bSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 2318d76bf4ffSLuigi Rizzo struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings]; 23191a26580eSLuigi Rizzo u_int i, len = MBUF_LEN(m); 2320b3d53016SLuigi Rizzo u_int error = EBUSY, lim = kring->nkr_num_slots - 1; 232168b8534bSLuigi Rizzo struct netmap_slot *slot; 232268b8534bSLuigi Rizzo 232368b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_HOST) 232468b8534bSLuigi Rizzo D("%s packet %d len %d from the stack", ifp->if_xname, 232568b8534bSLuigi Rizzo kring->nr_hwcur + kring->nr_hwavail, len); 2326849bec0eSLuigi Rizzo if (len > NETMAP_BUF_SIZE) { /* too long for us */ 2327849bec0eSLuigi Rizzo D("%s from_host, drop packet size %d > %d", ifp->if_xname, 2328849bec0eSLuigi Rizzo len, NETMAP_BUF_SIZE); 2329849bec0eSLuigi Rizzo m_freem(m); 2330849bec0eSLuigi Rizzo return EINVAL; 2331849bec0eSLuigi Rizzo } 2332f18be576SLuigi Rizzo if (na->na_bdg) 2333f18be576SLuigi Rizzo return bdg_netmap_start(ifp, m); 2334f18be576SLuigi Rizzo 23351a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_LOCK, 0); 233602ad4083SLuigi Rizzo if (kring->nr_hwavail >= lim) { 23375b248374SLuigi Rizzo if (netmap_verbose) 233868b8534bSLuigi Rizzo D("stack ring %s full\n", ifp->if_xname); 233968b8534bSLuigi Rizzo goto done; /* no space */ 234068b8534bSLuigi Rizzo } 234168b8534bSLuigi Rizzo 234268b8534bSLuigi Rizzo /* compute the insert position */ 234368b8534bSLuigi Rizzo i = kring->nr_hwcur + kring->nr_hwavail; 234402ad4083SLuigi Rizzo if (i > lim) 234502ad4083SLuigi Rizzo i -= lim + 1; 234668b8534bSLuigi Rizzo slot = &kring->ring->slot[i]; 234768b8534bSLuigi Rizzo m_copydata(m, 0, len, NMB(slot)); 234868b8534bSLuigi Rizzo slot->len = len; 2349091fd0abSLuigi Rizzo slot->flags = kring->nkr_slot_flags; 235068b8534bSLuigi Rizzo kring->nr_hwavail++; 235168b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_HOST) 2352d76bf4ffSLuigi Rizzo D("wake up host ring %s %d", na->ifp->if_xname, na->num_rx_rings); 235368b8534bSLuigi Rizzo selwakeuppri(&kring->si, PI_NET); 235468b8534bSLuigi Rizzo error = 0; 235568b8534bSLuigi Rizzo done: 23561a26580eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0); 235768b8534bSLuigi Rizzo 235868b8534bSLuigi Rizzo /* release the mbuf in either cases of success or failure. As an 235968b8534bSLuigi Rizzo * alternative, put the mbuf in a free list and free the list 236068b8534bSLuigi Rizzo * only when really necessary. 236168b8534bSLuigi Rizzo */ 236268b8534bSLuigi Rizzo m_freem(m); 236368b8534bSLuigi Rizzo 236468b8534bSLuigi Rizzo return (error); 236568b8534bSLuigi Rizzo } 236668b8534bSLuigi Rizzo 236768b8534bSLuigi Rizzo 236868b8534bSLuigi Rizzo /* 236968b8534bSLuigi Rizzo * netmap_reset() is called by the driver routines when reinitializing 237068b8534bSLuigi Rizzo * a ring. The driver is in charge of locking to protect the kring. 237168b8534bSLuigi Rizzo * If netmap mode is not set just return NULL. 237268b8534bSLuigi Rizzo */ 237368b8534bSLuigi Rizzo struct netmap_slot * 237468b8534bSLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, int n, 237568b8534bSLuigi Rizzo u_int new_cur) 237668b8534bSLuigi Rizzo { 237768b8534bSLuigi Rizzo struct netmap_kring *kring; 2378506cc70cSLuigi Rizzo int new_hwofs, lim; 237968b8534bSLuigi Rizzo 238068b8534bSLuigi Rizzo if (na == NULL) 238168b8534bSLuigi Rizzo return NULL; /* no netmap support here */ 238268b8534bSLuigi Rizzo if (!(na->ifp->if_capenable & IFCAP_NETMAP)) 238368b8534bSLuigi Rizzo return NULL; /* nothing to reinitialize */ 238468b8534bSLuigi Rizzo 238564ae02c3SLuigi Rizzo if (tx == NR_TX) { 23868241616dSLuigi Rizzo if (n >= na->num_tx_rings) 23878241616dSLuigi Rizzo return NULL; 238864ae02c3SLuigi Rizzo kring = na->tx_rings + n; 2389506cc70cSLuigi Rizzo new_hwofs = kring->nr_hwcur - new_cur; 239064ae02c3SLuigi Rizzo } else { 23918241616dSLuigi Rizzo if (n >= na->num_rx_rings) 23928241616dSLuigi Rizzo return NULL; 239364ae02c3SLuigi Rizzo kring = na->rx_rings + n; 2394506cc70cSLuigi Rizzo new_hwofs = kring->nr_hwcur + kring->nr_hwavail - new_cur; 239564ae02c3SLuigi Rizzo } 239664ae02c3SLuigi Rizzo lim = kring->nkr_num_slots - 1; 2397506cc70cSLuigi Rizzo if (new_hwofs > lim) 2398506cc70cSLuigi Rizzo new_hwofs -= lim + 1; 2399506cc70cSLuigi Rizzo 2400506cc70cSLuigi Rizzo /* Alwayws set the new offset value and realign the ring. */ 2401506cc70cSLuigi Rizzo kring->nkr_hwofs = new_hwofs; 2402506cc70cSLuigi Rizzo if (tx == NR_TX) 2403506cc70cSLuigi Rizzo kring->nr_hwavail = kring->nkr_num_slots - 1; 24048241616dSLuigi Rizzo ND(10, "new hwofs %d on %s %s[%d]", 2405506cc70cSLuigi Rizzo kring->nkr_hwofs, na->ifp->if_xname, 2406506cc70cSLuigi Rizzo tx == NR_TX ? "TX" : "RX", n); 2407506cc70cSLuigi Rizzo 2408f196ce38SLuigi Rizzo #if 0 // def linux 2409f196ce38SLuigi Rizzo /* XXX check that the mappings are correct */ 2410f196ce38SLuigi Rizzo /* need ring_nr, adapter->pdev, direction */ 2411f196ce38SLuigi Rizzo buffer_info->dma = dma_map_single(&pdev->dev, addr, adapter->rx_buffer_len, DMA_FROM_DEVICE); 2412f196ce38SLuigi Rizzo if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) { 2413f196ce38SLuigi Rizzo D("error mapping rx netmap buffer %d", i); 2414f196ce38SLuigi Rizzo // XXX fix error handling 2415f196ce38SLuigi Rizzo } 2416f196ce38SLuigi Rizzo 2417f196ce38SLuigi Rizzo #endif /* linux */ 241868b8534bSLuigi Rizzo /* 241964ae02c3SLuigi Rizzo * Wakeup on the individual and global lock 2420506cc70cSLuigi Rizzo * We do the wakeup here, but the ring is not yet reconfigured. 2421506cc70cSLuigi Rizzo * However, we are under lock so there are no races. 242268b8534bSLuigi Rizzo */ 242368b8534bSLuigi Rizzo selwakeuppri(&kring->si, PI_NET); 242464ae02c3SLuigi Rizzo selwakeuppri(tx == NR_TX ? &na->tx_si : &na->rx_si, PI_NET); 242568b8534bSLuigi Rizzo return kring->ring->slot; 242668b8534bSLuigi Rizzo } 242768b8534bSLuigi Rizzo 242868b8534bSLuigi Rizzo 2429f18be576SLuigi Rizzo /* returns the next position in the ring */ 2430f18be576SLuigi Rizzo static int 2431f18be576SLuigi Rizzo nm_bdg_preflush(struct netmap_adapter *na, u_int ring_nr, 2432f18be576SLuigi Rizzo struct netmap_kring *kring, u_int end) 2433f18be576SLuigi Rizzo { 2434f18be576SLuigi Rizzo struct netmap_ring *ring = kring->ring; 2435f18be576SLuigi Rizzo struct nm_bdg_fwd *ft = kring->nkr_ft; 2436f18be576SLuigi Rizzo u_int j = kring->nr_hwcur, lim = kring->nkr_num_slots - 1; 2437f18be576SLuigi Rizzo u_int ft_i = 0; /* start from 0 */ 2438f18be576SLuigi Rizzo 2439f18be576SLuigi Rizzo for (; likely(j != end); j = unlikely(j == lim) ? 0 : j+1) { 2440f18be576SLuigi Rizzo struct netmap_slot *slot = &ring->slot[j]; 2441*85233a7dSLuigi Rizzo char *buf = NMB(slot); 2442f18be576SLuigi Rizzo int len = ft[ft_i].ft_len = slot->len; 2443f18be576SLuigi Rizzo 2444*85233a7dSLuigi Rizzo ft[ft_i].ft_flags = slot->flags; 2445*85233a7dSLuigi Rizzo 2446*85233a7dSLuigi Rizzo ND("flags is 0x%x", slot->flags); 2447*85233a7dSLuigi Rizzo /* this slot goes into a list so initialize the link field */ 2448*85233a7dSLuigi Rizzo ft[ft_i].ft_next = NM_BDG_BATCH; /* equivalent to NULL */ 2449f18be576SLuigi Rizzo if (unlikely(len < 14)) 2450f18be576SLuigi Rizzo continue; 2451*85233a7dSLuigi Rizzo buf = ft[ft_i].ft_buf = (slot->flags & NS_INDIRECT) ? 2452*85233a7dSLuigi Rizzo *((void **)buf) : buf; 2453*85233a7dSLuigi Rizzo prefetch(buf); 2454f18be576SLuigi Rizzo if (unlikely(++ft_i == netmap_bridge)) 2455f18be576SLuigi Rizzo ft_i = nm_bdg_flush(ft, ft_i, na, ring_nr); 2456f18be576SLuigi Rizzo } 2457f18be576SLuigi Rizzo if (ft_i) 2458f18be576SLuigi Rizzo ft_i = nm_bdg_flush(ft, ft_i, na, ring_nr); 2459f18be576SLuigi Rizzo return j; 2460f18be576SLuigi Rizzo } 2461f18be576SLuigi Rizzo 2462f18be576SLuigi Rizzo 2463f18be576SLuigi Rizzo /* 2464f18be576SLuigi Rizzo * Pass packets from nic to the bridge. Must be called with 2465f18be576SLuigi Rizzo * proper locks on the source interface. 2466f18be576SLuigi Rizzo * Note, no user process can access this NIC so we can ignore 2467f18be576SLuigi Rizzo * the info in the 'ring'. 2468f18be576SLuigi Rizzo */ 2469f18be576SLuigi Rizzo static void 2470f18be576SLuigi Rizzo netmap_nic_to_bdg(struct ifnet *ifp, u_int ring_nr) 2471f18be576SLuigi Rizzo { 2472f18be576SLuigi Rizzo struct netmap_adapter *na = NA(ifp); 2473f18be576SLuigi Rizzo struct netmap_kring *kring = &na->rx_rings[ring_nr]; 2474f18be576SLuigi Rizzo struct netmap_ring *ring = kring->ring; 2475f18be576SLuigi Rizzo int j, k, lim = kring->nkr_num_slots - 1; 2476f18be576SLuigi Rizzo 2477f18be576SLuigi Rizzo /* fetch packets that have arrived */ 2478f18be576SLuigi Rizzo na->nm_rxsync(ifp, ring_nr, 0); 2479f18be576SLuigi Rizzo /* XXX we don't count reserved, but it should be 0 */ 2480f18be576SLuigi Rizzo j = kring->nr_hwcur; 2481f18be576SLuigi Rizzo k = j + kring->nr_hwavail; 2482f18be576SLuigi Rizzo if (k > lim) 2483f18be576SLuigi Rizzo k -= lim + 1; 2484f18be576SLuigi Rizzo if (k == j && netmap_verbose) { 2485f18be576SLuigi Rizzo D("how strange, interrupt with no packets on %s", 2486f18be576SLuigi Rizzo ifp->if_xname); 2487f18be576SLuigi Rizzo return; 2488f18be576SLuigi Rizzo } 2489f18be576SLuigi Rizzo 2490f18be576SLuigi Rizzo j = nm_bdg_preflush(na, ring_nr, kring, k); 2491f18be576SLuigi Rizzo 2492f18be576SLuigi Rizzo /* we consume everything, but we cannot update kring directly 2493f18be576SLuigi Rizzo * because the nic may have destroyed the info in the NIC ring. 2494f18be576SLuigi Rizzo * So we need to call rxsync again to restore it. 2495f18be576SLuigi Rizzo */ 2496f18be576SLuigi Rizzo ring->cur = j; 2497f18be576SLuigi Rizzo ring->avail = 0; 2498f18be576SLuigi Rizzo na->nm_rxsync(ifp, ring_nr, 0); 2499f18be576SLuigi Rizzo return; 2500f18be576SLuigi Rizzo } 2501f18be576SLuigi Rizzo 2502f18be576SLuigi Rizzo 250368b8534bSLuigi Rizzo /* 25041a26580eSLuigi Rizzo * Default functions to handle rx/tx interrupts 25051a26580eSLuigi Rizzo * we have 4 cases: 25061a26580eSLuigi Rizzo * 1 ring, single lock: 25071a26580eSLuigi Rizzo * lock(core); wake(i=0); unlock(core) 25081a26580eSLuigi Rizzo * N rings, single lock: 25091a26580eSLuigi Rizzo * lock(core); wake(i); wake(N+1) unlock(core) 25101a26580eSLuigi Rizzo * 1 ring, separate locks: (i=0) 25111a26580eSLuigi Rizzo * lock(i); wake(i); unlock(i) 25121a26580eSLuigi Rizzo * N rings, separate locks: 25131a26580eSLuigi Rizzo * lock(i); wake(i); unlock(i); lock(core) wake(N+1) unlock(core) 251464ae02c3SLuigi Rizzo * work_done is non-null on the RX path. 2515849bec0eSLuigi Rizzo * 2516849bec0eSLuigi Rizzo * The 'q' argument also includes flag to tell whether the queue is 2517849bec0eSLuigi Rizzo * already locked on enter, and whether it should remain locked on exit. 2518849bec0eSLuigi Rizzo * This helps adapting to different defaults in drivers and OSes. 25191a26580eSLuigi Rizzo */ 2520babc7c12SLuigi Rizzo int 2521babc7c12SLuigi Rizzo netmap_rx_irq(struct ifnet *ifp, int q, int *work_done) 25221a26580eSLuigi Rizzo { 25231a26580eSLuigi Rizzo struct netmap_adapter *na; 25241a26580eSLuigi Rizzo struct netmap_kring *r; 252564ae02c3SLuigi Rizzo NM_SELINFO_T *main_wq; 2526f18be576SLuigi Rizzo int locktype, unlocktype, nic_to_bridge, lock; 25271a26580eSLuigi Rizzo 25281a26580eSLuigi Rizzo if (!(ifp->if_capenable & IFCAP_NETMAP)) 25291a26580eSLuigi Rizzo return 0; 2530849bec0eSLuigi Rizzo 2531849bec0eSLuigi Rizzo lock = q & (NETMAP_LOCKED_ENTER | NETMAP_LOCKED_EXIT); 2532849bec0eSLuigi Rizzo q = q & NETMAP_RING_MASK; 2533849bec0eSLuigi Rizzo 25348241616dSLuigi Rizzo ND(5, "received %s queue %d", work_done ? "RX" : "TX" , q); 25351a26580eSLuigi Rizzo na = NA(ifp); 25368241616dSLuigi Rizzo if (na->na_flags & NAF_SKIP_INTR) { 25378241616dSLuigi Rizzo ND("use regular interrupt"); 25388241616dSLuigi Rizzo return 0; 25398241616dSLuigi Rizzo } 25408241616dSLuigi Rizzo 254164ae02c3SLuigi Rizzo if (work_done) { /* RX path */ 25428241616dSLuigi Rizzo if (q >= na->num_rx_rings) 2543849bec0eSLuigi Rizzo return 0; // not a physical queue 254464ae02c3SLuigi Rizzo r = na->rx_rings + q; 254564ae02c3SLuigi Rizzo r->nr_kflags |= NKR_PENDINTR; 2546d76bf4ffSLuigi Rizzo main_wq = (na->num_rx_rings > 1) ? &na->rx_si : NULL; 2547f18be576SLuigi Rizzo /* set a flag if the NIC is attached to a VALE switch */ 2548f18be576SLuigi Rizzo nic_to_bridge = (na->na_bdg != NULL); 2549849bec0eSLuigi Rizzo locktype = NETMAP_RX_LOCK; 2550849bec0eSLuigi Rizzo unlocktype = NETMAP_RX_UNLOCK; 2551849bec0eSLuigi Rizzo } else { /* TX path */ 25528241616dSLuigi Rizzo if (q >= na->num_tx_rings) 2553849bec0eSLuigi Rizzo return 0; // not a physical queue 255464ae02c3SLuigi Rizzo r = na->tx_rings + q; 2555d76bf4ffSLuigi Rizzo main_wq = (na->num_tx_rings > 1) ? &na->tx_si : NULL; 255664ae02c3SLuigi Rizzo work_done = &q; /* dummy */ 2557f18be576SLuigi Rizzo nic_to_bridge = 0; 2558849bec0eSLuigi Rizzo locktype = NETMAP_TX_LOCK; 2559849bec0eSLuigi Rizzo unlocktype = NETMAP_TX_UNLOCK; 256064ae02c3SLuigi Rizzo } 25611a26580eSLuigi Rizzo if (na->separate_locks) { 2562849bec0eSLuigi Rizzo if (!(lock & NETMAP_LOCKED_ENTER)) 2563849bec0eSLuigi Rizzo na->nm_lock(ifp, locktype, q); 2564f18be576SLuigi Rizzo /* If a NIC is attached to a bridge, flush packets 2565f18be576SLuigi Rizzo * (and no need to wakeup anyone). Otherwise, wakeup 2566f18be576SLuigi Rizzo * possible processes waiting for packets. 2567f18be576SLuigi Rizzo */ 2568f18be576SLuigi Rizzo if (nic_to_bridge) 2569f18be576SLuigi Rizzo netmap_nic_to_bdg(ifp, q); 2570f18be576SLuigi Rizzo else 257164ae02c3SLuigi Rizzo selwakeuppri(&r->si, PI_NET); 2572849bec0eSLuigi Rizzo na->nm_lock(ifp, unlocktype, q); 2573f18be576SLuigi Rizzo if (main_wq && !nic_to_bridge) { 2574849bec0eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_LOCK, 0); 257564ae02c3SLuigi Rizzo selwakeuppri(main_wq, PI_NET); 2576849bec0eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0); 25771a26580eSLuigi Rizzo } 2578849bec0eSLuigi Rizzo /* lock the queue again if requested */ 2579849bec0eSLuigi Rizzo if (lock & NETMAP_LOCKED_EXIT) 2580849bec0eSLuigi Rizzo na->nm_lock(ifp, locktype, q); 25811a26580eSLuigi Rizzo } else { 2582849bec0eSLuigi Rizzo if (!(lock & NETMAP_LOCKED_ENTER)) 2583849bec0eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_LOCK, 0); 2584f18be576SLuigi Rizzo if (nic_to_bridge) 2585f18be576SLuigi Rizzo netmap_nic_to_bdg(ifp, q); 2586f18be576SLuigi Rizzo else { 258764ae02c3SLuigi Rizzo selwakeuppri(&r->si, PI_NET); 258864ae02c3SLuigi Rizzo if (main_wq) 258964ae02c3SLuigi Rizzo selwakeuppri(main_wq, PI_NET); 2590f18be576SLuigi Rizzo } 2591849bec0eSLuigi Rizzo if (!(lock & NETMAP_LOCKED_EXIT)) 2592849bec0eSLuigi Rizzo na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0); 25931a26580eSLuigi Rizzo } 25941a26580eSLuigi Rizzo *work_done = 1; /* do not fire napi again */ 25951a26580eSLuigi Rizzo return 1; 25961a26580eSLuigi Rizzo } 25971a26580eSLuigi Rizzo 259864ae02c3SLuigi Rizzo 259901c7d25fSLuigi Rizzo #ifdef linux /* linux-specific routines */ 260001c7d25fSLuigi Rizzo 2601f18be576SLuigi Rizzo 260201c7d25fSLuigi Rizzo /* 260301c7d25fSLuigi Rizzo * Remap linux arguments into the FreeBSD call. 260401c7d25fSLuigi Rizzo * - pwait is the poll table, passed as 'dev'; 260501c7d25fSLuigi Rizzo * If pwait == NULL someone else already woke up before. We can report 260601c7d25fSLuigi Rizzo * events but they are filtered upstream. 260701c7d25fSLuigi Rizzo * If pwait != NULL, then pwait->key contains the list of events. 260801c7d25fSLuigi Rizzo * - events is computed from pwait as above. 260901c7d25fSLuigi Rizzo * - file is passed as 'td'; 261001c7d25fSLuigi Rizzo */ 261101c7d25fSLuigi Rizzo static u_int 261201c7d25fSLuigi Rizzo linux_netmap_poll(struct file * file, struct poll_table_struct *pwait) 261301c7d25fSLuigi Rizzo { 2614849bec0eSLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28) 2615849bec0eSLuigi Rizzo int events = POLLIN | POLLOUT; /* XXX maybe... */ 2616849bec0eSLuigi Rizzo #elif LINUX_VERSION_CODE < KERNEL_VERSION(3,4,0) 261701c7d25fSLuigi Rizzo int events = pwait ? pwait->key : POLLIN | POLLOUT; 261801c7d25fSLuigi Rizzo #else /* in 3.4.0 field 'key' was renamed to '_key' */ 261901c7d25fSLuigi Rizzo int events = pwait ? pwait->_key : POLLIN | POLLOUT; 262001c7d25fSLuigi Rizzo #endif 262101c7d25fSLuigi Rizzo return netmap_poll((void *)pwait, events, (void *)file); 262201c7d25fSLuigi Rizzo } 262301c7d25fSLuigi Rizzo 2624f18be576SLuigi Rizzo 262501c7d25fSLuigi Rizzo static int 262642a3a5bdSLuigi Rizzo linux_netmap_mmap(struct file *f, struct vm_area_struct *vma) 262701c7d25fSLuigi Rizzo { 262801c7d25fSLuigi Rizzo int lut_skip, i, j; 262901c7d25fSLuigi Rizzo int user_skip = 0; 263001c7d25fSLuigi Rizzo struct lut_entry *l_entry; 26318241616dSLuigi Rizzo int error = 0; 26328241616dSLuigi Rizzo unsigned long off, tomap; 263301c7d25fSLuigi Rizzo /* 263401c7d25fSLuigi Rizzo * vma->vm_start: start of mapping user address space 263501c7d25fSLuigi Rizzo * vma->vm_end: end of the mapping user address space 26368241616dSLuigi Rizzo * vma->vm_pfoff: offset of first page in the device 263701c7d25fSLuigi Rizzo */ 263801c7d25fSLuigi Rizzo 263901c7d25fSLuigi Rizzo // XXX security checks 264001c7d25fSLuigi Rizzo 26418241616dSLuigi Rizzo error = netmap_get_memory(f->private_data); 26428241616dSLuigi Rizzo ND("get_memory returned %d", error); 26438241616dSLuigi Rizzo if (error) 26448241616dSLuigi Rizzo return -error; 26458241616dSLuigi Rizzo 26468241616dSLuigi Rizzo off = vma->vm_pgoff << PAGE_SHIFT; /* offset in bytes */ 26478241616dSLuigi Rizzo tomap = vma->vm_end - vma->vm_start; 26488241616dSLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { /* loop through obj_pools */ 26498241616dSLuigi Rizzo const struct netmap_obj_pool *p = &nm_mem.pools[i]; 265001c7d25fSLuigi Rizzo /* 265101c7d25fSLuigi Rizzo * In each pool memory is allocated in clusters 265201c7d25fSLuigi Rizzo * of size _clustsize, each containing clustentries 265301c7d25fSLuigi Rizzo * entries. For each object k we already store the 26548241616dSLuigi Rizzo * vtophys mapping in lut[k] so we use that, scanning 265501c7d25fSLuigi Rizzo * the lut[] array in steps of clustentries, 265601c7d25fSLuigi Rizzo * and we map each cluster (not individual pages, 2657849bec0eSLuigi Rizzo * it would be overkill -- XXX slow ? 20130415). 265801c7d25fSLuigi Rizzo */ 26598241616dSLuigi Rizzo 26608241616dSLuigi Rizzo /* 26618241616dSLuigi Rizzo * We interpret vm_pgoff as an offset into the whole 26628241616dSLuigi Rizzo * netmap memory, as if all clusters where contiguous. 26638241616dSLuigi Rizzo */ 26648241616dSLuigi Rizzo for (lut_skip = 0, j = 0; j < p->_numclusters; j++, lut_skip += p->clustentries) { 26658241616dSLuigi Rizzo unsigned long paddr, mapsize; 26668241616dSLuigi Rizzo if (p->_clustsize <= off) { 26678241616dSLuigi Rizzo off -= p->_clustsize; 26688241616dSLuigi Rizzo continue; 26698241616dSLuigi Rizzo } 26708241616dSLuigi Rizzo l_entry = &p->lut[lut_skip]; /* first obj in the cluster */ 26718241616dSLuigi Rizzo paddr = l_entry->paddr + off; 26728241616dSLuigi Rizzo mapsize = p->_clustsize - off; 26738241616dSLuigi Rizzo off = 0; 26748241616dSLuigi Rizzo if (mapsize > tomap) 26758241616dSLuigi Rizzo mapsize = tomap; 26768241616dSLuigi Rizzo ND("remap_pfn_range(%lx, %lx, %lx)", 26778241616dSLuigi Rizzo vma->vm_start + user_skip, 26788241616dSLuigi Rizzo paddr >> PAGE_SHIFT, mapsize); 267901c7d25fSLuigi Rizzo if (remap_pfn_range(vma, vma->vm_start + user_skip, 26808241616dSLuigi Rizzo paddr >> PAGE_SHIFT, mapsize, 268101c7d25fSLuigi Rizzo vma->vm_page_prot)) 268201c7d25fSLuigi Rizzo return -EAGAIN; // XXX check return value 26838241616dSLuigi Rizzo user_skip += mapsize; 26848241616dSLuigi Rizzo tomap -= mapsize; 26858241616dSLuigi Rizzo if (tomap == 0) 26868241616dSLuigi Rizzo goto done; 268701c7d25fSLuigi Rizzo } 268801c7d25fSLuigi Rizzo } 26898241616dSLuigi Rizzo done: 269001c7d25fSLuigi Rizzo 269101c7d25fSLuigi Rizzo return 0; 269201c7d25fSLuigi Rizzo } 269301c7d25fSLuigi Rizzo 2694f18be576SLuigi Rizzo 269501c7d25fSLuigi Rizzo static netdev_tx_t 269642a3a5bdSLuigi Rizzo linux_netmap_start(struct sk_buff *skb, struct net_device *dev) 269701c7d25fSLuigi Rizzo { 269801c7d25fSLuigi Rizzo netmap_start(dev, skb); 269901c7d25fSLuigi Rizzo return (NETDEV_TX_OK); 270001c7d25fSLuigi Rizzo } 270101c7d25fSLuigi Rizzo 270201c7d25fSLuigi Rizzo 27030b8ed8e0SLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37) // XXX was 38 270401c7d25fSLuigi Rizzo #define LIN_IOCTL_NAME .ioctl 270501c7d25fSLuigi Rizzo int 270601c7d25fSLuigi Rizzo linux_netmap_ioctl(struct inode *inode, struct file *file, u_int cmd, u_long data /* arg */) 270701c7d25fSLuigi Rizzo #else 270801c7d25fSLuigi Rizzo #define LIN_IOCTL_NAME .unlocked_ioctl 270901c7d25fSLuigi Rizzo long 271001c7d25fSLuigi Rizzo linux_netmap_ioctl(struct file *file, u_int cmd, u_long data /* arg */) 271101c7d25fSLuigi Rizzo #endif 271201c7d25fSLuigi Rizzo { 271301c7d25fSLuigi Rizzo int ret; 271401c7d25fSLuigi Rizzo struct nmreq nmr; 271501c7d25fSLuigi Rizzo bzero(&nmr, sizeof(nmr)); 271601c7d25fSLuigi Rizzo 271701c7d25fSLuigi Rizzo if (data && copy_from_user(&nmr, (void *)data, sizeof(nmr) ) != 0) 271801c7d25fSLuigi Rizzo return -EFAULT; 271901c7d25fSLuigi Rizzo ret = netmap_ioctl(NULL, cmd, (caddr_t)&nmr, 0, (void *)file); 272001c7d25fSLuigi Rizzo if (data && copy_to_user((void*)data, &nmr, sizeof(nmr) ) != 0) 272101c7d25fSLuigi Rizzo return -EFAULT; 272201c7d25fSLuigi Rizzo return -ret; 272301c7d25fSLuigi Rizzo } 272401c7d25fSLuigi Rizzo 272501c7d25fSLuigi Rizzo 272601c7d25fSLuigi Rizzo static int 27270b8ed8e0SLuigi Rizzo netmap_release(struct inode *inode, struct file *file) 272801c7d25fSLuigi Rizzo { 27290b8ed8e0SLuigi Rizzo (void)inode; /* UNUSED */ 273001c7d25fSLuigi Rizzo if (file->private_data) 273101c7d25fSLuigi Rizzo netmap_dtor(file->private_data); 273201c7d25fSLuigi Rizzo return (0); 273301c7d25fSLuigi Rizzo } 273401c7d25fSLuigi Rizzo 2735f18be576SLuigi Rizzo 27368241616dSLuigi Rizzo static int 27378241616dSLuigi Rizzo linux_netmap_open(struct inode *inode, struct file *file) 27388241616dSLuigi Rizzo { 27398241616dSLuigi Rizzo struct netmap_priv_d *priv; 27408241616dSLuigi Rizzo (void)inode; /* UNUSED */ 27418241616dSLuigi Rizzo 27428241616dSLuigi Rizzo priv = malloc(sizeof(struct netmap_priv_d), M_DEVBUF, 27438241616dSLuigi Rizzo M_NOWAIT | M_ZERO); 27448241616dSLuigi Rizzo if (priv == NULL) 27458241616dSLuigi Rizzo return -ENOMEM; 27468241616dSLuigi Rizzo 27478241616dSLuigi Rizzo file->private_data = priv; 27488241616dSLuigi Rizzo 27498241616dSLuigi Rizzo return (0); 27508241616dSLuigi Rizzo } 275101c7d25fSLuigi Rizzo 2752f18be576SLuigi Rizzo 275301c7d25fSLuigi Rizzo static struct file_operations netmap_fops = { 2754f18be576SLuigi Rizzo .owner = THIS_MODULE, 27558241616dSLuigi Rizzo .open = linux_netmap_open, 275642a3a5bdSLuigi Rizzo .mmap = linux_netmap_mmap, 275701c7d25fSLuigi Rizzo LIN_IOCTL_NAME = linux_netmap_ioctl, 275801c7d25fSLuigi Rizzo .poll = linux_netmap_poll, 275901c7d25fSLuigi Rizzo .release = netmap_release, 276001c7d25fSLuigi Rizzo }; 276101c7d25fSLuigi Rizzo 2762f18be576SLuigi Rizzo 276301c7d25fSLuigi Rizzo static struct miscdevice netmap_cdevsw = { /* same name as FreeBSD */ 276401c7d25fSLuigi Rizzo MISC_DYNAMIC_MINOR, 276501c7d25fSLuigi Rizzo "netmap", 276601c7d25fSLuigi Rizzo &netmap_fops, 276701c7d25fSLuigi Rizzo }; 276801c7d25fSLuigi Rizzo 276901c7d25fSLuigi Rizzo static int netmap_init(void); 277001c7d25fSLuigi Rizzo static void netmap_fini(void); 277101c7d25fSLuigi Rizzo 2772f18be576SLuigi Rizzo 277342a3a5bdSLuigi Rizzo /* Errors have negative values on linux */ 277442a3a5bdSLuigi Rizzo static int linux_netmap_init(void) 277542a3a5bdSLuigi Rizzo { 277642a3a5bdSLuigi Rizzo return -netmap_init(); 277742a3a5bdSLuigi Rizzo } 277842a3a5bdSLuigi Rizzo 277942a3a5bdSLuigi Rizzo module_init(linux_netmap_init); 278001c7d25fSLuigi Rizzo module_exit(netmap_fini); 278101c7d25fSLuigi Rizzo /* export certain symbols to other modules */ 278201c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_attach); // driver attach routines 278301c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_detach); // driver detach routines 278401c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_ring_reinit); // ring init on error 278501c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_buffer_lut); 278601c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_total_buffers); // index check 278701c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_buffer_base); 278801c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_reset); // ring init routines 278901c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_buf_size); 279001c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_rx_irq); // default irq handler 279101c7d25fSLuigi Rizzo EXPORT_SYMBOL(netmap_no_pendintr); // XXX mitigation - should go away 2792f18be576SLuigi Rizzo EXPORT_SYMBOL(netmap_bdg_ctl); // bridge configuration routine 2793f18be576SLuigi Rizzo EXPORT_SYMBOL(netmap_bdg_learning); // the default lookup function 279401c7d25fSLuigi Rizzo 279501c7d25fSLuigi Rizzo 279601c7d25fSLuigi Rizzo MODULE_AUTHOR("http://info.iet.unipi.it/~luigi/netmap/"); 279701c7d25fSLuigi Rizzo MODULE_DESCRIPTION("The netmap packet I/O framework"); 279801c7d25fSLuigi Rizzo MODULE_LICENSE("Dual BSD/GPL"); /* the code here is all BSD. */ 279901c7d25fSLuigi Rizzo 280001c7d25fSLuigi Rizzo #else /* __FreeBSD__ */ 280101c7d25fSLuigi Rizzo 2802f18be576SLuigi Rizzo 2803babc7c12SLuigi Rizzo static struct cdevsw netmap_cdevsw = { 2804babc7c12SLuigi Rizzo .d_version = D_VERSION, 2805babc7c12SLuigi Rizzo .d_name = "netmap", 28068241616dSLuigi Rizzo .d_open = netmap_open, 2807babc7c12SLuigi Rizzo .d_mmap = netmap_mmap, 28088241616dSLuigi Rizzo .d_mmap_single = netmap_mmap_single, 2809babc7c12SLuigi Rizzo .d_ioctl = netmap_ioctl, 2810babc7c12SLuigi Rizzo .d_poll = netmap_poll, 28118241616dSLuigi Rizzo .d_close = netmap_close, 2812babc7c12SLuigi Rizzo }; 281301c7d25fSLuigi Rizzo #endif /* __FreeBSD__ */ 2814babc7c12SLuigi Rizzo 2815f196ce38SLuigi Rizzo #ifdef NM_BRIDGE 2816f196ce38SLuigi Rizzo /* 2817f196ce38SLuigi Rizzo *---- support for virtual bridge ----- 2818f196ce38SLuigi Rizzo */ 2819f196ce38SLuigi Rizzo 2820f196ce38SLuigi Rizzo /* ----- FreeBSD if_bridge hash function ------- */ 2821f196ce38SLuigi Rizzo 2822f196ce38SLuigi Rizzo /* 2823f196ce38SLuigi Rizzo * The following hash function is adapted from "Hash Functions" by Bob Jenkins 2824f196ce38SLuigi Rizzo * ("Algorithm Alley", Dr. Dobbs Journal, September 1997). 2825f196ce38SLuigi Rizzo * 2826f196ce38SLuigi Rizzo * http://www.burtleburtle.net/bob/hash/spooky.html 2827f196ce38SLuigi Rizzo */ 2828f196ce38SLuigi Rizzo #define mix(a, b, c) \ 2829f196ce38SLuigi Rizzo do { \ 2830f196ce38SLuigi Rizzo a -= b; a -= c; a ^= (c >> 13); \ 2831f196ce38SLuigi Rizzo b -= c; b -= a; b ^= (a << 8); \ 2832f196ce38SLuigi Rizzo c -= a; c -= b; c ^= (b >> 13); \ 2833f196ce38SLuigi Rizzo a -= b; a -= c; a ^= (c >> 12); \ 2834f196ce38SLuigi Rizzo b -= c; b -= a; b ^= (a << 16); \ 2835f196ce38SLuigi Rizzo c -= a; c -= b; c ^= (b >> 5); \ 2836f196ce38SLuigi Rizzo a -= b; a -= c; a ^= (c >> 3); \ 2837f196ce38SLuigi Rizzo b -= c; b -= a; b ^= (a << 10); \ 2838f196ce38SLuigi Rizzo c -= a; c -= b; c ^= (b >> 15); \ 2839f196ce38SLuigi Rizzo } while (/*CONSTCOND*/0) 2840f196ce38SLuigi Rizzo 2841f196ce38SLuigi Rizzo static __inline uint32_t 2842f196ce38SLuigi Rizzo nm_bridge_rthash(const uint8_t *addr) 2843f196ce38SLuigi Rizzo { 2844f196ce38SLuigi Rizzo uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = 0; // hask key 2845f196ce38SLuigi Rizzo 2846f196ce38SLuigi Rizzo b += addr[5] << 8; 2847f196ce38SLuigi Rizzo b += addr[4]; 2848f196ce38SLuigi Rizzo a += addr[3] << 24; 2849f196ce38SLuigi Rizzo a += addr[2] << 16; 2850f196ce38SLuigi Rizzo a += addr[1] << 8; 2851f196ce38SLuigi Rizzo a += addr[0]; 2852f196ce38SLuigi Rizzo 2853f196ce38SLuigi Rizzo mix(a, b, c); 2854f196ce38SLuigi Rizzo #define BRIDGE_RTHASH_MASK (NM_BDG_HASH-1) 2855f196ce38SLuigi Rizzo return (c & BRIDGE_RTHASH_MASK); 2856f196ce38SLuigi Rizzo } 2857f196ce38SLuigi Rizzo 2858f196ce38SLuigi Rizzo #undef mix 2859f196ce38SLuigi Rizzo 2860f196ce38SLuigi Rizzo 2861f196ce38SLuigi Rizzo static int 2862f196ce38SLuigi Rizzo bdg_netmap_reg(struct ifnet *ifp, int onoff) 2863f196ce38SLuigi Rizzo { 2864f18be576SLuigi Rizzo // struct nm_bridge *b = NA(ifp)->na_bdg; 2865f196ce38SLuigi Rizzo 2866f18be576SLuigi Rizzo /* the interface is already attached to the bridge, 2867f18be576SLuigi Rizzo * so we only need to toggle IFCAP_NETMAP. 2868f18be576SLuigi Rizzo * Locking is not necessary (we are already under 2869f18be576SLuigi Rizzo * NMA_LOCK, and the port is not in use during this call). 2870f196ce38SLuigi Rizzo */ 2871f18be576SLuigi Rizzo /* BDG_WLOCK(b); */ 2872f18be576SLuigi Rizzo if (onoff) { 2873f196ce38SLuigi Rizzo ifp->if_capenable |= IFCAP_NETMAP; 2874f196ce38SLuigi Rizzo } else { 2875f196ce38SLuigi Rizzo ifp->if_capenable &= ~IFCAP_NETMAP; 2876f196ce38SLuigi Rizzo } 2877f18be576SLuigi Rizzo /* BDG_WUNLOCK(b); */ 2878f18be576SLuigi Rizzo return 0; 2879f196ce38SLuigi Rizzo } 2880f196ce38SLuigi Rizzo 2881f196ce38SLuigi Rizzo 2882f18be576SLuigi Rizzo /* 2883f18be576SLuigi Rizzo * Lookup function for a learning bridge. 2884f18be576SLuigi Rizzo * Update the hash table with the source address, 2885f18be576SLuigi Rizzo * and then returns the destination port index, and the 2886f18be576SLuigi Rizzo * ring in *dst_ring (at the moment, always use ring 0) 2887f18be576SLuigi Rizzo */ 2888f18be576SLuigi Rizzo u_int 2889f18be576SLuigi Rizzo netmap_bdg_learning(char *buf, u_int len, uint8_t *dst_ring, 2890f18be576SLuigi Rizzo struct netmap_adapter *na) 2891f196ce38SLuigi Rizzo { 2892f18be576SLuigi Rizzo struct nm_hash_ent *ht = na->na_bdg->ht; 2893f196ce38SLuigi Rizzo uint32_t sh, dh; 2894f18be576SLuigi Rizzo u_int dst, mysrc = na->bdg_port; 2895f196ce38SLuigi Rizzo uint64_t smac, dmac; 2896f196ce38SLuigi Rizzo 2897f196ce38SLuigi Rizzo dmac = le64toh(*(uint64_t *)(buf)) & 0xffffffffffff; 2898f196ce38SLuigi Rizzo smac = le64toh(*(uint64_t *)(buf + 4)); 2899f196ce38SLuigi Rizzo smac >>= 16; 2900f18be576SLuigi Rizzo 2901f196ce38SLuigi Rizzo /* 2902f196ce38SLuigi Rizzo * The hash is somewhat expensive, there might be some 2903f196ce38SLuigi Rizzo * worthwhile optimizations here. 2904f196ce38SLuigi Rizzo */ 2905f196ce38SLuigi Rizzo if ((buf[6] & 1) == 0) { /* valid src */ 2906f196ce38SLuigi Rizzo uint8_t *s = buf+6; 2907f196ce38SLuigi Rizzo sh = nm_bridge_rthash(buf+6); // XXX hash of source 2908f196ce38SLuigi Rizzo /* update source port forwarding entry */ 2909f18be576SLuigi Rizzo ht[sh].mac = smac; /* XXX expire ? */ 2910f18be576SLuigi Rizzo ht[sh].ports = mysrc; 2911f196ce38SLuigi Rizzo if (netmap_verbose) 2912f196ce38SLuigi Rizzo D("src %02x:%02x:%02x:%02x:%02x:%02x on port %d", 2913f18be576SLuigi Rizzo s[0], s[1], s[2], s[3], s[4], s[5], mysrc); 2914f196ce38SLuigi Rizzo } 2915f18be576SLuigi Rizzo dst = NM_BDG_BROADCAST; 2916f196ce38SLuigi Rizzo if ((buf[0] & 1) == 0) { /* unicast */ 2917f196ce38SLuigi Rizzo dh = nm_bridge_rthash(buf); // XXX hash of dst 2918f18be576SLuigi Rizzo if (ht[dh].mac == dmac) { /* found dst */ 2919f18be576SLuigi Rizzo dst = ht[dh].ports; 2920f196ce38SLuigi Rizzo } 2921f18be576SLuigi Rizzo /* XXX otherwise return NM_BDG_UNKNOWN ? */ 2922f196ce38SLuigi Rizzo } 2923f18be576SLuigi Rizzo *dst_ring = 0; 2924f18be576SLuigi Rizzo return dst; 2925f196ce38SLuigi Rizzo } 2926f196ce38SLuigi Rizzo 2927f18be576SLuigi Rizzo 2928f18be576SLuigi Rizzo /* 2929f18be576SLuigi Rizzo * This flush routine supports only unicast and broadcast but a large 2930f18be576SLuigi Rizzo * number of ports, and lets us replace the learn and dispatch functions. 2931f18be576SLuigi Rizzo */ 2932f18be576SLuigi Rizzo int 2933f18be576SLuigi Rizzo nm_bdg_flush(struct nm_bdg_fwd *ft, int n, struct netmap_adapter *na, 2934f18be576SLuigi Rizzo u_int ring_nr) 2935f18be576SLuigi Rizzo { 2936f18be576SLuigi Rizzo struct nm_bdg_q *dst_ents, *brddst; 2937f18be576SLuigi Rizzo uint16_t num_dsts = 0, *dsts; 2938f18be576SLuigi Rizzo struct nm_bridge *b = na->na_bdg; 2939f18be576SLuigi Rizzo u_int i, me = na->bdg_port; 2940f18be576SLuigi Rizzo 2941f18be576SLuigi Rizzo dst_ents = (struct nm_bdg_q *)(ft + NM_BDG_BATCH); 2942f18be576SLuigi Rizzo dsts = (uint16_t *)(dst_ents + NM_BDG_MAXPORTS * NM_BDG_MAXRINGS + 1); 2943f18be576SLuigi Rizzo 2944f18be576SLuigi Rizzo BDG_RLOCK(b); 2945f18be576SLuigi Rizzo 2946f18be576SLuigi Rizzo /* first pass: find a destination */ 2947f18be576SLuigi Rizzo for (i = 0; likely(i < n); i++) { 2948*85233a7dSLuigi Rizzo uint8_t *buf = ft[i].ft_buf; 2949f18be576SLuigi Rizzo uint8_t dst_ring = ring_nr; 2950f18be576SLuigi Rizzo uint16_t dst_port, d_i; 2951f18be576SLuigi Rizzo struct nm_bdg_q *d; 2952f18be576SLuigi Rizzo 2953f18be576SLuigi Rizzo dst_port = b->nm_bdg_lookup(buf, ft[i].ft_len, &dst_ring, na); 2954f18be576SLuigi Rizzo if (dst_port == NM_BDG_NOPORT) { 2955f18be576SLuigi Rizzo continue; /* this packet is identified to be dropped */ 2956f18be576SLuigi Rizzo } else if (unlikely(dst_port > NM_BDG_MAXPORTS)) { 2957f18be576SLuigi Rizzo continue; 2958f18be576SLuigi Rizzo } else if (dst_port == NM_BDG_BROADCAST) { 2959f18be576SLuigi Rizzo dst_ring = 0; /* broadcasts always go to ring 0 */ 2960f18be576SLuigi Rizzo } else if (unlikely(dst_port == me || 2961f18be576SLuigi Rizzo !BDG_GET_VAR(b->bdg_ports[dst_port]))) { 2962f18be576SLuigi Rizzo continue; 2963f18be576SLuigi Rizzo } 2964f18be576SLuigi Rizzo 2965f18be576SLuigi Rizzo /* get a position in the scratch pad */ 2966f18be576SLuigi Rizzo d_i = dst_port * NM_BDG_MAXRINGS + dst_ring; 2967f18be576SLuigi Rizzo d = dst_ents + d_i; 2968f18be576SLuigi Rizzo if (d->bq_head == NM_BDG_BATCH) { /* new destination */ 2969f18be576SLuigi Rizzo d->bq_head = d->bq_tail = i; 2970f18be576SLuigi Rizzo /* remember this position to be scanned later */ 2971f18be576SLuigi Rizzo if (dst_port != NM_BDG_BROADCAST) 2972f18be576SLuigi Rizzo dsts[num_dsts++] = d_i; 2973*85233a7dSLuigi Rizzo } else { 2974f18be576SLuigi Rizzo ft[d->bq_tail].ft_next = i; 2975f18be576SLuigi Rizzo d->bq_tail = i; 2976f18be576SLuigi Rizzo } 2977*85233a7dSLuigi Rizzo } 2978f18be576SLuigi Rizzo 2979f18be576SLuigi Rizzo /* if there is a broadcast, set ring 0 of all ports to be scanned 2980f18be576SLuigi Rizzo * XXX This would be optimized by recording the highest index of active 2981f18be576SLuigi Rizzo * ports. 2982f18be576SLuigi Rizzo */ 2983f18be576SLuigi Rizzo brddst = dst_ents + NM_BDG_BROADCAST * NM_BDG_MAXRINGS; 2984f18be576SLuigi Rizzo if (brddst->bq_head != NM_BDG_BATCH) { 2985f18be576SLuigi Rizzo for (i = 0; likely(i < NM_BDG_MAXPORTS); i++) { 2986f18be576SLuigi Rizzo uint16_t d_i = i * NM_BDG_MAXRINGS; 2987f18be576SLuigi Rizzo if (unlikely(i == me) || !BDG_GET_VAR(b->bdg_ports[i])) 2988f18be576SLuigi Rizzo continue; 2989f18be576SLuigi Rizzo else if (dst_ents[d_i].bq_head == NM_BDG_BATCH) 2990f18be576SLuigi Rizzo dsts[num_dsts++] = d_i; 2991f18be576SLuigi Rizzo } 2992f18be576SLuigi Rizzo } 2993f18be576SLuigi Rizzo 2994f18be576SLuigi Rizzo /* second pass: scan destinations (XXX will be modular somehow) */ 2995f18be576SLuigi Rizzo for (i = 0; i < num_dsts; i++) { 2996f18be576SLuigi Rizzo struct ifnet *dst_ifp; 2997f18be576SLuigi Rizzo struct netmap_adapter *dst_na; 2998f196ce38SLuigi Rizzo struct netmap_kring *kring; 2999f196ce38SLuigi Rizzo struct netmap_ring *ring; 3000f18be576SLuigi Rizzo u_int dst_nr, is_vp, lim, j, sent = 0, d_i, next, brd_next; 3001f18be576SLuigi Rizzo int howmany, retry = netmap_txsync_retry; 3002f18be576SLuigi Rizzo struct nm_bdg_q *d; 3003f196ce38SLuigi Rizzo 3004f18be576SLuigi Rizzo d_i = dsts[i]; 3005f18be576SLuigi Rizzo d = dst_ents + d_i; 3006f18be576SLuigi Rizzo dst_na = BDG_GET_VAR(b->bdg_ports[d_i/NM_BDG_MAXRINGS]); 3007f18be576SLuigi Rizzo /* protect from the lookup function returning an inactive 3008f18be576SLuigi Rizzo * destination port 3009f18be576SLuigi Rizzo */ 3010f18be576SLuigi Rizzo if (unlikely(dst_na == NULL)) 3011f196ce38SLuigi Rizzo continue; 3012f18be576SLuigi Rizzo else if (dst_na->na_flags & NAF_SW_ONLY) 3013f196ce38SLuigi Rizzo continue; 3014f18be576SLuigi Rizzo dst_ifp = dst_na->ifp; 3015f18be576SLuigi Rizzo /* 3016f18be576SLuigi Rizzo * The interface may be in !netmap mode in two cases: 3017f18be576SLuigi Rizzo * - when na is attached but not activated yet; 3018f18be576SLuigi Rizzo * - when na is being deactivated but is still attached. 3019f18be576SLuigi Rizzo */ 3020f18be576SLuigi Rizzo if (unlikely(!(dst_ifp->if_capenable & IFCAP_NETMAP))) 3021f18be576SLuigi Rizzo continue; 3022f196ce38SLuigi Rizzo 3023f18be576SLuigi Rizzo /* there is at least one either unicast or broadcast packet */ 3024f18be576SLuigi Rizzo brd_next = brddst->bq_head; 3025f18be576SLuigi Rizzo next = d->bq_head; 3026f18be576SLuigi Rizzo 3027f18be576SLuigi Rizzo is_vp = nma_is_vp(dst_na); 3028f18be576SLuigi Rizzo dst_nr = d_i & (NM_BDG_MAXRINGS-1); 3029f18be576SLuigi Rizzo if (is_vp) { /* virtual port */ 3030f18be576SLuigi Rizzo if (dst_nr >= dst_na->num_rx_rings) 3031f18be576SLuigi Rizzo dst_nr = dst_nr % dst_na->num_rx_rings; 3032f18be576SLuigi Rizzo kring = &dst_na->rx_rings[dst_nr]; 3033f196ce38SLuigi Rizzo ring = kring->ring; 3034f196ce38SLuigi Rizzo lim = kring->nkr_num_slots - 1; 3035f18be576SLuigi Rizzo dst_na->nm_lock(dst_ifp, NETMAP_RX_LOCK, dst_nr); 3036f196ce38SLuigi Rizzo j = kring->nr_hwcur + kring->nr_hwavail; 3037f196ce38SLuigi Rizzo if (j > lim) 3038f196ce38SLuigi Rizzo j -= kring->nkr_num_slots; 3039f18be576SLuigi Rizzo howmany = lim - kring->nr_hwavail; 3040f18be576SLuigi Rizzo } else { /* hw or sw adapter */ 3041f18be576SLuigi Rizzo if (dst_nr >= dst_na->num_tx_rings) 3042f18be576SLuigi Rizzo dst_nr = dst_nr % dst_na->num_tx_rings; 3043f18be576SLuigi Rizzo kring = &dst_na->tx_rings[dst_nr]; 3044f18be576SLuigi Rizzo ring = kring->ring; 3045f18be576SLuigi Rizzo lim = kring->nkr_num_slots - 1; 3046f18be576SLuigi Rizzo dst_na->nm_lock(dst_ifp, NETMAP_TX_LOCK, dst_nr); 3047f18be576SLuigi Rizzo retry: 3048f18be576SLuigi Rizzo dst_na->nm_txsync(dst_ifp, dst_nr, 0); 3049f18be576SLuigi Rizzo /* see nm_bdg_flush() */ 3050f18be576SLuigi Rizzo j = kring->nr_hwcur; 3051f18be576SLuigi Rizzo howmany = kring->nr_hwavail; 3052f18be576SLuigi Rizzo } 3053f18be576SLuigi Rizzo while (howmany-- > 0) { 3054f18be576SLuigi Rizzo struct netmap_slot *slot; 3055f18be576SLuigi Rizzo struct nm_bdg_fwd *ft_p; 3056f18be576SLuigi Rizzo 3057*85233a7dSLuigi Rizzo /* our 'NULL' is always higher than valid indexes 3058*85233a7dSLuigi Rizzo * so we never dereference it if the other list 3059*85233a7dSLuigi Rizzo * has packets (and if both are NULL we never 3060*85233a7dSLuigi Rizzo * get here). 3061*85233a7dSLuigi Rizzo */ 3062f18be576SLuigi Rizzo if (next < brd_next) { 3063f18be576SLuigi Rizzo ft_p = ft + next; 3064f18be576SLuigi Rizzo next = ft_p->ft_next; 3065*85233a7dSLuigi Rizzo ND("j %d uni %d next %d %d", 3066*85233a7dSLuigi Rizzo j, ft_p - ft, next, brd_next); 3067f18be576SLuigi Rizzo } else { /* insert broadcast */ 3068f18be576SLuigi Rizzo ft_p = ft + brd_next; 3069f18be576SLuigi Rizzo brd_next = ft_p->ft_next; 3070*85233a7dSLuigi Rizzo ND("j %d brd %d next %d %d", 3071*85233a7dSLuigi Rizzo j, ft_p - ft, next, brd_next); 3072f18be576SLuigi Rizzo } 3073f196ce38SLuigi Rizzo slot = &ring->slot[j]; 3074f18be576SLuigi Rizzo ND("send %d %d bytes at %s:%d", i, ft_p->ft_len, dst_ifp->if_xname, j); 3075*85233a7dSLuigi Rizzo if (ft_p->ft_flags & NS_INDIRECT) { 3076*85233a7dSLuigi Rizzo ND("copying from INDIRECT source"); 3077*85233a7dSLuigi Rizzo copyin(ft_p->ft_buf, NMB(slot), 3078*85233a7dSLuigi Rizzo (ft_p->ft_len + 63) & ~63); 3079*85233a7dSLuigi Rizzo } else { 3080*85233a7dSLuigi Rizzo pkt_copy(ft_p->ft_buf, NMB(slot), ft_p->ft_len); 3081*85233a7dSLuigi Rizzo } 3082f18be576SLuigi Rizzo slot->len = ft_p->ft_len; 3083*85233a7dSLuigi Rizzo j = unlikely(j == lim) ? 0: j + 1; /* XXX to be macro-ed */ 3084f196ce38SLuigi Rizzo sent++; 3085*85233a7dSLuigi Rizzo /* are we done ? */ 3086*85233a7dSLuigi Rizzo if (next == NM_BDG_BATCH && brd_next == NM_BDG_BATCH) 3087f18be576SLuigi Rizzo break; 3088f196ce38SLuigi Rizzo } 3089f18be576SLuigi Rizzo if (netmap_verbose && (howmany < 0)) 3090f18be576SLuigi Rizzo D("rx ring full on %s", dst_ifp->if_xname); 3091f18be576SLuigi Rizzo if (is_vp) { 3092f18be576SLuigi Rizzo if (sent) { 3093f18be576SLuigi Rizzo kring->nr_hwavail += sent; 3094f196ce38SLuigi Rizzo selwakeuppri(&kring->si, PI_NET); 3095f196ce38SLuigi Rizzo } 3096f18be576SLuigi Rizzo dst_na->nm_lock(dst_ifp, NETMAP_RX_UNLOCK, dst_nr); 3097f18be576SLuigi Rizzo } else { 3098f18be576SLuigi Rizzo if (sent) { 3099f18be576SLuigi Rizzo ring->avail -= sent; 3100f18be576SLuigi Rizzo ring->cur = j; 3101f18be576SLuigi Rizzo dst_na->nm_txsync(dst_ifp, dst_nr, 0); 3102f196ce38SLuigi Rizzo } 3103f18be576SLuigi Rizzo /* retry to send more packets */ 3104f18be576SLuigi Rizzo if (nma_is_hw(dst_na) && howmany < 0 && retry--) 3105f18be576SLuigi Rizzo goto retry; 3106f18be576SLuigi Rizzo dst_na->nm_lock(dst_ifp, NETMAP_TX_UNLOCK, dst_nr); 3107f18be576SLuigi Rizzo } 3108*85233a7dSLuigi Rizzo /* NM_BDG_BATCH means 'no packet' */ 3109f18be576SLuigi Rizzo d->bq_head = d->bq_tail = NM_BDG_BATCH; /* cleanup */ 3110f18be576SLuigi Rizzo } 3111f18be576SLuigi Rizzo brddst->bq_head = brddst->bq_tail = NM_BDG_BATCH; /* cleanup */ 3112f18be576SLuigi Rizzo BDG_RUNLOCK(b); 3113f196ce38SLuigi Rizzo return 0; 3114f196ce38SLuigi Rizzo } 3115f196ce38SLuigi Rizzo 3116f18be576SLuigi Rizzo 3117f196ce38SLuigi Rizzo /* 3118f196ce38SLuigi Rizzo * main dispatch routine 3119f196ce38SLuigi Rizzo */ 3120f196ce38SLuigi Rizzo static int 3121f196ce38SLuigi Rizzo bdg_netmap_txsync(struct ifnet *ifp, u_int ring_nr, int do_lock) 3122f196ce38SLuigi Rizzo { 3123f196ce38SLuigi Rizzo struct netmap_adapter *na = NA(ifp); 3124f196ce38SLuigi Rizzo struct netmap_kring *kring = &na->tx_rings[ring_nr]; 3125f196ce38SLuigi Rizzo struct netmap_ring *ring = kring->ring; 3126f196ce38SLuigi Rizzo int i, j, k, lim = kring->nkr_num_slots - 1; 3127f196ce38SLuigi Rizzo 3128f196ce38SLuigi Rizzo k = ring->cur; 3129f196ce38SLuigi Rizzo if (k > lim) 3130f196ce38SLuigi Rizzo return netmap_ring_reinit(kring); 3131f196ce38SLuigi Rizzo if (do_lock) 3132f196ce38SLuigi Rizzo na->nm_lock(ifp, NETMAP_TX_LOCK, ring_nr); 3133f196ce38SLuigi Rizzo 3134f196ce38SLuigi Rizzo if (netmap_bridge <= 0) { /* testing only */ 3135f196ce38SLuigi Rizzo j = k; // used all 3136f196ce38SLuigi Rizzo goto done; 3137f196ce38SLuigi Rizzo } 3138f196ce38SLuigi Rizzo if (netmap_bridge > NM_BDG_BATCH) 3139f196ce38SLuigi Rizzo netmap_bridge = NM_BDG_BATCH; 3140f196ce38SLuigi Rizzo 3141f18be576SLuigi Rizzo j = nm_bdg_preflush(na, ring_nr, kring, k); 3142f196ce38SLuigi Rizzo i = k - j; 3143f196ce38SLuigi Rizzo if (i < 0) 3144f196ce38SLuigi Rizzo i += kring->nkr_num_slots; 3145f196ce38SLuigi Rizzo kring->nr_hwavail = kring->nkr_num_slots - 1 - i; 3146f196ce38SLuigi Rizzo if (j != k) 3147f196ce38SLuigi Rizzo D("early break at %d/ %d, avail %d", j, k, kring->nr_hwavail); 3148f196ce38SLuigi Rizzo 3149f196ce38SLuigi Rizzo done: 3150f196ce38SLuigi Rizzo kring->nr_hwcur = j; 3151f196ce38SLuigi Rizzo ring->avail = kring->nr_hwavail; 3152f196ce38SLuigi Rizzo if (do_lock) 3153f196ce38SLuigi Rizzo na->nm_lock(ifp, NETMAP_TX_UNLOCK, ring_nr); 3154f196ce38SLuigi Rizzo 3155f196ce38SLuigi Rizzo if (netmap_verbose) 3156f196ce38SLuigi Rizzo D("%s ring %d lock %d", ifp->if_xname, ring_nr, do_lock); 3157f196ce38SLuigi Rizzo return 0; 3158f196ce38SLuigi Rizzo } 3159f196ce38SLuigi Rizzo 3160f18be576SLuigi Rizzo 3161f196ce38SLuigi Rizzo static int 3162f196ce38SLuigi Rizzo bdg_netmap_rxsync(struct ifnet *ifp, u_int ring_nr, int do_lock) 3163f196ce38SLuigi Rizzo { 3164f196ce38SLuigi Rizzo struct netmap_adapter *na = NA(ifp); 3165f196ce38SLuigi Rizzo struct netmap_kring *kring = &na->rx_rings[ring_nr]; 3166f196ce38SLuigi Rizzo struct netmap_ring *ring = kring->ring; 3167f18be576SLuigi Rizzo u_int j, lim = kring->nkr_num_slots - 1; 3168f196ce38SLuigi Rizzo u_int k = ring->cur, resvd = ring->reserved; 3169f18be576SLuigi Rizzo int n; 3170f196ce38SLuigi Rizzo 3171f196ce38SLuigi Rizzo ND("%s ring %d lock %d avail %d", 3172f196ce38SLuigi Rizzo ifp->if_xname, ring_nr, do_lock, kring->nr_hwavail); 3173f196ce38SLuigi Rizzo 3174f196ce38SLuigi Rizzo if (k > lim) 3175f196ce38SLuigi Rizzo return netmap_ring_reinit(kring); 3176f196ce38SLuigi Rizzo if (do_lock) 3177f196ce38SLuigi Rizzo na->nm_lock(ifp, NETMAP_RX_LOCK, ring_nr); 3178f196ce38SLuigi Rizzo 3179f196ce38SLuigi Rizzo /* skip past packets that userspace has released */ 3180f196ce38SLuigi Rizzo j = kring->nr_hwcur; /* netmap ring index */ 3181f196ce38SLuigi Rizzo if (resvd > 0) { 3182f196ce38SLuigi Rizzo if (resvd + ring->avail >= lim + 1) { 3183f196ce38SLuigi Rizzo D("XXX invalid reserve/avail %d %d", resvd, ring->avail); 3184f196ce38SLuigi Rizzo ring->reserved = resvd = 0; // XXX panic... 3185f196ce38SLuigi Rizzo } 3186f196ce38SLuigi Rizzo k = (k >= resvd) ? k - resvd : k + lim + 1 - resvd; 3187f196ce38SLuigi Rizzo } 3188f196ce38SLuigi Rizzo 3189f196ce38SLuigi Rizzo if (j != k) { /* userspace has released some packets. */ 3190f196ce38SLuigi Rizzo n = k - j; 3191f196ce38SLuigi Rizzo if (n < 0) 3192f196ce38SLuigi Rizzo n += kring->nkr_num_slots; 3193f196ce38SLuigi Rizzo ND("userspace releases %d packets", n); 3194f196ce38SLuigi Rizzo for (n = 0; likely(j != k); n++) { 3195f196ce38SLuigi Rizzo struct netmap_slot *slot = &ring->slot[j]; 3196f196ce38SLuigi Rizzo void *addr = NMB(slot); 3197f196ce38SLuigi Rizzo 3198f196ce38SLuigi Rizzo if (addr == netmap_buffer_base) { /* bad buf */ 3199f196ce38SLuigi Rizzo if (do_lock) 3200f196ce38SLuigi Rizzo na->nm_lock(ifp, NETMAP_RX_UNLOCK, ring_nr); 3201f196ce38SLuigi Rizzo return netmap_ring_reinit(kring); 3202f196ce38SLuigi Rizzo } 3203f196ce38SLuigi Rizzo /* decrease refcount for buffer */ 3204f196ce38SLuigi Rizzo 3205f196ce38SLuigi Rizzo slot->flags &= ~NS_BUF_CHANGED; 3206f196ce38SLuigi Rizzo j = unlikely(j == lim) ? 0 : j + 1; 3207f196ce38SLuigi Rizzo } 3208f196ce38SLuigi Rizzo kring->nr_hwavail -= n; 3209f196ce38SLuigi Rizzo kring->nr_hwcur = k; 3210f196ce38SLuigi Rizzo } 3211f196ce38SLuigi Rizzo /* tell userspace that there are new packets */ 3212f196ce38SLuigi Rizzo ring->avail = kring->nr_hwavail - resvd; 3213f196ce38SLuigi Rizzo 3214f196ce38SLuigi Rizzo if (do_lock) 3215f196ce38SLuigi Rizzo na->nm_lock(ifp, NETMAP_RX_UNLOCK, ring_nr); 3216f196ce38SLuigi Rizzo return 0; 3217f196ce38SLuigi Rizzo } 3218f196ce38SLuigi Rizzo 3219f18be576SLuigi Rizzo 3220f196ce38SLuigi Rizzo static void 3221f18be576SLuigi Rizzo bdg_netmap_attach(struct netmap_adapter *arg) 3222f196ce38SLuigi Rizzo { 3223f196ce38SLuigi Rizzo struct netmap_adapter na; 3224f196ce38SLuigi Rizzo 3225f196ce38SLuigi Rizzo ND("attaching virtual bridge"); 3226f196ce38SLuigi Rizzo bzero(&na, sizeof(na)); 3227f196ce38SLuigi Rizzo 3228f18be576SLuigi Rizzo na.ifp = arg->ifp; 3229f196ce38SLuigi Rizzo na.separate_locks = 1; 3230f18be576SLuigi Rizzo na.num_tx_rings = arg->num_tx_rings; 3231f18be576SLuigi Rizzo na.num_rx_rings = arg->num_rx_rings; 3232f196ce38SLuigi Rizzo na.num_tx_desc = NM_BRIDGE_RINGSIZE; 3233f196ce38SLuigi Rizzo na.num_rx_desc = NM_BRIDGE_RINGSIZE; 3234f196ce38SLuigi Rizzo na.nm_txsync = bdg_netmap_txsync; 3235f196ce38SLuigi Rizzo na.nm_rxsync = bdg_netmap_rxsync; 3236f196ce38SLuigi Rizzo na.nm_register = bdg_netmap_reg; 3237f18be576SLuigi Rizzo netmap_attach(&na, na.num_tx_rings); 3238f196ce38SLuigi Rizzo } 3239f196ce38SLuigi Rizzo 3240f196ce38SLuigi Rizzo #endif /* NM_BRIDGE */ 3241babc7c12SLuigi Rizzo 3242babc7c12SLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */ 3243babc7c12SLuigi Rizzo 3244babc7c12SLuigi Rizzo 32451a26580eSLuigi Rizzo /* 324668b8534bSLuigi Rizzo * Module loader. 324768b8534bSLuigi Rizzo * 324868b8534bSLuigi Rizzo * Create the /dev/netmap device and initialize all global 324968b8534bSLuigi Rizzo * variables. 325068b8534bSLuigi Rizzo * 325168b8534bSLuigi Rizzo * Return 0 on success, errno on failure. 325268b8534bSLuigi Rizzo */ 325368b8534bSLuigi Rizzo static int 325468b8534bSLuigi Rizzo netmap_init(void) 325568b8534bSLuigi Rizzo { 325668b8534bSLuigi Rizzo int error; 325768b8534bSLuigi Rizzo 325868b8534bSLuigi Rizzo error = netmap_memory_init(); 325968b8534bSLuigi Rizzo if (error != 0) { 326042a3a5bdSLuigi Rizzo printf("netmap: unable to initialize the memory allocator.\n"); 326168b8534bSLuigi Rizzo return (error); 326268b8534bSLuigi Rizzo } 32638241616dSLuigi Rizzo printf("netmap: loaded module\n"); 326468b8534bSLuigi Rizzo netmap_dev = make_dev(&netmap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660, 326568b8534bSLuigi Rizzo "netmap"); 3266f196ce38SLuigi Rizzo 3267f196ce38SLuigi Rizzo #ifdef NM_BRIDGE 3268f196ce38SLuigi Rizzo { 3269f196ce38SLuigi Rizzo int i; 3270f18be576SLuigi Rizzo mtx_init(&netmap_bridge_mutex, "netmap_bridge_mutex", 3271f18be576SLuigi Rizzo MTX_NETWORK_LOCK, MTX_DEF); 3272f18be576SLuigi Rizzo bzero(nm_bridges, sizeof(struct nm_bridge) * NM_BRIDGES); /* safety */ 3273f196ce38SLuigi Rizzo for (i = 0; i < NM_BRIDGES; i++) 3274f18be576SLuigi Rizzo rw_init(&nm_bridges[i].bdg_lock, "bdg lock"); 3275f196ce38SLuigi Rizzo } 3276f196ce38SLuigi Rizzo #endif 3277babc7c12SLuigi Rizzo return (error); 327868b8534bSLuigi Rizzo } 327968b8534bSLuigi Rizzo 328068b8534bSLuigi Rizzo 328168b8534bSLuigi Rizzo /* 328268b8534bSLuigi Rizzo * Module unloader. 328368b8534bSLuigi Rizzo * 328468b8534bSLuigi Rizzo * Free all the memory, and destroy the ``/dev/netmap`` device. 328568b8534bSLuigi Rizzo */ 328668b8534bSLuigi Rizzo static void 328768b8534bSLuigi Rizzo netmap_fini(void) 328868b8534bSLuigi Rizzo { 328968b8534bSLuigi Rizzo destroy_dev(netmap_dev); 329068b8534bSLuigi Rizzo netmap_memory_fini(); 329168b8534bSLuigi Rizzo printf("netmap: unloaded module.\n"); 329268b8534bSLuigi Rizzo } 329368b8534bSLuigi Rizzo 329468b8534bSLuigi Rizzo 3295f196ce38SLuigi Rizzo #ifdef __FreeBSD__ 329668b8534bSLuigi Rizzo /* 329768b8534bSLuigi Rizzo * Kernel entry point. 329868b8534bSLuigi Rizzo * 329968b8534bSLuigi Rizzo * Initialize/finalize the module and return. 330068b8534bSLuigi Rizzo * 330168b8534bSLuigi Rizzo * Return 0 on success, errno on failure. 330268b8534bSLuigi Rizzo */ 330368b8534bSLuigi Rizzo static int 330468b8534bSLuigi Rizzo netmap_loader(__unused struct module *module, int event, __unused void *arg) 330568b8534bSLuigi Rizzo { 330668b8534bSLuigi Rizzo int error = 0; 330768b8534bSLuigi Rizzo 330868b8534bSLuigi Rizzo switch (event) { 330968b8534bSLuigi Rizzo case MOD_LOAD: 331068b8534bSLuigi Rizzo error = netmap_init(); 331168b8534bSLuigi Rizzo break; 331268b8534bSLuigi Rizzo 331368b8534bSLuigi Rizzo case MOD_UNLOAD: 331468b8534bSLuigi Rizzo netmap_fini(); 331568b8534bSLuigi Rizzo break; 331668b8534bSLuigi Rizzo 331768b8534bSLuigi Rizzo default: 331868b8534bSLuigi Rizzo error = EOPNOTSUPP; 331968b8534bSLuigi Rizzo break; 332068b8534bSLuigi Rizzo } 332168b8534bSLuigi Rizzo 332268b8534bSLuigi Rizzo return (error); 332368b8534bSLuigi Rizzo } 332468b8534bSLuigi Rizzo 332568b8534bSLuigi Rizzo 332668b8534bSLuigi Rizzo DEV_MODULE(netmap, netmap_loader, NULL); 3327f196ce38SLuigi Rizzo #endif /* __FreeBSD__ */ 3328