168b8534bSLuigi Rizzo /* 217885a7bSLuigi 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> 148*339f59c0SGleb Smirnoff #include <sys/jail.h> 149*339f59c0SGleb Smirnoff #include <net/vnet.h> 15068b8534bSLuigi Rizzo #include <net/if.h> 15176039bc8SGleb Smirnoff #include <net/if_var.h> 15268b8534bSLuigi Rizzo #include <net/bpf.h> /* BIOCIMMEDIATE */ 15368b8534bSLuigi Rizzo #include <machine/bus.h> /* bus_dmamap_* */ 154ce3ee1e7SLuigi Rizzo #include <sys/endian.h> 155ce3ee1e7SLuigi Rizzo #include <sys/refcount.h> 15668b8534bSLuigi Rizzo 15768b8534bSLuigi Rizzo 158f9790aebSLuigi Rizzo /* reduce conditional code */ 159f9790aebSLuigi Rizzo #define init_waitqueue_head(x) // only needed in linux 160ce3ee1e7SLuigi Rizzo 161ce3ee1e7SLuigi Rizzo 162ce3ee1e7SLuigi Rizzo 163ce3ee1e7SLuigi Rizzo #elif defined(linux) 164ce3ee1e7SLuigi Rizzo 165ce3ee1e7SLuigi Rizzo #include "bsd_glue.h" 166ce3ee1e7SLuigi Rizzo 167ce3ee1e7SLuigi Rizzo 168ce3ee1e7SLuigi Rizzo 169ce3ee1e7SLuigi Rizzo #elif defined(__APPLE__) 170ce3ee1e7SLuigi Rizzo 171ce3ee1e7SLuigi Rizzo #warning OSX support is only partial 172ce3ee1e7SLuigi Rizzo #include "osx_glue.h" 173ce3ee1e7SLuigi Rizzo 174ce3ee1e7SLuigi Rizzo #else 175ce3ee1e7SLuigi Rizzo 176ce3ee1e7SLuigi Rizzo #error Unsupported platform 177ce3ee1e7SLuigi Rizzo 178ce3ee1e7SLuigi Rizzo #endif /* unsupported */ 179ce3ee1e7SLuigi Rizzo 180ce3ee1e7SLuigi Rizzo /* 181ce3ee1e7SLuigi Rizzo * common headers 182ce3ee1e7SLuigi Rizzo */ 1830b8ed8e0SLuigi Rizzo #include <net/netmap.h> 1840b8ed8e0SLuigi Rizzo #include <dev/netmap/netmap_kern.h> 185ce3ee1e7SLuigi Rizzo #include <dev/netmap/netmap_mem2.h> 1860b8ed8e0SLuigi Rizzo 187ce3ee1e7SLuigi Rizzo 188ce3ee1e7SLuigi Rizzo MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map"); 189ce3ee1e7SLuigi Rizzo 190ce3ee1e7SLuigi Rizzo /* 191ce3ee1e7SLuigi Rizzo * The following variables are used by the drivers and replicate 192ce3ee1e7SLuigi Rizzo * fields in the global memory pool. They only refer to buffers 193ce3ee1e7SLuigi Rizzo * used by physical interfaces. 194ce3ee1e7SLuigi Rizzo */ 1955819da83SLuigi Rizzo u_int netmap_total_buffers; 1968241616dSLuigi Rizzo u_int netmap_buf_size; 197ce3ee1e7SLuigi Rizzo char *netmap_buffer_base; /* also address of an invalid buffer */ 1985819da83SLuigi Rizzo 1995819da83SLuigi Rizzo /* user-controlled variables */ 2005819da83SLuigi Rizzo int netmap_verbose; 2015819da83SLuigi Rizzo 2025819da83SLuigi Rizzo static int netmap_no_timestamp; /* don't timestamp on rxsync */ 2035819da83SLuigi Rizzo 2045819da83SLuigi Rizzo SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args"); 2055819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose, 2065819da83SLuigi Rizzo CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode"); 2075819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp, 2085819da83SLuigi Rizzo CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp"); 2095819da83SLuigi Rizzo int netmap_mitigate = 1; 2105819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mitigate, CTLFLAG_RW, &netmap_mitigate, 0, ""); 211c85cb1a0SLuigi Rizzo int netmap_no_pendintr = 1; 2125819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr, 2135819da83SLuigi Rizzo CTLFLAG_RW, &netmap_no_pendintr, 0, "Always look for new received packets."); 214f18be576SLuigi Rizzo int netmap_txsync_retry = 2; 215f18be576SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, txsync_retry, CTLFLAG_RW, 216f18be576SLuigi Rizzo &netmap_txsync_retry, 0 , "Number of txsync loops in bridge's flush."); 2175819da83SLuigi Rizzo 218f196ce38SLuigi Rizzo int netmap_flags = 0; /* debug flags */ 219091fd0abSLuigi Rizzo int netmap_fwd = 0; /* force transparent mode */ 220ce3ee1e7SLuigi Rizzo int netmap_mmap_unreg = 0; /* allow mmap of unregistered fds */ 221f196ce38SLuigi Rizzo 222f9790aebSLuigi Rizzo /* 223f9790aebSLuigi Rizzo * netmap_admode selects the netmap mode to use. 224f9790aebSLuigi Rizzo * Invalid values are reset to NETMAP_ADMODE_BEST 225f9790aebSLuigi Rizzo */ 226f9790aebSLuigi Rizzo enum { NETMAP_ADMODE_BEST = 0, /* use native, fallback to generic */ 227f9790aebSLuigi Rizzo NETMAP_ADMODE_NATIVE, /* either native or none */ 228f9790aebSLuigi Rizzo NETMAP_ADMODE_GENERIC, /* force generic */ 229f9790aebSLuigi Rizzo NETMAP_ADMODE_LAST }; 230f9790aebSLuigi Rizzo static int netmap_admode = NETMAP_ADMODE_BEST; 231f9790aebSLuigi Rizzo 232f9790aebSLuigi Rizzo int netmap_generic_mit = 100*1000; /* Generic mitigation interval in nanoseconds. */ 233f9790aebSLuigi Rizzo int netmap_generic_ringsize = 1024; /* Generic ringsize. */ 234f9790aebSLuigi Rizzo 235f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, flags, CTLFLAG_RW, &netmap_flags, 0 , ""); 236091fd0abSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, fwd, CTLFLAG_RW, &netmap_fwd, 0 , ""); 237ce3ee1e7SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mmap_unreg, CTLFLAG_RW, &netmap_mmap_unreg, 0, ""); 238f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, admode, CTLFLAG_RW, &netmap_admode, 0 , ""); 239f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_mit, CTLFLAG_RW, &netmap_generic_mit, 0 , ""); 240f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_ringsize, CTLFLAG_RW, &netmap_generic_ringsize, 0 , ""); 241f196ce38SLuigi Rizzo 242ce3ee1e7SLuigi Rizzo NMG_LOCK_T netmap_global_lock; 243ce3ee1e7SLuigi Rizzo 244ce3ee1e7SLuigi Rizzo 245f9790aebSLuigi Rizzo static void 246f9790aebSLuigi Rizzo nm_kr_get(struct netmap_kring *kr) 247ce3ee1e7SLuigi Rizzo { 248ce3ee1e7SLuigi Rizzo while (NM_ATOMIC_TEST_AND_SET(&kr->nr_busy)) 249ce3ee1e7SLuigi Rizzo tsleep(kr, 0, "NM_KR_GET", 4); 250ce3ee1e7SLuigi Rizzo } 251ce3ee1e7SLuigi Rizzo 252f9790aebSLuigi Rizzo 25317885a7bSLuigi Rizzo /* 25417885a7bSLuigi Rizzo * mark the ring as stopped, and run through the locks 25517885a7bSLuigi Rizzo * to make sure other users get to see it. 25617885a7bSLuigi Rizzo */ 257f9790aebSLuigi Rizzo void 258f9790aebSLuigi Rizzo netmap_disable_ring(struct netmap_kring *kr) 259ce3ee1e7SLuigi Rizzo { 260ce3ee1e7SLuigi Rizzo kr->nkr_stopped = 1; 261ce3ee1e7SLuigi Rizzo nm_kr_get(kr); 262ce3ee1e7SLuigi Rizzo mtx_lock(&kr->q_lock); 263ce3ee1e7SLuigi Rizzo mtx_unlock(&kr->q_lock); 264ce3ee1e7SLuigi Rizzo nm_kr_put(kr); 265ce3ee1e7SLuigi Rizzo } 266ce3ee1e7SLuigi Rizzo 267f9790aebSLuigi Rizzo 268f9790aebSLuigi Rizzo static void 269f9790aebSLuigi Rizzo netmap_set_all_rings(struct ifnet *ifp, int stopped) 270ce3ee1e7SLuigi Rizzo { 271ce3ee1e7SLuigi Rizzo struct netmap_adapter *na; 272ce3ee1e7SLuigi Rizzo int i; 273ce3ee1e7SLuigi Rizzo 274ce3ee1e7SLuigi Rizzo if (!(ifp->if_capenable & IFCAP_NETMAP)) 275ce3ee1e7SLuigi Rizzo return; 276ce3ee1e7SLuigi Rizzo 277ce3ee1e7SLuigi Rizzo na = NA(ifp); 278ce3ee1e7SLuigi Rizzo 279f9790aebSLuigi Rizzo for (i = 0; i <= na->num_tx_rings; i++) { 280f9790aebSLuigi Rizzo if (stopped) 281f9790aebSLuigi Rizzo netmap_disable_ring(na->tx_rings + i); 282f9790aebSLuigi Rizzo else 283ce3ee1e7SLuigi Rizzo na->tx_rings[i].nkr_stopped = 0; 284f9790aebSLuigi Rizzo na->nm_notify(na, i, NR_TX, NAF_DISABLE_NOTIFY | 285f9790aebSLuigi Rizzo (i == na->num_tx_rings ? NAF_GLOBAL_NOTIFY: 0)); 286ce3ee1e7SLuigi Rizzo } 287f9790aebSLuigi Rizzo 288f9790aebSLuigi Rizzo for (i = 0; i <= na->num_rx_rings; i++) { 289f9790aebSLuigi Rizzo if (stopped) 290f9790aebSLuigi Rizzo netmap_disable_ring(na->rx_rings + i); 291f9790aebSLuigi Rizzo else 292ce3ee1e7SLuigi Rizzo na->rx_rings[i].nkr_stopped = 0; 293f9790aebSLuigi Rizzo na->nm_notify(na, i, NR_RX, NAF_DISABLE_NOTIFY | 294f9790aebSLuigi Rizzo (i == na->num_rx_rings ? NAF_GLOBAL_NOTIFY: 0)); 295ce3ee1e7SLuigi Rizzo } 296ce3ee1e7SLuigi Rizzo } 297ce3ee1e7SLuigi Rizzo 298ce3ee1e7SLuigi Rizzo 299f9790aebSLuigi Rizzo void 300f9790aebSLuigi Rizzo netmap_disable_all_rings(struct ifnet *ifp) 301f9790aebSLuigi Rizzo { 302f9790aebSLuigi Rizzo netmap_set_all_rings(ifp, 1 /* stopped */); 303f9790aebSLuigi Rizzo } 304f9790aebSLuigi Rizzo 305f9790aebSLuigi Rizzo 306f9790aebSLuigi Rizzo void 307f9790aebSLuigi Rizzo netmap_enable_all_rings(struct ifnet *ifp) 308f9790aebSLuigi Rizzo { 309f9790aebSLuigi Rizzo netmap_set_all_rings(ifp, 0 /* enabled */); 310f9790aebSLuigi Rizzo } 311f9790aebSLuigi Rizzo 312f9790aebSLuigi Rizzo 313ce3ee1e7SLuigi Rizzo /* 314ce3ee1e7SLuigi Rizzo * generic bound_checking function 315ce3ee1e7SLuigi Rizzo */ 316ce3ee1e7SLuigi Rizzo u_int 317ce3ee1e7SLuigi Rizzo nm_bound_var(u_int *v, u_int dflt, u_int lo, u_int hi, const char *msg) 318ce3ee1e7SLuigi Rizzo { 319ce3ee1e7SLuigi Rizzo u_int oldv = *v; 320ce3ee1e7SLuigi Rizzo const char *op = NULL; 321ce3ee1e7SLuigi Rizzo 322ce3ee1e7SLuigi Rizzo if (dflt < lo) 323ce3ee1e7SLuigi Rizzo dflt = lo; 324ce3ee1e7SLuigi Rizzo if (dflt > hi) 325ce3ee1e7SLuigi Rizzo dflt = hi; 326ce3ee1e7SLuigi Rizzo if (oldv < lo) { 327ce3ee1e7SLuigi Rizzo *v = dflt; 328ce3ee1e7SLuigi Rizzo op = "Bump"; 329ce3ee1e7SLuigi Rizzo } else if (oldv > hi) { 330ce3ee1e7SLuigi Rizzo *v = hi; 331ce3ee1e7SLuigi Rizzo op = "Clamp"; 332ce3ee1e7SLuigi Rizzo } 333ce3ee1e7SLuigi Rizzo if (op && msg) 334ce3ee1e7SLuigi Rizzo printf("%s %s to %d (was %d)\n", op, msg, *v, oldv); 335ce3ee1e7SLuigi Rizzo return *v; 336ce3ee1e7SLuigi Rizzo } 337ce3ee1e7SLuigi Rizzo 338f9790aebSLuigi Rizzo 339ce3ee1e7SLuigi Rizzo /* 340ce3ee1e7SLuigi Rizzo * packet-dump function, user-supplied or static buffer. 341ce3ee1e7SLuigi Rizzo * The destination buffer must be at least 30+4*len 342ce3ee1e7SLuigi Rizzo */ 343ce3ee1e7SLuigi Rizzo const char * 344ce3ee1e7SLuigi Rizzo nm_dump_buf(char *p, int len, int lim, char *dst) 345ce3ee1e7SLuigi Rizzo { 346ce3ee1e7SLuigi Rizzo static char _dst[8192]; 347ce3ee1e7SLuigi Rizzo int i, j, i0; 348ce3ee1e7SLuigi Rizzo static char hex[] ="0123456789abcdef"; 349ce3ee1e7SLuigi Rizzo char *o; /* output position */ 350ce3ee1e7SLuigi Rizzo 351ce3ee1e7SLuigi Rizzo #define P_HI(x) hex[((x) & 0xf0)>>4] 352ce3ee1e7SLuigi Rizzo #define P_LO(x) hex[((x) & 0xf)] 353ce3ee1e7SLuigi Rizzo #define P_C(x) ((x) >= 0x20 && (x) <= 0x7e ? (x) : '.') 354ce3ee1e7SLuigi Rizzo if (!dst) 355ce3ee1e7SLuigi Rizzo dst = _dst; 356ce3ee1e7SLuigi Rizzo if (lim <= 0 || lim > len) 357ce3ee1e7SLuigi Rizzo lim = len; 358ce3ee1e7SLuigi Rizzo o = dst; 359ce3ee1e7SLuigi Rizzo sprintf(o, "buf 0x%p len %d lim %d\n", p, len, lim); 360ce3ee1e7SLuigi Rizzo o += strlen(o); 361ce3ee1e7SLuigi Rizzo /* hexdump routine */ 362ce3ee1e7SLuigi Rizzo for (i = 0; i < lim; ) { 363ce3ee1e7SLuigi Rizzo sprintf(o, "%5d: ", i); 364ce3ee1e7SLuigi Rizzo o += strlen(o); 365ce3ee1e7SLuigi Rizzo memset(o, ' ', 48); 366ce3ee1e7SLuigi Rizzo i0 = i; 367ce3ee1e7SLuigi Rizzo for (j=0; j < 16 && i < lim; i++, j++) { 368ce3ee1e7SLuigi Rizzo o[j*3] = P_HI(p[i]); 369ce3ee1e7SLuigi Rizzo o[j*3+1] = P_LO(p[i]); 370ce3ee1e7SLuigi Rizzo } 371ce3ee1e7SLuigi Rizzo i = i0; 372ce3ee1e7SLuigi Rizzo for (j=0; j < 16 && i < lim; i++, j++) 373ce3ee1e7SLuigi Rizzo o[j + 48] = P_C(p[i]); 374ce3ee1e7SLuigi Rizzo o[j+48] = '\n'; 375ce3ee1e7SLuigi Rizzo o += j+49; 376ce3ee1e7SLuigi Rizzo } 377ce3ee1e7SLuigi Rizzo *o = '\0'; 378ce3ee1e7SLuigi Rizzo #undef P_HI 379ce3ee1e7SLuigi Rizzo #undef P_LO 380ce3ee1e7SLuigi Rizzo #undef P_C 381ce3ee1e7SLuigi Rizzo return dst; 382ce3ee1e7SLuigi Rizzo } 383f196ce38SLuigi Rizzo 384f18be576SLuigi Rizzo 385ae10d1afSLuigi Rizzo /* 386ae10d1afSLuigi Rizzo * Fetch configuration from the device, to cope with dynamic 387ae10d1afSLuigi Rizzo * reconfigurations after loading the module. 388ae10d1afSLuigi Rizzo */ 389f9790aebSLuigi Rizzo int 390ae10d1afSLuigi Rizzo netmap_update_config(struct netmap_adapter *na) 391ae10d1afSLuigi Rizzo { 392ae10d1afSLuigi Rizzo struct ifnet *ifp = na->ifp; 393ae10d1afSLuigi Rizzo u_int txr, txd, rxr, rxd; 394ae10d1afSLuigi Rizzo 395ae10d1afSLuigi Rizzo txr = txd = rxr = rxd = 0; 396ae10d1afSLuigi Rizzo if (na->nm_config) { 397f9790aebSLuigi Rizzo na->nm_config(na, &txr, &txd, &rxr, &rxd); 398ae10d1afSLuigi Rizzo } else { 399ae10d1afSLuigi Rizzo /* take whatever we had at init time */ 400ae10d1afSLuigi Rizzo txr = na->num_tx_rings; 401ae10d1afSLuigi Rizzo txd = na->num_tx_desc; 402ae10d1afSLuigi Rizzo rxr = na->num_rx_rings; 403ae10d1afSLuigi Rizzo rxd = na->num_rx_desc; 404ae10d1afSLuigi Rizzo } 405ae10d1afSLuigi Rizzo 406ae10d1afSLuigi Rizzo if (na->num_tx_rings == txr && na->num_tx_desc == txd && 407ae10d1afSLuigi Rizzo na->num_rx_rings == rxr && na->num_rx_desc == rxd) 408ae10d1afSLuigi Rizzo return 0; /* nothing changed */ 409f9790aebSLuigi Rizzo if (netmap_verbose || na->active_fds > 0) { 410ae10d1afSLuigi Rizzo D("stored config %s: txring %d x %d, rxring %d x %d", 411f9790aebSLuigi Rizzo NM_IFPNAME(ifp), 412ae10d1afSLuigi Rizzo na->num_tx_rings, na->num_tx_desc, 413ae10d1afSLuigi Rizzo na->num_rx_rings, na->num_rx_desc); 414ae10d1afSLuigi Rizzo D("new config %s: txring %d x %d, rxring %d x %d", 415f9790aebSLuigi Rizzo NM_IFPNAME(ifp), txr, txd, rxr, rxd); 416ae10d1afSLuigi Rizzo } 417f9790aebSLuigi Rizzo if (na->active_fds == 0) { 418ae10d1afSLuigi Rizzo D("configuration changed (but fine)"); 419ae10d1afSLuigi Rizzo na->num_tx_rings = txr; 420ae10d1afSLuigi Rizzo na->num_tx_desc = txd; 421ae10d1afSLuigi Rizzo na->num_rx_rings = rxr; 422ae10d1afSLuigi Rizzo na->num_rx_desc = rxd; 423ae10d1afSLuigi Rizzo return 0; 424ae10d1afSLuigi Rizzo } 425ae10d1afSLuigi Rizzo D("configuration changed while active, this is bad..."); 426ae10d1afSLuigi Rizzo return 1; 427ae10d1afSLuigi Rizzo } 428ae10d1afSLuigi Rizzo 429f9790aebSLuigi Rizzo 430f9790aebSLuigi Rizzo int 431f9790aebSLuigi Rizzo netmap_krings_create(struct netmap_adapter *na, u_int ntx, u_int nrx, u_int tailroom) 432f9790aebSLuigi Rizzo { 433f9790aebSLuigi Rizzo u_int i, len, ndesc; 434f9790aebSLuigi Rizzo struct netmap_kring *kring; 435f9790aebSLuigi Rizzo 43617885a7bSLuigi Rizzo // XXX additional space for extra rings ? 437f9790aebSLuigi Rizzo len = (ntx + nrx) * sizeof(struct netmap_kring) + tailroom; 438f9790aebSLuigi Rizzo 439f9790aebSLuigi Rizzo na->tx_rings = malloc((size_t)len, M_DEVBUF, M_NOWAIT | M_ZERO); 440f9790aebSLuigi Rizzo if (na->tx_rings == NULL) { 441f9790aebSLuigi Rizzo D("Cannot allocate krings"); 442f9790aebSLuigi Rizzo return ENOMEM; 443f9790aebSLuigi Rizzo } 444f9790aebSLuigi Rizzo na->rx_rings = na->tx_rings + ntx; 445f9790aebSLuigi Rizzo 44617885a7bSLuigi Rizzo /* 44717885a7bSLuigi Rizzo * All fields in krings are 0 except the one initialized below. 44817885a7bSLuigi Rizzo * but better be explicit on important kring fields. 44917885a7bSLuigi Rizzo */ 450f9790aebSLuigi Rizzo ndesc = na->num_tx_desc; 451f9790aebSLuigi Rizzo for (i = 0; i < ntx; i++) { /* Transmit rings */ 452f9790aebSLuigi Rizzo kring = &na->tx_rings[i]; 453f9790aebSLuigi Rizzo bzero(kring, sizeof(*kring)); 454f9790aebSLuigi Rizzo kring->na = na; 45517885a7bSLuigi Rizzo kring->ring_id = i; 456f9790aebSLuigi Rizzo kring->nkr_num_slots = ndesc; 457f9790aebSLuigi Rizzo /* 45817885a7bSLuigi Rizzo * IMPORTANT: Always keep one slot empty. 459f9790aebSLuigi Rizzo */ 46017885a7bSLuigi Rizzo kring->rhead = kring->rcur = kring->nr_hwcur = 0; 46117885a7bSLuigi Rizzo kring->rtail = kring->nr_hwtail = ndesc - 1; 46217885a7bSLuigi Rizzo snprintf(kring->name, sizeof(kring->name) - 1, "%s TX%d", NM_IFPNAME(na->ifp), i); 463f9790aebSLuigi Rizzo mtx_init(&kring->q_lock, "nm_txq_lock", NULL, MTX_DEF); 464f9790aebSLuigi Rizzo init_waitqueue_head(&kring->si); 465f9790aebSLuigi Rizzo } 466f9790aebSLuigi Rizzo 467f9790aebSLuigi Rizzo ndesc = na->num_rx_desc; 468f9790aebSLuigi Rizzo for (i = 0; i < nrx; i++) { /* Receive rings */ 469f9790aebSLuigi Rizzo kring = &na->rx_rings[i]; 470f9790aebSLuigi Rizzo bzero(kring, sizeof(*kring)); 471f9790aebSLuigi Rizzo kring->na = na; 47217885a7bSLuigi Rizzo kring->ring_id = i; 473f9790aebSLuigi Rizzo kring->nkr_num_slots = ndesc; 47417885a7bSLuigi Rizzo kring->rhead = kring->rcur = kring->nr_hwcur = 0; 47517885a7bSLuigi Rizzo kring->rtail = kring->nr_hwtail = 0; 47617885a7bSLuigi Rizzo snprintf(kring->name, sizeof(kring->name) - 1, "%s RX%d", NM_IFPNAME(na->ifp), i); 477f9790aebSLuigi Rizzo mtx_init(&kring->q_lock, "nm_rxq_lock", NULL, MTX_DEF); 478f9790aebSLuigi Rizzo init_waitqueue_head(&kring->si); 479f9790aebSLuigi Rizzo } 480f9790aebSLuigi Rizzo init_waitqueue_head(&na->tx_si); 481f9790aebSLuigi Rizzo init_waitqueue_head(&na->rx_si); 482f9790aebSLuigi Rizzo 483f9790aebSLuigi Rizzo na->tailroom = na->rx_rings + nrx; 484f9790aebSLuigi Rizzo 485f9790aebSLuigi Rizzo return 0; 486f9790aebSLuigi Rizzo } 487f9790aebSLuigi Rizzo 488f9790aebSLuigi Rizzo 48917885a7bSLuigi Rizzo /* XXX check boundaries */ 490f9790aebSLuigi Rizzo void 491f9790aebSLuigi Rizzo netmap_krings_delete(struct netmap_adapter *na) 492f9790aebSLuigi Rizzo { 493f9790aebSLuigi Rizzo int i; 494f9790aebSLuigi Rizzo 495f9790aebSLuigi Rizzo for (i = 0; i < na->num_tx_rings + 1; i++) { 496f9790aebSLuigi Rizzo mtx_destroy(&na->tx_rings[i].q_lock); 497f9790aebSLuigi Rizzo } 498f9790aebSLuigi Rizzo for (i = 0; i < na->num_rx_rings + 1; i++) { 499f9790aebSLuigi Rizzo mtx_destroy(&na->rx_rings[i].q_lock); 500f9790aebSLuigi Rizzo } 501f9790aebSLuigi Rizzo free(na->tx_rings, M_DEVBUF); 502f9790aebSLuigi Rizzo na->tx_rings = na->rx_rings = na->tailroom = NULL; 503f9790aebSLuigi Rizzo } 504f9790aebSLuigi Rizzo 505f9790aebSLuigi Rizzo 50617885a7bSLuigi Rizzo /* 50717885a7bSLuigi Rizzo * Destructor for NIC ports. They also have an mbuf queue 50817885a7bSLuigi Rizzo * on the rings connected to the host so we need to purge 50917885a7bSLuigi Rizzo * them first. 51017885a7bSLuigi Rizzo */ 51117885a7bSLuigi Rizzo static void 51217885a7bSLuigi Rizzo netmap_hw_krings_delete(struct netmap_adapter *na) 51317885a7bSLuigi Rizzo { 51417885a7bSLuigi Rizzo struct mbq *q = &na->rx_rings[na->num_rx_rings].rx_queue; 51517885a7bSLuigi Rizzo 51617885a7bSLuigi Rizzo ND("destroy sw mbq with len %d", mbq_len(q)); 51717885a7bSLuigi Rizzo mbq_purge(q); 51817885a7bSLuigi Rizzo mbq_safe_destroy(q); 51917885a7bSLuigi Rizzo netmap_krings_delete(na); 52017885a7bSLuigi Rizzo } 52117885a7bSLuigi Rizzo 52217885a7bSLuigi Rizzo 523ce3ee1e7SLuigi Rizzo static struct netmap_if* 524ce3ee1e7SLuigi Rizzo netmap_if_new(const char *ifname, struct netmap_adapter *na) 525ce3ee1e7SLuigi Rizzo { 526f9790aebSLuigi Rizzo struct netmap_if *nifp; 527f9790aebSLuigi Rizzo 528ce3ee1e7SLuigi Rizzo if (netmap_update_config(na)) { 529ce3ee1e7SLuigi Rizzo /* configuration mismatch, report and fail */ 530ce3ee1e7SLuigi Rizzo return NULL; 531ce3ee1e7SLuigi Rizzo } 532f9790aebSLuigi Rizzo 533f9790aebSLuigi Rizzo if (na->active_fds) 534f9790aebSLuigi Rizzo goto final; 535f9790aebSLuigi Rizzo 536f9790aebSLuigi Rizzo if (na->nm_krings_create(na)) 537f9790aebSLuigi Rizzo goto cleanup; 538f9790aebSLuigi Rizzo 539f9790aebSLuigi Rizzo if (netmap_mem_rings_create(na)) 540f9790aebSLuigi Rizzo goto cleanup; 541f9790aebSLuigi Rizzo 542f9790aebSLuigi Rizzo final: 543f9790aebSLuigi Rizzo 544f9790aebSLuigi Rizzo nifp = netmap_mem_if_new(ifname, na); 545f9790aebSLuigi Rizzo if (nifp == NULL) 546f9790aebSLuigi Rizzo goto cleanup; 547f9790aebSLuigi Rizzo 548f9790aebSLuigi Rizzo return (nifp); 549f9790aebSLuigi Rizzo 550f9790aebSLuigi Rizzo cleanup: 551f9790aebSLuigi Rizzo 552f9790aebSLuigi Rizzo if (na->active_fds == 0) { 553f9790aebSLuigi Rizzo netmap_mem_rings_delete(na); 554f9790aebSLuigi Rizzo na->nm_krings_delete(na); 555ce3ee1e7SLuigi Rizzo } 55668b8534bSLuigi Rizzo 557f9790aebSLuigi Rizzo return NULL; 558f9790aebSLuigi Rizzo } 5598241616dSLuigi Rizzo 56068b8534bSLuigi Rizzo 561ce3ee1e7SLuigi Rizzo /* grab a reference to the memory allocator, if we don't have one already. The 562ce3ee1e7SLuigi Rizzo * reference is taken from the netmap_adapter registered with the priv. 563ce3ee1e7SLuigi Rizzo * 564ce3ee1e7SLuigi Rizzo */ 565ce3ee1e7SLuigi Rizzo static int 566ce3ee1e7SLuigi Rizzo netmap_get_memory_locked(struct netmap_priv_d* p) 567ce3ee1e7SLuigi Rizzo { 568ce3ee1e7SLuigi Rizzo struct netmap_mem_d *nmd; 569ce3ee1e7SLuigi Rizzo int error = 0; 570ce3ee1e7SLuigi Rizzo 571f9790aebSLuigi Rizzo if (p->np_na == NULL) { 572ce3ee1e7SLuigi Rizzo if (!netmap_mmap_unreg) 573ce3ee1e7SLuigi Rizzo return ENODEV; 574ce3ee1e7SLuigi Rizzo /* for compatibility with older versions of the API 575ce3ee1e7SLuigi Rizzo * we use the global allocator when no interface has been 576ce3ee1e7SLuigi Rizzo * registered 577ce3ee1e7SLuigi Rizzo */ 578ce3ee1e7SLuigi Rizzo nmd = &nm_mem; 579ce3ee1e7SLuigi Rizzo } else { 580f9790aebSLuigi Rizzo nmd = p->np_na->nm_mem; 581ce3ee1e7SLuigi Rizzo } 582ce3ee1e7SLuigi Rizzo if (p->np_mref == NULL) { 583ce3ee1e7SLuigi Rizzo error = netmap_mem_finalize(nmd); 584ce3ee1e7SLuigi Rizzo if (!error) 585ce3ee1e7SLuigi Rizzo p->np_mref = nmd; 586ce3ee1e7SLuigi Rizzo } else if (p->np_mref != nmd) { 587ce3ee1e7SLuigi Rizzo /* a virtual port has been registered, but previous 588ce3ee1e7SLuigi Rizzo * syscalls already used the global allocator. 589ce3ee1e7SLuigi Rizzo * We cannot continue 590ce3ee1e7SLuigi Rizzo */ 591ce3ee1e7SLuigi Rizzo error = ENODEV; 592ce3ee1e7SLuigi Rizzo } 593ce3ee1e7SLuigi Rizzo return error; 594ce3ee1e7SLuigi Rizzo } 59568b8534bSLuigi Rizzo 596f9790aebSLuigi Rizzo 597f9790aebSLuigi Rizzo int 5988241616dSLuigi Rizzo netmap_get_memory(struct netmap_priv_d* p) 5998241616dSLuigi Rizzo { 600ce3ee1e7SLuigi Rizzo int error; 601ce3ee1e7SLuigi Rizzo NMG_LOCK(); 602ce3ee1e7SLuigi Rizzo error = netmap_get_memory_locked(p); 603ce3ee1e7SLuigi Rizzo NMG_UNLOCK(); 6048241616dSLuigi Rizzo return error; 6058241616dSLuigi Rizzo } 6068241616dSLuigi Rizzo 607f9790aebSLuigi Rizzo 608ce3ee1e7SLuigi Rizzo static int 609ce3ee1e7SLuigi Rizzo netmap_have_memory_locked(struct netmap_priv_d* p) 610ce3ee1e7SLuigi Rizzo { 611ce3ee1e7SLuigi Rizzo return p->np_mref != NULL; 612ce3ee1e7SLuigi Rizzo } 613ce3ee1e7SLuigi Rizzo 614f9790aebSLuigi Rizzo 615ce3ee1e7SLuigi Rizzo static void 616ce3ee1e7SLuigi Rizzo netmap_drop_memory_locked(struct netmap_priv_d* p) 617ce3ee1e7SLuigi Rizzo { 618ce3ee1e7SLuigi Rizzo if (p->np_mref) { 619ce3ee1e7SLuigi Rizzo netmap_mem_deref(p->np_mref); 620ce3ee1e7SLuigi Rizzo p->np_mref = NULL; 621ce3ee1e7SLuigi Rizzo } 622ce3ee1e7SLuigi Rizzo } 623ce3ee1e7SLuigi Rizzo 624f9790aebSLuigi Rizzo 62568b8534bSLuigi Rizzo /* 62668b8534bSLuigi Rizzo * File descriptor's private data destructor. 62768b8534bSLuigi Rizzo * 62868b8534bSLuigi Rizzo * Call nm_register(ifp,0) to stop netmap mode on the interface and 629f9790aebSLuigi Rizzo * revert to normal operation. We expect that np_na->ifp has not gone. 630ce3ee1e7SLuigi Rizzo * The second argument is the nifp to work on. In some cases it is 631ce3ee1e7SLuigi Rizzo * not attached yet to the netmap_priv_d so we need to pass it as 632ce3ee1e7SLuigi Rizzo * a separate argument. 63368b8534bSLuigi Rizzo */ 634ce3ee1e7SLuigi Rizzo /* call with NMG_LOCK held */ 63568b8534bSLuigi Rizzo static void 636ce3ee1e7SLuigi Rizzo netmap_do_unregif(struct netmap_priv_d *priv, struct netmap_if *nifp) 63768b8534bSLuigi Rizzo { 638f9790aebSLuigi Rizzo struct netmap_adapter *na = priv->np_na; 639f9790aebSLuigi Rizzo struct ifnet *ifp = na->ifp; 64068b8534bSLuigi Rizzo 641ce3ee1e7SLuigi Rizzo NMG_LOCK_ASSERT(); 642f9790aebSLuigi Rizzo na->active_fds--; 643f9790aebSLuigi Rizzo if (na->active_fds <= 0) { /* last instance */ 64468b8534bSLuigi Rizzo 645ae10d1afSLuigi Rizzo if (netmap_verbose) 646f9790aebSLuigi Rizzo D("deleting last instance for %s", NM_IFPNAME(ifp)); 64768b8534bSLuigi Rizzo /* 648f18be576SLuigi Rizzo * (TO CHECK) This function is only called 649f18be576SLuigi Rizzo * when the last reference to this file descriptor goes 650f18be576SLuigi Rizzo * away. This means we cannot have any pending poll() 651f18be576SLuigi Rizzo * or interrupt routine operating on the structure. 652ce3ee1e7SLuigi Rizzo * XXX The file may be closed in a thread while 653ce3ee1e7SLuigi Rizzo * another thread is using it. 654ce3ee1e7SLuigi Rizzo * Linux keeps the file opened until the last reference 655ce3ee1e7SLuigi Rizzo * by any outstanding ioctl/poll or mmap is gone. 656ce3ee1e7SLuigi Rizzo * FreeBSD does not track mmap()s (but we do) and 657ce3ee1e7SLuigi Rizzo * wakes up any sleeping poll(). Need to check what 658ce3ee1e7SLuigi Rizzo * happens if the close() occurs while a concurrent 659ce3ee1e7SLuigi Rizzo * syscall is running. 66068b8534bSLuigi Rizzo */ 661f9790aebSLuigi Rizzo if (ifp) 662f9790aebSLuigi Rizzo na->nm_register(na, 0); /* off, clear flags */ 66368b8534bSLuigi Rizzo /* Wake up any sleeping threads. netmap_poll will 66468b8534bSLuigi Rizzo * then return POLLERR 665ce3ee1e7SLuigi Rizzo * XXX The wake up now must happen during *_down(), when 666ce3ee1e7SLuigi Rizzo * we order all activities to stop. -gl 66768b8534bSLuigi Rizzo */ 6682f70fca5SEd Maste /* XXX kqueue(9) needed; these will mirror knlist_init. */ 6692f70fca5SEd Maste /* knlist_destroy(&na->tx_si.si_note); */ 6702f70fca5SEd Maste /* knlist_destroy(&na->rx_si.si_note); */ 671f9790aebSLuigi Rizzo 672f9790aebSLuigi Rizzo /* delete rings and buffers */ 673f9790aebSLuigi Rizzo netmap_mem_rings_delete(na); 674f9790aebSLuigi Rizzo na->nm_krings_delete(na); 67568b8534bSLuigi Rizzo } 676f9790aebSLuigi Rizzo /* delete the nifp */ 677ce3ee1e7SLuigi Rizzo netmap_mem_if_delete(na, nifp); 6785819da83SLuigi Rizzo } 67968b8534bSLuigi Rizzo 680f18be576SLuigi Rizzo 681ce3ee1e7SLuigi Rizzo /* 682ce3ee1e7SLuigi Rizzo * returns 1 if this is the last instance and we can free priv 683ce3ee1e7SLuigi Rizzo */ 684f9790aebSLuigi Rizzo int 685ce3ee1e7SLuigi Rizzo netmap_dtor_locked(struct netmap_priv_d *priv) 686ce3ee1e7SLuigi Rizzo { 687f9790aebSLuigi Rizzo struct netmap_adapter *na = priv->np_na; 688ce3ee1e7SLuigi Rizzo 689ce3ee1e7SLuigi Rizzo #ifdef __FreeBSD__ 690ce3ee1e7SLuigi Rizzo /* 691ce3ee1e7SLuigi Rizzo * np_refcount is the number of active mmaps on 692ce3ee1e7SLuigi Rizzo * this file descriptor 693ce3ee1e7SLuigi Rizzo */ 694ce3ee1e7SLuigi Rizzo if (--priv->np_refcount > 0) { 695ce3ee1e7SLuigi Rizzo return 0; 696ce3ee1e7SLuigi Rizzo } 697ce3ee1e7SLuigi Rizzo #endif /* __FreeBSD__ */ 698f9790aebSLuigi Rizzo if (!na) { 699f9790aebSLuigi Rizzo return 1; //XXX is it correct? 700ce3ee1e7SLuigi Rizzo } 701f9790aebSLuigi Rizzo netmap_do_unregif(priv, priv->np_nifp); 702f9790aebSLuigi Rizzo priv->np_nifp = NULL; 703ce3ee1e7SLuigi Rizzo netmap_drop_memory_locked(priv); 704f9790aebSLuigi Rizzo if (priv->np_na) { 705f9790aebSLuigi Rizzo netmap_adapter_put(na); 706f9790aebSLuigi Rizzo priv->np_na = NULL; 707ce3ee1e7SLuigi Rizzo } 708ce3ee1e7SLuigi Rizzo return 1; 709f196ce38SLuigi Rizzo } 7105819da83SLuigi Rizzo 711f9790aebSLuigi Rizzo 712f9790aebSLuigi Rizzo void 7135819da83SLuigi Rizzo netmap_dtor(void *data) 7145819da83SLuigi Rizzo { 7155819da83SLuigi Rizzo struct netmap_priv_d *priv = data; 716ce3ee1e7SLuigi Rizzo int last_instance; 7175819da83SLuigi Rizzo 718ce3ee1e7SLuigi Rizzo NMG_LOCK(); 719ce3ee1e7SLuigi Rizzo last_instance = netmap_dtor_locked(priv); 720ce3ee1e7SLuigi Rizzo NMG_UNLOCK(); 721ce3ee1e7SLuigi Rizzo if (last_instance) { 722ce3ee1e7SLuigi Rizzo bzero(priv, sizeof(*priv)); /* for safety */ 72368b8534bSLuigi Rizzo free(priv, M_DEVBUF); 72468b8534bSLuigi Rizzo } 725ce3ee1e7SLuigi Rizzo } 72668b8534bSLuigi Rizzo 727f18be576SLuigi Rizzo 72868b8534bSLuigi Rizzo 72968b8534bSLuigi Rizzo 73068b8534bSLuigi Rizzo /* 73102ad4083SLuigi Rizzo * Handlers for synchronization of the queues from/to the host. 732091fd0abSLuigi Rizzo * Netmap has two operating modes: 733091fd0abSLuigi Rizzo * - in the default mode, the rings connected to the host stack are 734091fd0abSLuigi Rizzo * just another ring pair managed by userspace; 735091fd0abSLuigi Rizzo * - in transparent mode (XXX to be defined) incoming packets 736091fd0abSLuigi Rizzo * (from the host or the NIC) are marked as NS_FORWARD upon 737091fd0abSLuigi Rizzo * arrival, and the user application has a chance to reset the 738091fd0abSLuigi Rizzo * flag for packets that should be dropped. 739091fd0abSLuigi Rizzo * On the RXSYNC or poll(), packets in RX rings between 740091fd0abSLuigi Rizzo * kring->nr_kcur and ring->cur with NS_FORWARD still set are moved 741091fd0abSLuigi Rizzo * to the other side. 742091fd0abSLuigi Rizzo * The transfer NIC --> host is relatively easy, just encapsulate 743091fd0abSLuigi Rizzo * into mbufs and we are done. The host --> NIC side is slightly 744091fd0abSLuigi Rizzo * harder because there might not be room in the tx ring so it 745091fd0abSLuigi Rizzo * might take a while before releasing the buffer. 746091fd0abSLuigi Rizzo */ 747091fd0abSLuigi Rizzo 748f18be576SLuigi Rizzo 749091fd0abSLuigi Rizzo /* 750091fd0abSLuigi Rizzo * pass a chain of buffers to the host stack as coming from 'dst' 75117885a7bSLuigi Rizzo * We do not need to lock because the queue is private. 752091fd0abSLuigi Rizzo */ 753091fd0abSLuigi Rizzo static void 754f9790aebSLuigi Rizzo netmap_send_up(struct ifnet *dst, struct mbq *q) 755091fd0abSLuigi Rizzo { 756091fd0abSLuigi Rizzo struct mbuf *m; 757091fd0abSLuigi Rizzo 758091fd0abSLuigi Rizzo /* send packets up, outside the lock */ 759f9790aebSLuigi Rizzo while ((m = mbq_dequeue(q)) != NULL) { 760091fd0abSLuigi Rizzo if (netmap_verbose & NM_VERB_HOST) 761091fd0abSLuigi Rizzo D("sending up pkt %p size %d", m, MBUF_LEN(m)); 762091fd0abSLuigi Rizzo NM_SEND_UP(dst, m); 763091fd0abSLuigi Rizzo } 764f9790aebSLuigi Rizzo mbq_destroy(q); 765091fd0abSLuigi Rizzo } 766091fd0abSLuigi Rizzo 767f18be576SLuigi Rizzo 768091fd0abSLuigi Rizzo /* 769091fd0abSLuigi Rizzo * put a copy of the buffers marked NS_FORWARD into an mbuf chain. 77017885a7bSLuigi Rizzo * Take packets from hwcur to ring->head marked NS_FORWARD (or forced) 77117885a7bSLuigi Rizzo * and pass them up. Drop remaining packets in the unlikely event 77217885a7bSLuigi Rizzo * of an mbuf shortage. 773091fd0abSLuigi Rizzo */ 774091fd0abSLuigi Rizzo static void 775091fd0abSLuigi Rizzo netmap_grab_packets(struct netmap_kring *kring, struct mbq *q, int force) 776091fd0abSLuigi Rizzo { 77717885a7bSLuigi Rizzo u_int const lim = kring->nkr_num_slots - 1; 77817885a7bSLuigi Rizzo u_int const head = kring->ring->head; 77917885a7bSLuigi Rizzo u_int n; 780f9790aebSLuigi Rizzo struct netmap_adapter *na = kring->na; 781091fd0abSLuigi Rizzo 78217885a7bSLuigi Rizzo for (n = kring->nr_hwcur; n != head; n = nm_next(n, lim)) { 78317885a7bSLuigi Rizzo struct mbuf *m; 784091fd0abSLuigi Rizzo struct netmap_slot *slot = &kring->ring->slot[n]; 785091fd0abSLuigi Rizzo 786091fd0abSLuigi Rizzo if ((slot->flags & NS_FORWARD) == 0 && !force) 787091fd0abSLuigi Rizzo continue; 788f9790aebSLuigi Rizzo if (slot->len < 14 || slot->len > NETMAP_BDG_BUF_SIZE(na->nm_mem)) { 78917885a7bSLuigi Rizzo RD(5, "bad pkt at %d len %d", n, slot->len); 790091fd0abSLuigi Rizzo continue; 791091fd0abSLuigi Rizzo } 792091fd0abSLuigi Rizzo slot->flags &= ~NS_FORWARD; // XXX needed ? 79317885a7bSLuigi Rizzo /* XXX TODO: adapt to the case of a multisegment packet */ 794f9790aebSLuigi Rizzo m = m_devget(BDG_NMB(na, slot), slot->len, 0, na->ifp, NULL); 795091fd0abSLuigi Rizzo 796091fd0abSLuigi Rizzo if (m == NULL) 797091fd0abSLuigi Rizzo break; 798f9790aebSLuigi Rizzo mbq_enqueue(q, m); 799091fd0abSLuigi Rizzo } 800091fd0abSLuigi Rizzo } 801091fd0abSLuigi Rizzo 802f18be576SLuigi Rizzo 803091fd0abSLuigi Rizzo /* 80417885a7bSLuigi Rizzo * Send to the NIC rings packets marked NS_FORWARD between 80517885a7bSLuigi Rizzo * kring->nr_hwcur and kring->rhead 80617885a7bSLuigi Rizzo * Called under kring->rx_queue.lock on the sw rx ring, 807091fd0abSLuigi Rizzo */ 80817885a7bSLuigi Rizzo static u_int 809091fd0abSLuigi Rizzo netmap_sw_to_nic(struct netmap_adapter *na) 810091fd0abSLuigi Rizzo { 811091fd0abSLuigi Rizzo struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings]; 81217885a7bSLuigi Rizzo struct netmap_slot *rxslot = kring->ring->slot; 81317885a7bSLuigi Rizzo u_int i, rxcur = kring->nr_hwcur; 81417885a7bSLuigi Rizzo u_int const head = kring->rhead; 81517885a7bSLuigi Rizzo u_int const src_lim = kring->nkr_num_slots - 1; 81617885a7bSLuigi Rizzo u_int sent = 0; 817ce3ee1e7SLuigi Rizzo 81817885a7bSLuigi Rizzo /* scan rings to find space, then fill as much as possible */ 81917885a7bSLuigi Rizzo for (i = 0; i < na->num_tx_rings; i++) { 82017885a7bSLuigi Rizzo struct netmap_kring *kdst = &na->tx_rings[i]; 82117885a7bSLuigi Rizzo struct netmap_ring *rdst = kdst->ring; 82217885a7bSLuigi Rizzo u_int const dst_lim = kdst->nkr_num_slots - 1; 823ce3ee1e7SLuigi Rizzo 82417885a7bSLuigi Rizzo /* XXX do we trust ring or kring->rcur,rtail ? */ 82517885a7bSLuigi Rizzo for (; rxcur != head && !nm_ring_empty(rdst); 82617885a7bSLuigi Rizzo rxcur = nm_next(rxcur, src_lim) ) { 827091fd0abSLuigi Rizzo struct netmap_slot *src, *dst, tmp; 82817885a7bSLuigi Rizzo u_int dst_cur = rdst->cur; 82917885a7bSLuigi Rizzo 83017885a7bSLuigi Rizzo src = &rxslot[rxcur]; 83117885a7bSLuigi Rizzo if ((src->flags & NS_FORWARD) == 0 && !netmap_fwd) 83217885a7bSLuigi Rizzo continue; 83317885a7bSLuigi Rizzo 83417885a7bSLuigi Rizzo sent++; 83517885a7bSLuigi Rizzo 83617885a7bSLuigi Rizzo dst = &rdst->slot[dst_cur]; 83717885a7bSLuigi Rizzo 838091fd0abSLuigi Rizzo tmp = *src; 83917885a7bSLuigi Rizzo 840091fd0abSLuigi Rizzo src->buf_idx = dst->buf_idx; 841091fd0abSLuigi Rizzo src->flags = NS_BUF_CHANGED; 842091fd0abSLuigi Rizzo 843091fd0abSLuigi Rizzo dst->buf_idx = tmp.buf_idx; 844091fd0abSLuigi Rizzo dst->len = tmp.len; 845091fd0abSLuigi Rizzo dst->flags = NS_BUF_CHANGED; 846091fd0abSLuigi Rizzo 84717885a7bSLuigi Rizzo rdst->cur = nm_next(dst_cur, dst_lim); 848091fd0abSLuigi Rizzo } 84917885a7bSLuigi Rizzo /* if (sent) XXX txsync ? */ 850091fd0abSLuigi Rizzo } 85117885a7bSLuigi Rizzo return sent; 852091fd0abSLuigi Rizzo } 853091fd0abSLuigi Rizzo 854f18be576SLuigi Rizzo 855091fd0abSLuigi Rizzo /* 856ce3ee1e7SLuigi Rizzo * netmap_txsync_to_host() passes packets up. We are called from a 85702ad4083SLuigi Rizzo * system call in user process context, and the only contention 85802ad4083SLuigi Rizzo * can be among multiple user threads erroneously calling 859091fd0abSLuigi Rizzo * this routine concurrently. 86068b8534bSLuigi Rizzo */ 861f9790aebSLuigi Rizzo void 862ce3ee1e7SLuigi Rizzo netmap_txsync_to_host(struct netmap_adapter *na) 86368b8534bSLuigi Rizzo { 864d76bf4ffSLuigi Rizzo struct netmap_kring *kring = &na->tx_rings[na->num_tx_rings]; 86568b8534bSLuigi Rizzo struct netmap_ring *ring = kring->ring; 86617885a7bSLuigi Rizzo u_int const lim = kring->nkr_num_slots - 1; 86717885a7bSLuigi Rizzo u_int const head = nm_txsync_prologue(kring); 868f9790aebSLuigi Rizzo struct mbq q; 869f9790aebSLuigi Rizzo int error; 87068b8534bSLuigi Rizzo 871f9790aebSLuigi Rizzo error = nm_kr_tryget(kring); 872f9790aebSLuigi Rizzo if (error) { 873f9790aebSLuigi Rizzo if (error == NM_KR_BUSY) 874ce3ee1e7SLuigi Rizzo D("ring %p busy (user error)", kring); 87502ad4083SLuigi Rizzo return; 87602ad4083SLuigi Rizzo } 87717885a7bSLuigi Rizzo if (head > lim) { 878ce3ee1e7SLuigi Rizzo D("invalid ring index in stack TX kring %p", kring); 879ce3ee1e7SLuigi Rizzo netmap_ring_reinit(kring); 880ce3ee1e7SLuigi Rizzo nm_kr_put(kring); 881ce3ee1e7SLuigi Rizzo return; 882ce3ee1e7SLuigi Rizzo } 88368b8534bSLuigi Rizzo 88417885a7bSLuigi Rizzo /* Take packets from hwcur to head and pass them up. 88517885a7bSLuigi Rizzo * force head = cur since netmap_grab_packets() stops at head 88668b8534bSLuigi Rizzo * In case of no buffers we give up. At the end of the loop, 88768b8534bSLuigi Rizzo * the queue is drained in all cases. 88868b8534bSLuigi Rizzo */ 889f9790aebSLuigi Rizzo mbq_init(&q); 89017885a7bSLuigi Rizzo ring->cur = head; 89117885a7bSLuigi Rizzo netmap_grab_packets(kring, &q, 1 /* force */); 89217885a7bSLuigi Rizzo ND("have %d pkts in queue", mbq_len(&q)); 89317885a7bSLuigi Rizzo kring->nr_hwcur = head; 89417885a7bSLuigi Rizzo kring->nr_hwtail = head + lim; 89517885a7bSLuigi Rizzo if (kring->nr_hwtail > lim) 89617885a7bSLuigi Rizzo kring->nr_hwtail -= lim + 1; 89717885a7bSLuigi Rizzo nm_txsync_finalize(kring); 89868b8534bSLuigi Rizzo 899ce3ee1e7SLuigi Rizzo nm_kr_put(kring); 900f9790aebSLuigi Rizzo netmap_send_up(na->ifp, &q); 901f18be576SLuigi Rizzo } 902f18be576SLuigi Rizzo 903f18be576SLuigi Rizzo 90468b8534bSLuigi Rizzo /* 90502ad4083SLuigi Rizzo * rxsync backend for packets coming from the host stack. 90617885a7bSLuigi Rizzo * They have been put in kring->rx_queue by netmap_transmit(). 90717885a7bSLuigi Rizzo * We protect access to the kring using kring->rx_queue.lock 90802ad4083SLuigi Rizzo * 90968b8534bSLuigi Rizzo * This routine also does the selrecord if called from the poll handler 91068b8534bSLuigi Rizzo * (we know because td != NULL). 91101c7d25fSLuigi Rizzo * 91201c7d25fSLuigi Rizzo * NOTE: on linux, selrecord() is defined as a macro and uses pwait 91301c7d25fSLuigi Rizzo * as an additional hidden argument. 91417885a7bSLuigi Rizzo * returns the number of packets delivered to tx queues in 91517885a7bSLuigi Rizzo * transparent mode, or a negative value if error 91668b8534bSLuigi Rizzo */ 91717885a7bSLuigi Rizzo int 918ce3ee1e7SLuigi Rizzo netmap_rxsync_from_host(struct netmap_adapter *na, struct thread *td, void *pwait) 91968b8534bSLuigi Rizzo { 920d76bf4ffSLuigi Rizzo struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings]; 92168b8534bSLuigi Rizzo struct netmap_ring *ring = kring->ring; 92217885a7bSLuigi Rizzo u_int nm_i, n; 92317885a7bSLuigi Rizzo u_int const lim = kring->nkr_num_slots - 1; 92417885a7bSLuigi Rizzo u_int const head = nm_rxsync_prologue(kring); 92517885a7bSLuigi Rizzo int ret = 0; 92617885a7bSLuigi Rizzo struct mbq *q = &kring->rx_queue; 92768b8534bSLuigi Rizzo 92801c7d25fSLuigi Rizzo (void)pwait; /* disable unused warnings */ 929ce3ee1e7SLuigi Rizzo 93017885a7bSLuigi Rizzo if (head > lim) { 93164ae02c3SLuigi Rizzo netmap_ring_reinit(kring); 93217885a7bSLuigi Rizzo return EINVAL; 93317885a7bSLuigi Rizzo } 93417885a7bSLuigi Rizzo 93517885a7bSLuigi Rizzo if (kring->nkr_stopped) /* check a first time without lock */ 93617885a7bSLuigi Rizzo return EBUSY; 93717885a7bSLuigi Rizzo 93817885a7bSLuigi Rizzo mtx_lock(&q->lock); 93917885a7bSLuigi Rizzo 94017885a7bSLuigi Rizzo if (kring->nkr_stopped) { /* check again with lock held */ 94117885a7bSLuigi Rizzo ret = EBUSY; 942ce3ee1e7SLuigi Rizzo goto unlock_out; 94364ae02c3SLuigi Rizzo } 94417885a7bSLuigi Rizzo 94517885a7bSLuigi Rizzo /* First part: import newly received packets */ 94617885a7bSLuigi Rizzo n = mbq_len(q); 94717885a7bSLuigi Rizzo if (n) { /* grab packets from the queue */ 94817885a7bSLuigi Rizzo struct mbuf *m; 94917885a7bSLuigi Rizzo uint32_t stop_i; 95017885a7bSLuigi Rizzo 95117885a7bSLuigi Rizzo nm_i = kring->nr_hwtail; 95217885a7bSLuigi Rizzo stop_i = nm_prev(nm_i, lim); 95317885a7bSLuigi Rizzo while ( nm_i != stop_i && (m = mbq_dequeue(q)) != NULL ) { 95417885a7bSLuigi Rizzo int len = MBUF_LEN(m); 95517885a7bSLuigi Rizzo struct netmap_slot *slot = &ring->slot[nm_i]; 95617885a7bSLuigi Rizzo 95717885a7bSLuigi Rizzo m_copydata(m, 0, len, BDG_NMB(na, slot)); 95817885a7bSLuigi Rizzo ND("nm %d len %d", nm_i, len); 95917885a7bSLuigi Rizzo if (netmap_verbose) 96017885a7bSLuigi Rizzo D("%s", nm_dump_buf(BDG_NMB(na, slot),len, 128, NULL)); 96117885a7bSLuigi Rizzo 96217885a7bSLuigi Rizzo slot->len = len; 96317885a7bSLuigi Rizzo slot->flags = kring->nkr_slot_flags; 96417885a7bSLuigi Rizzo nm_i = nm_next(nm_i, lim); 96564ae02c3SLuigi Rizzo } 96617885a7bSLuigi Rizzo kring->nr_hwtail = nm_i; 96764ae02c3SLuigi Rizzo } 96817885a7bSLuigi Rizzo 96917885a7bSLuigi Rizzo /* 97017885a7bSLuigi Rizzo * Second part: skip past packets that userspace has released. 97117885a7bSLuigi Rizzo */ 97217885a7bSLuigi Rizzo nm_i = kring->nr_hwcur; 97317885a7bSLuigi Rizzo if (nm_i != head) { /* something was released */ 97417885a7bSLuigi Rizzo if (netmap_fwd || kring->ring->flags & NR_FORWARD) 97517885a7bSLuigi Rizzo ret = netmap_sw_to_nic(na); 97617885a7bSLuigi Rizzo kring->nr_hwcur = head; 97764ae02c3SLuigi Rizzo } 97817885a7bSLuigi Rizzo 97917885a7bSLuigi Rizzo nm_rxsync_finalize(kring); 98017885a7bSLuigi Rizzo 98117885a7bSLuigi Rizzo /* access copies of cur,tail in the kring */ 98217885a7bSLuigi Rizzo if (kring->rcur == kring->rtail && td) /* no bufs available */ 98368b8534bSLuigi Rizzo selrecord(td, &kring->si); 98417885a7bSLuigi Rizzo 985ce3ee1e7SLuigi Rizzo unlock_out: 986ce3ee1e7SLuigi Rizzo 98717885a7bSLuigi Rizzo mtx_unlock(&q->lock); 98817885a7bSLuigi Rizzo return ret; 98968b8534bSLuigi Rizzo } 99068b8534bSLuigi Rizzo 99168b8534bSLuigi Rizzo 992f9790aebSLuigi Rizzo /* Get a netmap adapter for the port. 993f9790aebSLuigi Rizzo * 994f9790aebSLuigi Rizzo * If it is possible to satisfy the request, return 0 995f9790aebSLuigi Rizzo * with *na containing the netmap adapter found. 996f9790aebSLuigi Rizzo * Otherwise return an error code, with *na containing NULL. 997f9790aebSLuigi Rizzo * 998f9790aebSLuigi Rizzo * When the port is attached to a bridge, we always return 999f9790aebSLuigi Rizzo * EBUSY. 1000f9790aebSLuigi Rizzo * Otherwise, if the port is already bound to a file descriptor, 1001f9790aebSLuigi Rizzo * then we unconditionally return the existing adapter into *na. 1002f9790aebSLuigi Rizzo * In all the other cases, we return (into *na) either native, 1003f9790aebSLuigi Rizzo * generic or NULL, according to the following table: 1004f9790aebSLuigi Rizzo * 1005f9790aebSLuigi Rizzo * native_support 1006f9790aebSLuigi Rizzo * active_fds dev.netmap.admode YES NO 1007f9790aebSLuigi Rizzo * ------------------------------------------------------- 1008f9790aebSLuigi Rizzo * >0 * NA(ifp) NA(ifp) 1009f9790aebSLuigi Rizzo * 1010f9790aebSLuigi Rizzo * 0 NETMAP_ADMODE_BEST NATIVE GENERIC 1011f9790aebSLuigi Rizzo * 0 NETMAP_ADMODE_NATIVE NATIVE NULL 1012f9790aebSLuigi Rizzo * 0 NETMAP_ADMODE_GENERIC GENERIC GENERIC 1013f9790aebSLuigi Rizzo * 1014f9790aebSLuigi Rizzo */ 1015f9790aebSLuigi Rizzo 1016f9790aebSLuigi Rizzo int 1017f9790aebSLuigi Rizzo netmap_get_hw_na(struct ifnet *ifp, struct netmap_adapter **na) 1018f9790aebSLuigi Rizzo { 1019f9790aebSLuigi Rizzo /* generic support */ 1020f9790aebSLuigi Rizzo int i = netmap_admode; /* Take a snapshot. */ 1021f9790aebSLuigi Rizzo int error = 0; 1022f9790aebSLuigi Rizzo struct netmap_adapter *prev_na; 1023f9790aebSLuigi Rizzo struct netmap_generic_adapter *gna; 1024f9790aebSLuigi Rizzo 1025f9790aebSLuigi Rizzo *na = NULL; /* default */ 1026f9790aebSLuigi Rizzo 1027f9790aebSLuigi Rizzo /* reset in case of invalid value */ 1028f9790aebSLuigi Rizzo if (i < NETMAP_ADMODE_BEST || i >= NETMAP_ADMODE_LAST) 1029f9790aebSLuigi Rizzo i = netmap_admode = NETMAP_ADMODE_BEST; 1030f9790aebSLuigi Rizzo 1031f9790aebSLuigi Rizzo if (NETMAP_CAPABLE(ifp)) { 1032f9790aebSLuigi Rizzo /* If an adapter already exists, but is 1033f9790aebSLuigi Rizzo * attached to a vale port, we report that the 1034f9790aebSLuigi Rizzo * port is busy. 1035f9790aebSLuigi Rizzo */ 1036f9790aebSLuigi Rizzo if (NETMAP_OWNED_BY_KERN(NA(ifp))) 1037f9790aebSLuigi Rizzo return EBUSY; 1038f9790aebSLuigi Rizzo 1039f9790aebSLuigi Rizzo /* If an adapter already exists, return it if 1040f9790aebSLuigi Rizzo * there are active file descriptors or if 1041f9790aebSLuigi Rizzo * netmap is not forced to use generic 1042f9790aebSLuigi Rizzo * adapters. 1043f9790aebSLuigi Rizzo */ 1044f9790aebSLuigi Rizzo if (NA(ifp)->active_fds > 0 || 1045f9790aebSLuigi Rizzo i != NETMAP_ADMODE_GENERIC) { 1046f9790aebSLuigi Rizzo *na = NA(ifp); 1047f9790aebSLuigi Rizzo return 0; 1048f9790aebSLuigi Rizzo } 1049f9790aebSLuigi Rizzo } 1050f9790aebSLuigi Rizzo 1051f9790aebSLuigi Rizzo /* If there isn't native support and netmap is not allowed 1052f9790aebSLuigi Rizzo * to use generic adapters, we cannot satisfy the request. 1053f9790aebSLuigi Rizzo */ 1054f9790aebSLuigi Rizzo if (!NETMAP_CAPABLE(ifp) && i == NETMAP_ADMODE_NATIVE) 1055f9790aebSLuigi Rizzo return EINVAL; 1056f9790aebSLuigi Rizzo 1057f9790aebSLuigi Rizzo /* Otherwise, create a generic adapter and return it, 1058f9790aebSLuigi Rizzo * saving the previously used netmap adapter, if any. 1059f9790aebSLuigi Rizzo * 1060f9790aebSLuigi Rizzo * Note that here 'prev_na', if not NULL, MUST be a 1061f9790aebSLuigi Rizzo * native adapter, and CANNOT be a generic one. This is 1062f9790aebSLuigi Rizzo * true because generic adapters are created on demand, and 1063f9790aebSLuigi Rizzo * destroyed when not used anymore. Therefore, if the adapter 1064f9790aebSLuigi Rizzo * currently attached to an interface 'ifp' is generic, it 1065f9790aebSLuigi Rizzo * must be that 1066f9790aebSLuigi Rizzo * (NA(ifp)->active_fds > 0 || NETMAP_OWNED_BY_KERN(NA(ifp))). 1067f9790aebSLuigi Rizzo * Consequently, if NA(ifp) is generic, we will enter one of 1068f9790aebSLuigi Rizzo * the branches above. This ensures that we never override 1069f9790aebSLuigi Rizzo * a generic adapter with another generic adapter. 1070f9790aebSLuigi Rizzo */ 1071f9790aebSLuigi Rizzo prev_na = NA(ifp); 1072f9790aebSLuigi Rizzo error = generic_netmap_attach(ifp); 1073f9790aebSLuigi Rizzo if (error) 1074f9790aebSLuigi Rizzo return error; 1075f9790aebSLuigi Rizzo 1076f9790aebSLuigi Rizzo *na = NA(ifp); 1077f9790aebSLuigi Rizzo gna = (struct netmap_generic_adapter*)NA(ifp); 1078f9790aebSLuigi Rizzo gna->prev = prev_na; /* save old na */ 1079f9790aebSLuigi Rizzo if (prev_na != NULL) { 1080f9790aebSLuigi Rizzo ifunit_ref(ifp->if_xname); 1081f9790aebSLuigi Rizzo // XXX add a refcount ? 1082f9790aebSLuigi Rizzo netmap_adapter_get(prev_na); 1083f9790aebSLuigi Rizzo } 108417885a7bSLuigi Rizzo ND("Created generic NA %p (prev %p)", gna, gna->prev); 1085f9790aebSLuigi Rizzo 1086f9790aebSLuigi Rizzo return 0; 1087f9790aebSLuigi Rizzo } 1088f9790aebSLuigi Rizzo 1089f9790aebSLuigi Rizzo 109068b8534bSLuigi Rizzo /* 1091ce3ee1e7SLuigi Rizzo * MUST BE CALLED UNDER NMG_LOCK() 1092ce3ee1e7SLuigi Rizzo * 109368b8534bSLuigi Rizzo * get a refcounted reference to an interface. 1094ce3ee1e7SLuigi Rizzo * This is always called in the execution of an ioctl(). 1095ce3ee1e7SLuigi Rizzo * 109668b8534bSLuigi Rizzo * Return ENXIO if the interface does not exist, EINVAL if netmap 109768b8534bSLuigi Rizzo * is not supported by the interface. 109868b8534bSLuigi Rizzo * If successful, hold a reference. 1099f18be576SLuigi Rizzo * 1100ce3ee1e7SLuigi Rizzo * When the NIC is attached to a bridge, reference is managed 1101f18be576SLuigi Rizzo * at na->na_bdg_refcount using ADD/DROP_BDG_REF() as well as 1102f18be576SLuigi Rizzo * virtual ports. Hence, on the final DROP_BDG_REF(), the NIC 1103f18be576SLuigi Rizzo * is detached from the bridge, then ifp's refcount is dropped (this 1104f18be576SLuigi Rizzo * is equivalent to that ifp is destroyed in case of virtual ports. 1105f18be576SLuigi Rizzo * 1106f18be576SLuigi Rizzo * This function uses if_rele() when we want to prevent the NIC from 1107f18be576SLuigi Rizzo * being detached from the bridge in error handling. But once refcount 1108f18be576SLuigi Rizzo * is acquired by this function, it must be released using nm_if_rele(). 110968b8534bSLuigi Rizzo */ 1110f9790aebSLuigi Rizzo int 1111f9790aebSLuigi Rizzo netmap_get_na(struct nmreq *nmr, struct netmap_adapter **na, int create) 111268b8534bSLuigi Rizzo { 1113f9790aebSLuigi Rizzo struct ifnet *ifp; 1114f9790aebSLuigi Rizzo int error = 0; 1115f9790aebSLuigi Rizzo struct netmap_adapter *ret; 1116f9790aebSLuigi Rizzo 1117f9790aebSLuigi Rizzo *na = NULL; /* default return value */ 1118f196ce38SLuigi Rizzo 1119ce3ee1e7SLuigi Rizzo /* first try to see if this is a bridge port. */ 1120ce3ee1e7SLuigi Rizzo NMG_LOCK_ASSERT(); 1121ce3ee1e7SLuigi Rizzo 1122f9790aebSLuigi Rizzo error = netmap_get_bdg_na(nmr, na, create); 1123f9790aebSLuigi Rizzo if (error || *na != NULL) /* valid match in netmap_get_bdg_na() */ 1124f9790aebSLuigi Rizzo return error; 1125ce3ee1e7SLuigi Rizzo 1126f9790aebSLuigi Rizzo ifp = ifunit_ref(nmr->nr_name); 1127f9790aebSLuigi Rizzo if (ifp == NULL) { 1128ce3ee1e7SLuigi Rizzo return ENXIO; 1129f196ce38SLuigi Rizzo } 1130ce3ee1e7SLuigi Rizzo 1131f9790aebSLuigi Rizzo error = netmap_get_hw_na(ifp, &ret); 1132f9790aebSLuigi Rizzo if (error) 1133f9790aebSLuigi Rizzo goto out; 1134f18be576SLuigi Rizzo 1135f9790aebSLuigi Rizzo if (ret != NULL) { 1136f9790aebSLuigi Rizzo /* Users cannot use the NIC attached to a bridge directly */ 1137f9790aebSLuigi Rizzo if (NETMAP_OWNED_BY_KERN(ret)) { 1138f9790aebSLuigi Rizzo error = EINVAL; 1139f9790aebSLuigi Rizzo goto out; 1140ce3ee1e7SLuigi Rizzo } 1141f9790aebSLuigi Rizzo error = 0; 1142f9790aebSLuigi Rizzo *na = ret; 1143f9790aebSLuigi Rizzo netmap_adapter_get(ret); 1144f9790aebSLuigi Rizzo } 1145f9790aebSLuigi Rizzo out: 1146f9790aebSLuigi Rizzo if_rele(ifp); 1147f18be576SLuigi Rizzo 11485ab0d24dSLuigi Rizzo return error; 11495ab0d24dSLuigi Rizzo } 1150ce3ee1e7SLuigi Rizzo 1151ce3ee1e7SLuigi Rizzo 1152f9790aebSLuigi Rizzo /* 1153f9790aebSLuigi Rizzo * validate parameters on entry for *_txsync() 1154f9790aebSLuigi Rizzo * Returns ring->cur if ok, or something >= kring->nkr_num_slots 115517885a7bSLuigi Rizzo * in case of error. 1156f9790aebSLuigi Rizzo * 115717885a7bSLuigi Rizzo * rhead, rcur and rtail=hwtail are stored from previous round. 115817885a7bSLuigi Rizzo * hwcur is the next packet to send to the ring. 1159f9790aebSLuigi Rizzo * 116017885a7bSLuigi Rizzo * We want 116117885a7bSLuigi Rizzo * hwcur <= *rhead <= head <= cur <= tail = *rtail <= hwtail 1162f9790aebSLuigi Rizzo * 116317885a7bSLuigi Rizzo * hwcur, rhead, rtail and hwtail are reliable 1164f9790aebSLuigi Rizzo */ 1165f9790aebSLuigi Rizzo u_int 116617885a7bSLuigi Rizzo nm_txsync_prologue(struct netmap_kring *kring) 1167f9790aebSLuigi Rizzo { 1168f9790aebSLuigi Rizzo struct netmap_ring *ring = kring->ring; 116917885a7bSLuigi Rizzo u_int head = ring->head; /* read only once */ 1170f9790aebSLuigi Rizzo u_int cur = ring->cur; /* read only once */ 1171f9790aebSLuigi Rizzo u_int n = kring->nkr_num_slots; 1172ce3ee1e7SLuigi Rizzo 117317885a7bSLuigi Rizzo ND(5, "%s kcur %d ktail %d head %d cur %d tail %d", 117417885a7bSLuigi Rizzo kring->name, 117517885a7bSLuigi Rizzo kring->nr_hwcur, kring->nr_hwtail, 117617885a7bSLuigi Rizzo ring->head, ring->cur, ring->tail); 117717885a7bSLuigi Rizzo #if 1 /* kernel sanity checks; but we can trust the kring. */ 117817885a7bSLuigi Rizzo if (kring->nr_hwcur >= n || kring->rhead >= n || 117917885a7bSLuigi Rizzo kring->rtail >= n || kring->nr_hwtail >= n) 1180f9790aebSLuigi Rizzo goto error; 1181f9790aebSLuigi Rizzo #endif /* kernel sanity checks */ 118217885a7bSLuigi Rizzo /* 118317885a7bSLuigi Rizzo * user sanity checks. We only use 'cur', 118417885a7bSLuigi Rizzo * A, B, ... are possible positions for cur: 118517885a7bSLuigi Rizzo * 118617885a7bSLuigi Rizzo * 0 A cur B tail C n-1 118717885a7bSLuigi Rizzo * 0 D tail E cur F n-1 118817885a7bSLuigi Rizzo * 118917885a7bSLuigi Rizzo * B, F, D are valid. A, C, E are wrong 119017885a7bSLuigi Rizzo */ 119117885a7bSLuigi Rizzo if (kring->rtail >= kring->rhead) { 119217885a7bSLuigi Rizzo /* want rhead <= head <= rtail */ 119317885a7bSLuigi Rizzo if (head < kring->rhead || head > kring->rtail) 1194f9790aebSLuigi Rizzo goto error; 119517885a7bSLuigi Rizzo /* and also head <= cur <= rtail */ 119617885a7bSLuigi Rizzo if (cur < head || cur > kring->rtail) 1197f9790aebSLuigi Rizzo goto error; 119817885a7bSLuigi Rizzo } else { /* here rtail < rhead */ 119917885a7bSLuigi Rizzo /* we need head outside rtail .. rhead */ 120017885a7bSLuigi Rizzo if (head > kring->rtail && head < kring->rhead) 120117885a7bSLuigi Rizzo goto error; 120217885a7bSLuigi Rizzo 120317885a7bSLuigi Rizzo /* two cases now: head <= rtail or head >= rhead */ 120417885a7bSLuigi Rizzo if (head <= kring->rtail) { 120517885a7bSLuigi Rizzo /* want head <= cur <= rtail */ 120617885a7bSLuigi Rizzo if (cur < head || cur > kring->rtail) 120717885a7bSLuigi Rizzo goto error; 120817885a7bSLuigi Rizzo } else { /* head >= rhead */ 120917885a7bSLuigi Rizzo /* cur must be outside rtail..head */ 121017885a7bSLuigi Rizzo if (cur > kring->rtail && cur < head) 121117885a7bSLuigi Rizzo goto error; 1212f18be576SLuigi Rizzo } 1213f9790aebSLuigi Rizzo } 121417885a7bSLuigi Rizzo if (ring->tail != kring->rtail) { 121517885a7bSLuigi Rizzo RD(5, "tail overwritten was %d need %d", 121617885a7bSLuigi Rizzo ring->tail, kring->rtail); 121717885a7bSLuigi Rizzo ring->tail = kring->rtail; 121817885a7bSLuigi Rizzo } 121917885a7bSLuigi Rizzo kring->rhead = head; 122017885a7bSLuigi Rizzo kring->rcur = cur; 122117885a7bSLuigi Rizzo return head; 1222f9790aebSLuigi Rizzo 1223f9790aebSLuigi Rizzo error: 122417885a7bSLuigi Rizzo RD(5, "%s kring error: hwcur %d rcur %d hwtail %d cur %d tail %d", 122517885a7bSLuigi Rizzo kring->name, 1226f9790aebSLuigi Rizzo kring->nr_hwcur, 122717885a7bSLuigi Rizzo kring->rcur, kring->nr_hwtail, 122817885a7bSLuigi Rizzo cur, ring->tail); 1229f9790aebSLuigi Rizzo return n; 123068b8534bSLuigi Rizzo } 123168b8534bSLuigi Rizzo 123268b8534bSLuigi Rizzo 123368b8534bSLuigi Rizzo /* 1234f9790aebSLuigi Rizzo * validate parameters on entry for *_rxsync() 123517885a7bSLuigi Rizzo * Returns ring->head if ok, kring->nkr_num_slots on error. 1236f9790aebSLuigi Rizzo * 123717885a7bSLuigi Rizzo * For a valid configuration, 123817885a7bSLuigi Rizzo * hwcur <= head <= cur <= tail <= hwtail 1239f9790aebSLuigi Rizzo * 124017885a7bSLuigi Rizzo * We only consider head and cur. 124117885a7bSLuigi Rizzo * hwcur and hwtail are reliable. 1242f9790aebSLuigi Rizzo * 1243f9790aebSLuigi Rizzo */ 1244f9790aebSLuigi Rizzo u_int 124517885a7bSLuigi Rizzo nm_rxsync_prologue(struct netmap_kring *kring) 1246f9790aebSLuigi Rizzo { 1247f9790aebSLuigi Rizzo struct netmap_ring *ring = kring->ring; 124817885a7bSLuigi Rizzo uint32_t const n = kring->nkr_num_slots; 124917885a7bSLuigi Rizzo uint32_t head, cur; 1250f9790aebSLuigi Rizzo 125117885a7bSLuigi Rizzo ND("%s kc %d kt %d h %d c %d t %d", 125217885a7bSLuigi Rizzo kring->name, 125317885a7bSLuigi Rizzo kring->nr_hwcur, kring->nr_hwtail, 125417885a7bSLuigi Rizzo ring->head, ring->cur, ring->tail); 125517885a7bSLuigi Rizzo /* 125617885a7bSLuigi Rizzo * Before storing the new values, we should check they do not 125717885a7bSLuigi Rizzo * move backwards. However: 125817885a7bSLuigi Rizzo * - head is not an issue because the previous value is hwcur; 125917885a7bSLuigi Rizzo * - cur could in principle go back, however it does not matter 126017885a7bSLuigi Rizzo * because we are processing a brand new rxsync() 126117885a7bSLuigi Rizzo */ 126217885a7bSLuigi Rizzo cur = kring->rcur = ring->cur; /* read only once */ 126317885a7bSLuigi Rizzo head = kring->rhead = ring->head; /* read only once */ 1264f9790aebSLuigi Rizzo #if 1 /* kernel sanity checks */ 126517885a7bSLuigi Rizzo if (kring->nr_hwcur >= n || kring->nr_hwtail >= n) 1266f9790aebSLuigi Rizzo goto error; 1267f9790aebSLuigi Rizzo #endif /* kernel sanity checks */ 1268f9790aebSLuigi Rizzo /* user sanity checks */ 126917885a7bSLuigi Rizzo if (kring->nr_hwtail >= kring->nr_hwcur) { 127017885a7bSLuigi Rizzo /* want hwcur <= rhead <= hwtail */ 127117885a7bSLuigi Rizzo if (head < kring->nr_hwcur || head > kring->nr_hwtail) 1272f9790aebSLuigi Rizzo goto error; 127317885a7bSLuigi Rizzo /* and also rhead <= rcur <= hwtail */ 127417885a7bSLuigi Rizzo if (cur < head || cur > kring->nr_hwtail) 1275f9790aebSLuigi Rizzo goto error; 1276f9790aebSLuigi Rizzo } else { 127717885a7bSLuigi Rizzo /* we need rhead outside hwtail..hwcur */ 127817885a7bSLuigi Rizzo if (head < kring->nr_hwcur && head > kring->nr_hwtail) 1279f9790aebSLuigi Rizzo goto error; 128017885a7bSLuigi Rizzo /* two cases now: head <= hwtail or head >= hwcur */ 128117885a7bSLuigi Rizzo if (head <= kring->nr_hwtail) { 128217885a7bSLuigi Rizzo /* want head <= cur <= hwtail */ 128317885a7bSLuigi Rizzo if (cur < head || cur > kring->nr_hwtail) 1284f9790aebSLuigi Rizzo goto error; 128517885a7bSLuigi Rizzo } else { 128617885a7bSLuigi Rizzo /* cur must be outside hwtail..head */ 128717885a7bSLuigi Rizzo if (cur < head && cur > kring->nr_hwtail) 1288f9790aebSLuigi Rizzo goto error; 1289f9790aebSLuigi Rizzo } 1290f9790aebSLuigi Rizzo } 129117885a7bSLuigi Rizzo if (ring->tail != kring->rtail) { 129217885a7bSLuigi Rizzo RD(5, "%s tail overwritten was %d need %d", 129317885a7bSLuigi Rizzo kring->name, 129417885a7bSLuigi Rizzo ring->tail, kring->rtail); 129517885a7bSLuigi Rizzo ring->tail = kring->rtail; 129617885a7bSLuigi Rizzo } 129717885a7bSLuigi Rizzo return head; 1298f9790aebSLuigi Rizzo 1299f9790aebSLuigi Rizzo error: 130017885a7bSLuigi Rizzo RD(5, "kring error: hwcur %d rcur %d hwtail %d head %d cur %d tail %d", 1301f9790aebSLuigi Rizzo kring->nr_hwcur, 130217885a7bSLuigi Rizzo kring->rcur, kring->nr_hwtail, 130317885a7bSLuigi Rizzo kring->rhead, kring->rcur, ring->tail); 1304f9790aebSLuigi Rizzo return n; 1305f9790aebSLuigi Rizzo } 1306f9790aebSLuigi Rizzo 130717885a7bSLuigi Rizzo 1308f9790aebSLuigi Rizzo /* 130968b8534bSLuigi Rizzo * Error routine called when txsync/rxsync detects an error. 131017885a7bSLuigi Rizzo * Can't do much more than resetting head =cur = hwcur, tail = hwtail 131168b8534bSLuigi Rizzo * Return 1 on reinit. 1312506cc70cSLuigi Rizzo * 1313506cc70cSLuigi Rizzo * This routine is only called by the upper half of the kernel. 1314506cc70cSLuigi Rizzo * It only reads hwcur (which is changed only by the upper half, too) 131517885a7bSLuigi Rizzo * and hwtail (which may be changed by the lower half, but only on 1316506cc70cSLuigi Rizzo * a tx ring and only to increase it, so any error will be recovered 1317506cc70cSLuigi Rizzo * on the next call). For the above, we don't strictly need to call 1318506cc70cSLuigi Rizzo * it under lock. 131968b8534bSLuigi Rizzo */ 132068b8534bSLuigi Rizzo int 132168b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring) 132268b8534bSLuigi Rizzo { 132368b8534bSLuigi Rizzo struct netmap_ring *ring = kring->ring; 132468b8534bSLuigi Rizzo u_int i, lim = kring->nkr_num_slots - 1; 132568b8534bSLuigi Rizzo int errors = 0; 132668b8534bSLuigi Rizzo 1327ce3ee1e7SLuigi Rizzo // XXX KASSERT nm_kr_tryget 1328f9790aebSLuigi Rizzo RD(10, "called for %s", NM_IFPNAME(kring->na->ifp)); 132917885a7bSLuigi Rizzo // XXX probably wrong to trust userspace 133017885a7bSLuigi Rizzo kring->rhead = ring->head; 133117885a7bSLuigi Rizzo kring->rcur = ring->cur; 133217885a7bSLuigi Rizzo kring->rtail = ring->tail; 133317885a7bSLuigi Rizzo 133468b8534bSLuigi Rizzo if (ring->cur > lim) 133568b8534bSLuigi Rizzo errors++; 133617885a7bSLuigi Rizzo if (ring->head > lim) 133717885a7bSLuigi Rizzo errors++; 133817885a7bSLuigi Rizzo if (ring->tail > lim) 133917885a7bSLuigi Rizzo errors++; 134068b8534bSLuigi Rizzo for (i = 0; i <= lim; i++) { 134168b8534bSLuigi Rizzo u_int idx = ring->slot[i].buf_idx; 134268b8534bSLuigi Rizzo u_int len = ring->slot[i].len; 134368b8534bSLuigi Rizzo if (idx < 2 || idx >= netmap_total_buffers) { 134417885a7bSLuigi Rizzo RD(5, "bad index at slot %d idx %d len %d ", i, idx, len); 134568b8534bSLuigi Rizzo ring->slot[i].buf_idx = 0; 134668b8534bSLuigi Rizzo ring->slot[i].len = 0; 1347ce3ee1e7SLuigi Rizzo } else if (len > NETMAP_BDG_BUF_SIZE(kring->na->nm_mem)) { 134868b8534bSLuigi Rizzo ring->slot[i].len = 0; 134917885a7bSLuigi Rizzo RD(5, "bad len at slot %d idx %d len %d", i, idx, len); 135068b8534bSLuigi Rizzo } 135168b8534bSLuigi Rizzo } 135268b8534bSLuigi Rizzo if (errors) { 13538241616dSLuigi Rizzo RD(10, "total %d errors", errors); 135417885a7bSLuigi Rizzo RD(10, "%s reinit, cur %d -> %d tail %d -> %d", 135517885a7bSLuigi Rizzo kring->name, 135668b8534bSLuigi Rizzo ring->cur, kring->nr_hwcur, 135717885a7bSLuigi Rizzo ring->tail, kring->nr_hwtail); 135817885a7bSLuigi Rizzo ring->head = kring->rhead = kring->nr_hwcur; 135917885a7bSLuigi Rizzo ring->cur = kring->rcur = kring->nr_hwcur; 136017885a7bSLuigi Rizzo ring->tail = kring->rtail = kring->nr_hwtail; 136168b8534bSLuigi Rizzo } 136268b8534bSLuigi Rizzo return (errors ? 1 : 0); 136368b8534bSLuigi Rizzo } 136468b8534bSLuigi Rizzo 136568b8534bSLuigi Rizzo 136668b8534bSLuigi Rizzo /* 136768b8534bSLuigi Rizzo * Set the ring ID. For devices with a single queue, a request 136868b8534bSLuigi Rizzo * for all rings is the same as a single ring. 136968b8534bSLuigi Rizzo */ 137068b8534bSLuigi Rizzo static int 137168b8534bSLuigi Rizzo netmap_set_ringid(struct netmap_priv_d *priv, u_int ringid) 137268b8534bSLuigi Rizzo { 1373f9790aebSLuigi Rizzo struct netmap_adapter *na = priv->np_na; 1374f9790aebSLuigi Rizzo struct ifnet *ifp = na->ifp; 137568b8534bSLuigi Rizzo u_int i = ringid & NETMAP_RING_MASK; 137664ae02c3SLuigi Rizzo /* initially (np_qfirst == np_qlast) we don't want to lock */ 1377ce3ee1e7SLuigi Rizzo u_int lim = na->num_rx_rings; 137868b8534bSLuigi Rizzo 1379d76bf4ffSLuigi Rizzo if (na->num_tx_rings > lim) 1380d76bf4ffSLuigi Rizzo lim = na->num_tx_rings; 138164ae02c3SLuigi Rizzo if ( (ringid & NETMAP_HW_RING) && i >= lim) { 138268b8534bSLuigi Rizzo D("invalid ring id %d", i); 138368b8534bSLuigi Rizzo return (EINVAL); 138468b8534bSLuigi Rizzo } 138568b8534bSLuigi Rizzo priv->np_ringid = ringid; 138668b8534bSLuigi Rizzo if (ringid & NETMAP_SW_RING) { 138764ae02c3SLuigi Rizzo priv->np_qfirst = NETMAP_SW_RING; 138864ae02c3SLuigi Rizzo priv->np_qlast = 0; 138968b8534bSLuigi Rizzo } else if (ringid & NETMAP_HW_RING) { 139068b8534bSLuigi Rizzo priv->np_qfirst = i; 139168b8534bSLuigi Rizzo priv->np_qlast = i + 1; 139268b8534bSLuigi Rizzo } else { 139368b8534bSLuigi Rizzo priv->np_qfirst = 0; 139464ae02c3SLuigi Rizzo priv->np_qlast = NETMAP_HW_RING ; 139568b8534bSLuigi Rizzo } 139668b8534bSLuigi Rizzo priv->np_txpoll = (ringid & NETMAP_NO_TX_POLL) ? 0 : 1; 1397ae10d1afSLuigi Rizzo if (netmap_verbose) { 139868b8534bSLuigi Rizzo if (ringid & NETMAP_SW_RING) 1399f9790aebSLuigi Rizzo D("ringid %s set to SW RING", NM_IFPNAME(ifp)); 140068b8534bSLuigi Rizzo else if (ringid & NETMAP_HW_RING) 1401f9790aebSLuigi Rizzo D("ringid %s set to HW RING %d", NM_IFPNAME(ifp), 140268b8534bSLuigi Rizzo priv->np_qfirst); 140368b8534bSLuigi Rizzo else 1404f9790aebSLuigi Rizzo D("ringid %s set to all %d HW RINGS", NM_IFPNAME(ifp), lim); 1405ae10d1afSLuigi Rizzo } 140668b8534bSLuigi Rizzo return 0; 140768b8534bSLuigi Rizzo } 140868b8534bSLuigi Rizzo 1409f18be576SLuigi Rizzo 1410f18be576SLuigi Rizzo /* 1411f18be576SLuigi Rizzo * possibly move the interface to netmap-mode. 1412f18be576SLuigi Rizzo * If success it returns a pointer to netmap_if, otherwise NULL. 1413ce3ee1e7SLuigi Rizzo * This must be called with NMG_LOCK held. 1414f18be576SLuigi Rizzo */ 1415f9790aebSLuigi Rizzo struct netmap_if * 1416f9790aebSLuigi Rizzo netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na, 1417f18be576SLuigi Rizzo uint16_t ringid, int *err) 1418f18be576SLuigi Rizzo { 1419f9790aebSLuigi Rizzo struct ifnet *ifp = na->ifp; 1420f18be576SLuigi Rizzo struct netmap_if *nifp = NULL; 1421f9790aebSLuigi Rizzo int error, need_mem = 0; 1422f18be576SLuigi Rizzo 1423ce3ee1e7SLuigi Rizzo NMG_LOCK_ASSERT(); 1424f18be576SLuigi Rizzo /* ring configuration may have changed, fetch from the card */ 1425f18be576SLuigi Rizzo netmap_update_config(na); 1426f9790aebSLuigi Rizzo priv->np_na = na; /* store the reference */ 1427f18be576SLuigi Rizzo error = netmap_set_ringid(priv, ringid); 1428f18be576SLuigi Rizzo if (error) 1429f18be576SLuigi Rizzo goto out; 1430ce3ee1e7SLuigi Rizzo /* ensure allocators are ready */ 1431ce3ee1e7SLuigi Rizzo need_mem = !netmap_have_memory_locked(priv); 1432ce3ee1e7SLuigi Rizzo if (need_mem) { 1433ce3ee1e7SLuigi Rizzo error = netmap_get_memory_locked(priv); 1434ce3ee1e7SLuigi Rizzo ND("get_memory returned %d", error); 1435ce3ee1e7SLuigi Rizzo if (error) 1436ce3ee1e7SLuigi Rizzo goto out; 1437ce3ee1e7SLuigi Rizzo } 1438f9790aebSLuigi Rizzo nifp = netmap_if_new(NM_IFPNAME(ifp), na); 1439f18be576SLuigi Rizzo if (nifp == NULL) { /* allocation failed */ 1440ce3ee1e7SLuigi Rizzo /* we should drop the allocator, but only 1441ce3ee1e7SLuigi Rizzo * if we were the ones who grabbed it 1442ce3ee1e7SLuigi Rizzo */ 1443f18be576SLuigi Rizzo error = ENOMEM; 1444ce3ee1e7SLuigi Rizzo goto out; 1445ce3ee1e7SLuigi Rizzo } 1446f9790aebSLuigi Rizzo na->active_fds++; 1447ce3ee1e7SLuigi Rizzo if (ifp->if_capenable & IFCAP_NETMAP) { 1448f18be576SLuigi Rizzo /* was already set */ 1449f18be576SLuigi Rizzo } else { 1450f18be576SLuigi Rizzo /* Otherwise set the card in netmap mode 1451f18be576SLuigi Rizzo * and make it use the shared buffers. 1452ce3ee1e7SLuigi Rizzo * 1453ce3ee1e7SLuigi Rizzo * do not core lock because the race is harmless here, 1454ce3ee1e7SLuigi Rizzo * there cannot be any traffic to netmap_transmit() 1455ce3ee1e7SLuigi Rizzo */ 1456f9790aebSLuigi Rizzo na->na_lut = na->nm_mem->pools[NETMAP_BUF_POOL].lut; 1457f9790aebSLuigi Rizzo ND("%p->na_lut == %p", na, na->na_lut); 1458f9790aebSLuigi Rizzo na->na_lut_objtotal = na->nm_mem->pools[NETMAP_BUF_POOL].objtotal; 1459f9790aebSLuigi Rizzo error = na->nm_register(na, 1); /* mode on */ 1460f18be576SLuigi Rizzo if (error) { 1461ce3ee1e7SLuigi Rizzo netmap_do_unregif(priv, nifp); 1462f18be576SLuigi Rizzo nifp = NULL; 1463f18be576SLuigi Rizzo } 1464f18be576SLuigi Rizzo } 1465f18be576SLuigi Rizzo out: 1466f18be576SLuigi Rizzo *err = error; 1467f9790aebSLuigi Rizzo if (error) { 1468f9790aebSLuigi Rizzo priv->np_na = NULL; 1469f9790aebSLuigi Rizzo if (need_mem) 1470f9790aebSLuigi Rizzo netmap_drop_memory_locked(priv); 1471f9790aebSLuigi Rizzo } 1472ce3ee1e7SLuigi Rizzo if (nifp != NULL) { 1473ce3ee1e7SLuigi Rizzo /* 1474ce3ee1e7SLuigi Rizzo * advertise that the interface is ready bt setting ni_nifp. 1475ce3ee1e7SLuigi Rizzo * The barrier is needed because readers (poll and *SYNC) 1476ce3ee1e7SLuigi Rizzo * check for priv->np_nifp != NULL without locking 1477ce3ee1e7SLuigi Rizzo */ 1478ce3ee1e7SLuigi Rizzo wmb(); /* make sure previous writes are visible to all CPUs */ 1479ce3ee1e7SLuigi Rizzo priv->np_nifp = nifp; 1480ce3ee1e7SLuigi Rizzo } 1481f18be576SLuigi Rizzo return nifp; 1482f18be576SLuigi Rizzo } 1483f18be576SLuigi Rizzo 1484f18be576SLuigi Rizzo 1485f18be576SLuigi Rizzo 148668b8534bSLuigi Rizzo /* 148768b8534bSLuigi Rizzo * ioctl(2) support for the "netmap" device. 148868b8534bSLuigi Rizzo * 148968b8534bSLuigi Rizzo * Following a list of accepted commands: 149068b8534bSLuigi Rizzo * - NIOCGINFO 149168b8534bSLuigi Rizzo * - SIOCGIFADDR just for convenience 149268b8534bSLuigi Rizzo * - NIOCREGIF 149368b8534bSLuigi Rizzo * - NIOCTXSYNC 149468b8534bSLuigi Rizzo * - NIOCRXSYNC 149568b8534bSLuigi Rizzo * 149668b8534bSLuigi Rizzo * Return 0 on success, errno otherwise. 149768b8534bSLuigi Rizzo */ 1498f9790aebSLuigi Rizzo int 14990b8ed8e0SLuigi Rizzo netmap_ioctl(struct cdev *dev, u_long cmd, caddr_t data, 15000b8ed8e0SLuigi Rizzo int fflag, struct thread *td) 150168b8534bSLuigi Rizzo { 150268b8534bSLuigi Rizzo struct netmap_priv_d *priv = NULL; 1503ce3ee1e7SLuigi Rizzo struct ifnet *ifp = NULL; 150468b8534bSLuigi Rizzo struct nmreq *nmr = (struct nmreq *) data; 1505ce3ee1e7SLuigi Rizzo struct netmap_adapter *na = NULL; 150668b8534bSLuigi Rizzo int error; 150764ae02c3SLuigi Rizzo u_int i, lim; 150868b8534bSLuigi Rizzo struct netmap_if *nifp; 1509ce3ee1e7SLuigi Rizzo struct netmap_kring *krings; 151068b8534bSLuigi Rizzo 15110b8ed8e0SLuigi Rizzo (void)dev; /* UNUSED */ 15120b8ed8e0SLuigi Rizzo (void)fflag; /* UNUSED */ 1513f196ce38SLuigi Rizzo #ifdef linux 1514f196ce38SLuigi Rizzo #define devfs_get_cdevpriv(pp) \ 1515f196ce38SLuigi Rizzo ({ *(struct netmap_priv_d **)pp = ((struct file *)td)->private_data; \ 1516f196ce38SLuigi Rizzo (*pp ? 0 : ENOENT); }) 1517f196ce38SLuigi Rizzo 1518f196ce38SLuigi Rizzo /* devfs_set_cdevpriv cannot fail on linux */ 1519f196ce38SLuigi Rizzo #define devfs_set_cdevpriv(p, fn) \ 1520f196ce38SLuigi Rizzo ({ ((struct file *)td)->private_data = p; (p ? 0 : EINVAL); }) 1521f196ce38SLuigi Rizzo 1522f196ce38SLuigi Rizzo 1523f196ce38SLuigi Rizzo #define devfs_clear_cdevpriv() do { \ 1524f196ce38SLuigi Rizzo netmap_dtor(priv); ((struct file *)td)->private_data = 0; \ 1525f196ce38SLuigi Rizzo } while (0) 1526f196ce38SLuigi Rizzo #endif /* linux */ 1527f196ce38SLuigi Rizzo 152817885a7bSLuigi Rizzo if (cmd == NIOCGINFO || cmd == NIOCREGIF) { 152917885a7bSLuigi Rizzo /* truncate name */ 153017885a7bSLuigi Rizzo nmr->nr_name[sizeof(nmr->nr_name) - 1] = '\0'; 153117885a7bSLuigi Rizzo if (nmr->nr_version != NETMAP_API) { 153217885a7bSLuigi Rizzo D("API mismatch for %s got %d need %d", 153317885a7bSLuigi Rizzo nmr->nr_name, 153417885a7bSLuigi Rizzo nmr->nr_version, NETMAP_API); 153517885a7bSLuigi Rizzo nmr->nr_version = NETMAP_API; 153617885a7bSLuigi Rizzo return EINVAL; 153717885a7bSLuigi Rizzo } 153817885a7bSLuigi Rizzo } 1539506cc70cSLuigi Rizzo CURVNET_SET(TD_TO_VNET(td)); 1540506cc70cSLuigi Rizzo 154168b8534bSLuigi Rizzo error = devfs_get_cdevpriv((void **)&priv); 15428241616dSLuigi Rizzo if (error) { 1543506cc70cSLuigi Rizzo CURVNET_RESTORE(); 15448241616dSLuigi Rizzo /* XXX ENOENT should be impossible, since the priv 15458241616dSLuigi Rizzo * is now created in the open */ 15468241616dSLuigi Rizzo return (error == ENOENT ? ENXIO : error); 1547506cc70cSLuigi Rizzo } 154868b8534bSLuigi Rizzo 154968b8534bSLuigi Rizzo switch (cmd) { 155068b8534bSLuigi Rizzo case NIOCGINFO: /* return capabilities etc */ 1551f18be576SLuigi Rizzo if (nmr->nr_cmd == NETMAP_BDG_LIST) { 1552f18be576SLuigi Rizzo error = netmap_bdg_ctl(nmr, NULL); 1553f18be576SLuigi Rizzo break; 1554f18be576SLuigi Rizzo } 1555ce3ee1e7SLuigi Rizzo 1556ce3ee1e7SLuigi Rizzo NMG_LOCK(); 1557ce3ee1e7SLuigi Rizzo do { 1558ce3ee1e7SLuigi Rizzo /* memsize is always valid */ 1559ce3ee1e7SLuigi Rizzo struct netmap_mem_d *nmd = &nm_mem; 1560ce3ee1e7SLuigi Rizzo u_int memflags; 1561ce3ee1e7SLuigi Rizzo 1562ce3ee1e7SLuigi Rizzo if (nmr->nr_name[0] != '\0') { 1563ce3ee1e7SLuigi Rizzo /* get a refcount */ 1564f9790aebSLuigi Rizzo error = netmap_get_na(nmr, &na, 1 /* create */); 15658241616dSLuigi Rizzo if (error) 15668241616dSLuigi Rizzo break; 1567f9790aebSLuigi Rizzo nmd = na->nm_mem; /* get memory allocator */ 1568ce3ee1e7SLuigi Rizzo } 1569ce3ee1e7SLuigi Rizzo 1570ce3ee1e7SLuigi Rizzo error = netmap_mem_get_info(nmd, &nmr->nr_memsize, &memflags); 1571ce3ee1e7SLuigi Rizzo if (error) 1572ce3ee1e7SLuigi Rizzo break; 1573ce3ee1e7SLuigi Rizzo if (na == NULL) /* only memory info */ 1574ce3ee1e7SLuigi Rizzo break; 15758241616dSLuigi Rizzo nmr->nr_offset = 0; 15768241616dSLuigi Rizzo nmr->nr_rx_slots = nmr->nr_tx_slots = 0; 1577ae10d1afSLuigi Rizzo netmap_update_config(na); 1578d76bf4ffSLuigi Rizzo nmr->nr_rx_rings = na->num_rx_rings; 1579d76bf4ffSLuigi Rizzo nmr->nr_tx_rings = na->num_tx_rings; 158064ae02c3SLuigi Rizzo nmr->nr_rx_slots = na->num_rx_desc; 158164ae02c3SLuigi Rizzo nmr->nr_tx_slots = na->num_tx_desc; 1582ce3ee1e7SLuigi Rizzo if (memflags & NETMAP_MEM_PRIVATE) 1583ce3ee1e7SLuigi Rizzo nmr->nr_ringid |= NETMAP_PRIV_MEM; 1584f9790aebSLuigi Rizzo netmap_adapter_put(na); 1585ce3ee1e7SLuigi Rizzo } while (0); 1586ce3ee1e7SLuigi Rizzo NMG_UNLOCK(); 158768b8534bSLuigi Rizzo break; 158868b8534bSLuigi Rizzo 158968b8534bSLuigi Rizzo case NIOCREGIF: 1590f18be576SLuigi Rizzo /* possibly attach/detach NIC and VALE switch */ 1591f18be576SLuigi Rizzo i = nmr->nr_cmd; 1592f9790aebSLuigi Rizzo if (i == NETMAP_BDG_ATTACH || i == NETMAP_BDG_DETACH 1593f9790aebSLuigi Rizzo || i == NETMAP_BDG_OFFSET) { 1594f18be576SLuigi Rizzo error = netmap_bdg_ctl(nmr, NULL); 1595f18be576SLuigi Rizzo break; 1596f18be576SLuigi Rizzo } else if (i != 0) { 1597f18be576SLuigi Rizzo D("nr_cmd must be 0 not %d", i); 1598f18be576SLuigi Rizzo error = EINVAL; 1599f18be576SLuigi Rizzo break; 1600f18be576SLuigi Rizzo } 1601f18be576SLuigi Rizzo 16028241616dSLuigi Rizzo /* protect access to priv from concurrent NIOCREGIF */ 1603ce3ee1e7SLuigi Rizzo NMG_LOCK(); 1604ce3ee1e7SLuigi Rizzo do { 1605ce3ee1e7SLuigi Rizzo u_int memflags; 1606ce3ee1e7SLuigi Rizzo 1607f9790aebSLuigi Rizzo if (priv->np_na != NULL) { /* thread already registered */ 1608506cc70cSLuigi Rizzo error = netmap_set_ringid(priv, nmr->nr_ringid); 1609506cc70cSLuigi Rizzo break; 1610506cc70cSLuigi Rizzo } 161168b8534bSLuigi Rizzo /* find the interface and a reference */ 1612f9790aebSLuigi Rizzo error = netmap_get_na(nmr, &na, 1 /* create */); /* keep reference */ 161368b8534bSLuigi Rizzo if (error) 1614ce3ee1e7SLuigi Rizzo break; 1615f9790aebSLuigi Rizzo ifp = na->ifp; 1616f9790aebSLuigi Rizzo if (NETMAP_OWNED_BY_KERN(na)) { 1617f9790aebSLuigi Rizzo netmap_adapter_put(na); 1618ce3ee1e7SLuigi Rizzo error = EBUSY; 1619ce3ee1e7SLuigi Rizzo break; 1620f196ce38SLuigi Rizzo } 1621f9790aebSLuigi Rizzo nifp = netmap_do_regif(priv, na, nmr->nr_ringid, &error); 1622f18be576SLuigi Rizzo if (!nifp) { /* reg. failed, release priv and ref */ 1623f9790aebSLuigi Rizzo netmap_adapter_put(na); 16248241616dSLuigi Rizzo priv->np_nifp = NULL; 1625ce3ee1e7SLuigi Rizzo break; 162668b8534bSLuigi Rizzo } 162768b8534bSLuigi Rizzo 162868b8534bSLuigi Rizzo /* return the offset of the netmap_if object */ 1629d76bf4ffSLuigi Rizzo nmr->nr_rx_rings = na->num_rx_rings; 1630d76bf4ffSLuigi Rizzo nmr->nr_tx_rings = na->num_tx_rings; 163164ae02c3SLuigi Rizzo nmr->nr_rx_slots = na->num_rx_desc; 163264ae02c3SLuigi Rizzo nmr->nr_tx_slots = na->num_tx_desc; 1633ce3ee1e7SLuigi Rizzo error = netmap_mem_get_info(na->nm_mem, &nmr->nr_memsize, &memflags); 1634ce3ee1e7SLuigi Rizzo if (error) { 1635f9790aebSLuigi Rizzo netmap_adapter_put(na); 1636ce3ee1e7SLuigi Rizzo break; 1637ce3ee1e7SLuigi Rizzo } 1638ce3ee1e7SLuigi Rizzo if (memflags & NETMAP_MEM_PRIVATE) { 1639ce3ee1e7SLuigi Rizzo nmr->nr_ringid |= NETMAP_PRIV_MEM; 16403d819cb6SLuigi Rizzo *(uint32_t *)(uintptr_t)&nifp->ni_flags |= NI_PRIV_MEM; 1641ce3ee1e7SLuigi Rizzo } 1642ce3ee1e7SLuigi Rizzo nmr->nr_offset = netmap_mem_if_offset(na->nm_mem, nifp); 1643ce3ee1e7SLuigi Rizzo } while (0); 1644ce3ee1e7SLuigi Rizzo NMG_UNLOCK(); 164568b8534bSLuigi Rizzo break; 164668b8534bSLuigi Rizzo 164768b8534bSLuigi Rizzo case NIOCTXSYNC: 164868b8534bSLuigi Rizzo case NIOCRXSYNC: 16498241616dSLuigi Rizzo nifp = priv->np_nifp; 16508241616dSLuigi Rizzo 16518241616dSLuigi Rizzo if (nifp == NULL) { 1652506cc70cSLuigi Rizzo error = ENXIO; 1653506cc70cSLuigi Rizzo break; 1654506cc70cSLuigi Rizzo } 16558241616dSLuigi Rizzo rmb(); /* make sure following reads are not from cache */ 16568241616dSLuigi Rizzo 1657f9790aebSLuigi Rizzo na = priv->np_na; /* we have a reference */ 16588241616dSLuigi Rizzo 1659f9790aebSLuigi Rizzo if (na == NULL) { 1660f9790aebSLuigi Rizzo D("Internal error: nifp != NULL && na == NULL"); 16618241616dSLuigi Rizzo error = ENXIO; 16628241616dSLuigi Rizzo break; 16638241616dSLuigi Rizzo } 16648241616dSLuigi Rizzo 1665f9790aebSLuigi Rizzo ifp = na->ifp; 1666f9790aebSLuigi Rizzo if (ifp == NULL) { 1667f9790aebSLuigi Rizzo RD(1, "the ifp is gone"); 1668f9790aebSLuigi Rizzo error = ENXIO; 1669f9790aebSLuigi Rizzo break; 1670f9790aebSLuigi Rizzo } 1671f9790aebSLuigi Rizzo 167264ae02c3SLuigi Rizzo if (priv->np_qfirst == NETMAP_SW_RING) { /* host rings */ 167368b8534bSLuigi Rizzo if (cmd == NIOCTXSYNC) 1674ce3ee1e7SLuigi Rizzo netmap_txsync_to_host(na); 167568b8534bSLuigi Rizzo else 1676ce3ee1e7SLuigi Rizzo netmap_rxsync_from_host(na, NULL, NULL); 1677506cc70cSLuigi Rizzo break; 167868b8534bSLuigi Rizzo } 167964ae02c3SLuigi Rizzo /* find the last ring to scan */ 168064ae02c3SLuigi Rizzo lim = priv->np_qlast; 168164ae02c3SLuigi Rizzo if (lim == NETMAP_HW_RING) 16823c0caf6cSLuigi Rizzo lim = (cmd == NIOCTXSYNC) ? 1683d76bf4ffSLuigi Rizzo na->num_tx_rings : na->num_rx_rings; 168468b8534bSLuigi Rizzo 1685ce3ee1e7SLuigi Rizzo krings = (cmd == NIOCTXSYNC) ? na->tx_rings : na->rx_rings; 168664ae02c3SLuigi Rizzo for (i = priv->np_qfirst; i < lim; i++) { 1687ce3ee1e7SLuigi Rizzo struct netmap_kring *kring = krings + i; 1688ce3ee1e7SLuigi Rizzo if (nm_kr_tryget(kring)) { 1689ce3ee1e7SLuigi Rizzo error = EBUSY; 1690ce3ee1e7SLuigi Rizzo goto out; 1691ce3ee1e7SLuigi Rizzo } 169268b8534bSLuigi Rizzo if (cmd == NIOCTXSYNC) { 169368b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_TXSYNC) 16943c0caf6cSLuigi Rizzo D("pre txsync ring %d cur %d hwcur %d", 169568b8534bSLuigi Rizzo i, kring->ring->cur, 169668b8534bSLuigi Rizzo kring->nr_hwcur); 169717885a7bSLuigi Rizzo if (nm_txsync_prologue(kring) >= kring->nkr_num_slots) { 169817885a7bSLuigi Rizzo netmap_ring_reinit(kring); 169917885a7bSLuigi Rizzo } else { 1700f9790aebSLuigi Rizzo na->nm_txsync(na, i, NAF_FORCE_RECLAIM); 170117885a7bSLuigi Rizzo } 170268b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_TXSYNC) 17033c0caf6cSLuigi Rizzo D("post txsync ring %d cur %d hwcur %d", 170468b8534bSLuigi Rizzo i, kring->ring->cur, 170568b8534bSLuigi Rizzo kring->nr_hwcur); 170668b8534bSLuigi Rizzo } else { 1707f9790aebSLuigi Rizzo na->nm_rxsync(na, i, NAF_FORCE_READ); 170868b8534bSLuigi Rizzo microtime(&na->rx_rings[i].ring->ts); 170968b8534bSLuigi Rizzo } 1710ce3ee1e7SLuigi Rizzo nm_kr_put(kring); 171168b8534bSLuigi Rizzo } 171268b8534bSLuigi Rizzo 171368b8534bSLuigi Rizzo break; 171468b8534bSLuigi Rizzo 1715f196ce38SLuigi Rizzo #ifdef __FreeBSD__ 171668b8534bSLuigi Rizzo case BIOCIMMEDIATE: 171768b8534bSLuigi Rizzo case BIOCGHDRCMPLT: 171868b8534bSLuigi Rizzo case BIOCSHDRCMPLT: 171968b8534bSLuigi Rizzo case BIOCSSEESENT: 172068b8534bSLuigi Rizzo D("ignore BIOCIMMEDIATE/BIOCSHDRCMPLT/BIOCSHDRCMPLT/BIOCSSEESENT"); 172168b8534bSLuigi Rizzo break; 172268b8534bSLuigi Rizzo 1723babc7c12SLuigi Rizzo default: /* allow device-specific ioctls */ 172468b8534bSLuigi Rizzo { 172568b8534bSLuigi Rizzo struct socket so; 1726ce3ee1e7SLuigi Rizzo 172768b8534bSLuigi Rizzo bzero(&so, sizeof(so)); 1728ce3ee1e7SLuigi Rizzo NMG_LOCK(); 1729f9790aebSLuigi Rizzo error = netmap_get_na(nmr, &na, 0 /* don't create */); /* keep reference */ 1730ce3ee1e7SLuigi Rizzo if (error) { 1731f9790aebSLuigi Rizzo netmap_adapter_put(na); 1732ce3ee1e7SLuigi Rizzo NMG_UNLOCK(); 173368b8534bSLuigi Rizzo break; 1734ce3ee1e7SLuigi Rizzo } 1735f9790aebSLuigi Rizzo ifp = na->ifp; 173668b8534bSLuigi Rizzo so.so_vnet = ifp->if_vnet; 173768b8534bSLuigi Rizzo // so->so_proto not null. 173868b8534bSLuigi Rizzo error = ifioctl(&so, cmd, data, td); 1739f9790aebSLuigi Rizzo netmap_adapter_put(na); 1740ce3ee1e7SLuigi Rizzo NMG_UNLOCK(); 1741babc7c12SLuigi Rizzo break; 174268b8534bSLuigi Rizzo } 1743f196ce38SLuigi Rizzo 1744f196ce38SLuigi Rizzo #else /* linux */ 1745f196ce38SLuigi Rizzo default: 1746f196ce38SLuigi Rizzo error = EOPNOTSUPP; 1747f196ce38SLuigi Rizzo #endif /* linux */ 174868b8534bSLuigi Rizzo } 1749ce3ee1e7SLuigi Rizzo out: 175068b8534bSLuigi Rizzo 1751506cc70cSLuigi Rizzo CURVNET_RESTORE(); 175268b8534bSLuigi Rizzo return (error); 175368b8534bSLuigi Rizzo } 175468b8534bSLuigi Rizzo 175568b8534bSLuigi Rizzo 175668b8534bSLuigi Rizzo /* 175768b8534bSLuigi Rizzo * select(2) and poll(2) handlers for the "netmap" device. 175868b8534bSLuigi Rizzo * 175968b8534bSLuigi Rizzo * Can be called for one or more queues. 176068b8534bSLuigi Rizzo * Return true the event mask corresponding to ready events. 176168b8534bSLuigi Rizzo * If there are no ready events, do a selrecord on either individual 1762ce3ee1e7SLuigi Rizzo * selinfo or on the global one. 176368b8534bSLuigi Rizzo * Device-dependent parts (locking and sync of tx/rx rings) 176468b8534bSLuigi Rizzo * are done through callbacks. 1765f196ce38SLuigi Rizzo * 176601c7d25fSLuigi Rizzo * On linux, arguments are really pwait, the poll table, and 'td' is struct file * 176701c7d25fSLuigi Rizzo * The first one is remapped to pwait as selrecord() uses the name as an 176801c7d25fSLuigi Rizzo * hidden argument. 176968b8534bSLuigi Rizzo */ 1770f9790aebSLuigi Rizzo int 177101c7d25fSLuigi Rizzo netmap_poll(struct cdev *dev, int events, struct thread *td) 177268b8534bSLuigi Rizzo { 177368b8534bSLuigi Rizzo struct netmap_priv_d *priv = NULL; 177468b8534bSLuigi Rizzo struct netmap_adapter *na; 177568b8534bSLuigi Rizzo struct ifnet *ifp; 177668b8534bSLuigi Rizzo struct netmap_kring *kring; 1777954dca4cSLuigi Rizzo u_int i, check_all_tx, check_all_rx, want_tx, want_rx, revents = 0; 177817885a7bSLuigi Rizzo u_int lim_tx, lim_rx; 177917885a7bSLuigi Rizzo struct mbq q; /* packets from hw queues to host stack */ 178001c7d25fSLuigi Rizzo void *pwait = dev; /* linux compatibility */ 178101c7d25fSLuigi Rizzo 1782f9790aebSLuigi Rizzo /* 1783f9790aebSLuigi Rizzo * In order to avoid nested locks, we need to "double check" 1784f9790aebSLuigi Rizzo * txsync and rxsync if we decide to do a selrecord(). 1785f9790aebSLuigi Rizzo * retry_tx (and retry_rx, later) prevent looping forever. 1786f9790aebSLuigi Rizzo */ 178717885a7bSLuigi Rizzo int retry_tx = 1, retry_rx = 1; 1788ce3ee1e7SLuigi Rizzo 178901c7d25fSLuigi Rizzo (void)pwait; 1790f9790aebSLuigi Rizzo mbq_init(&q); 179168b8534bSLuigi Rizzo 179268b8534bSLuigi Rizzo if (devfs_get_cdevpriv((void **)&priv) != 0 || priv == NULL) 179368b8534bSLuigi Rizzo return POLLERR; 179468b8534bSLuigi Rizzo 17958241616dSLuigi Rizzo if (priv->np_nifp == NULL) { 17968241616dSLuigi Rizzo D("No if registered"); 17978241616dSLuigi Rizzo return POLLERR; 17988241616dSLuigi Rizzo } 17998241616dSLuigi Rizzo rmb(); /* make sure following reads are not from cache */ 18008241616dSLuigi Rizzo 1801f9790aebSLuigi Rizzo na = priv->np_na; 1802f9790aebSLuigi Rizzo ifp = na->ifp; 1803f9790aebSLuigi Rizzo // check for deleted 1804f9790aebSLuigi Rizzo if (ifp == NULL) { 1805f9790aebSLuigi Rizzo RD(1, "the ifp is gone"); 1806f9790aebSLuigi Rizzo return POLLERR; 1807f9790aebSLuigi Rizzo } 1808f9790aebSLuigi Rizzo 180968b8534bSLuigi Rizzo if ( (ifp->if_capenable & IFCAP_NETMAP) == 0) 181068b8534bSLuigi Rizzo return POLLERR; 181168b8534bSLuigi Rizzo 181268b8534bSLuigi Rizzo if (netmap_verbose & 0x8000) 1813f9790aebSLuigi Rizzo D("device %s events 0x%x", NM_IFPNAME(ifp), events); 181468b8534bSLuigi Rizzo want_tx = events & (POLLOUT | POLLWRNORM); 181568b8534bSLuigi Rizzo want_rx = events & (POLLIN | POLLRDNORM); 181668b8534bSLuigi Rizzo 1817d76bf4ffSLuigi Rizzo lim_tx = na->num_tx_rings; 1818d76bf4ffSLuigi Rizzo lim_rx = na->num_rx_rings; 1819ce3ee1e7SLuigi Rizzo 182064ae02c3SLuigi Rizzo if (priv->np_qfirst == NETMAP_SW_RING) { 182117885a7bSLuigi Rizzo // XXX locking ? 1822ce3ee1e7SLuigi Rizzo /* handle the host stack ring */ 182368b8534bSLuigi Rizzo if (priv->np_txpoll || want_tx) { 182468b8534bSLuigi Rizzo /* push any packets up, then we are always ready */ 1825ce3ee1e7SLuigi Rizzo netmap_txsync_to_host(na); 182668b8534bSLuigi Rizzo revents |= want_tx; 182768b8534bSLuigi Rizzo } 182868b8534bSLuigi Rizzo if (want_rx) { 182964ae02c3SLuigi Rizzo kring = &na->rx_rings[lim_rx]; 183017885a7bSLuigi Rizzo /* XXX replace with rxprologue etc. */ 183117885a7bSLuigi Rizzo if (nm_ring_empty(kring->ring)) 1832ce3ee1e7SLuigi Rizzo netmap_rxsync_from_host(na, td, dev); 183317885a7bSLuigi Rizzo if (!nm_ring_empty(kring->ring)) 183468b8534bSLuigi Rizzo revents |= want_rx; 183568b8534bSLuigi Rizzo } 183668b8534bSLuigi Rizzo return (revents); 183768b8534bSLuigi Rizzo } 183868b8534bSLuigi Rizzo 1839091fd0abSLuigi Rizzo 184068b8534bSLuigi Rizzo /* 1841f9790aebSLuigi Rizzo * check_all_{tx|rx} are set if the card has more than one queue AND 1842f9790aebSLuigi Rizzo * the file descriptor is bound to all of them. If so, we sleep on 1843ce3ee1e7SLuigi Rizzo * the "global" selinfo, otherwise we sleep on individual selinfo 1844ce3ee1e7SLuigi Rizzo * (FreeBSD only allows two selinfo's per file descriptor). 1845ce3ee1e7SLuigi Rizzo * The interrupt routine in the driver wake one or the other 1846ce3ee1e7SLuigi Rizzo * (or both) depending on which clients are active. 184768b8534bSLuigi Rizzo * 184868b8534bSLuigi Rizzo * rxsync() is only called if we run out of buffers on a POLLIN. 184968b8534bSLuigi Rizzo * txsync() is called if we run out of buffers on POLLOUT, or 185068b8534bSLuigi Rizzo * there are pending packets to send. The latter can be disabled 185168b8534bSLuigi Rizzo * passing NETMAP_NO_TX_POLL in the NIOCREG call. 185268b8534bSLuigi Rizzo */ 1853954dca4cSLuigi Rizzo check_all_tx = (priv->np_qlast == NETMAP_HW_RING) && (lim_tx > 1); 1854954dca4cSLuigi Rizzo check_all_rx = (priv->np_qlast == NETMAP_HW_RING) && (lim_rx > 1); 185568b8534bSLuigi Rizzo 185664ae02c3SLuigi Rizzo if (priv->np_qlast != NETMAP_HW_RING) { 185764ae02c3SLuigi Rizzo lim_tx = lim_rx = priv->np_qlast; 185864ae02c3SLuigi Rizzo } 185964ae02c3SLuigi Rizzo 186068b8534bSLuigi Rizzo /* 1861f9790aebSLuigi Rizzo * We start with a lock free round which is cheap if we have 1862f9790aebSLuigi Rizzo * slots available. If this fails, then lock and call the sync 186368b8534bSLuigi Rizzo * routines. 186468b8534bSLuigi Rizzo */ 186564ae02c3SLuigi Rizzo for (i = priv->np_qfirst; want_rx && i < lim_rx; i++) { 186668b8534bSLuigi Rizzo kring = &na->rx_rings[i]; 186717885a7bSLuigi Rizzo /* XXX compare ring->cur and kring->tail */ 186817885a7bSLuigi Rizzo if (!nm_ring_empty(kring->ring)) { 186968b8534bSLuigi Rizzo revents |= want_rx; 187068b8534bSLuigi Rizzo want_rx = 0; /* also breaks the loop */ 187168b8534bSLuigi Rizzo } 187268b8534bSLuigi Rizzo } 187364ae02c3SLuigi Rizzo for (i = priv->np_qfirst; want_tx && i < lim_tx; i++) { 187468b8534bSLuigi Rizzo kring = &na->tx_rings[i]; 187517885a7bSLuigi Rizzo /* XXX compare ring->cur and kring->tail */ 187617885a7bSLuigi Rizzo if (!nm_ring_empty(kring->ring)) { 187768b8534bSLuigi Rizzo revents |= want_tx; 187868b8534bSLuigi Rizzo want_tx = 0; /* also breaks the loop */ 187968b8534bSLuigi Rizzo } 188068b8534bSLuigi Rizzo } 188168b8534bSLuigi Rizzo 188268b8534bSLuigi Rizzo /* 188317885a7bSLuigi Rizzo * If we want to push packets out (priv->np_txpoll) or 188417885a7bSLuigi Rizzo * want_tx is still set, we must issue txsync calls 188517885a7bSLuigi Rizzo * (on all rings, to avoid that the tx rings stall). 1886f9790aebSLuigi Rizzo * XXX should also check cur != hwcur on the tx rings. 1887f9790aebSLuigi Rizzo * Fortunately, normal tx mode has np_txpoll set. 188868b8534bSLuigi Rizzo */ 188968b8534bSLuigi Rizzo if (priv->np_txpoll || want_tx) { 189017885a7bSLuigi Rizzo /* 189117885a7bSLuigi Rizzo * The first round checks if anyone is ready, if not 189217885a7bSLuigi Rizzo * do a selrecord and another round to handle races. 189317885a7bSLuigi Rizzo * want_tx goes to 0 if any space is found, and is 189417885a7bSLuigi Rizzo * used to skip rings with no pending transmissions. 1895ce3ee1e7SLuigi Rizzo */ 1896091fd0abSLuigi Rizzo flush_tx: 189764ae02c3SLuigi Rizzo for (i = priv->np_qfirst; i < lim_tx; i++) { 189817885a7bSLuigi Rizzo int found = 0; 189917885a7bSLuigi Rizzo 190068b8534bSLuigi Rizzo kring = &na->tx_rings[i]; 190168b8534bSLuigi Rizzo if (!want_tx && kring->ring->cur == kring->nr_hwcur) 190268b8534bSLuigi Rizzo continue; 190317885a7bSLuigi Rizzo /* only one thread does txsync */ 1904ce3ee1e7SLuigi Rizzo if (nm_kr_tryget(kring)) { 190517885a7bSLuigi Rizzo D("%p lost race on txring %d, ok", priv, i); 190617885a7bSLuigi Rizzo continue; 190768b8534bSLuigi Rizzo } 190817885a7bSLuigi Rizzo if (nm_txsync_prologue(kring) >= kring->nkr_num_slots) { 190917885a7bSLuigi Rizzo netmap_ring_reinit(kring); 191017885a7bSLuigi Rizzo revents |= POLLERR; 191117885a7bSLuigi Rizzo } else { 1912f9790aebSLuigi Rizzo if (na->nm_txsync(na, i, 0)) 191368b8534bSLuigi Rizzo revents |= POLLERR; 191417885a7bSLuigi Rizzo } 191568b8534bSLuigi Rizzo 191617885a7bSLuigi Rizzo /* 191717885a7bSLuigi Rizzo * If we found new slots, notify potential 191817885a7bSLuigi Rizzo * listeners on the same ring. 191917885a7bSLuigi Rizzo * Since we just did a txsync, look at the copies 192017885a7bSLuigi Rizzo * of cur,tail in the kring. 1921f9790aebSLuigi Rizzo */ 192217885a7bSLuigi Rizzo found = kring->rcur != kring->rtail; 192317885a7bSLuigi Rizzo nm_kr_put(kring); 192417885a7bSLuigi Rizzo if (found) { /* notify other listeners */ 192568b8534bSLuigi Rizzo revents |= want_tx; 192668b8534bSLuigi Rizzo want_tx = 0; 192717885a7bSLuigi Rizzo na->nm_notify(na, i, NR_TX, NAF_GLOBAL_NOTIFY); 192868b8534bSLuigi Rizzo } 1929ce3ee1e7SLuigi Rizzo } 1930ce3ee1e7SLuigi Rizzo if (want_tx && retry_tx) { 1931954dca4cSLuigi Rizzo selrecord(td, check_all_tx ? 1932ce3ee1e7SLuigi Rizzo &na->tx_si : &na->tx_rings[priv->np_qfirst].si); 1933ce3ee1e7SLuigi Rizzo retry_tx = 0; 1934ce3ee1e7SLuigi Rizzo goto flush_tx; 193568b8534bSLuigi Rizzo } 193668b8534bSLuigi Rizzo } 193768b8534bSLuigi Rizzo 193868b8534bSLuigi Rizzo /* 193917885a7bSLuigi Rizzo * If want_rx is still set scan receive rings. 194068b8534bSLuigi Rizzo * Do it on all rings because otherwise we starve. 194168b8534bSLuigi Rizzo */ 194268b8534bSLuigi Rizzo if (want_rx) { 194317885a7bSLuigi Rizzo int send_down = 0; /* transparent mode */ 194417885a7bSLuigi Rizzo /* two rounds here to for race avoidance */ 1945ce3ee1e7SLuigi Rizzo do_retry_rx: 194664ae02c3SLuigi Rizzo for (i = priv->np_qfirst; i < lim_rx; i++) { 194717885a7bSLuigi Rizzo int found = 0; 194817885a7bSLuigi Rizzo 194968b8534bSLuigi Rizzo kring = &na->rx_rings[i]; 1950ce3ee1e7SLuigi Rizzo 1951ce3ee1e7SLuigi Rizzo if (nm_kr_tryget(kring)) { 195217885a7bSLuigi Rizzo D("%p lost race on rxring %d, ok", priv, i); 195317885a7bSLuigi Rizzo continue; 195468b8534bSLuigi Rizzo } 1955ce3ee1e7SLuigi Rizzo 195617885a7bSLuigi Rizzo /* 195717885a7bSLuigi Rizzo * transparent mode support: collect packets 195817885a7bSLuigi Rizzo * from the rxring(s). 195917885a7bSLuigi Rizzo * XXX NR_FORWARD should only be read on 1960ce3ee1e7SLuigi Rizzo * physical or NIC ports 1961ce3ee1e7SLuigi Rizzo */ 1962091fd0abSLuigi Rizzo if (netmap_fwd ||kring->ring->flags & NR_FORWARD) { 1963091fd0abSLuigi Rizzo ND(10, "forwarding some buffers up %d to %d", 1964091fd0abSLuigi Rizzo kring->nr_hwcur, kring->ring->cur); 1965091fd0abSLuigi Rizzo netmap_grab_packets(kring, &q, netmap_fwd); 1966091fd0abSLuigi Rizzo } 196768b8534bSLuigi Rizzo 1968f9790aebSLuigi Rizzo if (na->nm_rxsync(na, i, 0)) 196968b8534bSLuigi Rizzo revents |= POLLERR; 19705819da83SLuigi Rizzo if (netmap_no_timestamp == 0 || 19715819da83SLuigi Rizzo kring->ring->flags & NR_TIMESTAMP) { 197268b8534bSLuigi Rizzo microtime(&kring->ring->ts); 19735819da83SLuigi Rizzo } 197417885a7bSLuigi Rizzo /* after an rxsync we can use kring->rcur, rtail */ 197517885a7bSLuigi Rizzo found = kring->rcur != kring->rtail; 197617885a7bSLuigi Rizzo nm_kr_put(kring); 197717885a7bSLuigi Rizzo if (found) { 197868b8534bSLuigi Rizzo revents |= want_rx; 1979ce3ee1e7SLuigi Rizzo retry_rx = 0; 198017885a7bSLuigi Rizzo na->nm_notify(na, i, NR_RX, NAF_GLOBAL_NOTIFY); 198168b8534bSLuigi Rizzo } 198268b8534bSLuigi Rizzo } 198317885a7bSLuigi Rizzo 198417885a7bSLuigi Rizzo /* transparent mode XXX only during first pass ? */ 198517885a7bSLuigi Rizzo kring = &na->rx_rings[lim_rx]; 198617885a7bSLuigi Rizzo if (check_all_rx 198717885a7bSLuigi Rizzo && (netmap_fwd || kring->ring->flags & NR_FORWARD)) { 198817885a7bSLuigi Rizzo /* XXX fix to use kring fields */ 198917885a7bSLuigi Rizzo if (nm_ring_empty(kring->ring)) 199017885a7bSLuigi Rizzo send_down = netmap_rxsync_from_host(na, td, dev); 199117885a7bSLuigi Rizzo if (!nm_ring_empty(kring->ring)) 199217885a7bSLuigi Rizzo revents |= want_rx; 199317885a7bSLuigi Rizzo } 199417885a7bSLuigi Rizzo 199517885a7bSLuigi Rizzo if (retry_rx) 1996954dca4cSLuigi Rizzo selrecord(td, check_all_rx ? 1997ce3ee1e7SLuigi Rizzo &na->rx_si : &na->rx_rings[priv->np_qfirst].si); 199817885a7bSLuigi Rizzo if (send_down > 0 || retry_rx) { 199917885a7bSLuigi Rizzo retry_rx = 0; 200017885a7bSLuigi Rizzo if (send_down) 200117885a7bSLuigi Rizzo goto flush_tx; /* and retry_rx */ 200217885a7bSLuigi Rizzo else 2003ce3ee1e7SLuigi Rizzo goto do_retry_rx; 2004ce3ee1e7SLuigi Rizzo } 200568b8534bSLuigi Rizzo } 2006091fd0abSLuigi Rizzo 200717885a7bSLuigi Rizzo /* 200817885a7bSLuigi Rizzo * Transparent mode: marked bufs on rx rings between 200917885a7bSLuigi Rizzo * kring->nr_hwcur and ring->head 201017885a7bSLuigi Rizzo * are passed to the other endpoint. 201117885a7bSLuigi Rizzo * 201217885a7bSLuigi Rizzo * In this mode we also scan the sw rxring, which in 201317885a7bSLuigi Rizzo * turn passes packets up. 201417885a7bSLuigi Rizzo * 201517885a7bSLuigi Rizzo * XXX Transparent mode at the moment requires to bind all 201617885a7bSLuigi Rizzo * rings to a single file descriptor. 2017ce3ee1e7SLuigi Rizzo */ 2018091fd0abSLuigi Rizzo 2019091fd0abSLuigi Rizzo if (q.head) 2020f9790aebSLuigi Rizzo netmap_send_up(na->ifp, &q); 202168b8534bSLuigi Rizzo 202268b8534bSLuigi Rizzo return (revents); 202368b8534bSLuigi Rizzo } 202468b8534bSLuigi Rizzo 202517885a7bSLuigi Rizzo 202617885a7bSLuigi Rizzo /*-------------------- driver support routines -------------------*/ 202768b8534bSLuigi Rizzo 2028f9790aebSLuigi Rizzo static int netmap_hw_krings_create(struct netmap_adapter *); 2029f9790aebSLuigi Rizzo 2030f9790aebSLuigi Rizzo static int 203117885a7bSLuigi Rizzo netmap_notify(struct netmap_adapter *na, u_int n_ring, 203217885a7bSLuigi Rizzo enum txrx tx, int flags) 2033f9790aebSLuigi Rizzo { 2034f9790aebSLuigi Rizzo struct netmap_kring *kring; 2035f9790aebSLuigi Rizzo 2036f9790aebSLuigi Rizzo if (tx == NR_TX) { 2037f9790aebSLuigi Rizzo kring = na->tx_rings + n_ring; 2038f9790aebSLuigi Rizzo selwakeuppri(&kring->si, PI_NET); 2039f9790aebSLuigi Rizzo if (flags & NAF_GLOBAL_NOTIFY) 2040f9790aebSLuigi Rizzo selwakeuppri(&na->tx_si, PI_NET); 2041f9790aebSLuigi Rizzo } else { 2042f9790aebSLuigi Rizzo kring = na->rx_rings + n_ring; 2043f9790aebSLuigi Rizzo selwakeuppri(&kring->si, PI_NET); 2044f9790aebSLuigi Rizzo if (flags & NAF_GLOBAL_NOTIFY) 2045f9790aebSLuigi Rizzo selwakeuppri(&na->rx_si, PI_NET); 2046f9790aebSLuigi Rizzo } 2047f9790aebSLuigi Rizzo return 0; 2048f9790aebSLuigi Rizzo } 2049f9790aebSLuigi Rizzo 2050f9790aebSLuigi Rizzo 2051f9790aebSLuigi Rizzo // XXX check handling of failures 2052f9790aebSLuigi Rizzo int 2053f9790aebSLuigi Rizzo netmap_attach_common(struct netmap_adapter *na) 2054f9790aebSLuigi Rizzo { 2055f9790aebSLuigi Rizzo struct ifnet *ifp = na->ifp; 2056f9790aebSLuigi Rizzo 2057f9790aebSLuigi Rizzo if (na->num_tx_rings == 0 || na->num_rx_rings == 0) { 2058f9790aebSLuigi Rizzo D("%s: invalid rings tx %d rx %d", 2059f9790aebSLuigi Rizzo ifp->if_xname, na->num_tx_rings, na->num_rx_rings); 2060f9790aebSLuigi Rizzo return EINVAL; 2061f9790aebSLuigi Rizzo } 2062f9790aebSLuigi Rizzo WNA(ifp) = na; 206317885a7bSLuigi Rizzo 206417885a7bSLuigi Rizzo /* the following is only needed for na that use the host port. 206517885a7bSLuigi Rizzo * XXX do we have something similar for linux ? 206617885a7bSLuigi Rizzo */ 206717885a7bSLuigi Rizzo #ifdef __FreeBSD__ 206817885a7bSLuigi Rizzo na->if_input = ifp->if_input; /* for netmap_send_up */ 206917885a7bSLuigi Rizzo #endif /* __FreeBSD__ */ 207017885a7bSLuigi Rizzo 2071f9790aebSLuigi Rizzo NETMAP_SET_CAPABLE(ifp); 2072f9790aebSLuigi Rizzo if (na->nm_krings_create == NULL) { 2073f9790aebSLuigi Rizzo na->nm_krings_create = netmap_hw_krings_create; 207417885a7bSLuigi Rizzo na->nm_krings_delete = netmap_hw_krings_delete; 2075f9790aebSLuigi Rizzo } 2076f9790aebSLuigi Rizzo if (na->nm_notify == NULL) 2077f9790aebSLuigi Rizzo na->nm_notify = netmap_notify; 2078f9790aebSLuigi Rizzo na->active_fds = 0; 2079f9790aebSLuigi Rizzo 2080f9790aebSLuigi Rizzo if (na->nm_mem == NULL) 2081f9790aebSLuigi Rizzo na->nm_mem = &nm_mem; 2082f9790aebSLuigi Rizzo return 0; 2083f9790aebSLuigi Rizzo } 2084f9790aebSLuigi Rizzo 2085f9790aebSLuigi Rizzo 2086f9790aebSLuigi Rizzo void 2087f9790aebSLuigi Rizzo netmap_detach_common(struct netmap_adapter *na) 2088f9790aebSLuigi Rizzo { 2089f9790aebSLuigi Rizzo if (na->ifp) 2090f9790aebSLuigi Rizzo WNA(na->ifp) = NULL; /* XXX do we need this? */ 2091f9790aebSLuigi Rizzo 2092f9790aebSLuigi Rizzo if (na->tx_rings) { /* XXX should not happen */ 2093f9790aebSLuigi Rizzo D("freeing leftover tx_rings"); 2094f9790aebSLuigi Rizzo na->nm_krings_delete(na); 2095f9790aebSLuigi Rizzo } 2096f9790aebSLuigi Rizzo if (na->na_flags & NAF_MEM_OWNER) 2097f9790aebSLuigi Rizzo netmap_mem_private_delete(na->nm_mem); 2098f9790aebSLuigi Rizzo bzero(na, sizeof(*na)); 2099f9790aebSLuigi Rizzo free(na, M_DEVBUF); 2100f9790aebSLuigi Rizzo } 2101f9790aebSLuigi Rizzo 2102f18be576SLuigi Rizzo 210368b8534bSLuigi Rizzo /* 210468b8534bSLuigi Rizzo * Initialize a ``netmap_adapter`` object created by driver on attach. 210568b8534bSLuigi Rizzo * We allocate a block of memory with room for a struct netmap_adapter 210668b8534bSLuigi Rizzo * plus two sets of N+2 struct netmap_kring (where N is the number 210768b8534bSLuigi Rizzo * of hardware rings): 210868b8534bSLuigi Rizzo * krings 0..N-1 are for the hardware queues. 210968b8534bSLuigi Rizzo * kring N is for the host stack queue 211017885a7bSLuigi Rizzo * kring N+1 is only used for the selinfo for all queues. // XXX still true ? 211168b8534bSLuigi Rizzo * Return 0 on success, ENOMEM otherwise. 211268b8534bSLuigi Rizzo */ 211368b8534bSLuigi Rizzo int 2114f9790aebSLuigi Rizzo netmap_attach(struct netmap_adapter *arg) 211568b8534bSLuigi Rizzo { 2116f9790aebSLuigi Rizzo struct netmap_hw_adapter *hwna = NULL; 2117f9790aebSLuigi Rizzo // XXX when is arg == NULL ? 2118ae10d1afSLuigi Rizzo struct ifnet *ifp = arg ? arg->ifp : NULL; 211968b8534bSLuigi Rizzo 2120ae10d1afSLuigi Rizzo if (arg == NULL || ifp == NULL) 2121ae10d1afSLuigi Rizzo goto fail; 2122f9790aebSLuigi Rizzo hwna = malloc(sizeof(*hwna), M_DEVBUF, M_NOWAIT | M_ZERO); 2123f9790aebSLuigi Rizzo if (hwna == NULL) 2124ae10d1afSLuigi Rizzo goto fail; 2125f9790aebSLuigi Rizzo hwna->up = *arg; 2126f9790aebSLuigi Rizzo if (netmap_attach_common(&hwna->up)) { 2127f9790aebSLuigi Rizzo free(hwna, M_DEVBUF); 2128f9790aebSLuigi Rizzo goto fail; 2129f9790aebSLuigi Rizzo } 2130f9790aebSLuigi Rizzo netmap_adapter_get(&hwna->up); 2131f9790aebSLuigi Rizzo 213264ae02c3SLuigi Rizzo #ifdef linux 2133f18be576SLuigi Rizzo if (ifp->netdev_ops) { 2134f18be576SLuigi Rizzo /* prepare a clone of the netdev ops */ 2135f18be576SLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28) 2136f9790aebSLuigi Rizzo hwna->nm_ndo.ndo_start_xmit = ifp->netdev_ops; 2137f18be576SLuigi Rizzo #else 2138f9790aebSLuigi Rizzo hwna->nm_ndo = *ifp->netdev_ops; 2139f18be576SLuigi Rizzo #endif 2140f18be576SLuigi Rizzo } 2141f9790aebSLuigi Rizzo hwna->nm_ndo.ndo_start_xmit = linux_netmap_start_xmit; 2142ce3ee1e7SLuigi Rizzo #endif /* linux */ 2143f9790aebSLuigi Rizzo 2144f9790aebSLuigi Rizzo D("success for %s", NM_IFPNAME(ifp)); 2145ae10d1afSLuigi Rizzo return 0; 214668b8534bSLuigi Rizzo 2147ae10d1afSLuigi Rizzo fail: 2148f9790aebSLuigi Rizzo D("fail, arg %p ifp %p na %p", arg, ifp, hwna); 2149849bec0eSLuigi Rizzo netmap_detach(ifp); 2150f9790aebSLuigi Rizzo return (hwna ? EINVAL : ENOMEM); 215168b8534bSLuigi Rizzo } 215268b8534bSLuigi Rizzo 215368b8534bSLuigi Rizzo 2154f9790aebSLuigi Rizzo void 2155f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_get)(struct netmap_adapter *na) 2156f9790aebSLuigi Rizzo { 2157f9790aebSLuigi Rizzo if (!na) { 2158f9790aebSLuigi Rizzo return; 2159f9790aebSLuigi Rizzo } 2160f9790aebSLuigi Rizzo 2161f9790aebSLuigi Rizzo refcount_acquire(&na->na_refcount); 2162f9790aebSLuigi Rizzo } 2163f9790aebSLuigi Rizzo 2164f9790aebSLuigi Rizzo 2165f9790aebSLuigi Rizzo /* returns 1 iff the netmap_adapter is destroyed */ 2166f9790aebSLuigi Rizzo int 2167f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_put)(struct netmap_adapter *na) 2168f9790aebSLuigi Rizzo { 2169f9790aebSLuigi Rizzo if (!na) 2170f9790aebSLuigi Rizzo return 1; 2171f9790aebSLuigi Rizzo 2172f9790aebSLuigi Rizzo if (!refcount_release(&na->na_refcount)) 2173f9790aebSLuigi Rizzo return 0; 2174f9790aebSLuigi Rizzo 2175f9790aebSLuigi Rizzo if (na->nm_dtor) 2176f9790aebSLuigi Rizzo na->nm_dtor(na); 2177f9790aebSLuigi Rizzo 2178f9790aebSLuigi Rizzo netmap_detach_common(na); 2179f9790aebSLuigi Rizzo 2180f9790aebSLuigi Rizzo return 1; 2181f9790aebSLuigi Rizzo } 2182f9790aebSLuigi Rizzo 2183f9790aebSLuigi Rizzo 2184f9790aebSLuigi Rizzo int 2185f9790aebSLuigi Rizzo netmap_hw_krings_create(struct netmap_adapter *na) 2186f9790aebSLuigi Rizzo { 218717885a7bSLuigi Rizzo int ret = netmap_krings_create(na, 2188f9790aebSLuigi Rizzo na->num_tx_rings + 1, na->num_rx_rings + 1, 0); 218917885a7bSLuigi Rizzo if (ret == 0) { 219017885a7bSLuigi Rizzo /* initialize the mbq for the sw rx ring */ 219117885a7bSLuigi Rizzo mbq_safe_init(&na->rx_rings[na->num_rx_rings].rx_queue); 219217885a7bSLuigi Rizzo ND("initialized sw rx queue %d", na->num_rx_rings); 219317885a7bSLuigi Rizzo } 219417885a7bSLuigi Rizzo return ret; 2195f9790aebSLuigi Rizzo } 2196f9790aebSLuigi Rizzo 2197f9790aebSLuigi Rizzo 2198f9790aebSLuigi Rizzo 219968b8534bSLuigi Rizzo /* 220068b8534bSLuigi Rizzo * Free the allocated memory linked to the given ``netmap_adapter`` 220168b8534bSLuigi Rizzo * object. 220268b8534bSLuigi Rizzo */ 220368b8534bSLuigi Rizzo void 220468b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp) 220568b8534bSLuigi Rizzo { 220668b8534bSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 220768b8534bSLuigi Rizzo 220868b8534bSLuigi Rizzo if (!na) 220968b8534bSLuigi Rizzo return; 221068b8534bSLuigi Rizzo 2211f9790aebSLuigi Rizzo NMG_LOCK(); 2212f9790aebSLuigi Rizzo netmap_disable_all_rings(ifp); 2213fb25194fSLuigi Rizzo if (!netmap_adapter_put(na)) { 2214fb25194fSLuigi Rizzo /* someone is still using the adapter, 2215fb25194fSLuigi Rizzo * tell them that the interface is gone 2216fb25194fSLuigi Rizzo */ 2217f9790aebSLuigi Rizzo na->ifp = NULL; 2218fb25194fSLuigi Rizzo /* give them a chance to notice */ 2219f9790aebSLuigi Rizzo netmap_enable_all_rings(ifp); 2220fb25194fSLuigi Rizzo } 2221f9790aebSLuigi Rizzo NMG_UNLOCK(); 2222ae10d1afSLuigi Rizzo } 2223f18be576SLuigi Rizzo 2224f18be576SLuigi Rizzo 222568b8534bSLuigi Rizzo /* 222602ad4083SLuigi Rizzo * Intercept packets from the network stack and pass them 222702ad4083SLuigi Rizzo * to netmap as incoming packets on the 'software' ring. 222817885a7bSLuigi Rizzo * 222917885a7bSLuigi Rizzo * We only store packets in a bounded mbq and then copy them 223017885a7bSLuigi Rizzo * in the relevant rxsync routine. 223117885a7bSLuigi Rizzo * 2232ce3ee1e7SLuigi Rizzo * We rely on the OS to make sure that the ifp and na do not go 2233ce3ee1e7SLuigi Rizzo * away (typically the caller checks for IFF_DRV_RUNNING or the like). 2234ce3ee1e7SLuigi Rizzo * In nm_register() or whenever there is a reinitialization, 2235f9790aebSLuigi Rizzo * we make sure to make the mode change visible here. 223668b8534bSLuigi Rizzo */ 223768b8534bSLuigi Rizzo int 2238ce3ee1e7SLuigi Rizzo netmap_transmit(struct ifnet *ifp, struct mbuf *m) 223968b8534bSLuigi Rizzo { 224068b8534bSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 2241ce3ee1e7SLuigi Rizzo struct netmap_kring *kring; 224217885a7bSLuigi Rizzo u_int len = MBUF_LEN(m); 224317885a7bSLuigi Rizzo u_int error = ENOBUFS; 224417885a7bSLuigi Rizzo struct mbq *q; 224517885a7bSLuigi Rizzo int space; 224668b8534bSLuigi Rizzo 2247ce3ee1e7SLuigi Rizzo // XXX [Linux] we do not need this lock 2248ce3ee1e7SLuigi Rizzo // if we follow the down/configure/up protocol -gl 2249ce3ee1e7SLuigi Rizzo // mtx_lock(&na->core_lock); 225017885a7bSLuigi Rizzo 2251ce3ee1e7SLuigi Rizzo if ( (ifp->if_capenable & IFCAP_NETMAP) == 0) { 225217885a7bSLuigi Rizzo D("%s not in netmap mode anymore", NM_IFPNAME(ifp)); 2253ce3ee1e7SLuigi Rizzo error = ENXIO; 2254ce3ee1e7SLuigi Rizzo goto done; 2255ce3ee1e7SLuigi Rizzo } 2256ce3ee1e7SLuigi Rizzo 2257ce3ee1e7SLuigi Rizzo kring = &na->rx_rings[na->num_rx_rings]; 225817885a7bSLuigi Rizzo q = &kring->rx_queue; 225917885a7bSLuigi Rizzo 2260ce3ee1e7SLuigi Rizzo // XXX reconsider long packets if we handle fragments 2261ce3ee1e7SLuigi Rizzo if (len > NETMAP_BDG_BUF_SIZE(na->nm_mem)) { /* too long for us */ 2262f9790aebSLuigi Rizzo D("%s from_host, drop packet size %d > %d", NM_IFPNAME(ifp), 2263ce3ee1e7SLuigi Rizzo len, NETMAP_BDG_BUF_SIZE(na->nm_mem)); 2264ce3ee1e7SLuigi Rizzo goto done; 2265849bec0eSLuigi Rizzo } 226617885a7bSLuigi Rizzo 226717885a7bSLuigi Rizzo /* protect against rxsync_from_host(), netmap_sw_to_nic() 226817885a7bSLuigi Rizzo * and maybe other instances of netmap_transmit (the latter 226917885a7bSLuigi Rizzo * not possible on Linux). 227017885a7bSLuigi Rizzo * Also avoid overflowing the queue. 2271ce3ee1e7SLuigi Rizzo */ 227217885a7bSLuigi Rizzo mtx_lock(&q->lock); 227317885a7bSLuigi Rizzo 227417885a7bSLuigi Rizzo space = kring->nr_hwtail - kring->nr_hwcur; 227517885a7bSLuigi Rizzo if (space < 0) 227617885a7bSLuigi Rizzo space += kring->nkr_num_slots; 227717885a7bSLuigi Rizzo if (space + mbq_len(q) >= kring->nkr_num_slots - 1) { // XXX 227817885a7bSLuigi Rizzo RD(10, "%s full hwcur %d hwtail %d qlen %d len %d m %p", 227917885a7bSLuigi Rizzo NM_IFPNAME(ifp), kring->nr_hwcur, kring->nr_hwtail, mbq_len(q), 228017885a7bSLuigi Rizzo len, m); 2281ce3ee1e7SLuigi Rizzo } else { 228217885a7bSLuigi Rizzo mbq_enqueue(q, m); 228317885a7bSLuigi Rizzo ND(10, "%s %d bufs in queue len %d m %p", 228417885a7bSLuigi Rizzo NM_IFPNAME(ifp), mbq_len(q), len, m); 228517885a7bSLuigi Rizzo /* notify outside the lock */ 228617885a7bSLuigi Rizzo m = NULL; 228768b8534bSLuigi Rizzo error = 0; 2288ce3ee1e7SLuigi Rizzo } 228917885a7bSLuigi Rizzo mtx_unlock(&q->lock); 2290ce3ee1e7SLuigi Rizzo 229168b8534bSLuigi Rizzo done: 229217885a7bSLuigi Rizzo if (m) 229368b8534bSLuigi Rizzo m_freem(m); 229417885a7bSLuigi Rizzo /* unconditionally wake up listeners */ 229517885a7bSLuigi Rizzo na->nm_notify(na, na->num_rx_rings, NR_RX, 0); 229668b8534bSLuigi Rizzo 229768b8534bSLuigi Rizzo return (error); 229868b8534bSLuigi Rizzo } 229968b8534bSLuigi Rizzo 230068b8534bSLuigi Rizzo 230168b8534bSLuigi Rizzo /* 230268b8534bSLuigi Rizzo * netmap_reset() is called by the driver routines when reinitializing 230368b8534bSLuigi Rizzo * a ring. The driver is in charge of locking to protect the kring. 2304f9790aebSLuigi Rizzo * If native netmap mode is not set just return NULL. 230568b8534bSLuigi Rizzo */ 230668b8534bSLuigi Rizzo struct netmap_slot * 2307ce3ee1e7SLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, u_int n, 230868b8534bSLuigi Rizzo u_int new_cur) 230968b8534bSLuigi Rizzo { 231068b8534bSLuigi Rizzo struct netmap_kring *kring; 2311506cc70cSLuigi Rizzo int new_hwofs, lim; 231268b8534bSLuigi Rizzo 2313ce3ee1e7SLuigi Rizzo if (na == NULL) { 2314ce3ee1e7SLuigi Rizzo D("NULL na, should not happen"); 231568b8534bSLuigi Rizzo return NULL; /* no netmap support here */ 2316ce3ee1e7SLuigi Rizzo } 2317ce3ee1e7SLuigi Rizzo if (!(na->ifp->if_capenable & IFCAP_NETMAP)) { 23185864b3a5SLuigi Rizzo ND("interface not in netmap mode"); 231968b8534bSLuigi Rizzo return NULL; /* nothing to reinitialize */ 2320ce3ee1e7SLuigi Rizzo } 232168b8534bSLuigi Rizzo 2322ce3ee1e7SLuigi Rizzo /* XXX note- in the new scheme, we are not guaranteed to be 2323ce3ee1e7SLuigi Rizzo * under lock (e.g. when called on a device reset). 2324ce3ee1e7SLuigi Rizzo * In this case, we should set a flag and do not trust too 2325ce3ee1e7SLuigi Rizzo * much the values. In practice: TODO 2326ce3ee1e7SLuigi Rizzo * - set a RESET flag somewhere in the kring 2327ce3ee1e7SLuigi Rizzo * - do the processing in a conservative way 2328ce3ee1e7SLuigi Rizzo * - let the *sync() fixup at the end. 2329ce3ee1e7SLuigi Rizzo */ 233064ae02c3SLuigi Rizzo if (tx == NR_TX) { 23318241616dSLuigi Rizzo if (n >= na->num_tx_rings) 23328241616dSLuigi Rizzo return NULL; 233364ae02c3SLuigi Rizzo kring = na->tx_rings + n; 233417885a7bSLuigi Rizzo // XXX check whether we should use hwcur or rcur 2335506cc70cSLuigi Rizzo new_hwofs = kring->nr_hwcur - new_cur; 233664ae02c3SLuigi Rizzo } else { 23378241616dSLuigi Rizzo if (n >= na->num_rx_rings) 23388241616dSLuigi Rizzo return NULL; 233964ae02c3SLuigi Rizzo kring = na->rx_rings + n; 234017885a7bSLuigi Rizzo new_hwofs = kring->nr_hwtail - new_cur; 234164ae02c3SLuigi Rizzo } 234264ae02c3SLuigi Rizzo lim = kring->nkr_num_slots - 1; 2343506cc70cSLuigi Rizzo if (new_hwofs > lim) 2344506cc70cSLuigi Rizzo new_hwofs -= lim + 1; 2345506cc70cSLuigi Rizzo 2346ce3ee1e7SLuigi Rizzo /* Always set the new offset value and realign the ring. */ 234717885a7bSLuigi Rizzo if (netmap_verbose) 234817885a7bSLuigi Rizzo D("%s %s%d hwofs %d -> %d, hwtail %d -> %d", 234917885a7bSLuigi Rizzo NM_IFPNAME(na->ifp), 235017885a7bSLuigi Rizzo tx == NR_TX ? "TX" : "RX", n, 2351ce3ee1e7SLuigi Rizzo kring->nkr_hwofs, new_hwofs, 235217885a7bSLuigi Rizzo kring->nr_hwtail, 235317885a7bSLuigi Rizzo tx == NR_TX ? lim : kring->nr_hwtail); 2354506cc70cSLuigi Rizzo kring->nkr_hwofs = new_hwofs; 235517885a7bSLuigi Rizzo if (tx == NR_TX) { 235617885a7bSLuigi Rizzo kring->nr_hwtail = kring->nr_hwcur + lim; 235717885a7bSLuigi Rizzo if (kring->nr_hwtail > lim) 235817885a7bSLuigi Rizzo kring->nr_hwtail -= lim + 1; 235917885a7bSLuigi Rizzo } 2360506cc70cSLuigi Rizzo 2361f196ce38SLuigi Rizzo #if 0 // def linux 2362f196ce38SLuigi Rizzo /* XXX check that the mappings are correct */ 2363f196ce38SLuigi Rizzo /* need ring_nr, adapter->pdev, direction */ 2364f196ce38SLuigi Rizzo buffer_info->dma = dma_map_single(&pdev->dev, addr, adapter->rx_buffer_len, DMA_FROM_DEVICE); 2365f196ce38SLuigi Rizzo if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) { 2366f196ce38SLuigi Rizzo D("error mapping rx netmap buffer %d", i); 2367f196ce38SLuigi Rizzo // XXX fix error handling 2368f196ce38SLuigi Rizzo } 2369f196ce38SLuigi Rizzo 2370f196ce38SLuigi Rizzo #endif /* linux */ 237168b8534bSLuigi Rizzo /* 2372ce3ee1e7SLuigi Rizzo * Wakeup on the individual and global selwait 2373506cc70cSLuigi Rizzo * We do the wakeup here, but the ring is not yet reconfigured. 2374506cc70cSLuigi Rizzo * However, we are under lock so there are no races. 237568b8534bSLuigi Rizzo */ 2376f9790aebSLuigi Rizzo na->nm_notify(na, n, tx, NAF_GLOBAL_NOTIFY); 237768b8534bSLuigi Rizzo return kring->ring->slot; 237868b8534bSLuigi Rizzo } 237968b8534bSLuigi Rizzo 238068b8534bSLuigi Rizzo 2381ce3ee1e7SLuigi Rizzo /* 2382f9790aebSLuigi Rizzo * Dispatch rx/tx interrupts to the netmap rings. 2383ce3ee1e7SLuigi Rizzo * 2384ce3ee1e7SLuigi Rizzo * "work_done" is non-null on the RX path, NULL for the TX path. 2385ce3ee1e7SLuigi Rizzo * We rely on the OS to make sure that there is only one active 2386ce3ee1e7SLuigi Rizzo * instance per queue, and that there is appropriate locking. 2387849bec0eSLuigi Rizzo * 2388f9790aebSLuigi Rizzo * The 'notify' routine depends on what the ring is attached to. 2389f9790aebSLuigi Rizzo * - for a netmap file descriptor, do a selwakeup on the individual 2390f9790aebSLuigi Rizzo * waitqueue, plus one on the global one if needed 2391f9790aebSLuigi Rizzo * - for a switch, call the proper forwarding routine 2392f9790aebSLuigi Rizzo * - XXX more ? 2393f9790aebSLuigi Rizzo */ 2394f9790aebSLuigi Rizzo void 2395f9790aebSLuigi Rizzo netmap_common_irq(struct ifnet *ifp, u_int q, u_int *work_done) 2396f9790aebSLuigi Rizzo { 2397f9790aebSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 2398f9790aebSLuigi Rizzo struct netmap_kring *kring; 2399f9790aebSLuigi Rizzo 2400f9790aebSLuigi Rizzo q &= NETMAP_RING_MASK; 2401f9790aebSLuigi Rizzo 2402f9790aebSLuigi Rizzo if (netmap_verbose) { 2403f9790aebSLuigi Rizzo RD(5, "received %s queue %d", work_done ? "RX" : "TX" , q); 2404f9790aebSLuigi Rizzo } 2405f9790aebSLuigi Rizzo 2406f9790aebSLuigi Rizzo if (work_done) { /* RX path */ 2407f9790aebSLuigi Rizzo if (q >= na->num_rx_rings) 2408f9790aebSLuigi Rizzo return; // not a physical queue 2409f9790aebSLuigi Rizzo kring = na->rx_rings + q; 2410f9790aebSLuigi Rizzo kring->nr_kflags |= NKR_PENDINTR; // XXX atomic ? 2411f9790aebSLuigi Rizzo na->nm_notify(na, q, NR_RX, 2412f9790aebSLuigi Rizzo (na->num_rx_rings > 1 ? NAF_GLOBAL_NOTIFY : 0)); 2413f9790aebSLuigi Rizzo *work_done = 1; /* do not fire napi again */ 2414f9790aebSLuigi Rizzo } else { /* TX path */ 2415f9790aebSLuigi Rizzo if (q >= na->num_tx_rings) 2416f9790aebSLuigi Rizzo return; // not a physical queue 2417f9790aebSLuigi Rizzo kring = na->tx_rings + q; 2418f9790aebSLuigi Rizzo na->nm_notify(na, q, NR_TX, 2419f9790aebSLuigi Rizzo (na->num_tx_rings > 1 ? NAF_GLOBAL_NOTIFY : 0)); 2420f9790aebSLuigi Rizzo } 2421f9790aebSLuigi Rizzo } 2422f9790aebSLuigi Rizzo 242317885a7bSLuigi Rizzo 2424f9790aebSLuigi Rizzo /* 2425f9790aebSLuigi Rizzo * Default functions to handle rx/tx interrupts from a physical device. 2426f9790aebSLuigi Rizzo * "work_done" is non-null on the RX path, NULL for the TX path. 2427f9790aebSLuigi Rizzo * 2428ce3ee1e7SLuigi Rizzo * If the card is not in netmap mode, simply return 0, 2429ce3ee1e7SLuigi Rizzo * so that the caller proceeds with regular processing. 2430f9790aebSLuigi Rizzo * Otherwise call netmap_common_irq() and return 1. 2431ce3ee1e7SLuigi Rizzo * 2432ce3ee1e7SLuigi Rizzo * If the card is connected to a netmap file descriptor, 2433ce3ee1e7SLuigi Rizzo * do a selwakeup on the individual queue, plus one on the global one 2434ce3ee1e7SLuigi Rizzo * if needed (multiqueue card _and_ there are multiqueue listeners), 2435ce3ee1e7SLuigi Rizzo * and return 1. 2436ce3ee1e7SLuigi Rizzo * 2437ce3ee1e7SLuigi Rizzo * Finally, if called on rx from an interface connected to a switch, 2438ce3ee1e7SLuigi Rizzo * calls the proper forwarding routine, and return 1. 24391a26580eSLuigi Rizzo */ 2440babc7c12SLuigi Rizzo int 2441ce3ee1e7SLuigi Rizzo netmap_rx_irq(struct ifnet *ifp, u_int q, u_int *work_done) 24421a26580eSLuigi Rizzo { 2443f9790aebSLuigi Rizzo // XXX could we check NAF_NATIVE_ON ? 24441a26580eSLuigi Rizzo if (!(ifp->if_capenable & IFCAP_NETMAP)) 24451a26580eSLuigi Rizzo return 0; 2446849bec0eSLuigi Rizzo 2447f9790aebSLuigi Rizzo if (NA(ifp)->na_flags & NAF_SKIP_INTR) { 24488241616dSLuigi Rizzo ND("use regular interrupt"); 24498241616dSLuigi Rizzo return 0; 24508241616dSLuigi Rizzo } 24518241616dSLuigi Rizzo 2452f9790aebSLuigi Rizzo netmap_common_irq(ifp, q, work_done); 24531a26580eSLuigi Rizzo return 1; 24541a26580eSLuigi Rizzo } 24551a26580eSLuigi Rizzo 245664ae02c3SLuigi Rizzo 245701c7d25fSLuigi Rizzo /* 2458f9790aebSLuigi Rizzo * Module loader and unloader 2459f196ce38SLuigi Rizzo * 2460f9790aebSLuigi Rizzo * netmap_init() creates the /dev/netmap device and initializes 2461f9790aebSLuigi Rizzo * all global variables. Returns 0 on success, errno on failure 2462f9790aebSLuigi Rizzo * (but there is no chance) 2463f9790aebSLuigi Rizzo * 2464f9790aebSLuigi Rizzo * netmap_fini() destroys everything. 2465f196ce38SLuigi Rizzo */ 2466babc7c12SLuigi Rizzo 2467babc7c12SLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */ 2468f9790aebSLuigi Rizzo extern struct cdevsw netmap_cdevsw; 2469babc7c12SLuigi Rizzo 247017885a7bSLuigi Rizzo 2471f9790aebSLuigi Rizzo void 247268b8534bSLuigi Rizzo netmap_fini(void) 247368b8534bSLuigi Rizzo { 2474f9790aebSLuigi Rizzo // XXX destroy_bridges() ? 2475f9790aebSLuigi Rizzo if (netmap_dev) 247668b8534bSLuigi Rizzo destroy_dev(netmap_dev); 2477ce3ee1e7SLuigi Rizzo netmap_mem_fini(); 2478ce3ee1e7SLuigi Rizzo NMG_LOCK_DESTROY(); 247968b8534bSLuigi Rizzo printf("netmap: unloaded module.\n"); 248068b8534bSLuigi Rizzo } 248168b8534bSLuigi Rizzo 248217885a7bSLuigi Rizzo 2483f9790aebSLuigi Rizzo int 2484f9790aebSLuigi Rizzo netmap_init(void) 248568b8534bSLuigi Rizzo { 2486f9790aebSLuigi Rizzo int error; 248768b8534bSLuigi Rizzo 2488f9790aebSLuigi Rizzo NMG_LOCK_INIT(); 248968b8534bSLuigi Rizzo 2490f9790aebSLuigi Rizzo error = netmap_mem_init(); 2491f9790aebSLuigi Rizzo if (error != 0) 2492f9790aebSLuigi Rizzo goto fail; 2493f9790aebSLuigi Rizzo /* XXX could use make_dev_credv() to get error number */ 2494f9790aebSLuigi Rizzo netmap_dev = make_dev(&netmap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660, 2495f9790aebSLuigi Rizzo "netmap"); 2496f9790aebSLuigi Rizzo if (!netmap_dev) 2497f9790aebSLuigi Rizzo goto fail; 2498f9790aebSLuigi Rizzo 2499f9790aebSLuigi Rizzo netmap_init_bridges(); 2500f9790aebSLuigi Rizzo printf("netmap: loaded module\n"); 2501f9790aebSLuigi Rizzo return (0); 2502f9790aebSLuigi Rizzo fail: 250368b8534bSLuigi Rizzo netmap_fini(); 2504f9790aebSLuigi Rizzo return (EINVAL); /* may be incorrect */ 250568b8534bSLuigi Rizzo } 2506