168b8534bSLuigi Rizzo /* 2*17885a7bSLuigi Rizzo * Copyright (C) 2011-2014 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 26ce3ee1e7SLuigi Rizzo 2768b8534bSLuigi Rizzo /* 28f9790aebSLuigi Rizzo * $FreeBSD$ 29f9790aebSLuigi Rizzo * 3068b8534bSLuigi Rizzo * This module supports memory mapped access to network devices, 3168b8534bSLuigi Rizzo * see netmap(4). 3268b8534bSLuigi Rizzo * 3368b8534bSLuigi Rizzo * The module uses a large, memory pool allocated by the kernel 3468b8534bSLuigi Rizzo * and accessible as mmapped memory by multiple userspace threads/processes. 3568b8534bSLuigi Rizzo * The memory pool contains packet buffers and "netmap rings", 3668b8534bSLuigi Rizzo * i.e. user-accessible copies of the interface's queues. 3768b8534bSLuigi Rizzo * 3868b8534bSLuigi Rizzo * Access to the network card works like this: 3968b8534bSLuigi Rizzo * 1. a process/thread issues one or more open() on /dev/netmap, to create 4068b8534bSLuigi Rizzo * select()able file descriptor on which events are reported. 4168b8534bSLuigi Rizzo * 2. on each descriptor, the process issues an ioctl() to identify 4268b8534bSLuigi Rizzo * the interface that should report events to the file descriptor. 4368b8534bSLuigi Rizzo * 3. on each descriptor, the process issues an mmap() request to 4468b8534bSLuigi Rizzo * map the shared memory region within the process' address space. 4568b8534bSLuigi Rizzo * The list of interesting queues is indicated by a location in 4668b8534bSLuigi Rizzo * the shared memory region. 4768b8534bSLuigi Rizzo * 4. using the functions in the netmap(4) userspace API, a process 4868b8534bSLuigi Rizzo * can look up the occupation state of a queue, access memory buffers, 4968b8534bSLuigi Rizzo * and retrieve received packets or enqueue packets to transmit. 5068b8534bSLuigi Rizzo * 5. using some ioctl()s the process can synchronize the userspace view 5168b8534bSLuigi Rizzo * of the queue with the actual status in the kernel. This includes both 5268b8534bSLuigi Rizzo * receiving the notification of new packets, and transmitting new 5368b8534bSLuigi Rizzo * packets on the output interface. 5468b8534bSLuigi Rizzo * 6. select() or poll() can be used to wait for events on individual 5568b8534bSLuigi Rizzo * transmit or receive queues (or all queues for a given interface). 56ce3ee1e7SLuigi Rizzo * 57ce3ee1e7SLuigi Rizzo 58ce3ee1e7SLuigi Rizzo SYNCHRONIZATION (USER) 59ce3ee1e7SLuigi Rizzo 60ce3ee1e7SLuigi Rizzo The netmap rings and data structures may be shared among multiple 61ce3ee1e7SLuigi Rizzo user threads or even independent processes. 62ce3ee1e7SLuigi Rizzo Any synchronization among those threads/processes is delegated 63ce3ee1e7SLuigi Rizzo to the threads themselves. Only one thread at a time can be in 64ce3ee1e7SLuigi Rizzo a system call on the same netmap ring. The OS does not enforce 65ce3ee1e7SLuigi Rizzo this and only guarantees against system crashes in case of 66ce3ee1e7SLuigi Rizzo invalid usage. 67ce3ee1e7SLuigi Rizzo 68ce3ee1e7SLuigi Rizzo LOCKING (INTERNAL) 69ce3ee1e7SLuigi Rizzo 70ce3ee1e7SLuigi Rizzo Within the kernel, access to the netmap rings is protected as follows: 71ce3ee1e7SLuigi Rizzo 72ce3ee1e7SLuigi Rizzo - a spinlock on each ring, to handle producer/consumer races on 73ce3ee1e7SLuigi Rizzo RX rings attached to the host stack (against multiple host 74ce3ee1e7SLuigi Rizzo threads writing from the host stack to the same ring), 75ce3ee1e7SLuigi Rizzo and on 'destination' rings attached to a VALE switch 76ce3ee1e7SLuigi Rizzo (i.e. RX rings in VALE ports, and TX rings in NIC/host ports) 77ce3ee1e7SLuigi Rizzo protecting multiple active senders for the same destination) 78ce3ee1e7SLuigi Rizzo 79ce3ee1e7SLuigi Rizzo - an atomic variable to guarantee that there is at most one 80ce3ee1e7SLuigi Rizzo instance of *_*xsync() on the ring at any time. 81ce3ee1e7SLuigi Rizzo For rings connected to user file 82ce3ee1e7SLuigi Rizzo descriptors, an atomic_test_and_set() protects this, and the 83ce3ee1e7SLuigi Rizzo lock on the ring is not actually used. 84ce3ee1e7SLuigi Rizzo For NIC RX rings connected to a VALE switch, an atomic_test_and_set() 85ce3ee1e7SLuigi Rizzo is also used to prevent multiple executions (the driver might indeed 86ce3ee1e7SLuigi Rizzo already guarantee this). 87ce3ee1e7SLuigi Rizzo For NIC TX rings connected to a VALE switch, the lock arbitrates 88ce3ee1e7SLuigi Rizzo access to the queue (both when allocating buffers and when pushing 89ce3ee1e7SLuigi Rizzo them out). 90ce3ee1e7SLuigi Rizzo 91ce3ee1e7SLuigi Rizzo - *xsync() should be protected against initializations of the card. 92ce3ee1e7SLuigi Rizzo On FreeBSD most devices have the reset routine protected by 93ce3ee1e7SLuigi Rizzo a RING lock (ixgbe, igb, em) or core lock (re). lem is missing 94ce3ee1e7SLuigi Rizzo the RING protection on rx_reset(), this should be added. 95ce3ee1e7SLuigi Rizzo 96ce3ee1e7SLuigi Rizzo On linux there is an external lock on the tx path, which probably 97ce3ee1e7SLuigi Rizzo also arbitrates access to the reset routine. XXX to be revised 98ce3ee1e7SLuigi Rizzo 99ce3ee1e7SLuigi Rizzo - a per-interface core_lock protecting access from the host stack 100ce3ee1e7SLuigi Rizzo while interfaces may be detached from netmap mode. 101ce3ee1e7SLuigi Rizzo XXX there should be no need for this lock if we detach the interfaces 102ce3ee1e7SLuigi Rizzo only while they are down. 103ce3ee1e7SLuigi Rizzo 104ce3ee1e7SLuigi Rizzo 105ce3ee1e7SLuigi Rizzo --- VALE SWITCH --- 106ce3ee1e7SLuigi Rizzo 107ce3ee1e7SLuigi Rizzo NMG_LOCK() serializes all modifications to switches and ports. 108ce3ee1e7SLuigi Rizzo A switch cannot be deleted until all ports are gone. 109ce3ee1e7SLuigi Rizzo 110ce3ee1e7SLuigi Rizzo For each switch, an SX lock (RWlock on linux) protects 111ce3ee1e7SLuigi Rizzo deletion of ports. When configuring or deleting a new port, the 112ce3ee1e7SLuigi Rizzo lock is acquired in exclusive mode (after holding NMG_LOCK). 113ce3ee1e7SLuigi Rizzo When forwarding, the lock is acquired in shared mode (without NMG_LOCK). 114ce3ee1e7SLuigi Rizzo The lock is held throughout the entire forwarding cycle, 115ce3ee1e7SLuigi Rizzo during which the thread may incur in a page fault. 116ce3ee1e7SLuigi Rizzo Hence it is important that sleepable shared locks are used. 117ce3ee1e7SLuigi Rizzo 118ce3ee1e7SLuigi Rizzo On the rx ring, the per-port lock is grabbed initially to reserve 119ce3ee1e7SLuigi Rizzo a number of slot in the ring, then the lock is released, 120ce3ee1e7SLuigi Rizzo packets are copied from source to destination, and then 121ce3ee1e7SLuigi Rizzo the lock is acquired again and the receive ring is updated. 122ce3ee1e7SLuigi Rizzo (A similar thing is done on the tx ring for NIC and host stack 123ce3ee1e7SLuigi Rizzo ports attached to the switch) 124ce3ee1e7SLuigi Rizzo 12568b8534bSLuigi Rizzo */ 12668b8534bSLuigi Rizzo 127ce3ee1e7SLuigi Rizzo /* 128ce3ee1e7SLuigi Rizzo * OS-specific code that is used only within this file. 129ce3ee1e7SLuigi Rizzo * Other OS-specific code that must be accessed by drivers 130ce3ee1e7SLuigi Rizzo * is present in netmap_kern.h 131ce3ee1e7SLuigi Rizzo */ 13201c7d25fSLuigi Rizzo 133ce3ee1e7SLuigi Rizzo #if defined(__FreeBSD__) 13468b8534bSLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */ 13568b8534bSLuigi Rizzo #include <sys/types.h> 13668b8534bSLuigi Rizzo #include <sys/errno.h> 13768b8534bSLuigi Rizzo #include <sys/param.h> /* defines used in kernel.h */ 13868b8534bSLuigi Rizzo #include <sys/kernel.h> /* types used in module initialization */ 139f9790aebSLuigi Rizzo #include <sys/conf.h> /* cdevsw struct, UID, GID */ 14068b8534bSLuigi Rizzo #include <sys/sockio.h> 14168b8534bSLuigi Rizzo #include <sys/socketvar.h> /* struct socket */ 14268b8534bSLuigi Rizzo #include <sys/malloc.h> 14368b8534bSLuigi Rizzo #include <sys/poll.h> 14489f6b863SAttilio Rao #include <sys/rwlock.h> 14568b8534bSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */ 14668b8534bSLuigi Rizzo #include <sys/selinfo.h> 14768b8534bSLuigi Rizzo #include <sys/sysctl.h> 14868b8534bSLuigi Rizzo #include <net/if.h> 14976039bc8SGleb Smirnoff #include <net/if_var.h> 15068b8534bSLuigi Rizzo #include <net/bpf.h> /* BIOCIMMEDIATE */ 15168b8534bSLuigi Rizzo #include <machine/bus.h> /* bus_dmamap_* */ 152ce3ee1e7SLuigi Rizzo #include <sys/endian.h> 153ce3ee1e7SLuigi Rizzo #include <sys/refcount.h> 15468b8534bSLuigi Rizzo 15568b8534bSLuigi Rizzo 156f9790aebSLuigi Rizzo /* reduce conditional code */ 157f9790aebSLuigi Rizzo #define init_waitqueue_head(x) // only needed in linux 158ce3ee1e7SLuigi Rizzo 159ce3ee1e7SLuigi Rizzo 160ce3ee1e7SLuigi Rizzo 161ce3ee1e7SLuigi Rizzo #elif defined(linux) 162ce3ee1e7SLuigi Rizzo 163ce3ee1e7SLuigi Rizzo #include "bsd_glue.h" 164ce3ee1e7SLuigi Rizzo 165ce3ee1e7SLuigi Rizzo 166ce3ee1e7SLuigi Rizzo 167ce3ee1e7SLuigi Rizzo #elif defined(__APPLE__) 168ce3ee1e7SLuigi Rizzo 169ce3ee1e7SLuigi Rizzo #warning OSX support is only partial 170ce3ee1e7SLuigi Rizzo #include "osx_glue.h" 171ce3ee1e7SLuigi Rizzo 172ce3ee1e7SLuigi Rizzo #else 173ce3ee1e7SLuigi Rizzo 174ce3ee1e7SLuigi Rizzo #error Unsupported platform 175ce3ee1e7SLuigi Rizzo 176ce3ee1e7SLuigi Rizzo #endif /* unsupported */ 177ce3ee1e7SLuigi Rizzo 178ce3ee1e7SLuigi Rizzo /* 179ce3ee1e7SLuigi Rizzo * common headers 180ce3ee1e7SLuigi Rizzo */ 1810b8ed8e0SLuigi Rizzo #include <net/netmap.h> 1820b8ed8e0SLuigi Rizzo #include <dev/netmap/netmap_kern.h> 183ce3ee1e7SLuigi Rizzo #include <dev/netmap/netmap_mem2.h> 1840b8ed8e0SLuigi Rizzo 185ce3ee1e7SLuigi Rizzo 186ce3ee1e7SLuigi Rizzo MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map"); 187ce3ee1e7SLuigi Rizzo 188ce3ee1e7SLuigi Rizzo /* 189ce3ee1e7SLuigi Rizzo * The following variables are used by the drivers and replicate 190ce3ee1e7SLuigi Rizzo * fields in the global memory pool. They only refer to buffers 191ce3ee1e7SLuigi Rizzo * used by physical interfaces. 192ce3ee1e7SLuigi Rizzo */ 1935819da83SLuigi Rizzo u_int netmap_total_buffers; 1948241616dSLuigi Rizzo u_int netmap_buf_size; 195ce3ee1e7SLuigi Rizzo char *netmap_buffer_base; /* also address of an invalid buffer */ 1965819da83SLuigi Rizzo 1975819da83SLuigi Rizzo /* user-controlled variables */ 1985819da83SLuigi Rizzo int netmap_verbose; 1995819da83SLuigi Rizzo 2005819da83SLuigi Rizzo static int netmap_no_timestamp; /* don't timestamp on rxsync */ 2015819da83SLuigi Rizzo 2025819da83SLuigi Rizzo SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args"); 2035819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose, 2045819da83SLuigi Rizzo CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode"); 2055819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp, 2065819da83SLuigi Rizzo CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp"); 2075819da83SLuigi Rizzo int netmap_mitigate = 1; 2085819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mitigate, CTLFLAG_RW, &netmap_mitigate, 0, ""); 209c85cb1a0SLuigi Rizzo int netmap_no_pendintr = 1; 2105819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr, 2115819da83SLuigi Rizzo CTLFLAG_RW, &netmap_no_pendintr, 0, "Always look for new received packets."); 212f18be576SLuigi Rizzo int netmap_txsync_retry = 2; 213f18be576SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, txsync_retry, CTLFLAG_RW, 214f18be576SLuigi Rizzo &netmap_txsync_retry, 0 , "Number of txsync loops in bridge's flush."); 2155819da83SLuigi Rizzo 216f196ce38SLuigi Rizzo int netmap_flags = 0; /* debug flags */ 217091fd0abSLuigi Rizzo int netmap_fwd = 0; /* force transparent mode */ 218ce3ee1e7SLuigi Rizzo int netmap_mmap_unreg = 0; /* allow mmap of unregistered fds */ 219f196ce38SLuigi Rizzo 220f9790aebSLuigi Rizzo /* 221f9790aebSLuigi Rizzo * netmap_admode selects the netmap mode to use. 222f9790aebSLuigi Rizzo * Invalid values are reset to NETMAP_ADMODE_BEST 223f9790aebSLuigi Rizzo */ 224f9790aebSLuigi Rizzo enum { NETMAP_ADMODE_BEST = 0, /* use native, fallback to generic */ 225f9790aebSLuigi Rizzo NETMAP_ADMODE_NATIVE, /* either native or none */ 226f9790aebSLuigi Rizzo NETMAP_ADMODE_GENERIC, /* force generic */ 227f9790aebSLuigi Rizzo NETMAP_ADMODE_LAST }; 228f9790aebSLuigi Rizzo static int netmap_admode = NETMAP_ADMODE_BEST; 229f9790aebSLuigi Rizzo 230f9790aebSLuigi Rizzo int netmap_generic_mit = 100*1000; /* Generic mitigation interval in nanoseconds. */ 231f9790aebSLuigi Rizzo int netmap_generic_ringsize = 1024; /* Generic ringsize. */ 232f9790aebSLuigi Rizzo 233f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, flags, CTLFLAG_RW, &netmap_flags, 0 , ""); 234091fd0abSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, fwd, CTLFLAG_RW, &netmap_fwd, 0 , ""); 235ce3ee1e7SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mmap_unreg, CTLFLAG_RW, &netmap_mmap_unreg, 0, ""); 236f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, admode, CTLFLAG_RW, &netmap_admode, 0 , ""); 237f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_mit, CTLFLAG_RW, &netmap_generic_mit, 0 , ""); 238f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_ringsize, CTLFLAG_RW, &netmap_generic_ringsize, 0 , ""); 239f196ce38SLuigi Rizzo 240ce3ee1e7SLuigi Rizzo NMG_LOCK_T netmap_global_lock; 241ce3ee1e7SLuigi Rizzo 242ce3ee1e7SLuigi Rizzo 243f9790aebSLuigi Rizzo static void 244f9790aebSLuigi Rizzo nm_kr_get(struct netmap_kring *kr) 245ce3ee1e7SLuigi Rizzo { 246ce3ee1e7SLuigi Rizzo while (NM_ATOMIC_TEST_AND_SET(&kr->nr_busy)) 247ce3ee1e7SLuigi Rizzo tsleep(kr, 0, "NM_KR_GET", 4); 248ce3ee1e7SLuigi Rizzo } 249ce3ee1e7SLuigi Rizzo 250f9790aebSLuigi Rizzo 251*17885a7bSLuigi Rizzo /* 252*17885a7bSLuigi Rizzo * mark the ring as stopped, and run through the locks 253*17885a7bSLuigi Rizzo * to make sure other users get to see it. 254*17885a7bSLuigi Rizzo */ 255f9790aebSLuigi Rizzo void 256f9790aebSLuigi Rizzo netmap_disable_ring(struct netmap_kring *kr) 257ce3ee1e7SLuigi Rizzo { 258ce3ee1e7SLuigi Rizzo kr->nkr_stopped = 1; 259ce3ee1e7SLuigi Rizzo nm_kr_get(kr); 260ce3ee1e7SLuigi Rizzo mtx_lock(&kr->q_lock); 261ce3ee1e7SLuigi Rizzo mtx_unlock(&kr->q_lock); 262ce3ee1e7SLuigi Rizzo nm_kr_put(kr); 263ce3ee1e7SLuigi Rizzo } 264ce3ee1e7SLuigi Rizzo 265f9790aebSLuigi Rizzo 266f9790aebSLuigi Rizzo static void 267f9790aebSLuigi Rizzo netmap_set_all_rings(struct ifnet *ifp, int stopped) 268ce3ee1e7SLuigi Rizzo { 269ce3ee1e7SLuigi Rizzo struct netmap_adapter *na; 270ce3ee1e7SLuigi Rizzo int i; 271ce3ee1e7SLuigi Rizzo 272ce3ee1e7SLuigi Rizzo if (!(ifp->if_capenable & IFCAP_NETMAP)) 273ce3ee1e7SLuigi Rizzo return; 274ce3ee1e7SLuigi Rizzo 275ce3ee1e7SLuigi Rizzo na = NA(ifp); 276ce3ee1e7SLuigi Rizzo 277f9790aebSLuigi Rizzo for (i = 0; i <= na->num_tx_rings; i++) { 278f9790aebSLuigi Rizzo if (stopped) 279f9790aebSLuigi Rizzo netmap_disable_ring(na->tx_rings + i); 280f9790aebSLuigi Rizzo else 281ce3ee1e7SLuigi Rizzo na->tx_rings[i].nkr_stopped = 0; 282f9790aebSLuigi Rizzo na->nm_notify(na, i, NR_TX, NAF_DISABLE_NOTIFY | 283f9790aebSLuigi Rizzo (i == na->num_tx_rings ? NAF_GLOBAL_NOTIFY: 0)); 284ce3ee1e7SLuigi Rizzo } 285f9790aebSLuigi Rizzo 286f9790aebSLuigi Rizzo for (i = 0; i <= na->num_rx_rings; i++) { 287f9790aebSLuigi Rizzo if (stopped) 288f9790aebSLuigi Rizzo netmap_disable_ring(na->rx_rings + i); 289f9790aebSLuigi Rizzo else 290ce3ee1e7SLuigi Rizzo na->rx_rings[i].nkr_stopped = 0; 291f9790aebSLuigi Rizzo na->nm_notify(na, i, NR_RX, NAF_DISABLE_NOTIFY | 292f9790aebSLuigi Rizzo (i == na->num_rx_rings ? NAF_GLOBAL_NOTIFY: 0)); 293ce3ee1e7SLuigi Rizzo } 294ce3ee1e7SLuigi Rizzo } 295ce3ee1e7SLuigi Rizzo 296ce3ee1e7SLuigi Rizzo 297f9790aebSLuigi Rizzo void 298f9790aebSLuigi Rizzo netmap_disable_all_rings(struct ifnet *ifp) 299f9790aebSLuigi Rizzo { 300f9790aebSLuigi Rizzo netmap_set_all_rings(ifp, 1 /* stopped */); 301f9790aebSLuigi Rizzo } 302f9790aebSLuigi Rizzo 303f9790aebSLuigi Rizzo 304f9790aebSLuigi Rizzo void 305f9790aebSLuigi Rizzo netmap_enable_all_rings(struct ifnet *ifp) 306f9790aebSLuigi Rizzo { 307f9790aebSLuigi Rizzo netmap_set_all_rings(ifp, 0 /* enabled */); 308f9790aebSLuigi Rizzo } 309f9790aebSLuigi Rizzo 310f9790aebSLuigi Rizzo 311ce3ee1e7SLuigi Rizzo /* 312ce3ee1e7SLuigi Rizzo * generic bound_checking function 313ce3ee1e7SLuigi Rizzo */ 314ce3ee1e7SLuigi Rizzo u_int 315ce3ee1e7SLuigi Rizzo nm_bound_var(u_int *v, u_int dflt, u_int lo, u_int hi, const char *msg) 316ce3ee1e7SLuigi Rizzo { 317ce3ee1e7SLuigi Rizzo u_int oldv = *v; 318ce3ee1e7SLuigi Rizzo const char *op = NULL; 319ce3ee1e7SLuigi Rizzo 320ce3ee1e7SLuigi Rizzo if (dflt < lo) 321ce3ee1e7SLuigi Rizzo dflt = lo; 322ce3ee1e7SLuigi Rizzo if (dflt > hi) 323ce3ee1e7SLuigi Rizzo dflt = hi; 324ce3ee1e7SLuigi Rizzo if (oldv < lo) { 325ce3ee1e7SLuigi Rizzo *v = dflt; 326ce3ee1e7SLuigi Rizzo op = "Bump"; 327ce3ee1e7SLuigi Rizzo } else if (oldv > hi) { 328ce3ee1e7SLuigi Rizzo *v = hi; 329ce3ee1e7SLuigi Rizzo op = "Clamp"; 330ce3ee1e7SLuigi Rizzo } 331ce3ee1e7SLuigi Rizzo if (op && msg) 332ce3ee1e7SLuigi Rizzo printf("%s %s to %d (was %d)\n", op, msg, *v, oldv); 333ce3ee1e7SLuigi Rizzo return *v; 334ce3ee1e7SLuigi Rizzo } 335ce3ee1e7SLuigi Rizzo 336f9790aebSLuigi Rizzo 337ce3ee1e7SLuigi Rizzo /* 338ce3ee1e7SLuigi Rizzo * packet-dump function, user-supplied or static buffer. 339ce3ee1e7SLuigi Rizzo * The destination buffer must be at least 30+4*len 340ce3ee1e7SLuigi Rizzo */ 341ce3ee1e7SLuigi Rizzo const char * 342ce3ee1e7SLuigi Rizzo nm_dump_buf(char *p, int len, int lim, char *dst) 343ce3ee1e7SLuigi Rizzo { 344ce3ee1e7SLuigi Rizzo static char _dst[8192]; 345ce3ee1e7SLuigi Rizzo int i, j, i0; 346ce3ee1e7SLuigi Rizzo static char hex[] ="0123456789abcdef"; 347ce3ee1e7SLuigi Rizzo char *o; /* output position */ 348ce3ee1e7SLuigi Rizzo 349ce3ee1e7SLuigi Rizzo #define P_HI(x) hex[((x) & 0xf0)>>4] 350ce3ee1e7SLuigi Rizzo #define P_LO(x) hex[((x) & 0xf)] 351ce3ee1e7SLuigi Rizzo #define P_C(x) ((x) >= 0x20 && (x) <= 0x7e ? (x) : '.') 352ce3ee1e7SLuigi Rizzo if (!dst) 353ce3ee1e7SLuigi Rizzo dst = _dst; 354ce3ee1e7SLuigi Rizzo if (lim <= 0 || lim > len) 355ce3ee1e7SLuigi Rizzo lim = len; 356ce3ee1e7SLuigi Rizzo o = dst; 357ce3ee1e7SLuigi Rizzo sprintf(o, "buf 0x%p len %d lim %d\n", p, len, lim); 358ce3ee1e7SLuigi Rizzo o += strlen(o); 359ce3ee1e7SLuigi Rizzo /* hexdump routine */ 360ce3ee1e7SLuigi Rizzo for (i = 0; i < lim; ) { 361ce3ee1e7SLuigi Rizzo sprintf(o, "%5d: ", i); 362ce3ee1e7SLuigi Rizzo o += strlen(o); 363ce3ee1e7SLuigi Rizzo memset(o, ' ', 48); 364ce3ee1e7SLuigi Rizzo i0 = i; 365ce3ee1e7SLuigi Rizzo for (j=0; j < 16 && i < lim; i++, j++) { 366ce3ee1e7SLuigi Rizzo o[j*3] = P_HI(p[i]); 367ce3ee1e7SLuigi Rizzo o[j*3+1] = P_LO(p[i]); 368ce3ee1e7SLuigi Rizzo } 369ce3ee1e7SLuigi Rizzo i = i0; 370ce3ee1e7SLuigi Rizzo for (j=0; j < 16 && i < lim; i++, j++) 371ce3ee1e7SLuigi Rizzo o[j + 48] = P_C(p[i]); 372ce3ee1e7SLuigi Rizzo o[j+48] = '\n'; 373ce3ee1e7SLuigi Rizzo o += j+49; 374ce3ee1e7SLuigi Rizzo } 375ce3ee1e7SLuigi Rizzo *o = '\0'; 376ce3ee1e7SLuigi Rizzo #undef P_HI 377ce3ee1e7SLuigi Rizzo #undef P_LO 378ce3ee1e7SLuigi Rizzo #undef P_C 379ce3ee1e7SLuigi Rizzo return dst; 380ce3ee1e7SLuigi Rizzo } 381f196ce38SLuigi Rizzo 382f18be576SLuigi Rizzo 383ae10d1afSLuigi Rizzo /* 384ae10d1afSLuigi Rizzo * Fetch configuration from the device, to cope with dynamic 385ae10d1afSLuigi Rizzo * reconfigurations after loading the module. 386ae10d1afSLuigi Rizzo */ 387f9790aebSLuigi Rizzo int 388ae10d1afSLuigi Rizzo netmap_update_config(struct netmap_adapter *na) 389ae10d1afSLuigi Rizzo { 390ae10d1afSLuigi Rizzo struct ifnet *ifp = na->ifp; 391ae10d1afSLuigi Rizzo u_int txr, txd, rxr, rxd; 392ae10d1afSLuigi Rizzo 393ae10d1afSLuigi Rizzo txr = txd = rxr = rxd = 0; 394ae10d1afSLuigi Rizzo if (na->nm_config) { 395f9790aebSLuigi Rizzo na->nm_config(na, &txr, &txd, &rxr, &rxd); 396ae10d1afSLuigi Rizzo } else { 397ae10d1afSLuigi Rizzo /* take whatever we had at init time */ 398ae10d1afSLuigi Rizzo txr = na->num_tx_rings; 399ae10d1afSLuigi Rizzo txd = na->num_tx_desc; 400ae10d1afSLuigi Rizzo rxr = na->num_rx_rings; 401ae10d1afSLuigi Rizzo rxd = na->num_rx_desc; 402ae10d1afSLuigi Rizzo } 403ae10d1afSLuigi Rizzo 404ae10d1afSLuigi Rizzo if (na->num_tx_rings == txr && na->num_tx_desc == txd && 405ae10d1afSLuigi Rizzo na->num_rx_rings == rxr && na->num_rx_desc == rxd) 406ae10d1afSLuigi Rizzo return 0; /* nothing changed */ 407f9790aebSLuigi Rizzo if (netmap_verbose || na->active_fds > 0) { 408ae10d1afSLuigi Rizzo D("stored config %s: txring %d x %d, rxring %d x %d", 409f9790aebSLuigi Rizzo NM_IFPNAME(ifp), 410ae10d1afSLuigi Rizzo na->num_tx_rings, na->num_tx_desc, 411ae10d1afSLuigi Rizzo na->num_rx_rings, na->num_rx_desc); 412ae10d1afSLuigi Rizzo D("new config %s: txring %d x %d, rxring %d x %d", 413f9790aebSLuigi Rizzo NM_IFPNAME(ifp), txr, txd, rxr, rxd); 414ae10d1afSLuigi Rizzo } 415f9790aebSLuigi Rizzo if (na->active_fds == 0) { 416ae10d1afSLuigi Rizzo D("configuration changed (but fine)"); 417ae10d1afSLuigi Rizzo na->num_tx_rings = txr; 418ae10d1afSLuigi Rizzo na->num_tx_desc = txd; 419ae10d1afSLuigi Rizzo na->num_rx_rings = rxr; 420ae10d1afSLuigi Rizzo na->num_rx_desc = rxd; 421ae10d1afSLuigi Rizzo return 0; 422ae10d1afSLuigi Rizzo } 423ae10d1afSLuigi Rizzo D("configuration changed while active, this is bad..."); 424ae10d1afSLuigi Rizzo return 1; 425ae10d1afSLuigi Rizzo } 426ae10d1afSLuigi Rizzo 427f9790aebSLuigi Rizzo 428f9790aebSLuigi Rizzo int 429f9790aebSLuigi Rizzo netmap_krings_create(struct netmap_adapter *na, u_int ntx, u_int nrx, u_int tailroom) 430f9790aebSLuigi Rizzo { 431f9790aebSLuigi Rizzo u_int i, len, ndesc; 432f9790aebSLuigi Rizzo struct netmap_kring *kring; 433f9790aebSLuigi Rizzo 434*17885a7bSLuigi Rizzo // XXX additional space for extra rings ? 435f9790aebSLuigi Rizzo len = (ntx + nrx) * sizeof(struct netmap_kring) + tailroom; 436f9790aebSLuigi Rizzo 437f9790aebSLuigi Rizzo na->tx_rings = malloc((size_t)len, M_DEVBUF, M_NOWAIT | M_ZERO); 438f9790aebSLuigi Rizzo if (na->tx_rings == NULL) { 439f9790aebSLuigi Rizzo D("Cannot allocate krings"); 440f9790aebSLuigi Rizzo return ENOMEM; 441f9790aebSLuigi Rizzo } 442f9790aebSLuigi Rizzo na->rx_rings = na->tx_rings + ntx; 443f9790aebSLuigi Rizzo 444*17885a7bSLuigi Rizzo /* 445*17885a7bSLuigi Rizzo * All fields in krings are 0 except the one initialized below. 446*17885a7bSLuigi Rizzo * but better be explicit on important kring fields. 447*17885a7bSLuigi Rizzo */ 448f9790aebSLuigi Rizzo ndesc = na->num_tx_desc; 449f9790aebSLuigi Rizzo for (i = 0; i < ntx; i++) { /* Transmit rings */ 450f9790aebSLuigi Rizzo kring = &na->tx_rings[i]; 451f9790aebSLuigi Rizzo bzero(kring, sizeof(*kring)); 452f9790aebSLuigi Rizzo kring->na = na; 453*17885a7bSLuigi Rizzo kring->ring_id = i; 454f9790aebSLuigi Rizzo kring->nkr_num_slots = ndesc; 455f9790aebSLuigi Rizzo /* 456*17885a7bSLuigi Rizzo * IMPORTANT: Always keep one slot empty. 457f9790aebSLuigi Rizzo */ 458*17885a7bSLuigi Rizzo kring->rhead = kring->rcur = kring->nr_hwcur = 0; 459*17885a7bSLuigi Rizzo kring->rtail = kring->nr_hwtail = ndesc - 1; 460*17885a7bSLuigi Rizzo snprintf(kring->name, sizeof(kring->name) - 1, "%s TX%d", NM_IFPNAME(na->ifp), i); 461f9790aebSLuigi Rizzo mtx_init(&kring->q_lock, "nm_txq_lock", NULL, MTX_DEF); 462f9790aebSLuigi Rizzo init_waitqueue_head(&kring->si); 463f9790aebSLuigi Rizzo } 464f9790aebSLuigi Rizzo 465f9790aebSLuigi Rizzo ndesc = na->num_rx_desc; 466f9790aebSLuigi Rizzo for (i = 0; i < nrx; i++) { /* Receive rings */ 467f9790aebSLuigi Rizzo kring = &na->rx_rings[i]; 468f9790aebSLuigi Rizzo bzero(kring, sizeof(*kring)); 469f9790aebSLuigi Rizzo kring->na = na; 470*17885a7bSLuigi Rizzo kring->ring_id = i; 471f9790aebSLuigi Rizzo kring->nkr_num_slots = ndesc; 472*17885a7bSLuigi Rizzo kring->rhead = kring->rcur = kring->nr_hwcur = 0; 473*17885a7bSLuigi Rizzo kring->rtail = kring->nr_hwtail = 0; 474*17885a7bSLuigi Rizzo snprintf(kring->name, sizeof(kring->name) - 1, "%s RX%d", NM_IFPNAME(na->ifp), i); 475f9790aebSLuigi Rizzo mtx_init(&kring->q_lock, "nm_rxq_lock", NULL, MTX_DEF); 476f9790aebSLuigi Rizzo init_waitqueue_head(&kring->si); 477f9790aebSLuigi Rizzo } 478f9790aebSLuigi Rizzo init_waitqueue_head(&na->tx_si); 479f9790aebSLuigi Rizzo init_waitqueue_head(&na->rx_si); 480f9790aebSLuigi Rizzo 481f9790aebSLuigi Rizzo na->tailroom = na->rx_rings + nrx; 482f9790aebSLuigi Rizzo 483f9790aebSLuigi Rizzo return 0; 484f9790aebSLuigi Rizzo } 485f9790aebSLuigi Rizzo 486f9790aebSLuigi Rizzo 487*17885a7bSLuigi Rizzo /* XXX check boundaries */ 488f9790aebSLuigi Rizzo void 489f9790aebSLuigi Rizzo netmap_krings_delete(struct netmap_adapter *na) 490f9790aebSLuigi Rizzo { 491f9790aebSLuigi Rizzo int i; 492f9790aebSLuigi Rizzo 493f9790aebSLuigi Rizzo for (i = 0; i < na->num_tx_rings + 1; i++) { 494f9790aebSLuigi Rizzo mtx_destroy(&na->tx_rings[i].q_lock); 495f9790aebSLuigi Rizzo } 496f9790aebSLuigi Rizzo for (i = 0; i < na->num_rx_rings + 1; i++) { 497f9790aebSLuigi Rizzo mtx_destroy(&na->rx_rings[i].q_lock); 498f9790aebSLuigi Rizzo } 499f9790aebSLuigi Rizzo free(na->tx_rings, M_DEVBUF); 500f9790aebSLuigi Rizzo na->tx_rings = na->rx_rings = na->tailroom = NULL; 501f9790aebSLuigi Rizzo } 502f9790aebSLuigi Rizzo 503f9790aebSLuigi Rizzo 504*17885a7bSLuigi Rizzo /* 505*17885a7bSLuigi Rizzo * Destructor for NIC ports. They also have an mbuf queue 506*17885a7bSLuigi Rizzo * on the rings connected to the host so we need to purge 507*17885a7bSLuigi Rizzo * them first. 508*17885a7bSLuigi Rizzo */ 509*17885a7bSLuigi Rizzo static void 510*17885a7bSLuigi Rizzo netmap_hw_krings_delete(struct netmap_adapter *na) 511*17885a7bSLuigi Rizzo { 512*17885a7bSLuigi Rizzo struct mbq *q = &na->rx_rings[na->num_rx_rings].rx_queue; 513*17885a7bSLuigi Rizzo 514*17885a7bSLuigi Rizzo ND("destroy sw mbq with len %d", mbq_len(q)); 515*17885a7bSLuigi Rizzo mbq_purge(q); 516*17885a7bSLuigi Rizzo mbq_safe_destroy(q); 517*17885a7bSLuigi Rizzo netmap_krings_delete(na); 518*17885a7bSLuigi Rizzo } 519*17885a7bSLuigi Rizzo 520*17885a7bSLuigi Rizzo 521ce3ee1e7SLuigi Rizzo static struct netmap_if* 522ce3ee1e7SLuigi Rizzo netmap_if_new(const char *ifname, struct netmap_adapter *na) 523ce3ee1e7SLuigi Rizzo { 524f9790aebSLuigi Rizzo struct netmap_if *nifp; 525f9790aebSLuigi Rizzo 526ce3ee1e7SLuigi Rizzo if (netmap_update_config(na)) { 527ce3ee1e7SLuigi Rizzo /* configuration mismatch, report and fail */ 528ce3ee1e7SLuigi Rizzo return NULL; 529ce3ee1e7SLuigi Rizzo } 530f9790aebSLuigi Rizzo 531f9790aebSLuigi Rizzo if (na->active_fds) 532f9790aebSLuigi Rizzo goto final; 533f9790aebSLuigi Rizzo 534f9790aebSLuigi Rizzo if (na->nm_krings_create(na)) 535f9790aebSLuigi Rizzo goto cleanup; 536f9790aebSLuigi Rizzo 537f9790aebSLuigi Rizzo if (netmap_mem_rings_create(na)) 538f9790aebSLuigi Rizzo goto cleanup; 539f9790aebSLuigi Rizzo 540f9790aebSLuigi Rizzo final: 541f9790aebSLuigi Rizzo 542f9790aebSLuigi Rizzo nifp = netmap_mem_if_new(ifname, na); 543f9790aebSLuigi Rizzo if (nifp == NULL) 544f9790aebSLuigi Rizzo goto cleanup; 545f9790aebSLuigi Rizzo 546f9790aebSLuigi Rizzo return (nifp); 547f9790aebSLuigi Rizzo 548f9790aebSLuigi Rizzo cleanup: 549f9790aebSLuigi Rizzo 550f9790aebSLuigi Rizzo if (na->active_fds == 0) { 551f9790aebSLuigi Rizzo netmap_mem_rings_delete(na); 552f9790aebSLuigi Rizzo na->nm_krings_delete(na); 553ce3ee1e7SLuigi Rizzo } 55468b8534bSLuigi Rizzo 555f9790aebSLuigi Rizzo return NULL; 556f9790aebSLuigi Rizzo } 5578241616dSLuigi Rizzo 55868b8534bSLuigi Rizzo 559ce3ee1e7SLuigi Rizzo /* grab a reference to the memory allocator, if we don't have one already. The 560ce3ee1e7SLuigi Rizzo * reference is taken from the netmap_adapter registered with the priv. 561ce3ee1e7SLuigi Rizzo * 562ce3ee1e7SLuigi Rizzo */ 563ce3ee1e7SLuigi Rizzo static int 564ce3ee1e7SLuigi Rizzo netmap_get_memory_locked(struct netmap_priv_d* p) 565ce3ee1e7SLuigi Rizzo { 566ce3ee1e7SLuigi Rizzo struct netmap_mem_d *nmd; 567ce3ee1e7SLuigi Rizzo int error = 0; 568ce3ee1e7SLuigi Rizzo 569f9790aebSLuigi Rizzo if (p->np_na == NULL) { 570ce3ee1e7SLuigi Rizzo if (!netmap_mmap_unreg) 571ce3ee1e7SLuigi Rizzo return ENODEV; 572ce3ee1e7SLuigi Rizzo /* for compatibility with older versions of the API 573ce3ee1e7SLuigi Rizzo * we use the global allocator when no interface has been 574ce3ee1e7SLuigi Rizzo * registered 575ce3ee1e7SLuigi Rizzo */ 576ce3ee1e7SLuigi Rizzo nmd = &nm_mem; 577ce3ee1e7SLuigi Rizzo } else { 578f9790aebSLuigi Rizzo nmd = p->np_na->nm_mem; 579ce3ee1e7SLuigi Rizzo } 580ce3ee1e7SLuigi Rizzo if (p->np_mref == NULL) { 581ce3ee1e7SLuigi Rizzo error = netmap_mem_finalize(nmd); 582ce3ee1e7SLuigi Rizzo if (!error) 583ce3ee1e7SLuigi Rizzo p->np_mref = nmd; 584ce3ee1e7SLuigi Rizzo } else if (p->np_mref != nmd) { 585ce3ee1e7SLuigi Rizzo /* a virtual port has been registered, but previous 586ce3ee1e7SLuigi Rizzo * syscalls already used the global allocator. 587ce3ee1e7SLuigi Rizzo * We cannot continue 588ce3ee1e7SLuigi Rizzo */ 589ce3ee1e7SLuigi Rizzo error = ENODEV; 590ce3ee1e7SLuigi Rizzo } 591ce3ee1e7SLuigi Rizzo return error; 592ce3ee1e7SLuigi Rizzo } 59368b8534bSLuigi Rizzo 594f9790aebSLuigi Rizzo 595f9790aebSLuigi Rizzo int 5968241616dSLuigi Rizzo netmap_get_memory(struct netmap_priv_d* p) 5978241616dSLuigi Rizzo { 598ce3ee1e7SLuigi Rizzo int error; 599ce3ee1e7SLuigi Rizzo NMG_LOCK(); 600ce3ee1e7SLuigi Rizzo error = netmap_get_memory_locked(p); 601ce3ee1e7SLuigi Rizzo NMG_UNLOCK(); 6028241616dSLuigi Rizzo return error; 6038241616dSLuigi Rizzo } 6048241616dSLuigi Rizzo 605f9790aebSLuigi Rizzo 606ce3ee1e7SLuigi Rizzo static int 607ce3ee1e7SLuigi Rizzo netmap_have_memory_locked(struct netmap_priv_d* p) 608ce3ee1e7SLuigi Rizzo { 609ce3ee1e7SLuigi Rizzo return p->np_mref != NULL; 610ce3ee1e7SLuigi Rizzo } 611ce3ee1e7SLuigi Rizzo 612f9790aebSLuigi Rizzo 613ce3ee1e7SLuigi Rizzo static void 614ce3ee1e7SLuigi Rizzo netmap_drop_memory_locked(struct netmap_priv_d* p) 615ce3ee1e7SLuigi Rizzo { 616ce3ee1e7SLuigi Rizzo if (p->np_mref) { 617ce3ee1e7SLuigi Rizzo netmap_mem_deref(p->np_mref); 618ce3ee1e7SLuigi Rizzo p->np_mref = NULL; 619ce3ee1e7SLuigi Rizzo } 620ce3ee1e7SLuigi Rizzo } 621ce3ee1e7SLuigi Rizzo 622f9790aebSLuigi Rizzo 62368b8534bSLuigi Rizzo /* 62468b8534bSLuigi Rizzo * File descriptor's private data destructor. 62568b8534bSLuigi Rizzo * 62668b8534bSLuigi Rizzo * Call nm_register(ifp,0) to stop netmap mode on the interface and 627f9790aebSLuigi Rizzo * revert to normal operation. We expect that np_na->ifp has not gone. 628ce3ee1e7SLuigi Rizzo * The second argument is the nifp to work on. In some cases it is 629ce3ee1e7SLuigi Rizzo * not attached yet to the netmap_priv_d so we need to pass it as 630ce3ee1e7SLuigi Rizzo * a separate argument. 63168b8534bSLuigi Rizzo */ 632ce3ee1e7SLuigi Rizzo /* call with NMG_LOCK held */ 63368b8534bSLuigi Rizzo static void 634ce3ee1e7SLuigi Rizzo netmap_do_unregif(struct netmap_priv_d *priv, struct netmap_if *nifp) 63568b8534bSLuigi Rizzo { 636f9790aebSLuigi Rizzo struct netmap_adapter *na = priv->np_na; 637f9790aebSLuigi Rizzo struct ifnet *ifp = na->ifp; 63868b8534bSLuigi Rizzo 639ce3ee1e7SLuigi Rizzo NMG_LOCK_ASSERT(); 640f9790aebSLuigi Rizzo na->active_fds--; 641f9790aebSLuigi Rizzo if (na->active_fds <= 0) { /* last instance */ 64268b8534bSLuigi Rizzo 643ae10d1afSLuigi Rizzo if (netmap_verbose) 644f9790aebSLuigi Rizzo D("deleting last instance for %s", NM_IFPNAME(ifp)); 64568b8534bSLuigi Rizzo /* 646f18be576SLuigi Rizzo * (TO CHECK) This function is only called 647f18be576SLuigi Rizzo * when the last reference to this file descriptor goes 648f18be576SLuigi Rizzo * away. This means we cannot have any pending poll() 649f18be576SLuigi Rizzo * or interrupt routine operating on the structure. 650ce3ee1e7SLuigi Rizzo * XXX The file may be closed in a thread while 651ce3ee1e7SLuigi Rizzo * another thread is using it. 652ce3ee1e7SLuigi Rizzo * Linux keeps the file opened until the last reference 653ce3ee1e7SLuigi Rizzo * by any outstanding ioctl/poll or mmap is gone. 654ce3ee1e7SLuigi Rizzo * FreeBSD does not track mmap()s (but we do) and 655ce3ee1e7SLuigi Rizzo * wakes up any sleeping poll(). Need to check what 656ce3ee1e7SLuigi Rizzo * happens if the close() occurs while a concurrent 657ce3ee1e7SLuigi Rizzo * syscall is running. 65868b8534bSLuigi Rizzo */ 659f9790aebSLuigi Rizzo if (ifp) 660f9790aebSLuigi Rizzo na->nm_register(na, 0); /* off, clear flags */ 66168b8534bSLuigi Rizzo /* Wake up any sleeping threads. netmap_poll will 66268b8534bSLuigi Rizzo * then return POLLERR 663ce3ee1e7SLuigi Rizzo * XXX The wake up now must happen during *_down(), when 664ce3ee1e7SLuigi Rizzo * we order all activities to stop. -gl 66568b8534bSLuigi Rizzo */ 6662f70fca5SEd Maste /* XXX kqueue(9) needed; these will mirror knlist_init. */ 6672f70fca5SEd Maste /* knlist_destroy(&na->tx_si.si_note); */ 6682f70fca5SEd Maste /* knlist_destroy(&na->rx_si.si_note); */ 669f9790aebSLuigi Rizzo 670f9790aebSLuigi Rizzo /* delete rings and buffers */ 671f9790aebSLuigi Rizzo netmap_mem_rings_delete(na); 672f9790aebSLuigi Rizzo na->nm_krings_delete(na); 67368b8534bSLuigi Rizzo } 674f9790aebSLuigi Rizzo /* delete the nifp */ 675ce3ee1e7SLuigi Rizzo netmap_mem_if_delete(na, nifp); 6765819da83SLuigi Rizzo } 67768b8534bSLuigi Rizzo 678f18be576SLuigi Rizzo 679ce3ee1e7SLuigi Rizzo /* 680ce3ee1e7SLuigi Rizzo * returns 1 if this is the last instance and we can free priv 681ce3ee1e7SLuigi Rizzo */ 682f9790aebSLuigi Rizzo int 683ce3ee1e7SLuigi Rizzo netmap_dtor_locked(struct netmap_priv_d *priv) 684ce3ee1e7SLuigi Rizzo { 685f9790aebSLuigi Rizzo struct netmap_adapter *na = priv->np_na; 686ce3ee1e7SLuigi Rizzo 687ce3ee1e7SLuigi Rizzo #ifdef __FreeBSD__ 688ce3ee1e7SLuigi Rizzo /* 689ce3ee1e7SLuigi Rizzo * np_refcount is the number of active mmaps on 690ce3ee1e7SLuigi Rizzo * this file descriptor 691ce3ee1e7SLuigi Rizzo */ 692ce3ee1e7SLuigi Rizzo if (--priv->np_refcount > 0) { 693ce3ee1e7SLuigi Rizzo return 0; 694ce3ee1e7SLuigi Rizzo } 695ce3ee1e7SLuigi Rizzo #endif /* __FreeBSD__ */ 696f9790aebSLuigi Rizzo if (!na) { 697f9790aebSLuigi Rizzo return 1; //XXX is it correct? 698ce3ee1e7SLuigi Rizzo } 699f9790aebSLuigi Rizzo netmap_do_unregif(priv, priv->np_nifp); 700f9790aebSLuigi Rizzo priv->np_nifp = NULL; 701ce3ee1e7SLuigi Rizzo netmap_drop_memory_locked(priv); 702f9790aebSLuigi Rizzo if (priv->np_na) { 703f9790aebSLuigi Rizzo netmap_adapter_put(na); 704f9790aebSLuigi Rizzo priv->np_na = NULL; 705ce3ee1e7SLuigi Rizzo } 706ce3ee1e7SLuigi Rizzo return 1; 707f196ce38SLuigi Rizzo } 7085819da83SLuigi Rizzo 709f9790aebSLuigi Rizzo 710f9790aebSLuigi Rizzo void 7115819da83SLuigi Rizzo netmap_dtor(void *data) 7125819da83SLuigi Rizzo { 7135819da83SLuigi Rizzo struct netmap_priv_d *priv = data; 714ce3ee1e7SLuigi Rizzo int last_instance; 7155819da83SLuigi Rizzo 716ce3ee1e7SLuigi Rizzo NMG_LOCK(); 717ce3ee1e7SLuigi Rizzo last_instance = netmap_dtor_locked(priv); 718ce3ee1e7SLuigi Rizzo NMG_UNLOCK(); 719ce3ee1e7SLuigi Rizzo if (last_instance) { 720ce3ee1e7SLuigi Rizzo bzero(priv, sizeof(*priv)); /* for safety */ 72168b8534bSLuigi Rizzo free(priv, M_DEVBUF); 72268b8534bSLuigi Rizzo } 723ce3ee1e7SLuigi Rizzo } 72468b8534bSLuigi Rizzo 725f18be576SLuigi Rizzo 72668b8534bSLuigi Rizzo 72768b8534bSLuigi Rizzo 72868b8534bSLuigi Rizzo /* 72902ad4083SLuigi Rizzo * Handlers for synchronization of the queues from/to the host. 730091fd0abSLuigi Rizzo * Netmap has two operating modes: 731091fd0abSLuigi Rizzo * - in the default mode, the rings connected to the host stack are 732091fd0abSLuigi Rizzo * just another ring pair managed by userspace; 733091fd0abSLuigi Rizzo * - in transparent mode (XXX to be defined) incoming packets 734091fd0abSLuigi Rizzo * (from the host or the NIC) are marked as NS_FORWARD upon 735091fd0abSLuigi Rizzo * arrival, and the user application has a chance to reset the 736091fd0abSLuigi Rizzo * flag for packets that should be dropped. 737091fd0abSLuigi Rizzo * On the RXSYNC or poll(), packets in RX rings between 738091fd0abSLuigi Rizzo * kring->nr_kcur and ring->cur with NS_FORWARD still set are moved 739091fd0abSLuigi Rizzo * to the other side. 740091fd0abSLuigi Rizzo * The transfer NIC --> host is relatively easy, just encapsulate 741091fd0abSLuigi Rizzo * into mbufs and we are done. The host --> NIC side is slightly 742091fd0abSLuigi Rizzo * harder because there might not be room in the tx ring so it 743091fd0abSLuigi Rizzo * might take a while before releasing the buffer. 744091fd0abSLuigi Rizzo */ 745091fd0abSLuigi Rizzo 746f18be576SLuigi Rizzo 747091fd0abSLuigi Rizzo /* 748091fd0abSLuigi Rizzo * pass a chain of buffers to the host stack as coming from 'dst' 749*17885a7bSLuigi Rizzo * We do not need to lock because the queue is private. 750091fd0abSLuigi Rizzo */ 751091fd0abSLuigi Rizzo static void 752f9790aebSLuigi Rizzo netmap_send_up(struct ifnet *dst, struct mbq *q) 753091fd0abSLuigi Rizzo { 754091fd0abSLuigi Rizzo struct mbuf *m; 755091fd0abSLuigi Rizzo 756091fd0abSLuigi Rizzo /* send packets up, outside the lock */ 757f9790aebSLuigi Rizzo while ((m = mbq_dequeue(q)) != NULL) { 758091fd0abSLuigi Rizzo if (netmap_verbose & NM_VERB_HOST) 759091fd0abSLuigi Rizzo D("sending up pkt %p size %d", m, MBUF_LEN(m)); 760091fd0abSLuigi Rizzo NM_SEND_UP(dst, m); 761091fd0abSLuigi Rizzo } 762f9790aebSLuigi Rizzo mbq_destroy(q); 763091fd0abSLuigi Rizzo } 764091fd0abSLuigi Rizzo 765f18be576SLuigi Rizzo 766091fd0abSLuigi Rizzo /* 767091fd0abSLuigi Rizzo * put a copy of the buffers marked NS_FORWARD into an mbuf chain. 768*17885a7bSLuigi Rizzo * Take packets from hwcur to ring->head marked NS_FORWARD (or forced) 769*17885a7bSLuigi Rizzo * and pass them up. Drop remaining packets in the unlikely event 770*17885a7bSLuigi Rizzo * of an mbuf shortage. 771091fd0abSLuigi Rizzo */ 772091fd0abSLuigi Rizzo static void 773091fd0abSLuigi Rizzo netmap_grab_packets(struct netmap_kring *kring, struct mbq *q, int force) 774091fd0abSLuigi Rizzo { 775*17885a7bSLuigi Rizzo u_int const lim = kring->nkr_num_slots - 1; 776*17885a7bSLuigi Rizzo u_int const head = kring->ring->head; 777*17885a7bSLuigi Rizzo u_int n; 778f9790aebSLuigi Rizzo struct netmap_adapter *na = kring->na; 779091fd0abSLuigi Rizzo 780*17885a7bSLuigi Rizzo for (n = kring->nr_hwcur; n != head; n = nm_next(n, lim)) { 781*17885a7bSLuigi Rizzo struct mbuf *m; 782091fd0abSLuigi Rizzo struct netmap_slot *slot = &kring->ring->slot[n]; 783091fd0abSLuigi Rizzo 784091fd0abSLuigi Rizzo if ((slot->flags & NS_FORWARD) == 0 && !force) 785091fd0abSLuigi Rizzo continue; 786f9790aebSLuigi Rizzo if (slot->len < 14 || slot->len > NETMAP_BDG_BUF_SIZE(na->nm_mem)) { 787*17885a7bSLuigi Rizzo RD(5, "bad pkt at %d len %d", n, slot->len); 788091fd0abSLuigi Rizzo continue; 789091fd0abSLuigi Rizzo } 790091fd0abSLuigi Rizzo slot->flags &= ~NS_FORWARD; // XXX needed ? 791*17885a7bSLuigi Rizzo /* XXX TODO: adapt to the case of a multisegment packet */ 792f9790aebSLuigi Rizzo m = m_devget(BDG_NMB(na, slot), slot->len, 0, na->ifp, NULL); 793091fd0abSLuigi Rizzo 794091fd0abSLuigi Rizzo if (m == NULL) 795091fd0abSLuigi Rizzo break; 796f9790aebSLuigi Rizzo mbq_enqueue(q, m); 797091fd0abSLuigi Rizzo } 798091fd0abSLuigi Rizzo } 799091fd0abSLuigi Rizzo 800f18be576SLuigi Rizzo 801091fd0abSLuigi Rizzo /* 802*17885a7bSLuigi Rizzo * Send to the NIC rings packets marked NS_FORWARD between 803*17885a7bSLuigi Rizzo * kring->nr_hwcur and kring->rhead 804*17885a7bSLuigi Rizzo * Called under kring->rx_queue.lock on the sw rx ring, 805091fd0abSLuigi Rizzo */ 806*17885a7bSLuigi Rizzo static u_int 807091fd0abSLuigi Rizzo netmap_sw_to_nic(struct netmap_adapter *na) 808091fd0abSLuigi Rizzo { 809091fd0abSLuigi Rizzo struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings]; 810*17885a7bSLuigi Rizzo struct netmap_slot *rxslot = kring->ring->slot; 811*17885a7bSLuigi Rizzo u_int i, rxcur = kring->nr_hwcur; 812*17885a7bSLuigi Rizzo u_int const head = kring->rhead; 813*17885a7bSLuigi Rizzo u_int const src_lim = kring->nkr_num_slots - 1; 814*17885a7bSLuigi Rizzo u_int sent = 0; 815ce3ee1e7SLuigi Rizzo 816*17885a7bSLuigi Rizzo /* scan rings to find space, then fill as much as possible */ 817*17885a7bSLuigi Rizzo for (i = 0; i < na->num_tx_rings; i++) { 818*17885a7bSLuigi Rizzo struct netmap_kring *kdst = &na->tx_rings[i]; 819*17885a7bSLuigi Rizzo struct netmap_ring *rdst = kdst->ring; 820*17885a7bSLuigi Rizzo u_int const dst_lim = kdst->nkr_num_slots - 1; 821ce3ee1e7SLuigi Rizzo 822*17885a7bSLuigi Rizzo /* XXX do we trust ring or kring->rcur,rtail ? */ 823*17885a7bSLuigi Rizzo for (; rxcur != head && !nm_ring_empty(rdst); 824*17885a7bSLuigi Rizzo rxcur = nm_next(rxcur, src_lim) ) { 825091fd0abSLuigi Rizzo struct netmap_slot *src, *dst, tmp; 826*17885a7bSLuigi Rizzo u_int dst_cur = rdst->cur; 827*17885a7bSLuigi Rizzo 828*17885a7bSLuigi Rizzo src = &rxslot[rxcur]; 829*17885a7bSLuigi Rizzo if ((src->flags & NS_FORWARD) == 0 && !netmap_fwd) 830*17885a7bSLuigi Rizzo continue; 831*17885a7bSLuigi Rizzo 832*17885a7bSLuigi Rizzo sent++; 833*17885a7bSLuigi Rizzo 834*17885a7bSLuigi Rizzo dst = &rdst->slot[dst_cur]; 835*17885a7bSLuigi Rizzo 836091fd0abSLuigi Rizzo tmp = *src; 837*17885a7bSLuigi Rizzo 838091fd0abSLuigi Rizzo src->buf_idx = dst->buf_idx; 839091fd0abSLuigi Rizzo src->flags = NS_BUF_CHANGED; 840091fd0abSLuigi Rizzo 841091fd0abSLuigi Rizzo dst->buf_idx = tmp.buf_idx; 842091fd0abSLuigi Rizzo dst->len = tmp.len; 843091fd0abSLuigi Rizzo dst->flags = NS_BUF_CHANGED; 844091fd0abSLuigi Rizzo 845*17885a7bSLuigi Rizzo rdst->cur = nm_next(dst_cur, dst_lim); 846091fd0abSLuigi Rizzo } 847*17885a7bSLuigi Rizzo /* if (sent) XXX txsync ? */ 848091fd0abSLuigi Rizzo } 849*17885a7bSLuigi Rizzo return sent; 850091fd0abSLuigi Rizzo } 851091fd0abSLuigi Rizzo 852f18be576SLuigi Rizzo 853091fd0abSLuigi Rizzo /* 854ce3ee1e7SLuigi Rizzo * netmap_txsync_to_host() passes packets up. We are called from a 85502ad4083SLuigi Rizzo * system call in user process context, and the only contention 85602ad4083SLuigi Rizzo * can be among multiple user threads erroneously calling 857091fd0abSLuigi Rizzo * this routine concurrently. 85868b8534bSLuigi Rizzo */ 859f9790aebSLuigi Rizzo void 860ce3ee1e7SLuigi Rizzo netmap_txsync_to_host(struct netmap_adapter *na) 86168b8534bSLuigi Rizzo { 862d76bf4ffSLuigi Rizzo struct netmap_kring *kring = &na->tx_rings[na->num_tx_rings]; 86368b8534bSLuigi Rizzo struct netmap_ring *ring = kring->ring; 864*17885a7bSLuigi Rizzo u_int const lim = kring->nkr_num_slots - 1; 865*17885a7bSLuigi Rizzo u_int const head = nm_txsync_prologue(kring); 866f9790aebSLuigi Rizzo struct mbq q; 867f9790aebSLuigi Rizzo int error; 86868b8534bSLuigi Rizzo 869f9790aebSLuigi Rizzo error = nm_kr_tryget(kring); 870f9790aebSLuigi Rizzo if (error) { 871f9790aebSLuigi Rizzo if (error == NM_KR_BUSY) 872ce3ee1e7SLuigi Rizzo D("ring %p busy (user error)", kring); 87302ad4083SLuigi Rizzo return; 87402ad4083SLuigi Rizzo } 875*17885a7bSLuigi Rizzo if (head > lim) { 876ce3ee1e7SLuigi Rizzo D("invalid ring index in stack TX kring %p", kring); 877ce3ee1e7SLuigi Rizzo netmap_ring_reinit(kring); 878ce3ee1e7SLuigi Rizzo nm_kr_put(kring); 879ce3ee1e7SLuigi Rizzo return; 880ce3ee1e7SLuigi Rizzo } 88168b8534bSLuigi Rizzo 882*17885a7bSLuigi Rizzo /* Take packets from hwcur to head and pass them up. 883*17885a7bSLuigi Rizzo * force head = cur since netmap_grab_packets() stops at head 88468b8534bSLuigi Rizzo * In case of no buffers we give up. At the end of the loop, 88568b8534bSLuigi Rizzo * the queue is drained in all cases. 88668b8534bSLuigi Rizzo */ 887f9790aebSLuigi Rizzo mbq_init(&q); 888*17885a7bSLuigi Rizzo ring->cur = head; 889*17885a7bSLuigi Rizzo netmap_grab_packets(kring, &q, 1 /* force */); 890*17885a7bSLuigi Rizzo ND("have %d pkts in queue", mbq_len(&q)); 891*17885a7bSLuigi Rizzo kring->nr_hwcur = head; 892*17885a7bSLuigi Rizzo kring->nr_hwtail = head + lim; 893*17885a7bSLuigi Rizzo if (kring->nr_hwtail > lim) 894*17885a7bSLuigi Rizzo kring->nr_hwtail -= lim + 1; 895*17885a7bSLuigi Rizzo nm_txsync_finalize(kring); 89668b8534bSLuigi Rizzo 897ce3ee1e7SLuigi Rizzo nm_kr_put(kring); 898f9790aebSLuigi Rizzo netmap_send_up(na->ifp, &q); 899f18be576SLuigi Rizzo } 900f18be576SLuigi Rizzo 901f18be576SLuigi Rizzo 90268b8534bSLuigi Rizzo /* 90302ad4083SLuigi Rizzo * rxsync backend for packets coming from the host stack. 904*17885a7bSLuigi Rizzo * They have been put in kring->rx_queue by netmap_transmit(). 905*17885a7bSLuigi Rizzo * We protect access to the kring using kring->rx_queue.lock 90602ad4083SLuigi Rizzo * 90768b8534bSLuigi Rizzo * This routine also does the selrecord if called from the poll handler 90868b8534bSLuigi Rizzo * (we know because td != NULL). 90901c7d25fSLuigi Rizzo * 91001c7d25fSLuigi Rizzo * NOTE: on linux, selrecord() is defined as a macro and uses pwait 91101c7d25fSLuigi Rizzo * as an additional hidden argument. 912*17885a7bSLuigi Rizzo * returns the number of packets delivered to tx queues in 913*17885a7bSLuigi Rizzo * transparent mode, or a negative value if error 91468b8534bSLuigi Rizzo */ 915*17885a7bSLuigi Rizzo int 916ce3ee1e7SLuigi Rizzo netmap_rxsync_from_host(struct netmap_adapter *na, struct thread *td, void *pwait) 91768b8534bSLuigi Rizzo { 918d76bf4ffSLuigi Rizzo struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings]; 91968b8534bSLuigi Rizzo struct netmap_ring *ring = kring->ring; 920*17885a7bSLuigi Rizzo u_int nm_i, n; 921*17885a7bSLuigi Rizzo u_int const lim = kring->nkr_num_slots - 1; 922*17885a7bSLuigi Rizzo u_int const head = nm_rxsync_prologue(kring); 923*17885a7bSLuigi Rizzo int ret = 0; 924*17885a7bSLuigi Rizzo struct mbq *q = &kring->rx_queue; 92568b8534bSLuigi Rizzo 92601c7d25fSLuigi Rizzo (void)pwait; /* disable unused warnings */ 927ce3ee1e7SLuigi Rizzo 928*17885a7bSLuigi Rizzo if (head > lim) { 92964ae02c3SLuigi Rizzo netmap_ring_reinit(kring); 930*17885a7bSLuigi Rizzo return EINVAL; 931*17885a7bSLuigi Rizzo } 932*17885a7bSLuigi Rizzo 933*17885a7bSLuigi Rizzo if (kring->nkr_stopped) /* check a first time without lock */ 934*17885a7bSLuigi Rizzo return EBUSY; 935*17885a7bSLuigi Rizzo 936*17885a7bSLuigi Rizzo mtx_lock(&q->lock); 937*17885a7bSLuigi Rizzo 938*17885a7bSLuigi Rizzo if (kring->nkr_stopped) { /* check again with lock held */ 939*17885a7bSLuigi Rizzo ret = EBUSY; 940ce3ee1e7SLuigi Rizzo goto unlock_out; 94164ae02c3SLuigi Rizzo } 942*17885a7bSLuigi Rizzo 943*17885a7bSLuigi Rizzo /* First part: import newly received packets */ 944*17885a7bSLuigi Rizzo n = mbq_len(q); 945*17885a7bSLuigi Rizzo if (n) { /* grab packets from the queue */ 946*17885a7bSLuigi Rizzo struct mbuf *m; 947*17885a7bSLuigi Rizzo uint32_t stop_i; 948*17885a7bSLuigi Rizzo 949*17885a7bSLuigi Rizzo nm_i = kring->nr_hwtail; 950*17885a7bSLuigi Rizzo stop_i = nm_prev(nm_i, lim); 951*17885a7bSLuigi Rizzo while ( nm_i != stop_i && (m = mbq_dequeue(q)) != NULL ) { 952*17885a7bSLuigi Rizzo int len = MBUF_LEN(m); 953*17885a7bSLuigi Rizzo struct netmap_slot *slot = &ring->slot[nm_i]; 954*17885a7bSLuigi Rizzo 955*17885a7bSLuigi Rizzo m_copydata(m, 0, len, BDG_NMB(na, slot)); 956*17885a7bSLuigi Rizzo ND("nm %d len %d", nm_i, len); 957*17885a7bSLuigi Rizzo if (netmap_verbose) 958*17885a7bSLuigi Rizzo D("%s", nm_dump_buf(BDG_NMB(na, slot),len, 128, NULL)); 959*17885a7bSLuigi Rizzo 960*17885a7bSLuigi Rizzo slot->len = len; 961*17885a7bSLuigi Rizzo slot->flags = kring->nkr_slot_flags; 962*17885a7bSLuigi Rizzo nm_i = nm_next(nm_i, lim); 96364ae02c3SLuigi Rizzo } 964*17885a7bSLuigi Rizzo kring->nr_hwtail = nm_i; 96564ae02c3SLuigi Rizzo } 966*17885a7bSLuigi Rizzo 967*17885a7bSLuigi Rizzo /* 968*17885a7bSLuigi Rizzo * Second part: skip past packets that userspace has released. 969*17885a7bSLuigi Rizzo */ 970*17885a7bSLuigi Rizzo nm_i = kring->nr_hwcur; 971*17885a7bSLuigi Rizzo if (nm_i != head) { /* something was released */ 972*17885a7bSLuigi Rizzo if (netmap_fwd || kring->ring->flags & NR_FORWARD) 973*17885a7bSLuigi Rizzo ret = netmap_sw_to_nic(na); 974*17885a7bSLuigi Rizzo kring->nr_hwcur = head; 97564ae02c3SLuigi Rizzo } 976*17885a7bSLuigi Rizzo 977*17885a7bSLuigi Rizzo nm_rxsync_finalize(kring); 978*17885a7bSLuigi Rizzo 979*17885a7bSLuigi Rizzo /* access copies of cur,tail in the kring */ 980*17885a7bSLuigi Rizzo if (kring->rcur == kring->rtail && td) /* no bufs available */ 98168b8534bSLuigi Rizzo selrecord(td, &kring->si); 982*17885a7bSLuigi Rizzo 983ce3ee1e7SLuigi Rizzo unlock_out: 984ce3ee1e7SLuigi Rizzo 985*17885a7bSLuigi Rizzo mtx_unlock(&q->lock); 986*17885a7bSLuigi Rizzo return ret; 98768b8534bSLuigi Rizzo } 98868b8534bSLuigi Rizzo 98968b8534bSLuigi Rizzo 990f9790aebSLuigi Rizzo /* Get a netmap adapter for the port. 991f9790aebSLuigi Rizzo * 992f9790aebSLuigi Rizzo * If it is possible to satisfy the request, return 0 993f9790aebSLuigi Rizzo * with *na containing the netmap adapter found. 994f9790aebSLuigi Rizzo * Otherwise return an error code, with *na containing NULL. 995f9790aebSLuigi Rizzo * 996f9790aebSLuigi Rizzo * When the port is attached to a bridge, we always return 997f9790aebSLuigi Rizzo * EBUSY. 998f9790aebSLuigi Rizzo * Otherwise, if the port is already bound to a file descriptor, 999f9790aebSLuigi Rizzo * then we unconditionally return the existing adapter into *na. 1000f9790aebSLuigi Rizzo * In all the other cases, we return (into *na) either native, 1001f9790aebSLuigi Rizzo * generic or NULL, according to the following table: 1002f9790aebSLuigi Rizzo * 1003f9790aebSLuigi Rizzo * native_support 1004f9790aebSLuigi Rizzo * active_fds dev.netmap.admode YES NO 1005f9790aebSLuigi Rizzo * ------------------------------------------------------- 1006f9790aebSLuigi Rizzo * >0 * NA(ifp) NA(ifp) 1007f9790aebSLuigi Rizzo * 1008f9790aebSLuigi Rizzo * 0 NETMAP_ADMODE_BEST NATIVE GENERIC 1009f9790aebSLuigi Rizzo * 0 NETMAP_ADMODE_NATIVE NATIVE NULL 1010f9790aebSLuigi Rizzo * 0 NETMAP_ADMODE_GENERIC GENERIC GENERIC 1011f9790aebSLuigi Rizzo * 1012f9790aebSLuigi Rizzo */ 1013f9790aebSLuigi Rizzo 1014f9790aebSLuigi Rizzo int 1015f9790aebSLuigi Rizzo netmap_get_hw_na(struct ifnet *ifp, struct netmap_adapter **na) 1016f9790aebSLuigi Rizzo { 1017f9790aebSLuigi Rizzo /* generic support */ 1018f9790aebSLuigi Rizzo int i = netmap_admode; /* Take a snapshot. */ 1019f9790aebSLuigi Rizzo int error = 0; 1020f9790aebSLuigi Rizzo struct netmap_adapter *prev_na; 1021f9790aebSLuigi Rizzo struct netmap_generic_adapter *gna; 1022f9790aebSLuigi Rizzo 1023f9790aebSLuigi Rizzo *na = NULL; /* default */ 1024f9790aebSLuigi Rizzo 1025f9790aebSLuigi Rizzo /* reset in case of invalid value */ 1026f9790aebSLuigi Rizzo if (i < NETMAP_ADMODE_BEST || i >= NETMAP_ADMODE_LAST) 1027f9790aebSLuigi Rizzo i = netmap_admode = NETMAP_ADMODE_BEST; 1028f9790aebSLuigi Rizzo 1029f9790aebSLuigi Rizzo if (NETMAP_CAPABLE(ifp)) { 1030f9790aebSLuigi Rizzo /* If an adapter already exists, but is 1031f9790aebSLuigi Rizzo * attached to a vale port, we report that the 1032f9790aebSLuigi Rizzo * port is busy. 1033f9790aebSLuigi Rizzo */ 1034f9790aebSLuigi Rizzo if (NETMAP_OWNED_BY_KERN(NA(ifp))) 1035f9790aebSLuigi Rizzo return EBUSY; 1036f9790aebSLuigi Rizzo 1037f9790aebSLuigi Rizzo /* If an adapter already exists, return it if 1038f9790aebSLuigi Rizzo * there are active file descriptors or if 1039f9790aebSLuigi Rizzo * netmap is not forced to use generic 1040f9790aebSLuigi Rizzo * adapters. 1041f9790aebSLuigi Rizzo */ 1042f9790aebSLuigi Rizzo if (NA(ifp)->active_fds > 0 || 1043f9790aebSLuigi Rizzo i != NETMAP_ADMODE_GENERIC) { 1044f9790aebSLuigi Rizzo *na = NA(ifp); 1045f9790aebSLuigi Rizzo return 0; 1046f9790aebSLuigi Rizzo } 1047f9790aebSLuigi Rizzo } 1048f9790aebSLuigi Rizzo 1049f9790aebSLuigi Rizzo /* If there isn't native support and netmap is not allowed 1050f9790aebSLuigi Rizzo * to use generic adapters, we cannot satisfy the request. 1051f9790aebSLuigi Rizzo */ 1052f9790aebSLuigi Rizzo if (!NETMAP_CAPABLE(ifp) && i == NETMAP_ADMODE_NATIVE) 1053f9790aebSLuigi Rizzo return EINVAL; 1054f9790aebSLuigi Rizzo 1055f9790aebSLuigi Rizzo /* Otherwise, create a generic adapter and return it, 1056f9790aebSLuigi Rizzo * saving the previously used netmap adapter, if any. 1057f9790aebSLuigi Rizzo * 1058f9790aebSLuigi Rizzo * Note that here 'prev_na', if not NULL, MUST be a 1059f9790aebSLuigi Rizzo * native adapter, and CANNOT be a generic one. This is 1060f9790aebSLuigi Rizzo * true because generic adapters are created on demand, and 1061f9790aebSLuigi Rizzo * destroyed when not used anymore. Therefore, if the adapter 1062f9790aebSLuigi Rizzo * currently attached to an interface 'ifp' is generic, it 1063f9790aebSLuigi Rizzo * must be that 1064f9790aebSLuigi Rizzo * (NA(ifp)->active_fds > 0 || NETMAP_OWNED_BY_KERN(NA(ifp))). 1065f9790aebSLuigi Rizzo * Consequently, if NA(ifp) is generic, we will enter one of 1066f9790aebSLuigi Rizzo * the branches above. This ensures that we never override 1067f9790aebSLuigi Rizzo * a generic adapter with another generic adapter. 1068f9790aebSLuigi Rizzo */ 1069f9790aebSLuigi Rizzo prev_na = NA(ifp); 1070f9790aebSLuigi Rizzo error = generic_netmap_attach(ifp); 1071f9790aebSLuigi Rizzo if (error) 1072f9790aebSLuigi Rizzo return error; 1073f9790aebSLuigi Rizzo 1074f9790aebSLuigi Rizzo *na = NA(ifp); 1075f9790aebSLuigi Rizzo gna = (struct netmap_generic_adapter*)NA(ifp); 1076f9790aebSLuigi Rizzo gna->prev = prev_na; /* save old na */ 1077f9790aebSLuigi Rizzo if (prev_na != NULL) { 1078f9790aebSLuigi Rizzo ifunit_ref(ifp->if_xname); 1079f9790aebSLuigi Rizzo // XXX add a refcount ? 1080f9790aebSLuigi Rizzo netmap_adapter_get(prev_na); 1081f9790aebSLuigi Rizzo } 1082*17885a7bSLuigi Rizzo ND("Created generic NA %p (prev %p)", gna, gna->prev); 1083f9790aebSLuigi Rizzo 1084f9790aebSLuigi Rizzo return 0; 1085f9790aebSLuigi Rizzo } 1086f9790aebSLuigi Rizzo 1087f9790aebSLuigi Rizzo 108868b8534bSLuigi Rizzo /* 1089ce3ee1e7SLuigi Rizzo * MUST BE CALLED UNDER NMG_LOCK() 1090ce3ee1e7SLuigi Rizzo * 109168b8534bSLuigi Rizzo * get a refcounted reference to an interface. 1092ce3ee1e7SLuigi Rizzo * This is always called in the execution of an ioctl(). 1093ce3ee1e7SLuigi Rizzo * 109468b8534bSLuigi Rizzo * Return ENXIO if the interface does not exist, EINVAL if netmap 109568b8534bSLuigi Rizzo * is not supported by the interface. 109668b8534bSLuigi Rizzo * If successful, hold a reference. 1097f18be576SLuigi Rizzo * 1098ce3ee1e7SLuigi Rizzo * When the NIC is attached to a bridge, reference is managed 1099f18be576SLuigi Rizzo * at na->na_bdg_refcount using ADD/DROP_BDG_REF() as well as 1100f18be576SLuigi Rizzo * virtual ports. Hence, on the final DROP_BDG_REF(), the NIC 1101f18be576SLuigi Rizzo * is detached from the bridge, then ifp's refcount is dropped (this 1102f18be576SLuigi Rizzo * is equivalent to that ifp is destroyed in case of virtual ports. 1103f18be576SLuigi Rizzo * 1104f18be576SLuigi Rizzo * This function uses if_rele() when we want to prevent the NIC from 1105f18be576SLuigi Rizzo * being detached from the bridge in error handling. But once refcount 1106f18be576SLuigi Rizzo * is acquired by this function, it must be released using nm_if_rele(). 110768b8534bSLuigi Rizzo */ 1108f9790aebSLuigi Rizzo int 1109f9790aebSLuigi Rizzo netmap_get_na(struct nmreq *nmr, struct netmap_adapter **na, int create) 111068b8534bSLuigi Rizzo { 1111f9790aebSLuigi Rizzo struct ifnet *ifp; 1112f9790aebSLuigi Rizzo int error = 0; 1113f9790aebSLuigi Rizzo struct netmap_adapter *ret; 1114f9790aebSLuigi Rizzo 1115f9790aebSLuigi Rizzo *na = NULL; /* default return value */ 1116f196ce38SLuigi Rizzo 1117ce3ee1e7SLuigi Rizzo /* first try to see if this is a bridge port. */ 1118ce3ee1e7SLuigi Rizzo NMG_LOCK_ASSERT(); 1119ce3ee1e7SLuigi Rizzo 1120f9790aebSLuigi Rizzo error = netmap_get_bdg_na(nmr, na, create); 1121f9790aebSLuigi Rizzo if (error || *na != NULL) /* valid match in netmap_get_bdg_na() */ 1122f9790aebSLuigi Rizzo return error; 1123ce3ee1e7SLuigi Rizzo 1124f9790aebSLuigi Rizzo ifp = ifunit_ref(nmr->nr_name); 1125f9790aebSLuigi Rizzo if (ifp == NULL) { 1126ce3ee1e7SLuigi Rizzo return ENXIO; 1127f196ce38SLuigi Rizzo } 1128ce3ee1e7SLuigi Rizzo 1129f9790aebSLuigi Rizzo error = netmap_get_hw_na(ifp, &ret); 1130f9790aebSLuigi Rizzo if (error) 1131f9790aebSLuigi Rizzo goto out; 1132f18be576SLuigi Rizzo 1133f9790aebSLuigi Rizzo if (ret != NULL) { 1134f9790aebSLuigi Rizzo /* Users cannot use the NIC attached to a bridge directly */ 1135f9790aebSLuigi Rizzo if (NETMAP_OWNED_BY_KERN(ret)) { 1136f9790aebSLuigi Rizzo error = EINVAL; 1137f9790aebSLuigi Rizzo goto out; 1138ce3ee1e7SLuigi Rizzo } 1139f9790aebSLuigi Rizzo error = 0; 1140f9790aebSLuigi Rizzo *na = ret; 1141f9790aebSLuigi Rizzo netmap_adapter_get(ret); 1142f9790aebSLuigi Rizzo } 1143f9790aebSLuigi Rizzo out: 1144f9790aebSLuigi Rizzo if_rele(ifp); 1145f18be576SLuigi Rizzo 11465ab0d24dSLuigi Rizzo return error; 11475ab0d24dSLuigi Rizzo } 1148ce3ee1e7SLuigi Rizzo 1149ce3ee1e7SLuigi Rizzo 1150f9790aebSLuigi Rizzo /* 1151f9790aebSLuigi Rizzo * validate parameters on entry for *_txsync() 1152f9790aebSLuigi Rizzo * Returns ring->cur if ok, or something >= kring->nkr_num_slots 1153*17885a7bSLuigi Rizzo * in case of error. 1154f9790aebSLuigi Rizzo * 1155*17885a7bSLuigi Rizzo * rhead, rcur and rtail=hwtail are stored from previous round. 1156*17885a7bSLuigi Rizzo * hwcur is the next packet to send to the ring. 1157f9790aebSLuigi Rizzo * 1158*17885a7bSLuigi Rizzo * We want 1159*17885a7bSLuigi Rizzo * hwcur <= *rhead <= head <= cur <= tail = *rtail <= hwtail 1160f9790aebSLuigi Rizzo * 1161*17885a7bSLuigi Rizzo * hwcur, rhead, rtail and hwtail are reliable 1162f9790aebSLuigi Rizzo */ 1163f9790aebSLuigi Rizzo u_int 1164*17885a7bSLuigi Rizzo nm_txsync_prologue(struct netmap_kring *kring) 1165f9790aebSLuigi Rizzo { 1166f9790aebSLuigi Rizzo struct netmap_ring *ring = kring->ring; 1167*17885a7bSLuigi Rizzo u_int head = ring->head; /* read only once */ 1168f9790aebSLuigi Rizzo u_int cur = ring->cur; /* read only once */ 1169f9790aebSLuigi Rizzo u_int n = kring->nkr_num_slots; 1170ce3ee1e7SLuigi Rizzo 1171*17885a7bSLuigi Rizzo ND(5, "%s kcur %d ktail %d head %d cur %d tail %d", 1172*17885a7bSLuigi Rizzo kring->name, 1173*17885a7bSLuigi Rizzo kring->nr_hwcur, kring->nr_hwtail, 1174*17885a7bSLuigi Rizzo ring->head, ring->cur, ring->tail); 1175*17885a7bSLuigi Rizzo #if 1 /* kernel sanity checks; but we can trust the kring. */ 1176*17885a7bSLuigi Rizzo if (kring->nr_hwcur >= n || kring->rhead >= n || 1177*17885a7bSLuigi Rizzo kring->rtail >= n || kring->nr_hwtail >= n) 1178f9790aebSLuigi Rizzo goto error; 1179f9790aebSLuigi Rizzo #endif /* kernel sanity checks */ 1180*17885a7bSLuigi Rizzo /* 1181*17885a7bSLuigi Rizzo * user sanity checks. We only use 'cur', 1182*17885a7bSLuigi Rizzo * A, B, ... are possible positions for cur: 1183*17885a7bSLuigi Rizzo * 1184*17885a7bSLuigi Rizzo * 0 A cur B tail C n-1 1185*17885a7bSLuigi Rizzo * 0 D tail E cur F n-1 1186*17885a7bSLuigi Rizzo * 1187*17885a7bSLuigi Rizzo * B, F, D are valid. A, C, E are wrong 1188*17885a7bSLuigi Rizzo */ 1189*17885a7bSLuigi Rizzo if (kring->rtail >= kring->rhead) { 1190*17885a7bSLuigi Rizzo /* want rhead <= head <= rtail */ 1191*17885a7bSLuigi Rizzo if (head < kring->rhead || head > kring->rtail) 1192f9790aebSLuigi Rizzo goto error; 1193*17885a7bSLuigi Rizzo /* and also head <= cur <= rtail */ 1194*17885a7bSLuigi Rizzo if (cur < head || cur > kring->rtail) 1195f9790aebSLuigi Rizzo goto error; 1196*17885a7bSLuigi Rizzo } else { /* here rtail < rhead */ 1197*17885a7bSLuigi Rizzo /* we need head outside rtail .. rhead */ 1198*17885a7bSLuigi Rizzo if (head > kring->rtail && head < kring->rhead) 1199*17885a7bSLuigi Rizzo goto error; 1200*17885a7bSLuigi Rizzo 1201*17885a7bSLuigi Rizzo /* two cases now: head <= rtail or head >= rhead */ 1202*17885a7bSLuigi Rizzo if (head <= kring->rtail) { 1203*17885a7bSLuigi Rizzo /* want head <= cur <= rtail */ 1204*17885a7bSLuigi Rizzo if (cur < head || cur > kring->rtail) 1205*17885a7bSLuigi Rizzo goto error; 1206*17885a7bSLuigi Rizzo } else { /* head >= rhead */ 1207*17885a7bSLuigi Rizzo /* cur must be outside rtail..head */ 1208*17885a7bSLuigi Rizzo if (cur > kring->rtail && cur < head) 1209*17885a7bSLuigi Rizzo goto error; 1210f18be576SLuigi Rizzo } 1211f9790aebSLuigi Rizzo } 1212*17885a7bSLuigi Rizzo if (ring->tail != kring->rtail) { 1213*17885a7bSLuigi Rizzo RD(5, "tail overwritten was %d need %d", 1214*17885a7bSLuigi Rizzo ring->tail, kring->rtail); 1215*17885a7bSLuigi Rizzo ring->tail = kring->rtail; 1216*17885a7bSLuigi Rizzo } 1217*17885a7bSLuigi Rizzo kring->rhead = head; 1218*17885a7bSLuigi Rizzo kring->rcur = cur; 1219*17885a7bSLuigi Rizzo return head; 1220f9790aebSLuigi Rizzo 1221f9790aebSLuigi Rizzo error: 1222*17885a7bSLuigi Rizzo RD(5, "%s kring error: hwcur %d rcur %d hwtail %d cur %d tail %d", 1223*17885a7bSLuigi Rizzo kring->name, 1224f9790aebSLuigi Rizzo kring->nr_hwcur, 1225*17885a7bSLuigi Rizzo kring->rcur, kring->nr_hwtail, 1226*17885a7bSLuigi Rizzo cur, ring->tail); 1227f9790aebSLuigi Rizzo return n; 122868b8534bSLuigi Rizzo } 122968b8534bSLuigi Rizzo 123068b8534bSLuigi Rizzo 123168b8534bSLuigi Rizzo /* 1232f9790aebSLuigi Rizzo * validate parameters on entry for *_rxsync() 1233*17885a7bSLuigi Rizzo * Returns ring->head if ok, kring->nkr_num_slots on error. 1234f9790aebSLuigi Rizzo * 1235*17885a7bSLuigi Rizzo * For a valid configuration, 1236*17885a7bSLuigi Rizzo * hwcur <= head <= cur <= tail <= hwtail 1237f9790aebSLuigi Rizzo * 1238*17885a7bSLuigi Rizzo * We only consider head and cur. 1239*17885a7bSLuigi Rizzo * hwcur and hwtail are reliable. 1240f9790aebSLuigi Rizzo * 1241f9790aebSLuigi Rizzo */ 1242f9790aebSLuigi Rizzo u_int 1243*17885a7bSLuigi Rizzo nm_rxsync_prologue(struct netmap_kring *kring) 1244f9790aebSLuigi Rizzo { 1245f9790aebSLuigi Rizzo struct netmap_ring *ring = kring->ring; 1246*17885a7bSLuigi Rizzo uint32_t const n = kring->nkr_num_slots; 1247*17885a7bSLuigi Rizzo uint32_t head, cur; 1248f9790aebSLuigi Rizzo 1249*17885a7bSLuigi Rizzo ND("%s kc %d kt %d h %d c %d t %d", 1250*17885a7bSLuigi Rizzo kring->name, 1251*17885a7bSLuigi Rizzo kring->nr_hwcur, kring->nr_hwtail, 1252*17885a7bSLuigi Rizzo ring->head, ring->cur, ring->tail); 1253*17885a7bSLuigi Rizzo /* 1254*17885a7bSLuigi Rizzo * Before storing the new values, we should check they do not 1255*17885a7bSLuigi Rizzo * move backwards. However: 1256*17885a7bSLuigi Rizzo * - head is not an issue because the previous value is hwcur; 1257*17885a7bSLuigi Rizzo * - cur could in principle go back, however it does not matter 1258*17885a7bSLuigi Rizzo * because we are processing a brand new rxsync() 1259*17885a7bSLuigi Rizzo */ 1260*17885a7bSLuigi Rizzo cur = kring->rcur = ring->cur; /* read only once */ 1261*17885a7bSLuigi Rizzo head = kring->rhead = ring->head; /* read only once */ 1262f9790aebSLuigi Rizzo #if 1 /* kernel sanity checks */ 1263*17885a7bSLuigi Rizzo if (kring->nr_hwcur >= n || kring->nr_hwtail >= n) 1264f9790aebSLuigi Rizzo goto error; 1265f9790aebSLuigi Rizzo #endif /* kernel sanity checks */ 1266f9790aebSLuigi Rizzo /* user sanity checks */ 1267*17885a7bSLuigi Rizzo if (kring->nr_hwtail >= kring->nr_hwcur) { 1268*17885a7bSLuigi Rizzo /* want hwcur <= rhead <= hwtail */ 1269*17885a7bSLuigi Rizzo if (head < kring->nr_hwcur || head > kring->nr_hwtail) 1270f9790aebSLuigi Rizzo goto error; 1271*17885a7bSLuigi Rizzo /* and also rhead <= rcur <= hwtail */ 1272*17885a7bSLuigi Rizzo if (cur < head || cur > kring->nr_hwtail) 1273f9790aebSLuigi Rizzo goto error; 1274f9790aebSLuigi Rizzo } else { 1275*17885a7bSLuigi Rizzo /* we need rhead outside hwtail..hwcur */ 1276*17885a7bSLuigi Rizzo if (head < kring->nr_hwcur && head > kring->nr_hwtail) 1277f9790aebSLuigi Rizzo goto error; 1278*17885a7bSLuigi Rizzo /* two cases now: head <= hwtail or head >= hwcur */ 1279*17885a7bSLuigi Rizzo if (head <= kring->nr_hwtail) { 1280*17885a7bSLuigi Rizzo /* want head <= cur <= hwtail */ 1281*17885a7bSLuigi Rizzo if (cur < head || cur > kring->nr_hwtail) 1282f9790aebSLuigi Rizzo goto error; 1283*17885a7bSLuigi Rizzo } else { 1284*17885a7bSLuigi Rizzo /* cur must be outside hwtail..head */ 1285*17885a7bSLuigi Rizzo if (cur < head && cur > kring->nr_hwtail) 1286f9790aebSLuigi Rizzo goto error; 1287f9790aebSLuigi Rizzo } 1288f9790aebSLuigi Rizzo } 1289*17885a7bSLuigi Rizzo if (ring->tail != kring->rtail) { 1290*17885a7bSLuigi Rizzo RD(5, "%s tail overwritten was %d need %d", 1291*17885a7bSLuigi Rizzo kring->name, 1292*17885a7bSLuigi Rizzo ring->tail, kring->rtail); 1293*17885a7bSLuigi Rizzo ring->tail = kring->rtail; 1294*17885a7bSLuigi Rizzo } 1295*17885a7bSLuigi Rizzo return head; 1296f9790aebSLuigi Rizzo 1297f9790aebSLuigi Rizzo error: 1298*17885a7bSLuigi Rizzo RD(5, "kring error: hwcur %d rcur %d hwtail %d head %d cur %d tail %d", 1299f9790aebSLuigi Rizzo kring->nr_hwcur, 1300*17885a7bSLuigi Rizzo kring->rcur, kring->nr_hwtail, 1301*17885a7bSLuigi Rizzo kring->rhead, kring->rcur, ring->tail); 1302f9790aebSLuigi Rizzo return n; 1303f9790aebSLuigi Rizzo } 1304f9790aebSLuigi Rizzo 1305*17885a7bSLuigi Rizzo 1306f9790aebSLuigi Rizzo /* 130768b8534bSLuigi Rizzo * Error routine called when txsync/rxsync detects an error. 1308*17885a7bSLuigi Rizzo * Can't do much more than resetting head =cur = hwcur, tail = hwtail 130968b8534bSLuigi Rizzo * Return 1 on reinit. 1310506cc70cSLuigi Rizzo * 1311506cc70cSLuigi Rizzo * This routine is only called by the upper half of the kernel. 1312506cc70cSLuigi Rizzo * It only reads hwcur (which is changed only by the upper half, too) 1313*17885a7bSLuigi Rizzo * and hwtail (which may be changed by the lower half, but only on 1314506cc70cSLuigi Rizzo * a tx ring and only to increase it, so any error will be recovered 1315506cc70cSLuigi Rizzo * on the next call). For the above, we don't strictly need to call 1316506cc70cSLuigi Rizzo * it under lock. 131768b8534bSLuigi Rizzo */ 131868b8534bSLuigi Rizzo int 131968b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring) 132068b8534bSLuigi Rizzo { 132168b8534bSLuigi Rizzo struct netmap_ring *ring = kring->ring; 132268b8534bSLuigi Rizzo u_int i, lim = kring->nkr_num_slots - 1; 132368b8534bSLuigi Rizzo int errors = 0; 132468b8534bSLuigi Rizzo 1325ce3ee1e7SLuigi Rizzo // XXX KASSERT nm_kr_tryget 1326f9790aebSLuigi Rizzo RD(10, "called for %s", NM_IFPNAME(kring->na->ifp)); 1327*17885a7bSLuigi Rizzo // XXX probably wrong to trust userspace 1328*17885a7bSLuigi Rizzo kring->rhead = ring->head; 1329*17885a7bSLuigi Rizzo kring->rcur = ring->cur; 1330*17885a7bSLuigi Rizzo kring->rtail = ring->tail; 1331*17885a7bSLuigi Rizzo 133268b8534bSLuigi Rizzo if (ring->cur > lim) 133368b8534bSLuigi Rizzo errors++; 1334*17885a7bSLuigi Rizzo if (ring->head > lim) 1335*17885a7bSLuigi Rizzo errors++; 1336*17885a7bSLuigi Rizzo if (ring->tail > lim) 1337*17885a7bSLuigi Rizzo errors++; 133868b8534bSLuigi Rizzo for (i = 0; i <= lim; i++) { 133968b8534bSLuigi Rizzo u_int idx = ring->slot[i].buf_idx; 134068b8534bSLuigi Rizzo u_int len = ring->slot[i].len; 134168b8534bSLuigi Rizzo if (idx < 2 || idx >= netmap_total_buffers) { 1342*17885a7bSLuigi Rizzo RD(5, "bad index at slot %d idx %d len %d ", i, idx, len); 134368b8534bSLuigi Rizzo ring->slot[i].buf_idx = 0; 134468b8534bSLuigi Rizzo ring->slot[i].len = 0; 1345ce3ee1e7SLuigi Rizzo } else if (len > NETMAP_BDG_BUF_SIZE(kring->na->nm_mem)) { 134668b8534bSLuigi Rizzo ring->slot[i].len = 0; 1347*17885a7bSLuigi Rizzo RD(5, "bad len at slot %d idx %d len %d", i, idx, len); 134868b8534bSLuigi Rizzo } 134968b8534bSLuigi Rizzo } 135068b8534bSLuigi Rizzo if (errors) { 13518241616dSLuigi Rizzo RD(10, "total %d errors", errors); 1352*17885a7bSLuigi Rizzo RD(10, "%s reinit, cur %d -> %d tail %d -> %d", 1353*17885a7bSLuigi Rizzo kring->name, 135468b8534bSLuigi Rizzo ring->cur, kring->nr_hwcur, 1355*17885a7bSLuigi Rizzo ring->tail, kring->nr_hwtail); 1356*17885a7bSLuigi Rizzo ring->head = kring->rhead = kring->nr_hwcur; 1357*17885a7bSLuigi Rizzo ring->cur = kring->rcur = kring->nr_hwcur; 1358*17885a7bSLuigi Rizzo ring->tail = kring->rtail = kring->nr_hwtail; 135968b8534bSLuigi Rizzo } 136068b8534bSLuigi Rizzo return (errors ? 1 : 0); 136168b8534bSLuigi Rizzo } 136268b8534bSLuigi Rizzo 136368b8534bSLuigi Rizzo 136468b8534bSLuigi Rizzo /* 136568b8534bSLuigi Rizzo * Set the ring ID. For devices with a single queue, a request 136668b8534bSLuigi Rizzo * for all rings is the same as a single ring. 136768b8534bSLuigi Rizzo */ 136868b8534bSLuigi Rizzo static int 136968b8534bSLuigi Rizzo netmap_set_ringid(struct netmap_priv_d *priv, u_int ringid) 137068b8534bSLuigi Rizzo { 1371f9790aebSLuigi Rizzo struct netmap_adapter *na = priv->np_na; 1372f9790aebSLuigi Rizzo struct ifnet *ifp = na->ifp; 137368b8534bSLuigi Rizzo u_int i = ringid & NETMAP_RING_MASK; 137464ae02c3SLuigi Rizzo /* initially (np_qfirst == np_qlast) we don't want to lock */ 1375ce3ee1e7SLuigi Rizzo u_int lim = na->num_rx_rings; 137668b8534bSLuigi Rizzo 1377d76bf4ffSLuigi Rizzo if (na->num_tx_rings > lim) 1378d76bf4ffSLuigi Rizzo lim = na->num_tx_rings; 137964ae02c3SLuigi Rizzo if ( (ringid & NETMAP_HW_RING) && i >= lim) { 138068b8534bSLuigi Rizzo D("invalid ring id %d", i); 138168b8534bSLuigi Rizzo return (EINVAL); 138268b8534bSLuigi Rizzo } 138368b8534bSLuigi Rizzo priv->np_ringid = ringid; 138468b8534bSLuigi Rizzo if (ringid & NETMAP_SW_RING) { 138564ae02c3SLuigi Rizzo priv->np_qfirst = NETMAP_SW_RING; 138664ae02c3SLuigi Rizzo priv->np_qlast = 0; 138768b8534bSLuigi Rizzo } else if (ringid & NETMAP_HW_RING) { 138868b8534bSLuigi Rizzo priv->np_qfirst = i; 138968b8534bSLuigi Rizzo priv->np_qlast = i + 1; 139068b8534bSLuigi Rizzo } else { 139168b8534bSLuigi Rizzo priv->np_qfirst = 0; 139264ae02c3SLuigi Rizzo priv->np_qlast = NETMAP_HW_RING ; 139368b8534bSLuigi Rizzo } 139468b8534bSLuigi Rizzo priv->np_txpoll = (ringid & NETMAP_NO_TX_POLL) ? 0 : 1; 1395ae10d1afSLuigi Rizzo if (netmap_verbose) { 139668b8534bSLuigi Rizzo if (ringid & NETMAP_SW_RING) 1397f9790aebSLuigi Rizzo D("ringid %s set to SW RING", NM_IFPNAME(ifp)); 139868b8534bSLuigi Rizzo else if (ringid & NETMAP_HW_RING) 1399f9790aebSLuigi Rizzo D("ringid %s set to HW RING %d", NM_IFPNAME(ifp), 140068b8534bSLuigi Rizzo priv->np_qfirst); 140168b8534bSLuigi Rizzo else 1402f9790aebSLuigi Rizzo D("ringid %s set to all %d HW RINGS", NM_IFPNAME(ifp), lim); 1403ae10d1afSLuigi Rizzo } 140468b8534bSLuigi Rizzo return 0; 140568b8534bSLuigi Rizzo } 140668b8534bSLuigi Rizzo 1407f18be576SLuigi Rizzo 1408f18be576SLuigi Rizzo /* 1409f18be576SLuigi Rizzo * possibly move the interface to netmap-mode. 1410f18be576SLuigi Rizzo * If success it returns a pointer to netmap_if, otherwise NULL. 1411ce3ee1e7SLuigi Rizzo * This must be called with NMG_LOCK held. 1412f18be576SLuigi Rizzo */ 1413f9790aebSLuigi Rizzo struct netmap_if * 1414f9790aebSLuigi Rizzo netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na, 1415f18be576SLuigi Rizzo uint16_t ringid, int *err) 1416f18be576SLuigi Rizzo { 1417f9790aebSLuigi Rizzo struct ifnet *ifp = na->ifp; 1418f18be576SLuigi Rizzo struct netmap_if *nifp = NULL; 1419f9790aebSLuigi Rizzo int error, need_mem = 0; 1420f18be576SLuigi Rizzo 1421ce3ee1e7SLuigi Rizzo NMG_LOCK_ASSERT(); 1422f18be576SLuigi Rizzo /* ring configuration may have changed, fetch from the card */ 1423f18be576SLuigi Rizzo netmap_update_config(na); 1424f9790aebSLuigi Rizzo priv->np_na = na; /* store the reference */ 1425f18be576SLuigi Rizzo error = netmap_set_ringid(priv, ringid); 1426f18be576SLuigi Rizzo if (error) 1427f18be576SLuigi Rizzo goto out; 1428ce3ee1e7SLuigi Rizzo /* ensure allocators are ready */ 1429ce3ee1e7SLuigi Rizzo need_mem = !netmap_have_memory_locked(priv); 1430ce3ee1e7SLuigi Rizzo if (need_mem) { 1431ce3ee1e7SLuigi Rizzo error = netmap_get_memory_locked(priv); 1432ce3ee1e7SLuigi Rizzo ND("get_memory returned %d", error); 1433ce3ee1e7SLuigi Rizzo if (error) 1434ce3ee1e7SLuigi Rizzo goto out; 1435ce3ee1e7SLuigi Rizzo } 1436f9790aebSLuigi Rizzo nifp = netmap_if_new(NM_IFPNAME(ifp), na); 1437f18be576SLuigi Rizzo if (nifp == NULL) { /* allocation failed */ 1438ce3ee1e7SLuigi Rizzo /* we should drop the allocator, but only 1439ce3ee1e7SLuigi Rizzo * if we were the ones who grabbed it 1440ce3ee1e7SLuigi Rizzo */ 1441f18be576SLuigi Rizzo error = ENOMEM; 1442ce3ee1e7SLuigi Rizzo goto out; 1443ce3ee1e7SLuigi Rizzo } 1444f9790aebSLuigi Rizzo na->active_fds++; 1445ce3ee1e7SLuigi Rizzo if (ifp->if_capenable & IFCAP_NETMAP) { 1446f18be576SLuigi Rizzo /* was already set */ 1447f18be576SLuigi Rizzo } else { 1448f18be576SLuigi Rizzo /* Otherwise set the card in netmap mode 1449f18be576SLuigi Rizzo * and make it use the shared buffers. 1450ce3ee1e7SLuigi Rizzo * 1451ce3ee1e7SLuigi Rizzo * do not core lock because the race is harmless here, 1452ce3ee1e7SLuigi Rizzo * there cannot be any traffic to netmap_transmit() 1453ce3ee1e7SLuigi Rizzo */ 1454f9790aebSLuigi Rizzo na->na_lut = na->nm_mem->pools[NETMAP_BUF_POOL].lut; 1455f9790aebSLuigi Rizzo ND("%p->na_lut == %p", na, na->na_lut); 1456f9790aebSLuigi Rizzo na->na_lut_objtotal = na->nm_mem->pools[NETMAP_BUF_POOL].objtotal; 1457f9790aebSLuigi Rizzo error = na->nm_register(na, 1); /* mode on */ 1458f18be576SLuigi Rizzo if (error) { 1459ce3ee1e7SLuigi Rizzo netmap_do_unregif(priv, nifp); 1460f18be576SLuigi Rizzo nifp = NULL; 1461f18be576SLuigi Rizzo } 1462f18be576SLuigi Rizzo } 1463f18be576SLuigi Rizzo out: 1464f18be576SLuigi Rizzo *err = error; 1465f9790aebSLuigi Rizzo if (error) { 1466f9790aebSLuigi Rizzo priv->np_na = NULL; 1467f9790aebSLuigi Rizzo if (need_mem) 1468f9790aebSLuigi Rizzo netmap_drop_memory_locked(priv); 1469f9790aebSLuigi Rizzo } 1470ce3ee1e7SLuigi Rizzo if (nifp != NULL) { 1471ce3ee1e7SLuigi Rizzo /* 1472ce3ee1e7SLuigi Rizzo * advertise that the interface is ready bt setting ni_nifp. 1473ce3ee1e7SLuigi Rizzo * The barrier is needed because readers (poll and *SYNC) 1474ce3ee1e7SLuigi Rizzo * check for priv->np_nifp != NULL without locking 1475ce3ee1e7SLuigi Rizzo */ 1476ce3ee1e7SLuigi Rizzo wmb(); /* make sure previous writes are visible to all CPUs */ 1477ce3ee1e7SLuigi Rizzo priv->np_nifp = nifp; 1478ce3ee1e7SLuigi Rizzo } 1479f18be576SLuigi Rizzo return nifp; 1480f18be576SLuigi Rizzo } 1481f18be576SLuigi Rizzo 1482f18be576SLuigi Rizzo 1483f18be576SLuigi Rizzo 148468b8534bSLuigi Rizzo /* 148568b8534bSLuigi Rizzo * ioctl(2) support for the "netmap" device. 148668b8534bSLuigi Rizzo * 148768b8534bSLuigi Rizzo * Following a list of accepted commands: 148868b8534bSLuigi Rizzo * - NIOCGINFO 148968b8534bSLuigi Rizzo * - SIOCGIFADDR just for convenience 149068b8534bSLuigi Rizzo * - NIOCREGIF 149168b8534bSLuigi Rizzo * - NIOCTXSYNC 149268b8534bSLuigi Rizzo * - NIOCRXSYNC 149368b8534bSLuigi Rizzo * 149468b8534bSLuigi Rizzo * Return 0 on success, errno otherwise. 149568b8534bSLuigi Rizzo */ 1496f9790aebSLuigi Rizzo int 14970b8ed8e0SLuigi Rizzo netmap_ioctl(struct cdev *dev, u_long cmd, caddr_t data, 14980b8ed8e0SLuigi Rizzo int fflag, struct thread *td) 149968b8534bSLuigi Rizzo { 150068b8534bSLuigi Rizzo struct netmap_priv_d *priv = NULL; 1501ce3ee1e7SLuigi Rizzo struct ifnet *ifp = NULL; 150268b8534bSLuigi Rizzo struct nmreq *nmr = (struct nmreq *) data; 1503ce3ee1e7SLuigi Rizzo struct netmap_adapter *na = NULL; 150468b8534bSLuigi Rizzo int error; 150564ae02c3SLuigi Rizzo u_int i, lim; 150668b8534bSLuigi Rizzo struct netmap_if *nifp; 1507ce3ee1e7SLuigi Rizzo struct netmap_kring *krings; 150868b8534bSLuigi Rizzo 15090b8ed8e0SLuigi Rizzo (void)dev; /* UNUSED */ 15100b8ed8e0SLuigi Rizzo (void)fflag; /* UNUSED */ 1511f196ce38SLuigi Rizzo #ifdef linux 1512f196ce38SLuigi Rizzo #define devfs_get_cdevpriv(pp) \ 1513f196ce38SLuigi Rizzo ({ *(struct netmap_priv_d **)pp = ((struct file *)td)->private_data; \ 1514f196ce38SLuigi Rizzo (*pp ? 0 : ENOENT); }) 1515f196ce38SLuigi Rizzo 1516f196ce38SLuigi Rizzo /* devfs_set_cdevpriv cannot fail on linux */ 1517f196ce38SLuigi Rizzo #define devfs_set_cdevpriv(p, fn) \ 1518f196ce38SLuigi Rizzo ({ ((struct file *)td)->private_data = p; (p ? 0 : EINVAL); }) 1519f196ce38SLuigi Rizzo 1520f196ce38SLuigi Rizzo 1521f196ce38SLuigi Rizzo #define devfs_clear_cdevpriv() do { \ 1522f196ce38SLuigi Rizzo netmap_dtor(priv); ((struct file *)td)->private_data = 0; \ 1523f196ce38SLuigi Rizzo } while (0) 1524f196ce38SLuigi Rizzo #endif /* linux */ 1525f196ce38SLuigi Rizzo 1526*17885a7bSLuigi Rizzo if (cmd == NIOCGINFO || cmd == NIOCREGIF) { 1527*17885a7bSLuigi Rizzo /* truncate name */ 1528*17885a7bSLuigi Rizzo nmr->nr_name[sizeof(nmr->nr_name) - 1] = '\0'; 1529*17885a7bSLuigi Rizzo if (nmr->nr_version != NETMAP_API) { 1530*17885a7bSLuigi Rizzo D("API mismatch for %s got %d need %d", 1531*17885a7bSLuigi Rizzo nmr->nr_name, 1532*17885a7bSLuigi Rizzo nmr->nr_version, NETMAP_API); 1533*17885a7bSLuigi Rizzo nmr->nr_version = NETMAP_API; 1534*17885a7bSLuigi Rizzo return EINVAL; 1535*17885a7bSLuigi Rizzo } 1536*17885a7bSLuigi Rizzo } 1537506cc70cSLuigi Rizzo CURVNET_SET(TD_TO_VNET(td)); 1538506cc70cSLuigi Rizzo 153968b8534bSLuigi Rizzo error = devfs_get_cdevpriv((void **)&priv); 15408241616dSLuigi Rizzo if (error) { 1541506cc70cSLuigi Rizzo CURVNET_RESTORE(); 15428241616dSLuigi Rizzo /* XXX ENOENT should be impossible, since the priv 15438241616dSLuigi Rizzo * is now created in the open */ 15448241616dSLuigi Rizzo return (error == ENOENT ? ENXIO : error); 1545506cc70cSLuigi Rizzo } 154668b8534bSLuigi Rizzo 154768b8534bSLuigi Rizzo switch (cmd) { 154868b8534bSLuigi Rizzo case NIOCGINFO: /* return capabilities etc */ 1549f18be576SLuigi Rizzo if (nmr->nr_cmd == NETMAP_BDG_LIST) { 1550f18be576SLuigi Rizzo error = netmap_bdg_ctl(nmr, NULL); 1551f18be576SLuigi Rizzo break; 1552f18be576SLuigi Rizzo } 1553ce3ee1e7SLuigi Rizzo 1554ce3ee1e7SLuigi Rizzo NMG_LOCK(); 1555ce3ee1e7SLuigi Rizzo do { 1556ce3ee1e7SLuigi Rizzo /* memsize is always valid */ 1557ce3ee1e7SLuigi Rizzo struct netmap_mem_d *nmd = &nm_mem; 1558ce3ee1e7SLuigi Rizzo u_int memflags; 1559ce3ee1e7SLuigi Rizzo 1560ce3ee1e7SLuigi Rizzo if (nmr->nr_name[0] != '\0') { 1561ce3ee1e7SLuigi Rizzo /* get a refcount */ 1562f9790aebSLuigi Rizzo error = netmap_get_na(nmr, &na, 1 /* create */); 15638241616dSLuigi Rizzo if (error) 15648241616dSLuigi Rizzo break; 1565f9790aebSLuigi Rizzo nmd = na->nm_mem; /* get memory allocator */ 1566ce3ee1e7SLuigi Rizzo } 1567ce3ee1e7SLuigi Rizzo 1568ce3ee1e7SLuigi Rizzo error = netmap_mem_get_info(nmd, &nmr->nr_memsize, &memflags); 1569ce3ee1e7SLuigi Rizzo if (error) 1570ce3ee1e7SLuigi Rizzo break; 1571ce3ee1e7SLuigi Rizzo if (na == NULL) /* only memory info */ 1572ce3ee1e7SLuigi Rizzo break; 15738241616dSLuigi Rizzo nmr->nr_offset = 0; 15748241616dSLuigi Rizzo nmr->nr_rx_slots = nmr->nr_tx_slots = 0; 1575ae10d1afSLuigi Rizzo netmap_update_config(na); 1576d76bf4ffSLuigi Rizzo nmr->nr_rx_rings = na->num_rx_rings; 1577d76bf4ffSLuigi Rizzo nmr->nr_tx_rings = na->num_tx_rings; 157864ae02c3SLuigi Rizzo nmr->nr_rx_slots = na->num_rx_desc; 157964ae02c3SLuigi Rizzo nmr->nr_tx_slots = na->num_tx_desc; 1580ce3ee1e7SLuigi Rizzo if (memflags & NETMAP_MEM_PRIVATE) 1581ce3ee1e7SLuigi Rizzo nmr->nr_ringid |= NETMAP_PRIV_MEM; 1582f9790aebSLuigi Rizzo netmap_adapter_put(na); 1583ce3ee1e7SLuigi Rizzo } while (0); 1584ce3ee1e7SLuigi Rizzo NMG_UNLOCK(); 158568b8534bSLuigi Rizzo break; 158668b8534bSLuigi Rizzo 158768b8534bSLuigi Rizzo case NIOCREGIF: 1588f18be576SLuigi Rizzo /* possibly attach/detach NIC and VALE switch */ 1589f18be576SLuigi Rizzo i = nmr->nr_cmd; 1590f9790aebSLuigi Rizzo if (i == NETMAP_BDG_ATTACH || i == NETMAP_BDG_DETACH 1591f9790aebSLuigi Rizzo || i == NETMAP_BDG_OFFSET) { 1592f18be576SLuigi Rizzo error = netmap_bdg_ctl(nmr, NULL); 1593f18be576SLuigi Rizzo break; 1594f18be576SLuigi Rizzo } else if (i != 0) { 1595f18be576SLuigi Rizzo D("nr_cmd must be 0 not %d", i); 1596f18be576SLuigi Rizzo error = EINVAL; 1597f18be576SLuigi Rizzo break; 1598f18be576SLuigi Rizzo } 1599f18be576SLuigi Rizzo 16008241616dSLuigi Rizzo /* protect access to priv from concurrent NIOCREGIF */ 1601ce3ee1e7SLuigi Rizzo NMG_LOCK(); 1602ce3ee1e7SLuigi Rizzo do { 1603ce3ee1e7SLuigi Rizzo u_int memflags; 1604ce3ee1e7SLuigi Rizzo 1605f9790aebSLuigi Rizzo if (priv->np_na != NULL) { /* thread already registered */ 1606506cc70cSLuigi Rizzo error = netmap_set_ringid(priv, nmr->nr_ringid); 1607506cc70cSLuigi Rizzo break; 1608506cc70cSLuigi Rizzo } 160968b8534bSLuigi Rizzo /* find the interface and a reference */ 1610f9790aebSLuigi Rizzo error = netmap_get_na(nmr, &na, 1 /* create */); /* keep reference */ 161168b8534bSLuigi Rizzo if (error) 1612ce3ee1e7SLuigi Rizzo break; 1613f9790aebSLuigi Rizzo ifp = na->ifp; 1614f9790aebSLuigi Rizzo if (NETMAP_OWNED_BY_KERN(na)) { 1615f9790aebSLuigi Rizzo netmap_adapter_put(na); 1616ce3ee1e7SLuigi Rizzo error = EBUSY; 1617ce3ee1e7SLuigi Rizzo break; 1618f196ce38SLuigi Rizzo } 1619f9790aebSLuigi Rizzo nifp = netmap_do_regif(priv, na, nmr->nr_ringid, &error); 1620f18be576SLuigi Rizzo if (!nifp) { /* reg. failed, release priv and ref */ 1621f9790aebSLuigi Rizzo netmap_adapter_put(na); 16228241616dSLuigi Rizzo priv->np_nifp = NULL; 1623ce3ee1e7SLuigi Rizzo break; 162468b8534bSLuigi Rizzo } 162568b8534bSLuigi Rizzo 162668b8534bSLuigi Rizzo /* return the offset of the netmap_if object */ 1627d76bf4ffSLuigi Rizzo nmr->nr_rx_rings = na->num_rx_rings; 1628d76bf4ffSLuigi Rizzo nmr->nr_tx_rings = na->num_tx_rings; 162964ae02c3SLuigi Rizzo nmr->nr_rx_slots = na->num_rx_desc; 163064ae02c3SLuigi Rizzo nmr->nr_tx_slots = na->num_tx_desc; 1631ce3ee1e7SLuigi Rizzo error = netmap_mem_get_info(na->nm_mem, &nmr->nr_memsize, &memflags); 1632ce3ee1e7SLuigi Rizzo if (error) { 1633f9790aebSLuigi Rizzo netmap_adapter_put(na); 1634ce3ee1e7SLuigi Rizzo break; 1635ce3ee1e7SLuigi Rizzo } 1636ce3ee1e7SLuigi Rizzo if (memflags & NETMAP_MEM_PRIVATE) { 1637ce3ee1e7SLuigi Rizzo nmr->nr_ringid |= NETMAP_PRIV_MEM; 16383d819cb6SLuigi Rizzo *(uint32_t *)(uintptr_t)&nifp->ni_flags |= NI_PRIV_MEM; 1639ce3ee1e7SLuigi Rizzo } 1640ce3ee1e7SLuigi Rizzo nmr->nr_offset = netmap_mem_if_offset(na->nm_mem, nifp); 1641ce3ee1e7SLuigi Rizzo } while (0); 1642ce3ee1e7SLuigi Rizzo NMG_UNLOCK(); 164368b8534bSLuigi Rizzo break; 164468b8534bSLuigi Rizzo 164568b8534bSLuigi Rizzo case NIOCTXSYNC: 164668b8534bSLuigi Rizzo case NIOCRXSYNC: 16478241616dSLuigi Rizzo nifp = priv->np_nifp; 16488241616dSLuigi Rizzo 16498241616dSLuigi Rizzo if (nifp == NULL) { 1650506cc70cSLuigi Rizzo error = ENXIO; 1651506cc70cSLuigi Rizzo break; 1652506cc70cSLuigi Rizzo } 16538241616dSLuigi Rizzo rmb(); /* make sure following reads are not from cache */ 16548241616dSLuigi Rizzo 1655f9790aebSLuigi Rizzo na = priv->np_na; /* we have a reference */ 16568241616dSLuigi Rizzo 1657f9790aebSLuigi Rizzo if (na == NULL) { 1658f9790aebSLuigi Rizzo D("Internal error: nifp != NULL && na == NULL"); 16598241616dSLuigi Rizzo error = ENXIO; 16608241616dSLuigi Rizzo break; 16618241616dSLuigi Rizzo } 16628241616dSLuigi Rizzo 1663f9790aebSLuigi Rizzo ifp = na->ifp; 1664f9790aebSLuigi Rizzo if (ifp == NULL) { 1665f9790aebSLuigi Rizzo RD(1, "the ifp is gone"); 1666f9790aebSLuigi Rizzo error = ENXIO; 1667f9790aebSLuigi Rizzo break; 1668f9790aebSLuigi Rizzo } 1669f9790aebSLuigi Rizzo 167064ae02c3SLuigi Rizzo if (priv->np_qfirst == NETMAP_SW_RING) { /* host rings */ 167168b8534bSLuigi Rizzo if (cmd == NIOCTXSYNC) 1672ce3ee1e7SLuigi Rizzo netmap_txsync_to_host(na); 167368b8534bSLuigi Rizzo else 1674ce3ee1e7SLuigi Rizzo netmap_rxsync_from_host(na, NULL, NULL); 1675506cc70cSLuigi Rizzo break; 167668b8534bSLuigi Rizzo } 167764ae02c3SLuigi Rizzo /* find the last ring to scan */ 167864ae02c3SLuigi Rizzo lim = priv->np_qlast; 167964ae02c3SLuigi Rizzo if (lim == NETMAP_HW_RING) 16803c0caf6cSLuigi Rizzo lim = (cmd == NIOCTXSYNC) ? 1681d76bf4ffSLuigi Rizzo na->num_tx_rings : na->num_rx_rings; 168268b8534bSLuigi Rizzo 1683ce3ee1e7SLuigi Rizzo krings = (cmd == NIOCTXSYNC) ? na->tx_rings : na->rx_rings; 168464ae02c3SLuigi Rizzo for (i = priv->np_qfirst; i < lim; i++) { 1685ce3ee1e7SLuigi Rizzo struct netmap_kring *kring = krings + i; 1686ce3ee1e7SLuigi Rizzo if (nm_kr_tryget(kring)) { 1687ce3ee1e7SLuigi Rizzo error = EBUSY; 1688ce3ee1e7SLuigi Rizzo goto out; 1689ce3ee1e7SLuigi Rizzo } 169068b8534bSLuigi Rizzo if (cmd == NIOCTXSYNC) { 169168b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_TXSYNC) 16923c0caf6cSLuigi Rizzo D("pre txsync ring %d cur %d hwcur %d", 169368b8534bSLuigi Rizzo i, kring->ring->cur, 169468b8534bSLuigi Rizzo kring->nr_hwcur); 1695*17885a7bSLuigi Rizzo if (nm_txsync_prologue(kring) >= kring->nkr_num_slots) { 1696*17885a7bSLuigi Rizzo netmap_ring_reinit(kring); 1697*17885a7bSLuigi Rizzo } else { 1698f9790aebSLuigi Rizzo na->nm_txsync(na, i, NAF_FORCE_RECLAIM); 1699*17885a7bSLuigi Rizzo } 170068b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_TXSYNC) 17013c0caf6cSLuigi Rizzo D("post txsync ring %d cur %d hwcur %d", 170268b8534bSLuigi Rizzo i, kring->ring->cur, 170368b8534bSLuigi Rizzo kring->nr_hwcur); 170468b8534bSLuigi Rizzo } else { 1705f9790aebSLuigi Rizzo na->nm_rxsync(na, i, NAF_FORCE_READ); 170668b8534bSLuigi Rizzo microtime(&na->rx_rings[i].ring->ts); 170768b8534bSLuigi Rizzo } 1708ce3ee1e7SLuigi Rizzo nm_kr_put(kring); 170968b8534bSLuigi Rizzo } 171068b8534bSLuigi Rizzo 171168b8534bSLuigi Rizzo break; 171268b8534bSLuigi Rizzo 1713f196ce38SLuigi Rizzo #ifdef __FreeBSD__ 171468b8534bSLuigi Rizzo case BIOCIMMEDIATE: 171568b8534bSLuigi Rizzo case BIOCGHDRCMPLT: 171668b8534bSLuigi Rizzo case BIOCSHDRCMPLT: 171768b8534bSLuigi Rizzo case BIOCSSEESENT: 171868b8534bSLuigi Rizzo D("ignore BIOCIMMEDIATE/BIOCSHDRCMPLT/BIOCSHDRCMPLT/BIOCSSEESENT"); 171968b8534bSLuigi Rizzo break; 172068b8534bSLuigi Rizzo 1721babc7c12SLuigi Rizzo default: /* allow device-specific ioctls */ 172268b8534bSLuigi Rizzo { 172368b8534bSLuigi Rizzo struct socket so; 1724ce3ee1e7SLuigi Rizzo 172568b8534bSLuigi Rizzo bzero(&so, sizeof(so)); 1726ce3ee1e7SLuigi Rizzo NMG_LOCK(); 1727f9790aebSLuigi Rizzo error = netmap_get_na(nmr, &na, 0 /* don't create */); /* keep reference */ 1728ce3ee1e7SLuigi Rizzo if (error) { 1729f9790aebSLuigi Rizzo netmap_adapter_put(na); 1730ce3ee1e7SLuigi Rizzo NMG_UNLOCK(); 173168b8534bSLuigi Rizzo break; 1732ce3ee1e7SLuigi Rizzo } 1733f9790aebSLuigi Rizzo ifp = na->ifp; 173468b8534bSLuigi Rizzo so.so_vnet = ifp->if_vnet; 173568b8534bSLuigi Rizzo // so->so_proto not null. 173668b8534bSLuigi Rizzo error = ifioctl(&so, cmd, data, td); 1737f9790aebSLuigi Rizzo netmap_adapter_put(na); 1738ce3ee1e7SLuigi Rizzo NMG_UNLOCK(); 1739babc7c12SLuigi Rizzo break; 174068b8534bSLuigi Rizzo } 1741f196ce38SLuigi Rizzo 1742f196ce38SLuigi Rizzo #else /* linux */ 1743f196ce38SLuigi Rizzo default: 1744f196ce38SLuigi Rizzo error = EOPNOTSUPP; 1745f196ce38SLuigi Rizzo #endif /* linux */ 174668b8534bSLuigi Rizzo } 1747ce3ee1e7SLuigi Rizzo out: 174868b8534bSLuigi Rizzo 1749506cc70cSLuigi Rizzo CURVNET_RESTORE(); 175068b8534bSLuigi Rizzo return (error); 175168b8534bSLuigi Rizzo } 175268b8534bSLuigi Rizzo 175368b8534bSLuigi Rizzo 175468b8534bSLuigi Rizzo /* 175568b8534bSLuigi Rizzo * select(2) and poll(2) handlers for the "netmap" device. 175668b8534bSLuigi Rizzo * 175768b8534bSLuigi Rizzo * Can be called for one or more queues. 175868b8534bSLuigi Rizzo * Return true the event mask corresponding to ready events. 175968b8534bSLuigi Rizzo * If there are no ready events, do a selrecord on either individual 1760ce3ee1e7SLuigi Rizzo * selinfo or on the global one. 176168b8534bSLuigi Rizzo * Device-dependent parts (locking and sync of tx/rx rings) 176268b8534bSLuigi Rizzo * are done through callbacks. 1763f196ce38SLuigi Rizzo * 176401c7d25fSLuigi Rizzo * On linux, arguments are really pwait, the poll table, and 'td' is struct file * 176501c7d25fSLuigi Rizzo * The first one is remapped to pwait as selrecord() uses the name as an 176601c7d25fSLuigi Rizzo * hidden argument. 176768b8534bSLuigi Rizzo */ 1768f9790aebSLuigi Rizzo int 176901c7d25fSLuigi Rizzo netmap_poll(struct cdev *dev, int events, struct thread *td) 177068b8534bSLuigi Rizzo { 177168b8534bSLuigi Rizzo struct netmap_priv_d *priv = NULL; 177268b8534bSLuigi Rizzo struct netmap_adapter *na; 177368b8534bSLuigi Rizzo struct ifnet *ifp; 177468b8534bSLuigi Rizzo struct netmap_kring *kring; 1775954dca4cSLuigi Rizzo u_int i, check_all_tx, check_all_rx, want_tx, want_rx, revents = 0; 1776*17885a7bSLuigi Rizzo u_int lim_tx, lim_rx; 1777*17885a7bSLuigi Rizzo struct mbq q; /* packets from hw queues to host stack */ 177801c7d25fSLuigi Rizzo void *pwait = dev; /* linux compatibility */ 177901c7d25fSLuigi Rizzo 1780f9790aebSLuigi Rizzo /* 1781f9790aebSLuigi Rizzo * In order to avoid nested locks, we need to "double check" 1782f9790aebSLuigi Rizzo * txsync and rxsync if we decide to do a selrecord(). 1783f9790aebSLuigi Rizzo * retry_tx (and retry_rx, later) prevent looping forever. 1784f9790aebSLuigi Rizzo */ 1785*17885a7bSLuigi Rizzo int retry_tx = 1, retry_rx = 1; 1786ce3ee1e7SLuigi Rizzo 178701c7d25fSLuigi Rizzo (void)pwait; 1788f9790aebSLuigi Rizzo mbq_init(&q); 178968b8534bSLuigi Rizzo 179068b8534bSLuigi Rizzo if (devfs_get_cdevpriv((void **)&priv) != 0 || priv == NULL) 179168b8534bSLuigi Rizzo return POLLERR; 179268b8534bSLuigi Rizzo 17938241616dSLuigi Rizzo if (priv->np_nifp == NULL) { 17948241616dSLuigi Rizzo D("No if registered"); 17958241616dSLuigi Rizzo return POLLERR; 17968241616dSLuigi Rizzo } 17978241616dSLuigi Rizzo rmb(); /* make sure following reads are not from cache */ 17988241616dSLuigi Rizzo 1799f9790aebSLuigi Rizzo na = priv->np_na; 1800f9790aebSLuigi Rizzo ifp = na->ifp; 1801f9790aebSLuigi Rizzo // check for deleted 1802f9790aebSLuigi Rizzo if (ifp == NULL) { 1803f9790aebSLuigi Rizzo RD(1, "the ifp is gone"); 1804f9790aebSLuigi Rizzo return POLLERR; 1805f9790aebSLuigi Rizzo } 1806f9790aebSLuigi Rizzo 180768b8534bSLuigi Rizzo if ( (ifp->if_capenable & IFCAP_NETMAP) == 0) 180868b8534bSLuigi Rizzo return POLLERR; 180968b8534bSLuigi Rizzo 181068b8534bSLuigi Rizzo if (netmap_verbose & 0x8000) 1811f9790aebSLuigi Rizzo D("device %s events 0x%x", NM_IFPNAME(ifp), events); 181268b8534bSLuigi Rizzo want_tx = events & (POLLOUT | POLLWRNORM); 181368b8534bSLuigi Rizzo want_rx = events & (POLLIN | POLLRDNORM); 181468b8534bSLuigi Rizzo 1815d76bf4ffSLuigi Rizzo lim_tx = na->num_tx_rings; 1816d76bf4ffSLuigi Rizzo lim_rx = na->num_rx_rings; 1817ce3ee1e7SLuigi Rizzo 181864ae02c3SLuigi Rizzo if (priv->np_qfirst == NETMAP_SW_RING) { 1819*17885a7bSLuigi Rizzo // XXX locking ? 1820ce3ee1e7SLuigi Rizzo /* handle the host stack ring */ 182168b8534bSLuigi Rizzo if (priv->np_txpoll || want_tx) { 182268b8534bSLuigi Rizzo /* push any packets up, then we are always ready */ 1823ce3ee1e7SLuigi Rizzo netmap_txsync_to_host(na); 182468b8534bSLuigi Rizzo revents |= want_tx; 182568b8534bSLuigi Rizzo } 182668b8534bSLuigi Rizzo if (want_rx) { 182764ae02c3SLuigi Rizzo kring = &na->rx_rings[lim_rx]; 1828*17885a7bSLuigi Rizzo /* XXX replace with rxprologue etc. */ 1829*17885a7bSLuigi Rizzo if (nm_ring_empty(kring->ring)) 1830ce3ee1e7SLuigi Rizzo netmap_rxsync_from_host(na, td, dev); 1831*17885a7bSLuigi Rizzo if (!nm_ring_empty(kring->ring)) 183268b8534bSLuigi Rizzo revents |= want_rx; 183368b8534bSLuigi Rizzo } 183468b8534bSLuigi Rizzo return (revents); 183568b8534bSLuigi Rizzo } 183668b8534bSLuigi Rizzo 1837091fd0abSLuigi Rizzo 183868b8534bSLuigi Rizzo /* 1839f9790aebSLuigi Rizzo * check_all_{tx|rx} are set if the card has more than one queue AND 1840f9790aebSLuigi Rizzo * the file descriptor is bound to all of them. If so, we sleep on 1841ce3ee1e7SLuigi Rizzo * the "global" selinfo, otherwise we sleep on individual selinfo 1842ce3ee1e7SLuigi Rizzo * (FreeBSD only allows two selinfo's per file descriptor). 1843ce3ee1e7SLuigi Rizzo * The interrupt routine in the driver wake one or the other 1844ce3ee1e7SLuigi Rizzo * (or both) depending on which clients are active. 184568b8534bSLuigi Rizzo * 184668b8534bSLuigi Rizzo * rxsync() is only called if we run out of buffers on a POLLIN. 184768b8534bSLuigi Rizzo * txsync() is called if we run out of buffers on POLLOUT, or 184868b8534bSLuigi Rizzo * there are pending packets to send. The latter can be disabled 184968b8534bSLuigi Rizzo * passing NETMAP_NO_TX_POLL in the NIOCREG call. 185068b8534bSLuigi Rizzo */ 1851954dca4cSLuigi Rizzo check_all_tx = (priv->np_qlast == NETMAP_HW_RING) && (lim_tx > 1); 1852954dca4cSLuigi Rizzo check_all_rx = (priv->np_qlast == NETMAP_HW_RING) && (lim_rx > 1); 185368b8534bSLuigi Rizzo 185464ae02c3SLuigi Rizzo if (priv->np_qlast != NETMAP_HW_RING) { 185564ae02c3SLuigi Rizzo lim_tx = lim_rx = priv->np_qlast; 185664ae02c3SLuigi Rizzo } 185764ae02c3SLuigi Rizzo 185868b8534bSLuigi Rizzo /* 1859f9790aebSLuigi Rizzo * We start with a lock free round which is cheap if we have 1860f9790aebSLuigi Rizzo * slots available. If this fails, then lock and call the sync 186168b8534bSLuigi Rizzo * routines. 186268b8534bSLuigi Rizzo */ 186364ae02c3SLuigi Rizzo for (i = priv->np_qfirst; want_rx && i < lim_rx; i++) { 186468b8534bSLuigi Rizzo kring = &na->rx_rings[i]; 1865*17885a7bSLuigi Rizzo /* XXX compare ring->cur and kring->tail */ 1866*17885a7bSLuigi Rizzo if (!nm_ring_empty(kring->ring)) { 186768b8534bSLuigi Rizzo revents |= want_rx; 186868b8534bSLuigi Rizzo want_rx = 0; /* also breaks the loop */ 186968b8534bSLuigi Rizzo } 187068b8534bSLuigi Rizzo } 187164ae02c3SLuigi Rizzo for (i = priv->np_qfirst; want_tx && i < lim_tx; i++) { 187268b8534bSLuigi Rizzo kring = &na->tx_rings[i]; 1873*17885a7bSLuigi Rizzo /* XXX compare ring->cur and kring->tail */ 1874*17885a7bSLuigi Rizzo if (!nm_ring_empty(kring->ring)) { 187568b8534bSLuigi Rizzo revents |= want_tx; 187668b8534bSLuigi Rizzo want_tx = 0; /* also breaks the loop */ 187768b8534bSLuigi Rizzo } 187868b8534bSLuigi Rizzo } 187968b8534bSLuigi Rizzo 188068b8534bSLuigi Rizzo /* 1881*17885a7bSLuigi Rizzo * If we want to push packets out (priv->np_txpoll) or 1882*17885a7bSLuigi Rizzo * want_tx is still set, we must issue txsync calls 1883*17885a7bSLuigi Rizzo * (on all rings, to avoid that the tx rings stall). 1884f9790aebSLuigi Rizzo * XXX should also check cur != hwcur on the tx rings. 1885f9790aebSLuigi Rizzo * Fortunately, normal tx mode has np_txpoll set. 188668b8534bSLuigi Rizzo */ 188768b8534bSLuigi Rizzo if (priv->np_txpoll || want_tx) { 1888*17885a7bSLuigi Rizzo /* 1889*17885a7bSLuigi Rizzo * The first round checks if anyone is ready, if not 1890*17885a7bSLuigi Rizzo * do a selrecord and another round to handle races. 1891*17885a7bSLuigi Rizzo * want_tx goes to 0 if any space is found, and is 1892*17885a7bSLuigi Rizzo * used to skip rings with no pending transmissions. 1893ce3ee1e7SLuigi Rizzo */ 1894091fd0abSLuigi Rizzo flush_tx: 189564ae02c3SLuigi Rizzo for (i = priv->np_qfirst; i < lim_tx; i++) { 1896*17885a7bSLuigi Rizzo int found = 0; 1897*17885a7bSLuigi Rizzo 189868b8534bSLuigi Rizzo kring = &na->tx_rings[i]; 189968b8534bSLuigi Rizzo if (!want_tx && kring->ring->cur == kring->nr_hwcur) 190068b8534bSLuigi Rizzo continue; 1901*17885a7bSLuigi Rizzo /* only one thread does txsync */ 1902ce3ee1e7SLuigi Rizzo if (nm_kr_tryget(kring)) { 1903*17885a7bSLuigi Rizzo D("%p lost race on txring %d, ok", priv, i); 1904*17885a7bSLuigi Rizzo continue; 190568b8534bSLuigi Rizzo } 1906*17885a7bSLuigi Rizzo if (nm_txsync_prologue(kring) >= kring->nkr_num_slots) { 1907*17885a7bSLuigi Rizzo netmap_ring_reinit(kring); 1908*17885a7bSLuigi Rizzo revents |= POLLERR; 1909*17885a7bSLuigi Rizzo } else { 1910f9790aebSLuigi Rizzo if (na->nm_txsync(na, i, 0)) 191168b8534bSLuigi Rizzo revents |= POLLERR; 1912*17885a7bSLuigi Rizzo } 191368b8534bSLuigi Rizzo 1914*17885a7bSLuigi Rizzo /* 1915*17885a7bSLuigi Rizzo * If we found new slots, notify potential 1916*17885a7bSLuigi Rizzo * listeners on the same ring. 1917*17885a7bSLuigi Rizzo * Since we just did a txsync, look at the copies 1918*17885a7bSLuigi Rizzo * of cur,tail in the kring. 1919f9790aebSLuigi Rizzo */ 1920*17885a7bSLuigi Rizzo found = kring->rcur != kring->rtail; 1921*17885a7bSLuigi Rizzo nm_kr_put(kring); 1922*17885a7bSLuigi Rizzo if (found) { /* notify other listeners */ 192368b8534bSLuigi Rizzo revents |= want_tx; 192468b8534bSLuigi Rizzo want_tx = 0; 1925*17885a7bSLuigi Rizzo na->nm_notify(na, i, NR_TX, NAF_GLOBAL_NOTIFY); 192668b8534bSLuigi Rizzo } 1927ce3ee1e7SLuigi Rizzo } 1928ce3ee1e7SLuigi Rizzo if (want_tx && retry_tx) { 1929954dca4cSLuigi Rizzo selrecord(td, check_all_tx ? 1930ce3ee1e7SLuigi Rizzo &na->tx_si : &na->tx_rings[priv->np_qfirst].si); 1931ce3ee1e7SLuigi Rizzo retry_tx = 0; 1932ce3ee1e7SLuigi Rizzo goto flush_tx; 193368b8534bSLuigi Rizzo } 193468b8534bSLuigi Rizzo } 193568b8534bSLuigi Rizzo 193668b8534bSLuigi Rizzo /* 1937*17885a7bSLuigi Rizzo * If want_rx is still set scan receive rings. 193868b8534bSLuigi Rizzo * Do it on all rings because otherwise we starve. 193968b8534bSLuigi Rizzo */ 194068b8534bSLuigi Rizzo if (want_rx) { 1941*17885a7bSLuigi Rizzo int send_down = 0; /* transparent mode */ 1942*17885a7bSLuigi Rizzo /* two rounds here to for race avoidance */ 1943ce3ee1e7SLuigi Rizzo do_retry_rx: 194464ae02c3SLuigi Rizzo for (i = priv->np_qfirst; i < lim_rx; i++) { 1945*17885a7bSLuigi Rizzo int found = 0; 1946*17885a7bSLuigi Rizzo 194768b8534bSLuigi Rizzo kring = &na->rx_rings[i]; 1948ce3ee1e7SLuigi Rizzo 1949ce3ee1e7SLuigi Rizzo if (nm_kr_tryget(kring)) { 1950*17885a7bSLuigi Rizzo D("%p lost race on rxring %d, ok", priv, i); 1951*17885a7bSLuigi Rizzo continue; 195268b8534bSLuigi Rizzo } 1953ce3ee1e7SLuigi Rizzo 1954*17885a7bSLuigi Rizzo /* 1955*17885a7bSLuigi Rizzo * transparent mode support: collect packets 1956*17885a7bSLuigi Rizzo * from the rxring(s). 1957*17885a7bSLuigi Rizzo * XXX NR_FORWARD should only be read on 1958ce3ee1e7SLuigi Rizzo * physical or NIC ports 1959ce3ee1e7SLuigi Rizzo */ 1960091fd0abSLuigi Rizzo if (netmap_fwd ||kring->ring->flags & NR_FORWARD) { 1961091fd0abSLuigi Rizzo ND(10, "forwarding some buffers up %d to %d", 1962091fd0abSLuigi Rizzo kring->nr_hwcur, kring->ring->cur); 1963091fd0abSLuigi Rizzo netmap_grab_packets(kring, &q, netmap_fwd); 1964091fd0abSLuigi Rizzo } 196568b8534bSLuigi Rizzo 1966f9790aebSLuigi Rizzo if (na->nm_rxsync(na, i, 0)) 196768b8534bSLuigi Rizzo revents |= POLLERR; 19685819da83SLuigi Rizzo if (netmap_no_timestamp == 0 || 19695819da83SLuigi Rizzo kring->ring->flags & NR_TIMESTAMP) { 197068b8534bSLuigi Rizzo microtime(&kring->ring->ts); 19715819da83SLuigi Rizzo } 1972*17885a7bSLuigi Rizzo /* after an rxsync we can use kring->rcur, rtail */ 1973*17885a7bSLuigi Rizzo found = kring->rcur != kring->rtail; 1974*17885a7bSLuigi Rizzo nm_kr_put(kring); 1975*17885a7bSLuigi Rizzo if (found) { 197668b8534bSLuigi Rizzo revents |= want_rx; 1977ce3ee1e7SLuigi Rizzo retry_rx = 0; 1978*17885a7bSLuigi Rizzo na->nm_notify(na, i, NR_RX, NAF_GLOBAL_NOTIFY); 197968b8534bSLuigi Rizzo } 198068b8534bSLuigi Rizzo } 1981*17885a7bSLuigi Rizzo 1982*17885a7bSLuigi Rizzo /* transparent mode XXX only during first pass ? */ 1983*17885a7bSLuigi Rizzo kring = &na->rx_rings[lim_rx]; 1984*17885a7bSLuigi Rizzo if (check_all_rx 1985*17885a7bSLuigi Rizzo && (netmap_fwd || kring->ring->flags & NR_FORWARD)) { 1986*17885a7bSLuigi Rizzo /* XXX fix to use kring fields */ 1987*17885a7bSLuigi Rizzo if (nm_ring_empty(kring->ring)) 1988*17885a7bSLuigi Rizzo send_down = netmap_rxsync_from_host(na, td, dev); 1989*17885a7bSLuigi Rizzo if (!nm_ring_empty(kring->ring)) 1990*17885a7bSLuigi Rizzo revents |= want_rx; 1991*17885a7bSLuigi Rizzo } 1992*17885a7bSLuigi Rizzo 1993*17885a7bSLuigi Rizzo if (retry_rx) 1994954dca4cSLuigi Rizzo selrecord(td, check_all_rx ? 1995ce3ee1e7SLuigi Rizzo &na->rx_si : &na->rx_rings[priv->np_qfirst].si); 1996*17885a7bSLuigi Rizzo if (send_down > 0 || retry_rx) { 1997*17885a7bSLuigi Rizzo retry_rx = 0; 1998*17885a7bSLuigi Rizzo if (send_down) 1999*17885a7bSLuigi Rizzo goto flush_tx; /* and retry_rx */ 2000*17885a7bSLuigi Rizzo else 2001ce3ee1e7SLuigi Rizzo goto do_retry_rx; 2002ce3ee1e7SLuigi Rizzo } 200368b8534bSLuigi Rizzo } 2004091fd0abSLuigi Rizzo 2005*17885a7bSLuigi Rizzo /* 2006*17885a7bSLuigi Rizzo * Transparent mode: marked bufs on rx rings between 2007*17885a7bSLuigi Rizzo * kring->nr_hwcur and ring->head 2008*17885a7bSLuigi Rizzo * are passed to the other endpoint. 2009*17885a7bSLuigi Rizzo * 2010*17885a7bSLuigi Rizzo * In this mode we also scan the sw rxring, which in 2011*17885a7bSLuigi Rizzo * turn passes packets up. 2012*17885a7bSLuigi Rizzo * 2013*17885a7bSLuigi Rizzo * XXX Transparent mode at the moment requires to bind all 2014*17885a7bSLuigi Rizzo * rings to a single file descriptor. 2015ce3ee1e7SLuigi Rizzo */ 2016091fd0abSLuigi Rizzo 2017091fd0abSLuigi Rizzo if (q.head) 2018f9790aebSLuigi Rizzo netmap_send_up(na->ifp, &q); 201968b8534bSLuigi Rizzo 202068b8534bSLuigi Rizzo return (revents); 202168b8534bSLuigi Rizzo } 202268b8534bSLuigi Rizzo 2023*17885a7bSLuigi Rizzo 2024*17885a7bSLuigi Rizzo /*-------------------- driver support routines -------------------*/ 202568b8534bSLuigi Rizzo 2026f9790aebSLuigi Rizzo static int netmap_hw_krings_create(struct netmap_adapter *); 2027f9790aebSLuigi Rizzo 2028f9790aebSLuigi Rizzo static int 2029*17885a7bSLuigi Rizzo netmap_notify(struct netmap_adapter *na, u_int n_ring, 2030*17885a7bSLuigi Rizzo enum txrx tx, int flags) 2031f9790aebSLuigi Rizzo { 2032f9790aebSLuigi Rizzo struct netmap_kring *kring; 2033f9790aebSLuigi Rizzo 2034f9790aebSLuigi Rizzo if (tx == NR_TX) { 2035f9790aebSLuigi Rizzo kring = na->tx_rings + n_ring; 2036f9790aebSLuigi Rizzo selwakeuppri(&kring->si, PI_NET); 2037f9790aebSLuigi Rizzo if (flags & NAF_GLOBAL_NOTIFY) 2038f9790aebSLuigi Rizzo selwakeuppri(&na->tx_si, PI_NET); 2039f9790aebSLuigi Rizzo } else { 2040f9790aebSLuigi Rizzo kring = na->rx_rings + n_ring; 2041f9790aebSLuigi Rizzo selwakeuppri(&kring->si, PI_NET); 2042f9790aebSLuigi Rizzo if (flags & NAF_GLOBAL_NOTIFY) 2043f9790aebSLuigi Rizzo selwakeuppri(&na->rx_si, PI_NET); 2044f9790aebSLuigi Rizzo } 2045f9790aebSLuigi Rizzo return 0; 2046f9790aebSLuigi Rizzo } 2047f9790aebSLuigi Rizzo 2048f9790aebSLuigi Rizzo 2049f9790aebSLuigi Rizzo // XXX check handling of failures 2050f9790aebSLuigi Rizzo int 2051f9790aebSLuigi Rizzo netmap_attach_common(struct netmap_adapter *na) 2052f9790aebSLuigi Rizzo { 2053f9790aebSLuigi Rizzo struct ifnet *ifp = na->ifp; 2054f9790aebSLuigi Rizzo 2055f9790aebSLuigi Rizzo if (na->num_tx_rings == 0 || na->num_rx_rings == 0) { 2056f9790aebSLuigi Rizzo D("%s: invalid rings tx %d rx %d", 2057f9790aebSLuigi Rizzo ifp->if_xname, na->num_tx_rings, na->num_rx_rings); 2058f9790aebSLuigi Rizzo return EINVAL; 2059f9790aebSLuigi Rizzo } 2060f9790aebSLuigi Rizzo WNA(ifp) = na; 2061*17885a7bSLuigi Rizzo 2062*17885a7bSLuigi Rizzo /* the following is only needed for na that use the host port. 2063*17885a7bSLuigi Rizzo * XXX do we have something similar for linux ? 2064*17885a7bSLuigi Rizzo */ 2065*17885a7bSLuigi Rizzo #ifdef __FreeBSD__ 2066*17885a7bSLuigi Rizzo na->if_input = ifp->if_input; /* for netmap_send_up */ 2067*17885a7bSLuigi Rizzo #endif /* __FreeBSD__ */ 2068*17885a7bSLuigi Rizzo 2069f9790aebSLuigi Rizzo NETMAP_SET_CAPABLE(ifp); 2070f9790aebSLuigi Rizzo if (na->nm_krings_create == NULL) { 2071f9790aebSLuigi Rizzo na->nm_krings_create = netmap_hw_krings_create; 2072*17885a7bSLuigi Rizzo na->nm_krings_delete = netmap_hw_krings_delete; 2073f9790aebSLuigi Rizzo } 2074f9790aebSLuigi Rizzo if (na->nm_notify == NULL) 2075f9790aebSLuigi Rizzo na->nm_notify = netmap_notify; 2076f9790aebSLuigi Rizzo na->active_fds = 0; 2077f9790aebSLuigi Rizzo 2078f9790aebSLuigi Rizzo if (na->nm_mem == NULL) 2079f9790aebSLuigi Rizzo na->nm_mem = &nm_mem; 2080f9790aebSLuigi Rizzo return 0; 2081f9790aebSLuigi Rizzo } 2082f9790aebSLuigi Rizzo 2083f9790aebSLuigi Rizzo 2084f9790aebSLuigi Rizzo void 2085f9790aebSLuigi Rizzo netmap_detach_common(struct netmap_adapter *na) 2086f9790aebSLuigi Rizzo { 2087f9790aebSLuigi Rizzo if (na->ifp) 2088f9790aebSLuigi Rizzo WNA(na->ifp) = NULL; /* XXX do we need this? */ 2089f9790aebSLuigi Rizzo 2090f9790aebSLuigi Rizzo if (na->tx_rings) { /* XXX should not happen */ 2091f9790aebSLuigi Rizzo D("freeing leftover tx_rings"); 2092f9790aebSLuigi Rizzo na->nm_krings_delete(na); 2093f9790aebSLuigi Rizzo } 2094f9790aebSLuigi Rizzo if (na->na_flags & NAF_MEM_OWNER) 2095f9790aebSLuigi Rizzo netmap_mem_private_delete(na->nm_mem); 2096f9790aebSLuigi Rizzo bzero(na, sizeof(*na)); 2097f9790aebSLuigi Rizzo free(na, M_DEVBUF); 2098f9790aebSLuigi Rizzo } 2099f9790aebSLuigi Rizzo 2100f18be576SLuigi Rizzo 210168b8534bSLuigi Rizzo /* 210268b8534bSLuigi Rizzo * Initialize a ``netmap_adapter`` object created by driver on attach. 210368b8534bSLuigi Rizzo * We allocate a block of memory with room for a struct netmap_adapter 210468b8534bSLuigi Rizzo * plus two sets of N+2 struct netmap_kring (where N is the number 210568b8534bSLuigi Rizzo * of hardware rings): 210668b8534bSLuigi Rizzo * krings 0..N-1 are for the hardware queues. 210768b8534bSLuigi Rizzo * kring N is for the host stack queue 2108*17885a7bSLuigi Rizzo * kring N+1 is only used for the selinfo for all queues. // XXX still true ? 210968b8534bSLuigi Rizzo * Return 0 on success, ENOMEM otherwise. 211068b8534bSLuigi Rizzo */ 211168b8534bSLuigi Rizzo int 2112f9790aebSLuigi Rizzo netmap_attach(struct netmap_adapter *arg) 211368b8534bSLuigi Rizzo { 2114f9790aebSLuigi Rizzo struct netmap_hw_adapter *hwna = NULL; 2115f9790aebSLuigi Rizzo // XXX when is arg == NULL ? 2116ae10d1afSLuigi Rizzo struct ifnet *ifp = arg ? arg->ifp : NULL; 211768b8534bSLuigi Rizzo 2118ae10d1afSLuigi Rizzo if (arg == NULL || ifp == NULL) 2119ae10d1afSLuigi Rizzo goto fail; 2120f9790aebSLuigi Rizzo hwna = malloc(sizeof(*hwna), M_DEVBUF, M_NOWAIT | M_ZERO); 2121f9790aebSLuigi Rizzo if (hwna == NULL) 2122ae10d1afSLuigi Rizzo goto fail; 2123f9790aebSLuigi Rizzo hwna->up = *arg; 2124f9790aebSLuigi Rizzo if (netmap_attach_common(&hwna->up)) { 2125f9790aebSLuigi Rizzo free(hwna, M_DEVBUF); 2126f9790aebSLuigi Rizzo goto fail; 2127f9790aebSLuigi Rizzo } 2128f9790aebSLuigi Rizzo netmap_adapter_get(&hwna->up); 2129f9790aebSLuigi Rizzo 213064ae02c3SLuigi Rizzo #ifdef linux 2131f18be576SLuigi Rizzo if (ifp->netdev_ops) { 2132f18be576SLuigi Rizzo /* prepare a clone of the netdev ops */ 2133f18be576SLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28) 2134f9790aebSLuigi Rizzo hwna->nm_ndo.ndo_start_xmit = ifp->netdev_ops; 2135f18be576SLuigi Rizzo #else 2136f9790aebSLuigi Rizzo hwna->nm_ndo = *ifp->netdev_ops; 2137f18be576SLuigi Rizzo #endif 2138f18be576SLuigi Rizzo } 2139f9790aebSLuigi Rizzo hwna->nm_ndo.ndo_start_xmit = linux_netmap_start_xmit; 2140ce3ee1e7SLuigi Rizzo #endif /* linux */ 2141f9790aebSLuigi Rizzo 2142f9790aebSLuigi Rizzo D("success for %s", NM_IFPNAME(ifp)); 2143ae10d1afSLuigi Rizzo return 0; 214468b8534bSLuigi Rizzo 2145ae10d1afSLuigi Rizzo fail: 2146f9790aebSLuigi Rizzo D("fail, arg %p ifp %p na %p", arg, ifp, hwna); 2147849bec0eSLuigi Rizzo netmap_detach(ifp); 2148f9790aebSLuigi Rizzo return (hwna ? EINVAL : ENOMEM); 214968b8534bSLuigi Rizzo } 215068b8534bSLuigi Rizzo 215168b8534bSLuigi Rizzo 2152f9790aebSLuigi Rizzo void 2153f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_get)(struct netmap_adapter *na) 2154f9790aebSLuigi Rizzo { 2155f9790aebSLuigi Rizzo if (!na) { 2156f9790aebSLuigi Rizzo return; 2157f9790aebSLuigi Rizzo } 2158f9790aebSLuigi Rizzo 2159f9790aebSLuigi Rizzo refcount_acquire(&na->na_refcount); 2160f9790aebSLuigi Rizzo } 2161f9790aebSLuigi Rizzo 2162f9790aebSLuigi Rizzo 2163f9790aebSLuigi Rizzo /* returns 1 iff the netmap_adapter is destroyed */ 2164f9790aebSLuigi Rizzo int 2165f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_put)(struct netmap_adapter *na) 2166f9790aebSLuigi Rizzo { 2167f9790aebSLuigi Rizzo if (!na) 2168f9790aebSLuigi Rizzo return 1; 2169f9790aebSLuigi Rizzo 2170f9790aebSLuigi Rizzo if (!refcount_release(&na->na_refcount)) 2171f9790aebSLuigi Rizzo return 0; 2172f9790aebSLuigi Rizzo 2173f9790aebSLuigi Rizzo if (na->nm_dtor) 2174f9790aebSLuigi Rizzo na->nm_dtor(na); 2175f9790aebSLuigi Rizzo 2176f9790aebSLuigi Rizzo netmap_detach_common(na); 2177f9790aebSLuigi Rizzo 2178f9790aebSLuigi Rizzo return 1; 2179f9790aebSLuigi Rizzo } 2180f9790aebSLuigi Rizzo 2181f9790aebSLuigi Rizzo 2182f9790aebSLuigi Rizzo int 2183f9790aebSLuigi Rizzo netmap_hw_krings_create(struct netmap_adapter *na) 2184f9790aebSLuigi Rizzo { 2185*17885a7bSLuigi Rizzo int ret = netmap_krings_create(na, 2186f9790aebSLuigi Rizzo na->num_tx_rings + 1, na->num_rx_rings + 1, 0); 2187*17885a7bSLuigi Rizzo if (ret == 0) { 2188*17885a7bSLuigi Rizzo /* initialize the mbq for the sw rx ring */ 2189*17885a7bSLuigi Rizzo mbq_safe_init(&na->rx_rings[na->num_rx_rings].rx_queue); 2190*17885a7bSLuigi Rizzo ND("initialized sw rx queue %d", na->num_rx_rings); 2191*17885a7bSLuigi Rizzo } 2192*17885a7bSLuigi Rizzo return ret; 2193f9790aebSLuigi Rizzo } 2194f9790aebSLuigi Rizzo 2195f9790aebSLuigi Rizzo 2196f9790aebSLuigi Rizzo 219768b8534bSLuigi Rizzo /* 219868b8534bSLuigi Rizzo * Free the allocated memory linked to the given ``netmap_adapter`` 219968b8534bSLuigi Rizzo * object. 220068b8534bSLuigi Rizzo */ 220168b8534bSLuigi Rizzo void 220268b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp) 220368b8534bSLuigi Rizzo { 220468b8534bSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 220568b8534bSLuigi Rizzo 220668b8534bSLuigi Rizzo if (!na) 220768b8534bSLuigi Rizzo return; 220868b8534bSLuigi Rizzo 2209f9790aebSLuigi Rizzo NMG_LOCK(); 2210f9790aebSLuigi Rizzo netmap_disable_all_rings(ifp); 2211f9790aebSLuigi Rizzo netmap_adapter_put(na); 2212f9790aebSLuigi Rizzo na->ifp = NULL; 2213f9790aebSLuigi Rizzo netmap_enable_all_rings(ifp); 2214f9790aebSLuigi Rizzo NMG_UNLOCK(); 2215ae10d1afSLuigi Rizzo } 2216f18be576SLuigi Rizzo 2217f18be576SLuigi Rizzo 221868b8534bSLuigi Rizzo /* 221902ad4083SLuigi Rizzo * Intercept packets from the network stack and pass them 222002ad4083SLuigi Rizzo * to netmap as incoming packets on the 'software' ring. 2221*17885a7bSLuigi Rizzo * 2222*17885a7bSLuigi Rizzo * We only store packets in a bounded mbq and then copy them 2223*17885a7bSLuigi Rizzo * in the relevant rxsync routine. 2224*17885a7bSLuigi Rizzo * 2225ce3ee1e7SLuigi Rizzo * We rely on the OS to make sure that the ifp and na do not go 2226ce3ee1e7SLuigi Rizzo * away (typically the caller checks for IFF_DRV_RUNNING or the like). 2227ce3ee1e7SLuigi Rizzo * In nm_register() or whenever there is a reinitialization, 2228f9790aebSLuigi Rizzo * we make sure to make the mode change visible here. 222968b8534bSLuigi Rizzo */ 223068b8534bSLuigi Rizzo int 2231ce3ee1e7SLuigi Rizzo netmap_transmit(struct ifnet *ifp, struct mbuf *m) 223268b8534bSLuigi Rizzo { 223368b8534bSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 2234ce3ee1e7SLuigi Rizzo struct netmap_kring *kring; 2235*17885a7bSLuigi Rizzo u_int len = MBUF_LEN(m); 2236*17885a7bSLuigi Rizzo u_int error = ENOBUFS; 2237*17885a7bSLuigi Rizzo struct mbq *q; 2238*17885a7bSLuigi Rizzo int space; 223968b8534bSLuigi Rizzo 2240ce3ee1e7SLuigi Rizzo // XXX [Linux] we do not need this lock 2241ce3ee1e7SLuigi Rizzo // if we follow the down/configure/up protocol -gl 2242ce3ee1e7SLuigi Rizzo // mtx_lock(&na->core_lock); 2243*17885a7bSLuigi Rizzo 2244ce3ee1e7SLuigi Rizzo if ( (ifp->if_capenable & IFCAP_NETMAP) == 0) { 2245*17885a7bSLuigi Rizzo D("%s not in netmap mode anymore", NM_IFPNAME(ifp)); 2246ce3ee1e7SLuigi Rizzo error = ENXIO; 2247ce3ee1e7SLuigi Rizzo goto done; 2248ce3ee1e7SLuigi Rizzo } 2249ce3ee1e7SLuigi Rizzo 2250ce3ee1e7SLuigi Rizzo kring = &na->rx_rings[na->num_rx_rings]; 2251*17885a7bSLuigi Rizzo q = &kring->rx_queue; 2252*17885a7bSLuigi Rizzo 2253ce3ee1e7SLuigi Rizzo // XXX reconsider long packets if we handle fragments 2254ce3ee1e7SLuigi Rizzo if (len > NETMAP_BDG_BUF_SIZE(na->nm_mem)) { /* too long for us */ 2255f9790aebSLuigi Rizzo D("%s from_host, drop packet size %d > %d", NM_IFPNAME(ifp), 2256ce3ee1e7SLuigi Rizzo len, NETMAP_BDG_BUF_SIZE(na->nm_mem)); 2257ce3ee1e7SLuigi Rizzo goto done; 2258849bec0eSLuigi Rizzo } 2259*17885a7bSLuigi Rizzo 2260*17885a7bSLuigi Rizzo /* protect against rxsync_from_host(), netmap_sw_to_nic() 2261*17885a7bSLuigi Rizzo * and maybe other instances of netmap_transmit (the latter 2262*17885a7bSLuigi Rizzo * not possible on Linux). 2263*17885a7bSLuigi Rizzo * Also avoid overflowing the queue. 2264ce3ee1e7SLuigi Rizzo */ 2265*17885a7bSLuigi Rizzo mtx_lock(&q->lock); 2266*17885a7bSLuigi Rizzo 2267*17885a7bSLuigi Rizzo space = kring->nr_hwtail - kring->nr_hwcur; 2268*17885a7bSLuigi Rizzo if (space < 0) 2269*17885a7bSLuigi Rizzo space += kring->nkr_num_slots; 2270*17885a7bSLuigi Rizzo if (space + mbq_len(q) >= kring->nkr_num_slots - 1) { // XXX 2271*17885a7bSLuigi Rizzo RD(10, "%s full hwcur %d hwtail %d qlen %d len %d m %p", 2272*17885a7bSLuigi Rizzo NM_IFPNAME(ifp), kring->nr_hwcur, kring->nr_hwtail, mbq_len(q), 2273*17885a7bSLuigi Rizzo len, m); 2274ce3ee1e7SLuigi Rizzo } else { 2275*17885a7bSLuigi Rizzo mbq_enqueue(q, m); 2276*17885a7bSLuigi Rizzo ND(10, "%s %d bufs in queue len %d m %p", 2277*17885a7bSLuigi Rizzo NM_IFPNAME(ifp), mbq_len(q), len, m); 2278*17885a7bSLuigi Rizzo /* notify outside the lock */ 2279*17885a7bSLuigi Rizzo m = NULL; 228068b8534bSLuigi Rizzo error = 0; 2281ce3ee1e7SLuigi Rizzo } 2282*17885a7bSLuigi Rizzo mtx_unlock(&q->lock); 2283ce3ee1e7SLuigi Rizzo 228468b8534bSLuigi Rizzo done: 2285*17885a7bSLuigi Rizzo if (m) 228668b8534bSLuigi Rizzo m_freem(m); 2287*17885a7bSLuigi Rizzo /* unconditionally wake up listeners */ 2288*17885a7bSLuigi Rizzo na->nm_notify(na, na->num_rx_rings, NR_RX, 0); 228968b8534bSLuigi Rizzo 229068b8534bSLuigi Rizzo return (error); 229168b8534bSLuigi Rizzo } 229268b8534bSLuigi Rizzo 229368b8534bSLuigi Rizzo 229468b8534bSLuigi Rizzo /* 229568b8534bSLuigi Rizzo * netmap_reset() is called by the driver routines when reinitializing 229668b8534bSLuigi Rizzo * a ring. The driver is in charge of locking to protect the kring. 2297f9790aebSLuigi Rizzo * If native netmap mode is not set just return NULL. 229868b8534bSLuigi Rizzo */ 229968b8534bSLuigi Rizzo struct netmap_slot * 2300ce3ee1e7SLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, u_int n, 230168b8534bSLuigi Rizzo u_int new_cur) 230268b8534bSLuigi Rizzo { 230368b8534bSLuigi Rizzo struct netmap_kring *kring; 2304506cc70cSLuigi Rizzo int new_hwofs, lim; 230568b8534bSLuigi Rizzo 2306ce3ee1e7SLuigi Rizzo if (na == NULL) { 2307ce3ee1e7SLuigi Rizzo D("NULL na, should not happen"); 230868b8534bSLuigi Rizzo return NULL; /* no netmap support here */ 2309ce3ee1e7SLuigi Rizzo } 2310ce3ee1e7SLuigi Rizzo if (!(na->ifp->if_capenable & IFCAP_NETMAP)) { 23115864b3a5SLuigi Rizzo ND("interface not in netmap mode"); 231268b8534bSLuigi Rizzo return NULL; /* nothing to reinitialize */ 2313ce3ee1e7SLuigi Rizzo } 231468b8534bSLuigi Rizzo 2315ce3ee1e7SLuigi Rizzo /* XXX note- in the new scheme, we are not guaranteed to be 2316ce3ee1e7SLuigi Rizzo * under lock (e.g. when called on a device reset). 2317ce3ee1e7SLuigi Rizzo * In this case, we should set a flag and do not trust too 2318ce3ee1e7SLuigi Rizzo * much the values. In practice: TODO 2319ce3ee1e7SLuigi Rizzo * - set a RESET flag somewhere in the kring 2320ce3ee1e7SLuigi Rizzo * - do the processing in a conservative way 2321ce3ee1e7SLuigi Rizzo * - let the *sync() fixup at the end. 2322ce3ee1e7SLuigi Rizzo */ 232364ae02c3SLuigi Rizzo if (tx == NR_TX) { 23248241616dSLuigi Rizzo if (n >= na->num_tx_rings) 23258241616dSLuigi Rizzo return NULL; 232664ae02c3SLuigi Rizzo kring = na->tx_rings + n; 2327*17885a7bSLuigi Rizzo // XXX check whether we should use hwcur or rcur 2328506cc70cSLuigi Rizzo new_hwofs = kring->nr_hwcur - new_cur; 232964ae02c3SLuigi Rizzo } else { 23308241616dSLuigi Rizzo if (n >= na->num_rx_rings) 23318241616dSLuigi Rizzo return NULL; 233264ae02c3SLuigi Rizzo kring = na->rx_rings + n; 2333*17885a7bSLuigi Rizzo new_hwofs = kring->nr_hwtail - new_cur; 233464ae02c3SLuigi Rizzo } 233564ae02c3SLuigi Rizzo lim = kring->nkr_num_slots - 1; 2336506cc70cSLuigi Rizzo if (new_hwofs > lim) 2337506cc70cSLuigi Rizzo new_hwofs -= lim + 1; 2338506cc70cSLuigi Rizzo 2339ce3ee1e7SLuigi Rizzo /* Always set the new offset value and realign the ring. */ 2340*17885a7bSLuigi Rizzo if (netmap_verbose) 2341*17885a7bSLuigi Rizzo D("%s %s%d hwofs %d -> %d, hwtail %d -> %d", 2342*17885a7bSLuigi Rizzo NM_IFPNAME(na->ifp), 2343*17885a7bSLuigi Rizzo tx == NR_TX ? "TX" : "RX", n, 2344ce3ee1e7SLuigi Rizzo kring->nkr_hwofs, new_hwofs, 2345*17885a7bSLuigi Rizzo kring->nr_hwtail, 2346*17885a7bSLuigi Rizzo tx == NR_TX ? lim : kring->nr_hwtail); 2347506cc70cSLuigi Rizzo kring->nkr_hwofs = new_hwofs; 2348*17885a7bSLuigi Rizzo if (tx == NR_TX) { 2349*17885a7bSLuigi Rizzo kring->nr_hwtail = kring->nr_hwcur + lim; 2350*17885a7bSLuigi Rizzo if (kring->nr_hwtail > lim) 2351*17885a7bSLuigi Rizzo kring->nr_hwtail -= lim + 1; 2352*17885a7bSLuigi Rizzo } 2353506cc70cSLuigi Rizzo 2354f196ce38SLuigi Rizzo #if 0 // def linux 2355f196ce38SLuigi Rizzo /* XXX check that the mappings are correct */ 2356f196ce38SLuigi Rizzo /* need ring_nr, adapter->pdev, direction */ 2357f196ce38SLuigi Rizzo buffer_info->dma = dma_map_single(&pdev->dev, addr, adapter->rx_buffer_len, DMA_FROM_DEVICE); 2358f196ce38SLuigi Rizzo if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) { 2359f196ce38SLuigi Rizzo D("error mapping rx netmap buffer %d", i); 2360f196ce38SLuigi Rizzo // XXX fix error handling 2361f196ce38SLuigi Rizzo } 2362f196ce38SLuigi Rizzo 2363f196ce38SLuigi Rizzo #endif /* linux */ 236468b8534bSLuigi Rizzo /* 2365ce3ee1e7SLuigi Rizzo * Wakeup on the individual and global selwait 2366506cc70cSLuigi Rizzo * We do the wakeup here, but the ring is not yet reconfigured. 2367506cc70cSLuigi Rizzo * However, we are under lock so there are no races. 236868b8534bSLuigi Rizzo */ 2369f9790aebSLuigi Rizzo na->nm_notify(na, n, tx, NAF_GLOBAL_NOTIFY); 237068b8534bSLuigi Rizzo return kring->ring->slot; 237168b8534bSLuigi Rizzo } 237268b8534bSLuigi Rizzo 237368b8534bSLuigi Rizzo 2374ce3ee1e7SLuigi Rizzo /* 2375f9790aebSLuigi Rizzo * Dispatch rx/tx interrupts to the netmap rings. 2376ce3ee1e7SLuigi Rizzo * 2377ce3ee1e7SLuigi Rizzo * "work_done" is non-null on the RX path, NULL for the TX path. 2378ce3ee1e7SLuigi Rizzo * We rely on the OS to make sure that there is only one active 2379ce3ee1e7SLuigi Rizzo * instance per queue, and that there is appropriate locking. 2380849bec0eSLuigi Rizzo * 2381f9790aebSLuigi Rizzo * The 'notify' routine depends on what the ring is attached to. 2382f9790aebSLuigi Rizzo * - for a netmap file descriptor, do a selwakeup on the individual 2383f9790aebSLuigi Rizzo * waitqueue, plus one on the global one if needed 2384f9790aebSLuigi Rizzo * - for a switch, call the proper forwarding routine 2385f9790aebSLuigi Rizzo * - XXX more ? 2386f9790aebSLuigi Rizzo */ 2387f9790aebSLuigi Rizzo void 2388f9790aebSLuigi Rizzo netmap_common_irq(struct ifnet *ifp, u_int q, u_int *work_done) 2389f9790aebSLuigi Rizzo { 2390f9790aebSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 2391f9790aebSLuigi Rizzo struct netmap_kring *kring; 2392f9790aebSLuigi Rizzo 2393f9790aebSLuigi Rizzo q &= NETMAP_RING_MASK; 2394f9790aebSLuigi Rizzo 2395f9790aebSLuigi Rizzo if (netmap_verbose) { 2396f9790aebSLuigi Rizzo RD(5, "received %s queue %d", work_done ? "RX" : "TX" , q); 2397f9790aebSLuigi Rizzo } 2398f9790aebSLuigi Rizzo 2399f9790aebSLuigi Rizzo if (work_done) { /* RX path */ 2400f9790aebSLuigi Rizzo if (q >= na->num_rx_rings) 2401f9790aebSLuigi Rizzo return; // not a physical queue 2402f9790aebSLuigi Rizzo kring = na->rx_rings + q; 2403f9790aebSLuigi Rizzo kring->nr_kflags |= NKR_PENDINTR; // XXX atomic ? 2404f9790aebSLuigi Rizzo na->nm_notify(na, q, NR_RX, 2405f9790aebSLuigi Rizzo (na->num_rx_rings > 1 ? NAF_GLOBAL_NOTIFY : 0)); 2406f9790aebSLuigi Rizzo *work_done = 1; /* do not fire napi again */ 2407f9790aebSLuigi Rizzo } else { /* TX path */ 2408f9790aebSLuigi Rizzo if (q >= na->num_tx_rings) 2409f9790aebSLuigi Rizzo return; // not a physical queue 2410f9790aebSLuigi Rizzo kring = na->tx_rings + q; 2411f9790aebSLuigi Rizzo na->nm_notify(na, q, NR_TX, 2412f9790aebSLuigi Rizzo (na->num_tx_rings > 1 ? NAF_GLOBAL_NOTIFY : 0)); 2413f9790aebSLuigi Rizzo } 2414f9790aebSLuigi Rizzo } 2415f9790aebSLuigi Rizzo 2416*17885a7bSLuigi Rizzo 2417f9790aebSLuigi Rizzo /* 2418f9790aebSLuigi Rizzo * Default functions to handle rx/tx interrupts from a physical device. 2419f9790aebSLuigi Rizzo * "work_done" is non-null on the RX path, NULL for the TX path. 2420f9790aebSLuigi Rizzo * 2421ce3ee1e7SLuigi Rizzo * If the card is not in netmap mode, simply return 0, 2422ce3ee1e7SLuigi Rizzo * so that the caller proceeds with regular processing. 2423f9790aebSLuigi Rizzo * Otherwise call netmap_common_irq() and return 1. 2424ce3ee1e7SLuigi Rizzo * 2425ce3ee1e7SLuigi Rizzo * If the card is connected to a netmap file descriptor, 2426ce3ee1e7SLuigi Rizzo * do a selwakeup on the individual queue, plus one on the global one 2427ce3ee1e7SLuigi Rizzo * if needed (multiqueue card _and_ there are multiqueue listeners), 2428ce3ee1e7SLuigi Rizzo * and return 1. 2429ce3ee1e7SLuigi Rizzo * 2430ce3ee1e7SLuigi Rizzo * Finally, if called on rx from an interface connected to a switch, 2431ce3ee1e7SLuigi Rizzo * calls the proper forwarding routine, and return 1. 24321a26580eSLuigi Rizzo */ 2433babc7c12SLuigi Rizzo int 2434ce3ee1e7SLuigi Rizzo netmap_rx_irq(struct ifnet *ifp, u_int q, u_int *work_done) 24351a26580eSLuigi Rizzo { 2436f9790aebSLuigi Rizzo // XXX could we check NAF_NATIVE_ON ? 24371a26580eSLuigi Rizzo if (!(ifp->if_capenable & IFCAP_NETMAP)) 24381a26580eSLuigi Rizzo return 0; 2439849bec0eSLuigi Rizzo 2440f9790aebSLuigi Rizzo if (NA(ifp)->na_flags & NAF_SKIP_INTR) { 24418241616dSLuigi Rizzo ND("use regular interrupt"); 24428241616dSLuigi Rizzo return 0; 24438241616dSLuigi Rizzo } 24448241616dSLuigi Rizzo 2445f9790aebSLuigi Rizzo netmap_common_irq(ifp, q, work_done); 24461a26580eSLuigi Rizzo return 1; 24471a26580eSLuigi Rizzo } 24481a26580eSLuigi Rizzo 244964ae02c3SLuigi Rizzo 245001c7d25fSLuigi Rizzo /* 2451f9790aebSLuigi Rizzo * Module loader and unloader 2452f196ce38SLuigi Rizzo * 2453f9790aebSLuigi Rizzo * netmap_init() creates the /dev/netmap device and initializes 2454f9790aebSLuigi Rizzo * all global variables. Returns 0 on success, errno on failure 2455f9790aebSLuigi Rizzo * (but there is no chance) 2456f9790aebSLuigi Rizzo * 2457f9790aebSLuigi Rizzo * netmap_fini() destroys everything. 2458f196ce38SLuigi Rizzo */ 2459babc7c12SLuigi Rizzo 2460babc7c12SLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */ 2461f9790aebSLuigi Rizzo extern struct cdevsw netmap_cdevsw; 2462babc7c12SLuigi Rizzo 2463*17885a7bSLuigi Rizzo 2464f9790aebSLuigi Rizzo void 246568b8534bSLuigi Rizzo netmap_fini(void) 246668b8534bSLuigi Rizzo { 2467f9790aebSLuigi Rizzo // XXX destroy_bridges() ? 2468f9790aebSLuigi Rizzo if (netmap_dev) 246968b8534bSLuigi Rizzo destroy_dev(netmap_dev); 2470ce3ee1e7SLuigi Rizzo netmap_mem_fini(); 2471ce3ee1e7SLuigi Rizzo NMG_LOCK_DESTROY(); 247268b8534bSLuigi Rizzo printf("netmap: unloaded module.\n"); 247368b8534bSLuigi Rizzo } 247468b8534bSLuigi Rizzo 2475*17885a7bSLuigi Rizzo 2476f9790aebSLuigi Rizzo int 2477f9790aebSLuigi Rizzo netmap_init(void) 247868b8534bSLuigi Rizzo { 2479f9790aebSLuigi Rizzo int error; 248068b8534bSLuigi Rizzo 2481f9790aebSLuigi Rizzo NMG_LOCK_INIT(); 248268b8534bSLuigi Rizzo 2483f9790aebSLuigi Rizzo error = netmap_mem_init(); 2484f9790aebSLuigi Rizzo if (error != 0) 2485f9790aebSLuigi Rizzo goto fail; 2486f9790aebSLuigi Rizzo /* XXX could use make_dev_credv() to get error number */ 2487f9790aebSLuigi Rizzo netmap_dev = make_dev(&netmap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660, 2488f9790aebSLuigi Rizzo "netmap"); 2489f9790aebSLuigi Rizzo if (!netmap_dev) 2490f9790aebSLuigi Rizzo goto fail; 2491f9790aebSLuigi Rizzo 2492f9790aebSLuigi Rizzo netmap_init_bridges(); 2493f9790aebSLuigi Rizzo printf("netmap: loaded module\n"); 2494f9790aebSLuigi Rizzo return (0); 2495f9790aebSLuigi Rizzo fail: 249668b8534bSLuigi Rizzo netmap_fini(); 2497f9790aebSLuigi Rizzo return (EINVAL); /* may be incorrect */ 249868b8534bSLuigi Rizzo } 2499