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 */ 140*89e3fd52SLuigi Rizzo #include <sys/filio.h> /* FIONBIO */ 14168b8534bSLuigi Rizzo #include <sys/sockio.h> 14268b8534bSLuigi Rizzo #include <sys/socketvar.h> /* struct socket */ 14368b8534bSLuigi Rizzo #include <sys/malloc.h> 14468b8534bSLuigi Rizzo #include <sys/poll.h> 14589f6b863SAttilio Rao #include <sys/rwlock.h> 14668b8534bSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */ 14768b8534bSLuigi Rizzo #include <sys/selinfo.h> 14868b8534bSLuigi Rizzo #include <sys/sysctl.h> 149339f59c0SGleb Smirnoff #include <sys/jail.h> 150339f59c0SGleb Smirnoff #include <net/vnet.h> 15168b8534bSLuigi Rizzo #include <net/if.h> 15276039bc8SGleb Smirnoff #include <net/if_var.h> 15368b8534bSLuigi Rizzo #include <net/bpf.h> /* BIOCIMMEDIATE */ 15468b8534bSLuigi Rizzo #include <machine/bus.h> /* bus_dmamap_* */ 155ce3ee1e7SLuigi Rizzo #include <sys/endian.h> 156ce3ee1e7SLuigi Rizzo #include <sys/refcount.h> 15768b8534bSLuigi Rizzo 15868b8534bSLuigi Rizzo 159f9790aebSLuigi Rizzo /* reduce conditional code */ 160f0ea3689SLuigi Rizzo // linux API, use for the knlist in FreeBSD 161f0ea3689SLuigi Rizzo #define init_waitqueue_head(x) knlist_init_mtx(&(x)->si_note, NULL) 162ce3ee1e7SLuigi Rizzo 163f0ea3689SLuigi Rizzo void freebsd_selwakeup(struct selinfo *si, int pri); 164f0ea3689SLuigi Rizzo #define OS_selwakeup(a, b) freebsd_selwakeup(a, b) 165ce3ee1e7SLuigi Rizzo 166ce3ee1e7SLuigi Rizzo #elif defined(linux) 167ce3ee1e7SLuigi Rizzo 168ce3ee1e7SLuigi Rizzo #include "bsd_glue.h" 169ce3ee1e7SLuigi Rizzo 170ce3ee1e7SLuigi Rizzo 171ce3ee1e7SLuigi Rizzo 172ce3ee1e7SLuigi Rizzo #elif defined(__APPLE__) 173ce3ee1e7SLuigi Rizzo 174ce3ee1e7SLuigi Rizzo #warning OSX support is only partial 175ce3ee1e7SLuigi Rizzo #include "osx_glue.h" 176ce3ee1e7SLuigi Rizzo 177ce3ee1e7SLuigi Rizzo #else 178ce3ee1e7SLuigi Rizzo 179ce3ee1e7SLuigi Rizzo #error Unsupported platform 180ce3ee1e7SLuigi Rizzo 181ce3ee1e7SLuigi Rizzo #endif /* unsupported */ 182ce3ee1e7SLuigi Rizzo 183ce3ee1e7SLuigi Rizzo /* 184ce3ee1e7SLuigi Rizzo * common headers 185ce3ee1e7SLuigi Rizzo */ 1860b8ed8e0SLuigi Rizzo #include <net/netmap.h> 1870b8ed8e0SLuigi Rizzo #include <dev/netmap/netmap_kern.h> 188ce3ee1e7SLuigi Rizzo #include <dev/netmap/netmap_mem2.h> 1890b8ed8e0SLuigi Rizzo 190ce3ee1e7SLuigi Rizzo 191ce3ee1e7SLuigi Rizzo MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map"); 192ce3ee1e7SLuigi Rizzo 193ce3ee1e7SLuigi Rizzo /* 194ce3ee1e7SLuigi Rizzo * The following variables are used by the drivers and replicate 195ce3ee1e7SLuigi Rizzo * fields in the global memory pool. They only refer to buffers 196ce3ee1e7SLuigi Rizzo * used by physical interfaces. 197ce3ee1e7SLuigi Rizzo */ 1985819da83SLuigi Rizzo u_int netmap_total_buffers; 1998241616dSLuigi Rizzo u_int netmap_buf_size; 200ce3ee1e7SLuigi Rizzo char *netmap_buffer_base; /* also address of an invalid buffer */ 2015819da83SLuigi Rizzo 2025819da83SLuigi Rizzo /* user-controlled variables */ 2035819da83SLuigi Rizzo int netmap_verbose; 2045819da83SLuigi Rizzo 2055819da83SLuigi Rizzo static int netmap_no_timestamp; /* don't timestamp on rxsync */ 2065819da83SLuigi Rizzo 2075819da83SLuigi Rizzo SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args"); 2085819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose, 2095819da83SLuigi Rizzo CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode"); 2105819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp, 2115819da83SLuigi Rizzo CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp"); 2125819da83SLuigi Rizzo int netmap_mitigate = 1; 2135819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mitigate, CTLFLAG_RW, &netmap_mitigate, 0, ""); 214c85cb1a0SLuigi Rizzo int netmap_no_pendintr = 1; 2155819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr, 2165819da83SLuigi Rizzo CTLFLAG_RW, &netmap_no_pendintr, 0, "Always look for new received packets."); 217f18be576SLuigi Rizzo int netmap_txsync_retry = 2; 218f18be576SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, txsync_retry, CTLFLAG_RW, 219f18be576SLuigi Rizzo &netmap_txsync_retry, 0 , "Number of txsync loops in bridge's flush."); 2205819da83SLuigi Rizzo 221f196ce38SLuigi Rizzo int netmap_flags = 0; /* debug flags */ 222091fd0abSLuigi Rizzo int netmap_fwd = 0; /* force transparent mode */ 223ce3ee1e7SLuigi Rizzo int netmap_mmap_unreg = 0; /* allow mmap of unregistered fds */ 224f196ce38SLuigi Rizzo 225f9790aebSLuigi Rizzo /* 226f9790aebSLuigi Rizzo * netmap_admode selects the netmap mode to use. 227f9790aebSLuigi Rizzo * Invalid values are reset to NETMAP_ADMODE_BEST 228f9790aebSLuigi Rizzo */ 229f9790aebSLuigi Rizzo enum { NETMAP_ADMODE_BEST = 0, /* use native, fallback to generic */ 230f9790aebSLuigi Rizzo NETMAP_ADMODE_NATIVE, /* either native or none */ 231f9790aebSLuigi Rizzo NETMAP_ADMODE_GENERIC, /* force generic */ 232f9790aebSLuigi Rizzo NETMAP_ADMODE_LAST }; 233f9790aebSLuigi Rizzo static int netmap_admode = NETMAP_ADMODE_BEST; 234f9790aebSLuigi Rizzo 235f9790aebSLuigi Rizzo int netmap_generic_mit = 100*1000; /* Generic mitigation interval in nanoseconds. */ 236f9790aebSLuigi Rizzo int netmap_generic_ringsize = 1024; /* Generic ringsize. */ 237f0ea3689SLuigi Rizzo int netmap_generic_rings = 1; /* number of queues in generic. */ 238f9790aebSLuigi Rizzo 239f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, flags, CTLFLAG_RW, &netmap_flags, 0 , ""); 240091fd0abSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, fwd, CTLFLAG_RW, &netmap_fwd, 0 , ""); 241ce3ee1e7SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mmap_unreg, CTLFLAG_RW, &netmap_mmap_unreg, 0, ""); 242f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, admode, CTLFLAG_RW, &netmap_admode, 0 , ""); 243f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_mit, CTLFLAG_RW, &netmap_generic_mit, 0 , ""); 244f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_ringsize, CTLFLAG_RW, &netmap_generic_ringsize, 0 , ""); 245f0ea3689SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_rings, CTLFLAG_RW, &netmap_generic_rings, 0 , ""); 246f196ce38SLuigi Rizzo 247ce3ee1e7SLuigi Rizzo NMG_LOCK_T netmap_global_lock; 248ce3ee1e7SLuigi Rizzo 249ce3ee1e7SLuigi Rizzo 250f9790aebSLuigi Rizzo static void 251f9790aebSLuigi Rizzo nm_kr_get(struct netmap_kring *kr) 252ce3ee1e7SLuigi Rizzo { 253ce3ee1e7SLuigi Rizzo while (NM_ATOMIC_TEST_AND_SET(&kr->nr_busy)) 254ce3ee1e7SLuigi Rizzo tsleep(kr, 0, "NM_KR_GET", 4); 255ce3ee1e7SLuigi Rizzo } 256ce3ee1e7SLuigi Rizzo 257f9790aebSLuigi Rizzo 25817885a7bSLuigi Rizzo /* 25917885a7bSLuigi Rizzo * mark the ring as stopped, and run through the locks 26017885a7bSLuigi Rizzo * to make sure other users get to see it. 26117885a7bSLuigi Rizzo */ 262f9790aebSLuigi Rizzo void 263f9790aebSLuigi Rizzo netmap_disable_ring(struct netmap_kring *kr) 264ce3ee1e7SLuigi Rizzo { 265ce3ee1e7SLuigi Rizzo kr->nkr_stopped = 1; 266ce3ee1e7SLuigi Rizzo nm_kr_get(kr); 267ce3ee1e7SLuigi Rizzo mtx_lock(&kr->q_lock); 268ce3ee1e7SLuigi Rizzo mtx_unlock(&kr->q_lock); 269ce3ee1e7SLuigi Rizzo nm_kr_put(kr); 270ce3ee1e7SLuigi Rizzo } 271ce3ee1e7SLuigi Rizzo 272f9790aebSLuigi Rizzo 273f9790aebSLuigi Rizzo static void 274f9790aebSLuigi Rizzo netmap_set_all_rings(struct ifnet *ifp, int stopped) 275ce3ee1e7SLuigi Rizzo { 276ce3ee1e7SLuigi Rizzo struct netmap_adapter *na; 277ce3ee1e7SLuigi Rizzo int i; 278f0ea3689SLuigi Rizzo u_int ntx, nrx; 279ce3ee1e7SLuigi Rizzo 280ce3ee1e7SLuigi Rizzo if (!(ifp->if_capenable & IFCAP_NETMAP)) 281ce3ee1e7SLuigi Rizzo return; 282ce3ee1e7SLuigi Rizzo 283ce3ee1e7SLuigi Rizzo na = NA(ifp); 284ce3ee1e7SLuigi Rizzo 285f0ea3689SLuigi Rizzo ntx = netmap_real_tx_rings(na); 286f0ea3689SLuigi Rizzo nrx = netmap_real_rx_rings(na); 287f0ea3689SLuigi Rizzo 288f0ea3689SLuigi Rizzo for (i = 0; i < ntx; i++) { 289f9790aebSLuigi Rizzo if (stopped) 290f9790aebSLuigi Rizzo netmap_disable_ring(na->tx_rings + i); 291f9790aebSLuigi Rizzo else 292ce3ee1e7SLuigi Rizzo na->tx_rings[i].nkr_stopped = 0; 293f0ea3689SLuigi Rizzo na->nm_notify(na, i, NR_TX, NAF_DISABLE_NOTIFY); 294ce3ee1e7SLuigi Rizzo } 295f9790aebSLuigi Rizzo 296f0ea3689SLuigi Rizzo for (i = 0; i < nrx; i++) { 297f9790aebSLuigi Rizzo if (stopped) 298f9790aebSLuigi Rizzo netmap_disable_ring(na->rx_rings + i); 299f9790aebSLuigi Rizzo else 300ce3ee1e7SLuigi Rizzo na->rx_rings[i].nkr_stopped = 0; 301f0ea3689SLuigi Rizzo na->nm_notify(na, i, NR_RX, NAF_DISABLE_NOTIFY); 302ce3ee1e7SLuigi Rizzo } 303ce3ee1e7SLuigi Rizzo } 304ce3ee1e7SLuigi Rizzo 305ce3ee1e7SLuigi Rizzo 306f9790aebSLuigi Rizzo void 307f9790aebSLuigi Rizzo netmap_disable_all_rings(struct ifnet *ifp) 308f9790aebSLuigi Rizzo { 309f9790aebSLuigi Rizzo netmap_set_all_rings(ifp, 1 /* stopped */); 310f9790aebSLuigi Rizzo } 311f9790aebSLuigi Rizzo 312f9790aebSLuigi Rizzo 313f9790aebSLuigi Rizzo void 314f9790aebSLuigi Rizzo netmap_enable_all_rings(struct ifnet *ifp) 315f9790aebSLuigi Rizzo { 316f9790aebSLuigi Rizzo netmap_set_all_rings(ifp, 0 /* enabled */); 317f9790aebSLuigi Rizzo } 318f9790aebSLuigi Rizzo 319f9790aebSLuigi Rizzo 320ce3ee1e7SLuigi Rizzo /* 321ce3ee1e7SLuigi Rizzo * generic bound_checking function 322ce3ee1e7SLuigi Rizzo */ 323ce3ee1e7SLuigi Rizzo u_int 324ce3ee1e7SLuigi Rizzo nm_bound_var(u_int *v, u_int dflt, u_int lo, u_int hi, const char *msg) 325ce3ee1e7SLuigi Rizzo { 326ce3ee1e7SLuigi Rizzo u_int oldv = *v; 327ce3ee1e7SLuigi Rizzo const char *op = NULL; 328ce3ee1e7SLuigi Rizzo 329ce3ee1e7SLuigi Rizzo if (dflt < lo) 330ce3ee1e7SLuigi Rizzo dflt = lo; 331ce3ee1e7SLuigi Rizzo if (dflt > hi) 332ce3ee1e7SLuigi Rizzo dflt = hi; 333ce3ee1e7SLuigi Rizzo if (oldv < lo) { 334ce3ee1e7SLuigi Rizzo *v = dflt; 335ce3ee1e7SLuigi Rizzo op = "Bump"; 336ce3ee1e7SLuigi Rizzo } else if (oldv > hi) { 337ce3ee1e7SLuigi Rizzo *v = hi; 338ce3ee1e7SLuigi Rizzo op = "Clamp"; 339ce3ee1e7SLuigi Rizzo } 340ce3ee1e7SLuigi Rizzo if (op && msg) 341ce3ee1e7SLuigi Rizzo printf("%s %s to %d (was %d)\n", op, msg, *v, oldv); 342ce3ee1e7SLuigi Rizzo return *v; 343ce3ee1e7SLuigi Rizzo } 344ce3ee1e7SLuigi Rizzo 345f9790aebSLuigi Rizzo 346ce3ee1e7SLuigi Rizzo /* 347ce3ee1e7SLuigi Rizzo * packet-dump function, user-supplied or static buffer. 348ce3ee1e7SLuigi Rizzo * The destination buffer must be at least 30+4*len 349ce3ee1e7SLuigi Rizzo */ 350ce3ee1e7SLuigi Rizzo const char * 351ce3ee1e7SLuigi Rizzo nm_dump_buf(char *p, int len, int lim, char *dst) 352ce3ee1e7SLuigi Rizzo { 353ce3ee1e7SLuigi Rizzo static char _dst[8192]; 354ce3ee1e7SLuigi Rizzo int i, j, i0; 355ce3ee1e7SLuigi Rizzo static char hex[] ="0123456789abcdef"; 356ce3ee1e7SLuigi Rizzo char *o; /* output position */ 357ce3ee1e7SLuigi Rizzo 358ce3ee1e7SLuigi Rizzo #define P_HI(x) hex[((x) & 0xf0)>>4] 359ce3ee1e7SLuigi Rizzo #define P_LO(x) hex[((x) & 0xf)] 360ce3ee1e7SLuigi Rizzo #define P_C(x) ((x) >= 0x20 && (x) <= 0x7e ? (x) : '.') 361ce3ee1e7SLuigi Rizzo if (!dst) 362ce3ee1e7SLuigi Rizzo dst = _dst; 363ce3ee1e7SLuigi Rizzo if (lim <= 0 || lim > len) 364ce3ee1e7SLuigi Rizzo lim = len; 365ce3ee1e7SLuigi Rizzo o = dst; 366ce3ee1e7SLuigi Rizzo sprintf(o, "buf 0x%p len %d lim %d\n", p, len, lim); 367ce3ee1e7SLuigi Rizzo o += strlen(o); 368ce3ee1e7SLuigi Rizzo /* hexdump routine */ 369ce3ee1e7SLuigi Rizzo for (i = 0; i < lim; ) { 370ce3ee1e7SLuigi Rizzo sprintf(o, "%5d: ", i); 371ce3ee1e7SLuigi Rizzo o += strlen(o); 372ce3ee1e7SLuigi Rizzo memset(o, ' ', 48); 373ce3ee1e7SLuigi Rizzo i0 = i; 374ce3ee1e7SLuigi Rizzo for (j=0; j < 16 && i < lim; i++, j++) { 375ce3ee1e7SLuigi Rizzo o[j*3] = P_HI(p[i]); 376ce3ee1e7SLuigi Rizzo o[j*3+1] = P_LO(p[i]); 377ce3ee1e7SLuigi Rizzo } 378ce3ee1e7SLuigi Rizzo i = i0; 379ce3ee1e7SLuigi Rizzo for (j=0; j < 16 && i < lim; i++, j++) 380ce3ee1e7SLuigi Rizzo o[j + 48] = P_C(p[i]); 381ce3ee1e7SLuigi Rizzo o[j+48] = '\n'; 382ce3ee1e7SLuigi Rizzo o += j+49; 383ce3ee1e7SLuigi Rizzo } 384ce3ee1e7SLuigi Rizzo *o = '\0'; 385ce3ee1e7SLuigi Rizzo #undef P_HI 386ce3ee1e7SLuigi Rizzo #undef P_LO 387ce3ee1e7SLuigi Rizzo #undef P_C 388ce3ee1e7SLuigi Rizzo return dst; 389ce3ee1e7SLuigi Rizzo } 390f196ce38SLuigi Rizzo 391f18be576SLuigi Rizzo 392ae10d1afSLuigi Rizzo /* 393ae10d1afSLuigi Rizzo * Fetch configuration from the device, to cope with dynamic 394ae10d1afSLuigi Rizzo * reconfigurations after loading the module. 395ae10d1afSLuigi Rizzo */ 396f9790aebSLuigi Rizzo int 397ae10d1afSLuigi Rizzo netmap_update_config(struct netmap_adapter *na) 398ae10d1afSLuigi Rizzo { 399ae10d1afSLuigi Rizzo struct ifnet *ifp = na->ifp; 400ae10d1afSLuigi Rizzo u_int txr, txd, rxr, rxd; 401ae10d1afSLuigi Rizzo 402ae10d1afSLuigi Rizzo txr = txd = rxr = rxd = 0; 403ae10d1afSLuigi Rizzo if (na->nm_config) { 404f9790aebSLuigi Rizzo na->nm_config(na, &txr, &txd, &rxr, &rxd); 405ae10d1afSLuigi Rizzo } else { 406ae10d1afSLuigi Rizzo /* take whatever we had at init time */ 407ae10d1afSLuigi Rizzo txr = na->num_tx_rings; 408ae10d1afSLuigi Rizzo txd = na->num_tx_desc; 409ae10d1afSLuigi Rizzo rxr = na->num_rx_rings; 410ae10d1afSLuigi Rizzo rxd = na->num_rx_desc; 411ae10d1afSLuigi Rizzo } 412ae10d1afSLuigi Rizzo 413ae10d1afSLuigi Rizzo if (na->num_tx_rings == txr && na->num_tx_desc == txd && 414ae10d1afSLuigi Rizzo na->num_rx_rings == rxr && na->num_rx_desc == rxd) 415ae10d1afSLuigi Rizzo return 0; /* nothing changed */ 416f9790aebSLuigi Rizzo if (netmap_verbose || na->active_fds > 0) { 417ae10d1afSLuigi Rizzo D("stored config %s: txring %d x %d, rxring %d x %d", 418f9790aebSLuigi Rizzo NM_IFPNAME(ifp), 419ae10d1afSLuigi Rizzo na->num_tx_rings, na->num_tx_desc, 420ae10d1afSLuigi Rizzo na->num_rx_rings, na->num_rx_desc); 421ae10d1afSLuigi Rizzo D("new config %s: txring %d x %d, rxring %d x %d", 422f9790aebSLuigi Rizzo NM_IFPNAME(ifp), txr, txd, rxr, rxd); 423ae10d1afSLuigi Rizzo } 424f9790aebSLuigi Rizzo if (na->active_fds == 0) { 425ae10d1afSLuigi Rizzo D("configuration changed (but fine)"); 426ae10d1afSLuigi Rizzo na->num_tx_rings = txr; 427ae10d1afSLuigi Rizzo na->num_tx_desc = txd; 428ae10d1afSLuigi Rizzo na->num_rx_rings = rxr; 429ae10d1afSLuigi Rizzo na->num_rx_desc = rxd; 430ae10d1afSLuigi Rizzo return 0; 431ae10d1afSLuigi Rizzo } 432ae10d1afSLuigi Rizzo D("configuration changed while active, this is bad..."); 433ae10d1afSLuigi Rizzo return 1; 434ae10d1afSLuigi Rizzo } 435ae10d1afSLuigi Rizzo 436f0ea3689SLuigi Rizzo static int 437f0ea3689SLuigi Rizzo netmap_txsync_compat(struct netmap_kring *kring, int flags) 438f0ea3689SLuigi Rizzo { 439f0ea3689SLuigi Rizzo struct netmap_adapter *na = kring->na; 440f0ea3689SLuigi Rizzo return na->nm_txsync(na, kring->ring_id, flags); 441f0ea3689SLuigi Rizzo } 442f9790aebSLuigi Rizzo 443f0ea3689SLuigi Rizzo static int 444f0ea3689SLuigi Rizzo netmap_rxsync_compat(struct netmap_kring *kring, int flags) 445f0ea3689SLuigi Rizzo { 446f0ea3689SLuigi Rizzo struct netmap_adapter *na = kring->na; 447f0ea3689SLuigi Rizzo return na->nm_rxsync(na, kring->ring_id, flags); 448f0ea3689SLuigi Rizzo } 449f0ea3689SLuigi Rizzo 450f0ea3689SLuigi Rizzo static int 451f0ea3689SLuigi Rizzo netmap_txsync_to_host_compat(struct netmap_kring *kring, int flags) 452f0ea3689SLuigi Rizzo { 453f0ea3689SLuigi Rizzo (void)flags; 454f0ea3689SLuigi Rizzo netmap_txsync_to_host(kring->na); 455f0ea3689SLuigi Rizzo return 0; 456f0ea3689SLuigi Rizzo } 457f0ea3689SLuigi Rizzo 458f0ea3689SLuigi Rizzo static int 459f0ea3689SLuigi Rizzo netmap_rxsync_from_host_compat(struct netmap_kring *kring, int flags) 460f0ea3689SLuigi Rizzo { 461f0ea3689SLuigi Rizzo (void)flags; 462f0ea3689SLuigi Rizzo netmap_rxsync_from_host(kring->na, NULL, NULL); 463f0ea3689SLuigi Rizzo return 0; 464f0ea3689SLuigi Rizzo } 465f0ea3689SLuigi Rizzo 466f0ea3689SLuigi Rizzo 467f0ea3689SLuigi Rizzo 468f0ea3689SLuigi Rizzo /* create the krings array and initialize the fields common to all adapters. 469f0ea3689SLuigi Rizzo * The array layout is this: 470f0ea3689SLuigi Rizzo * 471f0ea3689SLuigi Rizzo * +----------+ 472f0ea3689SLuigi Rizzo * na->tx_rings ----->| | \ 473f0ea3689SLuigi Rizzo * | | } na->num_tx_ring 474f0ea3689SLuigi Rizzo * | | / 475f0ea3689SLuigi Rizzo * +----------+ 476f0ea3689SLuigi Rizzo * | | host tx kring 477f0ea3689SLuigi Rizzo * na->rx_rings ----> +----------+ 478f0ea3689SLuigi Rizzo * | | \ 479f0ea3689SLuigi Rizzo * | | } na->num_rx_rings 480f0ea3689SLuigi Rizzo * | | / 481f0ea3689SLuigi Rizzo * +----------+ 482f0ea3689SLuigi Rizzo * | | host rx kring 483f0ea3689SLuigi Rizzo * +----------+ 484f0ea3689SLuigi Rizzo * na->tailroom ----->| | \ 485f0ea3689SLuigi Rizzo * | | } tailroom bytes 486f0ea3689SLuigi Rizzo * | | / 487f0ea3689SLuigi Rizzo * +----------+ 488f0ea3689SLuigi Rizzo * 489f0ea3689SLuigi Rizzo * Note: for compatibility, host krings are created even when not needed. 490f0ea3689SLuigi Rizzo * The tailroom space is currently used by vale ports for allocating leases. 491f0ea3689SLuigi Rizzo */ 492f9790aebSLuigi Rizzo int 493f0ea3689SLuigi Rizzo netmap_krings_create(struct netmap_adapter *na, u_int tailroom) 494f9790aebSLuigi Rizzo { 495f9790aebSLuigi Rizzo u_int i, len, ndesc; 496f9790aebSLuigi Rizzo struct netmap_kring *kring; 497f0ea3689SLuigi Rizzo u_int ntx, nrx; 498f9790aebSLuigi Rizzo 499f0ea3689SLuigi Rizzo /* account for the (possibly fake) host rings */ 500f0ea3689SLuigi Rizzo ntx = na->num_tx_rings + 1; 501f0ea3689SLuigi Rizzo nrx = na->num_rx_rings + 1; 502f0ea3689SLuigi Rizzo 503f9790aebSLuigi Rizzo len = (ntx + nrx) * sizeof(struct netmap_kring) + tailroom; 504f9790aebSLuigi Rizzo 505f9790aebSLuigi Rizzo na->tx_rings = malloc((size_t)len, M_DEVBUF, M_NOWAIT | M_ZERO); 506f9790aebSLuigi Rizzo if (na->tx_rings == NULL) { 507f9790aebSLuigi Rizzo D("Cannot allocate krings"); 508f9790aebSLuigi Rizzo return ENOMEM; 509f9790aebSLuigi Rizzo } 510f9790aebSLuigi Rizzo na->rx_rings = na->tx_rings + ntx; 511f9790aebSLuigi Rizzo 51217885a7bSLuigi Rizzo /* 51317885a7bSLuigi Rizzo * All fields in krings are 0 except the one initialized below. 51417885a7bSLuigi Rizzo * but better be explicit on important kring fields. 51517885a7bSLuigi Rizzo */ 516f9790aebSLuigi Rizzo ndesc = na->num_tx_desc; 517f9790aebSLuigi Rizzo for (i = 0; i < ntx; i++) { /* Transmit rings */ 518f9790aebSLuigi Rizzo kring = &na->tx_rings[i]; 519f9790aebSLuigi Rizzo bzero(kring, sizeof(*kring)); 520f9790aebSLuigi Rizzo kring->na = na; 52117885a7bSLuigi Rizzo kring->ring_id = i; 522f9790aebSLuigi Rizzo kring->nkr_num_slots = ndesc; 523f0ea3689SLuigi Rizzo if (i < na->num_tx_rings) { 524f0ea3689SLuigi Rizzo kring->nm_sync = netmap_txsync_compat; // XXX 525f0ea3689SLuigi Rizzo } else if (i == na->num_tx_rings) { 526f0ea3689SLuigi Rizzo kring->nm_sync = netmap_txsync_to_host_compat; 527f0ea3689SLuigi Rizzo } 528f9790aebSLuigi Rizzo /* 52917885a7bSLuigi Rizzo * IMPORTANT: Always keep one slot empty. 530f9790aebSLuigi Rizzo */ 53117885a7bSLuigi Rizzo kring->rhead = kring->rcur = kring->nr_hwcur = 0; 53217885a7bSLuigi Rizzo kring->rtail = kring->nr_hwtail = ndesc - 1; 53317885a7bSLuigi Rizzo snprintf(kring->name, sizeof(kring->name) - 1, "%s TX%d", NM_IFPNAME(na->ifp), i); 534f0ea3689SLuigi Rizzo ND("ktx %s h %d c %d t %d", 535f0ea3689SLuigi Rizzo kring->name, kring->rhead, kring->rcur, kring->rtail); 536f9790aebSLuigi Rizzo mtx_init(&kring->q_lock, "nm_txq_lock", NULL, MTX_DEF); 537f9790aebSLuigi Rizzo init_waitqueue_head(&kring->si); 538f9790aebSLuigi Rizzo } 539f9790aebSLuigi Rizzo 540f9790aebSLuigi Rizzo ndesc = na->num_rx_desc; 541f9790aebSLuigi Rizzo for (i = 0; i < nrx; i++) { /* Receive rings */ 542f9790aebSLuigi Rizzo kring = &na->rx_rings[i]; 543f9790aebSLuigi Rizzo bzero(kring, sizeof(*kring)); 544f9790aebSLuigi Rizzo kring->na = na; 54517885a7bSLuigi Rizzo kring->ring_id = i; 546f9790aebSLuigi Rizzo kring->nkr_num_slots = ndesc; 547f0ea3689SLuigi Rizzo if (i < na->num_rx_rings) { 548f0ea3689SLuigi Rizzo kring->nm_sync = netmap_rxsync_compat; // XXX 549f0ea3689SLuigi Rizzo } else if (i == na->num_rx_rings) { 550f0ea3689SLuigi Rizzo kring->nm_sync = netmap_rxsync_from_host_compat; 551f0ea3689SLuigi Rizzo } 55217885a7bSLuigi Rizzo kring->rhead = kring->rcur = kring->nr_hwcur = 0; 55317885a7bSLuigi Rizzo kring->rtail = kring->nr_hwtail = 0; 55417885a7bSLuigi Rizzo snprintf(kring->name, sizeof(kring->name) - 1, "%s RX%d", NM_IFPNAME(na->ifp), i); 555f0ea3689SLuigi Rizzo ND("krx %s h %d c %d t %d", 556f0ea3689SLuigi Rizzo kring->name, kring->rhead, kring->rcur, kring->rtail); 557f9790aebSLuigi Rizzo mtx_init(&kring->q_lock, "nm_rxq_lock", NULL, MTX_DEF); 558f9790aebSLuigi Rizzo init_waitqueue_head(&kring->si); 559f9790aebSLuigi Rizzo } 560f9790aebSLuigi Rizzo init_waitqueue_head(&na->tx_si); 561f9790aebSLuigi Rizzo init_waitqueue_head(&na->rx_si); 562f9790aebSLuigi Rizzo 563f9790aebSLuigi Rizzo na->tailroom = na->rx_rings + nrx; 564f9790aebSLuigi Rizzo 565f9790aebSLuigi Rizzo return 0; 566f9790aebSLuigi Rizzo } 567f9790aebSLuigi Rizzo 568f9790aebSLuigi Rizzo 569f0ea3689SLuigi Rizzo /* undo the actions performed by netmap_krings_create */ 570f9790aebSLuigi Rizzo void 571f9790aebSLuigi Rizzo netmap_krings_delete(struct netmap_adapter *na) 572f9790aebSLuigi Rizzo { 573f0ea3689SLuigi Rizzo struct netmap_kring *kring = na->tx_rings; 574f9790aebSLuigi Rizzo 575f0ea3689SLuigi Rizzo /* we rely on the krings layout described above */ 576f0ea3689SLuigi Rizzo for ( ; kring != na->tailroom; kring++) { 577f0ea3689SLuigi Rizzo mtx_destroy(&kring->q_lock); 578f9790aebSLuigi Rizzo } 579f9790aebSLuigi Rizzo free(na->tx_rings, M_DEVBUF); 580f9790aebSLuigi Rizzo na->tx_rings = na->rx_rings = na->tailroom = NULL; 581f9790aebSLuigi Rizzo } 582f9790aebSLuigi Rizzo 583f9790aebSLuigi Rizzo 58417885a7bSLuigi Rizzo /* 58517885a7bSLuigi Rizzo * Destructor for NIC ports. They also have an mbuf queue 58617885a7bSLuigi Rizzo * on the rings connected to the host so we need to purge 58717885a7bSLuigi Rizzo * them first. 58817885a7bSLuigi Rizzo */ 58917885a7bSLuigi Rizzo static void 59017885a7bSLuigi Rizzo netmap_hw_krings_delete(struct netmap_adapter *na) 59117885a7bSLuigi Rizzo { 59217885a7bSLuigi Rizzo struct mbq *q = &na->rx_rings[na->num_rx_rings].rx_queue; 59317885a7bSLuigi Rizzo 59417885a7bSLuigi Rizzo ND("destroy sw mbq with len %d", mbq_len(q)); 59517885a7bSLuigi Rizzo mbq_purge(q); 59617885a7bSLuigi Rizzo mbq_safe_destroy(q); 59717885a7bSLuigi Rizzo netmap_krings_delete(na); 59817885a7bSLuigi Rizzo } 59917885a7bSLuigi Rizzo 60017885a7bSLuigi Rizzo 601ce3ee1e7SLuigi Rizzo static struct netmap_if* 602ce3ee1e7SLuigi Rizzo netmap_if_new(const char *ifname, struct netmap_adapter *na) 603ce3ee1e7SLuigi Rizzo { 604f9790aebSLuigi Rizzo struct netmap_if *nifp; 605f9790aebSLuigi Rizzo 606ce3ee1e7SLuigi Rizzo if (netmap_update_config(na)) { 607ce3ee1e7SLuigi Rizzo /* configuration mismatch, report and fail */ 608ce3ee1e7SLuigi Rizzo return NULL; 609ce3ee1e7SLuigi Rizzo } 610f9790aebSLuigi Rizzo 611f9790aebSLuigi Rizzo if (na->active_fds) 612f9790aebSLuigi Rizzo goto final; 613f9790aebSLuigi Rizzo 614f9790aebSLuigi Rizzo if (na->nm_krings_create(na)) 615f9790aebSLuigi Rizzo goto cleanup; 616f9790aebSLuigi Rizzo 617f9790aebSLuigi Rizzo if (netmap_mem_rings_create(na)) 618f9790aebSLuigi Rizzo goto cleanup; 619f9790aebSLuigi Rizzo 620f9790aebSLuigi Rizzo final: 621f9790aebSLuigi Rizzo 622f9790aebSLuigi Rizzo nifp = netmap_mem_if_new(ifname, na); 623f9790aebSLuigi Rizzo if (nifp == NULL) 624f9790aebSLuigi Rizzo goto cleanup; 625f9790aebSLuigi Rizzo 626f9790aebSLuigi Rizzo return (nifp); 627f9790aebSLuigi Rizzo 628f9790aebSLuigi Rizzo cleanup: 629f9790aebSLuigi Rizzo 630f9790aebSLuigi Rizzo if (na->active_fds == 0) { 631f9790aebSLuigi Rizzo netmap_mem_rings_delete(na); 632f9790aebSLuigi Rizzo na->nm_krings_delete(na); 633ce3ee1e7SLuigi Rizzo } 63468b8534bSLuigi Rizzo 635f9790aebSLuigi Rizzo return NULL; 636f9790aebSLuigi Rizzo } 6378241616dSLuigi Rizzo 63868b8534bSLuigi Rizzo 639ce3ee1e7SLuigi Rizzo /* grab a reference to the memory allocator, if we don't have one already. The 640ce3ee1e7SLuigi Rizzo * reference is taken from the netmap_adapter registered with the priv. 641ce3ee1e7SLuigi Rizzo * 642ce3ee1e7SLuigi Rizzo */ 643ce3ee1e7SLuigi Rizzo static int 644ce3ee1e7SLuigi Rizzo netmap_get_memory_locked(struct netmap_priv_d* p) 645ce3ee1e7SLuigi Rizzo { 646ce3ee1e7SLuigi Rizzo struct netmap_mem_d *nmd; 647ce3ee1e7SLuigi Rizzo int error = 0; 648ce3ee1e7SLuigi Rizzo 649f9790aebSLuigi Rizzo if (p->np_na == NULL) { 650ce3ee1e7SLuigi Rizzo if (!netmap_mmap_unreg) 651ce3ee1e7SLuigi Rizzo return ENODEV; 652ce3ee1e7SLuigi Rizzo /* for compatibility with older versions of the API 653ce3ee1e7SLuigi Rizzo * we use the global allocator when no interface has been 654ce3ee1e7SLuigi Rizzo * registered 655ce3ee1e7SLuigi Rizzo */ 656ce3ee1e7SLuigi Rizzo nmd = &nm_mem; 657ce3ee1e7SLuigi Rizzo } else { 658f9790aebSLuigi Rizzo nmd = p->np_na->nm_mem; 659ce3ee1e7SLuigi Rizzo } 660ce3ee1e7SLuigi Rizzo if (p->np_mref == NULL) { 661ce3ee1e7SLuigi Rizzo error = netmap_mem_finalize(nmd); 662ce3ee1e7SLuigi Rizzo if (!error) 663ce3ee1e7SLuigi Rizzo p->np_mref = nmd; 664ce3ee1e7SLuigi Rizzo } else if (p->np_mref != nmd) { 665ce3ee1e7SLuigi Rizzo /* a virtual port has been registered, but previous 666ce3ee1e7SLuigi Rizzo * syscalls already used the global allocator. 667ce3ee1e7SLuigi Rizzo * We cannot continue 668ce3ee1e7SLuigi Rizzo */ 669ce3ee1e7SLuigi Rizzo error = ENODEV; 670ce3ee1e7SLuigi Rizzo } 671ce3ee1e7SLuigi Rizzo return error; 672ce3ee1e7SLuigi Rizzo } 67368b8534bSLuigi Rizzo 674f9790aebSLuigi Rizzo 675f9790aebSLuigi Rizzo int 6768241616dSLuigi Rizzo netmap_get_memory(struct netmap_priv_d* p) 6778241616dSLuigi Rizzo { 678ce3ee1e7SLuigi Rizzo int error; 679ce3ee1e7SLuigi Rizzo NMG_LOCK(); 680ce3ee1e7SLuigi Rizzo error = netmap_get_memory_locked(p); 681ce3ee1e7SLuigi Rizzo NMG_UNLOCK(); 6828241616dSLuigi Rizzo return error; 6838241616dSLuigi Rizzo } 6848241616dSLuigi Rizzo 685f9790aebSLuigi Rizzo 686ce3ee1e7SLuigi Rizzo static int 687ce3ee1e7SLuigi Rizzo netmap_have_memory_locked(struct netmap_priv_d* p) 688ce3ee1e7SLuigi Rizzo { 689ce3ee1e7SLuigi Rizzo return p->np_mref != NULL; 690ce3ee1e7SLuigi Rizzo } 691ce3ee1e7SLuigi Rizzo 692f9790aebSLuigi Rizzo 693ce3ee1e7SLuigi Rizzo static void 694ce3ee1e7SLuigi Rizzo netmap_drop_memory_locked(struct netmap_priv_d* p) 695ce3ee1e7SLuigi Rizzo { 696ce3ee1e7SLuigi Rizzo if (p->np_mref) { 697ce3ee1e7SLuigi Rizzo netmap_mem_deref(p->np_mref); 698ce3ee1e7SLuigi Rizzo p->np_mref = NULL; 699ce3ee1e7SLuigi Rizzo } 700ce3ee1e7SLuigi Rizzo } 701ce3ee1e7SLuigi Rizzo 702f9790aebSLuigi Rizzo 70368b8534bSLuigi Rizzo /* 70468b8534bSLuigi Rizzo * File descriptor's private data destructor. 70568b8534bSLuigi Rizzo * 70668b8534bSLuigi Rizzo * Call nm_register(ifp,0) to stop netmap mode on the interface and 707f9790aebSLuigi Rizzo * revert to normal operation. We expect that np_na->ifp has not gone. 708ce3ee1e7SLuigi Rizzo * The second argument is the nifp to work on. In some cases it is 709ce3ee1e7SLuigi Rizzo * not attached yet to the netmap_priv_d so we need to pass it as 710ce3ee1e7SLuigi Rizzo * a separate argument. 71168b8534bSLuigi Rizzo */ 712ce3ee1e7SLuigi Rizzo /* call with NMG_LOCK held */ 71368b8534bSLuigi Rizzo static void 714ce3ee1e7SLuigi Rizzo netmap_do_unregif(struct netmap_priv_d *priv, struct netmap_if *nifp) 71568b8534bSLuigi Rizzo { 716f9790aebSLuigi Rizzo struct netmap_adapter *na = priv->np_na; 717f9790aebSLuigi Rizzo struct ifnet *ifp = na->ifp; 71868b8534bSLuigi Rizzo 719ce3ee1e7SLuigi Rizzo NMG_LOCK_ASSERT(); 720f9790aebSLuigi Rizzo na->active_fds--; 721f9790aebSLuigi Rizzo if (na->active_fds <= 0) { /* last instance */ 72268b8534bSLuigi Rizzo 723ae10d1afSLuigi Rizzo if (netmap_verbose) 724f9790aebSLuigi Rizzo D("deleting last instance for %s", NM_IFPNAME(ifp)); 72568b8534bSLuigi Rizzo /* 726f18be576SLuigi Rizzo * (TO CHECK) This function is only called 727f18be576SLuigi Rizzo * when the last reference to this file descriptor goes 728f18be576SLuigi Rizzo * away. This means we cannot have any pending poll() 729f18be576SLuigi Rizzo * or interrupt routine operating on the structure. 730ce3ee1e7SLuigi Rizzo * XXX The file may be closed in a thread while 731ce3ee1e7SLuigi Rizzo * another thread is using it. 732ce3ee1e7SLuigi Rizzo * Linux keeps the file opened until the last reference 733ce3ee1e7SLuigi Rizzo * by any outstanding ioctl/poll or mmap is gone. 734ce3ee1e7SLuigi Rizzo * FreeBSD does not track mmap()s (but we do) and 735ce3ee1e7SLuigi Rizzo * wakes up any sleeping poll(). Need to check what 736ce3ee1e7SLuigi Rizzo * happens if the close() occurs while a concurrent 737ce3ee1e7SLuigi Rizzo * syscall is running. 73868b8534bSLuigi Rizzo */ 739f9790aebSLuigi Rizzo if (ifp) 740f9790aebSLuigi Rizzo na->nm_register(na, 0); /* off, clear flags */ 74168b8534bSLuigi Rizzo /* Wake up any sleeping threads. netmap_poll will 74268b8534bSLuigi Rizzo * then return POLLERR 743ce3ee1e7SLuigi Rizzo * XXX The wake up now must happen during *_down(), when 744ce3ee1e7SLuigi Rizzo * we order all activities to stop. -gl 74568b8534bSLuigi Rizzo */ 7462f70fca5SEd Maste /* XXX kqueue(9) needed; these will mirror knlist_init. */ 7472f70fca5SEd Maste /* knlist_destroy(&na->tx_si.si_note); */ 7482f70fca5SEd Maste /* knlist_destroy(&na->rx_si.si_note); */ 749f9790aebSLuigi Rizzo 750f9790aebSLuigi Rizzo /* delete rings and buffers */ 751f9790aebSLuigi Rizzo netmap_mem_rings_delete(na); 752f9790aebSLuigi Rizzo na->nm_krings_delete(na); 75368b8534bSLuigi Rizzo } 754f9790aebSLuigi Rizzo /* delete the nifp */ 755ce3ee1e7SLuigi Rizzo netmap_mem_if_delete(na, nifp); 7565819da83SLuigi Rizzo } 75768b8534bSLuigi Rizzo 758f0ea3689SLuigi Rizzo static __inline int 759f0ea3689SLuigi Rizzo nm_tx_si_user(struct netmap_priv_d *priv) 760f0ea3689SLuigi Rizzo { 761f0ea3689SLuigi Rizzo return (priv->np_na != NULL && 762f0ea3689SLuigi Rizzo (priv->np_txqlast - priv->np_txqfirst > 1)); 763f0ea3689SLuigi Rizzo } 764f0ea3689SLuigi Rizzo 765f0ea3689SLuigi Rizzo static __inline int 766f0ea3689SLuigi Rizzo nm_rx_si_user(struct netmap_priv_d *priv) 767f0ea3689SLuigi Rizzo { 768f0ea3689SLuigi Rizzo return (priv->np_na != NULL && 769f0ea3689SLuigi Rizzo (priv->np_rxqlast - priv->np_rxqfirst > 1)); 770f0ea3689SLuigi Rizzo } 771f0ea3689SLuigi Rizzo 772f18be576SLuigi Rizzo 773ce3ee1e7SLuigi Rizzo /* 774ce3ee1e7SLuigi Rizzo * returns 1 if this is the last instance and we can free priv 775ce3ee1e7SLuigi Rizzo */ 776f9790aebSLuigi Rizzo int 777ce3ee1e7SLuigi Rizzo netmap_dtor_locked(struct netmap_priv_d *priv) 778ce3ee1e7SLuigi Rizzo { 779f9790aebSLuigi Rizzo struct netmap_adapter *na = priv->np_na; 780ce3ee1e7SLuigi Rizzo 781ce3ee1e7SLuigi Rizzo #ifdef __FreeBSD__ 782ce3ee1e7SLuigi Rizzo /* 783ce3ee1e7SLuigi Rizzo * np_refcount is the number of active mmaps on 784ce3ee1e7SLuigi Rizzo * this file descriptor 785ce3ee1e7SLuigi Rizzo */ 786ce3ee1e7SLuigi Rizzo if (--priv->np_refcount > 0) { 787ce3ee1e7SLuigi Rizzo return 0; 788ce3ee1e7SLuigi Rizzo } 789ce3ee1e7SLuigi Rizzo #endif /* __FreeBSD__ */ 790f9790aebSLuigi Rizzo if (!na) { 791f9790aebSLuigi Rizzo return 1; //XXX is it correct? 792ce3ee1e7SLuigi Rizzo } 793f9790aebSLuigi Rizzo netmap_do_unregif(priv, priv->np_nifp); 794f9790aebSLuigi Rizzo priv->np_nifp = NULL; 795ce3ee1e7SLuigi Rizzo netmap_drop_memory_locked(priv); 796f9790aebSLuigi Rizzo if (priv->np_na) { 797f0ea3689SLuigi Rizzo if (nm_tx_si_user(priv)) 798f0ea3689SLuigi Rizzo na->tx_si_users--; 799f0ea3689SLuigi Rizzo if (nm_rx_si_user(priv)) 800f0ea3689SLuigi Rizzo na->rx_si_users--; 801f9790aebSLuigi Rizzo netmap_adapter_put(na); 802f9790aebSLuigi Rizzo priv->np_na = NULL; 803ce3ee1e7SLuigi Rizzo } 804ce3ee1e7SLuigi Rizzo return 1; 805f196ce38SLuigi Rizzo } 8065819da83SLuigi Rizzo 807f9790aebSLuigi Rizzo 808f9790aebSLuigi Rizzo void 8095819da83SLuigi Rizzo netmap_dtor(void *data) 8105819da83SLuigi Rizzo { 8115819da83SLuigi Rizzo struct netmap_priv_d *priv = data; 812ce3ee1e7SLuigi Rizzo int last_instance; 8135819da83SLuigi Rizzo 814ce3ee1e7SLuigi Rizzo NMG_LOCK(); 815ce3ee1e7SLuigi Rizzo last_instance = netmap_dtor_locked(priv); 816ce3ee1e7SLuigi Rizzo NMG_UNLOCK(); 817ce3ee1e7SLuigi Rizzo if (last_instance) { 818ce3ee1e7SLuigi Rizzo bzero(priv, sizeof(*priv)); /* for safety */ 81968b8534bSLuigi Rizzo free(priv, M_DEVBUF); 82068b8534bSLuigi Rizzo } 821ce3ee1e7SLuigi Rizzo } 82268b8534bSLuigi Rizzo 823f18be576SLuigi Rizzo 82468b8534bSLuigi Rizzo 82568b8534bSLuigi Rizzo 82668b8534bSLuigi Rizzo /* 82702ad4083SLuigi Rizzo * Handlers for synchronization of the queues from/to the host. 828091fd0abSLuigi Rizzo * Netmap has two operating modes: 829091fd0abSLuigi Rizzo * - in the default mode, the rings connected to the host stack are 830091fd0abSLuigi Rizzo * just another ring pair managed by userspace; 831091fd0abSLuigi Rizzo * - in transparent mode (XXX to be defined) incoming packets 832091fd0abSLuigi Rizzo * (from the host or the NIC) are marked as NS_FORWARD upon 833091fd0abSLuigi Rizzo * arrival, and the user application has a chance to reset the 834091fd0abSLuigi Rizzo * flag for packets that should be dropped. 835091fd0abSLuigi Rizzo * On the RXSYNC or poll(), packets in RX rings between 836091fd0abSLuigi Rizzo * kring->nr_kcur and ring->cur with NS_FORWARD still set are moved 837091fd0abSLuigi Rizzo * to the other side. 838091fd0abSLuigi Rizzo * The transfer NIC --> host is relatively easy, just encapsulate 839091fd0abSLuigi Rizzo * into mbufs and we are done. The host --> NIC side is slightly 840091fd0abSLuigi Rizzo * harder because there might not be room in the tx ring so it 841091fd0abSLuigi Rizzo * might take a while before releasing the buffer. 842091fd0abSLuigi Rizzo */ 843091fd0abSLuigi Rizzo 844f18be576SLuigi Rizzo 845091fd0abSLuigi Rizzo /* 846091fd0abSLuigi Rizzo * pass a chain of buffers to the host stack as coming from 'dst' 84717885a7bSLuigi Rizzo * We do not need to lock because the queue is private. 848091fd0abSLuigi Rizzo */ 849091fd0abSLuigi Rizzo static void 850f9790aebSLuigi Rizzo netmap_send_up(struct ifnet *dst, struct mbq *q) 851091fd0abSLuigi Rizzo { 852091fd0abSLuigi Rizzo struct mbuf *m; 853091fd0abSLuigi Rizzo 854091fd0abSLuigi Rizzo /* send packets up, outside the lock */ 855f9790aebSLuigi Rizzo while ((m = mbq_dequeue(q)) != NULL) { 856091fd0abSLuigi Rizzo if (netmap_verbose & NM_VERB_HOST) 857091fd0abSLuigi Rizzo D("sending up pkt %p size %d", m, MBUF_LEN(m)); 858091fd0abSLuigi Rizzo NM_SEND_UP(dst, m); 859091fd0abSLuigi Rizzo } 860f9790aebSLuigi Rizzo mbq_destroy(q); 861091fd0abSLuigi Rizzo } 862091fd0abSLuigi Rizzo 863f18be576SLuigi Rizzo 864091fd0abSLuigi Rizzo /* 865091fd0abSLuigi Rizzo * put a copy of the buffers marked NS_FORWARD into an mbuf chain. 86617885a7bSLuigi Rizzo * Take packets from hwcur to ring->head marked NS_FORWARD (or forced) 86717885a7bSLuigi Rizzo * and pass them up. Drop remaining packets in the unlikely event 86817885a7bSLuigi Rizzo * of an mbuf shortage. 869091fd0abSLuigi Rizzo */ 870091fd0abSLuigi Rizzo static void 871091fd0abSLuigi Rizzo netmap_grab_packets(struct netmap_kring *kring, struct mbq *q, int force) 872091fd0abSLuigi Rizzo { 87317885a7bSLuigi Rizzo u_int const lim = kring->nkr_num_slots - 1; 87417885a7bSLuigi Rizzo u_int const head = kring->ring->head; 87517885a7bSLuigi Rizzo u_int n; 876f9790aebSLuigi Rizzo struct netmap_adapter *na = kring->na; 877091fd0abSLuigi Rizzo 87817885a7bSLuigi Rizzo for (n = kring->nr_hwcur; n != head; n = nm_next(n, lim)) { 87917885a7bSLuigi Rizzo struct mbuf *m; 880091fd0abSLuigi Rizzo struct netmap_slot *slot = &kring->ring->slot[n]; 881091fd0abSLuigi Rizzo 882091fd0abSLuigi Rizzo if ((slot->flags & NS_FORWARD) == 0 && !force) 883091fd0abSLuigi Rizzo continue; 884f9790aebSLuigi Rizzo if (slot->len < 14 || slot->len > NETMAP_BDG_BUF_SIZE(na->nm_mem)) { 88517885a7bSLuigi Rizzo RD(5, "bad pkt at %d len %d", n, slot->len); 886091fd0abSLuigi Rizzo continue; 887091fd0abSLuigi Rizzo } 888091fd0abSLuigi Rizzo slot->flags &= ~NS_FORWARD; // XXX needed ? 88917885a7bSLuigi Rizzo /* XXX TODO: adapt to the case of a multisegment packet */ 890f9790aebSLuigi Rizzo m = m_devget(BDG_NMB(na, slot), slot->len, 0, na->ifp, NULL); 891091fd0abSLuigi Rizzo 892091fd0abSLuigi Rizzo if (m == NULL) 893091fd0abSLuigi Rizzo break; 894f9790aebSLuigi Rizzo mbq_enqueue(q, m); 895091fd0abSLuigi Rizzo } 896091fd0abSLuigi Rizzo } 897091fd0abSLuigi Rizzo 898f18be576SLuigi Rizzo 899091fd0abSLuigi Rizzo /* 90017885a7bSLuigi Rizzo * Send to the NIC rings packets marked NS_FORWARD between 90117885a7bSLuigi Rizzo * kring->nr_hwcur and kring->rhead 90217885a7bSLuigi Rizzo * Called under kring->rx_queue.lock on the sw rx ring, 903091fd0abSLuigi Rizzo */ 90417885a7bSLuigi Rizzo static u_int 905091fd0abSLuigi Rizzo netmap_sw_to_nic(struct netmap_adapter *na) 906091fd0abSLuigi Rizzo { 907091fd0abSLuigi Rizzo struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings]; 90817885a7bSLuigi Rizzo struct netmap_slot *rxslot = kring->ring->slot; 90917885a7bSLuigi Rizzo u_int i, rxcur = kring->nr_hwcur; 91017885a7bSLuigi Rizzo u_int const head = kring->rhead; 91117885a7bSLuigi Rizzo u_int const src_lim = kring->nkr_num_slots - 1; 91217885a7bSLuigi Rizzo u_int sent = 0; 913ce3ee1e7SLuigi Rizzo 91417885a7bSLuigi Rizzo /* scan rings to find space, then fill as much as possible */ 91517885a7bSLuigi Rizzo for (i = 0; i < na->num_tx_rings; i++) { 91617885a7bSLuigi Rizzo struct netmap_kring *kdst = &na->tx_rings[i]; 91717885a7bSLuigi Rizzo struct netmap_ring *rdst = kdst->ring; 91817885a7bSLuigi Rizzo u_int const dst_lim = kdst->nkr_num_slots - 1; 919ce3ee1e7SLuigi Rizzo 92017885a7bSLuigi Rizzo /* XXX do we trust ring or kring->rcur,rtail ? */ 92117885a7bSLuigi Rizzo for (; rxcur != head && !nm_ring_empty(rdst); 92217885a7bSLuigi Rizzo rxcur = nm_next(rxcur, src_lim) ) { 923091fd0abSLuigi Rizzo struct netmap_slot *src, *dst, tmp; 92417885a7bSLuigi Rizzo u_int dst_cur = rdst->cur; 92517885a7bSLuigi Rizzo 92617885a7bSLuigi Rizzo src = &rxslot[rxcur]; 92717885a7bSLuigi Rizzo if ((src->flags & NS_FORWARD) == 0 && !netmap_fwd) 92817885a7bSLuigi Rizzo continue; 92917885a7bSLuigi Rizzo 93017885a7bSLuigi Rizzo sent++; 93117885a7bSLuigi Rizzo 93217885a7bSLuigi Rizzo dst = &rdst->slot[dst_cur]; 93317885a7bSLuigi Rizzo 934091fd0abSLuigi Rizzo tmp = *src; 93517885a7bSLuigi Rizzo 936091fd0abSLuigi Rizzo src->buf_idx = dst->buf_idx; 937091fd0abSLuigi Rizzo src->flags = NS_BUF_CHANGED; 938091fd0abSLuigi Rizzo 939091fd0abSLuigi Rizzo dst->buf_idx = tmp.buf_idx; 940091fd0abSLuigi Rizzo dst->len = tmp.len; 941091fd0abSLuigi Rizzo dst->flags = NS_BUF_CHANGED; 942091fd0abSLuigi Rizzo 94317885a7bSLuigi Rizzo rdst->cur = nm_next(dst_cur, dst_lim); 944091fd0abSLuigi Rizzo } 94517885a7bSLuigi Rizzo /* if (sent) XXX txsync ? */ 946091fd0abSLuigi Rizzo } 94717885a7bSLuigi Rizzo return sent; 948091fd0abSLuigi Rizzo } 949091fd0abSLuigi Rizzo 950f18be576SLuigi Rizzo 951091fd0abSLuigi Rizzo /* 952ce3ee1e7SLuigi Rizzo * netmap_txsync_to_host() passes packets up. We are called from a 95302ad4083SLuigi Rizzo * system call in user process context, and the only contention 95402ad4083SLuigi Rizzo * can be among multiple user threads erroneously calling 955091fd0abSLuigi Rizzo * this routine concurrently. 95668b8534bSLuigi Rizzo */ 957f9790aebSLuigi Rizzo void 958ce3ee1e7SLuigi Rizzo netmap_txsync_to_host(struct netmap_adapter *na) 95968b8534bSLuigi Rizzo { 960d76bf4ffSLuigi Rizzo struct netmap_kring *kring = &na->tx_rings[na->num_tx_rings]; 96168b8534bSLuigi Rizzo struct netmap_ring *ring = kring->ring; 96217885a7bSLuigi Rizzo u_int const lim = kring->nkr_num_slots - 1; 963f0ea3689SLuigi Rizzo u_int const head = kring->rhead; 964f9790aebSLuigi Rizzo struct mbq q; 96568b8534bSLuigi Rizzo 96617885a7bSLuigi Rizzo /* Take packets from hwcur to head and pass them up. 96717885a7bSLuigi Rizzo * force head = cur since netmap_grab_packets() stops at head 96868b8534bSLuigi Rizzo * In case of no buffers we give up. At the end of the loop, 96968b8534bSLuigi Rizzo * the queue is drained in all cases. 97068b8534bSLuigi Rizzo */ 971f9790aebSLuigi Rizzo mbq_init(&q); 97217885a7bSLuigi Rizzo ring->cur = head; 97317885a7bSLuigi Rizzo netmap_grab_packets(kring, &q, 1 /* force */); 97417885a7bSLuigi Rizzo ND("have %d pkts in queue", mbq_len(&q)); 97517885a7bSLuigi Rizzo kring->nr_hwcur = head; 97617885a7bSLuigi Rizzo kring->nr_hwtail = head + lim; 97717885a7bSLuigi Rizzo if (kring->nr_hwtail > lim) 97817885a7bSLuigi Rizzo kring->nr_hwtail -= lim + 1; 97917885a7bSLuigi Rizzo nm_txsync_finalize(kring); 98068b8534bSLuigi Rizzo 981f9790aebSLuigi Rizzo netmap_send_up(na->ifp, &q); 982f18be576SLuigi Rizzo } 983f18be576SLuigi Rizzo 984f18be576SLuigi Rizzo 98568b8534bSLuigi Rizzo /* 98602ad4083SLuigi Rizzo * rxsync backend for packets coming from the host stack. 98717885a7bSLuigi Rizzo * They have been put in kring->rx_queue by netmap_transmit(). 98817885a7bSLuigi Rizzo * We protect access to the kring using kring->rx_queue.lock 98902ad4083SLuigi Rizzo * 99068b8534bSLuigi Rizzo * This routine also does the selrecord if called from the poll handler 99168b8534bSLuigi Rizzo * (we know because td != NULL). 99201c7d25fSLuigi Rizzo * 99301c7d25fSLuigi Rizzo * NOTE: on linux, selrecord() is defined as a macro and uses pwait 99401c7d25fSLuigi Rizzo * as an additional hidden argument. 99517885a7bSLuigi Rizzo * returns the number of packets delivered to tx queues in 99617885a7bSLuigi Rizzo * transparent mode, or a negative value if error 99768b8534bSLuigi Rizzo */ 99817885a7bSLuigi Rizzo int 999ce3ee1e7SLuigi Rizzo netmap_rxsync_from_host(struct netmap_adapter *na, struct thread *td, void *pwait) 100068b8534bSLuigi Rizzo { 1001d76bf4ffSLuigi Rizzo struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings]; 100268b8534bSLuigi Rizzo struct netmap_ring *ring = kring->ring; 100317885a7bSLuigi Rizzo u_int nm_i, n; 100417885a7bSLuigi Rizzo u_int const lim = kring->nkr_num_slots - 1; 1005f0ea3689SLuigi Rizzo u_int const head = kring->rhead; 100617885a7bSLuigi Rizzo int ret = 0; 100717885a7bSLuigi Rizzo struct mbq *q = &kring->rx_queue; 100868b8534bSLuigi Rizzo 100901c7d25fSLuigi Rizzo (void)pwait; /* disable unused warnings */ 1010f0ea3689SLuigi Rizzo (void)td; 101117885a7bSLuigi Rizzo 101217885a7bSLuigi Rizzo mtx_lock(&q->lock); 101317885a7bSLuigi Rizzo 101417885a7bSLuigi Rizzo /* First part: import newly received packets */ 101517885a7bSLuigi Rizzo n = mbq_len(q); 101617885a7bSLuigi Rizzo if (n) { /* grab packets from the queue */ 101717885a7bSLuigi Rizzo struct mbuf *m; 101817885a7bSLuigi Rizzo uint32_t stop_i; 101917885a7bSLuigi Rizzo 102017885a7bSLuigi Rizzo nm_i = kring->nr_hwtail; 102117885a7bSLuigi Rizzo stop_i = nm_prev(nm_i, lim); 102217885a7bSLuigi Rizzo while ( nm_i != stop_i && (m = mbq_dequeue(q)) != NULL ) { 102317885a7bSLuigi Rizzo int len = MBUF_LEN(m); 102417885a7bSLuigi Rizzo struct netmap_slot *slot = &ring->slot[nm_i]; 102517885a7bSLuigi Rizzo 102617885a7bSLuigi Rizzo m_copydata(m, 0, len, BDG_NMB(na, slot)); 102717885a7bSLuigi Rizzo ND("nm %d len %d", nm_i, len); 102817885a7bSLuigi Rizzo if (netmap_verbose) 102917885a7bSLuigi Rizzo D("%s", nm_dump_buf(BDG_NMB(na, slot),len, 128, NULL)); 103017885a7bSLuigi Rizzo 103117885a7bSLuigi Rizzo slot->len = len; 103217885a7bSLuigi Rizzo slot->flags = kring->nkr_slot_flags; 103317885a7bSLuigi Rizzo nm_i = nm_next(nm_i, lim); 103464ae02c3SLuigi Rizzo } 103517885a7bSLuigi Rizzo kring->nr_hwtail = nm_i; 103664ae02c3SLuigi Rizzo } 103717885a7bSLuigi Rizzo 103817885a7bSLuigi Rizzo /* 103917885a7bSLuigi Rizzo * Second part: skip past packets that userspace has released. 104017885a7bSLuigi Rizzo */ 104117885a7bSLuigi Rizzo nm_i = kring->nr_hwcur; 104217885a7bSLuigi Rizzo if (nm_i != head) { /* something was released */ 104317885a7bSLuigi Rizzo if (netmap_fwd || kring->ring->flags & NR_FORWARD) 104417885a7bSLuigi Rizzo ret = netmap_sw_to_nic(na); 104517885a7bSLuigi Rizzo kring->nr_hwcur = head; 104664ae02c3SLuigi Rizzo } 104717885a7bSLuigi Rizzo 104817885a7bSLuigi Rizzo nm_rxsync_finalize(kring); 104917885a7bSLuigi Rizzo 105017885a7bSLuigi Rizzo /* access copies of cur,tail in the kring */ 105117885a7bSLuigi Rizzo if (kring->rcur == kring->rtail && td) /* no bufs available */ 105268b8534bSLuigi Rizzo selrecord(td, &kring->si); 105317885a7bSLuigi Rizzo 105417885a7bSLuigi Rizzo mtx_unlock(&q->lock); 105517885a7bSLuigi Rizzo return ret; 105668b8534bSLuigi Rizzo } 105768b8534bSLuigi Rizzo 105868b8534bSLuigi Rizzo 1059f9790aebSLuigi Rizzo /* Get a netmap adapter for the port. 1060f9790aebSLuigi Rizzo * 1061f9790aebSLuigi Rizzo * If it is possible to satisfy the request, return 0 1062f9790aebSLuigi Rizzo * with *na containing the netmap adapter found. 1063f9790aebSLuigi Rizzo * Otherwise return an error code, with *na containing NULL. 1064f9790aebSLuigi Rizzo * 1065f9790aebSLuigi Rizzo * When the port is attached to a bridge, we always return 1066f9790aebSLuigi Rizzo * EBUSY. 1067f9790aebSLuigi Rizzo * Otherwise, if the port is already bound to a file descriptor, 1068f9790aebSLuigi Rizzo * then we unconditionally return the existing adapter into *na. 1069f9790aebSLuigi Rizzo * In all the other cases, we return (into *na) either native, 1070f9790aebSLuigi Rizzo * generic or NULL, according to the following table: 1071f9790aebSLuigi Rizzo * 1072f9790aebSLuigi Rizzo * native_support 1073f9790aebSLuigi Rizzo * active_fds dev.netmap.admode YES NO 1074f9790aebSLuigi Rizzo * ------------------------------------------------------- 1075f9790aebSLuigi Rizzo * >0 * NA(ifp) NA(ifp) 1076f9790aebSLuigi Rizzo * 1077f9790aebSLuigi Rizzo * 0 NETMAP_ADMODE_BEST NATIVE GENERIC 1078f9790aebSLuigi Rizzo * 0 NETMAP_ADMODE_NATIVE NATIVE NULL 1079f9790aebSLuigi Rizzo * 0 NETMAP_ADMODE_GENERIC GENERIC GENERIC 1080f9790aebSLuigi Rizzo * 1081f9790aebSLuigi Rizzo */ 1082f9790aebSLuigi Rizzo 1083f9790aebSLuigi Rizzo int 1084f9790aebSLuigi Rizzo netmap_get_hw_na(struct ifnet *ifp, struct netmap_adapter **na) 1085f9790aebSLuigi Rizzo { 1086f9790aebSLuigi Rizzo /* generic support */ 1087f9790aebSLuigi Rizzo int i = netmap_admode; /* Take a snapshot. */ 1088f9790aebSLuigi Rizzo int error = 0; 1089f9790aebSLuigi Rizzo struct netmap_adapter *prev_na; 1090f9790aebSLuigi Rizzo struct netmap_generic_adapter *gna; 1091f9790aebSLuigi Rizzo 1092f9790aebSLuigi Rizzo *na = NULL; /* default */ 1093f9790aebSLuigi Rizzo 1094f9790aebSLuigi Rizzo /* reset in case of invalid value */ 1095f9790aebSLuigi Rizzo if (i < NETMAP_ADMODE_BEST || i >= NETMAP_ADMODE_LAST) 1096f9790aebSLuigi Rizzo i = netmap_admode = NETMAP_ADMODE_BEST; 1097f9790aebSLuigi Rizzo 1098f9790aebSLuigi Rizzo if (NETMAP_CAPABLE(ifp)) { 1099f9790aebSLuigi Rizzo /* If an adapter already exists, but is 1100f9790aebSLuigi Rizzo * attached to a vale port, we report that the 1101f9790aebSLuigi Rizzo * port is busy. 1102f9790aebSLuigi Rizzo */ 1103f9790aebSLuigi Rizzo if (NETMAP_OWNED_BY_KERN(NA(ifp))) 1104f9790aebSLuigi Rizzo return EBUSY; 1105f9790aebSLuigi Rizzo 1106f9790aebSLuigi Rizzo /* If an adapter already exists, return it if 1107f9790aebSLuigi Rizzo * there are active file descriptors or if 1108f9790aebSLuigi Rizzo * netmap is not forced to use generic 1109f9790aebSLuigi Rizzo * adapters. 1110f9790aebSLuigi Rizzo */ 1111f9790aebSLuigi Rizzo if (NA(ifp)->active_fds > 0 || 1112f9790aebSLuigi Rizzo i != NETMAP_ADMODE_GENERIC) { 1113f9790aebSLuigi Rizzo *na = NA(ifp); 1114f9790aebSLuigi Rizzo return 0; 1115f9790aebSLuigi Rizzo } 1116f9790aebSLuigi Rizzo } 1117f9790aebSLuigi Rizzo 1118f9790aebSLuigi Rizzo /* If there isn't native support and netmap is not allowed 1119f9790aebSLuigi Rizzo * to use generic adapters, we cannot satisfy the request. 1120f9790aebSLuigi Rizzo */ 1121f9790aebSLuigi Rizzo if (!NETMAP_CAPABLE(ifp) && i == NETMAP_ADMODE_NATIVE) 1122f2637526SLuigi Rizzo return EOPNOTSUPP; 1123f9790aebSLuigi Rizzo 1124f9790aebSLuigi Rizzo /* Otherwise, create a generic adapter and return it, 1125f9790aebSLuigi Rizzo * saving the previously used netmap adapter, if any. 1126f9790aebSLuigi Rizzo * 1127f9790aebSLuigi Rizzo * Note that here 'prev_na', if not NULL, MUST be a 1128f9790aebSLuigi Rizzo * native adapter, and CANNOT be a generic one. This is 1129f9790aebSLuigi Rizzo * true because generic adapters are created on demand, and 1130f9790aebSLuigi Rizzo * destroyed when not used anymore. Therefore, if the adapter 1131f9790aebSLuigi Rizzo * currently attached to an interface 'ifp' is generic, it 1132f9790aebSLuigi Rizzo * must be that 1133f9790aebSLuigi Rizzo * (NA(ifp)->active_fds > 0 || NETMAP_OWNED_BY_KERN(NA(ifp))). 1134f9790aebSLuigi Rizzo * Consequently, if NA(ifp) is generic, we will enter one of 1135f9790aebSLuigi Rizzo * the branches above. This ensures that we never override 1136f9790aebSLuigi Rizzo * a generic adapter with another generic adapter. 1137f9790aebSLuigi Rizzo */ 1138f9790aebSLuigi Rizzo prev_na = NA(ifp); 1139f9790aebSLuigi Rizzo error = generic_netmap_attach(ifp); 1140f9790aebSLuigi Rizzo if (error) 1141f9790aebSLuigi Rizzo return error; 1142f9790aebSLuigi Rizzo 1143f9790aebSLuigi Rizzo *na = NA(ifp); 1144f9790aebSLuigi Rizzo gna = (struct netmap_generic_adapter*)NA(ifp); 1145f9790aebSLuigi Rizzo gna->prev = prev_na; /* save old na */ 1146f9790aebSLuigi Rizzo if (prev_na != NULL) { 1147f9790aebSLuigi Rizzo ifunit_ref(ifp->if_xname); 1148f9790aebSLuigi Rizzo // XXX add a refcount ? 1149f9790aebSLuigi Rizzo netmap_adapter_get(prev_na); 1150f9790aebSLuigi Rizzo } 115117885a7bSLuigi Rizzo ND("Created generic NA %p (prev %p)", gna, gna->prev); 1152f9790aebSLuigi Rizzo 1153f9790aebSLuigi Rizzo return 0; 1154f9790aebSLuigi Rizzo } 1155f9790aebSLuigi Rizzo 1156f9790aebSLuigi Rizzo 115768b8534bSLuigi Rizzo /* 1158ce3ee1e7SLuigi Rizzo * MUST BE CALLED UNDER NMG_LOCK() 1159ce3ee1e7SLuigi Rizzo * 1160f2637526SLuigi Rizzo * Get a refcounted reference to a netmap adapter attached 1161f2637526SLuigi Rizzo * to the interface specified by nmr. 1162ce3ee1e7SLuigi Rizzo * This is always called in the execution of an ioctl(). 1163ce3ee1e7SLuigi Rizzo * 1164f2637526SLuigi Rizzo * Return ENXIO if the interface specified by the request does 1165f2637526SLuigi Rizzo * not exist, ENOTSUP if netmap is not supported by the interface, 1166f2637526SLuigi Rizzo * EBUSY if the interface is already attached to a bridge, 1167f2637526SLuigi Rizzo * EINVAL if parameters are invalid, ENOMEM if needed resources 1168f2637526SLuigi Rizzo * could not be allocated. 1169f2637526SLuigi Rizzo * If successful, hold a reference to the netmap adapter. 1170f18be576SLuigi Rizzo * 1171f2637526SLuigi Rizzo * No reference is kept on the real interface, which may then 1172f2637526SLuigi Rizzo * disappear at any time. 117368b8534bSLuigi Rizzo */ 1174f9790aebSLuigi Rizzo int 1175f9790aebSLuigi Rizzo netmap_get_na(struct nmreq *nmr, struct netmap_adapter **na, int create) 117668b8534bSLuigi Rizzo { 1177f0ea3689SLuigi Rizzo struct ifnet *ifp = NULL; 1178f9790aebSLuigi Rizzo int error = 0; 1179f0ea3689SLuigi Rizzo struct netmap_adapter *ret = NULL; 1180f9790aebSLuigi Rizzo 1181f9790aebSLuigi Rizzo *na = NULL; /* default return value */ 1182f196ce38SLuigi Rizzo 1183ce3ee1e7SLuigi Rizzo /* first try to see if this is a bridge port. */ 1184ce3ee1e7SLuigi Rizzo NMG_LOCK_ASSERT(); 1185ce3ee1e7SLuigi Rizzo 1186f0ea3689SLuigi Rizzo error = netmap_get_pipe_na(nmr, na, create); 1187f0ea3689SLuigi Rizzo if (error || *na != NULL) 1188f9790aebSLuigi Rizzo return error; 1189ce3ee1e7SLuigi Rizzo 1190f0ea3689SLuigi Rizzo error = netmap_get_bdg_na(nmr, na, create); 1191f0ea3689SLuigi Rizzo if (error) 1192f0ea3689SLuigi Rizzo return error; 1193f0ea3689SLuigi Rizzo 1194f0ea3689SLuigi Rizzo if (*na != NULL) /* valid match in netmap_get_bdg_na() */ 1195f0ea3689SLuigi Rizzo goto pipes; 1196f0ea3689SLuigi Rizzo 1197f9790aebSLuigi Rizzo ifp = ifunit_ref(nmr->nr_name); 1198f9790aebSLuigi Rizzo if (ifp == NULL) { 1199ce3ee1e7SLuigi Rizzo return ENXIO; 1200f196ce38SLuigi Rizzo } 1201ce3ee1e7SLuigi Rizzo 1202f9790aebSLuigi Rizzo error = netmap_get_hw_na(ifp, &ret); 1203f9790aebSLuigi Rizzo if (error) 1204f9790aebSLuigi Rizzo goto out; 1205f18be576SLuigi Rizzo 1206f9790aebSLuigi Rizzo /* Users cannot use the NIC attached to a bridge directly */ 1207f9790aebSLuigi Rizzo if (NETMAP_OWNED_BY_KERN(ret)) { 1208f2637526SLuigi Rizzo error = EBUSY; 1209f9790aebSLuigi Rizzo goto out; 1210ce3ee1e7SLuigi Rizzo } 1211f9790aebSLuigi Rizzo *na = ret; 1212f9790aebSLuigi Rizzo netmap_adapter_get(ret); 1213f0ea3689SLuigi Rizzo 1214f0ea3689SLuigi Rizzo pipes: 1215f0ea3689SLuigi Rizzo error = netmap_pipe_alloc(*na, nmr); 1216f0ea3689SLuigi Rizzo 1217f9790aebSLuigi Rizzo out: 1218f0ea3689SLuigi Rizzo if (error && ret != NULL) 1219f0ea3689SLuigi Rizzo netmap_adapter_put(ret); 1220f0ea3689SLuigi Rizzo 1221f0ea3689SLuigi Rizzo if (ifp) 1222f9790aebSLuigi Rizzo if_rele(ifp); 1223f18be576SLuigi Rizzo 12245ab0d24dSLuigi Rizzo return error; 12255ab0d24dSLuigi Rizzo } 1226ce3ee1e7SLuigi Rizzo 1227ce3ee1e7SLuigi Rizzo 1228f9790aebSLuigi Rizzo /* 1229f9790aebSLuigi Rizzo * validate parameters on entry for *_txsync() 1230f9790aebSLuigi Rizzo * Returns ring->cur if ok, or something >= kring->nkr_num_slots 123117885a7bSLuigi Rizzo * in case of error. 1232f9790aebSLuigi Rizzo * 123317885a7bSLuigi Rizzo * rhead, rcur and rtail=hwtail are stored from previous round. 123417885a7bSLuigi Rizzo * hwcur is the next packet to send to the ring. 1235f9790aebSLuigi Rizzo * 123617885a7bSLuigi Rizzo * We want 123717885a7bSLuigi Rizzo * hwcur <= *rhead <= head <= cur <= tail = *rtail <= hwtail 1238f9790aebSLuigi Rizzo * 123917885a7bSLuigi Rizzo * hwcur, rhead, rtail and hwtail are reliable 1240f9790aebSLuigi Rizzo */ 1241f9790aebSLuigi Rizzo u_int 124217885a7bSLuigi Rizzo nm_txsync_prologue(struct netmap_kring *kring) 1243f9790aebSLuigi Rizzo { 1244f9790aebSLuigi Rizzo struct netmap_ring *ring = kring->ring; 124517885a7bSLuigi Rizzo u_int head = ring->head; /* read only once */ 1246f9790aebSLuigi Rizzo u_int cur = ring->cur; /* read only once */ 1247f9790aebSLuigi Rizzo u_int n = kring->nkr_num_slots; 1248ce3ee1e7SLuigi Rizzo 124917885a7bSLuigi Rizzo ND(5, "%s kcur %d ktail %d head %d cur %d tail %d", 125017885a7bSLuigi Rizzo kring->name, 125117885a7bSLuigi Rizzo kring->nr_hwcur, kring->nr_hwtail, 125217885a7bSLuigi Rizzo ring->head, ring->cur, ring->tail); 125317885a7bSLuigi Rizzo #if 1 /* kernel sanity checks; but we can trust the kring. */ 125417885a7bSLuigi Rizzo if (kring->nr_hwcur >= n || kring->rhead >= n || 125517885a7bSLuigi Rizzo kring->rtail >= n || kring->nr_hwtail >= n) 1256f9790aebSLuigi Rizzo goto error; 1257f9790aebSLuigi Rizzo #endif /* kernel sanity checks */ 125817885a7bSLuigi Rizzo /* 125917885a7bSLuigi Rizzo * user sanity checks. We only use 'cur', 126017885a7bSLuigi Rizzo * A, B, ... are possible positions for cur: 126117885a7bSLuigi Rizzo * 126217885a7bSLuigi Rizzo * 0 A cur B tail C n-1 126317885a7bSLuigi Rizzo * 0 D tail E cur F n-1 126417885a7bSLuigi Rizzo * 126517885a7bSLuigi Rizzo * B, F, D are valid. A, C, E are wrong 126617885a7bSLuigi Rizzo */ 126717885a7bSLuigi Rizzo if (kring->rtail >= kring->rhead) { 126817885a7bSLuigi Rizzo /* want rhead <= head <= rtail */ 126917885a7bSLuigi Rizzo if (head < kring->rhead || head > kring->rtail) 1270f9790aebSLuigi Rizzo goto error; 127117885a7bSLuigi Rizzo /* and also head <= cur <= rtail */ 127217885a7bSLuigi Rizzo if (cur < head || cur > kring->rtail) 1273f9790aebSLuigi Rizzo goto error; 127417885a7bSLuigi Rizzo } else { /* here rtail < rhead */ 127517885a7bSLuigi Rizzo /* we need head outside rtail .. rhead */ 127617885a7bSLuigi Rizzo if (head > kring->rtail && head < kring->rhead) 127717885a7bSLuigi Rizzo goto error; 127817885a7bSLuigi Rizzo 127917885a7bSLuigi Rizzo /* two cases now: head <= rtail or head >= rhead */ 128017885a7bSLuigi Rizzo if (head <= kring->rtail) { 128117885a7bSLuigi Rizzo /* want head <= cur <= rtail */ 128217885a7bSLuigi Rizzo if (cur < head || cur > kring->rtail) 128317885a7bSLuigi Rizzo goto error; 128417885a7bSLuigi Rizzo } else { /* head >= rhead */ 128517885a7bSLuigi Rizzo /* cur must be outside rtail..head */ 128617885a7bSLuigi Rizzo if (cur > kring->rtail && cur < head) 128717885a7bSLuigi Rizzo goto error; 1288f18be576SLuigi Rizzo } 1289f9790aebSLuigi Rizzo } 129017885a7bSLuigi Rizzo if (ring->tail != kring->rtail) { 129117885a7bSLuigi Rizzo RD(5, "tail overwritten was %d need %d", 129217885a7bSLuigi Rizzo ring->tail, kring->rtail); 129317885a7bSLuigi Rizzo ring->tail = kring->rtail; 129417885a7bSLuigi Rizzo } 129517885a7bSLuigi Rizzo kring->rhead = head; 129617885a7bSLuigi Rizzo kring->rcur = cur; 129717885a7bSLuigi Rizzo return head; 1298f9790aebSLuigi Rizzo 1299f9790aebSLuigi Rizzo error: 130017885a7bSLuigi Rizzo RD(5, "%s kring error: hwcur %d rcur %d hwtail %d cur %d tail %d", 130117885a7bSLuigi Rizzo kring->name, 1302f9790aebSLuigi Rizzo kring->nr_hwcur, 130317885a7bSLuigi Rizzo kring->rcur, kring->nr_hwtail, 130417885a7bSLuigi Rizzo cur, ring->tail); 1305f9790aebSLuigi Rizzo return n; 130668b8534bSLuigi Rizzo } 130768b8534bSLuigi Rizzo 130868b8534bSLuigi Rizzo 130968b8534bSLuigi Rizzo /* 1310f9790aebSLuigi Rizzo * validate parameters on entry for *_rxsync() 131117885a7bSLuigi Rizzo * Returns ring->head if ok, kring->nkr_num_slots on error. 1312f9790aebSLuigi Rizzo * 131317885a7bSLuigi Rizzo * For a valid configuration, 131417885a7bSLuigi Rizzo * hwcur <= head <= cur <= tail <= hwtail 1315f9790aebSLuigi Rizzo * 131617885a7bSLuigi Rizzo * We only consider head and cur. 131717885a7bSLuigi Rizzo * hwcur and hwtail are reliable. 1318f9790aebSLuigi Rizzo * 1319f9790aebSLuigi Rizzo */ 1320f9790aebSLuigi Rizzo u_int 132117885a7bSLuigi Rizzo nm_rxsync_prologue(struct netmap_kring *kring) 1322f9790aebSLuigi Rizzo { 1323f9790aebSLuigi Rizzo struct netmap_ring *ring = kring->ring; 132417885a7bSLuigi Rizzo uint32_t const n = kring->nkr_num_slots; 132517885a7bSLuigi Rizzo uint32_t head, cur; 1326f9790aebSLuigi Rizzo 132717885a7bSLuigi Rizzo ND("%s kc %d kt %d h %d c %d t %d", 132817885a7bSLuigi Rizzo kring->name, 132917885a7bSLuigi Rizzo kring->nr_hwcur, kring->nr_hwtail, 133017885a7bSLuigi Rizzo ring->head, ring->cur, ring->tail); 133117885a7bSLuigi Rizzo /* 133217885a7bSLuigi Rizzo * Before storing the new values, we should check they do not 133317885a7bSLuigi Rizzo * move backwards. However: 133417885a7bSLuigi Rizzo * - head is not an issue because the previous value is hwcur; 133517885a7bSLuigi Rizzo * - cur could in principle go back, however it does not matter 133617885a7bSLuigi Rizzo * because we are processing a brand new rxsync() 133717885a7bSLuigi Rizzo */ 133817885a7bSLuigi Rizzo cur = kring->rcur = ring->cur; /* read only once */ 133917885a7bSLuigi Rizzo head = kring->rhead = ring->head; /* read only once */ 1340f9790aebSLuigi Rizzo #if 1 /* kernel sanity checks */ 134117885a7bSLuigi Rizzo if (kring->nr_hwcur >= n || kring->nr_hwtail >= n) 1342f9790aebSLuigi Rizzo goto error; 1343f9790aebSLuigi Rizzo #endif /* kernel sanity checks */ 1344f9790aebSLuigi Rizzo /* user sanity checks */ 134517885a7bSLuigi Rizzo if (kring->nr_hwtail >= kring->nr_hwcur) { 134617885a7bSLuigi Rizzo /* want hwcur <= rhead <= hwtail */ 134717885a7bSLuigi Rizzo if (head < kring->nr_hwcur || head > kring->nr_hwtail) 1348f9790aebSLuigi Rizzo goto error; 134917885a7bSLuigi Rizzo /* and also rhead <= rcur <= hwtail */ 135017885a7bSLuigi Rizzo if (cur < head || cur > kring->nr_hwtail) 1351f9790aebSLuigi Rizzo goto error; 1352f9790aebSLuigi Rizzo } else { 135317885a7bSLuigi Rizzo /* we need rhead outside hwtail..hwcur */ 135417885a7bSLuigi Rizzo if (head < kring->nr_hwcur && head > kring->nr_hwtail) 1355f9790aebSLuigi Rizzo goto error; 135617885a7bSLuigi Rizzo /* two cases now: head <= hwtail or head >= hwcur */ 135717885a7bSLuigi Rizzo if (head <= kring->nr_hwtail) { 135817885a7bSLuigi Rizzo /* want head <= cur <= hwtail */ 135917885a7bSLuigi Rizzo if (cur < head || cur > kring->nr_hwtail) 1360f9790aebSLuigi Rizzo goto error; 136117885a7bSLuigi Rizzo } else { 136217885a7bSLuigi Rizzo /* cur must be outside hwtail..head */ 136317885a7bSLuigi Rizzo if (cur < head && cur > kring->nr_hwtail) 1364f9790aebSLuigi Rizzo goto error; 1365f9790aebSLuigi Rizzo } 1366f9790aebSLuigi Rizzo } 136717885a7bSLuigi Rizzo if (ring->tail != kring->rtail) { 136817885a7bSLuigi Rizzo RD(5, "%s tail overwritten was %d need %d", 136917885a7bSLuigi Rizzo kring->name, 137017885a7bSLuigi Rizzo ring->tail, kring->rtail); 137117885a7bSLuigi Rizzo ring->tail = kring->rtail; 137217885a7bSLuigi Rizzo } 137317885a7bSLuigi Rizzo return head; 1374f9790aebSLuigi Rizzo 1375f9790aebSLuigi Rizzo error: 137617885a7bSLuigi Rizzo RD(5, "kring error: hwcur %d rcur %d hwtail %d head %d cur %d tail %d", 1377f9790aebSLuigi Rizzo kring->nr_hwcur, 137817885a7bSLuigi Rizzo kring->rcur, kring->nr_hwtail, 137917885a7bSLuigi Rizzo kring->rhead, kring->rcur, ring->tail); 1380f9790aebSLuigi Rizzo return n; 1381f9790aebSLuigi Rizzo } 1382f9790aebSLuigi Rizzo 138317885a7bSLuigi Rizzo 1384f9790aebSLuigi Rizzo /* 138568b8534bSLuigi Rizzo * Error routine called when txsync/rxsync detects an error. 138617885a7bSLuigi Rizzo * Can't do much more than resetting head =cur = hwcur, tail = hwtail 138768b8534bSLuigi Rizzo * Return 1 on reinit. 1388506cc70cSLuigi Rizzo * 1389506cc70cSLuigi Rizzo * This routine is only called by the upper half of the kernel. 1390506cc70cSLuigi Rizzo * It only reads hwcur (which is changed only by the upper half, too) 139117885a7bSLuigi Rizzo * and hwtail (which may be changed by the lower half, but only on 1392506cc70cSLuigi Rizzo * a tx ring and only to increase it, so any error will be recovered 1393506cc70cSLuigi Rizzo * on the next call). For the above, we don't strictly need to call 1394506cc70cSLuigi Rizzo * it under lock. 139568b8534bSLuigi Rizzo */ 139668b8534bSLuigi Rizzo int 139768b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring) 139868b8534bSLuigi Rizzo { 139968b8534bSLuigi Rizzo struct netmap_ring *ring = kring->ring; 140068b8534bSLuigi Rizzo u_int i, lim = kring->nkr_num_slots - 1; 140168b8534bSLuigi Rizzo int errors = 0; 140268b8534bSLuigi Rizzo 1403ce3ee1e7SLuigi Rizzo // XXX KASSERT nm_kr_tryget 1404f9790aebSLuigi Rizzo RD(10, "called for %s", NM_IFPNAME(kring->na->ifp)); 140517885a7bSLuigi Rizzo // XXX probably wrong to trust userspace 140617885a7bSLuigi Rizzo kring->rhead = ring->head; 140717885a7bSLuigi Rizzo kring->rcur = ring->cur; 140817885a7bSLuigi Rizzo kring->rtail = ring->tail; 140917885a7bSLuigi Rizzo 141068b8534bSLuigi Rizzo if (ring->cur > lim) 141168b8534bSLuigi Rizzo errors++; 141217885a7bSLuigi Rizzo if (ring->head > lim) 141317885a7bSLuigi Rizzo errors++; 141417885a7bSLuigi Rizzo if (ring->tail > lim) 141517885a7bSLuigi Rizzo errors++; 141668b8534bSLuigi Rizzo for (i = 0; i <= lim; i++) { 141768b8534bSLuigi Rizzo u_int idx = ring->slot[i].buf_idx; 141868b8534bSLuigi Rizzo u_int len = ring->slot[i].len; 141968b8534bSLuigi Rizzo if (idx < 2 || idx >= netmap_total_buffers) { 142017885a7bSLuigi Rizzo RD(5, "bad index at slot %d idx %d len %d ", i, idx, len); 142168b8534bSLuigi Rizzo ring->slot[i].buf_idx = 0; 142268b8534bSLuigi Rizzo ring->slot[i].len = 0; 1423ce3ee1e7SLuigi Rizzo } else if (len > NETMAP_BDG_BUF_SIZE(kring->na->nm_mem)) { 142468b8534bSLuigi Rizzo ring->slot[i].len = 0; 142517885a7bSLuigi Rizzo RD(5, "bad len at slot %d idx %d len %d", i, idx, len); 142668b8534bSLuigi Rizzo } 142768b8534bSLuigi Rizzo } 142868b8534bSLuigi Rizzo if (errors) { 14298241616dSLuigi Rizzo RD(10, "total %d errors", errors); 143017885a7bSLuigi Rizzo RD(10, "%s reinit, cur %d -> %d tail %d -> %d", 143117885a7bSLuigi Rizzo kring->name, 143268b8534bSLuigi Rizzo ring->cur, kring->nr_hwcur, 143317885a7bSLuigi Rizzo ring->tail, kring->nr_hwtail); 143417885a7bSLuigi Rizzo ring->head = kring->rhead = kring->nr_hwcur; 143517885a7bSLuigi Rizzo ring->cur = kring->rcur = kring->nr_hwcur; 143617885a7bSLuigi Rizzo ring->tail = kring->rtail = kring->nr_hwtail; 143768b8534bSLuigi Rizzo } 143868b8534bSLuigi Rizzo return (errors ? 1 : 0); 143968b8534bSLuigi Rizzo } 144068b8534bSLuigi Rizzo 144168b8534bSLuigi Rizzo 144268b8534bSLuigi Rizzo /* 144368b8534bSLuigi Rizzo * Set the ring ID. For devices with a single queue, a request 144468b8534bSLuigi Rizzo * for all rings is the same as a single ring. 144568b8534bSLuigi Rizzo */ 144668b8534bSLuigi Rizzo static int 1447f0ea3689SLuigi Rizzo netmap_set_ringid(struct netmap_priv_d *priv, uint16_t ringid, uint32_t flags) 144868b8534bSLuigi Rizzo { 1449f9790aebSLuigi Rizzo struct netmap_adapter *na = priv->np_na; 1450f0ea3689SLuigi Rizzo u_int j, i = ringid & NETMAP_RING_MASK; 1451f0ea3689SLuigi Rizzo u_int reg = flags & NR_REG_MASK; 145268b8534bSLuigi Rizzo 1453f0ea3689SLuigi Rizzo if (reg == NR_REG_DEFAULT) { 1454f0ea3689SLuigi Rizzo /* convert from old ringid to flags */ 145568b8534bSLuigi Rizzo if (ringid & NETMAP_SW_RING) { 1456f0ea3689SLuigi Rizzo reg = NR_REG_SW; 145768b8534bSLuigi Rizzo } else if (ringid & NETMAP_HW_RING) { 1458f0ea3689SLuigi Rizzo reg = NR_REG_ONE_NIC; 145968b8534bSLuigi Rizzo } else { 1460f0ea3689SLuigi Rizzo reg = NR_REG_ALL_NIC; 1461f0ea3689SLuigi Rizzo } 1462f0ea3689SLuigi Rizzo D("deprecated API, old ringid 0x%x -> ringid %x reg %d", ringid, i, reg); 1463f0ea3689SLuigi Rizzo } 1464f0ea3689SLuigi Rizzo switch (reg) { 1465f0ea3689SLuigi Rizzo case NR_REG_ALL_NIC: 1466f0ea3689SLuigi Rizzo case NR_REG_PIPE_MASTER: 1467f0ea3689SLuigi Rizzo case NR_REG_PIPE_SLAVE: 1468f0ea3689SLuigi Rizzo priv->np_txqfirst = 0; 1469f0ea3689SLuigi Rizzo priv->np_txqlast = na->num_tx_rings; 1470f0ea3689SLuigi Rizzo priv->np_rxqfirst = 0; 1471f0ea3689SLuigi Rizzo priv->np_rxqlast = na->num_rx_rings; 1472f0ea3689SLuigi Rizzo ND("%s %d %d", "ALL/PIPE", 1473f0ea3689SLuigi Rizzo priv->np_rxqfirst, priv->np_rxqlast); 1474f0ea3689SLuigi Rizzo break; 1475f0ea3689SLuigi Rizzo case NR_REG_SW: 1476f0ea3689SLuigi Rizzo case NR_REG_NIC_SW: 1477f0ea3689SLuigi Rizzo if (!(na->na_flags & NAF_HOST_RINGS)) { 1478f0ea3689SLuigi Rizzo D("host rings not supported"); 1479f0ea3689SLuigi Rizzo return EINVAL; 1480f0ea3689SLuigi Rizzo } 1481f0ea3689SLuigi Rizzo priv->np_txqfirst = (reg == NR_REG_SW ? 1482f0ea3689SLuigi Rizzo na->num_tx_rings : 0); 1483f0ea3689SLuigi Rizzo priv->np_txqlast = na->num_tx_rings + 1; 1484f0ea3689SLuigi Rizzo priv->np_rxqfirst = (reg == NR_REG_SW ? 1485f0ea3689SLuigi Rizzo na->num_rx_rings : 0); 1486f0ea3689SLuigi Rizzo priv->np_rxqlast = na->num_rx_rings + 1; 1487f0ea3689SLuigi Rizzo ND("%s %d %d", reg == NR_REG_SW ? "SW" : "NIC+SW", 1488f0ea3689SLuigi Rizzo priv->np_rxqfirst, priv->np_rxqlast); 1489f0ea3689SLuigi Rizzo break; 1490f0ea3689SLuigi Rizzo case NR_REG_ONE_NIC: 1491f0ea3689SLuigi Rizzo if (i >= na->num_tx_rings && i >= na->num_rx_rings) { 1492f0ea3689SLuigi Rizzo D("invalid ring id %d", i); 1493f0ea3689SLuigi Rizzo return EINVAL; 1494f0ea3689SLuigi Rizzo } 1495f0ea3689SLuigi Rizzo /* if not enough rings, use the first one */ 1496f0ea3689SLuigi Rizzo j = i; 1497f0ea3689SLuigi Rizzo if (j >= na->num_tx_rings) 1498f0ea3689SLuigi Rizzo j = 0; 1499f0ea3689SLuigi Rizzo priv->np_txqfirst = j; 1500f0ea3689SLuigi Rizzo priv->np_txqlast = j + 1; 1501f0ea3689SLuigi Rizzo j = i; 1502f0ea3689SLuigi Rizzo if (j >= na->num_rx_rings) 1503f0ea3689SLuigi Rizzo j = 0; 1504f0ea3689SLuigi Rizzo priv->np_rxqfirst = j; 1505f0ea3689SLuigi Rizzo priv->np_rxqlast = j + 1; 1506f0ea3689SLuigi Rizzo break; 1507f0ea3689SLuigi Rizzo default: 1508f0ea3689SLuigi Rizzo D("invalid regif type %d", reg); 1509f0ea3689SLuigi Rizzo return EINVAL; 151068b8534bSLuigi Rizzo } 151168b8534bSLuigi Rizzo priv->np_txpoll = (ringid & NETMAP_NO_TX_POLL) ? 0 : 1; 1512f0ea3689SLuigi Rizzo priv->np_flags = (flags & ~NR_REG_MASK) | reg; 1513f0ea3689SLuigi Rizzo if (nm_tx_si_user(priv)) 1514f0ea3689SLuigi Rizzo na->tx_si_users++; 1515f0ea3689SLuigi Rizzo if (nm_rx_si_user(priv)) 1516f0ea3689SLuigi Rizzo na->rx_si_users++; 1517ae10d1afSLuigi Rizzo if (netmap_verbose) { 1518f0ea3689SLuigi Rizzo D("%s: tx [%d,%d) rx [%d,%d) id %d", 1519f0ea3689SLuigi Rizzo NM_IFPNAME(na->ifp), 1520f0ea3689SLuigi Rizzo priv->np_txqfirst, 1521f0ea3689SLuigi Rizzo priv->np_txqlast, 1522f0ea3689SLuigi Rizzo priv->np_rxqfirst, 1523f0ea3689SLuigi Rizzo priv->np_rxqlast, 1524f0ea3689SLuigi Rizzo i); 1525ae10d1afSLuigi Rizzo } 152668b8534bSLuigi Rizzo return 0; 152768b8534bSLuigi Rizzo } 152868b8534bSLuigi Rizzo 1529f18be576SLuigi Rizzo /* 1530f18be576SLuigi Rizzo * possibly move the interface to netmap-mode. 1531f18be576SLuigi Rizzo * If success it returns a pointer to netmap_if, otherwise NULL. 1532ce3ee1e7SLuigi Rizzo * This must be called with NMG_LOCK held. 1533f18be576SLuigi Rizzo */ 1534f9790aebSLuigi Rizzo struct netmap_if * 1535f9790aebSLuigi Rizzo netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na, 1536f0ea3689SLuigi Rizzo uint16_t ringid, uint32_t flags, int *err) 1537f18be576SLuigi Rizzo { 1538f9790aebSLuigi Rizzo struct ifnet *ifp = na->ifp; 1539f18be576SLuigi Rizzo struct netmap_if *nifp = NULL; 1540f9790aebSLuigi Rizzo int error, need_mem = 0; 1541f18be576SLuigi Rizzo 1542ce3ee1e7SLuigi Rizzo NMG_LOCK_ASSERT(); 1543f18be576SLuigi Rizzo /* ring configuration may have changed, fetch from the card */ 1544f18be576SLuigi Rizzo netmap_update_config(na); 1545f9790aebSLuigi Rizzo priv->np_na = na; /* store the reference */ 1546f0ea3689SLuigi Rizzo error = netmap_set_ringid(priv, ringid, flags); 1547f18be576SLuigi Rizzo if (error) 1548f18be576SLuigi Rizzo goto out; 1549ce3ee1e7SLuigi Rizzo /* ensure allocators are ready */ 1550ce3ee1e7SLuigi Rizzo need_mem = !netmap_have_memory_locked(priv); 1551ce3ee1e7SLuigi Rizzo if (need_mem) { 1552ce3ee1e7SLuigi Rizzo error = netmap_get_memory_locked(priv); 1553ce3ee1e7SLuigi Rizzo ND("get_memory returned %d", error); 1554ce3ee1e7SLuigi Rizzo if (error) 1555ce3ee1e7SLuigi Rizzo goto out; 1556ce3ee1e7SLuigi Rizzo } 1557f9790aebSLuigi Rizzo nifp = netmap_if_new(NM_IFPNAME(ifp), na); 1558f18be576SLuigi Rizzo if (nifp == NULL) { /* allocation failed */ 1559ce3ee1e7SLuigi Rizzo /* we should drop the allocator, but only 1560ce3ee1e7SLuigi Rizzo * if we were the ones who grabbed it 1561ce3ee1e7SLuigi Rizzo */ 1562f18be576SLuigi Rizzo error = ENOMEM; 1563ce3ee1e7SLuigi Rizzo goto out; 1564ce3ee1e7SLuigi Rizzo } 1565f9790aebSLuigi Rizzo na->active_fds++; 1566ce3ee1e7SLuigi Rizzo if (ifp->if_capenable & IFCAP_NETMAP) { 1567f18be576SLuigi Rizzo /* was already set */ 1568f18be576SLuigi Rizzo } else { 1569f18be576SLuigi Rizzo /* Otherwise set the card in netmap mode 1570f18be576SLuigi Rizzo * and make it use the shared buffers. 1571ce3ee1e7SLuigi Rizzo * 1572ce3ee1e7SLuigi Rizzo * do not core lock because the race is harmless here, 1573ce3ee1e7SLuigi Rizzo * there cannot be any traffic to netmap_transmit() 1574ce3ee1e7SLuigi Rizzo */ 1575f9790aebSLuigi Rizzo na->na_lut = na->nm_mem->pools[NETMAP_BUF_POOL].lut; 1576f9790aebSLuigi Rizzo ND("%p->na_lut == %p", na, na->na_lut); 1577f9790aebSLuigi Rizzo na->na_lut_objtotal = na->nm_mem->pools[NETMAP_BUF_POOL].objtotal; 1578f9790aebSLuigi Rizzo error = na->nm_register(na, 1); /* mode on */ 1579f18be576SLuigi Rizzo if (error) { 1580ce3ee1e7SLuigi Rizzo netmap_do_unregif(priv, nifp); 1581f18be576SLuigi Rizzo nifp = NULL; 1582f18be576SLuigi Rizzo } 1583f18be576SLuigi Rizzo } 1584f18be576SLuigi Rizzo out: 1585f18be576SLuigi Rizzo *err = error; 1586f9790aebSLuigi Rizzo if (error) { 1587f9790aebSLuigi Rizzo priv->np_na = NULL; 1588f9790aebSLuigi Rizzo if (need_mem) 1589f9790aebSLuigi Rizzo netmap_drop_memory_locked(priv); 1590f9790aebSLuigi Rizzo } 1591ce3ee1e7SLuigi Rizzo if (nifp != NULL) { 1592ce3ee1e7SLuigi Rizzo /* 1593ce3ee1e7SLuigi Rizzo * advertise that the interface is ready bt setting ni_nifp. 1594ce3ee1e7SLuigi Rizzo * The barrier is needed because readers (poll and *SYNC) 1595ce3ee1e7SLuigi Rizzo * check for priv->np_nifp != NULL without locking 1596ce3ee1e7SLuigi Rizzo */ 1597ce3ee1e7SLuigi Rizzo wmb(); /* make sure previous writes are visible to all CPUs */ 1598ce3ee1e7SLuigi Rizzo priv->np_nifp = nifp; 1599ce3ee1e7SLuigi Rizzo } 1600f18be576SLuigi Rizzo return nifp; 1601f18be576SLuigi Rizzo } 1602f18be576SLuigi Rizzo 1603f18be576SLuigi Rizzo 1604f18be576SLuigi Rizzo 160568b8534bSLuigi Rizzo /* 160668b8534bSLuigi Rizzo * ioctl(2) support for the "netmap" device. 160768b8534bSLuigi Rizzo * 160868b8534bSLuigi Rizzo * Following a list of accepted commands: 160968b8534bSLuigi Rizzo * - NIOCGINFO 161068b8534bSLuigi Rizzo * - SIOCGIFADDR just for convenience 161168b8534bSLuigi Rizzo * - NIOCREGIF 161268b8534bSLuigi Rizzo * - NIOCTXSYNC 161368b8534bSLuigi Rizzo * - NIOCRXSYNC 161468b8534bSLuigi Rizzo * 161568b8534bSLuigi Rizzo * Return 0 on success, errno otherwise. 161668b8534bSLuigi Rizzo */ 1617f9790aebSLuigi Rizzo int 16180b8ed8e0SLuigi Rizzo netmap_ioctl(struct cdev *dev, u_long cmd, caddr_t data, 16190b8ed8e0SLuigi Rizzo int fflag, struct thread *td) 162068b8534bSLuigi Rizzo { 162168b8534bSLuigi Rizzo struct netmap_priv_d *priv = NULL; 1622ce3ee1e7SLuigi Rizzo struct ifnet *ifp = NULL; 162368b8534bSLuigi Rizzo struct nmreq *nmr = (struct nmreq *) data; 1624ce3ee1e7SLuigi Rizzo struct netmap_adapter *na = NULL; 162568b8534bSLuigi Rizzo int error; 1626f0ea3689SLuigi Rizzo u_int i, qfirst, qlast; 162768b8534bSLuigi Rizzo struct netmap_if *nifp; 1628ce3ee1e7SLuigi Rizzo struct netmap_kring *krings; 162968b8534bSLuigi Rizzo 16300b8ed8e0SLuigi Rizzo (void)dev; /* UNUSED */ 16310b8ed8e0SLuigi Rizzo (void)fflag; /* UNUSED */ 1632f196ce38SLuigi Rizzo 163317885a7bSLuigi Rizzo if (cmd == NIOCGINFO || cmd == NIOCREGIF) { 163417885a7bSLuigi Rizzo /* truncate name */ 163517885a7bSLuigi Rizzo nmr->nr_name[sizeof(nmr->nr_name) - 1] = '\0'; 163617885a7bSLuigi Rizzo if (nmr->nr_version != NETMAP_API) { 163717885a7bSLuigi Rizzo D("API mismatch for %s got %d need %d", 163817885a7bSLuigi Rizzo nmr->nr_name, 163917885a7bSLuigi Rizzo nmr->nr_version, NETMAP_API); 164017885a7bSLuigi Rizzo nmr->nr_version = NETMAP_API; 1641f0ea3689SLuigi Rizzo } 1642f0ea3689SLuigi Rizzo if (nmr->nr_version < NETMAP_MIN_API || 1643f0ea3689SLuigi Rizzo nmr->nr_version > NETMAP_MAX_API) { 164417885a7bSLuigi Rizzo return EINVAL; 164517885a7bSLuigi Rizzo } 164617885a7bSLuigi Rizzo } 1647506cc70cSLuigi Rizzo CURVNET_SET(TD_TO_VNET(td)); 1648506cc70cSLuigi Rizzo 164968b8534bSLuigi Rizzo error = devfs_get_cdevpriv((void **)&priv); 16508241616dSLuigi Rizzo if (error) { 1651506cc70cSLuigi Rizzo CURVNET_RESTORE(); 16528241616dSLuigi Rizzo /* XXX ENOENT should be impossible, since the priv 16538241616dSLuigi Rizzo * is now created in the open */ 16548241616dSLuigi Rizzo return (error == ENOENT ? ENXIO : error); 1655506cc70cSLuigi Rizzo } 165668b8534bSLuigi Rizzo 165768b8534bSLuigi Rizzo switch (cmd) { 165868b8534bSLuigi Rizzo case NIOCGINFO: /* return capabilities etc */ 1659f18be576SLuigi Rizzo if (nmr->nr_cmd == NETMAP_BDG_LIST) { 1660f18be576SLuigi Rizzo error = netmap_bdg_ctl(nmr, NULL); 1661f18be576SLuigi Rizzo break; 1662f18be576SLuigi Rizzo } 1663ce3ee1e7SLuigi Rizzo 1664ce3ee1e7SLuigi Rizzo NMG_LOCK(); 1665ce3ee1e7SLuigi Rizzo do { 1666ce3ee1e7SLuigi Rizzo /* memsize is always valid */ 1667ce3ee1e7SLuigi Rizzo struct netmap_mem_d *nmd = &nm_mem; 1668ce3ee1e7SLuigi Rizzo u_int memflags; 1669ce3ee1e7SLuigi Rizzo 1670ce3ee1e7SLuigi Rizzo if (nmr->nr_name[0] != '\0') { 1671ce3ee1e7SLuigi Rizzo /* get a refcount */ 1672f9790aebSLuigi Rizzo error = netmap_get_na(nmr, &na, 1 /* create */); 16738241616dSLuigi Rizzo if (error) 16748241616dSLuigi Rizzo break; 1675f9790aebSLuigi Rizzo nmd = na->nm_mem; /* get memory allocator */ 1676ce3ee1e7SLuigi Rizzo } 1677ce3ee1e7SLuigi Rizzo 1678f0ea3689SLuigi Rizzo error = netmap_mem_get_info(nmd, &nmr->nr_memsize, &memflags, 1679f0ea3689SLuigi Rizzo &nmr->nr_arg2); 1680ce3ee1e7SLuigi Rizzo if (error) 1681ce3ee1e7SLuigi Rizzo break; 1682ce3ee1e7SLuigi Rizzo if (na == NULL) /* only memory info */ 1683ce3ee1e7SLuigi Rizzo break; 16848241616dSLuigi Rizzo nmr->nr_offset = 0; 16858241616dSLuigi Rizzo nmr->nr_rx_slots = nmr->nr_tx_slots = 0; 1686ae10d1afSLuigi Rizzo netmap_update_config(na); 1687d76bf4ffSLuigi Rizzo nmr->nr_rx_rings = na->num_rx_rings; 1688d76bf4ffSLuigi Rizzo nmr->nr_tx_rings = na->num_tx_rings; 168964ae02c3SLuigi Rizzo nmr->nr_rx_slots = na->num_rx_desc; 169064ae02c3SLuigi Rizzo nmr->nr_tx_slots = na->num_tx_desc; 1691f9790aebSLuigi Rizzo netmap_adapter_put(na); 1692ce3ee1e7SLuigi Rizzo } while (0); 1693ce3ee1e7SLuigi Rizzo NMG_UNLOCK(); 169468b8534bSLuigi Rizzo break; 169568b8534bSLuigi Rizzo 169668b8534bSLuigi Rizzo case NIOCREGIF: 1697f18be576SLuigi Rizzo /* possibly attach/detach NIC and VALE switch */ 1698f18be576SLuigi Rizzo i = nmr->nr_cmd; 1699f9790aebSLuigi Rizzo if (i == NETMAP_BDG_ATTACH || i == NETMAP_BDG_DETACH 1700f0ea3689SLuigi Rizzo || i == NETMAP_BDG_VNET_HDR) { 1701f18be576SLuigi Rizzo error = netmap_bdg_ctl(nmr, NULL); 1702f18be576SLuigi Rizzo break; 1703f18be576SLuigi Rizzo } else if (i != 0) { 1704f18be576SLuigi Rizzo D("nr_cmd must be 0 not %d", i); 1705f18be576SLuigi Rizzo error = EINVAL; 1706f18be576SLuigi Rizzo break; 1707f18be576SLuigi Rizzo } 1708f18be576SLuigi Rizzo 17098241616dSLuigi Rizzo /* protect access to priv from concurrent NIOCREGIF */ 1710ce3ee1e7SLuigi Rizzo NMG_LOCK(); 1711ce3ee1e7SLuigi Rizzo do { 1712ce3ee1e7SLuigi Rizzo u_int memflags; 1713ce3ee1e7SLuigi Rizzo 1714f9790aebSLuigi Rizzo if (priv->np_na != NULL) { /* thread already registered */ 1715f0ea3689SLuigi Rizzo error = EBUSY; 1716506cc70cSLuigi Rizzo break; 1717506cc70cSLuigi Rizzo } 171868b8534bSLuigi Rizzo /* find the interface and a reference */ 1719f9790aebSLuigi Rizzo error = netmap_get_na(nmr, &na, 1 /* create */); /* keep reference */ 172068b8534bSLuigi Rizzo if (error) 1721ce3ee1e7SLuigi Rizzo break; 1722f9790aebSLuigi Rizzo ifp = na->ifp; 1723f9790aebSLuigi Rizzo if (NETMAP_OWNED_BY_KERN(na)) { 1724f9790aebSLuigi Rizzo netmap_adapter_put(na); 1725ce3ee1e7SLuigi Rizzo error = EBUSY; 1726ce3ee1e7SLuigi Rizzo break; 1727f196ce38SLuigi Rizzo } 1728f0ea3689SLuigi Rizzo nifp = netmap_do_regif(priv, na, nmr->nr_ringid, nmr->nr_flags, &error); 1729f18be576SLuigi Rizzo if (!nifp) { /* reg. failed, release priv and ref */ 1730f9790aebSLuigi Rizzo netmap_adapter_put(na); 17318241616dSLuigi Rizzo priv->np_nifp = NULL; 1732ce3ee1e7SLuigi Rizzo break; 173368b8534bSLuigi Rizzo } 1734f0ea3689SLuigi Rizzo priv->np_td = td; // XXX kqueue, debugging only 173568b8534bSLuigi Rizzo 173668b8534bSLuigi Rizzo /* return the offset of the netmap_if object */ 1737d76bf4ffSLuigi Rizzo nmr->nr_rx_rings = na->num_rx_rings; 1738d76bf4ffSLuigi Rizzo nmr->nr_tx_rings = na->num_tx_rings; 173964ae02c3SLuigi Rizzo nmr->nr_rx_slots = na->num_rx_desc; 174064ae02c3SLuigi Rizzo nmr->nr_tx_slots = na->num_tx_desc; 1741f0ea3689SLuigi Rizzo error = netmap_mem_get_info(na->nm_mem, &nmr->nr_memsize, &memflags, 1742f0ea3689SLuigi Rizzo &nmr->nr_arg2); 1743ce3ee1e7SLuigi Rizzo if (error) { 1744f9790aebSLuigi Rizzo netmap_adapter_put(na); 1745ce3ee1e7SLuigi Rizzo break; 1746ce3ee1e7SLuigi Rizzo } 1747ce3ee1e7SLuigi Rizzo if (memflags & NETMAP_MEM_PRIVATE) { 17483d819cb6SLuigi Rizzo *(uint32_t *)(uintptr_t)&nifp->ni_flags |= NI_PRIV_MEM; 1749ce3ee1e7SLuigi Rizzo } 1750f0ea3689SLuigi Rizzo priv->np_txsi = (priv->np_txqlast - priv->np_txqfirst > 1) ? 1751f0ea3689SLuigi Rizzo &na->tx_si : &na->tx_rings[priv->np_txqfirst].si; 1752f0ea3689SLuigi Rizzo priv->np_rxsi = (priv->np_rxqlast - priv->np_rxqfirst > 1) ? 1753f0ea3689SLuigi Rizzo &na->rx_si : &na->rx_rings[priv->np_rxqfirst].si; 1754f0ea3689SLuigi Rizzo 1755f0ea3689SLuigi Rizzo if (nmr->nr_arg3) { 1756f0ea3689SLuigi Rizzo D("requested %d extra buffers", nmr->nr_arg3); 1757f0ea3689SLuigi Rizzo nmr->nr_arg3 = netmap_extra_alloc(na, 1758f0ea3689SLuigi Rizzo &nifp->ni_bufs_head, nmr->nr_arg3); 1759f0ea3689SLuigi Rizzo D("got %d extra buffers", nmr->nr_arg3); 1760f0ea3689SLuigi Rizzo } 1761ce3ee1e7SLuigi Rizzo nmr->nr_offset = netmap_mem_if_offset(na->nm_mem, nifp); 1762ce3ee1e7SLuigi Rizzo } while (0); 1763ce3ee1e7SLuigi Rizzo NMG_UNLOCK(); 176468b8534bSLuigi Rizzo break; 176568b8534bSLuigi Rizzo 176668b8534bSLuigi Rizzo case NIOCTXSYNC: 176768b8534bSLuigi Rizzo case NIOCRXSYNC: 17688241616dSLuigi Rizzo nifp = priv->np_nifp; 17698241616dSLuigi Rizzo 17708241616dSLuigi Rizzo if (nifp == NULL) { 1771506cc70cSLuigi Rizzo error = ENXIO; 1772506cc70cSLuigi Rizzo break; 1773506cc70cSLuigi Rizzo } 17748241616dSLuigi Rizzo rmb(); /* make sure following reads are not from cache */ 17758241616dSLuigi Rizzo 1776f9790aebSLuigi Rizzo na = priv->np_na; /* we have a reference */ 17778241616dSLuigi Rizzo 1778f9790aebSLuigi Rizzo if (na == NULL) { 1779f9790aebSLuigi Rizzo D("Internal error: nifp != NULL && na == NULL"); 17808241616dSLuigi Rizzo error = ENXIO; 17818241616dSLuigi Rizzo break; 17828241616dSLuigi Rizzo } 17838241616dSLuigi Rizzo 1784f9790aebSLuigi Rizzo ifp = na->ifp; 1785f9790aebSLuigi Rizzo if (ifp == NULL) { 1786f9790aebSLuigi Rizzo RD(1, "the ifp is gone"); 1787f9790aebSLuigi Rizzo error = ENXIO; 1788f9790aebSLuigi Rizzo break; 1789f9790aebSLuigi Rizzo } 1790f9790aebSLuigi Rizzo 1791f0ea3689SLuigi Rizzo if (cmd == NIOCTXSYNC) { 1792f0ea3689SLuigi Rizzo krings = na->tx_rings; 1793f0ea3689SLuigi Rizzo qfirst = priv->np_txqfirst; 1794f0ea3689SLuigi Rizzo qlast = priv->np_txqlast; 1795f0ea3689SLuigi Rizzo } else { 1796f0ea3689SLuigi Rizzo krings = na->rx_rings; 1797f0ea3689SLuigi Rizzo qfirst = priv->np_rxqfirst; 1798f0ea3689SLuigi Rizzo qlast = priv->np_rxqlast; 179968b8534bSLuigi Rizzo } 180068b8534bSLuigi Rizzo 1801f0ea3689SLuigi Rizzo for (i = qfirst; i < qlast; i++) { 1802ce3ee1e7SLuigi Rizzo struct netmap_kring *kring = krings + i; 1803ce3ee1e7SLuigi Rizzo if (nm_kr_tryget(kring)) { 1804ce3ee1e7SLuigi Rizzo error = EBUSY; 1805ce3ee1e7SLuigi Rizzo goto out; 1806ce3ee1e7SLuigi Rizzo } 180768b8534bSLuigi Rizzo if (cmd == NIOCTXSYNC) { 180868b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_TXSYNC) 18093c0caf6cSLuigi Rizzo D("pre txsync ring %d cur %d hwcur %d", 181068b8534bSLuigi Rizzo i, kring->ring->cur, 181168b8534bSLuigi Rizzo kring->nr_hwcur); 181217885a7bSLuigi Rizzo if (nm_txsync_prologue(kring) >= kring->nkr_num_slots) { 181317885a7bSLuigi Rizzo netmap_ring_reinit(kring); 181417885a7bSLuigi Rizzo } else { 1815f0ea3689SLuigi Rizzo kring->nm_sync(kring, NAF_FORCE_RECLAIM); 181617885a7bSLuigi Rizzo } 181768b8534bSLuigi Rizzo if (netmap_verbose & NM_VERB_TXSYNC) 18183c0caf6cSLuigi Rizzo D("post txsync ring %d cur %d hwcur %d", 181968b8534bSLuigi Rizzo i, kring->ring->cur, 182068b8534bSLuigi Rizzo kring->nr_hwcur); 182168b8534bSLuigi Rizzo } else { 1822f0ea3689SLuigi Rizzo kring->nm_sync(kring, NAF_FORCE_READ); 182368b8534bSLuigi Rizzo microtime(&na->rx_rings[i].ring->ts); 182468b8534bSLuigi Rizzo } 1825ce3ee1e7SLuigi Rizzo nm_kr_put(kring); 182668b8534bSLuigi Rizzo } 182768b8534bSLuigi Rizzo 182868b8534bSLuigi Rizzo break; 182968b8534bSLuigi Rizzo 1830f196ce38SLuigi Rizzo #ifdef __FreeBSD__ 1831*89e3fd52SLuigi Rizzo case FIONBIO: 1832*89e3fd52SLuigi Rizzo case FIOASYNC: 1833*89e3fd52SLuigi Rizzo ND("FIONBIO/FIOASYNC are no-ops"); 1834*89e3fd52SLuigi Rizzo break; 1835*89e3fd52SLuigi Rizzo 183668b8534bSLuigi Rizzo case BIOCIMMEDIATE: 183768b8534bSLuigi Rizzo case BIOCGHDRCMPLT: 183868b8534bSLuigi Rizzo case BIOCSHDRCMPLT: 183968b8534bSLuigi Rizzo case BIOCSSEESENT: 184068b8534bSLuigi Rizzo D("ignore BIOCIMMEDIATE/BIOCSHDRCMPLT/BIOCSHDRCMPLT/BIOCSSEESENT"); 184168b8534bSLuigi Rizzo break; 184268b8534bSLuigi Rizzo 1843babc7c12SLuigi Rizzo default: /* allow device-specific ioctls */ 184468b8534bSLuigi Rizzo { 184568b8534bSLuigi Rizzo struct socket so; 1846ce3ee1e7SLuigi Rizzo 184768b8534bSLuigi Rizzo bzero(&so, sizeof(so)); 1848ce3ee1e7SLuigi Rizzo NMG_LOCK(); 1849f9790aebSLuigi Rizzo error = netmap_get_na(nmr, &na, 0 /* don't create */); /* keep reference */ 1850ce3ee1e7SLuigi Rizzo if (error) { 1851f9790aebSLuigi Rizzo netmap_adapter_put(na); 1852ce3ee1e7SLuigi Rizzo NMG_UNLOCK(); 185368b8534bSLuigi Rizzo break; 1854ce3ee1e7SLuigi Rizzo } 1855f9790aebSLuigi Rizzo ifp = na->ifp; 185668b8534bSLuigi Rizzo so.so_vnet = ifp->if_vnet; 185768b8534bSLuigi Rizzo // so->so_proto not null. 185868b8534bSLuigi Rizzo error = ifioctl(&so, cmd, data, td); 1859f9790aebSLuigi Rizzo netmap_adapter_put(na); 1860ce3ee1e7SLuigi Rizzo NMG_UNLOCK(); 1861babc7c12SLuigi Rizzo break; 186268b8534bSLuigi Rizzo } 1863f196ce38SLuigi Rizzo 1864f196ce38SLuigi Rizzo #else /* linux */ 1865f196ce38SLuigi Rizzo default: 1866f196ce38SLuigi Rizzo error = EOPNOTSUPP; 1867f196ce38SLuigi Rizzo #endif /* linux */ 186868b8534bSLuigi Rizzo } 1869ce3ee1e7SLuigi Rizzo out: 187068b8534bSLuigi Rizzo 1871506cc70cSLuigi Rizzo CURVNET_RESTORE(); 187268b8534bSLuigi Rizzo return (error); 187368b8534bSLuigi Rizzo } 187468b8534bSLuigi Rizzo 187568b8534bSLuigi Rizzo 187668b8534bSLuigi Rizzo /* 187768b8534bSLuigi Rizzo * select(2) and poll(2) handlers for the "netmap" device. 187868b8534bSLuigi Rizzo * 187968b8534bSLuigi Rizzo * Can be called for one or more queues. 188068b8534bSLuigi Rizzo * Return true the event mask corresponding to ready events. 188168b8534bSLuigi Rizzo * If there are no ready events, do a selrecord on either individual 1882ce3ee1e7SLuigi Rizzo * selinfo or on the global one. 188368b8534bSLuigi Rizzo * Device-dependent parts (locking and sync of tx/rx rings) 188468b8534bSLuigi Rizzo * are done through callbacks. 1885f196ce38SLuigi Rizzo * 188601c7d25fSLuigi Rizzo * On linux, arguments are really pwait, the poll table, and 'td' is struct file * 188701c7d25fSLuigi Rizzo * The first one is remapped to pwait as selrecord() uses the name as an 188801c7d25fSLuigi Rizzo * hidden argument. 188968b8534bSLuigi Rizzo */ 1890f9790aebSLuigi Rizzo int 189101c7d25fSLuigi Rizzo netmap_poll(struct cdev *dev, int events, struct thread *td) 189268b8534bSLuigi Rizzo { 189368b8534bSLuigi Rizzo struct netmap_priv_d *priv = NULL; 189468b8534bSLuigi Rizzo struct netmap_adapter *na; 189568b8534bSLuigi Rizzo struct ifnet *ifp; 189668b8534bSLuigi Rizzo struct netmap_kring *kring; 1897954dca4cSLuigi Rizzo u_int i, check_all_tx, check_all_rx, want_tx, want_rx, revents = 0; 189817885a7bSLuigi Rizzo struct mbq q; /* packets from hw queues to host stack */ 189901c7d25fSLuigi Rizzo void *pwait = dev; /* linux compatibility */ 1900f0ea3689SLuigi Rizzo int is_kevent = 0; 190101c7d25fSLuigi Rizzo 1902f9790aebSLuigi Rizzo /* 1903f9790aebSLuigi Rizzo * In order to avoid nested locks, we need to "double check" 1904f9790aebSLuigi Rizzo * txsync and rxsync if we decide to do a selrecord(). 1905f9790aebSLuigi Rizzo * retry_tx (and retry_rx, later) prevent looping forever. 1906f9790aebSLuigi Rizzo */ 190717885a7bSLuigi Rizzo int retry_tx = 1, retry_rx = 1; 1908ce3ee1e7SLuigi Rizzo 190901c7d25fSLuigi Rizzo (void)pwait; 1910f9790aebSLuigi Rizzo mbq_init(&q); 191168b8534bSLuigi Rizzo 1912f0ea3689SLuigi Rizzo /* 1913f0ea3689SLuigi Rizzo * XXX kevent has curthread->tp_fop == NULL, 1914f0ea3689SLuigi Rizzo * so devfs_get_cdevpriv() fails. We circumvent this by passing 1915f0ea3689SLuigi Rizzo * priv as the first argument, which is also useful to avoid 1916f0ea3689SLuigi Rizzo * the selrecord() which are not necessary in that case. 1917f0ea3689SLuigi Rizzo */ 1918f0ea3689SLuigi Rizzo if (devfs_get_cdevpriv((void **)&priv) != 0) { 1919f0ea3689SLuigi Rizzo is_kevent = 1; 1920f0ea3689SLuigi Rizzo if (netmap_verbose) 1921f0ea3689SLuigi Rizzo D("called from kevent"); 1922f0ea3689SLuigi Rizzo priv = (struct netmap_priv_d *)dev; 1923f0ea3689SLuigi Rizzo } 1924f0ea3689SLuigi Rizzo if (priv == NULL) 192568b8534bSLuigi Rizzo return POLLERR; 192668b8534bSLuigi Rizzo 19278241616dSLuigi Rizzo if (priv->np_nifp == NULL) { 19288241616dSLuigi Rizzo D("No if registered"); 19298241616dSLuigi Rizzo return POLLERR; 19308241616dSLuigi Rizzo } 19318241616dSLuigi Rizzo rmb(); /* make sure following reads are not from cache */ 19328241616dSLuigi Rizzo 1933f9790aebSLuigi Rizzo na = priv->np_na; 1934f9790aebSLuigi Rizzo ifp = na->ifp; 1935f9790aebSLuigi Rizzo // check for deleted 1936f9790aebSLuigi Rizzo if (ifp == NULL) { 1937f9790aebSLuigi Rizzo RD(1, "the ifp is gone"); 1938f9790aebSLuigi Rizzo return POLLERR; 1939f9790aebSLuigi Rizzo } 1940f9790aebSLuigi Rizzo 194168b8534bSLuigi Rizzo if ( (ifp->if_capenable & IFCAP_NETMAP) == 0) 194268b8534bSLuigi Rizzo return POLLERR; 194368b8534bSLuigi Rizzo 194468b8534bSLuigi Rizzo if (netmap_verbose & 0x8000) 1945f9790aebSLuigi Rizzo D("device %s events 0x%x", NM_IFPNAME(ifp), events); 194668b8534bSLuigi Rizzo want_tx = events & (POLLOUT | POLLWRNORM); 194768b8534bSLuigi Rizzo want_rx = events & (POLLIN | POLLRDNORM); 194868b8534bSLuigi Rizzo 1949091fd0abSLuigi Rizzo 195068b8534bSLuigi Rizzo /* 1951f9790aebSLuigi Rizzo * check_all_{tx|rx} are set if the card has more than one queue AND 1952f9790aebSLuigi Rizzo * the file descriptor is bound to all of them. If so, we sleep on 1953ce3ee1e7SLuigi Rizzo * the "global" selinfo, otherwise we sleep on individual selinfo 1954ce3ee1e7SLuigi Rizzo * (FreeBSD only allows two selinfo's per file descriptor). 1955ce3ee1e7SLuigi Rizzo * The interrupt routine in the driver wake one or the other 1956ce3ee1e7SLuigi Rizzo * (or both) depending on which clients are active. 195768b8534bSLuigi Rizzo * 195868b8534bSLuigi Rizzo * rxsync() is only called if we run out of buffers on a POLLIN. 195968b8534bSLuigi Rizzo * txsync() is called if we run out of buffers on POLLOUT, or 196068b8534bSLuigi Rizzo * there are pending packets to send. The latter can be disabled 196168b8534bSLuigi Rizzo * passing NETMAP_NO_TX_POLL in the NIOCREG call. 196268b8534bSLuigi Rizzo */ 1963f0ea3689SLuigi Rizzo check_all_tx = nm_tx_si_user(priv); 1964f0ea3689SLuigi Rizzo check_all_rx = nm_rx_si_user(priv); 196564ae02c3SLuigi Rizzo 196668b8534bSLuigi Rizzo /* 1967f9790aebSLuigi Rizzo * We start with a lock free round which is cheap if we have 1968f9790aebSLuigi Rizzo * slots available. If this fails, then lock and call the sync 196968b8534bSLuigi Rizzo * routines. 197068b8534bSLuigi Rizzo */ 1971f0ea3689SLuigi Rizzo for (i = priv->np_rxqfirst; want_rx && i < priv->np_rxqlast; i++) { 197268b8534bSLuigi Rizzo kring = &na->rx_rings[i]; 197317885a7bSLuigi Rizzo /* XXX compare ring->cur and kring->tail */ 197417885a7bSLuigi Rizzo if (!nm_ring_empty(kring->ring)) { 197568b8534bSLuigi Rizzo revents |= want_rx; 197668b8534bSLuigi Rizzo want_rx = 0; /* also breaks the loop */ 197768b8534bSLuigi Rizzo } 197868b8534bSLuigi Rizzo } 1979f0ea3689SLuigi Rizzo for (i = priv->np_txqfirst; want_tx && i < priv->np_txqlast; i++) { 198068b8534bSLuigi Rizzo kring = &na->tx_rings[i]; 198117885a7bSLuigi Rizzo /* XXX compare ring->cur and kring->tail */ 198217885a7bSLuigi Rizzo if (!nm_ring_empty(kring->ring)) { 198368b8534bSLuigi Rizzo revents |= want_tx; 198468b8534bSLuigi Rizzo want_tx = 0; /* also breaks the loop */ 198568b8534bSLuigi Rizzo } 198668b8534bSLuigi Rizzo } 198768b8534bSLuigi Rizzo 198868b8534bSLuigi Rizzo /* 198917885a7bSLuigi Rizzo * If we want to push packets out (priv->np_txpoll) or 199017885a7bSLuigi Rizzo * want_tx is still set, we must issue txsync calls 199117885a7bSLuigi Rizzo * (on all rings, to avoid that the tx rings stall). 1992f9790aebSLuigi Rizzo * XXX should also check cur != hwcur on the tx rings. 1993f9790aebSLuigi Rizzo * Fortunately, normal tx mode has np_txpoll set. 199468b8534bSLuigi Rizzo */ 199568b8534bSLuigi Rizzo if (priv->np_txpoll || want_tx) { 199617885a7bSLuigi Rizzo /* 199717885a7bSLuigi Rizzo * The first round checks if anyone is ready, if not 199817885a7bSLuigi Rizzo * do a selrecord and another round to handle races. 199917885a7bSLuigi Rizzo * want_tx goes to 0 if any space is found, and is 200017885a7bSLuigi Rizzo * used to skip rings with no pending transmissions. 2001ce3ee1e7SLuigi Rizzo */ 2002091fd0abSLuigi Rizzo flush_tx: 2003f0ea3689SLuigi Rizzo for (i = priv->np_txqfirst; i < priv->np_txqlast; i++) { 200417885a7bSLuigi Rizzo int found = 0; 200517885a7bSLuigi Rizzo 200668b8534bSLuigi Rizzo kring = &na->tx_rings[i]; 200768b8534bSLuigi Rizzo if (!want_tx && kring->ring->cur == kring->nr_hwcur) 200868b8534bSLuigi Rizzo continue; 200917885a7bSLuigi Rizzo /* only one thread does txsync */ 2010ce3ee1e7SLuigi Rizzo if (nm_kr_tryget(kring)) { 2011*89e3fd52SLuigi Rizzo if (netmap_verbose) 2012*89e3fd52SLuigi Rizzo RD(2, "%p lost race on txring %d, ok", 2013*89e3fd52SLuigi Rizzo priv, i); 201417885a7bSLuigi Rizzo continue; 201568b8534bSLuigi Rizzo } 201617885a7bSLuigi Rizzo if (nm_txsync_prologue(kring) >= kring->nkr_num_slots) { 201717885a7bSLuigi Rizzo netmap_ring_reinit(kring); 201817885a7bSLuigi Rizzo revents |= POLLERR; 201917885a7bSLuigi Rizzo } else { 2020f0ea3689SLuigi Rizzo if (kring->nm_sync(kring, 0)) 202168b8534bSLuigi Rizzo revents |= POLLERR; 202217885a7bSLuigi Rizzo } 202368b8534bSLuigi Rizzo 202417885a7bSLuigi Rizzo /* 202517885a7bSLuigi Rizzo * If we found new slots, notify potential 202617885a7bSLuigi Rizzo * listeners on the same ring. 202717885a7bSLuigi Rizzo * Since we just did a txsync, look at the copies 202817885a7bSLuigi Rizzo * of cur,tail in the kring. 2029f9790aebSLuigi Rizzo */ 203017885a7bSLuigi Rizzo found = kring->rcur != kring->rtail; 203117885a7bSLuigi Rizzo nm_kr_put(kring); 203217885a7bSLuigi Rizzo if (found) { /* notify other listeners */ 203368b8534bSLuigi Rizzo revents |= want_tx; 203468b8534bSLuigi Rizzo want_tx = 0; 2035f0ea3689SLuigi Rizzo na->nm_notify(na, i, NR_TX, 0); 203668b8534bSLuigi Rizzo } 2037ce3ee1e7SLuigi Rizzo } 2038f0ea3689SLuigi Rizzo if (want_tx && retry_tx && !is_kevent) { 2039954dca4cSLuigi Rizzo selrecord(td, check_all_tx ? 2040f0ea3689SLuigi Rizzo &na->tx_si : &na->tx_rings[priv->np_txqfirst].si); 2041ce3ee1e7SLuigi Rizzo retry_tx = 0; 2042ce3ee1e7SLuigi Rizzo goto flush_tx; 204368b8534bSLuigi Rizzo } 204468b8534bSLuigi Rizzo } 204568b8534bSLuigi Rizzo 204668b8534bSLuigi Rizzo /* 204717885a7bSLuigi Rizzo * If want_rx is still set scan receive rings. 204868b8534bSLuigi Rizzo * Do it on all rings because otherwise we starve. 204968b8534bSLuigi Rizzo */ 205068b8534bSLuigi Rizzo if (want_rx) { 205117885a7bSLuigi Rizzo int send_down = 0; /* transparent mode */ 205217885a7bSLuigi Rizzo /* two rounds here to for race avoidance */ 2053ce3ee1e7SLuigi Rizzo do_retry_rx: 2054f0ea3689SLuigi Rizzo for (i = priv->np_rxqfirst; i < priv->np_rxqlast; i++) { 205517885a7bSLuigi Rizzo int found = 0; 205617885a7bSLuigi Rizzo 205768b8534bSLuigi Rizzo kring = &na->rx_rings[i]; 2058ce3ee1e7SLuigi Rizzo 2059ce3ee1e7SLuigi Rizzo if (nm_kr_tryget(kring)) { 2060*89e3fd52SLuigi Rizzo if (netmap_verbose) 2061*89e3fd52SLuigi Rizzo RD(2, "%p lost race on rxring %d, ok", 2062*89e3fd52SLuigi Rizzo priv, i); 206317885a7bSLuigi Rizzo continue; 206468b8534bSLuigi Rizzo } 2065ce3ee1e7SLuigi Rizzo 206617885a7bSLuigi Rizzo /* 206717885a7bSLuigi Rizzo * transparent mode support: collect packets 206817885a7bSLuigi Rizzo * from the rxring(s). 206917885a7bSLuigi Rizzo * XXX NR_FORWARD should only be read on 2070ce3ee1e7SLuigi Rizzo * physical or NIC ports 2071ce3ee1e7SLuigi Rizzo */ 2072091fd0abSLuigi Rizzo if (netmap_fwd ||kring->ring->flags & NR_FORWARD) { 2073091fd0abSLuigi Rizzo ND(10, "forwarding some buffers up %d to %d", 2074091fd0abSLuigi Rizzo kring->nr_hwcur, kring->ring->cur); 2075091fd0abSLuigi Rizzo netmap_grab_packets(kring, &q, netmap_fwd); 2076091fd0abSLuigi Rizzo } 207768b8534bSLuigi Rizzo 2078f0ea3689SLuigi Rizzo if (kring->nm_sync(kring, 0)) 207968b8534bSLuigi Rizzo revents |= POLLERR; 20805819da83SLuigi Rizzo if (netmap_no_timestamp == 0 || 20815819da83SLuigi Rizzo kring->ring->flags & NR_TIMESTAMP) { 208268b8534bSLuigi Rizzo microtime(&kring->ring->ts); 20835819da83SLuigi Rizzo } 208417885a7bSLuigi Rizzo /* after an rxsync we can use kring->rcur, rtail */ 208517885a7bSLuigi Rizzo found = kring->rcur != kring->rtail; 208617885a7bSLuigi Rizzo nm_kr_put(kring); 208717885a7bSLuigi Rizzo if (found) { 208868b8534bSLuigi Rizzo revents |= want_rx; 2089ce3ee1e7SLuigi Rizzo retry_rx = 0; 2090f0ea3689SLuigi Rizzo na->nm_notify(na, i, NR_RX, 0); 209168b8534bSLuigi Rizzo } 209268b8534bSLuigi Rizzo } 209317885a7bSLuigi Rizzo 209417885a7bSLuigi Rizzo /* transparent mode XXX only during first pass ? */ 2095f0ea3689SLuigi Rizzo if (na->na_flags & NAF_HOST_RINGS) { 2096f0ea3689SLuigi Rizzo kring = &na->rx_rings[na->num_rx_rings]; 209717885a7bSLuigi Rizzo if (check_all_rx 209817885a7bSLuigi Rizzo && (netmap_fwd || kring->ring->flags & NR_FORWARD)) { 209917885a7bSLuigi Rizzo /* XXX fix to use kring fields */ 210017885a7bSLuigi Rizzo if (nm_ring_empty(kring->ring)) 210117885a7bSLuigi Rizzo send_down = netmap_rxsync_from_host(na, td, dev); 210217885a7bSLuigi Rizzo if (!nm_ring_empty(kring->ring)) 210317885a7bSLuigi Rizzo revents |= want_rx; 210417885a7bSLuigi Rizzo } 2105f0ea3689SLuigi Rizzo } 210617885a7bSLuigi Rizzo 2107f0ea3689SLuigi Rizzo if (retry_rx && !is_kevent) 2108954dca4cSLuigi Rizzo selrecord(td, check_all_rx ? 2109f0ea3689SLuigi Rizzo &na->rx_si : &na->rx_rings[priv->np_rxqfirst].si); 211017885a7bSLuigi Rizzo if (send_down > 0 || retry_rx) { 211117885a7bSLuigi Rizzo retry_rx = 0; 211217885a7bSLuigi Rizzo if (send_down) 211317885a7bSLuigi Rizzo goto flush_tx; /* and retry_rx */ 211417885a7bSLuigi Rizzo else 2115ce3ee1e7SLuigi Rizzo goto do_retry_rx; 2116ce3ee1e7SLuigi Rizzo } 211768b8534bSLuigi Rizzo } 2118091fd0abSLuigi Rizzo 211917885a7bSLuigi Rizzo /* 212017885a7bSLuigi Rizzo * Transparent mode: marked bufs on rx rings between 212117885a7bSLuigi Rizzo * kring->nr_hwcur and ring->head 212217885a7bSLuigi Rizzo * are passed to the other endpoint. 212317885a7bSLuigi Rizzo * 212417885a7bSLuigi Rizzo * In this mode we also scan the sw rxring, which in 212517885a7bSLuigi Rizzo * turn passes packets up. 212617885a7bSLuigi Rizzo * 212717885a7bSLuigi Rizzo * XXX Transparent mode at the moment requires to bind all 212817885a7bSLuigi Rizzo * rings to a single file descriptor. 2129ce3ee1e7SLuigi Rizzo */ 2130091fd0abSLuigi Rizzo 2131091fd0abSLuigi Rizzo if (q.head) 2132f9790aebSLuigi Rizzo netmap_send_up(na->ifp, &q); 213368b8534bSLuigi Rizzo 213468b8534bSLuigi Rizzo return (revents); 213568b8534bSLuigi Rizzo } 213668b8534bSLuigi Rizzo 213717885a7bSLuigi Rizzo 213817885a7bSLuigi Rizzo /*-------------------- driver support routines -------------------*/ 213968b8534bSLuigi Rizzo 2140f9790aebSLuigi Rizzo static int netmap_hw_krings_create(struct netmap_adapter *); 2141f9790aebSLuigi Rizzo 2142f9790aebSLuigi Rizzo static int 214317885a7bSLuigi Rizzo netmap_notify(struct netmap_adapter *na, u_int n_ring, 214417885a7bSLuigi Rizzo enum txrx tx, int flags) 2145f9790aebSLuigi Rizzo { 2146f9790aebSLuigi Rizzo struct netmap_kring *kring; 2147f9790aebSLuigi Rizzo 2148f9790aebSLuigi Rizzo if (tx == NR_TX) { 2149f9790aebSLuigi Rizzo kring = na->tx_rings + n_ring; 2150f0ea3689SLuigi Rizzo OS_selwakeup(&kring->si, PI_NET); 2151f0ea3689SLuigi Rizzo if (na->tx_si_users > 0) 2152f0ea3689SLuigi Rizzo OS_selwakeup(&na->tx_si, PI_NET); 2153f9790aebSLuigi Rizzo } else { 2154f9790aebSLuigi Rizzo kring = na->rx_rings + n_ring; 2155f0ea3689SLuigi Rizzo OS_selwakeup(&kring->si, PI_NET); 2156f0ea3689SLuigi Rizzo if (na->rx_si_users > 0) 2157f0ea3689SLuigi Rizzo OS_selwakeup(&na->rx_si, PI_NET); 2158f9790aebSLuigi Rizzo } 2159f9790aebSLuigi Rizzo return 0; 2160f9790aebSLuigi Rizzo } 2161f9790aebSLuigi Rizzo 2162f9790aebSLuigi Rizzo 2163f9790aebSLuigi Rizzo // XXX check handling of failures 2164f9790aebSLuigi Rizzo int 2165f9790aebSLuigi Rizzo netmap_attach_common(struct netmap_adapter *na) 2166f9790aebSLuigi Rizzo { 2167f9790aebSLuigi Rizzo struct ifnet *ifp = na->ifp; 2168f9790aebSLuigi Rizzo 2169f9790aebSLuigi Rizzo if (na->num_tx_rings == 0 || na->num_rx_rings == 0) { 2170f9790aebSLuigi Rizzo D("%s: invalid rings tx %d rx %d", 2171f9790aebSLuigi Rizzo ifp->if_xname, na->num_tx_rings, na->num_rx_rings); 2172f9790aebSLuigi Rizzo return EINVAL; 2173f9790aebSLuigi Rizzo } 2174f9790aebSLuigi Rizzo WNA(ifp) = na; 217517885a7bSLuigi Rizzo 217617885a7bSLuigi Rizzo /* the following is only needed for na that use the host port. 217717885a7bSLuigi Rizzo * XXX do we have something similar for linux ? 217817885a7bSLuigi Rizzo */ 217917885a7bSLuigi Rizzo #ifdef __FreeBSD__ 218017885a7bSLuigi Rizzo na->if_input = ifp->if_input; /* for netmap_send_up */ 218117885a7bSLuigi Rizzo #endif /* __FreeBSD__ */ 218217885a7bSLuigi Rizzo 2183f9790aebSLuigi Rizzo NETMAP_SET_CAPABLE(ifp); 2184f9790aebSLuigi Rizzo if (na->nm_krings_create == NULL) { 2185f9790aebSLuigi Rizzo na->nm_krings_create = netmap_hw_krings_create; 218617885a7bSLuigi Rizzo na->nm_krings_delete = netmap_hw_krings_delete; 2187f9790aebSLuigi Rizzo } 2188f9790aebSLuigi Rizzo if (na->nm_notify == NULL) 2189f9790aebSLuigi Rizzo na->nm_notify = netmap_notify; 2190f9790aebSLuigi Rizzo na->active_fds = 0; 2191f9790aebSLuigi Rizzo 2192f9790aebSLuigi Rizzo if (na->nm_mem == NULL) 2193f9790aebSLuigi Rizzo na->nm_mem = &nm_mem; 2194f9790aebSLuigi Rizzo return 0; 2195f9790aebSLuigi Rizzo } 2196f9790aebSLuigi Rizzo 2197f9790aebSLuigi Rizzo 2198f9790aebSLuigi Rizzo void 2199f9790aebSLuigi Rizzo netmap_detach_common(struct netmap_adapter *na) 2200f9790aebSLuigi Rizzo { 2201f9790aebSLuigi Rizzo if (na->ifp) 2202f9790aebSLuigi Rizzo WNA(na->ifp) = NULL; /* XXX do we need this? */ 2203f9790aebSLuigi Rizzo 2204f9790aebSLuigi Rizzo if (na->tx_rings) { /* XXX should not happen */ 2205f9790aebSLuigi Rizzo D("freeing leftover tx_rings"); 2206f9790aebSLuigi Rizzo na->nm_krings_delete(na); 2207f9790aebSLuigi Rizzo } 2208f0ea3689SLuigi Rizzo netmap_pipe_dealloc(na); 2209f9790aebSLuigi Rizzo if (na->na_flags & NAF_MEM_OWNER) 2210f9790aebSLuigi Rizzo netmap_mem_private_delete(na->nm_mem); 2211f9790aebSLuigi Rizzo bzero(na, sizeof(*na)); 2212f9790aebSLuigi Rizzo free(na, M_DEVBUF); 2213f9790aebSLuigi Rizzo } 2214f9790aebSLuigi Rizzo 2215f18be576SLuigi Rizzo 221668b8534bSLuigi Rizzo /* 221768b8534bSLuigi Rizzo * Initialize a ``netmap_adapter`` object created by driver on attach. 221868b8534bSLuigi Rizzo * We allocate a block of memory with room for a struct netmap_adapter 221968b8534bSLuigi Rizzo * plus two sets of N+2 struct netmap_kring (where N is the number 222068b8534bSLuigi Rizzo * of hardware rings): 222168b8534bSLuigi Rizzo * krings 0..N-1 are for the hardware queues. 222268b8534bSLuigi Rizzo * kring N is for the host stack queue 222317885a7bSLuigi Rizzo * kring N+1 is only used for the selinfo for all queues. // XXX still true ? 222468b8534bSLuigi Rizzo * Return 0 on success, ENOMEM otherwise. 222568b8534bSLuigi Rizzo */ 222668b8534bSLuigi Rizzo int 2227f9790aebSLuigi Rizzo netmap_attach(struct netmap_adapter *arg) 222868b8534bSLuigi Rizzo { 2229f9790aebSLuigi Rizzo struct netmap_hw_adapter *hwna = NULL; 2230f9790aebSLuigi Rizzo // XXX when is arg == NULL ? 2231ae10d1afSLuigi Rizzo struct ifnet *ifp = arg ? arg->ifp : NULL; 223268b8534bSLuigi Rizzo 2233ae10d1afSLuigi Rizzo if (arg == NULL || ifp == NULL) 2234ae10d1afSLuigi Rizzo goto fail; 2235f9790aebSLuigi Rizzo hwna = malloc(sizeof(*hwna), M_DEVBUF, M_NOWAIT | M_ZERO); 2236f9790aebSLuigi Rizzo if (hwna == NULL) 2237ae10d1afSLuigi Rizzo goto fail; 2238f9790aebSLuigi Rizzo hwna->up = *arg; 2239f0ea3689SLuigi Rizzo hwna->up.na_flags |= NAF_HOST_RINGS; 2240f9790aebSLuigi Rizzo if (netmap_attach_common(&hwna->up)) { 2241f9790aebSLuigi Rizzo free(hwna, M_DEVBUF); 2242f9790aebSLuigi Rizzo goto fail; 2243f9790aebSLuigi Rizzo } 2244f9790aebSLuigi Rizzo netmap_adapter_get(&hwna->up); 2245f9790aebSLuigi Rizzo 224664ae02c3SLuigi Rizzo #ifdef linux 2247f18be576SLuigi Rizzo if (ifp->netdev_ops) { 2248f18be576SLuigi Rizzo /* prepare a clone of the netdev ops */ 2249f18be576SLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28) 2250f9790aebSLuigi Rizzo hwna->nm_ndo.ndo_start_xmit = ifp->netdev_ops; 2251f18be576SLuigi Rizzo #else 2252f9790aebSLuigi Rizzo hwna->nm_ndo = *ifp->netdev_ops; 2253f18be576SLuigi Rizzo #endif 2254f18be576SLuigi Rizzo } 2255f9790aebSLuigi Rizzo hwna->nm_ndo.ndo_start_xmit = linux_netmap_start_xmit; 2256ce3ee1e7SLuigi Rizzo #endif /* linux */ 2257f9790aebSLuigi Rizzo 2258f9790aebSLuigi Rizzo D("success for %s", NM_IFPNAME(ifp)); 2259ae10d1afSLuigi Rizzo return 0; 226068b8534bSLuigi Rizzo 2261ae10d1afSLuigi Rizzo fail: 2262f9790aebSLuigi Rizzo D("fail, arg %p ifp %p na %p", arg, ifp, hwna); 2263849bec0eSLuigi Rizzo netmap_detach(ifp); 2264f9790aebSLuigi Rizzo return (hwna ? EINVAL : ENOMEM); 226568b8534bSLuigi Rizzo } 226668b8534bSLuigi Rizzo 226768b8534bSLuigi Rizzo 2268f9790aebSLuigi Rizzo void 2269f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_get)(struct netmap_adapter *na) 2270f9790aebSLuigi Rizzo { 2271f9790aebSLuigi Rizzo if (!na) { 2272f9790aebSLuigi Rizzo return; 2273f9790aebSLuigi Rizzo } 2274f9790aebSLuigi Rizzo 2275f9790aebSLuigi Rizzo refcount_acquire(&na->na_refcount); 2276f9790aebSLuigi Rizzo } 2277f9790aebSLuigi Rizzo 2278f9790aebSLuigi Rizzo 2279f9790aebSLuigi Rizzo /* returns 1 iff the netmap_adapter is destroyed */ 2280f9790aebSLuigi Rizzo int 2281f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_put)(struct netmap_adapter *na) 2282f9790aebSLuigi Rizzo { 2283f9790aebSLuigi Rizzo if (!na) 2284f9790aebSLuigi Rizzo return 1; 2285f9790aebSLuigi Rizzo 2286f9790aebSLuigi Rizzo if (!refcount_release(&na->na_refcount)) 2287f9790aebSLuigi Rizzo return 0; 2288f9790aebSLuigi Rizzo 2289f9790aebSLuigi Rizzo if (na->nm_dtor) 2290f9790aebSLuigi Rizzo na->nm_dtor(na); 2291f9790aebSLuigi Rizzo 2292f9790aebSLuigi Rizzo netmap_detach_common(na); 2293f9790aebSLuigi Rizzo 2294f9790aebSLuigi Rizzo return 1; 2295f9790aebSLuigi Rizzo } 2296f9790aebSLuigi Rizzo 2297f9790aebSLuigi Rizzo int 2298f9790aebSLuigi Rizzo netmap_hw_krings_create(struct netmap_adapter *na) 2299f9790aebSLuigi Rizzo { 2300f0ea3689SLuigi Rizzo int ret = netmap_krings_create(na, 0); 230117885a7bSLuigi Rizzo if (ret == 0) { 230217885a7bSLuigi Rizzo /* initialize the mbq for the sw rx ring */ 230317885a7bSLuigi Rizzo mbq_safe_init(&na->rx_rings[na->num_rx_rings].rx_queue); 230417885a7bSLuigi Rizzo ND("initialized sw rx queue %d", na->num_rx_rings); 230517885a7bSLuigi Rizzo } 230617885a7bSLuigi Rizzo return ret; 2307f9790aebSLuigi Rizzo } 2308f9790aebSLuigi Rizzo 2309f9790aebSLuigi Rizzo 2310f9790aebSLuigi Rizzo 231168b8534bSLuigi Rizzo /* 231268b8534bSLuigi Rizzo * Free the allocated memory linked to the given ``netmap_adapter`` 231368b8534bSLuigi Rizzo * object. 231468b8534bSLuigi Rizzo */ 231568b8534bSLuigi Rizzo void 231668b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp) 231768b8534bSLuigi Rizzo { 231868b8534bSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 231968b8534bSLuigi Rizzo 232068b8534bSLuigi Rizzo if (!na) 232168b8534bSLuigi Rizzo return; 232268b8534bSLuigi Rizzo 2323f9790aebSLuigi Rizzo NMG_LOCK(); 2324f9790aebSLuigi Rizzo netmap_disable_all_rings(ifp); 2325fb25194fSLuigi Rizzo if (!netmap_adapter_put(na)) { 2326fb25194fSLuigi Rizzo /* someone is still using the adapter, 2327fb25194fSLuigi Rizzo * tell them that the interface is gone 2328fb25194fSLuigi Rizzo */ 2329f9790aebSLuigi Rizzo na->ifp = NULL; 2330fb25194fSLuigi Rizzo /* give them a chance to notice */ 2331f9790aebSLuigi Rizzo netmap_enable_all_rings(ifp); 2332fb25194fSLuigi Rizzo } 2333f9790aebSLuigi Rizzo NMG_UNLOCK(); 2334ae10d1afSLuigi Rizzo } 2335f18be576SLuigi Rizzo 2336f18be576SLuigi Rizzo 233768b8534bSLuigi Rizzo /* 233802ad4083SLuigi Rizzo * Intercept packets from the network stack and pass them 233902ad4083SLuigi Rizzo * to netmap as incoming packets on the 'software' ring. 234017885a7bSLuigi Rizzo * 234117885a7bSLuigi Rizzo * We only store packets in a bounded mbq and then copy them 234217885a7bSLuigi Rizzo * in the relevant rxsync routine. 234317885a7bSLuigi Rizzo * 2344ce3ee1e7SLuigi Rizzo * We rely on the OS to make sure that the ifp and na do not go 2345ce3ee1e7SLuigi Rizzo * away (typically the caller checks for IFF_DRV_RUNNING or the like). 2346ce3ee1e7SLuigi Rizzo * In nm_register() or whenever there is a reinitialization, 2347f9790aebSLuigi Rizzo * we make sure to make the mode change visible here. 234868b8534bSLuigi Rizzo */ 234968b8534bSLuigi Rizzo int 2350ce3ee1e7SLuigi Rizzo netmap_transmit(struct ifnet *ifp, struct mbuf *m) 235168b8534bSLuigi Rizzo { 235268b8534bSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 2353ce3ee1e7SLuigi Rizzo struct netmap_kring *kring; 235417885a7bSLuigi Rizzo u_int len = MBUF_LEN(m); 235517885a7bSLuigi Rizzo u_int error = ENOBUFS; 235617885a7bSLuigi Rizzo struct mbq *q; 235717885a7bSLuigi Rizzo int space; 235868b8534bSLuigi Rizzo 2359ce3ee1e7SLuigi Rizzo // XXX [Linux] we do not need this lock 2360ce3ee1e7SLuigi Rizzo // if we follow the down/configure/up protocol -gl 2361ce3ee1e7SLuigi Rizzo // mtx_lock(&na->core_lock); 236217885a7bSLuigi Rizzo 2363ce3ee1e7SLuigi Rizzo if ( (ifp->if_capenable & IFCAP_NETMAP) == 0) { 236417885a7bSLuigi Rizzo D("%s not in netmap mode anymore", NM_IFPNAME(ifp)); 2365ce3ee1e7SLuigi Rizzo error = ENXIO; 2366ce3ee1e7SLuigi Rizzo goto done; 2367ce3ee1e7SLuigi Rizzo } 2368ce3ee1e7SLuigi Rizzo 2369ce3ee1e7SLuigi Rizzo kring = &na->rx_rings[na->num_rx_rings]; 237017885a7bSLuigi Rizzo q = &kring->rx_queue; 237117885a7bSLuigi Rizzo 2372ce3ee1e7SLuigi Rizzo // XXX reconsider long packets if we handle fragments 2373ce3ee1e7SLuigi Rizzo if (len > NETMAP_BDG_BUF_SIZE(na->nm_mem)) { /* too long for us */ 2374f9790aebSLuigi Rizzo D("%s from_host, drop packet size %d > %d", NM_IFPNAME(ifp), 2375ce3ee1e7SLuigi Rizzo len, NETMAP_BDG_BUF_SIZE(na->nm_mem)); 2376ce3ee1e7SLuigi Rizzo goto done; 2377849bec0eSLuigi Rizzo } 237817885a7bSLuigi Rizzo 237917885a7bSLuigi Rizzo /* protect against rxsync_from_host(), netmap_sw_to_nic() 238017885a7bSLuigi Rizzo * and maybe other instances of netmap_transmit (the latter 238117885a7bSLuigi Rizzo * not possible on Linux). 238217885a7bSLuigi Rizzo * Also avoid overflowing the queue. 2383ce3ee1e7SLuigi Rizzo */ 238417885a7bSLuigi Rizzo mtx_lock(&q->lock); 238517885a7bSLuigi Rizzo 238617885a7bSLuigi Rizzo space = kring->nr_hwtail - kring->nr_hwcur; 238717885a7bSLuigi Rizzo if (space < 0) 238817885a7bSLuigi Rizzo space += kring->nkr_num_slots; 238917885a7bSLuigi Rizzo if (space + mbq_len(q) >= kring->nkr_num_slots - 1) { // XXX 239017885a7bSLuigi Rizzo RD(10, "%s full hwcur %d hwtail %d qlen %d len %d m %p", 239117885a7bSLuigi Rizzo NM_IFPNAME(ifp), kring->nr_hwcur, kring->nr_hwtail, mbq_len(q), 239217885a7bSLuigi Rizzo len, m); 2393ce3ee1e7SLuigi Rizzo } else { 239417885a7bSLuigi Rizzo mbq_enqueue(q, m); 239517885a7bSLuigi Rizzo ND(10, "%s %d bufs in queue len %d m %p", 239617885a7bSLuigi Rizzo NM_IFPNAME(ifp), mbq_len(q), len, m); 239717885a7bSLuigi Rizzo /* notify outside the lock */ 239817885a7bSLuigi Rizzo m = NULL; 239968b8534bSLuigi Rizzo error = 0; 2400ce3ee1e7SLuigi Rizzo } 240117885a7bSLuigi Rizzo mtx_unlock(&q->lock); 2402ce3ee1e7SLuigi Rizzo 240368b8534bSLuigi Rizzo done: 240417885a7bSLuigi Rizzo if (m) 240568b8534bSLuigi Rizzo m_freem(m); 240617885a7bSLuigi Rizzo /* unconditionally wake up listeners */ 240717885a7bSLuigi Rizzo na->nm_notify(na, na->num_rx_rings, NR_RX, 0); 240868b8534bSLuigi Rizzo 240968b8534bSLuigi Rizzo return (error); 241068b8534bSLuigi Rizzo } 241168b8534bSLuigi Rizzo 241268b8534bSLuigi Rizzo 241368b8534bSLuigi Rizzo /* 241468b8534bSLuigi Rizzo * netmap_reset() is called by the driver routines when reinitializing 241568b8534bSLuigi Rizzo * a ring. The driver is in charge of locking to protect the kring. 2416f9790aebSLuigi Rizzo * If native netmap mode is not set just return NULL. 241768b8534bSLuigi Rizzo */ 241868b8534bSLuigi Rizzo struct netmap_slot * 2419ce3ee1e7SLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, u_int n, 242068b8534bSLuigi Rizzo u_int new_cur) 242168b8534bSLuigi Rizzo { 242268b8534bSLuigi Rizzo struct netmap_kring *kring; 2423506cc70cSLuigi Rizzo int new_hwofs, lim; 242468b8534bSLuigi Rizzo 2425ce3ee1e7SLuigi Rizzo if (na == NULL) { 2426ce3ee1e7SLuigi Rizzo D("NULL na, should not happen"); 242768b8534bSLuigi Rizzo return NULL; /* no netmap support here */ 2428ce3ee1e7SLuigi Rizzo } 2429ce3ee1e7SLuigi Rizzo if (!(na->ifp->if_capenable & IFCAP_NETMAP)) { 24305864b3a5SLuigi Rizzo ND("interface not in netmap mode"); 243168b8534bSLuigi Rizzo return NULL; /* nothing to reinitialize */ 2432ce3ee1e7SLuigi Rizzo } 243368b8534bSLuigi Rizzo 2434ce3ee1e7SLuigi Rizzo /* XXX note- in the new scheme, we are not guaranteed to be 2435ce3ee1e7SLuigi Rizzo * under lock (e.g. when called on a device reset). 2436ce3ee1e7SLuigi Rizzo * In this case, we should set a flag and do not trust too 2437ce3ee1e7SLuigi Rizzo * much the values. In practice: TODO 2438ce3ee1e7SLuigi Rizzo * - set a RESET flag somewhere in the kring 2439ce3ee1e7SLuigi Rizzo * - do the processing in a conservative way 2440ce3ee1e7SLuigi Rizzo * - let the *sync() fixup at the end. 2441ce3ee1e7SLuigi Rizzo */ 244264ae02c3SLuigi Rizzo if (tx == NR_TX) { 24438241616dSLuigi Rizzo if (n >= na->num_tx_rings) 24448241616dSLuigi Rizzo return NULL; 244564ae02c3SLuigi Rizzo kring = na->tx_rings + n; 244617885a7bSLuigi Rizzo // XXX check whether we should use hwcur or rcur 2447506cc70cSLuigi Rizzo new_hwofs = kring->nr_hwcur - new_cur; 244864ae02c3SLuigi Rizzo } else { 24498241616dSLuigi Rizzo if (n >= na->num_rx_rings) 24508241616dSLuigi Rizzo return NULL; 245164ae02c3SLuigi Rizzo kring = na->rx_rings + n; 245217885a7bSLuigi Rizzo new_hwofs = kring->nr_hwtail - new_cur; 245364ae02c3SLuigi Rizzo } 245464ae02c3SLuigi Rizzo lim = kring->nkr_num_slots - 1; 2455506cc70cSLuigi Rizzo if (new_hwofs > lim) 2456506cc70cSLuigi Rizzo new_hwofs -= lim + 1; 2457506cc70cSLuigi Rizzo 2458ce3ee1e7SLuigi Rizzo /* Always set the new offset value and realign the ring. */ 245917885a7bSLuigi Rizzo if (netmap_verbose) 246017885a7bSLuigi Rizzo D("%s %s%d hwofs %d -> %d, hwtail %d -> %d", 246117885a7bSLuigi Rizzo NM_IFPNAME(na->ifp), 246217885a7bSLuigi Rizzo tx == NR_TX ? "TX" : "RX", n, 2463ce3ee1e7SLuigi Rizzo kring->nkr_hwofs, new_hwofs, 246417885a7bSLuigi Rizzo kring->nr_hwtail, 246517885a7bSLuigi Rizzo tx == NR_TX ? lim : kring->nr_hwtail); 2466506cc70cSLuigi Rizzo kring->nkr_hwofs = new_hwofs; 246717885a7bSLuigi Rizzo if (tx == NR_TX) { 246817885a7bSLuigi Rizzo kring->nr_hwtail = kring->nr_hwcur + lim; 246917885a7bSLuigi Rizzo if (kring->nr_hwtail > lim) 247017885a7bSLuigi Rizzo kring->nr_hwtail -= lim + 1; 247117885a7bSLuigi Rizzo } 2472506cc70cSLuigi Rizzo 2473f196ce38SLuigi Rizzo #if 0 // def linux 2474f196ce38SLuigi Rizzo /* XXX check that the mappings are correct */ 2475f196ce38SLuigi Rizzo /* need ring_nr, adapter->pdev, direction */ 2476f196ce38SLuigi Rizzo buffer_info->dma = dma_map_single(&pdev->dev, addr, adapter->rx_buffer_len, DMA_FROM_DEVICE); 2477f196ce38SLuigi Rizzo if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) { 2478f196ce38SLuigi Rizzo D("error mapping rx netmap buffer %d", i); 2479f196ce38SLuigi Rizzo // XXX fix error handling 2480f196ce38SLuigi Rizzo } 2481f196ce38SLuigi Rizzo 2482f196ce38SLuigi Rizzo #endif /* linux */ 248368b8534bSLuigi Rizzo /* 2484ce3ee1e7SLuigi Rizzo * Wakeup on the individual and global selwait 2485506cc70cSLuigi Rizzo * We do the wakeup here, but the ring is not yet reconfigured. 2486506cc70cSLuigi Rizzo * However, we are under lock so there are no races. 248768b8534bSLuigi Rizzo */ 2488f0ea3689SLuigi Rizzo na->nm_notify(na, n, tx, 0); 248968b8534bSLuigi Rizzo return kring->ring->slot; 249068b8534bSLuigi Rizzo } 249168b8534bSLuigi Rizzo 249268b8534bSLuigi Rizzo 2493ce3ee1e7SLuigi Rizzo /* 2494f9790aebSLuigi Rizzo * Dispatch rx/tx interrupts to the netmap rings. 2495ce3ee1e7SLuigi Rizzo * 2496ce3ee1e7SLuigi Rizzo * "work_done" is non-null on the RX path, NULL for the TX path. 2497ce3ee1e7SLuigi Rizzo * We rely on the OS to make sure that there is only one active 2498ce3ee1e7SLuigi Rizzo * instance per queue, and that there is appropriate locking. 2499849bec0eSLuigi Rizzo * 2500f9790aebSLuigi Rizzo * The 'notify' routine depends on what the ring is attached to. 2501f9790aebSLuigi Rizzo * - for a netmap file descriptor, do a selwakeup on the individual 2502f9790aebSLuigi Rizzo * waitqueue, plus one on the global one if needed 2503f9790aebSLuigi Rizzo * - for a switch, call the proper forwarding routine 2504f9790aebSLuigi Rizzo * - XXX more ? 2505f9790aebSLuigi Rizzo */ 2506f9790aebSLuigi Rizzo void 2507f9790aebSLuigi Rizzo netmap_common_irq(struct ifnet *ifp, u_int q, u_int *work_done) 2508f9790aebSLuigi Rizzo { 2509f9790aebSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 2510f9790aebSLuigi Rizzo struct netmap_kring *kring; 2511f9790aebSLuigi Rizzo 2512f9790aebSLuigi Rizzo q &= NETMAP_RING_MASK; 2513f9790aebSLuigi Rizzo 2514f9790aebSLuigi Rizzo if (netmap_verbose) { 2515f9790aebSLuigi Rizzo RD(5, "received %s queue %d", work_done ? "RX" : "TX" , q); 2516f9790aebSLuigi Rizzo } 2517f9790aebSLuigi Rizzo 2518f9790aebSLuigi Rizzo if (work_done) { /* RX path */ 2519f9790aebSLuigi Rizzo if (q >= na->num_rx_rings) 2520f9790aebSLuigi Rizzo return; // not a physical queue 2521f9790aebSLuigi Rizzo kring = na->rx_rings + q; 2522f9790aebSLuigi Rizzo kring->nr_kflags |= NKR_PENDINTR; // XXX atomic ? 2523f0ea3689SLuigi Rizzo na->nm_notify(na, q, NR_RX, 0); 2524f9790aebSLuigi Rizzo *work_done = 1; /* do not fire napi again */ 2525f9790aebSLuigi Rizzo } else { /* TX path */ 2526f9790aebSLuigi Rizzo if (q >= na->num_tx_rings) 2527f9790aebSLuigi Rizzo return; // not a physical queue 2528f9790aebSLuigi Rizzo kring = na->tx_rings + q; 2529f0ea3689SLuigi Rizzo na->nm_notify(na, q, NR_TX, 0); 2530f9790aebSLuigi Rizzo } 2531f9790aebSLuigi Rizzo } 2532f9790aebSLuigi Rizzo 253317885a7bSLuigi Rizzo 2534f9790aebSLuigi Rizzo /* 2535f9790aebSLuigi Rizzo * Default functions to handle rx/tx interrupts from a physical device. 2536f9790aebSLuigi Rizzo * "work_done" is non-null on the RX path, NULL for the TX path. 2537f9790aebSLuigi Rizzo * 2538ce3ee1e7SLuigi Rizzo * If the card is not in netmap mode, simply return 0, 2539ce3ee1e7SLuigi Rizzo * so that the caller proceeds with regular processing. 2540f9790aebSLuigi Rizzo * Otherwise call netmap_common_irq() and return 1. 2541ce3ee1e7SLuigi Rizzo * 2542ce3ee1e7SLuigi Rizzo * If the card is connected to a netmap file descriptor, 2543ce3ee1e7SLuigi Rizzo * do a selwakeup on the individual queue, plus one on the global one 2544ce3ee1e7SLuigi Rizzo * if needed (multiqueue card _and_ there are multiqueue listeners), 2545ce3ee1e7SLuigi Rizzo * and return 1. 2546ce3ee1e7SLuigi Rizzo * 2547ce3ee1e7SLuigi Rizzo * Finally, if called on rx from an interface connected to a switch, 2548ce3ee1e7SLuigi Rizzo * calls the proper forwarding routine, and return 1. 25491a26580eSLuigi Rizzo */ 2550babc7c12SLuigi Rizzo int 2551ce3ee1e7SLuigi Rizzo netmap_rx_irq(struct ifnet *ifp, u_int q, u_int *work_done) 25521a26580eSLuigi Rizzo { 2553f9790aebSLuigi Rizzo // XXX could we check NAF_NATIVE_ON ? 25541a26580eSLuigi Rizzo if (!(ifp->if_capenable & IFCAP_NETMAP)) 25551a26580eSLuigi Rizzo return 0; 2556849bec0eSLuigi Rizzo 2557f9790aebSLuigi Rizzo if (NA(ifp)->na_flags & NAF_SKIP_INTR) { 25588241616dSLuigi Rizzo ND("use regular interrupt"); 25598241616dSLuigi Rizzo return 0; 25608241616dSLuigi Rizzo } 25618241616dSLuigi Rizzo 2562f9790aebSLuigi Rizzo netmap_common_irq(ifp, q, work_done); 25631a26580eSLuigi Rizzo return 1; 25641a26580eSLuigi Rizzo } 25651a26580eSLuigi Rizzo 256664ae02c3SLuigi Rizzo 256701c7d25fSLuigi Rizzo /* 2568f9790aebSLuigi Rizzo * Module loader and unloader 2569f196ce38SLuigi Rizzo * 2570f9790aebSLuigi Rizzo * netmap_init() creates the /dev/netmap device and initializes 2571f9790aebSLuigi Rizzo * all global variables. Returns 0 on success, errno on failure 2572f9790aebSLuigi Rizzo * (but there is no chance) 2573f9790aebSLuigi Rizzo * 2574f9790aebSLuigi Rizzo * netmap_fini() destroys everything. 2575f196ce38SLuigi Rizzo */ 2576babc7c12SLuigi Rizzo 2577babc7c12SLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */ 2578f9790aebSLuigi Rizzo extern struct cdevsw netmap_cdevsw; 2579babc7c12SLuigi Rizzo 258017885a7bSLuigi Rizzo 2581f9790aebSLuigi Rizzo void 258268b8534bSLuigi Rizzo netmap_fini(void) 258368b8534bSLuigi Rizzo { 2584f9790aebSLuigi Rizzo // XXX destroy_bridges() ? 2585f9790aebSLuigi Rizzo if (netmap_dev) 258668b8534bSLuigi Rizzo destroy_dev(netmap_dev); 2587ce3ee1e7SLuigi Rizzo netmap_mem_fini(); 2588ce3ee1e7SLuigi Rizzo NMG_LOCK_DESTROY(); 258968b8534bSLuigi Rizzo printf("netmap: unloaded module.\n"); 259068b8534bSLuigi Rizzo } 259168b8534bSLuigi Rizzo 259217885a7bSLuigi Rizzo 2593f9790aebSLuigi Rizzo int 2594f9790aebSLuigi Rizzo netmap_init(void) 259568b8534bSLuigi Rizzo { 2596f9790aebSLuigi Rizzo int error; 259768b8534bSLuigi Rizzo 2598f9790aebSLuigi Rizzo NMG_LOCK_INIT(); 259968b8534bSLuigi Rizzo 2600f9790aebSLuigi Rizzo error = netmap_mem_init(); 2601f9790aebSLuigi Rizzo if (error != 0) 2602f9790aebSLuigi Rizzo goto fail; 2603f9790aebSLuigi Rizzo /* XXX could use make_dev_credv() to get error number */ 2604f9790aebSLuigi Rizzo netmap_dev = make_dev(&netmap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660, 2605f9790aebSLuigi Rizzo "netmap"); 2606f9790aebSLuigi Rizzo if (!netmap_dev) 2607f9790aebSLuigi Rizzo goto fail; 2608f9790aebSLuigi Rizzo 2609f9790aebSLuigi Rizzo netmap_init_bridges(); 2610f9790aebSLuigi Rizzo printf("netmap: loaded module\n"); 2611f9790aebSLuigi Rizzo return (0); 2612f9790aebSLuigi Rizzo fail: 261368b8534bSLuigi Rizzo netmap_fini(); 2614f9790aebSLuigi Rizzo return (EINVAL); /* may be incorrect */ 261568b8534bSLuigi Rizzo } 2616